re PR target/57865 (Broken _save64gpr and _rest64gpr usage)
[official-gcc.git] / gcc / config / rs6000 / rs6000.c
blobc1acbd825ea5094360cfbc1fbb708b3149e4b075
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 /* Pointer to function (in rs6000-c.c) that can define or undefine target
288 macros that have changed. Languages that don't support the preprocessor
289 don't link in rs6000-c.c, so we can't call it directly. */
290 void (*rs6000_target_modify_macros_ptr) (bool, HOST_WIDE_INT, HOST_WIDE_INT);
292 /* Simplfy register classes into simpler classifications. We assume
293 GPR_REG_TYPE - FPR_REG_TYPE are ordered so that we can use a simple range
294 check for standard register classes (gpr/floating/altivec/vsx) and
295 floating/vector classes (float/altivec/vsx). */
297 enum rs6000_reg_type {
298 NO_REG_TYPE,
299 PSEUDO_REG_TYPE,
300 GPR_REG_TYPE,
301 VSX_REG_TYPE,
302 ALTIVEC_REG_TYPE,
303 FPR_REG_TYPE,
304 SPR_REG_TYPE,
305 CR_REG_TYPE,
306 SPE_ACC_TYPE,
307 SPEFSCR_REG_TYPE
310 /* Map register class to register type. */
311 static enum rs6000_reg_type reg_class_to_reg_type[N_REG_CLASSES];
313 /* First/last register type for the 'normal' register types (i.e. general
314 purpose, floating point, altivec, and VSX registers). */
315 #define IS_STD_REG_TYPE(RTYPE) IN_RANGE(RTYPE, GPR_REG_TYPE, FPR_REG_TYPE)
317 #define IS_FP_VECT_REG_TYPE(RTYPE) IN_RANGE(RTYPE, VSX_REG_TYPE, FPR_REG_TYPE)
319 /* Direct moves to/from vsx/gpr registers that need an additional register to
320 do the move. */
321 static enum insn_code reload_fpr_gpr[NUM_MACHINE_MODES];
322 static enum insn_code reload_gpr_vsx[NUM_MACHINE_MODES];
323 static enum insn_code reload_vsx_gpr[NUM_MACHINE_MODES];
326 /* Target cpu costs. */
328 struct processor_costs {
329 const int mulsi; /* cost of SImode multiplication. */
330 const int mulsi_const; /* cost of SImode multiplication by constant. */
331 const int mulsi_const9; /* cost of SImode mult by short constant. */
332 const int muldi; /* cost of DImode multiplication. */
333 const int divsi; /* cost of SImode division. */
334 const int divdi; /* cost of DImode division. */
335 const int fp; /* cost of simple SFmode and DFmode insns. */
336 const int dmul; /* cost of DFmode multiplication (and fmadd). */
337 const int sdiv; /* cost of SFmode division (fdivs). */
338 const int ddiv; /* cost of DFmode division (fdiv). */
339 const int cache_line_size; /* cache line size in bytes. */
340 const int l1_cache_size; /* size of l1 cache, in kilobytes. */
341 const int l2_cache_size; /* size of l2 cache, in kilobytes. */
342 const int simultaneous_prefetches; /* number of parallel prefetch
343 operations. */
346 const struct processor_costs *rs6000_cost;
348 /* Processor costs (relative to an add) */
350 /* Instruction size costs on 32bit processors. */
351 static const
352 struct processor_costs size32_cost = {
353 COSTS_N_INSNS (1), /* mulsi */
354 COSTS_N_INSNS (1), /* mulsi_const */
355 COSTS_N_INSNS (1), /* mulsi_const9 */
356 COSTS_N_INSNS (1), /* muldi */
357 COSTS_N_INSNS (1), /* divsi */
358 COSTS_N_INSNS (1), /* divdi */
359 COSTS_N_INSNS (1), /* fp */
360 COSTS_N_INSNS (1), /* dmul */
361 COSTS_N_INSNS (1), /* sdiv */
362 COSTS_N_INSNS (1), /* ddiv */
369 /* Instruction size costs on 64bit processors. */
370 static const
371 struct processor_costs size64_cost = {
372 COSTS_N_INSNS (1), /* mulsi */
373 COSTS_N_INSNS (1), /* mulsi_const */
374 COSTS_N_INSNS (1), /* mulsi_const9 */
375 COSTS_N_INSNS (1), /* muldi */
376 COSTS_N_INSNS (1), /* divsi */
377 COSTS_N_INSNS (1), /* divdi */
378 COSTS_N_INSNS (1), /* fp */
379 COSTS_N_INSNS (1), /* dmul */
380 COSTS_N_INSNS (1), /* sdiv */
381 COSTS_N_INSNS (1), /* ddiv */
382 128,
388 /* Instruction costs on RS64A processors. */
389 static const
390 struct processor_costs rs64a_cost = {
391 COSTS_N_INSNS (20), /* mulsi */
392 COSTS_N_INSNS (12), /* mulsi_const */
393 COSTS_N_INSNS (8), /* mulsi_const9 */
394 COSTS_N_INSNS (34), /* muldi */
395 COSTS_N_INSNS (65), /* divsi */
396 COSTS_N_INSNS (67), /* divdi */
397 COSTS_N_INSNS (4), /* fp */
398 COSTS_N_INSNS (4), /* dmul */
399 COSTS_N_INSNS (31), /* sdiv */
400 COSTS_N_INSNS (31), /* ddiv */
401 128, /* cache line size */
402 128, /* l1 cache */
403 2048, /* l2 cache */
404 1, /* streams */
407 /* Instruction costs on MPCCORE processors. */
408 static const
409 struct processor_costs mpccore_cost = {
410 COSTS_N_INSNS (2), /* mulsi */
411 COSTS_N_INSNS (2), /* mulsi_const */
412 COSTS_N_INSNS (2), /* mulsi_const9 */
413 COSTS_N_INSNS (2), /* muldi */
414 COSTS_N_INSNS (6), /* divsi */
415 COSTS_N_INSNS (6), /* divdi */
416 COSTS_N_INSNS (4), /* fp */
417 COSTS_N_INSNS (5), /* dmul */
418 COSTS_N_INSNS (10), /* sdiv */
419 COSTS_N_INSNS (17), /* ddiv */
420 32, /* cache line size */
421 4, /* l1 cache */
422 16, /* l2 cache */
423 1, /* streams */
426 /* Instruction costs on PPC403 processors. */
427 static const
428 struct processor_costs ppc403_cost = {
429 COSTS_N_INSNS (4), /* mulsi */
430 COSTS_N_INSNS (4), /* mulsi_const */
431 COSTS_N_INSNS (4), /* mulsi_const9 */
432 COSTS_N_INSNS (4), /* muldi */
433 COSTS_N_INSNS (33), /* divsi */
434 COSTS_N_INSNS (33), /* divdi */
435 COSTS_N_INSNS (11), /* fp */
436 COSTS_N_INSNS (11), /* dmul */
437 COSTS_N_INSNS (11), /* sdiv */
438 COSTS_N_INSNS (11), /* ddiv */
439 32, /* cache line size */
440 4, /* l1 cache */
441 16, /* l2 cache */
442 1, /* streams */
445 /* Instruction costs on PPC405 processors. */
446 static const
447 struct processor_costs ppc405_cost = {
448 COSTS_N_INSNS (5), /* mulsi */
449 COSTS_N_INSNS (4), /* mulsi_const */
450 COSTS_N_INSNS (3), /* mulsi_const9 */
451 COSTS_N_INSNS (5), /* muldi */
452 COSTS_N_INSNS (35), /* divsi */
453 COSTS_N_INSNS (35), /* divdi */
454 COSTS_N_INSNS (11), /* fp */
455 COSTS_N_INSNS (11), /* dmul */
456 COSTS_N_INSNS (11), /* sdiv */
457 COSTS_N_INSNS (11), /* ddiv */
458 32, /* cache line size */
459 16, /* l1 cache */
460 128, /* l2 cache */
461 1, /* streams */
464 /* Instruction costs on PPC440 processors. */
465 static const
466 struct processor_costs ppc440_cost = {
467 COSTS_N_INSNS (3), /* mulsi */
468 COSTS_N_INSNS (2), /* mulsi_const */
469 COSTS_N_INSNS (2), /* mulsi_const9 */
470 COSTS_N_INSNS (3), /* muldi */
471 COSTS_N_INSNS (34), /* divsi */
472 COSTS_N_INSNS (34), /* divdi */
473 COSTS_N_INSNS (5), /* fp */
474 COSTS_N_INSNS (5), /* dmul */
475 COSTS_N_INSNS (19), /* sdiv */
476 COSTS_N_INSNS (33), /* ddiv */
477 32, /* cache line size */
478 32, /* l1 cache */
479 256, /* l2 cache */
480 1, /* streams */
483 /* Instruction costs on PPC476 processors. */
484 static const
485 struct processor_costs ppc476_cost = {
486 COSTS_N_INSNS (4), /* mulsi */
487 COSTS_N_INSNS (4), /* mulsi_const */
488 COSTS_N_INSNS (4), /* mulsi_const9 */
489 COSTS_N_INSNS (4), /* muldi */
490 COSTS_N_INSNS (11), /* divsi */
491 COSTS_N_INSNS (11), /* divdi */
492 COSTS_N_INSNS (6), /* fp */
493 COSTS_N_INSNS (6), /* dmul */
494 COSTS_N_INSNS (19), /* sdiv */
495 COSTS_N_INSNS (33), /* ddiv */
496 32, /* l1 cache line size */
497 32, /* l1 cache */
498 512, /* l2 cache */
499 1, /* streams */
502 /* Instruction costs on PPC601 processors. */
503 static const
504 struct processor_costs ppc601_cost = {
505 COSTS_N_INSNS (5), /* mulsi */
506 COSTS_N_INSNS (5), /* mulsi_const */
507 COSTS_N_INSNS (5), /* mulsi_const9 */
508 COSTS_N_INSNS (5), /* muldi */
509 COSTS_N_INSNS (36), /* divsi */
510 COSTS_N_INSNS (36), /* divdi */
511 COSTS_N_INSNS (4), /* fp */
512 COSTS_N_INSNS (5), /* dmul */
513 COSTS_N_INSNS (17), /* sdiv */
514 COSTS_N_INSNS (31), /* ddiv */
515 32, /* cache line size */
516 32, /* l1 cache */
517 256, /* l2 cache */
518 1, /* streams */
521 /* Instruction costs on PPC603 processors. */
522 static const
523 struct processor_costs ppc603_cost = {
524 COSTS_N_INSNS (5), /* mulsi */
525 COSTS_N_INSNS (3), /* mulsi_const */
526 COSTS_N_INSNS (2), /* mulsi_const9 */
527 COSTS_N_INSNS (5), /* muldi */
528 COSTS_N_INSNS (37), /* divsi */
529 COSTS_N_INSNS (37), /* divdi */
530 COSTS_N_INSNS (3), /* fp */
531 COSTS_N_INSNS (4), /* dmul */
532 COSTS_N_INSNS (18), /* sdiv */
533 COSTS_N_INSNS (33), /* ddiv */
534 32, /* cache line size */
535 8, /* l1 cache */
536 64, /* l2 cache */
537 1, /* streams */
540 /* Instruction costs on PPC604 processors. */
541 static const
542 struct processor_costs ppc604_cost = {
543 COSTS_N_INSNS (4), /* mulsi */
544 COSTS_N_INSNS (4), /* mulsi_const */
545 COSTS_N_INSNS (4), /* mulsi_const9 */
546 COSTS_N_INSNS (4), /* muldi */
547 COSTS_N_INSNS (20), /* divsi */
548 COSTS_N_INSNS (20), /* divdi */
549 COSTS_N_INSNS (3), /* fp */
550 COSTS_N_INSNS (3), /* dmul */
551 COSTS_N_INSNS (18), /* sdiv */
552 COSTS_N_INSNS (32), /* ddiv */
553 32, /* cache line size */
554 16, /* l1 cache */
555 512, /* l2 cache */
556 1, /* streams */
559 /* Instruction costs on PPC604e processors. */
560 static const
561 struct processor_costs ppc604e_cost = {
562 COSTS_N_INSNS (2), /* mulsi */
563 COSTS_N_INSNS (2), /* mulsi_const */
564 COSTS_N_INSNS (2), /* mulsi_const9 */
565 COSTS_N_INSNS (2), /* muldi */
566 COSTS_N_INSNS (20), /* divsi */
567 COSTS_N_INSNS (20), /* divdi */
568 COSTS_N_INSNS (3), /* fp */
569 COSTS_N_INSNS (3), /* dmul */
570 COSTS_N_INSNS (18), /* sdiv */
571 COSTS_N_INSNS (32), /* ddiv */
572 32, /* cache line size */
573 32, /* l1 cache */
574 1024, /* l2 cache */
575 1, /* streams */
578 /* Instruction costs on PPC620 processors. */
579 static const
580 struct processor_costs ppc620_cost = {
581 COSTS_N_INSNS (5), /* mulsi */
582 COSTS_N_INSNS (4), /* mulsi_const */
583 COSTS_N_INSNS (3), /* mulsi_const9 */
584 COSTS_N_INSNS (7), /* muldi */
585 COSTS_N_INSNS (21), /* divsi */
586 COSTS_N_INSNS (37), /* divdi */
587 COSTS_N_INSNS (3), /* fp */
588 COSTS_N_INSNS (3), /* dmul */
589 COSTS_N_INSNS (18), /* sdiv */
590 COSTS_N_INSNS (32), /* ddiv */
591 128, /* cache line size */
592 32, /* l1 cache */
593 1024, /* l2 cache */
594 1, /* streams */
597 /* Instruction costs on PPC630 processors. */
598 static const
599 struct processor_costs ppc630_cost = {
600 COSTS_N_INSNS (5), /* mulsi */
601 COSTS_N_INSNS (4), /* mulsi_const */
602 COSTS_N_INSNS (3), /* mulsi_const9 */
603 COSTS_N_INSNS (7), /* muldi */
604 COSTS_N_INSNS (21), /* divsi */
605 COSTS_N_INSNS (37), /* divdi */
606 COSTS_N_INSNS (3), /* fp */
607 COSTS_N_INSNS (3), /* dmul */
608 COSTS_N_INSNS (17), /* sdiv */
609 COSTS_N_INSNS (21), /* ddiv */
610 128, /* cache line size */
611 64, /* l1 cache */
612 1024, /* l2 cache */
613 1, /* streams */
616 /* Instruction costs on Cell processor. */
617 /* COSTS_N_INSNS (1) ~ one add. */
618 static const
619 struct processor_costs ppccell_cost = {
620 COSTS_N_INSNS (9/2)+2, /* mulsi */
621 COSTS_N_INSNS (6/2), /* mulsi_const */
622 COSTS_N_INSNS (6/2), /* mulsi_const9 */
623 COSTS_N_INSNS (15/2)+2, /* muldi */
624 COSTS_N_INSNS (38/2), /* divsi */
625 COSTS_N_INSNS (70/2), /* divdi */
626 COSTS_N_INSNS (10/2), /* fp */
627 COSTS_N_INSNS (10/2), /* dmul */
628 COSTS_N_INSNS (74/2), /* sdiv */
629 COSTS_N_INSNS (74/2), /* ddiv */
630 128, /* cache line size */
631 32, /* l1 cache */
632 512, /* l2 cache */
633 6, /* streams */
636 /* Instruction costs on PPC750 and PPC7400 processors. */
637 static const
638 struct processor_costs ppc750_cost = {
639 COSTS_N_INSNS (5), /* mulsi */
640 COSTS_N_INSNS (3), /* mulsi_const */
641 COSTS_N_INSNS (2), /* mulsi_const9 */
642 COSTS_N_INSNS (5), /* muldi */
643 COSTS_N_INSNS (17), /* divsi */
644 COSTS_N_INSNS (17), /* divdi */
645 COSTS_N_INSNS (3), /* fp */
646 COSTS_N_INSNS (3), /* dmul */
647 COSTS_N_INSNS (17), /* sdiv */
648 COSTS_N_INSNS (31), /* ddiv */
649 32, /* cache line size */
650 32, /* l1 cache */
651 512, /* l2 cache */
652 1, /* streams */
655 /* Instruction costs on PPC7450 processors. */
656 static const
657 struct processor_costs ppc7450_cost = {
658 COSTS_N_INSNS (4), /* mulsi */
659 COSTS_N_INSNS (3), /* mulsi_const */
660 COSTS_N_INSNS (3), /* mulsi_const9 */
661 COSTS_N_INSNS (4), /* muldi */
662 COSTS_N_INSNS (23), /* divsi */
663 COSTS_N_INSNS (23), /* divdi */
664 COSTS_N_INSNS (5), /* fp */
665 COSTS_N_INSNS (5), /* dmul */
666 COSTS_N_INSNS (21), /* sdiv */
667 COSTS_N_INSNS (35), /* ddiv */
668 32, /* cache line size */
669 32, /* l1 cache */
670 1024, /* l2 cache */
671 1, /* streams */
674 /* Instruction costs on PPC8540 processors. */
675 static const
676 struct processor_costs ppc8540_cost = {
677 COSTS_N_INSNS (4), /* mulsi */
678 COSTS_N_INSNS (4), /* mulsi_const */
679 COSTS_N_INSNS (4), /* mulsi_const9 */
680 COSTS_N_INSNS (4), /* muldi */
681 COSTS_N_INSNS (19), /* divsi */
682 COSTS_N_INSNS (19), /* divdi */
683 COSTS_N_INSNS (4), /* fp */
684 COSTS_N_INSNS (4), /* dmul */
685 COSTS_N_INSNS (29), /* sdiv */
686 COSTS_N_INSNS (29), /* ddiv */
687 32, /* cache line size */
688 32, /* l1 cache */
689 256, /* l2 cache */
690 1, /* prefetch streams /*/
693 /* Instruction costs on E300C2 and E300C3 cores. */
694 static const
695 struct processor_costs ppce300c2c3_cost = {
696 COSTS_N_INSNS (4), /* mulsi */
697 COSTS_N_INSNS (4), /* mulsi_const */
698 COSTS_N_INSNS (4), /* mulsi_const9 */
699 COSTS_N_INSNS (4), /* muldi */
700 COSTS_N_INSNS (19), /* divsi */
701 COSTS_N_INSNS (19), /* divdi */
702 COSTS_N_INSNS (3), /* fp */
703 COSTS_N_INSNS (4), /* dmul */
704 COSTS_N_INSNS (18), /* sdiv */
705 COSTS_N_INSNS (33), /* ddiv */
707 16, /* l1 cache */
708 16, /* l2 cache */
709 1, /* prefetch streams /*/
712 /* Instruction costs on PPCE500MC processors. */
713 static const
714 struct processor_costs ppce500mc_cost = {
715 COSTS_N_INSNS (4), /* mulsi */
716 COSTS_N_INSNS (4), /* mulsi_const */
717 COSTS_N_INSNS (4), /* mulsi_const9 */
718 COSTS_N_INSNS (4), /* muldi */
719 COSTS_N_INSNS (14), /* divsi */
720 COSTS_N_INSNS (14), /* divdi */
721 COSTS_N_INSNS (8), /* fp */
722 COSTS_N_INSNS (10), /* dmul */
723 COSTS_N_INSNS (36), /* sdiv */
724 COSTS_N_INSNS (66), /* ddiv */
725 64, /* cache line size */
726 32, /* l1 cache */
727 128, /* l2 cache */
728 1, /* prefetch streams /*/
731 /* Instruction costs on PPCE500MC64 processors. */
732 static const
733 struct processor_costs ppce500mc64_cost = {
734 COSTS_N_INSNS (4), /* mulsi */
735 COSTS_N_INSNS (4), /* mulsi_const */
736 COSTS_N_INSNS (4), /* mulsi_const9 */
737 COSTS_N_INSNS (4), /* muldi */
738 COSTS_N_INSNS (14), /* divsi */
739 COSTS_N_INSNS (14), /* divdi */
740 COSTS_N_INSNS (4), /* fp */
741 COSTS_N_INSNS (10), /* dmul */
742 COSTS_N_INSNS (36), /* sdiv */
743 COSTS_N_INSNS (66), /* ddiv */
744 64, /* cache line size */
745 32, /* l1 cache */
746 128, /* l2 cache */
747 1, /* prefetch streams /*/
750 /* Instruction costs on PPCE5500 processors. */
751 static const
752 struct processor_costs ppce5500_cost = {
753 COSTS_N_INSNS (5), /* mulsi */
754 COSTS_N_INSNS (5), /* mulsi_const */
755 COSTS_N_INSNS (4), /* mulsi_const9 */
756 COSTS_N_INSNS (5), /* muldi */
757 COSTS_N_INSNS (14), /* divsi */
758 COSTS_N_INSNS (14), /* divdi */
759 COSTS_N_INSNS (7), /* fp */
760 COSTS_N_INSNS (10), /* dmul */
761 COSTS_N_INSNS (36), /* sdiv */
762 COSTS_N_INSNS (66), /* ddiv */
763 64, /* cache line size */
764 32, /* l1 cache */
765 128, /* l2 cache */
766 1, /* prefetch streams /*/
769 /* Instruction costs on PPCE6500 processors. */
770 static const
771 struct processor_costs ppce6500_cost = {
772 COSTS_N_INSNS (5), /* mulsi */
773 COSTS_N_INSNS (5), /* mulsi_const */
774 COSTS_N_INSNS (4), /* mulsi_const9 */
775 COSTS_N_INSNS (5), /* muldi */
776 COSTS_N_INSNS (14), /* divsi */
777 COSTS_N_INSNS (14), /* divdi */
778 COSTS_N_INSNS (7), /* fp */
779 COSTS_N_INSNS (10), /* dmul */
780 COSTS_N_INSNS (36), /* sdiv */
781 COSTS_N_INSNS (66), /* ddiv */
782 64, /* cache line size */
783 32, /* l1 cache */
784 128, /* l2 cache */
785 1, /* prefetch streams /*/
788 /* Instruction costs on AppliedMicro Titan processors. */
789 static const
790 struct processor_costs titan_cost = {
791 COSTS_N_INSNS (5), /* mulsi */
792 COSTS_N_INSNS (5), /* mulsi_const */
793 COSTS_N_INSNS (5), /* mulsi_const9 */
794 COSTS_N_INSNS (5), /* muldi */
795 COSTS_N_INSNS (18), /* divsi */
796 COSTS_N_INSNS (18), /* divdi */
797 COSTS_N_INSNS (10), /* fp */
798 COSTS_N_INSNS (10), /* dmul */
799 COSTS_N_INSNS (46), /* sdiv */
800 COSTS_N_INSNS (72), /* ddiv */
801 32, /* cache line size */
802 32, /* l1 cache */
803 512, /* l2 cache */
804 1, /* prefetch streams /*/
807 /* Instruction costs on POWER4 and POWER5 processors. */
808 static const
809 struct processor_costs power4_cost = {
810 COSTS_N_INSNS (3), /* mulsi */
811 COSTS_N_INSNS (2), /* mulsi_const */
812 COSTS_N_INSNS (2), /* mulsi_const9 */
813 COSTS_N_INSNS (4), /* muldi */
814 COSTS_N_INSNS (18), /* divsi */
815 COSTS_N_INSNS (34), /* divdi */
816 COSTS_N_INSNS (3), /* fp */
817 COSTS_N_INSNS (3), /* dmul */
818 COSTS_N_INSNS (17), /* sdiv */
819 COSTS_N_INSNS (17), /* ddiv */
820 128, /* cache line size */
821 32, /* l1 cache */
822 1024, /* l2 cache */
823 8, /* prefetch streams /*/
826 /* Instruction costs on POWER6 processors. */
827 static const
828 struct processor_costs power6_cost = {
829 COSTS_N_INSNS (8), /* mulsi */
830 COSTS_N_INSNS (8), /* mulsi_const */
831 COSTS_N_INSNS (8), /* mulsi_const9 */
832 COSTS_N_INSNS (8), /* muldi */
833 COSTS_N_INSNS (22), /* divsi */
834 COSTS_N_INSNS (28), /* divdi */
835 COSTS_N_INSNS (3), /* fp */
836 COSTS_N_INSNS (3), /* dmul */
837 COSTS_N_INSNS (13), /* sdiv */
838 COSTS_N_INSNS (16), /* ddiv */
839 128, /* cache line size */
840 64, /* l1 cache */
841 2048, /* l2 cache */
842 16, /* prefetch streams */
845 /* Instruction costs on POWER7 processors. */
846 static const
847 struct processor_costs power7_cost = {
848 COSTS_N_INSNS (2), /* mulsi */
849 COSTS_N_INSNS (2), /* mulsi_const */
850 COSTS_N_INSNS (2), /* mulsi_const9 */
851 COSTS_N_INSNS (2), /* muldi */
852 COSTS_N_INSNS (18), /* divsi */
853 COSTS_N_INSNS (34), /* divdi */
854 COSTS_N_INSNS (3), /* fp */
855 COSTS_N_INSNS (3), /* dmul */
856 COSTS_N_INSNS (13), /* sdiv */
857 COSTS_N_INSNS (16), /* ddiv */
858 128, /* cache line size */
859 32, /* l1 cache */
860 256, /* l2 cache */
861 12, /* prefetch streams */
864 /* Instruction costs on POWER8 processors. */
865 static const
866 struct processor_costs power8_cost = {
867 COSTS_N_INSNS (3), /* mulsi */
868 COSTS_N_INSNS (3), /* mulsi_const */
869 COSTS_N_INSNS (3), /* mulsi_const9 */
870 COSTS_N_INSNS (3), /* muldi */
871 COSTS_N_INSNS (19), /* divsi */
872 COSTS_N_INSNS (35), /* divdi */
873 COSTS_N_INSNS (3), /* fp */
874 COSTS_N_INSNS (3), /* dmul */
875 COSTS_N_INSNS (14), /* sdiv */
876 COSTS_N_INSNS (17), /* ddiv */
877 128, /* cache line size */
878 32, /* l1 cache */
879 256, /* l2 cache */
880 12, /* prefetch streams */
883 /* Instruction costs on POWER A2 processors. */
884 static const
885 struct processor_costs ppca2_cost = {
886 COSTS_N_INSNS (16), /* mulsi */
887 COSTS_N_INSNS (16), /* mulsi_const */
888 COSTS_N_INSNS (16), /* mulsi_const9 */
889 COSTS_N_INSNS (16), /* muldi */
890 COSTS_N_INSNS (22), /* divsi */
891 COSTS_N_INSNS (28), /* divdi */
892 COSTS_N_INSNS (3), /* fp */
893 COSTS_N_INSNS (3), /* dmul */
894 COSTS_N_INSNS (59), /* sdiv */
895 COSTS_N_INSNS (72), /* ddiv */
897 16, /* l1 cache */
898 2048, /* l2 cache */
899 16, /* prefetch streams */
903 /* Table that classifies rs6000 builtin functions (pure, const, etc.). */
904 #undef RS6000_BUILTIN_1
905 #undef RS6000_BUILTIN_2
906 #undef RS6000_BUILTIN_3
907 #undef RS6000_BUILTIN_A
908 #undef RS6000_BUILTIN_D
909 #undef RS6000_BUILTIN_E
910 #undef RS6000_BUILTIN_H
911 #undef RS6000_BUILTIN_P
912 #undef RS6000_BUILTIN_Q
913 #undef RS6000_BUILTIN_S
914 #undef RS6000_BUILTIN_X
916 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE) \
917 { NAME, ICODE, MASK, ATTR },
919 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE) \
920 { NAME, ICODE, MASK, ATTR },
922 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE) \
923 { NAME, ICODE, MASK, ATTR },
925 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE) \
926 { NAME, ICODE, MASK, ATTR },
928 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE) \
929 { NAME, ICODE, MASK, ATTR },
931 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE) \
932 { NAME, ICODE, MASK, ATTR },
934 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE) \
935 { NAME, ICODE, MASK, ATTR },
937 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE) \
938 { NAME, ICODE, MASK, ATTR },
940 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE) \
941 { NAME, ICODE, MASK, ATTR },
943 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE) \
944 { NAME, ICODE, MASK, ATTR },
946 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE) \
947 { NAME, ICODE, MASK, ATTR },
949 struct rs6000_builtin_info_type {
950 const char *name;
951 const enum insn_code icode;
952 const HOST_WIDE_INT mask;
953 const unsigned attr;
956 static const struct rs6000_builtin_info_type rs6000_builtin_info[] =
958 #include "rs6000-builtin.def"
961 #undef RS6000_BUILTIN_1
962 #undef RS6000_BUILTIN_2
963 #undef RS6000_BUILTIN_3
964 #undef RS6000_BUILTIN_A
965 #undef RS6000_BUILTIN_D
966 #undef RS6000_BUILTIN_E
967 #undef RS6000_BUILTIN_H
968 #undef RS6000_BUILTIN_P
969 #undef RS6000_BUILTIN_Q
970 #undef RS6000_BUILTIN_S
971 #undef RS6000_BUILTIN_X
973 /* Support for -mveclibabi=<xxx> to control which vector library to use. */
974 static tree (*rs6000_veclib_handler) (tree, tree, tree);
977 static bool rs6000_debug_legitimate_address_p (enum machine_mode, rtx, bool);
978 static bool spe_func_has_64bit_regs_p (void);
979 static struct machine_function * rs6000_init_machine_status (void);
980 static int rs6000_ra_ever_killed (void);
981 static tree rs6000_handle_longcall_attribute (tree *, tree, tree, int, bool *);
982 static tree rs6000_handle_altivec_attribute (tree *, tree, tree, int, bool *);
983 static tree rs6000_handle_struct_attribute (tree *, tree, tree, int, bool *);
984 static tree rs6000_builtin_vectorized_libmass (tree, tree, tree);
985 static rtx rs6000_emit_set_long_const (rtx, HOST_WIDE_INT, HOST_WIDE_INT);
986 static int rs6000_memory_move_cost (enum machine_mode, reg_class_t, bool);
987 static bool rs6000_debug_rtx_costs (rtx, int, int, int, int *, bool);
988 static int rs6000_debug_address_cost (rtx, enum machine_mode, addr_space_t,
989 bool);
990 static int rs6000_debug_adjust_cost (rtx, rtx, rtx, int);
991 static bool is_microcoded_insn (rtx);
992 static bool is_nonpipeline_insn (rtx);
993 static bool is_cracked_insn (rtx);
994 static bool is_load_insn (rtx, rtx *);
995 static bool is_store_insn (rtx, rtx *);
996 static bool set_to_load_agen (rtx,rtx);
997 static bool insn_terminates_group_p (rtx , enum group_termination);
998 static bool insn_must_be_first_in_group (rtx);
999 static bool insn_must_be_last_in_group (rtx);
1000 static void altivec_init_builtins (void);
1001 static tree builtin_function_type (enum machine_mode, enum machine_mode,
1002 enum machine_mode, enum machine_mode,
1003 enum rs6000_builtins, const char *name);
1004 static void rs6000_common_init_builtins (void);
1005 static void paired_init_builtins (void);
1006 static rtx paired_expand_predicate_builtin (enum insn_code, tree, rtx);
1007 static void spe_init_builtins (void);
1008 static void htm_init_builtins (void);
1009 static rtx spe_expand_predicate_builtin (enum insn_code, tree, rtx);
1010 static rtx spe_expand_evsel_builtin (enum insn_code, tree, rtx);
1011 static int rs6000_emit_int_cmove (rtx, rtx, rtx, rtx);
1012 static rs6000_stack_t *rs6000_stack_info (void);
1013 static void is_altivec_return_reg (rtx, void *);
1014 int easy_vector_constant (rtx, enum machine_mode);
1015 static rtx rs6000_debug_legitimize_address (rtx, rtx, enum machine_mode);
1016 static rtx rs6000_legitimize_tls_address (rtx, enum tls_model);
1017 static int rs6000_tls_symbol_ref_1 (rtx *, void *);
1018 static int rs6000_get_some_local_dynamic_name_1 (rtx *, void *);
1019 static rtx rs6000_darwin64_record_arg (CUMULATIVE_ARGS *, const_tree,
1020 bool, bool);
1021 #if TARGET_MACHO
1022 static void macho_branch_islands (void);
1023 #endif
1024 static rtx rs6000_legitimize_reload_address (rtx, enum machine_mode, int, int,
1025 int, int *);
1026 static rtx rs6000_debug_legitimize_reload_address (rtx, enum machine_mode, int,
1027 int, int, int *);
1028 static bool rs6000_mode_dependent_address (const_rtx);
1029 static bool rs6000_debug_mode_dependent_address (const_rtx);
1030 static enum reg_class rs6000_secondary_reload_class (enum reg_class,
1031 enum machine_mode, rtx);
1032 static enum reg_class rs6000_debug_secondary_reload_class (enum reg_class,
1033 enum machine_mode,
1034 rtx);
1035 static enum reg_class rs6000_preferred_reload_class (rtx, enum reg_class);
1036 static enum reg_class rs6000_debug_preferred_reload_class (rtx,
1037 enum reg_class);
1038 static bool rs6000_secondary_memory_needed (enum reg_class, enum reg_class,
1039 enum machine_mode);
1040 static bool rs6000_debug_secondary_memory_needed (enum reg_class,
1041 enum reg_class,
1042 enum machine_mode);
1043 static bool rs6000_cannot_change_mode_class (enum machine_mode,
1044 enum machine_mode,
1045 enum reg_class);
1046 static bool rs6000_debug_cannot_change_mode_class (enum machine_mode,
1047 enum machine_mode,
1048 enum reg_class);
1049 static bool rs6000_save_toc_in_prologue_p (void);
1051 rtx (*rs6000_legitimize_reload_address_ptr) (rtx, enum machine_mode, int, int,
1052 int, int *)
1053 = rs6000_legitimize_reload_address;
1055 static bool (*rs6000_mode_dependent_address_ptr) (const_rtx)
1056 = rs6000_mode_dependent_address;
1058 enum reg_class (*rs6000_secondary_reload_class_ptr) (enum reg_class,
1059 enum machine_mode, rtx)
1060 = rs6000_secondary_reload_class;
1062 enum reg_class (*rs6000_preferred_reload_class_ptr) (rtx, enum reg_class)
1063 = rs6000_preferred_reload_class;
1065 bool (*rs6000_secondary_memory_needed_ptr) (enum reg_class, enum reg_class,
1066 enum machine_mode)
1067 = rs6000_secondary_memory_needed;
1069 bool (*rs6000_cannot_change_mode_class_ptr) (enum machine_mode,
1070 enum machine_mode,
1071 enum reg_class)
1072 = rs6000_cannot_change_mode_class;
1074 const int INSN_NOT_AVAILABLE = -1;
1076 static void rs6000_print_isa_options (FILE *, int, const char *,
1077 HOST_WIDE_INT);
1078 static void rs6000_print_builtin_options (FILE *, int, const char *,
1079 HOST_WIDE_INT);
1081 static enum rs6000_reg_type register_to_reg_type (rtx, bool *);
1082 static bool rs6000_secondary_reload_move (enum rs6000_reg_type,
1083 enum rs6000_reg_type,
1084 enum machine_mode,
1085 secondary_reload_info *,
1086 bool);
1088 /* Hash table stuff for keeping track of TOC entries. */
1090 struct GTY(()) toc_hash_struct
1092 /* `key' will satisfy CONSTANT_P; in fact, it will satisfy
1093 ASM_OUTPUT_SPECIAL_POOL_ENTRY_P. */
1094 rtx key;
1095 enum machine_mode key_mode;
1096 int labelno;
1099 static GTY ((param_is (struct toc_hash_struct))) htab_t toc_hash_table;
1101 /* Hash table to keep track of the argument types for builtin functions. */
1103 struct GTY(()) builtin_hash_struct
1105 tree type;
1106 enum machine_mode mode[4]; /* return value + 3 arguments. */
1107 unsigned char uns_p[4]; /* and whether the types are unsigned. */
1110 static GTY ((param_is (struct builtin_hash_struct))) htab_t builtin_hash_table;
1113 /* Default register names. */
1114 char rs6000_reg_names[][8] =
1116 "0", "1", "2", "3", "4", "5", "6", "7",
1117 "8", "9", "10", "11", "12", "13", "14", "15",
1118 "16", "17", "18", "19", "20", "21", "22", "23",
1119 "24", "25", "26", "27", "28", "29", "30", "31",
1120 "0", "1", "2", "3", "4", "5", "6", "7",
1121 "8", "9", "10", "11", "12", "13", "14", "15",
1122 "16", "17", "18", "19", "20", "21", "22", "23",
1123 "24", "25", "26", "27", "28", "29", "30", "31",
1124 "mq", "lr", "ctr","ap",
1125 "0", "1", "2", "3", "4", "5", "6", "7",
1126 "ca",
1127 /* AltiVec registers. */
1128 "0", "1", "2", "3", "4", "5", "6", "7",
1129 "8", "9", "10", "11", "12", "13", "14", "15",
1130 "16", "17", "18", "19", "20", "21", "22", "23",
1131 "24", "25", "26", "27", "28", "29", "30", "31",
1132 "vrsave", "vscr",
1133 /* SPE registers. */
1134 "spe_acc", "spefscr",
1135 /* Soft frame pointer. */
1136 "sfp",
1137 /* HTM SPR registers. */
1138 "tfhar", "tfiar", "texasr"
1141 #ifdef TARGET_REGNAMES
1142 static const char alt_reg_names[][8] =
1144 "%r0", "%r1", "%r2", "%r3", "%r4", "%r5", "%r6", "%r7",
1145 "%r8", "%r9", "%r10", "%r11", "%r12", "%r13", "%r14", "%r15",
1146 "%r16", "%r17", "%r18", "%r19", "%r20", "%r21", "%r22", "%r23",
1147 "%r24", "%r25", "%r26", "%r27", "%r28", "%r29", "%r30", "%r31",
1148 "%f0", "%f1", "%f2", "%f3", "%f4", "%f5", "%f6", "%f7",
1149 "%f8", "%f9", "%f10", "%f11", "%f12", "%f13", "%f14", "%f15",
1150 "%f16", "%f17", "%f18", "%f19", "%f20", "%f21", "%f22", "%f23",
1151 "%f24", "%f25", "%f26", "%f27", "%f28", "%f29", "%f30", "%f31",
1152 "mq", "lr", "ctr", "ap",
1153 "%cr0", "%cr1", "%cr2", "%cr3", "%cr4", "%cr5", "%cr6", "%cr7",
1154 "ca",
1155 /* AltiVec registers. */
1156 "%v0", "%v1", "%v2", "%v3", "%v4", "%v5", "%v6", "%v7",
1157 "%v8", "%v9", "%v10", "%v11", "%v12", "%v13", "%v14", "%v15",
1158 "%v16", "%v17", "%v18", "%v19", "%v20", "%v21", "%v22", "%v23",
1159 "%v24", "%v25", "%v26", "%v27", "%v28", "%v29", "%v30", "%v31",
1160 "vrsave", "vscr",
1161 /* SPE registers. */
1162 "spe_acc", "spefscr",
1163 /* Soft frame pointer. */
1164 "sfp",
1165 /* HTM SPR registers. */
1166 "tfhar", "tfiar", "texasr"
1168 #endif
1170 /* Table of valid machine attributes. */
1172 static const struct attribute_spec rs6000_attribute_table[] =
1174 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
1175 affects_type_identity } */
1176 { "altivec", 1, 1, false, true, false, rs6000_handle_altivec_attribute,
1177 false },
1178 { "longcall", 0, 0, false, true, true, rs6000_handle_longcall_attribute,
1179 false },
1180 { "shortcall", 0, 0, false, true, true, rs6000_handle_longcall_attribute,
1181 false },
1182 { "ms_struct", 0, 0, false, false, false, rs6000_handle_struct_attribute,
1183 false },
1184 { "gcc_struct", 0, 0, false, false, false, rs6000_handle_struct_attribute,
1185 false },
1186 #ifdef SUBTARGET_ATTRIBUTE_TABLE
1187 SUBTARGET_ATTRIBUTE_TABLE,
1188 #endif
1189 { NULL, 0, 0, false, false, false, NULL, false }
1192 #ifndef TARGET_PROFILE_KERNEL
1193 #define TARGET_PROFILE_KERNEL 0
1194 #endif
1196 /* The VRSAVE bitmask puts bit %v0 as the most significant bit. */
1197 #define ALTIVEC_REG_BIT(REGNO) (0x80000000 >> ((REGNO) - FIRST_ALTIVEC_REGNO))
1199 /* Initialize the GCC target structure. */
1200 #undef TARGET_ATTRIBUTE_TABLE
1201 #define TARGET_ATTRIBUTE_TABLE rs6000_attribute_table
1202 #undef TARGET_SET_DEFAULT_TYPE_ATTRIBUTES
1203 #define TARGET_SET_DEFAULT_TYPE_ATTRIBUTES rs6000_set_default_type_attributes
1204 #undef TARGET_ATTRIBUTE_TAKES_IDENTIFIER_P
1205 #define TARGET_ATTRIBUTE_TAKES_IDENTIFIER_P rs6000_attribute_takes_identifier_p
1207 #undef TARGET_ASM_ALIGNED_DI_OP
1208 #define TARGET_ASM_ALIGNED_DI_OP DOUBLE_INT_ASM_OP
1210 /* Default unaligned ops are only provided for ELF. Find the ops needed
1211 for non-ELF systems. */
1212 #ifndef OBJECT_FORMAT_ELF
1213 #if TARGET_XCOFF
1214 /* For XCOFF. rs6000_assemble_integer will handle unaligned DIs on
1215 64-bit targets. */
1216 #undef TARGET_ASM_UNALIGNED_HI_OP
1217 #define TARGET_ASM_UNALIGNED_HI_OP "\t.vbyte\t2,"
1218 #undef TARGET_ASM_UNALIGNED_SI_OP
1219 #define TARGET_ASM_UNALIGNED_SI_OP "\t.vbyte\t4,"
1220 #undef TARGET_ASM_UNALIGNED_DI_OP
1221 #define TARGET_ASM_UNALIGNED_DI_OP "\t.vbyte\t8,"
1222 #else
1223 /* For Darwin. */
1224 #undef TARGET_ASM_UNALIGNED_HI_OP
1225 #define TARGET_ASM_UNALIGNED_HI_OP "\t.short\t"
1226 #undef TARGET_ASM_UNALIGNED_SI_OP
1227 #define TARGET_ASM_UNALIGNED_SI_OP "\t.long\t"
1228 #undef TARGET_ASM_UNALIGNED_DI_OP
1229 #define TARGET_ASM_UNALIGNED_DI_OP "\t.quad\t"
1230 #undef TARGET_ASM_ALIGNED_DI_OP
1231 #define TARGET_ASM_ALIGNED_DI_OP "\t.quad\t"
1232 #endif
1233 #endif
1235 /* This hook deals with fixups for relocatable code and DI-mode objects
1236 in 64-bit code. */
1237 #undef TARGET_ASM_INTEGER
1238 #define TARGET_ASM_INTEGER rs6000_assemble_integer
1240 #if defined (HAVE_GAS_HIDDEN) && !TARGET_MACHO
1241 #undef TARGET_ASM_ASSEMBLE_VISIBILITY
1242 #define TARGET_ASM_ASSEMBLE_VISIBILITY rs6000_assemble_visibility
1243 #endif
1245 #undef TARGET_SET_UP_BY_PROLOGUE
1246 #define TARGET_SET_UP_BY_PROLOGUE rs6000_set_up_by_prologue
1248 #undef TARGET_HAVE_TLS
1249 #define TARGET_HAVE_TLS HAVE_AS_TLS
1251 #undef TARGET_CANNOT_FORCE_CONST_MEM
1252 #define TARGET_CANNOT_FORCE_CONST_MEM rs6000_cannot_force_const_mem
1254 #undef TARGET_DELEGITIMIZE_ADDRESS
1255 #define TARGET_DELEGITIMIZE_ADDRESS rs6000_delegitimize_address
1257 #undef TARGET_CONST_NOT_OK_FOR_DEBUG_P
1258 #define TARGET_CONST_NOT_OK_FOR_DEBUG_P rs6000_const_not_ok_for_debug_p
1260 #undef TARGET_ASM_FUNCTION_PROLOGUE
1261 #define TARGET_ASM_FUNCTION_PROLOGUE rs6000_output_function_prologue
1262 #undef TARGET_ASM_FUNCTION_EPILOGUE
1263 #define TARGET_ASM_FUNCTION_EPILOGUE rs6000_output_function_epilogue
1265 #undef TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA
1266 #define TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA rs6000_output_addr_const_extra
1268 #undef TARGET_LEGITIMIZE_ADDRESS
1269 #define TARGET_LEGITIMIZE_ADDRESS rs6000_legitimize_address
1271 #undef TARGET_SCHED_VARIABLE_ISSUE
1272 #define TARGET_SCHED_VARIABLE_ISSUE rs6000_variable_issue
1274 #undef TARGET_SCHED_ISSUE_RATE
1275 #define TARGET_SCHED_ISSUE_RATE rs6000_issue_rate
1276 #undef TARGET_SCHED_ADJUST_COST
1277 #define TARGET_SCHED_ADJUST_COST rs6000_adjust_cost
1278 #undef TARGET_SCHED_ADJUST_PRIORITY
1279 #define TARGET_SCHED_ADJUST_PRIORITY rs6000_adjust_priority
1280 #undef TARGET_SCHED_IS_COSTLY_DEPENDENCE
1281 #define TARGET_SCHED_IS_COSTLY_DEPENDENCE rs6000_is_costly_dependence
1282 #undef TARGET_SCHED_INIT
1283 #define TARGET_SCHED_INIT rs6000_sched_init
1284 #undef TARGET_SCHED_FINISH
1285 #define TARGET_SCHED_FINISH rs6000_sched_finish
1286 #undef TARGET_SCHED_REORDER
1287 #define TARGET_SCHED_REORDER rs6000_sched_reorder
1288 #undef TARGET_SCHED_REORDER2
1289 #define TARGET_SCHED_REORDER2 rs6000_sched_reorder2
1291 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD
1292 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD rs6000_use_sched_lookahead
1294 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD_GUARD
1295 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD_GUARD rs6000_use_sched_lookahead_guard
1297 #undef TARGET_SCHED_ALLOC_SCHED_CONTEXT
1298 #define TARGET_SCHED_ALLOC_SCHED_CONTEXT rs6000_alloc_sched_context
1299 #undef TARGET_SCHED_INIT_SCHED_CONTEXT
1300 #define TARGET_SCHED_INIT_SCHED_CONTEXT rs6000_init_sched_context
1301 #undef TARGET_SCHED_SET_SCHED_CONTEXT
1302 #define TARGET_SCHED_SET_SCHED_CONTEXT rs6000_set_sched_context
1303 #undef TARGET_SCHED_FREE_SCHED_CONTEXT
1304 #define TARGET_SCHED_FREE_SCHED_CONTEXT rs6000_free_sched_context
1306 #undef TARGET_VECTORIZE_BUILTIN_MASK_FOR_LOAD
1307 #define TARGET_VECTORIZE_BUILTIN_MASK_FOR_LOAD rs6000_builtin_mask_for_load
1308 #undef TARGET_VECTORIZE_SUPPORT_VECTOR_MISALIGNMENT
1309 #define TARGET_VECTORIZE_SUPPORT_VECTOR_MISALIGNMENT \
1310 rs6000_builtin_support_vector_misalignment
1311 #undef TARGET_VECTORIZE_VECTOR_ALIGNMENT_REACHABLE
1312 #define TARGET_VECTORIZE_VECTOR_ALIGNMENT_REACHABLE rs6000_vector_alignment_reachable
1313 #undef TARGET_VECTORIZE_BUILTIN_VECTORIZATION_COST
1314 #define TARGET_VECTORIZE_BUILTIN_VECTORIZATION_COST \
1315 rs6000_builtin_vectorization_cost
1316 #undef TARGET_VECTORIZE_PREFERRED_SIMD_MODE
1317 #define TARGET_VECTORIZE_PREFERRED_SIMD_MODE \
1318 rs6000_preferred_simd_mode
1319 #undef TARGET_VECTORIZE_INIT_COST
1320 #define TARGET_VECTORIZE_INIT_COST rs6000_init_cost
1321 #undef TARGET_VECTORIZE_ADD_STMT_COST
1322 #define TARGET_VECTORIZE_ADD_STMT_COST rs6000_add_stmt_cost
1323 #undef TARGET_VECTORIZE_FINISH_COST
1324 #define TARGET_VECTORIZE_FINISH_COST rs6000_finish_cost
1325 #undef TARGET_VECTORIZE_DESTROY_COST_DATA
1326 #define TARGET_VECTORIZE_DESTROY_COST_DATA rs6000_destroy_cost_data
1328 #undef TARGET_INIT_BUILTINS
1329 #define TARGET_INIT_BUILTINS rs6000_init_builtins
1330 #undef TARGET_BUILTIN_DECL
1331 #define TARGET_BUILTIN_DECL rs6000_builtin_decl
1333 #undef TARGET_EXPAND_BUILTIN
1334 #define TARGET_EXPAND_BUILTIN rs6000_expand_builtin
1336 #undef TARGET_MANGLE_TYPE
1337 #define TARGET_MANGLE_TYPE rs6000_mangle_type
1339 #undef TARGET_INIT_LIBFUNCS
1340 #define TARGET_INIT_LIBFUNCS rs6000_init_libfuncs
1342 #if TARGET_MACHO
1343 #undef TARGET_BINDS_LOCAL_P
1344 #define TARGET_BINDS_LOCAL_P darwin_binds_local_p
1345 #endif
1347 #undef TARGET_MS_BITFIELD_LAYOUT_P
1348 #define TARGET_MS_BITFIELD_LAYOUT_P rs6000_ms_bitfield_layout_p
1350 #undef TARGET_ASM_OUTPUT_MI_THUNK
1351 #define TARGET_ASM_OUTPUT_MI_THUNK rs6000_output_mi_thunk
1353 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
1354 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK hook_bool_const_tree_hwi_hwi_const_tree_true
1356 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
1357 #define TARGET_FUNCTION_OK_FOR_SIBCALL rs6000_function_ok_for_sibcall
1359 #undef TARGET_REGISTER_MOVE_COST
1360 #define TARGET_REGISTER_MOVE_COST rs6000_register_move_cost
1361 #undef TARGET_MEMORY_MOVE_COST
1362 #define TARGET_MEMORY_MOVE_COST rs6000_memory_move_cost
1363 #undef TARGET_RTX_COSTS
1364 #define TARGET_RTX_COSTS rs6000_rtx_costs
1365 #undef TARGET_ADDRESS_COST
1366 #define TARGET_ADDRESS_COST hook_int_rtx_mode_as_bool_0
1368 #undef TARGET_DWARF_REGISTER_SPAN
1369 #define TARGET_DWARF_REGISTER_SPAN rs6000_dwarf_register_span
1371 #undef TARGET_INIT_DWARF_REG_SIZES_EXTRA
1372 #define TARGET_INIT_DWARF_REG_SIZES_EXTRA rs6000_init_dwarf_reg_sizes_extra
1374 #undef TARGET_MEMBER_TYPE_FORCES_BLK
1375 #define TARGET_MEMBER_TYPE_FORCES_BLK rs6000_member_type_forces_blk
1377 /* On rs6000, function arguments are promoted, as are function return
1378 values. */
1379 #undef TARGET_PROMOTE_FUNCTION_MODE
1380 #define TARGET_PROMOTE_FUNCTION_MODE default_promote_function_mode_always_promote
1382 #undef TARGET_RETURN_IN_MEMORY
1383 #define TARGET_RETURN_IN_MEMORY rs6000_return_in_memory
1385 #undef TARGET_SETUP_INCOMING_VARARGS
1386 #define TARGET_SETUP_INCOMING_VARARGS setup_incoming_varargs
1388 /* Always strict argument naming on rs6000. */
1389 #undef TARGET_STRICT_ARGUMENT_NAMING
1390 #define TARGET_STRICT_ARGUMENT_NAMING hook_bool_CUMULATIVE_ARGS_true
1391 #undef TARGET_PRETEND_OUTGOING_VARARGS_NAMED
1392 #define TARGET_PRETEND_OUTGOING_VARARGS_NAMED hook_bool_CUMULATIVE_ARGS_true
1393 #undef TARGET_SPLIT_COMPLEX_ARG
1394 #define TARGET_SPLIT_COMPLEX_ARG hook_bool_const_tree_true
1395 #undef TARGET_MUST_PASS_IN_STACK
1396 #define TARGET_MUST_PASS_IN_STACK rs6000_must_pass_in_stack
1397 #undef TARGET_PASS_BY_REFERENCE
1398 #define TARGET_PASS_BY_REFERENCE rs6000_pass_by_reference
1399 #undef TARGET_ARG_PARTIAL_BYTES
1400 #define TARGET_ARG_PARTIAL_BYTES rs6000_arg_partial_bytes
1401 #undef TARGET_FUNCTION_ARG_ADVANCE
1402 #define TARGET_FUNCTION_ARG_ADVANCE rs6000_function_arg_advance
1403 #undef TARGET_FUNCTION_ARG
1404 #define TARGET_FUNCTION_ARG rs6000_function_arg
1405 #undef TARGET_FUNCTION_ARG_BOUNDARY
1406 #define TARGET_FUNCTION_ARG_BOUNDARY rs6000_function_arg_boundary
1408 #undef TARGET_BUILD_BUILTIN_VA_LIST
1409 #define TARGET_BUILD_BUILTIN_VA_LIST rs6000_build_builtin_va_list
1411 #undef TARGET_EXPAND_BUILTIN_VA_START
1412 #define TARGET_EXPAND_BUILTIN_VA_START rs6000_va_start
1414 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
1415 #define TARGET_GIMPLIFY_VA_ARG_EXPR rs6000_gimplify_va_arg
1417 #undef TARGET_EH_RETURN_FILTER_MODE
1418 #define TARGET_EH_RETURN_FILTER_MODE rs6000_eh_return_filter_mode
1420 #undef TARGET_SCALAR_MODE_SUPPORTED_P
1421 #define TARGET_SCALAR_MODE_SUPPORTED_P rs6000_scalar_mode_supported_p
1423 #undef TARGET_VECTOR_MODE_SUPPORTED_P
1424 #define TARGET_VECTOR_MODE_SUPPORTED_P rs6000_vector_mode_supported_p
1426 #undef TARGET_INVALID_ARG_FOR_UNPROTOTYPED_FN
1427 #define TARGET_INVALID_ARG_FOR_UNPROTOTYPED_FN invalid_arg_for_unprototyped_fn
1429 #undef TARGET_ASM_LOOP_ALIGN_MAX_SKIP
1430 #define TARGET_ASM_LOOP_ALIGN_MAX_SKIP rs6000_loop_align_max_skip
1432 #undef TARGET_OPTION_OVERRIDE
1433 #define TARGET_OPTION_OVERRIDE rs6000_option_override
1435 #undef TARGET_VECTORIZE_BUILTIN_VECTORIZED_FUNCTION
1436 #define TARGET_VECTORIZE_BUILTIN_VECTORIZED_FUNCTION \
1437 rs6000_builtin_vectorized_function
1439 #if !TARGET_MACHO
1440 #undef TARGET_STACK_PROTECT_FAIL
1441 #define TARGET_STACK_PROTECT_FAIL rs6000_stack_protect_fail
1442 #endif
1444 /* MPC604EUM 3.5.2 Weak Consistency between Multiple Processors
1445 The PowerPC architecture requires only weak consistency among
1446 processors--that is, memory accesses between processors need not be
1447 sequentially consistent and memory accesses among processors can occur
1448 in any order. The ability to order memory accesses weakly provides
1449 opportunities for more efficient use of the system bus. Unless a
1450 dependency exists, the 604e allows read operations to precede store
1451 operations. */
1452 #undef TARGET_RELAXED_ORDERING
1453 #define TARGET_RELAXED_ORDERING true
1455 #ifdef HAVE_AS_TLS
1456 #undef TARGET_ASM_OUTPUT_DWARF_DTPREL
1457 #define TARGET_ASM_OUTPUT_DWARF_DTPREL rs6000_output_dwarf_dtprel
1458 #endif
1460 /* Use a 32-bit anchor range. This leads to sequences like:
1462 addis tmp,anchor,high
1463 add dest,tmp,low
1465 where tmp itself acts as an anchor, and can be shared between
1466 accesses to the same 64k page. */
1467 #undef TARGET_MIN_ANCHOR_OFFSET
1468 #define TARGET_MIN_ANCHOR_OFFSET -0x7fffffff - 1
1469 #undef TARGET_MAX_ANCHOR_OFFSET
1470 #define TARGET_MAX_ANCHOR_OFFSET 0x7fffffff
1471 #undef TARGET_USE_BLOCKS_FOR_CONSTANT_P
1472 #define TARGET_USE_BLOCKS_FOR_CONSTANT_P rs6000_use_blocks_for_constant_p
1473 #undef TARGET_USE_BLOCKS_FOR_DECL_P
1474 #define TARGET_USE_BLOCKS_FOR_DECL_P rs6000_use_blocks_for_decl_p
1476 #undef TARGET_BUILTIN_RECIPROCAL
1477 #define TARGET_BUILTIN_RECIPROCAL rs6000_builtin_reciprocal
1479 #undef TARGET_EXPAND_TO_RTL_HOOK
1480 #define TARGET_EXPAND_TO_RTL_HOOK rs6000_alloc_sdmode_stack_slot
1482 #undef TARGET_INSTANTIATE_DECLS
1483 #define TARGET_INSTANTIATE_DECLS rs6000_instantiate_decls
1485 #undef TARGET_SECONDARY_RELOAD
1486 #define TARGET_SECONDARY_RELOAD rs6000_secondary_reload
1488 #undef TARGET_LEGITIMATE_ADDRESS_P
1489 #define TARGET_LEGITIMATE_ADDRESS_P rs6000_legitimate_address_p
1491 #undef TARGET_MODE_DEPENDENT_ADDRESS_P
1492 #define TARGET_MODE_DEPENDENT_ADDRESS_P rs6000_mode_dependent_address_p
1494 #undef TARGET_CAN_ELIMINATE
1495 #define TARGET_CAN_ELIMINATE rs6000_can_eliminate
1497 #undef TARGET_CONDITIONAL_REGISTER_USAGE
1498 #define TARGET_CONDITIONAL_REGISTER_USAGE rs6000_conditional_register_usage
1500 #undef TARGET_TRAMPOLINE_INIT
1501 #define TARGET_TRAMPOLINE_INIT rs6000_trampoline_init
1503 #undef TARGET_FUNCTION_VALUE
1504 #define TARGET_FUNCTION_VALUE rs6000_function_value
1506 #undef TARGET_OPTION_VALID_ATTRIBUTE_P
1507 #define TARGET_OPTION_VALID_ATTRIBUTE_P rs6000_valid_attribute_p
1509 #undef TARGET_OPTION_SAVE
1510 #define TARGET_OPTION_SAVE rs6000_function_specific_save
1512 #undef TARGET_OPTION_RESTORE
1513 #define TARGET_OPTION_RESTORE rs6000_function_specific_restore
1515 #undef TARGET_OPTION_PRINT
1516 #define TARGET_OPTION_PRINT rs6000_function_specific_print
1518 #undef TARGET_CAN_INLINE_P
1519 #define TARGET_CAN_INLINE_P rs6000_can_inline_p
1521 #undef TARGET_SET_CURRENT_FUNCTION
1522 #define TARGET_SET_CURRENT_FUNCTION rs6000_set_current_function
1524 #undef TARGET_LEGITIMATE_CONSTANT_P
1525 #define TARGET_LEGITIMATE_CONSTANT_P rs6000_legitimate_constant_p
1527 #undef TARGET_VECTORIZE_VEC_PERM_CONST_OK
1528 #define TARGET_VECTORIZE_VEC_PERM_CONST_OK rs6000_vectorize_vec_perm_const_ok
1531 /* Processor table. */
1532 struct rs6000_ptt
1534 const char *const name; /* Canonical processor name. */
1535 const enum processor_type processor; /* Processor type enum value. */
1536 const HOST_WIDE_INT target_enable; /* Target flags to enable. */
1539 static struct rs6000_ptt const processor_target_table[] =
1541 #define RS6000_CPU(NAME, CPU, FLAGS) { NAME, CPU, FLAGS },
1542 #include "rs6000-cpus.def"
1543 #undef RS6000_CPU
1546 /* Look up a processor name for -mcpu=xxx and -mtune=xxx. Return -1 if the
1547 name is invalid. */
1549 static int
1550 rs6000_cpu_name_lookup (const char *name)
1552 size_t i;
1554 if (name != NULL)
1556 for (i = 0; i < ARRAY_SIZE (processor_target_table); i++)
1557 if (! strcmp (name, processor_target_table[i].name))
1558 return (int)i;
1561 return -1;
1565 /* Return number of consecutive hard regs needed starting at reg REGNO
1566 to hold something of mode MODE.
1567 This is ordinarily the length in words of a value of mode MODE
1568 but can be less for certain modes in special long registers.
1570 For the SPE, GPRs are 64 bits but only 32 bits are visible in
1571 scalar instructions. The upper 32 bits are only available to the
1572 SIMD instructions.
1574 POWER and PowerPC GPRs hold 32 bits worth;
1575 PowerPC64 GPRs and FPRs point register holds 64 bits worth. */
1577 static int
1578 rs6000_hard_regno_nregs_internal (int regno, enum machine_mode mode)
1580 unsigned HOST_WIDE_INT reg_size;
1582 /* TF/TD modes are special in that they always take 2 registers. */
1583 if (FP_REGNO_P (regno))
1584 reg_size = ((VECTOR_MEM_VSX_P (mode) && mode != TDmode && mode != TFmode)
1585 ? UNITS_PER_VSX_WORD
1586 : UNITS_PER_FP_WORD);
1588 else if (SPE_SIMD_REGNO_P (regno) && TARGET_SPE && SPE_VECTOR_MODE (mode))
1589 reg_size = UNITS_PER_SPE_WORD;
1591 else if (ALTIVEC_REGNO_P (regno))
1592 reg_size = UNITS_PER_ALTIVEC_WORD;
1594 /* The value returned for SCmode in the E500 double case is 2 for
1595 ABI compatibility; storing an SCmode value in a single register
1596 would require function_arg and rs6000_spe_function_arg to handle
1597 SCmode so as to pass the value correctly in a pair of
1598 registers. */
1599 else if (TARGET_E500_DOUBLE && FLOAT_MODE_P (mode) && mode != SCmode
1600 && !DECIMAL_FLOAT_MODE_P (mode))
1601 reg_size = UNITS_PER_FP_WORD;
1603 else
1604 reg_size = UNITS_PER_WORD;
1606 return (GET_MODE_SIZE (mode) + reg_size - 1) / reg_size;
1609 /* Value is 1 if hard register REGNO can hold a value of machine-mode
1610 MODE. */
1611 static int
1612 rs6000_hard_regno_mode_ok (int regno, enum machine_mode mode)
1614 int last_regno = regno + rs6000_hard_regno_nregs[mode][regno] - 1;
1616 /* PTImode can only go in GPRs. Quad word memory operations require even/odd
1617 register combinations, and use PTImode where we need to deal with quad
1618 word memory operations. Don't allow quad words in the argument or frame
1619 pointer registers, just registers 0..31. */
1620 if (mode == PTImode)
1621 return (IN_RANGE (regno, FIRST_GPR_REGNO, LAST_GPR_REGNO)
1622 && IN_RANGE (last_regno, FIRST_GPR_REGNO, LAST_GPR_REGNO)
1623 && ((regno & 1) == 0));
1625 /* VSX registers that overlap the FPR registers are larger than for non-VSX
1626 implementations. Don't allow an item to be split between a FP register
1627 and an Altivec register. */
1628 if (VECTOR_MEM_VSX_P (mode))
1630 if (FP_REGNO_P (regno))
1631 return FP_REGNO_P (last_regno);
1633 if (ALTIVEC_REGNO_P (regno))
1634 return ALTIVEC_REGNO_P (last_regno);
1637 /* Allow TImode in all VSX registers if the user asked for it. */
1638 if (mode == TImode && TARGET_VSX_TIMODE && VSX_REGNO_P (regno))
1639 return 1;
1641 /* The GPRs can hold any mode, but values bigger than one register
1642 cannot go past R31. */
1643 if (INT_REGNO_P (regno))
1644 return INT_REGNO_P (last_regno);
1646 /* The float registers (except for VSX vector modes) can only hold floating
1647 modes and DImode. */
1648 if (FP_REGNO_P (regno))
1650 if (SCALAR_FLOAT_MODE_P (mode)
1651 && (mode != TDmode || (regno % 2) == 0)
1652 && FP_REGNO_P (last_regno))
1653 return 1;
1655 if (GET_MODE_CLASS (mode) == MODE_INT
1656 && GET_MODE_SIZE (mode) == UNITS_PER_FP_WORD)
1657 return 1;
1659 if (PAIRED_SIMD_REGNO_P (regno) && TARGET_PAIRED_FLOAT
1660 && PAIRED_VECTOR_MODE (mode))
1661 return 1;
1663 return 0;
1666 /* The CR register can only hold CC modes. */
1667 if (CR_REGNO_P (regno))
1668 return GET_MODE_CLASS (mode) == MODE_CC;
1670 if (CA_REGNO_P (regno))
1671 return mode == BImode;
1673 /* AltiVec only in AldyVec registers. */
1674 if (ALTIVEC_REGNO_P (regno))
1675 return VECTOR_MEM_ALTIVEC_OR_VSX_P (mode);
1677 /* ...but GPRs can hold SIMD data on the SPE in one register. */
1678 if (SPE_SIMD_REGNO_P (regno) && TARGET_SPE && SPE_VECTOR_MODE (mode))
1679 return 1;
1681 /* We cannot put non-VSX TImode or PTImode anywhere except general register
1682 and it must be able to fit within the register set. */
1684 return GET_MODE_SIZE (mode) <= UNITS_PER_WORD;
1687 /* Print interesting facts about registers. */
1688 static void
1689 rs6000_debug_reg_print (int first_regno, int last_regno, const char *reg_name)
1691 int r, m;
1693 for (r = first_regno; r <= last_regno; ++r)
1695 const char *comma = "";
1696 int len;
1698 if (first_regno == last_regno)
1699 fprintf (stderr, "%s:\t", reg_name);
1700 else
1701 fprintf (stderr, "%s%d:\t", reg_name, r - first_regno);
1703 len = 8;
1704 for (m = 0; m < NUM_MACHINE_MODES; ++m)
1705 if (rs6000_hard_regno_mode_ok_p[m][r] && rs6000_hard_regno_nregs[m][r])
1707 if (len > 70)
1709 fprintf (stderr, ",\n\t");
1710 len = 8;
1711 comma = "";
1714 if (rs6000_hard_regno_nregs[m][r] > 1)
1715 len += fprintf (stderr, "%s%s/%d", comma, GET_MODE_NAME (m),
1716 rs6000_hard_regno_nregs[m][r]);
1717 else
1718 len += fprintf (stderr, "%s%s", comma, GET_MODE_NAME (m));
1720 comma = ", ";
1723 if (call_used_regs[r])
1725 if (len > 70)
1727 fprintf (stderr, ",\n\t");
1728 len = 8;
1729 comma = "";
1732 len += fprintf (stderr, "%s%s", comma, "call-used");
1733 comma = ", ";
1736 if (fixed_regs[r])
1738 if (len > 70)
1740 fprintf (stderr, ",\n\t");
1741 len = 8;
1742 comma = "";
1745 len += fprintf (stderr, "%s%s", comma, "fixed");
1746 comma = ", ";
1749 if (len > 70)
1751 fprintf (stderr, ",\n\t");
1752 comma = "";
1755 len += fprintf (stderr, "%sreg-class = %s", comma,
1756 reg_class_names[(int)rs6000_regno_regclass[r]]);
1757 comma = ", ";
1759 if (len > 70)
1761 fprintf (stderr, ",\n\t");
1762 comma = "";
1765 fprintf (stderr, "%sregno = %d\n", comma, r);
1769 #define DEBUG_FMT_ID "%-32s= "
1770 #define DEBUG_FMT_D DEBUG_FMT_ID "%d\n"
1771 #define DEBUG_FMT_WX DEBUG_FMT_ID "%#.12" HOST_WIDE_INT_PRINT "x: "
1772 #define DEBUG_FMT_S DEBUG_FMT_ID "%s\n"
1774 /* Print various interesting information with -mdebug=reg. */
1775 static void
1776 rs6000_debug_reg_global (void)
1778 static const char *const tf[2] = { "false", "true" };
1779 const char *nl = (const char *)0;
1780 int m;
1781 size_t m1, m2, v;
1782 char costly_num[20];
1783 char nop_num[20];
1784 char flags_buffer[40];
1785 const char *costly_str;
1786 const char *nop_str;
1787 const char *trace_str;
1788 const char *abi_str;
1789 const char *cmodel_str;
1790 struct cl_target_option cl_opts;
1792 /* Map enum rs6000_vector to string. */
1793 static const char *rs6000_debug_vector_unit[] = {
1794 "none",
1795 "altivec",
1796 "vsx",
1797 "p8_vector",
1798 "paired",
1799 "spe",
1800 "other"
1803 /* Modes we want tieable information on. */
1804 static const enum machine_mode print_tieable_modes[] = {
1805 QImode,
1806 HImode,
1807 SImode,
1808 DImode,
1809 TImode,
1810 PTImode,
1811 SFmode,
1812 DFmode,
1813 TFmode,
1814 SDmode,
1815 DDmode,
1816 TDmode,
1817 V8QImode,
1818 V4HImode,
1819 V2SImode,
1820 V16QImode,
1821 V8HImode,
1822 V4SImode,
1823 V2DImode,
1824 V32QImode,
1825 V16HImode,
1826 V8SImode,
1827 V4DImode,
1828 V2SFmode,
1829 V4SFmode,
1830 V2DFmode,
1831 V8SFmode,
1832 V4DFmode,
1833 CCmode,
1834 CCUNSmode,
1835 CCEQmode,
1838 /* Virtual regs we are interested in. */
1839 const static struct {
1840 int regno; /* register number. */
1841 const char *name; /* register name. */
1842 } virtual_regs[] = {
1843 { STACK_POINTER_REGNUM, "stack pointer:" },
1844 { TOC_REGNUM, "toc: " },
1845 { STATIC_CHAIN_REGNUM, "static chain: " },
1846 { RS6000_PIC_OFFSET_TABLE_REGNUM, "pic offset: " },
1847 { HARD_FRAME_POINTER_REGNUM, "hard frame: " },
1848 { ARG_POINTER_REGNUM, "arg pointer: " },
1849 { FRAME_POINTER_REGNUM, "frame pointer:" },
1850 { FIRST_PSEUDO_REGISTER, "first pseudo: " },
1851 { FIRST_VIRTUAL_REGISTER, "first virtual:" },
1852 { VIRTUAL_INCOMING_ARGS_REGNUM, "incoming_args:" },
1853 { VIRTUAL_STACK_VARS_REGNUM, "stack_vars: " },
1854 { VIRTUAL_STACK_DYNAMIC_REGNUM, "stack_dynamic:" },
1855 { VIRTUAL_OUTGOING_ARGS_REGNUM, "outgoing_args:" },
1856 { VIRTUAL_CFA_REGNUM, "cfa (frame): " },
1857 { VIRTUAL_PREFERRED_STACK_BOUNDARY_REGNUM, "stack boundry:" },
1858 { LAST_VIRTUAL_REGISTER, "last virtual: " },
1861 fputs ("\nHard register information:\n", stderr);
1862 rs6000_debug_reg_print (FIRST_GPR_REGNO, LAST_GPR_REGNO, "gr");
1863 rs6000_debug_reg_print (FIRST_FPR_REGNO, LAST_FPR_REGNO, "fp");
1864 rs6000_debug_reg_print (FIRST_ALTIVEC_REGNO,
1865 LAST_ALTIVEC_REGNO,
1866 "vs");
1867 rs6000_debug_reg_print (LR_REGNO, LR_REGNO, "lr");
1868 rs6000_debug_reg_print (CTR_REGNO, CTR_REGNO, "ctr");
1869 rs6000_debug_reg_print (CR0_REGNO, CR7_REGNO, "cr");
1870 rs6000_debug_reg_print (CA_REGNO, CA_REGNO, "ca");
1871 rs6000_debug_reg_print (VRSAVE_REGNO, VRSAVE_REGNO, "vrsave");
1872 rs6000_debug_reg_print (VSCR_REGNO, VSCR_REGNO, "vscr");
1873 rs6000_debug_reg_print (SPE_ACC_REGNO, SPE_ACC_REGNO, "spe_a");
1874 rs6000_debug_reg_print (SPEFSCR_REGNO, SPEFSCR_REGNO, "spe_f");
1876 fputs ("\nVirtual/stack/frame registers:\n", stderr);
1877 for (v = 0; v < ARRAY_SIZE (virtual_regs); v++)
1878 fprintf (stderr, "%s regno = %3d\n", virtual_regs[v].name, virtual_regs[v].regno);
1880 fprintf (stderr,
1881 "\n"
1882 "d reg_class = %s\n"
1883 "f reg_class = %s\n"
1884 "v reg_class = %s\n"
1885 "wa reg_class = %s\n"
1886 "wd reg_class = %s\n"
1887 "wf reg_class = %s\n"
1888 "wg reg_class = %s\n"
1889 "wl reg_class = %s\n"
1890 "wm reg_class = %s\n"
1891 "wr reg_class = %s\n"
1892 "ws reg_class = %s\n"
1893 "wt reg_class = %s\n"
1894 "wv reg_class = %s\n"
1895 "wx reg_class = %s\n"
1896 "wz reg_class = %s\n"
1897 "\n",
1898 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_d]],
1899 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_f]],
1900 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_v]],
1901 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wa]],
1902 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wd]],
1903 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wf]],
1904 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wg]],
1905 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wl]],
1906 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wm]],
1907 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wr]],
1908 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_ws]],
1909 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wt]],
1910 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wv]],
1911 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wx]],
1912 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wz]]);
1914 for (m = 0; m < NUM_MACHINE_MODES; ++m)
1915 if (rs6000_vector_unit[m] || rs6000_vector_mem[m]
1916 || (rs6000_vector_reload[m][0] != CODE_FOR_nothing)
1917 || (rs6000_vector_reload[m][1] != CODE_FOR_nothing))
1919 nl = "\n";
1920 fprintf (stderr,
1921 "Vector mode: %-5s arithmetic: %-10s move: %-10s "
1922 "reload-out: %c reload-in: %c\n",
1923 GET_MODE_NAME (m),
1924 rs6000_debug_vector_unit[ rs6000_vector_unit[m] ],
1925 rs6000_debug_vector_unit[ rs6000_vector_mem[m] ],
1926 (rs6000_vector_reload[m][0] != CODE_FOR_nothing) ? 'y' : 'n',
1927 (rs6000_vector_reload[m][1] != CODE_FOR_nothing) ? 'y' : 'n');
1930 if (nl)
1931 fputs (nl, stderr);
1933 for (m1 = 0; m1 < ARRAY_SIZE (print_tieable_modes); m1++)
1935 enum machine_mode mode1 = print_tieable_modes[m1];
1936 bool first_time = true;
1938 nl = (const char *)0;
1939 for (m2 = 0; m2 < ARRAY_SIZE (print_tieable_modes); m2++)
1941 enum machine_mode mode2 = print_tieable_modes[m2];
1942 if (mode1 != mode2 && MODES_TIEABLE_P (mode1, mode2))
1944 if (first_time)
1946 fprintf (stderr, "Tieable modes %s:", GET_MODE_NAME (mode1));
1947 nl = "\n";
1948 first_time = false;
1951 fprintf (stderr, " %s", GET_MODE_NAME (mode2));
1955 if (!first_time)
1956 fputs ("\n", stderr);
1959 if (nl)
1960 fputs (nl, stderr);
1962 if (rs6000_recip_control)
1964 fprintf (stderr, "\nReciprocal mask = 0x%x\n", rs6000_recip_control);
1966 for (m = 0; m < NUM_MACHINE_MODES; ++m)
1967 if (rs6000_recip_bits[m])
1969 fprintf (stderr,
1970 "Reciprocal estimate mode: %-5s divide: %s rsqrt: %s\n",
1971 GET_MODE_NAME (m),
1972 (RS6000_RECIP_AUTO_RE_P (m)
1973 ? "auto"
1974 : (RS6000_RECIP_HAVE_RE_P (m) ? "have" : "none")),
1975 (RS6000_RECIP_AUTO_RSQRTE_P (m)
1976 ? "auto"
1977 : (RS6000_RECIP_HAVE_RSQRTE_P (m) ? "have" : "none")));
1980 fputs ("\n", stderr);
1983 if (rs6000_cpu_index >= 0)
1985 const char *name = processor_target_table[rs6000_cpu_index].name;
1986 HOST_WIDE_INT flags
1987 = processor_target_table[rs6000_cpu_index].target_enable;
1989 sprintf (flags_buffer, "-mcpu=%s flags", name);
1990 rs6000_print_isa_options (stderr, 0, flags_buffer, flags);
1992 else
1993 fprintf (stderr, DEBUG_FMT_S, "cpu", "<none>");
1995 if (rs6000_tune_index >= 0)
1997 const char *name = processor_target_table[rs6000_tune_index].name;
1998 HOST_WIDE_INT flags
1999 = processor_target_table[rs6000_tune_index].target_enable;
2001 sprintf (flags_buffer, "-mtune=%s flags", name);
2002 rs6000_print_isa_options (stderr, 0, flags_buffer, flags);
2004 else
2005 fprintf (stderr, DEBUG_FMT_S, "tune", "<none>");
2007 cl_target_option_save (&cl_opts, &global_options);
2008 rs6000_print_isa_options (stderr, 0, "rs6000_isa_flags",
2009 rs6000_isa_flags);
2011 rs6000_print_isa_options (stderr, 0, "rs6000_isa_flags_explicit",
2012 rs6000_isa_flags_explicit);
2014 rs6000_print_builtin_options (stderr, 0, "rs6000_builtin_mask",
2015 rs6000_builtin_mask);
2017 rs6000_print_isa_options (stderr, 0, "TARGET_DEFAULT", TARGET_DEFAULT);
2019 fprintf (stderr, DEBUG_FMT_S, "--with-cpu default",
2020 OPTION_TARGET_CPU_DEFAULT ? OPTION_TARGET_CPU_DEFAULT : "<none>");
2022 switch (rs6000_sched_costly_dep)
2024 case max_dep_latency:
2025 costly_str = "max_dep_latency";
2026 break;
2028 case no_dep_costly:
2029 costly_str = "no_dep_costly";
2030 break;
2032 case all_deps_costly:
2033 costly_str = "all_deps_costly";
2034 break;
2036 case true_store_to_load_dep_costly:
2037 costly_str = "true_store_to_load_dep_costly";
2038 break;
2040 case store_to_load_dep_costly:
2041 costly_str = "store_to_load_dep_costly";
2042 break;
2044 default:
2045 costly_str = costly_num;
2046 sprintf (costly_num, "%d", (int)rs6000_sched_costly_dep);
2047 break;
2050 fprintf (stderr, DEBUG_FMT_S, "sched_costly_dep", costly_str);
2052 switch (rs6000_sched_insert_nops)
2054 case sched_finish_regroup_exact:
2055 nop_str = "sched_finish_regroup_exact";
2056 break;
2058 case sched_finish_pad_groups:
2059 nop_str = "sched_finish_pad_groups";
2060 break;
2062 case sched_finish_none:
2063 nop_str = "sched_finish_none";
2064 break;
2066 default:
2067 nop_str = nop_num;
2068 sprintf (nop_num, "%d", (int)rs6000_sched_insert_nops);
2069 break;
2072 fprintf (stderr, DEBUG_FMT_S, "sched_insert_nops", nop_str);
2074 switch (rs6000_sdata)
2076 default:
2077 case SDATA_NONE:
2078 break;
2080 case SDATA_DATA:
2081 fprintf (stderr, DEBUG_FMT_S, "sdata", "data");
2082 break;
2084 case SDATA_SYSV:
2085 fprintf (stderr, DEBUG_FMT_S, "sdata", "sysv");
2086 break;
2088 case SDATA_EABI:
2089 fprintf (stderr, DEBUG_FMT_S, "sdata", "eabi");
2090 break;
2094 switch (rs6000_traceback)
2096 case traceback_default: trace_str = "default"; break;
2097 case traceback_none: trace_str = "none"; break;
2098 case traceback_part: trace_str = "part"; break;
2099 case traceback_full: trace_str = "full"; break;
2100 default: trace_str = "unknown"; break;
2103 fprintf (stderr, DEBUG_FMT_S, "traceback", trace_str);
2105 switch (rs6000_current_cmodel)
2107 case CMODEL_SMALL: cmodel_str = "small"; break;
2108 case CMODEL_MEDIUM: cmodel_str = "medium"; break;
2109 case CMODEL_LARGE: cmodel_str = "large"; break;
2110 default: cmodel_str = "unknown"; break;
2113 fprintf (stderr, DEBUG_FMT_S, "cmodel", cmodel_str);
2115 switch (rs6000_current_abi)
2117 case ABI_NONE: abi_str = "none"; break;
2118 case ABI_AIX: abi_str = "aix"; break;
2119 case ABI_V4: abi_str = "V4"; break;
2120 case ABI_DARWIN: abi_str = "darwin"; break;
2121 default: abi_str = "unknown"; break;
2124 fprintf (stderr, DEBUG_FMT_S, "abi", abi_str);
2126 if (rs6000_altivec_abi)
2127 fprintf (stderr, DEBUG_FMT_S, "altivec_abi", "true");
2129 if (rs6000_spe_abi)
2130 fprintf (stderr, DEBUG_FMT_S, "spe_abi", "true");
2132 if (rs6000_darwin64_abi)
2133 fprintf (stderr, DEBUG_FMT_S, "darwin64_abi", "true");
2135 if (rs6000_float_gprs)
2136 fprintf (stderr, DEBUG_FMT_S, "float_gprs", "true");
2138 if (TARGET_LINK_STACK)
2139 fprintf (stderr, DEBUG_FMT_S, "link_stack", "true");
2141 if (targetm.lra_p ())
2142 fprintf (stderr, DEBUG_FMT_S, "lra", "true");
2144 if (TARGET_P8_FUSION)
2145 fprintf (stderr, DEBUG_FMT_S, "p8 fusion",
2146 (TARGET_P8_FUSION_SIGN) ? "zero+sign" : "zero");
2148 fprintf (stderr, DEBUG_FMT_S, "plt-format",
2149 TARGET_SECURE_PLT ? "secure" : "bss");
2150 fprintf (stderr, DEBUG_FMT_S, "struct-return",
2151 aix_struct_return ? "aix" : "sysv");
2152 fprintf (stderr, DEBUG_FMT_S, "always_hint", tf[!!rs6000_always_hint]);
2153 fprintf (stderr, DEBUG_FMT_S, "sched_groups", tf[!!rs6000_sched_groups]);
2154 fprintf (stderr, DEBUG_FMT_S, "align_branch",
2155 tf[!!rs6000_align_branch_targets]);
2156 fprintf (stderr, DEBUG_FMT_D, "tls_size", rs6000_tls_size);
2157 fprintf (stderr, DEBUG_FMT_D, "long_double_size",
2158 rs6000_long_double_type_size);
2159 fprintf (stderr, DEBUG_FMT_D, "sched_restricted_insns_priority",
2160 (int)rs6000_sched_restricted_insns_priority);
2161 fprintf (stderr, DEBUG_FMT_D, "Number of standard builtins",
2162 (int)END_BUILTINS);
2163 fprintf (stderr, DEBUG_FMT_D, "Number of rs6000 builtins",
2164 (int)RS6000_BUILTIN_COUNT);
2167 /* Initialize the various global tables that are based on register size. */
2168 static void
2169 rs6000_init_hard_regno_mode_ok (bool global_init_p)
2171 int r, m, c;
2172 int align64;
2173 int align32;
2175 /* Precalculate REGNO_REG_CLASS. */
2176 rs6000_regno_regclass[0] = GENERAL_REGS;
2177 for (r = 1; r < 32; ++r)
2178 rs6000_regno_regclass[r] = BASE_REGS;
2180 for (r = 32; r < 64; ++r)
2181 rs6000_regno_regclass[r] = FLOAT_REGS;
2183 for (r = 64; r < FIRST_PSEUDO_REGISTER; ++r)
2184 rs6000_regno_regclass[r] = NO_REGS;
2186 for (r = FIRST_ALTIVEC_REGNO; r <= LAST_ALTIVEC_REGNO; ++r)
2187 rs6000_regno_regclass[r] = ALTIVEC_REGS;
2189 rs6000_regno_regclass[CR0_REGNO] = CR0_REGS;
2190 for (r = CR1_REGNO; r <= CR7_REGNO; ++r)
2191 rs6000_regno_regclass[r] = CR_REGS;
2193 rs6000_regno_regclass[LR_REGNO] = LINK_REGS;
2194 rs6000_regno_regclass[CTR_REGNO] = CTR_REGS;
2195 rs6000_regno_regclass[CA_REGNO] = CA_REGS;
2196 rs6000_regno_regclass[VRSAVE_REGNO] = VRSAVE_REGS;
2197 rs6000_regno_regclass[VSCR_REGNO] = VRSAVE_REGS;
2198 rs6000_regno_regclass[SPE_ACC_REGNO] = SPE_ACC_REGS;
2199 rs6000_regno_regclass[SPEFSCR_REGNO] = SPEFSCR_REGS;
2200 rs6000_regno_regclass[TFHAR_REGNO] = SPR_REGS;
2201 rs6000_regno_regclass[TFIAR_REGNO] = SPR_REGS;
2202 rs6000_regno_regclass[TEXASR_REGNO] = SPR_REGS;
2203 rs6000_regno_regclass[ARG_POINTER_REGNUM] = BASE_REGS;
2204 rs6000_regno_regclass[FRAME_POINTER_REGNUM] = BASE_REGS;
2206 /* Precalculate register class to simpler reload register class. We don't
2207 need all of the register classes that are combinations of different
2208 classes, just the simple ones that have constraint letters. */
2209 for (c = 0; c < N_REG_CLASSES; c++)
2210 reg_class_to_reg_type[c] = NO_REG_TYPE;
2212 reg_class_to_reg_type[(int)GENERAL_REGS] = GPR_REG_TYPE;
2213 reg_class_to_reg_type[(int)BASE_REGS] = GPR_REG_TYPE;
2214 reg_class_to_reg_type[(int)VSX_REGS] = VSX_REG_TYPE;
2215 reg_class_to_reg_type[(int)VRSAVE_REGS] = SPR_REG_TYPE;
2216 reg_class_to_reg_type[(int)VSCR_REGS] = SPR_REG_TYPE;
2217 reg_class_to_reg_type[(int)LINK_REGS] = SPR_REG_TYPE;
2218 reg_class_to_reg_type[(int)CTR_REGS] = SPR_REG_TYPE;
2219 reg_class_to_reg_type[(int)LINK_OR_CTR_REGS] = SPR_REG_TYPE;
2220 reg_class_to_reg_type[(int)CR_REGS] = CR_REG_TYPE;
2221 reg_class_to_reg_type[(int)CR0_REGS] = CR_REG_TYPE;
2222 reg_class_to_reg_type[(int)SPE_ACC_REGS] = SPE_ACC_TYPE;
2223 reg_class_to_reg_type[(int)SPEFSCR_REGS] = SPEFSCR_REG_TYPE;
2225 if (TARGET_VSX)
2227 reg_class_to_reg_type[(int)FLOAT_REGS] = VSX_REG_TYPE;
2228 reg_class_to_reg_type[(int)ALTIVEC_REGS] = VSX_REG_TYPE;
2230 else
2232 reg_class_to_reg_type[(int)FLOAT_REGS] = FPR_REG_TYPE;
2233 reg_class_to_reg_type[(int)ALTIVEC_REGS] = ALTIVEC_REG_TYPE;
2236 /* Precalculate vector information, this must be set up before the
2237 rs6000_hard_regno_nregs_internal below. */
2238 for (m = 0; m < NUM_MACHINE_MODES; ++m)
2240 rs6000_vector_unit[m] = rs6000_vector_mem[m] = VECTOR_NONE;
2241 rs6000_vector_reload[m][0] = CODE_FOR_nothing;
2242 rs6000_vector_reload[m][1] = CODE_FOR_nothing;
2245 for (c = 0; c < (int)(int)RS6000_CONSTRAINT_MAX; c++)
2246 rs6000_constraints[c] = NO_REGS;
2248 /* The VSX hardware allows native alignment for vectors, but control whether the compiler
2249 believes it can use native alignment or still uses 128-bit alignment. */
2250 if (TARGET_VSX && !TARGET_VSX_ALIGN_128)
2252 align64 = 64;
2253 align32 = 32;
2255 else
2257 align64 = 128;
2258 align32 = 128;
2261 /* V2DF mode, VSX only. */
2262 if (TARGET_VSX)
2264 rs6000_vector_unit[V2DFmode] = VECTOR_VSX;
2265 rs6000_vector_mem[V2DFmode] = VECTOR_VSX;
2266 rs6000_vector_align[V2DFmode] = align64;
2269 /* V4SF mode, either VSX or Altivec. */
2270 if (TARGET_VSX)
2272 rs6000_vector_unit[V4SFmode] = VECTOR_VSX;
2273 rs6000_vector_mem[V4SFmode] = VECTOR_VSX;
2274 rs6000_vector_align[V4SFmode] = align32;
2276 else if (TARGET_ALTIVEC)
2278 rs6000_vector_unit[V4SFmode] = VECTOR_ALTIVEC;
2279 rs6000_vector_mem[V4SFmode] = VECTOR_ALTIVEC;
2280 rs6000_vector_align[V4SFmode] = align32;
2283 /* V16QImode, V8HImode, V4SImode are Altivec only, but possibly do VSX loads
2284 and stores. */
2285 if (TARGET_ALTIVEC)
2287 rs6000_vector_unit[V4SImode] = VECTOR_ALTIVEC;
2288 rs6000_vector_unit[V8HImode] = VECTOR_ALTIVEC;
2289 rs6000_vector_unit[V16QImode] = VECTOR_ALTIVEC;
2290 rs6000_vector_align[V4SImode] = align32;
2291 rs6000_vector_align[V8HImode] = align32;
2292 rs6000_vector_align[V16QImode] = align32;
2294 if (TARGET_VSX)
2296 rs6000_vector_mem[V4SImode] = VECTOR_VSX;
2297 rs6000_vector_mem[V8HImode] = VECTOR_VSX;
2298 rs6000_vector_mem[V16QImode] = VECTOR_VSX;
2300 else
2302 rs6000_vector_mem[V4SImode] = VECTOR_ALTIVEC;
2303 rs6000_vector_mem[V8HImode] = VECTOR_ALTIVEC;
2304 rs6000_vector_mem[V16QImode] = VECTOR_ALTIVEC;
2308 /* V2DImode, full mode depends on ISA 2.07 vector mode. Allow under VSX to
2309 do insert/splat/extract. Altivec doesn't have 64-bit integer support. */
2310 if (TARGET_VSX)
2312 rs6000_vector_mem[V2DImode] = VECTOR_VSX;
2313 rs6000_vector_unit[V2DImode]
2314 = (TARGET_P8_VECTOR) ? VECTOR_P8_VECTOR : VECTOR_NONE;
2315 rs6000_vector_align[V2DImode] = align64;
2318 /* DFmode, see if we want to use the VSX unit. */
2319 if (TARGET_VSX && TARGET_VSX_SCALAR_DOUBLE)
2321 rs6000_vector_unit[DFmode] = VECTOR_VSX;
2322 rs6000_vector_mem[DFmode]
2323 = (TARGET_VSX_SCALAR_MEMORY ? VECTOR_VSX : VECTOR_NONE);
2324 rs6000_vector_align[DFmode] = align64;
2327 /* Allow TImode in VSX register and set the VSX memory macros. */
2328 if (TARGET_VSX && TARGET_VSX_TIMODE)
2330 rs6000_vector_mem[TImode] = VECTOR_VSX;
2331 rs6000_vector_align[TImode] = align64;
2334 /* TODO add SPE and paired floating point vector support. */
2336 /* Register class constraints for the constraints that depend on compile
2337 switches. */
2338 if (TARGET_HARD_FLOAT && TARGET_FPRS)
2339 rs6000_constraints[RS6000_CONSTRAINT_f] = FLOAT_REGS;
2341 if (TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT)
2342 rs6000_constraints[RS6000_CONSTRAINT_d] = FLOAT_REGS;
2344 if (TARGET_VSX)
2346 /* At present, we just use VSX_REGS, but we have different constraints
2347 based on the use, in case we want to fine tune the default register
2348 class used. wa = any VSX register, wf = register class to use for
2349 V4SF, wd = register class to use for V2DF, and ws = register classs to
2350 use for DF scalars. */
2351 rs6000_constraints[RS6000_CONSTRAINT_wa] = VSX_REGS;
2352 rs6000_constraints[RS6000_CONSTRAINT_wf] = VSX_REGS;
2353 rs6000_constraints[RS6000_CONSTRAINT_wd] = VSX_REGS;
2354 rs6000_constraints[RS6000_CONSTRAINT_ws] = (TARGET_VSX_SCALAR_MEMORY
2355 ? VSX_REGS
2356 : FLOAT_REGS);
2357 if (TARGET_VSX_TIMODE)
2358 rs6000_constraints[RS6000_CONSTRAINT_wt] = VSX_REGS;
2361 /* Add conditional constraints based on various options, to allow us to
2362 collapse multiple insn patterns. */
2363 if (TARGET_ALTIVEC)
2364 rs6000_constraints[RS6000_CONSTRAINT_v] = ALTIVEC_REGS;
2366 if (TARGET_MFPGPR)
2367 rs6000_constraints[RS6000_CONSTRAINT_wg] = FLOAT_REGS;
2369 if (TARGET_LFIWAX)
2370 rs6000_constraints[RS6000_CONSTRAINT_wl] = FLOAT_REGS;
2372 if (TARGET_DIRECT_MOVE)
2373 rs6000_constraints[RS6000_CONSTRAINT_wm] = VSX_REGS;
2375 if (TARGET_POWERPC64)
2376 rs6000_constraints[RS6000_CONSTRAINT_wr] = GENERAL_REGS;
2378 if (TARGET_P8_VECTOR)
2379 rs6000_constraints[RS6000_CONSTRAINT_wv] = ALTIVEC_REGS;
2381 if (TARGET_STFIWX)
2382 rs6000_constraints[RS6000_CONSTRAINT_wx] = FLOAT_REGS;
2384 if (TARGET_LFIWZX)
2385 rs6000_constraints[RS6000_CONSTRAINT_wz] = FLOAT_REGS;
2387 /* Setup the direct move combinations. */
2388 for (m = 0; m < NUM_MACHINE_MODES; ++m)
2390 reload_fpr_gpr[m] = CODE_FOR_nothing;
2391 reload_gpr_vsx[m] = CODE_FOR_nothing;
2392 reload_vsx_gpr[m] = CODE_FOR_nothing;
2395 /* Set up the reload helper and direct move functions. */
2396 if (TARGET_VSX || TARGET_ALTIVEC)
2398 if (TARGET_64BIT)
2400 rs6000_vector_reload[V16QImode][0] = CODE_FOR_reload_v16qi_di_store;
2401 rs6000_vector_reload[V16QImode][1] = CODE_FOR_reload_v16qi_di_load;
2402 rs6000_vector_reload[V8HImode][0] = CODE_FOR_reload_v8hi_di_store;
2403 rs6000_vector_reload[V8HImode][1] = CODE_FOR_reload_v8hi_di_load;
2404 rs6000_vector_reload[V4SImode][0] = CODE_FOR_reload_v4si_di_store;
2405 rs6000_vector_reload[V4SImode][1] = CODE_FOR_reload_v4si_di_load;
2406 rs6000_vector_reload[V2DImode][0] = CODE_FOR_reload_v2di_di_store;
2407 rs6000_vector_reload[V2DImode][1] = CODE_FOR_reload_v2di_di_load;
2408 rs6000_vector_reload[V4SFmode][0] = CODE_FOR_reload_v4sf_di_store;
2409 rs6000_vector_reload[V4SFmode][1] = CODE_FOR_reload_v4sf_di_load;
2410 rs6000_vector_reload[V2DFmode][0] = CODE_FOR_reload_v2df_di_store;
2411 rs6000_vector_reload[V2DFmode][1] = CODE_FOR_reload_v2df_di_load;
2412 if (TARGET_VSX && TARGET_VSX_SCALAR_MEMORY)
2414 rs6000_vector_reload[DFmode][0] = CODE_FOR_reload_df_di_store;
2415 rs6000_vector_reload[DFmode][1] = CODE_FOR_reload_df_di_load;
2416 rs6000_vector_reload[DDmode][0] = CODE_FOR_reload_dd_di_store;
2417 rs6000_vector_reload[DDmode][1] = CODE_FOR_reload_dd_di_load;
2419 if (TARGET_P8_VECTOR)
2421 rs6000_vector_reload[SFmode][0] = CODE_FOR_reload_sf_di_store;
2422 rs6000_vector_reload[SFmode][1] = CODE_FOR_reload_sf_di_load;
2423 rs6000_vector_reload[SDmode][0] = CODE_FOR_reload_sd_di_store;
2424 rs6000_vector_reload[SDmode][1] = CODE_FOR_reload_sd_di_load;
2426 if (TARGET_VSX_TIMODE)
2428 rs6000_vector_reload[TImode][0] = CODE_FOR_reload_ti_di_store;
2429 rs6000_vector_reload[TImode][1] = CODE_FOR_reload_ti_di_load;
2431 if (TARGET_DIRECT_MOVE)
2433 if (TARGET_POWERPC64)
2435 reload_gpr_vsx[TImode] = CODE_FOR_reload_gpr_from_vsxti;
2436 reload_gpr_vsx[V2DFmode] = CODE_FOR_reload_gpr_from_vsxv2df;
2437 reload_gpr_vsx[V2DImode] = CODE_FOR_reload_gpr_from_vsxv2di;
2438 reload_gpr_vsx[V4SFmode] = CODE_FOR_reload_gpr_from_vsxv4sf;
2439 reload_gpr_vsx[V4SImode] = CODE_FOR_reload_gpr_from_vsxv4si;
2440 reload_gpr_vsx[V8HImode] = CODE_FOR_reload_gpr_from_vsxv8hi;
2441 reload_gpr_vsx[V16QImode] = CODE_FOR_reload_gpr_from_vsxv16qi;
2442 reload_gpr_vsx[SFmode] = CODE_FOR_reload_gpr_from_vsxsf;
2444 reload_vsx_gpr[TImode] = CODE_FOR_reload_vsx_from_gprti;
2445 reload_vsx_gpr[V2DFmode] = CODE_FOR_reload_vsx_from_gprv2df;
2446 reload_vsx_gpr[V2DImode] = CODE_FOR_reload_vsx_from_gprv2di;
2447 reload_vsx_gpr[V4SFmode] = CODE_FOR_reload_vsx_from_gprv4sf;
2448 reload_vsx_gpr[V4SImode] = CODE_FOR_reload_vsx_from_gprv4si;
2449 reload_vsx_gpr[V8HImode] = CODE_FOR_reload_vsx_from_gprv8hi;
2450 reload_vsx_gpr[V16QImode] = CODE_FOR_reload_vsx_from_gprv16qi;
2451 reload_vsx_gpr[SFmode] = CODE_FOR_reload_vsx_from_gprsf;
2453 else
2455 reload_fpr_gpr[DImode] = CODE_FOR_reload_fpr_from_gprdi;
2456 reload_fpr_gpr[DDmode] = CODE_FOR_reload_fpr_from_gprdd;
2457 reload_fpr_gpr[DFmode] = CODE_FOR_reload_fpr_from_gprdf;
2461 else
2463 rs6000_vector_reload[V16QImode][0] = CODE_FOR_reload_v16qi_si_store;
2464 rs6000_vector_reload[V16QImode][1] = CODE_FOR_reload_v16qi_si_load;
2465 rs6000_vector_reload[V8HImode][0] = CODE_FOR_reload_v8hi_si_store;
2466 rs6000_vector_reload[V8HImode][1] = CODE_FOR_reload_v8hi_si_load;
2467 rs6000_vector_reload[V4SImode][0] = CODE_FOR_reload_v4si_si_store;
2468 rs6000_vector_reload[V4SImode][1] = CODE_FOR_reload_v4si_si_load;
2469 rs6000_vector_reload[V2DImode][0] = CODE_FOR_reload_v2di_si_store;
2470 rs6000_vector_reload[V2DImode][1] = CODE_FOR_reload_v2di_si_load;
2471 rs6000_vector_reload[V4SFmode][0] = CODE_FOR_reload_v4sf_si_store;
2472 rs6000_vector_reload[V4SFmode][1] = CODE_FOR_reload_v4sf_si_load;
2473 rs6000_vector_reload[V2DFmode][0] = CODE_FOR_reload_v2df_si_store;
2474 rs6000_vector_reload[V2DFmode][1] = CODE_FOR_reload_v2df_si_load;
2475 if (TARGET_VSX && TARGET_VSX_SCALAR_MEMORY)
2477 rs6000_vector_reload[DFmode][0] = CODE_FOR_reload_df_si_store;
2478 rs6000_vector_reload[DFmode][1] = CODE_FOR_reload_df_si_load;
2479 rs6000_vector_reload[DDmode][0] = CODE_FOR_reload_dd_si_store;
2480 rs6000_vector_reload[DDmode][1] = CODE_FOR_reload_dd_si_load;
2482 if (TARGET_P8_VECTOR)
2484 rs6000_vector_reload[SFmode][0] = CODE_FOR_reload_sf_si_store;
2485 rs6000_vector_reload[SFmode][1] = CODE_FOR_reload_sf_si_load;
2486 rs6000_vector_reload[SDmode][0] = CODE_FOR_reload_sd_si_store;
2487 rs6000_vector_reload[SDmode][1] = CODE_FOR_reload_sd_si_load;
2489 if (TARGET_VSX_TIMODE)
2491 rs6000_vector_reload[TImode][0] = CODE_FOR_reload_ti_si_store;
2492 rs6000_vector_reload[TImode][1] = CODE_FOR_reload_ti_si_load;
2497 /* Precalculate HARD_REGNO_NREGS. */
2498 for (r = 0; r < FIRST_PSEUDO_REGISTER; ++r)
2499 for (m = 0; m < NUM_MACHINE_MODES; ++m)
2500 rs6000_hard_regno_nregs[m][r]
2501 = rs6000_hard_regno_nregs_internal (r, (enum machine_mode)m);
2503 /* Precalculate HARD_REGNO_MODE_OK. */
2504 for (r = 0; r < FIRST_PSEUDO_REGISTER; ++r)
2505 for (m = 0; m < NUM_MACHINE_MODES; ++m)
2506 if (rs6000_hard_regno_mode_ok (r, (enum machine_mode)m))
2507 rs6000_hard_regno_mode_ok_p[m][r] = true;
2509 /* Precalculate CLASS_MAX_NREGS sizes. */
2510 for (c = 0; c < LIM_REG_CLASSES; ++c)
2512 int reg_size;
2514 if (TARGET_VSX && VSX_REG_CLASS_P (c))
2515 reg_size = UNITS_PER_VSX_WORD;
2517 else if (c == ALTIVEC_REGS)
2518 reg_size = UNITS_PER_ALTIVEC_WORD;
2520 else if (c == FLOAT_REGS)
2521 reg_size = UNITS_PER_FP_WORD;
2523 else
2524 reg_size = UNITS_PER_WORD;
2526 for (m = 0; m < NUM_MACHINE_MODES; ++m)
2528 int reg_size2 = reg_size;
2530 /* TFmode/TDmode always takes 2 registers, even in VSX. */
2531 if (TARGET_VSX && VSX_REG_CLASS_P (c)
2532 && (m == TDmode || m == TFmode))
2533 reg_size2 = UNITS_PER_FP_WORD;
2535 rs6000_class_max_nregs[m][c]
2536 = (GET_MODE_SIZE (m) + reg_size2 - 1) / reg_size2;
2540 if (TARGET_E500_DOUBLE)
2541 rs6000_class_max_nregs[DFmode][GENERAL_REGS] = 1;
2543 /* Calculate which modes to automatically generate code to use a the
2544 reciprocal divide and square root instructions. In the future, possibly
2545 automatically generate the instructions even if the user did not specify
2546 -mrecip. The older machines double precision reciprocal sqrt estimate is
2547 not accurate enough. */
2548 memset (rs6000_recip_bits, 0, sizeof (rs6000_recip_bits));
2549 if (TARGET_FRES)
2550 rs6000_recip_bits[SFmode] = RS6000_RECIP_MASK_HAVE_RE;
2551 if (TARGET_FRE)
2552 rs6000_recip_bits[DFmode] = RS6000_RECIP_MASK_HAVE_RE;
2553 if (VECTOR_UNIT_ALTIVEC_OR_VSX_P (V4SFmode))
2554 rs6000_recip_bits[V4SFmode] = RS6000_RECIP_MASK_HAVE_RE;
2555 if (VECTOR_UNIT_VSX_P (V2DFmode))
2556 rs6000_recip_bits[V2DFmode] = RS6000_RECIP_MASK_HAVE_RE;
2558 if (TARGET_FRSQRTES)
2559 rs6000_recip_bits[SFmode] |= RS6000_RECIP_MASK_HAVE_RSQRTE;
2560 if (TARGET_FRSQRTE)
2561 rs6000_recip_bits[DFmode] |= RS6000_RECIP_MASK_HAVE_RSQRTE;
2562 if (VECTOR_UNIT_ALTIVEC_OR_VSX_P (V4SFmode))
2563 rs6000_recip_bits[V4SFmode] |= RS6000_RECIP_MASK_HAVE_RSQRTE;
2564 if (VECTOR_UNIT_VSX_P (V2DFmode))
2565 rs6000_recip_bits[V2DFmode] |= RS6000_RECIP_MASK_HAVE_RSQRTE;
2567 if (rs6000_recip_control)
2569 if (!flag_finite_math_only)
2570 warning (0, "-mrecip requires -ffinite-math or -ffast-math");
2571 if (flag_trapping_math)
2572 warning (0, "-mrecip requires -fno-trapping-math or -ffast-math");
2573 if (!flag_reciprocal_math)
2574 warning (0, "-mrecip requires -freciprocal-math or -ffast-math");
2575 if (flag_finite_math_only && !flag_trapping_math && flag_reciprocal_math)
2577 if (RS6000_RECIP_HAVE_RE_P (SFmode)
2578 && (rs6000_recip_control & RECIP_SF_DIV) != 0)
2579 rs6000_recip_bits[SFmode] |= RS6000_RECIP_MASK_AUTO_RE;
2581 if (RS6000_RECIP_HAVE_RE_P (DFmode)
2582 && (rs6000_recip_control & RECIP_DF_DIV) != 0)
2583 rs6000_recip_bits[DFmode] |= RS6000_RECIP_MASK_AUTO_RE;
2585 if (RS6000_RECIP_HAVE_RE_P (V4SFmode)
2586 && (rs6000_recip_control & RECIP_V4SF_DIV) != 0)
2587 rs6000_recip_bits[V4SFmode] |= RS6000_RECIP_MASK_AUTO_RE;
2589 if (RS6000_RECIP_HAVE_RE_P (V2DFmode)
2590 && (rs6000_recip_control & RECIP_V2DF_DIV) != 0)
2591 rs6000_recip_bits[V2DFmode] |= RS6000_RECIP_MASK_AUTO_RE;
2593 if (RS6000_RECIP_HAVE_RSQRTE_P (SFmode)
2594 && (rs6000_recip_control & RECIP_SF_RSQRT) != 0)
2595 rs6000_recip_bits[SFmode] |= RS6000_RECIP_MASK_AUTO_RSQRTE;
2597 if (RS6000_RECIP_HAVE_RSQRTE_P (DFmode)
2598 && (rs6000_recip_control & RECIP_DF_RSQRT) != 0)
2599 rs6000_recip_bits[DFmode] |= RS6000_RECIP_MASK_AUTO_RSQRTE;
2601 if (RS6000_RECIP_HAVE_RSQRTE_P (V4SFmode)
2602 && (rs6000_recip_control & RECIP_V4SF_RSQRT) != 0)
2603 rs6000_recip_bits[V4SFmode] |= RS6000_RECIP_MASK_AUTO_RSQRTE;
2605 if (RS6000_RECIP_HAVE_RSQRTE_P (V2DFmode)
2606 && (rs6000_recip_control & RECIP_V2DF_RSQRT) != 0)
2607 rs6000_recip_bits[V2DFmode] |= RS6000_RECIP_MASK_AUTO_RSQRTE;
2611 if (global_init_p || TARGET_DEBUG_TARGET)
2613 if (TARGET_DEBUG_REG)
2614 rs6000_debug_reg_global ();
2616 if (TARGET_DEBUG_COST || TARGET_DEBUG_REG)
2617 fprintf (stderr,
2618 "SImode variable mult cost = %d\n"
2619 "SImode constant mult cost = %d\n"
2620 "SImode short constant mult cost = %d\n"
2621 "DImode multipliciation cost = %d\n"
2622 "SImode division cost = %d\n"
2623 "DImode division cost = %d\n"
2624 "Simple fp operation cost = %d\n"
2625 "DFmode multiplication cost = %d\n"
2626 "SFmode division cost = %d\n"
2627 "DFmode division cost = %d\n"
2628 "cache line size = %d\n"
2629 "l1 cache size = %d\n"
2630 "l2 cache size = %d\n"
2631 "simultaneous prefetches = %d\n"
2632 "\n",
2633 rs6000_cost->mulsi,
2634 rs6000_cost->mulsi_const,
2635 rs6000_cost->mulsi_const9,
2636 rs6000_cost->muldi,
2637 rs6000_cost->divsi,
2638 rs6000_cost->divdi,
2639 rs6000_cost->fp,
2640 rs6000_cost->dmul,
2641 rs6000_cost->sdiv,
2642 rs6000_cost->ddiv,
2643 rs6000_cost->cache_line_size,
2644 rs6000_cost->l1_cache_size,
2645 rs6000_cost->l2_cache_size,
2646 rs6000_cost->simultaneous_prefetches);
2650 #if TARGET_MACHO
2651 /* The Darwin version of SUBTARGET_OVERRIDE_OPTIONS. */
2653 static void
2654 darwin_rs6000_override_options (void)
2656 /* The Darwin ABI always includes AltiVec, can't be (validly) turned
2657 off. */
2658 rs6000_altivec_abi = 1;
2659 TARGET_ALTIVEC_VRSAVE = 1;
2660 rs6000_current_abi = ABI_DARWIN;
2662 if (DEFAULT_ABI == ABI_DARWIN
2663 && TARGET_64BIT)
2664 darwin_one_byte_bool = 1;
2666 if (TARGET_64BIT && ! TARGET_POWERPC64)
2668 rs6000_isa_flags |= OPTION_MASK_POWERPC64;
2669 warning (0, "-m64 requires PowerPC64 architecture, enabling");
2671 if (flag_mkernel)
2673 rs6000_default_long_calls = 1;
2674 rs6000_isa_flags |= OPTION_MASK_SOFT_FLOAT;
2677 /* Make -m64 imply -maltivec. Darwin's 64-bit ABI includes
2678 Altivec. */
2679 if (!flag_mkernel && !flag_apple_kext
2680 && TARGET_64BIT
2681 && ! (rs6000_isa_flags_explicit & OPTION_MASK_ALTIVEC))
2682 rs6000_isa_flags |= OPTION_MASK_ALTIVEC;
2684 /* Unless the user (not the configurer) has explicitly overridden
2685 it with -mcpu=G3 or -mno-altivec, then 10.5+ targets default to
2686 G4 unless targeting the kernel. */
2687 if (!flag_mkernel
2688 && !flag_apple_kext
2689 && strverscmp (darwin_macosx_version_min, "10.5") >= 0
2690 && ! (rs6000_isa_flags_explicit & OPTION_MASK_ALTIVEC)
2691 && ! global_options_set.x_rs6000_cpu_index)
2693 rs6000_isa_flags |= OPTION_MASK_ALTIVEC;
2696 #endif
2698 /* If not otherwise specified by a target, make 'long double' equivalent to
2699 'double'. */
2701 #ifndef RS6000_DEFAULT_LONG_DOUBLE_SIZE
2702 #define RS6000_DEFAULT_LONG_DOUBLE_SIZE 64
2703 #endif
2705 /* Return the builtin mask of the various options used that could affect which
2706 builtins were used. In the past we used target_flags, but we've run out of
2707 bits, and some options like SPE and PAIRED are no longer in
2708 target_flags. */
2710 HOST_WIDE_INT
2711 rs6000_builtin_mask_calculate (void)
2713 return (((TARGET_ALTIVEC) ? RS6000_BTM_ALTIVEC : 0)
2714 | ((TARGET_VSX) ? RS6000_BTM_VSX : 0)
2715 | ((TARGET_SPE) ? RS6000_BTM_SPE : 0)
2716 | ((TARGET_PAIRED_FLOAT) ? RS6000_BTM_PAIRED : 0)
2717 | ((TARGET_FRE) ? RS6000_BTM_FRE : 0)
2718 | ((TARGET_FRES) ? RS6000_BTM_FRES : 0)
2719 | ((TARGET_FRSQRTE) ? RS6000_BTM_FRSQRTE : 0)
2720 | ((TARGET_FRSQRTES) ? RS6000_BTM_FRSQRTES : 0)
2721 | ((TARGET_POPCNTD) ? RS6000_BTM_POPCNTD : 0)
2722 | ((rs6000_cpu == PROCESSOR_CELL) ? RS6000_BTM_CELL : 0)
2723 | ((TARGET_P8_VECTOR) ? RS6000_BTM_P8_VECTOR : 0)
2724 | ((TARGET_CRYPTO) ? RS6000_BTM_CRYPTO : 0)
2725 | ((TARGET_HTM) ? RS6000_BTM_HTM : 0));
2728 /* Override command line options. Mostly we process the processor type and
2729 sometimes adjust other TARGET_ options. */
2731 static bool
2732 rs6000_option_override_internal (bool global_init_p)
2734 bool ret = true;
2735 bool have_cpu = false;
2737 /* The default cpu requested at configure time, if any. */
2738 const char *implicit_cpu = OPTION_TARGET_CPU_DEFAULT;
2740 HOST_WIDE_INT set_masks;
2741 int cpu_index;
2742 int tune_index;
2743 struct cl_target_option *main_target_opt
2744 = ((global_init_p || target_option_default_node == NULL)
2745 ? NULL : TREE_TARGET_OPTION (target_option_default_node));
2747 /* On 64-bit Darwin, power alignment is ABI-incompatible with some C
2748 library functions, so warn about it. The flag may be useful for
2749 performance studies from time to time though, so don't disable it
2750 entirely. */
2751 if (global_options_set.x_rs6000_alignment_flags
2752 && rs6000_alignment_flags == MASK_ALIGN_POWER
2753 && DEFAULT_ABI == ABI_DARWIN
2754 && TARGET_64BIT)
2755 warning (0, "-malign-power is not supported for 64-bit Darwin;"
2756 " it is incompatible with the installed C and C++ libraries");
2758 /* Numerous experiment shows that IRA based loop pressure
2759 calculation works better for RTL loop invariant motion on targets
2760 with enough (>= 32) registers. It is an expensive optimization.
2761 So it is on only for peak performance. */
2762 if (optimize >= 3 && global_init_p)
2763 flag_ira_loop_pressure = 1;
2765 /* Set the pointer size. */
2766 if (TARGET_64BIT)
2768 rs6000_pmode = (int)DImode;
2769 rs6000_pointer_size = 64;
2771 else
2773 rs6000_pmode = (int)SImode;
2774 rs6000_pointer_size = 32;
2777 /* Some OSs don't support saving the high part of 64-bit registers on context
2778 switch. Other OSs don't support saving Altivec registers. On those OSs,
2779 we don't touch the OPTION_MASK_POWERPC64 or OPTION_MASK_ALTIVEC settings;
2780 if the user wants either, the user must explicitly specify them and we
2781 won't interfere with the user's specification. */
2783 set_masks = POWERPC_MASKS;
2784 #ifdef OS_MISSING_POWERPC64
2785 if (OS_MISSING_POWERPC64)
2786 set_masks &= ~OPTION_MASK_POWERPC64;
2787 #endif
2788 #ifdef OS_MISSING_ALTIVEC
2789 if (OS_MISSING_ALTIVEC)
2790 set_masks &= ~(OPTION_MASK_ALTIVEC | OPTION_MASK_VSX);
2791 #endif
2793 /* Don't override by the processor default if given explicitly. */
2794 set_masks &= ~rs6000_isa_flags_explicit;
2796 /* Process the -mcpu=<xxx> and -mtune=<xxx> argument. If the user changed
2797 the cpu in a target attribute or pragma, but did not specify a tuning
2798 option, use the cpu for the tuning option rather than the option specified
2799 with -mtune on the command line. Process a '--with-cpu' configuration
2800 request as an implicit --cpu. */
2801 if (rs6000_cpu_index >= 0)
2803 cpu_index = rs6000_cpu_index;
2804 have_cpu = true;
2806 else if (main_target_opt != NULL && main_target_opt->x_rs6000_cpu_index >= 0)
2808 rs6000_cpu_index = cpu_index = main_target_opt->x_rs6000_cpu_index;
2809 have_cpu = true;
2811 else if (implicit_cpu)
2813 rs6000_cpu_index = cpu_index = rs6000_cpu_name_lookup (implicit_cpu);
2814 have_cpu = true;
2816 else
2818 const char *default_cpu = (TARGET_POWERPC64 ? "powerpc64" : "powerpc");
2819 rs6000_cpu_index = cpu_index = rs6000_cpu_name_lookup (default_cpu);
2820 have_cpu = false;
2823 gcc_assert (cpu_index >= 0);
2825 /* If we have a cpu, either through an explicit -mcpu=<xxx> or if the
2826 compiler was configured with --with-cpu=<xxx>, replace all of the ISA bits
2827 with those from the cpu, except for options that were explicitly set. If
2828 we don't have a cpu, do not override the target bits set in
2829 TARGET_DEFAULT. */
2830 if (have_cpu)
2832 rs6000_isa_flags &= ~set_masks;
2833 rs6000_isa_flags |= (processor_target_table[cpu_index].target_enable
2834 & set_masks);
2836 else
2837 rs6000_isa_flags |= (processor_target_table[cpu_index].target_enable
2838 & ~rs6000_isa_flags_explicit);
2840 /* If no -mcpu=<xxx>, inherit any default options that were cleared via
2841 POWERPC_MASKS. Originally, TARGET_DEFAULT was used to initialize
2842 target_flags via the TARGET_DEFAULT_TARGET_FLAGS hook. When we switched
2843 to using rs6000_isa_flags, we need to do the initialization here. */
2844 if (!have_cpu)
2845 rs6000_isa_flags |= (TARGET_DEFAULT & ~rs6000_isa_flags_explicit);
2847 if (rs6000_tune_index >= 0)
2848 tune_index = rs6000_tune_index;
2849 else if (have_cpu)
2850 rs6000_tune_index = tune_index = cpu_index;
2851 else
2853 size_t i;
2854 enum processor_type tune_proc
2855 = (TARGET_POWERPC64 ? PROCESSOR_DEFAULT64 : PROCESSOR_DEFAULT);
2857 tune_index = -1;
2858 for (i = 0; i < ARRAY_SIZE (processor_target_table); i++)
2859 if (processor_target_table[i].processor == tune_proc)
2861 rs6000_tune_index = tune_index = i;
2862 break;
2866 gcc_assert (tune_index >= 0);
2867 rs6000_cpu = processor_target_table[tune_index].processor;
2869 /* Pick defaults for SPE related control flags. Do this early to make sure
2870 that the TARGET_ macros are representative ASAP. */
2872 int spe_capable_cpu =
2873 (rs6000_cpu == PROCESSOR_PPC8540
2874 || rs6000_cpu == PROCESSOR_PPC8548);
2876 if (!global_options_set.x_rs6000_spe_abi)
2877 rs6000_spe_abi = spe_capable_cpu;
2879 if (!global_options_set.x_rs6000_spe)
2880 rs6000_spe = spe_capable_cpu;
2882 if (!global_options_set.x_rs6000_float_gprs)
2883 rs6000_float_gprs =
2884 (rs6000_cpu == PROCESSOR_PPC8540 ? 1
2885 : rs6000_cpu == PROCESSOR_PPC8548 ? 2
2886 : 0);
2889 if (global_options_set.x_rs6000_spe_abi
2890 && rs6000_spe_abi
2891 && !TARGET_SPE_ABI)
2892 error ("not configured for SPE ABI");
2894 if (global_options_set.x_rs6000_spe
2895 && rs6000_spe
2896 && !TARGET_SPE)
2897 error ("not configured for SPE instruction set");
2899 if (main_target_opt != NULL
2900 && ((main_target_opt->x_rs6000_spe_abi != rs6000_spe_abi)
2901 || (main_target_opt->x_rs6000_spe != rs6000_spe)
2902 || (main_target_opt->x_rs6000_float_gprs != rs6000_float_gprs)))
2903 error ("target attribute or pragma changes SPE ABI");
2905 if (rs6000_cpu == PROCESSOR_PPCE300C2 || rs6000_cpu == PROCESSOR_PPCE300C3
2906 || rs6000_cpu == PROCESSOR_PPCE500MC || rs6000_cpu == PROCESSOR_PPCE500MC64
2907 || rs6000_cpu == PROCESSOR_PPCE5500)
2909 if (TARGET_ALTIVEC)
2910 error ("AltiVec not supported in this target");
2911 if (TARGET_SPE)
2912 error ("SPE not supported in this target");
2914 if (rs6000_cpu == PROCESSOR_PPCE6500)
2916 if (TARGET_SPE)
2917 error ("SPE not supported in this target");
2920 /* Disable Cell microcode if we are optimizing for the Cell
2921 and not optimizing for size. */
2922 if (rs6000_gen_cell_microcode == -1)
2923 rs6000_gen_cell_microcode = !(rs6000_cpu == PROCESSOR_CELL
2924 && !optimize_size);
2926 /* If we are optimizing big endian systems for space and it's OK to
2927 use instructions that would be microcoded on the Cell, use the
2928 load/store multiple and string instructions. */
2929 if (BYTES_BIG_ENDIAN && optimize_size && rs6000_gen_cell_microcode)
2930 rs6000_isa_flags |= ~rs6000_isa_flags_explicit & (OPTION_MASK_MULTIPLE
2931 | OPTION_MASK_STRING);
2933 /* Don't allow -mmultiple or -mstring on little endian systems
2934 unless the cpu is a 750, because the hardware doesn't support the
2935 instructions used in little endian mode, and causes an alignment
2936 trap. The 750 does not cause an alignment trap (except when the
2937 target is unaligned). */
2939 if (!BYTES_BIG_ENDIAN && rs6000_cpu != PROCESSOR_PPC750)
2941 if (TARGET_MULTIPLE)
2943 rs6000_isa_flags &= ~OPTION_MASK_MULTIPLE;
2944 if ((rs6000_isa_flags_explicit & OPTION_MASK_MULTIPLE) != 0)
2945 warning (0, "-mmultiple is not supported on little endian systems");
2948 if (TARGET_STRING)
2950 rs6000_isa_flags &= ~OPTION_MASK_STRING;
2951 if ((rs6000_isa_flags_explicit & OPTION_MASK_STRING) != 0)
2952 warning (0, "-mstring is not supported on little endian systems");
2956 /* Add some warnings for VSX. */
2957 if (TARGET_VSX)
2959 const char *msg = NULL;
2960 if (!TARGET_HARD_FLOAT || !TARGET_FPRS
2961 || !TARGET_SINGLE_FLOAT || !TARGET_DOUBLE_FLOAT)
2963 if (rs6000_isa_flags_explicit & OPTION_MASK_VSX)
2964 msg = N_("-mvsx requires hardware floating point");
2965 else
2967 rs6000_isa_flags &= ~ OPTION_MASK_VSX;
2968 rs6000_isa_flags_explicit |= OPTION_MASK_VSX;
2971 else if (TARGET_PAIRED_FLOAT)
2972 msg = N_("-mvsx and -mpaired are incompatible");
2973 /* The hardware will allow VSX and little endian, but until we make sure
2974 things like vector select, etc. work don't allow VSX on little endian
2975 systems at this point. */
2976 else if (!BYTES_BIG_ENDIAN)
2977 msg = N_("-mvsx used with little endian code");
2978 else if (TARGET_AVOID_XFORM > 0)
2979 msg = N_("-mvsx needs indexed addressing");
2980 else if (!TARGET_ALTIVEC && (rs6000_isa_flags_explicit
2981 & OPTION_MASK_ALTIVEC))
2983 if (rs6000_isa_flags_explicit & OPTION_MASK_VSX)
2984 msg = N_("-mvsx and -mno-altivec are incompatible");
2985 else
2986 msg = N_("-mno-altivec disables vsx");
2989 if (msg)
2991 warning (0, msg);
2992 rs6000_isa_flags &= ~ OPTION_MASK_VSX;
2993 rs6000_isa_flags_explicit |= OPTION_MASK_VSX;
2997 /* If hard-float/altivec/vsx were explicitly turned off then don't allow
2998 the -mcpu setting to enable options that conflict. */
2999 if ((!TARGET_HARD_FLOAT || !TARGET_ALTIVEC || !TARGET_VSX)
3000 && (rs6000_isa_flags_explicit & (OPTION_MASK_SOFT_FLOAT
3001 | OPTION_MASK_ALTIVEC
3002 | OPTION_MASK_VSX)) != 0)
3003 rs6000_isa_flags &= ~((OPTION_MASK_P8_VECTOR | OPTION_MASK_CRYPTO
3004 | OPTION_MASK_DIRECT_MOVE)
3005 & ~rs6000_isa_flags_explicit);
3007 if (TARGET_DEBUG_REG || TARGET_DEBUG_TARGET)
3008 rs6000_print_isa_options (stderr, 0, "before defaults", rs6000_isa_flags);
3010 /* For the newer switches (vsx, dfp, etc.) set some of the older options,
3011 unless the user explicitly used the -mno-<option> to disable the code. */
3012 if (TARGET_P8_VECTOR || TARGET_DIRECT_MOVE || TARGET_CRYPTO)
3013 rs6000_isa_flags |= (ISA_2_7_MASKS_SERVER & ~rs6000_isa_flags_explicit);
3014 else if (TARGET_VSX)
3015 rs6000_isa_flags |= (ISA_2_6_MASKS_SERVER & ~rs6000_isa_flags_explicit);
3016 else if (TARGET_POPCNTD)
3017 rs6000_isa_flags |= (ISA_2_6_MASKS_EMBEDDED & ~rs6000_isa_flags_explicit);
3018 else if (TARGET_DFP)
3019 rs6000_isa_flags |= (ISA_2_5_MASKS_SERVER & ~rs6000_isa_flags_explicit);
3020 else if (TARGET_CMPB)
3021 rs6000_isa_flags |= (ISA_2_5_MASKS_EMBEDDED & ~rs6000_isa_flags_explicit);
3022 else if (TARGET_FPRND)
3023 rs6000_isa_flags |= (ISA_2_4_MASKS & ~rs6000_isa_flags_explicit);
3024 else if (TARGET_POPCNTB)
3025 rs6000_isa_flags |= (ISA_2_2_MASKS & ~rs6000_isa_flags_explicit);
3026 else if (TARGET_ALTIVEC)
3027 rs6000_isa_flags |= (OPTION_MASK_PPC_GFXOPT & ~rs6000_isa_flags_explicit);
3029 if (TARGET_CRYPTO && !TARGET_ALTIVEC)
3031 if (rs6000_isa_flags_explicit & OPTION_MASK_CRYPTO)
3032 error ("-mcrypto requires -maltivec");
3033 rs6000_isa_flags &= ~OPTION_MASK_CRYPTO;
3036 if (TARGET_DIRECT_MOVE && !TARGET_VSX)
3038 if (rs6000_isa_flags_explicit & OPTION_MASK_DIRECT_MOVE)
3039 error ("-mdirect-move requires -mvsx");
3040 rs6000_isa_flags &= ~OPTION_MASK_DIRECT_MOVE;
3043 if (TARGET_P8_VECTOR && !TARGET_ALTIVEC)
3045 if (rs6000_isa_flags_explicit & OPTION_MASK_P8_VECTOR)
3046 error ("-mpower8-vector requires -maltivec");
3047 rs6000_isa_flags &= ~OPTION_MASK_P8_VECTOR;
3050 if (TARGET_P8_VECTOR && !TARGET_VSX)
3052 if (rs6000_isa_flags_explicit & OPTION_MASK_P8_VECTOR)
3053 error ("-mpower8-vector requires -mvsx");
3054 rs6000_isa_flags &= ~OPTION_MASK_P8_VECTOR;
3057 if (TARGET_VSX_TIMODE && !TARGET_VSX)
3059 if (rs6000_isa_flags_explicit & OPTION_MASK_VSX_TIMODE)
3060 error ("-mvsx-timode requires -mvsx");
3061 rs6000_isa_flags &= ~OPTION_MASK_VSX_TIMODE;
3064 /* The quad memory instructions only works in 64-bit mode. In 32-bit mode,
3065 silently turn off quad memory mode. */
3066 if (TARGET_QUAD_MEMORY && !TARGET_POWERPC64)
3068 if ((rs6000_isa_flags_explicit & OPTION_MASK_QUAD_MEMORY) != 0)
3069 warning (0, N_("-mquad-memory requires 64-bit mode"));
3071 rs6000_isa_flags &= ~OPTION_MASK_QUAD_MEMORY;
3074 /* Enable power8 fusion if we are tuning for power8, even if we aren't
3075 generating power8 instructions. */
3076 if (!(rs6000_isa_flags_explicit & OPTION_MASK_P8_FUSION))
3077 rs6000_isa_flags |= (processor_target_table[tune_index].target_enable
3078 & OPTION_MASK_P8_FUSION);
3080 /* Power8 does not fuse sign extended loads with the addis. If we are
3081 optimizing at high levels for speed, convert a sign extended load into a
3082 zero extending load, and an explicit sign extension. */
3083 if (TARGET_P8_FUSION
3084 && !(rs6000_isa_flags_explicit & OPTION_MASK_P8_FUSION_SIGN)
3085 && optimize_function_for_speed_p (cfun)
3086 && optimize >= 3)
3087 rs6000_isa_flags |= OPTION_MASK_P8_FUSION_SIGN;
3089 if (TARGET_DEBUG_REG || TARGET_DEBUG_TARGET)
3090 rs6000_print_isa_options (stderr, 0, "after defaults", rs6000_isa_flags);
3092 /* E500mc does "better" if we inline more aggressively. Respect the
3093 user's opinion, though. */
3094 if (rs6000_block_move_inline_limit == 0
3095 && (rs6000_cpu == PROCESSOR_PPCE500MC
3096 || rs6000_cpu == PROCESSOR_PPCE500MC64
3097 || rs6000_cpu == PROCESSOR_PPCE5500
3098 || rs6000_cpu == PROCESSOR_PPCE6500))
3099 rs6000_block_move_inline_limit = 128;
3101 /* store_one_arg depends on expand_block_move to handle at least the
3102 size of reg_parm_stack_space. */
3103 if (rs6000_block_move_inline_limit < (TARGET_POWERPC64 ? 64 : 32))
3104 rs6000_block_move_inline_limit = (TARGET_POWERPC64 ? 64 : 32);
3106 if (global_init_p)
3108 /* If the appropriate debug option is enabled, replace the target hooks
3109 with debug versions that call the real version and then prints
3110 debugging information. */
3111 if (TARGET_DEBUG_COST)
3113 targetm.rtx_costs = rs6000_debug_rtx_costs;
3114 targetm.address_cost = rs6000_debug_address_cost;
3115 targetm.sched.adjust_cost = rs6000_debug_adjust_cost;
3118 if (TARGET_DEBUG_ADDR)
3120 targetm.legitimate_address_p = rs6000_debug_legitimate_address_p;
3121 targetm.legitimize_address = rs6000_debug_legitimize_address;
3122 rs6000_secondary_reload_class_ptr
3123 = rs6000_debug_secondary_reload_class;
3124 rs6000_secondary_memory_needed_ptr
3125 = rs6000_debug_secondary_memory_needed;
3126 rs6000_cannot_change_mode_class_ptr
3127 = rs6000_debug_cannot_change_mode_class;
3128 rs6000_preferred_reload_class_ptr
3129 = rs6000_debug_preferred_reload_class;
3130 rs6000_legitimize_reload_address_ptr
3131 = rs6000_debug_legitimize_reload_address;
3132 rs6000_mode_dependent_address_ptr
3133 = rs6000_debug_mode_dependent_address;
3136 if (rs6000_veclibabi_name)
3138 if (strcmp (rs6000_veclibabi_name, "mass") == 0)
3139 rs6000_veclib_handler = rs6000_builtin_vectorized_libmass;
3140 else
3142 error ("unknown vectorization library ABI type (%s) for "
3143 "-mveclibabi= switch", rs6000_veclibabi_name);
3144 ret = false;
3149 if (!global_options_set.x_rs6000_long_double_type_size)
3151 if (main_target_opt != NULL
3152 && (main_target_opt->x_rs6000_long_double_type_size
3153 != RS6000_DEFAULT_LONG_DOUBLE_SIZE))
3154 error ("target attribute or pragma changes long double size");
3155 else
3156 rs6000_long_double_type_size = RS6000_DEFAULT_LONG_DOUBLE_SIZE;
3159 #if !defined (POWERPC_LINUX) && !defined (POWERPC_FREEBSD)
3160 if (!global_options_set.x_rs6000_ieeequad)
3161 rs6000_ieeequad = 1;
3162 #endif
3164 /* Disable VSX and Altivec silently if the user switched cpus to power7 in a
3165 target attribute or pragma which automatically enables both options,
3166 unless the altivec ABI was set. This is set by default for 64-bit, but
3167 not for 32-bit. */
3168 if (main_target_opt != NULL && !main_target_opt->x_rs6000_altivec_abi)
3169 rs6000_isa_flags &= ~((OPTION_MASK_VSX | OPTION_MASK_ALTIVEC)
3170 & ~rs6000_isa_flags_explicit);
3172 /* Enable Altivec ABI for AIX -maltivec. */
3173 if (TARGET_XCOFF && (TARGET_ALTIVEC || TARGET_VSX))
3175 if (main_target_opt != NULL && !main_target_opt->x_rs6000_altivec_abi)
3176 error ("target attribute or pragma changes AltiVec ABI");
3177 else
3178 rs6000_altivec_abi = 1;
3181 /* The AltiVec ABI is the default for PowerPC-64 GNU/Linux. For
3182 PowerPC-32 GNU/Linux, -maltivec implies the AltiVec ABI. It can
3183 be explicitly overridden in either case. */
3184 if (TARGET_ELF)
3186 if (!global_options_set.x_rs6000_altivec_abi
3187 && (TARGET_64BIT || TARGET_ALTIVEC || TARGET_VSX))
3189 if (main_target_opt != NULL &&
3190 !main_target_opt->x_rs6000_altivec_abi)
3191 error ("target attribute or pragma changes AltiVec ABI");
3192 else
3193 rs6000_altivec_abi = 1;
3197 /* Set the Darwin64 ABI as default for 64-bit Darwin.
3198 So far, the only darwin64 targets are also MACH-O. */
3199 if (TARGET_MACHO
3200 && DEFAULT_ABI == ABI_DARWIN
3201 && TARGET_64BIT)
3203 if (main_target_opt != NULL && !main_target_opt->x_rs6000_darwin64_abi)
3204 error ("target attribute or pragma changes darwin64 ABI");
3205 else
3207 rs6000_darwin64_abi = 1;
3208 /* Default to natural alignment, for better performance. */
3209 rs6000_alignment_flags = MASK_ALIGN_NATURAL;
3213 /* Place FP constants in the constant pool instead of TOC
3214 if section anchors enabled. */
3215 if (flag_section_anchors
3216 && !global_options_set.x_TARGET_NO_FP_IN_TOC)
3217 TARGET_NO_FP_IN_TOC = 1;
3219 if (TARGET_DEBUG_REG || TARGET_DEBUG_TARGET)
3220 rs6000_print_isa_options (stderr, 0, "before subtarget", rs6000_isa_flags);
3222 #ifdef SUBTARGET_OVERRIDE_OPTIONS
3223 SUBTARGET_OVERRIDE_OPTIONS;
3224 #endif
3225 #ifdef SUBSUBTARGET_OVERRIDE_OPTIONS
3226 SUBSUBTARGET_OVERRIDE_OPTIONS;
3227 #endif
3228 #ifdef SUB3TARGET_OVERRIDE_OPTIONS
3229 SUB3TARGET_OVERRIDE_OPTIONS;
3230 #endif
3232 if (TARGET_DEBUG_REG || TARGET_DEBUG_TARGET)
3233 rs6000_print_isa_options (stderr, 0, "after subtarget", rs6000_isa_flags);
3235 /* For the E500 family of cores, reset the single/double FP flags to let us
3236 check that they remain constant across attributes or pragmas. Also,
3237 clear a possible request for string instructions, not supported and which
3238 we might have silently queried above for -Os.
3240 For other families, clear ISEL in case it was set implicitly.
3243 switch (rs6000_cpu)
3245 case PROCESSOR_PPC8540:
3246 case PROCESSOR_PPC8548:
3247 case PROCESSOR_PPCE500MC:
3248 case PROCESSOR_PPCE500MC64:
3249 case PROCESSOR_PPCE5500:
3250 case PROCESSOR_PPCE6500:
3252 rs6000_single_float = TARGET_E500_SINGLE || TARGET_E500_DOUBLE;
3253 rs6000_double_float = TARGET_E500_DOUBLE;
3255 rs6000_isa_flags &= ~OPTION_MASK_STRING;
3257 break;
3259 default:
3261 if (have_cpu && !(rs6000_isa_flags_explicit & OPTION_MASK_ISEL))
3262 rs6000_isa_flags &= ~OPTION_MASK_ISEL;
3264 break;
3267 if (main_target_opt)
3269 if (main_target_opt->x_rs6000_single_float != rs6000_single_float)
3270 error ("target attribute or pragma changes single precision floating "
3271 "point");
3272 if (main_target_opt->x_rs6000_double_float != rs6000_double_float)
3273 error ("target attribute or pragma changes double precision floating "
3274 "point");
3277 /* Detect invalid option combinations with E500. */
3278 CHECK_E500_OPTIONS;
3280 rs6000_always_hint = (rs6000_cpu != PROCESSOR_POWER4
3281 && rs6000_cpu != PROCESSOR_POWER5
3282 && rs6000_cpu != PROCESSOR_POWER6
3283 && rs6000_cpu != PROCESSOR_POWER7
3284 && rs6000_cpu != PROCESSOR_POWER8
3285 && rs6000_cpu != PROCESSOR_PPCA2
3286 && rs6000_cpu != PROCESSOR_CELL
3287 && rs6000_cpu != PROCESSOR_PPC476);
3288 rs6000_sched_groups = (rs6000_cpu == PROCESSOR_POWER4
3289 || rs6000_cpu == PROCESSOR_POWER5
3290 || rs6000_cpu == PROCESSOR_POWER7
3291 || rs6000_cpu == PROCESSOR_POWER8);
3292 rs6000_align_branch_targets = (rs6000_cpu == PROCESSOR_POWER4
3293 || rs6000_cpu == PROCESSOR_POWER5
3294 || rs6000_cpu == PROCESSOR_POWER6
3295 || rs6000_cpu == PROCESSOR_POWER7
3296 || rs6000_cpu == PROCESSOR_POWER8
3297 || rs6000_cpu == PROCESSOR_PPCE500MC
3298 || rs6000_cpu == PROCESSOR_PPCE500MC64
3299 || rs6000_cpu == PROCESSOR_PPCE5500
3300 || rs6000_cpu == PROCESSOR_PPCE6500);
3302 /* Allow debug switches to override the above settings. These are set to -1
3303 in rs6000.opt to indicate the user hasn't directly set the switch. */
3304 if (TARGET_ALWAYS_HINT >= 0)
3305 rs6000_always_hint = TARGET_ALWAYS_HINT;
3307 if (TARGET_SCHED_GROUPS >= 0)
3308 rs6000_sched_groups = TARGET_SCHED_GROUPS;
3310 if (TARGET_ALIGN_BRANCH_TARGETS >= 0)
3311 rs6000_align_branch_targets = TARGET_ALIGN_BRANCH_TARGETS;
3313 rs6000_sched_restricted_insns_priority
3314 = (rs6000_sched_groups ? 1 : 0);
3316 /* Handle -msched-costly-dep option. */
3317 rs6000_sched_costly_dep
3318 = (rs6000_sched_groups ? true_store_to_load_dep_costly : no_dep_costly);
3320 if (rs6000_sched_costly_dep_str)
3322 if (! strcmp (rs6000_sched_costly_dep_str, "no"))
3323 rs6000_sched_costly_dep = no_dep_costly;
3324 else if (! strcmp (rs6000_sched_costly_dep_str, "all"))
3325 rs6000_sched_costly_dep = all_deps_costly;
3326 else if (! strcmp (rs6000_sched_costly_dep_str, "true_store_to_load"))
3327 rs6000_sched_costly_dep = true_store_to_load_dep_costly;
3328 else if (! strcmp (rs6000_sched_costly_dep_str, "store_to_load"))
3329 rs6000_sched_costly_dep = store_to_load_dep_costly;
3330 else
3331 rs6000_sched_costly_dep = ((enum rs6000_dependence_cost)
3332 atoi (rs6000_sched_costly_dep_str));
3335 /* Handle -minsert-sched-nops option. */
3336 rs6000_sched_insert_nops
3337 = (rs6000_sched_groups ? sched_finish_regroup_exact : sched_finish_none);
3339 if (rs6000_sched_insert_nops_str)
3341 if (! strcmp (rs6000_sched_insert_nops_str, "no"))
3342 rs6000_sched_insert_nops = sched_finish_none;
3343 else if (! strcmp (rs6000_sched_insert_nops_str, "pad"))
3344 rs6000_sched_insert_nops = sched_finish_pad_groups;
3345 else if (! strcmp (rs6000_sched_insert_nops_str, "regroup_exact"))
3346 rs6000_sched_insert_nops = sched_finish_regroup_exact;
3347 else
3348 rs6000_sched_insert_nops = ((enum rs6000_nop_insertion)
3349 atoi (rs6000_sched_insert_nops_str));
3352 if (global_init_p)
3354 #ifdef TARGET_REGNAMES
3355 /* If the user desires alternate register names, copy in the
3356 alternate names now. */
3357 if (TARGET_REGNAMES)
3358 memcpy (rs6000_reg_names, alt_reg_names, sizeof (rs6000_reg_names));
3359 #endif
3361 /* Set aix_struct_return last, after the ABI is determined.
3362 If -maix-struct-return or -msvr4-struct-return was explicitly
3363 used, don't override with the ABI default. */
3364 if (!global_options_set.x_aix_struct_return)
3365 aix_struct_return = (DEFAULT_ABI != ABI_V4 || DRAFT_V4_STRUCT_RET);
3367 #if 0
3368 /* IBM XL compiler defaults to unsigned bitfields. */
3369 if (TARGET_XL_COMPAT)
3370 flag_signed_bitfields = 0;
3371 #endif
3373 if (TARGET_LONG_DOUBLE_128 && !TARGET_IEEEQUAD)
3374 REAL_MODE_FORMAT (TFmode) = &ibm_extended_format;
3376 if (TARGET_TOC)
3377 ASM_GENERATE_INTERNAL_LABEL (toc_label_name, "LCTOC", 1);
3379 /* We can only guarantee the availability of DI pseudo-ops when
3380 assembling for 64-bit targets. */
3381 if (!TARGET_64BIT)
3383 targetm.asm_out.aligned_op.di = NULL;
3384 targetm.asm_out.unaligned_op.di = NULL;
3388 /* Set branch target alignment, if not optimizing for size. */
3389 if (!optimize_size)
3391 /* Cell wants to be aligned 8byte for dual issue. Titan wants to be
3392 aligned 8byte to avoid misprediction by the branch predictor. */
3393 if (rs6000_cpu == PROCESSOR_TITAN
3394 || rs6000_cpu == PROCESSOR_CELL)
3396 if (align_functions <= 0)
3397 align_functions = 8;
3398 if (align_jumps <= 0)
3399 align_jumps = 8;
3400 if (align_loops <= 0)
3401 align_loops = 8;
3403 if (rs6000_align_branch_targets)
3405 if (align_functions <= 0)
3406 align_functions = 16;
3407 if (align_jumps <= 0)
3408 align_jumps = 16;
3409 if (align_loops <= 0)
3411 can_override_loop_align = 1;
3412 align_loops = 16;
3415 if (align_jumps_max_skip <= 0)
3416 align_jumps_max_skip = 15;
3417 if (align_loops_max_skip <= 0)
3418 align_loops_max_skip = 15;
3421 /* Arrange to save and restore machine status around nested functions. */
3422 init_machine_status = rs6000_init_machine_status;
3424 /* We should always be splitting complex arguments, but we can't break
3425 Linux and Darwin ABIs at the moment. For now, only AIX is fixed. */
3426 if (DEFAULT_ABI != ABI_AIX)
3427 targetm.calls.split_complex_arg = NULL;
3430 /* Initialize rs6000_cost with the appropriate target costs. */
3431 if (optimize_size)
3432 rs6000_cost = TARGET_POWERPC64 ? &size64_cost : &size32_cost;
3433 else
3434 switch (rs6000_cpu)
3436 case PROCESSOR_RS64A:
3437 rs6000_cost = &rs64a_cost;
3438 break;
3440 case PROCESSOR_MPCCORE:
3441 rs6000_cost = &mpccore_cost;
3442 break;
3444 case PROCESSOR_PPC403:
3445 rs6000_cost = &ppc403_cost;
3446 break;
3448 case PROCESSOR_PPC405:
3449 rs6000_cost = &ppc405_cost;
3450 break;
3452 case PROCESSOR_PPC440:
3453 rs6000_cost = &ppc440_cost;
3454 break;
3456 case PROCESSOR_PPC476:
3457 rs6000_cost = &ppc476_cost;
3458 break;
3460 case PROCESSOR_PPC601:
3461 rs6000_cost = &ppc601_cost;
3462 break;
3464 case PROCESSOR_PPC603:
3465 rs6000_cost = &ppc603_cost;
3466 break;
3468 case PROCESSOR_PPC604:
3469 rs6000_cost = &ppc604_cost;
3470 break;
3472 case PROCESSOR_PPC604e:
3473 rs6000_cost = &ppc604e_cost;
3474 break;
3476 case PROCESSOR_PPC620:
3477 rs6000_cost = &ppc620_cost;
3478 break;
3480 case PROCESSOR_PPC630:
3481 rs6000_cost = &ppc630_cost;
3482 break;
3484 case PROCESSOR_CELL:
3485 rs6000_cost = &ppccell_cost;
3486 break;
3488 case PROCESSOR_PPC750:
3489 case PROCESSOR_PPC7400:
3490 rs6000_cost = &ppc750_cost;
3491 break;
3493 case PROCESSOR_PPC7450:
3494 rs6000_cost = &ppc7450_cost;
3495 break;
3497 case PROCESSOR_PPC8540:
3498 case PROCESSOR_PPC8548:
3499 rs6000_cost = &ppc8540_cost;
3500 break;
3502 case PROCESSOR_PPCE300C2:
3503 case PROCESSOR_PPCE300C3:
3504 rs6000_cost = &ppce300c2c3_cost;
3505 break;
3507 case PROCESSOR_PPCE500MC:
3508 rs6000_cost = &ppce500mc_cost;
3509 break;
3511 case PROCESSOR_PPCE500MC64:
3512 rs6000_cost = &ppce500mc64_cost;
3513 break;
3515 case PROCESSOR_PPCE5500:
3516 rs6000_cost = &ppce5500_cost;
3517 break;
3519 case PROCESSOR_PPCE6500:
3520 rs6000_cost = &ppce6500_cost;
3521 break;
3523 case PROCESSOR_TITAN:
3524 rs6000_cost = &titan_cost;
3525 break;
3527 case PROCESSOR_POWER4:
3528 case PROCESSOR_POWER5:
3529 rs6000_cost = &power4_cost;
3530 break;
3532 case PROCESSOR_POWER6:
3533 rs6000_cost = &power6_cost;
3534 break;
3536 case PROCESSOR_POWER7:
3537 rs6000_cost = &power7_cost;
3538 break;
3540 case PROCESSOR_POWER8:
3541 rs6000_cost = &power8_cost;
3542 break;
3544 case PROCESSOR_PPCA2:
3545 rs6000_cost = &ppca2_cost;
3546 break;
3548 default:
3549 gcc_unreachable ();
3552 if (global_init_p)
3554 maybe_set_param_value (PARAM_SIMULTANEOUS_PREFETCHES,
3555 rs6000_cost->simultaneous_prefetches,
3556 global_options.x_param_values,
3557 global_options_set.x_param_values);
3558 maybe_set_param_value (PARAM_L1_CACHE_SIZE, rs6000_cost->l1_cache_size,
3559 global_options.x_param_values,
3560 global_options_set.x_param_values);
3561 maybe_set_param_value (PARAM_L1_CACHE_LINE_SIZE,
3562 rs6000_cost->cache_line_size,
3563 global_options.x_param_values,
3564 global_options_set.x_param_values);
3565 maybe_set_param_value (PARAM_L2_CACHE_SIZE, rs6000_cost->l2_cache_size,
3566 global_options.x_param_values,
3567 global_options_set.x_param_values);
3569 /* Increase loop peeling limits based on performance analysis. */
3570 maybe_set_param_value (PARAM_MAX_PEELED_INSNS, 400,
3571 global_options.x_param_values,
3572 global_options_set.x_param_values);
3573 maybe_set_param_value (PARAM_MAX_COMPLETELY_PEELED_INSNS, 400,
3574 global_options.x_param_values,
3575 global_options_set.x_param_values);
3577 /* If using typedef char *va_list, signal that
3578 __builtin_va_start (&ap, 0) can be optimized to
3579 ap = __builtin_next_arg (0). */
3580 if (DEFAULT_ABI != ABI_V4)
3581 targetm.expand_builtin_va_start = NULL;
3584 /* Set up single/double float flags.
3585 If TARGET_HARD_FLOAT is set, but neither single or double is set,
3586 then set both flags. */
3587 if (TARGET_HARD_FLOAT && TARGET_FPRS
3588 && rs6000_single_float == 0 && rs6000_double_float == 0)
3589 rs6000_single_float = rs6000_double_float = 1;
3591 /* If not explicitly specified via option, decide whether to generate indexed
3592 load/store instructions. */
3593 if (TARGET_AVOID_XFORM == -1)
3594 /* Avoid indexed addressing when targeting Power6 in order to avoid the
3595 DERAT mispredict penalty. However the LVE and STVE altivec instructions
3596 need indexed accesses and the type used is the scalar type of the element
3597 being loaded or stored. */
3598 TARGET_AVOID_XFORM = (rs6000_cpu == PROCESSOR_POWER6 && TARGET_CMPB
3599 && !TARGET_ALTIVEC);
3601 /* Set the -mrecip options. */
3602 if (rs6000_recip_name)
3604 char *p = ASTRDUP (rs6000_recip_name);
3605 char *q;
3606 unsigned int mask, i;
3607 bool invert;
3609 while ((q = strtok (p, ",")) != NULL)
3611 p = NULL;
3612 if (*q == '!')
3614 invert = true;
3615 q++;
3617 else
3618 invert = false;
3620 if (!strcmp (q, "default"))
3621 mask = ((TARGET_RECIP_PRECISION)
3622 ? RECIP_HIGH_PRECISION : RECIP_LOW_PRECISION);
3623 else
3625 for (i = 0; i < ARRAY_SIZE (recip_options); i++)
3626 if (!strcmp (q, recip_options[i].string))
3628 mask = recip_options[i].mask;
3629 break;
3632 if (i == ARRAY_SIZE (recip_options))
3634 error ("unknown option for -mrecip=%s", q);
3635 invert = false;
3636 mask = 0;
3637 ret = false;
3641 if (invert)
3642 rs6000_recip_control &= ~mask;
3643 else
3644 rs6000_recip_control |= mask;
3648 /* Set the builtin mask of the various options used that could affect which
3649 builtins were used. In the past we used target_flags, but we've run out
3650 of bits, and some options like SPE and PAIRED are no longer in
3651 target_flags. */
3652 rs6000_builtin_mask = rs6000_builtin_mask_calculate ();
3653 if (TARGET_DEBUG_BUILTIN || TARGET_DEBUG_TARGET)
3655 fprintf (stderr,
3656 "new builtin mask = " HOST_WIDE_INT_PRINT_HEX ", ",
3657 rs6000_builtin_mask);
3658 rs6000_print_builtin_options (stderr, 0, NULL, rs6000_builtin_mask);
3661 /* Initialize all of the registers. */
3662 rs6000_init_hard_regno_mode_ok (global_init_p);
3664 /* Save the initial options in case the user does function specific options */
3665 if (global_init_p)
3666 target_option_default_node = target_option_current_node
3667 = build_target_option_node ();
3669 /* If not explicitly specified via option, decide whether to generate the
3670 extra blr's required to preserve the link stack on some cpus (eg, 476). */
3671 if (TARGET_LINK_STACK == -1)
3672 SET_TARGET_LINK_STACK (rs6000_cpu == PROCESSOR_PPC476 && flag_pic);
3674 return ret;
3677 /* Implement TARGET_OPTION_OVERRIDE. On the RS/6000 this is used to
3678 define the target cpu type. */
3680 static void
3681 rs6000_option_override (void)
3683 (void) rs6000_option_override_internal (true);
3687 /* Implement targetm.vectorize.builtin_mask_for_load. */
3688 static tree
3689 rs6000_builtin_mask_for_load (void)
3691 if (TARGET_ALTIVEC || TARGET_VSX)
3692 return altivec_builtin_mask_for_load;
3693 else
3694 return 0;
3697 /* Implement LOOP_ALIGN. */
3699 rs6000_loop_align (rtx label)
3701 basic_block bb;
3702 int ninsns;
3704 /* Don't override loop alignment if -falign-loops was specified. */
3705 if (!can_override_loop_align)
3706 return align_loops_log;
3708 bb = BLOCK_FOR_INSN (label);
3709 ninsns = num_loop_insns(bb->loop_father);
3711 /* Align small loops to 32 bytes to fit in an icache sector, otherwise return default. */
3712 if (ninsns > 4 && ninsns <= 8
3713 && (rs6000_cpu == PROCESSOR_POWER4
3714 || rs6000_cpu == PROCESSOR_POWER5
3715 || rs6000_cpu == PROCESSOR_POWER6
3716 || rs6000_cpu == PROCESSOR_POWER7
3717 || rs6000_cpu == PROCESSOR_POWER8))
3718 return 5;
3719 else
3720 return align_loops_log;
3723 /* Implement TARGET_LOOP_ALIGN_MAX_SKIP. */
3724 static int
3725 rs6000_loop_align_max_skip (rtx label)
3727 return (1 << rs6000_loop_align (label)) - 1;
3730 /* Return true iff, data reference of TYPE can reach vector alignment (16)
3731 after applying N number of iterations. This routine does not determine
3732 how may iterations are required to reach desired alignment. */
3734 static bool
3735 rs6000_vector_alignment_reachable (const_tree type ATTRIBUTE_UNUSED, bool is_packed)
3737 if (is_packed)
3738 return false;
3740 if (TARGET_32BIT)
3742 if (rs6000_alignment_flags == MASK_ALIGN_NATURAL)
3743 return true;
3745 if (rs6000_alignment_flags == MASK_ALIGN_POWER)
3746 return true;
3748 return false;
3750 else
3752 if (TARGET_MACHO)
3753 return false;
3755 /* Assuming that all other types are naturally aligned. CHECKME! */
3756 return true;
3760 /* Return true if the vector misalignment factor is supported by the
3761 target. */
3762 static bool
3763 rs6000_builtin_support_vector_misalignment (enum machine_mode mode,
3764 const_tree type,
3765 int misalignment,
3766 bool is_packed)
3768 if (TARGET_VSX)
3770 /* Return if movmisalign pattern is not supported for this mode. */
3771 if (optab_handler (movmisalign_optab, mode) == CODE_FOR_nothing)
3772 return false;
3774 if (misalignment == -1)
3776 /* Misalignment factor is unknown at compile time but we know
3777 it's word aligned. */
3778 if (rs6000_vector_alignment_reachable (type, is_packed))
3780 int element_size = TREE_INT_CST_LOW (TYPE_SIZE (type));
3782 if (element_size == 64 || element_size == 32)
3783 return true;
3786 return false;
3789 /* VSX supports word-aligned vector. */
3790 if (misalignment % 4 == 0)
3791 return true;
3793 return false;
3796 /* Implement targetm.vectorize.builtin_vectorization_cost. */
3797 static int
3798 rs6000_builtin_vectorization_cost (enum vect_cost_for_stmt type_of_cost,
3799 tree vectype, int misalign)
3801 unsigned elements;
3802 tree elem_type;
3804 switch (type_of_cost)
3806 case scalar_stmt:
3807 case scalar_load:
3808 case scalar_store:
3809 case vector_stmt:
3810 case vector_load:
3811 case vector_store:
3812 case vec_to_scalar:
3813 case scalar_to_vec:
3814 case cond_branch_not_taken:
3815 return 1;
3817 case vec_perm:
3818 if (TARGET_VSX)
3819 return 3;
3820 else
3821 return 1;
3823 case vec_promote_demote:
3824 if (TARGET_VSX)
3825 return 4;
3826 else
3827 return 1;
3829 case cond_branch_taken:
3830 return 3;
3832 case unaligned_load:
3833 if (TARGET_VSX && TARGET_ALLOW_MOVMISALIGN)
3835 elements = TYPE_VECTOR_SUBPARTS (vectype);
3836 if (elements == 2)
3837 /* Double word aligned. */
3838 return 2;
3840 if (elements == 4)
3842 switch (misalign)
3844 case 8:
3845 /* Double word aligned. */
3846 return 2;
3848 case -1:
3849 /* Unknown misalignment. */
3850 case 4:
3851 case 12:
3852 /* Word aligned. */
3853 return 22;
3855 default:
3856 gcc_unreachable ();
3861 if (TARGET_ALTIVEC)
3862 /* Misaligned loads are not supported. */
3863 gcc_unreachable ();
3865 return 2;
3867 case unaligned_store:
3868 if (TARGET_VSX && TARGET_ALLOW_MOVMISALIGN)
3870 elements = TYPE_VECTOR_SUBPARTS (vectype);
3871 if (elements == 2)
3872 /* Double word aligned. */
3873 return 2;
3875 if (elements == 4)
3877 switch (misalign)
3879 case 8:
3880 /* Double word aligned. */
3881 return 2;
3883 case -1:
3884 /* Unknown misalignment. */
3885 case 4:
3886 case 12:
3887 /* Word aligned. */
3888 return 23;
3890 default:
3891 gcc_unreachable ();
3896 if (TARGET_ALTIVEC)
3897 /* Misaligned stores are not supported. */
3898 gcc_unreachable ();
3900 return 2;
3902 case vec_construct:
3903 elements = TYPE_VECTOR_SUBPARTS (vectype);
3904 elem_type = TREE_TYPE (vectype);
3905 /* 32-bit vectors loaded into registers are stored as double
3906 precision, so we need n/2 converts in addition to the usual
3907 n/2 merges to construct a vector of short floats from them. */
3908 if (SCALAR_FLOAT_TYPE_P (elem_type)
3909 && TYPE_PRECISION (elem_type) == 32)
3910 return elements + 1;
3911 else
3912 return elements / 2 + 1;
3914 default:
3915 gcc_unreachable ();
3919 /* Implement targetm.vectorize.preferred_simd_mode. */
3921 static enum machine_mode
3922 rs6000_preferred_simd_mode (enum machine_mode mode)
3924 if (TARGET_VSX)
3925 switch (mode)
3927 case DFmode:
3928 return V2DFmode;
3929 default:;
3931 if (TARGET_ALTIVEC || TARGET_VSX)
3932 switch (mode)
3934 case SFmode:
3935 return V4SFmode;
3936 case DImode:
3937 return V2DImode;
3938 case SImode:
3939 return V4SImode;
3940 case HImode:
3941 return V8HImode;
3942 case QImode:
3943 return V16QImode;
3944 default:;
3946 if (TARGET_SPE)
3947 switch (mode)
3949 case SFmode:
3950 return V2SFmode;
3951 case SImode:
3952 return V2SImode;
3953 default:;
3955 if (TARGET_PAIRED_FLOAT
3956 && mode == SFmode)
3957 return V2SFmode;
3958 return word_mode;
3961 typedef struct _rs6000_cost_data
3963 struct loop *loop_info;
3964 unsigned cost[3];
3965 } rs6000_cost_data;
3967 /* Test for likely overcommitment of vector hardware resources. If a
3968 loop iteration is relatively large, and too large a percentage of
3969 instructions in the loop are vectorized, the cost model may not
3970 adequately reflect delays from unavailable vector resources.
3971 Penalize the loop body cost for this case. */
3973 static void
3974 rs6000_density_test (rs6000_cost_data *data)
3976 const int DENSITY_PCT_THRESHOLD = 85;
3977 const int DENSITY_SIZE_THRESHOLD = 70;
3978 const int DENSITY_PENALTY = 10;
3979 struct loop *loop = data->loop_info;
3980 basic_block *bbs = get_loop_body (loop);
3981 int nbbs = loop->num_nodes;
3982 int vec_cost = data->cost[vect_body], not_vec_cost = 0;
3983 int i, density_pct;
3985 for (i = 0; i < nbbs; i++)
3987 basic_block bb = bbs[i];
3988 gimple_stmt_iterator gsi;
3990 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
3992 gimple stmt = gsi_stmt (gsi);
3993 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
3995 if (!STMT_VINFO_RELEVANT_P (stmt_info)
3996 && !STMT_VINFO_IN_PATTERN_P (stmt_info))
3997 not_vec_cost++;
4001 free (bbs);
4002 density_pct = (vec_cost * 100) / (vec_cost + not_vec_cost);
4004 if (density_pct > DENSITY_PCT_THRESHOLD
4005 && vec_cost + not_vec_cost > DENSITY_SIZE_THRESHOLD)
4007 data->cost[vect_body] = vec_cost * (100 + DENSITY_PENALTY) / 100;
4008 if (dump_enabled_p ())
4009 dump_printf_loc (MSG_NOTE, vect_location,
4010 "density %d%%, cost %d exceeds threshold, penalizing "
4011 "loop body cost by %d%%", density_pct,
4012 vec_cost + not_vec_cost, DENSITY_PENALTY);
4016 /* Implement targetm.vectorize.init_cost. */
4018 static void *
4019 rs6000_init_cost (struct loop *loop_info)
4021 rs6000_cost_data *data = XNEW (struct _rs6000_cost_data);
4022 data->loop_info = loop_info;
4023 data->cost[vect_prologue] = 0;
4024 data->cost[vect_body] = 0;
4025 data->cost[vect_epilogue] = 0;
4026 return data;
4029 /* Implement targetm.vectorize.add_stmt_cost. */
4031 static unsigned
4032 rs6000_add_stmt_cost (void *data, int count, enum vect_cost_for_stmt kind,
4033 struct _stmt_vec_info *stmt_info, int misalign,
4034 enum vect_cost_model_location where)
4036 rs6000_cost_data *cost_data = (rs6000_cost_data*) data;
4037 unsigned retval = 0;
4039 if (flag_vect_cost_model)
4041 tree vectype = stmt_info ? stmt_vectype (stmt_info) : NULL_TREE;
4042 int stmt_cost = rs6000_builtin_vectorization_cost (kind, vectype,
4043 misalign);
4044 /* Statements in an inner loop relative to the loop being
4045 vectorized are weighted more heavily. The value here is
4046 arbitrary and could potentially be improved with analysis. */
4047 if (where == vect_body && stmt_info && stmt_in_inner_loop_p (stmt_info))
4048 count *= 50; /* FIXME. */
4050 retval = (unsigned) (count * stmt_cost);
4051 cost_data->cost[where] += retval;
4054 return retval;
4057 /* Implement targetm.vectorize.finish_cost. */
4059 static void
4060 rs6000_finish_cost (void *data, unsigned *prologue_cost,
4061 unsigned *body_cost, unsigned *epilogue_cost)
4063 rs6000_cost_data *cost_data = (rs6000_cost_data*) data;
4065 if (cost_data->loop_info)
4066 rs6000_density_test (cost_data);
4068 *prologue_cost = cost_data->cost[vect_prologue];
4069 *body_cost = cost_data->cost[vect_body];
4070 *epilogue_cost = cost_data->cost[vect_epilogue];
4073 /* Implement targetm.vectorize.destroy_cost_data. */
4075 static void
4076 rs6000_destroy_cost_data (void *data)
4078 free (data);
4081 /* Handler for the Mathematical Acceleration Subsystem (mass) interface to a
4082 library with vectorized intrinsics. */
4084 static tree
4085 rs6000_builtin_vectorized_libmass (tree fndecl, tree type_out, tree type_in)
4087 char name[32];
4088 const char *suffix = NULL;
4089 tree fntype, new_fndecl, bdecl = NULL_TREE;
4090 int n_args = 1;
4091 const char *bname;
4092 enum machine_mode el_mode, in_mode;
4093 int n, in_n;
4095 /* Libmass is suitable for unsafe math only as it does not correctly support
4096 parts of IEEE with the required precision such as denormals. Only support
4097 it if we have VSX to use the simd d2 or f4 functions.
4098 XXX: Add variable length support. */
4099 if (!flag_unsafe_math_optimizations || !TARGET_VSX)
4100 return NULL_TREE;
4102 el_mode = TYPE_MODE (TREE_TYPE (type_out));
4103 n = TYPE_VECTOR_SUBPARTS (type_out);
4104 in_mode = TYPE_MODE (TREE_TYPE (type_in));
4105 in_n = TYPE_VECTOR_SUBPARTS (type_in);
4106 if (el_mode != in_mode
4107 || n != in_n)
4108 return NULL_TREE;
4110 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
4112 enum built_in_function fn = DECL_FUNCTION_CODE (fndecl);
4113 switch (fn)
4115 case BUILT_IN_ATAN2:
4116 case BUILT_IN_HYPOT:
4117 case BUILT_IN_POW:
4118 n_args = 2;
4119 /* fall through */
4121 case BUILT_IN_ACOS:
4122 case BUILT_IN_ACOSH:
4123 case BUILT_IN_ASIN:
4124 case BUILT_IN_ASINH:
4125 case BUILT_IN_ATAN:
4126 case BUILT_IN_ATANH:
4127 case BUILT_IN_CBRT:
4128 case BUILT_IN_COS:
4129 case BUILT_IN_COSH:
4130 case BUILT_IN_ERF:
4131 case BUILT_IN_ERFC:
4132 case BUILT_IN_EXP2:
4133 case BUILT_IN_EXP:
4134 case BUILT_IN_EXPM1:
4135 case BUILT_IN_LGAMMA:
4136 case BUILT_IN_LOG10:
4137 case BUILT_IN_LOG1P:
4138 case BUILT_IN_LOG2:
4139 case BUILT_IN_LOG:
4140 case BUILT_IN_SIN:
4141 case BUILT_IN_SINH:
4142 case BUILT_IN_SQRT:
4143 case BUILT_IN_TAN:
4144 case BUILT_IN_TANH:
4145 bdecl = builtin_decl_implicit (fn);
4146 suffix = "d2"; /* pow -> powd2 */
4147 if (el_mode != DFmode
4148 || n != 2
4149 || !bdecl)
4150 return NULL_TREE;
4151 break;
4153 case BUILT_IN_ATAN2F:
4154 case BUILT_IN_HYPOTF:
4155 case BUILT_IN_POWF:
4156 n_args = 2;
4157 /* fall through */
4159 case BUILT_IN_ACOSF:
4160 case BUILT_IN_ACOSHF:
4161 case BUILT_IN_ASINF:
4162 case BUILT_IN_ASINHF:
4163 case BUILT_IN_ATANF:
4164 case BUILT_IN_ATANHF:
4165 case BUILT_IN_CBRTF:
4166 case BUILT_IN_COSF:
4167 case BUILT_IN_COSHF:
4168 case BUILT_IN_ERFF:
4169 case BUILT_IN_ERFCF:
4170 case BUILT_IN_EXP2F:
4171 case BUILT_IN_EXPF:
4172 case BUILT_IN_EXPM1F:
4173 case BUILT_IN_LGAMMAF:
4174 case BUILT_IN_LOG10F:
4175 case BUILT_IN_LOG1PF:
4176 case BUILT_IN_LOG2F:
4177 case BUILT_IN_LOGF:
4178 case BUILT_IN_SINF:
4179 case BUILT_IN_SINHF:
4180 case BUILT_IN_SQRTF:
4181 case BUILT_IN_TANF:
4182 case BUILT_IN_TANHF:
4183 bdecl = builtin_decl_implicit (fn);
4184 suffix = "4"; /* powf -> powf4 */
4185 if (el_mode != SFmode
4186 || n != 4
4187 || !bdecl)
4188 return NULL_TREE;
4189 break;
4191 default:
4192 return NULL_TREE;
4195 else
4196 return NULL_TREE;
4198 gcc_assert (suffix != NULL);
4199 bname = IDENTIFIER_POINTER (DECL_NAME (bdecl));
4200 if (!bname)
4201 return NULL_TREE;
4203 strcpy (name, bname + sizeof ("__builtin_") - 1);
4204 strcat (name, suffix);
4206 if (n_args == 1)
4207 fntype = build_function_type_list (type_out, type_in, NULL);
4208 else if (n_args == 2)
4209 fntype = build_function_type_list (type_out, type_in, type_in, NULL);
4210 else
4211 gcc_unreachable ();
4213 /* Build a function declaration for the vectorized function. */
4214 new_fndecl = build_decl (BUILTINS_LOCATION,
4215 FUNCTION_DECL, get_identifier (name), fntype);
4216 TREE_PUBLIC (new_fndecl) = 1;
4217 DECL_EXTERNAL (new_fndecl) = 1;
4218 DECL_IS_NOVOPS (new_fndecl) = 1;
4219 TREE_READONLY (new_fndecl) = 1;
4221 return new_fndecl;
4224 /* Returns a function decl for a vectorized version of the builtin function
4225 with builtin function code FN and the result vector type TYPE, or NULL_TREE
4226 if it is not available. */
4228 static tree
4229 rs6000_builtin_vectorized_function (tree fndecl, tree type_out,
4230 tree type_in)
4232 enum machine_mode in_mode, out_mode;
4233 int in_n, out_n;
4235 if (TARGET_DEBUG_BUILTIN)
4236 fprintf (stderr, "rs6000_builtin_vectorized_function (%s, %s, %s)\n",
4237 IDENTIFIER_POINTER (DECL_NAME (fndecl)),
4238 GET_MODE_NAME (TYPE_MODE (type_out)),
4239 GET_MODE_NAME (TYPE_MODE (type_in)));
4241 if (TREE_CODE (type_out) != VECTOR_TYPE
4242 || TREE_CODE (type_in) != VECTOR_TYPE
4243 || !TARGET_VECTORIZE_BUILTINS)
4244 return NULL_TREE;
4246 out_mode = TYPE_MODE (TREE_TYPE (type_out));
4247 out_n = TYPE_VECTOR_SUBPARTS (type_out);
4248 in_mode = TYPE_MODE (TREE_TYPE (type_in));
4249 in_n = TYPE_VECTOR_SUBPARTS (type_in);
4251 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
4253 enum built_in_function fn = DECL_FUNCTION_CODE (fndecl);
4254 switch (fn)
4256 case BUILT_IN_CLZIMAX:
4257 case BUILT_IN_CLZLL:
4258 case BUILT_IN_CLZL:
4259 case BUILT_IN_CLZ:
4260 if (TARGET_P8_VECTOR && in_mode == out_mode && out_n == in_n)
4262 if (out_mode == QImode && out_n == 16)
4263 return rs6000_builtin_decls[P8V_BUILTIN_VCLZB];
4264 else if (out_mode == HImode && out_n == 8)
4265 return rs6000_builtin_decls[P8V_BUILTIN_VCLZH];
4266 else if (out_mode == SImode && out_n == 4)
4267 return rs6000_builtin_decls[P8V_BUILTIN_VCLZW];
4268 else if (out_mode == DImode && out_n == 2)
4269 return rs6000_builtin_decls[P8V_BUILTIN_VCLZD];
4271 break;
4272 case BUILT_IN_COPYSIGN:
4273 if (VECTOR_UNIT_VSX_P (V2DFmode)
4274 && out_mode == DFmode && out_n == 2
4275 && in_mode == DFmode && in_n == 2)
4276 return rs6000_builtin_decls[VSX_BUILTIN_CPSGNDP];
4277 break;
4278 case BUILT_IN_COPYSIGNF:
4279 if (out_mode != SFmode || out_n != 4
4280 || in_mode != SFmode || in_n != 4)
4281 break;
4282 if (VECTOR_UNIT_VSX_P (V4SFmode))
4283 return rs6000_builtin_decls[VSX_BUILTIN_CPSGNSP];
4284 if (VECTOR_UNIT_ALTIVEC_P (V4SFmode))
4285 return rs6000_builtin_decls[ALTIVEC_BUILTIN_COPYSIGN_V4SF];
4286 break;
4287 case BUILT_IN_POPCOUNTIMAX:
4288 case BUILT_IN_POPCOUNTLL:
4289 case BUILT_IN_POPCOUNTL:
4290 case BUILT_IN_POPCOUNT:
4291 if (TARGET_P8_VECTOR && in_mode == out_mode && out_n == in_n)
4293 if (out_mode == QImode && out_n == 16)
4294 return rs6000_builtin_decls[P8V_BUILTIN_VPOPCNTB];
4295 else if (out_mode == HImode && out_n == 8)
4296 return rs6000_builtin_decls[P8V_BUILTIN_VPOPCNTH];
4297 else if (out_mode == SImode && out_n == 4)
4298 return rs6000_builtin_decls[P8V_BUILTIN_VPOPCNTW];
4299 else if (out_mode == DImode && out_n == 2)
4300 return rs6000_builtin_decls[P8V_BUILTIN_VPOPCNTD];
4302 break;
4303 case BUILT_IN_SQRT:
4304 if (VECTOR_UNIT_VSX_P (V2DFmode)
4305 && out_mode == DFmode && out_n == 2
4306 && in_mode == DFmode && in_n == 2)
4307 return rs6000_builtin_decls[VSX_BUILTIN_XVSQRTDP];
4308 break;
4309 case BUILT_IN_SQRTF:
4310 if (VECTOR_UNIT_VSX_P (V4SFmode)
4311 && out_mode == SFmode && out_n == 4
4312 && in_mode == SFmode && in_n == 4)
4313 return rs6000_builtin_decls[VSX_BUILTIN_XVSQRTSP];
4314 break;
4315 case BUILT_IN_CEIL:
4316 if (VECTOR_UNIT_VSX_P (V2DFmode)
4317 && out_mode == DFmode && out_n == 2
4318 && in_mode == DFmode && in_n == 2)
4319 return rs6000_builtin_decls[VSX_BUILTIN_XVRDPIP];
4320 break;
4321 case BUILT_IN_CEILF:
4322 if (out_mode != SFmode || out_n != 4
4323 || in_mode != SFmode || in_n != 4)
4324 break;
4325 if (VECTOR_UNIT_VSX_P (V4SFmode))
4326 return rs6000_builtin_decls[VSX_BUILTIN_XVRSPIP];
4327 if (VECTOR_UNIT_ALTIVEC_P (V4SFmode))
4328 return rs6000_builtin_decls[ALTIVEC_BUILTIN_VRFIP];
4329 break;
4330 case BUILT_IN_FLOOR:
4331 if (VECTOR_UNIT_VSX_P (V2DFmode)
4332 && out_mode == DFmode && out_n == 2
4333 && in_mode == DFmode && in_n == 2)
4334 return rs6000_builtin_decls[VSX_BUILTIN_XVRDPIM];
4335 break;
4336 case BUILT_IN_FLOORF:
4337 if (out_mode != SFmode || out_n != 4
4338 || in_mode != SFmode || in_n != 4)
4339 break;
4340 if (VECTOR_UNIT_VSX_P (V4SFmode))
4341 return rs6000_builtin_decls[VSX_BUILTIN_XVRSPIM];
4342 if (VECTOR_UNIT_ALTIVEC_P (V4SFmode))
4343 return rs6000_builtin_decls[ALTIVEC_BUILTIN_VRFIM];
4344 break;
4345 case BUILT_IN_FMA:
4346 if (VECTOR_UNIT_VSX_P (V2DFmode)
4347 && out_mode == DFmode && out_n == 2
4348 && in_mode == DFmode && in_n == 2)
4349 return rs6000_builtin_decls[VSX_BUILTIN_XVMADDDP];
4350 break;
4351 case BUILT_IN_FMAF:
4352 if (VECTOR_UNIT_VSX_P (V4SFmode)
4353 && out_mode == SFmode && out_n == 4
4354 && in_mode == SFmode && in_n == 4)
4355 return rs6000_builtin_decls[VSX_BUILTIN_XVMADDSP];
4356 else if (VECTOR_UNIT_ALTIVEC_P (V4SFmode)
4357 && out_mode == SFmode && out_n == 4
4358 && in_mode == SFmode && in_n == 4)
4359 return rs6000_builtin_decls[ALTIVEC_BUILTIN_VMADDFP];
4360 break;
4361 case BUILT_IN_TRUNC:
4362 if (VECTOR_UNIT_VSX_P (V2DFmode)
4363 && out_mode == DFmode && out_n == 2
4364 && in_mode == DFmode && in_n == 2)
4365 return rs6000_builtin_decls[VSX_BUILTIN_XVRDPIZ];
4366 break;
4367 case BUILT_IN_TRUNCF:
4368 if (out_mode != SFmode || out_n != 4
4369 || in_mode != SFmode || in_n != 4)
4370 break;
4371 if (VECTOR_UNIT_VSX_P (V4SFmode))
4372 return rs6000_builtin_decls[VSX_BUILTIN_XVRSPIZ];
4373 if (VECTOR_UNIT_ALTIVEC_P (V4SFmode))
4374 return rs6000_builtin_decls[ALTIVEC_BUILTIN_VRFIZ];
4375 break;
4376 case BUILT_IN_NEARBYINT:
4377 if (VECTOR_UNIT_VSX_P (V2DFmode)
4378 && flag_unsafe_math_optimizations
4379 && out_mode == DFmode && out_n == 2
4380 && in_mode == DFmode && in_n == 2)
4381 return rs6000_builtin_decls[VSX_BUILTIN_XVRDPI];
4382 break;
4383 case BUILT_IN_NEARBYINTF:
4384 if (VECTOR_UNIT_VSX_P (V4SFmode)
4385 && flag_unsafe_math_optimizations
4386 && out_mode == SFmode && out_n == 4
4387 && in_mode == SFmode && in_n == 4)
4388 return rs6000_builtin_decls[VSX_BUILTIN_XVRSPI];
4389 break;
4390 case BUILT_IN_RINT:
4391 if (VECTOR_UNIT_VSX_P (V2DFmode)
4392 && !flag_trapping_math
4393 && out_mode == DFmode && out_n == 2
4394 && in_mode == DFmode && in_n == 2)
4395 return rs6000_builtin_decls[VSX_BUILTIN_XVRDPIC];
4396 break;
4397 case BUILT_IN_RINTF:
4398 if (VECTOR_UNIT_VSX_P (V4SFmode)
4399 && !flag_trapping_math
4400 && out_mode == SFmode && out_n == 4
4401 && in_mode == SFmode && in_n == 4)
4402 return rs6000_builtin_decls[VSX_BUILTIN_XVRSPIC];
4403 break;
4404 default:
4405 break;
4409 else if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
4411 enum rs6000_builtins fn
4412 = (enum rs6000_builtins)DECL_FUNCTION_CODE (fndecl);
4413 switch (fn)
4415 case RS6000_BUILTIN_RSQRTF:
4416 if (VECTOR_UNIT_ALTIVEC_OR_VSX_P (V4SFmode)
4417 && out_mode == SFmode && out_n == 4
4418 && in_mode == SFmode && in_n == 4)
4419 return rs6000_builtin_decls[ALTIVEC_BUILTIN_VRSQRTFP];
4420 break;
4421 case RS6000_BUILTIN_RSQRT:
4422 if (VECTOR_UNIT_VSX_P (V2DFmode)
4423 && out_mode == DFmode && out_n == 2
4424 && in_mode == DFmode && in_n == 2)
4425 return rs6000_builtin_decls[VSX_BUILTIN_RSQRT_2DF];
4426 break;
4427 case RS6000_BUILTIN_RECIPF:
4428 if (VECTOR_UNIT_ALTIVEC_OR_VSX_P (V4SFmode)
4429 && out_mode == SFmode && out_n == 4
4430 && in_mode == SFmode && in_n == 4)
4431 return rs6000_builtin_decls[ALTIVEC_BUILTIN_VRECIPFP];
4432 break;
4433 case RS6000_BUILTIN_RECIP:
4434 if (VECTOR_UNIT_VSX_P (V2DFmode)
4435 && out_mode == DFmode && out_n == 2
4436 && in_mode == DFmode && in_n == 2)
4437 return rs6000_builtin_decls[VSX_BUILTIN_RECIP_V2DF];
4438 break;
4439 default:
4440 break;
4444 /* Generate calls to libmass if appropriate. */
4445 if (rs6000_veclib_handler)
4446 return rs6000_veclib_handler (fndecl, type_out, type_in);
4448 return NULL_TREE;
4451 /* Default CPU string for rs6000*_file_start functions. */
4452 static const char *rs6000_default_cpu;
4454 /* Do anything needed at the start of the asm file. */
4456 static void
4457 rs6000_file_start (void)
4459 char buffer[80];
4460 const char *start = buffer;
4461 FILE *file = asm_out_file;
4463 rs6000_default_cpu = TARGET_CPU_DEFAULT;
4465 default_file_start ();
4467 if (flag_verbose_asm)
4469 sprintf (buffer, "\n%s rs6000/powerpc options:", ASM_COMMENT_START);
4471 if (rs6000_default_cpu != 0 && rs6000_default_cpu[0] != '\0')
4473 fprintf (file, "%s --with-cpu=%s", start, rs6000_default_cpu);
4474 start = "";
4477 if (global_options_set.x_rs6000_cpu_index)
4479 fprintf (file, "%s -mcpu=%s", start,
4480 processor_target_table[rs6000_cpu_index].name);
4481 start = "";
4484 if (global_options_set.x_rs6000_tune_index)
4486 fprintf (file, "%s -mtune=%s", start,
4487 processor_target_table[rs6000_tune_index].name);
4488 start = "";
4491 if (PPC405_ERRATUM77)
4493 fprintf (file, "%s PPC405CR_ERRATUM77", start);
4494 start = "";
4497 #ifdef USING_ELFOS_H
4498 switch (rs6000_sdata)
4500 case SDATA_NONE: fprintf (file, "%s -msdata=none", start); start = ""; break;
4501 case SDATA_DATA: fprintf (file, "%s -msdata=data", start); start = ""; break;
4502 case SDATA_SYSV: fprintf (file, "%s -msdata=sysv", start); start = ""; break;
4503 case SDATA_EABI: fprintf (file, "%s -msdata=eabi", start); start = ""; break;
4506 if (rs6000_sdata && g_switch_value)
4508 fprintf (file, "%s -G %d", start,
4509 g_switch_value);
4510 start = "";
4512 #endif
4514 if (*start == '\0')
4515 putc ('\n', file);
4518 if (DEFAULT_ABI == ABI_AIX || (TARGET_ELF && flag_pic == 2))
4520 switch_to_section (toc_section);
4521 switch_to_section (text_section);
4526 /* Return nonzero if this function is known to have a null epilogue. */
4529 direct_return (void)
4531 if (reload_completed)
4533 rs6000_stack_t *info = rs6000_stack_info ();
4535 if (info->first_gp_reg_save == 32
4536 && info->first_fp_reg_save == 64
4537 && info->first_altivec_reg_save == LAST_ALTIVEC_REGNO + 1
4538 && ! info->lr_save_p
4539 && ! info->cr_save_p
4540 && info->vrsave_mask == 0
4541 && ! info->push_p)
4542 return 1;
4545 return 0;
4548 /* Return the number of instructions it takes to form a constant in an
4549 integer register. */
4552 num_insns_constant_wide (HOST_WIDE_INT value)
4554 /* signed constant loadable with addi */
4555 if ((unsigned HOST_WIDE_INT) (value + 0x8000) < 0x10000)
4556 return 1;
4558 /* constant loadable with addis */
4559 else if ((value & 0xffff) == 0
4560 && (value >> 31 == -1 || value >> 31 == 0))
4561 return 1;
4563 else if (TARGET_POWERPC64)
4565 HOST_WIDE_INT low = ((value & 0xffffffff) ^ 0x80000000) - 0x80000000;
4566 HOST_WIDE_INT high = value >> 31;
4568 if (high == 0 || high == -1)
4569 return 2;
4571 high >>= 1;
4573 if (low == 0)
4574 return num_insns_constant_wide (high) + 1;
4575 else if (high == 0)
4576 return num_insns_constant_wide (low) + 1;
4577 else
4578 return (num_insns_constant_wide (high)
4579 + num_insns_constant_wide (low) + 1);
4582 else
4583 return 2;
4587 num_insns_constant (rtx op, enum machine_mode mode)
4589 HOST_WIDE_INT low, high;
4591 switch (GET_CODE (op))
4593 case CONST_INT:
4594 if ((INTVAL (op) >> 31) != 0 && (INTVAL (op) >> 31) != -1
4595 && mask64_operand (op, mode))
4596 return 2;
4597 else
4598 return num_insns_constant_wide (INTVAL (op));
4600 case CONST_DOUBLE:
4601 if (mode == SFmode || mode == SDmode)
4603 long l;
4604 REAL_VALUE_TYPE rv;
4606 REAL_VALUE_FROM_CONST_DOUBLE (rv, op);
4607 if (DECIMAL_FLOAT_MODE_P (mode))
4608 REAL_VALUE_TO_TARGET_DECIMAL32 (rv, l);
4609 else
4610 REAL_VALUE_TO_TARGET_SINGLE (rv, l);
4611 return num_insns_constant_wide ((HOST_WIDE_INT) l);
4614 long l[2];
4615 REAL_VALUE_TYPE rv;
4617 REAL_VALUE_FROM_CONST_DOUBLE (rv, op);
4618 if (DECIMAL_FLOAT_MODE_P (mode))
4619 REAL_VALUE_TO_TARGET_DECIMAL64 (rv, l);
4620 else
4621 REAL_VALUE_TO_TARGET_DOUBLE (rv, l);
4622 high = l[WORDS_BIG_ENDIAN == 0];
4623 low = l[WORDS_BIG_ENDIAN != 0];
4625 if (TARGET_32BIT)
4626 return (num_insns_constant_wide (low)
4627 + num_insns_constant_wide (high));
4628 else
4630 if ((high == 0 && low >= 0)
4631 || (high == -1 && low < 0))
4632 return num_insns_constant_wide (low);
4634 else if (mask64_operand (op, mode))
4635 return 2;
4637 else if (low == 0)
4638 return num_insns_constant_wide (high) + 1;
4640 else
4641 return (num_insns_constant_wide (high)
4642 + num_insns_constant_wide (low) + 1);
4645 default:
4646 gcc_unreachable ();
4650 /* Interpret element ELT of the CONST_VECTOR OP as an integer value.
4651 If the mode of OP is MODE_VECTOR_INT, this simply returns the
4652 corresponding element of the vector, but for V4SFmode and V2SFmode,
4653 the corresponding "float" is interpreted as an SImode integer. */
4655 HOST_WIDE_INT
4656 const_vector_elt_as_int (rtx op, unsigned int elt)
4658 rtx tmp;
4660 /* We can't handle V2DImode and V2DFmode vector constants here yet. */
4661 gcc_assert (GET_MODE (op) != V2DImode
4662 && GET_MODE (op) != V2DFmode);
4664 tmp = CONST_VECTOR_ELT (op, elt);
4665 if (GET_MODE (op) == V4SFmode
4666 || GET_MODE (op) == V2SFmode)
4667 tmp = gen_lowpart (SImode, tmp);
4668 return INTVAL (tmp);
4671 /* Return true if OP can be synthesized with a particular vspltisb, vspltish
4672 or vspltisw instruction. OP is a CONST_VECTOR. Which instruction is used
4673 depends on STEP and COPIES, one of which will be 1. If COPIES > 1,
4674 all items are set to the same value and contain COPIES replicas of the
4675 vsplt's operand; if STEP > 1, one in STEP elements is set to the vsplt's
4676 operand and the others are set to the value of the operand's msb. */
4678 static bool
4679 vspltis_constant (rtx op, unsigned step, unsigned copies)
4681 enum machine_mode mode = GET_MODE (op);
4682 enum machine_mode inner = GET_MODE_INNER (mode);
4684 unsigned i;
4685 unsigned nunits;
4686 unsigned bitsize;
4687 unsigned mask;
4689 HOST_WIDE_INT val;
4690 HOST_WIDE_INT splat_val;
4691 HOST_WIDE_INT msb_val;
4693 if (mode == V2DImode || mode == V2DFmode)
4694 return false;
4696 nunits = GET_MODE_NUNITS (mode);
4697 bitsize = GET_MODE_BITSIZE (inner);
4698 mask = GET_MODE_MASK (inner);
4700 val = const_vector_elt_as_int (op, BYTES_BIG_ENDIAN ? nunits - 1 : 0);
4701 splat_val = val;
4702 msb_val = val > 0 ? 0 : -1;
4704 /* Construct the value to be splatted, if possible. If not, return 0. */
4705 for (i = 2; i <= copies; i *= 2)
4707 HOST_WIDE_INT small_val;
4708 bitsize /= 2;
4709 small_val = splat_val >> bitsize;
4710 mask >>= bitsize;
4711 if (splat_val != ((small_val << bitsize) | (small_val & mask)))
4712 return false;
4713 splat_val = small_val;
4716 /* Check if SPLAT_VAL can really be the operand of a vspltis[bhw]. */
4717 if (EASY_VECTOR_15 (splat_val))
4720 /* Also check if we can splat, and then add the result to itself. Do so if
4721 the value is positive, of if the splat instruction is using OP's mode;
4722 for splat_val < 0, the splat and the add should use the same mode. */
4723 else if (EASY_VECTOR_15_ADD_SELF (splat_val)
4724 && (splat_val >= 0 || (step == 1 && copies == 1)))
4727 /* Also check if are loading up the most significant bit which can be done by
4728 loading up -1 and shifting the value left by -1. */
4729 else if (EASY_VECTOR_MSB (splat_val, inner))
4732 else
4733 return false;
4735 /* Check if VAL is present in every STEP-th element, and the
4736 other elements are filled with its most significant bit. */
4737 for (i = 0; i < nunits - 1; ++i)
4739 HOST_WIDE_INT desired_val;
4740 if (((BYTES_BIG_ENDIAN ? i + 1 : i) & (step - 1)) == 0)
4741 desired_val = val;
4742 else
4743 desired_val = msb_val;
4745 if (desired_val != const_vector_elt_as_int (op, i))
4746 return false;
4749 return true;
4753 /* Return true if OP is of the given MODE and can be synthesized
4754 with a vspltisb, vspltish or vspltisw. */
4756 bool
4757 easy_altivec_constant (rtx op, enum machine_mode mode)
4759 unsigned step, copies;
4761 if (mode == VOIDmode)
4762 mode = GET_MODE (op);
4763 else if (mode != GET_MODE (op))
4764 return false;
4766 /* V2DI/V2DF was added with VSX. Only allow 0 and all 1's as easy
4767 constants. */
4768 if (mode == V2DFmode)
4769 return zero_constant (op, mode);
4771 if (mode == V2DImode)
4773 /* In case the compiler is built 32-bit, CONST_DOUBLE constants are not
4774 easy. */
4775 if (GET_CODE (CONST_VECTOR_ELT (op, 0)) != CONST_INT
4776 || GET_CODE (CONST_VECTOR_ELT (op, 1)) != CONST_INT)
4777 return false;
4779 if (zero_constant (op, mode))
4780 return true;
4782 if (INTVAL (CONST_VECTOR_ELT (op, 0)) == -1
4783 && INTVAL (CONST_VECTOR_ELT (op, 1)) == -1)
4784 return true;
4786 return false;
4789 /* Start with a vspltisw. */
4790 step = GET_MODE_NUNITS (mode) / 4;
4791 copies = 1;
4793 if (vspltis_constant (op, step, copies))
4794 return true;
4796 /* Then try with a vspltish. */
4797 if (step == 1)
4798 copies <<= 1;
4799 else
4800 step >>= 1;
4802 if (vspltis_constant (op, step, copies))
4803 return true;
4805 /* And finally a vspltisb. */
4806 if (step == 1)
4807 copies <<= 1;
4808 else
4809 step >>= 1;
4811 if (vspltis_constant (op, step, copies))
4812 return true;
4814 return false;
4817 /* Generate a VEC_DUPLICATE representing a vspltis[bhw] instruction whose
4818 result is OP. Abort if it is not possible. */
4821 gen_easy_altivec_constant (rtx op)
4823 enum machine_mode mode = GET_MODE (op);
4824 int nunits = GET_MODE_NUNITS (mode);
4825 rtx val = CONST_VECTOR_ELT (op, BYTES_BIG_ENDIAN ? nunits - 1 : 0);
4826 unsigned step = nunits / 4;
4827 unsigned copies = 1;
4829 /* Start with a vspltisw. */
4830 if (vspltis_constant (op, step, copies))
4831 return gen_rtx_VEC_DUPLICATE (V4SImode, gen_lowpart (SImode, val));
4833 /* Then try with a vspltish. */
4834 if (step == 1)
4835 copies <<= 1;
4836 else
4837 step >>= 1;
4839 if (vspltis_constant (op, step, copies))
4840 return gen_rtx_VEC_DUPLICATE (V8HImode, gen_lowpart (HImode, val));
4842 /* And finally a vspltisb. */
4843 if (step == 1)
4844 copies <<= 1;
4845 else
4846 step >>= 1;
4848 if (vspltis_constant (op, step, copies))
4849 return gen_rtx_VEC_DUPLICATE (V16QImode, gen_lowpart (QImode, val));
4851 gcc_unreachable ();
4854 const char *
4855 output_vec_const_move (rtx *operands)
4857 int cst, cst2;
4858 enum machine_mode mode;
4859 rtx dest, vec;
4861 dest = operands[0];
4862 vec = operands[1];
4863 mode = GET_MODE (dest);
4865 if (TARGET_VSX)
4867 if (zero_constant (vec, mode))
4868 return "xxlxor %x0,%x0,%x0";
4870 if (mode == V2DImode
4871 && INTVAL (CONST_VECTOR_ELT (vec, 0)) == -1
4872 && INTVAL (CONST_VECTOR_ELT (vec, 1)) == -1)
4873 return "vspltisw %0,-1";
4876 if (TARGET_ALTIVEC)
4878 rtx splat_vec;
4879 if (zero_constant (vec, mode))
4880 return "vxor %0,%0,%0";
4882 splat_vec = gen_easy_altivec_constant (vec);
4883 gcc_assert (GET_CODE (splat_vec) == VEC_DUPLICATE);
4884 operands[1] = XEXP (splat_vec, 0);
4885 if (!EASY_VECTOR_15 (INTVAL (operands[1])))
4886 return "#";
4888 switch (GET_MODE (splat_vec))
4890 case V4SImode:
4891 return "vspltisw %0,%1";
4893 case V8HImode:
4894 return "vspltish %0,%1";
4896 case V16QImode:
4897 return "vspltisb %0,%1";
4899 default:
4900 gcc_unreachable ();
4904 gcc_assert (TARGET_SPE);
4906 /* Vector constant 0 is handled as a splitter of V2SI, and in the
4907 pattern of V1DI, V4HI, and V2SF.
4909 FIXME: We should probably return # and add post reload
4910 splitters for these, but this way is so easy ;-). */
4911 cst = INTVAL (CONST_VECTOR_ELT (vec, 0));
4912 cst2 = INTVAL (CONST_VECTOR_ELT (vec, 1));
4913 operands[1] = CONST_VECTOR_ELT (vec, 0);
4914 operands[2] = CONST_VECTOR_ELT (vec, 1);
4915 if (cst == cst2)
4916 return "li %0,%1\n\tevmergelo %0,%0,%0";
4917 else
4918 return "li %0,%1\n\tevmergelo %0,%0,%0\n\tli %0,%2";
4921 /* Initialize TARGET of vector PAIRED to VALS. */
4923 void
4924 paired_expand_vector_init (rtx target, rtx vals)
4926 enum machine_mode mode = GET_MODE (target);
4927 int n_elts = GET_MODE_NUNITS (mode);
4928 int n_var = 0;
4929 rtx x, new_rtx, tmp, constant_op, op1, op2;
4930 int i;
4932 for (i = 0; i < n_elts; ++i)
4934 x = XVECEXP (vals, 0, i);
4935 if (!(CONST_INT_P (x)
4936 || GET_CODE (x) == CONST_DOUBLE
4937 || GET_CODE (x) == CONST_FIXED))
4938 ++n_var;
4940 if (n_var == 0)
4942 /* Load from constant pool. */
4943 emit_move_insn (target, gen_rtx_CONST_VECTOR (mode, XVEC (vals, 0)));
4944 return;
4947 if (n_var == 2)
4949 /* The vector is initialized only with non-constants. */
4950 new_rtx = gen_rtx_VEC_CONCAT (V2SFmode, XVECEXP (vals, 0, 0),
4951 XVECEXP (vals, 0, 1));
4953 emit_move_insn (target, new_rtx);
4954 return;
4957 /* One field is non-constant and the other one is a constant. Load the
4958 constant from the constant pool and use ps_merge instruction to
4959 construct the whole vector. */
4960 op1 = XVECEXP (vals, 0, 0);
4961 op2 = XVECEXP (vals, 0, 1);
4963 constant_op = (CONSTANT_P (op1)) ? op1 : op2;
4965 tmp = gen_reg_rtx (GET_MODE (constant_op));
4966 emit_move_insn (tmp, constant_op);
4968 if (CONSTANT_P (op1))
4969 new_rtx = gen_rtx_VEC_CONCAT (V2SFmode, tmp, op2);
4970 else
4971 new_rtx = gen_rtx_VEC_CONCAT (V2SFmode, op1, tmp);
4973 emit_move_insn (target, new_rtx);
4976 void
4977 paired_expand_vector_move (rtx operands[])
4979 rtx op0 = operands[0], op1 = operands[1];
4981 emit_move_insn (op0, op1);
4984 /* Emit vector compare for code RCODE. DEST is destination, OP1 and
4985 OP2 are two VEC_COND_EXPR operands, CC_OP0 and CC_OP1 are the two
4986 operands for the relation operation COND. This is a recursive
4987 function. */
4989 static void
4990 paired_emit_vector_compare (enum rtx_code rcode,
4991 rtx dest, rtx op0, rtx op1,
4992 rtx cc_op0, rtx cc_op1)
4994 rtx tmp = gen_reg_rtx (V2SFmode);
4995 rtx tmp1, max, min;
4997 gcc_assert (TARGET_PAIRED_FLOAT);
4998 gcc_assert (GET_MODE (op0) == GET_MODE (op1));
5000 switch (rcode)
5002 case LT:
5003 case LTU:
5004 paired_emit_vector_compare (GE, dest, op1, op0, cc_op0, cc_op1);
5005 return;
5006 case GE:
5007 case GEU:
5008 emit_insn (gen_subv2sf3 (tmp, cc_op0, cc_op1));
5009 emit_insn (gen_selv2sf4 (dest, tmp, op0, op1, CONST0_RTX (SFmode)));
5010 return;
5011 case LE:
5012 case LEU:
5013 paired_emit_vector_compare (GE, dest, op0, op1, cc_op1, cc_op0);
5014 return;
5015 case GT:
5016 paired_emit_vector_compare (LE, dest, op1, op0, cc_op0, cc_op1);
5017 return;
5018 case EQ:
5019 tmp1 = gen_reg_rtx (V2SFmode);
5020 max = gen_reg_rtx (V2SFmode);
5021 min = gen_reg_rtx (V2SFmode);
5022 gen_reg_rtx (V2SFmode);
5024 emit_insn (gen_subv2sf3 (tmp, cc_op0, cc_op1));
5025 emit_insn (gen_selv2sf4
5026 (max, tmp, cc_op0, cc_op1, CONST0_RTX (SFmode)));
5027 emit_insn (gen_subv2sf3 (tmp, cc_op1, cc_op0));
5028 emit_insn (gen_selv2sf4
5029 (min, tmp, cc_op0, cc_op1, CONST0_RTX (SFmode)));
5030 emit_insn (gen_subv2sf3 (tmp1, min, max));
5031 emit_insn (gen_selv2sf4 (dest, tmp1, op0, op1, CONST0_RTX (SFmode)));
5032 return;
5033 case NE:
5034 paired_emit_vector_compare (EQ, dest, op1, op0, cc_op0, cc_op1);
5035 return;
5036 case UNLE:
5037 paired_emit_vector_compare (LE, dest, op1, op0, cc_op0, cc_op1);
5038 return;
5039 case UNLT:
5040 paired_emit_vector_compare (LT, dest, op1, op0, cc_op0, cc_op1);
5041 return;
5042 case UNGE:
5043 paired_emit_vector_compare (GE, dest, op1, op0, cc_op0, cc_op1);
5044 return;
5045 case UNGT:
5046 paired_emit_vector_compare (GT, dest, op1, op0, cc_op0, cc_op1);
5047 return;
5048 default:
5049 gcc_unreachable ();
5052 return;
5055 /* Emit vector conditional expression.
5056 DEST is destination. OP1 and OP2 are two VEC_COND_EXPR operands.
5057 CC_OP0 and CC_OP1 are the two operands for the relation operation COND. */
5060 paired_emit_vector_cond_expr (rtx dest, rtx op1, rtx op2,
5061 rtx cond, rtx cc_op0, rtx cc_op1)
5063 enum rtx_code rcode = GET_CODE (cond);
5065 if (!TARGET_PAIRED_FLOAT)
5066 return 0;
5068 paired_emit_vector_compare (rcode, dest, op1, op2, cc_op0, cc_op1);
5070 return 1;
5073 /* Initialize vector TARGET to VALS. */
5075 void
5076 rs6000_expand_vector_init (rtx target, rtx vals)
5078 enum machine_mode mode = GET_MODE (target);
5079 enum machine_mode inner_mode = GET_MODE_INNER (mode);
5080 int n_elts = GET_MODE_NUNITS (mode);
5081 int n_var = 0, one_var = -1;
5082 bool all_same = true, all_const_zero = true;
5083 rtx x, mem;
5084 int i;
5086 for (i = 0; i < n_elts; ++i)
5088 x = XVECEXP (vals, 0, i);
5089 if (!(CONST_INT_P (x)
5090 || GET_CODE (x) == CONST_DOUBLE
5091 || GET_CODE (x) == CONST_FIXED))
5092 ++n_var, one_var = i;
5093 else if (x != CONST0_RTX (inner_mode))
5094 all_const_zero = false;
5096 if (i > 0 && !rtx_equal_p (x, XVECEXP (vals, 0, 0)))
5097 all_same = false;
5100 if (n_var == 0)
5102 rtx const_vec = gen_rtx_CONST_VECTOR (mode, XVEC (vals, 0));
5103 bool int_vector_p = (GET_MODE_CLASS (mode) == MODE_VECTOR_INT);
5104 if ((int_vector_p || TARGET_VSX) && all_const_zero)
5106 /* Zero register. */
5107 emit_insn (gen_rtx_SET (VOIDmode, target,
5108 gen_rtx_XOR (mode, target, target)));
5109 return;
5111 else if (int_vector_p && easy_vector_constant (const_vec, mode))
5113 /* Splat immediate. */
5114 emit_insn (gen_rtx_SET (VOIDmode, target, const_vec));
5115 return;
5117 else
5119 /* Load from constant pool. */
5120 emit_move_insn (target, const_vec);
5121 return;
5125 /* Double word values on VSX can use xxpermdi or lxvdsx. */
5126 if (VECTOR_MEM_VSX_P (mode) && (mode == V2DFmode || mode == V2DImode))
5128 rtx op0 = XVECEXP (vals, 0, 0);
5129 rtx op1 = XVECEXP (vals, 0, 1);
5130 if (all_same)
5132 if (!MEM_P (op0) && !REG_P (op0))
5133 op0 = force_reg (inner_mode, op0);
5134 if (mode == V2DFmode)
5135 emit_insn (gen_vsx_splat_v2df (target, op0));
5136 else
5137 emit_insn (gen_vsx_splat_v2di (target, op0));
5139 else
5141 op0 = force_reg (inner_mode, op0);
5142 op1 = force_reg (inner_mode, op1);
5143 if (mode == V2DFmode)
5144 emit_insn (gen_vsx_concat_v2df (target, op0, op1));
5145 else
5146 emit_insn (gen_vsx_concat_v2di (target, op0, op1));
5148 return;
5151 /* With single precision floating point on VSX, know that internally single
5152 precision is actually represented as a double, and either make 2 V2DF
5153 vectors, and convert these vectors to single precision, or do one
5154 conversion, and splat the result to the other elements. */
5155 if (mode == V4SFmode && VECTOR_MEM_VSX_P (mode))
5157 if (all_same)
5159 rtx freg = gen_reg_rtx (V4SFmode);
5160 rtx sreg = force_reg (SFmode, XVECEXP (vals, 0, 0));
5161 rtx cvt = ((TARGET_XSCVDPSPN)
5162 ? gen_vsx_xscvdpspn_scalar (freg, sreg)
5163 : gen_vsx_xscvdpsp_scalar (freg, sreg));
5165 emit_insn (cvt);
5166 emit_insn (gen_vsx_xxspltw_v4sf (target, freg, const0_rtx));
5168 else
5170 rtx dbl_even = gen_reg_rtx (V2DFmode);
5171 rtx dbl_odd = gen_reg_rtx (V2DFmode);
5172 rtx flt_even = gen_reg_rtx (V4SFmode);
5173 rtx flt_odd = gen_reg_rtx (V4SFmode);
5174 rtx op0 = force_reg (SFmode, XVECEXP (vals, 0, 0));
5175 rtx op1 = force_reg (SFmode, XVECEXP (vals, 0, 1));
5176 rtx op2 = force_reg (SFmode, XVECEXP (vals, 0, 2));
5177 rtx op3 = force_reg (SFmode, XVECEXP (vals, 0, 3));
5179 emit_insn (gen_vsx_concat_v2sf (dbl_even, op0, op1));
5180 emit_insn (gen_vsx_concat_v2sf (dbl_odd, op2, op3));
5181 emit_insn (gen_vsx_xvcvdpsp (flt_even, dbl_even));
5182 emit_insn (gen_vsx_xvcvdpsp (flt_odd, dbl_odd));
5183 rs6000_expand_extract_even (target, flt_even, flt_odd);
5185 return;
5188 /* Store value to stack temp. Load vector element. Splat. However, splat
5189 of 64-bit items is not supported on Altivec. */
5190 if (all_same && GET_MODE_SIZE (inner_mode) <= 4)
5192 rtx field;
5193 mem = assign_stack_temp (mode, GET_MODE_SIZE (inner_mode));
5194 emit_move_insn (adjust_address_nv (mem, inner_mode, 0),
5195 XVECEXP (vals, 0, 0));
5196 x = gen_rtx_UNSPEC (VOIDmode,
5197 gen_rtvec (1, const0_rtx), UNSPEC_LVE);
5198 emit_insn (gen_rtx_PARALLEL (VOIDmode,
5199 gen_rtvec (2,
5200 gen_rtx_SET (VOIDmode,
5201 target, mem),
5202 x)));
5203 field = (BYTES_BIG_ENDIAN ? const0_rtx
5204 : GEN_INT (GET_MODE_NUNITS (mode) - 1));
5205 x = gen_rtx_VEC_SELECT (inner_mode, target,
5206 gen_rtx_PARALLEL (VOIDmode,
5207 gen_rtvec (1, field)));
5208 emit_insn (gen_rtx_SET (VOIDmode, target,
5209 gen_rtx_VEC_DUPLICATE (mode, x)));
5210 return;
5213 /* One field is non-constant. Load constant then overwrite
5214 varying field. */
5215 if (n_var == 1)
5217 rtx copy = copy_rtx (vals);
5219 /* Load constant part of vector, substitute neighboring value for
5220 varying element. */
5221 XVECEXP (copy, 0, one_var) = XVECEXP (vals, 0, (one_var + 1) % n_elts);
5222 rs6000_expand_vector_init (target, copy);
5224 /* Insert variable. */
5225 rs6000_expand_vector_set (target, XVECEXP (vals, 0, one_var), one_var);
5226 return;
5229 /* Construct the vector in memory one field at a time
5230 and load the whole vector. */
5231 mem = assign_stack_temp (mode, GET_MODE_SIZE (mode));
5232 for (i = 0; i < n_elts; i++)
5233 emit_move_insn (adjust_address_nv (mem, inner_mode,
5234 i * GET_MODE_SIZE (inner_mode)),
5235 XVECEXP (vals, 0, i));
5236 emit_move_insn (target, mem);
5239 /* Set field ELT of TARGET to VAL. */
5241 void
5242 rs6000_expand_vector_set (rtx target, rtx val, int elt)
5244 enum machine_mode mode = GET_MODE (target);
5245 enum machine_mode inner_mode = GET_MODE_INNER (mode);
5246 rtx reg = gen_reg_rtx (mode);
5247 rtx mask, mem, x;
5248 int width = GET_MODE_SIZE (inner_mode);
5249 int i;
5251 if (VECTOR_MEM_VSX_P (mode) && (mode == V2DFmode || mode == V2DImode))
5253 rtx (*set_func) (rtx, rtx, rtx, rtx)
5254 = ((mode == V2DFmode) ? gen_vsx_set_v2df : gen_vsx_set_v2di);
5255 emit_insn (set_func (target, target, val, GEN_INT (elt)));
5256 return;
5259 /* Load single variable value. */
5260 mem = assign_stack_temp (mode, GET_MODE_SIZE (inner_mode));
5261 emit_move_insn (adjust_address_nv (mem, inner_mode, 0), val);
5262 x = gen_rtx_UNSPEC (VOIDmode,
5263 gen_rtvec (1, const0_rtx), UNSPEC_LVE);
5264 emit_insn (gen_rtx_PARALLEL (VOIDmode,
5265 gen_rtvec (2,
5266 gen_rtx_SET (VOIDmode,
5267 reg, mem),
5268 x)));
5270 /* Linear sequence. */
5271 mask = gen_rtx_PARALLEL (V16QImode, rtvec_alloc (16));
5272 for (i = 0; i < 16; ++i)
5273 XVECEXP (mask, 0, i) = GEN_INT (i);
5275 /* Set permute mask to insert element into target. */
5276 for (i = 0; i < width; ++i)
5277 XVECEXP (mask, 0, elt*width + i)
5278 = GEN_INT (i + 0x10);
5279 x = gen_rtx_CONST_VECTOR (V16QImode, XVEC (mask, 0));
5280 x = gen_rtx_UNSPEC (mode,
5281 gen_rtvec (3, target, reg,
5282 force_reg (V16QImode, x)),
5283 UNSPEC_VPERM);
5284 emit_insn (gen_rtx_SET (VOIDmode, target, x));
5287 /* Extract field ELT from VEC into TARGET. */
5289 void
5290 rs6000_expand_vector_extract (rtx target, rtx vec, int elt)
5292 enum machine_mode mode = GET_MODE (vec);
5293 enum machine_mode inner_mode = GET_MODE_INNER (mode);
5294 rtx mem;
5296 if (VECTOR_MEM_VSX_P (mode))
5298 switch (mode)
5300 default:
5301 break;
5302 case V2DFmode:
5303 emit_insn (gen_vsx_extract_v2df (target, vec, GEN_INT (elt)));
5304 return;
5305 case V2DImode:
5306 emit_insn (gen_vsx_extract_v2di (target, vec, GEN_INT (elt)));
5307 return;
5308 case V4SFmode:
5309 emit_insn (gen_vsx_extract_v4sf (target, vec, GEN_INT (elt)));
5310 return;
5314 /* Allocate mode-sized buffer. */
5315 mem = assign_stack_temp (mode, GET_MODE_SIZE (mode));
5317 emit_move_insn (mem, vec);
5319 /* Add offset to field within buffer matching vector element. */
5320 mem = adjust_address_nv (mem, inner_mode, elt * GET_MODE_SIZE (inner_mode));
5322 emit_move_insn (target, adjust_address_nv (mem, inner_mode, 0));
5325 /* Generates shifts and masks for a pair of rldicl or rldicr insns to
5326 implement ANDing by the mask IN. */
5327 void
5328 build_mask64_2_operands (rtx in, rtx *out)
5330 unsigned HOST_WIDE_INT c, lsb, m1, m2;
5331 int shift;
5333 gcc_assert (GET_CODE (in) == CONST_INT);
5335 c = INTVAL (in);
5336 if (c & 1)
5338 /* Assume c initially something like 0x00fff000000fffff. The idea
5339 is to rotate the word so that the middle ^^^^^^ group of zeros
5340 is at the MS end and can be cleared with an rldicl mask. We then
5341 rotate back and clear off the MS ^^ group of zeros with a
5342 second rldicl. */
5343 c = ~c; /* c == 0xff000ffffff00000 */
5344 lsb = c & -c; /* lsb == 0x0000000000100000 */
5345 m1 = -lsb; /* m1 == 0xfffffffffff00000 */
5346 c = ~c; /* c == 0x00fff000000fffff */
5347 c &= -lsb; /* c == 0x00fff00000000000 */
5348 lsb = c & -c; /* lsb == 0x0000100000000000 */
5349 c = ~c; /* c == 0xff000fffffffffff */
5350 c &= -lsb; /* c == 0xff00000000000000 */
5351 shift = 0;
5352 while ((lsb >>= 1) != 0)
5353 shift++; /* shift == 44 on exit from loop */
5354 m1 <<= 64 - shift; /* m1 == 0xffffff0000000000 */
5355 m1 = ~m1; /* m1 == 0x000000ffffffffff */
5356 m2 = ~c; /* m2 == 0x00ffffffffffffff */
5358 else
5360 /* Assume c initially something like 0xff000f0000000000. The idea
5361 is to rotate the word so that the ^^^ middle group of zeros
5362 is at the LS end and can be cleared with an rldicr mask. We then
5363 rotate back and clear off the LS group of ^^^^^^^^^^ zeros with
5364 a second rldicr. */
5365 lsb = c & -c; /* lsb == 0x0000010000000000 */
5366 m2 = -lsb; /* m2 == 0xffffff0000000000 */
5367 c = ~c; /* c == 0x00fff0ffffffffff */
5368 c &= -lsb; /* c == 0x00fff00000000000 */
5369 lsb = c & -c; /* lsb == 0x0000100000000000 */
5370 c = ~c; /* c == 0xff000fffffffffff */
5371 c &= -lsb; /* c == 0xff00000000000000 */
5372 shift = 0;
5373 while ((lsb >>= 1) != 0)
5374 shift++; /* shift == 44 on exit from loop */
5375 m1 = ~c; /* m1 == 0x00ffffffffffffff */
5376 m1 >>= shift; /* m1 == 0x0000000000000fff */
5377 m1 = ~m1; /* m1 == 0xfffffffffffff000 */
5380 /* Note that when we only have two 0->1 and 1->0 transitions, one of the
5381 masks will be all 1's. We are guaranteed more than one transition. */
5382 out[0] = GEN_INT (64 - shift);
5383 out[1] = GEN_INT (m1);
5384 out[2] = GEN_INT (shift);
5385 out[3] = GEN_INT (m2);
5388 /* Return TRUE if OP is an invalid SUBREG operation on the e500. */
5390 bool
5391 invalid_e500_subreg (rtx op, enum machine_mode mode)
5393 if (TARGET_E500_DOUBLE)
5395 /* Reject (subreg:SI (reg:DF)); likewise with subreg:DI or
5396 subreg:TI and reg:TF. Decimal float modes are like integer
5397 modes (only low part of each register used) for this
5398 purpose. */
5399 if (GET_CODE (op) == SUBREG
5400 && (mode == SImode || mode == DImode || mode == TImode
5401 || mode == DDmode || mode == TDmode || mode == PTImode)
5402 && REG_P (SUBREG_REG (op))
5403 && (GET_MODE (SUBREG_REG (op)) == DFmode
5404 || GET_MODE (SUBREG_REG (op)) == TFmode))
5405 return true;
5407 /* Reject (subreg:DF (reg:DI)); likewise with subreg:TF and
5408 reg:TI. */
5409 if (GET_CODE (op) == SUBREG
5410 && (mode == DFmode || mode == TFmode)
5411 && REG_P (SUBREG_REG (op))
5412 && (GET_MODE (SUBREG_REG (op)) == DImode
5413 || GET_MODE (SUBREG_REG (op)) == TImode
5414 || GET_MODE (SUBREG_REG (op)) == PTImode
5415 || GET_MODE (SUBREG_REG (op)) == DDmode
5416 || GET_MODE (SUBREG_REG (op)) == TDmode))
5417 return true;
5420 if (TARGET_SPE
5421 && GET_CODE (op) == SUBREG
5422 && mode == SImode
5423 && REG_P (SUBREG_REG (op))
5424 && SPE_VECTOR_MODE (GET_MODE (SUBREG_REG (op))))
5425 return true;
5427 return false;
5430 /* Return alignment of TYPE. Existing alignment is ALIGN. HOW
5431 selects whether the alignment is abi mandated, optional, or
5432 both abi and optional alignment. */
5434 unsigned int
5435 rs6000_data_alignment (tree type, unsigned int align, enum data_align how)
5437 if (how != align_opt)
5439 if (TREE_CODE (type) == VECTOR_TYPE)
5441 if ((TARGET_SPE && SPE_VECTOR_MODE (TYPE_MODE (type)))
5442 || (TARGET_PAIRED_FLOAT && PAIRED_VECTOR_MODE (TYPE_MODE (type))))
5444 if (align < 64)
5445 align = 64;
5447 else if (align < 128)
5448 align = 128;
5450 else if (TARGET_E500_DOUBLE
5451 && TREE_CODE (type) == REAL_TYPE
5452 && TYPE_MODE (type) == DFmode)
5454 if (align < 64)
5455 align = 64;
5459 if (how != align_abi)
5461 if (TREE_CODE (type) == ARRAY_TYPE
5462 && TYPE_MODE (TREE_TYPE (type)) == QImode)
5464 if (align < BITS_PER_WORD)
5465 align = BITS_PER_WORD;
5469 return align;
5472 /* AIX increases natural record alignment to doubleword if the first
5473 field is an FP double while the FP fields remain word aligned. */
5475 unsigned int
5476 rs6000_special_round_type_align (tree type, unsigned int computed,
5477 unsigned int specified)
5479 unsigned int align = MAX (computed, specified);
5480 tree field = TYPE_FIELDS (type);
5482 /* Skip all non field decls */
5483 while (field != NULL && TREE_CODE (field) != FIELD_DECL)
5484 field = DECL_CHAIN (field);
5486 if (field != NULL && field != type)
5488 type = TREE_TYPE (field);
5489 while (TREE_CODE (type) == ARRAY_TYPE)
5490 type = TREE_TYPE (type);
5492 if (type != error_mark_node && TYPE_MODE (type) == DFmode)
5493 align = MAX (align, 64);
5496 return align;
5499 /* Darwin increases record alignment to the natural alignment of
5500 the first field. */
5502 unsigned int
5503 darwin_rs6000_special_round_type_align (tree type, unsigned int computed,
5504 unsigned int specified)
5506 unsigned int align = MAX (computed, specified);
5508 if (TYPE_PACKED (type))
5509 return align;
5511 /* Find the first field, looking down into aggregates. */
5512 do {
5513 tree field = TYPE_FIELDS (type);
5514 /* Skip all non field decls */
5515 while (field != NULL && TREE_CODE (field) != FIELD_DECL)
5516 field = DECL_CHAIN (field);
5517 if (! field)
5518 break;
5519 /* A packed field does not contribute any extra alignment. */
5520 if (DECL_PACKED (field))
5521 return align;
5522 type = TREE_TYPE (field);
5523 while (TREE_CODE (type) == ARRAY_TYPE)
5524 type = TREE_TYPE (type);
5525 } while (AGGREGATE_TYPE_P (type));
5527 if (! AGGREGATE_TYPE_P (type) && type != error_mark_node)
5528 align = MAX (align, TYPE_ALIGN (type));
5530 return align;
5533 /* Return 1 for an operand in small memory on V.4/eabi. */
5536 small_data_operand (rtx op ATTRIBUTE_UNUSED,
5537 enum machine_mode mode ATTRIBUTE_UNUSED)
5539 #if TARGET_ELF
5540 rtx sym_ref;
5542 if (rs6000_sdata == SDATA_NONE || rs6000_sdata == SDATA_DATA)
5543 return 0;
5545 if (DEFAULT_ABI != ABI_V4)
5546 return 0;
5548 /* Vector and float memory instructions have a limited offset on the
5549 SPE, so using a vector or float variable directly as an operand is
5550 not useful. */
5551 if (TARGET_SPE
5552 && (SPE_VECTOR_MODE (mode) || FLOAT_MODE_P (mode)))
5553 return 0;
5555 if (GET_CODE (op) == SYMBOL_REF)
5556 sym_ref = op;
5558 else if (GET_CODE (op) != CONST
5559 || GET_CODE (XEXP (op, 0)) != PLUS
5560 || GET_CODE (XEXP (XEXP (op, 0), 0)) != SYMBOL_REF
5561 || GET_CODE (XEXP (XEXP (op, 0), 1)) != CONST_INT)
5562 return 0;
5564 else
5566 rtx sum = XEXP (op, 0);
5567 HOST_WIDE_INT summand;
5569 /* We have to be careful here, because it is the referenced address
5570 that must be 32k from _SDA_BASE_, not just the symbol. */
5571 summand = INTVAL (XEXP (sum, 1));
5572 if (summand < 0 || summand > g_switch_value)
5573 return 0;
5575 sym_ref = XEXP (sum, 0);
5578 return SYMBOL_REF_SMALL_P (sym_ref);
5579 #else
5580 return 0;
5581 #endif
5584 /* Return true if either operand is a general purpose register. */
5586 bool
5587 gpr_or_gpr_p (rtx op0, rtx op1)
5589 return ((REG_P (op0) && INT_REGNO_P (REGNO (op0)))
5590 || (REG_P (op1) && INT_REGNO_P (REGNO (op1))));
5593 /* Return true if this is a move direct operation between GPR registers and
5594 floating point/VSX registers. */
5596 bool
5597 direct_move_p (rtx op0, rtx op1)
5599 int regno0, regno1;
5601 if (!REG_P (op0) || !REG_P (op1))
5602 return false;
5604 if (!TARGET_DIRECT_MOVE && !TARGET_MFPGPR)
5605 return false;
5607 regno0 = REGNO (op0);
5608 regno1 = REGNO (op1);
5609 if (regno0 >= FIRST_PSEUDO_REGISTER || regno1 >= FIRST_PSEUDO_REGISTER)
5610 return false;
5612 if (INT_REGNO_P (regno0))
5613 return (TARGET_DIRECT_MOVE) ? VSX_REGNO_P (regno1) : FP_REGNO_P (regno1);
5615 else if (INT_REGNO_P (regno1))
5617 if (TARGET_MFPGPR && FP_REGNO_P (regno0))
5618 return true;
5620 else if (TARGET_DIRECT_MOVE && VSX_REGNO_P (regno0))
5621 return true;
5624 return false;
5627 /* Return true if this is a load or store quad operation. */
5629 bool
5630 quad_load_store_p (rtx op0, rtx op1)
5632 bool ret;
5634 if (!TARGET_QUAD_MEMORY)
5635 ret = false;
5637 else if (REG_P (op0) && MEM_P (op1))
5638 ret = (quad_int_reg_operand (op0, GET_MODE (op0))
5639 && quad_memory_operand (op1, GET_MODE (op1))
5640 && !reg_overlap_mentioned_p (op0, op1));
5642 else if (MEM_P (op0) && REG_P (op1))
5643 ret = (quad_memory_operand (op0, GET_MODE (op0))
5644 && quad_int_reg_operand (op1, GET_MODE (op1)));
5646 else
5647 ret = false;
5649 if (TARGET_DEBUG_ADDR)
5651 fprintf (stderr, "\n========== quad_load_store, return %s\n",
5652 ret ? "true" : "false");
5653 debug_rtx (gen_rtx_SET (VOIDmode, op0, op1));
5656 return ret;
5659 /* Given an address, return a constant offset term if one exists. */
5661 static rtx
5662 address_offset (rtx op)
5664 if (GET_CODE (op) == PRE_INC
5665 || GET_CODE (op) == PRE_DEC)
5666 op = XEXP (op, 0);
5667 else if (GET_CODE (op) == PRE_MODIFY
5668 || GET_CODE (op) == LO_SUM)
5669 op = XEXP (op, 1);
5671 if (GET_CODE (op) == CONST)
5672 op = XEXP (op, 0);
5674 if (GET_CODE (op) == PLUS)
5675 op = XEXP (op, 1);
5677 if (CONST_INT_P (op))
5678 return op;
5680 return NULL_RTX;
5683 /* Return true if the MEM operand is a memory operand suitable for use
5684 with a (full width, possibly multiple) gpr load/store. On
5685 powerpc64 this means the offset must be divisible by 4.
5686 Implements 'Y' constraint.
5688 Accept direct, indexed, offset, lo_sum and tocref. Since this is
5689 a constraint function we know the operand has satisfied a suitable
5690 memory predicate. Also accept some odd rtl generated by reload
5691 (see rs6000_legitimize_reload_address for various forms). It is
5692 important that reload rtl be accepted by appropriate constraints
5693 but not by the operand predicate.
5695 Offsetting a lo_sum should not be allowed, except where we know by
5696 alignment that a 32k boundary is not crossed, but see the ???
5697 comment in rs6000_legitimize_reload_address. Note that by
5698 "offsetting" here we mean a further offset to access parts of the
5699 MEM. It's fine to have a lo_sum where the inner address is offset
5700 from a sym, since the same sym+offset will appear in the high part
5701 of the address calculation. */
5703 bool
5704 mem_operand_gpr (rtx op, enum machine_mode mode)
5706 unsigned HOST_WIDE_INT offset;
5707 int extra;
5708 rtx addr = XEXP (op, 0);
5710 op = address_offset (addr);
5711 if (op == NULL_RTX)
5712 return true;
5714 offset = INTVAL (op);
5715 if (TARGET_POWERPC64 && (offset & 3) != 0)
5716 return false;
5718 extra = GET_MODE_SIZE (mode) - UNITS_PER_WORD;
5719 gcc_assert (extra >= 0);
5721 if (GET_CODE (addr) == LO_SUM)
5722 /* For lo_sum addresses, we must allow any offset except one that
5723 causes a wrap, so test only the low 16 bits. */
5724 offset = ((offset & 0xffff) ^ 0x8000) - 0x8000;
5726 return offset + 0x8000 < 0x10000u - extra;
5729 /* Subroutines of rs6000_legitimize_address and rs6000_legitimate_address_p. */
5731 static bool
5732 reg_offset_addressing_ok_p (enum machine_mode mode)
5734 switch (mode)
5736 case V16QImode:
5737 case V8HImode:
5738 case V4SFmode:
5739 case V4SImode:
5740 case V2DFmode:
5741 case V2DImode:
5742 case TImode:
5743 /* AltiVec/VSX vector modes. Only reg+reg addressing is valid. While
5744 TImode is not a vector mode, if we want to use the VSX registers to
5745 move it around, we need to restrict ourselves to reg+reg
5746 addressing. */
5747 if (VECTOR_MEM_ALTIVEC_OR_VSX_P (mode))
5748 return false;
5749 break;
5751 case V4HImode:
5752 case V2SImode:
5753 case V1DImode:
5754 case V2SFmode:
5755 /* Paired vector modes. Only reg+reg addressing is valid. */
5756 if (TARGET_PAIRED_FLOAT)
5757 return false;
5758 break;
5760 case SDmode:
5761 /* If we can do direct load/stores of SDmode, restrict it to reg+reg
5762 addressing for the LFIWZX and STFIWX instructions. */
5763 if (TARGET_NO_SDMODE_STACK)
5764 return false;
5765 break;
5767 default:
5768 break;
5771 return true;
5774 static bool
5775 virtual_stack_registers_memory_p (rtx op)
5777 int regnum;
5779 if (GET_CODE (op) == REG)
5780 regnum = REGNO (op);
5782 else if (GET_CODE (op) == PLUS
5783 && GET_CODE (XEXP (op, 0)) == REG
5784 && GET_CODE (XEXP (op, 1)) == CONST_INT)
5785 regnum = REGNO (XEXP (op, 0));
5787 else
5788 return false;
5790 return (regnum >= FIRST_VIRTUAL_REGISTER
5791 && regnum <= LAST_VIRTUAL_POINTER_REGISTER);
5794 /* Return true if a MODE sized memory accesses to OP plus OFFSET
5795 is known to not straddle a 32k boundary. */
5797 static bool
5798 offsettable_ok_by_alignment (rtx op, HOST_WIDE_INT offset,
5799 enum machine_mode mode)
5801 tree decl, type;
5802 unsigned HOST_WIDE_INT dsize, dalign, lsb, mask;
5804 if (GET_CODE (op) != SYMBOL_REF)
5805 return false;
5807 dsize = GET_MODE_SIZE (mode);
5808 decl = SYMBOL_REF_DECL (op);
5809 if (!decl)
5811 if (dsize == 0)
5812 return false;
5814 /* -fsection-anchors loses the original SYMBOL_REF_DECL when
5815 replacing memory addresses with an anchor plus offset. We
5816 could find the decl by rummaging around in the block->objects
5817 VEC for the given offset but that seems like too much work. */
5818 dalign = BITS_PER_UNIT;
5819 if (SYMBOL_REF_HAS_BLOCK_INFO_P (op)
5820 && SYMBOL_REF_ANCHOR_P (op)
5821 && SYMBOL_REF_BLOCK (op) != NULL)
5823 struct object_block *block = SYMBOL_REF_BLOCK (op);
5825 dalign = block->alignment;
5826 offset += SYMBOL_REF_BLOCK_OFFSET (op);
5828 else if (CONSTANT_POOL_ADDRESS_P (op))
5830 /* It would be nice to have get_pool_align().. */
5831 enum machine_mode cmode = get_pool_mode (op);
5833 dalign = GET_MODE_ALIGNMENT (cmode);
5836 else if (DECL_P (decl))
5838 dalign = DECL_ALIGN (decl);
5840 if (dsize == 0)
5842 /* Allow BLKmode when the entire object is known to not
5843 cross a 32k boundary. */
5844 if (!DECL_SIZE_UNIT (decl))
5845 return false;
5847 if (!host_integerp (DECL_SIZE_UNIT (decl), 1))
5848 return false;
5850 dsize = tree_low_cst (DECL_SIZE_UNIT (decl), 1);
5851 if (dsize > 32768)
5852 return false;
5854 return dalign / BITS_PER_UNIT >= dsize;
5857 else
5859 type = TREE_TYPE (decl);
5861 dalign = TYPE_ALIGN (type);
5862 if (CONSTANT_CLASS_P (decl))
5863 dalign = CONSTANT_ALIGNMENT (decl, dalign);
5864 else
5865 dalign = DATA_ALIGNMENT (decl, dalign);
5867 if (dsize == 0)
5869 /* BLKmode, check the entire object. */
5870 if (TREE_CODE (decl) == STRING_CST)
5871 dsize = TREE_STRING_LENGTH (decl);
5872 else if (TYPE_SIZE_UNIT (type)
5873 && host_integerp (TYPE_SIZE_UNIT (type), 1))
5874 dsize = tree_low_cst (TYPE_SIZE_UNIT (type), 1);
5875 else
5876 return false;
5877 if (dsize > 32768)
5878 return false;
5880 return dalign / BITS_PER_UNIT >= dsize;
5884 /* Find how many bits of the alignment we know for this access. */
5885 mask = dalign / BITS_PER_UNIT - 1;
5886 lsb = offset & -offset;
5887 mask &= lsb - 1;
5888 dalign = mask + 1;
5890 return dalign >= dsize;
5893 static bool
5894 constant_pool_expr_p (rtx op)
5896 rtx base, offset;
5898 split_const (op, &base, &offset);
5899 return (GET_CODE (base) == SYMBOL_REF
5900 && CONSTANT_POOL_ADDRESS_P (base)
5901 && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (base), Pmode));
5904 static const_rtx tocrel_base, tocrel_offset;
5906 /* Return true if OP is a toc pointer relative address (the output
5907 of create_TOC_reference). If STRICT, do not match high part or
5908 non-split -mcmodel=large/medium toc pointer relative addresses. */
5910 bool
5911 toc_relative_expr_p (const_rtx op, bool strict)
5913 if (!TARGET_TOC)
5914 return false;
5916 if (TARGET_CMODEL != CMODEL_SMALL)
5918 /* Only match the low part. */
5919 if (GET_CODE (op) == LO_SUM
5920 && REG_P (XEXP (op, 0))
5921 && INT_REG_OK_FOR_BASE_P (XEXP (op, 0), strict))
5922 op = XEXP (op, 1);
5923 else if (strict)
5924 return false;
5927 tocrel_base = op;
5928 tocrel_offset = const0_rtx;
5929 if (GET_CODE (op) == PLUS && CONST_INT_P (XEXP (op, 1)))
5931 tocrel_base = XEXP (op, 0);
5932 tocrel_offset = XEXP (op, 1);
5935 return (GET_CODE (tocrel_base) == UNSPEC
5936 && XINT (tocrel_base, 1) == UNSPEC_TOCREL);
5939 /* Return true if X is a constant pool address, and also for cmodel=medium
5940 if X is a toc-relative address known to be offsettable within MODE. */
5942 bool
5943 legitimate_constant_pool_address_p (const_rtx x, enum machine_mode mode,
5944 bool strict)
5946 return (toc_relative_expr_p (x, strict)
5947 && (TARGET_CMODEL != CMODEL_MEDIUM
5948 || constant_pool_expr_p (XVECEXP (tocrel_base, 0, 0))
5949 || mode == QImode
5950 || offsettable_ok_by_alignment (XVECEXP (tocrel_base, 0, 0),
5951 INTVAL (tocrel_offset), mode)));
5954 static bool
5955 legitimate_small_data_p (enum machine_mode mode, rtx x)
5957 return (DEFAULT_ABI == ABI_V4
5958 && !flag_pic && !TARGET_TOC
5959 && (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == CONST)
5960 && small_data_operand (x, mode));
5963 /* SPE offset addressing is limited to 5-bits worth of double words. */
5964 #define SPE_CONST_OFFSET_OK(x) (((x) & ~0xf8) == 0)
5966 bool
5967 rs6000_legitimate_offset_address_p (enum machine_mode mode, rtx x,
5968 bool strict, bool worst_case)
5970 unsigned HOST_WIDE_INT offset;
5971 unsigned int extra;
5973 if (GET_CODE (x) != PLUS)
5974 return false;
5975 if (!REG_P (XEXP (x, 0)))
5976 return false;
5977 if (!INT_REG_OK_FOR_BASE_P (XEXP (x, 0), strict))
5978 return false;
5979 if (!reg_offset_addressing_ok_p (mode))
5980 return virtual_stack_registers_memory_p (x);
5981 if (legitimate_constant_pool_address_p (x, mode, strict))
5982 return true;
5983 if (GET_CODE (XEXP (x, 1)) != CONST_INT)
5984 return false;
5986 offset = INTVAL (XEXP (x, 1));
5987 extra = 0;
5988 switch (mode)
5990 case V4HImode:
5991 case V2SImode:
5992 case V1DImode:
5993 case V2SFmode:
5994 /* SPE vector modes. */
5995 return SPE_CONST_OFFSET_OK (offset);
5997 case DFmode:
5998 case DDmode:
5999 case DImode:
6000 /* On e500v2, we may have:
6002 (subreg:DF (mem:DI (plus (reg) (const_int))) 0).
6004 Which gets addressed with evldd instructions. */
6005 if (TARGET_E500_DOUBLE)
6006 return SPE_CONST_OFFSET_OK (offset);
6008 /* If we are using VSX scalar loads, restrict ourselves to reg+reg
6009 addressing. */
6010 if (VECTOR_MEM_VSX_P (mode))
6011 return false;
6013 if (!worst_case)
6014 break;
6015 if (!TARGET_POWERPC64)
6016 extra = 4;
6017 else if (offset & 3)
6018 return false;
6019 break;
6021 case TFmode:
6022 case TDmode:
6023 case TImode:
6024 case PTImode:
6025 if (TARGET_E500_DOUBLE)
6026 return (SPE_CONST_OFFSET_OK (offset)
6027 && SPE_CONST_OFFSET_OK (offset + 8));
6029 extra = 8;
6030 if (!worst_case)
6031 break;
6032 if (!TARGET_POWERPC64)
6033 extra = 12;
6034 else if (offset & 3)
6035 return false;
6036 break;
6038 default:
6039 break;
6042 offset += 0x8000;
6043 return offset < 0x10000 - extra;
6046 bool
6047 legitimate_indexed_address_p (rtx x, int strict)
6049 rtx op0, op1;
6051 if (GET_CODE (x) != PLUS)
6052 return false;
6054 op0 = XEXP (x, 0);
6055 op1 = XEXP (x, 1);
6057 /* Recognize the rtl generated by reload which we know will later be
6058 replaced with proper base and index regs. */
6059 if (!strict
6060 && reload_in_progress
6061 && (REG_P (op0) || GET_CODE (op0) == PLUS)
6062 && REG_P (op1))
6063 return true;
6065 return (REG_P (op0) && REG_P (op1)
6066 && ((INT_REG_OK_FOR_BASE_P (op0, strict)
6067 && INT_REG_OK_FOR_INDEX_P (op1, strict))
6068 || (INT_REG_OK_FOR_BASE_P (op1, strict)
6069 && INT_REG_OK_FOR_INDEX_P (op0, strict))));
6072 bool
6073 avoiding_indexed_address_p (enum machine_mode mode)
6075 /* Avoid indexed addressing for modes that have non-indexed
6076 load/store instruction forms. */
6077 return (TARGET_AVOID_XFORM && VECTOR_MEM_NONE_P (mode));
6080 bool
6081 legitimate_indirect_address_p (rtx x, int strict)
6083 return GET_CODE (x) == REG && INT_REG_OK_FOR_BASE_P (x, strict);
6086 bool
6087 macho_lo_sum_memory_operand (rtx x, enum machine_mode mode)
6089 if (!TARGET_MACHO || !flag_pic
6090 || mode != SImode || GET_CODE (x) != MEM)
6091 return false;
6092 x = XEXP (x, 0);
6094 if (GET_CODE (x) != LO_SUM)
6095 return false;
6096 if (GET_CODE (XEXP (x, 0)) != REG)
6097 return false;
6098 if (!INT_REG_OK_FOR_BASE_P (XEXP (x, 0), 0))
6099 return false;
6100 x = XEXP (x, 1);
6102 return CONSTANT_P (x);
6105 static bool
6106 legitimate_lo_sum_address_p (enum machine_mode mode, rtx x, int strict)
6108 if (GET_CODE (x) != LO_SUM)
6109 return false;
6110 if (GET_CODE (XEXP (x, 0)) != REG)
6111 return false;
6112 if (!INT_REG_OK_FOR_BASE_P (XEXP (x, 0), strict))
6113 return false;
6114 /* Restrict addressing for DI because of our SUBREG hackery. */
6115 if (TARGET_E500_DOUBLE && GET_MODE_SIZE (mode) > UNITS_PER_WORD)
6116 return false;
6117 x = XEXP (x, 1);
6119 if (TARGET_ELF || TARGET_MACHO)
6121 if (DEFAULT_ABI != ABI_AIX && DEFAULT_ABI != ABI_DARWIN && flag_pic)
6122 return false;
6123 if (TARGET_TOC)
6124 return false;
6125 if (GET_MODE_NUNITS (mode) != 1)
6126 return false;
6127 if (GET_MODE_SIZE (mode) > UNITS_PER_WORD
6128 && !(/* ??? Assume floating point reg based on mode? */
6129 TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT
6130 && (mode == DFmode || mode == DDmode)))
6131 return false;
6133 return CONSTANT_P (x);
6136 return false;
6140 /* Try machine-dependent ways of modifying an illegitimate address
6141 to be legitimate. If we find one, return the new, valid address.
6142 This is used from only one place: `memory_address' in explow.c.
6144 OLDX is the address as it was before break_out_memory_refs was
6145 called. In some cases it is useful to look at this to decide what
6146 needs to be done.
6148 It is always safe for this function to do nothing. It exists to
6149 recognize opportunities to optimize the output.
6151 On RS/6000, first check for the sum of a register with a constant
6152 integer that is out of range. If so, generate code to add the
6153 constant with the low-order 16 bits masked to the register and force
6154 this result into another register (this can be done with `cau').
6155 Then generate an address of REG+(CONST&0xffff), allowing for the
6156 possibility of bit 16 being a one.
6158 Then check for the sum of a register and something not constant, try to
6159 load the other things into a register and return the sum. */
6161 static rtx
6162 rs6000_legitimize_address (rtx x, rtx oldx ATTRIBUTE_UNUSED,
6163 enum machine_mode mode)
6165 unsigned int extra;
6167 if (!reg_offset_addressing_ok_p (mode))
6169 if (virtual_stack_registers_memory_p (x))
6170 return x;
6172 /* In theory we should not be seeing addresses of the form reg+0,
6173 but just in case it is generated, optimize it away. */
6174 if (GET_CODE (x) == PLUS && XEXP (x, 1) == const0_rtx)
6175 return force_reg (Pmode, XEXP (x, 0));
6177 /* For TImode with load/store quad, restrict addresses to just a single
6178 pointer, so it works with both GPRs and VSX registers. */
6179 /* Make sure both operands are registers. */
6180 else if (GET_CODE (x) == PLUS
6181 && (mode != TImode || !TARGET_QUAD_MEMORY))
6182 return gen_rtx_PLUS (Pmode,
6183 force_reg (Pmode, XEXP (x, 0)),
6184 force_reg (Pmode, XEXP (x, 1)));
6185 else
6186 return force_reg (Pmode, x);
6188 if (GET_CODE (x) == SYMBOL_REF)
6190 enum tls_model model = SYMBOL_REF_TLS_MODEL (x);
6191 if (model != 0)
6192 return rs6000_legitimize_tls_address (x, model);
6195 extra = 0;
6196 switch (mode)
6198 case TFmode:
6199 case TDmode:
6200 case TImode:
6201 case PTImode:
6202 /* As in legitimate_offset_address_p we do not assume
6203 worst-case. The mode here is just a hint as to the registers
6204 used. A TImode is usually in gprs, but may actually be in
6205 fprs. Leave worst-case scenario for reload to handle via
6206 insn constraints. PTImode is only GPRs. */
6207 extra = 8;
6208 break;
6209 default:
6210 break;
6213 if (GET_CODE (x) == PLUS
6214 && GET_CODE (XEXP (x, 0)) == REG
6215 && GET_CODE (XEXP (x, 1)) == CONST_INT
6216 && ((unsigned HOST_WIDE_INT) (INTVAL (XEXP (x, 1)) + 0x8000)
6217 >= 0x10000 - extra)
6218 && !(SPE_VECTOR_MODE (mode)
6219 || (TARGET_E500_DOUBLE && GET_MODE_SIZE (mode) > UNITS_PER_WORD)))
6221 HOST_WIDE_INT high_int, low_int;
6222 rtx sum;
6223 low_int = ((INTVAL (XEXP (x, 1)) & 0xffff) ^ 0x8000) - 0x8000;
6224 if (low_int >= 0x8000 - extra)
6225 low_int = 0;
6226 high_int = INTVAL (XEXP (x, 1)) - low_int;
6227 sum = force_operand (gen_rtx_PLUS (Pmode, XEXP (x, 0),
6228 GEN_INT (high_int)), 0);
6229 return plus_constant (Pmode, sum, low_int);
6231 else if (GET_CODE (x) == PLUS
6232 && GET_CODE (XEXP (x, 0)) == REG
6233 && GET_CODE (XEXP (x, 1)) != CONST_INT
6234 && GET_MODE_NUNITS (mode) == 1
6235 && (GET_MODE_SIZE (mode) <= UNITS_PER_WORD
6236 || (/* ??? Assume floating point reg based on mode? */
6237 (TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT)
6238 && (mode == DFmode || mode == DDmode)))
6239 && !avoiding_indexed_address_p (mode))
6241 return gen_rtx_PLUS (Pmode, XEXP (x, 0),
6242 force_reg (Pmode, force_operand (XEXP (x, 1), 0)));
6244 else if (SPE_VECTOR_MODE (mode)
6245 || (TARGET_E500_DOUBLE && GET_MODE_SIZE (mode) > UNITS_PER_WORD))
6247 if (mode == DImode)
6248 return x;
6249 /* We accept [reg + reg] and [reg + OFFSET]. */
6251 if (GET_CODE (x) == PLUS)
6253 rtx op1 = XEXP (x, 0);
6254 rtx op2 = XEXP (x, 1);
6255 rtx y;
6257 op1 = force_reg (Pmode, op1);
6259 if (GET_CODE (op2) != REG
6260 && (GET_CODE (op2) != CONST_INT
6261 || !SPE_CONST_OFFSET_OK (INTVAL (op2))
6262 || (GET_MODE_SIZE (mode) > 8
6263 && !SPE_CONST_OFFSET_OK (INTVAL (op2) + 8))))
6264 op2 = force_reg (Pmode, op2);
6266 /* We can't always do [reg + reg] for these, because [reg +
6267 reg + offset] is not a legitimate addressing mode. */
6268 y = gen_rtx_PLUS (Pmode, op1, op2);
6270 if ((GET_MODE_SIZE (mode) > 8 || mode == DDmode) && REG_P (op2))
6271 return force_reg (Pmode, y);
6272 else
6273 return y;
6276 return force_reg (Pmode, x);
6278 else if ((TARGET_ELF
6279 #if TARGET_MACHO
6280 || !MACHO_DYNAMIC_NO_PIC_P
6281 #endif
6283 && TARGET_32BIT
6284 && TARGET_NO_TOC
6285 && ! flag_pic
6286 && GET_CODE (x) != CONST_INT
6287 && GET_CODE (x) != CONST_DOUBLE
6288 && CONSTANT_P (x)
6289 && GET_MODE_NUNITS (mode) == 1
6290 && (GET_MODE_SIZE (mode) <= UNITS_PER_WORD
6291 || (/* ??? Assume floating point reg based on mode? */
6292 (TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT)
6293 && (mode == DFmode || mode == DDmode))))
6295 rtx reg = gen_reg_rtx (Pmode);
6296 if (TARGET_ELF)
6297 emit_insn (gen_elf_high (reg, x));
6298 else
6299 emit_insn (gen_macho_high (reg, x));
6300 return gen_rtx_LO_SUM (Pmode, reg, x);
6302 else if (TARGET_TOC
6303 && GET_CODE (x) == SYMBOL_REF
6304 && constant_pool_expr_p (x)
6305 && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (x), Pmode))
6306 return create_TOC_reference (x, NULL_RTX);
6307 else
6308 return x;
6311 /* Debug version of rs6000_legitimize_address. */
6312 static rtx
6313 rs6000_debug_legitimize_address (rtx x, rtx oldx, enum machine_mode mode)
6315 rtx ret;
6316 rtx insns;
6318 start_sequence ();
6319 ret = rs6000_legitimize_address (x, oldx, mode);
6320 insns = get_insns ();
6321 end_sequence ();
6323 if (ret != x)
6325 fprintf (stderr,
6326 "\nrs6000_legitimize_address: mode %s, old code %s, "
6327 "new code %s, modified\n",
6328 GET_MODE_NAME (mode), GET_RTX_NAME (GET_CODE (x)),
6329 GET_RTX_NAME (GET_CODE (ret)));
6331 fprintf (stderr, "Original address:\n");
6332 debug_rtx (x);
6334 fprintf (stderr, "oldx:\n");
6335 debug_rtx (oldx);
6337 fprintf (stderr, "New address:\n");
6338 debug_rtx (ret);
6340 if (insns)
6342 fprintf (stderr, "Insns added:\n");
6343 debug_rtx_list (insns, 20);
6346 else
6348 fprintf (stderr,
6349 "\nrs6000_legitimize_address: mode %s, code %s, no change:\n",
6350 GET_MODE_NAME (mode), GET_RTX_NAME (GET_CODE (x)));
6352 debug_rtx (x);
6355 if (insns)
6356 emit_insn (insns);
6358 return ret;
6361 /* This is called from dwarf2out.c via TARGET_ASM_OUTPUT_DWARF_DTPREL.
6362 We need to emit DTP-relative relocations. */
6364 static void rs6000_output_dwarf_dtprel (FILE *, int, rtx) ATTRIBUTE_UNUSED;
6365 static void
6366 rs6000_output_dwarf_dtprel (FILE *file, int size, rtx x)
6368 switch (size)
6370 case 4:
6371 fputs ("\t.long\t", file);
6372 break;
6373 case 8:
6374 fputs (DOUBLE_INT_ASM_OP, file);
6375 break;
6376 default:
6377 gcc_unreachable ();
6379 output_addr_const (file, x);
6380 fputs ("@dtprel+0x8000", file);
6383 /* In the name of slightly smaller debug output, and to cater to
6384 general assembler lossage, recognize various UNSPEC sequences
6385 and turn them back into a direct symbol reference. */
6387 static rtx
6388 rs6000_delegitimize_address (rtx orig_x)
6390 rtx x, y, offset;
6392 orig_x = delegitimize_mem_from_attrs (orig_x);
6393 x = orig_x;
6394 if (MEM_P (x))
6395 x = XEXP (x, 0);
6397 y = x;
6398 if (TARGET_CMODEL != CMODEL_SMALL
6399 && GET_CODE (y) == LO_SUM)
6400 y = XEXP (y, 1);
6402 offset = NULL_RTX;
6403 if (GET_CODE (y) == PLUS
6404 && GET_MODE (y) == Pmode
6405 && CONST_INT_P (XEXP (y, 1)))
6407 offset = XEXP (y, 1);
6408 y = XEXP (y, 0);
6411 if (GET_CODE (y) == UNSPEC
6412 && XINT (y, 1) == UNSPEC_TOCREL)
6414 #ifdef ENABLE_CHECKING
6415 if (REG_P (XVECEXP (y, 0, 1))
6416 && REGNO (XVECEXP (y, 0, 1)) == TOC_REGISTER)
6418 /* All good. */
6420 else if (GET_CODE (XVECEXP (y, 0, 1)) == DEBUG_EXPR)
6422 /* Weirdness alert. df_note_compute can replace r2 with a
6423 debug_expr when this unspec is in a debug_insn.
6424 Seen in gcc.dg/pr51957-1.c */
6426 else
6428 debug_rtx (orig_x);
6429 abort ();
6431 #endif
6432 y = XVECEXP (y, 0, 0);
6434 #ifdef HAVE_AS_TLS
6435 /* Do not associate thread-local symbols with the original
6436 constant pool symbol. */
6437 if (TARGET_XCOFF
6438 && GET_CODE (y) == SYMBOL_REF
6439 && CONSTANT_POOL_ADDRESS_P (y)
6440 && SYMBOL_REF_TLS_MODEL (get_pool_constant (y)) >= TLS_MODEL_REAL)
6441 return orig_x;
6442 #endif
6444 if (offset != NULL_RTX)
6445 y = gen_rtx_PLUS (Pmode, y, offset);
6446 if (!MEM_P (orig_x))
6447 return y;
6448 else
6449 return replace_equiv_address_nv (orig_x, y);
6452 if (TARGET_MACHO
6453 && GET_CODE (orig_x) == LO_SUM
6454 && GET_CODE (XEXP (orig_x, 1)) == CONST)
6456 y = XEXP (XEXP (orig_x, 1), 0);
6457 if (GET_CODE (y) == UNSPEC
6458 && XINT (y, 1) == UNSPEC_MACHOPIC_OFFSET)
6459 return XVECEXP (y, 0, 0);
6462 return orig_x;
6465 /* Return true if X shouldn't be emitted into the debug info.
6466 The linker doesn't like .toc section references from
6467 .debug_* sections, so reject .toc section symbols. */
6469 static bool
6470 rs6000_const_not_ok_for_debug_p (rtx x)
6472 if (GET_CODE (x) == SYMBOL_REF
6473 && CONSTANT_POOL_ADDRESS_P (x))
6475 rtx c = get_pool_constant (x);
6476 enum machine_mode cmode = get_pool_mode (x);
6477 if (ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (c, cmode))
6478 return true;
6481 return false;
6484 /* Construct the SYMBOL_REF for the tls_get_addr function. */
6486 static GTY(()) rtx rs6000_tls_symbol;
6487 static rtx
6488 rs6000_tls_get_addr (void)
6490 if (!rs6000_tls_symbol)
6491 rs6000_tls_symbol = init_one_libfunc ("__tls_get_addr");
6493 return rs6000_tls_symbol;
6496 /* Construct the SYMBOL_REF for TLS GOT references. */
6498 static GTY(()) rtx rs6000_got_symbol;
6499 static rtx
6500 rs6000_got_sym (void)
6502 if (!rs6000_got_symbol)
6504 rs6000_got_symbol = gen_rtx_SYMBOL_REF (Pmode, "_GLOBAL_OFFSET_TABLE_");
6505 SYMBOL_REF_FLAGS (rs6000_got_symbol) |= SYMBOL_FLAG_LOCAL;
6506 SYMBOL_REF_FLAGS (rs6000_got_symbol) |= SYMBOL_FLAG_EXTERNAL;
6509 return rs6000_got_symbol;
6512 /* AIX Thread-Local Address support. */
6514 static rtx
6515 rs6000_legitimize_tls_address_aix (rtx addr, enum tls_model model)
6517 rtx sym, mem, tocref, tlsreg, tmpreg, dest, tlsaddr;
6518 const char *name;
6519 char *tlsname;
6521 name = XSTR (addr, 0);
6522 /* Append TLS CSECT qualifier, unless the symbol already is qualified
6523 or the symbol will be in TLS private data section. */
6524 if (name[strlen (name) - 1] != ']'
6525 && (TREE_PUBLIC (SYMBOL_REF_DECL (addr))
6526 || bss_initializer_p (SYMBOL_REF_DECL (addr))))
6528 tlsname = XALLOCAVEC (char, strlen (name) + 4);
6529 strcpy (tlsname, name);
6530 strcat (tlsname,
6531 bss_initializer_p (SYMBOL_REF_DECL (addr)) ? "[UL]" : "[TL]");
6532 tlsaddr = copy_rtx (addr);
6533 XSTR (tlsaddr, 0) = ggc_strdup (tlsname);
6535 else
6536 tlsaddr = addr;
6538 /* Place addr into TOC constant pool. */
6539 sym = force_const_mem (GET_MODE (tlsaddr), tlsaddr);
6541 /* Output the TOC entry and create the MEM referencing the value. */
6542 if (constant_pool_expr_p (XEXP (sym, 0))
6543 && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (XEXP (sym, 0)), Pmode))
6545 tocref = create_TOC_reference (XEXP (sym, 0), NULL_RTX);
6546 mem = gen_const_mem (Pmode, tocref);
6547 set_mem_alias_set (mem, get_TOC_alias_set ());
6549 else
6550 return sym;
6552 /* Use global-dynamic for local-dynamic. */
6553 if (model == TLS_MODEL_GLOBAL_DYNAMIC
6554 || model == TLS_MODEL_LOCAL_DYNAMIC)
6556 /* Create new TOC reference for @m symbol. */
6557 name = XSTR (XVECEXP (XEXP (mem, 0), 0, 0), 0);
6558 tlsname = XALLOCAVEC (char, strlen (name) + 1);
6559 strcpy (tlsname, "*LCM");
6560 strcat (tlsname, name + 3);
6561 rtx modaddr = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (tlsname));
6562 SYMBOL_REF_FLAGS (modaddr) |= SYMBOL_FLAG_LOCAL;
6563 tocref = create_TOC_reference (modaddr, NULL_RTX);
6564 rtx modmem = gen_const_mem (Pmode, tocref);
6565 set_mem_alias_set (modmem, get_TOC_alias_set ());
6567 rtx modreg = gen_reg_rtx (Pmode);
6568 emit_insn (gen_rtx_SET (VOIDmode, modreg, modmem));
6570 tmpreg = gen_reg_rtx (Pmode);
6571 emit_insn (gen_rtx_SET (VOIDmode, tmpreg, mem));
6573 dest = gen_reg_rtx (Pmode);
6574 if (TARGET_32BIT)
6575 emit_insn (gen_tls_get_addrsi (dest, modreg, tmpreg));
6576 else
6577 emit_insn (gen_tls_get_addrdi (dest, modreg, tmpreg));
6578 return dest;
6580 /* Obtain TLS pointer: 32 bit call or 64 bit GPR 13. */
6581 else if (TARGET_32BIT)
6583 tlsreg = gen_reg_rtx (SImode);
6584 emit_insn (gen_tls_get_tpointer (tlsreg));
6586 else
6587 tlsreg = gen_rtx_REG (DImode, 13);
6589 /* Load the TOC value into temporary register. */
6590 tmpreg = gen_reg_rtx (Pmode);
6591 emit_insn (gen_rtx_SET (VOIDmode, tmpreg, mem));
6592 set_unique_reg_note (get_last_insn (), REG_EQUAL,
6593 gen_rtx_MINUS (Pmode, addr, tlsreg));
6595 /* Add TOC symbol value to TLS pointer. */
6596 dest = force_reg (Pmode, gen_rtx_PLUS (Pmode, tmpreg, tlsreg));
6598 return dest;
6601 /* ADDR contains a thread-local SYMBOL_REF. Generate code to compute
6602 this (thread-local) address. */
6604 static rtx
6605 rs6000_legitimize_tls_address (rtx addr, enum tls_model model)
6607 rtx dest, insn;
6609 if (TARGET_XCOFF)
6610 return rs6000_legitimize_tls_address_aix (addr, model);
6612 dest = gen_reg_rtx (Pmode);
6613 if (model == TLS_MODEL_LOCAL_EXEC && rs6000_tls_size == 16)
6615 rtx tlsreg;
6617 if (TARGET_64BIT)
6619 tlsreg = gen_rtx_REG (Pmode, 13);
6620 insn = gen_tls_tprel_64 (dest, tlsreg, addr);
6622 else
6624 tlsreg = gen_rtx_REG (Pmode, 2);
6625 insn = gen_tls_tprel_32 (dest, tlsreg, addr);
6627 emit_insn (insn);
6629 else if (model == TLS_MODEL_LOCAL_EXEC && rs6000_tls_size == 32)
6631 rtx tlsreg, tmp;
6633 tmp = gen_reg_rtx (Pmode);
6634 if (TARGET_64BIT)
6636 tlsreg = gen_rtx_REG (Pmode, 13);
6637 insn = gen_tls_tprel_ha_64 (tmp, tlsreg, addr);
6639 else
6641 tlsreg = gen_rtx_REG (Pmode, 2);
6642 insn = gen_tls_tprel_ha_32 (tmp, tlsreg, addr);
6644 emit_insn (insn);
6645 if (TARGET_64BIT)
6646 insn = gen_tls_tprel_lo_64 (dest, tmp, addr);
6647 else
6648 insn = gen_tls_tprel_lo_32 (dest, tmp, addr);
6649 emit_insn (insn);
6651 else
6653 rtx r3, got, tga, tmp1, tmp2, call_insn;
6655 /* We currently use relocations like @got@tlsgd for tls, which
6656 means the linker will handle allocation of tls entries, placing
6657 them in the .got section. So use a pointer to the .got section,
6658 not one to secondary TOC sections used by 64-bit -mminimal-toc,
6659 or to secondary GOT sections used by 32-bit -fPIC. */
6660 if (TARGET_64BIT)
6661 got = gen_rtx_REG (Pmode, 2);
6662 else
6664 if (flag_pic == 1)
6665 got = gen_rtx_REG (Pmode, RS6000_PIC_OFFSET_TABLE_REGNUM);
6666 else
6668 rtx gsym = rs6000_got_sym ();
6669 got = gen_reg_rtx (Pmode);
6670 if (flag_pic == 0)
6671 rs6000_emit_move (got, gsym, Pmode);
6672 else
6674 rtx mem, lab, last;
6676 tmp1 = gen_reg_rtx (Pmode);
6677 tmp2 = gen_reg_rtx (Pmode);
6678 mem = gen_const_mem (Pmode, tmp1);
6679 lab = gen_label_rtx ();
6680 emit_insn (gen_load_toc_v4_PIC_1b (gsym, lab));
6681 emit_move_insn (tmp1, gen_rtx_REG (Pmode, LR_REGNO));
6682 if (TARGET_LINK_STACK)
6683 emit_insn (gen_addsi3 (tmp1, tmp1, GEN_INT (4)));
6684 emit_move_insn (tmp2, mem);
6685 last = emit_insn (gen_addsi3 (got, tmp1, tmp2));
6686 set_unique_reg_note (last, REG_EQUAL, gsym);
6691 if (model == TLS_MODEL_GLOBAL_DYNAMIC)
6693 tga = rs6000_tls_get_addr ();
6694 emit_library_call_value (tga, dest, LCT_CONST, Pmode,
6695 1, const0_rtx, Pmode);
6697 r3 = gen_rtx_REG (Pmode, 3);
6698 if (DEFAULT_ABI == ABI_AIX && TARGET_64BIT)
6699 insn = gen_tls_gd_aix64 (r3, got, addr, tga, const0_rtx);
6700 else if (DEFAULT_ABI == ABI_AIX && !TARGET_64BIT)
6701 insn = gen_tls_gd_aix32 (r3, got, addr, tga, const0_rtx);
6702 else if (DEFAULT_ABI == ABI_V4)
6703 insn = gen_tls_gd_sysvsi (r3, got, addr, tga, const0_rtx);
6704 else
6705 gcc_unreachable ();
6706 call_insn = last_call_insn ();
6707 PATTERN (call_insn) = insn;
6708 if (DEFAULT_ABI == ABI_V4 && TARGET_SECURE_PLT && flag_pic)
6709 use_reg (&CALL_INSN_FUNCTION_USAGE (call_insn),
6710 pic_offset_table_rtx);
6712 else if (model == TLS_MODEL_LOCAL_DYNAMIC)
6714 tga = rs6000_tls_get_addr ();
6715 tmp1 = gen_reg_rtx (Pmode);
6716 emit_library_call_value (tga, tmp1, LCT_CONST, Pmode,
6717 1, const0_rtx, Pmode);
6719 r3 = gen_rtx_REG (Pmode, 3);
6720 if (DEFAULT_ABI == ABI_AIX && TARGET_64BIT)
6721 insn = gen_tls_ld_aix64 (r3, got, tga, const0_rtx);
6722 else if (DEFAULT_ABI == ABI_AIX && !TARGET_64BIT)
6723 insn = gen_tls_ld_aix32 (r3, got, tga, const0_rtx);
6724 else if (DEFAULT_ABI == ABI_V4)
6725 insn = gen_tls_ld_sysvsi (r3, got, tga, const0_rtx);
6726 else
6727 gcc_unreachable ();
6728 call_insn = last_call_insn ();
6729 PATTERN (call_insn) = insn;
6730 if (DEFAULT_ABI == ABI_V4 && TARGET_SECURE_PLT && flag_pic)
6731 use_reg (&CALL_INSN_FUNCTION_USAGE (call_insn),
6732 pic_offset_table_rtx);
6734 if (rs6000_tls_size == 16)
6736 if (TARGET_64BIT)
6737 insn = gen_tls_dtprel_64 (dest, tmp1, addr);
6738 else
6739 insn = gen_tls_dtprel_32 (dest, tmp1, addr);
6741 else if (rs6000_tls_size == 32)
6743 tmp2 = gen_reg_rtx (Pmode);
6744 if (TARGET_64BIT)
6745 insn = gen_tls_dtprel_ha_64 (tmp2, tmp1, addr);
6746 else
6747 insn = gen_tls_dtprel_ha_32 (tmp2, tmp1, addr);
6748 emit_insn (insn);
6749 if (TARGET_64BIT)
6750 insn = gen_tls_dtprel_lo_64 (dest, tmp2, addr);
6751 else
6752 insn = gen_tls_dtprel_lo_32 (dest, tmp2, addr);
6754 else
6756 tmp2 = gen_reg_rtx (Pmode);
6757 if (TARGET_64BIT)
6758 insn = gen_tls_got_dtprel_64 (tmp2, got, addr);
6759 else
6760 insn = gen_tls_got_dtprel_32 (tmp2, got, addr);
6761 emit_insn (insn);
6762 insn = gen_rtx_SET (Pmode, dest,
6763 gen_rtx_PLUS (Pmode, tmp2, tmp1));
6765 emit_insn (insn);
6767 else
6769 /* IE, or 64-bit offset LE. */
6770 tmp2 = gen_reg_rtx (Pmode);
6771 if (TARGET_64BIT)
6772 insn = gen_tls_got_tprel_64 (tmp2, got, addr);
6773 else
6774 insn = gen_tls_got_tprel_32 (tmp2, got, addr);
6775 emit_insn (insn);
6776 if (TARGET_64BIT)
6777 insn = gen_tls_tls_64 (dest, tmp2, addr);
6778 else
6779 insn = gen_tls_tls_32 (dest, tmp2, addr);
6780 emit_insn (insn);
6784 return dest;
6787 /* Return 1 if X contains a thread-local symbol. */
6789 static bool
6790 rs6000_tls_referenced_p (rtx x)
6792 if (! TARGET_HAVE_TLS)
6793 return false;
6795 return for_each_rtx (&x, &rs6000_tls_symbol_ref_1, 0);
6798 /* Implement TARGET_CANNOT_FORCE_CONST_MEM. */
6800 static bool
6801 rs6000_cannot_force_const_mem (enum machine_mode mode ATTRIBUTE_UNUSED, rtx x)
6803 if (GET_CODE (x) == HIGH
6804 && GET_CODE (XEXP (x, 0)) == UNSPEC)
6805 return true;
6807 /* A TLS symbol in the TOC cannot contain a sum. */
6808 if (GET_CODE (x) == CONST
6809 && GET_CODE (XEXP (x, 0)) == PLUS
6810 && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF
6811 && SYMBOL_REF_TLS_MODEL (XEXP (XEXP (x, 0), 0)) != 0)
6812 return true;
6814 /* Do not place an ELF TLS symbol in the constant pool. */
6815 return TARGET_ELF && rs6000_tls_referenced_p (x);
6818 /* Return 1 if *X is a thread-local symbol. This is the same as
6819 rs6000_tls_symbol_ref except for the type of the unused argument. */
6821 static int
6822 rs6000_tls_symbol_ref_1 (rtx *x, void *data ATTRIBUTE_UNUSED)
6824 return RS6000_SYMBOL_REF_TLS_P (*x);
6827 /* Return true iff the given SYMBOL_REF refers to a constant pool entry
6828 that we have put in the TOC, or for cmodel=medium, if the SYMBOL_REF
6829 can be addressed relative to the toc pointer. */
6831 static bool
6832 use_toc_relative_ref (rtx sym)
6834 return ((constant_pool_expr_p (sym)
6835 && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (sym),
6836 get_pool_mode (sym)))
6837 || (TARGET_CMODEL == CMODEL_MEDIUM
6838 && SYMBOL_REF_LOCAL_P (sym)));
6841 /* Our implementation of LEGITIMIZE_RELOAD_ADDRESS. Returns a value to
6842 replace the input X, or the original X if no replacement is called for.
6843 The output parameter *WIN is 1 if the calling macro should goto WIN,
6844 0 if it should not.
6846 For RS/6000, we wish to handle large displacements off a base
6847 register by splitting the addend across an addiu/addis and the mem insn.
6848 This cuts number of extra insns needed from 3 to 1.
6850 On Darwin, we use this to generate code for floating point constants.
6851 A movsf_low is generated so we wind up with 2 instructions rather than 3.
6852 The Darwin code is inside #if TARGET_MACHO because only then are the
6853 machopic_* functions defined. */
6854 static rtx
6855 rs6000_legitimize_reload_address (rtx x, enum machine_mode mode,
6856 int opnum, int type,
6857 int ind_levels ATTRIBUTE_UNUSED, int *win)
6859 bool reg_offset_p = reg_offset_addressing_ok_p (mode);
6861 /* Nasty hack for vsx_splat_V2DF/V2DI load from mem, which takes a
6862 DFmode/DImode MEM. */
6863 if (reg_offset_p
6864 && opnum == 1
6865 && ((mode == DFmode && recog_data.operand_mode[0] == V2DFmode)
6866 || (mode == DImode && recog_data.operand_mode[0] == V2DImode)))
6867 reg_offset_p = false;
6869 /* We must recognize output that we have already generated ourselves. */
6870 if (GET_CODE (x) == PLUS
6871 && GET_CODE (XEXP (x, 0)) == PLUS
6872 && GET_CODE (XEXP (XEXP (x, 0), 0)) == REG
6873 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6874 && GET_CODE (XEXP (x, 1)) == CONST_INT)
6876 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
6877 BASE_REG_CLASS, GET_MODE (x), VOIDmode, 0, 0,
6878 opnum, (enum reload_type) type);
6879 *win = 1;
6880 return x;
6883 /* Likewise for (lo_sum (high ...) ...) output we have generated. */
6884 if (GET_CODE (x) == LO_SUM
6885 && GET_CODE (XEXP (x, 0)) == HIGH)
6887 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
6888 BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
6889 opnum, (enum reload_type) type);
6890 *win = 1;
6891 return x;
6894 #if TARGET_MACHO
6895 if (DEFAULT_ABI == ABI_DARWIN && flag_pic
6896 && GET_CODE (x) == LO_SUM
6897 && GET_CODE (XEXP (x, 0)) == PLUS
6898 && XEXP (XEXP (x, 0), 0) == pic_offset_table_rtx
6899 && GET_CODE (XEXP (XEXP (x, 0), 1)) == HIGH
6900 && XEXP (XEXP (XEXP (x, 0), 1), 0) == XEXP (x, 1)
6901 && machopic_operand_p (XEXP (x, 1)))
6903 /* Result of previous invocation of this function on Darwin
6904 floating point constant. */
6905 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
6906 BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
6907 opnum, (enum reload_type) type);
6908 *win = 1;
6909 return x;
6911 #endif
6913 if (TARGET_CMODEL != CMODEL_SMALL
6914 && reg_offset_p
6915 && small_toc_ref (x, VOIDmode))
6917 rtx hi = gen_rtx_HIGH (Pmode, copy_rtx (x));
6918 x = gen_rtx_LO_SUM (Pmode, hi, x);
6919 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
6920 BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
6921 opnum, (enum reload_type) type);
6922 *win = 1;
6923 return x;
6926 if (GET_CODE (x) == PLUS
6927 && GET_CODE (XEXP (x, 0)) == REG
6928 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER
6929 && INT_REG_OK_FOR_BASE_P (XEXP (x, 0), 1)
6930 && GET_CODE (XEXP (x, 1)) == CONST_INT
6931 && reg_offset_p
6932 && !SPE_VECTOR_MODE (mode)
6933 && !(TARGET_E500_DOUBLE && GET_MODE_SIZE (mode) > UNITS_PER_WORD)
6934 && (!VECTOR_MODE_P (mode) || VECTOR_MEM_NONE_P (mode)))
6936 HOST_WIDE_INT val = INTVAL (XEXP (x, 1));
6937 HOST_WIDE_INT low = ((val & 0xffff) ^ 0x8000) - 0x8000;
6938 HOST_WIDE_INT high
6939 = (((val - low) & 0xffffffff) ^ 0x80000000) - 0x80000000;
6941 /* Check for 32-bit overflow. */
6942 if (high + low != val)
6944 *win = 0;
6945 return x;
6948 /* Reload the high part into a base reg; leave the low part
6949 in the mem directly. */
6951 x = gen_rtx_PLUS (GET_MODE (x),
6952 gen_rtx_PLUS (GET_MODE (x), XEXP (x, 0),
6953 GEN_INT (high)),
6954 GEN_INT (low));
6956 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
6957 BASE_REG_CLASS, GET_MODE (x), VOIDmode, 0, 0,
6958 opnum, (enum reload_type) type);
6959 *win = 1;
6960 return x;
6963 if (GET_CODE (x) == SYMBOL_REF
6964 && reg_offset_p
6965 && (!VECTOR_MODE_P (mode) || VECTOR_MEM_NONE_P (mode))
6966 && !SPE_VECTOR_MODE (mode)
6967 #if TARGET_MACHO
6968 && DEFAULT_ABI == ABI_DARWIN
6969 && (flag_pic || MACHO_DYNAMIC_NO_PIC_P)
6970 && machopic_symbol_defined_p (x)
6971 #else
6972 && DEFAULT_ABI == ABI_V4
6973 && !flag_pic
6974 #endif
6975 /* Don't do this for TFmode or TDmode, since the result isn't offsettable.
6976 The same goes for DImode without 64-bit gprs and DFmode and DDmode
6977 without fprs.
6978 ??? Assume floating point reg based on mode? This assumption is
6979 violated by eg. powerpc-linux -m32 compile of gcc.dg/pr28796-2.c
6980 where reload ends up doing a DFmode load of a constant from
6981 mem using two gprs. Unfortunately, at this point reload
6982 hasn't yet selected regs so poking around in reload data
6983 won't help and even if we could figure out the regs reliably,
6984 we'd still want to allow this transformation when the mem is
6985 naturally aligned. Since we say the address is good here, we
6986 can't disable offsets from LO_SUMs in mem_operand_gpr.
6987 FIXME: Allow offset from lo_sum for other modes too, when
6988 mem is sufficiently aligned. */
6989 && mode != TFmode
6990 && mode != TDmode
6991 && (mode != TImode || !TARGET_VSX_TIMODE)
6992 && mode != PTImode
6993 && (mode != DImode || TARGET_POWERPC64)
6994 && ((mode != DFmode && mode != DDmode) || TARGET_POWERPC64
6995 || (TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT)))
6997 #if TARGET_MACHO
6998 if (flag_pic)
7000 rtx offset = machopic_gen_offset (x);
7001 x = gen_rtx_LO_SUM (GET_MODE (x),
7002 gen_rtx_PLUS (Pmode, pic_offset_table_rtx,
7003 gen_rtx_HIGH (Pmode, offset)), offset);
7005 else
7006 #endif
7007 x = gen_rtx_LO_SUM (GET_MODE (x),
7008 gen_rtx_HIGH (Pmode, x), x);
7010 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
7011 BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
7012 opnum, (enum reload_type) type);
7013 *win = 1;
7014 return x;
7017 /* Reload an offset address wrapped by an AND that represents the
7018 masking of the lower bits. Strip the outer AND and let reload
7019 convert the offset address into an indirect address. For VSX,
7020 force reload to create the address with an AND in a separate
7021 register, because we can't guarantee an altivec register will
7022 be used. */
7023 if (VECTOR_MEM_ALTIVEC_P (mode)
7024 && GET_CODE (x) == AND
7025 && GET_CODE (XEXP (x, 0)) == PLUS
7026 && GET_CODE (XEXP (XEXP (x, 0), 0)) == REG
7027 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
7028 && GET_CODE (XEXP (x, 1)) == CONST_INT
7029 && INTVAL (XEXP (x, 1)) == -16)
7031 x = XEXP (x, 0);
7032 *win = 1;
7033 return x;
7036 if (TARGET_TOC
7037 && reg_offset_p
7038 && GET_CODE (x) == SYMBOL_REF
7039 && use_toc_relative_ref (x))
7041 x = create_TOC_reference (x, NULL_RTX);
7042 if (TARGET_CMODEL != CMODEL_SMALL)
7043 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
7044 BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
7045 opnum, (enum reload_type) type);
7046 *win = 1;
7047 return x;
7049 *win = 0;
7050 return x;
7053 /* Debug version of rs6000_legitimize_reload_address. */
7054 static rtx
7055 rs6000_debug_legitimize_reload_address (rtx x, enum machine_mode mode,
7056 int opnum, int type,
7057 int ind_levels, int *win)
7059 rtx ret = rs6000_legitimize_reload_address (x, mode, opnum, type,
7060 ind_levels, win);
7061 fprintf (stderr,
7062 "\nrs6000_legitimize_reload_address: mode = %s, opnum = %d, "
7063 "type = %d, ind_levels = %d, win = %d, original addr:\n",
7064 GET_MODE_NAME (mode), opnum, type, ind_levels, *win);
7065 debug_rtx (x);
7067 if (x == ret)
7068 fprintf (stderr, "Same address returned\n");
7069 else if (!ret)
7070 fprintf (stderr, "NULL returned\n");
7071 else
7073 fprintf (stderr, "New address:\n");
7074 debug_rtx (ret);
7077 return ret;
7080 /* TARGET_LEGITIMATE_ADDRESS_P recognizes an RTL expression
7081 that is a valid memory address for an instruction.
7082 The MODE argument is the machine mode for the MEM expression
7083 that wants to use this address.
7085 On the RS/6000, there are four valid address: a SYMBOL_REF that
7086 refers to a constant pool entry of an address (or the sum of it
7087 plus a constant), a short (16-bit signed) constant plus a register,
7088 the sum of two registers, or a register indirect, possibly with an
7089 auto-increment. For DFmode, DDmode and DImode with a constant plus
7090 register, we must ensure that both words are addressable or PowerPC64
7091 with offset word aligned.
7093 For modes spanning multiple registers (DFmode and DDmode in 32-bit GPRs,
7094 32-bit DImode, TImode, TFmode, TDmode), indexed addressing cannot be used
7095 because adjacent memory cells are accessed by adding word-sized offsets
7096 during assembly output. */
7097 static bool
7098 rs6000_legitimate_address_p (enum machine_mode mode, rtx x, bool reg_ok_strict)
7100 bool reg_offset_p = reg_offset_addressing_ok_p (mode);
7102 /* If this is an unaligned stvx/ldvx type address, discard the outer AND. */
7103 if (VECTOR_MEM_ALTIVEC_P (mode)
7104 && GET_CODE (x) == AND
7105 && GET_CODE (XEXP (x, 1)) == CONST_INT
7106 && INTVAL (XEXP (x, 1)) == -16)
7107 x = XEXP (x, 0);
7109 if (TARGET_ELF && RS6000_SYMBOL_REF_TLS_P (x))
7110 return 0;
7111 if (legitimate_indirect_address_p (x, reg_ok_strict))
7112 return 1;
7113 if ((GET_CODE (x) == PRE_INC || GET_CODE (x) == PRE_DEC)
7114 && !ALTIVEC_OR_VSX_VECTOR_MODE (mode)
7115 && !SPE_VECTOR_MODE (mode)
7116 && mode != TFmode
7117 && mode != TDmode
7118 && mode != TImode
7119 && mode != PTImode
7120 /* Restrict addressing for DI because of our SUBREG hackery. */
7121 && !(TARGET_E500_DOUBLE
7122 && (mode == DFmode || mode == DDmode || mode == DImode))
7123 && TARGET_UPDATE
7124 && legitimate_indirect_address_p (XEXP (x, 0), reg_ok_strict))
7125 return 1;
7126 if (virtual_stack_registers_memory_p (x))
7127 return 1;
7128 if (reg_offset_p && legitimate_small_data_p (mode, x))
7129 return 1;
7130 if (reg_offset_p
7131 && legitimate_constant_pool_address_p (x, mode, reg_ok_strict))
7132 return 1;
7133 /* For TImode, if we have load/store quad, only allow register indirect
7134 addresses. This will allow the values to go in either GPRs or VSX
7135 registers without reloading. The vector types would tend to go into VSX
7136 registers, so we allow REG+REG, while TImode seems somewhat split, in that
7137 some uses are GPR based, and some VSX based. */
7138 if (mode == TImode && TARGET_QUAD_MEMORY)
7139 return 0;
7140 /* If not REG_OK_STRICT (before reload) let pass any stack offset. */
7141 if (! reg_ok_strict
7142 && reg_offset_p
7143 && GET_CODE (x) == PLUS
7144 && GET_CODE (XEXP (x, 0)) == REG
7145 && (XEXP (x, 0) == virtual_stack_vars_rtx
7146 || XEXP (x, 0) == arg_pointer_rtx)
7147 && GET_CODE (XEXP (x, 1)) == CONST_INT)
7148 return 1;
7149 if (rs6000_legitimate_offset_address_p (mode, x, reg_ok_strict, false))
7150 return 1;
7151 if (mode != TFmode
7152 && mode != TDmode
7153 && ((TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT)
7154 || TARGET_POWERPC64
7155 || (mode != DFmode && mode != DDmode)
7156 || (TARGET_E500_DOUBLE && mode != DDmode))
7157 && (TARGET_POWERPC64 || mode != DImode)
7158 && (mode != TImode || VECTOR_MEM_VSX_P (TImode))
7159 && mode != PTImode
7160 && !avoiding_indexed_address_p (mode)
7161 && legitimate_indexed_address_p (x, reg_ok_strict))
7162 return 1;
7163 if (GET_CODE (x) == PRE_MODIFY
7164 && mode != TImode
7165 && mode != PTImode
7166 && mode != TFmode
7167 && mode != TDmode
7168 && ((TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT)
7169 || TARGET_POWERPC64
7170 || ((mode != DFmode && mode != DDmode) || TARGET_E500_DOUBLE))
7171 && (TARGET_POWERPC64 || mode != DImode)
7172 && !ALTIVEC_OR_VSX_VECTOR_MODE (mode)
7173 && !SPE_VECTOR_MODE (mode)
7174 /* Restrict addressing for DI because of our SUBREG hackery. */
7175 && !(TARGET_E500_DOUBLE
7176 && (mode == DFmode || mode == DDmode || mode == DImode))
7177 && TARGET_UPDATE
7178 && legitimate_indirect_address_p (XEXP (x, 0), reg_ok_strict)
7179 && (rs6000_legitimate_offset_address_p (mode, XEXP (x, 1),
7180 reg_ok_strict, false)
7181 || (!avoiding_indexed_address_p (mode)
7182 && legitimate_indexed_address_p (XEXP (x, 1), reg_ok_strict)))
7183 && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
7184 return 1;
7185 if (reg_offset_p && legitimate_lo_sum_address_p (mode, x, reg_ok_strict))
7186 return 1;
7187 return 0;
7190 /* Debug version of rs6000_legitimate_address_p. */
7191 static bool
7192 rs6000_debug_legitimate_address_p (enum machine_mode mode, rtx x,
7193 bool reg_ok_strict)
7195 bool ret = rs6000_legitimate_address_p (mode, x, reg_ok_strict);
7196 fprintf (stderr,
7197 "\nrs6000_legitimate_address_p: return = %s, mode = %s, "
7198 "strict = %d, code = %s\n",
7199 ret ? "true" : "false",
7200 GET_MODE_NAME (mode),
7201 reg_ok_strict,
7202 GET_RTX_NAME (GET_CODE (x)));
7203 debug_rtx (x);
7205 return ret;
7208 /* Implement TARGET_MODE_DEPENDENT_ADDRESS_P. */
7210 static bool
7211 rs6000_mode_dependent_address_p (const_rtx addr,
7212 addr_space_t as ATTRIBUTE_UNUSED)
7214 return rs6000_mode_dependent_address_ptr (addr);
7217 /* Go to LABEL if ADDR (a legitimate address expression)
7218 has an effect that depends on the machine mode it is used for.
7220 On the RS/6000 this is true of all integral offsets (since AltiVec
7221 and VSX modes don't allow them) or is a pre-increment or decrement.
7223 ??? Except that due to conceptual problems in offsettable_address_p
7224 we can't really report the problems of integral offsets. So leave
7225 this assuming that the adjustable offset must be valid for the
7226 sub-words of a TFmode operand, which is what we had before. */
7228 static bool
7229 rs6000_mode_dependent_address (const_rtx addr)
7231 switch (GET_CODE (addr))
7233 case PLUS:
7234 /* Any offset from virtual_stack_vars_rtx and arg_pointer_rtx
7235 is considered a legitimate address before reload, so there
7236 are no offset restrictions in that case. Note that this
7237 condition is safe in strict mode because any address involving
7238 virtual_stack_vars_rtx or arg_pointer_rtx would already have
7239 been rejected as illegitimate. */
7240 if (XEXP (addr, 0) != virtual_stack_vars_rtx
7241 && XEXP (addr, 0) != arg_pointer_rtx
7242 && GET_CODE (XEXP (addr, 1)) == CONST_INT)
7244 unsigned HOST_WIDE_INT val = INTVAL (XEXP (addr, 1));
7245 return val + 0x8000 >= 0x10000 - (TARGET_POWERPC64 ? 8 : 12);
7247 break;
7249 case LO_SUM:
7250 /* Anything in the constant pool is sufficiently aligned that
7251 all bytes have the same high part address. */
7252 return !legitimate_constant_pool_address_p (addr, QImode, false);
7254 /* Auto-increment cases are now treated generically in recog.c. */
7255 case PRE_MODIFY:
7256 return TARGET_UPDATE;
7258 /* AND is only allowed in Altivec loads. */
7259 case AND:
7260 return true;
7262 default:
7263 break;
7266 return false;
7269 /* Debug version of rs6000_mode_dependent_address. */
7270 static bool
7271 rs6000_debug_mode_dependent_address (const_rtx addr)
7273 bool ret = rs6000_mode_dependent_address (addr);
7275 fprintf (stderr, "\nrs6000_mode_dependent_address: ret = %s\n",
7276 ret ? "true" : "false");
7277 debug_rtx (addr);
7279 return ret;
7282 /* Implement FIND_BASE_TERM. */
7285 rs6000_find_base_term (rtx op)
7287 rtx base;
7289 base = op;
7290 if (GET_CODE (base) == CONST)
7291 base = XEXP (base, 0);
7292 if (GET_CODE (base) == PLUS)
7293 base = XEXP (base, 0);
7294 if (GET_CODE (base) == UNSPEC)
7295 switch (XINT (base, 1))
7297 case UNSPEC_TOCREL:
7298 case UNSPEC_MACHOPIC_OFFSET:
7299 /* OP represents SYM [+ OFFSET] - ANCHOR. SYM is the base term
7300 for aliasing purposes. */
7301 return XVECEXP (base, 0, 0);
7304 return op;
7307 /* More elaborate version of recog's offsettable_memref_p predicate
7308 that works around the ??? note of rs6000_mode_dependent_address.
7309 In particular it accepts
7311 (mem:DI (plus:SI (reg/f:SI 31 31) (const_int 32760 [0x7ff8])))
7313 in 32-bit mode, that the recog predicate rejects. */
7315 static bool
7316 rs6000_offsettable_memref_p (rtx op, enum machine_mode reg_mode)
7318 bool worst_case;
7320 if (!MEM_P (op))
7321 return false;
7323 /* First mimic offsettable_memref_p. */
7324 if (offsettable_address_p (true, GET_MODE (op), XEXP (op, 0)))
7325 return true;
7327 /* offsettable_address_p invokes rs6000_mode_dependent_address, but
7328 the latter predicate knows nothing about the mode of the memory
7329 reference and, therefore, assumes that it is the largest supported
7330 mode (TFmode). As a consequence, legitimate offsettable memory
7331 references are rejected. rs6000_legitimate_offset_address_p contains
7332 the correct logic for the PLUS case of rs6000_mode_dependent_address,
7333 at least with a little bit of help here given that we know the
7334 actual registers used. */
7335 worst_case = ((TARGET_POWERPC64 && GET_MODE_CLASS (reg_mode) == MODE_INT)
7336 || GET_MODE_SIZE (reg_mode) == 4);
7337 return rs6000_legitimate_offset_address_p (GET_MODE (op), XEXP (op, 0),
7338 true, worst_case);
7341 /* Change register usage conditional on target flags. */
7342 static void
7343 rs6000_conditional_register_usage (void)
7345 int i;
7347 if (TARGET_DEBUG_TARGET)
7348 fprintf (stderr, "rs6000_conditional_register_usage called\n");
7350 /* Set MQ register fixed (already call_used) so that it will not be
7351 allocated. */
7352 fixed_regs[64] = 1;
7354 /* 64-bit AIX and Linux reserve GPR13 for thread-private data. */
7355 if (TARGET_64BIT)
7356 fixed_regs[13] = call_used_regs[13]
7357 = call_really_used_regs[13] = 1;
7359 /* Conditionally disable FPRs. */
7360 if (TARGET_SOFT_FLOAT || !TARGET_FPRS)
7361 for (i = 32; i < 64; i++)
7362 fixed_regs[i] = call_used_regs[i]
7363 = call_really_used_regs[i] = 1;
7365 /* The TOC register is not killed across calls in a way that is
7366 visible to the compiler. */
7367 if (DEFAULT_ABI == ABI_AIX)
7368 call_really_used_regs[2] = 0;
7370 if (DEFAULT_ABI == ABI_V4
7371 && PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
7372 && flag_pic == 2)
7373 fixed_regs[RS6000_PIC_OFFSET_TABLE_REGNUM] = 1;
7375 if (DEFAULT_ABI == ABI_V4
7376 && PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
7377 && flag_pic == 1)
7378 fixed_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
7379 = call_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
7380 = call_really_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM] = 1;
7382 if (DEFAULT_ABI == ABI_DARWIN
7383 && PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM)
7384 fixed_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
7385 = call_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
7386 = call_really_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM] = 1;
7388 if (TARGET_TOC && TARGET_MINIMAL_TOC)
7389 fixed_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
7390 = call_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM] = 1;
7392 if (TARGET_SPE)
7394 global_regs[SPEFSCR_REGNO] = 1;
7395 /* We used to use r14 as FIXED_SCRATCH to address SPE 64-bit
7396 registers in prologues and epilogues. We no longer use r14
7397 for FIXED_SCRATCH, but we're keeping r14 out of the allocation
7398 pool for link-compatibility with older versions of GCC. Once
7399 "old" code has died out, we can return r14 to the allocation
7400 pool. */
7401 fixed_regs[14]
7402 = call_used_regs[14]
7403 = call_really_used_regs[14] = 1;
7406 if (!TARGET_ALTIVEC && !TARGET_VSX)
7408 for (i = FIRST_ALTIVEC_REGNO; i <= LAST_ALTIVEC_REGNO; ++i)
7409 fixed_regs[i] = call_used_regs[i] = call_really_used_regs[i] = 1;
7410 call_really_used_regs[VRSAVE_REGNO] = 1;
7413 if (TARGET_ALTIVEC || TARGET_VSX)
7414 global_regs[VSCR_REGNO] = 1;
7416 if (TARGET_ALTIVEC_ABI)
7418 for (i = FIRST_ALTIVEC_REGNO; i < FIRST_ALTIVEC_REGNO + 20; ++i)
7419 call_used_regs[i] = call_really_used_regs[i] = 1;
7421 /* AIX reserves VR20:31 in non-extended ABI mode. */
7422 if (TARGET_XCOFF)
7423 for (i = FIRST_ALTIVEC_REGNO + 20; i < FIRST_ALTIVEC_REGNO + 32; ++i)
7424 fixed_regs[i] = call_used_regs[i] = call_really_used_regs[i] = 1;
7428 /* Try to output insns to set TARGET equal to the constant C if it can
7429 be done in less than N insns. Do all computations in MODE.
7430 Returns the place where the output has been placed if it can be
7431 done and the insns have been emitted. If it would take more than N
7432 insns, zero is returned and no insns and emitted. */
7435 rs6000_emit_set_const (rtx dest, enum machine_mode mode,
7436 rtx source, int n ATTRIBUTE_UNUSED)
7438 rtx result, insn, set;
7439 HOST_WIDE_INT c0, c1;
7441 switch (mode)
7443 case QImode:
7444 case HImode:
7445 if (dest == NULL)
7446 dest = gen_reg_rtx (mode);
7447 emit_insn (gen_rtx_SET (VOIDmode, dest, source));
7448 return dest;
7450 case SImode:
7451 result = !can_create_pseudo_p () ? dest : gen_reg_rtx (SImode);
7453 emit_insn (gen_rtx_SET (VOIDmode, copy_rtx (result),
7454 GEN_INT (INTVAL (source)
7455 & (~ (HOST_WIDE_INT) 0xffff))));
7456 emit_insn (gen_rtx_SET (VOIDmode, dest,
7457 gen_rtx_IOR (SImode, copy_rtx (result),
7458 GEN_INT (INTVAL (source) & 0xffff))));
7459 result = dest;
7460 break;
7462 case DImode:
7463 switch (GET_CODE (source))
7465 case CONST_INT:
7466 c0 = INTVAL (source);
7467 c1 = -(c0 < 0);
7468 break;
7470 default:
7471 gcc_unreachable ();
7474 result = rs6000_emit_set_long_const (dest, c0, c1);
7475 break;
7477 default:
7478 gcc_unreachable ();
7481 insn = get_last_insn ();
7482 set = single_set (insn);
7483 if (! CONSTANT_P (SET_SRC (set)))
7484 set_unique_reg_note (insn, REG_EQUAL, source);
7486 return result;
7489 /* Having failed to find a 3 insn sequence in rs6000_emit_set_const,
7490 fall back to a straight forward decomposition. We do this to avoid
7491 exponential run times encountered when looking for longer sequences
7492 with rs6000_emit_set_const. */
7493 static rtx
7494 rs6000_emit_set_long_const (rtx dest, HOST_WIDE_INT c1, HOST_WIDE_INT c2)
7496 if (!TARGET_POWERPC64)
7498 rtx operand1, operand2;
7500 operand1 = operand_subword_force (dest, WORDS_BIG_ENDIAN == 0,
7501 DImode);
7502 operand2 = operand_subword_force (copy_rtx (dest), WORDS_BIG_ENDIAN != 0,
7503 DImode);
7504 emit_move_insn (operand1, GEN_INT (c1));
7505 emit_move_insn (operand2, GEN_INT (c2));
7507 else
7509 HOST_WIDE_INT ud1, ud2, ud3, ud4;
7511 ud1 = c1 & 0xffff;
7512 ud2 = (c1 & 0xffff0000) >> 16;
7513 c2 = c1 >> 32;
7514 ud3 = c2 & 0xffff;
7515 ud4 = (c2 & 0xffff0000) >> 16;
7517 if ((ud4 == 0xffff && ud3 == 0xffff && ud2 == 0xffff && (ud1 & 0x8000))
7518 || (ud4 == 0 && ud3 == 0 && ud2 == 0 && ! (ud1 & 0x8000)))
7519 emit_move_insn (dest, GEN_INT ((ud1 ^ 0x8000) - 0x8000));
7521 else if ((ud4 == 0xffff && ud3 == 0xffff && (ud2 & 0x8000))
7522 || (ud4 == 0 && ud3 == 0 && ! (ud2 & 0x8000)))
7524 emit_move_insn (dest, GEN_INT (((ud2 << 16) ^ 0x80000000)
7525 - 0x80000000));
7526 if (ud1 != 0)
7527 emit_move_insn (copy_rtx (dest),
7528 gen_rtx_IOR (DImode, copy_rtx (dest),
7529 GEN_INT (ud1)));
7531 else if (ud3 == 0 && ud4 == 0)
7533 gcc_assert (ud2 & 0x8000);
7534 emit_move_insn (dest, GEN_INT (((ud2 << 16) ^ 0x80000000)
7535 - 0x80000000));
7536 if (ud1 != 0)
7537 emit_move_insn (copy_rtx (dest),
7538 gen_rtx_IOR (DImode, copy_rtx (dest),
7539 GEN_INT (ud1)));
7540 emit_move_insn (copy_rtx (dest),
7541 gen_rtx_ZERO_EXTEND (DImode,
7542 gen_lowpart (SImode,
7543 copy_rtx (dest))));
7545 else if ((ud4 == 0xffff && (ud3 & 0x8000))
7546 || (ud4 == 0 && ! (ud3 & 0x8000)))
7548 emit_move_insn (dest, GEN_INT (((ud3 << 16) ^ 0x80000000)
7549 - 0x80000000));
7550 if (ud2 != 0)
7551 emit_move_insn (copy_rtx (dest),
7552 gen_rtx_IOR (DImode, copy_rtx (dest),
7553 GEN_INT (ud2)));
7554 emit_move_insn (copy_rtx (dest),
7555 gen_rtx_ASHIFT (DImode, copy_rtx (dest),
7556 GEN_INT (16)));
7557 if (ud1 != 0)
7558 emit_move_insn (copy_rtx (dest),
7559 gen_rtx_IOR (DImode, copy_rtx (dest),
7560 GEN_INT (ud1)));
7562 else
7564 emit_move_insn (dest, GEN_INT (((ud4 << 16) ^ 0x80000000)
7565 - 0x80000000));
7566 if (ud3 != 0)
7567 emit_move_insn (copy_rtx (dest),
7568 gen_rtx_IOR (DImode, copy_rtx (dest),
7569 GEN_INT (ud3)));
7571 emit_move_insn (copy_rtx (dest),
7572 gen_rtx_ASHIFT (DImode, copy_rtx (dest),
7573 GEN_INT (32)));
7574 if (ud2 != 0)
7575 emit_move_insn (copy_rtx (dest),
7576 gen_rtx_IOR (DImode, copy_rtx (dest),
7577 GEN_INT (ud2 << 16)));
7578 if (ud1 != 0)
7579 emit_move_insn (copy_rtx (dest),
7580 gen_rtx_IOR (DImode, copy_rtx (dest),
7581 GEN_INT (ud1)));
7584 return dest;
7587 /* Helper for the following. Get rid of [r+r] memory refs
7588 in cases where it won't work (TImode, TFmode, TDmode, PTImode). */
7590 static void
7591 rs6000_eliminate_indexed_memrefs (rtx operands[2])
7593 if (reload_in_progress)
7594 return;
7596 if (GET_CODE (operands[0]) == MEM
7597 && GET_CODE (XEXP (operands[0], 0)) != REG
7598 && ! legitimate_constant_pool_address_p (XEXP (operands[0], 0),
7599 GET_MODE (operands[0]), false))
7600 operands[0]
7601 = replace_equiv_address (operands[0],
7602 copy_addr_to_reg (XEXP (operands[0], 0)));
7604 if (GET_CODE (operands[1]) == MEM
7605 && GET_CODE (XEXP (operands[1], 0)) != REG
7606 && ! legitimate_constant_pool_address_p (XEXP (operands[1], 0),
7607 GET_MODE (operands[1]), false))
7608 operands[1]
7609 = replace_equiv_address (operands[1],
7610 copy_addr_to_reg (XEXP (operands[1], 0)));
7613 /* Emit a move from SOURCE to DEST in mode MODE. */
7614 void
7615 rs6000_emit_move (rtx dest, rtx source, enum machine_mode mode)
7617 rtx operands[2];
7618 operands[0] = dest;
7619 operands[1] = source;
7621 if (TARGET_DEBUG_ADDR)
7623 fprintf (stderr,
7624 "\nrs6000_emit_move: mode = %s, reload_in_progress = %d, "
7625 "reload_completed = %d, can_create_pseudos = %d.\ndest:\n",
7626 GET_MODE_NAME (mode),
7627 reload_in_progress,
7628 reload_completed,
7629 can_create_pseudo_p ());
7630 debug_rtx (dest);
7631 fprintf (stderr, "source:\n");
7632 debug_rtx (source);
7635 /* Sanity checks. Check that we get CONST_DOUBLE only when we should. */
7636 if (GET_CODE (operands[1]) == CONST_DOUBLE
7637 && ! FLOAT_MODE_P (mode)
7638 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
7640 /* FIXME. This should never happen. */
7641 /* Since it seems that it does, do the safe thing and convert
7642 to a CONST_INT. */
7643 operands[1] = gen_int_mode (CONST_DOUBLE_LOW (operands[1]), mode);
7645 gcc_assert (GET_CODE (operands[1]) != CONST_DOUBLE
7646 || FLOAT_MODE_P (mode)
7647 || ((CONST_DOUBLE_HIGH (operands[1]) != 0
7648 || CONST_DOUBLE_LOW (operands[1]) < 0)
7649 && (CONST_DOUBLE_HIGH (operands[1]) != -1
7650 || CONST_DOUBLE_LOW (operands[1]) >= 0)));
7652 /* Check if GCC is setting up a block move that will end up using FP
7653 registers as temporaries. We must make sure this is acceptable. */
7654 if (GET_CODE (operands[0]) == MEM
7655 && GET_CODE (operands[1]) == MEM
7656 && mode == DImode
7657 && (SLOW_UNALIGNED_ACCESS (DImode, MEM_ALIGN (operands[0]))
7658 || SLOW_UNALIGNED_ACCESS (DImode, MEM_ALIGN (operands[1])))
7659 && ! (SLOW_UNALIGNED_ACCESS (SImode, (MEM_ALIGN (operands[0]) > 32
7660 ? 32 : MEM_ALIGN (operands[0])))
7661 || SLOW_UNALIGNED_ACCESS (SImode, (MEM_ALIGN (operands[1]) > 32
7662 ? 32
7663 : MEM_ALIGN (operands[1]))))
7664 && ! MEM_VOLATILE_P (operands [0])
7665 && ! MEM_VOLATILE_P (operands [1]))
7667 emit_move_insn (adjust_address (operands[0], SImode, 0),
7668 adjust_address (operands[1], SImode, 0));
7669 emit_move_insn (adjust_address (copy_rtx (operands[0]), SImode, 4),
7670 adjust_address (copy_rtx (operands[1]), SImode, 4));
7671 return;
7674 if (can_create_pseudo_p () && GET_CODE (operands[0]) == MEM
7675 && !gpc_reg_operand (operands[1], mode))
7676 operands[1] = force_reg (mode, operands[1]);
7678 /* Recognize the case where operand[1] is a reference to thread-local
7679 data and load its address to a register. */
7680 if (rs6000_tls_referenced_p (operands[1]))
7682 enum tls_model model;
7683 rtx tmp = operands[1];
7684 rtx addend = NULL;
7686 if (GET_CODE (tmp) == CONST && GET_CODE (XEXP (tmp, 0)) == PLUS)
7688 addend = XEXP (XEXP (tmp, 0), 1);
7689 tmp = XEXP (XEXP (tmp, 0), 0);
7692 gcc_assert (GET_CODE (tmp) == SYMBOL_REF);
7693 model = SYMBOL_REF_TLS_MODEL (tmp);
7694 gcc_assert (model != 0);
7696 tmp = rs6000_legitimize_tls_address (tmp, model);
7697 if (addend)
7699 tmp = gen_rtx_PLUS (mode, tmp, addend);
7700 tmp = force_operand (tmp, operands[0]);
7702 operands[1] = tmp;
7705 /* Handle the case where reload calls us with an invalid address. */
7706 if (reload_in_progress && mode == Pmode
7707 && (! general_operand (operands[1], mode)
7708 || ! nonimmediate_operand (operands[0], mode)))
7709 goto emit_set;
7711 /* 128-bit constant floating-point values on Darwin should really be
7712 loaded as two parts. */
7713 if (!TARGET_IEEEQUAD && TARGET_LONG_DOUBLE_128
7714 && mode == TFmode && GET_CODE (operands[1]) == CONST_DOUBLE)
7716 rs6000_emit_move (simplify_gen_subreg (DFmode, operands[0], mode, 0),
7717 simplify_gen_subreg (DFmode, operands[1], mode, 0),
7718 DFmode);
7719 rs6000_emit_move (simplify_gen_subreg (DFmode, operands[0], mode,
7720 GET_MODE_SIZE (DFmode)),
7721 simplify_gen_subreg (DFmode, operands[1], mode,
7722 GET_MODE_SIZE (DFmode)),
7723 DFmode);
7724 return;
7727 if (reload_in_progress && cfun->machine->sdmode_stack_slot != NULL_RTX)
7728 cfun->machine->sdmode_stack_slot =
7729 eliminate_regs (cfun->machine->sdmode_stack_slot, VOIDmode, NULL_RTX);
7731 if (reload_in_progress
7732 && mode == SDmode
7733 && cfun->machine->sdmode_stack_slot != NULL_RTX
7734 && MEM_P (operands[0])
7735 && rtx_equal_p (operands[0], cfun->machine->sdmode_stack_slot)
7736 && REG_P (operands[1]))
7738 if (FP_REGNO_P (REGNO (operands[1])))
7740 rtx mem = adjust_address_nv (operands[0], DDmode, 0);
7741 mem = eliminate_regs (mem, VOIDmode, NULL_RTX);
7742 emit_insn (gen_movsd_store (mem, operands[1]));
7744 else if (INT_REGNO_P (REGNO (operands[1])))
7746 rtx mem = adjust_address_nv (operands[0], mode, 4);
7747 mem = eliminate_regs (mem, VOIDmode, NULL_RTX);
7748 emit_insn (gen_movsd_hardfloat (mem, operands[1]));
7750 else
7751 gcc_unreachable();
7752 return;
7754 if (reload_in_progress
7755 && mode == SDmode
7756 && REG_P (operands[0])
7757 && MEM_P (operands[1])
7758 && cfun->machine->sdmode_stack_slot != NULL_RTX
7759 && rtx_equal_p (operands[1], cfun->machine->sdmode_stack_slot))
7761 if (FP_REGNO_P (REGNO (operands[0])))
7763 rtx mem = adjust_address_nv (operands[1], DDmode, 0);
7764 mem = eliminate_regs (mem, VOIDmode, NULL_RTX);
7765 emit_insn (gen_movsd_load (operands[0], mem));
7767 else if (INT_REGNO_P (REGNO (operands[0])))
7769 rtx mem = adjust_address_nv (operands[1], mode, 4);
7770 mem = eliminate_regs (mem, VOIDmode, NULL_RTX);
7771 emit_insn (gen_movsd_hardfloat (operands[0], mem));
7773 else
7774 gcc_unreachable();
7775 return;
7778 /* FIXME: In the long term, this switch statement should go away
7779 and be replaced by a sequence of tests based on things like
7780 mode == Pmode. */
7781 switch (mode)
7783 case HImode:
7784 case QImode:
7785 if (CONSTANT_P (operands[1])
7786 && GET_CODE (operands[1]) != CONST_INT)
7787 operands[1] = force_const_mem (mode, operands[1]);
7788 break;
7790 case TFmode:
7791 case TDmode:
7792 rs6000_eliminate_indexed_memrefs (operands);
7793 /* fall through */
7795 case DFmode:
7796 case DDmode:
7797 case SFmode:
7798 case SDmode:
7799 if (CONSTANT_P (operands[1])
7800 && ! easy_fp_constant (operands[1], mode))
7801 operands[1] = force_const_mem (mode, operands[1]);
7802 break;
7804 case V16QImode:
7805 case V8HImode:
7806 case V4SFmode:
7807 case V4SImode:
7808 case V4HImode:
7809 case V2SFmode:
7810 case V2SImode:
7811 case V1DImode:
7812 case V2DFmode:
7813 case V2DImode:
7814 if (CONSTANT_P (operands[1])
7815 && !easy_vector_constant (operands[1], mode))
7816 operands[1] = force_const_mem (mode, operands[1]);
7817 break;
7819 case SImode:
7820 case DImode:
7821 /* Use default pattern for address of ELF small data */
7822 if (TARGET_ELF
7823 && mode == Pmode
7824 && DEFAULT_ABI == ABI_V4
7825 && (GET_CODE (operands[1]) == SYMBOL_REF
7826 || GET_CODE (operands[1]) == CONST)
7827 && small_data_operand (operands[1], mode))
7829 emit_insn (gen_rtx_SET (VOIDmode, operands[0], operands[1]));
7830 return;
7833 if (DEFAULT_ABI == ABI_V4
7834 && mode == Pmode && mode == SImode
7835 && flag_pic == 1 && got_operand (operands[1], mode))
7837 emit_insn (gen_movsi_got (operands[0], operands[1]));
7838 return;
7841 if ((TARGET_ELF || DEFAULT_ABI == ABI_DARWIN)
7842 && TARGET_NO_TOC
7843 && ! flag_pic
7844 && mode == Pmode
7845 && CONSTANT_P (operands[1])
7846 && GET_CODE (operands[1]) != HIGH
7847 && GET_CODE (operands[1]) != CONST_INT)
7849 rtx target = (!can_create_pseudo_p ()
7850 ? operands[0]
7851 : gen_reg_rtx (mode));
7853 /* If this is a function address on -mcall-aixdesc,
7854 convert it to the address of the descriptor. */
7855 if (DEFAULT_ABI == ABI_AIX
7856 && GET_CODE (operands[1]) == SYMBOL_REF
7857 && XSTR (operands[1], 0)[0] == '.')
7859 const char *name = XSTR (operands[1], 0);
7860 rtx new_ref;
7861 while (*name == '.')
7862 name++;
7863 new_ref = gen_rtx_SYMBOL_REF (Pmode, name);
7864 CONSTANT_POOL_ADDRESS_P (new_ref)
7865 = CONSTANT_POOL_ADDRESS_P (operands[1]);
7866 SYMBOL_REF_FLAGS (new_ref) = SYMBOL_REF_FLAGS (operands[1]);
7867 SYMBOL_REF_USED (new_ref) = SYMBOL_REF_USED (operands[1]);
7868 SYMBOL_REF_DATA (new_ref) = SYMBOL_REF_DATA (operands[1]);
7869 operands[1] = new_ref;
7872 if (DEFAULT_ABI == ABI_DARWIN)
7874 #if TARGET_MACHO
7875 if (MACHO_DYNAMIC_NO_PIC_P)
7877 /* Take care of any required data indirection. */
7878 operands[1] = rs6000_machopic_legitimize_pic_address (
7879 operands[1], mode, operands[0]);
7880 if (operands[0] != operands[1])
7881 emit_insn (gen_rtx_SET (VOIDmode,
7882 operands[0], operands[1]));
7883 return;
7885 #endif
7886 emit_insn (gen_macho_high (target, operands[1]));
7887 emit_insn (gen_macho_low (operands[0], target, operands[1]));
7888 return;
7891 emit_insn (gen_elf_high (target, operands[1]));
7892 emit_insn (gen_elf_low (operands[0], target, operands[1]));
7893 return;
7896 /* If this is a SYMBOL_REF that refers to a constant pool entry,
7897 and we have put it in the TOC, we just need to make a TOC-relative
7898 reference to it. */
7899 if (TARGET_TOC
7900 && GET_CODE (operands[1]) == SYMBOL_REF
7901 && use_toc_relative_ref (operands[1]))
7902 operands[1] = create_TOC_reference (operands[1], operands[0]);
7903 else if (mode == Pmode
7904 && CONSTANT_P (operands[1])
7905 && GET_CODE (operands[1]) != HIGH
7906 && ((GET_CODE (operands[1]) != CONST_INT
7907 && ! easy_fp_constant (operands[1], mode))
7908 || (GET_CODE (operands[1]) == CONST_INT
7909 && (num_insns_constant (operands[1], mode)
7910 > (TARGET_CMODEL != CMODEL_SMALL ? 3 : 2)))
7911 || (GET_CODE (operands[0]) == REG
7912 && FP_REGNO_P (REGNO (operands[0]))))
7913 && !toc_relative_expr_p (operands[1], false)
7914 && (TARGET_CMODEL == CMODEL_SMALL
7915 || can_create_pseudo_p ()
7916 || (REG_P (operands[0])
7917 && INT_REG_OK_FOR_BASE_P (operands[0], true))))
7920 #if TARGET_MACHO
7921 /* Darwin uses a special PIC legitimizer. */
7922 if (DEFAULT_ABI == ABI_DARWIN && MACHOPIC_INDIRECT)
7924 operands[1] =
7925 rs6000_machopic_legitimize_pic_address (operands[1], mode,
7926 operands[0]);
7927 if (operands[0] != operands[1])
7928 emit_insn (gen_rtx_SET (VOIDmode, operands[0], operands[1]));
7929 return;
7931 #endif
7933 /* If we are to limit the number of things we put in the TOC and
7934 this is a symbol plus a constant we can add in one insn,
7935 just put the symbol in the TOC and add the constant. Don't do
7936 this if reload is in progress. */
7937 if (GET_CODE (operands[1]) == CONST
7938 && TARGET_NO_SUM_IN_TOC && ! reload_in_progress
7939 && GET_CODE (XEXP (operands[1], 0)) == PLUS
7940 && add_operand (XEXP (XEXP (operands[1], 0), 1), mode)
7941 && (GET_CODE (XEXP (XEXP (operands[1], 0), 0)) == LABEL_REF
7942 || GET_CODE (XEXP (XEXP (operands[1], 0), 0)) == SYMBOL_REF)
7943 && ! side_effects_p (operands[0]))
7945 rtx sym =
7946 force_const_mem (mode, XEXP (XEXP (operands[1], 0), 0));
7947 rtx other = XEXP (XEXP (operands[1], 0), 1);
7949 sym = force_reg (mode, sym);
7950 emit_insn (gen_add3_insn (operands[0], sym, other));
7951 return;
7954 operands[1] = force_const_mem (mode, operands[1]);
7956 if (TARGET_TOC
7957 && GET_CODE (XEXP (operands[1], 0)) == SYMBOL_REF
7958 && constant_pool_expr_p (XEXP (operands[1], 0))
7959 && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (
7960 get_pool_constant (XEXP (operands[1], 0)),
7961 get_pool_mode (XEXP (operands[1], 0))))
7963 rtx tocref = create_TOC_reference (XEXP (operands[1], 0),
7964 operands[0]);
7965 operands[1] = gen_const_mem (mode, tocref);
7966 set_mem_alias_set (operands[1], get_TOC_alias_set ());
7969 break;
7971 case TImode:
7972 if (!VECTOR_MEM_VSX_P (TImode))
7973 rs6000_eliminate_indexed_memrefs (operands);
7974 break;
7976 case PTImode:
7977 rs6000_eliminate_indexed_memrefs (operands);
7978 break;
7980 default:
7981 fatal_insn ("bad move", gen_rtx_SET (VOIDmode, dest, source));
7984 /* Above, we may have called force_const_mem which may have returned
7985 an invalid address. If we can, fix this up; otherwise, reload will
7986 have to deal with it. */
7987 if (GET_CODE (operands[1]) == MEM && ! reload_in_progress)
7988 operands[1] = validize_mem (operands[1]);
7990 emit_set:
7991 emit_insn (gen_rtx_SET (VOIDmode, operands[0], operands[1]));
7994 /* Return true if a structure, union or array containing FIELD should be
7995 accessed using `BLKMODE'.
7997 For the SPE, simd types are V2SI, and gcc can be tempted to put the
7998 entire thing in a DI and use subregs to access the internals.
7999 store_bit_field() will force (subreg:DI (reg:V2SI x))'s to the
8000 back-end. Because a single GPR can hold a V2SI, but not a DI, the
8001 best thing to do is set structs to BLKmode and avoid Severe Tire
8002 Damage.
8004 On e500 v2, DF and DI modes suffer from the same anomaly. DF can
8005 fit into 1, whereas DI still needs two. */
8007 static bool
8008 rs6000_member_type_forces_blk (const_tree field, enum machine_mode mode)
8010 return ((TARGET_SPE && TREE_CODE (TREE_TYPE (field)) == VECTOR_TYPE)
8011 || (TARGET_E500_DOUBLE && mode == DFmode));
8014 /* Nonzero if we can use a floating-point register to pass this arg. */
8015 #define USE_FP_FOR_ARG_P(CUM,MODE,TYPE) \
8016 (SCALAR_FLOAT_MODE_P (MODE) \
8017 && (CUM)->fregno <= FP_ARG_MAX_REG \
8018 && TARGET_HARD_FLOAT && TARGET_FPRS)
8020 /* Nonzero if we can use an AltiVec register to pass this arg. */
8021 #define USE_ALTIVEC_FOR_ARG_P(CUM,MODE,TYPE,NAMED) \
8022 (ALTIVEC_OR_VSX_VECTOR_MODE (MODE) \
8023 && (CUM)->vregno <= ALTIVEC_ARG_MAX_REG \
8024 && TARGET_ALTIVEC_ABI \
8025 && (NAMED))
8027 /* Return a nonzero value to say to return the function value in
8028 memory, just as large structures are always returned. TYPE will be
8029 the data type of the value, and FNTYPE will be the type of the
8030 function doing the returning, or @code{NULL} for libcalls.
8032 The AIX ABI for the RS/6000 specifies that all structures are
8033 returned in memory. The Darwin ABI does the same.
8035 For the Darwin 64 Bit ABI, a function result can be returned in
8036 registers or in memory, depending on the size of the return data
8037 type. If it is returned in registers, the value occupies the same
8038 registers as it would if it were the first and only function
8039 argument. Otherwise, the function places its result in memory at
8040 the location pointed to by GPR3.
8042 The SVR4 ABI specifies that structures <= 8 bytes are returned in r3/r4,
8043 but a draft put them in memory, and GCC used to implement the draft
8044 instead of the final standard. Therefore, aix_struct_return
8045 controls this instead of DEFAULT_ABI; V.4 targets needing backward
8046 compatibility can change DRAFT_V4_STRUCT_RET to override the
8047 default, and -m switches get the final word. See
8048 rs6000_option_override_internal for more details.
8050 The PPC32 SVR4 ABI uses IEEE double extended for long double, if 128-bit
8051 long double support is enabled. These values are returned in memory.
8053 int_size_in_bytes returns -1 for variable size objects, which go in
8054 memory always. The cast to unsigned makes -1 > 8. */
8056 static bool
8057 rs6000_return_in_memory (const_tree type, const_tree fntype ATTRIBUTE_UNUSED)
8059 /* For the Darwin64 ABI, test if we can fit the return value in regs. */
8060 if (TARGET_MACHO
8061 && rs6000_darwin64_abi
8062 && TREE_CODE (type) == RECORD_TYPE
8063 && int_size_in_bytes (type) > 0)
8065 CUMULATIVE_ARGS valcum;
8066 rtx valret;
8068 valcum.words = 0;
8069 valcum.fregno = FP_ARG_MIN_REG;
8070 valcum.vregno = ALTIVEC_ARG_MIN_REG;
8071 /* Do a trial code generation as if this were going to be passed
8072 as an argument; if any part goes in memory, we return NULL. */
8073 valret = rs6000_darwin64_record_arg (&valcum, type, true, true);
8074 if (valret)
8075 return false;
8076 /* Otherwise fall through to more conventional ABI rules. */
8079 if (AGGREGATE_TYPE_P (type)
8080 && (aix_struct_return
8081 || (unsigned HOST_WIDE_INT) int_size_in_bytes (type) > 8))
8082 return true;
8084 /* Allow -maltivec -mabi=no-altivec without warning. Altivec vector
8085 modes only exist for GCC vector types if -maltivec. */
8086 if (TARGET_32BIT && !TARGET_ALTIVEC_ABI
8087 && ALTIVEC_VECTOR_MODE (TYPE_MODE (type)))
8088 return false;
8090 /* Return synthetic vectors in memory. */
8091 if (TREE_CODE (type) == VECTOR_TYPE
8092 && int_size_in_bytes (type) > (TARGET_ALTIVEC_ABI ? 16 : 8))
8094 static bool warned_for_return_big_vectors = false;
8095 if (!warned_for_return_big_vectors)
8097 warning (0, "GCC vector returned by reference: "
8098 "non-standard ABI extension with no compatibility guarantee");
8099 warned_for_return_big_vectors = true;
8101 return true;
8104 if (DEFAULT_ABI == ABI_V4 && TARGET_IEEEQUAD && TYPE_MODE (type) == TFmode)
8105 return true;
8107 return false;
8110 #ifdef HAVE_AS_GNU_ATTRIBUTE
8111 /* Return TRUE if a call to function FNDECL may be one that
8112 potentially affects the function calling ABI of the object file. */
8114 static bool
8115 call_ABI_of_interest (tree fndecl)
8117 if (cgraph_state == CGRAPH_STATE_EXPANSION)
8119 struct cgraph_node *c_node;
8121 /* Libcalls are always interesting. */
8122 if (fndecl == NULL_TREE)
8123 return true;
8125 /* Any call to an external function is interesting. */
8126 if (DECL_EXTERNAL (fndecl))
8127 return true;
8129 /* Interesting functions that we are emitting in this object file. */
8130 c_node = cgraph_get_node (fndecl);
8131 c_node = cgraph_function_or_thunk_node (c_node, NULL);
8132 return !cgraph_only_called_directly_p (c_node);
8134 return false;
8136 #endif
8138 /* Initialize a variable CUM of type CUMULATIVE_ARGS
8139 for a call to a function whose data type is FNTYPE.
8140 For a library call, FNTYPE is 0 and RETURN_MODE the return value mode.
8142 For incoming args we set the number of arguments in the prototype large
8143 so we never return a PARALLEL. */
8145 void
8146 init_cumulative_args (CUMULATIVE_ARGS *cum, tree fntype,
8147 rtx libname ATTRIBUTE_UNUSED, int incoming,
8148 int libcall, int n_named_args,
8149 tree fndecl ATTRIBUTE_UNUSED,
8150 enum machine_mode return_mode ATTRIBUTE_UNUSED)
8152 static CUMULATIVE_ARGS zero_cumulative;
8154 *cum = zero_cumulative;
8155 cum->words = 0;
8156 cum->fregno = FP_ARG_MIN_REG;
8157 cum->vregno = ALTIVEC_ARG_MIN_REG;
8158 cum->prototype = (fntype && prototype_p (fntype));
8159 cum->call_cookie = ((DEFAULT_ABI == ABI_V4 && libcall)
8160 ? CALL_LIBCALL : CALL_NORMAL);
8161 cum->sysv_gregno = GP_ARG_MIN_REG;
8162 cum->stdarg = stdarg_p (fntype);
8164 cum->nargs_prototype = 0;
8165 if (incoming || cum->prototype)
8166 cum->nargs_prototype = n_named_args;
8168 /* Check for a longcall attribute. */
8169 if ((!fntype && rs6000_default_long_calls)
8170 || (fntype
8171 && lookup_attribute ("longcall", TYPE_ATTRIBUTES (fntype))
8172 && !lookup_attribute ("shortcall", TYPE_ATTRIBUTES (fntype))))
8173 cum->call_cookie |= CALL_LONG;
8175 if (TARGET_DEBUG_ARG)
8177 fprintf (stderr, "\ninit_cumulative_args:");
8178 if (fntype)
8180 tree ret_type = TREE_TYPE (fntype);
8181 fprintf (stderr, " ret code = %s,",
8182 tree_code_name[ (int)TREE_CODE (ret_type) ]);
8185 if (cum->call_cookie & CALL_LONG)
8186 fprintf (stderr, " longcall,");
8188 fprintf (stderr, " proto = %d, nargs = %d\n",
8189 cum->prototype, cum->nargs_prototype);
8192 #ifdef HAVE_AS_GNU_ATTRIBUTE
8193 if (DEFAULT_ABI == ABI_V4)
8195 cum->escapes = call_ABI_of_interest (fndecl);
8196 if (cum->escapes)
8198 tree return_type;
8200 if (fntype)
8202 return_type = TREE_TYPE (fntype);
8203 return_mode = TYPE_MODE (return_type);
8205 else
8206 return_type = lang_hooks.types.type_for_mode (return_mode, 0);
8208 if (return_type != NULL)
8210 if (TREE_CODE (return_type) == RECORD_TYPE
8211 && TYPE_TRANSPARENT_AGGR (return_type))
8213 return_type = TREE_TYPE (first_field (return_type));
8214 return_mode = TYPE_MODE (return_type);
8216 if (AGGREGATE_TYPE_P (return_type)
8217 && ((unsigned HOST_WIDE_INT) int_size_in_bytes (return_type)
8218 <= 8))
8219 rs6000_returns_struct = true;
8221 if (SCALAR_FLOAT_MODE_P (return_mode))
8222 rs6000_passes_float = true;
8223 else if (ALTIVEC_OR_VSX_VECTOR_MODE (return_mode)
8224 || SPE_VECTOR_MODE (return_mode))
8225 rs6000_passes_vector = true;
8228 #endif
8230 if (fntype
8231 && !TARGET_ALTIVEC
8232 && TARGET_ALTIVEC_ABI
8233 && ALTIVEC_VECTOR_MODE (TYPE_MODE (TREE_TYPE (fntype))))
8235 error ("cannot return value in vector register because"
8236 " altivec instructions are disabled, use -maltivec"
8237 " to enable them");
8241 /* Return true if TYPE must be passed on the stack and not in registers. */
8243 static bool
8244 rs6000_must_pass_in_stack (enum machine_mode mode, const_tree type)
8246 if (DEFAULT_ABI == ABI_AIX || TARGET_64BIT)
8247 return must_pass_in_stack_var_size (mode, type);
8248 else
8249 return must_pass_in_stack_var_size_or_pad (mode, type);
8252 /* If defined, a C expression which determines whether, and in which
8253 direction, to pad out an argument with extra space. The value
8254 should be of type `enum direction': either `upward' to pad above
8255 the argument, `downward' to pad below, or `none' to inhibit
8256 padding.
8258 For the AIX ABI structs are always stored left shifted in their
8259 argument slot. */
8261 enum direction
8262 function_arg_padding (enum machine_mode mode, const_tree type)
8264 #ifndef AGGREGATE_PADDING_FIXED
8265 #define AGGREGATE_PADDING_FIXED 0
8266 #endif
8267 #ifndef AGGREGATES_PAD_UPWARD_ALWAYS
8268 #define AGGREGATES_PAD_UPWARD_ALWAYS 0
8269 #endif
8271 if (!AGGREGATE_PADDING_FIXED)
8273 /* GCC used to pass structures of the same size as integer types as
8274 if they were in fact integers, ignoring FUNCTION_ARG_PADDING.
8275 i.e. Structures of size 1 or 2 (or 4 when TARGET_64BIT) were
8276 passed padded downward, except that -mstrict-align further
8277 muddied the water in that multi-component structures of 2 and 4
8278 bytes in size were passed padded upward.
8280 The following arranges for best compatibility with previous
8281 versions of gcc, but removes the -mstrict-align dependency. */
8282 if (BYTES_BIG_ENDIAN)
8284 HOST_WIDE_INT size = 0;
8286 if (mode == BLKmode)
8288 if (type && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
8289 size = int_size_in_bytes (type);
8291 else
8292 size = GET_MODE_SIZE (mode);
8294 if (size == 1 || size == 2 || size == 4)
8295 return downward;
8297 return upward;
8300 if (AGGREGATES_PAD_UPWARD_ALWAYS)
8302 if (type != 0 && AGGREGATE_TYPE_P (type))
8303 return upward;
8306 /* Fall back to the default. */
8307 return DEFAULT_FUNCTION_ARG_PADDING (mode, type);
8310 /* If defined, a C expression that gives the alignment boundary, in bits,
8311 of an argument with the specified mode and type. If it is not defined,
8312 PARM_BOUNDARY is used for all arguments.
8314 V.4 wants long longs and doubles to be double word aligned. Just
8315 testing the mode size is a boneheaded way to do this as it means
8316 that other types such as complex int are also double word aligned.
8317 However, we're stuck with this because changing the ABI might break
8318 existing library interfaces.
8320 Doubleword align SPE vectors.
8321 Quadword align Altivec/VSX vectors.
8322 Quadword align large synthetic vector types. */
8324 static unsigned int
8325 rs6000_function_arg_boundary (enum machine_mode mode, const_tree type)
8327 if (DEFAULT_ABI == ABI_V4
8328 && (GET_MODE_SIZE (mode) == 8
8329 || (TARGET_HARD_FLOAT
8330 && TARGET_FPRS
8331 && (mode == TFmode || mode == TDmode))))
8332 return 64;
8333 else if (SPE_VECTOR_MODE (mode)
8334 || (type && TREE_CODE (type) == VECTOR_TYPE
8335 && int_size_in_bytes (type) >= 8
8336 && int_size_in_bytes (type) < 16))
8337 return 64;
8338 else if (ALTIVEC_OR_VSX_VECTOR_MODE (mode)
8339 || (type && TREE_CODE (type) == VECTOR_TYPE
8340 && int_size_in_bytes (type) >= 16))
8341 return 128;
8342 else if (((TARGET_MACHO && rs6000_darwin64_abi)
8343 || (DEFAULT_ABI == ABI_AIX && !rs6000_compat_align_parm))
8344 && mode == BLKmode
8345 && type && TYPE_ALIGN (type) > 64)
8346 return 128;
8347 else
8348 return PARM_BOUNDARY;
8351 /* For a function parm of MODE and TYPE, return the starting word in
8352 the parameter area. NWORDS of the parameter area are already used. */
8354 static unsigned int
8355 rs6000_parm_start (enum machine_mode mode, const_tree type,
8356 unsigned int nwords)
8358 unsigned int align;
8359 unsigned int parm_offset;
8361 align = rs6000_function_arg_boundary (mode, type) / PARM_BOUNDARY - 1;
8362 parm_offset = DEFAULT_ABI == ABI_V4 ? 2 : 6;
8363 return nwords + (-(parm_offset + nwords) & align);
8366 /* Compute the size (in words) of a function argument. */
8368 static unsigned long
8369 rs6000_arg_size (enum machine_mode mode, const_tree type)
8371 unsigned long size;
8373 if (mode != BLKmode)
8374 size = GET_MODE_SIZE (mode);
8375 else
8376 size = int_size_in_bytes (type);
8378 if (TARGET_32BIT)
8379 return (size + 3) >> 2;
8380 else
8381 return (size + 7) >> 3;
8384 /* Use this to flush pending int fields. */
8386 static void
8387 rs6000_darwin64_record_arg_advance_flush (CUMULATIVE_ARGS *cum,
8388 HOST_WIDE_INT bitpos, int final)
8390 unsigned int startbit, endbit;
8391 int intregs, intoffset;
8392 enum machine_mode mode;
8394 /* Handle the situations where a float is taking up the first half
8395 of the GPR, and the other half is empty (typically due to
8396 alignment restrictions). We can detect this by a 8-byte-aligned
8397 int field, or by seeing that this is the final flush for this
8398 argument. Count the word and continue on. */
8399 if (cum->floats_in_gpr == 1
8400 && (cum->intoffset % 64 == 0
8401 || (cum->intoffset == -1 && final)))
8403 cum->words++;
8404 cum->floats_in_gpr = 0;
8407 if (cum->intoffset == -1)
8408 return;
8410 intoffset = cum->intoffset;
8411 cum->intoffset = -1;
8412 cum->floats_in_gpr = 0;
8414 if (intoffset % BITS_PER_WORD != 0)
8416 mode = mode_for_size (BITS_PER_WORD - intoffset % BITS_PER_WORD,
8417 MODE_INT, 0);
8418 if (mode == BLKmode)
8420 /* We couldn't find an appropriate mode, which happens,
8421 e.g., in packed structs when there are 3 bytes to load.
8422 Back intoffset back to the beginning of the word in this
8423 case. */
8424 intoffset = intoffset & -BITS_PER_WORD;
8428 startbit = intoffset & -BITS_PER_WORD;
8429 endbit = (bitpos + BITS_PER_WORD - 1) & -BITS_PER_WORD;
8430 intregs = (endbit - startbit) / BITS_PER_WORD;
8431 cum->words += intregs;
8432 /* words should be unsigned. */
8433 if ((unsigned)cum->words < (endbit/BITS_PER_WORD))
8435 int pad = (endbit/BITS_PER_WORD) - cum->words;
8436 cum->words += pad;
8440 /* The darwin64 ABI calls for us to recurse down through structs,
8441 looking for elements passed in registers. Unfortunately, we have
8442 to track int register count here also because of misalignments
8443 in powerpc alignment mode. */
8445 static void
8446 rs6000_darwin64_record_arg_advance_recurse (CUMULATIVE_ARGS *cum,
8447 const_tree type,
8448 HOST_WIDE_INT startbitpos)
8450 tree f;
8452 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
8453 if (TREE_CODE (f) == FIELD_DECL)
8455 HOST_WIDE_INT bitpos = startbitpos;
8456 tree ftype = TREE_TYPE (f);
8457 enum machine_mode mode;
8458 if (ftype == error_mark_node)
8459 continue;
8460 mode = TYPE_MODE (ftype);
8462 if (DECL_SIZE (f) != 0
8463 && host_integerp (bit_position (f), 1))
8464 bitpos += int_bit_position (f);
8466 /* ??? FIXME: else assume zero offset. */
8468 if (TREE_CODE (ftype) == RECORD_TYPE)
8469 rs6000_darwin64_record_arg_advance_recurse (cum, ftype, bitpos);
8470 else if (USE_FP_FOR_ARG_P (cum, mode, ftype))
8472 unsigned n_fpregs = (GET_MODE_SIZE (mode) + 7) >> 3;
8473 rs6000_darwin64_record_arg_advance_flush (cum, bitpos, 0);
8474 cum->fregno += n_fpregs;
8475 /* Single-precision floats present a special problem for
8476 us, because they are smaller than an 8-byte GPR, and so
8477 the structure-packing rules combined with the standard
8478 varargs behavior mean that we want to pack float/float
8479 and float/int combinations into a single register's
8480 space. This is complicated by the arg advance flushing,
8481 which works on arbitrarily large groups of int-type
8482 fields. */
8483 if (mode == SFmode)
8485 if (cum->floats_in_gpr == 1)
8487 /* Two floats in a word; count the word and reset
8488 the float count. */
8489 cum->words++;
8490 cum->floats_in_gpr = 0;
8492 else if (bitpos % 64 == 0)
8494 /* A float at the beginning of an 8-byte word;
8495 count it and put off adjusting cum->words until
8496 we see if a arg advance flush is going to do it
8497 for us. */
8498 cum->floats_in_gpr++;
8500 else
8502 /* The float is at the end of a word, preceded
8503 by integer fields, so the arg advance flush
8504 just above has already set cum->words and
8505 everything is taken care of. */
8508 else
8509 cum->words += n_fpregs;
8511 else if (USE_ALTIVEC_FOR_ARG_P (cum, mode, type, 1))
8513 rs6000_darwin64_record_arg_advance_flush (cum, bitpos, 0);
8514 cum->vregno++;
8515 cum->words += 2;
8517 else if (cum->intoffset == -1)
8518 cum->intoffset = bitpos;
8522 /* Check for an item that needs to be considered specially under the darwin 64
8523 bit ABI. These are record types where the mode is BLK or the structure is
8524 8 bytes in size. */
8525 static int
8526 rs6000_darwin64_struct_check_p (enum machine_mode mode, const_tree type)
8528 return rs6000_darwin64_abi
8529 && ((mode == BLKmode
8530 && TREE_CODE (type) == RECORD_TYPE
8531 && int_size_in_bytes (type) > 0)
8532 || (type && TREE_CODE (type) == RECORD_TYPE
8533 && int_size_in_bytes (type) == 8)) ? 1 : 0;
8536 /* Update the data in CUM to advance over an argument
8537 of mode MODE and data type TYPE.
8538 (TYPE is null for libcalls where that information may not be available.)
8540 Note that for args passed by reference, function_arg will be called
8541 with MODE and TYPE set to that of the pointer to the arg, not the arg
8542 itself. */
8544 static void
8545 rs6000_function_arg_advance_1 (CUMULATIVE_ARGS *cum, enum machine_mode mode,
8546 const_tree type, bool named, int depth)
8548 /* Only tick off an argument if we're not recursing. */
8549 if (depth == 0)
8550 cum->nargs_prototype--;
8552 #ifdef HAVE_AS_GNU_ATTRIBUTE
8553 if (DEFAULT_ABI == ABI_V4
8554 && cum->escapes)
8556 if (SCALAR_FLOAT_MODE_P (mode))
8557 rs6000_passes_float = true;
8558 else if (named && ALTIVEC_OR_VSX_VECTOR_MODE (mode))
8559 rs6000_passes_vector = true;
8560 else if (SPE_VECTOR_MODE (mode)
8561 && !cum->stdarg
8562 && cum->sysv_gregno <= GP_ARG_MAX_REG)
8563 rs6000_passes_vector = true;
8565 #endif
8567 if (TARGET_ALTIVEC_ABI
8568 && (ALTIVEC_OR_VSX_VECTOR_MODE (mode)
8569 || (type && TREE_CODE (type) == VECTOR_TYPE
8570 && int_size_in_bytes (type) == 16)))
8572 bool stack = false;
8574 if (USE_ALTIVEC_FOR_ARG_P (cum, mode, type, named))
8576 cum->vregno++;
8577 if (!TARGET_ALTIVEC)
8578 error ("cannot pass argument in vector register because"
8579 " altivec instructions are disabled, use -maltivec"
8580 " to enable them");
8582 /* PowerPC64 Linux and AIX allocate GPRs for a vector argument
8583 even if it is going to be passed in a vector register.
8584 Darwin does the same for variable-argument functions. */
8585 if ((DEFAULT_ABI == ABI_AIX && TARGET_64BIT)
8586 || (cum->stdarg && DEFAULT_ABI != ABI_V4))
8587 stack = true;
8589 else
8590 stack = true;
8592 if (stack)
8594 int align;
8596 /* Vector parameters must be 16-byte aligned. This places
8597 them at 2 mod 4 in terms of words in 32-bit mode, since
8598 the parameter save area starts at offset 24 from the
8599 stack. In 64-bit mode, they just have to start on an
8600 even word, since the parameter save area is 16-byte
8601 aligned. Space for GPRs is reserved even if the argument
8602 will be passed in memory. */
8603 if (TARGET_32BIT)
8604 align = (2 - cum->words) & 3;
8605 else
8606 align = cum->words & 1;
8607 cum->words += align + rs6000_arg_size (mode, type);
8609 if (TARGET_DEBUG_ARG)
8611 fprintf (stderr, "function_adv: words = %2d, align=%d, ",
8612 cum->words, align);
8613 fprintf (stderr, "nargs = %4d, proto = %d, mode = %4s\n",
8614 cum->nargs_prototype, cum->prototype,
8615 GET_MODE_NAME (mode));
8619 else if (TARGET_SPE_ABI && TARGET_SPE && SPE_VECTOR_MODE (mode)
8620 && !cum->stdarg
8621 && cum->sysv_gregno <= GP_ARG_MAX_REG)
8622 cum->sysv_gregno++;
8624 else if (TARGET_MACHO && rs6000_darwin64_struct_check_p (mode, type))
8626 int size = int_size_in_bytes (type);
8627 /* Variable sized types have size == -1 and are
8628 treated as if consisting entirely of ints.
8629 Pad to 16 byte boundary if needed. */
8630 if (TYPE_ALIGN (type) >= 2 * BITS_PER_WORD
8631 && (cum->words % 2) != 0)
8632 cum->words++;
8633 /* For varargs, we can just go up by the size of the struct. */
8634 if (!named)
8635 cum->words += (size + 7) / 8;
8636 else
8638 /* It is tempting to say int register count just goes up by
8639 sizeof(type)/8, but this is wrong in a case such as
8640 { int; double; int; } [powerpc alignment]. We have to
8641 grovel through the fields for these too. */
8642 cum->intoffset = 0;
8643 cum->floats_in_gpr = 0;
8644 rs6000_darwin64_record_arg_advance_recurse (cum, type, 0);
8645 rs6000_darwin64_record_arg_advance_flush (cum,
8646 size * BITS_PER_UNIT, 1);
8648 if (TARGET_DEBUG_ARG)
8650 fprintf (stderr, "function_adv: words = %2d, align=%d, size=%d",
8651 cum->words, TYPE_ALIGN (type), size);
8652 fprintf (stderr,
8653 "nargs = %4d, proto = %d, mode = %4s (darwin64 abi)\n",
8654 cum->nargs_prototype, cum->prototype,
8655 GET_MODE_NAME (mode));
8658 else if (DEFAULT_ABI == ABI_V4)
8660 if (TARGET_HARD_FLOAT && TARGET_FPRS
8661 && ((TARGET_SINGLE_FLOAT && mode == SFmode)
8662 || (TARGET_DOUBLE_FLOAT && mode == DFmode)
8663 || (mode == TFmode && !TARGET_IEEEQUAD)
8664 || mode == SDmode || mode == DDmode || mode == TDmode))
8666 /* _Decimal128 must use an even/odd register pair. This assumes
8667 that the register number is odd when fregno is odd. */
8668 if (mode == TDmode && (cum->fregno % 2) == 1)
8669 cum->fregno++;
8671 if (cum->fregno + (mode == TFmode || mode == TDmode ? 1 : 0)
8672 <= FP_ARG_V4_MAX_REG)
8673 cum->fregno += (GET_MODE_SIZE (mode) + 7) >> 3;
8674 else
8676 cum->fregno = FP_ARG_V4_MAX_REG + 1;
8677 if (mode == DFmode || mode == TFmode
8678 || mode == DDmode || mode == TDmode)
8679 cum->words += cum->words & 1;
8680 cum->words += rs6000_arg_size (mode, type);
8683 else
8685 int n_words = rs6000_arg_size (mode, type);
8686 int gregno = cum->sysv_gregno;
8688 /* Long long and SPE vectors are put in (r3,r4), (r5,r6),
8689 (r7,r8) or (r9,r10). As does any other 2 word item such
8690 as complex int due to a historical mistake. */
8691 if (n_words == 2)
8692 gregno += (1 - gregno) & 1;
8694 /* Multi-reg args are not split between registers and stack. */
8695 if (gregno + n_words - 1 > GP_ARG_MAX_REG)
8697 /* Long long and SPE vectors are aligned on the stack.
8698 So are other 2 word items such as complex int due to
8699 a historical mistake. */
8700 if (n_words == 2)
8701 cum->words += cum->words & 1;
8702 cum->words += n_words;
8705 /* Note: continuing to accumulate gregno past when we've started
8706 spilling to the stack indicates the fact that we've started
8707 spilling to the stack to expand_builtin_saveregs. */
8708 cum->sysv_gregno = gregno + n_words;
8711 if (TARGET_DEBUG_ARG)
8713 fprintf (stderr, "function_adv: words = %2d, fregno = %2d, ",
8714 cum->words, cum->fregno);
8715 fprintf (stderr, "gregno = %2d, nargs = %4d, proto = %d, ",
8716 cum->sysv_gregno, cum->nargs_prototype, cum->prototype);
8717 fprintf (stderr, "mode = %4s, named = %d\n",
8718 GET_MODE_NAME (mode), named);
8721 else
8723 int n_words = rs6000_arg_size (mode, type);
8724 int start_words = cum->words;
8725 int align_words = rs6000_parm_start (mode, type, start_words);
8727 cum->words = align_words + n_words;
8729 if (SCALAR_FLOAT_MODE_P (mode)
8730 && TARGET_HARD_FLOAT && TARGET_FPRS)
8732 /* _Decimal128 must be passed in an even/odd float register pair.
8733 This assumes that the register number is odd when fregno is
8734 odd. */
8735 if (mode == TDmode && (cum->fregno % 2) == 1)
8736 cum->fregno++;
8737 cum->fregno += (GET_MODE_SIZE (mode) + 7) >> 3;
8740 if (TARGET_DEBUG_ARG)
8742 fprintf (stderr, "function_adv: words = %2d, fregno = %2d, ",
8743 cum->words, cum->fregno);
8744 fprintf (stderr, "nargs = %4d, proto = %d, mode = %4s, ",
8745 cum->nargs_prototype, cum->prototype, GET_MODE_NAME (mode));
8746 fprintf (stderr, "named = %d, align = %d, depth = %d\n",
8747 named, align_words - start_words, depth);
8752 static void
8753 rs6000_function_arg_advance (cumulative_args_t cum, enum machine_mode mode,
8754 const_tree type, bool named)
8756 rs6000_function_arg_advance_1 (get_cumulative_args (cum), mode, type, named,
8760 static rtx
8761 spe_build_register_parallel (enum machine_mode mode, int gregno)
8763 rtx r1, r3, r5, r7;
8765 switch (mode)
8767 case DFmode:
8768 r1 = gen_rtx_REG (DImode, gregno);
8769 r1 = gen_rtx_EXPR_LIST (VOIDmode, r1, const0_rtx);
8770 return gen_rtx_PARALLEL (mode, gen_rtvec (1, r1));
8772 case DCmode:
8773 case TFmode:
8774 r1 = gen_rtx_REG (DImode, gregno);
8775 r1 = gen_rtx_EXPR_LIST (VOIDmode, r1, const0_rtx);
8776 r3 = gen_rtx_REG (DImode, gregno + 2);
8777 r3 = gen_rtx_EXPR_LIST (VOIDmode, r3, GEN_INT (8));
8778 return gen_rtx_PARALLEL (mode, gen_rtvec (2, r1, r3));
8780 case TCmode:
8781 r1 = gen_rtx_REG (DImode, gregno);
8782 r1 = gen_rtx_EXPR_LIST (VOIDmode, r1, const0_rtx);
8783 r3 = gen_rtx_REG (DImode, gregno + 2);
8784 r3 = gen_rtx_EXPR_LIST (VOIDmode, r3, GEN_INT (8));
8785 r5 = gen_rtx_REG (DImode, gregno + 4);
8786 r5 = gen_rtx_EXPR_LIST (VOIDmode, r5, GEN_INT (16));
8787 r7 = gen_rtx_REG (DImode, gregno + 6);
8788 r7 = gen_rtx_EXPR_LIST (VOIDmode, r7, GEN_INT (24));
8789 return gen_rtx_PARALLEL (mode, gen_rtvec (4, r1, r3, r5, r7));
8791 default:
8792 gcc_unreachable ();
8796 /* Determine where to put a SIMD argument on the SPE. */
8797 static rtx
8798 rs6000_spe_function_arg (const CUMULATIVE_ARGS *cum, enum machine_mode mode,
8799 const_tree type)
8801 int gregno = cum->sysv_gregno;
8803 /* On E500 v2, double arithmetic is done on the full 64-bit GPR, but
8804 are passed and returned in a pair of GPRs for ABI compatibility. */
8805 if (TARGET_E500_DOUBLE && (mode == DFmode || mode == TFmode
8806 || mode == DCmode || mode == TCmode))
8808 int n_words = rs6000_arg_size (mode, type);
8810 /* Doubles go in an odd/even register pair (r5/r6, etc). */
8811 if (mode == DFmode)
8812 gregno += (1 - gregno) & 1;
8814 /* Multi-reg args are not split between registers and stack. */
8815 if (gregno + n_words - 1 > GP_ARG_MAX_REG)
8816 return NULL_RTX;
8818 return spe_build_register_parallel (mode, gregno);
8820 if (cum->stdarg)
8822 int n_words = rs6000_arg_size (mode, type);
8824 /* SPE vectors are put in odd registers. */
8825 if (n_words == 2 && (gregno & 1) == 0)
8826 gregno += 1;
8828 if (gregno + n_words - 1 <= GP_ARG_MAX_REG)
8830 rtx r1, r2;
8831 enum machine_mode m = SImode;
8833 r1 = gen_rtx_REG (m, gregno);
8834 r1 = gen_rtx_EXPR_LIST (m, r1, const0_rtx);
8835 r2 = gen_rtx_REG (m, gregno + 1);
8836 r2 = gen_rtx_EXPR_LIST (m, r2, GEN_INT (4));
8837 return gen_rtx_PARALLEL (mode, gen_rtvec (2, r1, r2));
8839 else
8840 return NULL_RTX;
8842 else
8844 if (gregno <= GP_ARG_MAX_REG)
8845 return gen_rtx_REG (mode, gregno);
8846 else
8847 return NULL_RTX;
8851 /* A subroutine of rs6000_darwin64_record_arg. Assign the bits of the
8852 structure between cum->intoffset and bitpos to integer registers. */
8854 static void
8855 rs6000_darwin64_record_arg_flush (CUMULATIVE_ARGS *cum,
8856 HOST_WIDE_INT bitpos, rtx rvec[], int *k)
8858 enum machine_mode mode;
8859 unsigned int regno;
8860 unsigned int startbit, endbit;
8861 int this_regno, intregs, intoffset;
8862 rtx reg;
8864 if (cum->intoffset == -1)
8865 return;
8867 intoffset = cum->intoffset;
8868 cum->intoffset = -1;
8870 /* If this is the trailing part of a word, try to only load that
8871 much into the register. Otherwise load the whole register. Note
8872 that in the latter case we may pick up unwanted bits. It's not a
8873 problem at the moment but may wish to revisit. */
8875 if (intoffset % BITS_PER_WORD != 0)
8877 mode = mode_for_size (BITS_PER_WORD - intoffset % BITS_PER_WORD,
8878 MODE_INT, 0);
8879 if (mode == BLKmode)
8881 /* We couldn't find an appropriate mode, which happens,
8882 e.g., in packed structs when there are 3 bytes to load.
8883 Back intoffset back to the beginning of the word in this
8884 case. */
8885 intoffset = intoffset & -BITS_PER_WORD;
8886 mode = word_mode;
8889 else
8890 mode = word_mode;
8892 startbit = intoffset & -BITS_PER_WORD;
8893 endbit = (bitpos + BITS_PER_WORD - 1) & -BITS_PER_WORD;
8894 intregs = (endbit - startbit) / BITS_PER_WORD;
8895 this_regno = cum->words + intoffset / BITS_PER_WORD;
8897 if (intregs > 0 && intregs > GP_ARG_NUM_REG - this_regno)
8898 cum->use_stack = 1;
8900 intregs = MIN (intregs, GP_ARG_NUM_REG - this_regno);
8901 if (intregs <= 0)
8902 return;
8904 intoffset /= BITS_PER_UNIT;
8907 regno = GP_ARG_MIN_REG + this_regno;
8908 reg = gen_rtx_REG (mode, regno);
8909 rvec[(*k)++] =
8910 gen_rtx_EXPR_LIST (VOIDmode, reg, GEN_INT (intoffset));
8912 this_regno += 1;
8913 intoffset = (intoffset | (UNITS_PER_WORD-1)) + 1;
8914 mode = word_mode;
8915 intregs -= 1;
8917 while (intregs > 0);
8920 /* Recursive workhorse for the following. */
8922 static void
8923 rs6000_darwin64_record_arg_recurse (CUMULATIVE_ARGS *cum, const_tree type,
8924 HOST_WIDE_INT startbitpos, rtx rvec[],
8925 int *k)
8927 tree f;
8929 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
8930 if (TREE_CODE (f) == FIELD_DECL)
8932 HOST_WIDE_INT bitpos = startbitpos;
8933 tree ftype = TREE_TYPE (f);
8934 enum machine_mode mode;
8935 if (ftype == error_mark_node)
8936 continue;
8937 mode = TYPE_MODE (ftype);
8939 if (DECL_SIZE (f) != 0
8940 && host_integerp (bit_position (f), 1))
8941 bitpos += int_bit_position (f);
8943 /* ??? FIXME: else assume zero offset. */
8945 if (TREE_CODE (ftype) == RECORD_TYPE)
8946 rs6000_darwin64_record_arg_recurse (cum, ftype, bitpos, rvec, k);
8947 else if (cum->named && USE_FP_FOR_ARG_P (cum, mode, ftype))
8949 unsigned n_fpreg = (GET_MODE_SIZE (mode) + 7) >> 3;
8950 #if 0
8951 switch (mode)
8953 case SCmode: mode = SFmode; break;
8954 case DCmode: mode = DFmode; break;
8955 case TCmode: mode = TFmode; break;
8956 default: break;
8958 #endif
8959 rs6000_darwin64_record_arg_flush (cum, bitpos, rvec, k);
8960 if (cum->fregno + n_fpreg > FP_ARG_MAX_REG + 1)
8962 gcc_assert (cum->fregno == FP_ARG_MAX_REG
8963 && (mode == TFmode || mode == TDmode));
8964 /* Long double or _Decimal128 split over regs and memory. */
8965 mode = DECIMAL_FLOAT_MODE_P (mode) ? DDmode : DFmode;
8966 cum->use_stack=1;
8968 rvec[(*k)++]
8969 = gen_rtx_EXPR_LIST (VOIDmode,
8970 gen_rtx_REG (mode, cum->fregno++),
8971 GEN_INT (bitpos / BITS_PER_UNIT));
8972 if (mode == TFmode || mode == TDmode)
8973 cum->fregno++;
8975 else if (cum->named && USE_ALTIVEC_FOR_ARG_P (cum, mode, ftype, 1))
8977 rs6000_darwin64_record_arg_flush (cum, bitpos, rvec, k);
8978 rvec[(*k)++]
8979 = gen_rtx_EXPR_LIST (VOIDmode,
8980 gen_rtx_REG (mode, cum->vregno++),
8981 GEN_INT (bitpos / BITS_PER_UNIT));
8983 else if (cum->intoffset == -1)
8984 cum->intoffset = bitpos;
8988 /* For the darwin64 ABI, we want to construct a PARALLEL consisting of
8989 the register(s) to be used for each field and subfield of a struct
8990 being passed by value, along with the offset of where the
8991 register's value may be found in the block. FP fields go in FP
8992 register, vector fields go in vector registers, and everything
8993 else goes in int registers, packed as in memory.
8995 This code is also used for function return values. RETVAL indicates
8996 whether this is the case.
8998 Much of this is taken from the SPARC V9 port, which has a similar
8999 calling convention. */
9001 static rtx
9002 rs6000_darwin64_record_arg (CUMULATIVE_ARGS *orig_cum, const_tree type,
9003 bool named, bool retval)
9005 rtx rvec[FIRST_PSEUDO_REGISTER];
9006 int k = 1, kbase = 1;
9007 HOST_WIDE_INT typesize = int_size_in_bytes (type);
9008 /* This is a copy; modifications are not visible to our caller. */
9009 CUMULATIVE_ARGS copy_cum = *orig_cum;
9010 CUMULATIVE_ARGS *cum = &copy_cum;
9012 /* Pad to 16 byte boundary if needed. */
9013 if (!retval && TYPE_ALIGN (type) >= 2 * BITS_PER_WORD
9014 && (cum->words % 2) != 0)
9015 cum->words++;
9017 cum->intoffset = 0;
9018 cum->use_stack = 0;
9019 cum->named = named;
9021 /* Put entries into rvec[] for individual FP and vector fields, and
9022 for the chunks of memory that go in int regs. Note we start at
9023 element 1; 0 is reserved for an indication of using memory, and
9024 may or may not be filled in below. */
9025 rs6000_darwin64_record_arg_recurse (cum, type, /* startbit pos= */ 0, rvec, &k);
9026 rs6000_darwin64_record_arg_flush (cum, typesize * BITS_PER_UNIT, rvec, &k);
9028 /* If any part of the struct went on the stack put all of it there.
9029 This hack is because the generic code for
9030 FUNCTION_ARG_PARTIAL_NREGS cannot handle cases where the register
9031 parts of the struct are not at the beginning. */
9032 if (cum->use_stack)
9034 if (retval)
9035 return NULL_RTX; /* doesn't go in registers at all */
9036 kbase = 0;
9037 rvec[0] = gen_rtx_EXPR_LIST (VOIDmode, NULL_RTX, const0_rtx);
9039 if (k > 1 || cum->use_stack)
9040 return gen_rtx_PARALLEL (BLKmode, gen_rtvec_v (k - kbase, &rvec[kbase]));
9041 else
9042 return NULL_RTX;
9045 /* Determine where to place an argument in 64-bit mode with 32-bit ABI. */
9047 static rtx
9048 rs6000_mixed_function_arg (enum machine_mode mode, const_tree type,
9049 int align_words)
9051 int n_units;
9052 int i, k;
9053 rtx rvec[GP_ARG_NUM_REG + 1];
9055 if (align_words >= GP_ARG_NUM_REG)
9056 return NULL_RTX;
9058 n_units = rs6000_arg_size (mode, type);
9060 /* Optimize the simple case where the arg fits in one gpr, except in
9061 the case of BLKmode due to assign_parms assuming that registers are
9062 BITS_PER_WORD wide. */
9063 if (n_units == 0
9064 || (n_units == 1 && mode != BLKmode))
9065 return gen_rtx_REG (mode, GP_ARG_MIN_REG + align_words);
9067 k = 0;
9068 if (align_words + n_units > GP_ARG_NUM_REG)
9069 /* Not all of the arg fits in gprs. Say that it goes in memory too,
9070 using a magic NULL_RTX component.
9071 This is not strictly correct. Only some of the arg belongs in
9072 memory, not all of it. However, the normal scheme using
9073 function_arg_partial_nregs can result in unusual subregs, eg.
9074 (subreg:SI (reg:DF) 4), which are not handled well. The code to
9075 store the whole arg to memory is often more efficient than code
9076 to store pieces, and we know that space is available in the right
9077 place for the whole arg. */
9078 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, NULL_RTX, const0_rtx);
9080 i = 0;
9083 rtx r = gen_rtx_REG (SImode, GP_ARG_MIN_REG + align_words);
9084 rtx off = GEN_INT (i++ * 4);
9085 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, off);
9087 while (++align_words < GP_ARG_NUM_REG && --n_units != 0);
9089 return gen_rtx_PARALLEL (mode, gen_rtvec_v (k, rvec));
9092 /* Determine where to put an argument to a function.
9093 Value is zero to push the argument on the stack,
9094 or a hard register in which to store the argument.
9096 MODE is the argument's machine mode.
9097 TYPE is the data type of the argument (as a tree).
9098 This is null for libcalls where that information may
9099 not be available.
9100 CUM is a variable of type CUMULATIVE_ARGS which gives info about
9101 the preceding args and about the function being called. It is
9102 not modified in this routine.
9103 NAMED is nonzero if this argument is a named parameter
9104 (otherwise it is an extra parameter matching an ellipsis).
9106 On RS/6000 the first eight words of non-FP are normally in registers
9107 and the rest are pushed. Under AIX, the first 13 FP args are in registers.
9108 Under V.4, the first 8 FP args are in registers.
9110 If this is floating-point and no prototype is specified, we use
9111 both an FP and integer register (or possibly FP reg and stack). Library
9112 functions (when CALL_LIBCALL is set) always have the proper types for args,
9113 so we can pass the FP value just in one register. emit_library_function
9114 doesn't support PARALLEL anyway.
9116 Note that for args passed by reference, function_arg will be called
9117 with MODE and TYPE set to that of the pointer to the arg, not the arg
9118 itself. */
9120 static rtx
9121 rs6000_function_arg (cumulative_args_t cum_v, enum machine_mode mode,
9122 const_tree type, bool named)
9124 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
9125 enum rs6000_abi abi = DEFAULT_ABI;
9127 /* Return a marker to indicate whether CR1 needs to set or clear the
9128 bit that V.4 uses to say fp args were passed in registers.
9129 Assume that we don't need the marker for software floating point,
9130 or compiler generated library calls. */
9131 if (mode == VOIDmode)
9133 if (abi == ABI_V4
9134 && (cum->call_cookie & CALL_LIBCALL) == 0
9135 && (cum->stdarg
9136 || (cum->nargs_prototype < 0
9137 && (cum->prototype || TARGET_NO_PROTOTYPE))))
9139 /* For the SPE, we need to crxor CR6 always. */
9140 if (TARGET_SPE_ABI)
9141 return GEN_INT (cum->call_cookie | CALL_V4_SET_FP_ARGS);
9142 else if (TARGET_HARD_FLOAT && TARGET_FPRS)
9143 return GEN_INT (cum->call_cookie
9144 | ((cum->fregno == FP_ARG_MIN_REG)
9145 ? CALL_V4_SET_FP_ARGS
9146 : CALL_V4_CLEAR_FP_ARGS));
9149 return GEN_INT (cum->call_cookie & ~CALL_LIBCALL);
9152 if (TARGET_MACHO && rs6000_darwin64_struct_check_p (mode, type))
9154 rtx rslt = rs6000_darwin64_record_arg (cum, type, named, /*retval= */false);
9155 if (rslt != NULL_RTX)
9156 return rslt;
9157 /* Else fall through to usual handling. */
9160 if (USE_ALTIVEC_FOR_ARG_P (cum, mode, type, named))
9161 if (TARGET_64BIT && ! cum->prototype)
9163 /* Vector parameters get passed in vector register
9164 and also in GPRs or memory, in absence of prototype. */
9165 int align_words;
9166 rtx slot;
9167 align_words = (cum->words + 1) & ~1;
9169 if (align_words >= GP_ARG_NUM_REG)
9171 slot = NULL_RTX;
9173 else
9175 slot = gen_rtx_REG (mode, GP_ARG_MIN_REG + align_words);
9177 return gen_rtx_PARALLEL (mode,
9178 gen_rtvec (2,
9179 gen_rtx_EXPR_LIST (VOIDmode,
9180 slot, const0_rtx),
9181 gen_rtx_EXPR_LIST (VOIDmode,
9182 gen_rtx_REG (mode, cum->vregno),
9183 const0_rtx)));
9185 else
9186 return gen_rtx_REG (mode, cum->vregno);
9187 else if (TARGET_ALTIVEC_ABI
9188 && (ALTIVEC_OR_VSX_VECTOR_MODE (mode)
9189 || (type && TREE_CODE (type) == VECTOR_TYPE
9190 && int_size_in_bytes (type) == 16)))
9192 if (named || abi == ABI_V4)
9193 return NULL_RTX;
9194 else
9196 /* Vector parameters to varargs functions under AIX or Darwin
9197 get passed in memory and possibly also in GPRs. */
9198 int align, align_words, n_words;
9199 enum machine_mode part_mode;
9201 /* Vector parameters must be 16-byte aligned. This places them at
9202 2 mod 4 in terms of words in 32-bit mode, since the parameter
9203 save area starts at offset 24 from the stack. In 64-bit mode,
9204 they just have to start on an even word, since the parameter
9205 save area is 16-byte aligned. */
9206 if (TARGET_32BIT)
9207 align = (2 - cum->words) & 3;
9208 else
9209 align = cum->words & 1;
9210 align_words = cum->words + align;
9212 /* Out of registers? Memory, then. */
9213 if (align_words >= GP_ARG_NUM_REG)
9214 return NULL_RTX;
9216 if (TARGET_32BIT && TARGET_POWERPC64)
9217 return rs6000_mixed_function_arg (mode, type, align_words);
9219 /* The vector value goes in GPRs. Only the part of the
9220 value in GPRs is reported here. */
9221 part_mode = mode;
9222 n_words = rs6000_arg_size (mode, type);
9223 if (align_words + n_words > GP_ARG_NUM_REG)
9224 /* Fortunately, there are only two possibilities, the value
9225 is either wholly in GPRs or half in GPRs and half not. */
9226 part_mode = DImode;
9228 return gen_rtx_REG (part_mode, GP_ARG_MIN_REG + align_words);
9231 else if (TARGET_SPE_ABI && TARGET_SPE
9232 && (SPE_VECTOR_MODE (mode)
9233 || (TARGET_E500_DOUBLE && (mode == DFmode
9234 || mode == DCmode
9235 || mode == TFmode
9236 || mode == TCmode))))
9237 return rs6000_spe_function_arg (cum, mode, type);
9239 else if (abi == ABI_V4)
9241 if (TARGET_HARD_FLOAT && TARGET_FPRS
9242 && ((TARGET_SINGLE_FLOAT && mode == SFmode)
9243 || (TARGET_DOUBLE_FLOAT && mode == DFmode)
9244 || (mode == TFmode && !TARGET_IEEEQUAD)
9245 || mode == SDmode || mode == DDmode || mode == TDmode))
9247 /* _Decimal128 must use an even/odd register pair. This assumes
9248 that the register number is odd when fregno is odd. */
9249 if (mode == TDmode && (cum->fregno % 2) == 1)
9250 cum->fregno++;
9252 if (cum->fregno + (mode == TFmode || mode == TDmode ? 1 : 0)
9253 <= FP_ARG_V4_MAX_REG)
9254 return gen_rtx_REG (mode, cum->fregno);
9255 else
9256 return NULL_RTX;
9258 else
9260 int n_words = rs6000_arg_size (mode, type);
9261 int gregno = cum->sysv_gregno;
9263 /* Long long and SPE vectors are put in (r3,r4), (r5,r6),
9264 (r7,r8) or (r9,r10). As does any other 2 word item such
9265 as complex int due to a historical mistake. */
9266 if (n_words == 2)
9267 gregno += (1 - gregno) & 1;
9269 /* Multi-reg args are not split between registers and stack. */
9270 if (gregno + n_words - 1 > GP_ARG_MAX_REG)
9271 return NULL_RTX;
9273 if (TARGET_32BIT && TARGET_POWERPC64)
9274 return rs6000_mixed_function_arg (mode, type,
9275 gregno - GP_ARG_MIN_REG);
9276 return gen_rtx_REG (mode, gregno);
9279 else
9281 int align_words = rs6000_parm_start (mode, type, cum->words);
9283 /* _Decimal128 must be passed in an even/odd float register pair.
9284 This assumes that the register number is odd when fregno is odd. */
9285 if (mode == TDmode && (cum->fregno % 2) == 1)
9286 cum->fregno++;
9288 if (USE_FP_FOR_ARG_P (cum, mode, type))
9290 rtx rvec[GP_ARG_NUM_REG + 1];
9291 rtx r;
9292 int k;
9293 bool needs_psave;
9294 enum machine_mode fmode = mode;
9295 unsigned long n_fpreg = (GET_MODE_SIZE (mode) + 7) >> 3;
9297 if (cum->fregno + n_fpreg > FP_ARG_MAX_REG + 1)
9299 /* Currently, we only ever need one reg here because complex
9300 doubles are split. */
9301 gcc_assert (cum->fregno == FP_ARG_MAX_REG
9302 && (fmode == TFmode || fmode == TDmode));
9304 /* Long double or _Decimal128 split over regs and memory. */
9305 fmode = DECIMAL_FLOAT_MODE_P (fmode) ? DDmode : DFmode;
9308 /* Do we also need to pass this arg in the parameter save
9309 area? */
9310 needs_psave = (type
9311 && (cum->nargs_prototype <= 0
9312 || (DEFAULT_ABI == ABI_AIX
9313 && TARGET_XL_COMPAT
9314 && align_words >= GP_ARG_NUM_REG)));
9316 if (!needs_psave && mode == fmode)
9317 return gen_rtx_REG (fmode, cum->fregno);
9319 k = 0;
9320 if (needs_psave)
9322 /* Describe the part that goes in gprs or the stack.
9323 This piece must come first, before the fprs. */
9324 if (align_words < GP_ARG_NUM_REG)
9326 unsigned long n_words = rs6000_arg_size (mode, type);
9328 if (align_words + n_words > GP_ARG_NUM_REG
9329 || (TARGET_32BIT && TARGET_POWERPC64))
9331 /* If this is partially on the stack, then we only
9332 include the portion actually in registers here. */
9333 enum machine_mode rmode = TARGET_32BIT ? SImode : DImode;
9334 rtx off;
9335 int i = 0;
9336 if (align_words + n_words > GP_ARG_NUM_REG)
9337 /* Not all of the arg fits in gprs. Say that it
9338 goes in memory too, using a magic NULL_RTX
9339 component. Also see comment in
9340 rs6000_mixed_function_arg for why the normal
9341 function_arg_partial_nregs scheme doesn't work
9342 in this case. */
9343 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, NULL_RTX,
9344 const0_rtx);
9347 r = gen_rtx_REG (rmode,
9348 GP_ARG_MIN_REG + align_words);
9349 off = GEN_INT (i++ * GET_MODE_SIZE (rmode));
9350 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, off);
9352 while (++align_words < GP_ARG_NUM_REG && --n_words != 0);
9354 else
9356 /* The whole arg fits in gprs. */
9357 r = gen_rtx_REG (mode, GP_ARG_MIN_REG + align_words);
9358 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, const0_rtx);
9361 else
9362 /* It's entirely in memory. */
9363 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, NULL_RTX, const0_rtx);
9366 /* Describe where this piece goes in the fprs. */
9367 r = gen_rtx_REG (fmode, cum->fregno);
9368 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, const0_rtx);
9370 return gen_rtx_PARALLEL (mode, gen_rtvec_v (k, rvec));
9372 else if (align_words < GP_ARG_NUM_REG)
9374 if (TARGET_32BIT && TARGET_POWERPC64)
9375 return rs6000_mixed_function_arg (mode, type, align_words);
9377 if (mode == BLKmode)
9378 mode = Pmode;
9380 return gen_rtx_REG (mode, GP_ARG_MIN_REG + align_words);
9382 else
9383 return NULL_RTX;
9387 /* For an arg passed partly in registers and partly in memory, this is
9388 the number of bytes passed in registers. For args passed entirely in
9389 registers or entirely in memory, zero. When an arg is described by a
9390 PARALLEL, perhaps using more than one register type, this function
9391 returns the number of bytes used by the first element of the PARALLEL. */
9393 static int
9394 rs6000_arg_partial_bytes (cumulative_args_t cum_v, enum machine_mode mode,
9395 tree type, bool named)
9397 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
9398 int ret = 0;
9399 int align_words;
9401 if (DEFAULT_ABI == ABI_V4)
9402 return 0;
9404 if (USE_ALTIVEC_FOR_ARG_P (cum, mode, type, named)
9405 && cum->nargs_prototype >= 0)
9406 return 0;
9408 /* In this complicated case we just disable the partial_nregs code. */
9409 if (TARGET_MACHO && rs6000_darwin64_struct_check_p (mode, type))
9410 return 0;
9412 align_words = rs6000_parm_start (mode, type, cum->words);
9414 if (USE_FP_FOR_ARG_P (cum, mode, type))
9416 /* If we are passing this arg in the fixed parameter save area
9417 (gprs or memory) as well as fprs, then this function should
9418 return the number of partial bytes passed in the parameter
9419 save area rather than partial bytes passed in fprs. */
9420 if (type
9421 && (cum->nargs_prototype <= 0
9422 || (DEFAULT_ABI == ABI_AIX
9423 && TARGET_XL_COMPAT
9424 && align_words >= GP_ARG_NUM_REG)))
9425 return 0;
9426 else if (cum->fregno + ((GET_MODE_SIZE (mode) + 7) >> 3)
9427 > FP_ARG_MAX_REG + 1)
9428 ret = (FP_ARG_MAX_REG + 1 - cum->fregno) * 8;
9429 else if (cum->nargs_prototype >= 0)
9430 return 0;
9433 if (align_words < GP_ARG_NUM_REG
9434 && GP_ARG_NUM_REG < align_words + rs6000_arg_size (mode, type))
9435 ret = (GP_ARG_NUM_REG - align_words) * (TARGET_32BIT ? 4 : 8);
9437 if (ret != 0 && TARGET_DEBUG_ARG)
9438 fprintf (stderr, "rs6000_arg_partial_bytes: %d\n", ret);
9440 return ret;
9443 /* A C expression that indicates when an argument must be passed by
9444 reference. If nonzero for an argument, a copy of that argument is
9445 made in memory and a pointer to the argument is passed instead of
9446 the argument itself. The pointer is passed in whatever way is
9447 appropriate for passing a pointer to that type.
9449 Under V.4, aggregates and long double are passed by reference.
9451 As an extension to all 32-bit ABIs, AltiVec vectors are passed by
9452 reference unless the AltiVec vector extension ABI is in force.
9454 As an extension to all ABIs, variable sized types are passed by
9455 reference. */
9457 static bool
9458 rs6000_pass_by_reference (cumulative_args_t cum ATTRIBUTE_UNUSED,
9459 enum machine_mode mode, const_tree type,
9460 bool named ATTRIBUTE_UNUSED)
9462 if (DEFAULT_ABI == ABI_V4 && TARGET_IEEEQUAD && mode == TFmode)
9464 if (TARGET_DEBUG_ARG)
9465 fprintf (stderr, "function_arg_pass_by_reference: V4 long double\n");
9466 return 1;
9469 if (!type)
9470 return 0;
9472 if (DEFAULT_ABI == ABI_V4 && AGGREGATE_TYPE_P (type))
9474 if (TARGET_DEBUG_ARG)
9475 fprintf (stderr, "function_arg_pass_by_reference: V4 aggregate\n");
9476 return 1;
9479 if (int_size_in_bytes (type) < 0)
9481 if (TARGET_DEBUG_ARG)
9482 fprintf (stderr, "function_arg_pass_by_reference: variable size\n");
9483 return 1;
9486 /* Allow -maltivec -mabi=no-altivec without warning. Altivec vector
9487 modes only exist for GCC vector types if -maltivec. */
9488 if (TARGET_32BIT && !TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (mode))
9490 if (TARGET_DEBUG_ARG)
9491 fprintf (stderr, "function_arg_pass_by_reference: AltiVec\n");
9492 return 1;
9495 /* Pass synthetic vectors in memory. */
9496 if (TREE_CODE (type) == VECTOR_TYPE
9497 && int_size_in_bytes (type) > (TARGET_ALTIVEC_ABI ? 16 : 8))
9499 static bool warned_for_pass_big_vectors = false;
9500 if (TARGET_DEBUG_ARG)
9501 fprintf (stderr, "function_arg_pass_by_reference: synthetic vector\n");
9502 if (!warned_for_pass_big_vectors)
9504 warning (0, "GCC vector passed by reference: "
9505 "non-standard ABI extension with no compatibility guarantee");
9506 warned_for_pass_big_vectors = true;
9508 return 1;
9511 return 0;
9514 static void
9515 rs6000_move_block_from_reg (int regno, rtx x, int nregs)
9517 int i;
9518 enum machine_mode reg_mode = TARGET_32BIT ? SImode : DImode;
9520 if (nregs == 0)
9521 return;
9523 for (i = 0; i < nregs; i++)
9525 rtx tem = adjust_address_nv (x, reg_mode, i * GET_MODE_SIZE (reg_mode));
9526 if (reload_completed)
9528 if (! strict_memory_address_p (reg_mode, XEXP (tem, 0)))
9529 tem = NULL_RTX;
9530 else
9531 tem = simplify_gen_subreg (reg_mode, x, BLKmode,
9532 i * GET_MODE_SIZE (reg_mode));
9534 else
9535 tem = replace_equiv_address (tem, XEXP (tem, 0));
9537 gcc_assert (tem);
9539 emit_move_insn (tem, gen_rtx_REG (reg_mode, regno + i));
9543 /* Perform any needed actions needed for a function that is receiving a
9544 variable number of arguments.
9546 CUM is as above.
9548 MODE and TYPE are the mode and type of the current parameter.
9550 PRETEND_SIZE is a variable that should be set to the amount of stack
9551 that must be pushed by the prolog to pretend that our caller pushed
9554 Normally, this macro will push all remaining incoming registers on the
9555 stack and set PRETEND_SIZE to the length of the registers pushed. */
9557 static void
9558 setup_incoming_varargs (cumulative_args_t cum, enum machine_mode mode,
9559 tree type, int *pretend_size ATTRIBUTE_UNUSED,
9560 int no_rtl)
9562 CUMULATIVE_ARGS next_cum;
9563 int reg_size = TARGET_32BIT ? 4 : 8;
9564 rtx save_area = NULL_RTX, mem;
9565 int first_reg_offset;
9566 alias_set_type set;
9568 /* Skip the last named argument. */
9569 next_cum = *get_cumulative_args (cum);
9570 rs6000_function_arg_advance_1 (&next_cum, mode, type, true, 0);
9572 if (DEFAULT_ABI == ABI_V4)
9574 first_reg_offset = next_cum.sysv_gregno - GP_ARG_MIN_REG;
9576 if (! no_rtl)
9578 int gpr_reg_num = 0, gpr_size = 0, fpr_size = 0;
9579 HOST_WIDE_INT offset = 0;
9581 /* Try to optimize the size of the varargs save area.
9582 The ABI requires that ap.reg_save_area is doubleword
9583 aligned, but we don't need to allocate space for all
9584 the bytes, only those to which we actually will save
9585 anything. */
9586 if (cfun->va_list_gpr_size && first_reg_offset < GP_ARG_NUM_REG)
9587 gpr_reg_num = GP_ARG_NUM_REG - first_reg_offset;
9588 if (TARGET_HARD_FLOAT && TARGET_FPRS
9589 && next_cum.fregno <= FP_ARG_V4_MAX_REG
9590 && cfun->va_list_fpr_size)
9592 if (gpr_reg_num)
9593 fpr_size = (next_cum.fregno - FP_ARG_MIN_REG)
9594 * UNITS_PER_FP_WORD;
9595 if (cfun->va_list_fpr_size
9596 < FP_ARG_V4_MAX_REG + 1 - next_cum.fregno)
9597 fpr_size += cfun->va_list_fpr_size * UNITS_PER_FP_WORD;
9598 else
9599 fpr_size += (FP_ARG_V4_MAX_REG + 1 - next_cum.fregno)
9600 * UNITS_PER_FP_WORD;
9602 if (gpr_reg_num)
9604 offset = -((first_reg_offset * reg_size) & ~7);
9605 if (!fpr_size && gpr_reg_num > cfun->va_list_gpr_size)
9607 gpr_reg_num = cfun->va_list_gpr_size;
9608 if (reg_size == 4 && (first_reg_offset & 1))
9609 gpr_reg_num++;
9611 gpr_size = (gpr_reg_num * reg_size + 7) & ~7;
9613 else if (fpr_size)
9614 offset = - (int) (next_cum.fregno - FP_ARG_MIN_REG)
9615 * UNITS_PER_FP_WORD
9616 - (int) (GP_ARG_NUM_REG * reg_size);
9618 if (gpr_size + fpr_size)
9620 rtx reg_save_area
9621 = assign_stack_local (BLKmode, gpr_size + fpr_size, 64);
9622 gcc_assert (GET_CODE (reg_save_area) == MEM);
9623 reg_save_area = XEXP (reg_save_area, 0);
9624 if (GET_CODE (reg_save_area) == PLUS)
9626 gcc_assert (XEXP (reg_save_area, 0)
9627 == virtual_stack_vars_rtx);
9628 gcc_assert (GET_CODE (XEXP (reg_save_area, 1)) == CONST_INT);
9629 offset += INTVAL (XEXP (reg_save_area, 1));
9631 else
9632 gcc_assert (reg_save_area == virtual_stack_vars_rtx);
9635 cfun->machine->varargs_save_offset = offset;
9636 save_area = plus_constant (Pmode, virtual_stack_vars_rtx, offset);
9639 else
9641 first_reg_offset = next_cum.words;
9642 save_area = virtual_incoming_args_rtx;
9644 if (targetm.calls.must_pass_in_stack (mode, type))
9645 first_reg_offset += rs6000_arg_size (TYPE_MODE (type), type);
9648 set = get_varargs_alias_set ();
9649 if (! no_rtl && first_reg_offset < GP_ARG_NUM_REG
9650 && cfun->va_list_gpr_size)
9652 int n_gpr, nregs = GP_ARG_NUM_REG - first_reg_offset;
9654 if (va_list_gpr_counter_field)
9655 /* V4 va_list_gpr_size counts number of registers needed. */
9656 n_gpr = cfun->va_list_gpr_size;
9657 else
9658 /* char * va_list instead counts number of bytes needed. */
9659 n_gpr = (cfun->va_list_gpr_size + reg_size - 1) / reg_size;
9661 if (nregs > n_gpr)
9662 nregs = n_gpr;
9664 mem = gen_rtx_MEM (BLKmode,
9665 plus_constant (Pmode, save_area,
9666 first_reg_offset * reg_size));
9667 MEM_NOTRAP_P (mem) = 1;
9668 set_mem_alias_set (mem, set);
9669 set_mem_align (mem, BITS_PER_WORD);
9671 rs6000_move_block_from_reg (GP_ARG_MIN_REG + first_reg_offset, mem,
9672 nregs);
9675 /* Save FP registers if needed. */
9676 if (DEFAULT_ABI == ABI_V4
9677 && TARGET_HARD_FLOAT && TARGET_FPRS
9678 && ! no_rtl
9679 && next_cum.fregno <= FP_ARG_V4_MAX_REG
9680 && cfun->va_list_fpr_size)
9682 int fregno = next_cum.fregno, nregs;
9683 rtx cr1 = gen_rtx_REG (CCmode, CR1_REGNO);
9684 rtx lab = gen_label_rtx ();
9685 int off = (GP_ARG_NUM_REG * reg_size) + ((fregno - FP_ARG_MIN_REG)
9686 * UNITS_PER_FP_WORD);
9688 emit_jump_insn
9689 (gen_rtx_SET (VOIDmode,
9690 pc_rtx,
9691 gen_rtx_IF_THEN_ELSE (VOIDmode,
9692 gen_rtx_NE (VOIDmode, cr1,
9693 const0_rtx),
9694 gen_rtx_LABEL_REF (VOIDmode, lab),
9695 pc_rtx)));
9697 for (nregs = 0;
9698 fregno <= FP_ARG_V4_MAX_REG && nregs < cfun->va_list_fpr_size;
9699 fregno++, off += UNITS_PER_FP_WORD, nregs++)
9701 mem = gen_rtx_MEM ((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT)
9702 ? DFmode : SFmode,
9703 plus_constant (Pmode, save_area, off));
9704 MEM_NOTRAP_P (mem) = 1;
9705 set_mem_alias_set (mem, set);
9706 set_mem_align (mem, GET_MODE_ALIGNMENT (
9707 (TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT)
9708 ? DFmode : SFmode));
9709 emit_move_insn (mem, gen_rtx_REG (
9710 (TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT)
9711 ? DFmode : SFmode, fregno));
9714 emit_label (lab);
9718 /* Create the va_list data type. */
9720 static tree
9721 rs6000_build_builtin_va_list (void)
9723 tree f_gpr, f_fpr, f_res, f_ovf, f_sav, record, type_decl;
9725 /* For AIX, prefer 'char *' because that's what the system
9726 header files like. */
9727 if (DEFAULT_ABI != ABI_V4)
9728 return build_pointer_type (char_type_node);
9730 record = (*lang_hooks.types.make_type) (RECORD_TYPE);
9731 type_decl = build_decl (BUILTINS_LOCATION, TYPE_DECL,
9732 get_identifier ("__va_list_tag"), record);
9734 f_gpr = build_decl (BUILTINS_LOCATION, FIELD_DECL, get_identifier ("gpr"),
9735 unsigned_char_type_node);
9736 f_fpr = build_decl (BUILTINS_LOCATION, FIELD_DECL, get_identifier ("fpr"),
9737 unsigned_char_type_node);
9738 /* Give the two bytes of padding a name, so that -Wpadded won't warn on
9739 every user file. */
9740 f_res = build_decl (BUILTINS_LOCATION, FIELD_DECL,
9741 get_identifier ("reserved"), short_unsigned_type_node);
9742 f_ovf = build_decl (BUILTINS_LOCATION, FIELD_DECL,
9743 get_identifier ("overflow_arg_area"),
9744 ptr_type_node);
9745 f_sav = build_decl (BUILTINS_LOCATION, FIELD_DECL,
9746 get_identifier ("reg_save_area"),
9747 ptr_type_node);
9749 va_list_gpr_counter_field = f_gpr;
9750 va_list_fpr_counter_field = f_fpr;
9752 DECL_FIELD_CONTEXT (f_gpr) = record;
9753 DECL_FIELD_CONTEXT (f_fpr) = record;
9754 DECL_FIELD_CONTEXT (f_res) = record;
9755 DECL_FIELD_CONTEXT (f_ovf) = record;
9756 DECL_FIELD_CONTEXT (f_sav) = record;
9758 TYPE_STUB_DECL (record) = type_decl;
9759 TYPE_NAME (record) = type_decl;
9760 TYPE_FIELDS (record) = f_gpr;
9761 DECL_CHAIN (f_gpr) = f_fpr;
9762 DECL_CHAIN (f_fpr) = f_res;
9763 DECL_CHAIN (f_res) = f_ovf;
9764 DECL_CHAIN (f_ovf) = f_sav;
9766 layout_type (record);
9768 /* The correct type is an array type of one element. */
9769 return build_array_type (record, build_index_type (size_zero_node));
9772 /* Implement va_start. */
9774 static void
9775 rs6000_va_start (tree valist, rtx nextarg)
9777 HOST_WIDE_INT words, n_gpr, n_fpr;
9778 tree f_gpr, f_fpr, f_res, f_ovf, f_sav;
9779 tree gpr, fpr, ovf, sav, t;
9781 /* Only SVR4 needs something special. */
9782 if (DEFAULT_ABI != ABI_V4)
9784 std_expand_builtin_va_start (valist, nextarg);
9785 return;
9788 f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
9789 f_fpr = DECL_CHAIN (f_gpr);
9790 f_res = DECL_CHAIN (f_fpr);
9791 f_ovf = DECL_CHAIN (f_res);
9792 f_sav = DECL_CHAIN (f_ovf);
9794 valist = build_simple_mem_ref (valist);
9795 gpr = build3 (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr, NULL_TREE);
9796 fpr = build3 (COMPONENT_REF, TREE_TYPE (f_fpr), unshare_expr (valist),
9797 f_fpr, NULL_TREE);
9798 ovf = build3 (COMPONENT_REF, TREE_TYPE (f_ovf), unshare_expr (valist),
9799 f_ovf, NULL_TREE);
9800 sav = build3 (COMPONENT_REF, TREE_TYPE (f_sav), unshare_expr (valist),
9801 f_sav, NULL_TREE);
9803 /* Count number of gp and fp argument registers used. */
9804 words = crtl->args.info.words;
9805 n_gpr = MIN (crtl->args.info.sysv_gregno - GP_ARG_MIN_REG,
9806 GP_ARG_NUM_REG);
9807 n_fpr = MIN (crtl->args.info.fregno - FP_ARG_MIN_REG,
9808 FP_ARG_NUM_REG);
9810 if (TARGET_DEBUG_ARG)
9811 fprintf (stderr, "va_start: words = "HOST_WIDE_INT_PRINT_DEC", n_gpr = "
9812 HOST_WIDE_INT_PRINT_DEC", n_fpr = "HOST_WIDE_INT_PRINT_DEC"\n",
9813 words, n_gpr, n_fpr);
9815 if (cfun->va_list_gpr_size)
9817 t = build2 (MODIFY_EXPR, TREE_TYPE (gpr), gpr,
9818 build_int_cst (NULL_TREE, n_gpr));
9819 TREE_SIDE_EFFECTS (t) = 1;
9820 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
9823 if (cfun->va_list_fpr_size)
9825 t = build2 (MODIFY_EXPR, TREE_TYPE (fpr), fpr,
9826 build_int_cst (NULL_TREE, n_fpr));
9827 TREE_SIDE_EFFECTS (t) = 1;
9828 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
9830 #ifdef HAVE_AS_GNU_ATTRIBUTE
9831 if (call_ABI_of_interest (cfun->decl))
9832 rs6000_passes_float = true;
9833 #endif
9836 /* Find the overflow area. */
9837 t = make_tree (TREE_TYPE (ovf), virtual_incoming_args_rtx);
9838 if (words != 0)
9839 t = fold_build_pointer_plus_hwi (t, words * UNITS_PER_WORD);
9840 t = build2 (MODIFY_EXPR, TREE_TYPE (ovf), ovf, t);
9841 TREE_SIDE_EFFECTS (t) = 1;
9842 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
9844 /* If there were no va_arg invocations, don't set up the register
9845 save area. */
9846 if (!cfun->va_list_gpr_size
9847 && !cfun->va_list_fpr_size
9848 && n_gpr < GP_ARG_NUM_REG
9849 && n_fpr < FP_ARG_V4_MAX_REG)
9850 return;
9852 /* Find the register save area. */
9853 t = make_tree (TREE_TYPE (sav), virtual_stack_vars_rtx);
9854 if (cfun->machine->varargs_save_offset)
9855 t = fold_build_pointer_plus_hwi (t, cfun->machine->varargs_save_offset);
9856 t = build2 (MODIFY_EXPR, TREE_TYPE (sav), sav, t);
9857 TREE_SIDE_EFFECTS (t) = 1;
9858 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
9861 /* Implement va_arg. */
9863 static tree
9864 rs6000_gimplify_va_arg (tree valist, tree type, gimple_seq *pre_p,
9865 gimple_seq *post_p)
9867 tree f_gpr, f_fpr, f_res, f_ovf, f_sav;
9868 tree gpr, fpr, ovf, sav, reg, t, u;
9869 int size, rsize, n_reg, sav_ofs, sav_scale;
9870 tree lab_false, lab_over, addr;
9871 int align;
9872 tree ptrtype = build_pointer_type_for_mode (type, ptr_mode, true);
9873 int regalign = 0;
9874 gimple stmt;
9876 if (pass_by_reference (NULL, TYPE_MODE (type), type, false))
9878 t = rs6000_gimplify_va_arg (valist, ptrtype, pre_p, post_p);
9879 return build_va_arg_indirect_ref (t);
9882 /* We need to deal with the fact that the darwin ppc64 ABI is defined by an
9883 earlier version of gcc, with the property that it always applied alignment
9884 adjustments to the va-args (even for zero-sized types). The cheapest way
9885 to deal with this is to replicate the effect of the part of
9886 std_gimplify_va_arg_expr that carries out the align adjust, for the case
9887 of relevance.
9888 We don't need to check for pass-by-reference because of the test above.
9889 We can return a simplifed answer, since we know there's no offset to add. */
9891 if (((TARGET_MACHO
9892 && rs6000_darwin64_abi)
9893 || (DEFAULT_ABI == ABI_AIX && !rs6000_compat_align_parm))
9894 && integer_zerop (TYPE_SIZE (type)))
9896 unsigned HOST_WIDE_INT align, boundary;
9897 tree valist_tmp = get_initialized_tmp_var (valist, pre_p, NULL);
9898 align = PARM_BOUNDARY / BITS_PER_UNIT;
9899 boundary = rs6000_function_arg_boundary (TYPE_MODE (type), type);
9900 if (boundary > MAX_SUPPORTED_STACK_ALIGNMENT)
9901 boundary = MAX_SUPPORTED_STACK_ALIGNMENT;
9902 boundary /= BITS_PER_UNIT;
9903 if (boundary > align)
9905 tree t ;
9906 /* This updates arg ptr by the amount that would be necessary
9907 to align the zero-sized (but not zero-alignment) item. */
9908 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
9909 fold_build_pointer_plus_hwi (valist_tmp, boundary - 1));
9910 gimplify_and_add (t, pre_p);
9912 t = fold_convert (sizetype, valist_tmp);
9913 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
9914 fold_convert (TREE_TYPE (valist),
9915 fold_build2 (BIT_AND_EXPR, sizetype, t,
9916 size_int (-boundary))));
9917 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist, t);
9918 gimplify_and_add (t, pre_p);
9920 /* Since it is zero-sized there's no increment for the item itself. */
9921 valist_tmp = fold_convert (build_pointer_type (type), valist_tmp);
9922 return build_va_arg_indirect_ref (valist_tmp);
9925 if (DEFAULT_ABI != ABI_V4)
9927 if (targetm.calls.split_complex_arg && TREE_CODE (type) == COMPLEX_TYPE)
9929 tree elem_type = TREE_TYPE (type);
9930 enum machine_mode elem_mode = TYPE_MODE (elem_type);
9931 int elem_size = GET_MODE_SIZE (elem_mode);
9933 if (elem_size < UNITS_PER_WORD)
9935 tree real_part, imag_part;
9936 gimple_seq post = NULL;
9938 real_part = rs6000_gimplify_va_arg (valist, elem_type, pre_p,
9939 &post);
9940 /* Copy the value into a temporary, lest the formal temporary
9941 be reused out from under us. */
9942 real_part = get_initialized_tmp_var (real_part, pre_p, &post);
9943 gimple_seq_add_seq (pre_p, post);
9945 imag_part = rs6000_gimplify_va_arg (valist, elem_type, pre_p,
9946 post_p);
9948 return build2 (COMPLEX_EXPR, type, real_part, imag_part);
9952 return std_gimplify_va_arg_expr (valist, type, pre_p, post_p);
9955 f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
9956 f_fpr = DECL_CHAIN (f_gpr);
9957 f_res = DECL_CHAIN (f_fpr);
9958 f_ovf = DECL_CHAIN (f_res);
9959 f_sav = DECL_CHAIN (f_ovf);
9961 valist = build_va_arg_indirect_ref (valist);
9962 gpr = build3 (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr, NULL_TREE);
9963 fpr = build3 (COMPONENT_REF, TREE_TYPE (f_fpr), unshare_expr (valist),
9964 f_fpr, NULL_TREE);
9965 ovf = build3 (COMPONENT_REF, TREE_TYPE (f_ovf), unshare_expr (valist),
9966 f_ovf, NULL_TREE);
9967 sav = build3 (COMPONENT_REF, TREE_TYPE (f_sav), unshare_expr (valist),
9968 f_sav, NULL_TREE);
9970 size = int_size_in_bytes (type);
9971 rsize = (size + 3) / 4;
9972 align = 1;
9974 if (TARGET_HARD_FLOAT && TARGET_FPRS
9975 && ((TARGET_SINGLE_FLOAT && TYPE_MODE (type) == SFmode)
9976 || (TARGET_DOUBLE_FLOAT
9977 && (TYPE_MODE (type) == DFmode
9978 || TYPE_MODE (type) == TFmode
9979 || TYPE_MODE (type) == SDmode
9980 || TYPE_MODE (type) == DDmode
9981 || TYPE_MODE (type) == TDmode))))
9983 /* FP args go in FP registers, if present. */
9984 reg = fpr;
9985 n_reg = (size + 7) / 8;
9986 sav_ofs = ((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT) ? 8 : 4) * 4;
9987 sav_scale = ((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT) ? 8 : 4);
9988 if (TYPE_MODE (type) != SFmode && TYPE_MODE (type) != SDmode)
9989 align = 8;
9991 else
9993 /* Otherwise into GP registers. */
9994 reg = gpr;
9995 n_reg = rsize;
9996 sav_ofs = 0;
9997 sav_scale = 4;
9998 if (n_reg == 2)
9999 align = 8;
10002 /* Pull the value out of the saved registers.... */
10004 lab_over = NULL;
10005 addr = create_tmp_var (ptr_type_node, "addr");
10007 /* AltiVec vectors never go in registers when -mabi=altivec. */
10008 if (TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (TYPE_MODE (type)))
10009 align = 16;
10010 else
10012 lab_false = create_artificial_label (input_location);
10013 lab_over = create_artificial_label (input_location);
10015 /* Long long and SPE vectors are aligned in the registers.
10016 As are any other 2 gpr item such as complex int due to a
10017 historical mistake. */
10018 u = reg;
10019 if (n_reg == 2 && reg == gpr)
10021 regalign = 1;
10022 u = build2 (BIT_AND_EXPR, TREE_TYPE (reg), unshare_expr (reg),
10023 build_int_cst (TREE_TYPE (reg), n_reg - 1));
10024 u = build2 (POSTINCREMENT_EXPR, TREE_TYPE (reg),
10025 unshare_expr (reg), u);
10027 /* _Decimal128 is passed in even/odd fpr pairs; the stored
10028 reg number is 0 for f1, so we want to make it odd. */
10029 else if (reg == fpr && TYPE_MODE (type) == TDmode)
10031 t = build2 (BIT_IOR_EXPR, TREE_TYPE (reg), unshare_expr (reg),
10032 build_int_cst (TREE_TYPE (reg), 1));
10033 u = build2 (MODIFY_EXPR, void_type_node, unshare_expr (reg), t);
10036 t = fold_convert (TREE_TYPE (reg), size_int (8 - n_reg + 1));
10037 t = build2 (GE_EXPR, boolean_type_node, u, t);
10038 u = build1 (GOTO_EXPR, void_type_node, lab_false);
10039 t = build3 (COND_EXPR, void_type_node, t, u, NULL_TREE);
10040 gimplify_and_add (t, pre_p);
10042 t = sav;
10043 if (sav_ofs)
10044 t = fold_build_pointer_plus_hwi (sav, sav_ofs);
10046 u = build2 (POSTINCREMENT_EXPR, TREE_TYPE (reg), unshare_expr (reg),
10047 build_int_cst (TREE_TYPE (reg), n_reg));
10048 u = fold_convert (sizetype, u);
10049 u = build2 (MULT_EXPR, sizetype, u, size_int (sav_scale));
10050 t = fold_build_pointer_plus (t, u);
10052 /* _Decimal32 varargs are located in the second word of the 64-bit
10053 FP register for 32-bit binaries. */
10054 if (!TARGET_POWERPC64
10055 && TARGET_HARD_FLOAT && TARGET_FPRS
10056 && TYPE_MODE (type) == SDmode)
10057 t = fold_build_pointer_plus_hwi (t, size);
10059 gimplify_assign (addr, t, pre_p);
10061 gimple_seq_add_stmt (pre_p, gimple_build_goto (lab_over));
10063 stmt = gimple_build_label (lab_false);
10064 gimple_seq_add_stmt (pre_p, stmt);
10066 if ((n_reg == 2 && !regalign) || n_reg > 2)
10068 /* Ensure that we don't find any more args in regs.
10069 Alignment has taken care of for special cases. */
10070 gimplify_assign (reg, build_int_cst (TREE_TYPE (reg), 8), pre_p);
10074 /* ... otherwise out of the overflow area. */
10076 /* Care for on-stack alignment if needed. */
10077 t = ovf;
10078 if (align != 1)
10080 t = fold_build_pointer_plus_hwi (t, align - 1);
10081 t = build2 (BIT_AND_EXPR, TREE_TYPE (t), t,
10082 build_int_cst (TREE_TYPE (t), -align));
10084 gimplify_expr (&t, pre_p, NULL, is_gimple_val, fb_rvalue);
10086 gimplify_assign (unshare_expr (addr), t, pre_p);
10088 t = fold_build_pointer_plus_hwi (t, size);
10089 gimplify_assign (unshare_expr (ovf), t, pre_p);
10091 if (lab_over)
10093 stmt = gimple_build_label (lab_over);
10094 gimple_seq_add_stmt (pre_p, stmt);
10097 if (STRICT_ALIGNMENT
10098 && (TYPE_ALIGN (type)
10099 > (unsigned) BITS_PER_UNIT * (align < 4 ? 4 : align)))
10101 /* The value (of type complex double, for example) may not be
10102 aligned in memory in the saved registers, so copy via a
10103 temporary. (This is the same code as used for SPARC.) */
10104 tree tmp = create_tmp_var (type, "va_arg_tmp");
10105 tree dest_addr = build_fold_addr_expr (tmp);
10107 tree copy = build_call_expr (builtin_decl_implicit (BUILT_IN_MEMCPY),
10108 3, dest_addr, addr, size_int (rsize * 4));
10110 gimplify_and_add (copy, pre_p);
10111 addr = dest_addr;
10114 addr = fold_convert (ptrtype, addr);
10115 return build_va_arg_indirect_ref (addr);
10118 /* Builtins. */
10120 static void
10121 def_builtin (const char *name, tree type, enum rs6000_builtins code)
10123 tree t;
10124 unsigned classify = rs6000_builtin_info[(int)code].attr;
10125 const char *attr_string = "";
10127 gcc_assert (name != NULL);
10128 gcc_assert (IN_RANGE ((int)code, 0, (int)RS6000_BUILTIN_COUNT));
10130 if (rs6000_builtin_decls[(int)code])
10131 fatal_error ("internal error: builtin function %s already processed", name);
10133 rs6000_builtin_decls[(int)code] = t =
10134 add_builtin_function (name, type, (int)code, BUILT_IN_MD, NULL, NULL_TREE);
10136 /* Set any special attributes. */
10137 if ((classify & RS6000_BTC_CONST) != 0)
10139 /* const function, function only depends on the inputs. */
10140 TREE_READONLY (t) = 1;
10141 TREE_NOTHROW (t) = 1;
10142 attr_string = ", pure";
10144 else if ((classify & RS6000_BTC_PURE) != 0)
10146 /* pure function, function can read global memory, but does not set any
10147 external state. */
10148 DECL_PURE_P (t) = 1;
10149 TREE_NOTHROW (t) = 1;
10150 attr_string = ", const";
10152 else if ((classify & RS6000_BTC_FP) != 0)
10154 /* Function is a math function. If rounding mode is on, then treat the
10155 function as not reading global memory, but it can have arbitrary side
10156 effects. If it is off, then assume the function is a const function.
10157 This mimics the ATTR_MATHFN_FPROUNDING attribute in
10158 builtin-attribute.def that is used for the math functions. */
10159 TREE_NOTHROW (t) = 1;
10160 if (flag_rounding_math)
10162 DECL_PURE_P (t) = 1;
10163 DECL_IS_NOVOPS (t) = 1;
10164 attr_string = ", fp, pure";
10166 else
10168 TREE_READONLY (t) = 1;
10169 attr_string = ", fp, const";
10172 else if ((classify & RS6000_BTC_ATTR_MASK) != 0)
10173 gcc_unreachable ();
10175 if (TARGET_DEBUG_BUILTIN)
10176 fprintf (stderr, "rs6000_builtin, code = %4d, %s%s\n",
10177 (int)code, name, attr_string);
10180 /* Simple ternary operations: VECd = foo (VECa, VECb, VECc). */
10182 #undef RS6000_BUILTIN_1
10183 #undef RS6000_BUILTIN_2
10184 #undef RS6000_BUILTIN_3
10185 #undef RS6000_BUILTIN_A
10186 #undef RS6000_BUILTIN_D
10187 #undef RS6000_BUILTIN_E
10188 #undef RS6000_BUILTIN_H
10189 #undef RS6000_BUILTIN_P
10190 #undef RS6000_BUILTIN_Q
10191 #undef RS6000_BUILTIN_S
10192 #undef RS6000_BUILTIN_X
10194 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
10195 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
10196 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE) \
10197 { MASK, ICODE, NAME, ENUM },
10199 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
10200 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
10201 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
10202 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
10203 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
10204 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
10205 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
10206 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
10208 static const struct builtin_description bdesc_3arg[] =
10210 #include "rs6000-builtin.def"
10213 /* DST operations: void foo (void *, const int, const char). */
10215 #undef RS6000_BUILTIN_1
10216 #undef RS6000_BUILTIN_2
10217 #undef RS6000_BUILTIN_3
10218 #undef RS6000_BUILTIN_A
10219 #undef RS6000_BUILTIN_D
10220 #undef RS6000_BUILTIN_E
10221 #undef RS6000_BUILTIN_H
10222 #undef RS6000_BUILTIN_P
10223 #undef RS6000_BUILTIN_Q
10224 #undef RS6000_BUILTIN_S
10225 #undef RS6000_BUILTIN_X
10227 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
10228 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
10229 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
10230 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
10231 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE) \
10232 { MASK, ICODE, NAME, ENUM },
10234 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
10235 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
10236 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
10237 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
10238 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
10239 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
10241 static const struct builtin_description bdesc_dst[] =
10243 #include "rs6000-builtin.def"
10246 /* Simple binary operations: VECc = foo (VECa, VECb). */
10248 #undef RS6000_BUILTIN_1
10249 #undef RS6000_BUILTIN_2
10250 #undef RS6000_BUILTIN_3
10251 #undef RS6000_BUILTIN_A
10252 #undef RS6000_BUILTIN_D
10253 #undef RS6000_BUILTIN_E
10254 #undef RS6000_BUILTIN_H
10255 #undef RS6000_BUILTIN_P
10256 #undef RS6000_BUILTIN_Q
10257 #undef RS6000_BUILTIN_S
10258 #undef RS6000_BUILTIN_X
10260 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
10261 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE) \
10262 { MASK, ICODE, NAME, ENUM },
10264 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
10265 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
10266 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
10267 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
10268 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
10269 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
10270 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
10271 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
10272 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
10274 static const struct builtin_description bdesc_2arg[] =
10276 #include "rs6000-builtin.def"
10279 #undef RS6000_BUILTIN_1
10280 #undef RS6000_BUILTIN_2
10281 #undef RS6000_BUILTIN_3
10282 #undef RS6000_BUILTIN_A
10283 #undef RS6000_BUILTIN_D
10284 #undef RS6000_BUILTIN_E
10285 #undef RS6000_BUILTIN_H
10286 #undef RS6000_BUILTIN_P
10287 #undef RS6000_BUILTIN_Q
10288 #undef RS6000_BUILTIN_S
10289 #undef RS6000_BUILTIN_X
10291 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
10292 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
10293 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
10294 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
10295 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
10296 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
10297 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
10298 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE) \
10299 { MASK, ICODE, NAME, ENUM },
10301 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
10302 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
10303 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
10305 /* AltiVec predicates. */
10307 static const struct builtin_description bdesc_altivec_preds[] =
10309 #include "rs6000-builtin.def"
10312 /* SPE predicates. */
10313 #undef RS6000_BUILTIN_1
10314 #undef RS6000_BUILTIN_2
10315 #undef RS6000_BUILTIN_3
10316 #undef RS6000_BUILTIN_A
10317 #undef RS6000_BUILTIN_D
10318 #undef RS6000_BUILTIN_E
10319 #undef RS6000_BUILTIN_H
10320 #undef RS6000_BUILTIN_P
10321 #undef RS6000_BUILTIN_Q
10322 #undef RS6000_BUILTIN_S
10323 #undef RS6000_BUILTIN_X
10325 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
10326 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
10327 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
10328 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
10329 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
10330 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
10331 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
10332 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
10333 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
10334 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE) \
10335 { MASK, ICODE, NAME, ENUM },
10337 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
10339 static const struct builtin_description bdesc_spe_predicates[] =
10341 #include "rs6000-builtin.def"
10344 /* SPE evsel predicates. */
10345 #undef RS6000_BUILTIN_1
10346 #undef RS6000_BUILTIN_2
10347 #undef RS6000_BUILTIN_3
10348 #undef RS6000_BUILTIN_A
10349 #undef RS6000_BUILTIN_D
10350 #undef RS6000_BUILTIN_E
10351 #undef RS6000_BUILTIN_H
10352 #undef RS6000_BUILTIN_P
10353 #undef RS6000_BUILTIN_Q
10354 #undef RS6000_BUILTIN_S
10355 #undef RS6000_BUILTIN_X
10357 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
10358 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
10359 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
10360 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
10361 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
10362 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE) \
10363 { MASK, ICODE, NAME, ENUM },
10365 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
10366 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
10367 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
10368 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
10369 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
10371 static const struct builtin_description bdesc_spe_evsel[] =
10373 #include "rs6000-builtin.def"
10376 /* PAIRED predicates. */
10377 #undef RS6000_BUILTIN_1
10378 #undef RS6000_BUILTIN_2
10379 #undef RS6000_BUILTIN_3
10380 #undef RS6000_BUILTIN_A
10381 #undef RS6000_BUILTIN_D
10382 #undef RS6000_BUILTIN_E
10383 #undef RS6000_BUILTIN_H
10384 #undef RS6000_BUILTIN_P
10385 #undef RS6000_BUILTIN_Q
10386 #undef RS6000_BUILTIN_S
10387 #undef RS6000_BUILTIN_X
10389 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
10390 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
10391 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
10392 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
10393 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
10394 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
10395 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
10396 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
10397 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE) \
10398 { MASK, ICODE, NAME, ENUM },
10400 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
10401 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
10403 static const struct builtin_description bdesc_paired_preds[] =
10405 #include "rs6000-builtin.def"
10408 /* ABS* operations. */
10410 #undef RS6000_BUILTIN_1
10411 #undef RS6000_BUILTIN_2
10412 #undef RS6000_BUILTIN_3
10413 #undef RS6000_BUILTIN_A
10414 #undef RS6000_BUILTIN_D
10415 #undef RS6000_BUILTIN_E
10416 #undef RS6000_BUILTIN_H
10417 #undef RS6000_BUILTIN_P
10418 #undef RS6000_BUILTIN_Q
10419 #undef RS6000_BUILTIN_S
10420 #undef RS6000_BUILTIN_X
10422 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
10423 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
10424 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
10425 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE) \
10426 { MASK, ICODE, NAME, ENUM },
10428 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
10429 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
10430 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
10431 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
10432 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
10433 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
10434 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
10436 static const struct builtin_description bdesc_abs[] =
10438 #include "rs6000-builtin.def"
10441 /* Simple unary operations: VECb = foo (unsigned literal) or VECb =
10442 foo (VECa). */
10444 #undef RS6000_BUILTIN_1
10445 #undef RS6000_BUILTIN_2
10446 #undef RS6000_BUILTIN_3
10447 #undef RS6000_BUILTIN_A
10448 #undef RS6000_BUILTIN_D
10449 #undef RS6000_BUILTIN_E
10450 #undef RS6000_BUILTIN_H
10451 #undef RS6000_BUILTIN_P
10452 #undef RS6000_BUILTIN_Q
10453 #undef RS6000_BUILTIN_S
10454 #undef RS6000_BUILTIN_X
10456 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE) \
10457 { MASK, ICODE, NAME, ENUM },
10459 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
10460 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
10461 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
10462 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
10463 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
10464 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE)
10465 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
10466 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
10467 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
10468 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
10470 static const struct builtin_description bdesc_1arg[] =
10472 #include "rs6000-builtin.def"
10475 /* HTM builtins. */
10476 #undef RS6000_BUILTIN_1
10477 #undef RS6000_BUILTIN_2
10478 #undef RS6000_BUILTIN_3
10479 #undef RS6000_BUILTIN_A
10480 #undef RS6000_BUILTIN_D
10481 #undef RS6000_BUILTIN_E
10482 #undef RS6000_BUILTIN_H
10483 #undef RS6000_BUILTIN_P
10484 #undef RS6000_BUILTIN_Q
10485 #undef RS6000_BUILTIN_S
10486 #undef RS6000_BUILTIN_X
10488 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
10489 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
10490 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
10491 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
10492 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
10493 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
10494 #define RS6000_BUILTIN_H(ENUM, NAME, MASK, ATTR, ICODE) \
10495 { MASK, ICODE, NAME, ENUM },
10497 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
10498 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
10499 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
10500 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
10502 static const struct builtin_description bdesc_htm[] =
10504 #include "rs6000-builtin.def"
10507 #undef RS6000_BUILTIN_1
10508 #undef RS6000_BUILTIN_2
10509 #undef RS6000_BUILTIN_3
10510 #undef RS6000_BUILTIN_A
10511 #undef RS6000_BUILTIN_D
10512 #undef RS6000_BUILTIN_E
10513 #undef RS6000_BUILTIN_H
10514 #undef RS6000_BUILTIN_P
10515 #undef RS6000_BUILTIN_Q
10516 #undef RS6000_BUILTIN_S
10518 /* Return true if a builtin function is overloaded. */
10519 bool
10520 rs6000_overloaded_builtin_p (enum rs6000_builtins fncode)
10522 return (rs6000_builtin_info[(int)fncode].attr & RS6000_BTC_OVERLOADED) != 0;
10525 /* Expand an expression EXP that calls a builtin without arguments. */
10526 static rtx
10527 rs6000_expand_zeroop_builtin (enum insn_code icode, rtx target)
10529 rtx pat;
10530 enum machine_mode tmode = insn_data[icode].operand[0].mode;
10532 if (icode == CODE_FOR_nothing)
10533 /* Builtin not supported on this processor. */
10534 return 0;
10536 if (target == 0
10537 || GET_MODE (target) != tmode
10538 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
10539 target = gen_reg_rtx (tmode);
10541 pat = GEN_FCN (icode) (target);
10542 if (! pat)
10543 return 0;
10544 emit_insn (pat);
10546 return target;
10550 static rtx
10551 rs6000_expand_unop_builtin (enum insn_code icode, tree exp, rtx target)
10553 rtx pat;
10554 tree arg0 = CALL_EXPR_ARG (exp, 0);
10555 rtx op0 = expand_normal (arg0);
10556 enum machine_mode tmode = insn_data[icode].operand[0].mode;
10557 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
10559 if (icode == CODE_FOR_nothing)
10560 /* Builtin not supported on this processor. */
10561 return 0;
10563 /* If we got invalid arguments bail out before generating bad rtl. */
10564 if (arg0 == error_mark_node)
10565 return const0_rtx;
10567 if (icode == CODE_FOR_altivec_vspltisb
10568 || icode == CODE_FOR_altivec_vspltish
10569 || icode == CODE_FOR_altivec_vspltisw
10570 || icode == CODE_FOR_spe_evsplatfi
10571 || icode == CODE_FOR_spe_evsplati)
10573 /* Only allow 5-bit *signed* literals. */
10574 if (GET_CODE (op0) != CONST_INT
10575 || INTVAL (op0) > 15
10576 || INTVAL (op0) < -16)
10578 error ("argument 1 must be a 5-bit signed literal");
10579 return const0_rtx;
10583 if (target == 0
10584 || GET_MODE (target) != tmode
10585 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
10586 target = gen_reg_rtx (tmode);
10588 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
10589 op0 = copy_to_mode_reg (mode0, op0);
10591 pat = GEN_FCN (icode) (target, op0);
10592 if (! pat)
10593 return 0;
10594 emit_insn (pat);
10596 return target;
10599 static rtx
10600 altivec_expand_abs_builtin (enum insn_code icode, tree exp, rtx target)
10602 rtx pat, scratch1, scratch2;
10603 tree arg0 = CALL_EXPR_ARG (exp, 0);
10604 rtx op0 = expand_normal (arg0);
10605 enum machine_mode tmode = insn_data[icode].operand[0].mode;
10606 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
10608 /* If we have invalid arguments, bail out before generating bad rtl. */
10609 if (arg0 == error_mark_node)
10610 return const0_rtx;
10612 if (target == 0
10613 || GET_MODE (target) != tmode
10614 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
10615 target = gen_reg_rtx (tmode);
10617 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
10618 op0 = copy_to_mode_reg (mode0, op0);
10620 scratch1 = gen_reg_rtx (mode0);
10621 scratch2 = gen_reg_rtx (mode0);
10623 pat = GEN_FCN (icode) (target, op0, scratch1, scratch2);
10624 if (! pat)
10625 return 0;
10626 emit_insn (pat);
10628 return target;
10631 static rtx
10632 rs6000_expand_binop_builtin (enum insn_code icode, tree exp, rtx target)
10634 rtx pat;
10635 tree arg0 = CALL_EXPR_ARG (exp, 0);
10636 tree arg1 = CALL_EXPR_ARG (exp, 1);
10637 rtx op0 = expand_normal (arg0);
10638 rtx op1 = expand_normal (arg1);
10639 enum machine_mode tmode = insn_data[icode].operand[0].mode;
10640 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
10641 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
10643 if (icode == CODE_FOR_nothing)
10644 /* Builtin not supported on this processor. */
10645 return 0;
10647 /* If we got invalid arguments bail out before generating bad rtl. */
10648 if (arg0 == error_mark_node || arg1 == error_mark_node)
10649 return const0_rtx;
10651 if (icode == CODE_FOR_altivec_vcfux
10652 || icode == CODE_FOR_altivec_vcfsx
10653 || icode == CODE_FOR_altivec_vctsxs
10654 || icode == CODE_FOR_altivec_vctuxs
10655 || icode == CODE_FOR_altivec_vspltb
10656 || icode == CODE_FOR_altivec_vsplth
10657 || icode == CODE_FOR_altivec_vspltw
10658 || icode == CODE_FOR_spe_evaddiw
10659 || icode == CODE_FOR_spe_evldd
10660 || icode == CODE_FOR_spe_evldh
10661 || icode == CODE_FOR_spe_evldw
10662 || icode == CODE_FOR_spe_evlhhesplat
10663 || icode == CODE_FOR_spe_evlhhossplat
10664 || icode == CODE_FOR_spe_evlhhousplat
10665 || icode == CODE_FOR_spe_evlwhe
10666 || icode == CODE_FOR_spe_evlwhos
10667 || icode == CODE_FOR_spe_evlwhou
10668 || icode == CODE_FOR_spe_evlwhsplat
10669 || icode == CODE_FOR_spe_evlwwsplat
10670 || icode == CODE_FOR_spe_evrlwi
10671 || icode == CODE_FOR_spe_evslwi
10672 || icode == CODE_FOR_spe_evsrwis
10673 || icode == CODE_FOR_spe_evsubifw
10674 || icode == CODE_FOR_spe_evsrwiu)
10676 /* Only allow 5-bit unsigned literals. */
10677 STRIP_NOPS (arg1);
10678 if (TREE_CODE (arg1) != INTEGER_CST
10679 || TREE_INT_CST_LOW (arg1) & ~0x1f)
10681 error ("argument 2 must be a 5-bit unsigned literal");
10682 return const0_rtx;
10686 if (target == 0
10687 || GET_MODE (target) != tmode
10688 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
10689 target = gen_reg_rtx (tmode);
10691 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
10692 op0 = copy_to_mode_reg (mode0, op0);
10693 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
10694 op1 = copy_to_mode_reg (mode1, op1);
10696 pat = GEN_FCN (icode) (target, op0, op1);
10697 if (! pat)
10698 return 0;
10699 emit_insn (pat);
10701 return target;
10704 static rtx
10705 altivec_expand_predicate_builtin (enum insn_code icode, tree exp, rtx target)
10707 rtx pat, scratch;
10708 tree cr6_form = CALL_EXPR_ARG (exp, 0);
10709 tree arg0 = CALL_EXPR_ARG (exp, 1);
10710 tree arg1 = CALL_EXPR_ARG (exp, 2);
10711 rtx op0 = expand_normal (arg0);
10712 rtx op1 = expand_normal (arg1);
10713 enum machine_mode tmode = SImode;
10714 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
10715 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
10716 int cr6_form_int;
10718 if (TREE_CODE (cr6_form) != INTEGER_CST)
10720 error ("argument 1 of __builtin_altivec_predicate must be a constant");
10721 return const0_rtx;
10723 else
10724 cr6_form_int = TREE_INT_CST_LOW (cr6_form);
10726 gcc_assert (mode0 == mode1);
10728 /* If we have invalid arguments, bail out before generating bad rtl. */
10729 if (arg0 == error_mark_node || arg1 == error_mark_node)
10730 return const0_rtx;
10732 if (target == 0
10733 || GET_MODE (target) != tmode
10734 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
10735 target = gen_reg_rtx (tmode);
10737 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
10738 op0 = copy_to_mode_reg (mode0, op0);
10739 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
10740 op1 = copy_to_mode_reg (mode1, op1);
10742 scratch = gen_reg_rtx (mode0);
10744 pat = GEN_FCN (icode) (scratch, op0, op1);
10745 if (! pat)
10746 return 0;
10747 emit_insn (pat);
10749 /* The vec_any* and vec_all* predicates use the same opcodes for two
10750 different operations, but the bits in CR6 will be different
10751 depending on what information we want. So we have to play tricks
10752 with CR6 to get the right bits out.
10754 If you think this is disgusting, look at the specs for the
10755 AltiVec predicates. */
10757 switch (cr6_form_int)
10759 case 0:
10760 emit_insn (gen_cr6_test_for_zero (target));
10761 break;
10762 case 1:
10763 emit_insn (gen_cr6_test_for_zero_reverse (target));
10764 break;
10765 case 2:
10766 emit_insn (gen_cr6_test_for_lt (target));
10767 break;
10768 case 3:
10769 emit_insn (gen_cr6_test_for_lt_reverse (target));
10770 break;
10771 default:
10772 error ("argument 1 of __builtin_altivec_predicate is out of range");
10773 break;
10776 return target;
10779 static rtx
10780 paired_expand_lv_builtin (enum insn_code icode, tree exp, rtx target)
10782 rtx pat, addr;
10783 tree arg0 = CALL_EXPR_ARG (exp, 0);
10784 tree arg1 = CALL_EXPR_ARG (exp, 1);
10785 enum machine_mode tmode = insn_data[icode].operand[0].mode;
10786 enum machine_mode mode0 = Pmode;
10787 enum machine_mode mode1 = Pmode;
10788 rtx op0 = expand_normal (arg0);
10789 rtx op1 = expand_normal (arg1);
10791 if (icode == CODE_FOR_nothing)
10792 /* Builtin not supported on this processor. */
10793 return 0;
10795 /* If we got invalid arguments bail out before generating bad rtl. */
10796 if (arg0 == error_mark_node || arg1 == error_mark_node)
10797 return const0_rtx;
10799 if (target == 0
10800 || GET_MODE (target) != tmode
10801 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
10802 target = gen_reg_rtx (tmode);
10804 op1 = copy_to_mode_reg (mode1, op1);
10806 if (op0 == const0_rtx)
10808 addr = gen_rtx_MEM (tmode, op1);
10810 else
10812 op0 = copy_to_mode_reg (mode0, op0);
10813 addr = gen_rtx_MEM (tmode, gen_rtx_PLUS (Pmode, op0, op1));
10816 pat = GEN_FCN (icode) (target, addr);
10818 if (! pat)
10819 return 0;
10820 emit_insn (pat);
10822 return target;
10825 static rtx
10826 altivec_expand_lv_builtin (enum insn_code icode, tree exp, rtx target, bool blk)
10828 rtx pat, addr;
10829 tree arg0 = CALL_EXPR_ARG (exp, 0);
10830 tree arg1 = CALL_EXPR_ARG (exp, 1);
10831 enum machine_mode tmode = insn_data[icode].operand[0].mode;
10832 enum machine_mode mode0 = Pmode;
10833 enum machine_mode mode1 = Pmode;
10834 rtx op0 = expand_normal (arg0);
10835 rtx op1 = expand_normal (arg1);
10837 if (icode == CODE_FOR_nothing)
10838 /* Builtin not supported on this processor. */
10839 return 0;
10841 /* If we got invalid arguments bail out before generating bad rtl. */
10842 if (arg0 == error_mark_node || arg1 == error_mark_node)
10843 return const0_rtx;
10845 if (target == 0
10846 || GET_MODE (target) != tmode
10847 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
10848 target = gen_reg_rtx (tmode);
10850 op1 = copy_to_mode_reg (mode1, op1);
10852 if (op0 == const0_rtx)
10854 addr = gen_rtx_MEM (blk ? BLKmode : tmode, op1);
10856 else
10858 op0 = copy_to_mode_reg (mode0, op0);
10859 addr = gen_rtx_MEM (blk ? BLKmode : tmode, gen_rtx_PLUS (Pmode, op0, op1));
10862 pat = GEN_FCN (icode) (target, addr);
10864 if (! pat)
10865 return 0;
10866 emit_insn (pat);
10868 return target;
10871 static rtx
10872 spe_expand_stv_builtin (enum insn_code icode, tree exp)
10874 tree arg0 = CALL_EXPR_ARG (exp, 0);
10875 tree arg1 = CALL_EXPR_ARG (exp, 1);
10876 tree arg2 = CALL_EXPR_ARG (exp, 2);
10877 rtx op0 = expand_normal (arg0);
10878 rtx op1 = expand_normal (arg1);
10879 rtx op2 = expand_normal (arg2);
10880 rtx pat;
10881 enum machine_mode mode0 = insn_data[icode].operand[0].mode;
10882 enum machine_mode mode1 = insn_data[icode].operand[1].mode;
10883 enum machine_mode mode2 = insn_data[icode].operand[2].mode;
10885 /* Invalid arguments. Bail before doing anything stoopid! */
10886 if (arg0 == error_mark_node
10887 || arg1 == error_mark_node
10888 || arg2 == error_mark_node)
10889 return const0_rtx;
10891 if (! (*insn_data[icode].operand[2].predicate) (op0, mode2))
10892 op0 = copy_to_mode_reg (mode2, op0);
10893 if (! (*insn_data[icode].operand[0].predicate) (op1, mode0))
10894 op1 = copy_to_mode_reg (mode0, op1);
10895 if (! (*insn_data[icode].operand[1].predicate) (op2, mode1))
10896 op2 = copy_to_mode_reg (mode1, op2);
10898 pat = GEN_FCN (icode) (op1, op2, op0);
10899 if (pat)
10900 emit_insn (pat);
10901 return NULL_RTX;
10904 static rtx
10905 paired_expand_stv_builtin (enum insn_code icode, tree exp)
10907 tree arg0 = CALL_EXPR_ARG (exp, 0);
10908 tree arg1 = CALL_EXPR_ARG (exp, 1);
10909 tree arg2 = CALL_EXPR_ARG (exp, 2);
10910 rtx op0 = expand_normal (arg0);
10911 rtx op1 = expand_normal (arg1);
10912 rtx op2 = expand_normal (arg2);
10913 rtx pat, addr;
10914 enum machine_mode tmode = insn_data[icode].operand[0].mode;
10915 enum machine_mode mode1 = Pmode;
10916 enum machine_mode mode2 = Pmode;
10918 /* Invalid arguments. Bail before doing anything stoopid! */
10919 if (arg0 == error_mark_node
10920 || arg1 == error_mark_node
10921 || arg2 == error_mark_node)
10922 return const0_rtx;
10924 if (! (*insn_data[icode].operand[1].predicate) (op0, tmode))
10925 op0 = copy_to_mode_reg (tmode, op0);
10927 op2 = copy_to_mode_reg (mode2, op2);
10929 if (op1 == const0_rtx)
10931 addr = gen_rtx_MEM (tmode, op2);
10933 else
10935 op1 = copy_to_mode_reg (mode1, op1);
10936 addr = gen_rtx_MEM (tmode, gen_rtx_PLUS (Pmode, op1, op2));
10939 pat = GEN_FCN (icode) (addr, op0);
10940 if (pat)
10941 emit_insn (pat);
10942 return NULL_RTX;
10945 static rtx
10946 altivec_expand_stv_builtin (enum insn_code icode, tree exp)
10948 tree arg0 = CALL_EXPR_ARG (exp, 0);
10949 tree arg1 = CALL_EXPR_ARG (exp, 1);
10950 tree arg2 = CALL_EXPR_ARG (exp, 2);
10951 rtx op0 = expand_normal (arg0);
10952 rtx op1 = expand_normal (arg1);
10953 rtx op2 = expand_normal (arg2);
10954 rtx pat, addr;
10955 enum machine_mode tmode = insn_data[icode].operand[0].mode;
10956 enum machine_mode smode = insn_data[icode].operand[1].mode;
10957 enum machine_mode mode1 = Pmode;
10958 enum machine_mode mode2 = Pmode;
10960 /* Invalid arguments. Bail before doing anything stoopid! */
10961 if (arg0 == error_mark_node
10962 || arg1 == error_mark_node
10963 || arg2 == error_mark_node)
10964 return const0_rtx;
10966 if (! (*insn_data[icode].operand[1].predicate) (op0, smode))
10967 op0 = copy_to_mode_reg (smode, op0);
10969 op2 = copy_to_mode_reg (mode2, op2);
10971 if (op1 == const0_rtx)
10973 addr = gen_rtx_MEM (tmode, op2);
10975 else
10977 op1 = copy_to_mode_reg (mode1, op1);
10978 addr = gen_rtx_MEM (tmode, gen_rtx_PLUS (Pmode, op1, op2));
10981 pat = GEN_FCN (icode) (addr, op0);
10982 if (pat)
10983 emit_insn (pat);
10984 return NULL_RTX;
10987 /* Return the appropriate SPR number associated with the given builtin. */
10988 static inline HOST_WIDE_INT
10989 htm_spr_num (enum rs6000_builtins code)
10991 if (code == HTM_BUILTIN_GET_TFHAR
10992 || code == HTM_BUILTIN_SET_TFHAR)
10993 return TFHAR_SPR;
10994 else if (code == HTM_BUILTIN_GET_TFIAR
10995 || code == HTM_BUILTIN_SET_TFIAR)
10996 return TFIAR_SPR;
10997 else if (code == HTM_BUILTIN_GET_TEXASR
10998 || code == HTM_BUILTIN_SET_TEXASR)
10999 return TEXASR_SPR;
11000 gcc_assert (code == HTM_BUILTIN_GET_TEXASRU
11001 || code == HTM_BUILTIN_SET_TEXASRU);
11002 return TEXASRU_SPR;
11005 /* Return the appropriate SPR regno associated with the given builtin. */
11006 static inline HOST_WIDE_INT
11007 htm_spr_regno (enum rs6000_builtins code)
11009 if (code == HTM_BUILTIN_GET_TFHAR
11010 || code == HTM_BUILTIN_SET_TFHAR)
11011 return TFHAR_REGNO;
11012 else if (code == HTM_BUILTIN_GET_TFIAR
11013 || code == HTM_BUILTIN_SET_TFIAR)
11014 return TFIAR_REGNO;
11015 gcc_assert (code == HTM_BUILTIN_GET_TEXASR
11016 || code == HTM_BUILTIN_SET_TEXASR
11017 || code == HTM_BUILTIN_GET_TEXASRU
11018 || code == HTM_BUILTIN_SET_TEXASRU);
11019 return TEXASR_REGNO;
11022 /* Return the correct ICODE value depending on whether we are
11023 setting or reading the HTM SPRs. */
11024 static inline enum insn_code
11025 rs6000_htm_spr_icode (bool nonvoid)
11027 if (nonvoid)
11028 return (TARGET_64BIT) ? CODE_FOR_htm_mfspr_di : CODE_FOR_htm_mfspr_si;
11029 else
11030 return (TARGET_64BIT) ? CODE_FOR_htm_mtspr_di : CODE_FOR_htm_mtspr_si;
11033 /* Expand the HTM builtin in EXP and store the result in TARGET.
11034 Store true in *EXPANDEDP if we found a builtin to expand. */
11035 static rtx
11036 htm_expand_builtin (tree exp, rtx target, bool * expandedp)
11038 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
11039 bool nonvoid = TREE_TYPE (TREE_TYPE (fndecl)) != void_type_node;
11040 enum rs6000_builtins fcode = (enum rs6000_builtins) DECL_FUNCTION_CODE (fndecl);
11041 const struct builtin_description *d;
11042 size_t i;
11044 *expandedp = false;
11046 /* Expand the HTM builtins. */
11047 d = bdesc_htm;
11048 for (i = 0; i < ARRAY_SIZE (bdesc_htm); i++, d++)
11049 if (d->code == fcode)
11051 rtx op[MAX_HTM_OPERANDS], pat;
11052 int nopnds = 0;
11053 tree arg;
11054 call_expr_arg_iterator iter;
11055 unsigned attr = rs6000_builtin_info[fcode].attr;
11056 enum insn_code icode = d->icode;
11058 if (attr & RS6000_BTC_SPR)
11059 icode = rs6000_htm_spr_icode (nonvoid);
11061 if (nonvoid)
11063 enum machine_mode tmode = insn_data[icode].operand[0].mode;
11064 if (!target
11065 || GET_MODE (target) != tmode
11066 || !(*insn_data[icode].operand[0].predicate) (target, tmode))
11067 target = gen_reg_rtx (tmode);
11068 op[nopnds++] = target;
11071 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
11073 const struct insn_operand_data *insn_op;
11075 if (arg == error_mark_node || nopnds >= MAX_HTM_OPERANDS)
11076 return NULL_RTX;
11078 insn_op = &insn_data[icode].operand[nopnds];
11080 op[nopnds] = expand_normal (arg);
11082 if (!(*insn_op->predicate) (op[nopnds], insn_op->mode))
11084 if (!strcmp (insn_op->constraint, "n"))
11086 int arg_num = (nonvoid) ? nopnds : nopnds + 1;
11087 if (!CONST_INT_P (op[nopnds]))
11088 error ("argument %d must be an unsigned literal", arg_num);
11089 else
11090 error ("argument %d is an unsigned literal that is "
11091 "out of range", arg_num);
11092 return const0_rtx;
11094 op[nopnds] = copy_to_mode_reg (insn_op->mode, op[nopnds]);
11097 nopnds++;
11100 /* Handle the builtins for extended mnemonics. These accept
11101 no arguments, but map to builtins that take arguments. */
11102 switch (fcode)
11104 case HTM_BUILTIN_TENDALL: /* Alias for: tend. 1 */
11105 case HTM_BUILTIN_TRESUME: /* Alias for: tsr. 1 */
11106 op[nopnds++] = GEN_INT (1);
11107 #ifdef ENABLE_CHECKING
11108 attr |= RS6000_BTC_UNARY;
11109 #endif
11110 break;
11111 case HTM_BUILTIN_TSUSPEND: /* Alias for: tsr. 0 */
11112 op[nopnds++] = GEN_INT (0);
11113 #ifdef ENABLE_CHECKING
11114 attr |= RS6000_BTC_UNARY;
11115 #endif
11116 break;
11117 default:
11118 break;
11121 /* If this builtin accesses SPRs, then pass in the appropriate
11122 SPR number and SPR regno as the last two operands. */
11123 if (attr & RS6000_BTC_SPR)
11125 op[nopnds++] = gen_rtx_CONST_INT (Pmode, htm_spr_num (fcode));
11126 op[nopnds++] = gen_rtx_REG (Pmode, htm_spr_regno (fcode));
11129 #ifdef ENABLE_CHECKING
11130 int expected_nopnds = 0;
11131 if ((attr & RS6000_BTC_TYPE_MASK) == RS6000_BTC_UNARY)
11132 expected_nopnds = 1;
11133 else if ((attr & RS6000_BTC_TYPE_MASK) == RS6000_BTC_BINARY)
11134 expected_nopnds = 2;
11135 else if ((attr & RS6000_BTC_TYPE_MASK) == RS6000_BTC_TERNARY)
11136 expected_nopnds = 3;
11137 if (!(attr & RS6000_BTC_VOID))
11138 expected_nopnds += 1;
11139 if (attr & RS6000_BTC_SPR)
11140 expected_nopnds += 2;
11142 gcc_assert (nopnds == expected_nopnds && nopnds <= MAX_HTM_OPERANDS);
11143 #endif
11145 switch (nopnds)
11147 case 1:
11148 pat = GEN_FCN (icode) (op[0]);
11149 break;
11150 case 2:
11151 pat = GEN_FCN (icode) (op[0], op[1]);
11152 break;
11153 case 3:
11154 pat = GEN_FCN (icode) (op[0], op[1], op[2]);
11155 break;
11156 case 4:
11157 pat = GEN_FCN (icode) (op[0], op[1], op[2], op[3]);
11158 break;
11159 default:
11160 gcc_unreachable ();
11162 if (!pat)
11163 return NULL_RTX;
11164 emit_insn (pat);
11166 *expandedp = true;
11167 if (nonvoid)
11168 return target;
11169 return const0_rtx;
11172 return NULL_RTX;
11175 static rtx
11176 rs6000_expand_ternop_builtin (enum insn_code icode, tree exp, rtx target)
11178 rtx pat;
11179 tree arg0 = CALL_EXPR_ARG (exp, 0);
11180 tree arg1 = CALL_EXPR_ARG (exp, 1);
11181 tree arg2 = CALL_EXPR_ARG (exp, 2);
11182 rtx op0 = expand_normal (arg0);
11183 rtx op1 = expand_normal (arg1);
11184 rtx op2 = expand_normal (arg2);
11185 enum machine_mode tmode = insn_data[icode].operand[0].mode;
11186 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
11187 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
11188 enum machine_mode mode2 = insn_data[icode].operand[3].mode;
11190 if (icode == CODE_FOR_nothing)
11191 /* Builtin not supported on this processor. */
11192 return 0;
11194 /* If we got invalid arguments bail out before generating bad rtl. */
11195 if (arg0 == error_mark_node
11196 || arg1 == error_mark_node
11197 || arg2 == error_mark_node)
11198 return const0_rtx;
11200 /* Check and prepare argument depending on the instruction code.
11202 Note that a switch statement instead of the sequence of tests
11203 would be incorrect as many of the CODE_FOR values could be
11204 CODE_FOR_nothing and that would yield multiple alternatives
11205 with identical values. We'd never reach here at runtime in
11206 this case. */
11207 if (icode == CODE_FOR_altivec_vsldoi_v4sf
11208 || icode == CODE_FOR_altivec_vsldoi_v4si
11209 || icode == CODE_FOR_altivec_vsldoi_v8hi
11210 || icode == CODE_FOR_altivec_vsldoi_v16qi)
11212 /* Only allow 4-bit unsigned literals. */
11213 STRIP_NOPS (arg2);
11214 if (TREE_CODE (arg2) != INTEGER_CST
11215 || TREE_INT_CST_LOW (arg2) & ~0xf)
11217 error ("argument 3 must be a 4-bit unsigned literal");
11218 return const0_rtx;
11221 else if (icode == CODE_FOR_vsx_xxpermdi_v2df
11222 || icode == CODE_FOR_vsx_xxpermdi_v2di
11223 || icode == CODE_FOR_vsx_xxsldwi_v16qi
11224 || icode == CODE_FOR_vsx_xxsldwi_v8hi
11225 || icode == CODE_FOR_vsx_xxsldwi_v4si
11226 || icode == CODE_FOR_vsx_xxsldwi_v4sf
11227 || icode == CODE_FOR_vsx_xxsldwi_v2di
11228 || icode == CODE_FOR_vsx_xxsldwi_v2df)
11230 /* Only allow 2-bit unsigned literals. */
11231 STRIP_NOPS (arg2);
11232 if (TREE_CODE (arg2) != INTEGER_CST
11233 || TREE_INT_CST_LOW (arg2) & ~0x3)
11235 error ("argument 3 must be a 2-bit unsigned literal");
11236 return const0_rtx;
11239 else if (icode == CODE_FOR_vsx_set_v2df
11240 || icode == CODE_FOR_vsx_set_v2di)
11242 /* Only allow 1-bit unsigned literals. */
11243 STRIP_NOPS (arg2);
11244 if (TREE_CODE (arg2) != INTEGER_CST
11245 || TREE_INT_CST_LOW (arg2) & ~0x1)
11247 error ("argument 3 must be a 1-bit unsigned literal");
11248 return const0_rtx;
11251 else if (icode == CODE_FOR_crypto_vshasigmaw
11252 || icode == CODE_FOR_crypto_vshasigmad)
11254 /* Check whether the 2nd and 3rd arguments are integer constants and in
11255 range and prepare arguments. */
11256 STRIP_NOPS (arg1);
11257 if (TREE_CODE (arg1) != INTEGER_CST
11258 || !IN_RANGE (TREE_INT_CST_LOW (arg1), 0, 1))
11260 error ("argument 2 must be 0 or 1");
11261 return const0_rtx;
11264 STRIP_NOPS (arg2);
11265 if (TREE_CODE (arg2) != INTEGER_CST
11266 || !IN_RANGE (TREE_INT_CST_LOW (arg2), 0, 15))
11268 error ("argument 3 must be in the range 0..15");
11269 return const0_rtx;
11273 if (target == 0
11274 || GET_MODE (target) != tmode
11275 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
11276 target = gen_reg_rtx (tmode);
11278 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
11279 op0 = copy_to_mode_reg (mode0, op0);
11280 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
11281 op1 = copy_to_mode_reg (mode1, op1);
11282 if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
11283 op2 = copy_to_mode_reg (mode2, op2);
11285 if (TARGET_PAIRED_FLOAT && icode == CODE_FOR_selv2sf4)
11286 pat = GEN_FCN (icode) (target, op0, op1, op2, CONST0_RTX (SFmode));
11287 else
11288 pat = GEN_FCN (icode) (target, op0, op1, op2);
11289 if (! pat)
11290 return 0;
11291 emit_insn (pat);
11293 return target;
11296 /* Expand the lvx builtins. */
11297 static rtx
11298 altivec_expand_ld_builtin (tree exp, rtx target, bool *expandedp)
11300 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
11301 unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
11302 tree arg0;
11303 enum machine_mode tmode, mode0;
11304 rtx pat, op0;
11305 enum insn_code icode;
11307 switch (fcode)
11309 case ALTIVEC_BUILTIN_LD_INTERNAL_16qi:
11310 icode = CODE_FOR_vector_altivec_load_v16qi;
11311 break;
11312 case ALTIVEC_BUILTIN_LD_INTERNAL_8hi:
11313 icode = CODE_FOR_vector_altivec_load_v8hi;
11314 break;
11315 case ALTIVEC_BUILTIN_LD_INTERNAL_4si:
11316 icode = CODE_FOR_vector_altivec_load_v4si;
11317 break;
11318 case ALTIVEC_BUILTIN_LD_INTERNAL_4sf:
11319 icode = CODE_FOR_vector_altivec_load_v4sf;
11320 break;
11321 case ALTIVEC_BUILTIN_LD_INTERNAL_2df:
11322 icode = CODE_FOR_vector_altivec_load_v2df;
11323 break;
11324 case ALTIVEC_BUILTIN_LD_INTERNAL_2di:
11325 icode = CODE_FOR_vector_altivec_load_v2di;
11326 break;
11327 default:
11328 *expandedp = false;
11329 return NULL_RTX;
11332 *expandedp = true;
11334 arg0 = CALL_EXPR_ARG (exp, 0);
11335 op0 = expand_normal (arg0);
11336 tmode = insn_data[icode].operand[0].mode;
11337 mode0 = insn_data[icode].operand[1].mode;
11339 if (target == 0
11340 || GET_MODE (target) != tmode
11341 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
11342 target = gen_reg_rtx (tmode);
11344 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
11345 op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
11347 pat = GEN_FCN (icode) (target, op0);
11348 if (! pat)
11349 return 0;
11350 emit_insn (pat);
11351 return target;
11354 /* Expand the stvx builtins. */
11355 static rtx
11356 altivec_expand_st_builtin (tree exp, rtx target ATTRIBUTE_UNUSED,
11357 bool *expandedp)
11359 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
11360 unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
11361 tree arg0, arg1;
11362 enum machine_mode mode0, mode1;
11363 rtx pat, op0, op1;
11364 enum insn_code icode;
11366 switch (fcode)
11368 case ALTIVEC_BUILTIN_ST_INTERNAL_16qi:
11369 icode = CODE_FOR_vector_altivec_store_v16qi;
11370 break;
11371 case ALTIVEC_BUILTIN_ST_INTERNAL_8hi:
11372 icode = CODE_FOR_vector_altivec_store_v8hi;
11373 break;
11374 case ALTIVEC_BUILTIN_ST_INTERNAL_4si:
11375 icode = CODE_FOR_vector_altivec_store_v4si;
11376 break;
11377 case ALTIVEC_BUILTIN_ST_INTERNAL_4sf:
11378 icode = CODE_FOR_vector_altivec_store_v4sf;
11379 break;
11380 case ALTIVEC_BUILTIN_ST_INTERNAL_2df:
11381 icode = CODE_FOR_vector_altivec_store_v2df;
11382 break;
11383 case ALTIVEC_BUILTIN_ST_INTERNAL_2di:
11384 icode = CODE_FOR_vector_altivec_store_v2di;
11385 break;
11386 default:
11387 *expandedp = false;
11388 return NULL_RTX;
11391 arg0 = CALL_EXPR_ARG (exp, 0);
11392 arg1 = CALL_EXPR_ARG (exp, 1);
11393 op0 = expand_normal (arg0);
11394 op1 = expand_normal (arg1);
11395 mode0 = insn_data[icode].operand[0].mode;
11396 mode1 = insn_data[icode].operand[1].mode;
11398 if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
11399 op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
11400 if (! (*insn_data[icode].operand[1].predicate) (op1, mode1))
11401 op1 = copy_to_mode_reg (mode1, op1);
11403 pat = GEN_FCN (icode) (op0, op1);
11404 if (pat)
11405 emit_insn (pat);
11407 *expandedp = true;
11408 return NULL_RTX;
11411 /* Expand the dst builtins. */
11412 static rtx
11413 altivec_expand_dst_builtin (tree exp, rtx target ATTRIBUTE_UNUSED,
11414 bool *expandedp)
11416 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
11417 enum rs6000_builtins fcode = (enum rs6000_builtins) DECL_FUNCTION_CODE (fndecl);
11418 tree arg0, arg1, arg2;
11419 enum machine_mode mode0, mode1;
11420 rtx pat, op0, op1, op2;
11421 const struct builtin_description *d;
11422 size_t i;
11424 *expandedp = false;
11426 /* Handle DST variants. */
11427 d = bdesc_dst;
11428 for (i = 0; i < ARRAY_SIZE (bdesc_dst); i++, d++)
11429 if (d->code == fcode)
11431 arg0 = CALL_EXPR_ARG (exp, 0);
11432 arg1 = CALL_EXPR_ARG (exp, 1);
11433 arg2 = CALL_EXPR_ARG (exp, 2);
11434 op0 = expand_normal (arg0);
11435 op1 = expand_normal (arg1);
11436 op2 = expand_normal (arg2);
11437 mode0 = insn_data[d->icode].operand[0].mode;
11438 mode1 = insn_data[d->icode].operand[1].mode;
11440 /* Invalid arguments, bail out before generating bad rtl. */
11441 if (arg0 == error_mark_node
11442 || arg1 == error_mark_node
11443 || arg2 == error_mark_node)
11444 return const0_rtx;
11446 *expandedp = true;
11447 STRIP_NOPS (arg2);
11448 if (TREE_CODE (arg2) != INTEGER_CST
11449 || TREE_INT_CST_LOW (arg2) & ~0x3)
11451 error ("argument to %qs must be a 2-bit unsigned literal", d->name);
11452 return const0_rtx;
11455 if (! (*insn_data[d->icode].operand[0].predicate) (op0, mode0))
11456 op0 = copy_to_mode_reg (Pmode, op0);
11457 if (! (*insn_data[d->icode].operand[1].predicate) (op1, mode1))
11458 op1 = copy_to_mode_reg (mode1, op1);
11460 pat = GEN_FCN (d->icode) (op0, op1, op2);
11461 if (pat != 0)
11462 emit_insn (pat);
11464 return NULL_RTX;
11467 return NULL_RTX;
11470 /* Expand vec_init builtin. */
11471 static rtx
11472 altivec_expand_vec_init_builtin (tree type, tree exp, rtx target)
11474 enum machine_mode tmode = TYPE_MODE (type);
11475 enum machine_mode inner_mode = GET_MODE_INNER (tmode);
11476 int i, n_elt = GET_MODE_NUNITS (tmode);
11477 rtvec v = rtvec_alloc (n_elt);
11479 gcc_assert (VECTOR_MODE_P (tmode));
11480 gcc_assert (n_elt == call_expr_nargs (exp));
11482 for (i = 0; i < n_elt; ++i)
11484 rtx x = expand_normal (CALL_EXPR_ARG (exp, i));
11485 RTVEC_ELT (v, i) = gen_lowpart (inner_mode, x);
11488 if (!target || !register_operand (target, tmode))
11489 target = gen_reg_rtx (tmode);
11491 rs6000_expand_vector_init (target, gen_rtx_PARALLEL (tmode, v));
11492 return target;
11495 /* Return the integer constant in ARG. Constrain it to be in the range
11496 of the subparts of VEC_TYPE; issue an error if not. */
11498 static int
11499 get_element_number (tree vec_type, tree arg)
11501 unsigned HOST_WIDE_INT elt, max = TYPE_VECTOR_SUBPARTS (vec_type) - 1;
11503 if (!host_integerp (arg, 1)
11504 || (elt = tree_low_cst (arg, 1), elt > max))
11506 error ("selector must be an integer constant in the range 0..%wi", max);
11507 return 0;
11510 return elt;
11513 /* Expand vec_set builtin. */
11514 static rtx
11515 altivec_expand_vec_set_builtin (tree exp)
11517 enum machine_mode tmode, mode1;
11518 tree arg0, arg1, arg2;
11519 int elt;
11520 rtx op0, op1;
11522 arg0 = CALL_EXPR_ARG (exp, 0);
11523 arg1 = CALL_EXPR_ARG (exp, 1);
11524 arg2 = CALL_EXPR_ARG (exp, 2);
11526 tmode = TYPE_MODE (TREE_TYPE (arg0));
11527 mode1 = TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0)));
11528 gcc_assert (VECTOR_MODE_P (tmode));
11530 op0 = expand_expr (arg0, NULL_RTX, tmode, EXPAND_NORMAL);
11531 op1 = expand_expr (arg1, NULL_RTX, mode1, EXPAND_NORMAL);
11532 elt = get_element_number (TREE_TYPE (arg0), arg2);
11534 if (GET_MODE (op1) != mode1 && GET_MODE (op1) != VOIDmode)
11535 op1 = convert_modes (mode1, GET_MODE (op1), op1, true);
11537 op0 = force_reg (tmode, op0);
11538 op1 = force_reg (mode1, op1);
11540 rs6000_expand_vector_set (op0, op1, elt);
11542 return op0;
11545 /* Expand vec_ext builtin. */
11546 static rtx
11547 altivec_expand_vec_ext_builtin (tree exp, rtx target)
11549 enum machine_mode tmode, mode0;
11550 tree arg0, arg1;
11551 int elt;
11552 rtx op0;
11554 arg0 = CALL_EXPR_ARG (exp, 0);
11555 arg1 = CALL_EXPR_ARG (exp, 1);
11557 op0 = expand_normal (arg0);
11558 elt = get_element_number (TREE_TYPE (arg0), arg1);
11560 tmode = TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0)));
11561 mode0 = TYPE_MODE (TREE_TYPE (arg0));
11562 gcc_assert (VECTOR_MODE_P (mode0));
11564 op0 = force_reg (mode0, op0);
11566 if (optimize || !target || !register_operand (target, tmode))
11567 target = gen_reg_rtx (tmode);
11569 rs6000_expand_vector_extract (target, op0, elt);
11571 return target;
11574 /* Expand the builtin in EXP and store the result in TARGET. Store
11575 true in *EXPANDEDP if we found a builtin to expand. */
11576 static rtx
11577 altivec_expand_builtin (tree exp, rtx target, bool *expandedp)
11579 const struct builtin_description *d;
11580 size_t i;
11581 enum insn_code icode;
11582 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
11583 tree arg0;
11584 rtx op0, pat;
11585 enum machine_mode tmode, mode0;
11586 enum rs6000_builtins fcode
11587 = (enum rs6000_builtins) DECL_FUNCTION_CODE (fndecl);
11589 if (rs6000_overloaded_builtin_p (fcode))
11591 *expandedp = true;
11592 error ("unresolved overload for Altivec builtin %qF", fndecl);
11594 /* Given it is invalid, just generate a normal call. */
11595 return expand_call (exp, target, false);
11598 target = altivec_expand_ld_builtin (exp, target, expandedp);
11599 if (*expandedp)
11600 return target;
11602 target = altivec_expand_st_builtin (exp, target, expandedp);
11603 if (*expandedp)
11604 return target;
11606 target = altivec_expand_dst_builtin (exp, target, expandedp);
11607 if (*expandedp)
11608 return target;
11610 *expandedp = true;
11612 switch (fcode)
11614 case ALTIVEC_BUILTIN_STVX:
11615 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvx_v4si, exp);
11616 case ALTIVEC_BUILTIN_STVEBX:
11617 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvebx, exp);
11618 case ALTIVEC_BUILTIN_STVEHX:
11619 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvehx, exp);
11620 case ALTIVEC_BUILTIN_STVEWX:
11621 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvewx, exp);
11622 case ALTIVEC_BUILTIN_STVXL:
11623 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvxl, exp);
11625 case ALTIVEC_BUILTIN_STVLX:
11626 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvlx, exp);
11627 case ALTIVEC_BUILTIN_STVLXL:
11628 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvlxl, exp);
11629 case ALTIVEC_BUILTIN_STVRX:
11630 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvrx, exp);
11631 case ALTIVEC_BUILTIN_STVRXL:
11632 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvrxl, exp);
11634 case VSX_BUILTIN_STXVD2X_V2DF:
11635 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v2df, exp);
11636 case VSX_BUILTIN_STXVD2X_V2DI:
11637 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v2di, exp);
11638 case VSX_BUILTIN_STXVW4X_V4SF:
11639 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v4sf, exp);
11640 case VSX_BUILTIN_STXVW4X_V4SI:
11641 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v4si, exp);
11642 case VSX_BUILTIN_STXVW4X_V8HI:
11643 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v8hi, exp);
11644 case VSX_BUILTIN_STXVW4X_V16QI:
11645 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v16qi, exp);
11647 case ALTIVEC_BUILTIN_MFVSCR:
11648 icode = CODE_FOR_altivec_mfvscr;
11649 tmode = insn_data[icode].operand[0].mode;
11651 if (target == 0
11652 || GET_MODE (target) != tmode
11653 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
11654 target = gen_reg_rtx (tmode);
11656 pat = GEN_FCN (icode) (target);
11657 if (! pat)
11658 return 0;
11659 emit_insn (pat);
11660 return target;
11662 case ALTIVEC_BUILTIN_MTVSCR:
11663 icode = CODE_FOR_altivec_mtvscr;
11664 arg0 = CALL_EXPR_ARG (exp, 0);
11665 op0 = expand_normal (arg0);
11666 mode0 = insn_data[icode].operand[0].mode;
11668 /* If we got invalid arguments bail out before generating bad rtl. */
11669 if (arg0 == error_mark_node)
11670 return const0_rtx;
11672 if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
11673 op0 = copy_to_mode_reg (mode0, op0);
11675 pat = GEN_FCN (icode) (op0);
11676 if (pat)
11677 emit_insn (pat);
11678 return NULL_RTX;
11680 case ALTIVEC_BUILTIN_DSSALL:
11681 emit_insn (gen_altivec_dssall ());
11682 return NULL_RTX;
11684 case ALTIVEC_BUILTIN_DSS:
11685 icode = CODE_FOR_altivec_dss;
11686 arg0 = CALL_EXPR_ARG (exp, 0);
11687 STRIP_NOPS (arg0);
11688 op0 = expand_normal (arg0);
11689 mode0 = insn_data[icode].operand[0].mode;
11691 /* If we got invalid arguments bail out before generating bad rtl. */
11692 if (arg0 == error_mark_node)
11693 return const0_rtx;
11695 if (TREE_CODE (arg0) != INTEGER_CST
11696 || TREE_INT_CST_LOW (arg0) & ~0x3)
11698 error ("argument to dss must be a 2-bit unsigned literal");
11699 return const0_rtx;
11702 if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
11703 op0 = copy_to_mode_reg (mode0, op0);
11705 emit_insn (gen_altivec_dss (op0));
11706 return NULL_RTX;
11708 case ALTIVEC_BUILTIN_VEC_INIT_V4SI:
11709 case ALTIVEC_BUILTIN_VEC_INIT_V8HI:
11710 case ALTIVEC_BUILTIN_VEC_INIT_V16QI:
11711 case ALTIVEC_BUILTIN_VEC_INIT_V4SF:
11712 case VSX_BUILTIN_VEC_INIT_V2DF:
11713 case VSX_BUILTIN_VEC_INIT_V2DI:
11714 return altivec_expand_vec_init_builtin (TREE_TYPE (exp), exp, target);
11716 case ALTIVEC_BUILTIN_VEC_SET_V4SI:
11717 case ALTIVEC_BUILTIN_VEC_SET_V8HI:
11718 case ALTIVEC_BUILTIN_VEC_SET_V16QI:
11719 case ALTIVEC_BUILTIN_VEC_SET_V4SF:
11720 case VSX_BUILTIN_VEC_SET_V2DF:
11721 case VSX_BUILTIN_VEC_SET_V2DI:
11722 return altivec_expand_vec_set_builtin (exp);
11724 case ALTIVEC_BUILTIN_VEC_EXT_V4SI:
11725 case ALTIVEC_BUILTIN_VEC_EXT_V8HI:
11726 case ALTIVEC_BUILTIN_VEC_EXT_V16QI:
11727 case ALTIVEC_BUILTIN_VEC_EXT_V4SF:
11728 case VSX_BUILTIN_VEC_EXT_V2DF:
11729 case VSX_BUILTIN_VEC_EXT_V2DI:
11730 return altivec_expand_vec_ext_builtin (exp, target);
11732 default:
11733 break;
11734 /* Fall through. */
11737 /* Expand abs* operations. */
11738 d = bdesc_abs;
11739 for (i = 0; i < ARRAY_SIZE (bdesc_abs); i++, d++)
11740 if (d->code == fcode)
11741 return altivec_expand_abs_builtin (d->icode, exp, target);
11743 /* Expand the AltiVec predicates. */
11744 d = bdesc_altivec_preds;
11745 for (i = 0; i < ARRAY_SIZE (bdesc_altivec_preds); i++, d++)
11746 if (d->code == fcode)
11747 return altivec_expand_predicate_builtin (d->icode, exp, target);
11749 /* LV* are funky. We initialized them differently. */
11750 switch (fcode)
11752 case ALTIVEC_BUILTIN_LVSL:
11753 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvsl,
11754 exp, target, false);
11755 case ALTIVEC_BUILTIN_LVSR:
11756 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvsr,
11757 exp, target, false);
11758 case ALTIVEC_BUILTIN_LVEBX:
11759 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvebx,
11760 exp, target, false);
11761 case ALTIVEC_BUILTIN_LVEHX:
11762 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvehx,
11763 exp, target, false);
11764 case ALTIVEC_BUILTIN_LVEWX:
11765 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvewx,
11766 exp, target, false);
11767 case ALTIVEC_BUILTIN_LVXL:
11768 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvxl,
11769 exp, target, false);
11770 case ALTIVEC_BUILTIN_LVX:
11771 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvx_v4si,
11772 exp, target, false);
11773 case ALTIVEC_BUILTIN_LVLX:
11774 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvlx,
11775 exp, target, true);
11776 case ALTIVEC_BUILTIN_LVLXL:
11777 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvlxl,
11778 exp, target, true);
11779 case ALTIVEC_BUILTIN_LVRX:
11780 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvrx,
11781 exp, target, true);
11782 case ALTIVEC_BUILTIN_LVRXL:
11783 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvrxl,
11784 exp, target, true);
11785 case VSX_BUILTIN_LXVD2X_V2DF:
11786 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v2df,
11787 exp, target, false);
11788 case VSX_BUILTIN_LXVD2X_V2DI:
11789 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v2di,
11790 exp, target, false);
11791 case VSX_BUILTIN_LXVW4X_V4SF:
11792 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v4sf,
11793 exp, target, false);
11794 case VSX_BUILTIN_LXVW4X_V4SI:
11795 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v4si,
11796 exp, target, false);
11797 case VSX_BUILTIN_LXVW4X_V8HI:
11798 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v8hi,
11799 exp, target, false);
11800 case VSX_BUILTIN_LXVW4X_V16QI:
11801 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v16qi,
11802 exp, target, false);
11803 break;
11804 default:
11805 break;
11806 /* Fall through. */
11809 *expandedp = false;
11810 return NULL_RTX;
11813 /* Expand the builtin in EXP and store the result in TARGET. Store
11814 true in *EXPANDEDP if we found a builtin to expand. */
11815 static rtx
11816 paired_expand_builtin (tree exp, rtx target, bool * expandedp)
11818 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
11819 enum rs6000_builtins fcode = (enum rs6000_builtins) DECL_FUNCTION_CODE (fndecl);
11820 const struct builtin_description *d;
11821 size_t i;
11823 *expandedp = true;
11825 switch (fcode)
11827 case PAIRED_BUILTIN_STX:
11828 return paired_expand_stv_builtin (CODE_FOR_paired_stx, exp);
11829 case PAIRED_BUILTIN_LX:
11830 return paired_expand_lv_builtin (CODE_FOR_paired_lx, exp, target);
11831 default:
11832 break;
11833 /* Fall through. */
11836 /* Expand the paired predicates. */
11837 d = bdesc_paired_preds;
11838 for (i = 0; i < ARRAY_SIZE (bdesc_paired_preds); i++, d++)
11839 if (d->code == fcode)
11840 return paired_expand_predicate_builtin (d->icode, exp, target);
11842 *expandedp = false;
11843 return NULL_RTX;
11846 /* Binops that need to be initialized manually, but can be expanded
11847 automagically by rs6000_expand_binop_builtin. */
11848 static const struct builtin_description bdesc_2arg_spe[] =
11850 { RS6000_BTM_SPE, CODE_FOR_spe_evlddx, "__builtin_spe_evlddx", SPE_BUILTIN_EVLDDX },
11851 { RS6000_BTM_SPE, CODE_FOR_spe_evldwx, "__builtin_spe_evldwx", SPE_BUILTIN_EVLDWX },
11852 { RS6000_BTM_SPE, CODE_FOR_spe_evldhx, "__builtin_spe_evldhx", SPE_BUILTIN_EVLDHX },
11853 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhex, "__builtin_spe_evlwhex", SPE_BUILTIN_EVLWHEX },
11854 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhoux, "__builtin_spe_evlwhoux", SPE_BUILTIN_EVLWHOUX },
11855 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhosx, "__builtin_spe_evlwhosx", SPE_BUILTIN_EVLWHOSX },
11856 { RS6000_BTM_SPE, CODE_FOR_spe_evlwwsplatx, "__builtin_spe_evlwwsplatx", SPE_BUILTIN_EVLWWSPLATX },
11857 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhsplatx, "__builtin_spe_evlwhsplatx", SPE_BUILTIN_EVLWHSPLATX },
11858 { RS6000_BTM_SPE, CODE_FOR_spe_evlhhesplatx, "__builtin_spe_evlhhesplatx", SPE_BUILTIN_EVLHHESPLATX },
11859 { RS6000_BTM_SPE, CODE_FOR_spe_evlhhousplatx, "__builtin_spe_evlhhousplatx", SPE_BUILTIN_EVLHHOUSPLATX },
11860 { RS6000_BTM_SPE, CODE_FOR_spe_evlhhossplatx, "__builtin_spe_evlhhossplatx", SPE_BUILTIN_EVLHHOSSPLATX },
11861 { RS6000_BTM_SPE, CODE_FOR_spe_evldd, "__builtin_spe_evldd", SPE_BUILTIN_EVLDD },
11862 { RS6000_BTM_SPE, CODE_FOR_spe_evldw, "__builtin_spe_evldw", SPE_BUILTIN_EVLDW },
11863 { RS6000_BTM_SPE, CODE_FOR_spe_evldh, "__builtin_spe_evldh", SPE_BUILTIN_EVLDH },
11864 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhe, "__builtin_spe_evlwhe", SPE_BUILTIN_EVLWHE },
11865 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhou, "__builtin_spe_evlwhou", SPE_BUILTIN_EVLWHOU },
11866 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhos, "__builtin_spe_evlwhos", SPE_BUILTIN_EVLWHOS },
11867 { RS6000_BTM_SPE, CODE_FOR_spe_evlwwsplat, "__builtin_spe_evlwwsplat", SPE_BUILTIN_EVLWWSPLAT },
11868 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhsplat, "__builtin_spe_evlwhsplat", SPE_BUILTIN_EVLWHSPLAT },
11869 { RS6000_BTM_SPE, CODE_FOR_spe_evlhhesplat, "__builtin_spe_evlhhesplat", SPE_BUILTIN_EVLHHESPLAT },
11870 { RS6000_BTM_SPE, CODE_FOR_spe_evlhhousplat, "__builtin_spe_evlhhousplat", SPE_BUILTIN_EVLHHOUSPLAT },
11871 { RS6000_BTM_SPE, CODE_FOR_spe_evlhhossplat, "__builtin_spe_evlhhossplat", SPE_BUILTIN_EVLHHOSSPLAT }
11874 /* Expand the builtin in EXP and store the result in TARGET. Store
11875 true in *EXPANDEDP if we found a builtin to expand.
11877 This expands the SPE builtins that are not simple unary and binary
11878 operations. */
11879 static rtx
11880 spe_expand_builtin (tree exp, rtx target, bool *expandedp)
11882 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
11883 tree arg1, arg0;
11884 enum rs6000_builtins fcode = (enum rs6000_builtins) DECL_FUNCTION_CODE (fndecl);
11885 enum insn_code icode;
11886 enum machine_mode tmode, mode0;
11887 rtx pat, op0;
11888 const struct builtin_description *d;
11889 size_t i;
11891 *expandedp = true;
11893 /* Syntax check for a 5-bit unsigned immediate. */
11894 switch (fcode)
11896 case SPE_BUILTIN_EVSTDD:
11897 case SPE_BUILTIN_EVSTDH:
11898 case SPE_BUILTIN_EVSTDW:
11899 case SPE_BUILTIN_EVSTWHE:
11900 case SPE_BUILTIN_EVSTWHO:
11901 case SPE_BUILTIN_EVSTWWE:
11902 case SPE_BUILTIN_EVSTWWO:
11903 arg1 = CALL_EXPR_ARG (exp, 2);
11904 if (TREE_CODE (arg1) != INTEGER_CST
11905 || TREE_INT_CST_LOW (arg1) & ~0x1f)
11907 error ("argument 2 must be a 5-bit unsigned literal");
11908 return const0_rtx;
11910 break;
11911 default:
11912 break;
11915 /* The evsplat*i instructions are not quite generic. */
11916 switch (fcode)
11918 case SPE_BUILTIN_EVSPLATFI:
11919 return rs6000_expand_unop_builtin (CODE_FOR_spe_evsplatfi,
11920 exp, target);
11921 case SPE_BUILTIN_EVSPLATI:
11922 return rs6000_expand_unop_builtin (CODE_FOR_spe_evsplati,
11923 exp, target);
11924 default:
11925 break;
11928 d = bdesc_2arg_spe;
11929 for (i = 0; i < ARRAY_SIZE (bdesc_2arg_spe); ++i, ++d)
11930 if (d->code == fcode)
11931 return rs6000_expand_binop_builtin (d->icode, exp, target);
11933 d = bdesc_spe_predicates;
11934 for (i = 0; i < ARRAY_SIZE (bdesc_spe_predicates); ++i, ++d)
11935 if (d->code == fcode)
11936 return spe_expand_predicate_builtin (d->icode, exp, target);
11938 d = bdesc_spe_evsel;
11939 for (i = 0; i < ARRAY_SIZE (bdesc_spe_evsel); ++i, ++d)
11940 if (d->code == fcode)
11941 return spe_expand_evsel_builtin (d->icode, exp, target);
11943 switch (fcode)
11945 case SPE_BUILTIN_EVSTDDX:
11946 return spe_expand_stv_builtin (CODE_FOR_spe_evstddx, exp);
11947 case SPE_BUILTIN_EVSTDHX:
11948 return spe_expand_stv_builtin (CODE_FOR_spe_evstdhx, exp);
11949 case SPE_BUILTIN_EVSTDWX:
11950 return spe_expand_stv_builtin (CODE_FOR_spe_evstdwx, exp);
11951 case SPE_BUILTIN_EVSTWHEX:
11952 return spe_expand_stv_builtin (CODE_FOR_spe_evstwhex, exp);
11953 case SPE_BUILTIN_EVSTWHOX:
11954 return spe_expand_stv_builtin (CODE_FOR_spe_evstwhox, exp);
11955 case SPE_BUILTIN_EVSTWWEX:
11956 return spe_expand_stv_builtin (CODE_FOR_spe_evstwwex, exp);
11957 case SPE_BUILTIN_EVSTWWOX:
11958 return spe_expand_stv_builtin (CODE_FOR_spe_evstwwox, exp);
11959 case SPE_BUILTIN_EVSTDD:
11960 return spe_expand_stv_builtin (CODE_FOR_spe_evstdd, exp);
11961 case SPE_BUILTIN_EVSTDH:
11962 return spe_expand_stv_builtin (CODE_FOR_spe_evstdh, exp);
11963 case SPE_BUILTIN_EVSTDW:
11964 return spe_expand_stv_builtin (CODE_FOR_spe_evstdw, exp);
11965 case SPE_BUILTIN_EVSTWHE:
11966 return spe_expand_stv_builtin (CODE_FOR_spe_evstwhe, exp);
11967 case SPE_BUILTIN_EVSTWHO:
11968 return spe_expand_stv_builtin (CODE_FOR_spe_evstwho, exp);
11969 case SPE_BUILTIN_EVSTWWE:
11970 return spe_expand_stv_builtin (CODE_FOR_spe_evstwwe, exp);
11971 case SPE_BUILTIN_EVSTWWO:
11972 return spe_expand_stv_builtin (CODE_FOR_spe_evstwwo, exp);
11973 case SPE_BUILTIN_MFSPEFSCR:
11974 icode = CODE_FOR_spe_mfspefscr;
11975 tmode = insn_data[icode].operand[0].mode;
11977 if (target == 0
11978 || GET_MODE (target) != tmode
11979 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
11980 target = gen_reg_rtx (tmode);
11982 pat = GEN_FCN (icode) (target);
11983 if (! pat)
11984 return 0;
11985 emit_insn (pat);
11986 return target;
11987 case SPE_BUILTIN_MTSPEFSCR:
11988 icode = CODE_FOR_spe_mtspefscr;
11989 arg0 = CALL_EXPR_ARG (exp, 0);
11990 op0 = expand_normal (arg0);
11991 mode0 = insn_data[icode].operand[0].mode;
11993 if (arg0 == error_mark_node)
11994 return const0_rtx;
11996 if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
11997 op0 = copy_to_mode_reg (mode0, op0);
11999 pat = GEN_FCN (icode) (op0);
12000 if (pat)
12001 emit_insn (pat);
12002 return NULL_RTX;
12003 default:
12004 break;
12007 *expandedp = false;
12008 return NULL_RTX;
12011 static rtx
12012 paired_expand_predicate_builtin (enum insn_code icode, tree exp, rtx target)
12014 rtx pat, scratch, tmp;
12015 tree form = CALL_EXPR_ARG (exp, 0);
12016 tree arg0 = CALL_EXPR_ARG (exp, 1);
12017 tree arg1 = CALL_EXPR_ARG (exp, 2);
12018 rtx op0 = expand_normal (arg0);
12019 rtx op1 = expand_normal (arg1);
12020 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
12021 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
12022 int form_int;
12023 enum rtx_code code;
12025 if (TREE_CODE (form) != INTEGER_CST)
12027 error ("argument 1 of __builtin_paired_predicate must be a constant");
12028 return const0_rtx;
12030 else
12031 form_int = TREE_INT_CST_LOW (form);
12033 gcc_assert (mode0 == mode1);
12035 if (arg0 == error_mark_node || arg1 == error_mark_node)
12036 return const0_rtx;
12038 if (target == 0
12039 || GET_MODE (target) != SImode
12040 || !(*insn_data[icode].operand[0].predicate) (target, SImode))
12041 target = gen_reg_rtx (SImode);
12042 if (!(*insn_data[icode].operand[1].predicate) (op0, mode0))
12043 op0 = copy_to_mode_reg (mode0, op0);
12044 if (!(*insn_data[icode].operand[2].predicate) (op1, mode1))
12045 op1 = copy_to_mode_reg (mode1, op1);
12047 scratch = gen_reg_rtx (CCFPmode);
12049 pat = GEN_FCN (icode) (scratch, op0, op1);
12050 if (!pat)
12051 return const0_rtx;
12053 emit_insn (pat);
12055 switch (form_int)
12057 /* LT bit. */
12058 case 0:
12059 code = LT;
12060 break;
12061 /* GT bit. */
12062 case 1:
12063 code = GT;
12064 break;
12065 /* EQ bit. */
12066 case 2:
12067 code = EQ;
12068 break;
12069 /* UN bit. */
12070 case 3:
12071 emit_insn (gen_move_from_CR_ov_bit (target, scratch));
12072 return target;
12073 default:
12074 error ("argument 1 of __builtin_paired_predicate is out of range");
12075 return const0_rtx;
12078 tmp = gen_rtx_fmt_ee (code, SImode, scratch, const0_rtx);
12079 emit_move_insn (target, tmp);
12080 return target;
12083 static rtx
12084 spe_expand_predicate_builtin (enum insn_code icode, tree exp, rtx target)
12086 rtx pat, scratch, tmp;
12087 tree form = CALL_EXPR_ARG (exp, 0);
12088 tree arg0 = CALL_EXPR_ARG (exp, 1);
12089 tree arg1 = CALL_EXPR_ARG (exp, 2);
12090 rtx op0 = expand_normal (arg0);
12091 rtx op1 = expand_normal (arg1);
12092 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
12093 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
12094 int form_int;
12095 enum rtx_code code;
12097 if (TREE_CODE (form) != INTEGER_CST)
12099 error ("argument 1 of __builtin_spe_predicate must be a constant");
12100 return const0_rtx;
12102 else
12103 form_int = TREE_INT_CST_LOW (form);
12105 gcc_assert (mode0 == mode1);
12107 if (arg0 == error_mark_node || arg1 == error_mark_node)
12108 return const0_rtx;
12110 if (target == 0
12111 || GET_MODE (target) != SImode
12112 || ! (*insn_data[icode].operand[0].predicate) (target, SImode))
12113 target = gen_reg_rtx (SImode);
12115 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12116 op0 = copy_to_mode_reg (mode0, op0);
12117 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12118 op1 = copy_to_mode_reg (mode1, op1);
12120 scratch = gen_reg_rtx (CCmode);
12122 pat = GEN_FCN (icode) (scratch, op0, op1);
12123 if (! pat)
12124 return const0_rtx;
12125 emit_insn (pat);
12127 /* There are 4 variants for each predicate: _any_, _all_, _upper_,
12128 _lower_. We use one compare, but look in different bits of the
12129 CR for each variant.
12131 There are 2 elements in each SPE simd type (upper/lower). The CR
12132 bits are set as follows:
12134 BIT0 | BIT 1 | BIT 2 | BIT 3
12135 U | L | (U | L) | (U & L)
12137 So, for an "all" relationship, BIT 3 would be set.
12138 For an "any" relationship, BIT 2 would be set. Etc.
12140 Following traditional nomenclature, these bits map to:
12142 BIT0 | BIT 1 | BIT 2 | BIT 3
12143 LT | GT | EQ | OV
12145 Later, we will generate rtl to look in the LT/EQ/EQ/OV bits.
12148 switch (form_int)
12150 /* All variant. OV bit. */
12151 case 0:
12152 /* We need to get to the OV bit, which is the ORDERED bit. We
12153 could generate (ordered:SI (reg:CC xx) (const_int 0)), but
12154 that's ugly and will make validate_condition_mode die.
12155 So let's just use another pattern. */
12156 emit_insn (gen_move_from_CR_ov_bit (target, scratch));
12157 return target;
12158 /* Any variant. EQ bit. */
12159 case 1:
12160 code = EQ;
12161 break;
12162 /* Upper variant. LT bit. */
12163 case 2:
12164 code = LT;
12165 break;
12166 /* Lower variant. GT bit. */
12167 case 3:
12168 code = GT;
12169 break;
12170 default:
12171 error ("argument 1 of __builtin_spe_predicate is out of range");
12172 return const0_rtx;
12175 tmp = gen_rtx_fmt_ee (code, SImode, scratch, const0_rtx);
12176 emit_move_insn (target, tmp);
12178 return target;
12181 /* The evsel builtins look like this:
12183 e = __builtin_spe_evsel_OP (a, b, c, d);
12185 and work like this:
12187 e[upper] = a[upper] *OP* b[upper] ? c[upper] : d[upper];
12188 e[lower] = a[lower] *OP* b[lower] ? c[lower] : d[lower];
12191 static rtx
12192 spe_expand_evsel_builtin (enum insn_code icode, tree exp, rtx target)
12194 rtx pat, scratch;
12195 tree arg0 = CALL_EXPR_ARG (exp, 0);
12196 tree arg1 = CALL_EXPR_ARG (exp, 1);
12197 tree arg2 = CALL_EXPR_ARG (exp, 2);
12198 tree arg3 = CALL_EXPR_ARG (exp, 3);
12199 rtx op0 = expand_normal (arg0);
12200 rtx op1 = expand_normal (arg1);
12201 rtx op2 = expand_normal (arg2);
12202 rtx op3 = expand_normal (arg3);
12203 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
12204 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
12206 gcc_assert (mode0 == mode1);
12208 if (arg0 == error_mark_node || arg1 == error_mark_node
12209 || arg2 == error_mark_node || arg3 == error_mark_node)
12210 return const0_rtx;
12212 if (target == 0
12213 || GET_MODE (target) != mode0
12214 || ! (*insn_data[icode].operand[0].predicate) (target, mode0))
12215 target = gen_reg_rtx (mode0);
12217 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12218 op0 = copy_to_mode_reg (mode0, op0);
12219 if (! (*insn_data[icode].operand[1].predicate) (op1, mode1))
12220 op1 = copy_to_mode_reg (mode0, op1);
12221 if (! (*insn_data[icode].operand[1].predicate) (op2, mode1))
12222 op2 = copy_to_mode_reg (mode0, op2);
12223 if (! (*insn_data[icode].operand[1].predicate) (op3, mode1))
12224 op3 = copy_to_mode_reg (mode0, op3);
12226 /* Generate the compare. */
12227 scratch = gen_reg_rtx (CCmode);
12228 pat = GEN_FCN (icode) (scratch, op0, op1);
12229 if (! pat)
12230 return const0_rtx;
12231 emit_insn (pat);
12233 if (mode0 == V2SImode)
12234 emit_insn (gen_spe_evsel (target, op2, op3, scratch));
12235 else
12236 emit_insn (gen_spe_evsel_fs (target, op2, op3, scratch));
12238 return target;
12241 /* Raise an error message for a builtin function that is called without the
12242 appropriate target options being set. */
12244 static void
12245 rs6000_invalid_builtin (enum rs6000_builtins fncode)
12247 size_t uns_fncode = (size_t)fncode;
12248 const char *name = rs6000_builtin_info[uns_fncode].name;
12249 HOST_WIDE_INT fnmask = rs6000_builtin_info[uns_fncode].mask;
12251 gcc_assert (name != NULL);
12252 if ((fnmask & RS6000_BTM_CELL) != 0)
12253 error ("Builtin function %s is only valid for the cell processor", name);
12254 else if ((fnmask & RS6000_BTM_VSX) != 0)
12255 error ("Builtin function %s requires the -mvsx option", name);
12256 else if ((fnmask & RS6000_BTM_HTM) != 0)
12257 error ("Builtin function %s requires the -mhtm option", name);
12258 else if ((fnmask & RS6000_BTM_ALTIVEC) != 0)
12259 error ("Builtin function %s requires the -maltivec option", name);
12260 else if ((fnmask & RS6000_BTM_PAIRED) != 0)
12261 error ("Builtin function %s requires the -mpaired option", name);
12262 else if ((fnmask & RS6000_BTM_SPE) != 0)
12263 error ("Builtin function %s requires the -mspe option", name);
12264 else
12265 error ("Builtin function %s is not supported with the current options",
12266 name);
12269 /* Expand an expression EXP that calls a built-in function,
12270 with result going to TARGET if that's convenient
12271 (and in mode MODE if that's convenient).
12272 SUBTARGET may be used as the target for computing one of EXP's operands.
12273 IGNORE is nonzero if the value is to be ignored. */
12275 static rtx
12276 rs6000_expand_builtin (tree exp, rtx target, rtx subtarget ATTRIBUTE_UNUSED,
12277 enum machine_mode mode ATTRIBUTE_UNUSED,
12278 int ignore ATTRIBUTE_UNUSED)
12280 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
12281 enum rs6000_builtins fcode
12282 = (enum rs6000_builtins)DECL_FUNCTION_CODE (fndecl);
12283 size_t uns_fcode = (size_t)fcode;
12284 const struct builtin_description *d;
12285 size_t i;
12286 rtx ret;
12287 bool success;
12288 HOST_WIDE_INT mask = rs6000_builtin_info[uns_fcode].mask;
12289 bool func_valid_p = ((rs6000_builtin_mask & mask) == mask);
12291 if (TARGET_DEBUG_BUILTIN)
12293 enum insn_code icode = rs6000_builtin_info[uns_fcode].icode;
12294 const char *name1 = rs6000_builtin_info[uns_fcode].name;
12295 const char *name2 = ((icode != CODE_FOR_nothing)
12296 ? get_insn_name ((int)icode)
12297 : "nothing");
12298 const char *name3;
12300 switch (rs6000_builtin_info[uns_fcode].attr & RS6000_BTC_TYPE_MASK)
12302 default: name3 = "unknown"; break;
12303 case RS6000_BTC_SPECIAL: name3 = "special"; break;
12304 case RS6000_BTC_UNARY: name3 = "unary"; break;
12305 case RS6000_BTC_BINARY: name3 = "binary"; break;
12306 case RS6000_BTC_TERNARY: name3 = "ternary"; break;
12307 case RS6000_BTC_PREDICATE: name3 = "predicate"; break;
12308 case RS6000_BTC_ABS: name3 = "abs"; break;
12309 case RS6000_BTC_EVSEL: name3 = "evsel"; break;
12310 case RS6000_BTC_DST: name3 = "dst"; break;
12314 fprintf (stderr,
12315 "rs6000_expand_builtin, %s (%d), insn = %s (%d), type=%s%s\n",
12316 (name1) ? name1 : "---", fcode,
12317 (name2) ? name2 : "---", (int)icode,
12318 name3,
12319 func_valid_p ? "" : ", not valid");
12322 if (!func_valid_p)
12324 rs6000_invalid_builtin (fcode);
12326 /* Given it is invalid, just generate a normal call. */
12327 return expand_call (exp, target, ignore);
12330 switch (fcode)
12332 case RS6000_BUILTIN_RECIP:
12333 return rs6000_expand_binop_builtin (CODE_FOR_recipdf3, exp, target);
12335 case RS6000_BUILTIN_RECIPF:
12336 return rs6000_expand_binop_builtin (CODE_FOR_recipsf3, exp, target);
12338 case RS6000_BUILTIN_RSQRTF:
12339 return rs6000_expand_unop_builtin (CODE_FOR_rsqrtsf2, exp, target);
12341 case RS6000_BUILTIN_RSQRT:
12342 return rs6000_expand_unop_builtin (CODE_FOR_rsqrtdf2, exp, target);
12344 case POWER7_BUILTIN_BPERMD:
12345 return rs6000_expand_binop_builtin (((TARGET_64BIT)
12346 ? CODE_FOR_bpermd_di
12347 : CODE_FOR_bpermd_si), exp, target);
12349 case RS6000_BUILTIN_GET_TB:
12350 return rs6000_expand_zeroop_builtin (CODE_FOR_rs6000_get_timebase,
12351 target);
12353 case RS6000_BUILTIN_MFTB:
12354 return rs6000_expand_zeroop_builtin (((TARGET_64BIT)
12355 ? CODE_FOR_rs6000_mftb_di
12356 : CODE_FOR_rs6000_mftb_si),
12357 target);
12359 case ALTIVEC_BUILTIN_MASK_FOR_LOAD:
12360 case ALTIVEC_BUILTIN_MASK_FOR_STORE:
12362 int icode = (BYTES_BIG_ENDIAN ? (int) CODE_FOR_altivec_lvsr
12363 : (int) CODE_FOR_altivec_lvsl);
12364 enum machine_mode tmode = insn_data[icode].operand[0].mode;
12365 enum machine_mode mode = insn_data[icode].operand[1].mode;
12366 tree arg;
12367 rtx op, addr, pat;
12369 gcc_assert (TARGET_ALTIVEC);
12371 arg = CALL_EXPR_ARG (exp, 0);
12372 gcc_assert (POINTER_TYPE_P (TREE_TYPE (arg)));
12373 op = expand_expr (arg, NULL_RTX, Pmode, EXPAND_NORMAL);
12374 addr = memory_address (mode, op);
12375 if (fcode == ALTIVEC_BUILTIN_MASK_FOR_STORE)
12376 op = addr;
12377 else
12379 /* For the load case need to negate the address. */
12380 op = gen_reg_rtx (GET_MODE (addr));
12381 emit_insn (gen_rtx_SET (VOIDmode, op,
12382 gen_rtx_NEG (GET_MODE (addr), addr)));
12384 op = gen_rtx_MEM (mode, op);
12386 if (target == 0
12387 || GET_MODE (target) != tmode
12388 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12389 target = gen_reg_rtx (tmode);
12391 /*pat = gen_altivec_lvsr (target, op);*/
12392 pat = GEN_FCN (icode) (target, op);
12393 if (!pat)
12394 return 0;
12395 emit_insn (pat);
12397 return target;
12400 case ALTIVEC_BUILTIN_VCFUX:
12401 case ALTIVEC_BUILTIN_VCFSX:
12402 case ALTIVEC_BUILTIN_VCTUXS:
12403 case ALTIVEC_BUILTIN_VCTSXS:
12404 /* FIXME: There's got to be a nicer way to handle this case than
12405 constructing a new CALL_EXPR. */
12406 if (call_expr_nargs (exp) == 1)
12408 exp = build_call_nary (TREE_TYPE (exp), CALL_EXPR_FN (exp),
12409 2, CALL_EXPR_ARG (exp, 0), integer_zero_node);
12411 break;
12413 default:
12414 break;
12417 if (TARGET_ALTIVEC)
12419 ret = altivec_expand_builtin (exp, target, &success);
12421 if (success)
12422 return ret;
12424 if (TARGET_SPE)
12426 ret = spe_expand_builtin (exp, target, &success);
12428 if (success)
12429 return ret;
12431 if (TARGET_PAIRED_FLOAT)
12433 ret = paired_expand_builtin (exp, target, &success);
12435 if (success)
12436 return ret;
12438 if (TARGET_HTM)
12440 ret = htm_expand_builtin (exp, target, &success);
12442 if (success)
12443 return ret;
12446 gcc_assert (TARGET_ALTIVEC || TARGET_VSX || TARGET_SPE || TARGET_PAIRED_FLOAT);
12448 /* Handle simple unary operations. */
12449 d = bdesc_1arg;
12450 for (i = 0; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
12451 if (d->code == fcode)
12452 return rs6000_expand_unop_builtin (d->icode, exp, target);
12454 /* Handle simple binary operations. */
12455 d = bdesc_2arg;
12456 for (i = 0; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
12457 if (d->code == fcode)
12458 return rs6000_expand_binop_builtin (d->icode, exp, target);
12460 /* Handle simple ternary operations. */
12461 d = bdesc_3arg;
12462 for (i = 0; i < ARRAY_SIZE (bdesc_3arg); i++, d++)
12463 if (d->code == fcode)
12464 return rs6000_expand_ternop_builtin (d->icode, exp, target);
12466 gcc_unreachable ();
12469 static void
12470 rs6000_init_builtins (void)
12472 tree tdecl;
12473 tree ftype;
12474 enum machine_mode mode;
12476 if (TARGET_DEBUG_BUILTIN)
12477 fprintf (stderr, "rs6000_init_builtins%s%s%s%s\n",
12478 (TARGET_PAIRED_FLOAT) ? ", paired" : "",
12479 (TARGET_SPE) ? ", spe" : "",
12480 (TARGET_ALTIVEC) ? ", altivec" : "",
12481 (TARGET_VSX) ? ", vsx" : "");
12483 V2SI_type_node = build_vector_type (intSI_type_node, 2);
12484 V2SF_type_node = build_vector_type (float_type_node, 2);
12485 V2DI_type_node = build_vector_type (intDI_type_node, 2);
12486 V2DF_type_node = build_vector_type (double_type_node, 2);
12487 V4HI_type_node = build_vector_type (intHI_type_node, 4);
12488 V4SI_type_node = build_vector_type (intSI_type_node, 4);
12489 V4SF_type_node = build_vector_type (float_type_node, 4);
12490 V8HI_type_node = build_vector_type (intHI_type_node, 8);
12491 V16QI_type_node = build_vector_type (intQI_type_node, 16);
12493 unsigned_V16QI_type_node = build_vector_type (unsigned_intQI_type_node, 16);
12494 unsigned_V8HI_type_node = build_vector_type (unsigned_intHI_type_node, 8);
12495 unsigned_V4SI_type_node = build_vector_type (unsigned_intSI_type_node, 4);
12496 unsigned_V2DI_type_node = build_vector_type (unsigned_intDI_type_node, 2);
12498 opaque_V2SF_type_node = build_opaque_vector_type (float_type_node, 2);
12499 opaque_V2SI_type_node = build_opaque_vector_type (intSI_type_node, 2);
12500 opaque_p_V2SI_type_node = build_pointer_type (opaque_V2SI_type_node);
12501 opaque_V4SI_type_node = build_opaque_vector_type (intSI_type_node, 4);
12503 /* The 'vector bool ...' types must be kept distinct from 'vector unsigned ...'
12504 types, especially in C++ land. Similarly, 'vector pixel' is distinct from
12505 'vector unsigned short'. */
12507 bool_char_type_node = build_distinct_type_copy (unsigned_intQI_type_node);
12508 bool_short_type_node = build_distinct_type_copy (unsigned_intHI_type_node);
12509 bool_int_type_node = build_distinct_type_copy (unsigned_intSI_type_node);
12510 bool_long_type_node = build_distinct_type_copy (unsigned_intDI_type_node);
12511 pixel_type_node = build_distinct_type_copy (unsigned_intHI_type_node);
12513 long_integer_type_internal_node = long_integer_type_node;
12514 long_unsigned_type_internal_node = long_unsigned_type_node;
12515 long_long_integer_type_internal_node = long_long_integer_type_node;
12516 long_long_unsigned_type_internal_node = long_long_unsigned_type_node;
12517 intQI_type_internal_node = intQI_type_node;
12518 uintQI_type_internal_node = unsigned_intQI_type_node;
12519 intHI_type_internal_node = intHI_type_node;
12520 uintHI_type_internal_node = unsigned_intHI_type_node;
12521 intSI_type_internal_node = intSI_type_node;
12522 uintSI_type_internal_node = unsigned_intSI_type_node;
12523 intDI_type_internal_node = intDI_type_node;
12524 uintDI_type_internal_node = unsigned_intDI_type_node;
12525 float_type_internal_node = float_type_node;
12526 double_type_internal_node = double_type_node;
12527 void_type_internal_node = void_type_node;
12529 /* Initialize the modes for builtin_function_type, mapping a machine mode to
12530 tree type node. */
12531 builtin_mode_to_type[QImode][0] = integer_type_node;
12532 builtin_mode_to_type[HImode][0] = integer_type_node;
12533 builtin_mode_to_type[SImode][0] = intSI_type_node;
12534 builtin_mode_to_type[SImode][1] = unsigned_intSI_type_node;
12535 builtin_mode_to_type[DImode][0] = intDI_type_node;
12536 builtin_mode_to_type[DImode][1] = unsigned_intDI_type_node;
12537 builtin_mode_to_type[SFmode][0] = float_type_node;
12538 builtin_mode_to_type[DFmode][0] = double_type_node;
12539 builtin_mode_to_type[V2SImode][0] = V2SI_type_node;
12540 builtin_mode_to_type[V2SFmode][0] = V2SF_type_node;
12541 builtin_mode_to_type[V2DImode][0] = V2DI_type_node;
12542 builtin_mode_to_type[V2DImode][1] = unsigned_V2DI_type_node;
12543 builtin_mode_to_type[V2DFmode][0] = V2DF_type_node;
12544 builtin_mode_to_type[V4HImode][0] = V4HI_type_node;
12545 builtin_mode_to_type[V4SImode][0] = V4SI_type_node;
12546 builtin_mode_to_type[V4SImode][1] = unsigned_V4SI_type_node;
12547 builtin_mode_to_type[V4SFmode][0] = V4SF_type_node;
12548 builtin_mode_to_type[V8HImode][0] = V8HI_type_node;
12549 builtin_mode_to_type[V8HImode][1] = unsigned_V8HI_type_node;
12550 builtin_mode_to_type[V16QImode][0] = V16QI_type_node;
12551 builtin_mode_to_type[V16QImode][1] = unsigned_V16QI_type_node;
12553 tdecl = add_builtin_type ("__bool char", bool_char_type_node);
12554 TYPE_NAME (bool_char_type_node) = tdecl;
12556 tdecl = add_builtin_type ("__bool short", bool_short_type_node);
12557 TYPE_NAME (bool_short_type_node) = tdecl;
12559 tdecl = add_builtin_type ("__bool int", bool_int_type_node);
12560 TYPE_NAME (bool_int_type_node) = tdecl;
12562 tdecl = add_builtin_type ("__pixel", pixel_type_node);
12563 TYPE_NAME (pixel_type_node) = tdecl;
12565 bool_V16QI_type_node = build_vector_type (bool_char_type_node, 16);
12566 bool_V8HI_type_node = build_vector_type (bool_short_type_node, 8);
12567 bool_V4SI_type_node = build_vector_type (bool_int_type_node, 4);
12568 bool_V2DI_type_node = build_vector_type (bool_long_type_node, 2);
12569 pixel_V8HI_type_node = build_vector_type (pixel_type_node, 8);
12571 tdecl = add_builtin_type ("__vector unsigned char", unsigned_V16QI_type_node);
12572 TYPE_NAME (unsigned_V16QI_type_node) = tdecl;
12574 tdecl = add_builtin_type ("__vector signed char", V16QI_type_node);
12575 TYPE_NAME (V16QI_type_node) = tdecl;
12577 tdecl = add_builtin_type ("__vector __bool char", bool_V16QI_type_node);
12578 TYPE_NAME ( bool_V16QI_type_node) = tdecl;
12580 tdecl = add_builtin_type ("__vector unsigned short", unsigned_V8HI_type_node);
12581 TYPE_NAME (unsigned_V8HI_type_node) = tdecl;
12583 tdecl = add_builtin_type ("__vector signed short", V8HI_type_node);
12584 TYPE_NAME (V8HI_type_node) = tdecl;
12586 tdecl = add_builtin_type ("__vector __bool short", bool_V8HI_type_node);
12587 TYPE_NAME (bool_V8HI_type_node) = tdecl;
12589 tdecl = add_builtin_type ("__vector unsigned int", unsigned_V4SI_type_node);
12590 TYPE_NAME (unsigned_V4SI_type_node) = tdecl;
12592 tdecl = add_builtin_type ("__vector signed int", V4SI_type_node);
12593 TYPE_NAME (V4SI_type_node) = tdecl;
12595 tdecl = add_builtin_type ("__vector __bool int", bool_V4SI_type_node);
12596 TYPE_NAME (bool_V4SI_type_node) = tdecl;
12598 tdecl = add_builtin_type ("__vector float", V4SF_type_node);
12599 TYPE_NAME (V4SF_type_node) = tdecl;
12601 tdecl = add_builtin_type ("__vector __pixel", pixel_V8HI_type_node);
12602 TYPE_NAME (pixel_V8HI_type_node) = tdecl;
12604 tdecl = add_builtin_type ("__vector double", V2DF_type_node);
12605 TYPE_NAME (V2DF_type_node) = tdecl;
12607 tdecl = add_builtin_type ("__vector long", V2DI_type_node);
12608 TYPE_NAME (V2DI_type_node) = tdecl;
12610 tdecl = add_builtin_type ("__vector unsigned long", unsigned_V2DI_type_node);
12611 TYPE_NAME (unsigned_V2DI_type_node) = tdecl;
12613 tdecl = add_builtin_type ("__vector __bool long", bool_V2DI_type_node);
12614 TYPE_NAME (bool_V2DI_type_node) = tdecl;
12616 /* Paired and SPE builtins are only available if you build a compiler with
12617 the appropriate options, so only create those builtins with the
12618 appropriate compiler option. Create Altivec and VSX builtins on machines
12619 with at least the general purpose extensions (970 and newer) to allow the
12620 use of the target attribute. */
12621 if (TARGET_PAIRED_FLOAT)
12622 paired_init_builtins ();
12623 if (TARGET_SPE)
12624 spe_init_builtins ();
12625 if (TARGET_EXTRA_BUILTINS)
12626 altivec_init_builtins ();
12627 if (TARGET_HTM)
12628 htm_init_builtins ();
12630 if (TARGET_EXTRA_BUILTINS || TARGET_SPE || TARGET_PAIRED_FLOAT)
12631 rs6000_common_init_builtins ();
12633 ftype = builtin_function_type (DFmode, DFmode, DFmode, VOIDmode,
12634 RS6000_BUILTIN_RECIP, "__builtin_recipdiv");
12635 def_builtin ("__builtin_recipdiv", ftype, RS6000_BUILTIN_RECIP);
12637 ftype = builtin_function_type (SFmode, SFmode, SFmode, VOIDmode,
12638 RS6000_BUILTIN_RECIPF, "__builtin_recipdivf");
12639 def_builtin ("__builtin_recipdivf", ftype, RS6000_BUILTIN_RECIPF);
12641 ftype = builtin_function_type (DFmode, DFmode, VOIDmode, VOIDmode,
12642 RS6000_BUILTIN_RSQRT, "__builtin_rsqrt");
12643 def_builtin ("__builtin_rsqrt", ftype, RS6000_BUILTIN_RSQRT);
12645 ftype = builtin_function_type (SFmode, SFmode, VOIDmode, VOIDmode,
12646 RS6000_BUILTIN_RSQRTF, "__builtin_rsqrtf");
12647 def_builtin ("__builtin_rsqrtf", ftype, RS6000_BUILTIN_RSQRTF);
12649 mode = (TARGET_64BIT) ? DImode : SImode;
12650 ftype = builtin_function_type (mode, mode, mode, VOIDmode,
12651 POWER7_BUILTIN_BPERMD, "__builtin_bpermd");
12652 def_builtin ("__builtin_bpermd", ftype, POWER7_BUILTIN_BPERMD);
12654 ftype = build_function_type_list (unsigned_intDI_type_node,
12655 NULL_TREE);
12656 def_builtin ("__builtin_ppc_get_timebase", ftype, RS6000_BUILTIN_GET_TB);
12658 if (TARGET_64BIT)
12659 ftype = build_function_type_list (unsigned_intDI_type_node,
12660 NULL_TREE);
12661 else
12662 ftype = build_function_type_list (unsigned_intSI_type_node,
12663 NULL_TREE);
12664 def_builtin ("__builtin_ppc_mftb", ftype, RS6000_BUILTIN_MFTB);
12666 #if TARGET_XCOFF
12667 /* AIX libm provides clog as __clog. */
12668 if ((tdecl = builtin_decl_explicit (BUILT_IN_CLOG)) != NULL_TREE)
12669 set_user_assembler_name (tdecl, "__clog");
12670 #endif
12672 #ifdef SUBTARGET_INIT_BUILTINS
12673 SUBTARGET_INIT_BUILTINS;
12674 #endif
12677 /* Returns the rs6000 builtin decl for CODE. */
12679 static tree
12680 rs6000_builtin_decl (unsigned code, bool initialize_p ATTRIBUTE_UNUSED)
12682 HOST_WIDE_INT fnmask;
12684 if (code >= RS6000_BUILTIN_COUNT)
12685 return error_mark_node;
12687 fnmask = rs6000_builtin_info[code].mask;
12688 if ((fnmask & rs6000_builtin_mask) != fnmask)
12690 rs6000_invalid_builtin ((enum rs6000_builtins)code);
12691 return error_mark_node;
12694 return rs6000_builtin_decls[code];
12697 static void
12698 spe_init_builtins (void)
12700 tree puint_type_node = build_pointer_type (unsigned_type_node);
12701 tree pushort_type_node = build_pointer_type (short_unsigned_type_node);
12702 const struct builtin_description *d;
12703 size_t i;
12705 tree v2si_ftype_4_v2si
12706 = build_function_type_list (opaque_V2SI_type_node,
12707 opaque_V2SI_type_node,
12708 opaque_V2SI_type_node,
12709 opaque_V2SI_type_node,
12710 opaque_V2SI_type_node,
12711 NULL_TREE);
12713 tree v2sf_ftype_4_v2sf
12714 = build_function_type_list (opaque_V2SF_type_node,
12715 opaque_V2SF_type_node,
12716 opaque_V2SF_type_node,
12717 opaque_V2SF_type_node,
12718 opaque_V2SF_type_node,
12719 NULL_TREE);
12721 tree int_ftype_int_v2si_v2si
12722 = build_function_type_list (integer_type_node,
12723 integer_type_node,
12724 opaque_V2SI_type_node,
12725 opaque_V2SI_type_node,
12726 NULL_TREE);
12728 tree int_ftype_int_v2sf_v2sf
12729 = build_function_type_list (integer_type_node,
12730 integer_type_node,
12731 opaque_V2SF_type_node,
12732 opaque_V2SF_type_node,
12733 NULL_TREE);
12735 tree void_ftype_v2si_puint_int
12736 = build_function_type_list (void_type_node,
12737 opaque_V2SI_type_node,
12738 puint_type_node,
12739 integer_type_node,
12740 NULL_TREE);
12742 tree void_ftype_v2si_puint_char
12743 = build_function_type_list (void_type_node,
12744 opaque_V2SI_type_node,
12745 puint_type_node,
12746 char_type_node,
12747 NULL_TREE);
12749 tree void_ftype_v2si_pv2si_int
12750 = build_function_type_list (void_type_node,
12751 opaque_V2SI_type_node,
12752 opaque_p_V2SI_type_node,
12753 integer_type_node,
12754 NULL_TREE);
12756 tree void_ftype_v2si_pv2si_char
12757 = build_function_type_list (void_type_node,
12758 opaque_V2SI_type_node,
12759 opaque_p_V2SI_type_node,
12760 char_type_node,
12761 NULL_TREE);
12763 tree void_ftype_int
12764 = build_function_type_list (void_type_node, integer_type_node, NULL_TREE);
12766 tree int_ftype_void
12767 = build_function_type_list (integer_type_node, NULL_TREE);
12769 tree v2si_ftype_pv2si_int
12770 = build_function_type_list (opaque_V2SI_type_node,
12771 opaque_p_V2SI_type_node,
12772 integer_type_node,
12773 NULL_TREE);
12775 tree v2si_ftype_puint_int
12776 = build_function_type_list (opaque_V2SI_type_node,
12777 puint_type_node,
12778 integer_type_node,
12779 NULL_TREE);
12781 tree v2si_ftype_pushort_int
12782 = build_function_type_list (opaque_V2SI_type_node,
12783 pushort_type_node,
12784 integer_type_node,
12785 NULL_TREE);
12787 tree v2si_ftype_signed_char
12788 = build_function_type_list (opaque_V2SI_type_node,
12789 signed_char_type_node,
12790 NULL_TREE);
12792 add_builtin_type ("__ev64_opaque__", opaque_V2SI_type_node);
12794 /* Initialize irregular SPE builtins. */
12796 def_builtin ("__builtin_spe_mtspefscr", void_ftype_int, SPE_BUILTIN_MTSPEFSCR);
12797 def_builtin ("__builtin_spe_mfspefscr", int_ftype_void, SPE_BUILTIN_MFSPEFSCR);
12798 def_builtin ("__builtin_spe_evstddx", void_ftype_v2si_pv2si_int, SPE_BUILTIN_EVSTDDX);
12799 def_builtin ("__builtin_spe_evstdhx", void_ftype_v2si_pv2si_int, SPE_BUILTIN_EVSTDHX);
12800 def_builtin ("__builtin_spe_evstdwx", void_ftype_v2si_pv2si_int, SPE_BUILTIN_EVSTDWX);
12801 def_builtin ("__builtin_spe_evstwhex", void_ftype_v2si_puint_int, SPE_BUILTIN_EVSTWHEX);
12802 def_builtin ("__builtin_spe_evstwhox", void_ftype_v2si_puint_int, SPE_BUILTIN_EVSTWHOX);
12803 def_builtin ("__builtin_spe_evstwwex", void_ftype_v2si_puint_int, SPE_BUILTIN_EVSTWWEX);
12804 def_builtin ("__builtin_spe_evstwwox", void_ftype_v2si_puint_int, SPE_BUILTIN_EVSTWWOX);
12805 def_builtin ("__builtin_spe_evstdd", void_ftype_v2si_pv2si_char, SPE_BUILTIN_EVSTDD);
12806 def_builtin ("__builtin_spe_evstdh", void_ftype_v2si_pv2si_char, SPE_BUILTIN_EVSTDH);
12807 def_builtin ("__builtin_spe_evstdw", void_ftype_v2si_pv2si_char, SPE_BUILTIN_EVSTDW);
12808 def_builtin ("__builtin_spe_evstwhe", void_ftype_v2si_puint_char, SPE_BUILTIN_EVSTWHE);
12809 def_builtin ("__builtin_spe_evstwho", void_ftype_v2si_puint_char, SPE_BUILTIN_EVSTWHO);
12810 def_builtin ("__builtin_spe_evstwwe", void_ftype_v2si_puint_char, SPE_BUILTIN_EVSTWWE);
12811 def_builtin ("__builtin_spe_evstwwo", void_ftype_v2si_puint_char, SPE_BUILTIN_EVSTWWO);
12812 def_builtin ("__builtin_spe_evsplatfi", v2si_ftype_signed_char, SPE_BUILTIN_EVSPLATFI);
12813 def_builtin ("__builtin_spe_evsplati", v2si_ftype_signed_char, SPE_BUILTIN_EVSPLATI);
12815 /* Loads. */
12816 def_builtin ("__builtin_spe_evlddx", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDDX);
12817 def_builtin ("__builtin_spe_evldwx", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDWX);
12818 def_builtin ("__builtin_spe_evldhx", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDHX);
12819 def_builtin ("__builtin_spe_evlwhex", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHEX);
12820 def_builtin ("__builtin_spe_evlwhoux", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHOUX);
12821 def_builtin ("__builtin_spe_evlwhosx", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHOSX);
12822 def_builtin ("__builtin_spe_evlwwsplatx", v2si_ftype_puint_int, SPE_BUILTIN_EVLWWSPLATX);
12823 def_builtin ("__builtin_spe_evlwhsplatx", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHSPLATX);
12824 def_builtin ("__builtin_spe_evlhhesplatx", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHESPLATX);
12825 def_builtin ("__builtin_spe_evlhhousplatx", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHOUSPLATX);
12826 def_builtin ("__builtin_spe_evlhhossplatx", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHOSSPLATX);
12827 def_builtin ("__builtin_spe_evldd", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDD);
12828 def_builtin ("__builtin_spe_evldw", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDW);
12829 def_builtin ("__builtin_spe_evldh", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDH);
12830 def_builtin ("__builtin_spe_evlhhesplat", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHESPLAT);
12831 def_builtin ("__builtin_spe_evlhhossplat", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHOSSPLAT);
12832 def_builtin ("__builtin_spe_evlhhousplat", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHOUSPLAT);
12833 def_builtin ("__builtin_spe_evlwhe", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHE);
12834 def_builtin ("__builtin_spe_evlwhos", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHOS);
12835 def_builtin ("__builtin_spe_evlwhou", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHOU);
12836 def_builtin ("__builtin_spe_evlwhsplat", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHSPLAT);
12837 def_builtin ("__builtin_spe_evlwwsplat", v2si_ftype_puint_int, SPE_BUILTIN_EVLWWSPLAT);
12839 /* Predicates. */
12840 d = bdesc_spe_predicates;
12841 for (i = 0; i < ARRAY_SIZE (bdesc_spe_predicates); ++i, d++)
12843 tree type;
12845 switch (insn_data[d->icode].operand[1].mode)
12847 case V2SImode:
12848 type = int_ftype_int_v2si_v2si;
12849 break;
12850 case V2SFmode:
12851 type = int_ftype_int_v2sf_v2sf;
12852 break;
12853 default:
12854 gcc_unreachable ();
12857 def_builtin (d->name, type, d->code);
12860 /* Evsel predicates. */
12861 d = bdesc_spe_evsel;
12862 for (i = 0; i < ARRAY_SIZE (bdesc_spe_evsel); ++i, d++)
12864 tree type;
12866 switch (insn_data[d->icode].operand[1].mode)
12868 case V2SImode:
12869 type = v2si_ftype_4_v2si;
12870 break;
12871 case V2SFmode:
12872 type = v2sf_ftype_4_v2sf;
12873 break;
12874 default:
12875 gcc_unreachable ();
12878 def_builtin (d->name, type, d->code);
12882 static void
12883 paired_init_builtins (void)
12885 const struct builtin_description *d;
12886 size_t i;
12888 tree int_ftype_int_v2sf_v2sf
12889 = build_function_type_list (integer_type_node,
12890 integer_type_node,
12891 V2SF_type_node,
12892 V2SF_type_node,
12893 NULL_TREE);
12894 tree pcfloat_type_node =
12895 build_pointer_type (build_qualified_type
12896 (float_type_node, TYPE_QUAL_CONST));
12898 tree v2sf_ftype_long_pcfloat = build_function_type_list (V2SF_type_node,
12899 long_integer_type_node,
12900 pcfloat_type_node,
12901 NULL_TREE);
12902 tree void_ftype_v2sf_long_pcfloat =
12903 build_function_type_list (void_type_node,
12904 V2SF_type_node,
12905 long_integer_type_node,
12906 pcfloat_type_node,
12907 NULL_TREE);
12910 def_builtin ("__builtin_paired_lx", v2sf_ftype_long_pcfloat,
12911 PAIRED_BUILTIN_LX);
12914 def_builtin ("__builtin_paired_stx", void_ftype_v2sf_long_pcfloat,
12915 PAIRED_BUILTIN_STX);
12917 /* Predicates. */
12918 d = bdesc_paired_preds;
12919 for (i = 0; i < ARRAY_SIZE (bdesc_paired_preds); ++i, d++)
12921 tree type;
12923 if (TARGET_DEBUG_BUILTIN)
12924 fprintf (stderr, "paired pred #%d, insn = %s [%d], mode = %s\n",
12925 (int)i, get_insn_name (d->icode), (int)d->icode,
12926 GET_MODE_NAME (insn_data[d->icode].operand[1].mode));
12928 switch (insn_data[d->icode].operand[1].mode)
12930 case V2SFmode:
12931 type = int_ftype_int_v2sf_v2sf;
12932 break;
12933 default:
12934 gcc_unreachable ();
12937 def_builtin (d->name, type, d->code);
12941 static void
12942 altivec_init_builtins (void)
12944 const struct builtin_description *d;
12945 size_t i;
12946 tree ftype;
12947 tree decl;
12949 tree pvoid_type_node = build_pointer_type (void_type_node);
12951 tree pcvoid_type_node
12952 = build_pointer_type (build_qualified_type (void_type_node,
12953 TYPE_QUAL_CONST));
12955 tree int_ftype_opaque
12956 = build_function_type_list (integer_type_node,
12957 opaque_V4SI_type_node, NULL_TREE);
12958 tree opaque_ftype_opaque
12959 = build_function_type_list (integer_type_node, NULL_TREE);
12960 tree opaque_ftype_opaque_int
12961 = build_function_type_list (opaque_V4SI_type_node,
12962 opaque_V4SI_type_node, integer_type_node, NULL_TREE);
12963 tree opaque_ftype_opaque_opaque_int
12964 = build_function_type_list (opaque_V4SI_type_node,
12965 opaque_V4SI_type_node, opaque_V4SI_type_node,
12966 integer_type_node, NULL_TREE);
12967 tree int_ftype_int_opaque_opaque
12968 = build_function_type_list (integer_type_node,
12969 integer_type_node, opaque_V4SI_type_node,
12970 opaque_V4SI_type_node, NULL_TREE);
12971 tree int_ftype_int_v4si_v4si
12972 = build_function_type_list (integer_type_node,
12973 integer_type_node, V4SI_type_node,
12974 V4SI_type_node, NULL_TREE);
12975 tree int_ftype_int_v2di_v2di
12976 = build_function_type_list (integer_type_node,
12977 integer_type_node, V2DI_type_node,
12978 V2DI_type_node, NULL_TREE);
12979 tree void_ftype_v4si
12980 = build_function_type_list (void_type_node, V4SI_type_node, NULL_TREE);
12981 tree v8hi_ftype_void
12982 = build_function_type_list (V8HI_type_node, NULL_TREE);
12983 tree void_ftype_void
12984 = build_function_type_list (void_type_node, NULL_TREE);
12985 tree void_ftype_int
12986 = build_function_type_list (void_type_node, integer_type_node, NULL_TREE);
12988 tree opaque_ftype_long_pcvoid
12989 = build_function_type_list (opaque_V4SI_type_node,
12990 long_integer_type_node, pcvoid_type_node,
12991 NULL_TREE);
12992 tree v16qi_ftype_long_pcvoid
12993 = build_function_type_list (V16QI_type_node,
12994 long_integer_type_node, pcvoid_type_node,
12995 NULL_TREE);
12996 tree v8hi_ftype_long_pcvoid
12997 = build_function_type_list (V8HI_type_node,
12998 long_integer_type_node, pcvoid_type_node,
12999 NULL_TREE);
13000 tree v4si_ftype_long_pcvoid
13001 = build_function_type_list (V4SI_type_node,
13002 long_integer_type_node, pcvoid_type_node,
13003 NULL_TREE);
13004 tree v4sf_ftype_long_pcvoid
13005 = build_function_type_list (V4SF_type_node,
13006 long_integer_type_node, pcvoid_type_node,
13007 NULL_TREE);
13008 tree v2df_ftype_long_pcvoid
13009 = build_function_type_list (V2DF_type_node,
13010 long_integer_type_node, pcvoid_type_node,
13011 NULL_TREE);
13012 tree v2di_ftype_long_pcvoid
13013 = build_function_type_list (V2DI_type_node,
13014 long_integer_type_node, pcvoid_type_node,
13015 NULL_TREE);
13017 tree void_ftype_opaque_long_pvoid
13018 = build_function_type_list (void_type_node,
13019 opaque_V4SI_type_node, long_integer_type_node,
13020 pvoid_type_node, NULL_TREE);
13021 tree void_ftype_v4si_long_pvoid
13022 = build_function_type_list (void_type_node,
13023 V4SI_type_node, long_integer_type_node,
13024 pvoid_type_node, NULL_TREE);
13025 tree void_ftype_v16qi_long_pvoid
13026 = build_function_type_list (void_type_node,
13027 V16QI_type_node, long_integer_type_node,
13028 pvoid_type_node, NULL_TREE);
13029 tree void_ftype_v8hi_long_pvoid
13030 = build_function_type_list (void_type_node,
13031 V8HI_type_node, long_integer_type_node,
13032 pvoid_type_node, NULL_TREE);
13033 tree void_ftype_v4sf_long_pvoid
13034 = build_function_type_list (void_type_node,
13035 V4SF_type_node, long_integer_type_node,
13036 pvoid_type_node, NULL_TREE);
13037 tree void_ftype_v2df_long_pvoid
13038 = build_function_type_list (void_type_node,
13039 V2DF_type_node, long_integer_type_node,
13040 pvoid_type_node, NULL_TREE);
13041 tree void_ftype_v2di_long_pvoid
13042 = build_function_type_list (void_type_node,
13043 V2DI_type_node, long_integer_type_node,
13044 pvoid_type_node, NULL_TREE);
13045 tree int_ftype_int_v8hi_v8hi
13046 = build_function_type_list (integer_type_node,
13047 integer_type_node, V8HI_type_node,
13048 V8HI_type_node, NULL_TREE);
13049 tree int_ftype_int_v16qi_v16qi
13050 = build_function_type_list (integer_type_node,
13051 integer_type_node, V16QI_type_node,
13052 V16QI_type_node, NULL_TREE);
13053 tree int_ftype_int_v4sf_v4sf
13054 = build_function_type_list (integer_type_node,
13055 integer_type_node, V4SF_type_node,
13056 V4SF_type_node, NULL_TREE);
13057 tree int_ftype_int_v2df_v2df
13058 = build_function_type_list (integer_type_node,
13059 integer_type_node, V2DF_type_node,
13060 V2DF_type_node, NULL_TREE);
13061 tree v2di_ftype_v2di
13062 = build_function_type_list (V2DI_type_node, V2DI_type_node, NULL_TREE);
13063 tree v4si_ftype_v4si
13064 = build_function_type_list (V4SI_type_node, V4SI_type_node, NULL_TREE);
13065 tree v8hi_ftype_v8hi
13066 = build_function_type_list (V8HI_type_node, V8HI_type_node, NULL_TREE);
13067 tree v16qi_ftype_v16qi
13068 = build_function_type_list (V16QI_type_node, V16QI_type_node, NULL_TREE);
13069 tree v4sf_ftype_v4sf
13070 = build_function_type_list (V4SF_type_node, V4SF_type_node, NULL_TREE);
13071 tree v2df_ftype_v2df
13072 = build_function_type_list (V2DF_type_node, V2DF_type_node, NULL_TREE);
13073 tree void_ftype_pcvoid_int_int
13074 = build_function_type_list (void_type_node,
13075 pcvoid_type_node, integer_type_node,
13076 integer_type_node, NULL_TREE);
13078 def_builtin ("__builtin_altivec_mtvscr", void_ftype_v4si, ALTIVEC_BUILTIN_MTVSCR);
13079 def_builtin ("__builtin_altivec_mfvscr", v8hi_ftype_void, ALTIVEC_BUILTIN_MFVSCR);
13080 def_builtin ("__builtin_altivec_dssall", void_ftype_void, ALTIVEC_BUILTIN_DSSALL);
13081 def_builtin ("__builtin_altivec_dss", void_ftype_int, ALTIVEC_BUILTIN_DSS);
13082 def_builtin ("__builtin_altivec_lvsl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVSL);
13083 def_builtin ("__builtin_altivec_lvsr", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVSR);
13084 def_builtin ("__builtin_altivec_lvebx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVEBX);
13085 def_builtin ("__builtin_altivec_lvehx", v8hi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVEHX);
13086 def_builtin ("__builtin_altivec_lvewx", v4si_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVEWX);
13087 def_builtin ("__builtin_altivec_lvxl", v4si_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVXL);
13088 def_builtin ("__builtin_altivec_lvx", v4si_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVX);
13089 def_builtin ("__builtin_altivec_stvx", void_ftype_v4si_long_pvoid, ALTIVEC_BUILTIN_STVX);
13090 def_builtin ("__builtin_altivec_stvewx", void_ftype_v4si_long_pvoid, ALTIVEC_BUILTIN_STVEWX);
13091 def_builtin ("__builtin_altivec_stvxl", void_ftype_v4si_long_pvoid, ALTIVEC_BUILTIN_STVXL);
13092 def_builtin ("__builtin_altivec_stvebx", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_STVEBX);
13093 def_builtin ("__builtin_altivec_stvehx", void_ftype_v8hi_long_pvoid, ALTIVEC_BUILTIN_STVEHX);
13094 def_builtin ("__builtin_vec_ld", opaque_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LD);
13095 def_builtin ("__builtin_vec_lde", opaque_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LDE);
13096 def_builtin ("__builtin_vec_ldl", opaque_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LDL);
13097 def_builtin ("__builtin_vec_lvsl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVSL);
13098 def_builtin ("__builtin_vec_lvsr", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVSR);
13099 def_builtin ("__builtin_vec_lvebx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVEBX);
13100 def_builtin ("__builtin_vec_lvehx", v8hi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVEHX);
13101 def_builtin ("__builtin_vec_lvewx", v4si_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVEWX);
13102 def_builtin ("__builtin_vec_st", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_ST);
13103 def_builtin ("__builtin_vec_ste", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_STE);
13104 def_builtin ("__builtin_vec_stl", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_STL);
13105 def_builtin ("__builtin_vec_stvewx", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_STVEWX);
13106 def_builtin ("__builtin_vec_stvebx", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_STVEBX);
13107 def_builtin ("__builtin_vec_stvehx", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_STVEHX);
13109 def_builtin ("__builtin_vsx_lxvd2x_v2df", v2df_ftype_long_pcvoid,
13110 VSX_BUILTIN_LXVD2X_V2DF);
13111 def_builtin ("__builtin_vsx_lxvd2x_v2di", v2di_ftype_long_pcvoid,
13112 VSX_BUILTIN_LXVD2X_V2DI);
13113 def_builtin ("__builtin_vsx_lxvw4x_v4sf", v4sf_ftype_long_pcvoid,
13114 VSX_BUILTIN_LXVW4X_V4SF);
13115 def_builtin ("__builtin_vsx_lxvw4x_v4si", v4si_ftype_long_pcvoid,
13116 VSX_BUILTIN_LXVW4X_V4SI);
13117 def_builtin ("__builtin_vsx_lxvw4x_v8hi", v8hi_ftype_long_pcvoid,
13118 VSX_BUILTIN_LXVW4X_V8HI);
13119 def_builtin ("__builtin_vsx_lxvw4x_v16qi", v16qi_ftype_long_pcvoid,
13120 VSX_BUILTIN_LXVW4X_V16QI);
13121 def_builtin ("__builtin_vsx_stxvd2x_v2df", void_ftype_v2df_long_pvoid,
13122 VSX_BUILTIN_STXVD2X_V2DF);
13123 def_builtin ("__builtin_vsx_stxvd2x_v2di", void_ftype_v2di_long_pvoid,
13124 VSX_BUILTIN_STXVD2X_V2DI);
13125 def_builtin ("__builtin_vsx_stxvw4x_v4sf", void_ftype_v4sf_long_pvoid,
13126 VSX_BUILTIN_STXVW4X_V4SF);
13127 def_builtin ("__builtin_vsx_stxvw4x_v4si", void_ftype_v4si_long_pvoid,
13128 VSX_BUILTIN_STXVW4X_V4SI);
13129 def_builtin ("__builtin_vsx_stxvw4x_v8hi", void_ftype_v8hi_long_pvoid,
13130 VSX_BUILTIN_STXVW4X_V8HI);
13131 def_builtin ("__builtin_vsx_stxvw4x_v16qi", void_ftype_v16qi_long_pvoid,
13132 VSX_BUILTIN_STXVW4X_V16QI);
13133 def_builtin ("__builtin_vec_vsx_ld", opaque_ftype_long_pcvoid,
13134 VSX_BUILTIN_VEC_LD);
13135 def_builtin ("__builtin_vec_vsx_st", void_ftype_opaque_long_pvoid,
13136 VSX_BUILTIN_VEC_ST);
13138 def_builtin ("__builtin_vec_step", int_ftype_opaque, ALTIVEC_BUILTIN_VEC_STEP);
13139 def_builtin ("__builtin_vec_splats", opaque_ftype_opaque, ALTIVEC_BUILTIN_VEC_SPLATS);
13140 def_builtin ("__builtin_vec_promote", opaque_ftype_opaque, ALTIVEC_BUILTIN_VEC_PROMOTE);
13142 def_builtin ("__builtin_vec_sld", opaque_ftype_opaque_opaque_int, ALTIVEC_BUILTIN_VEC_SLD);
13143 def_builtin ("__builtin_vec_splat", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_SPLAT);
13144 def_builtin ("__builtin_vec_extract", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_EXTRACT);
13145 def_builtin ("__builtin_vec_insert", opaque_ftype_opaque_opaque_int, ALTIVEC_BUILTIN_VEC_INSERT);
13146 def_builtin ("__builtin_vec_vspltw", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_VSPLTW);
13147 def_builtin ("__builtin_vec_vsplth", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_VSPLTH);
13148 def_builtin ("__builtin_vec_vspltb", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_VSPLTB);
13149 def_builtin ("__builtin_vec_ctf", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_CTF);
13150 def_builtin ("__builtin_vec_vcfsx", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_VCFSX);
13151 def_builtin ("__builtin_vec_vcfux", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_VCFUX);
13152 def_builtin ("__builtin_vec_cts", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_CTS);
13153 def_builtin ("__builtin_vec_ctu", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_CTU);
13155 /* Cell builtins. */
13156 def_builtin ("__builtin_altivec_lvlx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVLX);
13157 def_builtin ("__builtin_altivec_lvlxl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVLXL);
13158 def_builtin ("__builtin_altivec_lvrx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVRX);
13159 def_builtin ("__builtin_altivec_lvrxl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVRXL);
13161 def_builtin ("__builtin_vec_lvlx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVLX);
13162 def_builtin ("__builtin_vec_lvlxl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVLXL);
13163 def_builtin ("__builtin_vec_lvrx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVRX);
13164 def_builtin ("__builtin_vec_lvrxl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVRXL);
13166 def_builtin ("__builtin_altivec_stvlx", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_STVLX);
13167 def_builtin ("__builtin_altivec_stvlxl", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_STVLXL);
13168 def_builtin ("__builtin_altivec_stvrx", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_STVRX);
13169 def_builtin ("__builtin_altivec_stvrxl", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_STVRXL);
13171 def_builtin ("__builtin_vec_stvlx", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_VEC_STVLX);
13172 def_builtin ("__builtin_vec_stvlxl", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_VEC_STVLXL);
13173 def_builtin ("__builtin_vec_stvrx", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_VEC_STVRX);
13174 def_builtin ("__builtin_vec_stvrxl", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_VEC_STVRXL);
13176 /* Add the DST variants. */
13177 d = bdesc_dst;
13178 for (i = 0; i < ARRAY_SIZE (bdesc_dst); i++, d++)
13179 def_builtin (d->name, void_ftype_pcvoid_int_int, d->code);
13181 /* Initialize the predicates. */
13182 d = bdesc_altivec_preds;
13183 for (i = 0; i < ARRAY_SIZE (bdesc_altivec_preds); i++, d++)
13185 enum machine_mode mode1;
13186 tree type;
13188 if (rs6000_overloaded_builtin_p (d->code))
13189 mode1 = VOIDmode;
13190 else
13191 mode1 = insn_data[d->icode].operand[1].mode;
13193 switch (mode1)
13195 case VOIDmode:
13196 type = int_ftype_int_opaque_opaque;
13197 break;
13198 case V2DImode:
13199 type = int_ftype_int_v2di_v2di;
13200 break;
13201 case V4SImode:
13202 type = int_ftype_int_v4si_v4si;
13203 break;
13204 case V8HImode:
13205 type = int_ftype_int_v8hi_v8hi;
13206 break;
13207 case V16QImode:
13208 type = int_ftype_int_v16qi_v16qi;
13209 break;
13210 case V4SFmode:
13211 type = int_ftype_int_v4sf_v4sf;
13212 break;
13213 case V2DFmode:
13214 type = int_ftype_int_v2df_v2df;
13215 break;
13216 default:
13217 gcc_unreachable ();
13220 def_builtin (d->name, type, d->code);
13223 /* Initialize the abs* operators. */
13224 d = bdesc_abs;
13225 for (i = 0; i < ARRAY_SIZE (bdesc_abs); i++, d++)
13227 enum machine_mode mode0;
13228 tree type;
13230 mode0 = insn_data[d->icode].operand[0].mode;
13232 switch (mode0)
13234 case V2DImode:
13235 type = v2di_ftype_v2di;
13236 break;
13237 case V4SImode:
13238 type = v4si_ftype_v4si;
13239 break;
13240 case V8HImode:
13241 type = v8hi_ftype_v8hi;
13242 break;
13243 case V16QImode:
13244 type = v16qi_ftype_v16qi;
13245 break;
13246 case V4SFmode:
13247 type = v4sf_ftype_v4sf;
13248 break;
13249 case V2DFmode:
13250 type = v2df_ftype_v2df;
13251 break;
13252 default:
13253 gcc_unreachable ();
13256 def_builtin (d->name, type, d->code);
13259 /* Initialize target builtin that implements
13260 targetm.vectorize.builtin_mask_for_load. */
13262 decl = add_builtin_function ("__builtin_altivec_mask_for_load",
13263 v16qi_ftype_long_pcvoid,
13264 ALTIVEC_BUILTIN_MASK_FOR_LOAD,
13265 BUILT_IN_MD, NULL, NULL_TREE);
13266 TREE_READONLY (decl) = 1;
13267 /* Record the decl. Will be used by rs6000_builtin_mask_for_load. */
13268 altivec_builtin_mask_for_load = decl;
13270 /* Access to the vec_init patterns. */
13271 ftype = build_function_type_list (V4SI_type_node, integer_type_node,
13272 integer_type_node, integer_type_node,
13273 integer_type_node, NULL_TREE);
13274 def_builtin ("__builtin_vec_init_v4si", ftype, ALTIVEC_BUILTIN_VEC_INIT_V4SI);
13276 ftype = build_function_type_list (V8HI_type_node, short_integer_type_node,
13277 short_integer_type_node,
13278 short_integer_type_node,
13279 short_integer_type_node,
13280 short_integer_type_node,
13281 short_integer_type_node,
13282 short_integer_type_node,
13283 short_integer_type_node, NULL_TREE);
13284 def_builtin ("__builtin_vec_init_v8hi", ftype, ALTIVEC_BUILTIN_VEC_INIT_V8HI);
13286 ftype = build_function_type_list (V16QI_type_node, char_type_node,
13287 char_type_node, char_type_node,
13288 char_type_node, char_type_node,
13289 char_type_node, char_type_node,
13290 char_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, NULL_TREE);
13295 def_builtin ("__builtin_vec_init_v16qi", ftype,
13296 ALTIVEC_BUILTIN_VEC_INIT_V16QI);
13298 ftype = build_function_type_list (V4SF_type_node, float_type_node,
13299 float_type_node, float_type_node,
13300 float_type_node, NULL_TREE);
13301 def_builtin ("__builtin_vec_init_v4sf", ftype, ALTIVEC_BUILTIN_VEC_INIT_V4SF);
13303 /* VSX builtins. */
13304 ftype = build_function_type_list (V2DF_type_node, double_type_node,
13305 double_type_node, NULL_TREE);
13306 def_builtin ("__builtin_vec_init_v2df", ftype, VSX_BUILTIN_VEC_INIT_V2DF);
13308 ftype = build_function_type_list (V2DI_type_node, intDI_type_node,
13309 intDI_type_node, NULL_TREE);
13310 def_builtin ("__builtin_vec_init_v2di", ftype, VSX_BUILTIN_VEC_INIT_V2DI);
13312 /* Access to the vec_set patterns. */
13313 ftype = build_function_type_list (V4SI_type_node, V4SI_type_node,
13314 intSI_type_node,
13315 integer_type_node, NULL_TREE);
13316 def_builtin ("__builtin_vec_set_v4si", ftype, ALTIVEC_BUILTIN_VEC_SET_V4SI);
13318 ftype = build_function_type_list (V8HI_type_node, V8HI_type_node,
13319 intHI_type_node,
13320 integer_type_node, NULL_TREE);
13321 def_builtin ("__builtin_vec_set_v8hi", ftype, ALTIVEC_BUILTIN_VEC_SET_V8HI);
13323 ftype = build_function_type_list (V16QI_type_node, V16QI_type_node,
13324 intQI_type_node,
13325 integer_type_node, NULL_TREE);
13326 def_builtin ("__builtin_vec_set_v16qi", ftype, ALTIVEC_BUILTIN_VEC_SET_V16QI);
13328 ftype = build_function_type_list (V4SF_type_node, V4SF_type_node,
13329 float_type_node,
13330 integer_type_node, NULL_TREE);
13331 def_builtin ("__builtin_vec_set_v4sf", ftype, ALTIVEC_BUILTIN_VEC_SET_V4SF);
13333 ftype = build_function_type_list (V2DF_type_node, V2DF_type_node,
13334 double_type_node,
13335 integer_type_node, NULL_TREE);
13336 def_builtin ("__builtin_vec_set_v2df", ftype, VSX_BUILTIN_VEC_SET_V2DF);
13338 ftype = build_function_type_list (V2DI_type_node, V2DI_type_node,
13339 intDI_type_node,
13340 integer_type_node, NULL_TREE);
13341 def_builtin ("__builtin_vec_set_v2di", ftype, VSX_BUILTIN_VEC_SET_V2DI);
13343 /* Access to the vec_extract patterns. */
13344 ftype = build_function_type_list (intSI_type_node, V4SI_type_node,
13345 integer_type_node, NULL_TREE);
13346 def_builtin ("__builtin_vec_ext_v4si", ftype, ALTIVEC_BUILTIN_VEC_EXT_V4SI);
13348 ftype = build_function_type_list (intHI_type_node, V8HI_type_node,
13349 integer_type_node, NULL_TREE);
13350 def_builtin ("__builtin_vec_ext_v8hi", ftype, ALTIVEC_BUILTIN_VEC_EXT_V8HI);
13352 ftype = build_function_type_list (intQI_type_node, V16QI_type_node,
13353 integer_type_node, NULL_TREE);
13354 def_builtin ("__builtin_vec_ext_v16qi", ftype, ALTIVEC_BUILTIN_VEC_EXT_V16QI);
13356 ftype = build_function_type_list (float_type_node, V4SF_type_node,
13357 integer_type_node, NULL_TREE);
13358 def_builtin ("__builtin_vec_ext_v4sf", ftype, ALTIVEC_BUILTIN_VEC_EXT_V4SF);
13360 ftype = build_function_type_list (double_type_node, V2DF_type_node,
13361 integer_type_node, NULL_TREE);
13362 def_builtin ("__builtin_vec_ext_v2df", ftype, VSX_BUILTIN_VEC_EXT_V2DF);
13364 ftype = build_function_type_list (intDI_type_node, V2DI_type_node,
13365 integer_type_node, NULL_TREE);
13366 def_builtin ("__builtin_vec_ext_v2di", ftype, VSX_BUILTIN_VEC_EXT_V2DI);
13369 static void
13370 htm_init_builtins (void)
13372 HOST_WIDE_INT builtin_mask = rs6000_builtin_mask;
13373 const struct builtin_description *d;
13374 size_t i;
13376 d = bdesc_htm;
13377 for (i = 0; i < ARRAY_SIZE (bdesc_htm); i++, d++)
13379 tree op[MAX_HTM_OPERANDS], type;
13380 HOST_WIDE_INT mask = d->mask;
13381 unsigned attr = rs6000_builtin_info[d->code].attr;
13382 bool void_func = (attr & RS6000_BTC_VOID);
13383 int attr_args = (attr & RS6000_BTC_TYPE_MASK);
13384 int nopnds = 0;
13385 tree argtype = (attr & RS6000_BTC_SPR) ? long_unsigned_type_node
13386 : unsigned_type_node;
13388 if ((mask & builtin_mask) != mask)
13390 if (TARGET_DEBUG_BUILTIN)
13391 fprintf (stderr, "htm_builtin, skip binary %s\n", d->name);
13392 continue;
13395 if (d->name == 0)
13397 if (TARGET_DEBUG_BUILTIN)
13398 fprintf (stderr, "htm_builtin, bdesc_htm[%ld] no name\n",
13399 (long unsigned) i);
13400 continue;
13403 op[nopnds++] = (void_func) ? void_type_node : argtype;
13405 if (attr_args == RS6000_BTC_UNARY)
13406 op[nopnds++] = argtype;
13407 else if (attr_args == RS6000_BTC_BINARY)
13409 op[nopnds++] = argtype;
13410 op[nopnds++] = argtype;
13412 else if (attr_args == RS6000_BTC_TERNARY)
13414 op[nopnds++] = argtype;
13415 op[nopnds++] = argtype;
13416 op[nopnds++] = argtype;
13419 switch (nopnds)
13421 case 1:
13422 type = build_function_type_list (op[0], NULL_TREE);
13423 break;
13424 case 2:
13425 type = build_function_type_list (op[0], op[1], NULL_TREE);
13426 break;
13427 case 3:
13428 type = build_function_type_list (op[0], op[1], op[2], NULL_TREE);
13429 break;
13430 case 4:
13431 type = build_function_type_list (op[0], op[1], op[2], op[3],
13432 NULL_TREE);
13433 break;
13434 default:
13435 gcc_unreachable ();
13438 def_builtin (d->name, type, d->code);
13442 /* Hash function for builtin functions with up to 3 arguments and a return
13443 type. */
13444 static unsigned
13445 builtin_hash_function (const void *hash_entry)
13447 unsigned ret = 0;
13448 int i;
13449 const struct builtin_hash_struct *bh =
13450 (const struct builtin_hash_struct *) hash_entry;
13452 for (i = 0; i < 4; i++)
13454 ret = (ret * (unsigned)MAX_MACHINE_MODE) + ((unsigned)bh->mode[i]);
13455 ret = (ret * 2) + bh->uns_p[i];
13458 return ret;
13461 /* Compare builtin hash entries H1 and H2 for equivalence. */
13462 static int
13463 builtin_hash_eq (const void *h1, const void *h2)
13465 const struct builtin_hash_struct *p1 = (const struct builtin_hash_struct *) h1;
13466 const struct builtin_hash_struct *p2 = (const struct builtin_hash_struct *) h2;
13468 return ((p1->mode[0] == p2->mode[0])
13469 && (p1->mode[1] == p2->mode[1])
13470 && (p1->mode[2] == p2->mode[2])
13471 && (p1->mode[3] == p2->mode[3])
13472 && (p1->uns_p[0] == p2->uns_p[0])
13473 && (p1->uns_p[1] == p2->uns_p[1])
13474 && (p1->uns_p[2] == p2->uns_p[2])
13475 && (p1->uns_p[3] == p2->uns_p[3]));
13478 /* Map types for builtin functions with an explicit return type and up to 3
13479 arguments. Functions with fewer than 3 arguments use VOIDmode as the type
13480 of the argument. */
13481 static tree
13482 builtin_function_type (enum machine_mode mode_ret, enum machine_mode mode_arg0,
13483 enum machine_mode mode_arg1, enum machine_mode mode_arg2,
13484 enum rs6000_builtins builtin, const char *name)
13486 struct builtin_hash_struct h;
13487 struct builtin_hash_struct *h2;
13488 void **found;
13489 int num_args = 3;
13490 int i;
13491 tree ret_type = NULL_TREE;
13492 tree arg_type[3] = { NULL_TREE, NULL_TREE, NULL_TREE };
13494 /* Create builtin_hash_table. */
13495 if (builtin_hash_table == NULL)
13496 builtin_hash_table = htab_create_ggc (1500, builtin_hash_function,
13497 builtin_hash_eq, NULL);
13499 h.type = NULL_TREE;
13500 h.mode[0] = mode_ret;
13501 h.mode[1] = mode_arg0;
13502 h.mode[2] = mode_arg1;
13503 h.mode[3] = mode_arg2;
13504 h.uns_p[0] = 0;
13505 h.uns_p[1] = 0;
13506 h.uns_p[2] = 0;
13507 h.uns_p[3] = 0;
13509 /* If the builtin is a type that produces unsigned results or takes unsigned
13510 arguments, and it is returned as a decl for the vectorizer (such as
13511 widening multiplies, permute), make sure the arguments and return value
13512 are type correct. */
13513 switch (builtin)
13515 /* unsigned 1 argument functions. */
13516 case CRYPTO_BUILTIN_VSBOX:
13517 case P8V_BUILTIN_VGBBD:
13518 h.uns_p[0] = 1;
13519 h.uns_p[1] = 1;
13520 break;
13522 /* unsigned 2 argument functions. */
13523 case ALTIVEC_BUILTIN_VMULEUB_UNS:
13524 case ALTIVEC_BUILTIN_VMULEUH_UNS:
13525 case ALTIVEC_BUILTIN_VMULOUB_UNS:
13526 case ALTIVEC_BUILTIN_VMULOUH_UNS:
13527 case CRYPTO_BUILTIN_VCIPHER:
13528 case CRYPTO_BUILTIN_VCIPHERLAST:
13529 case CRYPTO_BUILTIN_VNCIPHER:
13530 case CRYPTO_BUILTIN_VNCIPHERLAST:
13531 case CRYPTO_BUILTIN_VPMSUMB:
13532 case CRYPTO_BUILTIN_VPMSUMH:
13533 case CRYPTO_BUILTIN_VPMSUMW:
13534 case CRYPTO_BUILTIN_VPMSUMD:
13535 case CRYPTO_BUILTIN_VPMSUM:
13536 h.uns_p[0] = 1;
13537 h.uns_p[1] = 1;
13538 h.uns_p[2] = 1;
13539 break;
13541 /* unsigned 3 argument functions. */
13542 case ALTIVEC_BUILTIN_VPERM_16QI_UNS:
13543 case ALTIVEC_BUILTIN_VPERM_8HI_UNS:
13544 case ALTIVEC_BUILTIN_VPERM_4SI_UNS:
13545 case ALTIVEC_BUILTIN_VPERM_2DI_UNS:
13546 case ALTIVEC_BUILTIN_VSEL_16QI_UNS:
13547 case ALTIVEC_BUILTIN_VSEL_8HI_UNS:
13548 case ALTIVEC_BUILTIN_VSEL_4SI_UNS:
13549 case ALTIVEC_BUILTIN_VSEL_2DI_UNS:
13550 case VSX_BUILTIN_VPERM_16QI_UNS:
13551 case VSX_BUILTIN_VPERM_8HI_UNS:
13552 case VSX_BUILTIN_VPERM_4SI_UNS:
13553 case VSX_BUILTIN_VPERM_2DI_UNS:
13554 case VSX_BUILTIN_XXSEL_16QI_UNS:
13555 case VSX_BUILTIN_XXSEL_8HI_UNS:
13556 case VSX_BUILTIN_XXSEL_4SI_UNS:
13557 case VSX_BUILTIN_XXSEL_2DI_UNS:
13558 case CRYPTO_BUILTIN_VPERMXOR:
13559 case CRYPTO_BUILTIN_VPERMXOR_V2DI:
13560 case CRYPTO_BUILTIN_VPERMXOR_V4SI:
13561 case CRYPTO_BUILTIN_VPERMXOR_V8HI:
13562 case CRYPTO_BUILTIN_VPERMXOR_V16QI:
13563 case CRYPTO_BUILTIN_VSHASIGMAW:
13564 case CRYPTO_BUILTIN_VSHASIGMAD:
13565 case CRYPTO_BUILTIN_VSHASIGMA:
13566 h.uns_p[0] = 1;
13567 h.uns_p[1] = 1;
13568 h.uns_p[2] = 1;
13569 h.uns_p[3] = 1;
13570 break;
13572 /* signed permute functions with unsigned char mask. */
13573 case ALTIVEC_BUILTIN_VPERM_16QI:
13574 case ALTIVEC_BUILTIN_VPERM_8HI:
13575 case ALTIVEC_BUILTIN_VPERM_4SI:
13576 case ALTIVEC_BUILTIN_VPERM_4SF:
13577 case ALTIVEC_BUILTIN_VPERM_2DI:
13578 case ALTIVEC_BUILTIN_VPERM_2DF:
13579 case VSX_BUILTIN_VPERM_16QI:
13580 case VSX_BUILTIN_VPERM_8HI:
13581 case VSX_BUILTIN_VPERM_4SI:
13582 case VSX_BUILTIN_VPERM_4SF:
13583 case VSX_BUILTIN_VPERM_2DI:
13584 case VSX_BUILTIN_VPERM_2DF:
13585 h.uns_p[3] = 1;
13586 break;
13588 /* unsigned args, signed return. */
13589 case VSX_BUILTIN_XVCVUXDDP_UNS:
13590 case ALTIVEC_BUILTIN_UNSFLOAT_V4SI_V4SF:
13591 h.uns_p[1] = 1;
13592 break;
13594 /* signed args, unsigned return. */
13595 case VSX_BUILTIN_XVCVDPUXDS_UNS:
13596 case ALTIVEC_BUILTIN_FIXUNS_V4SF_V4SI:
13597 h.uns_p[0] = 1;
13598 break;
13600 default:
13601 break;
13604 /* Figure out how many args are present. */
13605 while (num_args > 0 && h.mode[num_args] == VOIDmode)
13606 num_args--;
13608 if (num_args == 0)
13609 fatal_error ("internal error: builtin function %s had no type", name);
13611 ret_type = builtin_mode_to_type[h.mode[0]][h.uns_p[0]];
13612 if (!ret_type && h.uns_p[0])
13613 ret_type = builtin_mode_to_type[h.mode[0]][0];
13615 if (!ret_type)
13616 fatal_error ("internal error: builtin function %s had an unexpected "
13617 "return type %s", name, GET_MODE_NAME (h.mode[0]));
13619 for (i = 0; i < (int) ARRAY_SIZE (arg_type); i++)
13620 arg_type[i] = NULL_TREE;
13622 for (i = 0; i < num_args; i++)
13624 int m = (int) h.mode[i+1];
13625 int uns_p = h.uns_p[i+1];
13627 arg_type[i] = builtin_mode_to_type[m][uns_p];
13628 if (!arg_type[i] && uns_p)
13629 arg_type[i] = builtin_mode_to_type[m][0];
13631 if (!arg_type[i])
13632 fatal_error ("internal error: builtin function %s, argument %d "
13633 "had unexpected argument type %s", name, i,
13634 GET_MODE_NAME (m));
13637 found = htab_find_slot (builtin_hash_table, &h, INSERT);
13638 if (*found == NULL)
13640 h2 = ggc_alloc_builtin_hash_struct ();
13641 *h2 = h;
13642 *found = (void *)h2;
13644 h2->type = build_function_type_list (ret_type, arg_type[0], arg_type[1],
13645 arg_type[2], NULL_TREE);
13648 return ((struct builtin_hash_struct *)(*found))->type;
13651 static void
13652 rs6000_common_init_builtins (void)
13654 const struct builtin_description *d;
13655 size_t i;
13657 tree opaque_ftype_opaque = NULL_TREE;
13658 tree opaque_ftype_opaque_opaque = NULL_TREE;
13659 tree opaque_ftype_opaque_opaque_opaque = NULL_TREE;
13660 tree v2si_ftype_qi = NULL_TREE;
13661 tree v2si_ftype_v2si_qi = NULL_TREE;
13662 tree v2si_ftype_int_qi = NULL_TREE;
13663 HOST_WIDE_INT builtin_mask = rs6000_builtin_mask;
13665 if (!TARGET_PAIRED_FLOAT)
13667 builtin_mode_to_type[V2SImode][0] = opaque_V2SI_type_node;
13668 builtin_mode_to_type[V2SFmode][0] = opaque_V2SF_type_node;
13671 /* Paired and SPE builtins are only available if you build a compiler with
13672 the appropriate options, so only create those builtins with the
13673 appropriate compiler option. Create Altivec and VSX builtins on machines
13674 with at least the general purpose extensions (970 and newer) to allow the
13675 use of the target attribute.. */
13677 if (TARGET_EXTRA_BUILTINS)
13678 builtin_mask |= RS6000_BTM_COMMON;
13680 /* Add the ternary operators. */
13681 d = bdesc_3arg;
13682 for (i = 0; i < ARRAY_SIZE (bdesc_3arg); i++, d++)
13684 tree type;
13685 HOST_WIDE_INT mask = d->mask;
13687 if ((mask & builtin_mask) != mask)
13689 if (TARGET_DEBUG_BUILTIN)
13690 fprintf (stderr, "rs6000_builtin, skip ternary %s\n", d->name);
13691 continue;
13694 if (rs6000_overloaded_builtin_p (d->code))
13696 if (! (type = opaque_ftype_opaque_opaque_opaque))
13697 type = opaque_ftype_opaque_opaque_opaque
13698 = build_function_type_list (opaque_V4SI_type_node,
13699 opaque_V4SI_type_node,
13700 opaque_V4SI_type_node,
13701 opaque_V4SI_type_node,
13702 NULL_TREE);
13704 else
13706 enum insn_code icode = d->icode;
13707 if (d->name == 0)
13709 if (TARGET_DEBUG_BUILTIN)
13710 fprintf (stderr, "rs6000_builtin, bdesc_3arg[%ld] no name\n",
13711 (long unsigned)i);
13713 continue;
13716 if (icode == CODE_FOR_nothing)
13718 if (TARGET_DEBUG_BUILTIN)
13719 fprintf (stderr, "rs6000_builtin, skip ternary %s (no code)\n",
13720 d->name);
13722 continue;
13725 type = builtin_function_type (insn_data[icode].operand[0].mode,
13726 insn_data[icode].operand[1].mode,
13727 insn_data[icode].operand[2].mode,
13728 insn_data[icode].operand[3].mode,
13729 d->code, d->name);
13732 def_builtin (d->name, type, d->code);
13735 /* Add the binary operators. */
13736 d = bdesc_2arg;
13737 for (i = 0; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
13739 enum machine_mode mode0, mode1, mode2;
13740 tree type;
13741 HOST_WIDE_INT mask = d->mask;
13743 if ((mask & builtin_mask) != mask)
13745 if (TARGET_DEBUG_BUILTIN)
13746 fprintf (stderr, "rs6000_builtin, skip binary %s\n", d->name);
13747 continue;
13750 if (rs6000_overloaded_builtin_p (d->code))
13752 if (! (type = opaque_ftype_opaque_opaque))
13753 type = opaque_ftype_opaque_opaque
13754 = build_function_type_list (opaque_V4SI_type_node,
13755 opaque_V4SI_type_node,
13756 opaque_V4SI_type_node,
13757 NULL_TREE);
13759 else
13761 enum insn_code icode = d->icode;
13762 if (d->name == 0)
13764 if (TARGET_DEBUG_BUILTIN)
13765 fprintf (stderr, "rs6000_builtin, bdesc_2arg[%ld] no name\n",
13766 (long unsigned)i);
13768 continue;
13771 if (icode == CODE_FOR_nothing)
13773 if (TARGET_DEBUG_BUILTIN)
13774 fprintf (stderr, "rs6000_builtin, skip binary %s (no code)\n",
13775 d->name);
13777 continue;
13780 mode0 = insn_data[icode].operand[0].mode;
13781 mode1 = insn_data[icode].operand[1].mode;
13782 mode2 = insn_data[icode].operand[2].mode;
13784 if (mode0 == V2SImode && mode1 == V2SImode && mode2 == QImode)
13786 if (! (type = v2si_ftype_v2si_qi))
13787 type = v2si_ftype_v2si_qi
13788 = build_function_type_list (opaque_V2SI_type_node,
13789 opaque_V2SI_type_node,
13790 char_type_node,
13791 NULL_TREE);
13794 else if (mode0 == V2SImode && GET_MODE_CLASS (mode1) == MODE_INT
13795 && mode2 == QImode)
13797 if (! (type = v2si_ftype_int_qi))
13798 type = v2si_ftype_int_qi
13799 = build_function_type_list (opaque_V2SI_type_node,
13800 integer_type_node,
13801 char_type_node,
13802 NULL_TREE);
13805 else
13806 type = builtin_function_type (mode0, mode1, mode2, VOIDmode,
13807 d->code, d->name);
13810 def_builtin (d->name, type, d->code);
13813 /* Add the simple unary operators. */
13814 d = bdesc_1arg;
13815 for (i = 0; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
13817 enum machine_mode mode0, mode1;
13818 tree type;
13819 HOST_WIDE_INT mask = d->mask;
13821 if ((mask & builtin_mask) != mask)
13823 if (TARGET_DEBUG_BUILTIN)
13824 fprintf (stderr, "rs6000_builtin, skip unary %s\n", d->name);
13825 continue;
13828 if (rs6000_overloaded_builtin_p (d->code))
13830 if (! (type = opaque_ftype_opaque))
13831 type = opaque_ftype_opaque
13832 = build_function_type_list (opaque_V4SI_type_node,
13833 opaque_V4SI_type_node,
13834 NULL_TREE);
13836 else
13838 enum insn_code icode = d->icode;
13839 if (d->name == 0)
13841 if (TARGET_DEBUG_BUILTIN)
13842 fprintf (stderr, "rs6000_builtin, bdesc_1arg[%ld] no name\n",
13843 (long unsigned)i);
13845 continue;
13848 if (icode == CODE_FOR_nothing)
13850 if (TARGET_DEBUG_BUILTIN)
13851 fprintf (stderr, "rs6000_builtin, skip unary %s (no code)\n",
13852 d->name);
13854 continue;
13857 mode0 = insn_data[icode].operand[0].mode;
13858 mode1 = insn_data[icode].operand[1].mode;
13860 if (mode0 == V2SImode && mode1 == QImode)
13862 if (! (type = v2si_ftype_qi))
13863 type = v2si_ftype_qi
13864 = build_function_type_list (opaque_V2SI_type_node,
13865 char_type_node,
13866 NULL_TREE);
13869 else
13870 type = builtin_function_type (mode0, mode1, VOIDmode, VOIDmode,
13871 d->code, d->name);
13874 def_builtin (d->name, type, d->code);
13878 static void
13879 rs6000_init_libfuncs (void)
13881 if (!TARGET_IEEEQUAD)
13882 /* AIX/Darwin/64-bit Linux quad floating point routines. */
13883 if (!TARGET_XL_COMPAT)
13885 set_optab_libfunc (add_optab, TFmode, "__gcc_qadd");
13886 set_optab_libfunc (sub_optab, TFmode, "__gcc_qsub");
13887 set_optab_libfunc (smul_optab, TFmode, "__gcc_qmul");
13888 set_optab_libfunc (sdiv_optab, TFmode, "__gcc_qdiv");
13890 if (!(TARGET_HARD_FLOAT && (TARGET_FPRS || TARGET_E500_DOUBLE)))
13892 set_optab_libfunc (neg_optab, TFmode, "__gcc_qneg");
13893 set_optab_libfunc (eq_optab, TFmode, "__gcc_qeq");
13894 set_optab_libfunc (ne_optab, TFmode, "__gcc_qne");
13895 set_optab_libfunc (gt_optab, TFmode, "__gcc_qgt");
13896 set_optab_libfunc (ge_optab, TFmode, "__gcc_qge");
13897 set_optab_libfunc (lt_optab, TFmode, "__gcc_qlt");
13898 set_optab_libfunc (le_optab, TFmode, "__gcc_qle");
13900 set_conv_libfunc (sext_optab, TFmode, SFmode, "__gcc_stoq");
13901 set_conv_libfunc (sext_optab, TFmode, DFmode, "__gcc_dtoq");
13902 set_conv_libfunc (trunc_optab, SFmode, TFmode, "__gcc_qtos");
13903 set_conv_libfunc (trunc_optab, DFmode, TFmode, "__gcc_qtod");
13904 set_conv_libfunc (sfix_optab, SImode, TFmode, "__gcc_qtoi");
13905 set_conv_libfunc (ufix_optab, SImode, TFmode, "__gcc_qtou");
13906 set_conv_libfunc (sfloat_optab, TFmode, SImode, "__gcc_itoq");
13907 set_conv_libfunc (ufloat_optab, TFmode, SImode, "__gcc_utoq");
13910 if (!(TARGET_HARD_FLOAT && TARGET_FPRS))
13911 set_optab_libfunc (unord_optab, TFmode, "__gcc_qunord");
13913 else
13915 set_optab_libfunc (add_optab, TFmode, "_xlqadd");
13916 set_optab_libfunc (sub_optab, TFmode, "_xlqsub");
13917 set_optab_libfunc (smul_optab, TFmode, "_xlqmul");
13918 set_optab_libfunc (sdiv_optab, TFmode, "_xlqdiv");
13920 else
13922 /* 32-bit SVR4 quad floating point routines. */
13924 set_optab_libfunc (add_optab, TFmode, "_q_add");
13925 set_optab_libfunc (sub_optab, TFmode, "_q_sub");
13926 set_optab_libfunc (neg_optab, TFmode, "_q_neg");
13927 set_optab_libfunc (smul_optab, TFmode, "_q_mul");
13928 set_optab_libfunc (sdiv_optab, TFmode, "_q_div");
13929 if (TARGET_PPC_GPOPT)
13930 set_optab_libfunc (sqrt_optab, TFmode, "_q_sqrt");
13932 set_optab_libfunc (eq_optab, TFmode, "_q_feq");
13933 set_optab_libfunc (ne_optab, TFmode, "_q_fne");
13934 set_optab_libfunc (gt_optab, TFmode, "_q_fgt");
13935 set_optab_libfunc (ge_optab, TFmode, "_q_fge");
13936 set_optab_libfunc (lt_optab, TFmode, "_q_flt");
13937 set_optab_libfunc (le_optab, TFmode, "_q_fle");
13939 set_conv_libfunc (sext_optab, TFmode, SFmode, "_q_stoq");
13940 set_conv_libfunc (sext_optab, TFmode, DFmode, "_q_dtoq");
13941 set_conv_libfunc (trunc_optab, SFmode, TFmode, "_q_qtos");
13942 set_conv_libfunc (trunc_optab, DFmode, TFmode, "_q_qtod");
13943 set_conv_libfunc (sfix_optab, SImode, TFmode, "_q_qtoi");
13944 set_conv_libfunc (ufix_optab, SImode, TFmode, "_q_qtou");
13945 set_conv_libfunc (sfloat_optab, TFmode, SImode, "_q_itoq");
13946 set_conv_libfunc (ufloat_optab, TFmode, SImode, "_q_utoq");
13951 /* Expand a block clear operation, and return 1 if successful. Return 0
13952 if we should let the compiler generate normal code.
13954 operands[0] is the destination
13955 operands[1] is the length
13956 operands[3] is the alignment */
13959 expand_block_clear (rtx operands[])
13961 rtx orig_dest = operands[0];
13962 rtx bytes_rtx = operands[1];
13963 rtx align_rtx = operands[3];
13964 bool constp = (GET_CODE (bytes_rtx) == CONST_INT);
13965 HOST_WIDE_INT align;
13966 HOST_WIDE_INT bytes;
13967 int offset;
13968 int clear_bytes;
13969 int clear_step;
13971 /* If this is not a fixed size move, just call memcpy */
13972 if (! constp)
13973 return 0;
13975 /* This must be a fixed size alignment */
13976 gcc_assert (GET_CODE (align_rtx) == CONST_INT);
13977 align = INTVAL (align_rtx) * BITS_PER_UNIT;
13979 /* Anything to clear? */
13980 bytes = INTVAL (bytes_rtx);
13981 if (bytes <= 0)
13982 return 1;
13984 /* Use the builtin memset after a point, to avoid huge code bloat.
13985 When optimize_size, avoid any significant code bloat; calling
13986 memset is about 4 instructions, so allow for one instruction to
13987 load zero and three to do clearing. */
13988 if (TARGET_ALTIVEC && align >= 128)
13989 clear_step = 16;
13990 else if (TARGET_POWERPC64 && align >= 32)
13991 clear_step = 8;
13992 else if (TARGET_SPE && align >= 64)
13993 clear_step = 8;
13994 else
13995 clear_step = 4;
13997 if (optimize_size && bytes > 3 * clear_step)
13998 return 0;
13999 if (! optimize_size && bytes > 8 * clear_step)
14000 return 0;
14002 for (offset = 0; bytes > 0; offset += clear_bytes, bytes -= clear_bytes)
14004 enum machine_mode mode = BLKmode;
14005 rtx dest;
14007 if (bytes >= 16 && TARGET_ALTIVEC && align >= 128)
14009 clear_bytes = 16;
14010 mode = V4SImode;
14012 else if (bytes >= 8 && TARGET_SPE && align >= 64)
14014 clear_bytes = 8;
14015 mode = V2SImode;
14017 else if (bytes >= 8 && TARGET_POWERPC64
14018 /* 64-bit loads and stores require word-aligned
14019 displacements. */
14020 && (align >= 64 || (!STRICT_ALIGNMENT && align >= 32)))
14022 clear_bytes = 8;
14023 mode = DImode;
14025 else if (bytes >= 4 && (align >= 32 || !STRICT_ALIGNMENT))
14026 { /* move 4 bytes */
14027 clear_bytes = 4;
14028 mode = SImode;
14030 else if (bytes >= 2 && (align >= 16 || !STRICT_ALIGNMENT))
14031 { /* move 2 bytes */
14032 clear_bytes = 2;
14033 mode = HImode;
14035 else /* move 1 byte at a time */
14037 clear_bytes = 1;
14038 mode = QImode;
14041 dest = adjust_address (orig_dest, mode, offset);
14043 emit_move_insn (dest, CONST0_RTX (mode));
14046 return 1;
14050 /* Expand a block move operation, and return 1 if successful. Return 0
14051 if we should let the compiler generate normal code.
14053 operands[0] is the destination
14054 operands[1] is the source
14055 operands[2] is the length
14056 operands[3] is the alignment */
14058 #define MAX_MOVE_REG 4
14061 expand_block_move (rtx operands[])
14063 rtx orig_dest = operands[0];
14064 rtx orig_src = operands[1];
14065 rtx bytes_rtx = operands[2];
14066 rtx align_rtx = operands[3];
14067 int constp = (GET_CODE (bytes_rtx) == CONST_INT);
14068 int align;
14069 int bytes;
14070 int offset;
14071 int move_bytes;
14072 rtx stores[MAX_MOVE_REG];
14073 int num_reg = 0;
14075 /* If this is not a fixed size move, just call memcpy */
14076 if (! constp)
14077 return 0;
14079 /* This must be a fixed size alignment */
14080 gcc_assert (GET_CODE (align_rtx) == CONST_INT);
14081 align = INTVAL (align_rtx) * BITS_PER_UNIT;
14083 /* Anything to move? */
14084 bytes = INTVAL (bytes_rtx);
14085 if (bytes <= 0)
14086 return 1;
14088 if (bytes > rs6000_block_move_inline_limit)
14089 return 0;
14091 for (offset = 0; bytes > 0; offset += move_bytes, bytes -= move_bytes)
14093 union {
14094 rtx (*movmemsi) (rtx, rtx, rtx, rtx);
14095 rtx (*mov) (rtx, rtx);
14096 } gen_func;
14097 enum machine_mode mode = BLKmode;
14098 rtx src, dest;
14100 /* Altivec first, since it will be faster than a string move
14101 when it applies, and usually not significantly larger. */
14102 if (TARGET_ALTIVEC && bytes >= 16 && align >= 128)
14104 move_bytes = 16;
14105 mode = V4SImode;
14106 gen_func.mov = gen_movv4si;
14108 else if (TARGET_SPE && bytes >= 8 && align >= 64)
14110 move_bytes = 8;
14111 mode = V2SImode;
14112 gen_func.mov = gen_movv2si;
14114 else if (TARGET_STRING
14115 && bytes > 24 /* move up to 32 bytes at a time */
14116 && ! fixed_regs[5]
14117 && ! fixed_regs[6]
14118 && ! fixed_regs[7]
14119 && ! fixed_regs[8]
14120 && ! fixed_regs[9]
14121 && ! fixed_regs[10]
14122 && ! fixed_regs[11]
14123 && ! fixed_regs[12])
14125 move_bytes = (bytes > 32) ? 32 : bytes;
14126 gen_func.movmemsi = gen_movmemsi_8reg;
14128 else if (TARGET_STRING
14129 && bytes > 16 /* move up to 24 bytes at a time */
14130 && ! fixed_regs[5]
14131 && ! fixed_regs[6]
14132 && ! fixed_regs[7]
14133 && ! fixed_regs[8]
14134 && ! fixed_regs[9]
14135 && ! fixed_regs[10])
14137 move_bytes = (bytes > 24) ? 24 : bytes;
14138 gen_func.movmemsi = gen_movmemsi_6reg;
14140 else if (TARGET_STRING
14141 && bytes > 8 /* move up to 16 bytes at a time */
14142 && ! fixed_regs[5]
14143 && ! fixed_regs[6]
14144 && ! fixed_regs[7]
14145 && ! fixed_regs[8])
14147 move_bytes = (bytes > 16) ? 16 : bytes;
14148 gen_func.movmemsi = gen_movmemsi_4reg;
14150 else if (bytes >= 8 && TARGET_POWERPC64
14151 /* 64-bit loads and stores require word-aligned
14152 displacements. */
14153 && (align >= 64 || (!STRICT_ALIGNMENT && align >= 32)))
14155 move_bytes = 8;
14156 mode = DImode;
14157 gen_func.mov = gen_movdi;
14159 else if (TARGET_STRING && bytes > 4 && !TARGET_POWERPC64)
14160 { /* move up to 8 bytes at a time */
14161 move_bytes = (bytes > 8) ? 8 : bytes;
14162 gen_func.movmemsi = gen_movmemsi_2reg;
14164 else if (bytes >= 4 && (align >= 32 || !STRICT_ALIGNMENT))
14165 { /* move 4 bytes */
14166 move_bytes = 4;
14167 mode = SImode;
14168 gen_func.mov = gen_movsi;
14170 else if (bytes >= 2 && (align >= 16 || !STRICT_ALIGNMENT))
14171 { /* move 2 bytes */
14172 move_bytes = 2;
14173 mode = HImode;
14174 gen_func.mov = gen_movhi;
14176 else if (TARGET_STRING && bytes > 1)
14177 { /* move up to 4 bytes at a time */
14178 move_bytes = (bytes > 4) ? 4 : bytes;
14179 gen_func.movmemsi = gen_movmemsi_1reg;
14181 else /* move 1 byte at a time */
14183 move_bytes = 1;
14184 mode = QImode;
14185 gen_func.mov = gen_movqi;
14188 src = adjust_address (orig_src, mode, offset);
14189 dest = adjust_address (orig_dest, mode, offset);
14191 if (mode != BLKmode)
14193 rtx tmp_reg = gen_reg_rtx (mode);
14195 emit_insn ((*gen_func.mov) (tmp_reg, src));
14196 stores[num_reg++] = (*gen_func.mov) (dest, tmp_reg);
14199 if (mode == BLKmode || num_reg >= MAX_MOVE_REG || bytes == move_bytes)
14201 int i;
14202 for (i = 0; i < num_reg; i++)
14203 emit_insn (stores[i]);
14204 num_reg = 0;
14207 if (mode == BLKmode)
14209 /* Move the address into scratch registers. The movmemsi
14210 patterns require zero offset. */
14211 if (!REG_P (XEXP (src, 0)))
14213 rtx src_reg = copy_addr_to_reg (XEXP (src, 0));
14214 src = replace_equiv_address (src, src_reg);
14216 set_mem_size (src, move_bytes);
14218 if (!REG_P (XEXP (dest, 0)))
14220 rtx dest_reg = copy_addr_to_reg (XEXP (dest, 0));
14221 dest = replace_equiv_address (dest, dest_reg);
14223 set_mem_size (dest, move_bytes);
14225 emit_insn ((*gen_func.movmemsi) (dest, src,
14226 GEN_INT (move_bytes & 31),
14227 align_rtx));
14231 return 1;
14235 /* Return a string to perform a load_multiple operation.
14236 operands[0] is the vector.
14237 operands[1] is the source address.
14238 operands[2] is the first destination register. */
14240 const char *
14241 rs6000_output_load_multiple (rtx operands[3])
14243 /* We have to handle the case where the pseudo used to contain the address
14244 is assigned to one of the output registers. */
14245 int i, j;
14246 int words = XVECLEN (operands[0], 0);
14247 rtx xop[10];
14249 if (XVECLEN (operands[0], 0) == 1)
14250 return "lwz %2,0(%1)";
14252 for (i = 0; i < words; i++)
14253 if (refers_to_regno_p (REGNO (operands[2]) + i,
14254 REGNO (operands[2]) + i + 1, operands[1], 0))
14256 if (i == words-1)
14258 xop[0] = GEN_INT (4 * (words-1));
14259 xop[1] = operands[1];
14260 xop[2] = operands[2];
14261 output_asm_insn ("lswi %2,%1,%0\n\tlwz %1,%0(%1)", xop);
14262 return "";
14264 else if (i == 0)
14266 xop[0] = GEN_INT (4 * (words-1));
14267 xop[1] = operands[1];
14268 xop[2] = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
14269 output_asm_insn ("addi %1,%1,4\n\tlswi %2,%1,%0\n\tlwz %1,-4(%1)", xop);
14270 return "";
14272 else
14274 for (j = 0; j < words; j++)
14275 if (j != i)
14277 xop[0] = GEN_INT (j * 4);
14278 xop[1] = operands[1];
14279 xop[2] = gen_rtx_REG (SImode, REGNO (operands[2]) + j);
14280 output_asm_insn ("lwz %2,%0(%1)", xop);
14282 xop[0] = GEN_INT (i * 4);
14283 xop[1] = operands[1];
14284 output_asm_insn ("lwz %1,%0(%1)", xop);
14285 return "";
14289 return "lswi %2,%1,%N0";
14293 /* A validation routine: say whether CODE, a condition code, and MODE
14294 match. The other alternatives either don't make sense or should
14295 never be generated. */
14297 void
14298 validate_condition_mode (enum rtx_code code, enum machine_mode mode)
14300 gcc_assert ((GET_RTX_CLASS (code) == RTX_COMPARE
14301 || GET_RTX_CLASS (code) == RTX_COMM_COMPARE)
14302 && GET_MODE_CLASS (mode) == MODE_CC);
14304 /* These don't make sense. */
14305 gcc_assert ((code != GT && code != LT && code != GE && code != LE)
14306 || mode != CCUNSmode);
14308 gcc_assert ((code != GTU && code != LTU && code != GEU && code != LEU)
14309 || mode == CCUNSmode);
14311 gcc_assert (mode == CCFPmode
14312 || (code != ORDERED && code != UNORDERED
14313 && code != UNEQ && code != LTGT
14314 && code != UNGT && code != UNLT
14315 && code != UNGE && code != UNLE));
14317 /* These should never be generated except for
14318 flag_finite_math_only. */
14319 gcc_assert (mode != CCFPmode
14320 || flag_finite_math_only
14321 || (code != LE && code != GE
14322 && code != UNEQ && code != LTGT
14323 && code != UNGT && code != UNLT));
14325 /* These are invalid; the information is not there. */
14326 gcc_assert (mode != CCEQmode || code == EQ || code == NE);
14330 /* Return 1 if ANDOP is a mask that has no bits on that are not in the
14331 mask required to convert the result of a rotate insn into a shift
14332 left insn of SHIFTOP bits. Both are known to be SImode CONST_INT. */
14335 includes_lshift_p (rtx shiftop, rtx andop)
14337 unsigned HOST_WIDE_INT shift_mask = ~(unsigned HOST_WIDE_INT) 0;
14339 shift_mask <<= INTVAL (shiftop);
14341 return (INTVAL (andop) & 0xffffffff & ~shift_mask) == 0;
14344 /* Similar, but for right shift. */
14347 includes_rshift_p (rtx shiftop, rtx andop)
14349 unsigned HOST_WIDE_INT shift_mask = ~(unsigned HOST_WIDE_INT) 0;
14351 shift_mask >>= INTVAL (shiftop);
14353 return (INTVAL (andop) & 0xffffffff & ~shift_mask) == 0;
14356 /* Return 1 if ANDOP is a mask suitable for use with an rldic insn
14357 to perform a left shift. It must have exactly SHIFTOP least
14358 significant 0's, then one or more 1's, then zero or more 0's. */
14361 includes_rldic_lshift_p (rtx shiftop, rtx andop)
14363 if (GET_CODE (andop) == CONST_INT)
14365 HOST_WIDE_INT c, lsb, shift_mask;
14367 c = INTVAL (andop);
14368 if (c == 0 || c == ~0)
14369 return 0;
14371 shift_mask = ~0;
14372 shift_mask <<= INTVAL (shiftop);
14374 /* Find the least significant one bit. */
14375 lsb = c & -c;
14377 /* It must coincide with the LSB of the shift mask. */
14378 if (-lsb != shift_mask)
14379 return 0;
14381 /* Invert to look for the next transition (if any). */
14382 c = ~c;
14384 /* Remove the low group of ones (originally low group of zeros). */
14385 c &= -lsb;
14387 /* Again find the lsb, and check we have all 1's above. */
14388 lsb = c & -c;
14389 return c == -lsb;
14391 else
14392 return 0;
14395 /* Return 1 if ANDOP is a mask suitable for use with an rldicr insn
14396 to perform a left shift. It must have SHIFTOP or more least
14397 significant 0's, with the remainder of the word 1's. */
14400 includes_rldicr_lshift_p (rtx shiftop, rtx andop)
14402 if (GET_CODE (andop) == CONST_INT)
14404 HOST_WIDE_INT c, lsb, shift_mask;
14406 shift_mask = ~0;
14407 shift_mask <<= INTVAL (shiftop);
14408 c = INTVAL (andop);
14410 /* Find the least significant one bit. */
14411 lsb = c & -c;
14413 /* It must be covered by the shift mask.
14414 This test also rejects c == 0. */
14415 if ((lsb & shift_mask) == 0)
14416 return 0;
14418 /* Check we have all 1's above the transition, and reject all 1's. */
14419 return c == -lsb && lsb != 1;
14421 else
14422 return 0;
14425 /* Return 1 if operands will generate a valid arguments to rlwimi
14426 instruction for insert with right shift in 64-bit mode. The mask may
14427 not start on the first bit or stop on the last bit because wrap-around
14428 effects of instruction do not correspond to semantics of RTL insn. */
14431 insvdi_rshift_rlwimi_p (rtx sizeop, rtx startop, rtx shiftop)
14433 if (INTVAL (startop) > 32
14434 && INTVAL (startop) < 64
14435 && INTVAL (sizeop) > 1
14436 && INTVAL (sizeop) + INTVAL (startop) < 64
14437 && INTVAL (shiftop) > 0
14438 && INTVAL (sizeop) + INTVAL (shiftop) < 32
14439 && (64 - (INTVAL (shiftop) & 63)) >= INTVAL (sizeop))
14440 return 1;
14442 return 0;
14445 /* Return 1 if REGNO (reg1) == REGNO (reg2) - 1 making them candidates
14446 for lfq and stfq insns iff the registers are hard registers. */
14449 registers_ok_for_quad_peep (rtx reg1, rtx reg2)
14451 /* We might have been passed a SUBREG. */
14452 if (GET_CODE (reg1) != REG || GET_CODE (reg2) != REG)
14453 return 0;
14455 /* We might have been passed non floating point registers. */
14456 if (!FP_REGNO_P (REGNO (reg1))
14457 || !FP_REGNO_P (REGNO (reg2)))
14458 return 0;
14460 return (REGNO (reg1) == REGNO (reg2) - 1);
14463 /* Return 1 if addr1 and addr2 are suitable for lfq or stfq insn.
14464 addr1 and addr2 must be in consecutive memory locations
14465 (addr2 == addr1 + 8). */
14468 mems_ok_for_quad_peep (rtx mem1, rtx mem2)
14470 rtx addr1, addr2;
14471 unsigned int reg1, reg2;
14472 int offset1, offset2;
14474 /* The mems cannot be volatile. */
14475 if (MEM_VOLATILE_P (mem1) || MEM_VOLATILE_P (mem2))
14476 return 0;
14478 addr1 = XEXP (mem1, 0);
14479 addr2 = XEXP (mem2, 0);
14481 /* Extract an offset (if used) from the first addr. */
14482 if (GET_CODE (addr1) == PLUS)
14484 /* If not a REG, return zero. */
14485 if (GET_CODE (XEXP (addr1, 0)) != REG)
14486 return 0;
14487 else
14489 reg1 = REGNO (XEXP (addr1, 0));
14490 /* The offset must be constant! */
14491 if (GET_CODE (XEXP (addr1, 1)) != CONST_INT)
14492 return 0;
14493 offset1 = INTVAL (XEXP (addr1, 1));
14496 else if (GET_CODE (addr1) != REG)
14497 return 0;
14498 else
14500 reg1 = REGNO (addr1);
14501 /* This was a simple (mem (reg)) expression. Offset is 0. */
14502 offset1 = 0;
14505 /* And now for the second addr. */
14506 if (GET_CODE (addr2) == PLUS)
14508 /* If not a REG, return zero. */
14509 if (GET_CODE (XEXP (addr2, 0)) != REG)
14510 return 0;
14511 else
14513 reg2 = REGNO (XEXP (addr2, 0));
14514 /* The offset must be constant. */
14515 if (GET_CODE (XEXP (addr2, 1)) != CONST_INT)
14516 return 0;
14517 offset2 = INTVAL (XEXP (addr2, 1));
14520 else if (GET_CODE (addr2) != REG)
14521 return 0;
14522 else
14524 reg2 = REGNO (addr2);
14525 /* This was a simple (mem (reg)) expression. Offset is 0. */
14526 offset2 = 0;
14529 /* Both of these must have the same base register. */
14530 if (reg1 != reg2)
14531 return 0;
14533 /* The offset for the second addr must be 8 more than the first addr. */
14534 if (offset2 != offset1 + 8)
14535 return 0;
14537 /* All the tests passed. addr1 and addr2 are valid for lfq or stfq
14538 instructions. */
14539 return 1;
14544 rs6000_secondary_memory_needed_rtx (enum machine_mode mode)
14546 static bool eliminated = false;
14547 rtx ret;
14549 if (mode != SDmode || TARGET_NO_SDMODE_STACK)
14550 ret = assign_stack_local (mode, GET_MODE_SIZE (mode), 0);
14551 else
14553 rtx mem = cfun->machine->sdmode_stack_slot;
14554 gcc_assert (mem != NULL_RTX);
14556 if (!eliminated)
14558 mem = eliminate_regs (mem, VOIDmode, NULL_RTX);
14559 cfun->machine->sdmode_stack_slot = mem;
14560 eliminated = true;
14562 ret = mem;
14565 if (TARGET_DEBUG_ADDR)
14567 fprintf (stderr, "\nrs6000_secondary_memory_needed_rtx, mode %s, rtx:\n",
14568 GET_MODE_NAME (mode));
14569 if (!ret)
14570 fprintf (stderr, "\tNULL_RTX\n");
14571 else
14572 debug_rtx (ret);
14575 return ret;
14578 static tree
14579 rs6000_check_sdmode (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
14581 /* Don't walk into types. */
14582 if (*tp == NULL_TREE || *tp == error_mark_node || TYPE_P (*tp))
14584 *walk_subtrees = 0;
14585 return NULL_TREE;
14588 switch (TREE_CODE (*tp))
14590 case VAR_DECL:
14591 case PARM_DECL:
14592 case FIELD_DECL:
14593 case RESULT_DECL:
14594 case SSA_NAME:
14595 case REAL_CST:
14596 case MEM_REF:
14597 case VIEW_CONVERT_EXPR:
14598 if (TYPE_MODE (TREE_TYPE (*tp)) == SDmode)
14599 return *tp;
14600 break;
14601 default:
14602 break;
14605 return NULL_TREE;
14608 /* Classify a register type. Because the FMRGOW/FMRGEW instructions only work
14609 on traditional floating point registers, and the VMRGOW/VMRGEW instructions
14610 only work on the traditional altivec registers, note if an altivec register
14611 was chosen. */
14613 static enum rs6000_reg_type
14614 register_to_reg_type (rtx reg, bool *is_altivec)
14616 HOST_WIDE_INT regno;
14617 enum reg_class rclass;
14619 if (GET_CODE (reg) == SUBREG)
14620 reg = SUBREG_REG (reg);
14622 if (!REG_P (reg))
14623 return NO_REG_TYPE;
14625 regno = REGNO (reg);
14626 if (regno >= FIRST_PSEUDO_REGISTER)
14628 if (!lra_in_progress && !reload_in_progress && !reload_completed)
14629 return PSEUDO_REG_TYPE;
14631 regno = true_regnum (reg);
14632 if (regno < 0 || regno >= FIRST_PSEUDO_REGISTER)
14633 return PSEUDO_REG_TYPE;
14636 gcc_assert (regno >= 0);
14638 if (is_altivec && ALTIVEC_REGNO_P (regno))
14639 *is_altivec = true;
14641 rclass = rs6000_regno_regclass[regno];
14642 return reg_class_to_reg_type[(int)rclass];
14645 /* Helper function for rs6000_secondary_reload to return true if a move to a
14646 different register classe is really a simple move. */
14648 static bool
14649 rs6000_secondary_reload_simple_move (enum rs6000_reg_type to_type,
14650 enum rs6000_reg_type from_type,
14651 enum machine_mode mode)
14653 int size;
14655 /* Add support for various direct moves available. In this function, we only
14656 look at cases where we don't need any extra registers, and one or more
14657 simple move insns are issued. At present, 32-bit integers are not allowed
14658 in FPR/VSX registers. Single precision binary floating is not a simple
14659 move because we need to convert to the single precision memory layout.
14660 The 4-byte SDmode can be moved. */
14661 size = GET_MODE_SIZE (mode);
14662 if (TARGET_DIRECT_MOVE
14663 && ((mode == SDmode) || (TARGET_POWERPC64 && size == 8))
14664 && ((to_type == GPR_REG_TYPE && from_type == VSX_REG_TYPE)
14665 || (to_type == VSX_REG_TYPE && from_type == GPR_REG_TYPE)))
14666 return true;
14668 else if (TARGET_MFPGPR && TARGET_POWERPC64 && size == 8
14669 && ((to_type == GPR_REG_TYPE && from_type == FPR_REG_TYPE)
14670 || (to_type == FPR_REG_TYPE && from_type == GPR_REG_TYPE)))
14671 return true;
14673 else if ((size == 4 || (TARGET_POWERPC64 && size == 8))
14674 && ((to_type == GPR_REG_TYPE && from_type == SPR_REG_TYPE)
14675 || (to_type == SPR_REG_TYPE && from_type == GPR_REG_TYPE)))
14676 return true;
14678 return false;
14681 /* Power8 helper function for rs6000_secondary_reload, handle all of the
14682 special direct moves that involve allocating an extra register, return the
14683 insn code of the helper function if there is such a function or
14684 CODE_FOR_nothing if not. */
14686 static bool
14687 rs6000_secondary_reload_direct_move (enum rs6000_reg_type to_type,
14688 enum rs6000_reg_type from_type,
14689 enum machine_mode mode,
14690 secondary_reload_info *sri,
14691 bool altivec_p)
14693 bool ret = false;
14694 enum insn_code icode = CODE_FOR_nothing;
14695 int cost = 0;
14696 int size = GET_MODE_SIZE (mode);
14698 if (TARGET_POWERPC64)
14700 if (size == 16)
14702 /* Handle moving 128-bit values from GPRs to VSX point registers on
14703 power8 when running in 64-bit mode using XXPERMDI to glue the two
14704 64-bit values back together. */
14705 if (to_type == VSX_REG_TYPE && from_type == GPR_REG_TYPE)
14707 cost = 3; /* 2 mtvsrd's, 1 xxpermdi. */
14708 icode = reload_vsx_gpr[(int)mode];
14711 /* Handle moving 128-bit values from VSX point registers to GPRs on
14712 power8 when running in 64-bit mode using XXPERMDI to get access to the
14713 bottom 64-bit value. */
14714 else if (to_type == GPR_REG_TYPE && from_type == VSX_REG_TYPE)
14716 cost = 3; /* 2 mfvsrd's, 1 xxpermdi. */
14717 icode = reload_gpr_vsx[(int)mode];
14721 else if (mode == SFmode)
14723 if (to_type == GPR_REG_TYPE && from_type == VSX_REG_TYPE)
14725 cost = 3; /* xscvdpspn, mfvsrd, and. */
14726 icode = reload_gpr_vsx[(int)mode];
14729 else if (to_type == VSX_REG_TYPE && from_type == GPR_REG_TYPE)
14731 cost = 2; /* mtvsrz, xscvspdpn. */
14732 icode = reload_vsx_gpr[(int)mode];
14737 if (TARGET_POWERPC64 && size == 16)
14739 /* Handle moving 128-bit values from GPRs to VSX point registers on
14740 power8 when running in 64-bit mode using XXPERMDI to glue the two
14741 64-bit values back together. */
14742 if (to_type == VSX_REG_TYPE && from_type == GPR_REG_TYPE)
14744 cost = 3; /* 2 mtvsrd's, 1 xxpermdi. */
14745 icode = reload_vsx_gpr[(int)mode];
14748 /* Handle moving 128-bit values from VSX point registers to GPRs on
14749 power8 when running in 64-bit mode using XXPERMDI to get access to the
14750 bottom 64-bit value. */
14751 else if (to_type == GPR_REG_TYPE && from_type == VSX_REG_TYPE)
14753 cost = 3; /* 2 mfvsrd's, 1 xxpermdi. */
14754 icode = reload_gpr_vsx[(int)mode];
14758 else if (!TARGET_POWERPC64 && size == 8)
14760 /* Handle moving 64-bit values from GPRs to floating point registers on
14761 power8 when running in 32-bit mode using FMRGOW to glue the two 32-bit
14762 values back together. Altivec register classes must be handled
14763 specially since a different instruction is used, and the secondary
14764 reload support requires a single instruction class in the scratch
14765 register constraint. However, right now TFmode is not allowed in
14766 Altivec registers, so the pattern will never match. */
14767 if (to_type == VSX_REG_TYPE && from_type == GPR_REG_TYPE && !altivec_p)
14769 cost = 3; /* 2 mtvsrwz's, 1 fmrgow. */
14770 icode = reload_fpr_gpr[(int)mode];
14774 if (icode != CODE_FOR_nothing)
14776 ret = true;
14777 if (sri)
14779 sri->icode = icode;
14780 sri->extra_cost = cost;
14784 return ret;
14787 /* Return whether a move between two register classes can be done either
14788 directly (simple move) or via a pattern that uses a single extra temporary
14789 (using power8's direct move in this case. */
14791 static bool
14792 rs6000_secondary_reload_move (enum rs6000_reg_type to_type,
14793 enum rs6000_reg_type from_type,
14794 enum machine_mode mode,
14795 secondary_reload_info *sri,
14796 bool altivec_p)
14798 /* Fall back to load/store reloads if either type is not a register. */
14799 if (to_type == NO_REG_TYPE || from_type == NO_REG_TYPE)
14800 return false;
14802 /* If we haven't allocated registers yet, assume the move can be done for the
14803 standard register types. */
14804 if ((to_type == PSEUDO_REG_TYPE && from_type == PSEUDO_REG_TYPE)
14805 || (to_type == PSEUDO_REG_TYPE && IS_STD_REG_TYPE (from_type))
14806 || (from_type == PSEUDO_REG_TYPE && IS_STD_REG_TYPE (to_type)))
14807 return true;
14809 /* Moves to the same set of registers is a simple move for non-specialized
14810 registers. */
14811 if (to_type == from_type && IS_STD_REG_TYPE (to_type))
14812 return true;
14814 /* Check whether a simple move can be done directly. */
14815 if (rs6000_secondary_reload_simple_move (to_type, from_type, mode))
14817 if (sri)
14819 sri->icode = CODE_FOR_nothing;
14820 sri->extra_cost = 0;
14822 return true;
14825 /* Now check if we can do it in a few steps. */
14826 return rs6000_secondary_reload_direct_move (to_type, from_type, mode, sri,
14827 altivec_p);
14830 /* Inform reload about cases where moving X with a mode MODE to a register in
14831 RCLASS requires an extra scratch or immediate register. Return the class
14832 needed for the immediate register.
14834 For VSX and Altivec, we may need a register to convert sp+offset into
14835 reg+sp.
14837 For misaligned 64-bit gpr loads and stores we need a register to
14838 convert an offset address to indirect. */
14840 static reg_class_t
14841 rs6000_secondary_reload (bool in_p,
14842 rtx x,
14843 reg_class_t rclass_i,
14844 enum machine_mode mode,
14845 secondary_reload_info *sri)
14847 enum reg_class rclass = (enum reg_class) rclass_i;
14848 reg_class_t ret = ALL_REGS;
14849 enum insn_code icode;
14850 bool default_p = false;
14852 sri->icode = CODE_FOR_nothing;
14853 icode = rs6000_vector_reload[mode][in_p != false];
14855 if (REG_P (x) || register_operand (x, mode))
14857 enum rs6000_reg_type to_type = reg_class_to_reg_type[(int)rclass];
14858 bool altivec_p = (rclass == ALTIVEC_REGS);
14859 enum rs6000_reg_type from_type = register_to_reg_type (x, &altivec_p);
14861 if (!in_p)
14863 enum rs6000_reg_type exchange = to_type;
14864 to_type = from_type;
14865 from_type = exchange;
14868 if (rs6000_secondary_reload_move (to_type, from_type, mode, sri,
14869 altivec_p))
14871 icode = (enum insn_code)sri->icode;
14872 default_p = false;
14873 ret = NO_REGS;
14877 /* Handle vector moves with reload helper functions. */
14878 if (ret == ALL_REGS && icode != CODE_FOR_nothing)
14880 ret = NO_REGS;
14881 sri->icode = CODE_FOR_nothing;
14882 sri->extra_cost = 0;
14884 if (GET_CODE (x) == MEM)
14886 rtx addr = XEXP (x, 0);
14888 /* Loads to and stores from gprs can do reg+offset, and wouldn't need
14889 an extra register in that case, but it would need an extra
14890 register if the addressing is reg+reg or (reg+reg)&(-16). Special
14891 case load/store quad. */
14892 if (rclass == GENERAL_REGS || rclass == BASE_REGS)
14894 if (TARGET_POWERPC64 && TARGET_QUAD_MEMORY
14895 && GET_MODE_SIZE (mode) == 16
14896 && quad_memory_operand (x, mode))
14898 sri->icode = icode;
14899 sri->extra_cost = 2;
14902 else if (!legitimate_indirect_address_p (addr, false)
14903 && !rs6000_legitimate_offset_address_p (PTImode, addr,
14904 false, true))
14906 sri->icode = icode;
14907 /* account for splitting the loads, and converting the
14908 address from reg+reg to reg. */
14909 sri->extra_cost = (((TARGET_64BIT) ? 3 : 5)
14910 + ((GET_CODE (addr) == AND) ? 1 : 0));
14913 /* Allow scalar loads to/from the traditional floating point
14914 registers, even if VSX memory is set. */
14915 else if ((rclass == FLOAT_REGS || rclass == NO_REGS)
14916 && (GET_MODE_SIZE (mode) == 4 || GET_MODE_SIZE (mode) == 8)
14917 && (legitimate_indirect_address_p (addr, false)
14918 || legitimate_indirect_address_p (addr, false)
14919 || rs6000_legitimate_offset_address_p (mode, addr,
14920 false, true)))
14923 /* Loads to and stores from vector registers can only do reg+reg
14924 addressing. Altivec registers can also do (reg+reg)&(-16). Allow
14925 scalar modes loading up the traditional floating point registers
14926 to use offset addresses. */
14927 else if (rclass == VSX_REGS || rclass == ALTIVEC_REGS
14928 || rclass == FLOAT_REGS || rclass == NO_REGS)
14930 if (!VECTOR_MEM_ALTIVEC_P (mode)
14931 && GET_CODE (addr) == AND
14932 && GET_CODE (XEXP (addr, 1)) == CONST_INT
14933 && INTVAL (XEXP (addr, 1)) == -16
14934 && (legitimate_indirect_address_p (XEXP (addr, 0), false)
14935 || legitimate_indexed_address_p (XEXP (addr, 0), false)))
14937 sri->icode = icode;
14938 sri->extra_cost = ((GET_CODE (XEXP (addr, 0)) == PLUS)
14939 ? 2 : 1);
14941 else if (!legitimate_indirect_address_p (addr, false)
14942 && (rclass == NO_REGS
14943 || !legitimate_indexed_address_p (addr, false)))
14945 sri->icode = icode;
14946 sri->extra_cost = 1;
14948 else
14949 icode = CODE_FOR_nothing;
14951 /* Any other loads, including to pseudo registers which haven't been
14952 assigned to a register yet, default to require a scratch
14953 register. */
14954 else
14956 sri->icode = icode;
14957 sri->extra_cost = 2;
14960 else if (REG_P (x))
14962 int regno = true_regnum (x);
14964 icode = CODE_FOR_nothing;
14965 if (regno < 0 || regno >= FIRST_PSEUDO_REGISTER)
14966 default_p = true;
14967 else
14969 enum reg_class xclass = REGNO_REG_CLASS (regno);
14970 enum rs6000_reg_type rtype1 = reg_class_to_reg_type[(int)rclass];
14971 enum rs6000_reg_type rtype2 = reg_class_to_reg_type[(int)xclass];
14973 /* If memory is needed, use default_secondary_reload to create the
14974 stack slot. */
14975 if (rtype1 != rtype2 || !IS_STD_REG_TYPE (rtype1))
14976 default_p = true;
14977 else
14978 ret = NO_REGS;
14981 else
14982 default_p = true;
14984 else if (TARGET_POWERPC64
14985 && reg_class_to_reg_type[(int)rclass] == GPR_REG_TYPE
14986 && MEM_P (x)
14987 && GET_MODE_SIZE (GET_MODE (x)) >= UNITS_PER_WORD)
14989 rtx addr = XEXP (x, 0);
14990 rtx off = address_offset (addr);
14992 if (off != NULL_RTX)
14994 unsigned int extra = GET_MODE_SIZE (GET_MODE (x)) - UNITS_PER_WORD;
14995 unsigned HOST_WIDE_INT offset = INTVAL (off);
14997 /* We need a secondary reload when our legitimate_address_p
14998 says the address is good (as otherwise the entire address
14999 will be reloaded), and the offset is not a multiple of
15000 four or we have an address wrap. Address wrap will only
15001 occur for LO_SUMs since legitimate_offset_address_p
15002 rejects addresses for 16-byte mems that will wrap. */
15003 if (GET_CODE (addr) == LO_SUM
15004 ? (1 /* legitimate_address_p allows any offset for lo_sum */
15005 && ((offset & 3) != 0
15006 || ((offset & 0xffff) ^ 0x8000) >= 0x10000 - extra))
15007 : (offset + 0x8000 < 0x10000 - extra /* legitimate_address_p */
15008 && (offset & 3) != 0))
15010 if (in_p)
15011 sri->icode = CODE_FOR_reload_di_load;
15012 else
15013 sri->icode = CODE_FOR_reload_di_store;
15014 sri->extra_cost = 2;
15015 ret = NO_REGS;
15017 else
15018 default_p = true;
15020 else
15021 default_p = true;
15023 else if (!TARGET_POWERPC64
15024 && reg_class_to_reg_type[(int)rclass] == GPR_REG_TYPE
15025 && MEM_P (x)
15026 && GET_MODE_SIZE (GET_MODE (x)) > UNITS_PER_WORD)
15028 rtx addr = XEXP (x, 0);
15029 rtx off = address_offset (addr);
15031 if (off != NULL_RTX)
15033 unsigned int extra = GET_MODE_SIZE (GET_MODE (x)) - UNITS_PER_WORD;
15034 unsigned HOST_WIDE_INT offset = INTVAL (off);
15036 /* We need a secondary reload when our legitimate_address_p
15037 says the address is good (as otherwise the entire address
15038 will be reloaded), and we have a wrap.
15040 legitimate_lo_sum_address_p allows LO_SUM addresses to
15041 have any offset so test for wrap in the low 16 bits.
15043 legitimate_offset_address_p checks for the range
15044 [-0x8000,0x7fff] for mode size of 8 and [-0x8000,0x7ff7]
15045 for mode size of 16. We wrap at [0x7ffc,0x7fff] and
15046 [0x7ff4,0x7fff] respectively, so test for the
15047 intersection of these ranges, [0x7ffc,0x7fff] and
15048 [0x7ff4,0x7ff7] respectively.
15050 Note that the address we see here may have been
15051 manipulated by legitimize_reload_address. */
15052 if (GET_CODE (addr) == LO_SUM
15053 ? ((offset & 0xffff) ^ 0x8000) >= 0x10000 - extra
15054 : offset - (0x8000 - extra) < UNITS_PER_WORD)
15056 if (in_p)
15057 sri->icode = CODE_FOR_reload_si_load;
15058 else
15059 sri->icode = CODE_FOR_reload_si_store;
15060 sri->extra_cost = 2;
15061 ret = NO_REGS;
15063 else
15064 default_p = true;
15066 else
15067 default_p = true;
15069 else
15070 default_p = true;
15072 if (default_p)
15073 ret = default_secondary_reload (in_p, x, rclass, mode, sri);
15075 gcc_assert (ret != ALL_REGS);
15077 if (TARGET_DEBUG_ADDR)
15079 fprintf (stderr,
15080 "\nrs6000_secondary_reload, return %s, in_p = %s, rclass = %s, "
15081 "mode = %s",
15082 reg_class_names[ret],
15083 in_p ? "true" : "false",
15084 reg_class_names[rclass],
15085 GET_MODE_NAME (mode));
15087 if (default_p)
15088 fprintf (stderr, ", default secondary reload");
15090 if (sri->icode != CODE_FOR_nothing)
15091 fprintf (stderr, ", reload func = %s, extra cost = %d\n",
15092 insn_data[sri->icode].name, sri->extra_cost);
15093 else
15094 fprintf (stderr, "\n");
15096 debug_rtx (x);
15099 return ret;
15102 /* Better tracing for rs6000_secondary_reload_inner. */
15104 static void
15105 rs6000_secondary_reload_trace (int line, rtx reg, rtx mem, rtx scratch,
15106 bool store_p)
15108 rtx set, clobber;
15110 gcc_assert (reg != NULL_RTX && mem != NULL_RTX && scratch != NULL_RTX);
15112 fprintf (stderr, "rs6000_secondary_reload_inner:%d, type = %s\n", line,
15113 store_p ? "store" : "load");
15115 if (store_p)
15116 set = gen_rtx_SET (VOIDmode, mem, reg);
15117 else
15118 set = gen_rtx_SET (VOIDmode, reg, mem);
15120 clobber = gen_rtx_CLOBBER (VOIDmode, scratch);
15121 debug_rtx (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, set, clobber)));
15124 static void
15125 rs6000_secondary_reload_fail (int line, rtx reg, rtx mem, rtx scratch,
15126 bool store_p)
15128 rs6000_secondary_reload_trace (line, reg, mem, scratch, store_p);
15129 gcc_unreachable ();
15132 /* Fixup reload addresses for Altivec or VSX loads/stores to change SP+offset
15133 to SP+reg addressing. */
15135 void
15136 rs6000_secondary_reload_inner (rtx reg, rtx mem, rtx scratch, bool store_p)
15138 int regno = true_regnum (reg);
15139 enum machine_mode mode = GET_MODE (reg);
15140 enum reg_class rclass;
15141 rtx addr;
15142 rtx and_op2 = NULL_RTX;
15143 rtx addr_op1;
15144 rtx addr_op2;
15145 rtx scratch_or_premodify = scratch;
15146 rtx and_rtx;
15147 rtx cc_clobber;
15149 if (TARGET_DEBUG_ADDR)
15150 rs6000_secondary_reload_trace (__LINE__, reg, mem, scratch, store_p);
15152 if (regno < 0 || regno >= FIRST_PSEUDO_REGISTER)
15153 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
15155 if (GET_CODE (mem) != MEM)
15156 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
15158 rclass = REGNO_REG_CLASS (regno);
15159 addr = XEXP (mem, 0);
15161 switch (rclass)
15163 /* GPRs can handle reg + small constant, all other addresses need to use
15164 the scratch register. */
15165 case GENERAL_REGS:
15166 case BASE_REGS:
15167 if (GET_CODE (addr) == AND)
15169 and_op2 = XEXP (addr, 1);
15170 addr = XEXP (addr, 0);
15173 if (GET_CODE (addr) == PRE_MODIFY)
15175 scratch_or_premodify = XEXP (addr, 0);
15176 if (!REG_P (scratch_or_premodify))
15177 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
15179 if (GET_CODE (XEXP (addr, 1)) != PLUS)
15180 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
15182 addr = XEXP (addr, 1);
15185 if (GET_CODE (addr) == PLUS
15186 && (and_op2 != NULL_RTX
15187 || !rs6000_legitimate_offset_address_p (PTImode, addr,
15188 false, true)))
15190 addr_op1 = XEXP (addr, 0);
15191 addr_op2 = XEXP (addr, 1);
15192 if (!legitimate_indirect_address_p (addr_op1, false))
15193 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
15195 if (!REG_P (addr_op2)
15196 && (GET_CODE (addr_op2) != CONST_INT
15197 || !satisfies_constraint_I (addr_op2)))
15199 if (TARGET_DEBUG_ADDR)
15201 fprintf (stderr,
15202 "\nMove plus addr to register %s, mode = %s: ",
15203 rs6000_reg_names[REGNO (scratch)],
15204 GET_MODE_NAME (mode));
15205 debug_rtx (addr_op2);
15207 rs6000_emit_move (scratch, addr_op2, Pmode);
15208 addr_op2 = scratch;
15211 emit_insn (gen_rtx_SET (VOIDmode,
15212 scratch_or_premodify,
15213 gen_rtx_PLUS (Pmode,
15214 addr_op1,
15215 addr_op2)));
15217 addr = scratch_or_premodify;
15218 scratch_or_premodify = scratch;
15220 else if (!legitimate_indirect_address_p (addr, false)
15221 && !rs6000_legitimate_offset_address_p (PTImode, addr,
15222 false, true))
15224 if (TARGET_DEBUG_ADDR)
15226 fprintf (stderr, "\nMove addr to register %s, mode = %s: ",
15227 rs6000_reg_names[REGNO (scratch_or_premodify)],
15228 GET_MODE_NAME (mode));
15229 debug_rtx (addr);
15231 rs6000_emit_move (scratch_or_premodify, addr, Pmode);
15232 addr = scratch_or_premodify;
15233 scratch_or_premodify = scratch;
15235 break;
15237 /* Float registers can do offset+reg addressing for scalar types. */
15238 case FLOAT_REGS:
15239 if (legitimate_indirect_address_p (addr, false) /* reg */
15240 || legitimate_indexed_address_p (addr, false) /* reg+reg */
15241 || ((GET_MODE_SIZE (mode) == 4 || GET_MODE_SIZE (mode) == 8)
15242 && and_op2 == NULL_RTX
15243 && scratch_or_premodify == scratch
15244 && rs6000_legitimate_offset_address_p (mode, addr, false, false)))
15245 break;
15247 /* If this isn't a legacy floating point load/store, fall through to the
15248 VSX defaults. */
15250 /* VSX/Altivec registers can only handle reg+reg addressing. Move other
15251 addresses into a scratch register. */
15252 case VSX_REGS:
15253 case ALTIVEC_REGS:
15255 /* With float regs, we need to handle the AND ourselves, since we can't
15256 use the Altivec instruction with an implicit AND -16. Allow scalar
15257 loads to float registers to use reg+offset even if VSX. */
15258 if (GET_CODE (addr) == AND
15259 && (rclass != ALTIVEC_REGS || GET_MODE_SIZE (mode) != 16
15260 || GET_CODE (XEXP (addr, 1)) != CONST_INT
15261 || INTVAL (XEXP (addr, 1)) != -16
15262 || !VECTOR_MEM_ALTIVEC_P (mode)))
15264 and_op2 = XEXP (addr, 1);
15265 addr = XEXP (addr, 0);
15268 /* If we aren't using a VSX load, save the PRE_MODIFY register and use it
15269 as the address later. */
15270 if (GET_CODE (addr) == PRE_MODIFY
15271 && ((ALTIVEC_OR_VSX_VECTOR_MODE (mode)
15272 && (rclass != FLOAT_REGS
15273 || (GET_MODE_SIZE (mode) != 4 && GET_MODE_SIZE (mode) != 8)))
15274 || and_op2 != NULL_RTX
15275 || !legitimate_indexed_address_p (XEXP (addr, 1), false)))
15277 scratch_or_premodify = XEXP (addr, 0);
15278 if (!legitimate_indirect_address_p (scratch_or_premodify, false))
15279 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
15281 if (GET_CODE (XEXP (addr, 1)) != PLUS)
15282 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
15284 addr = XEXP (addr, 1);
15287 if (legitimate_indirect_address_p (addr, false) /* reg */
15288 || legitimate_indexed_address_p (addr, false) /* reg+reg */
15289 || (GET_CODE (addr) == AND /* Altivec memory */
15290 && rclass == ALTIVEC_REGS
15291 && GET_CODE (XEXP (addr, 1)) == CONST_INT
15292 && INTVAL (XEXP (addr, 1)) == -16
15293 && (legitimate_indirect_address_p (XEXP (addr, 0), false)
15294 || legitimate_indexed_address_p (XEXP (addr, 0), false))))
15297 else if (GET_CODE (addr) == PLUS)
15299 addr_op1 = XEXP (addr, 0);
15300 addr_op2 = XEXP (addr, 1);
15301 if (!REG_P (addr_op1))
15302 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
15304 if (TARGET_DEBUG_ADDR)
15306 fprintf (stderr, "\nMove plus addr to register %s, mode = %s: ",
15307 rs6000_reg_names[REGNO (scratch)], GET_MODE_NAME (mode));
15308 debug_rtx (addr_op2);
15310 rs6000_emit_move (scratch, addr_op2, Pmode);
15311 emit_insn (gen_rtx_SET (VOIDmode,
15312 scratch_or_premodify,
15313 gen_rtx_PLUS (Pmode,
15314 addr_op1,
15315 scratch)));
15316 addr = scratch_or_premodify;
15317 scratch_or_premodify = scratch;
15320 else if (GET_CODE (addr) == SYMBOL_REF || GET_CODE (addr) == CONST
15321 || GET_CODE (addr) == CONST_INT || GET_CODE (addr) == LO_SUM
15322 || REG_P (addr))
15324 if (TARGET_DEBUG_ADDR)
15326 fprintf (stderr, "\nMove addr to register %s, mode = %s: ",
15327 rs6000_reg_names[REGNO (scratch_or_premodify)],
15328 GET_MODE_NAME (mode));
15329 debug_rtx (addr);
15332 rs6000_emit_move (scratch_or_premodify, addr, Pmode);
15333 addr = scratch_or_premodify;
15334 scratch_or_premodify = scratch;
15337 else
15338 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
15340 break;
15342 default:
15343 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
15346 /* If the original address involved a pre-modify that we couldn't use the VSX
15347 memory instruction with update, and we haven't taken care of already,
15348 store the address in the pre-modify register and use that as the
15349 address. */
15350 if (scratch_or_premodify != scratch && scratch_or_premodify != addr)
15352 emit_insn (gen_rtx_SET (VOIDmode, scratch_or_premodify, addr));
15353 addr = scratch_or_premodify;
15356 /* If the original address involved an AND -16 and we couldn't use an ALTIVEC
15357 memory instruction, recreate the AND now, including the clobber which is
15358 generated by the general ANDSI3/ANDDI3 patterns for the
15359 andi. instruction. */
15360 if (and_op2 != NULL_RTX)
15362 if (! legitimate_indirect_address_p (addr, false))
15364 emit_insn (gen_rtx_SET (VOIDmode, scratch, addr));
15365 addr = scratch;
15368 if (TARGET_DEBUG_ADDR)
15370 fprintf (stderr, "\nAnd addr to register %s, mode = %s: ",
15371 rs6000_reg_names[REGNO (scratch)], GET_MODE_NAME (mode));
15372 debug_rtx (and_op2);
15375 and_rtx = gen_rtx_SET (VOIDmode,
15376 scratch,
15377 gen_rtx_AND (Pmode,
15378 addr,
15379 and_op2));
15381 cc_clobber = gen_rtx_CLOBBER (CCmode, gen_rtx_SCRATCH (CCmode));
15382 emit_insn (gen_rtx_PARALLEL (VOIDmode,
15383 gen_rtvec (2, and_rtx, cc_clobber)));
15384 addr = scratch;
15387 /* Adjust the address if it changed. */
15388 if (addr != XEXP (mem, 0))
15390 mem = replace_equiv_address_nv (mem, addr);
15391 if (TARGET_DEBUG_ADDR)
15392 fprintf (stderr, "\nrs6000_secondary_reload_inner, mem adjusted.\n");
15395 /* Now create the move. */
15396 if (store_p)
15397 emit_insn (gen_rtx_SET (VOIDmode, mem, reg));
15398 else
15399 emit_insn (gen_rtx_SET (VOIDmode, reg, mem));
15401 return;
15404 /* Convert reloads involving 64-bit gprs and misaligned offset
15405 addressing, or multiple 32-bit gprs and offsets that are too large,
15406 to use indirect addressing. */
15408 void
15409 rs6000_secondary_reload_gpr (rtx reg, rtx mem, rtx scratch, bool store_p)
15411 int regno = true_regnum (reg);
15412 enum reg_class rclass;
15413 rtx addr;
15414 rtx scratch_or_premodify = scratch;
15416 if (TARGET_DEBUG_ADDR)
15418 fprintf (stderr, "\nrs6000_secondary_reload_gpr, type = %s\n",
15419 store_p ? "store" : "load");
15420 fprintf (stderr, "reg:\n");
15421 debug_rtx (reg);
15422 fprintf (stderr, "mem:\n");
15423 debug_rtx (mem);
15424 fprintf (stderr, "scratch:\n");
15425 debug_rtx (scratch);
15428 gcc_assert (regno >= 0 && regno < FIRST_PSEUDO_REGISTER);
15429 gcc_assert (GET_CODE (mem) == MEM);
15430 rclass = REGNO_REG_CLASS (regno);
15431 gcc_assert (rclass == GENERAL_REGS || rclass == BASE_REGS);
15432 addr = XEXP (mem, 0);
15434 if (GET_CODE (addr) == PRE_MODIFY)
15436 scratch_or_premodify = XEXP (addr, 0);
15437 gcc_assert (REG_P (scratch_or_premodify));
15438 addr = XEXP (addr, 1);
15440 gcc_assert (GET_CODE (addr) == PLUS || GET_CODE (addr) == LO_SUM);
15442 rs6000_emit_move (scratch_or_premodify, addr, Pmode);
15444 mem = replace_equiv_address_nv (mem, scratch_or_premodify);
15446 /* Now create the move. */
15447 if (store_p)
15448 emit_insn (gen_rtx_SET (VOIDmode, mem, reg));
15449 else
15450 emit_insn (gen_rtx_SET (VOIDmode, reg, mem));
15452 return;
15455 /* Allocate a 64-bit stack slot to be used for copying SDmode values through if
15456 this function has any SDmode references. If we are on a power7 or later, we
15457 don't need the 64-bit stack slot since the LFIWZX and STIFWX instructions
15458 can load/store the value. */
15460 static void
15461 rs6000_alloc_sdmode_stack_slot (void)
15463 tree t;
15464 basic_block bb;
15465 gimple_stmt_iterator gsi;
15467 gcc_assert (cfun->machine->sdmode_stack_slot == NULL_RTX);
15469 if (TARGET_NO_SDMODE_STACK)
15470 return;
15472 FOR_EACH_BB (bb)
15473 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
15475 tree ret = walk_gimple_op (gsi_stmt (gsi), rs6000_check_sdmode, NULL);
15476 if (ret)
15478 rtx stack = assign_stack_local (DDmode, GET_MODE_SIZE (DDmode), 0);
15479 cfun->machine->sdmode_stack_slot = adjust_address_nv (stack,
15480 SDmode, 0);
15481 return;
15485 /* Check for any SDmode parameters of the function. */
15486 for (t = DECL_ARGUMENTS (cfun->decl); t; t = DECL_CHAIN (t))
15488 if (TREE_TYPE (t) == error_mark_node)
15489 continue;
15491 if (TYPE_MODE (TREE_TYPE (t)) == SDmode
15492 || TYPE_MODE (DECL_ARG_TYPE (t)) == SDmode)
15494 rtx stack = assign_stack_local (DDmode, GET_MODE_SIZE (DDmode), 0);
15495 cfun->machine->sdmode_stack_slot = adjust_address_nv (stack,
15496 SDmode, 0);
15497 return;
15502 static void
15503 rs6000_instantiate_decls (void)
15505 if (cfun->machine->sdmode_stack_slot != NULL_RTX)
15506 instantiate_decl_rtl (cfun->machine->sdmode_stack_slot);
15509 /* Given an rtx X being reloaded into a reg required to be
15510 in class CLASS, return the class of reg to actually use.
15511 In general this is just CLASS; but on some machines
15512 in some cases it is preferable to use a more restrictive class.
15514 On the RS/6000, we have to return NO_REGS when we want to reload a
15515 floating-point CONST_DOUBLE to force it to be copied to memory.
15517 We also don't want to reload integer values into floating-point
15518 registers if we can at all help it. In fact, this can
15519 cause reload to die, if it tries to generate a reload of CTR
15520 into a FP register and discovers it doesn't have the memory location
15521 required.
15523 ??? Would it be a good idea to have reload do the converse, that is
15524 try to reload floating modes into FP registers if possible?
15527 static enum reg_class
15528 rs6000_preferred_reload_class (rtx x, enum reg_class rclass)
15530 enum machine_mode mode = GET_MODE (x);
15532 if (TARGET_VSX && x == CONST0_RTX (mode) && VSX_REG_CLASS_P (rclass))
15533 return rclass;
15535 if (VECTOR_UNIT_ALTIVEC_OR_VSX_P (mode)
15536 && (rclass == ALTIVEC_REGS || rclass == VSX_REGS)
15537 && easy_vector_constant (x, mode))
15538 return ALTIVEC_REGS;
15540 if (CONSTANT_P (x) && reg_classes_intersect_p (rclass, FLOAT_REGS))
15541 return NO_REGS;
15543 if (GET_MODE_CLASS (mode) == MODE_INT && rclass == NON_SPECIAL_REGS)
15544 return GENERAL_REGS;
15546 /* For VSX, prefer the traditional registers for 64-bit values because we can
15547 use the non-VSX loads. Prefer the Altivec registers if Altivec is
15548 handling the vector operations (i.e. V16QI, V8HI, and V4SI), or if we
15549 prefer Altivec loads.. */
15550 if (rclass == VSX_REGS)
15552 if (GET_MODE_SIZE (mode) <= 8)
15553 return FLOAT_REGS;
15555 if (VECTOR_UNIT_ALTIVEC_P (mode) || VECTOR_MEM_ALTIVEC_P (mode))
15556 return ALTIVEC_REGS;
15558 return rclass;
15561 return rclass;
15564 /* Debug version of rs6000_preferred_reload_class. */
15565 static enum reg_class
15566 rs6000_debug_preferred_reload_class (rtx x, enum reg_class rclass)
15568 enum reg_class ret = rs6000_preferred_reload_class (x, rclass);
15570 fprintf (stderr,
15571 "\nrs6000_preferred_reload_class, return %s, rclass = %s, "
15572 "mode = %s, x:\n",
15573 reg_class_names[ret], reg_class_names[rclass],
15574 GET_MODE_NAME (GET_MODE (x)));
15575 debug_rtx (x);
15577 return ret;
15580 /* If we are copying between FP or AltiVec registers and anything else, we need
15581 a memory location. The exception is when we are targeting ppc64 and the
15582 move to/from fpr to gpr instructions are available. Also, under VSX, you
15583 can copy vector registers from the FP register set to the Altivec register
15584 set and vice versa. */
15586 static bool
15587 rs6000_secondary_memory_needed (enum reg_class from_class,
15588 enum reg_class to_class,
15589 enum machine_mode mode)
15591 enum rs6000_reg_type from_type, to_type;
15592 bool altivec_p = ((from_class == ALTIVEC_REGS)
15593 || (to_class == ALTIVEC_REGS));
15595 /* If a simple/direct move is available, we don't need secondary memory */
15596 from_type = reg_class_to_reg_type[(int)from_class];
15597 to_type = reg_class_to_reg_type[(int)to_class];
15599 if (rs6000_secondary_reload_move (to_type, from_type, mode,
15600 (secondary_reload_info *)0, altivec_p))
15601 return false;
15603 /* If we have a floating point or vector register class, we need to use
15604 memory to transfer the data. */
15605 if (IS_FP_VECT_REG_TYPE (from_type) || IS_FP_VECT_REG_TYPE (to_type))
15606 return true;
15608 return false;
15611 /* Debug version of rs6000_secondary_memory_needed. */
15612 static bool
15613 rs6000_debug_secondary_memory_needed (enum reg_class from_class,
15614 enum reg_class to_class,
15615 enum machine_mode mode)
15617 bool ret = rs6000_secondary_memory_needed (from_class, to_class, mode);
15619 fprintf (stderr,
15620 "rs6000_secondary_memory_needed, return: %s, from_class = %s, "
15621 "to_class = %s, mode = %s\n",
15622 ret ? "true" : "false",
15623 reg_class_names[from_class],
15624 reg_class_names[to_class],
15625 GET_MODE_NAME (mode));
15627 return ret;
15630 /* Return the register class of a scratch register needed to copy IN into
15631 or out of a register in RCLASS in MODE. If it can be done directly,
15632 NO_REGS is returned. */
15634 static enum reg_class
15635 rs6000_secondary_reload_class (enum reg_class rclass, enum machine_mode mode,
15636 rtx in)
15638 int regno;
15640 if (TARGET_ELF || (DEFAULT_ABI == ABI_DARWIN
15641 #if TARGET_MACHO
15642 && MACHOPIC_INDIRECT
15643 #endif
15646 /* We cannot copy a symbolic operand directly into anything
15647 other than BASE_REGS for TARGET_ELF. So indicate that a
15648 register from BASE_REGS is needed as an intermediate
15649 register.
15651 On Darwin, pic addresses require a load from memory, which
15652 needs a base register. */
15653 if (rclass != BASE_REGS
15654 && (GET_CODE (in) == SYMBOL_REF
15655 || GET_CODE (in) == HIGH
15656 || GET_CODE (in) == LABEL_REF
15657 || GET_CODE (in) == CONST))
15658 return BASE_REGS;
15661 if (GET_CODE (in) == REG)
15663 regno = REGNO (in);
15664 if (regno >= FIRST_PSEUDO_REGISTER)
15666 regno = true_regnum (in);
15667 if (regno >= FIRST_PSEUDO_REGISTER)
15668 regno = -1;
15671 else if (GET_CODE (in) == SUBREG)
15673 regno = true_regnum (in);
15674 if (regno >= FIRST_PSEUDO_REGISTER)
15675 regno = -1;
15677 else
15678 regno = -1;
15680 /* We can place anything into GENERAL_REGS and can put GENERAL_REGS
15681 into anything. */
15682 if (rclass == GENERAL_REGS || rclass == BASE_REGS
15683 || (regno >= 0 && INT_REGNO_P (regno)))
15684 return NO_REGS;
15686 /* Constants, memory, and FP registers can go into FP registers. */
15687 if ((regno == -1 || FP_REGNO_P (regno))
15688 && (rclass == FLOAT_REGS || rclass == NON_SPECIAL_REGS))
15689 return (mode != SDmode) ? NO_REGS : GENERAL_REGS;
15691 /* Memory, and FP/altivec registers can go into fp/altivec registers under
15692 VSX. However, for scalar variables, use the traditional floating point
15693 registers so that we can use offset+register addressing. */
15694 if (TARGET_VSX
15695 && (regno == -1 || VSX_REGNO_P (regno))
15696 && VSX_REG_CLASS_P (rclass))
15698 if (GET_MODE_SIZE (mode) < 16)
15699 return FLOAT_REGS;
15701 return NO_REGS;
15704 /* Memory, and AltiVec registers can go into AltiVec registers. */
15705 if ((regno == -1 || ALTIVEC_REGNO_P (regno))
15706 && rclass == ALTIVEC_REGS)
15707 return NO_REGS;
15709 /* We can copy among the CR registers. */
15710 if ((rclass == CR_REGS || rclass == CR0_REGS)
15711 && regno >= 0 && CR_REGNO_P (regno))
15712 return NO_REGS;
15714 /* Otherwise, we need GENERAL_REGS. */
15715 return GENERAL_REGS;
15718 /* Debug version of rs6000_secondary_reload_class. */
15719 static enum reg_class
15720 rs6000_debug_secondary_reload_class (enum reg_class rclass,
15721 enum machine_mode mode, rtx in)
15723 enum reg_class ret = rs6000_secondary_reload_class (rclass, mode, in);
15724 fprintf (stderr,
15725 "\nrs6000_secondary_reload_class, return %s, rclass = %s, "
15726 "mode = %s, input rtx:\n",
15727 reg_class_names[ret], reg_class_names[rclass],
15728 GET_MODE_NAME (mode));
15729 debug_rtx (in);
15731 return ret;
15734 /* Return nonzero if for CLASS a mode change from FROM to TO is invalid. */
15736 static bool
15737 rs6000_cannot_change_mode_class (enum machine_mode from,
15738 enum machine_mode to,
15739 enum reg_class rclass)
15741 unsigned from_size = GET_MODE_SIZE (from);
15742 unsigned to_size = GET_MODE_SIZE (to);
15744 if (from_size != to_size)
15746 enum reg_class xclass = (TARGET_VSX) ? VSX_REGS : FLOAT_REGS;
15748 if (reg_classes_intersect_p (xclass, rclass))
15750 unsigned to_nregs = hard_regno_nregs[FIRST_FPR_REGNO][to];
15751 unsigned from_nregs = hard_regno_nregs[FIRST_FPR_REGNO][from];
15753 /* Don't allow 64-bit types to overlap with 128-bit types that take a
15754 single register under VSX because the scalar part of the register
15755 is in the upper 64-bits, and not the lower 64-bits. Types like
15756 TFmode/TDmode that take 2 scalar register can overlap. 128-bit
15757 IEEE floating point can't overlap, and neither can small
15758 values. */
15760 if (TARGET_IEEEQUAD && (to == TFmode || from == TFmode))
15761 return true;
15763 if (from_size < 8 || to_size < 8)
15764 return true;
15766 if (from_size == 8 && (8 * to_nregs) != to_size)
15767 return true;
15769 if (to_size == 8 && (8 * from_nregs) != from_size)
15770 return true;
15772 return false;
15774 else
15775 return false;
15778 if (TARGET_E500_DOUBLE
15779 && ((((to) == DFmode) + ((from) == DFmode)) == 1
15780 || (((to) == TFmode) + ((from) == TFmode)) == 1
15781 || (((to) == DDmode) + ((from) == DDmode)) == 1
15782 || (((to) == TDmode) + ((from) == TDmode)) == 1
15783 || (((to) == DImode) + ((from) == DImode)) == 1))
15784 return true;
15786 /* Since the VSX register set includes traditional floating point registers
15787 and altivec registers, just check for the size being different instead of
15788 trying to check whether the modes are vector modes. Otherwise it won't
15789 allow say DF and DI to change classes. For types like TFmode and TDmode
15790 that take 2 64-bit registers, rather than a single 128-bit register, don't
15791 allow subregs of those types to other 128 bit types. */
15792 if (TARGET_VSX && VSX_REG_CLASS_P (rclass))
15794 unsigned num_regs = (from_size + 15) / 16;
15795 if (hard_regno_nregs[FIRST_FPR_REGNO][to] > num_regs
15796 || hard_regno_nregs[FIRST_FPR_REGNO][from] > num_regs)
15797 return true;
15799 return (from_size != 8 && from_size != 16);
15802 if (TARGET_ALTIVEC && rclass == ALTIVEC_REGS
15803 && (ALTIVEC_VECTOR_MODE (from) + ALTIVEC_VECTOR_MODE (to)) == 1)
15804 return true;
15806 if (TARGET_SPE && (SPE_VECTOR_MODE (from) + SPE_VECTOR_MODE (to)) == 1
15807 && reg_classes_intersect_p (GENERAL_REGS, rclass))
15808 return true;
15810 return false;
15813 /* Debug version of rs6000_cannot_change_mode_class. */
15814 static bool
15815 rs6000_debug_cannot_change_mode_class (enum machine_mode from,
15816 enum machine_mode to,
15817 enum reg_class rclass)
15819 bool ret = rs6000_cannot_change_mode_class (from, to, rclass);
15821 fprintf (stderr,
15822 "rs6000_cannot_change_mode_class, return %s, from = %s, "
15823 "to = %s, rclass = %s\n",
15824 ret ? "true" : "false",
15825 GET_MODE_NAME (from), GET_MODE_NAME (to),
15826 reg_class_names[rclass]);
15828 return ret;
15831 /* Return a string to do a move operation of 128 bits of data. */
15833 const char *
15834 rs6000_output_move_128bit (rtx operands[])
15836 rtx dest = operands[0];
15837 rtx src = operands[1];
15838 enum machine_mode mode = GET_MODE (dest);
15839 int dest_regno;
15840 int src_regno;
15841 bool dest_gpr_p, dest_fp_p, dest_av_p, dest_vsx_p;
15842 bool src_gpr_p, src_fp_p, src_av_p, src_vsx_p;
15844 if (REG_P (dest))
15846 dest_regno = REGNO (dest);
15847 dest_gpr_p = INT_REGNO_P (dest_regno);
15848 dest_fp_p = FP_REGNO_P (dest_regno);
15849 dest_av_p = ALTIVEC_REGNO_P (dest_regno);
15850 dest_vsx_p = dest_fp_p | dest_av_p;
15852 else
15854 dest_regno = -1;
15855 dest_gpr_p = dest_fp_p = dest_av_p = dest_vsx_p = false;
15858 if (REG_P (src))
15860 src_regno = REGNO (src);
15861 src_gpr_p = INT_REGNO_P (src_regno);
15862 src_fp_p = FP_REGNO_P (src_regno);
15863 src_av_p = ALTIVEC_REGNO_P (src_regno);
15864 src_vsx_p = src_fp_p | src_av_p;
15866 else
15868 src_regno = -1;
15869 src_gpr_p = src_fp_p = src_av_p = src_vsx_p = false;
15872 /* Register moves. */
15873 if (dest_regno >= 0 && src_regno >= 0)
15875 if (dest_gpr_p)
15877 if (src_gpr_p)
15878 return "#";
15880 else if (TARGET_VSX && TARGET_DIRECT_MOVE && src_vsx_p)
15881 return "#";
15884 else if (TARGET_VSX && dest_vsx_p)
15886 if (src_vsx_p)
15887 return "xxlor %x0,%x1,%x1";
15889 else if (TARGET_DIRECT_MOVE && src_gpr_p)
15890 return "#";
15893 else if (TARGET_ALTIVEC && dest_av_p && src_av_p)
15894 return "vor %0,%1,%1";
15896 else if (dest_fp_p && src_fp_p)
15897 return "#";
15900 /* Loads. */
15901 else if (dest_regno >= 0 && MEM_P (src))
15903 if (dest_gpr_p)
15905 if (TARGET_QUAD_MEMORY && (dest_regno & 1) == 0
15906 && quad_memory_operand (src, mode)
15907 && !reg_overlap_mentioned_p (dest, src))
15909 /* lq/stq only has DQ-form, so avoid X-form that %y produces. */
15910 return REG_P (XEXP (src, 0)) ? "lq %0,%1" : "lq %0,%y1";
15912 else
15913 return "#";
15916 else if (TARGET_ALTIVEC && dest_av_p
15917 && altivec_indexed_or_indirect_operand (src, mode))
15918 return "lvx %0,%y1";
15920 else if (TARGET_VSX && dest_vsx_p)
15922 if (mode == V16QImode || mode == V8HImode || mode == V4SImode)
15923 return "lxvw4x %x0,%y1";
15924 else
15925 return "lxvd2x %x0,%y1";
15928 else if (TARGET_ALTIVEC && dest_av_p)
15929 return "lvx %0,%y1";
15931 else if (dest_fp_p)
15932 return "#";
15935 /* Stores. */
15936 else if (src_regno >= 0 && MEM_P (dest))
15938 if (src_gpr_p)
15940 if (TARGET_QUAD_MEMORY && (src_regno & 1) == 0
15941 && quad_memory_operand (dest, mode))
15943 /* lq/stq only has DQ-form, so avoid X-form that %y produces. */
15944 return REG_P (XEXP (dest, 0)) ? "stq %1,%0" : "stq %1,%y0";
15946 else
15947 return "#";
15950 else if (TARGET_ALTIVEC && src_av_p
15951 && altivec_indexed_or_indirect_operand (src, mode))
15952 return "stvx %1,%y0";
15954 else if (TARGET_VSX && src_vsx_p)
15956 if (mode == V16QImode || mode == V8HImode || mode == V4SImode)
15957 return "stxvw4x %x1,%y0";
15958 else
15959 return "stxvd2x %x1,%y0";
15962 else if (TARGET_ALTIVEC && src_av_p)
15963 return "stvx %1,%y0";
15965 else if (src_fp_p)
15966 return "#";
15969 /* Constants. */
15970 else if (dest_regno >= 0
15971 && (GET_CODE (src) == CONST_INT
15972 || GET_CODE (src) == CONST_DOUBLE
15973 || GET_CODE (src) == CONST_VECTOR))
15975 if (dest_gpr_p)
15976 return "#";
15978 else if (TARGET_VSX && dest_vsx_p && zero_constant (src, mode))
15979 return "xxlxor %x0,%x0,%x0";
15981 else if (TARGET_ALTIVEC && dest_av_p)
15982 return output_vec_const_move (operands);
15985 if (TARGET_DEBUG_ADDR)
15987 fprintf (stderr, "\n===== Bad 128 bit move:\n");
15988 debug_rtx (gen_rtx_SET (VOIDmode, dest, src));
15991 gcc_unreachable ();
15995 /* Given a comparison operation, return the bit number in CCR to test. We
15996 know this is a valid comparison.
15998 SCC_P is 1 if this is for an scc. That means that %D will have been
15999 used instead of %C, so the bits will be in different places.
16001 Return -1 if OP isn't a valid comparison for some reason. */
16004 ccr_bit (rtx op, int scc_p)
16006 enum rtx_code code = GET_CODE (op);
16007 enum machine_mode cc_mode;
16008 int cc_regnum;
16009 int base_bit;
16010 rtx reg;
16012 if (!COMPARISON_P (op))
16013 return -1;
16015 reg = XEXP (op, 0);
16017 gcc_assert (GET_CODE (reg) == REG && CR_REGNO_P (REGNO (reg)));
16019 cc_mode = GET_MODE (reg);
16020 cc_regnum = REGNO (reg);
16021 base_bit = 4 * (cc_regnum - CR0_REGNO);
16023 validate_condition_mode (code, cc_mode);
16025 /* When generating a sCOND operation, only positive conditions are
16026 allowed. */
16027 gcc_assert (!scc_p
16028 || code == EQ || code == GT || code == LT || code == UNORDERED
16029 || code == GTU || code == LTU);
16031 switch (code)
16033 case NE:
16034 return scc_p ? base_bit + 3 : base_bit + 2;
16035 case EQ:
16036 return base_bit + 2;
16037 case GT: case GTU: case UNLE:
16038 return base_bit + 1;
16039 case LT: case LTU: case UNGE:
16040 return base_bit;
16041 case ORDERED: case UNORDERED:
16042 return base_bit + 3;
16044 case GE: case GEU:
16045 /* If scc, we will have done a cror to put the bit in the
16046 unordered position. So test that bit. For integer, this is ! LT
16047 unless this is an scc insn. */
16048 return scc_p ? base_bit + 3 : base_bit;
16050 case LE: case LEU:
16051 return scc_p ? base_bit + 3 : base_bit + 1;
16053 default:
16054 gcc_unreachable ();
16058 /* Return the GOT register. */
16061 rs6000_got_register (rtx value ATTRIBUTE_UNUSED)
16063 /* The second flow pass currently (June 1999) can't update
16064 regs_ever_live without disturbing other parts of the compiler, so
16065 update it here to make the prolog/epilogue code happy. */
16066 if (!can_create_pseudo_p ()
16067 && !df_regs_ever_live_p (RS6000_PIC_OFFSET_TABLE_REGNUM))
16068 df_set_regs_ever_live (RS6000_PIC_OFFSET_TABLE_REGNUM, true);
16070 crtl->uses_pic_offset_table = 1;
16072 return pic_offset_table_rtx;
16075 static rs6000_stack_t stack_info;
16077 /* Function to init struct machine_function.
16078 This will be called, via a pointer variable,
16079 from push_function_context. */
16081 static struct machine_function *
16082 rs6000_init_machine_status (void)
16084 stack_info.reload_completed = 0;
16085 return ggc_alloc_cleared_machine_function ();
16088 #define INT_P(X) (GET_CODE (X) == CONST_INT && GET_MODE (X) == VOIDmode)
16091 extract_MB (rtx op)
16093 int i;
16094 unsigned long val = INTVAL (op);
16096 /* If the high bit is zero, the value is the first 1 bit we find
16097 from the left. */
16098 if ((val & 0x80000000) == 0)
16100 gcc_assert (val & 0xffffffff);
16102 i = 1;
16103 while (((val <<= 1) & 0x80000000) == 0)
16104 ++i;
16105 return i;
16108 /* If the high bit is set and the low bit is not, or the mask is all
16109 1's, the value is zero. */
16110 if ((val & 1) == 0 || (val & 0xffffffff) == 0xffffffff)
16111 return 0;
16113 /* Otherwise we have a wrap-around mask. Look for the first 0 bit
16114 from the right. */
16115 i = 31;
16116 while (((val >>= 1) & 1) != 0)
16117 --i;
16119 return i;
16123 extract_ME (rtx op)
16125 int i;
16126 unsigned long val = INTVAL (op);
16128 /* If the low bit is zero, the value is the first 1 bit we find from
16129 the right. */
16130 if ((val & 1) == 0)
16132 gcc_assert (val & 0xffffffff);
16134 i = 30;
16135 while (((val >>= 1) & 1) == 0)
16136 --i;
16138 return i;
16141 /* If the low bit is set and the high bit is not, or the mask is all
16142 1's, the value is 31. */
16143 if ((val & 0x80000000) == 0 || (val & 0xffffffff) == 0xffffffff)
16144 return 31;
16146 /* Otherwise we have a wrap-around mask. Look for the first 0 bit
16147 from the left. */
16148 i = 0;
16149 while (((val <<= 1) & 0x80000000) != 0)
16150 ++i;
16152 return i;
16155 /* Locate some local-dynamic symbol still in use by this function
16156 so that we can print its name in some tls_ld pattern. */
16158 static const char *
16159 rs6000_get_some_local_dynamic_name (void)
16161 rtx insn;
16163 if (cfun->machine->some_ld_name)
16164 return cfun->machine->some_ld_name;
16166 for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
16167 if (INSN_P (insn)
16168 && for_each_rtx (&PATTERN (insn),
16169 rs6000_get_some_local_dynamic_name_1, 0))
16170 return cfun->machine->some_ld_name;
16172 gcc_unreachable ();
16175 /* Helper function for rs6000_get_some_local_dynamic_name. */
16177 static int
16178 rs6000_get_some_local_dynamic_name_1 (rtx *px, void *data ATTRIBUTE_UNUSED)
16180 rtx x = *px;
16182 if (GET_CODE (x) == SYMBOL_REF)
16184 const char *str = XSTR (x, 0);
16185 if (SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_DYNAMIC)
16187 cfun->machine->some_ld_name = str;
16188 return 1;
16192 return 0;
16195 /* Write out a function code label. */
16197 void
16198 rs6000_output_function_entry (FILE *file, const char *fname)
16200 if (fname[0] != '.')
16202 switch (DEFAULT_ABI)
16204 default:
16205 gcc_unreachable ();
16207 case ABI_AIX:
16208 if (DOT_SYMBOLS)
16209 putc ('.', file);
16210 else
16211 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "L.");
16212 break;
16214 case ABI_V4:
16215 case ABI_DARWIN:
16216 break;
16220 RS6000_OUTPUT_BASENAME (file, fname);
16223 /* Print an operand. Recognize special options, documented below. */
16225 #if TARGET_ELF
16226 #define SMALL_DATA_RELOC ((rs6000_sdata == SDATA_EABI) ? "sda21" : "sdarel")
16227 #define SMALL_DATA_REG ((rs6000_sdata == SDATA_EABI) ? 0 : 13)
16228 #else
16229 #define SMALL_DATA_RELOC "sda21"
16230 #define SMALL_DATA_REG 0
16231 #endif
16233 void
16234 print_operand (FILE *file, rtx x, int code)
16236 int i;
16237 unsigned HOST_WIDE_INT uval;
16239 switch (code)
16241 /* %a is output_address. */
16243 case 'b':
16244 /* If constant, low-order 16 bits of constant, unsigned.
16245 Otherwise, write normally. */
16246 if (INT_P (x))
16247 fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) & 0xffff);
16248 else
16249 print_operand (file, x, 0);
16250 return;
16252 case 'B':
16253 /* If the low-order bit is zero, write 'r'; otherwise, write 'l'
16254 for 64-bit mask direction. */
16255 putc (((INTVAL (x) & 1) == 0 ? 'r' : 'l'), file);
16256 return;
16258 /* %c is output_addr_const if a CONSTANT_ADDRESS_P, otherwise
16259 output_operand. */
16261 case 'D':
16262 /* Like 'J' but get to the GT bit only. */
16263 gcc_assert (REG_P (x));
16265 /* Bit 1 is GT bit. */
16266 i = 4 * (REGNO (x) - CR0_REGNO) + 1;
16268 /* Add one for shift count in rlinm for scc. */
16269 fprintf (file, "%d", i + 1);
16270 return;
16272 case 'E':
16273 /* X is a CR register. Print the number of the EQ bit of the CR */
16274 if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
16275 output_operand_lossage ("invalid %%E value");
16276 else
16277 fprintf (file, "%d", 4 * (REGNO (x) - CR0_REGNO) + 2);
16278 return;
16280 case 'f':
16281 /* X is a CR register. Print the shift count needed to move it
16282 to the high-order four bits. */
16283 if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
16284 output_operand_lossage ("invalid %%f value");
16285 else
16286 fprintf (file, "%d", 4 * (REGNO (x) - CR0_REGNO));
16287 return;
16289 case 'F':
16290 /* Similar, but print the count for the rotate in the opposite
16291 direction. */
16292 if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
16293 output_operand_lossage ("invalid %%F value");
16294 else
16295 fprintf (file, "%d", 32 - 4 * (REGNO (x) - CR0_REGNO));
16296 return;
16298 case 'G':
16299 /* X is a constant integer. If it is negative, print "m",
16300 otherwise print "z". This is to make an aze or ame insn. */
16301 if (GET_CODE (x) != CONST_INT)
16302 output_operand_lossage ("invalid %%G value");
16303 else if (INTVAL (x) >= 0)
16304 putc ('z', file);
16305 else
16306 putc ('m', file);
16307 return;
16309 case 'h':
16310 /* If constant, output low-order five bits. Otherwise, write
16311 normally. */
16312 if (INT_P (x))
16313 fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) & 31);
16314 else
16315 print_operand (file, x, 0);
16316 return;
16318 case 'H':
16319 /* If constant, output low-order six bits. Otherwise, write
16320 normally. */
16321 if (INT_P (x))
16322 fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) & 63);
16323 else
16324 print_operand (file, x, 0);
16325 return;
16327 case 'I':
16328 /* Print `i' if this is a constant, else nothing. */
16329 if (INT_P (x))
16330 putc ('i', file);
16331 return;
16333 case 'j':
16334 /* Write the bit number in CCR for jump. */
16335 i = ccr_bit (x, 0);
16336 if (i == -1)
16337 output_operand_lossage ("invalid %%j code");
16338 else
16339 fprintf (file, "%d", i);
16340 return;
16342 case 'J':
16343 /* Similar, but add one for shift count in rlinm for scc and pass
16344 scc flag to `ccr_bit'. */
16345 i = ccr_bit (x, 1);
16346 if (i == -1)
16347 output_operand_lossage ("invalid %%J code");
16348 else
16349 /* If we want bit 31, write a shift count of zero, not 32. */
16350 fprintf (file, "%d", i == 31 ? 0 : i + 1);
16351 return;
16353 case 'k':
16354 /* X must be a constant. Write the 1's complement of the
16355 constant. */
16356 if (! INT_P (x))
16357 output_operand_lossage ("invalid %%k value");
16358 else
16359 fprintf (file, HOST_WIDE_INT_PRINT_DEC, ~ INTVAL (x));
16360 return;
16362 case 'K':
16363 /* X must be a symbolic constant on ELF. Write an
16364 expression suitable for an 'addi' that adds in the low 16
16365 bits of the MEM. */
16366 if (GET_CODE (x) == CONST)
16368 if (GET_CODE (XEXP (x, 0)) != PLUS
16369 || (GET_CODE (XEXP (XEXP (x, 0), 0)) != SYMBOL_REF
16370 && GET_CODE (XEXP (XEXP (x, 0), 0)) != LABEL_REF)
16371 || GET_CODE (XEXP (XEXP (x, 0), 1)) != CONST_INT)
16372 output_operand_lossage ("invalid %%K value");
16374 print_operand_address (file, x);
16375 fputs ("@l", file);
16376 return;
16378 /* %l is output_asm_label. */
16380 case 'L':
16381 /* Write second word of DImode or DFmode reference. Works on register
16382 or non-indexed memory only. */
16383 if (REG_P (x))
16384 fputs (reg_names[REGNO (x) + 1], file);
16385 else if (MEM_P (x))
16387 /* Handle possible auto-increment. Since it is pre-increment and
16388 we have already done it, we can just use an offset of word. */
16389 if (GET_CODE (XEXP (x, 0)) == PRE_INC
16390 || GET_CODE (XEXP (x, 0)) == PRE_DEC)
16391 output_address (plus_constant (Pmode, XEXP (XEXP (x, 0), 0),
16392 UNITS_PER_WORD));
16393 else if (GET_CODE (XEXP (x, 0)) == PRE_MODIFY)
16394 output_address (plus_constant (Pmode, XEXP (XEXP (x, 0), 0),
16395 UNITS_PER_WORD));
16396 else
16397 output_address (XEXP (adjust_address_nv (x, SImode,
16398 UNITS_PER_WORD),
16399 0));
16401 if (small_data_operand (x, GET_MODE (x)))
16402 fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
16403 reg_names[SMALL_DATA_REG]);
16405 return;
16407 case 'm':
16408 /* MB value for a mask operand. */
16409 if (! mask_operand (x, SImode))
16410 output_operand_lossage ("invalid %%m value");
16412 fprintf (file, "%d", extract_MB (x));
16413 return;
16415 case 'M':
16416 /* ME value for a mask operand. */
16417 if (! mask_operand (x, SImode))
16418 output_operand_lossage ("invalid %%M value");
16420 fprintf (file, "%d", extract_ME (x));
16421 return;
16423 /* %n outputs the negative of its operand. */
16425 case 'N':
16426 /* Write the number of elements in the vector times 4. */
16427 if (GET_CODE (x) != PARALLEL)
16428 output_operand_lossage ("invalid %%N value");
16429 else
16430 fprintf (file, "%d", XVECLEN (x, 0) * 4);
16431 return;
16433 case 'O':
16434 /* Similar, but subtract 1 first. */
16435 if (GET_CODE (x) != PARALLEL)
16436 output_operand_lossage ("invalid %%O value");
16437 else
16438 fprintf (file, "%d", (XVECLEN (x, 0) - 1) * 4);
16439 return;
16441 case 'p':
16442 /* X is a CONST_INT that is a power of two. Output the logarithm. */
16443 if (! INT_P (x)
16444 || INTVAL (x) < 0
16445 || (i = exact_log2 (INTVAL (x))) < 0)
16446 output_operand_lossage ("invalid %%p value");
16447 else
16448 fprintf (file, "%d", i);
16449 return;
16451 case 'P':
16452 /* The operand must be an indirect memory reference. The result
16453 is the register name. */
16454 if (GET_CODE (x) != MEM || GET_CODE (XEXP (x, 0)) != REG
16455 || REGNO (XEXP (x, 0)) >= 32)
16456 output_operand_lossage ("invalid %%P value");
16457 else
16458 fputs (reg_names[REGNO (XEXP (x, 0))], file);
16459 return;
16461 case 'q':
16462 /* This outputs the logical code corresponding to a boolean
16463 expression. The expression may have one or both operands
16464 negated (if one, only the first one). For condition register
16465 logical operations, it will also treat the negated
16466 CR codes as NOTs, but not handle NOTs of them. */
16468 const char *const *t = 0;
16469 const char *s;
16470 enum rtx_code code = GET_CODE (x);
16471 static const char * const tbl[3][3] = {
16472 { "and", "andc", "nor" },
16473 { "or", "orc", "nand" },
16474 { "xor", "eqv", "xor" } };
16476 if (code == AND)
16477 t = tbl[0];
16478 else if (code == IOR)
16479 t = tbl[1];
16480 else if (code == XOR)
16481 t = tbl[2];
16482 else
16483 output_operand_lossage ("invalid %%q value");
16485 if (GET_CODE (XEXP (x, 0)) != NOT)
16486 s = t[0];
16487 else
16489 if (GET_CODE (XEXP (x, 1)) == NOT)
16490 s = t[2];
16491 else
16492 s = t[1];
16495 fputs (s, file);
16497 return;
16499 case 'Q':
16500 if (! TARGET_MFCRF)
16501 return;
16502 fputc (',', file);
16503 /* FALLTHRU */
16505 case 'R':
16506 /* X is a CR register. Print the mask for `mtcrf'. */
16507 if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
16508 output_operand_lossage ("invalid %%R value");
16509 else
16510 fprintf (file, "%d", 128 >> (REGNO (x) - CR0_REGNO));
16511 return;
16513 case 's':
16514 /* Low 5 bits of 32 - value */
16515 if (! INT_P (x))
16516 output_operand_lossage ("invalid %%s value");
16517 else
16518 fprintf (file, HOST_WIDE_INT_PRINT_DEC, (32 - INTVAL (x)) & 31);
16519 return;
16521 case 'S':
16522 /* PowerPC64 mask position. All 0's is excluded.
16523 CONST_INT 32-bit mask is considered sign-extended so any
16524 transition must occur within the CONST_INT, not on the boundary. */
16525 if (! mask64_operand (x, DImode))
16526 output_operand_lossage ("invalid %%S value");
16528 uval = INTVAL (x);
16530 if (uval & 1) /* Clear Left */
16532 #if HOST_BITS_PER_WIDE_INT > 64
16533 uval &= ((unsigned HOST_WIDE_INT) 1 << 64) - 1;
16534 #endif
16535 i = 64;
16537 else /* Clear Right */
16539 uval = ~uval;
16540 #if HOST_BITS_PER_WIDE_INT > 64
16541 uval &= ((unsigned HOST_WIDE_INT) 1 << 64) - 1;
16542 #endif
16543 i = 63;
16545 while (uval != 0)
16546 --i, uval >>= 1;
16547 gcc_assert (i >= 0);
16548 fprintf (file, "%d", i);
16549 return;
16551 case 't':
16552 /* Like 'J' but get to the OVERFLOW/UNORDERED bit. */
16553 gcc_assert (REG_P (x) && GET_MODE (x) == CCmode);
16555 /* Bit 3 is OV bit. */
16556 i = 4 * (REGNO (x) - CR0_REGNO) + 3;
16558 /* If we want bit 31, write a shift count of zero, not 32. */
16559 fprintf (file, "%d", i == 31 ? 0 : i + 1);
16560 return;
16562 case 'T':
16563 /* Print the symbolic name of a branch target register. */
16564 if (GET_CODE (x) != REG || (REGNO (x) != LR_REGNO
16565 && REGNO (x) != CTR_REGNO))
16566 output_operand_lossage ("invalid %%T value");
16567 else if (REGNO (x) == LR_REGNO)
16568 fputs ("lr", file);
16569 else
16570 fputs ("ctr", file);
16571 return;
16573 case 'u':
16574 /* High-order 16 bits of constant for use in unsigned operand. */
16575 if (! INT_P (x))
16576 output_operand_lossage ("invalid %%u value");
16577 else
16578 fprintf (file, HOST_WIDE_INT_PRINT_HEX,
16579 (INTVAL (x) >> 16) & 0xffff);
16580 return;
16582 case 'v':
16583 /* High-order 16 bits of constant for use in signed operand. */
16584 if (! INT_P (x))
16585 output_operand_lossage ("invalid %%v value");
16586 else
16587 fprintf (file, HOST_WIDE_INT_PRINT_HEX,
16588 (INTVAL (x) >> 16) & 0xffff);
16589 return;
16591 case 'U':
16592 /* Print `u' if this has an auto-increment or auto-decrement. */
16593 if (MEM_P (x)
16594 && (GET_CODE (XEXP (x, 0)) == PRE_INC
16595 || GET_CODE (XEXP (x, 0)) == PRE_DEC
16596 || GET_CODE (XEXP (x, 0)) == PRE_MODIFY))
16597 putc ('u', file);
16598 return;
16600 case 'V':
16601 /* Print the trap code for this operand. */
16602 switch (GET_CODE (x))
16604 case EQ:
16605 fputs ("eq", file); /* 4 */
16606 break;
16607 case NE:
16608 fputs ("ne", file); /* 24 */
16609 break;
16610 case LT:
16611 fputs ("lt", file); /* 16 */
16612 break;
16613 case LE:
16614 fputs ("le", file); /* 20 */
16615 break;
16616 case GT:
16617 fputs ("gt", file); /* 8 */
16618 break;
16619 case GE:
16620 fputs ("ge", file); /* 12 */
16621 break;
16622 case LTU:
16623 fputs ("llt", file); /* 2 */
16624 break;
16625 case LEU:
16626 fputs ("lle", file); /* 6 */
16627 break;
16628 case GTU:
16629 fputs ("lgt", file); /* 1 */
16630 break;
16631 case GEU:
16632 fputs ("lge", file); /* 5 */
16633 break;
16634 default:
16635 gcc_unreachable ();
16637 break;
16639 case 'w':
16640 /* If constant, low-order 16 bits of constant, signed. Otherwise, write
16641 normally. */
16642 if (INT_P (x))
16643 fprintf (file, HOST_WIDE_INT_PRINT_DEC,
16644 ((INTVAL (x) & 0xffff) ^ 0x8000) - 0x8000);
16645 else
16646 print_operand (file, x, 0);
16647 return;
16649 case 'W':
16650 /* MB value for a PowerPC64 rldic operand. */
16651 i = clz_hwi (INTVAL (x));
16653 fprintf (file, "%d", i);
16654 return;
16656 case 'x':
16657 /* X is a FPR or Altivec register used in a VSX context. */
16658 if (GET_CODE (x) != REG || !VSX_REGNO_P (REGNO (x)))
16659 output_operand_lossage ("invalid %%x value");
16660 else
16662 int reg = REGNO (x);
16663 int vsx_reg = (FP_REGNO_P (reg)
16664 ? reg - 32
16665 : reg - FIRST_ALTIVEC_REGNO + 32);
16667 #ifdef TARGET_REGNAMES
16668 if (TARGET_REGNAMES)
16669 fprintf (file, "%%vs%d", vsx_reg);
16670 else
16671 #endif
16672 fprintf (file, "%d", vsx_reg);
16674 return;
16676 case 'X':
16677 if (MEM_P (x)
16678 && (legitimate_indexed_address_p (XEXP (x, 0), 0)
16679 || (GET_CODE (XEXP (x, 0)) == PRE_MODIFY
16680 && legitimate_indexed_address_p (XEXP (XEXP (x, 0), 1), 0))))
16681 putc ('x', file);
16682 return;
16684 case 'Y':
16685 /* Like 'L', for third word of TImode/PTImode */
16686 if (REG_P (x))
16687 fputs (reg_names[REGNO (x) + 2], file);
16688 else if (MEM_P (x))
16690 if (GET_CODE (XEXP (x, 0)) == PRE_INC
16691 || GET_CODE (XEXP (x, 0)) == PRE_DEC)
16692 output_address (plus_constant (Pmode, XEXP (XEXP (x, 0), 0), 8));
16693 else if (GET_CODE (XEXP (x, 0)) == PRE_MODIFY)
16694 output_address (plus_constant (Pmode, XEXP (XEXP (x, 0), 0), 8));
16695 else
16696 output_address (XEXP (adjust_address_nv (x, SImode, 8), 0));
16697 if (small_data_operand (x, GET_MODE (x)))
16698 fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
16699 reg_names[SMALL_DATA_REG]);
16701 return;
16703 case 'z':
16704 /* X is a SYMBOL_REF. Write out the name preceded by a
16705 period and without any trailing data in brackets. Used for function
16706 names. If we are configured for System V (or the embedded ABI) on
16707 the PowerPC, do not emit the period, since those systems do not use
16708 TOCs and the like. */
16709 gcc_assert (GET_CODE (x) == SYMBOL_REF);
16711 /* For macho, check to see if we need a stub. */
16712 if (TARGET_MACHO)
16714 const char *name = XSTR (x, 0);
16715 #if TARGET_MACHO
16716 if (darwin_emit_branch_islands
16717 && MACHOPIC_INDIRECT
16718 && machopic_classify_symbol (x) == MACHOPIC_UNDEFINED_FUNCTION)
16719 name = machopic_indirection_name (x, /*stub_p=*/true);
16720 #endif
16721 assemble_name (file, name);
16723 else if (!DOT_SYMBOLS)
16724 assemble_name (file, XSTR (x, 0));
16725 else
16726 rs6000_output_function_entry (file, XSTR (x, 0));
16727 return;
16729 case 'Z':
16730 /* Like 'L', for last word of TImode/PTImode. */
16731 if (REG_P (x))
16732 fputs (reg_names[REGNO (x) + 3], file);
16733 else if (MEM_P (x))
16735 if (GET_CODE (XEXP (x, 0)) == PRE_INC
16736 || GET_CODE (XEXP (x, 0)) == PRE_DEC)
16737 output_address (plus_constant (Pmode, XEXP (XEXP (x, 0), 0), 12));
16738 else if (GET_CODE (XEXP (x, 0)) == PRE_MODIFY)
16739 output_address (plus_constant (Pmode, XEXP (XEXP (x, 0), 0), 12));
16740 else
16741 output_address (XEXP (adjust_address_nv (x, SImode, 12), 0));
16742 if (small_data_operand (x, GET_MODE (x)))
16743 fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
16744 reg_names[SMALL_DATA_REG]);
16746 return;
16748 /* Print AltiVec or SPE memory operand. */
16749 case 'y':
16751 rtx tmp;
16753 gcc_assert (MEM_P (x));
16755 tmp = XEXP (x, 0);
16757 /* Ugly hack because %y is overloaded. */
16758 if ((TARGET_SPE || TARGET_E500_DOUBLE)
16759 && (GET_MODE_SIZE (GET_MODE (x)) == 8
16760 || GET_MODE (x) == TFmode
16761 || GET_MODE (x) == TImode
16762 || GET_MODE (x) == PTImode))
16764 /* Handle [reg]. */
16765 if (REG_P (tmp))
16767 fprintf (file, "0(%s)", reg_names[REGNO (tmp)]);
16768 break;
16770 /* Handle [reg+UIMM]. */
16771 else if (GET_CODE (tmp) == PLUS &&
16772 GET_CODE (XEXP (tmp, 1)) == CONST_INT)
16774 int x;
16776 gcc_assert (REG_P (XEXP (tmp, 0)));
16778 x = INTVAL (XEXP (tmp, 1));
16779 fprintf (file, "%d(%s)", x, reg_names[REGNO (XEXP (tmp, 0))]);
16780 break;
16783 /* Fall through. Must be [reg+reg]. */
16785 if (VECTOR_MEM_ALTIVEC_P (GET_MODE (x))
16786 && GET_CODE (tmp) == AND
16787 && GET_CODE (XEXP (tmp, 1)) == CONST_INT
16788 && INTVAL (XEXP (tmp, 1)) == -16)
16789 tmp = XEXP (tmp, 0);
16790 else if (VECTOR_MEM_VSX_P (GET_MODE (x))
16791 && GET_CODE (tmp) == PRE_MODIFY)
16792 tmp = XEXP (tmp, 1);
16793 if (REG_P (tmp))
16794 fprintf (file, "0,%s", reg_names[REGNO (tmp)]);
16795 else
16797 if (!GET_CODE (tmp) == PLUS
16798 || !REG_P (XEXP (tmp, 0))
16799 || !REG_P (XEXP (tmp, 1)))
16801 output_operand_lossage ("invalid %%y value, try using the 'Z' constraint");
16802 break;
16805 if (REGNO (XEXP (tmp, 0)) == 0)
16806 fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (tmp, 1)) ],
16807 reg_names[ REGNO (XEXP (tmp, 0)) ]);
16808 else
16809 fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (tmp, 0)) ],
16810 reg_names[ REGNO (XEXP (tmp, 1)) ]);
16812 break;
16815 case 0:
16816 if (REG_P (x))
16817 fprintf (file, "%s", reg_names[REGNO (x)]);
16818 else if (MEM_P (x))
16820 /* We need to handle PRE_INC and PRE_DEC here, since we need to
16821 know the width from the mode. */
16822 if (GET_CODE (XEXP (x, 0)) == PRE_INC)
16823 fprintf (file, "%d(%s)", GET_MODE_SIZE (GET_MODE (x)),
16824 reg_names[REGNO (XEXP (XEXP (x, 0), 0))]);
16825 else if (GET_CODE (XEXP (x, 0)) == PRE_DEC)
16826 fprintf (file, "%d(%s)", - GET_MODE_SIZE (GET_MODE (x)),
16827 reg_names[REGNO (XEXP (XEXP (x, 0), 0))]);
16828 else if (GET_CODE (XEXP (x, 0)) == PRE_MODIFY)
16829 output_address (XEXP (XEXP (x, 0), 1));
16830 else
16831 output_address (XEXP (x, 0));
16833 else
16835 if (toc_relative_expr_p (x, false))
16836 /* This hack along with a corresponding hack in
16837 rs6000_output_addr_const_extra arranges to output addends
16838 where the assembler expects to find them. eg.
16839 (plus (unspec [(symbol_ref ("x")) (reg 2)] tocrel) 4)
16840 without this hack would be output as "x@toc+4". We
16841 want "x+4@toc". */
16842 output_addr_const (file, CONST_CAST_RTX (tocrel_base));
16843 else
16844 output_addr_const (file, x);
16846 return;
16848 case '&':
16849 assemble_name (file, rs6000_get_some_local_dynamic_name ());
16850 return;
16852 default:
16853 output_operand_lossage ("invalid %%xn code");
16857 /* Print the address of an operand. */
16859 void
16860 print_operand_address (FILE *file, rtx x)
16862 if (REG_P (x))
16863 fprintf (file, "0(%s)", reg_names[ REGNO (x) ]);
16864 else if (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == CONST
16865 || GET_CODE (x) == LABEL_REF)
16867 output_addr_const (file, x);
16868 if (small_data_operand (x, GET_MODE (x)))
16869 fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
16870 reg_names[SMALL_DATA_REG]);
16871 else
16872 gcc_assert (!TARGET_TOC);
16874 else if (GET_CODE (x) == PLUS && REG_P (XEXP (x, 0))
16875 && REG_P (XEXP (x, 1)))
16877 if (REGNO (XEXP (x, 0)) == 0)
16878 fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (x, 1)) ],
16879 reg_names[ REGNO (XEXP (x, 0)) ]);
16880 else
16881 fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (x, 0)) ],
16882 reg_names[ REGNO (XEXP (x, 1)) ]);
16884 else if (GET_CODE (x) == PLUS && REG_P (XEXP (x, 0))
16885 && GET_CODE (XEXP (x, 1)) == CONST_INT)
16886 fprintf (file, HOST_WIDE_INT_PRINT_DEC "(%s)",
16887 INTVAL (XEXP (x, 1)), reg_names[ REGNO (XEXP (x, 0)) ]);
16888 #if TARGET_MACHO
16889 else if (GET_CODE (x) == LO_SUM && REG_P (XEXP (x, 0))
16890 && CONSTANT_P (XEXP (x, 1)))
16892 fprintf (file, "lo16(");
16893 output_addr_const (file, XEXP (x, 1));
16894 fprintf (file, ")(%s)", reg_names[ REGNO (XEXP (x, 0)) ]);
16896 #endif
16897 #if TARGET_ELF
16898 else if (GET_CODE (x) == LO_SUM && REG_P (XEXP (x, 0))
16899 && CONSTANT_P (XEXP (x, 1)))
16901 output_addr_const (file, XEXP (x, 1));
16902 fprintf (file, "@l(%s)", reg_names[ REGNO (XEXP (x, 0)) ]);
16904 #endif
16905 else if (toc_relative_expr_p (x, false))
16907 /* This hack along with a corresponding hack in
16908 rs6000_output_addr_const_extra arranges to output addends
16909 where the assembler expects to find them. eg.
16910 (lo_sum (reg 9)
16911 . (plus (unspec [(symbol_ref ("x")) (reg 2)] tocrel) 8))
16912 without this hack would be output as "x@toc+8@l(9)". We
16913 want "x+8@toc@l(9)". */
16914 output_addr_const (file, CONST_CAST_RTX (tocrel_base));
16915 if (GET_CODE (x) == LO_SUM)
16916 fprintf (file, "@l(%s)", reg_names[REGNO (XEXP (x, 0))]);
16917 else
16918 fprintf (file, "(%s)", reg_names[REGNO (XVECEXP (tocrel_base, 0, 1))]);
16920 else
16921 gcc_unreachable ();
16924 /* Implement TARGET_OUTPUT_ADDR_CONST_EXTRA. */
16926 static bool
16927 rs6000_output_addr_const_extra (FILE *file, rtx x)
16929 if (GET_CODE (x) == UNSPEC)
16930 switch (XINT (x, 1))
16932 case UNSPEC_TOCREL:
16933 gcc_checking_assert (GET_CODE (XVECEXP (x, 0, 0)) == SYMBOL_REF
16934 && REG_P (XVECEXP (x, 0, 1))
16935 && REGNO (XVECEXP (x, 0, 1)) == TOC_REGISTER);
16936 output_addr_const (file, XVECEXP (x, 0, 0));
16937 if (x == tocrel_base && tocrel_offset != const0_rtx)
16939 if (INTVAL (tocrel_offset) >= 0)
16940 fprintf (file, "+");
16941 output_addr_const (file, CONST_CAST_RTX (tocrel_offset));
16943 if (!TARGET_AIX || (TARGET_ELF && TARGET_MINIMAL_TOC))
16945 putc ('-', file);
16946 assemble_name (file, toc_label_name);
16948 else if (TARGET_ELF)
16949 fputs ("@toc", file);
16950 return true;
16952 #if TARGET_MACHO
16953 case UNSPEC_MACHOPIC_OFFSET:
16954 output_addr_const (file, XVECEXP (x, 0, 0));
16955 putc ('-', file);
16956 machopic_output_function_base_name (file);
16957 return true;
16958 #endif
16960 return false;
16963 /* Target hook for assembling integer objects. The PowerPC version has
16964 to handle fixup entries for relocatable code if RELOCATABLE_NEEDS_FIXUP
16965 is defined. It also needs to handle DI-mode objects on 64-bit
16966 targets. */
16968 static bool
16969 rs6000_assemble_integer (rtx x, unsigned int size, int aligned_p)
16971 #ifdef RELOCATABLE_NEEDS_FIXUP
16972 /* Special handling for SI values. */
16973 if (RELOCATABLE_NEEDS_FIXUP && size == 4 && aligned_p)
16975 static int recurse = 0;
16977 /* For -mrelocatable, we mark all addresses that need to be fixed up in
16978 the .fixup section. Since the TOC section is already relocated, we
16979 don't need to mark it here. We used to skip the text section, but it
16980 should never be valid for relocated addresses to be placed in the text
16981 section. */
16982 if (TARGET_RELOCATABLE
16983 && in_section != toc_section
16984 && !recurse
16985 && GET_CODE (x) != CONST_INT
16986 && GET_CODE (x) != CONST_DOUBLE
16987 && CONSTANT_P (x))
16989 char buf[256];
16991 recurse = 1;
16992 ASM_GENERATE_INTERNAL_LABEL (buf, "LCP", fixuplabelno);
16993 fixuplabelno++;
16994 ASM_OUTPUT_LABEL (asm_out_file, buf);
16995 fprintf (asm_out_file, "\t.long\t(");
16996 output_addr_const (asm_out_file, x);
16997 fprintf (asm_out_file, ")@fixup\n");
16998 fprintf (asm_out_file, "\t.section\t\".fixup\",\"aw\"\n");
16999 ASM_OUTPUT_ALIGN (asm_out_file, 2);
17000 fprintf (asm_out_file, "\t.long\t");
17001 assemble_name (asm_out_file, buf);
17002 fprintf (asm_out_file, "\n\t.previous\n");
17003 recurse = 0;
17004 return true;
17006 /* Remove initial .'s to turn a -mcall-aixdesc function
17007 address into the address of the descriptor, not the function
17008 itself. */
17009 else if (GET_CODE (x) == SYMBOL_REF
17010 && XSTR (x, 0)[0] == '.'
17011 && DEFAULT_ABI == ABI_AIX)
17013 const char *name = XSTR (x, 0);
17014 while (*name == '.')
17015 name++;
17017 fprintf (asm_out_file, "\t.long\t%s\n", name);
17018 return true;
17021 #endif /* RELOCATABLE_NEEDS_FIXUP */
17022 return default_assemble_integer (x, size, aligned_p);
17025 #if defined (HAVE_GAS_HIDDEN) && !TARGET_MACHO
17026 /* Emit an assembler directive to set symbol visibility for DECL to
17027 VISIBILITY_TYPE. */
17029 static void
17030 rs6000_assemble_visibility (tree decl, int vis)
17032 if (TARGET_XCOFF)
17033 return;
17035 /* Functions need to have their entry point symbol visibility set as
17036 well as their descriptor symbol visibility. */
17037 if (DEFAULT_ABI == ABI_AIX
17038 && DOT_SYMBOLS
17039 && TREE_CODE (decl) == FUNCTION_DECL)
17041 static const char * const visibility_types[] = {
17042 NULL, "internal", "hidden", "protected"
17045 const char *name, *type;
17047 name = ((* targetm.strip_name_encoding)
17048 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl))));
17049 type = visibility_types[vis];
17051 fprintf (asm_out_file, "\t.%s\t%s\n", type, name);
17052 fprintf (asm_out_file, "\t.%s\t.%s\n", type, name);
17054 else
17055 default_assemble_visibility (decl, vis);
17057 #endif
17059 enum rtx_code
17060 rs6000_reverse_condition (enum machine_mode mode, enum rtx_code code)
17062 /* Reversal of FP compares takes care -- an ordered compare
17063 becomes an unordered compare and vice versa. */
17064 if (mode == CCFPmode
17065 && (!flag_finite_math_only
17066 || code == UNLT || code == UNLE || code == UNGT || code == UNGE
17067 || code == UNEQ || code == LTGT))
17068 return reverse_condition_maybe_unordered (code);
17069 else
17070 return reverse_condition (code);
17073 /* Generate a compare for CODE. Return a brand-new rtx that
17074 represents the result of the compare. */
17076 static rtx
17077 rs6000_generate_compare (rtx cmp, enum machine_mode mode)
17079 enum machine_mode comp_mode;
17080 rtx compare_result;
17081 enum rtx_code code = GET_CODE (cmp);
17082 rtx op0 = XEXP (cmp, 0);
17083 rtx op1 = XEXP (cmp, 1);
17085 if (FLOAT_MODE_P (mode))
17086 comp_mode = CCFPmode;
17087 else if (code == GTU || code == LTU
17088 || code == GEU || code == LEU)
17089 comp_mode = CCUNSmode;
17090 else if ((code == EQ || code == NE)
17091 && unsigned_reg_p (op0)
17092 && (unsigned_reg_p (op1)
17093 || (CONST_INT_P (op1) && INTVAL (op1) != 0)))
17094 /* These are unsigned values, perhaps there will be a later
17095 ordering compare that can be shared with this one. */
17096 comp_mode = CCUNSmode;
17097 else
17098 comp_mode = CCmode;
17100 /* If we have an unsigned compare, make sure we don't have a signed value as
17101 an immediate. */
17102 if (comp_mode == CCUNSmode && GET_CODE (op1) == CONST_INT
17103 && INTVAL (op1) < 0)
17105 op0 = copy_rtx_if_shared (op0);
17106 op1 = force_reg (GET_MODE (op0), op1);
17107 cmp = gen_rtx_fmt_ee (code, GET_MODE (cmp), op0, op1);
17110 /* First, the compare. */
17111 compare_result = gen_reg_rtx (comp_mode);
17113 /* E500 FP compare instructions on the GPRs. Yuck! */
17114 if ((!TARGET_FPRS && TARGET_HARD_FLOAT)
17115 && FLOAT_MODE_P (mode))
17117 rtx cmp, or_result, compare_result2;
17118 enum machine_mode op_mode = GET_MODE (op0);
17119 bool reverse_p;
17121 if (op_mode == VOIDmode)
17122 op_mode = GET_MODE (op1);
17124 /* First reverse the condition codes that aren't directly supported. */
17125 switch (code)
17127 case NE:
17128 case UNLT:
17129 case UNLE:
17130 case UNGT:
17131 case UNGE:
17132 code = reverse_condition_maybe_unordered (code);
17133 reverse_p = true;
17134 break;
17136 case EQ:
17137 case LT:
17138 case LE:
17139 case GT:
17140 case GE:
17141 reverse_p = false;
17142 break;
17144 default:
17145 gcc_unreachable ();
17148 /* The E500 FP compare instructions toggle the GT bit (CR bit 1) only.
17149 This explains the following mess. */
17151 switch (code)
17153 case EQ:
17154 switch (op_mode)
17156 case SFmode:
17157 cmp = (flag_finite_math_only && !flag_trapping_math)
17158 ? gen_tstsfeq_gpr (compare_result, op0, op1)
17159 : gen_cmpsfeq_gpr (compare_result, op0, op1);
17160 break;
17162 case DFmode:
17163 cmp = (flag_finite_math_only && !flag_trapping_math)
17164 ? gen_tstdfeq_gpr (compare_result, op0, op1)
17165 : gen_cmpdfeq_gpr (compare_result, op0, op1);
17166 break;
17168 case TFmode:
17169 cmp = (flag_finite_math_only && !flag_trapping_math)
17170 ? gen_tsttfeq_gpr (compare_result, op0, op1)
17171 : gen_cmptfeq_gpr (compare_result, op0, op1);
17172 break;
17174 default:
17175 gcc_unreachable ();
17177 break;
17179 case GT:
17180 case GE:
17181 switch (op_mode)
17183 case SFmode:
17184 cmp = (flag_finite_math_only && !flag_trapping_math)
17185 ? gen_tstsfgt_gpr (compare_result, op0, op1)
17186 : gen_cmpsfgt_gpr (compare_result, op0, op1);
17187 break;
17189 case DFmode:
17190 cmp = (flag_finite_math_only && !flag_trapping_math)
17191 ? gen_tstdfgt_gpr (compare_result, op0, op1)
17192 : gen_cmpdfgt_gpr (compare_result, op0, op1);
17193 break;
17195 case TFmode:
17196 cmp = (flag_finite_math_only && !flag_trapping_math)
17197 ? gen_tsttfgt_gpr (compare_result, op0, op1)
17198 : gen_cmptfgt_gpr (compare_result, op0, op1);
17199 break;
17201 default:
17202 gcc_unreachable ();
17204 break;
17206 case LT:
17207 case LE:
17208 switch (op_mode)
17210 case SFmode:
17211 cmp = (flag_finite_math_only && !flag_trapping_math)
17212 ? gen_tstsflt_gpr (compare_result, op0, op1)
17213 : gen_cmpsflt_gpr (compare_result, op0, op1);
17214 break;
17216 case DFmode:
17217 cmp = (flag_finite_math_only && !flag_trapping_math)
17218 ? gen_tstdflt_gpr (compare_result, op0, op1)
17219 : gen_cmpdflt_gpr (compare_result, op0, op1);
17220 break;
17222 case TFmode:
17223 cmp = (flag_finite_math_only && !flag_trapping_math)
17224 ? gen_tsttflt_gpr (compare_result, op0, op1)
17225 : gen_cmptflt_gpr (compare_result, op0, op1);
17226 break;
17228 default:
17229 gcc_unreachable ();
17231 break;
17233 default:
17234 gcc_unreachable ();
17237 /* Synthesize LE and GE from LT/GT || EQ. */
17238 if (code == LE || code == GE)
17240 emit_insn (cmp);
17242 compare_result2 = gen_reg_rtx (CCFPmode);
17244 /* Do the EQ. */
17245 switch (op_mode)
17247 case SFmode:
17248 cmp = (flag_finite_math_only && !flag_trapping_math)
17249 ? gen_tstsfeq_gpr (compare_result2, op0, op1)
17250 : gen_cmpsfeq_gpr (compare_result2, op0, op1);
17251 break;
17253 case DFmode:
17254 cmp = (flag_finite_math_only && !flag_trapping_math)
17255 ? gen_tstdfeq_gpr (compare_result2, op0, op1)
17256 : gen_cmpdfeq_gpr (compare_result2, op0, op1);
17257 break;
17259 case TFmode:
17260 cmp = (flag_finite_math_only && !flag_trapping_math)
17261 ? gen_tsttfeq_gpr (compare_result2, op0, op1)
17262 : gen_cmptfeq_gpr (compare_result2, op0, op1);
17263 break;
17265 default:
17266 gcc_unreachable ();
17269 emit_insn (cmp);
17271 /* OR them together. */
17272 or_result = gen_reg_rtx (CCFPmode);
17273 cmp = gen_e500_cr_ior_compare (or_result, compare_result,
17274 compare_result2);
17275 compare_result = or_result;
17278 code = reverse_p ? NE : EQ;
17280 emit_insn (cmp);
17282 else
17284 /* Generate XLC-compatible TFmode compare as PARALLEL with extra
17285 CLOBBERs to match cmptf_internal2 pattern. */
17286 if (comp_mode == CCFPmode && TARGET_XL_COMPAT
17287 && GET_MODE (op0) == TFmode
17288 && !TARGET_IEEEQUAD
17289 && TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_LONG_DOUBLE_128)
17290 emit_insn (gen_rtx_PARALLEL (VOIDmode,
17291 gen_rtvec (10,
17292 gen_rtx_SET (VOIDmode,
17293 compare_result,
17294 gen_rtx_COMPARE (comp_mode, op0, op1)),
17295 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
17296 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
17297 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
17298 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
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 (Pmode)))));
17304 else if (GET_CODE (op1) == UNSPEC
17305 && XINT (op1, 1) == UNSPEC_SP_TEST)
17307 rtx op1b = XVECEXP (op1, 0, 0);
17308 comp_mode = CCEQmode;
17309 compare_result = gen_reg_rtx (CCEQmode);
17310 if (TARGET_64BIT)
17311 emit_insn (gen_stack_protect_testdi (compare_result, op0, op1b));
17312 else
17313 emit_insn (gen_stack_protect_testsi (compare_result, op0, op1b));
17315 else
17316 emit_insn (gen_rtx_SET (VOIDmode, compare_result,
17317 gen_rtx_COMPARE (comp_mode, op0, op1)));
17320 /* Some kinds of FP comparisons need an OR operation;
17321 under flag_finite_math_only we don't bother. */
17322 if (FLOAT_MODE_P (mode)
17323 && !flag_finite_math_only
17324 && !(TARGET_HARD_FLOAT && !TARGET_FPRS)
17325 && (code == LE || code == GE
17326 || code == UNEQ || code == LTGT
17327 || code == UNGT || code == UNLT))
17329 enum rtx_code or1, or2;
17330 rtx or1_rtx, or2_rtx, compare2_rtx;
17331 rtx or_result = gen_reg_rtx (CCEQmode);
17333 switch (code)
17335 case LE: or1 = LT; or2 = EQ; break;
17336 case GE: or1 = GT; or2 = EQ; break;
17337 case UNEQ: or1 = UNORDERED; or2 = EQ; break;
17338 case LTGT: or1 = LT; or2 = GT; break;
17339 case UNGT: or1 = UNORDERED; or2 = GT; break;
17340 case UNLT: or1 = UNORDERED; or2 = LT; break;
17341 default: gcc_unreachable ();
17343 validate_condition_mode (or1, comp_mode);
17344 validate_condition_mode (or2, comp_mode);
17345 or1_rtx = gen_rtx_fmt_ee (or1, SImode, compare_result, const0_rtx);
17346 or2_rtx = gen_rtx_fmt_ee (or2, SImode, compare_result, const0_rtx);
17347 compare2_rtx = gen_rtx_COMPARE (CCEQmode,
17348 gen_rtx_IOR (SImode, or1_rtx, or2_rtx),
17349 const_true_rtx);
17350 emit_insn (gen_rtx_SET (VOIDmode, or_result, compare2_rtx));
17352 compare_result = or_result;
17353 code = EQ;
17356 validate_condition_mode (code, GET_MODE (compare_result));
17358 return gen_rtx_fmt_ee (code, VOIDmode, compare_result, const0_rtx);
17362 /* Emit the RTL for an sISEL pattern. */
17364 void
17365 rs6000_emit_sISEL (enum machine_mode mode ATTRIBUTE_UNUSED, rtx operands[])
17367 rs6000_emit_int_cmove (operands[0], operands[1], const1_rtx, const0_rtx);
17370 void
17371 rs6000_emit_sCOND (enum machine_mode mode, rtx operands[])
17373 rtx condition_rtx;
17374 enum machine_mode op_mode;
17375 enum rtx_code cond_code;
17376 rtx result = operands[0];
17378 if (TARGET_ISEL && (mode == SImode || mode == DImode))
17380 rs6000_emit_sISEL (mode, operands);
17381 return;
17384 condition_rtx = rs6000_generate_compare (operands[1], mode);
17385 cond_code = GET_CODE (condition_rtx);
17387 if (FLOAT_MODE_P (mode)
17388 && !TARGET_FPRS && TARGET_HARD_FLOAT)
17390 rtx t;
17392 PUT_MODE (condition_rtx, SImode);
17393 t = XEXP (condition_rtx, 0);
17395 gcc_assert (cond_code == NE || cond_code == EQ);
17397 if (cond_code == NE)
17398 emit_insn (gen_e500_flip_gt_bit (t, t));
17400 emit_insn (gen_move_from_CR_gt_bit (result, t));
17401 return;
17404 if (cond_code == NE
17405 || cond_code == GE || cond_code == LE
17406 || cond_code == GEU || cond_code == LEU
17407 || cond_code == ORDERED || cond_code == UNGE || cond_code == UNLE)
17409 rtx not_result = gen_reg_rtx (CCEQmode);
17410 rtx not_op, rev_cond_rtx;
17411 enum machine_mode cc_mode;
17413 cc_mode = GET_MODE (XEXP (condition_rtx, 0));
17415 rev_cond_rtx = gen_rtx_fmt_ee (rs6000_reverse_condition (cc_mode, cond_code),
17416 SImode, XEXP (condition_rtx, 0), const0_rtx);
17417 not_op = gen_rtx_COMPARE (CCEQmode, rev_cond_rtx, const0_rtx);
17418 emit_insn (gen_rtx_SET (VOIDmode, not_result, not_op));
17419 condition_rtx = gen_rtx_EQ (VOIDmode, not_result, const0_rtx);
17422 op_mode = GET_MODE (XEXP (operands[1], 0));
17423 if (op_mode == VOIDmode)
17424 op_mode = GET_MODE (XEXP (operands[1], 1));
17426 if (TARGET_POWERPC64 && (op_mode == DImode || FLOAT_MODE_P (mode)))
17428 PUT_MODE (condition_rtx, DImode);
17429 convert_move (result, condition_rtx, 0);
17431 else
17433 PUT_MODE (condition_rtx, SImode);
17434 emit_insn (gen_rtx_SET (VOIDmode, result, condition_rtx));
17438 /* Emit a branch of kind CODE to location LOC. */
17440 void
17441 rs6000_emit_cbranch (enum machine_mode mode, rtx operands[])
17443 rtx condition_rtx, loc_ref;
17445 condition_rtx = rs6000_generate_compare (operands[0], mode);
17446 loc_ref = gen_rtx_LABEL_REF (VOIDmode, operands[3]);
17447 emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx,
17448 gen_rtx_IF_THEN_ELSE (VOIDmode, condition_rtx,
17449 loc_ref, pc_rtx)));
17452 /* Return the string to output a conditional branch to LABEL, which is
17453 the operand template of the label, or NULL if the branch is really a
17454 conditional return.
17456 OP is the conditional expression. XEXP (OP, 0) is assumed to be a
17457 condition code register and its mode specifies what kind of
17458 comparison we made.
17460 REVERSED is nonzero if we should reverse the sense of the comparison.
17462 INSN is the insn. */
17464 char *
17465 output_cbranch (rtx op, const char *label, int reversed, rtx insn)
17467 static char string[64];
17468 enum rtx_code code = GET_CODE (op);
17469 rtx cc_reg = XEXP (op, 0);
17470 enum machine_mode mode = GET_MODE (cc_reg);
17471 int cc_regno = REGNO (cc_reg) - CR0_REGNO;
17472 int need_longbranch = label != NULL && get_attr_length (insn) == 8;
17473 int really_reversed = reversed ^ need_longbranch;
17474 char *s = string;
17475 const char *ccode;
17476 const char *pred;
17477 rtx note;
17479 validate_condition_mode (code, mode);
17481 /* Work out which way this really branches. We could use
17482 reverse_condition_maybe_unordered here always but this
17483 makes the resulting assembler clearer. */
17484 if (really_reversed)
17486 /* Reversal of FP compares takes care -- an ordered compare
17487 becomes an unordered compare and vice versa. */
17488 if (mode == CCFPmode)
17489 code = reverse_condition_maybe_unordered (code);
17490 else
17491 code = reverse_condition (code);
17494 if ((!TARGET_FPRS && TARGET_HARD_FLOAT) && mode == CCFPmode)
17496 /* The efscmp/tst* instructions twiddle bit 2, which maps nicely
17497 to the GT bit. */
17498 switch (code)
17500 case EQ:
17501 /* Opposite of GT. */
17502 code = GT;
17503 break;
17505 case NE:
17506 code = UNLE;
17507 break;
17509 default:
17510 gcc_unreachable ();
17514 switch (code)
17516 /* Not all of these are actually distinct opcodes, but
17517 we distinguish them for clarity of the resulting assembler. */
17518 case NE: case LTGT:
17519 ccode = "ne"; break;
17520 case EQ: case UNEQ:
17521 ccode = "eq"; break;
17522 case GE: case GEU:
17523 ccode = "ge"; break;
17524 case GT: case GTU: case UNGT:
17525 ccode = "gt"; break;
17526 case LE: case LEU:
17527 ccode = "le"; break;
17528 case LT: case LTU: case UNLT:
17529 ccode = "lt"; break;
17530 case UNORDERED: ccode = "un"; break;
17531 case ORDERED: ccode = "nu"; break;
17532 case UNGE: ccode = "nl"; break;
17533 case UNLE: ccode = "ng"; break;
17534 default:
17535 gcc_unreachable ();
17538 /* Maybe we have a guess as to how likely the branch is. */
17539 pred = "";
17540 note = find_reg_note (insn, REG_BR_PROB, NULL_RTX);
17541 if (note != NULL_RTX)
17543 /* PROB is the difference from 50%. */
17544 int prob = INTVAL (XEXP (note, 0)) - REG_BR_PROB_BASE / 2;
17546 /* Only hint for highly probable/improbable branches on newer
17547 cpus as static prediction overrides processor dynamic
17548 prediction. For older cpus we may as well always hint, but
17549 assume not taken for branches that are very close to 50% as a
17550 mispredicted taken branch is more expensive than a
17551 mispredicted not-taken branch. */
17552 if (rs6000_always_hint
17553 || (abs (prob) > REG_BR_PROB_BASE / 100 * 48
17554 && br_prob_note_reliable_p (note)))
17556 if (abs (prob) > REG_BR_PROB_BASE / 20
17557 && ((prob > 0) ^ need_longbranch))
17558 pred = "+";
17559 else
17560 pred = "-";
17564 if (label == NULL)
17565 s += sprintf (s, "b%slr%s ", ccode, pred);
17566 else
17567 s += sprintf (s, "b%s%s ", ccode, pred);
17569 /* We need to escape any '%' characters in the reg_names string.
17570 Assume they'd only be the first character.... */
17571 if (reg_names[cc_regno + CR0_REGNO][0] == '%')
17572 *s++ = '%';
17573 s += sprintf (s, "%s", reg_names[cc_regno + CR0_REGNO]);
17575 if (label != NULL)
17577 /* If the branch distance was too far, we may have to use an
17578 unconditional branch to go the distance. */
17579 if (need_longbranch)
17580 s += sprintf (s, ",$+8\n\tb %s", label);
17581 else
17582 s += sprintf (s, ",%s", label);
17585 return string;
17588 /* Return the string to flip the GT bit on a CR. */
17589 char *
17590 output_e500_flip_gt_bit (rtx dst, rtx src)
17592 static char string[64];
17593 int a, b;
17595 gcc_assert (GET_CODE (dst) == REG && CR_REGNO_P (REGNO (dst))
17596 && GET_CODE (src) == REG && CR_REGNO_P (REGNO (src)));
17598 /* GT bit. */
17599 a = 4 * (REGNO (dst) - CR0_REGNO) + 1;
17600 b = 4 * (REGNO (src) - CR0_REGNO) + 1;
17602 sprintf (string, "crnot %d,%d", a, b);
17603 return string;
17606 /* Return insn for VSX or Altivec comparisons. */
17608 static rtx
17609 rs6000_emit_vector_compare_inner (enum rtx_code code, rtx op0, rtx op1)
17611 rtx mask;
17612 enum machine_mode mode = GET_MODE (op0);
17614 switch (code)
17616 default:
17617 break;
17619 case GE:
17620 if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
17621 return NULL_RTX;
17623 case EQ:
17624 case GT:
17625 case GTU:
17626 case ORDERED:
17627 case UNORDERED:
17628 case UNEQ:
17629 case LTGT:
17630 mask = gen_reg_rtx (mode);
17631 emit_insn (gen_rtx_SET (VOIDmode,
17632 mask,
17633 gen_rtx_fmt_ee (code, mode, op0, op1)));
17634 return mask;
17637 return NULL_RTX;
17640 /* Emit vector compare for operands OP0 and OP1 using code RCODE.
17641 DMODE is expected destination mode. This is a recursive function. */
17643 static rtx
17644 rs6000_emit_vector_compare (enum rtx_code rcode,
17645 rtx op0, rtx op1,
17646 enum machine_mode dmode)
17648 rtx mask;
17649 bool swap_operands = false;
17650 bool try_again = false;
17652 gcc_assert (VECTOR_UNIT_ALTIVEC_OR_VSX_P (dmode));
17653 gcc_assert (GET_MODE (op0) == GET_MODE (op1));
17655 /* See if the comparison works as is. */
17656 mask = rs6000_emit_vector_compare_inner (rcode, op0, op1);
17657 if (mask)
17658 return mask;
17660 switch (rcode)
17662 case LT:
17663 rcode = GT;
17664 swap_operands = true;
17665 try_again = true;
17666 break;
17667 case LTU:
17668 rcode = GTU;
17669 swap_operands = true;
17670 try_again = true;
17671 break;
17672 case NE:
17673 case UNLE:
17674 case UNLT:
17675 case UNGE:
17676 case UNGT:
17677 /* Invert condition and try again.
17678 e.g., A != B becomes ~(A==B). */
17680 enum rtx_code rev_code;
17681 enum insn_code nor_code;
17682 rtx mask2;
17684 rev_code = reverse_condition_maybe_unordered (rcode);
17685 if (rev_code == UNKNOWN)
17686 return NULL_RTX;
17688 nor_code = optab_handler (one_cmpl_optab, dmode);
17689 if (nor_code == CODE_FOR_nothing)
17690 return NULL_RTX;
17692 mask2 = rs6000_emit_vector_compare (rev_code, op0, op1, dmode);
17693 if (!mask2)
17694 return NULL_RTX;
17696 mask = gen_reg_rtx (dmode);
17697 emit_insn (GEN_FCN (nor_code) (mask, mask2));
17698 return mask;
17700 break;
17701 case GE:
17702 case GEU:
17703 case LE:
17704 case LEU:
17705 /* Try GT/GTU/LT/LTU OR EQ */
17707 rtx c_rtx, eq_rtx;
17708 enum insn_code ior_code;
17709 enum rtx_code new_code;
17711 switch (rcode)
17713 case GE:
17714 new_code = GT;
17715 break;
17717 case GEU:
17718 new_code = GTU;
17719 break;
17721 case LE:
17722 new_code = LT;
17723 break;
17725 case LEU:
17726 new_code = LTU;
17727 break;
17729 default:
17730 gcc_unreachable ();
17733 ior_code = optab_handler (ior_optab, dmode);
17734 if (ior_code == CODE_FOR_nothing)
17735 return NULL_RTX;
17737 c_rtx = rs6000_emit_vector_compare (new_code, op0, op1, dmode);
17738 if (!c_rtx)
17739 return NULL_RTX;
17741 eq_rtx = rs6000_emit_vector_compare (EQ, op0, op1, dmode);
17742 if (!eq_rtx)
17743 return NULL_RTX;
17745 mask = gen_reg_rtx (dmode);
17746 emit_insn (GEN_FCN (ior_code) (mask, c_rtx, eq_rtx));
17747 return mask;
17749 break;
17750 default:
17751 return NULL_RTX;
17754 if (try_again)
17756 if (swap_operands)
17758 rtx tmp;
17759 tmp = op0;
17760 op0 = op1;
17761 op1 = tmp;
17764 mask = rs6000_emit_vector_compare_inner (rcode, op0, op1);
17765 if (mask)
17766 return mask;
17769 /* You only get two chances. */
17770 return NULL_RTX;
17773 /* Emit vector conditional expression. DEST is destination. OP_TRUE and
17774 OP_FALSE are two VEC_COND_EXPR operands. CC_OP0 and CC_OP1 are the two
17775 operands for the relation operation COND. */
17778 rs6000_emit_vector_cond_expr (rtx dest, rtx op_true, rtx op_false,
17779 rtx cond, rtx cc_op0, rtx cc_op1)
17781 enum machine_mode dest_mode = GET_MODE (dest);
17782 enum machine_mode mask_mode = GET_MODE (cc_op0);
17783 enum rtx_code rcode = GET_CODE (cond);
17784 enum machine_mode cc_mode = CCmode;
17785 rtx mask;
17786 rtx cond2;
17787 rtx tmp;
17788 bool invert_move = false;
17790 if (VECTOR_UNIT_NONE_P (dest_mode))
17791 return 0;
17793 gcc_assert (GET_MODE_SIZE (dest_mode) == GET_MODE_SIZE (mask_mode)
17794 && GET_MODE_NUNITS (dest_mode) == GET_MODE_NUNITS (mask_mode));
17796 switch (rcode)
17798 /* Swap operands if we can, and fall back to doing the operation as
17799 specified, and doing a NOR to invert the test. */
17800 case NE:
17801 case UNLE:
17802 case UNLT:
17803 case UNGE:
17804 case UNGT:
17805 /* Invert condition and try again.
17806 e.g., A = (B != C) ? D : E becomes A = (B == C) ? E : D. */
17807 invert_move = true;
17808 rcode = reverse_condition_maybe_unordered (rcode);
17809 if (rcode == UNKNOWN)
17810 return 0;
17811 break;
17813 /* Mark unsigned tests with CCUNSmode. */
17814 case GTU:
17815 case GEU:
17816 case LTU:
17817 case LEU:
17818 cc_mode = CCUNSmode;
17819 break;
17821 default:
17822 break;
17825 /* Get the vector mask for the given relational operations. */
17826 mask = rs6000_emit_vector_compare (rcode, cc_op0, cc_op1, mask_mode);
17828 if (!mask)
17829 return 0;
17831 if (invert_move)
17833 tmp = op_true;
17834 op_true = op_false;
17835 op_false = tmp;
17838 cond2 = gen_rtx_fmt_ee (NE, cc_mode, gen_lowpart (dest_mode, mask),
17839 CONST0_RTX (dest_mode));
17840 emit_insn (gen_rtx_SET (VOIDmode,
17841 dest,
17842 gen_rtx_IF_THEN_ELSE (dest_mode,
17843 cond2,
17844 op_true,
17845 op_false)));
17846 return 1;
17849 /* Emit a conditional move: move TRUE_COND to DEST if OP of the
17850 operands of the last comparison is nonzero/true, FALSE_COND if it
17851 is zero/false. Return 0 if the hardware has no such operation. */
17854 rs6000_emit_cmove (rtx dest, rtx op, rtx true_cond, rtx false_cond)
17856 enum rtx_code code = GET_CODE (op);
17857 rtx op0 = XEXP (op, 0);
17858 rtx op1 = XEXP (op, 1);
17859 REAL_VALUE_TYPE c1;
17860 enum machine_mode compare_mode = GET_MODE (op0);
17861 enum machine_mode result_mode = GET_MODE (dest);
17862 rtx temp;
17863 bool is_against_zero;
17865 /* These modes should always match. */
17866 if (GET_MODE (op1) != compare_mode
17867 /* In the isel case however, we can use a compare immediate, so
17868 op1 may be a small constant. */
17869 && (!TARGET_ISEL || !short_cint_operand (op1, VOIDmode)))
17870 return 0;
17871 if (GET_MODE (true_cond) != result_mode)
17872 return 0;
17873 if (GET_MODE (false_cond) != result_mode)
17874 return 0;
17876 /* Don't allow using floating point comparisons for integer results for
17877 now. */
17878 if (FLOAT_MODE_P (compare_mode) && !FLOAT_MODE_P (result_mode))
17879 return 0;
17881 /* First, work out if the hardware can do this at all, or
17882 if it's too slow.... */
17883 if (!FLOAT_MODE_P (compare_mode))
17885 if (TARGET_ISEL)
17886 return rs6000_emit_int_cmove (dest, op, true_cond, false_cond);
17887 return 0;
17889 else if (TARGET_HARD_FLOAT && !TARGET_FPRS
17890 && SCALAR_FLOAT_MODE_P (compare_mode))
17891 return 0;
17893 is_against_zero = op1 == CONST0_RTX (compare_mode);
17895 /* A floating-point subtract might overflow, underflow, or produce
17896 an inexact result, thus changing the floating-point flags, so it
17897 can't be generated if we care about that. It's safe if one side
17898 of the construct is zero, since then no subtract will be
17899 generated. */
17900 if (SCALAR_FLOAT_MODE_P (compare_mode)
17901 && flag_trapping_math && ! is_against_zero)
17902 return 0;
17904 /* Eliminate half of the comparisons by switching operands, this
17905 makes the remaining code simpler. */
17906 if (code == UNLT || code == UNGT || code == UNORDERED || code == NE
17907 || code == LTGT || code == LT || code == UNLE)
17909 code = reverse_condition_maybe_unordered (code);
17910 temp = true_cond;
17911 true_cond = false_cond;
17912 false_cond = temp;
17915 /* UNEQ and LTGT take four instructions for a comparison with zero,
17916 it'll probably be faster to use a branch here too. */
17917 if (code == UNEQ && HONOR_NANS (compare_mode))
17918 return 0;
17920 if (GET_CODE (op1) == CONST_DOUBLE)
17921 REAL_VALUE_FROM_CONST_DOUBLE (c1, op1);
17923 /* We're going to try to implement comparisons by performing
17924 a subtract, then comparing against zero. Unfortunately,
17925 Inf - Inf is NaN which is not zero, and so if we don't
17926 know that the operand is finite and the comparison
17927 would treat EQ different to UNORDERED, we can't do it. */
17928 if (HONOR_INFINITIES (compare_mode)
17929 && code != GT && code != UNGE
17930 && (GET_CODE (op1) != CONST_DOUBLE || real_isinf (&c1))
17931 /* Constructs of the form (a OP b ? a : b) are safe. */
17932 && ((! rtx_equal_p (op0, false_cond) && ! rtx_equal_p (op1, false_cond))
17933 || (! rtx_equal_p (op0, true_cond)
17934 && ! rtx_equal_p (op1, true_cond))))
17935 return 0;
17937 /* At this point we know we can use fsel. */
17939 /* Reduce the comparison to a comparison against zero. */
17940 if (! is_against_zero)
17942 temp = gen_reg_rtx (compare_mode);
17943 emit_insn (gen_rtx_SET (VOIDmode, temp,
17944 gen_rtx_MINUS (compare_mode, op0, op1)));
17945 op0 = temp;
17946 op1 = CONST0_RTX (compare_mode);
17949 /* If we don't care about NaNs we can reduce some of the comparisons
17950 down to faster ones. */
17951 if (! HONOR_NANS (compare_mode))
17952 switch (code)
17954 case GT:
17955 code = LE;
17956 temp = true_cond;
17957 true_cond = false_cond;
17958 false_cond = temp;
17959 break;
17960 case UNGE:
17961 code = GE;
17962 break;
17963 case UNEQ:
17964 code = EQ;
17965 break;
17966 default:
17967 break;
17970 /* Now, reduce everything down to a GE. */
17971 switch (code)
17973 case GE:
17974 break;
17976 case LE:
17977 temp = gen_reg_rtx (compare_mode);
17978 emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_NEG (compare_mode, op0)));
17979 op0 = temp;
17980 break;
17982 case ORDERED:
17983 temp = gen_reg_rtx (compare_mode);
17984 emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_ABS (compare_mode, op0)));
17985 op0 = temp;
17986 break;
17988 case EQ:
17989 temp = gen_reg_rtx (compare_mode);
17990 emit_insn (gen_rtx_SET (VOIDmode, temp,
17991 gen_rtx_NEG (compare_mode,
17992 gen_rtx_ABS (compare_mode, op0))));
17993 op0 = temp;
17994 break;
17996 case UNGE:
17997 /* a UNGE 0 <-> (a GE 0 || -a UNLT 0) */
17998 temp = gen_reg_rtx (result_mode);
17999 emit_insn (gen_rtx_SET (VOIDmode, temp,
18000 gen_rtx_IF_THEN_ELSE (result_mode,
18001 gen_rtx_GE (VOIDmode,
18002 op0, op1),
18003 true_cond, false_cond)));
18004 false_cond = true_cond;
18005 true_cond = temp;
18007 temp = gen_reg_rtx (compare_mode);
18008 emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_NEG (compare_mode, op0)));
18009 op0 = temp;
18010 break;
18012 case GT:
18013 /* a GT 0 <-> (a GE 0 && -a UNLT 0) */
18014 temp = gen_reg_rtx (result_mode);
18015 emit_insn (gen_rtx_SET (VOIDmode, temp,
18016 gen_rtx_IF_THEN_ELSE (result_mode,
18017 gen_rtx_GE (VOIDmode,
18018 op0, op1),
18019 true_cond, false_cond)));
18020 true_cond = false_cond;
18021 false_cond = temp;
18023 temp = gen_reg_rtx (compare_mode);
18024 emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_NEG (compare_mode, op0)));
18025 op0 = temp;
18026 break;
18028 default:
18029 gcc_unreachable ();
18032 emit_insn (gen_rtx_SET (VOIDmode, dest,
18033 gen_rtx_IF_THEN_ELSE (result_mode,
18034 gen_rtx_GE (VOIDmode,
18035 op0, op1),
18036 true_cond, false_cond)));
18037 return 1;
18040 /* Same as above, but for ints (isel). */
18042 static int
18043 rs6000_emit_int_cmove (rtx dest, rtx op, rtx true_cond, rtx false_cond)
18045 rtx condition_rtx, cr;
18046 enum machine_mode mode = GET_MODE (dest);
18047 enum rtx_code cond_code;
18048 rtx (*isel_func) (rtx, rtx, rtx, rtx, rtx);
18049 bool signedp;
18051 if (mode != SImode && (!TARGET_POWERPC64 || mode != DImode))
18052 return 0;
18054 /* We still have to do the compare, because isel doesn't do a
18055 compare, it just looks at the CRx bits set by a previous compare
18056 instruction. */
18057 condition_rtx = rs6000_generate_compare (op, mode);
18058 cond_code = GET_CODE (condition_rtx);
18059 cr = XEXP (condition_rtx, 0);
18060 signedp = GET_MODE (cr) == CCmode;
18062 isel_func = (mode == SImode
18063 ? (signedp ? gen_isel_signed_si : gen_isel_unsigned_si)
18064 : (signedp ? gen_isel_signed_di : gen_isel_unsigned_di));
18066 switch (cond_code)
18068 case LT: case GT: case LTU: case GTU: case EQ:
18069 /* isel handles these directly. */
18070 break;
18072 default:
18073 /* We need to swap the sense of the comparison. */
18075 rtx t = true_cond;
18076 true_cond = false_cond;
18077 false_cond = t;
18078 PUT_CODE (condition_rtx, reverse_condition (cond_code));
18080 break;
18083 false_cond = force_reg (mode, false_cond);
18084 if (true_cond != const0_rtx)
18085 true_cond = force_reg (mode, true_cond);
18087 emit_insn (isel_func (dest, condition_rtx, true_cond, false_cond, cr));
18089 return 1;
18092 const char *
18093 output_isel (rtx *operands)
18095 enum rtx_code code;
18097 code = GET_CODE (operands[1]);
18099 if (code == GE || code == GEU || code == LE || code == LEU || code == NE)
18101 gcc_assert (GET_CODE (operands[2]) == REG
18102 && GET_CODE (operands[3]) == REG);
18103 PUT_CODE (operands[1], reverse_condition (code));
18104 return "isel %0,%3,%2,%j1";
18107 return "isel %0,%2,%3,%j1";
18110 void
18111 rs6000_emit_minmax (rtx dest, enum rtx_code code, rtx op0, rtx op1)
18113 enum machine_mode mode = GET_MODE (op0);
18114 enum rtx_code c;
18115 rtx target;
18117 /* VSX/altivec have direct min/max insns. */
18118 if ((code == SMAX || code == SMIN)
18119 && (VECTOR_UNIT_ALTIVEC_OR_VSX_P (mode)
18120 || (mode == SFmode && VECTOR_UNIT_VSX_P (DFmode))))
18122 emit_insn (gen_rtx_SET (VOIDmode,
18123 dest,
18124 gen_rtx_fmt_ee (code, mode, op0, op1)));
18125 return;
18128 if (code == SMAX || code == SMIN)
18129 c = GE;
18130 else
18131 c = GEU;
18133 if (code == SMAX || code == UMAX)
18134 target = emit_conditional_move (dest, c, op0, op1, mode,
18135 op0, op1, mode, 0);
18136 else
18137 target = emit_conditional_move (dest, c, op0, op1, mode,
18138 op1, op0, mode, 0);
18139 gcc_assert (target);
18140 if (target != dest)
18141 emit_move_insn (dest, target);
18144 /* A subroutine of the atomic operation splitters. Jump to LABEL if
18145 COND is true. Mark the jump as unlikely to be taken. */
18147 static void
18148 emit_unlikely_jump (rtx cond, rtx label)
18150 rtx very_unlikely = GEN_INT (REG_BR_PROB_BASE / 100 - 1);
18151 rtx x;
18153 x = gen_rtx_IF_THEN_ELSE (VOIDmode, cond, label, pc_rtx);
18154 x = emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, x));
18155 add_reg_note (x, REG_BR_PROB, very_unlikely);
18158 /* A subroutine of the atomic operation splitters. Emit a load-locked
18159 instruction in MODE. For QI/HImode, possibly use a pattern than includes
18160 the zero_extend operation. */
18162 static void
18163 emit_load_locked (enum machine_mode mode, rtx reg, rtx mem)
18165 rtx (*fn) (rtx, rtx) = NULL;
18167 switch (mode)
18169 case QImode:
18170 fn = gen_load_lockedqi;
18171 break;
18172 case HImode:
18173 fn = gen_load_lockedhi;
18174 break;
18175 case SImode:
18176 if (GET_MODE (mem) == QImode)
18177 fn = gen_load_lockedqi_si;
18178 else if (GET_MODE (mem) == HImode)
18179 fn = gen_load_lockedhi_si;
18180 else
18181 fn = gen_load_lockedsi;
18182 break;
18183 case DImode:
18184 fn = gen_load_lockeddi;
18185 break;
18186 case TImode:
18187 fn = gen_load_lockedti;
18188 break;
18189 default:
18190 gcc_unreachable ();
18192 emit_insn (fn (reg, mem));
18195 /* A subroutine of the atomic operation splitters. Emit a store-conditional
18196 instruction in MODE. */
18198 static void
18199 emit_store_conditional (enum machine_mode mode, rtx res, rtx mem, rtx val)
18201 rtx (*fn) (rtx, rtx, rtx) = NULL;
18203 switch (mode)
18205 case QImode:
18206 fn = gen_store_conditionalqi;
18207 break;
18208 case HImode:
18209 fn = gen_store_conditionalhi;
18210 break;
18211 case SImode:
18212 fn = gen_store_conditionalsi;
18213 break;
18214 case DImode:
18215 fn = gen_store_conditionaldi;
18216 break;
18217 case TImode:
18218 fn = gen_store_conditionalti;
18219 break;
18220 default:
18221 gcc_unreachable ();
18224 /* Emit sync before stwcx. to address PPC405 Erratum. */
18225 if (PPC405_ERRATUM77)
18226 emit_insn (gen_hwsync ());
18228 emit_insn (fn (res, mem, val));
18231 /* Expand barriers before and after a load_locked/store_cond sequence. */
18233 static rtx
18234 rs6000_pre_atomic_barrier (rtx mem, enum memmodel model)
18236 rtx addr = XEXP (mem, 0);
18237 int strict_p = (reload_in_progress || reload_completed);
18239 if (!legitimate_indirect_address_p (addr, strict_p)
18240 && !legitimate_indexed_address_p (addr, strict_p))
18242 addr = force_reg (Pmode, addr);
18243 mem = replace_equiv_address_nv (mem, addr);
18246 switch (model)
18248 case MEMMODEL_RELAXED:
18249 case MEMMODEL_CONSUME:
18250 case MEMMODEL_ACQUIRE:
18251 break;
18252 case MEMMODEL_RELEASE:
18253 case MEMMODEL_ACQ_REL:
18254 emit_insn (gen_lwsync ());
18255 break;
18256 case MEMMODEL_SEQ_CST:
18257 emit_insn (gen_hwsync ());
18258 break;
18259 default:
18260 gcc_unreachable ();
18262 return mem;
18265 static void
18266 rs6000_post_atomic_barrier (enum memmodel model)
18268 switch (model)
18270 case MEMMODEL_RELAXED:
18271 case MEMMODEL_CONSUME:
18272 case MEMMODEL_RELEASE:
18273 break;
18274 case MEMMODEL_ACQUIRE:
18275 case MEMMODEL_ACQ_REL:
18276 case MEMMODEL_SEQ_CST:
18277 emit_insn (gen_isync ());
18278 break;
18279 default:
18280 gcc_unreachable ();
18284 /* A subroutine of the various atomic expanders. For sub-word operations,
18285 we must adjust things to operate on SImode. Given the original MEM,
18286 return a new aligned memory. Also build and return the quantities by
18287 which to shift and mask. */
18289 static rtx
18290 rs6000_adjust_atomic_subword (rtx orig_mem, rtx *pshift, rtx *pmask)
18292 rtx addr, align, shift, mask, mem;
18293 HOST_WIDE_INT shift_mask;
18294 enum machine_mode mode = GET_MODE (orig_mem);
18296 /* For smaller modes, we have to implement this via SImode. */
18297 shift_mask = (mode == QImode ? 0x18 : 0x10);
18299 addr = XEXP (orig_mem, 0);
18300 addr = force_reg (GET_MODE (addr), addr);
18302 /* Aligned memory containing subword. Generate a new memory. We
18303 do not want any of the existing MEM_ATTR data, as we're now
18304 accessing memory outside the original object. */
18305 align = expand_simple_binop (Pmode, AND, addr, GEN_INT (-4),
18306 NULL_RTX, 1, OPTAB_LIB_WIDEN);
18307 mem = gen_rtx_MEM (SImode, align);
18308 MEM_VOLATILE_P (mem) = MEM_VOLATILE_P (orig_mem);
18309 if (MEM_ALIAS_SET (orig_mem) == ALIAS_SET_MEMORY_BARRIER)
18310 set_mem_alias_set (mem, ALIAS_SET_MEMORY_BARRIER);
18312 /* Shift amount for subword relative to aligned word. */
18313 shift = gen_reg_rtx (SImode);
18314 addr = gen_lowpart (SImode, addr);
18315 emit_insn (gen_rlwinm (shift, addr, GEN_INT (3), GEN_INT (shift_mask)));
18316 if (WORDS_BIG_ENDIAN)
18317 shift = expand_simple_binop (SImode, XOR, shift, GEN_INT (shift_mask),
18318 shift, 1, OPTAB_LIB_WIDEN);
18319 *pshift = shift;
18321 /* Mask for insertion. */
18322 mask = expand_simple_binop (SImode, ASHIFT, GEN_INT (GET_MODE_MASK (mode)),
18323 shift, NULL_RTX, 1, OPTAB_LIB_WIDEN);
18324 *pmask = mask;
18326 return mem;
18329 /* A subroutine of the various atomic expanders. For sub-word operands,
18330 combine OLDVAL and NEWVAL via MASK. Returns a new pseduo. */
18332 static rtx
18333 rs6000_mask_atomic_subword (rtx oldval, rtx newval, rtx mask)
18335 rtx x;
18337 x = gen_reg_rtx (SImode);
18338 emit_insn (gen_rtx_SET (VOIDmode, x,
18339 gen_rtx_AND (SImode,
18340 gen_rtx_NOT (SImode, mask),
18341 oldval)));
18343 x = expand_simple_binop (SImode, IOR, newval, x, x, 1, OPTAB_LIB_WIDEN);
18345 return x;
18348 /* A subroutine of the various atomic expanders. For sub-word operands,
18349 extract WIDE to NARROW via SHIFT. */
18351 static void
18352 rs6000_finish_atomic_subword (rtx narrow, rtx wide, rtx shift)
18354 wide = expand_simple_binop (SImode, LSHIFTRT, wide, shift,
18355 wide, 1, OPTAB_LIB_WIDEN);
18356 emit_move_insn (narrow, gen_lowpart (GET_MODE (narrow), wide));
18359 /* Expand an atomic compare and swap operation. */
18361 void
18362 rs6000_expand_atomic_compare_and_swap (rtx operands[])
18364 rtx boolval, retval, mem, oldval, newval, cond;
18365 rtx label1, label2, x, mask, shift;
18366 enum machine_mode mode, orig_mode;
18367 enum memmodel mod_s, mod_f;
18368 bool is_weak;
18370 boolval = operands[0];
18371 retval = operands[1];
18372 mem = operands[2];
18373 oldval = operands[3];
18374 newval = operands[4];
18375 is_weak = (INTVAL (operands[5]) != 0);
18376 mod_s = (enum memmodel) INTVAL (operands[6]);
18377 mod_f = (enum memmodel) INTVAL (operands[7]);
18378 orig_mode = mode = GET_MODE (mem);
18380 mask = shift = NULL_RTX;
18381 if (mode == QImode || mode == HImode)
18383 /* Before power8, we didn't have access to lbarx/lharx, so generate a
18384 lwarx and shift/mask operations. With power8, we need to do the
18385 comparison in SImode, but the store is still done in QI/HImode. */
18386 oldval = convert_modes (SImode, mode, oldval, 1);
18388 if (!TARGET_SYNC_HI_QI)
18390 mem = rs6000_adjust_atomic_subword (mem, &shift, &mask);
18392 /* Shift and mask OLDVAL into position with the word. */
18393 oldval = expand_simple_binop (SImode, ASHIFT, oldval, shift,
18394 NULL_RTX, 1, OPTAB_LIB_WIDEN);
18396 /* Shift and mask NEWVAL into position within the word. */
18397 newval = convert_modes (SImode, mode, newval, 1);
18398 newval = expand_simple_binop (SImode, ASHIFT, newval, shift,
18399 NULL_RTX, 1, OPTAB_LIB_WIDEN);
18402 /* Prepare to adjust the return value. */
18403 retval = gen_reg_rtx (SImode);
18404 mode = SImode;
18406 else if (reg_overlap_mentioned_p (retval, oldval))
18407 oldval = copy_to_reg (oldval);
18409 mem = rs6000_pre_atomic_barrier (mem, mod_s);
18411 label1 = NULL_RTX;
18412 if (!is_weak)
18414 label1 = gen_rtx_LABEL_REF (VOIDmode, gen_label_rtx ());
18415 emit_label (XEXP (label1, 0));
18417 label2 = gen_rtx_LABEL_REF (VOIDmode, gen_label_rtx ());
18419 emit_load_locked (mode, retval, mem);
18421 x = retval;
18422 if (mask)
18424 x = expand_simple_binop (SImode, AND, retval, mask,
18425 NULL_RTX, 1, OPTAB_LIB_WIDEN);
18428 cond = gen_reg_rtx (CCmode);
18429 /* If we have TImode, synthesize a comparison. */
18430 if (mode != TImode)
18431 x = gen_rtx_COMPARE (CCmode, x, oldval);
18432 else
18434 rtx xor1_result = gen_reg_rtx (DImode);
18435 rtx xor2_result = gen_reg_rtx (DImode);
18436 rtx or_result = gen_reg_rtx (DImode);
18437 rtx new_word0 = simplify_gen_subreg (DImode, x, TImode, 0);
18438 rtx new_word1 = simplify_gen_subreg (DImode, x, TImode, 8);
18439 rtx old_word0 = simplify_gen_subreg (DImode, oldval, TImode, 0);
18440 rtx old_word1 = simplify_gen_subreg (DImode, oldval, TImode, 8);
18442 emit_insn (gen_xordi3 (xor1_result, new_word0, old_word0));
18443 emit_insn (gen_xordi3 (xor2_result, new_word1, old_word1));
18444 emit_insn (gen_iordi3 (or_result, xor1_result, xor2_result));
18445 x = gen_rtx_COMPARE (CCmode, or_result, const0_rtx);
18448 emit_insn (gen_rtx_SET (VOIDmode, cond, x));
18450 x = gen_rtx_NE (VOIDmode, cond, const0_rtx);
18451 emit_unlikely_jump (x, label2);
18453 x = newval;
18454 if (mask)
18455 x = rs6000_mask_atomic_subword (retval, newval, mask);
18457 emit_store_conditional (orig_mode, cond, mem, x);
18459 if (!is_weak)
18461 x = gen_rtx_NE (VOIDmode, cond, const0_rtx);
18462 emit_unlikely_jump (x, label1);
18465 if (mod_f != MEMMODEL_RELAXED)
18466 emit_label (XEXP (label2, 0));
18468 rs6000_post_atomic_barrier (mod_s);
18470 if (mod_f == MEMMODEL_RELAXED)
18471 emit_label (XEXP (label2, 0));
18473 if (shift)
18474 rs6000_finish_atomic_subword (operands[1], retval, shift);
18475 else if (mode != GET_MODE (operands[1]))
18476 convert_move (operands[1], retval, 1);
18478 /* In all cases, CR0 contains EQ on success, and NE on failure. */
18479 x = gen_rtx_EQ (SImode, cond, const0_rtx);
18480 emit_insn (gen_rtx_SET (VOIDmode, boolval, x));
18483 /* Expand an atomic exchange operation. */
18485 void
18486 rs6000_expand_atomic_exchange (rtx operands[])
18488 rtx retval, mem, val, cond;
18489 enum machine_mode mode;
18490 enum memmodel model;
18491 rtx label, x, mask, shift;
18493 retval = operands[0];
18494 mem = operands[1];
18495 val = operands[2];
18496 model = (enum memmodel) INTVAL (operands[3]);
18497 mode = GET_MODE (mem);
18499 mask = shift = NULL_RTX;
18500 if (!TARGET_SYNC_HI_QI && (mode == QImode || mode == HImode))
18502 mem = rs6000_adjust_atomic_subword (mem, &shift, &mask);
18504 /* Shift and mask VAL into position with the word. */
18505 val = convert_modes (SImode, mode, val, 1);
18506 val = expand_simple_binop (SImode, ASHIFT, val, shift,
18507 NULL_RTX, 1, OPTAB_LIB_WIDEN);
18509 /* Prepare to adjust the return value. */
18510 retval = gen_reg_rtx (SImode);
18511 mode = SImode;
18514 mem = rs6000_pre_atomic_barrier (mem, model);
18516 label = gen_rtx_LABEL_REF (VOIDmode, gen_label_rtx ());
18517 emit_label (XEXP (label, 0));
18519 emit_load_locked (mode, retval, mem);
18521 x = val;
18522 if (mask)
18523 x = rs6000_mask_atomic_subword (retval, val, mask);
18525 cond = gen_reg_rtx (CCmode);
18526 emit_store_conditional (mode, cond, mem, x);
18528 x = gen_rtx_NE (VOIDmode, cond, const0_rtx);
18529 emit_unlikely_jump (x, label);
18531 rs6000_post_atomic_barrier (model);
18533 if (shift)
18534 rs6000_finish_atomic_subword (operands[0], retval, shift);
18537 /* Expand an atomic fetch-and-operate pattern. CODE is the binary operation
18538 to perform. MEM is the memory on which to operate. VAL is the second
18539 operand of the binary operator. BEFORE and AFTER are optional locations to
18540 return the value of MEM either before of after the operation. MODEL_RTX
18541 is a CONST_INT containing the memory model to use. */
18543 void
18544 rs6000_expand_atomic_op (enum rtx_code code, rtx mem, rtx val,
18545 rtx orig_before, rtx orig_after, rtx model_rtx)
18547 enum memmodel model = (enum memmodel) INTVAL (model_rtx);
18548 enum machine_mode mode = GET_MODE (mem);
18549 enum machine_mode store_mode = mode;
18550 rtx label, x, cond, mask, shift;
18551 rtx before = orig_before, after = orig_after;
18553 mask = shift = NULL_RTX;
18554 /* On power8, we want to use SImode for the operation. On previous systems,
18555 use the operation in a subword and shift/mask to get the proper byte or
18556 halfword. */
18557 if (mode == QImode || mode == HImode)
18559 if (TARGET_SYNC_HI_QI)
18561 val = convert_modes (SImode, mode, val, 1);
18563 /* Prepare to adjust the return value. */
18564 before = gen_reg_rtx (SImode);
18565 if (after)
18566 after = gen_reg_rtx (SImode);
18567 mode = SImode;
18569 else
18571 mem = rs6000_adjust_atomic_subword (mem, &shift, &mask);
18573 /* Shift and mask VAL into position with the word. */
18574 val = convert_modes (SImode, mode, val, 1);
18575 val = expand_simple_binop (SImode, ASHIFT, val, shift,
18576 NULL_RTX, 1, OPTAB_LIB_WIDEN);
18578 switch (code)
18580 case IOR:
18581 case XOR:
18582 /* We've already zero-extended VAL. That is sufficient to
18583 make certain that it does not affect other bits. */
18584 mask = NULL;
18585 break;
18587 case AND:
18588 /* If we make certain that all of the other bits in VAL are
18589 set, that will be sufficient to not affect other bits. */
18590 x = gen_rtx_NOT (SImode, mask);
18591 x = gen_rtx_IOR (SImode, x, val);
18592 emit_insn (gen_rtx_SET (VOIDmode, val, x));
18593 mask = NULL;
18594 break;
18596 case NOT:
18597 case PLUS:
18598 case MINUS:
18599 /* These will all affect bits outside the field and need
18600 adjustment via MASK within the loop. */
18601 break;
18603 default:
18604 gcc_unreachable ();
18607 /* Prepare to adjust the return value. */
18608 before = gen_reg_rtx (SImode);
18609 if (after)
18610 after = gen_reg_rtx (SImode);
18611 store_mode = mode = SImode;
18615 mem = rs6000_pre_atomic_barrier (mem, model);
18617 label = gen_label_rtx ();
18618 emit_label (label);
18619 label = gen_rtx_LABEL_REF (VOIDmode, label);
18621 if (before == NULL_RTX)
18622 before = gen_reg_rtx (mode);
18624 emit_load_locked (mode, before, mem);
18626 if (code == NOT)
18628 x = expand_simple_binop (mode, AND, before, val,
18629 NULL_RTX, 1, OPTAB_LIB_WIDEN);
18630 after = expand_simple_unop (mode, NOT, x, after, 1);
18632 else
18634 after = expand_simple_binop (mode, code, before, val,
18635 after, 1, OPTAB_LIB_WIDEN);
18638 x = after;
18639 if (mask)
18641 x = expand_simple_binop (SImode, AND, after, mask,
18642 NULL_RTX, 1, OPTAB_LIB_WIDEN);
18643 x = rs6000_mask_atomic_subword (before, x, mask);
18645 else if (store_mode != mode)
18646 x = convert_modes (store_mode, mode, x, 1);
18648 cond = gen_reg_rtx (CCmode);
18649 emit_store_conditional (store_mode, cond, mem, x);
18651 x = gen_rtx_NE (VOIDmode, cond, const0_rtx);
18652 emit_unlikely_jump (x, label);
18654 rs6000_post_atomic_barrier (model);
18656 if (shift)
18658 /* QImode/HImode on machines without lbarx/lharx where we do a lwarx and
18659 then do the calcuations in a SImode register. */
18660 if (orig_before)
18661 rs6000_finish_atomic_subword (orig_before, before, shift);
18662 if (orig_after)
18663 rs6000_finish_atomic_subword (orig_after, after, shift);
18665 else if (store_mode != mode)
18667 /* QImode/HImode on machines with lbarx/lharx where we do the native
18668 operation and then do the calcuations in a SImode register. */
18669 if (orig_before)
18670 convert_move (orig_before, before, 1);
18671 if (orig_after)
18672 convert_move (orig_after, after, 1);
18674 else if (orig_after && after != orig_after)
18675 emit_move_insn (orig_after, after);
18678 /* Emit instructions to move SRC to DST. Called by splitters for
18679 multi-register moves. It will emit at most one instruction for
18680 each register that is accessed; that is, it won't emit li/lis pairs
18681 (or equivalent for 64-bit code). One of SRC or DST must be a hard
18682 register. */
18684 void
18685 rs6000_split_multireg_move (rtx dst, rtx src)
18687 /* The register number of the first register being moved. */
18688 int reg;
18689 /* The mode that is to be moved. */
18690 enum machine_mode mode;
18691 /* The mode that the move is being done in, and its size. */
18692 enum machine_mode reg_mode;
18693 int reg_mode_size;
18694 /* The number of registers that will be moved. */
18695 int nregs;
18697 reg = REG_P (dst) ? REGNO (dst) : REGNO (src);
18698 mode = GET_MODE (dst);
18699 nregs = hard_regno_nregs[reg][mode];
18700 if (FP_REGNO_P (reg))
18701 reg_mode = DECIMAL_FLOAT_MODE_P (mode) ? DDmode :
18702 ((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT) ? DFmode : SFmode);
18703 else if (ALTIVEC_REGNO_P (reg))
18704 reg_mode = V16QImode;
18705 else if (TARGET_E500_DOUBLE && mode == TFmode)
18706 reg_mode = DFmode;
18707 else
18708 reg_mode = word_mode;
18709 reg_mode_size = GET_MODE_SIZE (reg_mode);
18711 gcc_assert (reg_mode_size * nregs == GET_MODE_SIZE (mode));
18713 if (REG_P (src) && REG_P (dst) && (REGNO (src) < REGNO (dst)))
18715 /* Move register range backwards, if we might have destructive
18716 overlap. */
18717 int i;
18718 for (i = nregs - 1; i >= 0; i--)
18719 emit_insn (gen_rtx_SET (VOIDmode,
18720 simplify_gen_subreg (reg_mode, dst, mode,
18721 i * reg_mode_size),
18722 simplify_gen_subreg (reg_mode, src, mode,
18723 i * reg_mode_size)));
18725 else
18727 int i;
18728 int j = -1;
18729 bool used_update = false;
18730 rtx restore_basereg = NULL_RTX;
18732 if (MEM_P (src) && INT_REGNO_P (reg))
18734 rtx breg;
18736 if (GET_CODE (XEXP (src, 0)) == PRE_INC
18737 || GET_CODE (XEXP (src, 0)) == PRE_DEC)
18739 rtx delta_rtx;
18740 breg = XEXP (XEXP (src, 0), 0);
18741 delta_rtx = (GET_CODE (XEXP (src, 0)) == PRE_INC
18742 ? GEN_INT (GET_MODE_SIZE (GET_MODE (src)))
18743 : GEN_INT (-GET_MODE_SIZE (GET_MODE (src))));
18744 emit_insn (gen_add3_insn (breg, breg, delta_rtx));
18745 src = replace_equiv_address (src, breg);
18747 else if (! rs6000_offsettable_memref_p (src, reg_mode))
18749 if (GET_CODE (XEXP (src, 0)) == PRE_MODIFY)
18751 rtx basereg = XEXP (XEXP (src, 0), 0);
18752 if (TARGET_UPDATE)
18754 rtx ndst = simplify_gen_subreg (reg_mode, dst, mode, 0);
18755 emit_insn (gen_rtx_SET (VOIDmode, ndst,
18756 gen_rtx_MEM (reg_mode, XEXP (src, 0))));
18757 used_update = true;
18759 else
18760 emit_insn (gen_rtx_SET (VOIDmode, basereg,
18761 XEXP (XEXP (src, 0), 1)));
18762 src = replace_equiv_address (src, basereg);
18764 else
18766 rtx basereg = gen_rtx_REG (Pmode, reg);
18767 emit_insn (gen_rtx_SET (VOIDmode, basereg, XEXP (src, 0)));
18768 src = replace_equiv_address (src, basereg);
18772 breg = XEXP (src, 0);
18773 if (GET_CODE (breg) == PLUS || GET_CODE (breg) == LO_SUM)
18774 breg = XEXP (breg, 0);
18776 /* If the base register we are using to address memory is
18777 also a destination reg, then change that register last. */
18778 if (REG_P (breg)
18779 && REGNO (breg) >= REGNO (dst)
18780 && REGNO (breg) < REGNO (dst) + nregs)
18781 j = REGNO (breg) - REGNO (dst);
18783 else if (MEM_P (dst) && INT_REGNO_P (reg))
18785 rtx breg;
18787 if (GET_CODE (XEXP (dst, 0)) == PRE_INC
18788 || GET_CODE (XEXP (dst, 0)) == PRE_DEC)
18790 rtx delta_rtx;
18791 breg = XEXP (XEXP (dst, 0), 0);
18792 delta_rtx = (GET_CODE (XEXP (dst, 0)) == PRE_INC
18793 ? GEN_INT (GET_MODE_SIZE (GET_MODE (dst)))
18794 : GEN_INT (-GET_MODE_SIZE (GET_MODE (dst))));
18796 /* We have to update the breg before doing the store.
18797 Use store with update, if available. */
18799 if (TARGET_UPDATE)
18801 rtx nsrc = simplify_gen_subreg (reg_mode, src, mode, 0);
18802 emit_insn (TARGET_32BIT
18803 ? (TARGET_POWERPC64
18804 ? gen_movdi_si_update (breg, breg, delta_rtx, nsrc)
18805 : gen_movsi_update (breg, breg, delta_rtx, nsrc))
18806 : gen_movdi_di_update (breg, breg, delta_rtx, nsrc));
18807 used_update = true;
18809 else
18810 emit_insn (gen_add3_insn (breg, breg, delta_rtx));
18811 dst = replace_equiv_address (dst, breg);
18813 else if (!rs6000_offsettable_memref_p (dst, reg_mode)
18814 && GET_CODE (XEXP (dst, 0)) != LO_SUM)
18816 if (GET_CODE (XEXP (dst, 0)) == PRE_MODIFY)
18818 rtx basereg = XEXP (XEXP (dst, 0), 0);
18819 if (TARGET_UPDATE)
18821 rtx nsrc = simplify_gen_subreg (reg_mode, src, mode, 0);
18822 emit_insn (gen_rtx_SET (VOIDmode,
18823 gen_rtx_MEM (reg_mode, XEXP (dst, 0)), nsrc));
18824 used_update = true;
18826 else
18827 emit_insn (gen_rtx_SET (VOIDmode, basereg,
18828 XEXP (XEXP (dst, 0), 1)));
18829 dst = replace_equiv_address (dst, basereg);
18831 else
18833 rtx basereg = XEXP (XEXP (dst, 0), 0);
18834 rtx offsetreg = XEXP (XEXP (dst, 0), 1);
18835 gcc_assert (GET_CODE (XEXP (dst, 0)) == PLUS
18836 && REG_P (basereg)
18837 && REG_P (offsetreg)
18838 && REGNO (basereg) != REGNO (offsetreg));
18839 if (REGNO (basereg) == 0)
18841 rtx tmp = offsetreg;
18842 offsetreg = basereg;
18843 basereg = tmp;
18845 emit_insn (gen_add3_insn (basereg, basereg, offsetreg));
18846 restore_basereg = gen_sub3_insn (basereg, basereg, offsetreg);
18847 dst = replace_equiv_address (dst, basereg);
18850 else if (GET_CODE (XEXP (dst, 0)) != LO_SUM)
18851 gcc_assert (rs6000_offsettable_memref_p (dst, reg_mode));
18854 for (i = 0; i < nregs; i++)
18856 /* Calculate index to next subword. */
18857 ++j;
18858 if (j == nregs)
18859 j = 0;
18861 /* If compiler already emitted move of first word by
18862 store with update, no need to do anything. */
18863 if (j == 0 && used_update)
18864 continue;
18866 emit_insn (gen_rtx_SET (VOIDmode,
18867 simplify_gen_subreg (reg_mode, dst, mode,
18868 j * reg_mode_size),
18869 simplify_gen_subreg (reg_mode, src, mode,
18870 j * reg_mode_size)));
18872 if (restore_basereg != NULL_RTX)
18873 emit_insn (restore_basereg);
18878 /* This page contains routines that are used to determine what the
18879 function prologue and epilogue code will do and write them out. */
18881 static inline bool
18882 save_reg_p (int r)
18884 return !call_used_regs[r] && df_regs_ever_live_p (r);
18887 /* Return the first fixed-point register that is required to be
18888 saved. 32 if none. */
18891 first_reg_to_save (void)
18893 int first_reg;
18895 /* Find lowest numbered live register. */
18896 for (first_reg = 13; first_reg <= 31; first_reg++)
18897 if (save_reg_p (first_reg))
18898 break;
18900 if (first_reg > RS6000_PIC_OFFSET_TABLE_REGNUM
18901 && ((DEFAULT_ABI == ABI_V4 && flag_pic != 0)
18902 || (DEFAULT_ABI == ABI_DARWIN && flag_pic)
18903 || (TARGET_TOC && TARGET_MINIMAL_TOC))
18904 && df_regs_ever_live_p (RS6000_PIC_OFFSET_TABLE_REGNUM))
18905 first_reg = RS6000_PIC_OFFSET_TABLE_REGNUM;
18907 #if TARGET_MACHO
18908 if (flag_pic
18909 && crtl->uses_pic_offset_table
18910 && first_reg > RS6000_PIC_OFFSET_TABLE_REGNUM)
18911 return RS6000_PIC_OFFSET_TABLE_REGNUM;
18912 #endif
18914 return first_reg;
18917 /* Similar, for FP regs. */
18920 first_fp_reg_to_save (void)
18922 int first_reg;
18924 /* Find lowest numbered live register. */
18925 for (first_reg = 14 + 32; first_reg <= 63; first_reg++)
18926 if (save_reg_p (first_reg))
18927 break;
18929 return first_reg;
18932 /* Similar, for AltiVec regs. */
18934 static int
18935 first_altivec_reg_to_save (void)
18937 int i;
18939 /* Stack frame remains as is unless we are in AltiVec ABI. */
18940 if (! TARGET_ALTIVEC_ABI)
18941 return LAST_ALTIVEC_REGNO + 1;
18943 /* On Darwin, the unwind routines are compiled without
18944 TARGET_ALTIVEC, and use save_world to save/restore the
18945 altivec registers when necessary. */
18946 if (DEFAULT_ABI == ABI_DARWIN && crtl->calls_eh_return
18947 && ! TARGET_ALTIVEC)
18948 return FIRST_ALTIVEC_REGNO + 20;
18950 /* Find lowest numbered live register. */
18951 for (i = FIRST_ALTIVEC_REGNO + 20; i <= LAST_ALTIVEC_REGNO; ++i)
18952 if (save_reg_p (i))
18953 break;
18955 return i;
18958 /* Return a 32-bit mask of the AltiVec registers we need to set in
18959 VRSAVE. Bit n of the return value is 1 if Vn is live. The MSB in
18960 the 32-bit word is 0. */
18962 static unsigned int
18963 compute_vrsave_mask (void)
18965 unsigned int i, mask = 0;
18967 /* On Darwin, the unwind routines are compiled without
18968 TARGET_ALTIVEC, and use save_world to save/restore the
18969 call-saved altivec registers when necessary. */
18970 if (DEFAULT_ABI == ABI_DARWIN && crtl->calls_eh_return
18971 && ! TARGET_ALTIVEC)
18972 mask |= 0xFFF;
18974 /* First, find out if we use _any_ altivec registers. */
18975 for (i = FIRST_ALTIVEC_REGNO; i <= LAST_ALTIVEC_REGNO; ++i)
18976 if (df_regs_ever_live_p (i))
18977 mask |= ALTIVEC_REG_BIT (i);
18979 if (mask == 0)
18980 return mask;
18982 /* Next, remove the argument registers from the set. These must
18983 be in the VRSAVE mask set by the caller, so we don't need to add
18984 them in again. More importantly, the mask we compute here is
18985 used to generate CLOBBERs in the set_vrsave insn, and we do not
18986 wish the argument registers to die. */
18987 for (i = crtl->args.info.vregno - 1; i >= ALTIVEC_ARG_MIN_REG; --i)
18988 mask &= ~ALTIVEC_REG_BIT (i);
18990 /* Similarly, remove the return value from the set. */
18992 bool yes = false;
18993 diddle_return_value (is_altivec_return_reg, &yes);
18994 if (yes)
18995 mask &= ~ALTIVEC_REG_BIT (ALTIVEC_ARG_RETURN);
18998 return mask;
19001 /* For a very restricted set of circumstances, we can cut down the
19002 size of prologues/epilogues by calling our own save/restore-the-world
19003 routines. */
19005 static void
19006 compute_save_world_info (rs6000_stack_t *info_ptr)
19008 info_ptr->world_save_p = 1;
19009 info_ptr->world_save_p
19010 = (WORLD_SAVE_P (info_ptr)
19011 && DEFAULT_ABI == ABI_DARWIN
19012 && !cfun->has_nonlocal_label
19013 && info_ptr->first_fp_reg_save == FIRST_SAVED_FP_REGNO
19014 && info_ptr->first_gp_reg_save == FIRST_SAVED_GP_REGNO
19015 && info_ptr->first_altivec_reg_save == FIRST_SAVED_ALTIVEC_REGNO
19016 && info_ptr->cr_save_p);
19018 /* This will not work in conjunction with sibcalls. Make sure there
19019 are none. (This check is expensive, but seldom executed.) */
19020 if (WORLD_SAVE_P (info_ptr))
19022 rtx insn;
19023 for (insn = get_last_insn_anywhere (); insn; insn = PREV_INSN (insn))
19024 if (CALL_P (insn) && SIBLING_CALL_P (insn))
19026 info_ptr->world_save_p = 0;
19027 break;
19031 if (WORLD_SAVE_P (info_ptr))
19033 /* Even if we're not touching VRsave, make sure there's room on the
19034 stack for it, if it looks like we're calling SAVE_WORLD, which
19035 will attempt to save it. */
19036 info_ptr->vrsave_size = 4;
19038 /* If we are going to save the world, we need to save the link register too. */
19039 info_ptr->lr_save_p = 1;
19041 /* "Save" the VRsave register too if we're saving the world. */
19042 if (info_ptr->vrsave_mask == 0)
19043 info_ptr->vrsave_mask = compute_vrsave_mask ();
19045 /* Because the Darwin register save/restore routines only handle
19046 F14 .. F31 and V20 .. V31 as per the ABI, perform a consistency
19047 check. */
19048 gcc_assert (info_ptr->first_fp_reg_save >= FIRST_SAVED_FP_REGNO
19049 && (info_ptr->first_altivec_reg_save
19050 >= FIRST_SAVED_ALTIVEC_REGNO));
19052 return;
19056 static void
19057 is_altivec_return_reg (rtx reg, void *xyes)
19059 bool *yes = (bool *) xyes;
19060 if (REGNO (reg) == ALTIVEC_ARG_RETURN)
19061 *yes = true;
19065 /* Look for user-defined global regs in the range FIRST to LAST-1.
19066 We should not restore these, and so cannot use lmw or out-of-line
19067 restore functions if there are any. We also can't save them
19068 (well, emit frame notes for them), because frame unwinding during
19069 exception handling will restore saved registers. */
19071 static bool
19072 global_regs_p (unsigned first, unsigned last)
19074 while (first < last)
19075 if (global_regs[first++])
19076 return true;
19077 return false;
19080 /* Determine the strategy for savings/restoring registers. */
19082 enum {
19083 SAVRES_MULTIPLE = 0x1,
19084 SAVE_INLINE_FPRS = 0x2,
19085 SAVE_INLINE_GPRS = 0x4,
19086 REST_INLINE_FPRS = 0x8,
19087 REST_INLINE_GPRS = 0x10,
19088 SAVE_NOINLINE_GPRS_SAVES_LR = 0x20,
19089 SAVE_NOINLINE_FPRS_SAVES_LR = 0x40,
19090 REST_NOINLINE_FPRS_DOESNT_RESTORE_LR = 0x80,
19091 SAVE_INLINE_VRS = 0x100,
19092 REST_INLINE_VRS = 0x200
19095 static int
19096 rs6000_savres_strategy (rs6000_stack_t *info,
19097 bool using_static_chain_p)
19099 int strategy = 0;
19100 bool lr_save_p;
19102 if (TARGET_MULTIPLE
19103 && !TARGET_POWERPC64
19104 && !(TARGET_SPE_ABI && info->spe_64bit_regs_used)
19105 && info->first_gp_reg_save < 31
19106 && !global_regs_p (info->first_gp_reg_save, 32))
19107 strategy |= SAVRES_MULTIPLE;
19109 if (crtl->calls_eh_return
19110 || cfun->machine->ra_need_lr)
19111 strategy |= (SAVE_INLINE_FPRS | REST_INLINE_FPRS
19112 | SAVE_INLINE_GPRS | REST_INLINE_GPRS
19113 | SAVE_INLINE_VRS | REST_INLINE_VRS);
19115 if (info->first_fp_reg_save == 64
19116 /* The out-of-line FP routines use double-precision stores;
19117 we can't use those routines if we don't have such stores. */
19118 || (TARGET_HARD_FLOAT && !TARGET_DOUBLE_FLOAT)
19119 || global_regs_p (info->first_fp_reg_save, 64))
19120 strategy |= SAVE_INLINE_FPRS | REST_INLINE_FPRS;
19122 if (info->first_gp_reg_save == 32
19123 || (!(strategy & SAVRES_MULTIPLE)
19124 && global_regs_p (info->first_gp_reg_save, 32)))
19125 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
19127 if (info->first_altivec_reg_save == LAST_ALTIVEC_REGNO + 1
19128 || global_regs_p (info->first_altivec_reg_save, LAST_ALTIVEC_REGNO + 1))
19129 strategy |= SAVE_INLINE_VRS | REST_INLINE_VRS;
19131 /* Define cutoff for using out-of-line functions to save registers. */
19132 if (DEFAULT_ABI == ABI_V4 || TARGET_ELF)
19134 if (!optimize_size)
19136 strategy |= SAVE_INLINE_FPRS | REST_INLINE_FPRS;
19137 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
19138 strategy |= SAVE_INLINE_VRS | REST_INLINE_VRS;
19140 else
19142 /* Prefer out-of-line restore if it will exit. */
19143 if (info->first_fp_reg_save > 61)
19144 strategy |= SAVE_INLINE_FPRS;
19145 if (info->first_gp_reg_save > 29)
19147 if (info->first_fp_reg_save == 64)
19148 strategy |= SAVE_INLINE_GPRS;
19149 else
19150 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
19152 if (info->first_altivec_reg_save == LAST_ALTIVEC_REGNO)
19153 strategy |= SAVE_INLINE_VRS | REST_INLINE_VRS;
19156 else if (DEFAULT_ABI == ABI_DARWIN)
19158 if (info->first_fp_reg_save > 60)
19159 strategy |= SAVE_INLINE_FPRS | REST_INLINE_FPRS;
19160 if (info->first_gp_reg_save > 29)
19161 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
19162 strategy |= SAVE_INLINE_VRS | REST_INLINE_VRS;
19164 else
19166 gcc_checking_assert (DEFAULT_ABI == ABI_AIX);
19167 if (info->first_fp_reg_save > 61)
19168 strategy |= SAVE_INLINE_FPRS | REST_INLINE_FPRS;
19169 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
19170 strategy |= SAVE_INLINE_VRS | REST_INLINE_VRS;
19173 /* Don't bother to try to save things out-of-line if r11 is occupied
19174 by the static chain. It would require too much fiddling and the
19175 static chain is rarely used anyway. FPRs are saved w.r.t the stack
19176 pointer on Darwin, and AIX uses r1 or r12. */
19177 if (using_static_chain_p && DEFAULT_ABI != ABI_AIX)
19178 strategy |= ((DEFAULT_ABI == ABI_DARWIN ? 0 : SAVE_INLINE_FPRS)
19179 | SAVE_INLINE_GPRS
19180 | SAVE_INLINE_VRS | REST_INLINE_VRS);
19182 /* We can only use the out-of-line routines to restore if we've
19183 saved all the registers from first_fp_reg_save in the prologue.
19184 Otherwise, we risk loading garbage. */
19185 if ((strategy & (SAVE_INLINE_FPRS | REST_INLINE_FPRS)) == SAVE_INLINE_FPRS)
19187 int i;
19189 for (i = info->first_fp_reg_save; i < 64; i++)
19190 if (!save_reg_p (i))
19192 strategy |= REST_INLINE_FPRS;
19193 break;
19197 /* If we are going to use store multiple, then don't even bother
19198 with the out-of-line routines, since the store-multiple
19199 instruction will always be smaller. */
19200 if ((strategy & SAVRES_MULTIPLE))
19201 strategy |= SAVE_INLINE_GPRS;
19203 /* info->lr_save_p isn't yet set if the only reason lr needs to be
19204 saved is an out-of-line save or restore. Set up the value for
19205 the next test (excluding out-of-line gpr restore). */
19206 lr_save_p = (info->lr_save_p
19207 || !(strategy & SAVE_INLINE_GPRS)
19208 || !(strategy & SAVE_INLINE_FPRS)
19209 || !(strategy & SAVE_INLINE_VRS)
19210 || !(strategy & REST_INLINE_FPRS)
19211 || !(strategy & REST_INLINE_VRS));
19213 /* The situation is more complicated with load multiple. We'd
19214 prefer to use the out-of-line routines for restores, since the
19215 "exit" out-of-line routines can handle the restore of LR and the
19216 frame teardown. However if doesn't make sense to use the
19217 out-of-line routine if that is the only reason we'd need to save
19218 LR, and we can't use the "exit" out-of-line gpr restore if we
19219 have saved some fprs; In those cases it is advantageous to use
19220 load multiple when available. */
19221 if ((strategy & SAVRES_MULTIPLE)
19222 && (!lr_save_p
19223 || info->first_fp_reg_save != 64))
19224 strategy |= REST_INLINE_GPRS;
19226 /* Saving CR interferes with the exit routines used on the SPE, so
19227 just punt here. */
19228 if (TARGET_SPE_ABI
19229 && info->spe_64bit_regs_used
19230 && info->cr_save_p)
19231 strategy |= REST_INLINE_GPRS;
19233 /* We can only use load multiple or the out-of-line routines to
19234 restore if we've used store multiple or out-of-line routines
19235 in the prologue, i.e. if we've saved all the registers from
19236 first_gp_reg_save. Otherwise, we risk loading garbage. */
19237 if ((strategy & (SAVE_INLINE_GPRS | REST_INLINE_GPRS | SAVRES_MULTIPLE))
19238 == SAVE_INLINE_GPRS)
19240 int i;
19242 for (i = info->first_gp_reg_save; i < 32; i++)
19243 if (!save_reg_p (i))
19245 strategy |= REST_INLINE_GPRS;
19246 break;
19250 if (TARGET_ELF && TARGET_64BIT)
19252 if (!(strategy & SAVE_INLINE_FPRS))
19253 strategy |= SAVE_NOINLINE_FPRS_SAVES_LR;
19254 else if (!(strategy & SAVE_INLINE_GPRS)
19255 && info->first_fp_reg_save == 64)
19256 strategy |= SAVE_NOINLINE_GPRS_SAVES_LR;
19258 else if (TARGET_AIX && !(strategy & REST_INLINE_FPRS))
19259 strategy |= REST_NOINLINE_FPRS_DOESNT_RESTORE_LR;
19261 if (TARGET_MACHO && !(strategy & SAVE_INLINE_FPRS))
19262 strategy |= SAVE_NOINLINE_FPRS_SAVES_LR;
19264 return strategy;
19267 /* Calculate the stack information for the current function. This is
19268 complicated by having two separate calling sequences, the AIX calling
19269 sequence and the V.4 calling sequence.
19271 AIX (and Darwin/Mac OS X) stack frames look like:
19272 32-bit 64-bit
19273 SP----> +---------------------------------------+
19274 | back chain to caller | 0 0
19275 +---------------------------------------+
19276 | saved CR | 4 8 (8-11)
19277 +---------------------------------------+
19278 | saved LR | 8 16
19279 +---------------------------------------+
19280 | reserved for compilers | 12 24
19281 +---------------------------------------+
19282 | reserved for binders | 16 32
19283 +---------------------------------------+
19284 | saved TOC pointer | 20 40
19285 +---------------------------------------+
19286 | Parameter save area (P) | 24 48
19287 +---------------------------------------+
19288 | Alloca space (A) | 24+P etc.
19289 +---------------------------------------+
19290 | Local variable space (L) | 24+P+A
19291 +---------------------------------------+
19292 | Float/int conversion temporary (X) | 24+P+A+L
19293 +---------------------------------------+
19294 | Save area for AltiVec registers (W) | 24+P+A+L+X
19295 +---------------------------------------+
19296 | AltiVec alignment padding (Y) | 24+P+A+L+X+W
19297 +---------------------------------------+
19298 | Save area for VRSAVE register (Z) | 24+P+A+L+X+W+Y
19299 +---------------------------------------+
19300 | Save area for GP registers (G) | 24+P+A+X+L+X+W+Y+Z
19301 +---------------------------------------+
19302 | Save area for FP registers (F) | 24+P+A+X+L+X+W+Y+Z+G
19303 +---------------------------------------+
19304 old SP->| back chain to caller's caller |
19305 +---------------------------------------+
19307 The required alignment for AIX configurations is two words (i.e., 8
19308 or 16 bytes).
19311 V.4 stack frames look like:
19313 SP----> +---------------------------------------+
19314 | back chain to caller | 0
19315 +---------------------------------------+
19316 | caller's saved LR | 4
19317 +---------------------------------------+
19318 | Parameter save area (P) | 8
19319 +---------------------------------------+
19320 | Alloca space (A) | 8+P
19321 +---------------------------------------+
19322 | Varargs save area (V) | 8+P+A
19323 +---------------------------------------+
19324 | Local variable space (L) | 8+P+A+V
19325 +---------------------------------------+
19326 | Float/int conversion temporary (X) | 8+P+A+V+L
19327 +---------------------------------------+
19328 | Save area for AltiVec registers (W) | 8+P+A+V+L+X
19329 +---------------------------------------+
19330 | AltiVec alignment padding (Y) | 8+P+A+V+L+X+W
19331 +---------------------------------------+
19332 | Save area for VRSAVE register (Z) | 8+P+A+V+L+X+W+Y
19333 +---------------------------------------+
19334 | SPE: area for 64-bit GP registers |
19335 +---------------------------------------+
19336 | SPE alignment padding |
19337 +---------------------------------------+
19338 | saved CR (C) | 8+P+A+V+L+X+W+Y+Z
19339 +---------------------------------------+
19340 | Save area for GP registers (G) | 8+P+A+V+L+X+W+Y+Z+C
19341 +---------------------------------------+
19342 | Save area for FP registers (F) | 8+P+A+V+L+X+W+Y+Z+C+G
19343 +---------------------------------------+
19344 old SP->| back chain to caller's caller |
19345 +---------------------------------------+
19347 The required alignment for V.4 is 16 bytes, or 8 bytes if -meabi is
19348 given. (But note below and in sysv4.h that we require only 8 and
19349 may round up the size of our stack frame anyways. The historical
19350 reason is early versions of powerpc-linux which didn't properly
19351 align the stack at program startup. A happy side-effect is that
19352 -mno-eabi libraries can be used with -meabi programs.)
19354 The EABI configuration defaults to the V.4 layout. However,
19355 the stack alignment requirements may differ. If -mno-eabi is not
19356 given, the required stack alignment is 8 bytes; if -mno-eabi is
19357 given, the required alignment is 16 bytes. (But see V.4 comment
19358 above.) */
19360 #ifndef ABI_STACK_BOUNDARY
19361 #define ABI_STACK_BOUNDARY STACK_BOUNDARY
19362 #endif
19364 static rs6000_stack_t *
19365 rs6000_stack_info (void)
19367 rs6000_stack_t *info_ptr = &stack_info;
19368 int reg_size = TARGET_32BIT ? 4 : 8;
19369 int ehrd_size;
19370 int save_align;
19371 int first_gp;
19372 HOST_WIDE_INT non_fixed_size;
19373 bool using_static_chain_p;
19375 if (reload_completed && info_ptr->reload_completed)
19376 return info_ptr;
19378 memset (info_ptr, 0, sizeof (*info_ptr));
19379 info_ptr->reload_completed = reload_completed;
19381 if (TARGET_SPE)
19383 /* Cache value so we don't rescan instruction chain over and over. */
19384 if (cfun->machine->insn_chain_scanned_p == 0)
19385 cfun->machine->insn_chain_scanned_p
19386 = spe_func_has_64bit_regs_p () + 1;
19387 info_ptr->spe_64bit_regs_used = cfun->machine->insn_chain_scanned_p - 1;
19390 /* Select which calling sequence. */
19391 info_ptr->abi = DEFAULT_ABI;
19393 /* Calculate which registers need to be saved & save area size. */
19394 info_ptr->first_gp_reg_save = first_reg_to_save ();
19395 /* Assume that we will have to save RS6000_PIC_OFFSET_TABLE_REGNUM,
19396 even if it currently looks like we won't. Reload may need it to
19397 get at a constant; if so, it will have already created a constant
19398 pool entry for it. */
19399 if (((TARGET_TOC && TARGET_MINIMAL_TOC)
19400 || (flag_pic == 1 && DEFAULT_ABI == ABI_V4)
19401 || (flag_pic && DEFAULT_ABI == ABI_DARWIN))
19402 && crtl->uses_const_pool
19403 && info_ptr->first_gp_reg_save > RS6000_PIC_OFFSET_TABLE_REGNUM)
19404 first_gp = RS6000_PIC_OFFSET_TABLE_REGNUM;
19405 else
19406 first_gp = info_ptr->first_gp_reg_save;
19408 info_ptr->gp_size = reg_size * (32 - first_gp);
19410 /* For the SPE, we have an additional upper 32-bits on each GPR.
19411 Ideally we should save the entire 64-bits only when the upper
19412 half is used in SIMD instructions. Since we only record
19413 registers live (not the size they are used in), this proves
19414 difficult because we'd have to traverse the instruction chain at
19415 the right time, taking reload into account. This is a real pain,
19416 so we opt to save the GPRs in 64-bits always if but one register
19417 gets used in 64-bits. Otherwise, all the registers in the frame
19418 get saved in 32-bits.
19420 So... since when we save all GPRs (except the SP) in 64-bits, the
19421 traditional GP save area will be empty. */
19422 if (TARGET_SPE_ABI && info_ptr->spe_64bit_regs_used != 0)
19423 info_ptr->gp_size = 0;
19425 info_ptr->first_fp_reg_save = first_fp_reg_to_save ();
19426 info_ptr->fp_size = 8 * (64 - info_ptr->first_fp_reg_save);
19428 info_ptr->first_altivec_reg_save = first_altivec_reg_to_save ();
19429 info_ptr->altivec_size = 16 * (LAST_ALTIVEC_REGNO + 1
19430 - info_ptr->first_altivec_reg_save);
19432 /* Does this function call anything? */
19433 info_ptr->calls_p = (! crtl->is_leaf
19434 || cfun->machine->ra_needs_full_frame);
19436 /* Determine if we need to save the condition code registers. */
19437 if (df_regs_ever_live_p (CR2_REGNO)
19438 || df_regs_ever_live_p (CR3_REGNO)
19439 || df_regs_ever_live_p (CR4_REGNO))
19441 info_ptr->cr_save_p = 1;
19442 if (DEFAULT_ABI == ABI_V4)
19443 info_ptr->cr_size = reg_size;
19446 /* If the current function calls __builtin_eh_return, then we need
19447 to allocate stack space for registers that will hold data for
19448 the exception handler. */
19449 if (crtl->calls_eh_return)
19451 unsigned int i;
19452 for (i = 0; EH_RETURN_DATA_REGNO (i) != INVALID_REGNUM; ++i)
19453 continue;
19455 /* SPE saves EH registers in 64-bits. */
19456 ehrd_size = i * (TARGET_SPE_ABI
19457 && info_ptr->spe_64bit_regs_used != 0
19458 ? UNITS_PER_SPE_WORD : UNITS_PER_WORD);
19460 else
19461 ehrd_size = 0;
19463 /* Determine various sizes. */
19464 info_ptr->reg_size = reg_size;
19465 info_ptr->fixed_size = RS6000_SAVE_AREA;
19466 info_ptr->vars_size = RS6000_ALIGN (get_frame_size (), 8);
19467 info_ptr->parm_size = RS6000_ALIGN (crtl->outgoing_args_size,
19468 TARGET_ALTIVEC ? 16 : 8);
19469 if (FRAME_GROWS_DOWNWARD)
19470 info_ptr->vars_size
19471 += RS6000_ALIGN (info_ptr->fixed_size + info_ptr->vars_size
19472 + info_ptr->parm_size,
19473 ABI_STACK_BOUNDARY / BITS_PER_UNIT)
19474 - (info_ptr->fixed_size + info_ptr->vars_size
19475 + info_ptr->parm_size);
19477 if (TARGET_SPE_ABI && info_ptr->spe_64bit_regs_used != 0)
19478 info_ptr->spe_gp_size = 8 * (32 - first_gp);
19479 else
19480 info_ptr->spe_gp_size = 0;
19482 if (TARGET_ALTIVEC_ABI)
19483 info_ptr->vrsave_mask = compute_vrsave_mask ();
19484 else
19485 info_ptr->vrsave_mask = 0;
19487 if (TARGET_ALTIVEC_VRSAVE && info_ptr->vrsave_mask)
19488 info_ptr->vrsave_size = 4;
19489 else
19490 info_ptr->vrsave_size = 0;
19492 compute_save_world_info (info_ptr);
19494 /* Calculate the offsets. */
19495 switch (DEFAULT_ABI)
19497 case ABI_NONE:
19498 default:
19499 gcc_unreachable ();
19501 case ABI_AIX:
19502 case ABI_DARWIN:
19503 info_ptr->fp_save_offset = - info_ptr->fp_size;
19504 info_ptr->gp_save_offset = info_ptr->fp_save_offset - info_ptr->gp_size;
19506 if (TARGET_ALTIVEC_ABI)
19508 info_ptr->vrsave_save_offset
19509 = info_ptr->gp_save_offset - info_ptr->vrsave_size;
19511 /* Align stack so vector save area is on a quadword boundary.
19512 The padding goes above the vectors. */
19513 if (info_ptr->altivec_size != 0)
19514 info_ptr->altivec_padding_size
19515 = info_ptr->vrsave_save_offset & 0xF;
19516 else
19517 info_ptr->altivec_padding_size = 0;
19519 info_ptr->altivec_save_offset
19520 = info_ptr->vrsave_save_offset
19521 - info_ptr->altivec_padding_size
19522 - info_ptr->altivec_size;
19523 gcc_assert (info_ptr->altivec_size == 0
19524 || info_ptr->altivec_save_offset % 16 == 0);
19526 /* Adjust for AltiVec case. */
19527 info_ptr->ehrd_offset = info_ptr->altivec_save_offset - ehrd_size;
19529 else
19530 info_ptr->ehrd_offset = info_ptr->gp_save_offset - ehrd_size;
19531 info_ptr->cr_save_offset = reg_size; /* first word when 64-bit. */
19532 info_ptr->lr_save_offset = 2*reg_size;
19533 break;
19535 case ABI_V4:
19536 info_ptr->fp_save_offset = - info_ptr->fp_size;
19537 info_ptr->gp_save_offset = info_ptr->fp_save_offset - info_ptr->gp_size;
19538 info_ptr->cr_save_offset = info_ptr->gp_save_offset - info_ptr->cr_size;
19540 if (TARGET_SPE_ABI && info_ptr->spe_64bit_regs_used != 0)
19542 /* Align stack so SPE GPR save area is aligned on a
19543 double-word boundary. */
19544 if (info_ptr->spe_gp_size != 0 && info_ptr->cr_save_offset != 0)
19545 info_ptr->spe_padding_size
19546 = 8 - (-info_ptr->cr_save_offset % 8);
19547 else
19548 info_ptr->spe_padding_size = 0;
19550 info_ptr->spe_gp_save_offset
19551 = info_ptr->cr_save_offset
19552 - info_ptr->spe_padding_size
19553 - info_ptr->spe_gp_size;
19555 /* Adjust for SPE case. */
19556 info_ptr->ehrd_offset = info_ptr->spe_gp_save_offset;
19558 else if (TARGET_ALTIVEC_ABI)
19560 info_ptr->vrsave_save_offset
19561 = info_ptr->cr_save_offset - info_ptr->vrsave_size;
19563 /* Align stack so vector save area is on a quadword boundary. */
19564 if (info_ptr->altivec_size != 0)
19565 info_ptr->altivec_padding_size
19566 = 16 - (-info_ptr->vrsave_save_offset % 16);
19567 else
19568 info_ptr->altivec_padding_size = 0;
19570 info_ptr->altivec_save_offset
19571 = info_ptr->vrsave_save_offset
19572 - info_ptr->altivec_padding_size
19573 - info_ptr->altivec_size;
19575 /* Adjust for AltiVec case. */
19576 info_ptr->ehrd_offset = info_ptr->altivec_save_offset;
19578 else
19579 info_ptr->ehrd_offset = info_ptr->cr_save_offset;
19580 info_ptr->ehrd_offset -= ehrd_size;
19581 info_ptr->lr_save_offset = reg_size;
19582 break;
19585 save_align = (TARGET_ALTIVEC_ABI || DEFAULT_ABI == ABI_DARWIN) ? 16 : 8;
19586 info_ptr->save_size = RS6000_ALIGN (info_ptr->fp_size
19587 + info_ptr->gp_size
19588 + info_ptr->altivec_size
19589 + info_ptr->altivec_padding_size
19590 + info_ptr->spe_gp_size
19591 + info_ptr->spe_padding_size
19592 + ehrd_size
19593 + info_ptr->cr_size
19594 + info_ptr->vrsave_size,
19595 save_align);
19597 non_fixed_size = (info_ptr->vars_size
19598 + info_ptr->parm_size
19599 + info_ptr->save_size);
19601 info_ptr->total_size = RS6000_ALIGN (non_fixed_size + info_ptr->fixed_size,
19602 ABI_STACK_BOUNDARY / BITS_PER_UNIT);
19604 /* Determine if we need to save the link register. */
19605 if (info_ptr->calls_p
19606 || (DEFAULT_ABI == ABI_AIX
19607 && crtl->profile
19608 && !TARGET_PROFILE_KERNEL)
19609 || (DEFAULT_ABI == ABI_V4 && cfun->calls_alloca)
19610 #ifdef TARGET_RELOCATABLE
19611 || (TARGET_RELOCATABLE && (get_pool_size () != 0))
19612 #endif
19613 || rs6000_ra_ever_killed ())
19614 info_ptr->lr_save_p = 1;
19616 using_static_chain_p = (cfun->static_chain_decl != NULL_TREE
19617 && df_regs_ever_live_p (STATIC_CHAIN_REGNUM)
19618 && call_used_regs[STATIC_CHAIN_REGNUM]);
19619 info_ptr->savres_strategy = rs6000_savres_strategy (info_ptr,
19620 using_static_chain_p);
19622 if (!(info_ptr->savres_strategy & SAVE_INLINE_GPRS)
19623 || !(info_ptr->savres_strategy & SAVE_INLINE_FPRS)
19624 || !(info_ptr->savres_strategy & SAVE_INLINE_VRS)
19625 || !(info_ptr->savres_strategy & REST_INLINE_GPRS)
19626 || !(info_ptr->savres_strategy & REST_INLINE_FPRS)
19627 || !(info_ptr->savres_strategy & REST_INLINE_VRS))
19628 info_ptr->lr_save_p = 1;
19630 if (info_ptr->lr_save_p)
19631 df_set_regs_ever_live (LR_REGNO, true);
19633 /* Determine if we need to allocate any stack frame:
19635 For AIX we need to push the stack if a frame pointer is needed
19636 (because the stack might be dynamically adjusted), if we are
19637 debugging, if we make calls, or if the sum of fp_save, gp_save,
19638 and local variables are more than the space needed to save all
19639 non-volatile registers: 32-bit: 18*8 + 19*4 = 220 or 64-bit: 18*8
19640 + 18*8 = 288 (GPR13 reserved).
19642 For V.4 we don't have the stack cushion that AIX uses, but assume
19643 that the debugger can handle stackless frames. */
19645 if (info_ptr->calls_p)
19646 info_ptr->push_p = 1;
19648 else if (DEFAULT_ABI == ABI_V4)
19649 info_ptr->push_p = non_fixed_size != 0;
19651 else if (frame_pointer_needed)
19652 info_ptr->push_p = 1;
19654 else if (TARGET_XCOFF && write_symbols != NO_DEBUG)
19655 info_ptr->push_p = 1;
19657 else
19658 info_ptr->push_p = non_fixed_size > (TARGET_32BIT ? 220 : 288);
19660 /* Zero offsets if we're not saving those registers. */
19661 if (info_ptr->fp_size == 0)
19662 info_ptr->fp_save_offset = 0;
19664 if (info_ptr->gp_size == 0)
19665 info_ptr->gp_save_offset = 0;
19667 if (! TARGET_ALTIVEC_ABI || info_ptr->altivec_size == 0)
19668 info_ptr->altivec_save_offset = 0;
19670 /* Zero VRSAVE offset if not saved and restored. */
19671 if (! TARGET_ALTIVEC_VRSAVE || info_ptr->vrsave_mask == 0)
19672 info_ptr->vrsave_save_offset = 0;
19674 if (! TARGET_SPE_ABI
19675 || info_ptr->spe_64bit_regs_used == 0
19676 || info_ptr->spe_gp_size == 0)
19677 info_ptr->spe_gp_save_offset = 0;
19679 if (! info_ptr->lr_save_p)
19680 info_ptr->lr_save_offset = 0;
19682 if (! info_ptr->cr_save_p)
19683 info_ptr->cr_save_offset = 0;
19685 return info_ptr;
19688 /* Return true if the current function uses any GPRs in 64-bit SIMD
19689 mode. */
19691 static bool
19692 spe_func_has_64bit_regs_p (void)
19694 rtx insns, insn;
19696 /* Functions that save and restore all the call-saved registers will
19697 need to save/restore the registers in 64-bits. */
19698 if (crtl->calls_eh_return
19699 || cfun->calls_setjmp
19700 || crtl->has_nonlocal_goto)
19701 return true;
19703 insns = get_insns ();
19705 for (insn = NEXT_INSN (insns); insn != NULL_RTX; insn = NEXT_INSN (insn))
19707 if (INSN_P (insn))
19709 rtx i;
19711 /* FIXME: This should be implemented with attributes...
19713 (set_attr "spe64" "true")....then,
19714 if (get_spe64(insn)) return true;
19716 It's the only reliable way to do the stuff below. */
19718 i = PATTERN (insn);
19719 if (GET_CODE (i) == SET)
19721 enum machine_mode mode = GET_MODE (SET_SRC (i));
19723 if (SPE_VECTOR_MODE (mode))
19724 return true;
19725 if (TARGET_E500_DOUBLE && (mode == DFmode || mode == TFmode))
19726 return true;
19731 return false;
19734 static void
19735 debug_stack_info (rs6000_stack_t *info)
19737 const char *abi_string;
19739 if (! info)
19740 info = rs6000_stack_info ();
19742 fprintf (stderr, "\nStack information for function %s:\n",
19743 ((current_function_decl && DECL_NAME (current_function_decl))
19744 ? IDENTIFIER_POINTER (DECL_NAME (current_function_decl))
19745 : "<unknown>"));
19747 switch (info->abi)
19749 default: abi_string = "Unknown"; break;
19750 case ABI_NONE: abi_string = "NONE"; break;
19751 case ABI_AIX: abi_string = "AIX"; break;
19752 case ABI_DARWIN: abi_string = "Darwin"; break;
19753 case ABI_V4: abi_string = "V.4"; break;
19756 fprintf (stderr, "\tABI = %5s\n", abi_string);
19758 if (TARGET_ALTIVEC_ABI)
19759 fprintf (stderr, "\tALTIVEC ABI extensions enabled.\n");
19761 if (TARGET_SPE_ABI)
19762 fprintf (stderr, "\tSPE ABI extensions enabled.\n");
19764 if (info->first_gp_reg_save != 32)
19765 fprintf (stderr, "\tfirst_gp_reg_save = %5d\n", info->first_gp_reg_save);
19767 if (info->first_fp_reg_save != 64)
19768 fprintf (stderr, "\tfirst_fp_reg_save = %5d\n", info->first_fp_reg_save);
19770 if (info->first_altivec_reg_save <= LAST_ALTIVEC_REGNO)
19771 fprintf (stderr, "\tfirst_altivec_reg_save = %5d\n",
19772 info->first_altivec_reg_save);
19774 if (info->lr_save_p)
19775 fprintf (stderr, "\tlr_save_p = %5d\n", info->lr_save_p);
19777 if (info->cr_save_p)
19778 fprintf (stderr, "\tcr_save_p = %5d\n", info->cr_save_p);
19780 if (info->vrsave_mask)
19781 fprintf (stderr, "\tvrsave_mask = 0x%x\n", info->vrsave_mask);
19783 if (info->push_p)
19784 fprintf (stderr, "\tpush_p = %5d\n", info->push_p);
19786 if (info->calls_p)
19787 fprintf (stderr, "\tcalls_p = %5d\n", info->calls_p);
19789 if (info->gp_save_offset)
19790 fprintf (stderr, "\tgp_save_offset = %5d\n", info->gp_save_offset);
19792 if (info->fp_save_offset)
19793 fprintf (stderr, "\tfp_save_offset = %5d\n", info->fp_save_offset);
19795 if (info->altivec_save_offset)
19796 fprintf (stderr, "\taltivec_save_offset = %5d\n",
19797 info->altivec_save_offset);
19799 if (info->spe_gp_save_offset)
19800 fprintf (stderr, "\tspe_gp_save_offset = %5d\n",
19801 info->spe_gp_save_offset);
19803 if (info->vrsave_save_offset)
19804 fprintf (stderr, "\tvrsave_save_offset = %5d\n",
19805 info->vrsave_save_offset);
19807 if (info->lr_save_offset)
19808 fprintf (stderr, "\tlr_save_offset = %5d\n", info->lr_save_offset);
19810 if (info->cr_save_offset)
19811 fprintf (stderr, "\tcr_save_offset = %5d\n", info->cr_save_offset);
19813 if (info->varargs_save_offset)
19814 fprintf (stderr, "\tvarargs_save_offset = %5d\n", info->varargs_save_offset);
19816 if (info->total_size)
19817 fprintf (stderr, "\ttotal_size = "HOST_WIDE_INT_PRINT_DEC"\n",
19818 info->total_size);
19820 if (info->vars_size)
19821 fprintf (stderr, "\tvars_size = "HOST_WIDE_INT_PRINT_DEC"\n",
19822 info->vars_size);
19824 if (info->parm_size)
19825 fprintf (stderr, "\tparm_size = %5d\n", info->parm_size);
19827 if (info->fixed_size)
19828 fprintf (stderr, "\tfixed_size = %5d\n", info->fixed_size);
19830 if (info->gp_size)
19831 fprintf (stderr, "\tgp_size = %5d\n", info->gp_size);
19833 if (info->spe_gp_size)
19834 fprintf (stderr, "\tspe_gp_size = %5d\n", info->spe_gp_size);
19836 if (info->fp_size)
19837 fprintf (stderr, "\tfp_size = %5d\n", info->fp_size);
19839 if (info->altivec_size)
19840 fprintf (stderr, "\taltivec_size = %5d\n", info->altivec_size);
19842 if (info->vrsave_size)
19843 fprintf (stderr, "\tvrsave_size = %5d\n", info->vrsave_size);
19845 if (info->altivec_padding_size)
19846 fprintf (stderr, "\taltivec_padding_size= %5d\n",
19847 info->altivec_padding_size);
19849 if (info->spe_padding_size)
19850 fprintf (stderr, "\tspe_padding_size = %5d\n",
19851 info->spe_padding_size);
19853 if (info->cr_size)
19854 fprintf (stderr, "\tcr_size = %5d\n", info->cr_size);
19856 if (info->save_size)
19857 fprintf (stderr, "\tsave_size = %5d\n", info->save_size);
19859 if (info->reg_size != 4)
19860 fprintf (stderr, "\treg_size = %5d\n", info->reg_size);
19862 fprintf (stderr, "\tsave-strategy = %04x\n", info->savres_strategy);
19864 fprintf (stderr, "\n");
19868 rs6000_return_addr (int count, rtx frame)
19870 /* Currently we don't optimize very well between prolog and body
19871 code and for PIC code the code can be actually quite bad, so
19872 don't try to be too clever here. */
19873 if (count != 0 || (DEFAULT_ABI != ABI_AIX && flag_pic))
19875 cfun->machine->ra_needs_full_frame = 1;
19877 return
19878 gen_rtx_MEM
19879 (Pmode,
19880 memory_address
19881 (Pmode,
19882 plus_constant (Pmode,
19883 copy_to_reg
19884 (gen_rtx_MEM (Pmode,
19885 memory_address (Pmode, frame))),
19886 RETURN_ADDRESS_OFFSET)));
19889 cfun->machine->ra_need_lr = 1;
19890 return get_hard_reg_initial_val (Pmode, LR_REGNO);
19893 /* Say whether a function is a candidate for sibcall handling or not. */
19895 static bool
19896 rs6000_function_ok_for_sibcall (tree decl, tree exp)
19898 tree fntype;
19900 if (decl)
19901 fntype = TREE_TYPE (decl);
19902 else
19903 fntype = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (exp)));
19905 /* We can't do it if the called function has more vector parameters
19906 than the current function; there's nowhere to put the VRsave code. */
19907 if (TARGET_ALTIVEC_ABI
19908 && TARGET_ALTIVEC_VRSAVE
19909 && !(decl && decl == current_function_decl))
19911 function_args_iterator args_iter;
19912 tree type;
19913 int nvreg = 0;
19915 /* Functions with vector parameters are required to have a
19916 prototype, so the argument type info must be available
19917 here. */
19918 FOREACH_FUNCTION_ARGS(fntype, type, args_iter)
19919 if (TREE_CODE (type) == VECTOR_TYPE
19920 && ALTIVEC_OR_VSX_VECTOR_MODE (TYPE_MODE (type)))
19921 nvreg++;
19923 FOREACH_FUNCTION_ARGS(TREE_TYPE (current_function_decl), type, args_iter)
19924 if (TREE_CODE (type) == VECTOR_TYPE
19925 && ALTIVEC_OR_VSX_VECTOR_MODE (TYPE_MODE (type)))
19926 nvreg--;
19928 if (nvreg > 0)
19929 return false;
19932 /* Under the AIX ABI we can't allow calls to non-local functions,
19933 because the callee may have a different TOC pointer to the
19934 caller and there's no way to ensure we restore the TOC when we
19935 return. With the secure-plt SYSV ABI we can't make non-local
19936 calls when -fpic/PIC because the plt call stubs use r30. */
19937 if (DEFAULT_ABI == ABI_DARWIN
19938 || (DEFAULT_ABI == ABI_AIX
19939 && decl
19940 && !DECL_EXTERNAL (decl)
19941 && (*targetm.binds_local_p) (decl))
19942 || (DEFAULT_ABI == ABI_V4
19943 && (!TARGET_SECURE_PLT
19944 || !flag_pic
19945 || (decl
19946 && (*targetm.binds_local_p) (decl)))))
19948 tree attr_list = TYPE_ATTRIBUTES (fntype);
19950 if (!lookup_attribute ("longcall", attr_list)
19951 || lookup_attribute ("shortcall", attr_list))
19952 return true;
19955 return false;
19958 static int
19959 rs6000_ra_ever_killed (void)
19961 rtx top;
19962 rtx reg;
19963 rtx insn;
19965 if (cfun->is_thunk)
19966 return 0;
19968 if (cfun->machine->lr_save_state)
19969 return cfun->machine->lr_save_state - 1;
19971 /* regs_ever_live has LR marked as used if any sibcalls are present,
19972 but this should not force saving and restoring in the
19973 pro/epilogue. Likewise, reg_set_between_p thinks a sibcall
19974 clobbers LR, so that is inappropriate. */
19976 /* Also, the prologue can generate a store into LR that
19977 doesn't really count, like this:
19979 move LR->R0
19980 bcl to set PIC register
19981 move LR->R31
19982 move R0->LR
19984 When we're called from the epilogue, we need to avoid counting
19985 this as a store. */
19987 push_topmost_sequence ();
19988 top = get_insns ();
19989 pop_topmost_sequence ();
19990 reg = gen_rtx_REG (Pmode, LR_REGNO);
19992 for (insn = NEXT_INSN (top); insn != NULL_RTX; insn = NEXT_INSN (insn))
19994 if (INSN_P (insn))
19996 if (CALL_P (insn))
19998 if (!SIBLING_CALL_P (insn))
19999 return 1;
20001 else if (find_regno_note (insn, REG_INC, LR_REGNO))
20002 return 1;
20003 else if (set_of (reg, insn) != NULL_RTX
20004 && !prologue_epilogue_contains (insn))
20005 return 1;
20008 return 0;
20011 /* Emit instructions needed to load the TOC register.
20012 This is only needed when TARGET_TOC, TARGET_MINIMAL_TOC, and there is
20013 a constant pool; or for SVR4 -fpic. */
20015 void
20016 rs6000_emit_load_toc_table (int fromprolog)
20018 rtx dest;
20019 dest = gen_rtx_REG (Pmode, RS6000_PIC_OFFSET_TABLE_REGNUM);
20021 if (TARGET_ELF && TARGET_SECURE_PLT && DEFAULT_ABI != ABI_AIX && flag_pic)
20023 char buf[30];
20024 rtx lab, tmp1, tmp2, got;
20026 lab = gen_label_rtx ();
20027 ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (lab));
20028 lab = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
20029 if (flag_pic == 2)
20030 got = gen_rtx_SYMBOL_REF (Pmode, toc_label_name);
20031 else
20032 got = rs6000_got_sym ();
20033 tmp1 = tmp2 = dest;
20034 if (!fromprolog)
20036 tmp1 = gen_reg_rtx (Pmode);
20037 tmp2 = gen_reg_rtx (Pmode);
20039 emit_insn (gen_load_toc_v4_PIC_1 (lab));
20040 emit_move_insn (tmp1, gen_rtx_REG (Pmode, LR_REGNO));
20041 emit_insn (gen_load_toc_v4_PIC_3b (tmp2, tmp1, got, lab));
20042 emit_insn (gen_load_toc_v4_PIC_3c (dest, tmp2, got, lab));
20044 else if (TARGET_ELF && DEFAULT_ABI == ABI_V4 && flag_pic == 1)
20046 emit_insn (gen_load_toc_v4_pic_si ());
20047 emit_move_insn (dest, gen_rtx_REG (Pmode, LR_REGNO));
20049 else if (TARGET_ELF && DEFAULT_ABI != ABI_AIX && flag_pic == 2)
20051 char buf[30];
20052 rtx temp0 = (fromprolog
20053 ? gen_rtx_REG (Pmode, 0)
20054 : gen_reg_rtx (Pmode));
20056 if (fromprolog)
20058 rtx symF, symL;
20060 ASM_GENERATE_INTERNAL_LABEL (buf, "LCF", rs6000_pic_labelno);
20061 symF = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
20063 ASM_GENERATE_INTERNAL_LABEL (buf, "LCL", rs6000_pic_labelno);
20064 symL = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
20066 emit_insn (gen_load_toc_v4_PIC_1 (symF));
20067 emit_move_insn (dest, gen_rtx_REG (Pmode, LR_REGNO));
20068 emit_insn (gen_load_toc_v4_PIC_2 (temp0, dest, symL, symF));
20070 else
20072 rtx tocsym, lab;
20074 tocsym = gen_rtx_SYMBOL_REF (Pmode, toc_label_name);
20075 lab = gen_label_rtx ();
20076 emit_insn (gen_load_toc_v4_PIC_1b (tocsym, lab));
20077 emit_move_insn (dest, gen_rtx_REG (Pmode, LR_REGNO));
20078 if (TARGET_LINK_STACK)
20079 emit_insn (gen_addsi3 (dest, dest, GEN_INT (4)));
20080 emit_move_insn (temp0, gen_rtx_MEM (Pmode, dest));
20082 emit_insn (gen_addsi3 (dest, temp0, dest));
20084 else if (TARGET_ELF && !TARGET_AIX && flag_pic == 0 && TARGET_MINIMAL_TOC)
20086 /* This is for AIX code running in non-PIC ELF32. */
20087 char buf[30];
20088 rtx realsym;
20089 ASM_GENERATE_INTERNAL_LABEL (buf, "LCTOC", 1);
20090 realsym = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
20092 emit_insn (gen_elf_high (dest, realsym));
20093 emit_insn (gen_elf_low (dest, dest, realsym));
20095 else
20097 gcc_assert (DEFAULT_ABI == ABI_AIX);
20099 if (TARGET_32BIT)
20100 emit_insn (gen_load_toc_aix_si (dest));
20101 else
20102 emit_insn (gen_load_toc_aix_di (dest));
20106 /* Emit instructions to restore the link register after determining where
20107 its value has been stored. */
20109 void
20110 rs6000_emit_eh_reg_restore (rtx source, rtx scratch)
20112 rs6000_stack_t *info = rs6000_stack_info ();
20113 rtx operands[2];
20115 operands[0] = source;
20116 operands[1] = scratch;
20118 if (info->lr_save_p)
20120 rtx frame_rtx = stack_pointer_rtx;
20121 HOST_WIDE_INT sp_offset = 0;
20122 rtx tmp;
20124 if (frame_pointer_needed
20125 || cfun->calls_alloca
20126 || info->total_size > 32767)
20128 tmp = gen_frame_mem (Pmode, frame_rtx);
20129 emit_move_insn (operands[1], tmp);
20130 frame_rtx = operands[1];
20132 else if (info->push_p)
20133 sp_offset = info->total_size;
20135 tmp = plus_constant (Pmode, frame_rtx,
20136 info->lr_save_offset + sp_offset);
20137 tmp = gen_frame_mem (Pmode, tmp);
20138 emit_move_insn (tmp, operands[0]);
20140 else
20141 emit_move_insn (gen_rtx_REG (Pmode, LR_REGNO), operands[0]);
20143 /* Freeze lr_save_p. We've just emitted rtl that depends on the
20144 state of lr_save_p so any change from here on would be a bug. In
20145 particular, stop rs6000_ra_ever_killed from considering the SET
20146 of lr we may have added just above. */
20147 cfun->machine->lr_save_state = info->lr_save_p + 1;
20150 static GTY(()) alias_set_type set = -1;
20152 alias_set_type
20153 get_TOC_alias_set (void)
20155 if (set == -1)
20156 set = new_alias_set ();
20157 return set;
20160 /* This returns nonzero if the current function uses the TOC. This is
20161 determined by the presence of (use (unspec ... UNSPEC_TOC)), which
20162 is generated by the ABI_V4 load_toc_* patterns. */
20163 #if TARGET_ELF
20164 static int
20165 uses_TOC (void)
20167 rtx insn;
20169 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
20170 if (INSN_P (insn))
20172 rtx pat = PATTERN (insn);
20173 int i;
20175 if (GET_CODE (pat) == PARALLEL)
20176 for (i = 0; i < XVECLEN (pat, 0); i++)
20178 rtx sub = XVECEXP (pat, 0, i);
20179 if (GET_CODE (sub) == USE)
20181 sub = XEXP (sub, 0);
20182 if (GET_CODE (sub) == UNSPEC
20183 && XINT (sub, 1) == UNSPEC_TOC)
20184 return 1;
20188 return 0;
20190 #endif
20193 create_TOC_reference (rtx symbol, rtx largetoc_reg)
20195 rtx tocrel, tocreg, hi;
20197 if (TARGET_DEBUG_ADDR)
20199 if (GET_CODE (symbol) == SYMBOL_REF)
20200 fprintf (stderr, "\ncreate_TOC_reference, (symbol_ref %s)\n",
20201 XSTR (symbol, 0));
20202 else
20204 fprintf (stderr, "\ncreate_TOC_reference, code %s:\n",
20205 GET_RTX_NAME (GET_CODE (symbol)));
20206 debug_rtx (symbol);
20210 if (!can_create_pseudo_p ())
20211 df_set_regs_ever_live (TOC_REGISTER, true);
20213 tocreg = gen_rtx_REG (Pmode, TOC_REGISTER);
20214 tocrel = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, symbol, tocreg), UNSPEC_TOCREL);
20215 if (TARGET_CMODEL == CMODEL_SMALL || can_create_pseudo_p ())
20216 return tocrel;
20218 hi = gen_rtx_HIGH (Pmode, copy_rtx (tocrel));
20219 if (largetoc_reg != NULL)
20221 emit_move_insn (largetoc_reg, hi);
20222 hi = largetoc_reg;
20224 return gen_rtx_LO_SUM (Pmode, hi, tocrel);
20227 /* Issue assembly directives that create a reference to the given DWARF
20228 FRAME_TABLE_LABEL from the current function section. */
20229 void
20230 rs6000_aix_asm_output_dwarf_table_ref (char * frame_table_label)
20232 fprintf (asm_out_file, "\t.ref %s\n",
20233 (* targetm.strip_name_encoding) (frame_table_label));
20236 /* This ties together stack memory (MEM with an alias set of frame_alias_set)
20237 and the change to the stack pointer. */
20239 static void
20240 rs6000_emit_stack_tie (rtx fp, bool hard_frame_needed)
20242 rtvec p;
20243 int i;
20244 rtx regs[3];
20246 i = 0;
20247 regs[i++] = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
20248 if (hard_frame_needed)
20249 regs[i++] = gen_rtx_REG (Pmode, HARD_FRAME_POINTER_REGNUM);
20250 if (!(REGNO (fp) == STACK_POINTER_REGNUM
20251 || (hard_frame_needed
20252 && REGNO (fp) == HARD_FRAME_POINTER_REGNUM)))
20253 regs[i++] = fp;
20255 p = rtvec_alloc (i);
20256 while (--i >= 0)
20258 rtx mem = gen_frame_mem (BLKmode, regs[i]);
20259 RTVEC_ELT (p, i) = gen_rtx_SET (VOIDmode, mem, const0_rtx);
20262 emit_insn (gen_stack_tie (gen_rtx_PARALLEL (VOIDmode, p)));
20265 /* Emit the correct code for allocating stack space, as insns.
20266 If COPY_REG, make sure a copy of the old frame is left there.
20267 The generated code may use hard register 0 as a temporary. */
20269 static void
20270 rs6000_emit_allocate_stack (HOST_WIDE_INT size, rtx copy_reg, int copy_off)
20272 rtx insn;
20273 rtx stack_reg = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
20274 rtx tmp_reg = gen_rtx_REG (Pmode, 0);
20275 rtx todec = gen_int_mode (-size, Pmode);
20276 rtx par, set, mem;
20278 if (INTVAL (todec) != -size)
20280 warning (0, "stack frame too large");
20281 emit_insn (gen_trap ());
20282 return;
20285 if (crtl->limit_stack)
20287 if (REG_P (stack_limit_rtx)
20288 && REGNO (stack_limit_rtx) > 1
20289 && REGNO (stack_limit_rtx) <= 31)
20291 emit_insn (gen_add3_insn (tmp_reg, stack_limit_rtx, GEN_INT (size)));
20292 emit_insn (gen_cond_trap (LTU, stack_reg, tmp_reg,
20293 const0_rtx));
20295 else if (GET_CODE (stack_limit_rtx) == SYMBOL_REF
20296 && TARGET_32BIT
20297 && DEFAULT_ABI == ABI_V4)
20299 rtx toload = gen_rtx_CONST (VOIDmode,
20300 gen_rtx_PLUS (Pmode,
20301 stack_limit_rtx,
20302 GEN_INT (size)));
20304 emit_insn (gen_elf_high (tmp_reg, toload));
20305 emit_insn (gen_elf_low (tmp_reg, tmp_reg, toload));
20306 emit_insn (gen_cond_trap (LTU, stack_reg, tmp_reg,
20307 const0_rtx));
20309 else
20310 warning (0, "stack limit expression is not supported");
20313 if (copy_reg)
20315 if (copy_off != 0)
20316 emit_insn (gen_add3_insn (copy_reg, stack_reg, GEN_INT (copy_off)));
20317 else
20318 emit_move_insn (copy_reg, stack_reg);
20321 if (size > 32767)
20323 /* Need a note here so that try_split doesn't get confused. */
20324 if (get_last_insn () == NULL_RTX)
20325 emit_note (NOTE_INSN_DELETED);
20326 insn = emit_move_insn (tmp_reg, todec);
20327 try_split (PATTERN (insn), insn, 0);
20328 todec = tmp_reg;
20331 insn = emit_insn (TARGET_32BIT
20332 ? gen_movsi_update_stack (stack_reg, stack_reg,
20333 todec, stack_reg)
20334 : gen_movdi_di_update_stack (stack_reg, stack_reg,
20335 todec, stack_reg));
20336 /* Since we didn't use gen_frame_mem to generate the MEM, grab
20337 it now and set the alias set/attributes. The above gen_*_update
20338 calls will generate a PARALLEL with the MEM set being the first
20339 operation. */
20340 par = PATTERN (insn);
20341 gcc_assert (GET_CODE (par) == PARALLEL);
20342 set = XVECEXP (par, 0, 0);
20343 gcc_assert (GET_CODE (set) == SET);
20344 mem = SET_DEST (set);
20345 gcc_assert (MEM_P (mem));
20346 MEM_NOTRAP_P (mem) = 1;
20347 set_mem_alias_set (mem, get_frame_alias_set ());
20349 RTX_FRAME_RELATED_P (insn) = 1;
20350 add_reg_note (insn, REG_FRAME_RELATED_EXPR,
20351 gen_rtx_SET (VOIDmode, stack_reg,
20352 gen_rtx_PLUS (Pmode, stack_reg,
20353 GEN_INT (-size))));
20356 #define PROBE_INTERVAL (1 << STACK_CHECK_PROBE_INTERVAL_EXP)
20358 #if PROBE_INTERVAL > 32768
20359 #error Cannot use indexed addressing mode for stack probing
20360 #endif
20362 /* Emit code to probe a range of stack addresses from FIRST to FIRST+SIZE,
20363 inclusive. These are offsets from the current stack pointer. */
20365 static void
20366 rs6000_emit_probe_stack_range (HOST_WIDE_INT first, HOST_WIDE_INT size)
20368 /* See if we have a constant small number of probes to generate. If so,
20369 that's the easy case. */
20370 if (first + size <= 32768)
20372 HOST_WIDE_INT i;
20374 /* Probe at FIRST + N * PROBE_INTERVAL for values of N from 1 until
20375 it exceeds SIZE. If only one probe is needed, this will not
20376 generate any code. Then probe at FIRST + SIZE. */
20377 for (i = PROBE_INTERVAL; i < size; i += PROBE_INTERVAL)
20378 emit_stack_probe (plus_constant (Pmode, stack_pointer_rtx,
20379 -(first + i)));
20381 emit_stack_probe (plus_constant (Pmode, stack_pointer_rtx,
20382 -(first + size)));
20385 /* Otherwise, do the same as above, but in a loop. Note that we must be
20386 extra careful with variables wrapping around because we might be at
20387 the very top (or the very bottom) of the address space and we have
20388 to be able to handle this case properly; in particular, we use an
20389 equality test for the loop condition. */
20390 else
20392 HOST_WIDE_INT rounded_size;
20393 rtx r12 = gen_rtx_REG (Pmode, 12);
20394 rtx r0 = gen_rtx_REG (Pmode, 0);
20396 /* Sanity check for the addressing mode we're going to use. */
20397 gcc_assert (first <= 32768);
20399 /* Step 1: round SIZE to the previous multiple of the interval. */
20401 rounded_size = size & -PROBE_INTERVAL;
20404 /* Step 2: compute initial and final value of the loop counter. */
20406 /* TEST_ADDR = SP + FIRST. */
20407 emit_insn (gen_rtx_SET (VOIDmode, r12,
20408 plus_constant (Pmode, stack_pointer_rtx,
20409 -first)));
20411 /* LAST_ADDR = SP + FIRST + ROUNDED_SIZE. */
20412 if (rounded_size > 32768)
20414 emit_move_insn (r0, GEN_INT (-rounded_size));
20415 emit_insn (gen_rtx_SET (VOIDmode, r0,
20416 gen_rtx_PLUS (Pmode, r12, r0)));
20418 else
20419 emit_insn (gen_rtx_SET (VOIDmode, r0,
20420 plus_constant (Pmode, r12, -rounded_size)));
20423 /* Step 3: the loop
20425 while (TEST_ADDR != LAST_ADDR)
20427 TEST_ADDR = TEST_ADDR + PROBE_INTERVAL
20428 probe at TEST_ADDR
20431 probes at FIRST + N * PROBE_INTERVAL for values of N from 1
20432 until it is equal to ROUNDED_SIZE. */
20434 if (TARGET_64BIT)
20435 emit_insn (gen_probe_stack_rangedi (r12, r12, r0));
20436 else
20437 emit_insn (gen_probe_stack_rangesi (r12, r12, r0));
20440 /* Step 4: probe at FIRST + SIZE if we cannot assert at compile-time
20441 that SIZE is equal to ROUNDED_SIZE. */
20443 if (size != rounded_size)
20444 emit_stack_probe (plus_constant (Pmode, r12, rounded_size - size));
20448 /* Probe a range of stack addresses from REG1 to REG2 inclusive. These are
20449 absolute addresses. */
20451 const char *
20452 output_probe_stack_range (rtx reg1, rtx reg2)
20454 static int labelno = 0;
20455 char loop_lab[32], end_lab[32];
20456 rtx xops[2];
20458 ASM_GENERATE_INTERNAL_LABEL (loop_lab, "LPSRL", labelno);
20459 ASM_GENERATE_INTERNAL_LABEL (end_lab, "LPSRE", labelno++);
20461 ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, loop_lab);
20463 /* Jump to END_LAB if TEST_ADDR == LAST_ADDR. */
20464 xops[0] = reg1;
20465 xops[1] = reg2;
20466 if (TARGET_64BIT)
20467 output_asm_insn ("cmpd 0,%0,%1", xops);
20468 else
20469 output_asm_insn ("cmpw 0,%0,%1", xops);
20471 fputs ("\tbeq 0,", asm_out_file);
20472 assemble_name_raw (asm_out_file, end_lab);
20473 fputc ('\n', asm_out_file);
20475 /* TEST_ADDR = TEST_ADDR + PROBE_INTERVAL. */
20476 xops[1] = GEN_INT (-PROBE_INTERVAL);
20477 output_asm_insn ("addi %0,%0,%1", xops);
20479 /* Probe at TEST_ADDR and branch. */
20480 xops[1] = gen_rtx_REG (Pmode, 0);
20481 output_asm_insn ("stw %1,0(%0)", xops);
20482 fprintf (asm_out_file, "\tb ");
20483 assemble_name_raw (asm_out_file, loop_lab);
20484 fputc ('\n', asm_out_file);
20486 ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, end_lab);
20488 return "";
20491 /* Add to 'insn' a note which is PATTERN (INSN) but with REG replaced
20492 with (plus:P (reg 1) VAL), and with REG2 replaced with RREG if REG2
20493 is not NULL. It would be nice if dwarf2out_frame_debug_expr could
20494 deduce these equivalences by itself so it wasn't necessary to hold
20495 its hand so much. Don't be tempted to always supply d2_f_d_e with
20496 the actual cfa register, ie. r31 when we are using a hard frame
20497 pointer. That fails when saving regs off r1, and sched moves the
20498 r31 setup past the reg saves. */
20500 static rtx
20501 rs6000_frame_related (rtx insn, rtx reg, HOST_WIDE_INT val,
20502 rtx reg2, rtx rreg)
20504 rtx real, temp;
20506 if (REGNO (reg) == STACK_POINTER_REGNUM && reg2 == NULL_RTX)
20508 /* No need for any replacement. Just set RTX_FRAME_RELATED_P. */
20509 int i;
20511 gcc_checking_assert (val == 0);
20512 real = PATTERN (insn);
20513 if (GET_CODE (real) == PARALLEL)
20514 for (i = 0; i < XVECLEN (real, 0); i++)
20515 if (GET_CODE (XVECEXP (real, 0, i)) == SET)
20517 rtx set = XVECEXP (real, 0, i);
20519 RTX_FRAME_RELATED_P (set) = 1;
20521 RTX_FRAME_RELATED_P (insn) = 1;
20522 return insn;
20525 /* copy_rtx will not make unique copies of registers, so we need to
20526 ensure we don't have unwanted sharing here. */
20527 if (reg == reg2)
20528 reg = gen_raw_REG (GET_MODE (reg), REGNO (reg));
20530 if (reg == rreg)
20531 reg = gen_raw_REG (GET_MODE (reg), REGNO (reg));
20533 real = copy_rtx (PATTERN (insn));
20535 if (reg2 != NULL_RTX)
20536 real = replace_rtx (real, reg2, rreg);
20538 if (REGNO (reg) == STACK_POINTER_REGNUM)
20539 gcc_checking_assert (val == 0);
20540 else
20541 real = replace_rtx (real, reg,
20542 gen_rtx_PLUS (Pmode, gen_rtx_REG (Pmode,
20543 STACK_POINTER_REGNUM),
20544 GEN_INT (val)));
20546 /* We expect that 'real' is either a SET or a PARALLEL containing
20547 SETs (and possibly other stuff). In a PARALLEL, all the SETs
20548 are important so they all have to be marked RTX_FRAME_RELATED_P. */
20550 if (GET_CODE (real) == SET)
20552 rtx set = real;
20554 temp = simplify_rtx (SET_SRC (set));
20555 if (temp)
20556 SET_SRC (set) = temp;
20557 temp = simplify_rtx (SET_DEST (set));
20558 if (temp)
20559 SET_DEST (set) = temp;
20560 if (GET_CODE (SET_DEST (set)) == MEM)
20562 temp = simplify_rtx (XEXP (SET_DEST (set), 0));
20563 if (temp)
20564 XEXP (SET_DEST (set), 0) = temp;
20567 else
20569 int i;
20571 gcc_assert (GET_CODE (real) == PARALLEL);
20572 for (i = 0; i < XVECLEN (real, 0); i++)
20573 if (GET_CODE (XVECEXP (real, 0, i)) == SET)
20575 rtx set = XVECEXP (real, 0, i);
20577 temp = simplify_rtx (SET_SRC (set));
20578 if (temp)
20579 SET_SRC (set) = temp;
20580 temp = simplify_rtx (SET_DEST (set));
20581 if (temp)
20582 SET_DEST (set) = temp;
20583 if (GET_CODE (SET_DEST (set)) == MEM)
20585 temp = simplify_rtx (XEXP (SET_DEST (set), 0));
20586 if (temp)
20587 XEXP (SET_DEST (set), 0) = temp;
20589 RTX_FRAME_RELATED_P (set) = 1;
20593 RTX_FRAME_RELATED_P (insn) = 1;
20594 add_reg_note (insn, REG_FRAME_RELATED_EXPR, real);
20596 return insn;
20599 /* Returns an insn that has a vrsave set operation with the
20600 appropriate CLOBBERs. */
20602 static rtx
20603 generate_set_vrsave (rtx reg, rs6000_stack_t *info, int epiloguep)
20605 int nclobs, i;
20606 rtx insn, clobs[TOTAL_ALTIVEC_REGS + 1];
20607 rtx vrsave = gen_rtx_REG (SImode, VRSAVE_REGNO);
20609 clobs[0]
20610 = gen_rtx_SET (VOIDmode,
20611 vrsave,
20612 gen_rtx_UNSPEC_VOLATILE (SImode,
20613 gen_rtvec (2, reg, vrsave),
20614 UNSPECV_SET_VRSAVE));
20616 nclobs = 1;
20618 /* We need to clobber the registers in the mask so the scheduler
20619 does not move sets to VRSAVE before sets of AltiVec registers.
20621 However, if the function receives nonlocal gotos, reload will set
20622 all call saved registers live. We will end up with:
20624 (set (reg 999) (mem))
20625 (parallel [ (set (reg vrsave) (unspec blah))
20626 (clobber (reg 999))])
20628 The clobber will cause the store into reg 999 to be dead, and
20629 flow will attempt to delete an epilogue insn. In this case, we
20630 need an unspec use/set of the register. */
20632 for (i = FIRST_ALTIVEC_REGNO; i <= LAST_ALTIVEC_REGNO; ++i)
20633 if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
20635 if (!epiloguep || call_used_regs [i])
20636 clobs[nclobs++] = gen_rtx_CLOBBER (VOIDmode,
20637 gen_rtx_REG (V4SImode, i));
20638 else
20640 rtx reg = gen_rtx_REG (V4SImode, i);
20642 clobs[nclobs++]
20643 = gen_rtx_SET (VOIDmode,
20644 reg,
20645 gen_rtx_UNSPEC (V4SImode,
20646 gen_rtvec (1, reg), 27));
20650 insn = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (nclobs));
20652 for (i = 0; i < nclobs; ++i)
20653 XVECEXP (insn, 0, i) = clobs[i];
20655 return insn;
20658 static rtx
20659 gen_frame_set (rtx reg, rtx frame_reg, int offset, bool store)
20661 rtx addr, mem;
20663 addr = gen_rtx_PLUS (Pmode, frame_reg, GEN_INT (offset));
20664 mem = gen_frame_mem (GET_MODE (reg), addr);
20665 return gen_rtx_SET (VOIDmode, store ? mem : reg, store ? reg : mem);
20668 static rtx
20669 gen_frame_load (rtx reg, rtx frame_reg, int offset)
20671 return gen_frame_set (reg, frame_reg, offset, false);
20674 static rtx
20675 gen_frame_store (rtx reg, rtx frame_reg, int offset)
20677 return gen_frame_set (reg, frame_reg, offset, true);
20680 /* Save a register into the frame, and emit RTX_FRAME_RELATED_P notes.
20681 Save REGNO into [FRAME_REG + OFFSET] in mode MODE. */
20683 static rtx
20684 emit_frame_save (rtx frame_reg, enum machine_mode mode,
20685 unsigned int regno, int offset, HOST_WIDE_INT frame_reg_to_sp)
20687 rtx reg, insn;
20689 /* Some cases that need register indexed addressing. */
20690 gcc_checking_assert (!((TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (mode))
20691 || (TARGET_VSX && ALTIVEC_OR_VSX_VECTOR_MODE (mode))
20692 || (TARGET_E500_DOUBLE && mode == DFmode)
20693 || (TARGET_SPE_ABI
20694 && SPE_VECTOR_MODE (mode)
20695 && !SPE_CONST_OFFSET_OK (offset))));
20697 reg = gen_rtx_REG (mode, regno);
20698 insn = emit_insn (gen_frame_store (reg, frame_reg, offset));
20699 return rs6000_frame_related (insn, frame_reg, frame_reg_to_sp,
20700 NULL_RTX, NULL_RTX);
20703 /* Emit an offset memory reference suitable for a frame store, while
20704 converting to a valid addressing mode. */
20706 static rtx
20707 gen_frame_mem_offset (enum machine_mode mode, rtx reg, int offset)
20709 rtx int_rtx, offset_rtx;
20711 int_rtx = GEN_INT (offset);
20713 if ((TARGET_SPE_ABI && SPE_VECTOR_MODE (mode) && !SPE_CONST_OFFSET_OK (offset))
20714 || (TARGET_E500_DOUBLE && mode == DFmode))
20716 offset_rtx = gen_rtx_REG (Pmode, FIXED_SCRATCH);
20717 emit_move_insn (offset_rtx, int_rtx);
20719 else
20720 offset_rtx = int_rtx;
20722 return gen_frame_mem (mode, gen_rtx_PLUS (Pmode, reg, offset_rtx));
20725 #ifndef TARGET_FIX_AND_CONTINUE
20726 #define TARGET_FIX_AND_CONTINUE 0
20727 #endif
20729 /* It's really GPR 13 or 14, FPR 14 and VR 20. We need the smallest. */
20730 #define FIRST_SAVRES_REGISTER FIRST_SAVED_GP_REGNO
20731 #define LAST_SAVRES_REGISTER 31
20732 #define N_SAVRES_REGISTERS (LAST_SAVRES_REGISTER - FIRST_SAVRES_REGISTER + 1)
20734 enum {
20735 SAVRES_LR = 0x1,
20736 SAVRES_SAVE = 0x2,
20737 SAVRES_REG = 0x0c,
20738 SAVRES_GPR = 0,
20739 SAVRES_FPR = 4,
20740 SAVRES_VR = 8
20743 static GTY(()) rtx savres_routine_syms[N_SAVRES_REGISTERS][12];
20745 /* Temporary holding space for an out-of-line register save/restore
20746 routine name. */
20747 static char savres_routine_name[30];
20749 /* Return the name for an out-of-line register save/restore routine.
20750 We are saving/restoring GPRs if GPR is true. */
20752 static char *
20753 rs6000_savres_routine_name (rs6000_stack_t *info, int regno, int sel)
20755 const char *prefix = "";
20756 const char *suffix = "";
20758 /* Different targets are supposed to define
20759 {SAVE,RESTORE}_FP_{PREFIX,SUFFIX} with the idea that the needed
20760 routine name could be defined with:
20762 sprintf (name, "%s%d%s", SAVE_FP_PREFIX, regno, SAVE_FP_SUFFIX)
20764 This is a nice idea in practice, but in reality, things are
20765 complicated in several ways:
20767 - ELF targets have save/restore routines for GPRs.
20769 - SPE targets use different prefixes for 32/64-bit registers, and
20770 neither of them fit neatly in the FOO_{PREFIX,SUFFIX} regimen.
20772 - PPC64 ELF targets have routines for save/restore of GPRs that
20773 differ in what they do with the link register, so having a set
20774 prefix doesn't work. (We only use one of the save routines at
20775 the moment, though.)
20777 - PPC32 elf targets have "exit" versions of the restore routines
20778 that restore the link register and can save some extra space.
20779 These require an extra suffix. (There are also "tail" versions
20780 of the restore routines and "GOT" versions of the save routines,
20781 but we don't generate those at present. Same problems apply,
20782 though.)
20784 We deal with all this by synthesizing our own prefix/suffix and
20785 using that for the simple sprintf call shown above. */
20786 if (TARGET_SPE)
20788 /* No floating point saves on the SPE. */
20789 gcc_assert ((sel & SAVRES_REG) == SAVRES_GPR);
20791 if ((sel & SAVRES_SAVE))
20792 prefix = info->spe_64bit_regs_used ? "_save64gpr_" : "_save32gpr_";
20793 else
20794 prefix = info->spe_64bit_regs_used ? "_rest64gpr_" : "_rest32gpr_";
20796 if ((sel & SAVRES_LR))
20797 suffix = "_x";
20799 else if (DEFAULT_ABI == ABI_V4)
20801 if (TARGET_64BIT)
20802 goto aix_names;
20804 if ((sel & SAVRES_REG) == SAVRES_GPR)
20805 prefix = (sel & SAVRES_SAVE) ? "_savegpr_" : "_restgpr_";
20806 else if ((sel & SAVRES_REG) == SAVRES_FPR)
20807 prefix = (sel & SAVRES_SAVE) ? "_savefpr_" : "_restfpr_";
20808 else if ((sel & SAVRES_REG) == SAVRES_VR)
20809 prefix = (sel & SAVRES_SAVE) ? "_savevr_" : "_restvr_";
20810 else
20811 abort ();
20813 if ((sel & SAVRES_LR))
20814 suffix = "_x";
20816 else if (DEFAULT_ABI == ABI_AIX)
20818 #if !defined (POWERPC_LINUX) && !defined (POWERPC_FREEBSD)
20819 /* No out-of-line save/restore routines for GPRs on AIX. */
20820 gcc_assert (!TARGET_AIX || (sel & SAVRES_REG) != SAVRES_GPR);
20821 #endif
20823 aix_names:
20824 if ((sel & SAVRES_REG) == SAVRES_GPR)
20825 prefix = ((sel & SAVRES_SAVE)
20826 ? ((sel & SAVRES_LR) ? "_savegpr0_" : "_savegpr1_")
20827 : ((sel & SAVRES_LR) ? "_restgpr0_" : "_restgpr1_"));
20828 else if ((sel & SAVRES_REG) == SAVRES_FPR)
20830 #if defined (POWERPC_LINUX) || defined (POWERPC_FREEBSD)
20831 if ((sel & SAVRES_LR))
20832 prefix = ((sel & SAVRES_SAVE) ? "_savefpr_" : "_restfpr_");
20833 else
20834 #endif
20836 prefix = (sel & SAVRES_SAVE) ? SAVE_FP_PREFIX : RESTORE_FP_PREFIX;
20837 suffix = (sel & SAVRES_SAVE) ? SAVE_FP_SUFFIX : RESTORE_FP_SUFFIX;
20840 else if ((sel & SAVRES_REG) == SAVRES_VR)
20841 prefix = (sel & SAVRES_SAVE) ? "_savevr_" : "_restvr_";
20842 else
20843 abort ();
20846 if (DEFAULT_ABI == ABI_DARWIN)
20848 /* The Darwin approach is (slightly) different, in order to be
20849 compatible with code generated by the system toolchain. There is a
20850 single symbol for the start of save sequence, and the code here
20851 embeds an offset into that code on the basis of the first register
20852 to be saved. */
20853 prefix = (sel & SAVRES_SAVE) ? "save" : "rest" ;
20854 if ((sel & SAVRES_REG) == SAVRES_GPR)
20855 sprintf (savres_routine_name, "*%sGPR%s%s%.0d ; %s r%d-r31", prefix,
20856 ((sel & SAVRES_LR) ? "x" : ""), (regno == 13 ? "" : "+"),
20857 (regno - 13) * 4, prefix, regno);
20858 else if ((sel & SAVRES_REG) == SAVRES_FPR)
20859 sprintf (savres_routine_name, "*%sFP%s%.0d ; %s f%d-f31", prefix,
20860 (regno == 14 ? "" : "+"), (regno - 14) * 4, prefix, regno);
20861 else if ((sel & SAVRES_REG) == SAVRES_VR)
20862 sprintf (savres_routine_name, "*%sVEC%s%.0d ; %s v%d-v31", prefix,
20863 (regno == 20 ? "" : "+"), (regno - 20) * 8, prefix, regno);
20864 else
20865 abort ();
20867 else
20868 sprintf (savres_routine_name, "%s%d%s", prefix, regno, suffix);
20870 return savres_routine_name;
20873 /* Return an RTL SYMBOL_REF for an out-of-line register save/restore routine.
20874 We are saving/restoring GPRs if GPR is true. */
20876 static rtx
20877 rs6000_savres_routine_sym (rs6000_stack_t *info, int sel)
20879 int regno = ((sel & SAVRES_REG) == SAVRES_GPR
20880 ? info->first_gp_reg_save
20881 : (sel & SAVRES_REG) == SAVRES_FPR
20882 ? info->first_fp_reg_save - 32
20883 : (sel & SAVRES_REG) == SAVRES_VR
20884 ? info->first_altivec_reg_save - FIRST_ALTIVEC_REGNO
20885 : -1);
20886 rtx sym;
20887 int select = sel;
20889 /* On the SPE, we never have any FPRs, but we do have 32/64-bit
20890 versions of the gpr routines. */
20891 if (TARGET_SPE_ABI && (sel & SAVRES_REG) == SAVRES_GPR
20892 && info->spe_64bit_regs_used)
20893 select ^= SAVRES_FPR ^ SAVRES_GPR;
20895 /* Don't generate bogus routine names. */
20896 gcc_assert (FIRST_SAVRES_REGISTER <= regno
20897 && regno <= LAST_SAVRES_REGISTER
20898 && select >= 0 && select <= 12);
20900 sym = savres_routine_syms[regno-FIRST_SAVRES_REGISTER][select];
20902 if (sym == NULL)
20904 char *name;
20906 name = rs6000_savres_routine_name (info, regno, sel);
20908 sym = savres_routine_syms[regno-FIRST_SAVRES_REGISTER][select]
20909 = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (name));
20910 SYMBOL_REF_FLAGS (sym) |= SYMBOL_FLAG_FUNCTION;
20913 return sym;
20916 /* Emit a sequence of insns, including a stack tie if needed, for
20917 resetting the stack pointer. If UPDT_REGNO is not 1, then don't
20918 reset the stack pointer, but move the base of the frame into
20919 reg UPDT_REGNO for use by out-of-line register restore routines. */
20921 static rtx
20922 rs6000_emit_stack_reset (rs6000_stack_t *info,
20923 rtx frame_reg_rtx, HOST_WIDE_INT frame_off,
20924 unsigned updt_regno)
20926 rtx updt_reg_rtx;
20928 /* This blockage is needed so that sched doesn't decide to move
20929 the sp change before the register restores. */
20930 if (DEFAULT_ABI == ABI_V4
20931 || (TARGET_SPE_ABI
20932 && info->spe_64bit_regs_used != 0
20933 && info->first_gp_reg_save != 32))
20934 rs6000_emit_stack_tie (frame_reg_rtx, frame_pointer_needed);
20936 /* If we are restoring registers out-of-line, we will be using the
20937 "exit" variants of the restore routines, which will reset the
20938 stack for us. But we do need to point updt_reg into the
20939 right place for those routines. */
20940 updt_reg_rtx = gen_rtx_REG (Pmode, updt_regno);
20942 if (frame_off != 0)
20943 return emit_insn (gen_add3_insn (updt_reg_rtx,
20944 frame_reg_rtx, GEN_INT (frame_off)));
20945 else if (REGNO (frame_reg_rtx) != updt_regno)
20946 return emit_move_insn (updt_reg_rtx, frame_reg_rtx);
20948 return NULL_RTX;
20951 /* Return the register number used as a pointer by out-of-line
20952 save/restore functions. */
20954 static inline unsigned
20955 ptr_regno_for_savres (int sel)
20957 if (DEFAULT_ABI == ABI_AIX)
20958 return (sel & SAVRES_REG) == SAVRES_FPR || (sel & SAVRES_LR) ? 1 : 12;
20959 return DEFAULT_ABI == ABI_DARWIN && (sel & SAVRES_REG) == SAVRES_FPR ? 1 : 11;
20962 /* Construct a parallel rtx describing the effect of a call to an
20963 out-of-line register save/restore routine, and emit the insn
20964 or jump_insn as appropriate. */
20966 static rtx
20967 rs6000_emit_savres_rtx (rs6000_stack_t *info,
20968 rtx frame_reg_rtx, int save_area_offset, int lr_offset,
20969 enum machine_mode reg_mode, int sel)
20971 int i;
20972 int offset, start_reg, end_reg, n_regs, use_reg;
20973 int reg_size = GET_MODE_SIZE (reg_mode);
20974 rtx sym;
20975 rtvec p;
20976 rtx par, insn;
20978 offset = 0;
20979 start_reg = ((sel & SAVRES_REG) == SAVRES_GPR
20980 ? info->first_gp_reg_save
20981 : (sel & SAVRES_REG) == SAVRES_FPR
20982 ? info->first_fp_reg_save
20983 : (sel & SAVRES_REG) == SAVRES_VR
20984 ? info->first_altivec_reg_save
20985 : -1);
20986 end_reg = ((sel & SAVRES_REG) == SAVRES_GPR
20987 ? 32
20988 : (sel & SAVRES_REG) == SAVRES_FPR
20989 ? 64
20990 : (sel & SAVRES_REG) == SAVRES_VR
20991 ? LAST_ALTIVEC_REGNO + 1
20992 : -1);
20993 n_regs = end_reg - start_reg;
20994 p = rtvec_alloc (3 + ((sel & SAVRES_LR) ? 1 : 0)
20995 + ((sel & SAVRES_REG) == SAVRES_VR ? 1 : 0)
20996 + n_regs);
20998 if (!(sel & SAVRES_SAVE) && (sel & SAVRES_LR))
20999 RTVEC_ELT (p, offset++) = ret_rtx;
21001 RTVEC_ELT (p, offset++)
21002 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, LR_REGNO));
21004 sym = rs6000_savres_routine_sym (info, sel);
21005 RTVEC_ELT (p, offset++) = gen_rtx_USE (VOIDmode, sym);
21007 use_reg = ptr_regno_for_savres (sel);
21008 if ((sel & SAVRES_REG) == SAVRES_VR)
21010 /* Vector regs are saved/restored using [reg+reg] addressing. */
21011 RTVEC_ELT (p, offset++)
21012 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, use_reg));
21013 RTVEC_ELT (p, offset++)
21014 = gen_rtx_USE (VOIDmode, gen_rtx_REG (Pmode, 0));
21016 else
21017 RTVEC_ELT (p, offset++)
21018 = gen_rtx_USE (VOIDmode, gen_rtx_REG (Pmode, use_reg));
21020 for (i = 0; i < end_reg - start_reg; i++)
21021 RTVEC_ELT (p, i + offset)
21022 = gen_frame_set (gen_rtx_REG (reg_mode, start_reg + i),
21023 frame_reg_rtx, save_area_offset + reg_size * i,
21024 (sel & SAVRES_SAVE) != 0);
21026 if ((sel & SAVRES_SAVE) && (sel & SAVRES_LR))
21027 RTVEC_ELT (p, i + offset)
21028 = gen_frame_store (gen_rtx_REG (Pmode, 0), frame_reg_rtx, lr_offset);
21030 par = gen_rtx_PARALLEL (VOIDmode, p);
21032 if (!(sel & SAVRES_SAVE) && (sel & SAVRES_LR))
21034 insn = emit_jump_insn (par);
21035 JUMP_LABEL (insn) = ret_rtx;
21037 else
21038 insn = emit_insn (par);
21039 return insn;
21042 /* Determine whether the gp REG is really used. */
21044 static bool
21045 rs6000_reg_live_or_pic_offset_p (int reg)
21047 /* If the function calls eh_return, claim used all the registers that would
21048 be checked for liveness otherwise. This is required for the PIC offset
21049 register with -mminimal-toc on AIX, as it is advertised as "fixed" for
21050 register allocation purposes in this case. */
21052 return (((crtl->calls_eh_return || df_regs_ever_live_p (reg))
21053 && (!call_used_regs[reg]
21054 || (reg == RS6000_PIC_OFFSET_TABLE_REGNUM
21055 && !TARGET_SINGLE_PIC_BASE
21056 && TARGET_TOC && TARGET_MINIMAL_TOC)))
21057 || (reg == RS6000_PIC_OFFSET_TABLE_REGNUM
21058 && !TARGET_SINGLE_PIC_BASE
21059 && ((DEFAULT_ABI == ABI_V4 && flag_pic != 0)
21060 || (DEFAULT_ABI == ABI_DARWIN && flag_pic))));
21063 /* Emit function prologue as insns. */
21065 void
21066 rs6000_emit_prologue (void)
21068 rs6000_stack_t *info = rs6000_stack_info ();
21069 enum machine_mode reg_mode = Pmode;
21070 int reg_size = TARGET_32BIT ? 4 : 8;
21071 rtx sp_reg_rtx = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
21072 rtx frame_reg_rtx = sp_reg_rtx;
21073 unsigned int cr_save_regno;
21074 rtx cr_save_rtx = NULL_RTX;
21075 rtx insn;
21076 int strategy;
21077 int using_static_chain_p = (cfun->static_chain_decl != NULL_TREE
21078 && df_regs_ever_live_p (STATIC_CHAIN_REGNUM)
21079 && call_used_regs[STATIC_CHAIN_REGNUM]);
21080 /* Offset to top of frame for frame_reg and sp respectively. */
21081 HOST_WIDE_INT frame_off = 0;
21082 HOST_WIDE_INT sp_off = 0;
21084 #ifdef ENABLE_CHECKING
21085 /* Track and check usage of r0, r11, r12. */
21086 int reg_inuse = using_static_chain_p ? 1 << 11 : 0;
21087 #define START_USE(R) do \
21089 gcc_assert ((reg_inuse & (1 << (R))) == 0); \
21090 reg_inuse |= 1 << (R); \
21091 } while (0)
21092 #define END_USE(R) do \
21094 gcc_assert ((reg_inuse & (1 << (R))) != 0); \
21095 reg_inuse &= ~(1 << (R)); \
21096 } while (0)
21097 #define NOT_INUSE(R) do \
21099 gcc_assert ((reg_inuse & (1 << (R))) == 0); \
21100 } while (0)
21101 #else
21102 #define START_USE(R) do {} while (0)
21103 #define END_USE(R) do {} while (0)
21104 #define NOT_INUSE(R) do {} while (0)
21105 #endif
21107 if (flag_stack_usage_info)
21108 current_function_static_stack_size = info->total_size;
21110 if (flag_stack_check == STATIC_BUILTIN_STACK_CHECK && info->total_size)
21111 rs6000_emit_probe_stack_range (STACK_CHECK_PROTECT, info->total_size);
21113 if (TARGET_FIX_AND_CONTINUE)
21115 /* gdb on darwin arranges to forward a function from the old
21116 address by modifying the first 5 instructions of the function
21117 to branch to the overriding function. This is necessary to
21118 permit function pointers that point to the old function to
21119 actually forward to the new function. */
21120 emit_insn (gen_nop ());
21121 emit_insn (gen_nop ());
21122 emit_insn (gen_nop ());
21123 emit_insn (gen_nop ());
21124 emit_insn (gen_nop ());
21127 if (TARGET_SPE_ABI && info->spe_64bit_regs_used != 0)
21129 reg_mode = V2SImode;
21130 reg_size = 8;
21133 /* Handle world saves specially here. */
21134 if (WORLD_SAVE_P (info))
21136 int i, j, sz;
21137 rtx treg;
21138 rtvec p;
21139 rtx reg0;
21141 /* save_world expects lr in r0. */
21142 reg0 = gen_rtx_REG (Pmode, 0);
21143 if (info->lr_save_p)
21145 insn = emit_move_insn (reg0,
21146 gen_rtx_REG (Pmode, LR_REGNO));
21147 RTX_FRAME_RELATED_P (insn) = 1;
21150 /* The SAVE_WORLD and RESTORE_WORLD routines make a number of
21151 assumptions about the offsets of various bits of the stack
21152 frame. */
21153 gcc_assert (info->gp_save_offset == -220
21154 && info->fp_save_offset == -144
21155 && info->lr_save_offset == 8
21156 && info->cr_save_offset == 4
21157 && info->push_p
21158 && info->lr_save_p
21159 && (!crtl->calls_eh_return
21160 || info->ehrd_offset == -432)
21161 && info->vrsave_save_offset == -224
21162 && info->altivec_save_offset == -416);
21164 treg = gen_rtx_REG (SImode, 11);
21165 emit_move_insn (treg, GEN_INT (-info->total_size));
21167 /* SAVE_WORLD takes the caller's LR in R0 and the frame size
21168 in R11. It also clobbers R12, so beware! */
21170 /* Preserve CR2 for save_world prologues */
21171 sz = 5;
21172 sz += 32 - info->first_gp_reg_save;
21173 sz += 64 - info->first_fp_reg_save;
21174 sz += LAST_ALTIVEC_REGNO - info->first_altivec_reg_save + 1;
21175 p = rtvec_alloc (sz);
21176 j = 0;
21177 RTVEC_ELT (p, j++) = gen_rtx_CLOBBER (VOIDmode,
21178 gen_rtx_REG (SImode,
21179 LR_REGNO));
21180 RTVEC_ELT (p, j++) = gen_rtx_USE (VOIDmode,
21181 gen_rtx_SYMBOL_REF (Pmode,
21182 "*save_world"));
21183 /* We do floats first so that the instruction pattern matches
21184 properly. */
21185 for (i = 0; i < 64 - info->first_fp_reg_save; i++)
21186 RTVEC_ELT (p, j++)
21187 = gen_frame_store (gen_rtx_REG (TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT
21188 ? DFmode : SFmode,
21189 info->first_fp_reg_save + i),
21190 frame_reg_rtx,
21191 info->fp_save_offset + frame_off + 8 * i);
21192 for (i = 0; info->first_altivec_reg_save + i <= LAST_ALTIVEC_REGNO; i++)
21193 RTVEC_ELT (p, j++)
21194 = gen_frame_store (gen_rtx_REG (V4SImode,
21195 info->first_altivec_reg_save + i),
21196 frame_reg_rtx,
21197 info->altivec_save_offset + frame_off + 16 * i);
21198 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
21199 RTVEC_ELT (p, j++)
21200 = gen_frame_store (gen_rtx_REG (reg_mode, info->first_gp_reg_save + i),
21201 frame_reg_rtx,
21202 info->gp_save_offset + frame_off + reg_size * i);
21204 /* CR register traditionally saved as CR2. */
21205 RTVEC_ELT (p, j++)
21206 = gen_frame_store (gen_rtx_REG (SImode, CR2_REGNO),
21207 frame_reg_rtx, info->cr_save_offset + frame_off);
21208 /* Explain about use of R0. */
21209 if (info->lr_save_p)
21210 RTVEC_ELT (p, j++)
21211 = gen_frame_store (reg0,
21212 frame_reg_rtx, info->lr_save_offset + frame_off);
21213 /* Explain what happens to the stack pointer. */
21215 rtx newval = gen_rtx_PLUS (Pmode, sp_reg_rtx, treg);
21216 RTVEC_ELT (p, j++) = gen_rtx_SET (VOIDmode, sp_reg_rtx, newval);
21219 insn = emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
21220 rs6000_frame_related (insn, frame_reg_rtx, sp_off - frame_off,
21221 treg, GEN_INT (-info->total_size));
21222 sp_off = frame_off = info->total_size;
21225 strategy = info->savres_strategy;
21227 /* For V.4, update stack before we do any saving and set back pointer. */
21228 if (! WORLD_SAVE_P (info)
21229 && info->push_p
21230 && (DEFAULT_ABI == ABI_V4
21231 || crtl->calls_eh_return))
21233 bool need_r11 = (TARGET_SPE
21234 ? (!(strategy & SAVE_INLINE_GPRS)
21235 && info->spe_64bit_regs_used == 0)
21236 : (!(strategy & SAVE_INLINE_FPRS)
21237 || !(strategy & SAVE_INLINE_GPRS)
21238 || !(strategy & SAVE_INLINE_VRS)));
21239 int ptr_regno = -1;
21240 rtx ptr_reg = NULL_RTX;
21241 int ptr_off = 0;
21243 if (info->total_size < 32767)
21244 frame_off = info->total_size;
21245 else if (need_r11)
21246 ptr_regno = 11;
21247 else if (info->cr_save_p
21248 || info->lr_save_p
21249 || info->first_fp_reg_save < 64
21250 || info->first_gp_reg_save < 32
21251 || info->altivec_size != 0
21252 || info->vrsave_mask != 0
21253 || crtl->calls_eh_return)
21254 ptr_regno = 12;
21255 else
21257 /* The prologue won't be saving any regs so there is no need
21258 to set up a frame register to access any frame save area.
21259 We also won't be using frame_off anywhere below, but set
21260 the correct value anyway to protect against future
21261 changes to this function. */
21262 frame_off = info->total_size;
21264 if (ptr_regno != -1)
21266 /* Set up the frame offset to that needed by the first
21267 out-of-line save function. */
21268 START_USE (ptr_regno);
21269 ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
21270 frame_reg_rtx = ptr_reg;
21271 if (!(strategy & SAVE_INLINE_FPRS) && info->fp_size != 0)
21272 gcc_checking_assert (info->fp_save_offset + info->fp_size == 0);
21273 else if (!(strategy & SAVE_INLINE_GPRS) && info->first_gp_reg_save < 32)
21274 ptr_off = info->gp_save_offset + info->gp_size;
21275 else if (!(strategy & SAVE_INLINE_VRS) && info->altivec_size != 0)
21276 ptr_off = info->altivec_save_offset + info->altivec_size;
21277 frame_off = -ptr_off;
21279 rs6000_emit_allocate_stack (info->total_size, ptr_reg, ptr_off);
21280 sp_off = info->total_size;
21281 if (frame_reg_rtx != sp_reg_rtx)
21282 rs6000_emit_stack_tie (frame_reg_rtx, false);
21285 /* If we use the link register, get it into r0. */
21286 if (!WORLD_SAVE_P (info) && info->lr_save_p)
21288 rtx addr, reg, mem;
21290 reg = gen_rtx_REG (Pmode, 0);
21291 START_USE (0);
21292 insn = emit_move_insn (reg, gen_rtx_REG (Pmode, LR_REGNO));
21293 RTX_FRAME_RELATED_P (insn) = 1;
21295 if (!(strategy & (SAVE_NOINLINE_GPRS_SAVES_LR
21296 | SAVE_NOINLINE_FPRS_SAVES_LR)))
21298 addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
21299 GEN_INT (info->lr_save_offset + frame_off));
21300 mem = gen_rtx_MEM (Pmode, addr);
21301 /* This should not be of rs6000_sr_alias_set, because of
21302 __builtin_return_address. */
21304 insn = emit_move_insn (mem, reg);
21305 rs6000_frame_related (insn, frame_reg_rtx, sp_off - frame_off,
21306 NULL_RTX, NULL_RTX);
21307 END_USE (0);
21311 /* If we need to save CR, put it into r12 or r11. Choose r12 except when
21312 r12 will be needed by out-of-line gpr restore. */
21313 cr_save_regno = (DEFAULT_ABI == ABI_AIX
21314 && !(strategy & (SAVE_INLINE_GPRS
21315 | SAVE_NOINLINE_GPRS_SAVES_LR))
21316 ? 11 : 12);
21317 if (!WORLD_SAVE_P (info)
21318 && info->cr_save_p
21319 && REGNO (frame_reg_rtx) != cr_save_regno
21320 && !(using_static_chain_p && cr_save_regno == 11))
21322 rtx set;
21324 cr_save_rtx = gen_rtx_REG (SImode, cr_save_regno);
21325 START_USE (cr_save_regno);
21326 insn = emit_insn (gen_movesi_from_cr (cr_save_rtx));
21327 RTX_FRAME_RELATED_P (insn) = 1;
21328 /* Now, there's no way that dwarf2out_frame_debug_expr is going
21329 to understand '(unspec:SI [(reg:CC 68) ...] UNSPEC_MOVESI_FROM_CR)'.
21330 But that's OK. All we have to do is specify that _one_ condition
21331 code register is saved in this stack slot. The thrower's epilogue
21332 will then restore all the call-saved registers.
21333 We use CR2_REGNO (70) to be compatible with gcc-2.95 on Linux. */
21334 set = gen_rtx_SET (VOIDmode, cr_save_rtx,
21335 gen_rtx_REG (SImode, CR2_REGNO));
21336 add_reg_note (insn, REG_FRAME_RELATED_EXPR, set);
21339 /* Do any required saving of fpr's. If only one or two to save, do
21340 it ourselves. Otherwise, call function. */
21341 if (!WORLD_SAVE_P (info) && (strategy & SAVE_INLINE_FPRS))
21343 int i;
21344 for (i = 0; i < 64 - info->first_fp_reg_save; i++)
21345 if (save_reg_p (info->first_fp_reg_save + i))
21346 emit_frame_save (frame_reg_rtx,
21347 (TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT
21348 ? DFmode : SFmode),
21349 info->first_fp_reg_save + i,
21350 info->fp_save_offset + frame_off + 8 * i,
21351 sp_off - frame_off);
21353 else if (!WORLD_SAVE_P (info) && info->first_fp_reg_save != 64)
21355 bool lr = (strategy & SAVE_NOINLINE_FPRS_SAVES_LR) != 0;
21356 int sel = SAVRES_SAVE | SAVRES_FPR | (lr ? SAVRES_LR : 0);
21357 unsigned ptr_regno = ptr_regno_for_savres (sel);
21358 rtx ptr_reg = frame_reg_rtx;
21360 if (REGNO (frame_reg_rtx) == ptr_regno)
21361 gcc_checking_assert (frame_off == 0);
21362 else
21364 ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
21365 NOT_INUSE (ptr_regno);
21366 emit_insn (gen_add3_insn (ptr_reg,
21367 frame_reg_rtx, GEN_INT (frame_off)));
21369 insn = rs6000_emit_savres_rtx (info, ptr_reg,
21370 info->fp_save_offset,
21371 info->lr_save_offset,
21372 DFmode, sel);
21373 rs6000_frame_related (insn, ptr_reg, sp_off,
21374 NULL_RTX, NULL_RTX);
21375 if (lr)
21376 END_USE (0);
21379 /* Save GPRs. This is done as a PARALLEL if we are using
21380 the store-multiple instructions. */
21381 if (!WORLD_SAVE_P (info)
21382 && TARGET_SPE_ABI
21383 && info->spe_64bit_regs_used != 0
21384 && info->first_gp_reg_save != 32)
21386 int i;
21387 rtx spe_save_area_ptr;
21388 HOST_WIDE_INT save_off;
21389 int ool_adjust = 0;
21391 /* Determine whether we can address all of the registers that need
21392 to be saved with an offset from frame_reg_rtx that fits in
21393 the small const field for SPE memory instructions. */
21394 int spe_regs_addressable
21395 = (SPE_CONST_OFFSET_OK (info->spe_gp_save_offset + frame_off
21396 + reg_size * (32 - info->first_gp_reg_save - 1))
21397 && (strategy & SAVE_INLINE_GPRS));
21399 if (spe_regs_addressable)
21401 spe_save_area_ptr = frame_reg_rtx;
21402 save_off = frame_off;
21404 else
21406 /* Make r11 point to the start of the SPE save area. We need
21407 to be careful here if r11 is holding the static chain. If
21408 it is, then temporarily save it in r0. */
21409 HOST_WIDE_INT offset;
21411 if (!(strategy & SAVE_INLINE_GPRS))
21412 ool_adjust = 8 * (info->first_gp_reg_save - FIRST_SAVED_GP_REGNO);
21413 offset = info->spe_gp_save_offset + frame_off - ool_adjust;
21414 spe_save_area_ptr = gen_rtx_REG (Pmode, 11);
21415 save_off = frame_off - offset;
21417 if (using_static_chain_p)
21419 rtx r0 = gen_rtx_REG (Pmode, 0);
21421 START_USE (0);
21422 gcc_assert (info->first_gp_reg_save > 11);
21424 emit_move_insn (r0, spe_save_area_ptr);
21426 else if (REGNO (frame_reg_rtx) != 11)
21427 START_USE (11);
21429 emit_insn (gen_addsi3 (spe_save_area_ptr,
21430 frame_reg_rtx, GEN_INT (offset)));
21431 if (!using_static_chain_p && REGNO (frame_reg_rtx) == 11)
21432 frame_off = -info->spe_gp_save_offset + ool_adjust;
21435 if ((strategy & SAVE_INLINE_GPRS))
21437 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
21438 if (rs6000_reg_live_or_pic_offset_p (info->first_gp_reg_save + i))
21439 emit_frame_save (spe_save_area_ptr, reg_mode,
21440 info->first_gp_reg_save + i,
21441 (info->spe_gp_save_offset + save_off
21442 + reg_size * i),
21443 sp_off - save_off);
21445 else
21447 insn = rs6000_emit_savres_rtx (info, spe_save_area_ptr,
21448 info->spe_gp_save_offset + save_off,
21449 0, reg_mode,
21450 SAVRES_SAVE | SAVRES_GPR);
21452 rs6000_frame_related (insn, spe_save_area_ptr, sp_off - save_off,
21453 NULL_RTX, NULL_RTX);
21456 /* Move the static chain pointer back. */
21457 if (!spe_regs_addressable)
21459 if (using_static_chain_p)
21461 emit_move_insn (spe_save_area_ptr, gen_rtx_REG (Pmode, 0));
21462 END_USE (0);
21464 else if (REGNO (frame_reg_rtx) != 11)
21465 END_USE (11);
21468 else if (!WORLD_SAVE_P (info) && !(strategy & SAVE_INLINE_GPRS))
21470 bool lr = (strategy & SAVE_NOINLINE_GPRS_SAVES_LR) != 0;
21471 int sel = SAVRES_SAVE | SAVRES_GPR | (lr ? SAVRES_LR : 0);
21472 unsigned ptr_regno = ptr_regno_for_savres (sel);
21473 rtx ptr_reg = frame_reg_rtx;
21474 bool ptr_set_up = REGNO (ptr_reg) == ptr_regno;
21475 int end_save = info->gp_save_offset + info->gp_size;
21476 int ptr_off;
21478 if (!ptr_set_up)
21479 ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
21481 /* Need to adjust r11 (r12) if we saved any FPRs. */
21482 if (end_save + frame_off != 0)
21484 rtx offset = GEN_INT (end_save + frame_off);
21486 if (ptr_set_up)
21487 frame_off = -end_save;
21488 else
21489 NOT_INUSE (ptr_regno);
21490 emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx, offset));
21492 else if (!ptr_set_up)
21494 NOT_INUSE (ptr_regno);
21495 emit_move_insn (ptr_reg, frame_reg_rtx);
21497 ptr_off = -end_save;
21498 insn = rs6000_emit_savres_rtx (info, ptr_reg,
21499 info->gp_save_offset + ptr_off,
21500 info->lr_save_offset + ptr_off,
21501 reg_mode, sel);
21502 rs6000_frame_related (insn, ptr_reg, sp_off - ptr_off,
21503 NULL_RTX, NULL_RTX);
21504 if (lr)
21505 END_USE (0);
21507 else if (!WORLD_SAVE_P (info) && (strategy & SAVRES_MULTIPLE))
21509 rtvec p;
21510 int i;
21511 p = rtvec_alloc (32 - info->first_gp_reg_save);
21512 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
21513 RTVEC_ELT (p, i)
21514 = gen_frame_store (gen_rtx_REG (reg_mode, info->first_gp_reg_save + i),
21515 frame_reg_rtx,
21516 info->gp_save_offset + frame_off + reg_size * i);
21517 insn = emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
21518 rs6000_frame_related (insn, frame_reg_rtx, sp_off - frame_off,
21519 NULL_RTX, NULL_RTX);
21521 else if (!WORLD_SAVE_P (info))
21523 int i;
21524 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
21525 if (rs6000_reg_live_or_pic_offset_p (info->first_gp_reg_save + i))
21526 emit_frame_save (frame_reg_rtx, reg_mode,
21527 info->first_gp_reg_save + i,
21528 info->gp_save_offset + frame_off + reg_size * i,
21529 sp_off - frame_off);
21532 if (crtl->calls_eh_return)
21534 unsigned int i;
21535 rtvec p;
21537 for (i = 0; ; ++i)
21539 unsigned int regno = EH_RETURN_DATA_REGNO (i);
21540 if (regno == INVALID_REGNUM)
21541 break;
21544 p = rtvec_alloc (i);
21546 for (i = 0; ; ++i)
21548 unsigned int regno = EH_RETURN_DATA_REGNO (i);
21549 if (regno == INVALID_REGNUM)
21550 break;
21552 insn
21553 = gen_frame_store (gen_rtx_REG (reg_mode, regno),
21554 sp_reg_rtx,
21555 info->ehrd_offset + sp_off + reg_size * (int) i);
21556 RTVEC_ELT (p, i) = insn;
21557 RTX_FRAME_RELATED_P (insn) = 1;
21560 insn = emit_insn (gen_blockage ());
21561 RTX_FRAME_RELATED_P (insn) = 1;
21562 add_reg_note (insn, REG_FRAME_RELATED_EXPR, gen_rtx_PARALLEL (VOIDmode, p));
21565 /* In AIX ABI we need to make sure r2 is really saved. */
21566 if (TARGET_AIX && crtl->calls_eh_return)
21568 rtx tmp_reg, tmp_reg_si, hi, lo, compare_result, toc_save_done, jump;
21569 rtx save_insn, join_insn, note;
21570 long toc_restore_insn;
21572 tmp_reg = gen_rtx_REG (Pmode, 11);
21573 tmp_reg_si = gen_rtx_REG (SImode, 11);
21574 if (using_static_chain_p)
21576 START_USE (0);
21577 emit_move_insn (gen_rtx_REG (Pmode, 0), tmp_reg);
21579 else
21580 START_USE (11);
21581 emit_move_insn (tmp_reg, gen_rtx_REG (Pmode, LR_REGNO));
21582 /* Peek at instruction to which this function returns. If it's
21583 restoring r2, then we know we've already saved r2. We can't
21584 unconditionally save r2 because the value we have will already
21585 be updated if we arrived at this function via a plt call or
21586 toc adjusting stub. */
21587 emit_move_insn (tmp_reg_si, gen_rtx_MEM (SImode, tmp_reg));
21588 toc_restore_insn = TARGET_32BIT ? 0x80410014 : 0xE8410028;
21589 hi = gen_int_mode (toc_restore_insn & ~0xffff, SImode);
21590 emit_insn (gen_xorsi3 (tmp_reg_si, tmp_reg_si, hi));
21591 compare_result = gen_rtx_REG (CCUNSmode, CR0_REGNO);
21592 validate_condition_mode (EQ, CCUNSmode);
21593 lo = gen_int_mode (toc_restore_insn & 0xffff, SImode);
21594 emit_insn (gen_rtx_SET (VOIDmode, compare_result,
21595 gen_rtx_COMPARE (CCUNSmode, tmp_reg_si, lo)));
21596 toc_save_done = gen_label_rtx ();
21597 jump = gen_rtx_IF_THEN_ELSE (VOIDmode,
21598 gen_rtx_EQ (VOIDmode, compare_result,
21599 const0_rtx),
21600 gen_rtx_LABEL_REF (VOIDmode, toc_save_done),
21601 pc_rtx);
21602 jump = emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, jump));
21603 JUMP_LABEL (jump) = toc_save_done;
21604 LABEL_NUSES (toc_save_done) += 1;
21606 save_insn = emit_frame_save (frame_reg_rtx, reg_mode,
21607 TOC_REGNUM, frame_off + 5 * reg_size,
21608 sp_off - frame_off);
21610 emit_label (toc_save_done);
21612 /* ??? If we leave SAVE_INSN as marked as saving R2, then we'll
21613 have a CFG that has different saves along different paths.
21614 Move the note to a dummy blockage insn, which describes that
21615 R2 is unconditionally saved after the label. */
21616 /* ??? An alternate representation might be a special insn pattern
21617 containing both the branch and the store. That might let the
21618 code that minimizes the number of DW_CFA_advance opcodes better
21619 freedom in placing the annotations. */
21620 note = find_reg_note (save_insn, REG_FRAME_RELATED_EXPR, NULL);
21621 if (note)
21622 remove_note (save_insn, note);
21623 else
21624 note = alloc_reg_note (REG_FRAME_RELATED_EXPR,
21625 copy_rtx (PATTERN (save_insn)), NULL_RTX);
21626 RTX_FRAME_RELATED_P (save_insn) = 0;
21628 join_insn = emit_insn (gen_blockage ());
21629 REG_NOTES (join_insn) = note;
21630 RTX_FRAME_RELATED_P (join_insn) = 1;
21632 if (using_static_chain_p)
21634 emit_move_insn (tmp_reg, gen_rtx_REG (Pmode, 0));
21635 END_USE (0);
21637 else
21638 END_USE (11);
21641 /* Save CR if we use any that must be preserved. */
21642 if (!WORLD_SAVE_P (info) && info->cr_save_p)
21644 rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
21645 GEN_INT (info->cr_save_offset + frame_off));
21646 rtx mem = gen_frame_mem (SImode, addr);
21647 /* See the large comment above about why CR2_REGNO is used. */
21648 rtx magic_eh_cr_reg = gen_rtx_REG (SImode, CR2_REGNO);
21650 /* If we didn't copy cr before, do so now using r0. */
21651 if (cr_save_rtx == NULL_RTX)
21653 rtx set;
21655 START_USE (0);
21656 cr_save_rtx = gen_rtx_REG (SImode, 0);
21657 insn = emit_insn (gen_movesi_from_cr (cr_save_rtx));
21658 RTX_FRAME_RELATED_P (insn) = 1;
21659 set = gen_rtx_SET (VOIDmode, cr_save_rtx, magic_eh_cr_reg);
21660 add_reg_note (insn, REG_FRAME_RELATED_EXPR, set);
21662 insn = emit_move_insn (mem, cr_save_rtx);
21663 END_USE (REGNO (cr_save_rtx));
21665 rs6000_frame_related (insn, frame_reg_rtx, sp_off - frame_off,
21666 NULL_RTX, NULL_RTX);
21669 /* Update stack and set back pointer unless this is V.4,
21670 for which it was done previously. */
21671 if (!WORLD_SAVE_P (info) && info->push_p
21672 && !(DEFAULT_ABI == ABI_V4 || crtl->calls_eh_return))
21674 rtx ptr_reg = NULL;
21675 int ptr_off = 0;
21677 /* If saving altivec regs we need to be able to address all save
21678 locations using a 16-bit offset. */
21679 if ((strategy & SAVE_INLINE_VRS) == 0
21680 || (info->altivec_size != 0
21681 && (info->altivec_save_offset + info->altivec_size - 16
21682 + info->total_size - frame_off) > 32767)
21683 || (info->vrsave_size != 0
21684 && (info->vrsave_save_offset
21685 + info->total_size - frame_off) > 32767))
21687 int sel = SAVRES_SAVE | SAVRES_VR;
21688 unsigned ptr_regno = ptr_regno_for_savres (sel);
21690 if (using_static_chain_p
21691 && ptr_regno == STATIC_CHAIN_REGNUM)
21692 ptr_regno = 12;
21693 if (REGNO (frame_reg_rtx) != ptr_regno)
21694 START_USE (ptr_regno);
21695 ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
21696 frame_reg_rtx = ptr_reg;
21697 ptr_off = info->altivec_save_offset + info->altivec_size;
21698 frame_off = -ptr_off;
21700 else if (REGNO (frame_reg_rtx) == 1)
21701 frame_off = info->total_size;
21702 rs6000_emit_allocate_stack (info->total_size, ptr_reg, ptr_off);
21703 sp_off = info->total_size;
21704 if (frame_reg_rtx != sp_reg_rtx)
21705 rs6000_emit_stack_tie (frame_reg_rtx, false);
21708 /* Set frame pointer, if needed. */
21709 if (frame_pointer_needed)
21711 insn = emit_move_insn (gen_rtx_REG (Pmode, HARD_FRAME_POINTER_REGNUM),
21712 sp_reg_rtx);
21713 RTX_FRAME_RELATED_P (insn) = 1;
21716 /* Save AltiVec registers if needed. Save here because the red zone does
21717 not always include AltiVec registers. */
21718 if (!WORLD_SAVE_P (info) && TARGET_ALTIVEC_ABI
21719 && info->altivec_size != 0 && (strategy & SAVE_INLINE_VRS) == 0)
21721 int end_save = info->altivec_save_offset + info->altivec_size;
21722 int ptr_off;
21723 /* Oddly, the vector save/restore functions point r0 at the end
21724 of the save area, then use r11 or r12 to load offsets for
21725 [reg+reg] addressing. */
21726 rtx ptr_reg = gen_rtx_REG (Pmode, 0);
21727 int scratch_regno = ptr_regno_for_savres (SAVRES_SAVE | SAVRES_VR);
21728 rtx scratch_reg = gen_rtx_REG (Pmode, scratch_regno);
21730 gcc_checking_assert (scratch_regno == 11 || scratch_regno == 12);
21731 NOT_INUSE (0);
21732 if (end_save + frame_off != 0)
21734 rtx offset = GEN_INT (end_save + frame_off);
21736 emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx, offset));
21738 else
21739 emit_move_insn (ptr_reg, frame_reg_rtx);
21741 ptr_off = -end_save;
21742 insn = rs6000_emit_savres_rtx (info, scratch_reg,
21743 info->altivec_save_offset + ptr_off,
21744 0, V4SImode, SAVRES_SAVE | SAVRES_VR);
21745 rs6000_frame_related (insn, scratch_reg, sp_off - ptr_off,
21746 NULL_RTX, NULL_RTX);
21747 if (REGNO (frame_reg_rtx) == REGNO (scratch_reg))
21749 /* The oddity mentioned above clobbered our frame reg. */
21750 emit_move_insn (frame_reg_rtx, ptr_reg);
21751 frame_off = ptr_off;
21754 else if (!WORLD_SAVE_P (info) && TARGET_ALTIVEC_ABI
21755 && info->altivec_size != 0)
21757 int i;
21759 for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
21760 if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
21762 rtx areg, savereg, mem;
21763 int offset;
21765 offset = (info->altivec_save_offset + frame_off
21766 + 16 * (i - info->first_altivec_reg_save));
21768 savereg = gen_rtx_REG (V4SImode, i);
21770 NOT_INUSE (0);
21771 areg = gen_rtx_REG (Pmode, 0);
21772 emit_move_insn (areg, GEN_INT (offset));
21774 /* AltiVec addressing mode is [reg+reg]. */
21775 mem = gen_frame_mem (V4SImode,
21776 gen_rtx_PLUS (Pmode, frame_reg_rtx, areg));
21778 insn = emit_move_insn (mem, savereg);
21780 rs6000_frame_related (insn, frame_reg_rtx, sp_off - frame_off,
21781 areg, GEN_INT (offset));
21785 /* VRSAVE is a bit vector representing which AltiVec registers
21786 are used. The OS uses this to determine which vector
21787 registers to save on a context switch. We need to save
21788 VRSAVE on the stack frame, add whatever AltiVec registers we
21789 used in this function, and do the corresponding magic in the
21790 epilogue. */
21792 if (!WORLD_SAVE_P (info)
21793 && TARGET_ALTIVEC
21794 && TARGET_ALTIVEC_VRSAVE
21795 && info->vrsave_mask != 0)
21797 rtx reg, vrsave;
21798 int offset;
21799 int save_regno;
21801 /* Get VRSAVE onto a GPR. Note that ABI_V4 and ABI_DARWIN might
21802 be using r12 as frame_reg_rtx and r11 as the static chain
21803 pointer for nested functions. */
21804 save_regno = 12;
21805 if (DEFAULT_ABI == ABI_AIX && !using_static_chain_p)
21806 save_regno = 11;
21807 else if (REGNO (frame_reg_rtx) == 12)
21809 save_regno = 11;
21810 if (using_static_chain_p)
21811 save_regno = 0;
21814 NOT_INUSE (save_regno);
21815 reg = gen_rtx_REG (SImode, save_regno);
21816 vrsave = gen_rtx_REG (SImode, VRSAVE_REGNO);
21817 if (TARGET_MACHO)
21818 emit_insn (gen_get_vrsave_internal (reg));
21819 else
21820 emit_insn (gen_rtx_SET (VOIDmode, reg, vrsave));
21822 /* Save VRSAVE. */
21823 offset = info->vrsave_save_offset + frame_off;
21824 insn = emit_insn (gen_frame_store (reg, frame_reg_rtx, offset));
21826 /* Include the registers in the mask. */
21827 emit_insn (gen_iorsi3 (reg, reg, GEN_INT ((int) info->vrsave_mask)));
21829 insn = emit_insn (generate_set_vrsave (reg, info, 0));
21832 /* If we are using RS6000_PIC_OFFSET_TABLE_REGNUM, we need to set it up. */
21833 if (!TARGET_SINGLE_PIC_BASE
21834 && ((TARGET_TOC && TARGET_MINIMAL_TOC && get_pool_size () != 0)
21835 || (DEFAULT_ABI == ABI_V4
21836 && (flag_pic == 1 || (flag_pic && TARGET_SECURE_PLT))
21837 && df_regs_ever_live_p (RS6000_PIC_OFFSET_TABLE_REGNUM))))
21839 /* If emit_load_toc_table will use the link register, we need to save
21840 it. We use R12 for this purpose because emit_load_toc_table
21841 can use register 0. This allows us to use a plain 'blr' to return
21842 from the procedure more often. */
21843 int save_LR_around_toc_setup = (TARGET_ELF
21844 && DEFAULT_ABI != ABI_AIX
21845 && flag_pic
21846 && ! info->lr_save_p
21847 && EDGE_COUNT (EXIT_BLOCK_PTR->preds) > 0);
21848 if (save_LR_around_toc_setup)
21850 rtx lr = gen_rtx_REG (Pmode, LR_REGNO);
21851 rtx tmp = gen_rtx_REG (Pmode, 12);
21853 insn = emit_move_insn (tmp, lr);
21854 RTX_FRAME_RELATED_P (insn) = 1;
21856 rs6000_emit_load_toc_table (TRUE);
21858 insn = emit_move_insn (lr, tmp);
21859 add_reg_note (insn, REG_CFA_RESTORE, lr);
21860 RTX_FRAME_RELATED_P (insn) = 1;
21862 else
21863 rs6000_emit_load_toc_table (TRUE);
21866 #if TARGET_MACHO
21867 if (!TARGET_SINGLE_PIC_BASE
21868 && DEFAULT_ABI == ABI_DARWIN
21869 && flag_pic && crtl->uses_pic_offset_table)
21871 rtx lr = gen_rtx_REG (Pmode, LR_REGNO);
21872 rtx src = gen_rtx_SYMBOL_REF (Pmode, MACHOPIC_FUNCTION_BASE_NAME);
21874 /* Save and restore LR locally around this call (in R0). */
21875 if (!info->lr_save_p)
21876 emit_move_insn (gen_rtx_REG (Pmode, 0), lr);
21878 emit_insn (gen_load_macho_picbase (src));
21880 emit_move_insn (gen_rtx_REG (Pmode,
21881 RS6000_PIC_OFFSET_TABLE_REGNUM),
21882 lr);
21884 if (!info->lr_save_p)
21885 emit_move_insn (lr, gen_rtx_REG (Pmode, 0));
21887 #endif
21889 /* If we need to, save the TOC register after doing the stack setup.
21890 Do not emit eh frame info for this save. The unwinder wants info,
21891 conceptually attached to instructions in this function, about
21892 register values in the caller of this function. This R2 may have
21893 already been changed from the value in the caller.
21894 We don't attempt to write accurate DWARF EH frame info for R2
21895 because code emitted by gcc for a (non-pointer) function call
21896 doesn't save and restore R2. Instead, R2 is managed out-of-line
21897 by a linker generated plt call stub when the function resides in
21898 a shared library. This behaviour is costly to describe in DWARF,
21899 both in terms of the size of DWARF info and the time taken in the
21900 unwinder to interpret it. R2 changes, apart from the
21901 calls_eh_return case earlier in this function, are handled by
21902 linux-unwind.h frob_update_context. */
21903 if (rs6000_save_toc_in_prologue_p ())
21905 rtx reg = gen_rtx_REG (reg_mode, TOC_REGNUM);
21906 emit_insn (gen_frame_store (reg, sp_reg_rtx, 5 * reg_size));
21910 /* Write function prologue. */
21912 static void
21913 rs6000_output_function_prologue (FILE *file,
21914 HOST_WIDE_INT size ATTRIBUTE_UNUSED)
21916 rs6000_stack_t *info = rs6000_stack_info ();
21918 if (TARGET_DEBUG_STACK)
21919 debug_stack_info (info);
21921 /* Write .extern for any function we will call to save and restore
21922 fp values. */
21923 if (info->first_fp_reg_save < 64
21924 && !TARGET_MACHO
21925 && !TARGET_ELF)
21927 char *name;
21928 int regno = info->first_fp_reg_save - 32;
21930 if ((info->savres_strategy & SAVE_INLINE_FPRS) == 0)
21932 bool lr = (info->savres_strategy & SAVE_NOINLINE_FPRS_SAVES_LR) != 0;
21933 int sel = SAVRES_SAVE | SAVRES_FPR | (lr ? SAVRES_LR : 0);
21934 name = rs6000_savres_routine_name (info, regno, sel);
21935 fprintf (file, "\t.extern %s\n", name);
21937 if ((info->savres_strategy & REST_INLINE_FPRS) == 0)
21939 bool lr = (info->savres_strategy
21940 & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR) == 0;
21941 int sel = SAVRES_FPR | (lr ? SAVRES_LR : 0);
21942 name = rs6000_savres_routine_name (info, regno, sel);
21943 fprintf (file, "\t.extern %s\n", name);
21947 rs6000_pic_labelno++;
21950 /* Non-zero if vmx regs are restored before the frame pop, zero if
21951 we restore after the pop when possible. */
21952 #define ALWAYS_RESTORE_ALTIVEC_BEFORE_POP 0
21954 /* Restoring cr is a two step process: loading a reg from the frame
21955 save, then moving the reg to cr. For ABI_V4 we must let the
21956 unwinder know that the stack location is no longer valid at or
21957 before the stack deallocation, but we can't emit a cfa_restore for
21958 cr at the stack deallocation like we do for other registers.
21959 The trouble is that it is possible for the move to cr to be
21960 scheduled after the stack deallocation. So say exactly where cr
21961 is located on each of the two insns. */
21963 static rtx
21964 load_cr_save (int regno, rtx frame_reg_rtx, int offset, bool exit_func)
21966 rtx mem = gen_frame_mem_offset (SImode, frame_reg_rtx, offset);
21967 rtx reg = gen_rtx_REG (SImode, regno);
21968 rtx insn = emit_move_insn (reg, mem);
21970 if (!exit_func && DEFAULT_ABI == ABI_V4)
21972 rtx cr = gen_rtx_REG (SImode, CR2_REGNO);
21973 rtx set = gen_rtx_SET (VOIDmode, reg, cr);
21975 add_reg_note (insn, REG_CFA_REGISTER, set);
21976 RTX_FRAME_RELATED_P (insn) = 1;
21978 return reg;
21981 /* Reload CR from REG. */
21983 static void
21984 restore_saved_cr (rtx reg, int using_mfcr_multiple, bool exit_func)
21986 int count = 0;
21987 int i;
21989 if (using_mfcr_multiple)
21991 for (i = 0; i < 8; i++)
21992 if (save_reg_p (CR0_REGNO + i))
21993 count++;
21994 gcc_assert (count);
21997 if (using_mfcr_multiple && count > 1)
21999 rtvec p;
22000 int ndx;
22002 p = rtvec_alloc (count);
22004 ndx = 0;
22005 for (i = 0; i < 8; i++)
22006 if (save_reg_p (CR0_REGNO + i))
22008 rtvec r = rtvec_alloc (2);
22009 RTVEC_ELT (r, 0) = reg;
22010 RTVEC_ELT (r, 1) = GEN_INT (1 << (7-i));
22011 RTVEC_ELT (p, ndx) =
22012 gen_rtx_SET (VOIDmode, gen_rtx_REG (CCmode, CR0_REGNO + i),
22013 gen_rtx_UNSPEC (CCmode, r, UNSPEC_MOVESI_TO_CR));
22014 ndx++;
22016 emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
22017 gcc_assert (ndx == count);
22019 else
22020 for (i = 0; i < 8; i++)
22021 if (save_reg_p (CR0_REGNO + i))
22022 emit_insn (gen_movsi_to_cr_one (gen_rtx_REG (CCmode, CR0_REGNO + i),
22023 reg));
22025 if (!exit_func && (DEFAULT_ABI == ABI_V4 || flag_shrink_wrap))
22027 rtx insn = get_last_insn ();
22028 rtx cr = gen_rtx_REG (SImode, CR2_REGNO);
22030 add_reg_note (insn, REG_CFA_RESTORE, cr);
22031 RTX_FRAME_RELATED_P (insn) = 1;
22035 /* Like cr, the move to lr instruction can be scheduled after the
22036 stack deallocation, but unlike cr, its stack frame save is still
22037 valid. So we only need to emit the cfa_restore on the correct
22038 instruction. */
22040 static void
22041 load_lr_save (int regno, rtx frame_reg_rtx, int offset)
22043 rtx mem = gen_frame_mem_offset (Pmode, frame_reg_rtx, offset);
22044 rtx reg = gen_rtx_REG (Pmode, regno);
22046 emit_move_insn (reg, mem);
22049 static void
22050 restore_saved_lr (int regno, bool exit_func)
22052 rtx reg = gen_rtx_REG (Pmode, regno);
22053 rtx lr = gen_rtx_REG (Pmode, LR_REGNO);
22054 rtx insn = emit_move_insn (lr, reg);
22056 if (!exit_func && flag_shrink_wrap)
22058 add_reg_note (insn, REG_CFA_RESTORE, lr);
22059 RTX_FRAME_RELATED_P (insn) = 1;
22063 static rtx
22064 add_crlr_cfa_restore (const rs6000_stack_t *info, rtx cfa_restores)
22066 if (info->cr_save_p)
22067 cfa_restores = alloc_reg_note (REG_CFA_RESTORE,
22068 gen_rtx_REG (SImode, CR2_REGNO),
22069 cfa_restores);
22070 if (info->lr_save_p)
22071 cfa_restores = alloc_reg_note (REG_CFA_RESTORE,
22072 gen_rtx_REG (Pmode, LR_REGNO),
22073 cfa_restores);
22074 return cfa_restores;
22077 /* Return true if OFFSET from stack pointer can be clobbered by signals.
22078 V.4 doesn't have any stack cushion, AIX ABIs have 220 or 288 bytes
22079 below stack pointer not cloberred by signals. */
22081 static inline bool
22082 offset_below_red_zone_p (HOST_WIDE_INT offset)
22084 return offset < (DEFAULT_ABI == ABI_V4
22086 : TARGET_32BIT ? -220 : -288);
22089 /* Append CFA_RESTORES to any existing REG_NOTES on the last insn. */
22091 static void
22092 emit_cfa_restores (rtx cfa_restores)
22094 rtx insn = get_last_insn ();
22095 rtx *loc = &REG_NOTES (insn);
22097 while (*loc)
22098 loc = &XEXP (*loc, 1);
22099 *loc = cfa_restores;
22100 RTX_FRAME_RELATED_P (insn) = 1;
22103 /* Emit function epilogue as insns. */
22105 void
22106 rs6000_emit_epilogue (int sibcall)
22108 rs6000_stack_t *info;
22109 int restoring_GPRs_inline;
22110 int restoring_FPRs_inline;
22111 int using_load_multiple;
22112 int using_mtcr_multiple;
22113 int use_backchain_to_restore_sp;
22114 int restore_lr;
22115 int strategy;
22116 HOST_WIDE_INT frame_off = 0;
22117 rtx sp_reg_rtx = gen_rtx_REG (Pmode, 1);
22118 rtx frame_reg_rtx = sp_reg_rtx;
22119 rtx cfa_restores = NULL_RTX;
22120 rtx insn;
22121 rtx cr_save_reg = NULL_RTX;
22122 enum machine_mode reg_mode = Pmode;
22123 int reg_size = TARGET_32BIT ? 4 : 8;
22124 int i;
22125 bool exit_func;
22126 unsigned ptr_regno;
22128 info = rs6000_stack_info ();
22130 if (TARGET_SPE_ABI && info->spe_64bit_regs_used != 0)
22132 reg_mode = V2SImode;
22133 reg_size = 8;
22136 strategy = info->savres_strategy;
22137 using_load_multiple = strategy & SAVRES_MULTIPLE;
22138 restoring_FPRs_inline = sibcall || (strategy & REST_INLINE_FPRS);
22139 restoring_GPRs_inline = sibcall || (strategy & REST_INLINE_GPRS);
22140 using_mtcr_multiple = (rs6000_cpu == PROCESSOR_PPC601
22141 || rs6000_cpu == PROCESSOR_PPC603
22142 || rs6000_cpu == PROCESSOR_PPC750
22143 || optimize_size);
22144 /* Restore via the backchain when we have a large frame, since this
22145 is more efficient than an addis, addi pair. The second condition
22146 here will not trigger at the moment; We don't actually need a
22147 frame pointer for alloca, but the generic parts of the compiler
22148 give us one anyway. */
22149 use_backchain_to_restore_sp = (info->total_size > 32767 - info->lr_save_offset
22150 || (cfun->calls_alloca
22151 && !frame_pointer_needed));
22152 restore_lr = (info->lr_save_p
22153 && (restoring_FPRs_inline
22154 || (strategy & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR))
22155 && (restoring_GPRs_inline
22156 || info->first_fp_reg_save < 64));
22158 if (WORLD_SAVE_P (info))
22160 int i, j;
22161 char rname[30];
22162 const char *alloc_rname;
22163 rtvec p;
22165 /* eh_rest_world_r10 will return to the location saved in the LR
22166 stack slot (which is not likely to be our caller.)
22167 Input: R10 -- stack adjustment. Clobbers R0, R11, R12, R7, R8.
22168 rest_world is similar, except any R10 parameter is ignored.
22169 The exception-handling stuff that was here in 2.95 is no
22170 longer necessary. */
22172 p = rtvec_alloc (9
22174 + 32 - info->first_gp_reg_save
22175 + LAST_ALTIVEC_REGNO + 1 - info->first_altivec_reg_save
22176 + 63 + 1 - info->first_fp_reg_save);
22178 strcpy (rname, ((crtl->calls_eh_return) ?
22179 "*eh_rest_world_r10" : "*rest_world"));
22180 alloc_rname = ggc_strdup (rname);
22182 j = 0;
22183 RTVEC_ELT (p, j++) = ret_rtx;
22184 RTVEC_ELT (p, j++) = gen_rtx_USE (VOIDmode,
22185 gen_rtx_REG (Pmode,
22186 LR_REGNO));
22187 RTVEC_ELT (p, j++)
22188 = gen_rtx_USE (VOIDmode, gen_rtx_SYMBOL_REF (Pmode, alloc_rname));
22189 /* The instruction pattern requires a clobber here;
22190 it is shared with the restVEC helper. */
22191 RTVEC_ELT (p, j++)
22192 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, 11));
22195 /* CR register traditionally saved as CR2. */
22196 rtx reg = gen_rtx_REG (SImode, CR2_REGNO);
22197 RTVEC_ELT (p, j++)
22198 = gen_frame_load (reg, frame_reg_rtx, info->cr_save_offset);
22199 if (flag_shrink_wrap)
22201 cfa_restores = alloc_reg_note (REG_CFA_RESTORE,
22202 gen_rtx_REG (Pmode, LR_REGNO),
22203 cfa_restores);
22204 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
22208 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
22210 rtx reg = gen_rtx_REG (reg_mode, info->first_gp_reg_save + i);
22211 RTVEC_ELT (p, j++)
22212 = gen_frame_load (reg,
22213 frame_reg_rtx, info->gp_save_offset + reg_size * i);
22214 if (flag_shrink_wrap)
22215 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
22217 for (i = 0; info->first_altivec_reg_save + i <= LAST_ALTIVEC_REGNO; i++)
22219 rtx reg = gen_rtx_REG (V4SImode, info->first_altivec_reg_save + i);
22220 RTVEC_ELT (p, j++)
22221 = gen_frame_load (reg,
22222 frame_reg_rtx, info->altivec_save_offset + 16 * i);
22223 if (flag_shrink_wrap)
22224 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
22226 for (i = 0; info->first_fp_reg_save + i <= 63; i++)
22228 rtx reg = gen_rtx_REG ((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT
22229 ? DFmode : SFmode),
22230 info->first_fp_reg_save + i);
22231 RTVEC_ELT (p, j++)
22232 = gen_frame_load (reg, frame_reg_rtx, info->fp_save_offset + 8 * i);
22233 if (flag_shrink_wrap)
22234 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
22236 RTVEC_ELT (p, j++)
22237 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, 0));
22238 RTVEC_ELT (p, j++)
22239 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (SImode, 12));
22240 RTVEC_ELT (p, j++)
22241 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (SImode, 7));
22242 RTVEC_ELT (p, j++)
22243 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (SImode, 8));
22244 RTVEC_ELT (p, j++)
22245 = gen_rtx_USE (VOIDmode, gen_rtx_REG (SImode, 10));
22246 insn = emit_jump_insn (gen_rtx_PARALLEL (VOIDmode, p));
22248 if (flag_shrink_wrap)
22250 REG_NOTES (insn) = cfa_restores;
22251 add_reg_note (insn, REG_CFA_DEF_CFA, sp_reg_rtx);
22252 RTX_FRAME_RELATED_P (insn) = 1;
22254 return;
22257 /* frame_reg_rtx + frame_off points to the top of this stack frame. */
22258 if (info->push_p)
22259 frame_off = info->total_size;
22261 /* Restore AltiVec registers if we must do so before adjusting the
22262 stack. */
22263 if (TARGET_ALTIVEC_ABI
22264 && info->altivec_size != 0
22265 && (ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
22266 || (DEFAULT_ABI != ABI_V4
22267 && offset_below_red_zone_p (info->altivec_save_offset))))
22269 int i;
22270 int scratch_regno = ptr_regno_for_savres (SAVRES_VR);
22272 gcc_checking_assert (scratch_regno == 11 || scratch_regno == 12);
22273 if (use_backchain_to_restore_sp)
22275 int frame_regno = 11;
22277 if ((strategy & REST_INLINE_VRS) == 0)
22279 /* Of r11 and r12, select the one not clobbered by an
22280 out-of-line restore function for the frame register. */
22281 frame_regno = 11 + 12 - scratch_regno;
22283 frame_reg_rtx = gen_rtx_REG (Pmode, frame_regno);
22284 emit_move_insn (frame_reg_rtx,
22285 gen_rtx_MEM (Pmode, sp_reg_rtx));
22286 frame_off = 0;
22288 else if (frame_pointer_needed)
22289 frame_reg_rtx = hard_frame_pointer_rtx;
22291 if ((strategy & REST_INLINE_VRS) == 0)
22293 int end_save = info->altivec_save_offset + info->altivec_size;
22294 int ptr_off;
22295 rtx ptr_reg = gen_rtx_REG (Pmode, 0);
22296 rtx scratch_reg = gen_rtx_REG (Pmode, scratch_regno);
22298 if (end_save + frame_off != 0)
22300 rtx offset = GEN_INT (end_save + frame_off);
22302 emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx, offset));
22304 else
22305 emit_move_insn (ptr_reg, frame_reg_rtx);
22307 ptr_off = -end_save;
22308 insn = rs6000_emit_savres_rtx (info, scratch_reg,
22309 info->altivec_save_offset + ptr_off,
22310 0, V4SImode, SAVRES_VR);
22312 else
22314 for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
22315 if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
22317 rtx addr, areg, mem, reg;
22319 areg = gen_rtx_REG (Pmode, 0);
22320 emit_move_insn
22321 (areg, GEN_INT (info->altivec_save_offset
22322 + frame_off
22323 + 16 * (i - info->first_altivec_reg_save)));
22325 /* AltiVec addressing mode is [reg+reg]. */
22326 addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, areg);
22327 mem = gen_frame_mem (V4SImode, addr);
22329 reg = gen_rtx_REG (V4SImode, i);
22330 emit_move_insn (reg, mem);
22334 for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
22335 if (((strategy & REST_INLINE_VRS) == 0
22336 || (info->vrsave_mask & ALTIVEC_REG_BIT (i)) != 0)
22337 && (flag_shrink_wrap
22338 || (offset_below_red_zone_p
22339 (info->altivec_save_offset
22340 + 16 * (i - info->first_altivec_reg_save)))))
22342 rtx reg = gen_rtx_REG (V4SImode, i);
22343 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
22347 /* Restore VRSAVE if we must do so before adjusting the stack. */
22348 if (TARGET_ALTIVEC
22349 && TARGET_ALTIVEC_VRSAVE
22350 && info->vrsave_mask != 0
22351 && (ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
22352 || (DEFAULT_ABI != ABI_V4
22353 && offset_below_red_zone_p (info->vrsave_save_offset))))
22355 rtx reg;
22357 if (frame_reg_rtx == sp_reg_rtx)
22359 if (use_backchain_to_restore_sp)
22361 frame_reg_rtx = gen_rtx_REG (Pmode, 11);
22362 emit_move_insn (frame_reg_rtx,
22363 gen_rtx_MEM (Pmode, sp_reg_rtx));
22364 frame_off = 0;
22366 else if (frame_pointer_needed)
22367 frame_reg_rtx = hard_frame_pointer_rtx;
22370 reg = gen_rtx_REG (SImode, 12);
22371 emit_insn (gen_frame_load (reg, frame_reg_rtx,
22372 info->vrsave_save_offset + frame_off));
22374 emit_insn (generate_set_vrsave (reg, info, 1));
22377 insn = NULL_RTX;
22378 /* If we have a large stack frame, restore the old stack pointer
22379 using the backchain. */
22380 if (use_backchain_to_restore_sp)
22382 if (frame_reg_rtx == sp_reg_rtx)
22384 /* Under V.4, don't reset the stack pointer until after we're done
22385 loading the saved registers. */
22386 if (DEFAULT_ABI == ABI_V4)
22387 frame_reg_rtx = gen_rtx_REG (Pmode, 11);
22389 insn = emit_move_insn (frame_reg_rtx,
22390 gen_rtx_MEM (Pmode, sp_reg_rtx));
22391 frame_off = 0;
22393 else if (ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
22394 && DEFAULT_ABI == ABI_V4)
22395 /* frame_reg_rtx has been set up by the altivec restore. */
22397 else
22399 insn = emit_move_insn (sp_reg_rtx, frame_reg_rtx);
22400 frame_reg_rtx = sp_reg_rtx;
22403 /* If we have a frame pointer, we can restore the old stack pointer
22404 from it. */
22405 else if (frame_pointer_needed)
22407 frame_reg_rtx = sp_reg_rtx;
22408 if (DEFAULT_ABI == ABI_V4)
22409 frame_reg_rtx = gen_rtx_REG (Pmode, 11);
22410 /* Prevent reordering memory accesses against stack pointer restore. */
22411 else if (cfun->calls_alloca
22412 || offset_below_red_zone_p (-info->total_size))
22413 rs6000_emit_stack_tie (frame_reg_rtx, true);
22415 insn = emit_insn (gen_add3_insn (frame_reg_rtx, hard_frame_pointer_rtx,
22416 GEN_INT (info->total_size)));
22417 frame_off = 0;
22419 else if (info->push_p
22420 && DEFAULT_ABI != ABI_V4
22421 && !crtl->calls_eh_return)
22423 /* Prevent reordering memory accesses against stack pointer restore. */
22424 if (cfun->calls_alloca
22425 || offset_below_red_zone_p (-info->total_size))
22426 rs6000_emit_stack_tie (frame_reg_rtx, false);
22427 insn = emit_insn (gen_add3_insn (sp_reg_rtx, sp_reg_rtx,
22428 GEN_INT (info->total_size)));
22429 frame_off = 0;
22431 if (insn && frame_reg_rtx == sp_reg_rtx)
22433 if (cfa_restores)
22435 REG_NOTES (insn) = cfa_restores;
22436 cfa_restores = NULL_RTX;
22438 add_reg_note (insn, REG_CFA_DEF_CFA, sp_reg_rtx);
22439 RTX_FRAME_RELATED_P (insn) = 1;
22442 /* Restore AltiVec registers if we have not done so already. */
22443 if (!ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
22444 && TARGET_ALTIVEC_ABI
22445 && info->altivec_size != 0
22446 && (DEFAULT_ABI == ABI_V4
22447 || !offset_below_red_zone_p (info->altivec_save_offset)))
22449 int i;
22451 if ((strategy & REST_INLINE_VRS) == 0)
22453 int end_save = info->altivec_save_offset + info->altivec_size;
22454 int ptr_off;
22455 rtx ptr_reg = gen_rtx_REG (Pmode, 0);
22456 int scratch_regno = ptr_regno_for_savres (SAVRES_VR);
22457 rtx scratch_reg = gen_rtx_REG (Pmode, scratch_regno);
22459 if (end_save + frame_off != 0)
22461 rtx offset = GEN_INT (end_save + frame_off);
22463 emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx, offset));
22465 else
22466 emit_move_insn (ptr_reg, frame_reg_rtx);
22468 ptr_off = -end_save;
22469 insn = rs6000_emit_savres_rtx (info, scratch_reg,
22470 info->altivec_save_offset + ptr_off,
22471 0, V4SImode, SAVRES_VR);
22472 if (REGNO (frame_reg_rtx) == REGNO (scratch_reg))
22474 /* Frame reg was clobbered by out-of-line save. Restore it
22475 from ptr_reg, and if we are calling out-of-line gpr or
22476 fpr restore set up the correct pointer and offset. */
22477 unsigned newptr_regno = 1;
22478 if (!restoring_GPRs_inline)
22480 bool lr = info->gp_save_offset + info->gp_size == 0;
22481 int sel = SAVRES_GPR | (lr ? SAVRES_LR : 0);
22482 newptr_regno = ptr_regno_for_savres (sel);
22483 end_save = info->gp_save_offset + info->gp_size;
22485 else if (!restoring_FPRs_inline)
22487 bool lr = !(strategy & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR);
22488 int sel = SAVRES_FPR | (lr ? SAVRES_LR : 0);
22489 newptr_regno = ptr_regno_for_savres (sel);
22490 end_save = info->gp_save_offset + info->gp_size;
22493 if (newptr_regno != 1 && REGNO (frame_reg_rtx) != newptr_regno)
22494 frame_reg_rtx = gen_rtx_REG (Pmode, newptr_regno);
22496 if (end_save + ptr_off != 0)
22498 rtx offset = GEN_INT (end_save + ptr_off);
22500 frame_off = -end_save;
22501 emit_insn (gen_add3_insn (frame_reg_rtx, ptr_reg, offset));
22503 else
22505 frame_off = ptr_off;
22506 emit_move_insn (frame_reg_rtx, ptr_reg);
22510 else
22512 for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
22513 if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
22515 rtx addr, areg, mem, reg;
22517 areg = gen_rtx_REG (Pmode, 0);
22518 emit_move_insn
22519 (areg, GEN_INT (info->altivec_save_offset
22520 + frame_off
22521 + 16 * (i - info->first_altivec_reg_save)));
22523 /* AltiVec addressing mode is [reg+reg]. */
22524 addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, areg);
22525 mem = gen_frame_mem (V4SImode, addr);
22527 reg = gen_rtx_REG (V4SImode, i);
22528 emit_move_insn (reg, mem);
22532 for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
22533 if (((strategy & REST_INLINE_VRS) == 0
22534 || (info->vrsave_mask & ALTIVEC_REG_BIT (i)) != 0)
22535 && (DEFAULT_ABI == ABI_V4 || flag_shrink_wrap))
22537 rtx reg = gen_rtx_REG (V4SImode, i);
22538 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
22542 /* Restore VRSAVE if we have not done so already. */
22543 if (!ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
22544 && TARGET_ALTIVEC
22545 && TARGET_ALTIVEC_VRSAVE
22546 && info->vrsave_mask != 0
22547 && (DEFAULT_ABI == ABI_V4
22548 || !offset_below_red_zone_p (info->vrsave_save_offset)))
22550 rtx reg;
22552 reg = gen_rtx_REG (SImode, 12);
22553 emit_insn (gen_frame_load (reg, frame_reg_rtx,
22554 info->vrsave_save_offset + frame_off));
22556 emit_insn (generate_set_vrsave (reg, info, 1));
22559 /* If we exit by an out-of-line restore function on ABI_V4 then that
22560 function will deallocate the stack, so we don't need to worry
22561 about the unwinder restoring cr from an invalid stack frame
22562 location. */
22563 exit_func = (!restoring_FPRs_inline
22564 || (!restoring_GPRs_inline
22565 && info->first_fp_reg_save == 64));
22567 /* Get the old lr if we saved it. If we are restoring registers
22568 out-of-line, then the out-of-line routines can do this for us. */
22569 if (restore_lr && restoring_GPRs_inline)
22570 load_lr_save (0, frame_reg_rtx, info->lr_save_offset + frame_off);
22572 /* Get the old cr if we saved it. */
22573 if (info->cr_save_p)
22575 unsigned cr_save_regno = 12;
22577 if (!restoring_GPRs_inline)
22579 /* Ensure we don't use the register used by the out-of-line
22580 gpr register restore below. */
22581 bool lr = info->gp_save_offset + info->gp_size == 0;
22582 int sel = SAVRES_GPR | (lr ? SAVRES_LR : 0);
22583 int gpr_ptr_regno = ptr_regno_for_savres (sel);
22585 if (gpr_ptr_regno == 12)
22586 cr_save_regno = 11;
22587 gcc_checking_assert (REGNO (frame_reg_rtx) != cr_save_regno);
22589 else if (REGNO (frame_reg_rtx) == 12)
22590 cr_save_regno = 11;
22592 cr_save_reg = load_cr_save (cr_save_regno, frame_reg_rtx,
22593 info->cr_save_offset + frame_off,
22594 exit_func);
22597 /* Set LR here to try to overlap restores below. */
22598 if (restore_lr && restoring_GPRs_inline)
22599 restore_saved_lr (0, exit_func);
22601 /* Load exception handler data registers, if needed. */
22602 if (crtl->calls_eh_return)
22604 unsigned int i, regno;
22606 if (TARGET_AIX)
22608 rtx reg = gen_rtx_REG (reg_mode, 2);
22609 emit_insn (gen_frame_load (reg, frame_reg_rtx,
22610 frame_off + 5 * reg_size));
22613 for (i = 0; ; ++i)
22615 rtx mem;
22617 regno = EH_RETURN_DATA_REGNO (i);
22618 if (regno == INVALID_REGNUM)
22619 break;
22621 /* Note: possible use of r0 here to address SPE regs. */
22622 mem = gen_frame_mem_offset (reg_mode, frame_reg_rtx,
22623 info->ehrd_offset + frame_off
22624 + reg_size * (int) i);
22626 emit_move_insn (gen_rtx_REG (reg_mode, regno), mem);
22630 /* Restore GPRs. This is done as a PARALLEL if we are using
22631 the load-multiple instructions. */
22632 if (TARGET_SPE_ABI
22633 && info->spe_64bit_regs_used
22634 && info->first_gp_reg_save != 32)
22636 /* Determine whether we can address all of the registers that need
22637 to be saved with an offset from frame_reg_rtx that fits in
22638 the small const field for SPE memory instructions. */
22639 int spe_regs_addressable
22640 = (SPE_CONST_OFFSET_OK (info->spe_gp_save_offset + frame_off
22641 + reg_size * (32 - info->first_gp_reg_save - 1))
22642 && restoring_GPRs_inline);
22644 if (!spe_regs_addressable)
22646 int ool_adjust = 0;
22647 rtx old_frame_reg_rtx = frame_reg_rtx;
22648 /* Make r11 point to the start of the SPE save area. We worried about
22649 not clobbering it when we were saving registers in the prologue.
22650 There's no need to worry here because the static chain is passed
22651 anew to every function. */
22653 if (!restoring_GPRs_inline)
22654 ool_adjust = 8 * (info->first_gp_reg_save - FIRST_SAVED_GP_REGNO);
22655 frame_reg_rtx = gen_rtx_REG (Pmode, 11);
22656 emit_insn (gen_addsi3 (frame_reg_rtx, old_frame_reg_rtx,
22657 GEN_INT (info->spe_gp_save_offset
22658 + frame_off
22659 - ool_adjust)));
22660 /* Keep the invariant that frame_reg_rtx + frame_off points
22661 at the top of the stack frame. */
22662 frame_off = -info->spe_gp_save_offset + ool_adjust;
22665 if (restoring_GPRs_inline)
22667 HOST_WIDE_INT spe_offset = info->spe_gp_save_offset + frame_off;
22669 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
22670 if (rs6000_reg_live_or_pic_offset_p (info->first_gp_reg_save + i))
22672 rtx offset, addr, mem, reg;
22674 /* We're doing all this to ensure that the immediate offset
22675 fits into the immediate field of 'evldd'. */
22676 gcc_assert (SPE_CONST_OFFSET_OK (spe_offset + reg_size * i));
22678 offset = GEN_INT (spe_offset + reg_size * i);
22679 addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, offset);
22680 mem = gen_rtx_MEM (V2SImode, addr);
22681 reg = gen_rtx_REG (reg_mode, info->first_gp_reg_save + i);
22683 emit_move_insn (reg, mem);
22686 else
22687 rs6000_emit_savres_rtx (info, frame_reg_rtx,
22688 info->spe_gp_save_offset + frame_off,
22689 info->lr_save_offset + frame_off,
22690 reg_mode,
22691 SAVRES_GPR | SAVRES_LR);
22693 else if (!restoring_GPRs_inline)
22695 /* We are jumping to an out-of-line function. */
22696 rtx ptr_reg;
22697 int end_save = info->gp_save_offset + info->gp_size;
22698 bool can_use_exit = end_save == 0;
22699 int sel = SAVRES_GPR | (can_use_exit ? SAVRES_LR : 0);
22700 int ptr_off;
22702 /* Emit stack reset code if we need it. */
22703 ptr_regno = ptr_regno_for_savres (sel);
22704 ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
22705 if (can_use_exit)
22706 rs6000_emit_stack_reset (info, frame_reg_rtx, frame_off, ptr_regno);
22707 else if (end_save + frame_off != 0)
22708 emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx,
22709 GEN_INT (end_save + frame_off)));
22710 else if (REGNO (frame_reg_rtx) != ptr_regno)
22711 emit_move_insn (ptr_reg, frame_reg_rtx);
22712 if (REGNO (frame_reg_rtx) == ptr_regno)
22713 frame_off = -end_save;
22715 if (can_use_exit && info->cr_save_p)
22716 restore_saved_cr (cr_save_reg, using_mtcr_multiple, true);
22718 ptr_off = -end_save;
22719 rs6000_emit_savres_rtx (info, ptr_reg,
22720 info->gp_save_offset + ptr_off,
22721 info->lr_save_offset + ptr_off,
22722 reg_mode, sel);
22724 else if (using_load_multiple)
22726 rtvec p;
22727 p = rtvec_alloc (32 - info->first_gp_reg_save);
22728 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
22729 RTVEC_ELT (p, i)
22730 = gen_frame_load (gen_rtx_REG (reg_mode, info->first_gp_reg_save + i),
22731 frame_reg_rtx,
22732 info->gp_save_offset + frame_off + reg_size * i);
22733 emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
22735 else
22737 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
22738 if (rs6000_reg_live_or_pic_offset_p (info->first_gp_reg_save + i))
22739 emit_insn (gen_frame_load
22740 (gen_rtx_REG (reg_mode, info->first_gp_reg_save + i),
22741 frame_reg_rtx,
22742 info->gp_save_offset + frame_off + reg_size * i));
22745 if (DEFAULT_ABI == ABI_V4 || flag_shrink_wrap)
22747 /* If the frame pointer was used then we can't delay emitting
22748 a REG_CFA_DEF_CFA note. This must happen on the insn that
22749 restores the frame pointer, r31. We may have already emitted
22750 a REG_CFA_DEF_CFA note, but that's OK; A duplicate is
22751 discarded by dwarf2cfi.c/dwarf2out.c, and in any case would
22752 be harmless if emitted. */
22753 if (frame_pointer_needed)
22755 insn = get_last_insn ();
22756 add_reg_note (insn, REG_CFA_DEF_CFA,
22757 plus_constant (Pmode, frame_reg_rtx, frame_off));
22758 RTX_FRAME_RELATED_P (insn) = 1;
22761 /* Set up cfa_restores. We always need these when
22762 shrink-wrapping. If not shrink-wrapping then we only need
22763 the cfa_restore when the stack location is no longer valid.
22764 The cfa_restores must be emitted on or before the insn that
22765 invalidates the stack, and of course must not be emitted
22766 before the insn that actually does the restore. The latter
22767 is why it is a bad idea to emit the cfa_restores as a group
22768 on the last instruction here that actually does a restore:
22769 That insn may be reordered with respect to others doing
22770 restores. */
22771 if (flag_shrink_wrap
22772 && !restoring_GPRs_inline
22773 && info->first_fp_reg_save == 64)
22774 cfa_restores = add_crlr_cfa_restore (info, cfa_restores);
22776 for (i = info->first_gp_reg_save; i < 32; i++)
22777 if (!restoring_GPRs_inline
22778 || using_load_multiple
22779 || rs6000_reg_live_or_pic_offset_p (i))
22781 rtx reg = gen_rtx_REG (reg_mode, i);
22783 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
22787 if (!restoring_GPRs_inline
22788 && info->first_fp_reg_save == 64)
22790 /* We are jumping to an out-of-line function. */
22791 if (cfa_restores)
22792 emit_cfa_restores (cfa_restores);
22793 return;
22796 if (restore_lr && !restoring_GPRs_inline)
22798 load_lr_save (0, frame_reg_rtx, info->lr_save_offset + frame_off);
22799 restore_saved_lr (0, exit_func);
22802 /* Restore fpr's if we need to do it without calling a function. */
22803 if (restoring_FPRs_inline)
22804 for (i = 0; i < 64 - info->first_fp_reg_save; i++)
22805 if (save_reg_p (info->first_fp_reg_save + i))
22807 rtx reg = gen_rtx_REG ((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT
22808 ? DFmode : SFmode),
22809 info->first_fp_reg_save + i);
22810 emit_insn (gen_frame_load (reg, frame_reg_rtx,
22811 info->fp_save_offset + frame_off + 8 * i));
22812 if (DEFAULT_ABI == ABI_V4 || flag_shrink_wrap)
22813 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
22816 /* If we saved cr, restore it here. Just those that were used. */
22817 if (info->cr_save_p)
22818 restore_saved_cr (cr_save_reg, using_mtcr_multiple, exit_func);
22820 /* If this is V.4, unwind the stack pointer after all of the loads
22821 have been done, or set up r11 if we are restoring fp out of line. */
22822 ptr_regno = 1;
22823 if (!restoring_FPRs_inline)
22825 bool lr = (strategy & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR) == 0;
22826 int sel = SAVRES_FPR | (lr ? SAVRES_LR : 0);
22827 ptr_regno = ptr_regno_for_savres (sel);
22830 insn = rs6000_emit_stack_reset (info, frame_reg_rtx, frame_off, ptr_regno);
22831 if (REGNO (frame_reg_rtx) == ptr_regno)
22832 frame_off = 0;
22834 if (insn && restoring_FPRs_inline)
22836 if (cfa_restores)
22838 REG_NOTES (insn) = cfa_restores;
22839 cfa_restores = NULL_RTX;
22841 add_reg_note (insn, REG_CFA_DEF_CFA, sp_reg_rtx);
22842 RTX_FRAME_RELATED_P (insn) = 1;
22845 if (crtl->calls_eh_return)
22847 rtx sa = EH_RETURN_STACKADJ_RTX;
22848 emit_insn (gen_add3_insn (sp_reg_rtx, sp_reg_rtx, sa));
22851 if (!sibcall)
22853 rtvec p;
22854 bool lr = (strategy & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR) == 0;
22855 if (! restoring_FPRs_inline)
22857 p = rtvec_alloc (4 + 64 - info->first_fp_reg_save);
22858 RTVEC_ELT (p, 0) = ret_rtx;
22860 else
22862 if (cfa_restores)
22864 /* We can't hang the cfa_restores off a simple return,
22865 since the shrink-wrap code sometimes uses an existing
22866 return. This means there might be a path from
22867 pre-prologue code to this return, and dwarf2cfi code
22868 wants the eh_frame unwinder state to be the same on
22869 all paths to any point. So we need to emit the
22870 cfa_restores before the return. For -m64 we really
22871 don't need epilogue cfa_restores at all, except for
22872 this irritating dwarf2cfi with shrink-wrap
22873 requirement; The stack red-zone means eh_frame info
22874 from the prologue telling the unwinder to restore
22875 from the stack is perfectly good right to the end of
22876 the function. */
22877 emit_insn (gen_blockage ());
22878 emit_cfa_restores (cfa_restores);
22879 cfa_restores = NULL_RTX;
22881 p = rtvec_alloc (2);
22882 RTVEC_ELT (p, 0) = simple_return_rtx;
22885 RTVEC_ELT (p, 1) = ((restoring_FPRs_inline || !lr)
22886 ? gen_rtx_USE (VOIDmode,
22887 gen_rtx_REG (Pmode, LR_REGNO))
22888 : gen_rtx_CLOBBER (VOIDmode,
22889 gen_rtx_REG (Pmode, LR_REGNO)));
22891 /* If we have to restore more than two FP registers, branch to the
22892 restore function. It will return to our caller. */
22893 if (! restoring_FPRs_inline)
22895 int i;
22896 rtx sym;
22898 if (flag_shrink_wrap)
22899 cfa_restores = add_crlr_cfa_restore (info, cfa_restores);
22901 sym = rs6000_savres_routine_sym (info,
22902 SAVRES_FPR | (lr ? SAVRES_LR : 0));
22903 RTVEC_ELT (p, 2) = gen_rtx_USE (VOIDmode, sym);
22904 RTVEC_ELT (p, 3) = gen_rtx_USE (VOIDmode,
22905 gen_rtx_REG (Pmode,
22906 DEFAULT_ABI == ABI_AIX
22907 ? 1 : 11));
22908 for (i = 0; i < 64 - info->first_fp_reg_save; i++)
22910 rtx reg = gen_rtx_REG (DFmode, info->first_fp_reg_save + i);
22912 RTVEC_ELT (p, i + 4)
22913 = gen_frame_load (reg, sp_reg_rtx, info->fp_save_offset + 8 * i);
22914 if (flag_shrink_wrap)
22915 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg,
22916 cfa_restores);
22920 emit_jump_insn (gen_rtx_PARALLEL (VOIDmode, p));
22923 if (cfa_restores)
22925 if (sibcall)
22926 /* Ensure the cfa_restores are hung off an insn that won't
22927 be reordered above other restores. */
22928 emit_insn (gen_blockage ());
22930 emit_cfa_restores (cfa_restores);
22934 /* Write function epilogue. */
22936 static void
22937 rs6000_output_function_epilogue (FILE *file,
22938 HOST_WIDE_INT size ATTRIBUTE_UNUSED)
22940 #if TARGET_MACHO
22941 macho_branch_islands ();
22942 /* Mach-O doesn't support labels at the end of objects, so if
22943 it looks like we might want one, insert a NOP. */
22945 rtx insn = get_last_insn ();
22946 rtx deleted_debug_label = NULL_RTX;
22947 while (insn
22948 && NOTE_P (insn)
22949 && NOTE_KIND (insn) != NOTE_INSN_DELETED_LABEL)
22951 /* Don't insert a nop for NOTE_INSN_DELETED_DEBUG_LABEL
22952 notes only, instead set their CODE_LABEL_NUMBER to -1,
22953 otherwise there would be code generation differences
22954 in between -g and -g0. */
22955 if (NOTE_P (insn) && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL)
22956 deleted_debug_label = insn;
22957 insn = PREV_INSN (insn);
22959 if (insn
22960 && (LABEL_P (insn)
22961 || (NOTE_P (insn)
22962 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL)))
22963 fputs ("\tnop\n", file);
22964 else if (deleted_debug_label)
22965 for (insn = deleted_debug_label; insn; insn = NEXT_INSN (insn))
22966 if (NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL)
22967 CODE_LABEL_NUMBER (insn) = -1;
22969 #endif
22971 /* Output a traceback table here. See /usr/include/sys/debug.h for info
22972 on its format.
22974 We don't output a traceback table if -finhibit-size-directive was
22975 used. The documentation for -finhibit-size-directive reads
22976 ``don't output a @code{.size} assembler directive, or anything
22977 else that would cause trouble if the function is split in the
22978 middle, and the two halves are placed at locations far apart in
22979 memory.'' The traceback table has this property, since it
22980 includes the offset from the start of the function to the
22981 traceback table itself.
22983 System V.4 Powerpc's (and the embedded ABI derived from it) use a
22984 different traceback table. */
22985 if (DEFAULT_ABI == ABI_AIX && ! flag_inhibit_size_directive
22986 && rs6000_traceback != traceback_none && !cfun->is_thunk)
22988 const char *fname = NULL;
22989 const char *language_string = lang_hooks.name;
22990 int fixed_parms = 0, float_parms = 0, parm_info = 0;
22991 int i;
22992 int optional_tbtab;
22993 rs6000_stack_t *info = rs6000_stack_info ();
22995 if (rs6000_traceback == traceback_full)
22996 optional_tbtab = 1;
22997 else if (rs6000_traceback == traceback_part)
22998 optional_tbtab = 0;
22999 else
23000 optional_tbtab = !optimize_size && !TARGET_ELF;
23002 if (optional_tbtab)
23004 fname = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
23005 while (*fname == '.') /* V.4 encodes . in the name */
23006 fname++;
23008 /* Need label immediately before tbtab, so we can compute
23009 its offset from the function start. */
23010 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LT");
23011 ASM_OUTPUT_LABEL (file, fname);
23014 /* The .tbtab pseudo-op can only be used for the first eight
23015 expressions, since it can't handle the possibly variable
23016 length fields that follow. However, if you omit the optional
23017 fields, the assembler outputs zeros for all optional fields
23018 anyways, giving each variable length field is minimum length
23019 (as defined in sys/debug.h). Thus we can not use the .tbtab
23020 pseudo-op at all. */
23022 /* An all-zero word flags the start of the tbtab, for debuggers
23023 that have to find it by searching forward from the entry
23024 point or from the current pc. */
23025 fputs ("\t.long 0\n", file);
23027 /* Tbtab format type. Use format type 0. */
23028 fputs ("\t.byte 0,", file);
23030 /* Language type. Unfortunately, there does not seem to be any
23031 official way to discover the language being compiled, so we
23032 use language_string.
23033 C is 0. Fortran is 1. Pascal is 2. Ada is 3. C++ is 9.
23034 Java is 13. Objective-C is 14. Objective-C++ isn't assigned
23035 a number, so for now use 9. LTO and Go aren't assigned numbers
23036 either, so for now use 0. */
23037 if (! strcmp (language_string, "GNU C")
23038 || ! strcmp (language_string, "GNU GIMPLE")
23039 || ! strcmp (language_string, "GNU Go"))
23040 i = 0;
23041 else if (! strcmp (language_string, "GNU F77")
23042 || ! strcmp (language_string, "GNU Fortran"))
23043 i = 1;
23044 else if (! strcmp (language_string, "GNU Pascal"))
23045 i = 2;
23046 else if (! strcmp (language_string, "GNU Ada"))
23047 i = 3;
23048 else if (! strcmp (language_string, "GNU C++")
23049 || ! strcmp (language_string, "GNU Objective-C++"))
23050 i = 9;
23051 else if (! strcmp (language_string, "GNU Java"))
23052 i = 13;
23053 else if (! strcmp (language_string, "GNU Objective-C"))
23054 i = 14;
23055 else
23056 gcc_unreachable ();
23057 fprintf (file, "%d,", i);
23059 /* 8 single bit fields: global linkage (not set for C extern linkage,
23060 apparently a PL/I convention?), out-of-line epilogue/prologue, offset
23061 from start of procedure stored in tbtab, internal function, function
23062 has controlled storage, function has no toc, function uses fp,
23063 function logs/aborts fp operations. */
23064 /* Assume that fp operations are used if any fp reg must be saved. */
23065 fprintf (file, "%d,",
23066 (optional_tbtab << 5) | ((info->first_fp_reg_save != 64) << 1));
23068 /* 6 bitfields: function is interrupt handler, name present in
23069 proc table, function calls alloca, on condition directives
23070 (controls stack walks, 3 bits), saves condition reg, saves
23071 link reg. */
23072 /* The `function calls alloca' bit seems to be set whenever reg 31 is
23073 set up as a frame pointer, even when there is no alloca call. */
23074 fprintf (file, "%d,",
23075 ((optional_tbtab << 6)
23076 | ((optional_tbtab & frame_pointer_needed) << 5)
23077 | (info->cr_save_p << 1)
23078 | (info->lr_save_p)));
23080 /* 3 bitfields: saves backchain, fixup code, number of fpr saved
23081 (6 bits). */
23082 fprintf (file, "%d,",
23083 (info->push_p << 7) | (64 - info->first_fp_reg_save));
23085 /* 2 bitfields: spare bits (2 bits), number of gpr saved (6 bits). */
23086 fprintf (file, "%d,", (32 - first_reg_to_save ()));
23088 if (optional_tbtab)
23090 /* Compute the parameter info from the function decl argument
23091 list. */
23092 tree decl;
23093 int next_parm_info_bit = 31;
23095 for (decl = DECL_ARGUMENTS (current_function_decl);
23096 decl; decl = DECL_CHAIN (decl))
23098 rtx parameter = DECL_INCOMING_RTL (decl);
23099 enum machine_mode mode = GET_MODE (parameter);
23101 if (GET_CODE (parameter) == REG)
23103 if (SCALAR_FLOAT_MODE_P (mode))
23105 int bits;
23107 float_parms++;
23109 switch (mode)
23111 case SFmode:
23112 case SDmode:
23113 bits = 0x2;
23114 break;
23116 case DFmode:
23117 case DDmode:
23118 case TFmode:
23119 case TDmode:
23120 bits = 0x3;
23121 break;
23123 default:
23124 gcc_unreachable ();
23127 /* If only one bit will fit, don't or in this entry. */
23128 if (next_parm_info_bit > 0)
23129 parm_info |= (bits << (next_parm_info_bit - 1));
23130 next_parm_info_bit -= 2;
23132 else
23134 fixed_parms += ((GET_MODE_SIZE (mode)
23135 + (UNITS_PER_WORD - 1))
23136 / UNITS_PER_WORD);
23137 next_parm_info_bit -= 1;
23143 /* Number of fixed point parameters. */
23144 /* This is actually the number of words of fixed point parameters; thus
23145 an 8 byte struct counts as 2; and thus the maximum value is 8. */
23146 fprintf (file, "%d,", fixed_parms);
23148 /* 2 bitfields: number of floating point parameters (7 bits), parameters
23149 all on stack. */
23150 /* This is actually the number of fp registers that hold parameters;
23151 and thus the maximum value is 13. */
23152 /* Set parameters on stack bit if parameters are not in their original
23153 registers, regardless of whether they are on the stack? Xlc
23154 seems to set the bit when not optimizing. */
23155 fprintf (file, "%d\n", ((float_parms << 1) | (! optimize)));
23157 if (! optional_tbtab)
23158 return;
23160 /* Optional fields follow. Some are variable length. */
23162 /* Parameter types, left adjusted bit fields: 0 fixed, 10 single float,
23163 11 double float. */
23164 /* There is an entry for each parameter in a register, in the order that
23165 they occur in the parameter list. Any intervening arguments on the
23166 stack are ignored. If the list overflows a long (max possible length
23167 34 bits) then completely leave off all elements that don't fit. */
23168 /* Only emit this long if there was at least one parameter. */
23169 if (fixed_parms || float_parms)
23170 fprintf (file, "\t.long %d\n", parm_info);
23172 /* Offset from start of code to tb table. */
23173 fputs ("\t.long ", file);
23174 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LT");
23175 RS6000_OUTPUT_BASENAME (file, fname);
23176 putc ('-', file);
23177 rs6000_output_function_entry (file, fname);
23178 putc ('\n', file);
23180 /* Interrupt handler mask. */
23181 /* Omit this long, since we never set the interrupt handler bit
23182 above. */
23184 /* Number of CTL (controlled storage) anchors. */
23185 /* Omit this long, since the has_ctl bit is never set above. */
23187 /* Displacement into stack of each CTL anchor. */
23188 /* Omit this list of longs, because there are no CTL anchors. */
23190 /* Length of function name. */
23191 if (*fname == '*')
23192 ++fname;
23193 fprintf (file, "\t.short %d\n", (int) strlen (fname));
23195 /* Function name. */
23196 assemble_string (fname, strlen (fname));
23198 /* Register for alloca automatic storage; this is always reg 31.
23199 Only emit this if the alloca bit was set above. */
23200 if (frame_pointer_needed)
23201 fputs ("\t.byte 31\n", file);
23203 fputs ("\t.align 2\n", file);
23207 /* A C compound statement that outputs the assembler code for a thunk
23208 function, used to implement C++ virtual function calls with
23209 multiple inheritance. The thunk acts as a wrapper around a virtual
23210 function, adjusting the implicit object parameter before handing
23211 control off to the real function.
23213 First, emit code to add the integer DELTA to the location that
23214 contains the incoming first argument. Assume that this argument
23215 contains a pointer, and is the one used to pass the `this' pointer
23216 in C++. This is the incoming argument *before* the function
23217 prologue, e.g. `%o0' on a sparc. The addition must preserve the
23218 values of all other incoming arguments.
23220 After the addition, emit code to jump to FUNCTION, which is a
23221 `FUNCTION_DECL'. This is a direct pure jump, not a call, and does
23222 not touch the return address. Hence returning from FUNCTION will
23223 return to whoever called the current `thunk'.
23225 The effect must be as if FUNCTION had been called directly with the
23226 adjusted first argument. This macro is responsible for emitting
23227 all of the code for a thunk function; output_function_prologue()
23228 and output_function_epilogue() are not invoked.
23230 The THUNK_FNDECL is redundant. (DELTA and FUNCTION have already
23231 been extracted from it.) It might possibly be useful on some
23232 targets, but probably not.
23234 If you do not define this macro, the target-independent code in the
23235 C++ frontend will generate a less efficient heavyweight thunk that
23236 calls FUNCTION instead of jumping to it. The generic approach does
23237 not support varargs. */
23239 static void
23240 rs6000_output_mi_thunk (FILE *file, tree thunk_fndecl ATTRIBUTE_UNUSED,
23241 HOST_WIDE_INT delta, HOST_WIDE_INT vcall_offset,
23242 tree function)
23244 rtx this_rtx, insn, funexp;
23246 reload_completed = 1;
23247 epilogue_completed = 1;
23249 /* Mark the end of the (empty) prologue. */
23250 emit_note (NOTE_INSN_PROLOGUE_END);
23252 /* Find the "this" pointer. If the function returns a structure,
23253 the structure return pointer is in r3. */
23254 if (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function))
23255 this_rtx = gen_rtx_REG (Pmode, 4);
23256 else
23257 this_rtx = gen_rtx_REG (Pmode, 3);
23259 /* Apply the constant offset, if required. */
23260 if (delta)
23261 emit_insn (gen_add3_insn (this_rtx, this_rtx, GEN_INT (delta)));
23263 /* Apply the offset from the vtable, if required. */
23264 if (vcall_offset)
23266 rtx vcall_offset_rtx = GEN_INT (vcall_offset);
23267 rtx tmp = gen_rtx_REG (Pmode, 12);
23269 emit_move_insn (tmp, gen_rtx_MEM (Pmode, this_rtx));
23270 if (((unsigned HOST_WIDE_INT) vcall_offset) + 0x8000 >= 0x10000)
23272 emit_insn (gen_add3_insn (tmp, tmp, vcall_offset_rtx));
23273 emit_move_insn (tmp, gen_rtx_MEM (Pmode, tmp));
23275 else
23277 rtx loc = gen_rtx_PLUS (Pmode, tmp, vcall_offset_rtx);
23279 emit_move_insn (tmp, gen_rtx_MEM (Pmode, loc));
23281 emit_insn (gen_add3_insn (this_rtx, this_rtx, tmp));
23284 /* Generate a tail call to the target function. */
23285 if (!TREE_USED (function))
23287 assemble_external (function);
23288 TREE_USED (function) = 1;
23290 funexp = XEXP (DECL_RTL (function), 0);
23291 funexp = gen_rtx_MEM (FUNCTION_MODE, funexp);
23293 #if TARGET_MACHO
23294 if (MACHOPIC_INDIRECT)
23295 funexp = machopic_indirect_call_target (funexp);
23296 #endif
23298 /* gen_sibcall expects reload to convert scratch pseudo to LR so we must
23299 generate sibcall RTL explicitly. */
23300 insn = emit_call_insn (
23301 gen_rtx_PARALLEL (VOIDmode,
23302 gen_rtvec (4,
23303 gen_rtx_CALL (VOIDmode,
23304 funexp, const0_rtx),
23305 gen_rtx_USE (VOIDmode, const0_rtx),
23306 gen_rtx_USE (VOIDmode,
23307 gen_rtx_REG (SImode,
23308 LR_REGNO)),
23309 simple_return_rtx)));
23310 SIBLING_CALL_P (insn) = 1;
23311 emit_barrier ();
23313 /* Run just enough of rest_of_compilation to get the insns emitted.
23314 There's not really enough bulk here to make other passes such as
23315 instruction scheduling worth while. Note that use_thunk calls
23316 assemble_start_function and assemble_end_function. */
23317 insn = get_insns ();
23318 shorten_branches (insn);
23319 final_start_function (insn, file, 1);
23320 final (insn, file, 1);
23321 final_end_function ();
23323 reload_completed = 0;
23324 epilogue_completed = 0;
23327 /* A quick summary of the various types of 'constant-pool tables'
23328 under PowerPC:
23330 Target Flags Name One table per
23331 AIX (none) AIX TOC object file
23332 AIX -mfull-toc AIX TOC object file
23333 AIX -mminimal-toc AIX minimal TOC translation unit
23334 SVR4/EABI (none) SVR4 SDATA object file
23335 SVR4/EABI -fpic SVR4 pic object file
23336 SVR4/EABI -fPIC SVR4 PIC translation unit
23337 SVR4/EABI -mrelocatable EABI TOC function
23338 SVR4/EABI -maix AIX TOC object file
23339 SVR4/EABI -maix -mminimal-toc
23340 AIX minimal TOC translation unit
23342 Name Reg. Set by entries contains:
23343 made by addrs? fp? sum?
23345 AIX TOC 2 crt0 as Y option option
23346 AIX minimal TOC 30 prolog gcc Y Y option
23347 SVR4 SDATA 13 crt0 gcc N Y N
23348 SVR4 pic 30 prolog ld Y not yet N
23349 SVR4 PIC 30 prolog gcc Y option option
23350 EABI TOC 30 prolog gcc Y option option
23354 /* Hash functions for the hash table. */
23356 static unsigned
23357 rs6000_hash_constant (rtx k)
23359 enum rtx_code code = GET_CODE (k);
23360 enum machine_mode mode = GET_MODE (k);
23361 unsigned result = (code << 3) ^ mode;
23362 const char *format;
23363 int flen, fidx;
23365 format = GET_RTX_FORMAT (code);
23366 flen = strlen (format);
23367 fidx = 0;
23369 switch (code)
23371 case LABEL_REF:
23372 return result * 1231 + (unsigned) INSN_UID (XEXP (k, 0));
23374 case CONST_DOUBLE:
23375 if (mode != VOIDmode)
23376 return real_hash (CONST_DOUBLE_REAL_VALUE (k)) * result;
23377 flen = 2;
23378 break;
23380 case CODE_LABEL:
23381 fidx = 3;
23382 break;
23384 default:
23385 break;
23388 for (; fidx < flen; fidx++)
23389 switch (format[fidx])
23391 case 's':
23393 unsigned i, len;
23394 const char *str = XSTR (k, fidx);
23395 len = strlen (str);
23396 result = result * 613 + len;
23397 for (i = 0; i < len; i++)
23398 result = result * 613 + (unsigned) str[i];
23399 break;
23401 case 'u':
23402 case 'e':
23403 result = result * 1231 + rs6000_hash_constant (XEXP (k, fidx));
23404 break;
23405 case 'i':
23406 case 'n':
23407 result = result * 613 + (unsigned) XINT (k, fidx);
23408 break;
23409 case 'w':
23410 if (sizeof (unsigned) >= sizeof (HOST_WIDE_INT))
23411 result = result * 613 + (unsigned) XWINT (k, fidx);
23412 else
23414 size_t i;
23415 for (i = 0; i < sizeof (HOST_WIDE_INT) / sizeof (unsigned); i++)
23416 result = result * 613 + (unsigned) (XWINT (k, fidx)
23417 >> CHAR_BIT * i);
23419 break;
23420 case '0':
23421 break;
23422 default:
23423 gcc_unreachable ();
23426 return result;
23429 static unsigned
23430 toc_hash_function (const void *hash_entry)
23432 const struct toc_hash_struct *thc =
23433 (const struct toc_hash_struct *) hash_entry;
23434 return rs6000_hash_constant (thc->key) ^ thc->key_mode;
23437 /* Compare H1 and H2 for equivalence. */
23439 static int
23440 toc_hash_eq (const void *h1, const void *h2)
23442 rtx r1 = ((const struct toc_hash_struct *) h1)->key;
23443 rtx r2 = ((const struct toc_hash_struct *) h2)->key;
23445 if (((const struct toc_hash_struct *) h1)->key_mode
23446 != ((const struct toc_hash_struct *) h2)->key_mode)
23447 return 0;
23449 return rtx_equal_p (r1, r2);
23452 /* These are the names given by the C++ front-end to vtables, and
23453 vtable-like objects. Ideally, this logic should not be here;
23454 instead, there should be some programmatic way of inquiring as
23455 to whether or not an object is a vtable. */
23457 #define VTABLE_NAME_P(NAME) \
23458 (strncmp ("_vt.", name, strlen ("_vt.")) == 0 \
23459 || strncmp ("_ZTV", name, strlen ("_ZTV")) == 0 \
23460 || strncmp ("_ZTT", name, strlen ("_ZTT")) == 0 \
23461 || strncmp ("_ZTI", name, strlen ("_ZTI")) == 0 \
23462 || strncmp ("_ZTC", name, strlen ("_ZTC")) == 0)
23464 #ifdef NO_DOLLAR_IN_LABEL
23465 /* Return a GGC-allocated character string translating dollar signs in
23466 input NAME to underscores. Used by XCOFF ASM_OUTPUT_LABELREF. */
23468 const char *
23469 rs6000_xcoff_strip_dollar (const char *name)
23471 char *strip, *p;
23472 const char *q;
23473 size_t len;
23475 q = (const char *) strchr (name, '$');
23477 if (q == 0 || q == name)
23478 return name;
23480 len = strlen (name);
23481 strip = XALLOCAVEC (char, len + 1);
23482 strcpy (strip, name);
23483 p = strip + (q - name);
23484 while (p)
23486 *p = '_';
23487 p = strchr (p + 1, '$');
23490 return ggc_alloc_string (strip, len);
23492 #endif
23494 void
23495 rs6000_output_symbol_ref (FILE *file, rtx x)
23497 /* Currently C++ toc references to vtables can be emitted before it
23498 is decided whether the vtable is public or private. If this is
23499 the case, then the linker will eventually complain that there is
23500 a reference to an unknown section. Thus, for vtables only,
23501 we emit the TOC reference to reference the symbol and not the
23502 section. */
23503 const char *name = XSTR (x, 0);
23505 if (VTABLE_NAME_P (name))
23507 RS6000_OUTPUT_BASENAME (file, name);
23509 else
23510 assemble_name (file, name);
23513 /* Output a TOC entry. We derive the entry name from what is being
23514 written. */
23516 void
23517 output_toc (FILE *file, rtx x, int labelno, enum machine_mode mode)
23519 char buf[256];
23520 const char *name = buf;
23521 rtx base = x;
23522 HOST_WIDE_INT offset = 0;
23524 gcc_assert (!TARGET_NO_TOC);
23526 /* When the linker won't eliminate them, don't output duplicate
23527 TOC entries (this happens on AIX if there is any kind of TOC,
23528 and on SVR4 under -fPIC or -mrelocatable). Don't do this for
23529 CODE_LABELs. */
23530 if (TARGET_TOC && GET_CODE (x) != LABEL_REF)
23532 struct toc_hash_struct *h;
23533 void * * found;
23535 /* Create toc_hash_table. This can't be done at TARGET_OPTION_OVERRIDE
23536 time because GGC is not initialized at that point. */
23537 if (toc_hash_table == NULL)
23538 toc_hash_table = htab_create_ggc (1021, toc_hash_function,
23539 toc_hash_eq, NULL);
23541 h = ggc_alloc_toc_hash_struct ();
23542 h->key = x;
23543 h->key_mode = mode;
23544 h->labelno = labelno;
23546 found = htab_find_slot (toc_hash_table, h, INSERT);
23547 if (*found == NULL)
23548 *found = h;
23549 else /* This is indeed a duplicate.
23550 Set this label equal to that label. */
23552 fputs ("\t.set ", file);
23553 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LC");
23554 fprintf (file, "%d,", labelno);
23555 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LC");
23556 fprintf (file, "%d\n", ((*(const struct toc_hash_struct **)
23557 found)->labelno));
23559 #ifdef HAVE_AS_TLS
23560 if (TARGET_XCOFF && GET_CODE (x) == SYMBOL_REF
23561 && (SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_GLOBAL_DYNAMIC
23562 || SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_DYNAMIC))
23564 fputs ("\t.set ", file);
23565 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LCM");
23566 fprintf (file, "%d,", labelno);
23567 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LCM");
23568 fprintf (file, "%d\n", ((*(const struct toc_hash_struct **)
23569 found)->labelno));
23571 #endif
23572 return;
23576 /* If we're going to put a double constant in the TOC, make sure it's
23577 aligned properly when strict alignment is on. */
23578 if (GET_CODE (x) == CONST_DOUBLE
23579 && STRICT_ALIGNMENT
23580 && GET_MODE_BITSIZE (mode) >= 64
23581 && ! (TARGET_NO_FP_IN_TOC && ! TARGET_MINIMAL_TOC)) {
23582 ASM_OUTPUT_ALIGN (file, 3);
23585 (*targetm.asm_out.internal_label) (file, "LC", labelno);
23587 /* Handle FP constants specially. Note that if we have a minimal
23588 TOC, things we put here aren't actually in the TOC, so we can allow
23589 FP constants. */
23590 if (GET_CODE (x) == CONST_DOUBLE &&
23591 (GET_MODE (x) == TFmode || GET_MODE (x) == TDmode))
23593 REAL_VALUE_TYPE rv;
23594 long k[4];
23596 REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
23597 if (DECIMAL_FLOAT_MODE_P (GET_MODE (x)))
23598 REAL_VALUE_TO_TARGET_DECIMAL128 (rv, k);
23599 else
23600 REAL_VALUE_TO_TARGET_LONG_DOUBLE (rv, k);
23602 if (TARGET_64BIT)
23604 if (TARGET_ELF || TARGET_MINIMAL_TOC)
23605 fputs (DOUBLE_INT_ASM_OP, file);
23606 else
23607 fprintf (file, "\t.tc FT_%lx_%lx_%lx_%lx[TC],",
23608 k[0] & 0xffffffff, k[1] & 0xffffffff,
23609 k[2] & 0xffffffff, k[3] & 0xffffffff);
23610 fprintf (file, "0x%lx%08lx,0x%lx%08lx\n",
23611 k[WORDS_BIG_ENDIAN ? 0 : 1] & 0xffffffff,
23612 k[WORDS_BIG_ENDIAN ? 1 : 0] & 0xffffffff,
23613 k[WORDS_BIG_ENDIAN ? 2 : 3] & 0xffffffff,
23614 k[WORDS_BIG_ENDIAN ? 3 : 2] & 0xffffffff);
23615 return;
23617 else
23619 if (TARGET_ELF || TARGET_MINIMAL_TOC)
23620 fputs ("\t.long ", file);
23621 else
23622 fprintf (file, "\t.tc FT_%lx_%lx_%lx_%lx[TC],",
23623 k[0] & 0xffffffff, k[1] & 0xffffffff,
23624 k[2] & 0xffffffff, k[3] & 0xffffffff);
23625 fprintf (file, "0x%lx,0x%lx,0x%lx,0x%lx\n",
23626 k[0] & 0xffffffff, k[1] & 0xffffffff,
23627 k[2] & 0xffffffff, k[3] & 0xffffffff);
23628 return;
23631 else if (GET_CODE (x) == CONST_DOUBLE &&
23632 (GET_MODE (x) == DFmode || GET_MODE (x) == DDmode))
23634 REAL_VALUE_TYPE rv;
23635 long k[2];
23637 REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
23639 if (DECIMAL_FLOAT_MODE_P (GET_MODE (x)))
23640 REAL_VALUE_TO_TARGET_DECIMAL64 (rv, k);
23641 else
23642 REAL_VALUE_TO_TARGET_DOUBLE (rv, k);
23644 if (TARGET_64BIT)
23646 if (TARGET_ELF || TARGET_MINIMAL_TOC)
23647 fputs (DOUBLE_INT_ASM_OP, file);
23648 else
23649 fprintf (file, "\t.tc FD_%lx_%lx[TC],",
23650 k[0] & 0xffffffff, k[1] & 0xffffffff);
23651 fprintf (file, "0x%lx%08lx\n",
23652 k[WORDS_BIG_ENDIAN ? 0 : 1] & 0xffffffff,
23653 k[WORDS_BIG_ENDIAN ? 1 : 0] & 0xffffffff);
23654 return;
23656 else
23658 if (TARGET_ELF || TARGET_MINIMAL_TOC)
23659 fputs ("\t.long ", file);
23660 else
23661 fprintf (file, "\t.tc FD_%lx_%lx[TC],",
23662 k[0] & 0xffffffff, k[1] & 0xffffffff);
23663 fprintf (file, "0x%lx,0x%lx\n",
23664 k[0] & 0xffffffff, k[1] & 0xffffffff);
23665 return;
23668 else if (GET_CODE (x) == CONST_DOUBLE &&
23669 (GET_MODE (x) == SFmode || GET_MODE (x) == SDmode))
23671 REAL_VALUE_TYPE rv;
23672 long l;
23674 REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
23675 if (DECIMAL_FLOAT_MODE_P (GET_MODE (x)))
23676 REAL_VALUE_TO_TARGET_DECIMAL32 (rv, l);
23677 else
23678 REAL_VALUE_TO_TARGET_SINGLE (rv, l);
23680 if (TARGET_64BIT)
23682 if (TARGET_ELF || TARGET_MINIMAL_TOC)
23683 fputs (DOUBLE_INT_ASM_OP, file);
23684 else
23685 fprintf (file, "\t.tc FS_%lx[TC],", l & 0xffffffff);
23686 if (WORDS_BIG_ENDIAN)
23687 fprintf (file, "0x%lx00000000\n", l & 0xffffffff);
23688 else
23689 fprintf (file, "0x%lx\n", l & 0xffffffff);
23690 return;
23692 else
23694 if (TARGET_ELF || TARGET_MINIMAL_TOC)
23695 fputs ("\t.long ", file);
23696 else
23697 fprintf (file, "\t.tc FS_%lx[TC],", l & 0xffffffff);
23698 fprintf (file, "0x%lx\n", l & 0xffffffff);
23699 return;
23702 else if (GET_MODE (x) == VOIDmode && GET_CODE (x) == CONST_INT)
23704 unsigned HOST_WIDE_INT low;
23705 HOST_WIDE_INT high;
23707 low = INTVAL (x) & 0xffffffff;
23708 high = (HOST_WIDE_INT) INTVAL (x) >> 32;
23710 /* TOC entries are always Pmode-sized, so when big-endian
23711 smaller integer constants in the TOC need to be padded.
23712 (This is still a win over putting the constants in
23713 a separate constant pool, because then we'd have
23714 to have both a TOC entry _and_ the actual constant.)
23716 For a 32-bit target, CONST_INT values are loaded and shifted
23717 entirely within `low' and can be stored in one TOC entry. */
23719 /* It would be easy to make this work, but it doesn't now. */
23720 gcc_assert (!TARGET_64BIT || POINTER_SIZE >= GET_MODE_BITSIZE (mode));
23722 if (WORDS_BIG_ENDIAN && POINTER_SIZE > GET_MODE_BITSIZE (mode))
23724 low |= high << 32;
23725 low <<= POINTER_SIZE - GET_MODE_BITSIZE (mode);
23726 high = (HOST_WIDE_INT) low >> 32;
23727 low &= 0xffffffff;
23730 if (TARGET_64BIT)
23732 if (TARGET_ELF || TARGET_MINIMAL_TOC)
23733 fputs (DOUBLE_INT_ASM_OP, file);
23734 else
23735 fprintf (file, "\t.tc ID_%lx_%lx[TC],",
23736 (long) high & 0xffffffff, (long) low & 0xffffffff);
23737 fprintf (file, "0x%lx%08lx\n",
23738 (long) high & 0xffffffff, (long) low & 0xffffffff);
23739 return;
23741 else
23743 if (POINTER_SIZE < GET_MODE_BITSIZE (mode))
23745 if (TARGET_ELF || TARGET_MINIMAL_TOC)
23746 fputs ("\t.long ", file);
23747 else
23748 fprintf (file, "\t.tc ID_%lx_%lx[TC],",
23749 (long) high & 0xffffffff, (long) low & 0xffffffff);
23750 fprintf (file, "0x%lx,0x%lx\n",
23751 (long) high & 0xffffffff, (long) low & 0xffffffff);
23753 else
23755 if (TARGET_ELF || TARGET_MINIMAL_TOC)
23756 fputs ("\t.long ", file);
23757 else
23758 fprintf (file, "\t.tc IS_%lx[TC],", (long) low & 0xffffffff);
23759 fprintf (file, "0x%lx\n", (long) low & 0xffffffff);
23761 return;
23765 if (GET_CODE (x) == CONST)
23767 gcc_assert (GET_CODE (XEXP (x, 0)) == PLUS
23768 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT);
23770 base = XEXP (XEXP (x, 0), 0);
23771 offset = INTVAL (XEXP (XEXP (x, 0), 1));
23774 switch (GET_CODE (base))
23776 case SYMBOL_REF:
23777 name = XSTR (base, 0);
23778 break;
23780 case LABEL_REF:
23781 ASM_GENERATE_INTERNAL_LABEL (buf, "L",
23782 CODE_LABEL_NUMBER (XEXP (base, 0)));
23783 break;
23785 case CODE_LABEL:
23786 ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (base));
23787 break;
23789 default:
23790 gcc_unreachable ();
23793 if (TARGET_ELF || TARGET_MINIMAL_TOC)
23794 fputs (TARGET_32BIT ? "\t.long " : DOUBLE_INT_ASM_OP, file);
23795 else
23797 fputs ("\t.tc ", file);
23798 RS6000_OUTPUT_BASENAME (file, name);
23800 if (offset < 0)
23801 fprintf (file, ".N" HOST_WIDE_INT_PRINT_UNSIGNED, - offset);
23802 else if (offset)
23803 fprintf (file, ".P" HOST_WIDE_INT_PRINT_UNSIGNED, offset);
23805 /* Mark large TOC symbols on AIX with [TE] so they are mapped
23806 after other TOC symbols, reducing overflow of small TOC access
23807 to [TC] symbols. */
23808 fputs (TARGET_XCOFF && TARGET_CMODEL != CMODEL_SMALL
23809 ? "[TE]," : "[TC],", file);
23812 /* Currently C++ toc references to vtables can be emitted before it
23813 is decided whether the vtable is public or private. If this is
23814 the case, then the linker will eventually complain that there is
23815 a TOC reference to an unknown section. Thus, for vtables only,
23816 we emit the TOC reference to reference the symbol and not the
23817 section. */
23818 if (VTABLE_NAME_P (name))
23820 RS6000_OUTPUT_BASENAME (file, name);
23821 if (offset < 0)
23822 fprintf (file, HOST_WIDE_INT_PRINT_DEC, offset);
23823 else if (offset > 0)
23824 fprintf (file, "+" HOST_WIDE_INT_PRINT_DEC, offset);
23826 else
23827 output_addr_const (file, x);
23829 #if HAVE_AS_TLS
23830 if (TARGET_XCOFF && GET_CODE (base) == SYMBOL_REF
23831 && SYMBOL_REF_TLS_MODEL (base) != 0)
23833 if (SYMBOL_REF_TLS_MODEL (base) == TLS_MODEL_LOCAL_EXEC)
23834 fputs ("@le", file);
23835 else if (SYMBOL_REF_TLS_MODEL (base) == TLS_MODEL_INITIAL_EXEC)
23836 fputs ("@ie", file);
23837 /* Use global-dynamic for local-dynamic. */
23838 else if (SYMBOL_REF_TLS_MODEL (base) == TLS_MODEL_GLOBAL_DYNAMIC
23839 || SYMBOL_REF_TLS_MODEL (base) == TLS_MODEL_LOCAL_DYNAMIC)
23841 putc ('\n', file);
23842 (*targetm.asm_out.internal_label) (file, "LCM", labelno);
23843 fputs ("\t.tc .", file);
23844 RS6000_OUTPUT_BASENAME (file, name);
23845 fputs ("[TC],", file);
23846 output_addr_const (file, x);
23847 fputs ("@m", file);
23850 #endif
23852 putc ('\n', file);
23855 /* Output an assembler pseudo-op to write an ASCII string of N characters
23856 starting at P to FILE.
23858 On the RS/6000, we have to do this using the .byte operation and
23859 write out special characters outside the quoted string.
23860 Also, the assembler is broken; very long strings are truncated,
23861 so we must artificially break them up early. */
23863 void
23864 output_ascii (FILE *file, const char *p, int n)
23866 char c;
23867 int i, count_string;
23868 const char *for_string = "\t.byte \"";
23869 const char *for_decimal = "\t.byte ";
23870 const char *to_close = NULL;
23872 count_string = 0;
23873 for (i = 0; i < n; i++)
23875 c = *p++;
23876 if (c >= ' ' && c < 0177)
23878 if (for_string)
23879 fputs (for_string, file);
23880 putc (c, file);
23882 /* Write two quotes to get one. */
23883 if (c == '"')
23885 putc (c, file);
23886 ++count_string;
23889 for_string = NULL;
23890 for_decimal = "\"\n\t.byte ";
23891 to_close = "\"\n";
23892 ++count_string;
23894 if (count_string >= 512)
23896 fputs (to_close, file);
23898 for_string = "\t.byte \"";
23899 for_decimal = "\t.byte ";
23900 to_close = NULL;
23901 count_string = 0;
23904 else
23906 if (for_decimal)
23907 fputs (for_decimal, file);
23908 fprintf (file, "%d", c);
23910 for_string = "\n\t.byte \"";
23911 for_decimal = ", ";
23912 to_close = "\n";
23913 count_string = 0;
23917 /* Now close the string if we have written one. Then end the line. */
23918 if (to_close)
23919 fputs (to_close, file);
23922 /* Generate a unique section name for FILENAME for a section type
23923 represented by SECTION_DESC. Output goes into BUF.
23925 SECTION_DESC can be any string, as long as it is different for each
23926 possible section type.
23928 We name the section in the same manner as xlc. The name begins with an
23929 underscore followed by the filename (after stripping any leading directory
23930 names) with the last period replaced by the string SECTION_DESC. If
23931 FILENAME does not contain a period, SECTION_DESC is appended to the end of
23932 the name. */
23934 void
23935 rs6000_gen_section_name (char **buf, const char *filename,
23936 const char *section_desc)
23938 const char *q, *after_last_slash, *last_period = 0;
23939 char *p;
23940 int len;
23942 after_last_slash = filename;
23943 for (q = filename; *q; q++)
23945 if (*q == '/')
23946 after_last_slash = q + 1;
23947 else if (*q == '.')
23948 last_period = q;
23951 len = strlen (after_last_slash) + strlen (section_desc) + 2;
23952 *buf = (char *) xmalloc (len);
23954 p = *buf;
23955 *p++ = '_';
23957 for (q = after_last_slash; *q; q++)
23959 if (q == last_period)
23961 strcpy (p, section_desc);
23962 p += strlen (section_desc);
23963 break;
23966 else if (ISALNUM (*q))
23967 *p++ = *q;
23970 if (last_period == 0)
23971 strcpy (p, section_desc);
23972 else
23973 *p = '\0';
23976 /* Emit profile function. */
23978 void
23979 output_profile_hook (int labelno ATTRIBUTE_UNUSED)
23981 /* Non-standard profiling for kernels, which just saves LR then calls
23982 _mcount without worrying about arg saves. The idea is to change
23983 the function prologue as little as possible as it isn't easy to
23984 account for arg save/restore code added just for _mcount. */
23985 if (TARGET_PROFILE_KERNEL)
23986 return;
23988 if (DEFAULT_ABI == ABI_AIX)
23990 #ifndef NO_PROFILE_COUNTERS
23991 # define NO_PROFILE_COUNTERS 0
23992 #endif
23993 if (NO_PROFILE_COUNTERS)
23994 emit_library_call (init_one_libfunc (RS6000_MCOUNT),
23995 LCT_NORMAL, VOIDmode, 0);
23996 else
23998 char buf[30];
23999 const char *label_name;
24000 rtx fun;
24002 ASM_GENERATE_INTERNAL_LABEL (buf, "LP", labelno);
24003 label_name = ggc_strdup ((*targetm.strip_name_encoding) (buf));
24004 fun = gen_rtx_SYMBOL_REF (Pmode, label_name);
24006 emit_library_call (init_one_libfunc (RS6000_MCOUNT),
24007 LCT_NORMAL, VOIDmode, 1, fun, Pmode);
24010 else if (DEFAULT_ABI == ABI_DARWIN)
24012 const char *mcount_name = RS6000_MCOUNT;
24013 int caller_addr_regno = LR_REGNO;
24015 /* Be conservative and always set this, at least for now. */
24016 crtl->uses_pic_offset_table = 1;
24018 #if TARGET_MACHO
24019 /* For PIC code, set up a stub and collect the caller's address
24020 from r0, which is where the prologue puts it. */
24021 if (MACHOPIC_INDIRECT
24022 && crtl->uses_pic_offset_table)
24023 caller_addr_regno = 0;
24024 #endif
24025 emit_library_call (gen_rtx_SYMBOL_REF (Pmode, mcount_name),
24026 LCT_NORMAL, VOIDmode, 1,
24027 gen_rtx_REG (Pmode, caller_addr_regno), Pmode);
24031 /* Write function profiler code. */
24033 void
24034 output_function_profiler (FILE *file, int labelno)
24036 char buf[100];
24038 switch (DEFAULT_ABI)
24040 default:
24041 gcc_unreachable ();
24043 case ABI_V4:
24044 if (!TARGET_32BIT)
24046 warning (0, "no profiling of 64-bit code for this ABI");
24047 return;
24049 ASM_GENERATE_INTERNAL_LABEL (buf, "LP", labelno);
24050 fprintf (file, "\tmflr %s\n", reg_names[0]);
24051 if (NO_PROFILE_COUNTERS)
24053 asm_fprintf (file, "\tstw %s,4(%s)\n",
24054 reg_names[0], reg_names[1]);
24056 else if (TARGET_SECURE_PLT && flag_pic)
24058 if (TARGET_LINK_STACK)
24060 char name[32];
24061 get_ppc476_thunk_name (name);
24062 asm_fprintf (file, "\tbl %s\n", name);
24064 else
24065 asm_fprintf (file, "\tbcl 20,31,1f\n1:\n");
24066 asm_fprintf (file, "\tstw %s,4(%s)\n",
24067 reg_names[0], reg_names[1]);
24068 asm_fprintf (file, "\tmflr %s\n", reg_names[12]);
24069 asm_fprintf (file, "\taddis %s,%s,",
24070 reg_names[12], reg_names[12]);
24071 assemble_name (file, buf);
24072 asm_fprintf (file, "-1b@ha\n\tla %s,", reg_names[0]);
24073 assemble_name (file, buf);
24074 asm_fprintf (file, "-1b@l(%s)\n", reg_names[12]);
24076 else if (flag_pic == 1)
24078 fputs ("\tbl _GLOBAL_OFFSET_TABLE_@local-4\n", file);
24079 asm_fprintf (file, "\tstw %s,4(%s)\n",
24080 reg_names[0], reg_names[1]);
24081 asm_fprintf (file, "\tmflr %s\n", reg_names[12]);
24082 asm_fprintf (file, "\tlwz %s,", reg_names[0]);
24083 assemble_name (file, buf);
24084 asm_fprintf (file, "@got(%s)\n", reg_names[12]);
24086 else if (flag_pic > 1)
24088 asm_fprintf (file, "\tstw %s,4(%s)\n",
24089 reg_names[0], reg_names[1]);
24090 /* Now, we need to get the address of the label. */
24091 if (TARGET_LINK_STACK)
24093 char name[32];
24094 get_ppc476_thunk_name (name);
24095 asm_fprintf (file, "\tbl %s\n\tb 1f\n\t.long ", name);
24096 assemble_name (file, buf);
24097 fputs ("-.\n1:", file);
24098 asm_fprintf (file, "\tmflr %s\n", reg_names[11]);
24099 asm_fprintf (file, "\taddi %s,%s,4\n",
24100 reg_names[11], reg_names[11]);
24102 else
24104 fputs ("\tbcl 20,31,1f\n\t.long ", file);
24105 assemble_name (file, buf);
24106 fputs ("-.\n1:", file);
24107 asm_fprintf (file, "\tmflr %s\n", reg_names[11]);
24109 asm_fprintf (file, "\tlwz %s,0(%s)\n",
24110 reg_names[0], reg_names[11]);
24111 asm_fprintf (file, "\tadd %s,%s,%s\n",
24112 reg_names[0], reg_names[0], reg_names[11]);
24114 else
24116 asm_fprintf (file, "\tlis %s,", reg_names[12]);
24117 assemble_name (file, buf);
24118 fputs ("@ha\n", file);
24119 asm_fprintf (file, "\tstw %s,4(%s)\n",
24120 reg_names[0], reg_names[1]);
24121 asm_fprintf (file, "\tla %s,", reg_names[0]);
24122 assemble_name (file, buf);
24123 asm_fprintf (file, "@l(%s)\n", reg_names[12]);
24126 /* ABI_V4 saves the static chain reg with ASM_OUTPUT_REG_PUSH. */
24127 fprintf (file, "\tbl %s%s\n",
24128 RS6000_MCOUNT, flag_pic ? "@plt" : "");
24129 break;
24131 case ABI_AIX:
24132 case ABI_DARWIN:
24133 if (!TARGET_PROFILE_KERNEL)
24135 /* Don't do anything, done in output_profile_hook (). */
24137 else
24139 gcc_assert (!TARGET_32BIT);
24141 asm_fprintf (file, "\tmflr %s\n", reg_names[0]);
24142 asm_fprintf (file, "\tstd %s,16(%s)\n", reg_names[0], reg_names[1]);
24144 if (cfun->static_chain_decl != NULL)
24146 asm_fprintf (file, "\tstd %s,24(%s)\n",
24147 reg_names[STATIC_CHAIN_REGNUM], reg_names[1]);
24148 fprintf (file, "\tbl %s\n", RS6000_MCOUNT);
24149 asm_fprintf (file, "\tld %s,24(%s)\n",
24150 reg_names[STATIC_CHAIN_REGNUM], reg_names[1]);
24152 else
24153 fprintf (file, "\tbl %s\n", RS6000_MCOUNT);
24155 break;
24161 /* The following variable value is the last issued insn. */
24163 static rtx last_scheduled_insn;
24165 /* The following variable helps to balance issuing of load and
24166 store instructions */
24168 static int load_store_pendulum;
24170 /* Power4 load update and store update instructions are cracked into a
24171 load or store and an integer insn which are executed in the same cycle.
24172 Branches have their own dispatch slot which does not count against the
24173 GCC issue rate, but it changes the program flow so there are no other
24174 instructions to issue in this cycle. */
24176 static int
24177 rs6000_variable_issue_1 (rtx insn, int more)
24179 last_scheduled_insn = insn;
24180 if (GET_CODE (PATTERN (insn)) == USE
24181 || GET_CODE (PATTERN (insn)) == CLOBBER)
24183 cached_can_issue_more = more;
24184 return cached_can_issue_more;
24187 if (insn_terminates_group_p (insn, current_group))
24189 cached_can_issue_more = 0;
24190 return cached_can_issue_more;
24193 /* If no reservation, but reach here */
24194 if (recog_memoized (insn) < 0)
24195 return more;
24197 if (rs6000_sched_groups)
24199 if (is_microcoded_insn (insn))
24200 cached_can_issue_more = 0;
24201 else if (is_cracked_insn (insn))
24202 cached_can_issue_more = more > 2 ? more - 2 : 0;
24203 else
24204 cached_can_issue_more = more - 1;
24206 return cached_can_issue_more;
24209 if (rs6000_cpu_attr == CPU_CELL && is_nonpipeline_insn (insn))
24210 return 0;
24212 cached_can_issue_more = more - 1;
24213 return cached_can_issue_more;
24216 static int
24217 rs6000_variable_issue (FILE *stream, int verbose, rtx insn, int more)
24219 int r = rs6000_variable_issue_1 (insn, more);
24220 if (verbose)
24221 fprintf (stream, "// rs6000_variable_issue (more = %d) = %d\n", more, r);
24222 return r;
24225 /* Adjust the cost of a scheduling dependency. Return the new cost of
24226 a dependency LINK or INSN on DEP_INSN. COST is the current cost. */
24228 static int
24229 rs6000_adjust_cost (rtx insn, rtx link, rtx dep_insn, int cost)
24231 enum attr_type attr_type;
24233 if (! recog_memoized (insn))
24234 return 0;
24236 switch (REG_NOTE_KIND (link))
24238 case REG_DEP_TRUE:
24240 /* Data dependency; DEP_INSN writes a register that INSN reads
24241 some cycles later. */
24243 /* Separate a load from a narrower, dependent store. */
24244 if (rs6000_sched_groups
24245 && GET_CODE (PATTERN (insn)) == SET
24246 && GET_CODE (PATTERN (dep_insn)) == SET
24247 && GET_CODE (XEXP (PATTERN (insn), 1)) == MEM
24248 && GET_CODE (XEXP (PATTERN (dep_insn), 0)) == MEM
24249 && (GET_MODE_SIZE (GET_MODE (XEXP (PATTERN (insn), 1)))
24250 > GET_MODE_SIZE (GET_MODE (XEXP (PATTERN (dep_insn), 0)))))
24251 return cost + 14;
24253 attr_type = get_attr_type (insn);
24255 switch (attr_type)
24257 case TYPE_JMPREG:
24258 /* Tell the first scheduling pass about the latency between
24259 a mtctr and bctr (and mtlr and br/blr). The first
24260 scheduling pass will not know about this latency since
24261 the mtctr instruction, which has the latency associated
24262 to it, will be generated by reload. */
24263 return 4;
24264 case TYPE_BRANCH:
24265 /* Leave some extra cycles between a compare and its
24266 dependent branch, to inhibit expensive mispredicts. */
24267 if ((rs6000_cpu_attr == CPU_PPC603
24268 || rs6000_cpu_attr == CPU_PPC604
24269 || rs6000_cpu_attr == CPU_PPC604E
24270 || rs6000_cpu_attr == CPU_PPC620
24271 || rs6000_cpu_attr == CPU_PPC630
24272 || rs6000_cpu_attr == CPU_PPC750
24273 || rs6000_cpu_attr == CPU_PPC7400
24274 || rs6000_cpu_attr == CPU_PPC7450
24275 || rs6000_cpu_attr == CPU_PPCE5500
24276 || rs6000_cpu_attr == CPU_PPCE6500
24277 || rs6000_cpu_attr == CPU_POWER4
24278 || rs6000_cpu_attr == CPU_POWER5
24279 || rs6000_cpu_attr == CPU_POWER7
24280 || rs6000_cpu_attr == CPU_POWER8
24281 || rs6000_cpu_attr == CPU_CELL)
24282 && recog_memoized (dep_insn)
24283 && (INSN_CODE (dep_insn) >= 0))
24285 switch (get_attr_type (dep_insn))
24287 case TYPE_CMP:
24288 case TYPE_COMPARE:
24289 case TYPE_DELAYED_COMPARE:
24290 case TYPE_IMUL_COMPARE:
24291 case TYPE_LMUL_COMPARE:
24292 case TYPE_FPCOMPARE:
24293 case TYPE_CR_LOGICAL:
24294 case TYPE_DELAYED_CR:
24295 return cost + 2;
24296 default:
24297 break;
24299 break;
24301 case TYPE_STORE:
24302 case TYPE_STORE_U:
24303 case TYPE_STORE_UX:
24304 case TYPE_FPSTORE:
24305 case TYPE_FPSTORE_U:
24306 case TYPE_FPSTORE_UX:
24307 if ((rs6000_cpu == PROCESSOR_POWER6)
24308 && recog_memoized (dep_insn)
24309 && (INSN_CODE (dep_insn) >= 0))
24312 if (GET_CODE (PATTERN (insn)) != SET)
24313 /* If this happens, we have to extend this to schedule
24314 optimally. Return default for now. */
24315 return cost;
24317 /* Adjust the cost for the case where the value written
24318 by a fixed point operation is used as the address
24319 gen value on a store. */
24320 switch (get_attr_type (dep_insn))
24322 case TYPE_LOAD:
24323 case TYPE_LOAD_U:
24324 case TYPE_LOAD_UX:
24325 case TYPE_CNTLZ:
24327 if (! store_data_bypass_p (dep_insn, insn))
24328 return 4;
24329 break;
24331 case TYPE_LOAD_EXT:
24332 case TYPE_LOAD_EXT_U:
24333 case TYPE_LOAD_EXT_UX:
24334 case TYPE_VAR_SHIFT_ROTATE:
24335 case TYPE_VAR_DELAYED_COMPARE:
24337 if (! store_data_bypass_p (dep_insn, insn))
24338 return 6;
24339 break;
24341 case TYPE_INTEGER:
24342 case TYPE_COMPARE:
24343 case TYPE_FAST_COMPARE:
24344 case TYPE_EXTS:
24345 case TYPE_SHIFT:
24346 case TYPE_INSERT_WORD:
24347 case TYPE_INSERT_DWORD:
24348 case TYPE_FPLOAD_U:
24349 case TYPE_FPLOAD_UX:
24350 case TYPE_STORE_U:
24351 case TYPE_STORE_UX:
24352 case TYPE_FPSTORE_U:
24353 case TYPE_FPSTORE_UX:
24355 if (! store_data_bypass_p (dep_insn, insn))
24356 return 3;
24357 break;
24359 case TYPE_IMUL:
24360 case TYPE_IMUL2:
24361 case TYPE_IMUL3:
24362 case TYPE_LMUL:
24363 case TYPE_IMUL_COMPARE:
24364 case TYPE_LMUL_COMPARE:
24366 if (! store_data_bypass_p (dep_insn, insn))
24367 return 17;
24368 break;
24370 case TYPE_IDIV:
24372 if (! store_data_bypass_p (dep_insn, insn))
24373 return 45;
24374 break;
24376 case TYPE_LDIV:
24378 if (! store_data_bypass_p (dep_insn, insn))
24379 return 57;
24380 break;
24382 default:
24383 break;
24386 break;
24388 case TYPE_LOAD:
24389 case TYPE_LOAD_U:
24390 case TYPE_LOAD_UX:
24391 case TYPE_LOAD_EXT:
24392 case TYPE_LOAD_EXT_U:
24393 case TYPE_LOAD_EXT_UX:
24394 if ((rs6000_cpu == PROCESSOR_POWER6)
24395 && recog_memoized (dep_insn)
24396 && (INSN_CODE (dep_insn) >= 0))
24399 /* Adjust the cost for the case where the value written
24400 by a fixed point instruction is used within the address
24401 gen portion of a subsequent load(u)(x) */
24402 switch (get_attr_type (dep_insn))
24404 case TYPE_LOAD:
24405 case TYPE_LOAD_U:
24406 case TYPE_LOAD_UX:
24407 case TYPE_CNTLZ:
24409 if (set_to_load_agen (dep_insn, insn))
24410 return 4;
24411 break;
24413 case TYPE_LOAD_EXT:
24414 case TYPE_LOAD_EXT_U:
24415 case TYPE_LOAD_EXT_UX:
24416 case TYPE_VAR_SHIFT_ROTATE:
24417 case TYPE_VAR_DELAYED_COMPARE:
24419 if (set_to_load_agen (dep_insn, insn))
24420 return 6;
24421 break;
24423 case TYPE_INTEGER:
24424 case TYPE_COMPARE:
24425 case TYPE_FAST_COMPARE:
24426 case TYPE_EXTS:
24427 case TYPE_SHIFT:
24428 case TYPE_INSERT_WORD:
24429 case TYPE_INSERT_DWORD:
24430 case TYPE_FPLOAD_U:
24431 case TYPE_FPLOAD_UX:
24432 case TYPE_STORE_U:
24433 case TYPE_STORE_UX:
24434 case TYPE_FPSTORE_U:
24435 case TYPE_FPSTORE_UX:
24437 if (set_to_load_agen (dep_insn, insn))
24438 return 3;
24439 break;
24441 case TYPE_IMUL:
24442 case TYPE_IMUL2:
24443 case TYPE_IMUL3:
24444 case TYPE_LMUL:
24445 case TYPE_IMUL_COMPARE:
24446 case TYPE_LMUL_COMPARE:
24448 if (set_to_load_agen (dep_insn, insn))
24449 return 17;
24450 break;
24452 case TYPE_IDIV:
24454 if (set_to_load_agen (dep_insn, insn))
24455 return 45;
24456 break;
24458 case TYPE_LDIV:
24460 if (set_to_load_agen (dep_insn, insn))
24461 return 57;
24462 break;
24464 default:
24465 break;
24468 break;
24470 case TYPE_FPLOAD:
24471 if ((rs6000_cpu == PROCESSOR_POWER6)
24472 && recog_memoized (dep_insn)
24473 && (INSN_CODE (dep_insn) >= 0)
24474 && (get_attr_type (dep_insn) == TYPE_MFFGPR))
24475 return 2;
24477 default:
24478 break;
24481 /* Fall out to return default cost. */
24483 break;
24485 case REG_DEP_OUTPUT:
24486 /* Output dependency; DEP_INSN writes a register that INSN writes some
24487 cycles later. */
24488 if ((rs6000_cpu == PROCESSOR_POWER6)
24489 && recog_memoized (dep_insn)
24490 && (INSN_CODE (dep_insn) >= 0))
24492 attr_type = get_attr_type (insn);
24494 switch (attr_type)
24496 case TYPE_FP:
24497 if (get_attr_type (dep_insn) == TYPE_FP)
24498 return 1;
24499 break;
24500 case TYPE_FPLOAD:
24501 if (get_attr_type (dep_insn) == TYPE_MFFGPR)
24502 return 2;
24503 break;
24504 default:
24505 break;
24508 case REG_DEP_ANTI:
24509 /* Anti dependency; DEP_INSN reads a register that INSN writes some
24510 cycles later. */
24511 return 0;
24513 default:
24514 gcc_unreachable ();
24517 return cost;
24520 /* Debug version of rs6000_adjust_cost. */
24522 static int
24523 rs6000_debug_adjust_cost (rtx insn, rtx link, rtx dep_insn, int cost)
24525 int ret = rs6000_adjust_cost (insn, link, dep_insn, cost);
24527 if (ret != cost)
24529 const char *dep;
24531 switch (REG_NOTE_KIND (link))
24533 default: dep = "unknown depencency"; break;
24534 case REG_DEP_TRUE: dep = "data dependency"; break;
24535 case REG_DEP_OUTPUT: dep = "output dependency"; break;
24536 case REG_DEP_ANTI: dep = "anti depencency"; break;
24539 fprintf (stderr,
24540 "\nrs6000_adjust_cost, final cost = %d, orig cost = %d, "
24541 "%s, insn:\n", ret, cost, dep);
24543 debug_rtx (insn);
24546 return ret;
24549 /* The function returns a true if INSN is microcoded.
24550 Return false otherwise. */
24552 static bool
24553 is_microcoded_insn (rtx insn)
24555 if (!insn || !NONDEBUG_INSN_P (insn)
24556 || GET_CODE (PATTERN (insn)) == USE
24557 || GET_CODE (PATTERN (insn)) == CLOBBER)
24558 return false;
24560 if (rs6000_cpu_attr == CPU_CELL)
24561 return get_attr_cell_micro (insn) == CELL_MICRO_ALWAYS;
24563 if (rs6000_sched_groups
24564 && (rs6000_cpu == PROCESSOR_POWER4 || rs6000_cpu == PROCESSOR_POWER5))
24566 enum attr_type type = get_attr_type (insn);
24567 if (type == TYPE_LOAD_EXT_U
24568 || type == TYPE_LOAD_EXT_UX
24569 || type == TYPE_LOAD_UX
24570 || type == TYPE_STORE_UX
24571 || type == TYPE_MFCR)
24572 return true;
24575 return false;
24578 /* The function returns true if INSN is cracked into 2 instructions
24579 by the processor (and therefore occupies 2 issue slots). */
24581 static bool
24582 is_cracked_insn (rtx insn)
24584 if (!insn || !NONDEBUG_INSN_P (insn)
24585 || GET_CODE (PATTERN (insn)) == USE
24586 || GET_CODE (PATTERN (insn)) == CLOBBER)
24587 return false;
24589 if (rs6000_sched_groups
24590 && (rs6000_cpu == PROCESSOR_POWER4 || rs6000_cpu == PROCESSOR_POWER5))
24592 enum attr_type type = get_attr_type (insn);
24593 if (type == TYPE_LOAD_U || type == TYPE_STORE_U
24594 || type == TYPE_FPLOAD_U || type == TYPE_FPSTORE_U
24595 || type == TYPE_FPLOAD_UX || type == TYPE_FPSTORE_UX
24596 || type == TYPE_LOAD_EXT || type == TYPE_DELAYED_CR
24597 || type == TYPE_COMPARE || type == TYPE_DELAYED_COMPARE
24598 || type == TYPE_IMUL_COMPARE || type == TYPE_LMUL_COMPARE
24599 || type == TYPE_IDIV || type == TYPE_LDIV
24600 || type == TYPE_INSERT_WORD)
24601 return true;
24604 return false;
24607 /* The function returns true if INSN can be issued only from
24608 the branch slot. */
24610 static bool
24611 is_branch_slot_insn (rtx insn)
24613 if (!insn || !NONDEBUG_INSN_P (insn)
24614 || GET_CODE (PATTERN (insn)) == USE
24615 || GET_CODE (PATTERN (insn)) == CLOBBER)
24616 return false;
24618 if (rs6000_sched_groups)
24620 enum attr_type type = get_attr_type (insn);
24621 if (type == TYPE_BRANCH || type == TYPE_JMPREG)
24622 return true;
24623 return false;
24626 return false;
24629 /* The function returns true if out_inst sets a value that is
24630 used in the address generation computation of in_insn */
24631 static bool
24632 set_to_load_agen (rtx out_insn, rtx in_insn)
24634 rtx out_set, in_set;
24636 /* For performance reasons, only handle the simple case where
24637 both loads are a single_set. */
24638 out_set = single_set (out_insn);
24639 if (out_set)
24641 in_set = single_set (in_insn);
24642 if (in_set)
24643 return reg_mentioned_p (SET_DEST (out_set), SET_SRC (in_set));
24646 return false;
24649 /* Try to determine base/offset/size parts of the given MEM.
24650 Return true if successful, false if all the values couldn't
24651 be determined.
24653 This function only looks for REG or REG+CONST address forms.
24654 REG+REG address form will return false. */
24656 static bool
24657 get_memref_parts (rtx mem, rtx *base, HOST_WIDE_INT *offset,
24658 HOST_WIDE_INT *size)
24660 rtx addr_rtx;
24661 if MEM_SIZE_KNOWN_P (mem)
24662 *size = MEM_SIZE (mem);
24663 else
24664 return false;
24666 if (GET_CODE (XEXP (mem, 0)) == PRE_MODIFY)
24667 addr_rtx = XEXP (XEXP (mem, 0), 1);
24668 else
24669 addr_rtx = (XEXP (mem, 0));
24671 if (GET_CODE (addr_rtx) == REG)
24673 *base = addr_rtx;
24674 *offset = 0;
24676 else if (GET_CODE (addr_rtx) == PLUS
24677 && CONST_INT_P (XEXP (addr_rtx, 1)))
24679 *base = XEXP (addr_rtx, 0);
24680 *offset = INTVAL (XEXP (addr_rtx, 1));
24682 else
24683 return false;
24685 return true;
24688 /* The function returns true if the target storage location of
24689 mem1 is adjacent to the target storage location of mem2 */
24690 /* Return 1 if memory locations are adjacent. */
24692 static bool
24693 adjacent_mem_locations (rtx mem1, rtx mem2)
24695 rtx reg1, reg2;
24696 HOST_WIDE_INT off1, size1, off2, size2;
24698 if (get_memref_parts (mem1, &reg1, &off1, &size1)
24699 && get_memref_parts (mem2, &reg2, &off2, &size2))
24700 return ((REGNO (reg1) == REGNO (reg2))
24701 && ((off1 + size1 == off2)
24702 || (off2 + size2 == off1)));
24704 return false;
24707 /* This function returns true if it can be determined that the two MEM
24708 locations overlap by at least 1 byte based on base reg/offset/size. */
24710 static bool
24711 mem_locations_overlap (rtx mem1, rtx mem2)
24713 rtx reg1, reg2;
24714 HOST_WIDE_INT off1, size1, off2, size2;
24716 if (get_memref_parts (mem1, &reg1, &off1, &size1)
24717 && get_memref_parts (mem2, &reg2, &off2, &size2))
24718 return ((REGNO (reg1) == REGNO (reg2))
24719 && (((off1 <= off2) && (off1 + size1 > off2))
24720 || ((off2 <= off1) && (off2 + size2 > off1))));
24722 return false;
24725 /* A C statement (sans semicolon) to update the integer scheduling
24726 priority INSN_PRIORITY (INSN). Increase the priority to execute the
24727 INSN earlier, reduce the priority to execute INSN later. Do not
24728 define this macro if you do not need to adjust the scheduling
24729 priorities of insns. */
24731 static int
24732 rs6000_adjust_priority (rtx insn ATTRIBUTE_UNUSED, int priority)
24734 rtx load_mem, str_mem;
24735 /* On machines (like the 750) which have asymmetric integer units,
24736 where one integer unit can do multiply and divides and the other
24737 can't, reduce the priority of multiply/divide so it is scheduled
24738 before other integer operations. */
24740 #if 0
24741 if (! INSN_P (insn))
24742 return priority;
24744 if (GET_CODE (PATTERN (insn)) == USE)
24745 return priority;
24747 switch (rs6000_cpu_attr) {
24748 case CPU_PPC750:
24749 switch (get_attr_type (insn))
24751 default:
24752 break;
24754 case TYPE_IMUL:
24755 case TYPE_IDIV:
24756 fprintf (stderr, "priority was %#x (%d) before adjustment\n",
24757 priority, priority);
24758 if (priority >= 0 && priority < 0x01000000)
24759 priority >>= 3;
24760 break;
24763 #endif
24765 if (insn_must_be_first_in_group (insn)
24766 && reload_completed
24767 && current_sched_info->sched_max_insns_priority
24768 && rs6000_sched_restricted_insns_priority)
24771 /* Prioritize insns that can be dispatched only in the first
24772 dispatch slot. */
24773 if (rs6000_sched_restricted_insns_priority == 1)
24774 /* Attach highest priority to insn. This means that in
24775 haifa-sched.c:ready_sort(), dispatch-slot restriction considerations
24776 precede 'priority' (critical path) considerations. */
24777 return current_sched_info->sched_max_insns_priority;
24778 else if (rs6000_sched_restricted_insns_priority == 2)
24779 /* Increase priority of insn by a minimal amount. This means that in
24780 haifa-sched.c:ready_sort(), only 'priority' (critical path)
24781 considerations precede dispatch-slot restriction considerations. */
24782 return (priority + 1);
24785 if (rs6000_cpu == PROCESSOR_POWER6
24786 && ((load_store_pendulum == -2 && is_load_insn (insn, &load_mem))
24787 || (load_store_pendulum == 2 && is_store_insn (insn, &str_mem))))
24788 /* Attach highest priority to insn if the scheduler has just issued two
24789 stores and this instruction is a load, or two loads and this instruction
24790 is a store. Power6 wants loads and stores scheduled alternately
24791 when possible */
24792 return current_sched_info->sched_max_insns_priority;
24794 return priority;
24797 /* Return true if the instruction is nonpipelined on the Cell. */
24798 static bool
24799 is_nonpipeline_insn (rtx insn)
24801 enum attr_type type;
24802 if (!insn || !NONDEBUG_INSN_P (insn)
24803 || GET_CODE (PATTERN (insn)) == USE
24804 || GET_CODE (PATTERN (insn)) == CLOBBER)
24805 return false;
24807 type = get_attr_type (insn);
24808 if (type == TYPE_IMUL
24809 || type == TYPE_IMUL2
24810 || type == TYPE_IMUL3
24811 || type == TYPE_LMUL
24812 || type == TYPE_IDIV
24813 || type == TYPE_LDIV
24814 || type == TYPE_SDIV
24815 || type == TYPE_DDIV
24816 || type == TYPE_SSQRT
24817 || type == TYPE_DSQRT
24818 || type == TYPE_MFCR
24819 || type == TYPE_MFCRF
24820 || type == TYPE_MFJMPR)
24822 return true;
24824 return false;
24828 /* Return how many instructions the machine can issue per cycle. */
24830 static int
24831 rs6000_issue_rate (void)
24833 /* Unless scheduling for register pressure, use issue rate of 1 for
24834 first scheduling pass to decrease degradation. */
24835 if (!reload_completed && !flag_sched_pressure)
24836 return 1;
24838 switch (rs6000_cpu_attr) {
24839 case CPU_RS64A:
24840 case CPU_PPC601: /* ? */
24841 case CPU_PPC7450:
24842 return 3;
24843 case CPU_PPC440:
24844 case CPU_PPC603:
24845 case CPU_PPC750:
24846 case CPU_PPC7400:
24847 case CPU_PPC8540:
24848 case CPU_PPC8548:
24849 case CPU_CELL:
24850 case CPU_PPCE300C2:
24851 case CPU_PPCE300C3:
24852 case CPU_PPCE500MC:
24853 case CPU_PPCE500MC64:
24854 case CPU_PPCE5500:
24855 case CPU_PPCE6500:
24856 case CPU_TITAN:
24857 return 2;
24858 case CPU_PPC476:
24859 case CPU_PPC604:
24860 case CPU_PPC604E:
24861 case CPU_PPC620:
24862 case CPU_PPC630:
24863 return 4;
24864 case CPU_POWER4:
24865 case CPU_POWER5:
24866 case CPU_POWER6:
24867 case CPU_POWER7:
24868 return 5;
24869 case CPU_POWER8:
24870 return 7;
24871 default:
24872 return 1;
24876 /* Return how many instructions to look ahead for better insn
24877 scheduling. */
24879 static int
24880 rs6000_use_sched_lookahead (void)
24882 switch (rs6000_cpu_attr)
24884 case CPU_PPC8540:
24885 case CPU_PPC8548:
24886 return 4;
24888 case CPU_CELL:
24889 return (reload_completed ? 8 : 0);
24891 default:
24892 return 0;
24896 /* We are choosing insn from the ready queue. Return nonzero if INSN can be chosen. */
24897 static int
24898 rs6000_use_sched_lookahead_guard (rtx insn)
24900 if (rs6000_cpu_attr != CPU_CELL)
24901 return 1;
24903 if (insn == NULL_RTX || !INSN_P (insn))
24904 abort ();
24906 if (!reload_completed
24907 || is_nonpipeline_insn (insn)
24908 || is_microcoded_insn (insn))
24909 return 0;
24911 return 1;
24914 /* Determine if PAT refers to memory. If so, set MEM_REF to the MEM rtx
24915 and return true. */
24917 static bool
24918 find_mem_ref (rtx pat, rtx *mem_ref)
24920 const char * fmt;
24921 int i, j;
24923 /* stack_tie does not produce any real memory traffic. */
24924 if (tie_operand (pat, VOIDmode))
24925 return false;
24927 if (GET_CODE (pat) == MEM)
24929 *mem_ref = pat;
24930 return true;
24933 /* Recursively process the pattern. */
24934 fmt = GET_RTX_FORMAT (GET_CODE (pat));
24936 for (i = GET_RTX_LENGTH (GET_CODE (pat)) - 1; i >= 0; i--)
24938 if (fmt[i] == 'e')
24940 if (find_mem_ref (XEXP (pat, i), mem_ref))
24941 return true;
24943 else if (fmt[i] == 'E')
24944 for (j = XVECLEN (pat, i) - 1; j >= 0; j--)
24946 if (find_mem_ref (XVECEXP (pat, i, j), mem_ref))
24947 return true;
24951 return false;
24954 /* Determine if PAT is a PATTERN of a load insn. */
24956 static bool
24957 is_load_insn1 (rtx pat, rtx *load_mem)
24959 if (!pat || pat == NULL_RTX)
24960 return false;
24962 if (GET_CODE (pat) == SET)
24963 return find_mem_ref (SET_SRC (pat), load_mem);
24965 if (GET_CODE (pat) == PARALLEL)
24967 int i;
24969 for (i = 0; i < XVECLEN (pat, 0); i++)
24970 if (is_load_insn1 (XVECEXP (pat, 0, i), load_mem))
24971 return true;
24974 return false;
24977 /* Determine if INSN loads from memory. */
24979 static bool
24980 is_load_insn (rtx insn, rtx *load_mem)
24982 if (!insn || !INSN_P (insn))
24983 return false;
24985 if (CALL_P (insn))
24986 return false;
24988 return is_load_insn1 (PATTERN (insn), load_mem);
24991 /* Determine if PAT is a PATTERN of a store insn. */
24993 static bool
24994 is_store_insn1 (rtx pat, rtx *str_mem)
24996 if (!pat || pat == NULL_RTX)
24997 return false;
24999 if (GET_CODE (pat) == SET)
25000 return find_mem_ref (SET_DEST (pat), str_mem);
25002 if (GET_CODE (pat) == PARALLEL)
25004 int i;
25006 for (i = 0; i < XVECLEN (pat, 0); i++)
25007 if (is_store_insn1 (XVECEXP (pat, 0, i), str_mem))
25008 return true;
25011 return false;
25014 /* Determine if INSN stores to memory. */
25016 static bool
25017 is_store_insn (rtx insn, rtx *str_mem)
25019 if (!insn || !INSN_P (insn))
25020 return false;
25022 return is_store_insn1 (PATTERN (insn), str_mem);
25025 /* Returns whether the dependence between INSN and NEXT is considered
25026 costly by the given target. */
25028 static bool
25029 rs6000_is_costly_dependence (dep_t dep, int cost, int distance)
25031 rtx insn;
25032 rtx next;
25033 rtx load_mem, str_mem;
25035 /* If the flag is not enabled - no dependence is considered costly;
25036 allow all dependent insns in the same group.
25037 This is the most aggressive option. */
25038 if (rs6000_sched_costly_dep == no_dep_costly)
25039 return false;
25041 /* If the flag is set to 1 - a dependence is always considered costly;
25042 do not allow dependent instructions in the same group.
25043 This is the most conservative option. */
25044 if (rs6000_sched_costly_dep == all_deps_costly)
25045 return true;
25047 insn = DEP_PRO (dep);
25048 next = DEP_CON (dep);
25050 if (rs6000_sched_costly_dep == store_to_load_dep_costly
25051 && is_load_insn (next, &load_mem)
25052 && is_store_insn (insn, &str_mem))
25053 /* Prevent load after store in the same group. */
25054 return true;
25056 if (rs6000_sched_costly_dep == true_store_to_load_dep_costly
25057 && is_load_insn (next, &load_mem)
25058 && is_store_insn (insn, &str_mem)
25059 && DEP_TYPE (dep) == REG_DEP_TRUE
25060 && mem_locations_overlap(str_mem, load_mem))
25061 /* Prevent load after store in the same group if it is a true
25062 dependence. */
25063 return true;
25065 /* The flag is set to X; dependences with latency >= X are considered costly,
25066 and will not be scheduled in the same group. */
25067 if (rs6000_sched_costly_dep <= max_dep_latency
25068 && ((cost - distance) >= (int)rs6000_sched_costly_dep))
25069 return true;
25071 return false;
25074 /* Return the next insn after INSN that is found before TAIL is reached,
25075 skipping any "non-active" insns - insns that will not actually occupy
25076 an issue slot. Return NULL_RTX if such an insn is not found. */
25078 static rtx
25079 get_next_active_insn (rtx insn, rtx tail)
25081 if (insn == NULL_RTX || insn == tail)
25082 return NULL_RTX;
25084 while (1)
25086 insn = NEXT_INSN (insn);
25087 if (insn == NULL_RTX || insn == tail)
25088 return NULL_RTX;
25090 if (CALL_P (insn)
25091 || JUMP_P (insn) || JUMP_TABLE_DATA_P (insn)
25092 || (NONJUMP_INSN_P (insn)
25093 && GET_CODE (PATTERN (insn)) != USE
25094 && GET_CODE (PATTERN (insn)) != CLOBBER
25095 && INSN_CODE (insn) != CODE_FOR_stack_tie))
25096 break;
25098 return insn;
25101 /* We are about to begin issuing insns for this clock cycle. */
25103 static int
25104 rs6000_sched_reorder (FILE *dump ATTRIBUTE_UNUSED, int sched_verbose,
25105 rtx *ready ATTRIBUTE_UNUSED,
25106 int *pn_ready ATTRIBUTE_UNUSED,
25107 int clock_var ATTRIBUTE_UNUSED)
25109 int n_ready = *pn_ready;
25111 if (sched_verbose)
25112 fprintf (dump, "// rs6000_sched_reorder :\n");
25114 /* Reorder the ready list, if the second to last ready insn
25115 is a nonepipeline insn. */
25116 if (rs6000_cpu_attr == CPU_CELL && n_ready > 1)
25118 if (is_nonpipeline_insn (ready[n_ready - 1])
25119 && (recog_memoized (ready[n_ready - 2]) > 0))
25120 /* Simply swap first two insns. */
25122 rtx tmp = ready[n_ready - 1];
25123 ready[n_ready - 1] = ready[n_ready - 2];
25124 ready[n_ready - 2] = tmp;
25128 if (rs6000_cpu == PROCESSOR_POWER6)
25129 load_store_pendulum = 0;
25131 return rs6000_issue_rate ();
25134 /* Like rs6000_sched_reorder, but called after issuing each insn. */
25136 static int
25137 rs6000_sched_reorder2 (FILE *dump, int sched_verbose, rtx *ready,
25138 int *pn_ready, int clock_var ATTRIBUTE_UNUSED)
25140 if (sched_verbose)
25141 fprintf (dump, "// rs6000_sched_reorder2 :\n");
25143 /* For Power6, we need to handle some special cases to try and keep the
25144 store queue from overflowing and triggering expensive flushes.
25146 This code monitors how load and store instructions are being issued
25147 and skews the ready list one way or the other to increase the likelihood
25148 that a desired instruction is issued at the proper time.
25150 A couple of things are done. First, we maintain a "load_store_pendulum"
25151 to track the current state of load/store issue.
25153 - If the pendulum is at zero, then no loads or stores have been
25154 issued in the current cycle so we do nothing.
25156 - If the pendulum is 1, then a single load has been issued in this
25157 cycle and we attempt to locate another load in the ready list to
25158 issue with it.
25160 - If the pendulum is -2, then two stores have already been
25161 issued in this cycle, so we increase the priority of the first load
25162 in the ready list to increase it's likelihood of being chosen first
25163 in the next cycle.
25165 - If the pendulum is -1, then a single store has been issued in this
25166 cycle and we attempt to locate another store in the ready list to
25167 issue with it, preferring a store to an adjacent memory location to
25168 facilitate store pairing in the store queue.
25170 - If the pendulum is 2, then two loads have already been
25171 issued in this cycle, so we increase the priority of the first store
25172 in the ready list to increase it's likelihood of being chosen first
25173 in the next cycle.
25175 - If the pendulum < -2 or > 2, then do nothing.
25177 Note: This code covers the most common scenarios. There exist non
25178 load/store instructions which make use of the LSU and which
25179 would need to be accounted for to strictly model the behavior
25180 of the machine. Those instructions are currently unaccounted
25181 for to help minimize compile time overhead of this code.
25183 if (rs6000_cpu == PROCESSOR_POWER6 && last_scheduled_insn)
25185 int pos;
25186 int i;
25187 rtx tmp, load_mem, str_mem;
25189 if (is_store_insn (last_scheduled_insn, &str_mem))
25190 /* Issuing a store, swing the load_store_pendulum to the left */
25191 load_store_pendulum--;
25192 else if (is_load_insn (last_scheduled_insn, &load_mem))
25193 /* Issuing a load, swing the load_store_pendulum to the right */
25194 load_store_pendulum++;
25195 else
25196 return cached_can_issue_more;
25198 /* If the pendulum is balanced, or there is only one instruction on
25199 the ready list, then all is well, so return. */
25200 if ((load_store_pendulum == 0) || (*pn_ready <= 1))
25201 return cached_can_issue_more;
25203 if (load_store_pendulum == 1)
25205 /* A load has been issued in this cycle. Scan the ready list
25206 for another load to issue with it */
25207 pos = *pn_ready-1;
25209 while (pos >= 0)
25211 if (is_load_insn (ready[pos], &load_mem))
25213 /* Found a load. Move it to the head of the ready list,
25214 and adjust it's priority so that it is more likely to
25215 stay there */
25216 tmp = ready[pos];
25217 for (i=pos; i<*pn_ready-1; i++)
25218 ready[i] = ready[i + 1];
25219 ready[*pn_ready-1] = tmp;
25221 if (!sel_sched_p () && INSN_PRIORITY_KNOWN (tmp))
25222 INSN_PRIORITY (tmp)++;
25223 break;
25225 pos--;
25228 else if (load_store_pendulum == -2)
25230 /* Two stores have been issued in this cycle. Increase the
25231 priority of the first load in the ready list to favor it for
25232 issuing in the next cycle. */
25233 pos = *pn_ready-1;
25235 while (pos >= 0)
25237 if (is_load_insn (ready[pos], &load_mem)
25238 && !sel_sched_p ()
25239 && INSN_PRIORITY_KNOWN (ready[pos]))
25241 INSN_PRIORITY (ready[pos])++;
25243 /* Adjust the pendulum to account for the fact that a load
25244 was found and increased in priority. This is to prevent
25245 increasing the priority of multiple loads */
25246 load_store_pendulum--;
25248 break;
25250 pos--;
25253 else if (load_store_pendulum == -1)
25255 /* A store has been issued in this cycle. Scan the ready list for
25256 another store to issue with it, preferring a store to an adjacent
25257 memory location */
25258 int first_store_pos = -1;
25260 pos = *pn_ready-1;
25262 while (pos >= 0)
25264 if (is_store_insn (ready[pos], &str_mem))
25266 rtx str_mem2;
25267 /* Maintain the index of the first store found on the
25268 list */
25269 if (first_store_pos == -1)
25270 first_store_pos = pos;
25272 if (is_store_insn (last_scheduled_insn, &str_mem2)
25273 && adjacent_mem_locations (str_mem, str_mem2))
25275 /* Found an adjacent store. Move it to the head of the
25276 ready list, and adjust it's priority so that it is
25277 more likely to stay there */
25278 tmp = ready[pos];
25279 for (i=pos; i<*pn_ready-1; i++)
25280 ready[i] = ready[i + 1];
25281 ready[*pn_ready-1] = tmp;
25283 if (!sel_sched_p () && INSN_PRIORITY_KNOWN (tmp))
25284 INSN_PRIORITY (tmp)++;
25286 first_store_pos = -1;
25288 break;
25291 pos--;
25294 if (first_store_pos >= 0)
25296 /* An adjacent store wasn't found, but a non-adjacent store was,
25297 so move the non-adjacent store to the front of the ready
25298 list, and adjust its priority so that it is more likely to
25299 stay there. */
25300 tmp = ready[first_store_pos];
25301 for (i=first_store_pos; i<*pn_ready-1; i++)
25302 ready[i] = ready[i + 1];
25303 ready[*pn_ready-1] = tmp;
25304 if (!sel_sched_p () && INSN_PRIORITY_KNOWN (tmp))
25305 INSN_PRIORITY (tmp)++;
25308 else if (load_store_pendulum == 2)
25310 /* Two loads have been issued in this cycle. Increase the priority
25311 of the first store in the ready list to favor it for issuing in
25312 the next cycle. */
25313 pos = *pn_ready-1;
25315 while (pos >= 0)
25317 if (is_store_insn (ready[pos], &str_mem)
25318 && !sel_sched_p ()
25319 && INSN_PRIORITY_KNOWN (ready[pos]))
25321 INSN_PRIORITY (ready[pos])++;
25323 /* Adjust the pendulum to account for the fact that a store
25324 was found and increased in priority. This is to prevent
25325 increasing the priority of multiple stores */
25326 load_store_pendulum++;
25328 break;
25330 pos--;
25335 return cached_can_issue_more;
25338 /* Return whether the presence of INSN causes a dispatch group termination
25339 of group WHICH_GROUP.
25341 If WHICH_GROUP == current_group, this function will return true if INSN
25342 causes the termination of the current group (i.e, the dispatch group to
25343 which INSN belongs). This means that INSN will be the last insn in the
25344 group it belongs to.
25346 If WHICH_GROUP == previous_group, this function will return true if INSN
25347 causes the termination of the previous group (i.e, the dispatch group that
25348 precedes the group to which INSN belongs). This means that INSN will be
25349 the first insn in the group it belongs to). */
25351 static bool
25352 insn_terminates_group_p (rtx insn, enum group_termination which_group)
25354 bool first, last;
25356 if (! insn)
25357 return false;
25359 first = insn_must_be_first_in_group (insn);
25360 last = insn_must_be_last_in_group (insn);
25362 if (first && last)
25363 return true;
25365 if (which_group == current_group)
25366 return last;
25367 else if (which_group == previous_group)
25368 return first;
25370 return false;
25374 static bool
25375 insn_must_be_first_in_group (rtx insn)
25377 enum attr_type type;
25379 if (!insn
25380 || NOTE_P (insn)
25381 || DEBUG_INSN_P (insn)
25382 || GET_CODE (PATTERN (insn)) == USE
25383 || GET_CODE (PATTERN (insn)) == CLOBBER)
25384 return false;
25386 switch (rs6000_cpu)
25388 case PROCESSOR_POWER5:
25389 if (is_cracked_insn (insn))
25390 return true;
25391 case PROCESSOR_POWER4:
25392 if (is_microcoded_insn (insn))
25393 return true;
25395 if (!rs6000_sched_groups)
25396 return false;
25398 type = get_attr_type (insn);
25400 switch (type)
25402 case TYPE_MFCR:
25403 case TYPE_MFCRF:
25404 case TYPE_MTCR:
25405 case TYPE_DELAYED_CR:
25406 case TYPE_CR_LOGICAL:
25407 case TYPE_MTJMPR:
25408 case TYPE_MFJMPR:
25409 case TYPE_IDIV:
25410 case TYPE_LDIV:
25411 case TYPE_LOAD_L:
25412 case TYPE_STORE_C:
25413 case TYPE_ISYNC:
25414 case TYPE_SYNC:
25415 return true;
25416 default:
25417 break;
25419 break;
25420 case PROCESSOR_POWER6:
25421 type = get_attr_type (insn);
25423 switch (type)
25425 case TYPE_INSERT_DWORD:
25426 case TYPE_EXTS:
25427 case TYPE_CNTLZ:
25428 case TYPE_SHIFT:
25429 case TYPE_VAR_SHIFT_ROTATE:
25430 case TYPE_TRAP:
25431 case TYPE_IMUL:
25432 case TYPE_IMUL2:
25433 case TYPE_IMUL3:
25434 case TYPE_LMUL:
25435 case TYPE_IDIV:
25436 case TYPE_INSERT_WORD:
25437 case TYPE_DELAYED_COMPARE:
25438 case TYPE_IMUL_COMPARE:
25439 case TYPE_LMUL_COMPARE:
25440 case TYPE_FPCOMPARE:
25441 case TYPE_MFCR:
25442 case TYPE_MTCR:
25443 case TYPE_MFJMPR:
25444 case TYPE_MTJMPR:
25445 case TYPE_ISYNC:
25446 case TYPE_SYNC:
25447 case TYPE_LOAD_L:
25448 case TYPE_STORE_C:
25449 case TYPE_LOAD_U:
25450 case TYPE_LOAD_UX:
25451 case TYPE_LOAD_EXT_UX:
25452 case TYPE_STORE_U:
25453 case TYPE_STORE_UX:
25454 case TYPE_FPLOAD_U:
25455 case TYPE_FPLOAD_UX:
25456 case TYPE_FPSTORE_U:
25457 case TYPE_FPSTORE_UX:
25458 return true;
25459 default:
25460 break;
25462 break;
25463 case PROCESSOR_POWER7:
25464 type = get_attr_type (insn);
25466 switch (type)
25468 case TYPE_CR_LOGICAL:
25469 case TYPE_MFCR:
25470 case TYPE_MFCRF:
25471 case TYPE_MTCR:
25472 case TYPE_IDIV:
25473 case TYPE_LDIV:
25474 case TYPE_COMPARE:
25475 case TYPE_DELAYED_COMPARE:
25476 case TYPE_VAR_DELAYED_COMPARE:
25477 case TYPE_ISYNC:
25478 case TYPE_LOAD_L:
25479 case TYPE_STORE_C:
25480 case TYPE_LOAD_U:
25481 case TYPE_LOAD_UX:
25482 case TYPE_LOAD_EXT:
25483 case TYPE_LOAD_EXT_U:
25484 case TYPE_LOAD_EXT_UX:
25485 case TYPE_STORE_U:
25486 case TYPE_STORE_UX:
25487 case TYPE_FPLOAD_U:
25488 case TYPE_FPLOAD_UX:
25489 case TYPE_FPSTORE_U:
25490 case TYPE_FPSTORE_UX:
25491 case TYPE_MFJMPR:
25492 case TYPE_MTJMPR:
25493 return true;
25494 default:
25495 break;
25497 break;
25498 case PROCESSOR_POWER8:
25499 type = get_attr_type (insn);
25501 switch (type)
25503 case TYPE_CR_LOGICAL:
25504 case TYPE_DELAYED_CR:
25505 case TYPE_MFCR:
25506 case TYPE_MFCRF:
25507 case TYPE_MTCR:
25508 case TYPE_COMPARE:
25509 case TYPE_DELAYED_COMPARE:
25510 case TYPE_VAR_DELAYED_COMPARE:
25511 case TYPE_IMUL_COMPARE:
25512 case TYPE_LMUL_COMPARE:
25513 case TYPE_SYNC:
25514 case TYPE_ISYNC:
25515 case TYPE_LOAD_L:
25516 case TYPE_STORE_C:
25517 case TYPE_LOAD_U:
25518 case TYPE_LOAD_UX:
25519 case TYPE_LOAD_EXT:
25520 case TYPE_LOAD_EXT_U:
25521 case TYPE_LOAD_EXT_UX:
25522 case TYPE_STORE_UX:
25523 case TYPE_VECSTORE:
25524 case TYPE_MFJMPR:
25525 case TYPE_MTJMPR:
25526 return true;
25527 default:
25528 break;
25530 break;
25531 default:
25532 break;
25535 return false;
25538 static bool
25539 insn_must_be_last_in_group (rtx insn)
25541 enum attr_type type;
25543 if (!insn
25544 || NOTE_P (insn)
25545 || DEBUG_INSN_P (insn)
25546 || GET_CODE (PATTERN (insn)) == USE
25547 || GET_CODE (PATTERN (insn)) == CLOBBER)
25548 return false;
25550 switch (rs6000_cpu) {
25551 case PROCESSOR_POWER4:
25552 case PROCESSOR_POWER5:
25553 if (is_microcoded_insn (insn))
25554 return true;
25556 if (is_branch_slot_insn (insn))
25557 return true;
25559 break;
25560 case PROCESSOR_POWER6:
25561 type = get_attr_type (insn);
25563 switch (type)
25565 case TYPE_EXTS:
25566 case TYPE_CNTLZ:
25567 case TYPE_SHIFT:
25568 case TYPE_VAR_SHIFT_ROTATE:
25569 case TYPE_TRAP:
25570 case TYPE_IMUL:
25571 case TYPE_IMUL2:
25572 case TYPE_IMUL3:
25573 case TYPE_LMUL:
25574 case TYPE_IDIV:
25575 case TYPE_DELAYED_COMPARE:
25576 case TYPE_IMUL_COMPARE:
25577 case TYPE_LMUL_COMPARE:
25578 case TYPE_FPCOMPARE:
25579 case TYPE_MFCR:
25580 case TYPE_MTCR:
25581 case TYPE_MFJMPR:
25582 case TYPE_MTJMPR:
25583 case TYPE_ISYNC:
25584 case TYPE_SYNC:
25585 case TYPE_LOAD_L:
25586 case TYPE_STORE_C:
25587 return true;
25588 default:
25589 break;
25591 break;
25592 case PROCESSOR_POWER7:
25593 type = get_attr_type (insn);
25595 switch (type)
25597 case TYPE_ISYNC:
25598 case TYPE_SYNC:
25599 case TYPE_LOAD_L:
25600 case TYPE_STORE_C:
25601 case TYPE_LOAD_EXT_U:
25602 case TYPE_LOAD_EXT_UX:
25603 case TYPE_STORE_UX:
25604 return true;
25605 default:
25606 break;
25608 break;
25609 case PROCESSOR_POWER8:
25610 type = get_attr_type (insn);
25612 switch (type)
25614 case TYPE_MFCR:
25615 case TYPE_MTCR:
25616 case TYPE_ISYNC:
25617 case TYPE_SYNC:
25618 case TYPE_LOAD_L:
25619 case TYPE_STORE_C:
25620 case TYPE_LOAD_EXT_U:
25621 case TYPE_LOAD_EXT_UX:
25622 case TYPE_STORE_UX:
25623 return true;
25624 default:
25625 break;
25627 break;
25628 default:
25629 break;
25632 return false;
25635 /* Return true if it is recommended to keep NEXT_INSN "far" (in a separate
25636 dispatch group) from the insns in GROUP_INSNS. Return false otherwise. */
25638 static bool
25639 is_costly_group (rtx *group_insns, rtx next_insn)
25641 int i;
25642 int issue_rate = rs6000_issue_rate ();
25644 for (i = 0; i < issue_rate; i++)
25646 sd_iterator_def sd_it;
25647 dep_t dep;
25648 rtx insn = group_insns[i];
25650 if (!insn)
25651 continue;
25653 FOR_EACH_DEP (insn, SD_LIST_RES_FORW, sd_it, dep)
25655 rtx next = DEP_CON (dep);
25657 if (next == next_insn
25658 && rs6000_is_costly_dependence (dep, dep_cost (dep), 0))
25659 return true;
25663 return false;
25666 /* Utility of the function redefine_groups.
25667 Check if it is too costly to schedule NEXT_INSN together with GROUP_INSNS
25668 in the same dispatch group. If so, insert nops before NEXT_INSN, in order
25669 to keep it "far" (in a separate group) from GROUP_INSNS, following
25670 one of the following schemes, depending on the value of the flag
25671 -minsert_sched_nops = X:
25672 (1) X == sched_finish_regroup_exact: insert exactly as many nops as needed
25673 in order to force NEXT_INSN into a separate group.
25674 (2) X < sched_finish_regroup_exact: insert exactly X nops.
25675 GROUP_END, CAN_ISSUE_MORE and GROUP_COUNT record the state after nop
25676 insertion (has a group just ended, how many vacant issue slots remain in the
25677 last group, and how many dispatch groups were encountered so far). */
25679 static int
25680 force_new_group (int sched_verbose, FILE *dump, rtx *group_insns,
25681 rtx next_insn, bool *group_end, int can_issue_more,
25682 int *group_count)
25684 rtx nop;
25685 bool force;
25686 int issue_rate = rs6000_issue_rate ();
25687 bool end = *group_end;
25688 int i;
25690 if (next_insn == NULL_RTX || DEBUG_INSN_P (next_insn))
25691 return can_issue_more;
25693 if (rs6000_sched_insert_nops > sched_finish_regroup_exact)
25694 return can_issue_more;
25696 force = is_costly_group (group_insns, next_insn);
25697 if (!force)
25698 return can_issue_more;
25700 if (sched_verbose > 6)
25701 fprintf (dump,"force: group count = %d, can_issue_more = %d\n",
25702 *group_count ,can_issue_more);
25704 if (rs6000_sched_insert_nops == sched_finish_regroup_exact)
25706 if (*group_end)
25707 can_issue_more = 0;
25709 /* Since only a branch can be issued in the last issue_slot, it is
25710 sufficient to insert 'can_issue_more - 1' nops if next_insn is not
25711 a branch. If next_insn is a branch, we insert 'can_issue_more' nops;
25712 in this case the last nop will start a new group and the branch
25713 will be forced to the new group. */
25714 if (can_issue_more && !is_branch_slot_insn (next_insn))
25715 can_issue_more--;
25717 /* Do we have a special group ending nop? */
25718 if (rs6000_cpu_attr == CPU_POWER6 || rs6000_cpu_attr == CPU_POWER7
25719 || rs6000_cpu_attr == CPU_POWER8)
25721 nop = gen_group_ending_nop ();
25722 emit_insn_before (nop, next_insn);
25723 can_issue_more = 0;
25725 else
25726 while (can_issue_more > 0)
25728 nop = gen_nop ();
25729 emit_insn_before (nop, next_insn);
25730 can_issue_more--;
25733 *group_end = true;
25734 return 0;
25737 if (rs6000_sched_insert_nops < sched_finish_regroup_exact)
25739 int n_nops = rs6000_sched_insert_nops;
25741 /* Nops can't be issued from the branch slot, so the effective
25742 issue_rate for nops is 'issue_rate - 1'. */
25743 if (can_issue_more == 0)
25744 can_issue_more = issue_rate;
25745 can_issue_more--;
25746 if (can_issue_more == 0)
25748 can_issue_more = issue_rate - 1;
25749 (*group_count)++;
25750 end = true;
25751 for (i = 0; i < issue_rate; i++)
25753 group_insns[i] = 0;
25757 while (n_nops > 0)
25759 nop = gen_nop ();
25760 emit_insn_before (nop, next_insn);
25761 if (can_issue_more == issue_rate - 1) /* new group begins */
25762 end = false;
25763 can_issue_more--;
25764 if (can_issue_more == 0)
25766 can_issue_more = issue_rate - 1;
25767 (*group_count)++;
25768 end = true;
25769 for (i = 0; i < issue_rate; i++)
25771 group_insns[i] = 0;
25774 n_nops--;
25777 /* Scale back relative to 'issue_rate' (instead of 'issue_rate - 1'). */
25778 can_issue_more++;
25780 /* Is next_insn going to start a new group? */
25781 *group_end
25782 = (end
25783 || (can_issue_more == 1 && !is_branch_slot_insn (next_insn))
25784 || (can_issue_more <= 2 && is_cracked_insn (next_insn))
25785 || (can_issue_more < issue_rate &&
25786 insn_terminates_group_p (next_insn, previous_group)));
25787 if (*group_end && end)
25788 (*group_count)--;
25790 if (sched_verbose > 6)
25791 fprintf (dump, "done force: group count = %d, can_issue_more = %d\n",
25792 *group_count, can_issue_more);
25793 return can_issue_more;
25796 return can_issue_more;
25799 /* This function tries to synch the dispatch groups that the compiler "sees"
25800 with the dispatch groups that the processor dispatcher is expected to
25801 form in practice. It tries to achieve this synchronization by forcing the
25802 estimated processor grouping on the compiler (as opposed to the function
25803 'pad_goups' which tries to force the scheduler's grouping on the processor).
25805 The function scans the insn sequence between PREV_HEAD_INSN and TAIL and
25806 examines the (estimated) dispatch groups that will be formed by the processor
25807 dispatcher. It marks these group boundaries to reflect the estimated
25808 processor grouping, overriding the grouping that the scheduler had marked.
25809 Depending on the value of the flag '-minsert-sched-nops' this function can
25810 force certain insns into separate groups or force a certain distance between
25811 them by inserting nops, for example, if there exists a "costly dependence"
25812 between the insns.
25814 The function estimates the group boundaries that the processor will form as
25815 follows: It keeps track of how many vacant issue slots are available after
25816 each insn. A subsequent insn will start a new group if one of the following
25817 4 cases applies:
25818 - no more vacant issue slots remain in the current dispatch group.
25819 - only the last issue slot, which is the branch slot, is vacant, but the next
25820 insn is not a branch.
25821 - only the last 2 or less issue slots, including the branch slot, are vacant,
25822 which means that a cracked insn (which occupies two issue slots) can't be
25823 issued in this group.
25824 - less than 'issue_rate' slots are vacant, and the next insn always needs to
25825 start a new group. */
25827 static int
25828 redefine_groups (FILE *dump, int sched_verbose, rtx prev_head_insn, rtx tail)
25830 rtx insn, next_insn;
25831 int issue_rate;
25832 int can_issue_more;
25833 int slot, i;
25834 bool group_end;
25835 int group_count = 0;
25836 rtx *group_insns;
25838 /* Initialize. */
25839 issue_rate = rs6000_issue_rate ();
25840 group_insns = XALLOCAVEC (rtx, issue_rate);
25841 for (i = 0; i < issue_rate; i++)
25843 group_insns[i] = 0;
25845 can_issue_more = issue_rate;
25846 slot = 0;
25847 insn = get_next_active_insn (prev_head_insn, tail);
25848 group_end = false;
25850 while (insn != NULL_RTX)
25852 slot = (issue_rate - can_issue_more);
25853 group_insns[slot] = insn;
25854 can_issue_more =
25855 rs6000_variable_issue (dump, sched_verbose, insn, can_issue_more);
25856 if (insn_terminates_group_p (insn, current_group))
25857 can_issue_more = 0;
25859 next_insn = get_next_active_insn (insn, tail);
25860 if (next_insn == NULL_RTX)
25861 return group_count + 1;
25863 /* Is next_insn going to start a new group? */
25864 group_end
25865 = (can_issue_more == 0
25866 || (can_issue_more == 1 && !is_branch_slot_insn (next_insn))
25867 || (can_issue_more <= 2 && is_cracked_insn (next_insn))
25868 || (can_issue_more < issue_rate &&
25869 insn_terminates_group_p (next_insn, previous_group)));
25871 can_issue_more = force_new_group (sched_verbose, dump, group_insns,
25872 next_insn, &group_end, can_issue_more,
25873 &group_count);
25875 if (group_end)
25877 group_count++;
25878 can_issue_more = 0;
25879 for (i = 0; i < issue_rate; i++)
25881 group_insns[i] = 0;
25885 if (GET_MODE (next_insn) == TImode && can_issue_more)
25886 PUT_MODE (next_insn, VOIDmode);
25887 else if (!can_issue_more && GET_MODE (next_insn) != TImode)
25888 PUT_MODE (next_insn, TImode);
25890 insn = next_insn;
25891 if (can_issue_more == 0)
25892 can_issue_more = issue_rate;
25893 } /* while */
25895 return group_count;
25898 /* Scan the insn sequence between PREV_HEAD_INSN and TAIL and examine the
25899 dispatch group boundaries that the scheduler had marked. Pad with nops
25900 any dispatch groups which have vacant issue slots, in order to force the
25901 scheduler's grouping on the processor dispatcher. The function
25902 returns the number of dispatch groups found. */
25904 static int
25905 pad_groups (FILE *dump, int sched_verbose, rtx prev_head_insn, rtx tail)
25907 rtx insn, next_insn;
25908 rtx nop;
25909 int issue_rate;
25910 int can_issue_more;
25911 int group_end;
25912 int group_count = 0;
25914 /* Initialize issue_rate. */
25915 issue_rate = rs6000_issue_rate ();
25916 can_issue_more = issue_rate;
25918 insn = get_next_active_insn (prev_head_insn, tail);
25919 next_insn = get_next_active_insn (insn, tail);
25921 while (insn != NULL_RTX)
25923 can_issue_more =
25924 rs6000_variable_issue (dump, sched_verbose, insn, can_issue_more);
25926 group_end = (next_insn == NULL_RTX || GET_MODE (next_insn) == TImode);
25928 if (next_insn == NULL_RTX)
25929 break;
25931 if (group_end)
25933 /* If the scheduler had marked group termination at this location
25934 (between insn and next_insn), and neither insn nor next_insn will
25935 force group termination, pad the group with nops to force group
25936 termination. */
25937 if (can_issue_more
25938 && (rs6000_sched_insert_nops == sched_finish_pad_groups)
25939 && !insn_terminates_group_p (insn, current_group)
25940 && !insn_terminates_group_p (next_insn, previous_group))
25942 if (!is_branch_slot_insn (next_insn))
25943 can_issue_more--;
25945 while (can_issue_more)
25947 nop = gen_nop ();
25948 emit_insn_before (nop, next_insn);
25949 can_issue_more--;
25953 can_issue_more = issue_rate;
25954 group_count++;
25957 insn = next_insn;
25958 next_insn = get_next_active_insn (insn, tail);
25961 return group_count;
25964 /* We're beginning a new block. Initialize data structures as necessary. */
25966 static void
25967 rs6000_sched_init (FILE *dump ATTRIBUTE_UNUSED,
25968 int sched_verbose ATTRIBUTE_UNUSED,
25969 int max_ready ATTRIBUTE_UNUSED)
25971 last_scheduled_insn = NULL_RTX;
25972 load_store_pendulum = 0;
25975 /* The following function is called at the end of scheduling BB.
25976 After reload, it inserts nops at insn group bundling. */
25978 static void
25979 rs6000_sched_finish (FILE *dump, int sched_verbose)
25981 int n_groups;
25983 if (sched_verbose)
25984 fprintf (dump, "=== Finishing schedule.\n");
25986 if (reload_completed && rs6000_sched_groups)
25988 /* Do not run sched_finish hook when selective scheduling enabled. */
25989 if (sel_sched_p ())
25990 return;
25992 if (rs6000_sched_insert_nops == sched_finish_none)
25993 return;
25995 if (rs6000_sched_insert_nops == sched_finish_pad_groups)
25996 n_groups = pad_groups (dump, sched_verbose,
25997 current_sched_info->prev_head,
25998 current_sched_info->next_tail);
25999 else
26000 n_groups = redefine_groups (dump, sched_verbose,
26001 current_sched_info->prev_head,
26002 current_sched_info->next_tail);
26004 if (sched_verbose >= 6)
26006 fprintf (dump, "ngroups = %d\n", n_groups);
26007 print_rtl (dump, current_sched_info->prev_head);
26008 fprintf (dump, "Done finish_sched\n");
26013 struct _rs6000_sched_context
26015 short cached_can_issue_more;
26016 rtx last_scheduled_insn;
26017 int load_store_pendulum;
26020 typedef struct _rs6000_sched_context rs6000_sched_context_def;
26021 typedef rs6000_sched_context_def *rs6000_sched_context_t;
26023 /* Allocate store for new scheduling context. */
26024 static void *
26025 rs6000_alloc_sched_context (void)
26027 return xmalloc (sizeof (rs6000_sched_context_def));
26030 /* If CLEAN_P is true then initializes _SC with clean data,
26031 and from the global context otherwise. */
26032 static void
26033 rs6000_init_sched_context (void *_sc, bool clean_p)
26035 rs6000_sched_context_t sc = (rs6000_sched_context_t) _sc;
26037 if (clean_p)
26039 sc->cached_can_issue_more = 0;
26040 sc->last_scheduled_insn = NULL_RTX;
26041 sc->load_store_pendulum = 0;
26043 else
26045 sc->cached_can_issue_more = cached_can_issue_more;
26046 sc->last_scheduled_insn = last_scheduled_insn;
26047 sc->load_store_pendulum = load_store_pendulum;
26051 /* Sets the global scheduling context to the one pointed to by _SC. */
26052 static void
26053 rs6000_set_sched_context (void *_sc)
26055 rs6000_sched_context_t sc = (rs6000_sched_context_t) _sc;
26057 gcc_assert (sc != NULL);
26059 cached_can_issue_more = sc->cached_can_issue_more;
26060 last_scheduled_insn = sc->last_scheduled_insn;
26061 load_store_pendulum = sc->load_store_pendulum;
26064 /* Free _SC. */
26065 static void
26066 rs6000_free_sched_context (void *_sc)
26068 gcc_assert (_sc != NULL);
26070 free (_sc);
26074 /* Length in units of the trampoline for entering a nested function. */
26077 rs6000_trampoline_size (void)
26079 int ret = 0;
26081 switch (DEFAULT_ABI)
26083 default:
26084 gcc_unreachable ();
26086 case ABI_AIX:
26087 ret = (TARGET_32BIT) ? 12 : 24;
26088 break;
26090 case ABI_DARWIN:
26091 case ABI_V4:
26092 ret = (TARGET_32BIT) ? 40 : 48;
26093 break;
26096 return ret;
26099 /* Emit RTL insns to initialize the variable parts of a trampoline.
26100 FNADDR is an RTX for the address of the function's pure code.
26101 CXT is an RTX for the static chain value for the function. */
26103 static void
26104 rs6000_trampoline_init (rtx m_tramp, tree fndecl, rtx cxt)
26106 int regsize = (TARGET_32BIT) ? 4 : 8;
26107 rtx fnaddr = XEXP (DECL_RTL (fndecl), 0);
26108 rtx ctx_reg = force_reg (Pmode, cxt);
26109 rtx addr = force_reg (Pmode, XEXP (m_tramp, 0));
26111 switch (DEFAULT_ABI)
26113 default:
26114 gcc_unreachable ();
26116 /* Under AIX, just build the 3 word function descriptor */
26117 case ABI_AIX:
26119 rtx fnmem, fn_reg, toc_reg;
26121 if (!TARGET_POINTERS_TO_NESTED_FUNCTIONS)
26122 error ("You cannot take the address of a nested function if you use "
26123 "the -mno-pointers-to-nested-functions option.");
26125 fnmem = gen_const_mem (Pmode, force_reg (Pmode, fnaddr));
26126 fn_reg = gen_reg_rtx (Pmode);
26127 toc_reg = gen_reg_rtx (Pmode);
26129 /* Macro to shorten the code expansions below. */
26130 # define MEM_PLUS(MEM, OFFSET) adjust_address (MEM, Pmode, OFFSET)
26132 m_tramp = replace_equiv_address (m_tramp, addr);
26134 emit_move_insn (fn_reg, MEM_PLUS (fnmem, 0));
26135 emit_move_insn (toc_reg, MEM_PLUS (fnmem, regsize));
26136 emit_move_insn (MEM_PLUS (m_tramp, 0), fn_reg);
26137 emit_move_insn (MEM_PLUS (m_tramp, regsize), toc_reg);
26138 emit_move_insn (MEM_PLUS (m_tramp, 2*regsize), ctx_reg);
26140 # undef MEM_PLUS
26142 break;
26144 /* Under V.4/eabi/darwin, __trampoline_setup does the real work. */
26145 case ABI_DARWIN:
26146 case ABI_V4:
26147 emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "__trampoline_setup"),
26148 LCT_NORMAL, VOIDmode, 4,
26149 addr, Pmode,
26150 GEN_INT (rs6000_trampoline_size ()), SImode,
26151 fnaddr, Pmode,
26152 ctx_reg, Pmode);
26153 break;
26158 /* Returns TRUE iff the target attribute indicated by ATTR_ID takes a plain
26159 identifier as an argument, so the front end shouldn't look it up. */
26161 static bool
26162 rs6000_attribute_takes_identifier_p (const_tree attr_id)
26164 return is_attribute_p ("altivec", attr_id);
26167 /* Handle the "altivec" attribute. The attribute may have
26168 arguments as follows:
26170 __attribute__((altivec(vector__)))
26171 __attribute__((altivec(pixel__))) (always followed by 'unsigned short')
26172 __attribute__((altivec(bool__))) (always followed by 'unsigned')
26174 and may appear more than once (e.g., 'vector bool char') in a
26175 given declaration. */
26177 static tree
26178 rs6000_handle_altivec_attribute (tree *node,
26179 tree name ATTRIBUTE_UNUSED,
26180 tree args,
26181 int flags ATTRIBUTE_UNUSED,
26182 bool *no_add_attrs)
26184 tree type = *node, result = NULL_TREE;
26185 enum machine_mode mode;
26186 int unsigned_p;
26187 char altivec_type
26188 = ((args && TREE_CODE (args) == TREE_LIST && TREE_VALUE (args)
26189 && TREE_CODE (TREE_VALUE (args)) == IDENTIFIER_NODE)
26190 ? *IDENTIFIER_POINTER (TREE_VALUE (args))
26191 : '?');
26193 while (POINTER_TYPE_P (type)
26194 || TREE_CODE (type) == FUNCTION_TYPE
26195 || TREE_CODE (type) == METHOD_TYPE
26196 || TREE_CODE (type) == ARRAY_TYPE)
26197 type = TREE_TYPE (type);
26199 mode = TYPE_MODE (type);
26201 /* Check for invalid AltiVec type qualifiers. */
26202 if (type == long_double_type_node)
26203 error ("use of %<long double%> in AltiVec types is invalid");
26204 else if (type == boolean_type_node)
26205 error ("use of boolean types in AltiVec types is invalid");
26206 else if (TREE_CODE (type) == COMPLEX_TYPE)
26207 error ("use of %<complex%> in AltiVec types is invalid");
26208 else if (DECIMAL_FLOAT_MODE_P (mode))
26209 error ("use of decimal floating point types in AltiVec types is invalid");
26210 else if (!TARGET_VSX)
26212 if (type == long_unsigned_type_node || type == long_integer_type_node)
26214 if (TARGET_64BIT)
26215 error ("use of %<long%> in AltiVec types is invalid for "
26216 "64-bit code without -mvsx");
26217 else if (rs6000_warn_altivec_long)
26218 warning (0, "use of %<long%> in AltiVec types is deprecated; "
26219 "use %<int%>");
26221 else if (type == long_long_unsigned_type_node
26222 || type == long_long_integer_type_node)
26223 error ("use of %<long long%> in AltiVec types is invalid without "
26224 "-mvsx");
26225 else if (type == double_type_node)
26226 error ("use of %<double%> in AltiVec types is invalid without -mvsx");
26229 switch (altivec_type)
26231 case 'v':
26232 unsigned_p = TYPE_UNSIGNED (type);
26233 switch (mode)
26235 case DImode:
26236 result = (unsigned_p ? unsigned_V2DI_type_node : V2DI_type_node);
26237 break;
26238 case SImode:
26239 result = (unsigned_p ? unsigned_V4SI_type_node : V4SI_type_node);
26240 break;
26241 case HImode:
26242 result = (unsigned_p ? unsigned_V8HI_type_node : V8HI_type_node);
26243 break;
26244 case QImode:
26245 result = (unsigned_p ? unsigned_V16QI_type_node : V16QI_type_node);
26246 break;
26247 case SFmode: result = V4SF_type_node; break;
26248 case DFmode: result = V2DF_type_node; break;
26249 /* If the user says 'vector int bool', we may be handed the 'bool'
26250 attribute _before_ the 'vector' attribute, and so select the
26251 proper type in the 'b' case below. */
26252 case V4SImode: case V8HImode: case V16QImode: case V4SFmode:
26253 case V2DImode: case V2DFmode:
26254 result = type;
26255 default: break;
26257 break;
26258 case 'b':
26259 switch (mode)
26261 case DImode: case V2DImode: result = bool_V2DI_type_node; break;
26262 case SImode: case V4SImode: result = bool_V4SI_type_node; break;
26263 case HImode: case V8HImode: result = bool_V8HI_type_node; break;
26264 case QImode: case V16QImode: result = bool_V16QI_type_node;
26265 default: break;
26267 break;
26268 case 'p':
26269 switch (mode)
26271 case V8HImode: result = pixel_V8HI_type_node;
26272 default: break;
26274 default: break;
26277 /* Propagate qualifiers attached to the element type
26278 onto the vector type. */
26279 if (result && result != type && TYPE_QUALS (type))
26280 result = build_qualified_type (result, TYPE_QUALS (type));
26282 *no_add_attrs = true; /* No need to hang on to the attribute. */
26284 if (result)
26285 *node = lang_hooks.types.reconstruct_complex_type (*node, result);
26287 return NULL_TREE;
26290 /* AltiVec defines four built-in scalar types that serve as vector
26291 elements; we must teach the compiler how to mangle them. */
26293 static const char *
26294 rs6000_mangle_type (const_tree type)
26296 type = TYPE_MAIN_VARIANT (type);
26298 if (TREE_CODE (type) != VOID_TYPE && TREE_CODE (type) != BOOLEAN_TYPE
26299 && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
26300 return NULL;
26302 if (type == bool_char_type_node) return "U6__boolc";
26303 if (type == bool_short_type_node) return "U6__bools";
26304 if (type == pixel_type_node) return "u7__pixel";
26305 if (type == bool_int_type_node) return "U6__booli";
26306 if (type == bool_long_type_node) return "U6__booll";
26308 /* Mangle IBM extended float long double as `g' (__float128) on
26309 powerpc*-linux where long-double-64 previously was the default. */
26310 if (TYPE_MAIN_VARIANT (type) == long_double_type_node
26311 && TARGET_ELF
26312 && TARGET_LONG_DOUBLE_128
26313 && !TARGET_IEEEQUAD)
26314 return "g";
26316 /* For all other types, use normal C++ mangling. */
26317 return NULL;
26320 /* Handle a "longcall" or "shortcall" attribute; arguments as in
26321 struct attribute_spec.handler. */
26323 static tree
26324 rs6000_handle_longcall_attribute (tree *node, tree name,
26325 tree args ATTRIBUTE_UNUSED,
26326 int flags ATTRIBUTE_UNUSED,
26327 bool *no_add_attrs)
26329 if (TREE_CODE (*node) != FUNCTION_TYPE
26330 && TREE_CODE (*node) != FIELD_DECL
26331 && TREE_CODE (*node) != TYPE_DECL)
26333 warning (OPT_Wattributes, "%qE attribute only applies to functions",
26334 name);
26335 *no_add_attrs = true;
26338 return NULL_TREE;
26341 /* Set longcall attributes on all functions declared when
26342 rs6000_default_long_calls is true. */
26343 static void
26344 rs6000_set_default_type_attributes (tree type)
26346 if (rs6000_default_long_calls
26347 && (TREE_CODE (type) == FUNCTION_TYPE
26348 || TREE_CODE (type) == METHOD_TYPE))
26349 TYPE_ATTRIBUTES (type) = tree_cons (get_identifier ("longcall"),
26350 NULL_TREE,
26351 TYPE_ATTRIBUTES (type));
26353 #if TARGET_MACHO
26354 darwin_set_default_type_attributes (type);
26355 #endif
26358 /* Return a reference suitable for calling a function with the
26359 longcall attribute. */
26362 rs6000_longcall_ref (rtx call_ref)
26364 const char *call_name;
26365 tree node;
26367 if (GET_CODE (call_ref) != SYMBOL_REF)
26368 return call_ref;
26370 /* System V adds '.' to the internal name, so skip them. */
26371 call_name = XSTR (call_ref, 0);
26372 if (*call_name == '.')
26374 while (*call_name == '.')
26375 call_name++;
26377 node = get_identifier (call_name);
26378 call_ref = gen_rtx_SYMBOL_REF (VOIDmode, IDENTIFIER_POINTER (node));
26381 return force_reg (Pmode, call_ref);
26384 #ifndef TARGET_USE_MS_BITFIELD_LAYOUT
26385 #define TARGET_USE_MS_BITFIELD_LAYOUT 0
26386 #endif
26388 /* Handle a "ms_struct" or "gcc_struct" attribute; arguments as in
26389 struct attribute_spec.handler. */
26390 static tree
26391 rs6000_handle_struct_attribute (tree *node, tree name,
26392 tree args ATTRIBUTE_UNUSED,
26393 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
26395 tree *type = NULL;
26396 if (DECL_P (*node))
26398 if (TREE_CODE (*node) == TYPE_DECL)
26399 type = &TREE_TYPE (*node);
26401 else
26402 type = node;
26404 if (!(type && (TREE_CODE (*type) == RECORD_TYPE
26405 || TREE_CODE (*type) == UNION_TYPE)))
26407 warning (OPT_Wattributes, "%qE attribute ignored", name);
26408 *no_add_attrs = true;
26411 else if ((is_attribute_p ("ms_struct", name)
26412 && lookup_attribute ("gcc_struct", TYPE_ATTRIBUTES (*type)))
26413 || ((is_attribute_p ("gcc_struct", name)
26414 && lookup_attribute ("ms_struct", TYPE_ATTRIBUTES (*type)))))
26416 warning (OPT_Wattributes, "%qE incompatible attribute ignored",
26417 name);
26418 *no_add_attrs = true;
26421 return NULL_TREE;
26424 static bool
26425 rs6000_ms_bitfield_layout_p (const_tree record_type)
26427 return (TARGET_USE_MS_BITFIELD_LAYOUT &&
26428 !lookup_attribute ("gcc_struct", TYPE_ATTRIBUTES (record_type)))
26429 || lookup_attribute ("ms_struct", TYPE_ATTRIBUTES (record_type));
26432 #ifdef USING_ELFOS_H
26434 /* A get_unnamed_section callback, used for switching to toc_section. */
26436 static void
26437 rs6000_elf_output_toc_section_asm_op (const void *data ATTRIBUTE_UNUSED)
26439 if (DEFAULT_ABI == ABI_AIX
26440 && TARGET_MINIMAL_TOC
26441 && !TARGET_RELOCATABLE)
26443 if (!toc_initialized)
26445 toc_initialized = 1;
26446 fprintf (asm_out_file, "%s\n", TOC_SECTION_ASM_OP);
26447 (*targetm.asm_out.internal_label) (asm_out_file, "LCTOC", 0);
26448 fprintf (asm_out_file, "\t.tc ");
26449 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (asm_out_file, "LCTOC1[TC],");
26450 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (asm_out_file, "LCTOC1");
26451 fprintf (asm_out_file, "\n");
26453 fprintf (asm_out_file, "%s\n", MINIMAL_TOC_SECTION_ASM_OP);
26454 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (asm_out_file, "LCTOC1");
26455 fprintf (asm_out_file, " = .+32768\n");
26457 else
26458 fprintf (asm_out_file, "%s\n", MINIMAL_TOC_SECTION_ASM_OP);
26460 else if (DEFAULT_ABI == ABI_AIX && !TARGET_RELOCATABLE)
26461 fprintf (asm_out_file, "%s\n", TOC_SECTION_ASM_OP);
26462 else
26464 fprintf (asm_out_file, "%s\n", MINIMAL_TOC_SECTION_ASM_OP);
26465 if (!toc_initialized)
26467 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (asm_out_file, "LCTOC1");
26468 fprintf (asm_out_file, " = .+32768\n");
26469 toc_initialized = 1;
26474 /* Implement TARGET_ASM_INIT_SECTIONS. */
26476 static void
26477 rs6000_elf_asm_init_sections (void)
26479 toc_section
26480 = get_unnamed_section (0, rs6000_elf_output_toc_section_asm_op, NULL);
26482 sdata2_section
26483 = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
26484 SDATA2_SECTION_ASM_OP);
26487 /* Implement TARGET_SELECT_RTX_SECTION. */
26489 static section *
26490 rs6000_elf_select_rtx_section (enum machine_mode mode, rtx x,
26491 unsigned HOST_WIDE_INT align)
26493 if (ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (x, mode))
26494 return toc_section;
26495 else
26496 return default_elf_select_rtx_section (mode, x, align);
26499 /* For a SYMBOL_REF, set generic flags and then perform some
26500 target-specific processing.
26502 When the AIX ABI is requested on a non-AIX system, replace the
26503 function name with the real name (with a leading .) rather than the
26504 function descriptor name. This saves a lot of overriding code to
26505 read the prefixes. */
26507 static void rs6000_elf_encode_section_info (tree, rtx, int) ATTRIBUTE_UNUSED;
26508 static void
26509 rs6000_elf_encode_section_info (tree decl, rtx rtl, int first)
26511 default_encode_section_info (decl, rtl, first);
26513 if (first
26514 && TREE_CODE (decl) == FUNCTION_DECL
26515 && !TARGET_AIX
26516 && DEFAULT_ABI == ABI_AIX)
26518 rtx sym_ref = XEXP (rtl, 0);
26519 size_t len = strlen (XSTR (sym_ref, 0));
26520 char *str = XALLOCAVEC (char, len + 2);
26521 str[0] = '.';
26522 memcpy (str + 1, XSTR (sym_ref, 0), len + 1);
26523 XSTR (sym_ref, 0) = ggc_alloc_string (str, len + 1);
26527 static inline bool
26528 compare_section_name (const char *section, const char *templ)
26530 int len;
26532 len = strlen (templ);
26533 return (strncmp (section, templ, len) == 0
26534 && (section[len] == 0 || section[len] == '.'));
26537 bool
26538 rs6000_elf_in_small_data_p (const_tree decl)
26540 if (rs6000_sdata == SDATA_NONE)
26541 return false;
26543 /* We want to merge strings, so we never consider them small data. */
26544 if (TREE_CODE (decl) == STRING_CST)
26545 return false;
26547 /* Functions are never in the small data area. */
26548 if (TREE_CODE (decl) == FUNCTION_DECL)
26549 return false;
26551 if (TREE_CODE (decl) == VAR_DECL && DECL_SECTION_NAME (decl))
26553 const char *section = TREE_STRING_POINTER (DECL_SECTION_NAME (decl));
26554 if (compare_section_name (section, ".sdata")
26555 || compare_section_name (section, ".sdata2")
26556 || compare_section_name (section, ".gnu.linkonce.s")
26557 || compare_section_name (section, ".sbss")
26558 || compare_section_name (section, ".sbss2")
26559 || compare_section_name (section, ".gnu.linkonce.sb")
26560 || strcmp (section, ".PPC.EMB.sdata0") == 0
26561 || strcmp (section, ".PPC.EMB.sbss0") == 0)
26562 return true;
26564 else
26566 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (decl));
26568 if (size > 0
26569 && size <= g_switch_value
26570 /* If it's not public, and we're not going to reference it there,
26571 there's no need to put it in the small data section. */
26572 && (rs6000_sdata != SDATA_DATA || TREE_PUBLIC (decl)))
26573 return true;
26576 return false;
26579 #endif /* USING_ELFOS_H */
26581 /* Implement TARGET_USE_BLOCKS_FOR_CONSTANT_P. */
26583 static bool
26584 rs6000_use_blocks_for_constant_p (enum machine_mode mode, const_rtx x)
26586 return !ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (x, mode);
26589 /* Do not place thread-local symbols refs in the object blocks. */
26591 static bool
26592 rs6000_use_blocks_for_decl_p (const_tree decl)
26594 return !DECL_THREAD_LOCAL_P (decl);
26597 /* Return a REG that occurs in ADDR with coefficient 1.
26598 ADDR can be effectively incremented by incrementing REG.
26600 r0 is special and we must not select it as an address
26601 register by this routine since our caller will try to
26602 increment the returned register via an "la" instruction. */
26605 find_addr_reg (rtx addr)
26607 while (GET_CODE (addr) == PLUS)
26609 if (GET_CODE (XEXP (addr, 0)) == REG
26610 && REGNO (XEXP (addr, 0)) != 0)
26611 addr = XEXP (addr, 0);
26612 else if (GET_CODE (XEXP (addr, 1)) == REG
26613 && REGNO (XEXP (addr, 1)) != 0)
26614 addr = XEXP (addr, 1);
26615 else if (CONSTANT_P (XEXP (addr, 0)))
26616 addr = XEXP (addr, 1);
26617 else if (CONSTANT_P (XEXP (addr, 1)))
26618 addr = XEXP (addr, 0);
26619 else
26620 gcc_unreachable ();
26622 gcc_assert (GET_CODE (addr) == REG && REGNO (addr) != 0);
26623 return addr;
26626 void
26627 rs6000_fatal_bad_address (rtx op)
26629 fatal_insn ("bad address", op);
26632 #if TARGET_MACHO
26634 typedef struct branch_island_d {
26635 tree function_name;
26636 tree label_name;
26637 int line_number;
26638 } branch_island;
26641 static vec<branch_island, va_gc> *branch_islands;
26643 /* Remember to generate a branch island for far calls to the given
26644 function. */
26646 static void
26647 add_compiler_branch_island (tree label_name, tree function_name,
26648 int line_number)
26650 branch_island bi = {function_name, label_name, line_number};
26651 vec_safe_push (branch_islands, bi);
26654 /* Generate far-jump branch islands for everything recorded in
26655 branch_islands. Invoked immediately after the last instruction of
26656 the epilogue has been emitted; the branch islands must be appended
26657 to, and contiguous with, the function body. Mach-O stubs are
26658 generated in machopic_output_stub(). */
26660 static void
26661 macho_branch_islands (void)
26663 char tmp_buf[512];
26665 while (!vec_safe_is_empty (branch_islands))
26667 branch_island *bi = &branch_islands->last ();
26668 const char *label = IDENTIFIER_POINTER (bi->label_name);
26669 const char *name = IDENTIFIER_POINTER (bi->function_name);
26670 char name_buf[512];
26671 /* Cheap copy of the details from the Darwin ASM_OUTPUT_LABELREF(). */
26672 if (name[0] == '*' || name[0] == '&')
26673 strcpy (name_buf, name+1);
26674 else
26676 name_buf[0] = '_';
26677 strcpy (name_buf+1, name);
26679 strcpy (tmp_buf, "\n");
26680 strcat (tmp_buf, label);
26681 #if defined (DBX_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)
26682 if (write_symbols == DBX_DEBUG || write_symbols == XCOFF_DEBUG)
26683 dbxout_stabd (N_SLINE, bi->line_number);
26684 #endif /* DBX_DEBUGGING_INFO || XCOFF_DEBUGGING_INFO */
26685 if (flag_pic)
26687 if (TARGET_LINK_STACK)
26689 char name[32];
26690 get_ppc476_thunk_name (name);
26691 strcat (tmp_buf, ":\n\tmflr r0\n\tbl ");
26692 strcat (tmp_buf, name);
26693 strcat (tmp_buf, "\n");
26694 strcat (tmp_buf, label);
26695 strcat (tmp_buf, "_pic:\n\tmflr r11\n");
26697 else
26699 strcat (tmp_buf, ":\n\tmflr r0\n\tbcl 20,31,");
26700 strcat (tmp_buf, label);
26701 strcat (tmp_buf, "_pic\n");
26702 strcat (tmp_buf, label);
26703 strcat (tmp_buf, "_pic:\n\tmflr r11\n");
26706 strcat (tmp_buf, "\taddis r11,r11,ha16(");
26707 strcat (tmp_buf, name_buf);
26708 strcat (tmp_buf, " - ");
26709 strcat (tmp_buf, label);
26710 strcat (tmp_buf, "_pic)\n");
26712 strcat (tmp_buf, "\tmtlr r0\n");
26714 strcat (tmp_buf, "\taddi r12,r11,lo16(");
26715 strcat (tmp_buf, name_buf);
26716 strcat (tmp_buf, " - ");
26717 strcat (tmp_buf, label);
26718 strcat (tmp_buf, "_pic)\n");
26720 strcat (tmp_buf, "\tmtctr r12\n\tbctr\n");
26722 else
26724 strcat (tmp_buf, ":\nlis r12,hi16(");
26725 strcat (tmp_buf, name_buf);
26726 strcat (tmp_buf, ")\n\tori r12,r12,lo16(");
26727 strcat (tmp_buf, name_buf);
26728 strcat (tmp_buf, ")\n\tmtctr r12\n\tbctr");
26730 output_asm_insn (tmp_buf, 0);
26731 #if defined (DBX_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)
26732 if (write_symbols == DBX_DEBUG || write_symbols == XCOFF_DEBUG)
26733 dbxout_stabd (N_SLINE, bi->line_number);
26734 #endif /* DBX_DEBUGGING_INFO || XCOFF_DEBUGGING_INFO */
26735 branch_islands->pop ();
26739 /* NO_PREVIOUS_DEF checks in the link list whether the function name is
26740 already there or not. */
26742 static int
26743 no_previous_def (tree function_name)
26745 branch_island *bi;
26746 unsigned ix;
26748 FOR_EACH_VEC_SAFE_ELT (branch_islands, ix, bi)
26749 if (function_name == bi->function_name)
26750 return 0;
26751 return 1;
26754 /* GET_PREV_LABEL gets the label name from the previous definition of
26755 the function. */
26757 static tree
26758 get_prev_label (tree function_name)
26760 branch_island *bi;
26761 unsigned ix;
26763 FOR_EACH_VEC_SAFE_ELT (branch_islands, ix, bi)
26764 if (function_name == bi->function_name)
26765 return bi->label_name;
26766 return NULL_TREE;
26769 /* INSN is either a function call or a millicode call. It may have an
26770 unconditional jump in its delay slot.
26772 CALL_DEST is the routine we are calling. */
26774 char *
26775 output_call (rtx insn, rtx *operands, int dest_operand_number,
26776 int cookie_operand_number)
26778 static char buf[256];
26779 if (darwin_emit_branch_islands
26780 && GET_CODE (operands[dest_operand_number]) == SYMBOL_REF
26781 && (INTVAL (operands[cookie_operand_number]) & CALL_LONG))
26783 tree labelname;
26784 tree funname = get_identifier (XSTR (operands[dest_operand_number], 0));
26786 if (no_previous_def (funname))
26788 rtx label_rtx = gen_label_rtx ();
26789 char *label_buf, temp_buf[256];
26790 ASM_GENERATE_INTERNAL_LABEL (temp_buf, "L",
26791 CODE_LABEL_NUMBER (label_rtx));
26792 label_buf = temp_buf[0] == '*' ? temp_buf + 1 : temp_buf;
26793 labelname = get_identifier (label_buf);
26794 add_compiler_branch_island (labelname, funname, insn_line (insn));
26796 else
26797 labelname = get_prev_label (funname);
26799 /* "jbsr foo, L42" is Mach-O for "Link as 'bl foo' if a 'bl'
26800 instruction will reach 'foo', otherwise link as 'bl L42'".
26801 "L42" should be a 'branch island', that will do a far jump to
26802 'foo'. Branch islands are generated in
26803 macho_branch_islands(). */
26804 sprintf (buf, "jbsr %%z%d,%.246s",
26805 dest_operand_number, IDENTIFIER_POINTER (labelname));
26807 else
26808 sprintf (buf, "bl %%z%d", dest_operand_number);
26809 return buf;
26812 /* Generate PIC and indirect symbol stubs. */
26814 void
26815 machopic_output_stub (FILE *file, const char *symb, const char *stub)
26817 unsigned int length;
26818 char *symbol_name, *lazy_ptr_name;
26819 char *local_label_0;
26820 static int label = 0;
26822 /* Lose our funky encoding stuff so it doesn't contaminate the stub. */
26823 symb = (*targetm.strip_name_encoding) (symb);
26826 length = strlen (symb);
26827 symbol_name = XALLOCAVEC (char, length + 32);
26828 GEN_SYMBOL_NAME_FOR_SYMBOL (symbol_name, symb, length);
26830 lazy_ptr_name = XALLOCAVEC (char, length + 32);
26831 GEN_LAZY_PTR_NAME_FOR_SYMBOL (lazy_ptr_name, symb, length);
26833 if (flag_pic == 2)
26834 switch_to_section (darwin_sections[machopic_picsymbol_stub1_section]);
26835 else
26836 switch_to_section (darwin_sections[machopic_symbol_stub1_section]);
26838 if (flag_pic == 2)
26840 fprintf (file, "\t.align 5\n");
26842 fprintf (file, "%s:\n", stub);
26843 fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
26845 label++;
26846 local_label_0 = XALLOCAVEC (char, sizeof ("\"L00000000000$spb\""));
26847 sprintf (local_label_0, "\"L%011d$spb\"", label);
26849 fprintf (file, "\tmflr r0\n");
26850 if (TARGET_LINK_STACK)
26852 char name[32];
26853 get_ppc476_thunk_name (name);
26854 fprintf (file, "\tbl %s\n", name);
26855 fprintf (file, "%s:\n\tmflr r11\n", local_label_0);
26857 else
26859 fprintf (file, "\tbcl 20,31,%s\n", local_label_0);
26860 fprintf (file, "%s:\n\tmflr r11\n", local_label_0);
26862 fprintf (file, "\taddis r11,r11,ha16(%s-%s)\n",
26863 lazy_ptr_name, local_label_0);
26864 fprintf (file, "\tmtlr r0\n");
26865 fprintf (file, "\t%s r12,lo16(%s-%s)(r11)\n",
26866 (TARGET_64BIT ? "ldu" : "lwzu"),
26867 lazy_ptr_name, local_label_0);
26868 fprintf (file, "\tmtctr r12\n");
26869 fprintf (file, "\tbctr\n");
26871 else
26873 fprintf (file, "\t.align 4\n");
26875 fprintf (file, "%s:\n", stub);
26876 fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
26878 fprintf (file, "\tlis r11,ha16(%s)\n", lazy_ptr_name);
26879 fprintf (file, "\t%s r12,lo16(%s)(r11)\n",
26880 (TARGET_64BIT ? "ldu" : "lwzu"),
26881 lazy_ptr_name);
26882 fprintf (file, "\tmtctr r12\n");
26883 fprintf (file, "\tbctr\n");
26886 switch_to_section (darwin_sections[machopic_lazy_symbol_ptr_section]);
26887 fprintf (file, "%s:\n", lazy_ptr_name);
26888 fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
26889 fprintf (file, "%sdyld_stub_binding_helper\n",
26890 (TARGET_64BIT ? DOUBLE_INT_ASM_OP : "\t.long\t"));
26893 /* Legitimize PIC addresses. If the address is already
26894 position-independent, we return ORIG. Newly generated
26895 position-independent addresses go into a reg. This is REG if non
26896 zero, otherwise we allocate register(s) as necessary. */
26898 #define SMALL_INT(X) ((UINTVAL (X) + 0x8000) < 0x10000)
26901 rs6000_machopic_legitimize_pic_address (rtx orig, enum machine_mode mode,
26902 rtx reg)
26904 rtx base, offset;
26906 if (reg == NULL && ! reload_in_progress && ! reload_completed)
26907 reg = gen_reg_rtx (Pmode);
26909 if (GET_CODE (orig) == CONST)
26911 rtx reg_temp;
26913 if (GET_CODE (XEXP (orig, 0)) == PLUS
26914 && XEXP (XEXP (orig, 0), 0) == pic_offset_table_rtx)
26915 return orig;
26917 gcc_assert (GET_CODE (XEXP (orig, 0)) == PLUS);
26919 /* Use a different reg for the intermediate value, as
26920 it will be marked UNCHANGING. */
26921 reg_temp = !can_create_pseudo_p () ? reg : gen_reg_rtx (Pmode);
26922 base = rs6000_machopic_legitimize_pic_address (XEXP (XEXP (orig, 0), 0),
26923 Pmode, reg_temp);
26924 offset =
26925 rs6000_machopic_legitimize_pic_address (XEXP (XEXP (orig, 0), 1),
26926 Pmode, reg);
26928 if (GET_CODE (offset) == CONST_INT)
26930 if (SMALL_INT (offset))
26931 return plus_constant (Pmode, base, INTVAL (offset));
26932 else if (! reload_in_progress && ! reload_completed)
26933 offset = force_reg (Pmode, offset);
26934 else
26936 rtx mem = force_const_mem (Pmode, orig);
26937 return machopic_legitimize_pic_address (mem, Pmode, reg);
26940 return gen_rtx_PLUS (Pmode, base, offset);
26943 /* Fall back on generic machopic code. */
26944 return machopic_legitimize_pic_address (orig, mode, reg);
26947 /* Output a .machine directive for the Darwin assembler, and call
26948 the generic start_file routine. */
26950 static void
26951 rs6000_darwin_file_start (void)
26953 static const struct
26955 const char *arg;
26956 const char *name;
26957 HOST_WIDE_INT if_set;
26958 } mapping[] = {
26959 { "ppc64", "ppc64", MASK_64BIT },
26960 { "970", "ppc970", MASK_PPC_GPOPT | MASK_MFCRF | MASK_POWERPC64 },
26961 { "power4", "ppc970", 0 },
26962 { "G5", "ppc970", 0 },
26963 { "7450", "ppc7450", 0 },
26964 { "7400", "ppc7400", MASK_ALTIVEC },
26965 { "G4", "ppc7400", 0 },
26966 { "750", "ppc750", 0 },
26967 { "740", "ppc750", 0 },
26968 { "G3", "ppc750", 0 },
26969 { "604e", "ppc604e", 0 },
26970 { "604", "ppc604", 0 },
26971 { "603e", "ppc603", 0 },
26972 { "603", "ppc603", 0 },
26973 { "601", "ppc601", 0 },
26974 { NULL, "ppc", 0 } };
26975 const char *cpu_id = "";
26976 size_t i;
26978 rs6000_file_start ();
26979 darwin_file_start ();
26981 /* Determine the argument to -mcpu=. Default to G3 if not specified. */
26983 if (rs6000_default_cpu != 0 && rs6000_default_cpu[0] != '\0')
26984 cpu_id = rs6000_default_cpu;
26986 if (global_options_set.x_rs6000_cpu_index)
26987 cpu_id = processor_target_table[rs6000_cpu_index].name;
26989 /* Look through the mapping array. Pick the first name that either
26990 matches the argument, has a bit set in IF_SET that is also set
26991 in the target flags, or has a NULL name. */
26993 i = 0;
26994 while (mapping[i].arg != NULL
26995 && strcmp (mapping[i].arg, cpu_id) != 0
26996 && (mapping[i].if_set & rs6000_isa_flags) == 0)
26997 i++;
26999 fprintf (asm_out_file, "\t.machine %s\n", mapping[i].name);
27002 #endif /* TARGET_MACHO */
27004 #if TARGET_ELF
27005 static int
27006 rs6000_elf_reloc_rw_mask (void)
27008 if (flag_pic)
27009 return 3;
27010 else if (DEFAULT_ABI == ABI_AIX)
27011 return 2;
27012 else
27013 return 0;
27016 /* Record an element in the table of global constructors. SYMBOL is
27017 a SYMBOL_REF of the function to be called; PRIORITY is a number
27018 between 0 and MAX_INIT_PRIORITY.
27020 This differs from default_named_section_asm_out_constructor in
27021 that we have special handling for -mrelocatable. */
27023 static void rs6000_elf_asm_out_constructor (rtx, int) ATTRIBUTE_UNUSED;
27024 static void
27025 rs6000_elf_asm_out_constructor (rtx symbol, int priority)
27027 const char *section = ".ctors";
27028 char buf[16];
27030 if (priority != DEFAULT_INIT_PRIORITY)
27032 sprintf (buf, ".ctors.%.5u",
27033 /* Invert the numbering so the linker puts us in the proper
27034 order; constructors are run from right to left, and the
27035 linker sorts in increasing order. */
27036 MAX_INIT_PRIORITY - priority);
27037 section = buf;
27040 switch_to_section (get_section (section, SECTION_WRITE, NULL));
27041 assemble_align (POINTER_SIZE);
27043 if (TARGET_RELOCATABLE)
27045 fputs ("\t.long (", asm_out_file);
27046 output_addr_const (asm_out_file, symbol);
27047 fputs (")@fixup\n", asm_out_file);
27049 else
27050 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
27053 static void rs6000_elf_asm_out_destructor (rtx, int) ATTRIBUTE_UNUSED;
27054 static void
27055 rs6000_elf_asm_out_destructor (rtx symbol, int priority)
27057 const char *section = ".dtors";
27058 char buf[16];
27060 if (priority != DEFAULT_INIT_PRIORITY)
27062 sprintf (buf, ".dtors.%.5u",
27063 /* Invert the numbering so the linker puts us in the proper
27064 order; constructors are run from right to left, and the
27065 linker sorts in increasing order. */
27066 MAX_INIT_PRIORITY - priority);
27067 section = buf;
27070 switch_to_section (get_section (section, SECTION_WRITE, NULL));
27071 assemble_align (POINTER_SIZE);
27073 if (TARGET_RELOCATABLE)
27075 fputs ("\t.long (", asm_out_file);
27076 output_addr_const (asm_out_file, symbol);
27077 fputs (")@fixup\n", asm_out_file);
27079 else
27080 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
27083 void
27084 rs6000_elf_declare_function_name (FILE *file, const char *name, tree decl)
27086 if (TARGET_64BIT)
27088 fputs ("\t.section\t\".opd\",\"aw\"\n\t.align 3\n", file);
27089 ASM_OUTPUT_LABEL (file, name);
27090 fputs (DOUBLE_INT_ASM_OP, file);
27091 rs6000_output_function_entry (file, name);
27092 fputs (",.TOC.@tocbase,0\n\t.previous\n", file);
27093 if (DOT_SYMBOLS)
27095 fputs ("\t.size\t", file);
27096 assemble_name (file, name);
27097 fputs (",24\n\t.type\t.", file);
27098 assemble_name (file, name);
27099 fputs (",@function\n", file);
27100 if (TREE_PUBLIC (decl) && ! DECL_WEAK (decl))
27102 fputs ("\t.globl\t.", file);
27103 assemble_name (file, name);
27104 putc ('\n', file);
27107 else
27108 ASM_OUTPUT_TYPE_DIRECTIVE (file, name, "function");
27109 ASM_DECLARE_RESULT (file, DECL_RESULT (decl));
27110 rs6000_output_function_entry (file, name);
27111 fputs (":\n", file);
27112 return;
27115 if (TARGET_RELOCATABLE
27116 && !TARGET_SECURE_PLT
27117 && (get_pool_size () != 0 || crtl->profile)
27118 && uses_TOC ())
27120 char buf[256];
27122 (*targetm.asm_out.internal_label) (file, "LCL", rs6000_pic_labelno);
27124 ASM_GENERATE_INTERNAL_LABEL (buf, "LCTOC", 1);
27125 fprintf (file, "\t.long ");
27126 assemble_name (file, buf);
27127 putc ('-', file);
27128 ASM_GENERATE_INTERNAL_LABEL (buf, "LCF", rs6000_pic_labelno);
27129 assemble_name (file, buf);
27130 putc ('\n', file);
27133 ASM_OUTPUT_TYPE_DIRECTIVE (file, name, "function");
27134 ASM_DECLARE_RESULT (file, DECL_RESULT (decl));
27136 if (DEFAULT_ABI == ABI_AIX)
27138 const char *desc_name, *orig_name;
27140 orig_name = (*targetm.strip_name_encoding) (name);
27141 desc_name = orig_name;
27142 while (*desc_name == '.')
27143 desc_name++;
27145 if (TREE_PUBLIC (decl))
27146 fprintf (file, "\t.globl %s\n", desc_name);
27148 fprintf (file, "%s\n", MINIMAL_TOC_SECTION_ASM_OP);
27149 fprintf (file, "%s:\n", desc_name);
27150 fprintf (file, "\t.long %s\n", orig_name);
27151 fputs ("\t.long _GLOBAL_OFFSET_TABLE_\n", file);
27152 if (DEFAULT_ABI == ABI_AIX)
27153 fputs ("\t.long 0\n", file);
27154 fprintf (file, "\t.previous\n");
27156 ASM_OUTPUT_LABEL (file, name);
27159 static void rs6000_elf_file_end (void) ATTRIBUTE_UNUSED;
27160 static void
27161 rs6000_elf_file_end (void)
27163 #ifdef HAVE_AS_GNU_ATTRIBUTE
27164 if (TARGET_32BIT && DEFAULT_ABI == ABI_V4)
27166 if (rs6000_passes_float)
27167 fprintf (asm_out_file, "\t.gnu_attribute 4, %d\n",
27168 ((TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT) ? 1
27169 : (TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_SINGLE_FLOAT) ? 3
27170 : 2));
27171 if (rs6000_passes_vector)
27172 fprintf (asm_out_file, "\t.gnu_attribute 8, %d\n",
27173 (TARGET_ALTIVEC_ABI ? 2
27174 : TARGET_SPE_ABI ? 3
27175 : 1));
27176 if (rs6000_returns_struct)
27177 fprintf (asm_out_file, "\t.gnu_attribute 12, %d\n",
27178 aix_struct_return ? 2 : 1);
27180 #endif
27181 #if defined (POWERPC_LINUX) || defined (POWERPC_FREEBSD)
27182 if (TARGET_32BIT)
27183 file_end_indicate_exec_stack ();
27184 #endif
27186 #endif
27188 #if TARGET_XCOFF
27189 static void
27190 rs6000_xcoff_asm_output_anchor (rtx symbol)
27192 char buffer[100];
27194 sprintf (buffer, "$ + " HOST_WIDE_INT_PRINT_DEC,
27195 SYMBOL_REF_BLOCK_OFFSET (symbol));
27196 ASM_OUTPUT_DEF (asm_out_file, XSTR (symbol, 0), buffer);
27199 static void
27200 rs6000_xcoff_asm_globalize_label (FILE *stream, const char *name)
27202 fputs (GLOBAL_ASM_OP, stream);
27203 RS6000_OUTPUT_BASENAME (stream, name);
27204 putc ('\n', stream);
27207 /* A get_unnamed_decl callback, used for read-only sections. PTR
27208 points to the section string variable. */
27210 static void
27211 rs6000_xcoff_output_readonly_section_asm_op (const void *directive)
27213 fprintf (asm_out_file, "\t.csect %s[RO],%s\n",
27214 *(const char *const *) directive,
27215 XCOFF_CSECT_DEFAULT_ALIGNMENT_STR);
27218 /* Likewise for read-write sections. */
27220 static void
27221 rs6000_xcoff_output_readwrite_section_asm_op (const void *directive)
27223 fprintf (asm_out_file, "\t.csect %s[RW],%s\n",
27224 *(const char *const *) directive,
27225 XCOFF_CSECT_DEFAULT_ALIGNMENT_STR);
27228 static void
27229 rs6000_xcoff_output_tls_section_asm_op (const void *directive)
27231 fprintf (asm_out_file, "\t.csect %s[TL],%s\n",
27232 *(const char *const *) directive,
27233 XCOFF_CSECT_DEFAULT_ALIGNMENT_STR);
27236 /* A get_unnamed_section callback, used for switching to toc_section. */
27238 static void
27239 rs6000_xcoff_output_toc_section_asm_op (const void *data ATTRIBUTE_UNUSED)
27241 if (TARGET_MINIMAL_TOC)
27243 /* toc_section is always selected at least once from
27244 rs6000_xcoff_file_start, so this is guaranteed to
27245 always be defined once and only once in each file. */
27246 if (!toc_initialized)
27248 fputs ("\t.toc\nLCTOC..1:\n", asm_out_file);
27249 fputs ("\t.tc toc_table[TC],toc_table[RW]\n", asm_out_file);
27250 toc_initialized = 1;
27252 fprintf (asm_out_file, "\t.csect toc_table[RW]%s\n",
27253 (TARGET_32BIT ? "" : ",3"));
27255 else
27256 fputs ("\t.toc\n", asm_out_file);
27259 /* Implement TARGET_ASM_INIT_SECTIONS. */
27261 static void
27262 rs6000_xcoff_asm_init_sections (void)
27264 read_only_data_section
27265 = get_unnamed_section (0, rs6000_xcoff_output_readonly_section_asm_op,
27266 &xcoff_read_only_section_name);
27268 private_data_section
27269 = get_unnamed_section (SECTION_WRITE,
27270 rs6000_xcoff_output_readwrite_section_asm_op,
27271 &xcoff_private_data_section_name);
27273 tls_data_section
27274 = get_unnamed_section (SECTION_TLS,
27275 rs6000_xcoff_output_tls_section_asm_op,
27276 &xcoff_tls_data_section_name);
27278 tls_private_data_section
27279 = get_unnamed_section (SECTION_TLS,
27280 rs6000_xcoff_output_tls_section_asm_op,
27281 &xcoff_private_data_section_name);
27283 read_only_private_data_section
27284 = get_unnamed_section (0, rs6000_xcoff_output_readonly_section_asm_op,
27285 &xcoff_private_data_section_name);
27287 toc_section
27288 = get_unnamed_section (0, rs6000_xcoff_output_toc_section_asm_op, NULL);
27290 readonly_data_section = read_only_data_section;
27291 exception_section = data_section;
27294 static int
27295 rs6000_xcoff_reloc_rw_mask (void)
27297 return 3;
27300 static void
27301 rs6000_xcoff_asm_named_section (const char *name, unsigned int flags,
27302 tree decl ATTRIBUTE_UNUSED)
27304 int smclass;
27305 static const char * const suffix[4] = { "PR", "RO", "RW", "TL" };
27307 if (flags & SECTION_CODE)
27308 smclass = 0;
27309 else if (flags & SECTION_TLS)
27310 smclass = 3;
27311 else if (flags & SECTION_WRITE)
27312 smclass = 2;
27313 else
27314 smclass = 1;
27316 fprintf (asm_out_file, "\t.csect %s%s[%s],%u\n",
27317 (flags & SECTION_CODE) ? "." : "",
27318 name, suffix[smclass], flags & SECTION_ENTSIZE);
27321 static section *
27322 rs6000_xcoff_select_section (tree decl, int reloc,
27323 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
27325 if (decl_readonly_section (decl, reloc))
27327 if (TREE_PUBLIC (decl))
27328 return read_only_data_section;
27329 else
27330 return read_only_private_data_section;
27332 else
27334 #if HAVE_AS_TLS
27335 if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
27337 if (TREE_PUBLIC (decl))
27338 return tls_data_section;
27339 else if (bss_initializer_p (decl))
27341 /* Convert to COMMON to emit in BSS. */
27342 DECL_COMMON (decl) = 1;
27343 return tls_comm_section;
27345 else
27346 return tls_private_data_section;
27348 else
27349 #endif
27350 if (TREE_PUBLIC (decl))
27351 return data_section;
27352 else
27353 return private_data_section;
27357 static void
27358 rs6000_xcoff_unique_section (tree decl, int reloc ATTRIBUTE_UNUSED)
27360 const char *name;
27362 /* Use select_section for private and uninitialized data. */
27363 if (!TREE_PUBLIC (decl)
27364 || DECL_COMMON (decl)
27365 || DECL_INITIAL (decl) == NULL_TREE
27366 || DECL_INITIAL (decl) == error_mark_node
27367 || (flag_zero_initialized_in_bss
27368 && initializer_zerop (DECL_INITIAL (decl))))
27369 return;
27371 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
27372 name = (*targetm.strip_name_encoding) (name);
27373 DECL_SECTION_NAME (decl) = build_string (strlen (name), name);
27376 /* Select section for constant in constant pool.
27378 On RS/6000, all constants are in the private read-only data area.
27379 However, if this is being placed in the TOC it must be output as a
27380 toc entry. */
27382 static section *
27383 rs6000_xcoff_select_rtx_section (enum machine_mode mode, rtx x,
27384 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
27386 if (ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (x, mode))
27387 return toc_section;
27388 else
27389 return read_only_private_data_section;
27392 /* Remove any trailing [DS] or the like from the symbol name. */
27394 static const char *
27395 rs6000_xcoff_strip_name_encoding (const char *name)
27397 size_t len;
27398 if (*name == '*')
27399 name++;
27400 len = strlen (name);
27401 if (name[len - 1] == ']')
27402 return ggc_alloc_string (name, len - 4);
27403 else
27404 return name;
27407 /* Section attributes. AIX is always PIC. */
27409 static unsigned int
27410 rs6000_xcoff_section_type_flags (tree decl, const char *name, int reloc)
27412 unsigned int align;
27413 unsigned int flags = default_section_type_flags (decl, name, reloc);
27415 /* Align to at least UNIT size. */
27416 if ((flags & SECTION_CODE) != 0 || !decl || !DECL_P (decl))
27417 align = MIN_UNITS_PER_WORD;
27418 else
27419 /* Increase alignment of large objects if not already stricter. */
27420 align = MAX ((DECL_ALIGN (decl) / BITS_PER_UNIT),
27421 int_size_in_bytes (TREE_TYPE (decl)) > MIN_UNITS_PER_WORD
27422 ? UNITS_PER_FP_WORD : MIN_UNITS_PER_WORD);
27424 return flags | (exact_log2 (align) & SECTION_ENTSIZE);
27427 /* Output at beginning of assembler file.
27429 Initialize the section names for the RS/6000 at this point.
27431 Specify filename, including full path, to assembler.
27433 We want to go into the TOC section so at least one .toc will be emitted.
27434 Also, in order to output proper .bs/.es pairs, we need at least one static
27435 [RW] section emitted.
27437 Finally, declare mcount when profiling to make the assembler happy. */
27439 static void
27440 rs6000_xcoff_file_start (void)
27442 rs6000_gen_section_name (&xcoff_bss_section_name,
27443 main_input_filename, ".bss_");
27444 rs6000_gen_section_name (&xcoff_private_data_section_name,
27445 main_input_filename, ".rw_");
27446 rs6000_gen_section_name (&xcoff_read_only_section_name,
27447 main_input_filename, ".ro_");
27448 rs6000_gen_section_name (&xcoff_tls_data_section_name,
27449 main_input_filename, ".tls_");
27450 rs6000_gen_section_name (&xcoff_tbss_section_name,
27451 main_input_filename, ".tbss_[UL]");
27453 fputs ("\t.file\t", asm_out_file);
27454 output_quoted_string (asm_out_file, main_input_filename);
27455 fputc ('\n', asm_out_file);
27456 if (write_symbols != NO_DEBUG)
27457 switch_to_section (private_data_section);
27458 switch_to_section (text_section);
27459 if (profile_flag)
27460 fprintf (asm_out_file, "\t.extern %s\n", RS6000_MCOUNT);
27461 rs6000_file_start ();
27464 /* Output at end of assembler file.
27465 On the RS/6000, referencing data should automatically pull in text. */
27467 static void
27468 rs6000_xcoff_file_end (void)
27470 switch_to_section (text_section);
27471 fputs ("_section_.text:\n", asm_out_file);
27472 switch_to_section (data_section);
27473 fputs (TARGET_32BIT
27474 ? "\t.long _section_.text\n" : "\t.llong _section_.text\n",
27475 asm_out_file);
27478 #ifdef HAVE_AS_TLS
27479 static void
27480 rs6000_xcoff_encode_section_info (tree decl, rtx rtl, int first)
27482 rtx symbol;
27483 int flags;
27485 default_encode_section_info (decl, rtl, first);
27487 /* Careful not to prod global register variables. */
27488 if (!MEM_P (rtl))
27489 return;
27490 symbol = XEXP (rtl, 0);
27491 if (GET_CODE (symbol) != SYMBOL_REF)
27492 return;
27494 flags = SYMBOL_REF_FLAGS (symbol);
27496 if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
27497 flags &= ~SYMBOL_FLAG_HAS_BLOCK_INFO;
27499 SYMBOL_REF_FLAGS (symbol) = flags;
27501 #endif /* HAVE_AS_TLS */
27502 #endif /* TARGET_XCOFF */
27504 /* Compute a (partial) cost for rtx X. Return true if the complete
27505 cost has been computed, and false if subexpressions should be
27506 scanned. In either case, *TOTAL contains the cost result. */
27508 static bool
27509 rs6000_rtx_costs (rtx x, int code, int outer_code, int opno ATTRIBUTE_UNUSED,
27510 int *total, bool speed)
27512 enum machine_mode mode = GET_MODE (x);
27514 switch (code)
27516 /* On the RS/6000, if it is valid in the insn, it is free. */
27517 case CONST_INT:
27518 if (((outer_code == SET
27519 || outer_code == PLUS
27520 || outer_code == MINUS)
27521 && (satisfies_constraint_I (x)
27522 || satisfies_constraint_L (x)))
27523 || (outer_code == AND
27524 && (satisfies_constraint_K (x)
27525 || (mode == SImode
27526 ? satisfies_constraint_L (x)
27527 : satisfies_constraint_J (x))
27528 || mask_operand (x, mode)
27529 || (mode == DImode
27530 && mask64_operand (x, DImode))))
27531 || ((outer_code == IOR || outer_code == XOR)
27532 && (satisfies_constraint_K (x)
27533 || (mode == SImode
27534 ? satisfies_constraint_L (x)
27535 : satisfies_constraint_J (x))))
27536 || outer_code == ASHIFT
27537 || outer_code == ASHIFTRT
27538 || outer_code == LSHIFTRT
27539 || outer_code == ROTATE
27540 || outer_code == ROTATERT
27541 || outer_code == ZERO_EXTRACT
27542 || (outer_code == MULT
27543 && satisfies_constraint_I (x))
27544 || ((outer_code == DIV || outer_code == UDIV
27545 || outer_code == MOD || outer_code == UMOD)
27546 && exact_log2 (INTVAL (x)) >= 0)
27547 || (outer_code == COMPARE
27548 && (satisfies_constraint_I (x)
27549 || satisfies_constraint_K (x)))
27550 || ((outer_code == EQ || outer_code == NE)
27551 && (satisfies_constraint_I (x)
27552 || satisfies_constraint_K (x)
27553 || (mode == SImode
27554 ? satisfies_constraint_L (x)
27555 : satisfies_constraint_J (x))))
27556 || (outer_code == GTU
27557 && satisfies_constraint_I (x))
27558 || (outer_code == LTU
27559 && satisfies_constraint_P (x)))
27561 *total = 0;
27562 return true;
27564 else if ((outer_code == PLUS
27565 && reg_or_add_cint_operand (x, VOIDmode))
27566 || (outer_code == MINUS
27567 && reg_or_sub_cint_operand (x, VOIDmode))
27568 || ((outer_code == SET
27569 || outer_code == IOR
27570 || outer_code == XOR)
27571 && (INTVAL (x)
27572 & ~ (unsigned HOST_WIDE_INT) 0xffffffff) == 0))
27574 *total = COSTS_N_INSNS (1);
27575 return true;
27577 /* FALLTHRU */
27579 case CONST_DOUBLE:
27580 case CONST:
27581 case HIGH:
27582 case SYMBOL_REF:
27583 case MEM:
27584 /* When optimizing for size, MEM should be slightly more expensive
27585 than generating address, e.g., (plus (reg) (const)).
27586 L1 cache latency is about two instructions. */
27587 *total = !speed ? COSTS_N_INSNS (1) + 1 : COSTS_N_INSNS (2);
27588 return true;
27590 case LABEL_REF:
27591 *total = 0;
27592 return true;
27594 case PLUS:
27595 case MINUS:
27596 if (FLOAT_MODE_P (mode))
27597 *total = rs6000_cost->fp;
27598 else
27599 *total = COSTS_N_INSNS (1);
27600 return false;
27602 case MULT:
27603 if (GET_CODE (XEXP (x, 1)) == CONST_INT
27604 && satisfies_constraint_I (XEXP (x, 1)))
27606 if (INTVAL (XEXP (x, 1)) >= -256
27607 && INTVAL (XEXP (x, 1)) <= 255)
27608 *total = rs6000_cost->mulsi_const9;
27609 else
27610 *total = rs6000_cost->mulsi_const;
27612 else if (mode == SFmode)
27613 *total = rs6000_cost->fp;
27614 else if (FLOAT_MODE_P (mode))
27615 *total = rs6000_cost->dmul;
27616 else if (mode == DImode)
27617 *total = rs6000_cost->muldi;
27618 else
27619 *total = rs6000_cost->mulsi;
27620 return false;
27622 case FMA:
27623 if (mode == SFmode)
27624 *total = rs6000_cost->fp;
27625 else
27626 *total = rs6000_cost->dmul;
27627 break;
27629 case DIV:
27630 case MOD:
27631 if (FLOAT_MODE_P (mode))
27633 *total = mode == DFmode ? rs6000_cost->ddiv
27634 : rs6000_cost->sdiv;
27635 return false;
27637 /* FALLTHRU */
27639 case UDIV:
27640 case UMOD:
27641 if (GET_CODE (XEXP (x, 1)) == CONST_INT
27642 && exact_log2 (INTVAL (XEXP (x, 1))) >= 0)
27644 if (code == DIV || code == MOD)
27645 /* Shift, addze */
27646 *total = COSTS_N_INSNS (2);
27647 else
27648 /* Shift */
27649 *total = COSTS_N_INSNS (1);
27651 else
27653 if (GET_MODE (XEXP (x, 1)) == DImode)
27654 *total = rs6000_cost->divdi;
27655 else
27656 *total = rs6000_cost->divsi;
27658 /* Add in shift and subtract for MOD. */
27659 if (code == MOD || code == UMOD)
27660 *total += COSTS_N_INSNS (2);
27661 return false;
27663 case CTZ:
27664 case FFS:
27665 *total = COSTS_N_INSNS (4);
27666 return false;
27668 case POPCOUNT:
27669 *total = COSTS_N_INSNS (TARGET_POPCNTD ? 1 : 6);
27670 return false;
27672 case PARITY:
27673 *total = COSTS_N_INSNS (TARGET_CMPB ? 2 : 6);
27674 return false;
27676 case NOT:
27677 if (outer_code == AND || outer_code == IOR || outer_code == XOR)
27679 *total = 0;
27680 return false;
27682 /* FALLTHRU */
27684 case AND:
27685 case CLZ:
27686 case IOR:
27687 case XOR:
27688 case ZERO_EXTRACT:
27689 *total = COSTS_N_INSNS (1);
27690 return false;
27692 case ASHIFT:
27693 case ASHIFTRT:
27694 case LSHIFTRT:
27695 case ROTATE:
27696 case ROTATERT:
27697 /* Handle mul_highpart. */
27698 if (outer_code == TRUNCATE
27699 && GET_CODE (XEXP (x, 0)) == MULT)
27701 if (mode == DImode)
27702 *total = rs6000_cost->muldi;
27703 else
27704 *total = rs6000_cost->mulsi;
27705 return true;
27707 else if (outer_code == AND)
27708 *total = 0;
27709 else
27710 *total = COSTS_N_INSNS (1);
27711 return false;
27713 case SIGN_EXTEND:
27714 case ZERO_EXTEND:
27715 if (GET_CODE (XEXP (x, 0)) == MEM)
27716 *total = 0;
27717 else
27718 *total = COSTS_N_INSNS (1);
27719 return false;
27721 case COMPARE:
27722 case NEG:
27723 case ABS:
27724 if (!FLOAT_MODE_P (mode))
27726 *total = COSTS_N_INSNS (1);
27727 return false;
27729 /* FALLTHRU */
27731 case FLOAT:
27732 case UNSIGNED_FLOAT:
27733 case FIX:
27734 case UNSIGNED_FIX:
27735 case FLOAT_TRUNCATE:
27736 *total = rs6000_cost->fp;
27737 return false;
27739 case FLOAT_EXTEND:
27740 if (mode == DFmode)
27741 *total = 0;
27742 else
27743 *total = rs6000_cost->fp;
27744 return false;
27746 case UNSPEC:
27747 switch (XINT (x, 1))
27749 case UNSPEC_FRSP:
27750 *total = rs6000_cost->fp;
27751 return true;
27753 default:
27754 break;
27756 break;
27758 case CALL:
27759 case IF_THEN_ELSE:
27760 if (!speed)
27762 *total = COSTS_N_INSNS (1);
27763 return true;
27765 else if (FLOAT_MODE_P (mode)
27766 && TARGET_PPC_GFXOPT && TARGET_HARD_FLOAT && TARGET_FPRS)
27768 *total = rs6000_cost->fp;
27769 return false;
27771 break;
27773 case EQ:
27774 case GTU:
27775 case LTU:
27776 /* Carry bit requires mode == Pmode.
27777 NEG or PLUS already counted so only add one. */
27778 if (mode == Pmode
27779 && (outer_code == NEG || outer_code == PLUS))
27781 *total = COSTS_N_INSNS (1);
27782 return true;
27784 if (outer_code == SET)
27786 if (XEXP (x, 1) == const0_rtx)
27788 if (TARGET_ISEL && !TARGET_MFCRF)
27789 *total = COSTS_N_INSNS (8);
27790 else
27791 *total = COSTS_N_INSNS (2);
27792 return true;
27794 else if (mode == Pmode)
27796 *total = COSTS_N_INSNS (3);
27797 return false;
27800 /* FALLTHRU */
27802 case GT:
27803 case LT:
27804 case UNORDERED:
27805 if (outer_code == SET && (XEXP (x, 1) == const0_rtx))
27807 if (TARGET_ISEL && !TARGET_MFCRF)
27808 *total = COSTS_N_INSNS (8);
27809 else
27810 *total = COSTS_N_INSNS (2);
27811 return true;
27813 /* CC COMPARE. */
27814 if (outer_code == COMPARE)
27816 *total = 0;
27817 return true;
27819 break;
27821 default:
27822 break;
27825 return false;
27828 /* Debug form of r6000_rtx_costs that is selected if -mdebug=cost. */
27830 static bool
27831 rs6000_debug_rtx_costs (rtx x, int code, int outer_code, int opno, int *total,
27832 bool speed)
27834 bool ret = rs6000_rtx_costs (x, code, outer_code, opno, total, speed);
27836 fprintf (stderr,
27837 "\nrs6000_rtx_costs, return = %s, code = %s, outer_code = %s, "
27838 "opno = %d, total = %d, speed = %s, x:\n",
27839 ret ? "complete" : "scan inner",
27840 GET_RTX_NAME (code),
27841 GET_RTX_NAME (outer_code),
27842 opno,
27843 *total,
27844 speed ? "true" : "false");
27846 debug_rtx (x);
27848 return ret;
27851 /* Debug form of ADDRESS_COST that is selected if -mdebug=cost. */
27853 static int
27854 rs6000_debug_address_cost (rtx x, enum machine_mode mode,
27855 addr_space_t as, bool speed)
27857 int ret = TARGET_ADDRESS_COST (x, mode, as, speed);
27859 fprintf (stderr, "\nrs6000_address_cost, return = %d, speed = %s, x:\n",
27860 ret, speed ? "true" : "false");
27861 debug_rtx (x);
27863 return ret;
27867 /* A C expression returning the cost of moving data from a register of class
27868 CLASS1 to one of CLASS2. */
27870 static int
27871 rs6000_register_move_cost (enum machine_mode mode,
27872 reg_class_t from, reg_class_t to)
27874 int ret;
27876 if (TARGET_DEBUG_COST)
27877 dbg_cost_ctrl++;
27879 /* Moves from/to GENERAL_REGS. */
27880 if (reg_classes_intersect_p (to, GENERAL_REGS)
27881 || reg_classes_intersect_p (from, GENERAL_REGS))
27883 reg_class_t rclass = from;
27885 if (! reg_classes_intersect_p (to, GENERAL_REGS))
27886 rclass = to;
27888 if (rclass == FLOAT_REGS || rclass == ALTIVEC_REGS || rclass == VSX_REGS)
27889 ret = (rs6000_memory_move_cost (mode, rclass, false)
27890 + rs6000_memory_move_cost (mode, GENERAL_REGS, false));
27892 /* It's more expensive to move CR_REGS than CR0_REGS because of the
27893 shift. */
27894 else if (rclass == CR_REGS)
27895 ret = 4;
27897 /* For those processors that have slow LR/CTR moves, make them more
27898 expensive than memory in order to bias spills to memory .*/
27899 else if ((rs6000_cpu == PROCESSOR_POWER6
27900 || rs6000_cpu == PROCESSOR_POWER7
27901 || rs6000_cpu == PROCESSOR_POWER8)
27902 && reg_classes_intersect_p (rclass, LINK_OR_CTR_REGS))
27903 ret = 6 * hard_regno_nregs[0][mode];
27905 else
27906 /* A move will cost one instruction per GPR moved. */
27907 ret = 2 * hard_regno_nregs[0][mode];
27910 /* If we have VSX, we can easily move between FPR or Altivec registers. */
27911 else if (VECTOR_MEM_VSX_P (mode)
27912 && reg_classes_intersect_p (to, VSX_REGS)
27913 && reg_classes_intersect_p (from, VSX_REGS))
27914 ret = 2 * hard_regno_nregs[32][mode];
27916 /* Moving between two similar registers is just one instruction. */
27917 else if (reg_classes_intersect_p (to, from))
27918 ret = (mode == TFmode || mode == TDmode) ? 4 : 2;
27920 /* Everything else has to go through GENERAL_REGS. */
27921 else
27922 ret = (rs6000_register_move_cost (mode, GENERAL_REGS, to)
27923 + rs6000_register_move_cost (mode, from, GENERAL_REGS));
27925 if (TARGET_DEBUG_COST)
27927 if (dbg_cost_ctrl == 1)
27928 fprintf (stderr,
27929 "rs6000_register_move_cost:, ret=%d, mode=%s, from=%s, to=%s\n",
27930 ret, GET_MODE_NAME (mode), reg_class_names[from],
27931 reg_class_names[to]);
27932 dbg_cost_ctrl--;
27935 return ret;
27938 /* A C expressions returning the cost of moving data of MODE from a register to
27939 or from memory. */
27941 static int
27942 rs6000_memory_move_cost (enum machine_mode mode, reg_class_t rclass,
27943 bool in ATTRIBUTE_UNUSED)
27945 int ret;
27947 if (TARGET_DEBUG_COST)
27948 dbg_cost_ctrl++;
27950 if (reg_classes_intersect_p (rclass, GENERAL_REGS))
27951 ret = 4 * hard_regno_nregs[0][mode];
27952 else if ((reg_classes_intersect_p (rclass, FLOAT_REGS)
27953 || reg_classes_intersect_p (rclass, VSX_REGS)))
27954 ret = 4 * hard_regno_nregs[32][mode];
27955 else if (reg_classes_intersect_p (rclass, ALTIVEC_REGS))
27956 ret = 4 * hard_regno_nregs[FIRST_ALTIVEC_REGNO][mode];
27957 else
27958 ret = 4 + rs6000_register_move_cost (mode, rclass, GENERAL_REGS);
27960 if (TARGET_DEBUG_COST)
27962 if (dbg_cost_ctrl == 1)
27963 fprintf (stderr,
27964 "rs6000_memory_move_cost: ret=%d, mode=%s, rclass=%s, in=%d\n",
27965 ret, GET_MODE_NAME (mode), reg_class_names[rclass], in);
27966 dbg_cost_ctrl--;
27969 return ret;
27972 /* Returns a code for a target-specific builtin that implements
27973 reciprocal of the function, or NULL_TREE if not available. */
27975 static tree
27976 rs6000_builtin_reciprocal (unsigned int fn, bool md_fn,
27977 bool sqrt ATTRIBUTE_UNUSED)
27979 if (optimize_insn_for_size_p ())
27980 return NULL_TREE;
27982 if (md_fn)
27983 switch (fn)
27985 case VSX_BUILTIN_XVSQRTDP:
27986 if (!RS6000_RECIP_AUTO_RSQRTE_P (V2DFmode))
27987 return NULL_TREE;
27989 return rs6000_builtin_decls[VSX_BUILTIN_RSQRT_2DF];
27991 case VSX_BUILTIN_XVSQRTSP:
27992 if (!RS6000_RECIP_AUTO_RSQRTE_P (V4SFmode))
27993 return NULL_TREE;
27995 return rs6000_builtin_decls[VSX_BUILTIN_RSQRT_4SF];
27997 default:
27998 return NULL_TREE;
28001 else
28002 switch (fn)
28004 case BUILT_IN_SQRT:
28005 if (!RS6000_RECIP_AUTO_RSQRTE_P (DFmode))
28006 return NULL_TREE;
28008 return rs6000_builtin_decls[RS6000_BUILTIN_RSQRT];
28010 case BUILT_IN_SQRTF:
28011 if (!RS6000_RECIP_AUTO_RSQRTE_P (SFmode))
28012 return NULL_TREE;
28014 return rs6000_builtin_decls[RS6000_BUILTIN_RSQRTF];
28016 default:
28017 return NULL_TREE;
28021 /* Load up a constant. If the mode is a vector mode, splat the value across
28022 all of the vector elements. */
28024 static rtx
28025 rs6000_load_constant_and_splat (enum machine_mode mode, REAL_VALUE_TYPE dconst)
28027 rtx reg;
28029 if (mode == SFmode || mode == DFmode)
28031 rtx d = CONST_DOUBLE_FROM_REAL_VALUE (dconst, mode);
28032 reg = force_reg (mode, d);
28034 else if (mode == V4SFmode)
28036 rtx d = CONST_DOUBLE_FROM_REAL_VALUE (dconst, SFmode);
28037 rtvec v = gen_rtvec (4, d, d, d, d);
28038 reg = gen_reg_rtx (mode);
28039 rs6000_expand_vector_init (reg, gen_rtx_PARALLEL (mode, v));
28041 else if (mode == V2DFmode)
28043 rtx d = CONST_DOUBLE_FROM_REAL_VALUE (dconst, DFmode);
28044 rtvec v = gen_rtvec (2, d, d);
28045 reg = gen_reg_rtx (mode);
28046 rs6000_expand_vector_init (reg, gen_rtx_PARALLEL (mode, v));
28048 else
28049 gcc_unreachable ();
28051 return reg;
28054 /* Generate an FMA instruction. */
28056 static void
28057 rs6000_emit_madd (rtx target, rtx m1, rtx m2, rtx a)
28059 enum machine_mode mode = GET_MODE (target);
28060 rtx dst;
28062 dst = expand_ternary_op (mode, fma_optab, m1, m2, a, target, 0);
28063 gcc_assert (dst != NULL);
28065 if (dst != target)
28066 emit_move_insn (target, dst);
28069 /* Generate a FMSUB instruction: dst = fma(m1, m2, -a). */
28071 static void
28072 rs6000_emit_msub (rtx target, rtx m1, rtx m2, rtx a)
28074 enum machine_mode mode = GET_MODE (target);
28075 rtx dst;
28077 /* Altivec does not support fms directly;
28078 generate in terms of fma in that case. */
28079 if (optab_handler (fms_optab, mode) != CODE_FOR_nothing)
28080 dst = expand_ternary_op (mode, fms_optab, m1, m2, a, target, 0);
28081 else
28083 a = expand_unop (mode, neg_optab, a, NULL_RTX, 0);
28084 dst = expand_ternary_op (mode, fma_optab, m1, m2, a, target, 0);
28086 gcc_assert (dst != NULL);
28088 if (dst != target)
28089 emit_move_insn (target, dst);
28092 /* Generate a FNMSUB instruction: dst = -fma(m1, m2, -a). */
28094 static void
28095 rs6000_emit_nmsub (rtx dst, rtx m1, rtx m2, rtx a)
28097 enum machine_mode mode = GET_MODE (dst);
28098 rtx r;
28100 /* This is a tad more complicated, since the fnma_optab is for
28101 a different expression: fma(-m1, m2, a), which is the same
28102 thing except in the case of signed zeros.
28104 Fortunately we know that if FMA is supported that FNMSUB is
28105 also supported in the ISA. Just expand it directly. */
28107 gcc_assert (optab_handler (fma_optab, mode) != CODE_FOR_nothing);
28109 r = gen_rtx_NEG (mode, a);
28110 r = gen_rtx_FMA (mode, m1, m2, r);
28111 r = gen_rtx_NEG (mode, r);
28112 emit_insn (gen_rtx_SET (VOIDmode, dst, r));
28115 /* Newton-Raphson approximation of floating point divide DST = N/D. If NOTE_P,
28116 add a reg_note saying that this was a division. Support both scalar and
28117 vector divide. Assumes no trapping math and finite arguments. */
28119 void
28120 rs6000_emit_swdiv (rtx dst, rtx n, rtx d, bool note_p)
28122 enum machine_mode mode = GET_MODE (dst);
28123 rtx one, x0, e0, x1, xprev, eprev, xnext, enext, u, v;
28124 int i;
28126 /* Low precision estimates guarantee 5 bits of accuracy. High
28127 precision estimates guarantee 14 bits of accuracy. SFmode
28128 requires 23 bits of accuracy. DFmode requires 52 bits of
28129 accuracy. Each pass at least doubles the accuracy, leading
28130 to the following. */
28131 int passes = (TARGET_RECIP_PRECISION) ? 1 : 3;
28132 if (mode == DFmode || mode == V2DFmode)
28133 passes++;
28135 enum insn_code code = optab_handler (smul_optab, mode);
28136 insn_gen_fn gen_mul = GEN_FCN (code);
28138 gcc_assert (code != CODE_FOR_nothing);
28140 one = rs6000_load_constant_and_splat (mode, dconst1);
28142 /* x0 = 1./d estimate */
28143 x0 = gen_reg_rtx (mode);
28144 emit_insn (gen_rtx_SET (VOIDmode, x0,
28145 gen_rtx_UNSPEC (mode, gen_rtvec (1, d),
28146 UNSPEC_FRES)));
28148 /* Each iteration but the last calculates x_(i+1) = x_i * (2 - d * x_i). */
28149 if (passes > 1) {
28151 /* e0 = 1. - d * x0 */
28152 e0 = gen_reg_rtx (mode);
28153 rs6000_emit_nmsub (e0, d, x0, one);
28155 /* x1 = x0 + e0 * x0 */
28156 x1 = gen_reg_rtx (mode);
28157 rs6000_emit_madd (x1, e0, x0, x0);
28159 for (i = 0, xprev = x1, eprev = e0; i < passes - 2;
28160 ++i, xprev = xnext, eprev = enext) {
28162 /* enext = eprev * eprev */
28163 enext = gen_reg_rtx (mode);
28164 emit_insn (gen_mul (enext, eprev, eprev));
28166 /* xnext = xprev + enext * xprev */
28167 xnext = gen_reg_rtx (mode);
28168 rs6000_emit_madd (xnext, enext, xprev, xprev);
28171 } else
28172 xprev = x0;
28174 /* The last iteration calculates x_(i+1) = n * x_i * (2 - d * x_i). */
28176 /* u = n * xprev */
28177 u = gen_reg_rtx (mode);
28178 emit_insn (gen_mul (u, n, xprev));
28180 /* v = n - (d * u) */
28181 v = gen_reg_rtx (mode);
28182 rs6000_emit_nmsub (v, d, u, n);
28184 /* dst = (v * xprev) + u */
28185 rs6000_emit_madd (dst, v, xprev, u);
28187 if (note_p)
28188 add_reg_note (get_last_insn (), REG_EQUAL, gen_rtx_DIV (mode, n, d));
28191 /* Newton-Raphson approximation of single/double-precision floating point
28192 rsqrt. Assumes no trapping math and finite arguments. */
28194 void
28195 rs6000_emit_swrsqrt (rtx dst, rtx src)
28197 enum machine_mode mode = GET_MODE (src);
28198 rtx x0 = gen_reg_rtx (mode);
28199 rtx y = gen_reg_rtx (mode);
28201 /* Low precision estimates guarantee 5 bits of accuracy. High
28202 precision estimates guarantee 14 bits of accuracy. SFmode
28203 requires 23 bits of accuracy. DFmode requires 52 bits of
28204 accuracy. Each pass at least doubles the accuracy, leading
28205 to the following. */
28206 int passes = (TARGET_RECIP_PRECISION) ? 1 : 3;
28207 if (mode == DFmode || mode == V2DFmode)
28208 passes++;
28210 REAL_VALUE_TYPE dconst3_2;
28211 int i;
28212 rtx halfthree;
28213 enum insn_code code = optab_handler (smul_optab, mode);
28214 insn_gen_fn gen_mul = GEN_FCN (code);
28216 gcc_assert (code != CODE_FOR_nothing);
28218 /* Load up the constant 1.5 either as a scalar, or as a vector. */
28219 real_from_integer (&dconst3_2, VOIDmode, 3, 0, 0);
28220 SET_REAL_EXP (&dconst3_2, REAL_EXP (&dconst3_2) - 1);
28222 halfthree = rs6000_load_constant_and_splat (mode, dconst3_2);
28224 /* x0 = rsqrt estimate */
28225 emit_insn (gen_rtx_SET (VOIDmode, x0,
28226 gen_rtx_UNSPEC (mode, gen_rtvec (1, src),
28227 UNSPEC_RSQRT)));
28229 /* y = 0.5 * src = 1.5 * src - src -> fewer constants */
28230 rs6000_emit_msub (y, src, halfthree, src);
28232 for (i = 0; i < passes; i++)
28234 rtx x1 = gen_reg_rtx (mode);
28235 rtx u = gen_reg_rtx (mode);
28236 rtx v = gen_reg_rtx (mode);
28238 /* x1 = x0 * (1.5 - y * (x0 * x0)) */
28239 emit_insn (gen_mul (u, x0, x0));
28240 rs6000_emit_nmsub (v, y, u, halfthree);
28241 emit_insn (gen_mul (x1, x0, v));
28242 x0 = x1;
28245 emit_move_insn (dst, x0);
28246 return;
28249 /* Emit popcount intrinsic on TARGET_POPCNTB (Power5) and TARGET_POPCNTD
28250 (Power7) targets. DST is the target, and SRC is the argument operand. */
28252 void
28253 rs6000_emit_popcount (rtx dst, rtx src)
28255 enum machine_mode mode = GET_MODE (dst);
28256 rtx tmp1, tmp2;
28258 /* Use the PPC ISA 2.06 popcnt{w,d} instruction if we can. */
28259 if (TARGET_POPCNTD)
28261 if (mode == SImode)
28262 emit_insn (gen_popcntdsi2 (dst, src));
28263 else
28264 emit_insn (gen_popcntddi2 (dst, src));
28265 return;
28268 tmp1 = gen_reg_rtx (mode);
28270 if (mode == SImode)
28272 emit_insn (gen_popcntbsi2 (tmp1, src));
28273 tmp2 = expand_mult (SImode, tmp1, GEN_INT (0x01010101),
28274 NULL_RTX, 0);
28275 tmp2 = force_reg (SImode, tmp2);
28276 emit_insn (gen_lshrsi3 (dst, tmp2, GEN_INT (24)));
28278 else
28280 emit_insn (gen_popcntbdi2 (tmp1, src));
28281 tmp2 = expand_mult (DImode, tmp1,
28282 GEN_INT ((HOST_WIDE_INT)
28283 0x01010101 << 32 | 0x01010101),
28284 NULL_RTX, 0);
28285 tmp2 = force_reg (DImode, tmp2);
28286 emit_insn (gen_lshrdi3 (dst, tmp2, GEN_INT (56)));
28291 /* Emit parity intrinsic on TARGET_POPCNTB targets. DST is the
28292 target, and SRC is the argument operand. */
28294 void
28295 rs6000_emit_parity (rtx dst, rtx src)
28297 enum machine_mode mode = GET_MODE (dst);
28298 rtx tmp;
28300 tmp = gen_reg_rtx (mode);
28302 /* Use the PPC ISA 2.05 prtyw/prtyd instruction if we can. */
28303 if (TARGET_CMPB)
28305 if (mode == SImode)
28307 emit_insn (gen_popcntbsi2 (tmp, src));
28308 emit_insn (gen_paritysi2_cmpb (dst, tmp));
28310 else
28312 emit_insn (gen_popcntbdi2 (tmp, src));
28313 emit_insn (gen_paritydi2_cmpb (dst, tmp));
28315 return;
28318 if (mode == SImode)
28320 /* Is mult+shift >= shift+xor+shift+xor? */
28321 if (rs6000_cost->mulsi_const >= COSTS_N_INSNS (3))
28323 rtx tmp1, tmp2, tmp3, tmp4;
28325 tmp1 = gen_reg_rtx (SImode);
28326 emit_insn (gen_popcntbsi2 (tmp1, src));
28328 tmp2 = gen_reg_rtx (SImode);
28329 emit_insn (gen_lshrsi3 (tmp2, tmp1, GEN_INT (16)));
28330 tmp3 = gen_reg_rtx (SImode);
28331 emit_insn (gen_xorsi3 (tmp3, tmp1, tmp2));
28333 tmp4 = gen_reg_rtx (SImode);
28334 emit_insn (gen_lshrsi3 (tmp4, tmp3, GEN_INT (8)));
28335 emit_insn (gen_xorsi3 (tmp, tmp3, tmp4));
28337 else
28338 rs6000_emit_popcount (tmp, src);
28339 emit_insn (gen_andsi3 (dst, tmp, const1_rtx));
28341 else
28343 /* Is mult+shift >= shift+xor+shift+xor+shift+xor? */
28344 if (rs6000_cost->muldi >= COSTS_N_INSNS (5))
28346 rtx tmp1, tmp2, tmp3, tmp4, tmp5, tmp6;
28348 tmp1 = gen_reg_rtx (DImode);
28349 emit_insn (gen_popcntbdi2 (tmp1, src));
28351 tmp2 = gen_reg_rtx (DImode);
28352 emit_insn (gen_lshrdi3 (tmp2, tmp1, GEN_INT (32)));
28353 tmp3 = gen_reg_rtx (DImode);
28354 emit_insn (gen_xordi3 (tmp3, tmp1, tmp2));
28356 tmp4 = gen_reg_rtx (DImode);
28357 emit_insn (gen_lshrdi3 (tmp4, tmp3, GEN_INT (16)));
28358 tmp5 = gen_reg_rtx (DImode);
28359 emit_insn (gen_xordi3 (tmp5, tmp3, tmp4));
28361 tmp6 = gen_reg_rtx (DImode);
28362 emit_insn (gen_lshrdi3 (tmp6, tmp5, GEN_INT (8)));
28363 emit_insn (gen_xordi3 (tmp, tmp5, tmp6));
28365 else
28366 rs6000_emit_popcount (tmp, src);
28367 emit_insn (gen_anddi3 (dst, tmp, const1_rtx));
28371 /* Expand an Altivec constant permutation. Return true if we match
28372 an efficient implementation; false to fall back to VPERM. */
28374 bool
28375 altivec_expand_vec_perm_const (rtx operands[4])
28377 struct altivec_perm_insn {
28378 HOST_WIDE_INT mask;
28379 enum insn_code impl;
28380 unsigned char perm[16];
28382 static const struct altivec_perm_insn patterns[] = {
28383 { OPTION_MASK_ALTIVEC, CODE_FOR_altivec_vpkuhum,
28384 { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31 } },
28385 { OPTION_MASK_ALTIVEC, CODE_FOR_altivec_vpkuwum,
28386 { 2, 3, 6, 7, 10, 11, 14, 15, 18, 19, 22, 23, 26, 27, 30, 31 } },
28387 { OPTION_MASK_ALTIVEC, CODE_FOR_altivec_vmrghb,
28388 { 0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23 } },
28389 { OPTION_MASK_ALTIVEC, CODE_FOR_altivec_vmrghh,
28390 { 0, 1, 16, 17, 2, 3, 18, 19, 4, 5, 20, 21, 6, 7, 22, 23 } },
28391 { OPTION_MASK_ALTIVEC, CODE_FOR_altivec_vmrghw,
28392 { 0, 1, 2, 3, 16, 17, 18, 19, 4, 5, 6, 7, 20, 21, 22, 23 } },
28393 { OPTION_MASK_ALTIVEC, CODE_FOR_altivec_vmrglb,
28394 { 8, 24, 9, 25, 10, 26, 11, 27, 12, 28, 13, 29, 14, 30, 15, 31 } },
28395 { OPTION_MASK_ALTIVEC, CODE_FOR_altivec_vmrglh,
28396 { 8, 9, 24, 25, 10, 11, 26, 27, 12, 13, 28, 29, 14, 15, 30, 31 } },
28397 { OPTION_MASK_ALTIVEC, CODE_FOR_altivec_vmrglw,
28398 { 8, 9, 10, 11, 24, 25, 26, 27, 12, 13, 14, 15, 28, 29, 30, 31 } },
28399 { OPTION_MASK_P8_VECTOR, CODE_FOR_p8_vmrgew,
28400 { 0, 1, 2, 3, 16, 17, 18, 19, 8, 9, 10, 11, 24, 25, 26, 27 } },
28401 { OPTION_MASK_P8_VECTOR, CODE_FOR_p8_vmrgow,
28402 { 4, 5, 6, 7, 20, 21, 22, 23, 12, 13, 14, 15, 28, 29, 30, 31 } }
28405 unsigned int i, j, elt, which;
28406 unsigned char perm[16];
28407 rtx target, op0, op1, sel, x;
28408 bool one_vec;
28410 target = operands[0];
28411 op0 = operands[1];
28412 op1 = operands[2];
28413 sel = operands[3];
28415 /* Unpack the constant selector. */
28416 for (i = which = 0; i < 16; ++i)
28418 rtx e = XVECEXP (sel, 0, i);
28419 elt = INTVAL (e) & 31;
28420 which |= (elt < 16 ? 1 : 2);
28421 perm[i] = elt;
28424 /* Simplify the constant selector based on operands. */
28425 switch (which)
28427 default:
28428 gcc_unreachable ();
28430 case 3:
28431 one_vec = false;
28432 if (!rtx_equal_p (op0, op1))
28433 break;
28434 /* FALLTHRU */
28436 case 2:
28437 for (i = 0; i < 16; ++i)
28438 perm[i] &= 15;
28439 op0 = op1;
28440 one_vec = true;
28441 break;
28443 case 1:
28444 op1 = op0;
28445 one_vec = true;
28446 break;
28449 /* Look for splat patterns. */
28450 if (one_vec)
28452 elt = perm[0];
28454 for (i = 0; i < 16; ++i)
28455 if (perm[i] != elt)
28456 break;
28457 if (i == 16)
28459 emit_insn (gen_altivec_vspltb (target, op0, GEN_INT (elt)));
28460 return true;
28463 if (elt % 2 == 0)
28465 for (i = 0; i < 16; i += 2)
28466 if (perm[i] != elt || perm[i + 1] != elt + 1)
28467 break;
28468 if (i == 16)
28470 int field = BYTES_BIG_ENDIAN ? elt / 2 : 7 - elt / 2;
28471 x = gen_reg_rtx (V8HImode);
28472 emit_insn (gen_altivec_vsplth (x, gen_lowpart (V8HImode, op0),
28473 GEN_INT (field)));
28474 emit_move_insn (target, gen_lowpart (V16QImode, x));
28475 return true;
28479 if (elt % 4 == 0)
28481 for (i = 0; i < 16; i += 4)
28482 if (perm[i] != elt
28483 || perm[i + 1] != elt + 1
28484 || perm[i + 2] != elt + 2
28485 || perm[i + 3] != elt + 3)
28486 break;
28487 if (i == 16)
28489 int field = BYTES_BIG_ENDIAN ? elt / 4 : 3 - elt / 4;
28490 x = gen_reg_rtx (V4SImode);
28491 emit_insn (gen_altivec_vspltw (x, gen_lowpart (V4SImode, op0),
28492 GEN_INT (field)));
28493 emit_move_insn (target, gen_lowpart (V16QImode, x));
28494 return true;
28499 /* Look for merge and pack patterns. */
28500 for (j = 0; j < ARRAY_SIZE (patterns); ++j)
28502 bool swapped;
28504 if ((patterns[j].mask & rs6000_isa_flags) == 0)
28505 continue;
28507 elt = patterns[j].perm[0];
28508 if (perm[0] == elt)
28509 swapped = false;
28510 else if (perm[0] == elt + 16)
28511 swapped = true;
28512 else
28513 continue;
28514 for (i = 1; i < 16; ++i)
28516 elt = patterns[j].perm[i];
28517 if (swapped)
28518 elt = (elt >= 16 ? elt - 16 : elt + 16);
28519 else if (one_vec && elt >= 16)
28520 elt -= 16;
28521 if (perm[i] != elt)
28522 break;
28524 if (i == 16)
28526 enum insn_code icode = patterns[j].impl;
28527 enum machine_mode omode = insn_data[icode].operand[0].mode;
28528 enum machine_mode imode = insn_data[icode].operand[1].mode;
28530 /* For little-endian, the two input operands must be swapped
28531 (or swapped back) to ensure proper right-to-left numbering
28532 from 0 to 2N-1. */
28533 if (swapped ^ !BYTES_BIG_ENDIAN)
28534 x = op0, op0 = op1, op1 = x;
28535 if (imode != V16QImode)
28537 op0 = gen_lowpart (imode, op0);
28538 op1 = gen_lowpart (imode, op1);
28540 if (omode == V16QImode)
28541 x = target;
28542 else
28543 x = gen_reg_rtx (omode);
28544 emit_insn (GEN_FCN (icode) (x, op0, op1));
28545 if (omode != V16QImode)
28546 emit_move_insn (target, gen_lowpart (V16QImode, x));
28547 return true;
28551 return false;
28554 /* Expand a Paired Single, VSX Permute Doubleword, or SPE constant permutation.
28555 Return true if we match an efficient implementation. */
28557 static bool
28558 rs6000_expand_vec_perm_const_1 (rtx target, rtx op0, rtx op1,
28559 unsigned char perm0, unsigned char perm1)
28561 rtx x;
28563 /* If both selectors come from the same operand, fold to single op. */
28564 if ((perm0 & 2) == (perm1 & 2))
28566 if (perm0 & 2)
28567 op0 = op1;
28568 else
28569 op1 = op0;
28571 /* If both operands are equal, fold to simpler permutation. */
28572 if (rtx_equal_p (op0, op1))
28574 perm0 = perm0 & 1;
28575 perm1 = (perm1 & 1) + 2;
28577 /* If the first selector comes from the second operand, swap. */
28578 else if (perm0 & 2)
28580 if (perm1 & 2)
28581 return false;
28582 perm0 -= 2;
28583 perm1 += 2;
28584 x = op0, op0 = op1, op1 = x;
28586 /* If the second selector does not come from the second operand, fail. */
28587 else if ((perm1 & 2) == 0)
28588 return false;
28590 /* Success! */
28591 if (target != NULL)
28593 enum machine_mode vmode, dmode;
28594 rtvec v;
28596 vmode = GET_MODE (target);
28597 gcc_assert (GET_MODE_NUNITS (vmode) == 2);
28598 dmode = mode_for_vector (GET_MODE_INNER (vmode), 4);
28600 x = gen_rtx_VEC_CONCAT (dmode, op0, op1);
28601 v = gen_rtvec (2, GEN_INT (perm0), GEN_INT (perm1));
28602 x = gen_rtx_VEC_SELECT (vmode, x, gen_rtx_PARALLEL (VOIDmode, v));
28603 emit_insn (gen_rtx_SET (VOIDmode, target, x));
28605 return true;
28608 bool
28609 rs6000_expand_vec_perm_const (rtx operands[4])
28611 rtx target, op0, op1, sel;
28612 unsigned char perm0, perm1;
28614 target = operands[0];
28615 op0 = operands[1];
28616 op1 = operands[2];
28617 sel = operands[3];
28619 /* Unpack the constant selector. */
28620 perm0 = INTVAL (XVECEXP (sel, 0, 0)) & 3;
28621 perm1 = INTVAL (XVECEXP (sel, 0, 1)) & 3;
28623 return rs6000_expand_vec_perm_const_1 (target, op0, op1, perm0, perm1);
28626 /* Test whether a constant permutation is supported. */
28628 static bool
28629 rs6000_vectorize_vec_perm_const_ok (enum machine_mode vmode,
28630 const unsigned char *sel)
28632 /* AltiVec (and thus VSX) can handle arbitrary permutations. */
28633 if (TARGET_ALTIVEC)
28634 return true;
28636 /* Check for ps_merge* or evmerge* insns. */
28637 if ((TARGET_PAIRED_FLOAT && vmode == V2SFmode)
28638 || (TARGET_SPE && vmode == V2SImode))
28640 rtx op0 = gen_raw_REG (vmode, LAST_VIRTUAL_REGISTER + 1);
28641 rtx op1 = gen_raw_REG (vmode, LAST_VIRTUAL_REGISTER + 2);
28642 return rs6000_expand_vec_perm_const_1 (NULL, op0, op1, sel[0], sel[1]);
28645 return false;
28648 /* A subroutine for rs6000_expand_extract_even & rs6000_expand_interleave. */
28650 static void
28651 rs6000_do_expand_vec_perm (rtx target, rtx op0, rtx op1,
28652 enum machine_mode vmode, unsigned nelt, rtx perm[])
28654 enum machine_mode imode;
28655 rtx x;
28657 imode = vmode;
28658 if (GET_MODE_CLASS (vmode) != MODE_VECTOR_INT)
28660 imode = GET_MODE_INNER (vmode);
28661 imode = mode_for_size (GET_MODE_BITSIZE (imode), MODE_INT, 0);
28662 imode = mode_for_vector (imode, nelt);
28665 x = gen_rtx_CONST_VECTOR (imode, gen_rtvec_v (nelt, perm));
28666 x = expand_vec_perm (vmode, op0, op1, x, target);
28667 if (x != target)
28668 emit_move_insn (target, x);
28671 /* Expand an extract even operation. */
28673 void
28674 rs6000_expand_extract_even (rtx target, rtx op0, rtx op1)
28676 enum machine_mode vmode = GET_MODE (target);
28677 unsigned i, nelt = GET_MODE_NUNITS (vmode);
28678 rtx perm[16];
28680 for (i = 0; i < nelt; i++)
28681 perm[i] = GEN_INT (i * 2);
28683 rs6000_do_expand_vec_perm (target, op0, op1, vmode, nelt, perm);
28686 /* Expand a vector interleave operation. */
28688 void
28689 rs6000_expand_interleave (rtx target, rtx op0, rtx op1, bool highp)
28691 enum machine_mode vmode = GET_MODE (target);
28692 unsigned i, high, nelt = GET_MODE_NUNITS (vmode);
28693 rtx perm[16];
28695 high = (highp == BYTES_BIG_ENDIAN ? 0 : nelt / 2);
28696 for (i = 0; i < nelt / 2; i++)
28698 perm[i * 2] = GEN_INT (i + high);
28699 perm[i * 2 + 1] = GEN_INT (i + nelt + high);
28702 rs6000_do_expand_vec_perm (target, op0, op1, vmode, nelt, perm);
28705 /* Return an RTX representing where to find the function value of a
28706 function returning MODE. */
28707 static rtx
28708 rs6000_complex_function_value (enum machine_mode mode)
28710 unsigned int regno;
28711 rtx r1, r2;
28712 enum machine_mode inner = GET_MODE_INNER (mode);
28713 unsigned int inner_bytes = GET_MODE_SIZE (inner);
28715 if (FLOAT_MODE_P (mode) && TARGET_HARD_FLOAT && TARGET_FPRS)
28716 regno = FP_ARG_RETURN;
28717 else
28719 regno = GP_ARG_RETURN;
28721 /* 32-bit is OK since it'll go in r3/r4. */
28722 if (TARGET_32BIT && inner_bytes >= 4)
28723 return gen_rtx_REG (mode, regno);
28726 if (inner_bytes >= 8)
28727 return gen_rtx_REG (mode, regno);
28729 r1 = gen_rtx_EXPR_LIST (inner, gen_rtx_REG (inner, regno),
28730 const0_rtx);
28731 r2 = gen_rtx_EXPR_LIST (inner, gen_rtx_REG (inner, regno + 1),
28732 GEN_INT (inner_bytes));
28733 return gen_rtx_PARALLEL (mode, gen_rtvec (2, r1, r2));
28736 /* Target hook for TARGET_FUNCTION_VALUE.
28738 On the SPE, both FPs and vectors are returned in r3.
28740 On RS/6000 an integer value is in r3 and a floating-point value is in
28741 fp1, unless -msoft-float. */
28743 static rtx
28744 rs6000_function_value (const_tree valtype,
28745 const_tree fn_decl_or_type ATTRIBUTE_UNUSED,
28746 bool outgoing ATTRIBUTE_UNUSED)
28748 enum machine_mode mode;
28749 unsigned int regno;
28751 /* Special handling for structs in darwin64. */
28752 if (TARGET_MACHO
28753 && rs6000_darwin64_struct_check_p (TYPE_MODE (valtype), valtype))
28755 CUMULATIVE_ARGS valcum;
28756 rtx valret;
28758 valcum.words = 0;
28759 valcum.fregno = FP_ARG_MIN_REG;
28760 valcum.vregno = ALTIVEC_ARG_MIN_REG;
28761 /* Do a trial code generation as if this were going to be passed as
28762 an argument; if any part goes in memory, we return NULL. */
28763 valret = rs6000_darwin64_record_arg (&valcum, valtype, true, /* retval= */ true);
28764 if (valret)
28765 return valret;
28766 /* Otherwise fall through to standard ABI rules. */
28769 if (TARGET_32BIT && TARGET_POWERPC64 && TYPE_MODE (valtype) == DImode)
28771 /* Long long return value need be split in -mpowerpc64, 32bit ABI. */
28772 return gen_rtx_PARALLEL (DImode,
28773 gen_rtvec (2,
28774 gen_rtx_EXPR_LIST (VOIDmode,
28775 gen_rtx_REG (SImode, GP_ARG_RETURN),
28776 const0_rtx),
28777 gen_rtx_EXPR_LIST (VOIDmode,
28778 gen_rtx_REG (SImode,
28779 GP_ARG_RETURN + 1),
28780 GEN_INT (4))));
28782 if (TARGET_32BIT && TARGET_POWERPC64 && TYPE_MODE (valtype) == DCmode)
28784 return gen_rtx_PARALLEL (DCmode,
28785 gen_rtvec (4,
28786 gen_rtx_EXPR_LIST (VOIDmode,
28787 gen_rtx_REG (SImode, GP_ARG_RETURN),
28788 const0_rtx),
28789 gen_rtx_EXPR_LIST (VOIDmode,
28790 gen_rtx_REG (SImode,
28791 GP_ARG_RETURN + 1),
28792 GEN_INT (4)),
28793 gen_rtx_EXPR_LIST (VOIDmode,
28794 gen_rtx_REG (SImode,
28795 GP_ARG_RETURN + 2),
28796 GEN_INT (8)),
28797 gen_rtx_EXPR_LIST (VOIDmode,
28798 gen_rtx_REG (SImode,
28799 GP_ARG_RETURN + 3),
28800 GEN_INT (12))));
28803 mode = TYPE_MODE (valtype);
28804 if ((INTEGRAL_TYPE_P (valtype) && GET_MODE_BITSIZE (mode) < BITS_PER_WORD)
28805 || POINTER_TYPE_P (valtype))
28806 mode = TARGET_32BIT ? SImode : DImode;
28808 if (DECIMAL_FLOAT_MODE_P (mode) && TARGET_HARD_FLOAT && TARGET_FPRS)
28809 /* _Decimal128 must use an even/odd register pair. */
28810 regno = (mode == TDmode) ? FP_ARG_RETURN + 1 : FP_ARG_RETURN;
28811 else if (SCALAR_FLOAT_TYPE_P (valtype) && TARGET_HARD_FLOAT && TARGET_FPRS
28812 && ((TARGET_SINGLE_FLOAT && (mode == SFmode)) || TARGET_DOUBLE_FLOAT))
28813 regno = FP_ARG_RETURN;
28814 else if (TREE_CODE (valtype) == COMPLEX_TYPE
28815 && targetm.calls.split_complex_arg)
28816 return rs6000_complex_function_value (mode);
28817 /* VSX is a superset of Altivec and adds V2DImode/V2DFmode. Since the same
28818 return register is used in both cases, and we won't see V2DImode/V2DFmode
28819 for pure altivec, combine the two cases. */
28820 else if (TREE_CODE (valtype) == VECTOR_TYPE
28821 && TARGET_ALTIVEC && TARGET_ALTIVEC_ABI
28822 && ALTIVEC_OR_VSX_VECTOR_MODE (mode))
28823 regno = ALTIVEC_ARG_RETURN;
28824 else if (TARGET_E500_DOUBLE && TARGET_HARD_FLOAT
28825 && (mode == DFmode || mode == DCmode
28826 || mode == TFmode || mode == TCmode))
28827 return spe_build_register_parallel (mode, GP_ARG_RETURN);
28828 else
28829 regno = GP_ARG_RETURN;
28831 return gen_rtx_REG (mode, regno);
28834 /* Define how to find the value returned by a library function
28835 assuming the value has mode MODE. */
28837 rs6000_libcall_value (enum machine_mode mode)
28839 unsigned int regno;
28841 if (TARGET_32BIT && TARGET_POWERPC64 && mode == DImode)
28843 /* Long long return value need be split in -mpowerpc64, 32bit ABI. */
28844 return gen_rtx_PARALLEL (DImode,
28845 gen_rtvec (2,
28846 gen_rtx_EXPR_LIST (VOIDmode,
28847 gen_rtx_REG (SImode, GP_ARG_RETURN),
28848 const0_rtx),
28849 gen_rtx_EXPR_LIST (VOIDmode,
28850 gen_rtx_REG (SImode,
28851 GP_ARG_RETURN + 1),
28852 GEN_INT (4))));
28855 if (DECIMAL_FLOAT_MODE_P (mode) && TARGET_HARD_FLOAT && TARGET_FPRS)
28856 /* _Decimal128 must use an even/odd register pair. */
28857 regno = (mode == TDmode) ? FP_ARG_RETURN + 1 : FP_ARG_RETURN;
28858 else if (SCALAR_FLOAT_MODE_P (mode)
28859 && TARGET_HARD_FLOAT && TARGET_FPRS
28860 && ((TARGET_SINGLE_FLOAT && mode == SFmode) || TARGET_DOUBLE_FLOAT))
28861 regno = FP_ARG_RETURN;
28862 /* VSX is a superset of Altivec and adds V2DImode/V2DFmode. Since the same
28863 return register is used in both cases, and we won't see V2DImode/V2DFmode
28864 for pure altivec, combine the two cases. */
28865 else if (ALTIVEC_OR_VSX_VECTOR_MODE (mode)
28866 && TARGET_ALTIVEC && TARGET_ALTIVEC_ABI)
28867 regno = ALTIVEC_ARG_RETURN;
28868 else if (COMPLEX_MODE_P (mode) && targetm.calls.split_complex_arg)
28869 return rs6000_complex_function_value (mode);
28870 else if (TARGET_E500_DOUBLE && TARGET_HARD_FLOAT
28871 && (mode == DFmode || mode == DCmode
28872 || mode == TFmode || mode == TCmode))
28873 return spe_build_register_parallel (mode, GP_ARG_RETURN);
28874 else
28875 regno = GP_ARG_RETURN;
28877 return gen_rtx_REG (mode, regno);
28881 /* Given FROM and TO register numbers, say whether this elimination is allowed.
28882 Frame pointer elimination is automatically handled.
28884 For the RS/6000, if frame pointer elimination is being done, we would like
28885 to convert ap into fp, not sp.
28887 We need r30 if -mminimal-toc was specified, and there are constant pool
28888 references. */
28890 static bool
28891 rs6000_can_eliminate (const int from, const int to)
28893 return (from == ARG_POINTER_REGNUM && to == STACK_POINTER_REGNUM
28894 ? ! frame_pointer_needed
28895 : from == RS6000_PIC_OFFSET_TABLE_REGNUM
28896 ? ! TARGET_MINIMAL_TOC || TARGET_NO_TOC || get_pool_size () == 0
28897 : true);
28900 /* Define the offset between two registers, FROM to be eliminated and its
28901 replacement TO, at the start of a routine. */
28902 HOST_WIDE_INT
28903 rs6000_initial_elimination_offset (int from, int to)
28905 rs6000_stack_t *info = rs6000_stack_info ();
28906 HOST_WIDE_INT offset;
28908 if (from == HARD_FRAME_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
28909 offset = info->push_p ? 0 : -info->total_size;
28910 else if (from == FRAME_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
28912 offset = info->push_p ? 0 : -info->total_size;
28913 if (FRAME_GROWS_DOWNWARD)
28914 offset += info->fixed_size + info->vars_size + info->parm_size;
28916 else if (from == FRAME_POINTER_REGNUM && to == HARD_FRAME_POINTER_REGNUM)
28917 offset = FRAME_GROWS_DOWNWARD
28918 ? info->fixed_size + info->vars_size + info->parm_size
28919 : 0;
28920 else if (from == ARG_POINTER_REGNUM && to == HARD_FRAME_POINTER_REGNUM)
28921 offset = info->total_size;
28922 else if (from == ARG_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
28923 offset = info->push_p ? info->total_size : 0;
28924 else if (from == RS6000_PIC_OFFSET_TABLE_REGNUM)
28925 offset = 0;
28926 else
28927 gcc_unreachable ();
28929 return offset;
28932 static rtx
28933 rs6000_dwarf_register_span (rtx reg)
28935 rtx parts[8];
28936 int i, words;
28937 unsigned regno = REGNO (reg);
28938 enum machine_mode mode = GET_MODE (reg);
28940 if (TARGET_SPE
28941 && regno < 32
28942 && (SPE_VECTOR_MODE (GET_MODE (reg))
28943 || (TARGET_E500_DOUBLE && FLOAT_MODE_P (mode)
28944 && mode != SFmode && mode != SDmode && mode != SCmode)))
28946 else
28947 return NULL_RTX;
28949 regno = REGNO (reg);
28951 /* The duality of the SPE register size wreaks all kinds of havoc.
28952 This is a way of distinguishing r0 in 32-bits from r0 in
28953 64-bits. */
28954 words = (GET_MODE_SIZE (mode) + UNITS_PER_FP_WORD - 1) / UNITS_PER_FP_WORD;
28955 gcc_assert (words <= 4);
28956 for (i = 0; i < words; i++, regno++)
28958 if (BYTES_BIG_ENDIAN)
28960 parts[2 * i] = gen_rtx_REG (SImode, regno + 1200);
28961 parts[2 * i + 1] = gen_rtx_REG (SImode, regno);
28963 else
28965 parts[2 * i] = gen_rtx_REG (SImode, regno);
28966 parts[2 * i + 1] = gen_rtx_REG (SImode, regno + 1200);
28970 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (words * 2, parts));
28973 /* Fill in sizes for SPE register high parts in table used by unwinder. */
28975 static void
28976 rs6000_init_dwarf_reg_sizes_extra (tree address)
28978 if (TARGET_SPE)
28980 int i;
28981 enum machine_mode mode = TYPE_MODE (char_type_node);
28982 rtx addr = expand_expr (address, NULL_RTX, VOIDmode, EXPAND_NORMAL);
28983 rtx mem = gen_rtx_MEM (BLKmode, addr);
28984 rtx value = gen_int_mode (4, mode);
28986 for (i = 1201; i < 1232; i++)
28988 int column = DWARF_REG_TO_UNWIND_COLUMN (i);
28989 HOST_WIDE_INT offset
28990 = DWARF_FRAME_REGNUM (column) * GET_MODE_SIZE (mode);
28992 emit_move_insn (adjust_address (mem, mode, offset), value);
28997 /* Map internal gcc register numbers to DWARF2 register numbers. */
28999 unsigned int
29000 rs6000_dbx_register_number (unsigned int regno)
29002 if (regno <= 63 || write_symbols != DWARF2_DEBUG)
29003 return regno;
29004 if (regno == LR_REGNO)
29005 return 108;
29006 if (regno == CTR_REGNO)
29007 return 109;
29008 if (CR_REGNO_P (regno))
29009 return regno - CR0_REGNO + 86;
29010 if (regno == CA_REGNO)
29011 return 101; /* XER */
29012 if (ALTIVEC_REGNO_P (regno))
29013 return regno - FIRST_ALTIVEC_REGNO + 1124;
29014 if (regno == VRSAVE_REGNO)
29015 return 356;
29016 if (regno == VSCR_REGNO)
29017 return 67;
29018 if (regno == SPE_ACC_REGNO)
29019 return 99;
29020 if (regno == SPEFSCR_REGNO)
29021 return 612;
29022 /* SPE high reg number. We get these values of regno from
29023 rs6000_dwarf_register_span. */
29024 gcc_assert (regno >= 1200 && regno < 1232);
29025 return regno;
29028 /* target hook eh_return_filter_mode */
29029 static enum machine_mode
29030 rs6000_eh_return_filter_mode (void)
29032 return TARGET_32BIT ? SImode : word_mode;
29035 /* Target hook for scalar_mode_supported_p. */
29036 static bool
29037 rs6000_scalar_mode_supported_p (enum machine_mode mode)
29039 if (DECIMAL_FLOAT_MODE_P (mode))
29040 return default_decimal_float_supported_p ();
29041 else
29042 return default_scalar_mode_supported_p (mode);
29045 /* Target hook for vector_mode_supported_p. */
29046 static bool
29047 rs6000_vector_mode_supported_p (enum machine_mode mode)
29050 if (TARGET_PAIRED_FLOAT && PAIRED_VECTOR_MODE (mode))
29051 return true;
29053 if (TARGET_SPE && SPE_VECTOR_MODE (mode))
29054 return true;
29056 else if (VECTOR_MEM_ALTIVEC_OR_VSX_P (mode))
29057 return true;
29059 else
29060 return false;
29063 /* Target hook for invalid_arg_for_unprototyped_fn. */
29064 static const char *
29065 invalid_arg_for_unprototyped_fn (const_tree typelist, const_tree funcdecl, const_tree val)
29067 return (!rs6000_darwin64_abi
29068 && typelist == 0
29069 && TREE_CODE (TREE_TYPE (val)) == VECTOR_TYPE
29070 && (funcdecl == NULL_TREE
29071 || (TREE_CODE (funcdecl) == FUNCTION_DECL
29072 && DECL_BUILT_IN_CLASS (funcdecl) != BUILT_IN_MD)))
29073 ? N_("AltiVec argument passed to unprototyped function")
29074 : NULL;
29077 /* For TARGET_SECURE_PLT 32-bit PIC code we can save PIC register
29078 setup by using __stack_chk_fail_local hidden function instead of
29079 calling __stack_chk_fail directly. Otherwise it is better to call
29080 __stack_chk_fail directly. */
29082 static tree ATTRIBUTE_UNUSED
29083 rs6000_stack_protect_fail (void)
29085 return (DEFAULT_ABI == ABI_V4 && TARGET_SECURE_PLT && flag_pic)
29086 ? default_hidden_stack_protect_fail ()
29087 : default_external_stack_protect_fail ();
29090 void
29091 rs6000_final_prescan_insn (rtx insn, rtx *operand ATTRIBUTE_UNUSED,
29092 int num_operands ATTRIBUTE_UNUSED)
29094 if (rs6000_warn_cell_microcode)
29096 const char *temp;
29097 int insn_code_number = recog_memoized (insn);
29098 location_t location = INSN_LOCATION (insn);
29100 /* Punt on insns we cannot recognize. */
29101 if (insn_code_number < 0)
29102 return;
29104 temp = get_insn_template (insn_code_number, insn);
29106 if (get_attr_cell_micro (insn) == CELL_MICRO_ALWAYS)
29107 warning_at (location, OPT_mwarn_cell_microcode,
29108 "emitting microcode insn %s\t[%s] #%d",
29109 temp, insn_data[INSN_CODE (insn)].name, INSN_UID (insn));
29110 else if (get_attr_cell_micro (insn) == CELL_MICRO_CONDITIONAL)
29111 warning_at (location, OPT_mwarn_cell_microcode,
29112 "emitting conditional microcode insn %s\t[%s] #%d",
29113 temp, insn_data[INSN_CODE (insn)].name, INSN_UID (insn));
29117 /* Implement the TARGET_ASAN_SHADOW_OFFSET hook. */
29119 #if TARGET_ELF
29120 static unsigned HOST_WIDE_INT
29121 rs6000_asan_shadow_offset (void)
29123 return (unsigned HOST_WIDE_INT) 1 << (TARGET_64BIT ? 41 : 29);
29125 #endif
29127 /* Mask options that we want to support inside of attribute((target)) and
29128 #pragma GCC target operations. Note, we do not include things like
29129 64/32-bit, endianess, hard/soft floating point, etc. that would have
29130 different calling sequences. */
29132 struct rs6000_opt_mask {
29133 const char *name; /* option name */
29134 HOST_WIDE_INT mask; /* mask to set */
29135 bool invert; /* invert sense of mask */
29136 bool valid_target; /* option is a target option */
29139 static struct rs6000_opt_mask const rs6000_opt_masks[] =
29141 { "altivec", OPTION_MASK_ALTIVEC, false, true },
29142 { "cmpb", OPTION_MASK_CMPB, false, true },
29143 { "crypto", OPTION_MASK_CRYPTO, false, true },
29144 { "direct-move", OPTION_MASK_DIRECT_MOVE, false, true },
29145 { "dlmzb", OPTION_MASK_DLMZB, false, true },
29146 { "fprnd", OPTION_MASK_FPRND, false, true },
29147 { "hard-dfp", OPTION_MASK_DFP, false, true },
29148 { "htm", OPTION_MASK_HTM, false, true },
29149 { "isel", OPTION_MASK_ISEL, false, true },
29150 { "mfcrf", OPTION_MASK_MFCRF, false, true },
29151 { "mfpgpr", OPTION_MASK_MFPGPR, false, true },
29152 { "mulhw", OPTION_MASK_MULHW, false, true },
29153 { "multiple", OPTION_MASK_MULTIPLE, false, true },
29154 { "popcntb", OPTION_MASK_POPCNTB, false, true },
29155 { "popcntd", OPTION_MASK_POPCNTD, false, true },
29156 { "power8-fusion", OPTION_MASK_P8_FUSION, false, true },
29157 { "power8-fusion-sign", OPTION_MASK_P8_FUSION_SIGN, false, true },
29158 { "power8-vector", OPTION_MASK_P8_VECTOR, false, true },
29159 { "powerpc-gfxopt", OPTION_MASK_PPC_GFXOPT, false, true },
29160 { "powerpc-gpopt", OPTION_MASK_PPC_GPOPT, false, true },
29161 { "quad-memory", OPTION_MASK_QUAD_MEMORY, false, true },
29162 { "recip-precision", OPTION_MASK_RECIP_PRECISION, false, true },
29163 { "string", OPTION_MASK_STRING, false, true },
29164 { "update", OPTION_MASK_NO_UPDATE, true , true },
29165 { "vsx", OPTION_MASK_VSX, false, true },
29166 { "vsx-timode", OPTION_MASK_VSX_TIMODE, false, true },
29167 #ifdef OPTION_MASK_64BIT
29168 #if TARGET_AIX_OS
29169 { "aix64", OPTION_MASK_64BIT, false, false },
29170 { "aix32", OPTION_MASK_64BIT, true, false },
29171 #else
29172 { "64", OPTION_MASK_64BIT, false, false },
29173 { "32", OPTION_MASK_64BIT, true, false },
29174 #endif
29175 #endif
29176 #ifdef OPTION_MASK_EABI
29177 { "eabi", OPTION_MASK_EABI, false, false },
29178 #endif
29179 #ifdef OPTION_MASK_LITTLE_ENDIAN
29180 { "little", OPTION_MASK_LITTLE_ENDIAN, false, false },
29181 { "big", OPTION_MASK_LITTLE_ENDIAN, true, false },
29182 #endif
29183 #ifdef OPTION_MASK_RELOCATABLE
29184 { "relocatable", OPTION_MASK_RELOCATABLE, false, false },
29185 #endif
29186 #ifdef OPTION_MASK_STRICT_ALIGN
29187 { "strict-align", OPTION_MASK_STRICT_ALIGN, false, false },
29188 #endif
29189 { "soft-float", OPTION_MASK_SOFT_FLOAT, false, false },
29190 { "string", OPTION_MASK_STRING, false, false },
29193 /* Builtin mask mapping for printing the flags. */
29194 static struct rs6000_opt_mask const rs6000_builtin_mask_names[] =
29196 { "altivec", RS6000_BTM_ALTIVEC, false, false },
29197 { "vsx", RS6000_BTM_VSX, false, false },
29198 { "spe", RS6000_BTM_SPE, false, false },
29199 { "paired", RS6000_BTM_PAIRED, false, false },
29200 { "fre", RS6000_BTM_FRE, false, false },
29201 { "fres", RS6000_BTM_FRES, false, false },
29202 { "frsqrte", RS6000_BTM_FRSQRTE, false, false },
29203 { "frsqrtes", RS6000_BTM_FRSQRTES, false, false },
29204 { "popcntd", RS6000_BTM_POPCNTD, false, false },
29205 { "cell", RS6000_BTM_CELL, false, false },
29206 { "power8-vector", RS6000_BTM_P8_VECTOR, false, false },
29207 { "crypto", RS6000_BTM_CRYPTO, false, false },
29208 { "htm", RS6000_BTM_HTM, false, false },
29211 /* Option variables that we want to support inside attribute((target)) and
29212 #pragma GCC target operations. */
29214 struct rs6000_opt_var {
29215 const char *name; /* option name */
29216 size_t global_offset; /* offset of the option in global_options. */
29217 size_t target_offset; /* offset of the option in target optiosn. */
29220 static struct rs6000_opt_var const rs6000_opt_vars[] =
29222 { "friz",
29223 offsetof (struct gcc_options, x_TARGET_FRIZ),
29224 offsetof (struct cl_target_option, x_TARGET_FRIZ), },
29225 { "avoid-indexed-addresses",
29226 offsetof (struct gcc_options, x_TARGET_AVOID_XFORM),
29227 offsetof (struct cl_target_option, x_TARGET_AVOID_XFORM) },
29228 { "paired",
29229 offsetof (struct gcc_options, x_rs6000_paired_float),
29230 offsetof (struct cl_target_option, x_rs6000_paired_float), },
29231 { "longcall",
29232 offsetof (struct gcc_options, x_rs6000_default_long_calls),
29233 offsetof (struct cl_target_option, x_rs6000_default_long_calls), },
29236 /* Inner function to handle attribute((target("..."))) and #pragma GCC target
29237 parsing. Return true if there were no errors. */
29239 static bool
29240 rs6000_inner_target_options (tree args, bool attr_p)
29242 bool ret = true;
29244 if (args == NULL_TREE)
29247 else if (TREE_CODE (args) == STRING_CST)
29249 char *p = ASTRDUP (TREE_STRING_POINTER (args));
29250 char *q;
29252 while ((q = strtok (p, ",")) != NULL)
29254 bool error_p = false;
29255 bool not_valid_p = false;
29256 const char *cpu_opt = NULL;
29258 p = NULL;
29259 if (strncmp (q, "cpu=", 4) == 0)
29261 int cpu_index = rs6000_cpu_name_lookup (q+4);
29262 if (cpu_index >= 0)
29263 rs6000_cpu_index = cpu_index;
29264 else
29266 error_p = true;
29267 cpu_opt = q+4;
29270 else if (strncmp (q, "tune=", 5) == 0)
29272 int tune_index = rs6000_cpu_name_lookup (q+5);
29273 if (tune_index >= 0)
29274 rs6000_tune_index = tune_index;
29275 else
29277 error_p = true;
29278 cpu_opt = q+5;
29281 else
29283 size_t i;
29284 bool invert = false;
29285 char *r = q;
29287 error_p = true;
29288 if (strncmp (r, "no-", 3) == 0)
29290 invert = true;
29291 r += 3;
29294 for (i = 0; i < ARRAY_SIZE (rs6000_opt_masks); i++)
29295 if (strcmp (r, rs6000_opt_masks[i].name) == 0)
29297 HOST_WIDE_INT mask = rs6000_opt_masks[i].mask;
29299 if (!rs6000_opt_masks[i].valid_target)
29300 not_valid_p = true;
29301 else
29303 error_p = false;
29304 rs6000_isa_flags_explicit |= mask;
29306 /* VSX needs altivec, so -mvsx automagically sets
29307 altivec. */
29308 if (mask == OPTION_MASK_VSX && !invert)
29309 mask |= OPTION_MASK_ALTIVEC;
29311 if (rs6000_opt_masks[i].invert)
29312 invert = !invert;
29314 if (invert)
29315 rs6000_isa_flags &= ~mask;
29316 else
29317 rs6000_isa_flags |= mask;
29319 break;
29322 if (error_p && !not_valid_p)
29324 for (i = 0; i < ARRAY_SIZE (rs6000_opt_vars); i++)
29325 if (strcmp (r, rs6000_opt_vars[i].name) == 0)
29327 size_t j = rs6000_opt_vars[i].global_offset;
29328 *((int *) ((char *)&global_options + j)) = !invert;
29329 error_p = false;
29330 break;
29335 if (error_p)
29337 const char *eprefix, *esuffix;
29339 ret = false;
29340 if (attr_p)
29342 eprefix = "__attribute__((__target__(";
29343 esuffix = ")))";
29345 else
29347 eprefix = "#pragma GCC target ";
29348 esuffix = "";
29351 if (cpu_opt)
29352 error ("invalid cpu \"%s\" for %s\"%s\"%s", cpu_opt, eprefix,
29353 q, esuffix);
29354 else if (not_valid_p)
29355 error ("%s\"%s\"%s is not allowed", eprefix, q, esuffix);
29356 else
29357 error ("%s\"%s\"%s is invalid", eprefix, q, esuffix);
29362 else if (TREE_CODE (args) == TREE_LIST)
29366 tree value = TREE_VALUE (args);
29367 if (value)
29369 bool ret2 = rs6000_inner_target_options (value, attr_p);
29370 if (!ret2)
29371 ret = false;
29373 args = TREE_CHAIN (args);
29375 while (args != NULL_TREE);
29378 else
29379 gcc_unreachable ();
29381 return ret;
29384 /* Print out the target options as a list for -mdebug=target. */
29386 static void
29387 rs6000_debug_target_options (tree args, const char *prefix)
29389 if (args == NULL_TREE)
29390 fprintf (stderr, "%s<NULL>", prefix);
29392 else if (TREE_CODE (args) == STRING_CST)
29394 char *p = ASTRDUP (TREE_STRING_POINTER (args));
29395 char *q;
29397 while ((q = strtok (p, ",")) != NULL)
29399 p = NULL;
29400 fprintf (stderr, "%s\"%s\"", prefix, q);
29401 prefix = ", ";
29405 else if (TREE_CODE (args) == TREE_LIST)
29409 tree value = TREE_VALUE (args);
29410 if (value)
29412 rs6000_debug_target_options (value, prefix);
29413 prefix = ", ";
29415 args = TREE_CHAIN (args);
29417 while (args != NULL_TREE);
29420 else
29421 gcc_unreachable ();
29423 return;
29427 /* Hook to validate attribute((target("..."))). */
29429 static bool
29430 rs6000_valid_attribute_p (tree fndecl,
29431 tree ARG_UNUSED (name),
29432 tree args,
29433 int flags)
29435 struct cl_target_option cur_target;
29436 bool ret;
29437 tree old_optimize = build_optimization_node ();
29438 tree new_target, new_optimize;
29439 tree func_optimize = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl);
29441 gcc_assert ((fndecl != NULL_TREE) && (args != NULL_TREE));
29443 if (TARGET_DEBUG_TARGET)
29445 tree tname = DECL_NAME (fndecl);
29446 fprintf (stderr, "\n==================== rs6000_valid_attribute_p:\n");
29447 if (tname)
29448 fprintf (stderr, "function: %.*s\n",
29449 (int) IDENTIFIER_LENGTH (tname),
29450 IDENTIFIER_POINTER (tname));
29451 else
29452 fprintf (stderr, "function: unknown\n");
29454 fprintf (stderr, "args:");
29455 rs6000_debug_target_options (args, " ");
29456 fprintf (stderr, "\n");
29458 if (flags)
29459 fprintf (stderr, "flags: 0x%x\n", flags);
29461 fprintf (stderr, "--------------------\n");
29464 old_optimize = build_optimization_node ();
29465 func_optimize = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl);
29467 /* If the function changed the optimization levels as well as setting target
29468 options, start with the optimizations specified. */
29469 if (func_optimize && func_optimize != old_optimize)
29470 cl_optimization_restore (&global_options,
29471 TREE_OPTIMIZATION (func_optimize));
29473 /* The target attributes may also change some optimization flags, so update
29474 the optimization options if necessary. */
29475 cl_target_option_save (&cur_target, &global_options);
29476 rs6000_cpu_index = rs6000_tune_index = -1;
29477 ret = rs6000_inner_target_options (args, true);
29479 /* Set up any additional state. */
29480 if (ret)
29482 ret = rs6000_option_override_internal (false);
29483 new_target = build_target_option_node ();
29485 else
29486 new_target = NULL;
29488 new_optimize = build_optimization_node ();
29490 if (!new_target)
29491 ret = false;
29493 else if (fndecl)
29495 DECL_FUNCTION_SPECIFIC_TARGET (fndecl) = new_target;
29497 if (old_optimize != new_optimize)
29498 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl) = new_optimize;
29501 cl_target_option_restore (&global_options, &cur_target);
29503 if (old_optimize != new_optimize)
29504 cl_optimization_restore (&global_options,
29505 TREE_OPTIMIZATION (old_optimize));
29507 return ret;
29511 /* Hook to validate the current #pragma GCC target and set the state, and
29512 update the macros based on what was changed. If ARGS is NULL, then
29513 POP_TARGET is used to reset the options. */
29515 bool
29516 rs6000_pragma_target_parse (tree args, tree pop_target)
29518 tree prev_tree = build_target_option_node ();
29519 tree cur_tree;
29520 struct cl_target_option *prev_opt, *cur_opt;
29521 HOST_WIDE_INT prev_flags, cur_flags, diff_flags;
29522 HOST_WIDE_INT prev_bumask, cur_bumask, diff_bumask;
29524 if (TARGET_DEBUG_TARGET)
29526 fprintf (stderr, "\n==================== rs6000_pragma_target_parse\n");
29527 fprintf (stderr, "args:");
29528 rs6000_debug_target_options (args, " ");
29529 fprintf (stderr, "\n");
29531 if (pop_target)
29533 fprintf (stderr, "pop_target:\n");
29534 debug_tree (pop_target);
29536 else
29537 fprintf (stderr, "pop_target: <NULL>\n");
29539 fprintf (stderr, "--------------------\n");
29542 if (! args)
29544 cur_tree = ((pop_target)
29545 ? pop_target
29546 : target_option_default_node);
29547 cl_target_option_restore (&global_options,
29548 TREE_TARGET_OPTION (cur_tree));
29550 else
29552 rs6000_cpu_index = rs6000_tune_index = -1;
29553 if (!rs6000_inner_target_options (args, false)
29554 || !rs6000_option_override_internal (false)
29555 || (cur_tree = build_target_option_node ()) == NULL_TREE)
29557 if (TARGET_DEBUG_BUILTIN || TARGET_DEBUG_TARGET)
29558 fprintf (stderr, "invalid pragma\n");
29560 return false;
29564 target_option_current_node = cur_tree;
29566 /* If we have the preprocessor linked in (i.e. C or C++ languages), possibly
29567 change the macros that are defined. */
29568 if (rs6000_target_modify_macros_ptr)
29570 prev_opt = TREE_TARGET_OPTION (prev_tree);
29571 prev_bumask = prev_opt->x_rs6000_builtin_mask;
29572 prev_flags = prev_opt->x_rs6000_isa_flags;
29574 cur_opt = TREE_TARGET_OPTION (cur_tree);
29575 cur_flags = cur_opt->x_rs6000_isa_flags;
29576 cur_bumask = cur_opt->x_rs6000_builtin_mask;
29578 diff_bumask = (prev_bumask ^ cur_bumask);
29579 diff_flags = (prev_flags ^ cur_flags);
29581 if ((diff_flags != 0) || (diff_bumask != 0))
29583 /* Delete old macros. */
29584 rs6000_target_modify_macros_ptr (false,
29585 prev_flags & diff_flags,
29586 prev_bumask & diff_bumask);
29588 /* Define new macros. */
29589 rs6000_target_modify_macros_ptr (true,
29590 cur_flags & diff_flags,
29591 cur_bumask & diff_bumask);
29595 return true;
29599 /* Remember the last target of rs6000_set_current_function. */
29600 static GTY(()) tree rs6000_previous_fndecl;
29602 /* Establish appropriate back-end context for processing the function
29603 FNDECL. The argument might be NULL to indicate processing at top
29604 level, outside of any function scope. */
29605 static void
29606 rs6000_set_current_function (tree fndecl)
29608 tree old_tree = (rs6000_previous_fndecl
29609 ? DECL_FUNCTION_SPECIFIC_TARGET (rs6000_previous_fndecl)
29610 : NULL_TREE);
29612 tree new_tree = (fndecl
29613 ? DECL_FUNCTION_SPECIFIC_TARGET (fndecl)
29614 : NULL_TREE);
29616 if (TARGET_DEBUG_TARGET)
29618 bool print_final = false;
29619 fprintf (stderr, "\n==================== rs6000_set_current_function");
29621 if (fndecl)
29622 fprintf (stderr, ", fndecl %s (%p)",
29623 (DECL_NAME (fndecl)
29624 ? IDENTIFIER_POINTER (DECL_NAME (fndecl))
29625 : "<unknown>"), (void *)fndecl);
29627 if (rs6000_previous_fndecl)
29628 fprintf (stderr, ", prev_fndecl (%p)", (void *)rs6000_previous_fndecl);
29630 fprintf (stderr, "\n");
29631 if (new_tree)
29633 fprintf (stderr, "\nnew fndecl target specific options:\n");
29634 debug_tree (new_tree);
29635 print_final = true;
29638 if (old_tree)
29640 fprintf (stderr, "\nold fndecl target specific options:\n");
29641 debug_tree (old_tree);
29642 print_final = true;
29645 if (print_final)
29646 fprintf (stderr, "--------------------\n");
29649 /* Only change the context if the function changes. This hook is called
29650 several times in the course of compiling a function, and we don't want to
29651 slow things down too much or call target_reinit when it isn't safe. */
29652 if (fndecl && fndecl != rs6000_previous_fndecl)
29654 rs6000_previous_fndecl = fndecl;
29655 if (old_tree == new_tree)
29658 else if (new_tree)
29660 cl_target_option_restore (&global_options,
29661 TREE_TARGET_OPTION (new_tree));
29662 target_reinit ();
29665 else if (old_tree)
29667 struct cl_target_option *def
29668 = TREE_TARGET_OPTION (target_option_current_node);
29670 cl_target_option_restore (&global_options, def);
29671 target_reinit ();
29677 /* Save the current options */
29679 static void
29680 rs6000_function_specific_save (struct cl_target_option *ptr)
29682 ptr->x_rs6000_isa_flags = rs6000_isa_flags;
29683 ptr->x_rs6000_isa_flags_explicit = rs6000_isa_flags_explicit;
29686 /* Restore the current options */
29688 static void
29689 rs6000_function_specific_restore (struct cl_target_option *ptr)
29691 rs6000_isa_flags = ptr->x_rs6000_isa_flags;
29692 rs6000_isa_flags_explicit = ptr->x_rs6000_isa_flags_explicit;
29693 (void) rs6000_option_override_internal (false);
29696 /* Print the current options */
29698 static void
29699 rs6000_function_specific_print (FILE *file, int indent,
29700 struct cl_target_option *ptr)
29702 rs6000_print_isa_options (file, indent, "Isa options set",
29703 ptr->x_rs6000_isa_flags);
29705 rs6000_print_isa_options (file, indent, "Isa options explicit",
29706 ptr->x_rs6000_isa_flags_explicit);
29709 /* Helper function to print the current isa or misc options on a line. */
29711 static void
29712 rs6000_print_options_internal (FILE *file,
29713 int indent,
29714 const char *string,
29715 HOST_WIDE_INT flags,
29716 const char *prefix,
29717 const struct rs6000_opt_mask *opts,
29718 size_t num_elements)
29720 size_t i;
29721 size_t start_column = 0;
29722 size_t cur_column;
29723 size_t max_column = 76;
29724 const char *comma = "";
29725 const char *nl = "\n";
29727 if (indent)
29728 start_column += fprintf (file, "%*s", indent, "");
29730 if (!flags)
29732 fprintf (stderr, DEBUG_FMT_S, string, "<none>");
29733 return;
29736 start_column += fprintf (stderr, DEBUG_FMT_WX, string, flags);
29738 /* Print the various mask options. */
29739 cur_column = start_column;
29740 for (i = 0; i < num_elements; i++)
29742 if ((flags & opts[i].mask) != 0)
29744 const char *no_str = rs6000_opt_masks[i].invert ? "no-" : "";
29745 size_t len = (strlen (comma)
29746 + strlen (prefix)
29747 + strlen (no_str)
29748 + strlen (rs6000_opt_masks[i].name));
29750 cur_column += len;
29751 if (cur_column > max_column)
29753 fprintf (stderr, ", \\\n%*s", (int)start_column, "");
29754 cur_column = start_column + len;
29755 comma = "";
29756 nl = "\n\n";
29759 fprintf (file, "%s%s%s%s", comma, prefix, no_str,
29760 rs6000_opt_masks[i].name);
29761 flags &= ~ opts[i].mask;
29762 comma = ", ";
29766 fputs (nl, file);
29769 /* Helper function to print the current isa options on a line. */
29771 static void
29772 rs6000_print_isa_options (FILE *file, int indent, const char *string,
29773 HOST_WIDE_INT flags)
29775 rs6000_print_options_internal (file, indent, string, flags, "-m",
29776 &rs6000_opt_masks[0],
29777 ARRAY_SIZE (rs6000_opt_masks));
29780 static void
29781 rs6000_print_builtin_options (FILE *file, int indent, const char *string,
29782 HOST_WIDE_INT flags)
29784 rs6000_print_options_internal (file, indent, string, flags, "",
29785 &rs6000_builtin_mask_names[0],
29786 ARRAY_SIZE (rs6000_builtin_mask_names));
29790 /* Hook to determine if one function can safely inline another. */
29792 static bool
29793 rs6000_can_inline_p (tree caller, tree callee)
29795 bool ret = false;
29796 tree caller_tree = DECL_FUNCTION_SPECIFIC_TARGET (caller);
29797 tree callee_tree = DECL_FUNCTION_SPECIFIC_TARGET (callee);
29799 /* If callee has no option attributes, then it is ok to inline. */
29800 if (!callee_tree)
29801 ret = true;
29803 /* If caller has no option attributes, but callee does then it is not ok to
29804 inline. */
29805 else if (!caller_tree)
29806 ret = false;
29808 else
29810 struct cl_target_option *caller_opts = TREE_TARGET_OPTION (caller_tree);
29811 struct cl_target_option *callee_opts = TREE_TARGET_OPTION (callee_tree);
29813 /* Callee's options should a subset of the caller's, i.e. a vsx function
29814 can inline an altivec function but a non-vsx function can't inline a
29815 vsx function. */
29816 if ((caller_opts->x_rs6000_isa_flags & callee_opts->x_rs6000_isa_flags)
29817 == callee_opts->x_rs6000_isa_flags)
29818 ret = true;
29821 if (TARGET_DEBUG_TARGET)
29822 fprintf (stderr, "rs6000_can_inline_p:, caller %s, callee %s, %s inline\n",
29823 (DECL_NAME (caller)
29824 ? IDENTIFIER_POINTER (DECL_NAME (caller))
29825 : "<unknown>"),
29826 (DECL_NAME (callee)
29827 ? IDENTIFIER_POINTER (DECL_NAME (callee))
29828 : "<unknown>"),
29829 (ret ? "can" : "cannot"));
29831 return ret;
29834 /* Allocate a stack temp and fixup the address so it meets the particular
29835 memory requirements (either offetable or REG+REG addressing). */
29838 rs6000_allocate_stack_temp (enum machine_mode mode,
29839 bool offsettable_p,
29840 bool reg_reg_p)
29842 rtx stack = assign_stack_temp (mode, GET_MODE_SIZE (mode));
29843 rtx addr = XEXP (stack, 0);
29844 int strict_p = (reload_in_progress || reload_completed);
29846 if (!legitimate_indirect_address_p (addr, strict_p))
29848 if (offsettable_p
29849 && !rs6000_legitimate_offset_address_p (mode, addr, strict_p, true))
29850 stack = replace_equiv_address (stack, copy_addr_to_reg (addr));
29852 else if (reg_reg_p && !legitimate_indexed_address_p (addr, strict_p))
29853 stack = replace_equiv_address (stack, copy_addr_to_reg (addr));
29856 return stack;
29859 /* Given a memory reference, if it is not a reg or reg+reg addressing, convert
29860 to such a form to deal with memory reference instructions like STFIWX that
29861 only take reg+reg addressing. */
29864 rs6000_address_for_fpconvert (rtx x)
29866 int strict_p = (reload_in_progress || reload_completed);
29867 rtx addr;
29869 gcc_assert (MEM_P (x));
29870 addr = XEXP (x, 0);
29871 if (! legitimate_indirect_address_p (addr, strict_p)
29872 && ! legitimate_indexed_address_p (addr, strict_p))
29874 if (GET_CODE (addr) == PRE_INC || GET_CODE (addr) == PRE_DEC)
29876 rtx reg = XEXP (addr, 0);
29877 HOST_WIDE_INT size = GET_MODE_SIZE (GET_MODE (x));
29878 rtx size_rtx = GEN_INT ((GET_CODE (addr) == PRE_DEC) ? -size : size);
29879 gcc_assert (REG_P (reg));
29880 emit_insn (gen_add3_insn (reg, reg, size_rtx));
29881 addr = reg;
29883 else if (GET_CODE (addr) == PRE_MODIFY)
29885 rtx reg = XEXP (addr, 0);
29886 rtx expr = XEXP (addr, 1);
29887 gcc_assert (REG_P (reg));
29888 gcc_assert (GET_CODE (expr) == PLUS);
29889 emit_insn (gen_add3_insn (reg, XEXP (expr, 0), XEXP (expr, 1)));
29890 addr = reg;
29893 x = replace_equiv_address (x, copy_addr_to_reg (addr));
29896 return x;
29899 /* Given a memory reference, if it is not in the form for altivec memory
29900 reference instructions (i.e. reg or reg+reg addressing with AND of -16),
29901 convert to the altivec format. */
29904 rs6000_address_for_altivec (rtx x)
29906 gcc_assert (MEM_P (x));
29907 if (!altivec_indexed_or_indirect_operand (x, GET_MODE (x)))
29909 rtx addr = XEXP (x, 0);
29910 int strict_p = (reload_in_progress || reload_completed);
29912 if (!legitimate_indexed_address_p (addr, strict_p)
29913 && !legitimate_indirect_address_p (addr, strict_p))
29914 addr = copy_to_mode_reg (Pmode, addr);
29916 addr = gen_rtx_AND (Pmode, addr, GEN_INT (-16));
29917 x = change_address (x, GET_MODE (x), addr);
29920 return x;
29923 /* Implement TARGET_LEGITIMATE_CONSTANT_P.
29925 On the RS/6000, all integer constants are acceptable, most won't be valid
29926 for particular insns, though. Only easy FP constants are acceptable. */
29928 static bool
29929 rs6000_legitimate_constant_p (enum machine_mode mode, rtx x)
29931 if (TARGET_ELF && rs6000_tls_referenced_p (x))
29932 return false;
29934 return ((GET_CODE (x) != CONST_DOUBLE && GET_CODE (x) != CONST_VECTOR)
29935 || GET_MODE (x) == VOIDmode
29936 || (TARGET_POWERPC64 && mode == DImode)
29937 || easy_fp_constant (x, mode)
29938 || easy_vector_constant (x, mode));
29942 /* A function pointer under AIX is a pointer to a data area whose first word
29943 contains the actual address of the function, whose second word contains a
29944 pointer to its TOC, and whose third word contains a value to place in the
29945 static chain register (r11). Note that if we load the static chain, our
29946 "trampoline" need not have any executable code. */
29948 void
29949 rs6000_call_indirect_aix (rtx value, rtx func_desc, rtx flag)
29951 rtx func_addr;
29952 rtx toc_reg;
29953 rtx sc_reg;
29954 rtx stack_ptr;
29955 rtx stack_toc_offset;
29956 rtx stack_toc_mem;
29957 rtx func_toc_offset;
29958 rtx func_toc_mem;
29959 rtx func_sc_offset;
29960 rtx func_sc_mem;
29961 rtx insn;
29962 rtx (*call_func) (rtx, rtx, rtx, rtx);
29963 rtx (*call_value_func) (rtx, rtx, rtx, rtx, rtx);
29965 stack_ptr = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
29966 toc_reg = gen_rtx_REG (Pmode, TOC_REGNUM);
29968 /* Load up address of the actual function. */
29969 func_desc = force_reg (Pmode, func_desc);
29970 func_addr = gen_reg_rtx (Pmode);
29971 emit_move_insn (func_addr, gen_rtx_MEM (Pmode, func_desc));
29973 if (TARGET_32BIT)
29976 stack_toc_offset = GEN_INT (TOC_SAVE_OFFSET_32BIT);
29977 func_toc_offset = GEN_INT (AIX_FUNC_DESC_TOC_32BIT);
29978 func_sc_offset = GEN_INT (AIX_FUNC_DESC_SC_32BIT);
29979 if (TARGET_POINTERS_TO_NESTED_FUNCTIONS)
29981 call_func = gen_call_indirect_aix32bit;
29982 call_value_func = gen_call_value_indirect_aix32bit;
29984 else
29986 call_func = gen_call_indirect_aix32bit_nor11;
29987 call_value_func = gen_call_value_indirect_aix32bit_nor11;
29990 else
29992 stack_toc_offset = GEN_INT (TOC_SAVE_OFFSET_64BIT);
29993 func_toc_offset = GEN_INT (AIX_FUNC_DESC_TOC_64BIT);
29994 func_sc_offset = GEN_INT (AIX_FUNC_DESC_SC_64BIT);
29995 if (TARGET_POINTERS_TO_NESTED_FUNCTIONS)
29997 call_func = gen_call_indirect_aix64bit;
29998 call_value_func = gen_call_value_indirect_aix64bit;
30000 else
30002 call_func = gen_call_indirect_aix64bit_nor11;
30003 call_value_func = gen_call_value_indirect_aix64bit_nor11;
30007 /* Reserved spot to store the TOC. */
30008 stack_toc_mem = gen_frame_mem (Pmode,
30009 gen_rtx_PLUS (Pmode,
30010 stack_ptr,
30011 stack_toc_offset));
30013 gcc_assert (cfun);
30014 gcc_assert (cfun->machine);
30016 /* Can we optimize saving the TOC in the prologue or do we need to do it at
30017 every call? */
30018 if (TARGET_SAVE_TOC_INDIRECT && !cfun->calls_alloca)
30019 cfun->machine->save_toc_in_prologue = true;
30021 else
30023 MEM_VOLATILE_P (stack_toc_mem) = 1;
30024 emit_move_insn (stack_toc_mem, toc_reg);
30027 /* Calculate the address to load the TOC of the called function. We don't
30028 actually load this until the split after reload. */
30029 func_toc_mem = gen_rtx_MEM (Pmode,
30030 gen_rtx_PLUS (Pmode,
30031 func_desc,
30032 func_toc_offset));
30034 /* If we have a static chain, load it up. */
30035 if (TARGET_POINTERS_TO_NESTED_FUNCTIONS)
30037 func_sc_mem = gen_rtx_MEM (Pmode,
30038 gen_rtx_PLUS (Pmode,
30039 func_desc,
30040 func_sc_offset));
30042 sc_reg = gen_rtx_REG (Pmode, STATIC_CHAIN_REGNUM);
30043 emit_move_insn (sc_reg, func_sc_mem);
30046 /* Create the call. */
30047 if (value)
30048 insn = call_value_func (value, func_addr, flag, func_toc_mem,
30049 stack_toc_mem);
30050 else
30051 insn = call_func (func_addr, flag, func_toc_mem, stack_toc_mem);
30053 emit_call_insn (insn);
30056 /* Return whether we need to always update the saved TOC pointer when we update
30057 the stack pointer. */
30059 static bool
30060 rs6000_save_toc_in_prologue_p (void)
30062 return (cfun && cfun->machine && cfun->machine->save_toc_in_prologue);
30065 #ifdef HAVE_GAS_HIDDEN
30066 # define USE_HIDDEN_LINKONCE 1
30067 #else
30068 # define USE_HIDDEN_LINKONCE 0
30069 #endif
30071 /* Fills in the label name that should be used for a 476 link stack thunk. */
30073 void
30074 get_ppc476_thunk_name (char name[32])
30076 gcc_assert (TARGET_LINK_STACK);
30078 if (USE_HIDDEN_LINKONCE)
30079 sprintf (name, "__ppc476.get_thunk");
30080 else
30081 ASM_GENERATE_INTERNAL_LABEL (name, "LPPC476_", 0);
30084 /* This function emits the simple thunk routine that is used to preserve
30085 the link stack on the 476 cpu. */
30087 static void rs6000_code_end (void) ATTRIBUTE_UNUSED;
30088 static void
30089 rs6000_code_end (void)
30091 char name[32];
30092 tree decl;
30094 if (!TARGET_LINK_STACK)
30095 return;
30097 get_ppc476_thunk_name (name);
30099 decl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL, get_identifier (name),
30100 build_function_type_list (void_type_node, NULL_TREE));
30101 DECL_RESULT (decl) = build_decl (BUILTINS_LOCATION, RESULT_DECL,
30102 NULL_TREE, void_type_node);
30103 TREE_PUBLIC (decl) = 1;
30104 TREE_STATIC (decl) = 1;
30106 #if RS6000_WEAK
30107 if (USE_HIDDEN_LINKONCE)
30109 DECL_COMDAT_GROUP (decl) = DECL_ASSEMBLER_NAME (decl);
30110 targetm.asm_out.unique_section (decl, 0);
30111 switch_to_section (get_named_section (decl, NULL, 0));
30112 DECL_WEAK (decl) = 1;
30113 ASM_WEAKEN_DECL (asm_out_file, decl, name, 0);
30114 targetm.asm_out.globalize_label (asm_out_file, name);
30115 targetm.asm_out.assemble_visibility (decl, VISIBILITY_HIDDEN);
30116 ASM_DECLARE_FUNCTION_NAME (asm_out_file, name, decl);
30118 else
30119 #endif
30121 switch_to_section (text_section);
30122 ASM_OUTPUT_LABEL (asm_out_file, name);
30125 DECL_INITIAL (decl) = make_node (BLOCK);
30126 current_function_decl = decl;
30127 init_function_start (decl);
30128 first_function_block_is_cold = false;
30129 /* Make sure unwind info is emitted for the thunk if needed. */
30130 final_start_function (emit_barrier (), asm_out_file, 1);
30132 fputs ("\tblr\n", asm_out_file);
30134 final_end_function ();
30135 init_insn_lengths ();
30136 free_after_compilation (cfun);
30137 set_cfun (NULL);
30138 current_function_decl = NULL;
30141 /* Add r30 to hard reg set if the prologue sets it up and it is not
30142 pic_offset_table_rtx. */
30144 static void
30145 rs6000_set_up_by_prologue (struct hard_reg_set_container *set)
30147 if (!TARGET_SINGLE_PIC_BASE
30148 && TARGET_TOC
30149 && TARGET_MINIMAL_TOC
30150 && get_pool_size () != 0)
30151 add_to_hard_reg_set (&set->set, Pmode, RS6000_PIC_OFFSET_TABLE_REGNUM);
30155 /* Helper function for rs6000_split_logical to emit a logical instruction after
30156 spliting the operation to single GPR registers.
30158 DEST is the destination register.
30159 OP1 and OP2 are the input source registers.
30160 CODE is the base operation (AND, IOR, XOR, NOT).
30161 MODE is the machine mode.
30162 If COMPLEMENT_FINAL_P is true, wrap the whole operation with NOT.
30163 If COMPLEMENT_OP1_P is true, wrap operand1 with NOT.
30164 If COMPLEMENT_OP2_P is true, wrap operand2 with NOT.
30165 CLOBBER_REG is either NULL or a scratch register of type CC to allow
30166 formation of the AND instructions. */
30168 static void
30169 rs6000_split_logical_inner (rtx dest,
30170 rtx op1,
30171 rtx op2,
30172 enum rtx_code code,
30173 enum machine_mode mode,
30174 bool complement_final_p,
30175 bool complement_op1_p,
30176 bool complement_op2_p,
30177 rtx clobber_reg)
30179 rtx bool_rtx;
30180 rtx set_rtx;
30182 /* Optimize AND of 0/0xffffffff and IOR/XOR of 0. */
30183 if (op2 && GET_CODE (op2) == CONST_INT
30184 && (mode == SImode || (mode == DImode && TARGET_POWERPC64))
30185 && !complement_final_p && !complement_op1_p && !complement_op2_p)
30187 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
30188 HOST_WIDE_INT value = INTVAL (op2) & mask;
30190 /* Optimize AND of 0 to just set 0. Optimize AND of -1 to be a move. */
30191 if (code == AND)
30193 if (value == 0)
30195 emit_insn (gen_rtx_SET (VOIDmode, dest, const0_rtx));
30196 return;
30199 else if (value == mask)
30201 if (!rtx_equal_p (dest, op1))
30202 emit_insn (gen_rtx_SET (VOIDmode, dest, op1));
30203 return;
30207 /* Optimize IOR/XOR of 0 to be a simple move. Split large operations
30208 into separate ORI/ORIS or XORI/XORIS instrucitons. */
30209 else if (code == IOR || code == XOR)
30211 if (value == 0)
30213 if (!rtx_equal_p (dest, op1))
30214 emit_insn (gen_rtx_SET (VOIDmode, dest, op1));
30215 return;
30220 if (complement_op1_p)
30221 op1 = gen_rtx_NOT (mode, op1);
30223 if (complement_op2_p)
30224 op2 = gen_rtx_NOT (mode, op2);
30226 bool_rtx = ((code == NOT)
30227 ? gen_rtx_NOT (mode, op1)
30228 : gen_rtx_fmt_ee (code, mode, op1, op2));
30230 if (complement_final_p)
30231 bool_rtx = gen_rtx_NOT (mode, bool_rtx);
30233 set_rtx = gen_rtx_SET (VOIDmode, dest, bool_rtx);
30235 /* Is this AND with an explicit clobber? */
30236 if (clobber_reg)
30238 rtx clobber = gen_rtx_CLOBBER (VOIDmode, clobber_reg);
30239 set_rtx = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, set_rtx, clobber));
30242 emit_insn (set_rtx);
30243 return;
30246 /* Split a DImode AND/IOR/XOR with a constant on a 32-bit system. These
30247 operations are split immediately during RTL generation to allow for more
30248 optimizations of the AND/IOR/XOR.
30250 OPERANDS is an array containing the destination and two input operands.
30251 CODE is the base operation (AND, IOR, XOR, NOT).
30252 MODE is the machine mode.
30253 If COMPLEMENT_FINAL_P is true, wrap the whole operation with NOT.
30254 If COMPLEMENT_OP1_P is true, wrap operand1 with NOT.
30255 If COMPLEMENT_OP2_P is true, wrap operand2 with NOT.
30256 CLOBBER_REG is either NULL or a scratch register of type CC to allow
30257 formation of the AND instructions. */
30259 static void
30260 rs6000_split_logical_di (rtx operands[3],
30261 enum rtx_code code,
30262 bool complement_final_p,
30263 bool complement_op1_p,
30264 bool complement_op2_p,
30265 rtx clobber_reg)
30267 const HOST_WIDE_INT lower_32bits = HOST_WIDE_INT_C(0xffffffff);
30268 const HOST_WIDE_INT upper_32bits = ~ lower_32bits;
30269 const HOST_WIDE_INT sign_bit = HOST_WIDE_INT_C(0x80000000);
30270 enum hi_lo { hi = 0, lo = 1 };
30271 rtx op0_hi_lo[2], op1_hi_lo[2], op2_hi_lo[2];
30272 size_t i;
30274 op0_hi_lo[hi] = gen_highpart (SImode, operands[0]);
30275 op1_hi_lo[hi] = gen_highpart (SImode, operands[1]);
30276 op0_hi_lo[lo] = gen_lowpart (SImode, operands[0]);
30277 op1_hi_lo[lo] = gen_lowpart (SImode, operands[1]);
30279 if (code == NOT)
30280 op2_hi_lo[hi] = op2_hi_lo[lo] = NULL_RTX;
30281 else
30283 if (GET_CODE (operands[2]) != CONST_INT)
30285 op2_hi_lo[hi] = gen_highpart_mode (SImode, DImode, operands[2]);
30286 op2_hi_lo[lo] = gen_lowpart (SImode, operands[2]);
30288 else
30290 HOST_WIDE_INT value = INTVAL (operands[2]);
30291 HOST_WIDE_INT value_hi_lo[2];
30293 gcc_assert (!complement_final_p);
30294 gcc_assert (!complement_op1_p);
30295 gcc_assert (!complement_op2_p);
30297 value_hi_lo[hi] = value >> 32;
30298 value_hi_lo[lo] = value & lower_32bits;
30300 for (i = 0; i < 2; i++)
30302 HOST_WIDE_INT sub_value = value_hi_lo[i];
30304 if (sub_value & sign_bit)
30305 sub_value |= upper_32bits;
30307 op2_hi_lo[i] = GEN_INT (sub_value);
30309 /* If this is an AND instruction, check to see if we need to load
30310 the value in a register. */
30311 if (code == AND && sub_value != -1 && sub_value != 0
30312 && !and_operand (op2_hi_lo[i], SImode))
30313 op2_hi_lo[i] = force_reg (SImode, op2_hi_lo[i]);
30318 for (i = 0; i < 2; i++)
30320 /* Split large IOR/XOR operations. */
30321 if ((code == IOR || code == XOR)
30322 && GET_CODE (op2_hi_lo[i]) == CONST_INT
30323 && !complement_final_p
30324 && !complement_op1_p
30325 && !complement_op2_p
30326 && clobber_reg == NULL_RTX
30327 && !logical_const_operand (op2_hi_lo[i], SImode))
30329 HOST_WIDE_INT value = INTVAL (op2_hi_lo[i]);
30330 HOST_WIDE_INT hi_16bits = value & HOST_WIDE_INT_C(0xffff0000);
30331 HOST_WIDE_INT lo_16bits = value & HOST_WIDE_INT_C(0x0000ffff);
30332 rtx tmp = gen_reg_rtx (SImode);
30334 /* Make sure the constant is sign extended. */
30335 if ((hi_16bits & sign_bit) != 0)
30336 hi_16bits |= upper_32bits;
30338 rs6000_split_logical_inner (tmp, op1_hi_lo[i], GEN_INT (hi_16bits),
30339 code, SImode, false, false, false,
30340 NULL_RTX);
30342 rs6000_split_logical_inner (op0_hi_lo[i], tmp, GEN_INT (lo_16bits),
30343 code, SImode, false, false, false,
30344 NULL_RTX);
30346 else
30347 rs6000_split_logical_inner (op0_hi_lo[i], op1_hi_lo[i], op2_hi_lo[i],
30348 code, SImode, complement_final_p,
30349 complement_op1_p, complement_op2_p,
30350 clobber_reg);
30353 return;
30356 /* Split the insns that make up boolean operations operating on multiple GPR
30357 registers. The boolean MD patterns ensure that the inputs either are
30358 exactly the same as the output registers, or there is no overlap.
30360 OPERANDS is an array containing the destination and two input operands.
30361 CODE is the base operation (AND, IOR, XOR, NOT).
30362 MODE is the machine mode.
30363 If COMPLEMENT_FINAL_P is true, wrap the whole operation with NOT.
30364 If COMPLEMENT_OP1_P is true, wrap operand1 with NOT.
30365 If COMPLEMENT_OP2_P is true, wrap operand2 with NOT.
30366 CLOBBER_REG is either NULL or a scratch register of type CC to allow
30367 formation of the AND instructions. */
30369 void
30370 rs6000_split_logical (rtx operands[3],
30371 enum rtx_code code,
30372 bool complement_final_p,
30373 bool complement_op1_p,
30374 bool complement_op2_p,
30375 rtx clobber_reg)
30377 enum machine_mode mode = GET_MODE (operands[0]);
30378 enum machine_mode sub_mode;
30379 rtx op0, op1, op2;
30380 int sub_size, regno0, regno1, nregs, i;
30382 /* If this is DImode, use the specialized version that can run before
30383 register allocation. */
30384 if (mode == DImode && !TARGET_POWERPC64)
30386 rs6000_split_logical_di (operands, code, complement_final_p,
30387 complement_op1_p, complement_op2_p,
30388 clobber_reg);
30389 return;
30392 op0 = operands[0];
30393 op1 = operands[1];
30394 op2 = (code == NOT) ? NULL_RTX : operands[2];
30395 sub_mode = (TARGET_POWERPC64) ? DImode : SImode;
30396 sub_size = GET_MODE_SIZE (sub_mode);
30397 regno0 = REGNO (op0);
30398 regno1 = REGNO (op1);
30400 gcc_assert (reload_completed);
30401 gcc_assert (IN_RANGE (regno0, FIRST_GPR_REGNO, LAST_GPR_REGNO));
30402 gcc_assert (IN_RANGE (regno1, FIRST_GPR_REGNO, LAST_GPR_REGNO));
30404 nregs = rs6000_hard_regno_nregs[(int)mode][regno0];
30405 gcc_assert (nregs > 1);
30407 if (op2 && REG_P (op2))
30408 gcc_assert (IN_RANGE (REGNO (op2), FIRST_GPR_REGNO, LAST_GPR_REGNO));
30410 for (i = 0; i < nregs; i++)
30412 int offset = i * sub_size;
30413 rtx sub_op0 = simplify_subreg (sub_mode, op0, mode, offset);
30414 rtx sub_op1 = simplify_subreg (sub_mode, op1, mode, offset);
30415 rtx sub_op2 = ((code == NOT)
30416 ? NULL_RTX
30417 : simplify_subreg (sub_mode, op2, mode, offset));
30419 rs6000_split_logical_inner (sub_op0, sub_op1, sub_op2, code, sub_mode,
30420 complement_final_p, complement_op1_p,
30421 complement_op2_p, clobber_reg);
30424 return;
30428 /* Return true if the peephole2 can combine a load involving a combination of
30429 an addis instruction and a load with an offset that can be fused together on
30430 a power8.
30432 The operands are:
30433 operands[0] register set with addis
30434 operands[1] value set via addis
30435 operands[2] target register being loaded
30436 operands[3] D-form memory reference using operands[0].
30438 In addition, we are passed a boolean that is true if this is a peephole2,
30439 and we can use see if the addis_reg is dead after the insn and can be
30440 replaced by the target register. */
30442 bool
30443 fusion_gpr_load_p (rtx *operands, bool peep2_p)
30445 rtx addis_reg = operands[0];
30446 rtx addis_value = operands[1];
30447 rtx target = operands[2];
30448 rtx mem = operands[3];
30449 rtx addr;
30450 rtx base_reg;
30452 /* Validate arguments. */
30453 if (!base_reg_operand (addis_reg, GET_MODE (addis_reg)))
30454 return false;
30456 if (!base_reg_operand (target, GET_MODE (target)))
30457 return false;
30459 if (!fusion_gpr_addis (addis_value, GET_MODE (addis_value)))
30460 return false;
30462 if (!fusion_gpr_mem_load (mem, GET_MODE (mem)))
30463 return false;
30465 /* Allow sign/zero extension. */
30466 if (GET_CODE (mem) == ZERO_EXTEND
30467 || (GET_CODE (mem) == SIGN_EXTEND && TARGET_P8_FUSION_SIGN))
30468 mem = XEXP (mem, 0);
30470 if (!MEM_P (mem))
30471 return false;
30473 addr = XEXP (mem, 0); /* either PLUS or LO_SUM. */
30474 if (GET_CODE (addr) != PLUS && GET_CODE (addr) != LO_SUM)
30475 return false;
30477 /* Validate that the register used to load the high value is either the
30478 register being loaded, or we can safely replace its use in a peephole2.
30480 If this is a peephole2, we assume that there are 2 instructions in the
30481 peephole (addis and load), so we want to check if the target register was
30482 not used in the memory address and the register to hold the addis result
30483 is dead after the peephole. */
30484 if (REGNO (addis_reg) != REGNO (target))
30486 if (!peep2_p)
30487 return false;
30489 if (reg_mentioned_p (target, mem))
30490 return false;
30492 if (!peep2_reg_dead_p (2, addis_reg))
30493 return false;
30496 base_reg = XEXP (addr, 0);
30497 return REGNO (addis_reg) == REGNO (base_reg);
30500 /* During the peephole2 pass, adjust and expand the insns for a load fusion
30501 sequence. We adjust the addis register to use the target register. If the
30502 load sign extends, we adjust the code to do the zero extending load, and an
30503 explicit sign extension later since the fusion only covers zero extending
30504 loads.
30506 The operands are:
30507 operands[0] register set with addis (to be replaced with target)
30508 operands[1] value set via addis
30509 operands[2] target register being loaded
30510 operands[3] D-form memory reference using operands[0]. */
30512 void
30513 expand_fusion_gpr_load (rtx *operands)
30515 rtx addis_value = operands[1];
30516 rtx target = operands[2];
30517 rtx orig_mem = operands[3];
30518 rtx new_addr, new_mem, orig_addr, offset;
30519 enum rtx_code plus_or_lo_sum;
30520 enum machine_mode target_mode = GET_MODE (target);
30521 enum machine_mode extend_mode = target_mode;
30522 enum machine_mode ptr_mode = Pmode;
30523 enum rtx_code extend = UNKNOWN;
30524 rtx addis_reg = ((ptr_mode == target_mode)
30525 ? target
30526 : simplify_subreg (ptr_mode, target, target_mode, 0));
30528 if (GET_CODE (orig_mem) == ZERO_EXTEND
30529 || (TARGET_P8_FUSION_SIGN && GET_CODE (orig_mem) == SIGN_EXTEND))
30531 extend = GET_CODE (orig_mem);
30532 orig_mem = XEXP (orig_mem, 0);
30533 target_mode = GET_MODE (orig_mem);
30536 gcc_assert (MEM_P (orig_mem));
30538 orig_addr = XEXP (orig_mem, 0);
30539 plus_or_lo_sum = GET_CODE (orig_addr);
30540 gcc_assert (plus_or_lo_sum == PLUS || plus_or_lo_sum == LO_SUM);
30542 offset = XEXP (orig_addr, 1);
30543 new_addr = gen_rtx_fmt_ee (plus_or_lo_sum, ptr_mode, addis_reg, offset);
30544 new_mem = change_address (orig_mem, target_mode, new_addr);
30546 if (extend != UNKNOWN)
30547 new_mem = gen_rtx_fmt_e (ZERO_EXTEND, extend_mode, new_mem);
30549 emit_insn (gen_rtx_SET (VOIDmode, addis_reg, addis_value));
30550 emit_insn (gen_rtx_SET (VOIDmode, target, new_mem));
30552 if (extend == SIGN_EXTEND)
30554 int sub_off = ((BYTES_BIG_ENDIAN)
30555 ? GET_MODE_SIZE (extend_mode) - GET_MODE_SIZE (target_mode)
30556 : 0);
30557 rtx sign_reg
30558 = simplify_subreg (target_mode, target, extend_mode, sub_off);
30560 emit_insn (gen_rtx_SET (VOIDmode, target,
30561 gen_rtx_SIGN_EXTEND (extend_mode, sign_reg)));
30564 return;
30567 /* Return a string to fuse an addis instruction with a gpr load to the same
30568 register that we loaded up the addis instruction. The code is complicated,
30569 so we call output_asm_insn directly, and just return "".
30571 The operands are:
30572 operands[0] register set with addis (must be same reg as target).
30573 operands[1] value set via addis
30574 operands[2] target register being loaded
30575 operands[3] D-form memory reference using operands[0]. */
30577 const char *
30578 emit_fusion_gpr_load (rtx *operands)
30580 rtx addis_reg = operands[0];
30581 rtx addis_value = operands[1];
30582 rtx target = operands[2];
30583 rtx mem = operands[3];
30584 rtx fuse_ops[10];
30585 rtx addr;
30586 rtx load_offset;
30587 const char *addis_str = NULL;
30588 const char *load_str = NULL;
30589 const char *extend_insn = NULL;
30590 const char *mode_name = NULL;
30591 char insn_template[80];
30592 enum machine_mode mode;
30593 const char *comment_str = ASM_COMMENT_START;
30594 bool sign_p = false;
30596 gcc_assert (REG_P (addis_reg) && REG_P (target));
30597 gcc_assert (REGNO (addis_reg) == REGNO (target));
30599 if (*comment_str == ' ')
30600 comment_str++;
30602 /* Allow sign/zero extension. */
30603 if (GET_CODE (mem) == ZERO_EXTEND)
30604 mem = XEXP (mem, 0);
30606 else if (GET_CODE (mem) == SIGN_EXTEND && TARGET_P8_FUSION_SIGN)
30608 sign_p = true;
30609 mem = XEXP (mem, 0);
30612 gcc_assert (MEM_P (mem));
30613 addr = XEXP (mem, 0);
30614 if (GET_CODE (addr) != PLUS && GET_CODE (addr) != LO_SUM)
30615 gcc_unreachable ();
30617 load_offset = XEXP (addr, 1);
30619 /* Now emit the load instruction to the same register. */
30620 mode = GET_MODE (mem);
30621 switch (mode)
30623 case QImode:
30624 mode_name = "char";
30625 load_str = "lbz";
30626 extend_insn = "extsb %0,%0";
30627 break;
30629 case HImode:
30630 mode_name = "short";
30631 load_str = "lhz";
30632 extend_insn = "extsh %0,%0";
30633 break;
30635 case SImode:
30636 mode_name = "int";
30637 load_str = "lwz";
30638 extend_insn = "extsw %0,%0";
30639 break;
30641 case DImode:
30642 if (TARGET_POWERPC64)
30644 mode_name = "long";
30645 load_str = "ld";
30647 else
30648 gcc_unreachable ();
30649 break;
30651 default:
30652 gcc_unreachable ();
30655 /* Emit the addis instruction. */
30656 fuse_ops[0] = target;
30657 if (satisfies_constraint_L (addis_value))
30659 fuse_ops[1] = addis_value;
30660 addis_str = "lis %0,%v1";
30663 else if (GET_CODE (addis_value) == PLUS)
30665 rtx op0 = XEXP (addis_value, 0);
30666 rtx op1 = XEXP (addis_value, 1);
30668 if (REG_P (op0) && CONST_INT_P (op1)
30669 && satisfies_constraint_L (op1))
30671 fuse_ops[1] = op0;
30672 fuse_ops[2] = op1;
30673 addis_str = "addis %0,%1,%v2";
30677 else if (GET_CODE (addis_value) == HIGH)
30679 rtx value = XEXP (addis_value, 0);
30680 if (GET_CODE (value) == UNSPEC && XINT (value, 1) == UNSPEC_TOCREL)
30682 fuse_ops[1] = XVECEXP (value, 0, 0); /* symbol ref. */
30683 fuse_ops[2] = XVECEXP (value, 0, 1); /* TOC register. */
30684 if (TARGET_ELF)
30685 addis_str = "addis %0,%2,%1@toc@ha";
30687 else if (TARGET_XCOFF)
30688 addis_str = "addis %0,%1@u(%2)";
30690 else
30691 gcc_unreachable ();
30694 else if (GET_CODE (value) == PLUS)
30696 rtx op0 = XEXP (value, 0);
30697 rtx op1 = XEXP (value, 1);
30699 if (GET_CODE (op0) == UNSPEC
30700 && XINT (op0, 1) == UNSPEC_TOCREL
30701 && CONST_INT_P (op1))
30703 fuse_ops[1] = XVECEXP (op0, 0, 0); /* symbol ref. */
30704 fuse_ops[2] = XVECEXP (op0, 0, 1); /* TOC register. */
30705 fuse_ops[3] = op1;
30706 if (TARGET_ELF)
30707 addis_str = "addis %0,%2,%1+%3@toc@ha";
30709 else if (TARGET_XCOFF)
30710 addis_str = "addis %0,%1+%3@u(%2)";
30712 else
30713 gcc_unreachable ();
30717 else if (satisfies_constraint_L (value))
30719 fuse_ops[1] = value;
30720 addis_str = "lis %0,%v1";
30723 else if (TARGET_ELF && !TARGET_POWERPC64 && CONSTANT_P (value))
30725 fuse_ops[1] = value;
30726 addis_str = "lis %0,%1@ha";
30730 if (!addis_str)
30731 fatal_insn ("Could not generate addis value for fusion", addis_value);
30733 sprintf (insn_template, "%s\t\t%s gpr load fusion, type %s", addis_str,
30734 comment_str, mode_name);
30735 output_asm_insn (insn_template, fuse_ops);
30737 /* Emit the D-form load instruction. */
30738 if (CONST_INT_P (load_offset) && satisfies_constraint_I (load_offset))
30740 sprintf (insn_template, "%s %%0,%%1(%%0)", load_str);
30741 fuse_ops[1] = load_offset;
30742 output_asm_insn (insn_template, fuse_ops);
30745 else if (GET_CODE (load_offset) == UNSPEC
30746 && XINT (load_offset, 1) == UNSPEC_TOCREL)
30748 if (TARGET_ELF)
30749 sprintf (insn_template, "%s %%0,%%1@toc@l(%%0)", load_str);
30751 else if (TARGET_XCOFF)
30752 sprintf (insn_template, "%s %%0,%%1@l(%%0)", load_str);
30754 else
30755 gcc_unreachable ();
30757 fuse_ops[1] = XVECEXP (load_offset, 0, 0);
30758 output_asm_insn (insn_template, fuse_ops);
30761 else if (GET_CODE (load_offset) == PLUS
30762 && GET_CODE (XEXP (load_offset, 0)) == UNSPEC
30763 && XINT (XEXP (load_offset, 0), 1) == UNSPEC_TOCREL
30764 && CONST_INT_P (XEXP (load_offset, 1)))
30766 rtx tocrel_unspec = XEXP (load_offset, 0);
30767 if (TARGET_ELF)
30768 sprintf (insn_template, "%s %%0,%%1+%%2@toc@l(%%0)", load_str);
30770 else if (TARGET_XCOFF)
30771 sprintf (insn_template, "%s %%0,%%1+%%2@l(%%0)", load_str);
30773 else
30774 gcc_unreachable ();
30776 fuse_ops[1] = XVECEXP (tocrel_unspec, 0, 0);
30777 fuse_ops[2] = XEXP (load_offset, 1);
30778 output_asm_insn (insn_template, fuse_ops);
30781 else if (TARGET_ELF && !TARGET_POWERPC64 && CONSTANT_P (load_offset))
30783 sprintf (insn_template, "%s %%0,%%1@l(%%0)", load_str);
30785 fuse_ops[1] = load_offset;
30786 output_asm_insn (insn_template, fuse_ops);
30789 else
30790 fatal_insn ("Unable to generate load offset for fusion", load_offset);
30792 /* Handle sign extension. The peephole2 pass generates this as a separate
30793 insn, but we handle it just in case it got reattached. */
30794 if (sign_p)
30796 gcc_assert (extend_insn != NULL);
30797 output_asm_insn (extend_insn, fuse_ops);
30800 return "";
30804 struct gcc_target targetm = TARGET_INITIALIZER;
30806 #include "gt-rs6000.h"