2012-07-27 Segher Boessenkool <segher@kernel.crashing.org>
[official-gcc.git] / gcc / config / rs6000 / rs6000.c
blob337a95d907b484564b7d4a6d35046022125de51a
1 /* Subroutines used for code generation on IBM RS/6000.
2 Copyright (C) 1991-2012 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 #if TARGET_XCOFF
61 #include "xcoffout.h" /* get declarations of xcoff_*_section_name */
62 #endif
63 #if TARGET_MACHO
64 #include "gstab.h" /* for N_SLINE */
65 #endif
67 #ifndef TARGET_NO_PROTOTYPE
68 #define TARGET_NO_PROTOTYPE 0
69 #endif
71 #define min(A,B) ((A) < (B) ? (A) : (B))
72 #define max(A,B) ((A) > (B) ? (A) : (B))
74 /* Structure used to define the rs6000 stack */
75 typedef struct rs6000_stack {
76 int reload_completed; /* stack info won't change from here on */
77 int first_gp_reg_save; /* first callee saved GP register used */
78 int first_fp_reg_save; /* first callee saved FP register used */
79 int first_altivec_reg_save; /* first callee saved AltiVec register used */
80 int lr_save_p; /* true if the link reg needs to be saved */
81 int cr_save_p; /* true if the CR reg needs to be saved */
82 unsigned int vrsave_mask; /* mask of vec registers to save */
83 int push_p; /* true if we need to allocate stack space */
84 int calls_p; /* true if the function makes any calls */
85 int world_save_p; /* true if we're saving *everything*:
86 r13-r31, cr, f14-f31, vrsave, v20-v31 */
87 enum rs6000_abi abi; /* which ABI to use */
88 int gp_save_offset; /* offset to save GP regs from initial SP */
89 int fp_save_offset; /* offset to save FP regs from initial SP */
90 int altivec_save_offset; /* offset to save AltiVec regs from initial SP */
91 int lr_save_offset; /* offset to save LR from initial SP */
92 int cr_save_offset; /* offset to save CR from initial SP */
93 int vrsave_save_offset; /* offset to save VRSAVE from initial SP */
94 int spe_gp_save_offset; /* offset to save spe 64-bit gprs */
95 int varargs_save_offset; /* offset to save the varargs registers */
96 int ehrd_offset; /* offset to EH return data */
97 int reg_size; /* register size (4 or 8) */
98 HOST_WIDE_INT vars_size; /* variable save area size */
99 int parm_size; /* outgoing parameter size */
100 int save_size; /* save area size */
101 int fixed_size; /* fixed size of stack frame */
102 int gp_size; /* size of saved GP registers */
103 int fp_size; /* size of saved FP registers */
104 int altivec_size; /* size of saved AltiVec registers */
105 int cr_size; /* size to hold CR if not in save_size */
106 int vrsave_size; /* size to hold VRSAVE if not in save_size */
107 int altivec_padding_size; /* size of altivec alignment padding if
108 not in save_size */
109 int spe_gp_size; /* size of 64-bit GPR save size for SPE */
110 int spe_padding_size;
111 HOST_WIDE_INT total_size; /* total bytes allocated for stack */
112 int spe_64bit_regs_used;
113 int savres_strategy;
114 } rs6000_stack_t;
116 /* A C structure for machine-specific, per-function data.
117 This is added to the cfun structure. */
118 typedef struct GTY(()) machine_function
120 /* Some local-dynamic symbol. */
121 const char *some_ld_name;
122 /* Whether the instruction chain has been scanned already. */
123 int insn_chain_scanned_p;
124 /* Flags if __builtin_return_address (n) with n >= 1 was used. */
125 int ra_needs_full_frame;
126 /* Flags if __builtin_return_address (0) was used. */
127 int ra_need_lr;
128 /* Cache lr_save_p after expansion of builtin_eh_return. */
129 int lr_save_state;
130 /* Whether we need to save the TOC to the reserved stack location in the
131 function prologue. */
132 bool save_toc_in_prologue;
133 /* Offset from virtual_stack_vars_rtx to the start of the ABI_V4
134 varargs save area. */
135 HOST_WIDE_INT varargs_save_offset;
136 /* Temporary stack slot to use for SDmode copies. This slot is
137 64-bits wide and is allocated early enough so that the offset
138 does not overflow the 16-bit load/store offset field. */
139 rtx sdmode_stack_slot;
140 } machine_function;
142 /* Support targetm.vectorize.builtin_mask_for_load. */
143 static GTY(()) tree altivec_builtin_mask_for_load;
145 /* Set to nonzero once AIX common-mode calls have been defined. */
146 static GTY(()) int common_mode_defined;
148 /* Label number of label created for -mrelocatable, to call to so we can
149 get the address of the GOT section */
150 static int rs6000_pic_labelno;
152 #ifdef USING_ELFOS_H
153 /* Counter for labels which are to be placed in .fixup. */
154 int fixuplabelno = 0;
155 #endif
157 /* Whether to use variant of AIX ABI for PowerPC64 Linux. */
158 int dot_symbols;
160 /* Specify the machine mode that pointers have. After generation of rtl, the
161 compiler makes no further distinction between pointers and any other objects
162 of this machine mode. The type is unsigned since not all things that
163 include rs6000.h also include machmode.h. */
164 unsigned rs6000_pmode;
166 /* Width in bits of a pointer. */
167 unsigned rs6000_pointer_size;
169 #ifdef HAVE_AS_GNU_ATTRIBUTE
170 /* Flag whether floating point values have been passed/returned. */
171 static bool rs6000_passes_float;
172 /* Flag whether vector values have been passed/returned. */
173 static bool rs6000_passes_vector;
174 /* Flag whether small (<= 8 byte) structures have been returned. */
175 static bool rs6000_returns_struct;
176 #endif
178 /* Value is TRUE if register/mode pair is acceptable. */
179 bool rs6000_hard_regno_mode_ok_p[NUM_MACHINE_MODES][FIRST_PSEUDO_REGISTER];
181 /* Maximum number of registers needed for a given register class and mode. */
182 unsigned char rs6000_class_max_nregs[NUM_MACHINE_MODES][LIM_REG_CLASSES];
184 /* How many registers are needed for a given register and mode. */
185 unsigned char rs6000_hard_regno_nregs[NUM_MACHINE_MODES][FIRST_PSEUDO_REGISTER];
187 /* Map register number to register class. */
188 enum reg_class rs6000_regno_regclass[FIRST_PSEUDO_REGISTER];
190 /* Reload functions based on the type and the vector unit. */
191 static enum insn_code rs6000_vector_reload[NUM_MACHINE_MODES][2];
193 static int dbg_cost_ctrl;
195 /* Built in types. */
196 tree rs6000_builtin_types[RS6000_BTI_MAX];
197 tree rs6000_builtin_decls[RS6000_BUILTIN_COUNT];
199 /* Flag to say the TOC is initialized */
200 int toc_initialized;
201 char toc_label_name[10];
203 /* Cached value of rs6000_variable_issue. This is cached in
204 rs6000_variable_issue hook and returned from rs6000_sched_reorder2. */
205 static short cached_can_issue_more;
207 static GTY(()) section *read_only_data_section;
208 static GTY(()) section *private_data_section;
209 static GTY(()) section *read_only_private_data_section;
210 static GTY(()) section *sdata2_section;
211 static GTY(()) section *toc_section;
213 struct builtin_description
215 const unsigned int mask;
216 const enum insn_code icode;
217 const char *const name;
218 const enum rs6000_builtins code;
221 /* Describe the vector unit used for modes. */
222 enum rs6000_vector rs6000_vector_unit[NUM_MACHINE_MODES];
223 enum rs6000_vector rs6000_vector_mem[NUM_MACHINE_MODES];
225 /* Register classes for various constraints that are based on the target
226 switches. */
227 enum reg_class rs6000_constraints[RS6000_CONSTRAINT_MAX];
229 /* Describe the alignment of a vector. */
230 int rs6000_vector_align[NUM_MACHINE_MODES];
232 /* Map selected modes to types for builtins. */
233 static GTY(()) tree builtin_mode_to_type[MAX_MACHINE_MODE][2];
235 /* What modes to automatically generate reciprocal divide estimate (fre) and
236 reciprocal sqrt (frsqrte) for. */
237 unsigned char rs6000_recip_bits[MAX_MACHINE_MODE];
239 /* Masks to determine which reciprocal esitmate instructions to generate
240 automatically. */
241 enum rs6000_recip_mask {
242 RECIP_SF_DIV = 0x001, /* Use divide estimate */
243 RECIP_DF_DIV = 0x002,
244 RECIP_V4SF_DIV = 0x004,
245 RECIP_V2DF_DIV = 0x008,
247 RECIP_SF_RSQRT = 0x010, /* Use reciprocal sqrt estimate. */
248 RECIP_DF_RSQRT = 0x020,
249 RECIP_V4SF_RSQRT = 0x040,
250 RECIP_V2DF_RSQRT = 0x080,
252 /* Various combination of flags for -mrecip=xxx. */
253 RECIP_NONE = 0,
254 RECIP_ALL = (RECIP_SF_DIV | RECIP_DF_DIV | RECIP_V4SF_DIV
255 | RECIP_V2DF_DIV | RECIP_SF_RSQRT | RECIP_DF_RSQRT
256 | RECIP_V4SF_RSQRT | RECIP_V2DF_RSQRT),
258 RECIP_HIGH_PRECISION = RECIP_ALL,
260 /* On low precision machines like the power5, don't enable double precision
261 reciprocal square root estimate, since it isn't accurate enough. */
262 RECIP_LOW_PRECISION = (RECIP_ALL & ~(RECIP_DF_RSQRT | RECIP_V2DF_RSQRT))
265 /* -mrecip options. */
266 static struct
268 const char *string; /* option name */
269 unsigned int mask; /* mask bits to set */
270 } recip_options[] = {
271 { "all", RECIP_ALL },
272 { "none", RECIP_NONE },
273 { "div", (RECIP_SF_DIV | RECIP_DF_DIV | RECIP_V4SF_DIV
274 | RECIP_V2DF_DIV) },
275 { "divf", (RECIP_SF_DIV | RECIP_V4SF_DIV) },
276 { "divd", (RECIP_DF_DIV | RECIP_V2DF_DIV) },
277 { "rsqrt", (RECIP_SF_RSQRT | RECIP_DF_RSQRT | RECIP_V4SF_RSQRT
278 | RECIP_V2DF_RSQRT) },
279 { "rsqrtf", (RECIP_SF_RSQRT | RECIP_V4SF_RSQRT) },
280 { "rsqrtd", (RECIP_DF_RSQRT | RECIP_V2DF_RSQRT) },
283 /* 2 argument gen function typedef. */
284 typedef rtx (*gen_2arg_fn_t) (rtx, rtx, rtx);
286 /* Pointer to function (in rs6000-c.c) that can define or undefine target
287 macros that have changed. Languages that don't support the preprocessor
288 don't link in rs6000-c.c, so we can't call it directly. */
289 void (*rs6000_target_modify_macros_ptr) (bool, int, unsigned);
292 /* Target cpu costs. */
294 struct processor_costs {
295 const int mulsi; /* cost of SImode multiplication. */
296 const int mulsi_const; /* cost of SImode multiplication by constant. */
297 const int mulsi_const9; /* cost of SImode mult by short constant. */
298 const int muldi; /* cost of DImode multiplication. */
299 const int divsi; /* cost of SImode division. */
300 const int divdi; /* cost of DImode division. */
301 const int fp; /* cost of simple SFmode and DFmode insns. */
302 const int dmul; /* cost of DFmode multiplication (and fmadd). */
303 const int sdiv; /* cost of SFmode division (fdivs). */
304 const int ddiv; /* cost of DFmode division (fdiv). */
305 const int cache_line_size; /* cache line size in bytes. */
306 const int l1_cache_size; /* size of l1 cache, in kilobytes. */
307 const int l2_cache_size; /* size of l2 cache, in kilobytes. */
308 const int simultaneous_prefetches; /* number of parallel prefetch
309 operations. */
312 const struct processor_costs *rs6000_cost;
314 /* Processor costs (relative to an add) */
316 /* Instruction size costs on 32bit processors. */
317 static const
318 struct processor_costs size32_cost = {
319 COSTS_N_INSNS (1), /* mulsi */
320 COSTS_N_INSNS (1), /* mulsi_const */
321 COSTS_N_INSNS (1), /* mulsi_const9 */
322 COSTS_N_INSNS (1), /* muldi */
323 COSTS_N_INSNS (1), /* divsi */
324 COSTS_N_INSNS (1), /* divdi */
325 COSTS_N_INSNS (1), /* fp */
326 COSTS_N_INSNS (1), /* dmul */
327 COSTS_N_INSNS (1), /* sdiv */
328 COSTS_N_INSNS (1), /* ddiv */
335 /* Instruction size costs on 64bit processors. */
336 static const
337 struct processor_costs size64_cost = {
338 COSTS_N_INSNS (1), /* mulsi */
339 COSTS_N_INSNS (1), /* mulsi_const */
340 COSTS_N_INSNS (1), /* mulsi_const9 */
341 COSTS_N_INSNS (1), /* muldi */
342 COSTS_N_INSNS (1), /* divsi */
343 COSTS_N_INSNS (1), /* divdi */
344 COSTS_N_INSNS (1), /* fp */
345 COSTS_N_INSNS (1), /* dmul */
346 COSTS_N_INSNS (1), /* sdiv */
347 COSTS_N_INSNS (1), /* ddiv */
348 128,
354 /* Instruction costs on RS64A processors. */
355 static const
356 struct processor_costs rs64a_cost = {
357 COSTS_N_INSNS (20), /* mulsi */
358 COSTS_N_INSNS (12), /* mulsi_const */
359 COSTS_N_INSNS (8), /* mulsi_const9 */
360 COSTS_N_INSNS (34), /* muldi */
361 COSTS_N_INSNS (65), /* divsi */
362 COSTS_N_INSNS (67), /* divdi */
363 COSTS_N_INSNS (4), /* fp */
364 COSTS_N_INSNS (4), /* dmul */
365 COSTS_N_INSNS (31), /* sdiv */
366 COSTS_N_INSNS (31), /* ddiv */
367 128, /* cache line size */
368 128, /* l1 cache */
369 2048, /* l2 cache */
370 1, /* streams */
373 /* Instruction costs on MPCCORE processors. */
374 static const
375 struct processor_costs mpccore_cost = {
376 COSTS_N_INSNS (2), /* mulsi */
377 COSTS_N_INSNS (2), /* mulsi_const */
378 COSTS_N_INSNS (2), /* mulsi_const9 */
379 COSTS_N_INSNS (2), /* muldi */
380 COSTS_N_INSNS (6), /* divsi */
381 COSTS_N_INSNS (6), /* divdi */
382 COSTS_N_INSNS (4), /* fp */
383 COSTS_N_INSNS (5), /* dmul */
384 COSTS_N_INSNS (10), /* sdiv */
385 COSTS_N_INSNS (17), /* ddiv */
386 32, /* cache line size */
387 4, /* l1 cache */
388 16, /* l2 cache */
389 1, /* streams */
392 /* Instruction costs on PPC403 processors. */
393 static const
394 struct processor_costs ppc403_cost = {
395 COSTS_N_INSNS (4), /* mulsi */
396 COSTS_N_INSNS (4), /* mulsi_const */
397 COSTS_N_INSNS (4), /* mulsi_const9 */
398 COSTS_N_INSNS (4), /* muldi */
399 COSTS_N_INSNS (33), /* divsi */
400 COSTS_N_INSNS (33), /* divdi */
401 COSTS_N_INSNS (11), /* fp */
402 COSTS_N_INSNS (11), /* dmul */
403 COSTS_N_INSNS (11), /* sdiv */
404 COSTS_N_INSNS (11), /* ddiv */
405 32, /* cache line size */
406 4, /* l1 cache */
407 16, /* l2 cache */
408 1, /* streams */
411 /* Instruction costs on PPC405 processors. */
412 static const
413 struct processor_costs ppc405_cost = {
414 COSTS_N_INSNS (5), /* mulsi */
415 COSTS_N_INSNS (4), /* mulsi_const */
416 COSTS_N_INSNS (3), /* mulsi_const9 */
417 COSTS_N_INSNS (5), /* muldi */
418 COSTS_N_INSNS (35), /* divsi */
419 COSTS_N_INSNS (35), /* divdi */
420 COSTS_N_INSNS (11), /* fp */
421 COSTS_N_INSNS (11), /* dmul */
422 COSTS_N_INSNS (11), /* sdiv */
423 COSTS_N_INSNS (11), /* ddiv */
424 32, /* cache line size */
425 16, /* l1 cache */
426 128, /* l2 cache */
427 1, /* streams */
430 /* Instruction costs on PPC440 processors. */
431 static const
432 struct processor_costs ppc440_cost = {
433 COSTS_N_INSNS (3), /* mulsi */
434 COSTS_N_INSNS (2), /* mulsi_const */
435 COSTS_N_INSNS (2), /* mulsi_const9 */
436 COSTS_N_INSNS (3), /* muldi */
437 COSTS_N_INSNS (34), /* divsi */
438 COSTS_N_INSNS (34), /* divdi */
439 COSTS_N_INSNS (5), /* fp */
440 COSTS_N_INSNS (5), /* dmul */
441 COSTS_N_INSNS (19), /* sdiv */
442 COSTS_N_INSNS (33), /* ddiv */
443 32, /* cache line size */
444 32, /* l1 cache */
445 256, /* l2 cache */
446 1, /* streams */
449 /* Instruction costs on PPC476 processors. */
450 static const
451 struct processor_costs ppc476_cost = {
452 COSTS_N_INSNS (4), /* mulsi */
453 COSTS_N_INSNS (4), /* mulsi_const */
454 COSTS_N_INSNS (4), /* mulsi_const9 */
455 COSTS_N_INSNS (4), /* muldi */
456 COSTS_N_INSNS (11), /* divsi */
457 COSTS_N_INSNS (11), /* divdi */
458 COSTS_N_INSNS (6), /* fp */
459 COSTS_N_INSNS (6), /* dmul */
460 COSTS_N_INSNS (19), /* sdiv */
461 COSTS_N_INSNS (33), /* ddiv */
462 32, /* l1 cache line size */
463 32, /* l1 cache */
464 512, /* l2 cache */
465 1, /* streams */
468 /* Instruction costs on PPC601 processors. */
469 static const
470 struct processor_costs ppc601_cost = {
471 COSTS_N_INSNS (5), /* mulsi */
472 COSTS_N_INSNS (5), /* mulsi_const */
473 COSTS_N_INSNS (5), /* mulsi_const9 */
474 COSTS_N_INSNS (5), /* muldi */
475 COSTS_N_INSNS (36), /* divsi */
476 COSTS_N_INSNS (36), /* divdi */
477 COSTS_N_INSNS (4), /* fp */
478 COSTS_N_INSNS (5), /* dmul */
479 COSTS_N_INSNS (17), /* sdiv */
480 COSTS_N_INSNS (31), /* ddiv */
481 32, /* cache line size */
482 32, /* l1 cache */
483 256, /* l2 cache */
484 1, /* streams */
487 /* Instruction costs on PPC603 processors. */
488 static const
489 struct processor_costs ppc603_cost = {
490 COSTS_N_INSNS (5), /* mulsi */
491 COSTS_N_INSNS (3), /* mulsi_const */
492 COSTS_N_INSNS (2), /* mulsi_const9 */
493 COSTS_N_INSNS (5), /* muldi */
494 COSTS_N_INSNS (37), /* divsi */
495 COSTS_N_INSNS (37), /* divdi */
496 COSTS_N_INSNS (3), /* fp */
497 COSTS_N_INSNS (4), /* dmul */
498 COSTS_N_INSNS (18), /* sdiv */
499 COSTS_N_INSNS (33), /* ddiv */
500 32, /* cache line size */
501 8, /* l1 cache */
502 64, /* l2 cache */
503 1, /* streams */
506 /* Instruction costs on PPC604 processors. */
507 static const
508 struct processor_costs ppc604_cost = {
509 COSTS_N_INSNS (4), /* mulsi */
510 COSTS_N_INSNS (4), /* mulsi_const */
511 COSTS_N_INSNS (4), /* mulsi_const9 */
512 COSTS_N_INSNS (4), /* muldi */
513 COSTS_N_INSNS (20), /* divsi */
514 COSTS_N_INSNS (20), /* divdi */
515 COSTS_N_INSNS (3), /* fp */
516 COSTS_N_INSNS (3), /* dmul */
517 COSTS_N_INSNS (18), /* sdiv */
518 COSTS_N_INSNS (32), /* ddiv */
519 32, /* cache line size */
520 16, /* l1 cache */
521 512, /* l2 cache */
522 1, /* streams */
525 /* Instruction costs on PPC604e processors. */
526 static const
527 struct processor_costs ppc604e_cost = {
528 COSTS_N_INSNS (2), /* mulsi */
529 COSTS_N_INSNS (2), /* mulsi_const */
530 COSTS_N_INSNS (2), /* mulsi_const9 */
531 COSTS_N_INSNS (2), /* muldi */
532 COSTS_N_INSNS (20), /* divsi */
533 COSTS_N_INSNS (20), /* divdi */
534 COSTS_N_INSNS (3), /* fp */
535 COSTS_N_INSNS (3), /* dmul */
536 COSTS_N_INSNS (18), /* sdiv */
537 COSTS_N_INSNS (32), /* ddiv */
538 32, /* cache line size */
539 32, /* l1 cache */
540 1024, /* l2 cache */
541 1, /* streams */
544 /* Instruction costs on PPC620 processors. */
545 static const
546 struct processor_costs ppc620_cost = {
547 COSTS_N_INSNS (5), /* mulsi */
548 COSTS_N_INSNS (4), /* mulsi_const */
549 COSTS_N_INSNS (3), /* mulsi_const9 */
550 COSTS_N_INSNS (7), /* muldi */
551 COSTS_N_INSNS (21), /* divsi */
552 COSTS_N_INSNS (37), /* divdi */
553 COSTS_N_INSNS (3), /* fp */
554 COSTS_N_INSNS (3), /* dmul */
555 COSTS_N_INSNS (18), /* sdiv */
556 COSTS_N_INSNS (32), /* ddiv */
557 128, /* cache line size */
558 32, /* l1 cache */
559 1024, /* l2 cache */
560 1, /* streams */
563 /* Instruction costs on PPC630 processors. */
564 static const
565 struct processor_costs ppc630_cost = {
566 COSTS_N_INSNS (5), /* mulsi */
567 COSTS_N_INSNS (4), /* mulsi_const */
568 COSTS_N_INSNS (3), /* mulsi_const9 */
569 COSTS_N_INSNS (7), /* muldi */
570 COSTS_N_INSNS (21), /* divsi */
571 COSTS_N_INSNS (37), /* divdi */
572 COSTS_N_INSNS (3), /* fp */
573 COSTS_N_INSNS (3), /* dmul */
574 COSTS_N_INSNS (17), /* sdiv */
575 COSTS_N_INSNS (21), /* ddiv */
576 128, /* cache line size */
577 64, /* l1 cache */
578 1024, /* l2 cache */
579 1, /* streams */
582 /* Instruction costs on Cell processor. */
583 /* COSTS_N_INSNS (1) ~ one add. */
584 static const
585 struct processor_costs ppccell_cost = {
586 COSTS_N_INSNS (9/2)+2, /* mulsi */
587 COSTS_N_INSNS (6/2), /* mulsi_const */
588 COSTS_N_INSNS (6/2), /* mulsi_const9 */
589 COSTS_N_INSNS (15/2)+2, /* muldi */
590 COSTS_N_INSNS (38/2), /* divsi */
591 COSTS_N_INSNS (70/2), /* divdi */
592 COSTS_N_INSNS (10/2), /* fp */
593 COSTS_N_INSNS (10/2), /* dmul */
594 COSTS_N_INSNS (74/2), /* sdiv */
595 COSTS_N_INSNS (74/2), /* ddiv */
596 128, /* cache line size */
597 32, /* l1 cache */
598 512, /* l2 cache */
599 6, /* streams */
602 /* Instruction costs on PPC750 and PPC7400 processors. */
603 static const
604 struct processor_costs ppc750_cost = {
605 COSTS_N_INSNS (5), /* mulsi */
606 COSTS_N_INSNS (3), /* mulsi_const */
607 COSTS_N_INSNS (2), /* mulsi_const9 */
608 COSTS_N_INSNS (5), /* muldi */
609 COSTS_N_INSNS (17), /* divsi */
610 COSTS_N_INSNS (17), /* divdi */
611 COSTS_N_INSNS (3), /* fp */
612 COSTS_N_INSNS (3), /* dmul */
613 COSTS_N_INSNS (17), /* sdiv */
614 COSTS_N_INSNS (31), /* ddiv */
615 32, /* cache line size */
616 32, /* l1 cache */
617 512, /* l2 cache */
618 1, /* streams */
621 /* Instruction costs on PPC7450 processors. */
622 static const
623 struct processor_costs ppc7450_cost = {
624 COSTS_N_INSNS (4), /* mulsi */
625 COSTS_N_INSNS (3), /* mulsi_const */
626 COSTS_N_INSNS (3), /* mulsi_const9 */
627 COSTS_N_INSNS (4), /* muldi */
628 COSTS_N_INSNS (23), /* divsi */
629 COSTS_N_INSNS (23), /* divdi */
630 COSTS_N_INSNS (5), /* fp */
631 COSTS_N_INSNS (5), /* dmul */
632 COSTS_N_INSNS (21), /* sdiv */
633 COSTS_N_INSNS (35), /* ddiv */
634 32, /* cache line size */
635 32, /* l1 cache */
636 1024, /* l2 cache */
637 1, /* streams */
640 /* Instruction costs on PPC8540 processors. */
641 static const
642 struct processor_costs ppc8540_cost = {
643 COSTS_N_INSNS (4), /* mulsi */
644 COSTS_N_INSNS (4), /* mulsi_const */
645 COSTS_N_INSNS (4), /* mulsi_const9 */
646 COSTS_N_INSNS (4), /* muldi */
647 COSTS_N_INSNS (19), /* divsi */
648 COSTS_N_INSNS (19), /* divdi */
649 COSTS_N_INSNS (4), /* fp */
650 COSTS_N_INSNS (4), /* dmul */
651 COSTS_N_INSNS (29), /* sdiv */
652 COSTS_N_INSNS (29), /* ddiv */
653 32, /* cache line size */
654 32, /* l1 cache */
655 256, /* l2 cache */
656 1, /* prefetch streams /*/
659 /* Instruction costs on E300C2 and E300C3 cores. */
660 static const
661 struct processor_costs ppce300c2c3_cost = {
662 COSTS_N_INSNS (4), /* mulsi */
663 COSTS_N_INSNS (4), /* mulsi_const */
664 COSTS_N_INSNS (4), /* mulsi_const9 */
665 COSTS_N_INSNS (4), /* muldi */
666 COSTS_N_INSNS (19), /* divsi */
667 COSTS_N_INSNS (19), /* divdi */
668 COSTS_N_INSNS (3), /* fp */
669 COSTS_N_INSNS (4), /* dmul */
670 COSTS_N_INSNS (18), /* sdiv */
671 COSTS_N_INSNS (33), /* ddiv */
673 16, /* l1 cache */
674 16, /* l2 cache */
675 1, /* prefetch streams /*/
678 /* Instruction costs on PPCE500MC processors. */
679 static const
680 struct processor_costs ppce500mc_cost = {
681 COSTS_N_INSNS (4), /* mulsi */
682 COSTS_N_INSNS (4), /* mulsi_const */
683 COSTS_N_INSNS (4), /* mulsi_const9 */
684 COSTS_N_INSNS (4), /* muldi */
685 COSTS_N_INSNS (14), /* divsi */
686 COSTS_N_INSNS (14), /* divdi */
687 COSTS_N_INSNS (8), /* fp */
688 COSTS_N_INSNS (10), /* dmul */
689 COSTS_N_INSNS (36), /* sdiv */
690 COSTS_N_INSNS (66), /* ddiv */
691 64, /* cache line size */
692 32, /* l1 cache */
693 128, /* l2 cache */
694 1, /* prefetch streams /*/
697 /* Instruction costs on PPCE500MC64 processors. */
698 static const
699 struct processor_costs ppce500mc64_cost = {
700 COSTS_N_INSNS (4), /* mulsi */
701 COSTS_N_INSNS (4), /* mulsi_const */
702 COSTS_N_INSNS (4), /* mulsi_const9 */
703 COSTS_N_INSNS (4), /* muldi */
704 COSTS_N_INSNS (14), /* divsi */
705 COSTS_N_INSNS (14), /* divdi */
706 COSTS_N_INSNS (4), /* fp */
707 COSTS_N_INSNS (10), /* dmul */
708 COSTS_N_INSNS (36), /* sdiv */
709 COSTS_N_INSNS (66), /* ddiv */
710 64, /* cache line size */
711 32, /* l1 cache */
712 128, /* l2 cache */
713 1, /* prefetch streams /*/
716 /* Instruction costs on PPCE5500 processors. */
717 static const
718 struct processor_costs ppce5500_cost = {
719 COSTS_N_INSNS (5), /* mulsi */
720 COSTS_N_INSNS (5), /* mulsi_const */
721 COSTS_N_INSNS (4), /* mulsi_const9 */
722 COSTS_N_INSNS (5), /* muldi */
723 COSTS_N_INSNS (14), /* divsi */
724 COSTS_N_INSNS (14), /* divdi */
725 COSTS_N_INSNS (7), /* fp */
726 COSTS_N_INSNS (10), /* dmul */
727 COSTS_N_INSNS (36), /* sdiv */
728 COSTS_N_INSNS (66), /* ddiv */
729 64, /* cache line size */
730 32, /* l1 cache */
731 128, /* l2 cache */
732 1, /* prefetch streams /*/
735 /* Instruction costs on PPCE6500 processors. */
736 static const
737 struct processor_costs ppce6500_cost = {
738 COSTS_N_INSNS (5), /* mulsi */
739 COSTS_N_INSNS (5), /* mulsi_const */
740 COSTS_N_INSNS (4), /* mulsi_const9 */
741 COSTS_N_INSNS (5), /* muldi */
742 COSTS_N_INSNS (14), /* divsi */
743 COSTS_N_INSNS (14), /* divdi */
744 COSTS_N_INSNS (7), /* fp */
745 COSTS_N_INSNS (10), /* dmul */
746 COSTS_N_INSNS (36), /* sdiv */
747 COSTS_N_INSNS (66), /* ddiv */
748 64, /* cache line size */
749 32, /* l1 cache */
750 128, /* l2 cache */
751 1, /* prefetch streams /*/
754 /* Instruction costs on AppliedMicro Titan processors. */
755 static const
756 struct processor_costs titan_cost = {
757 COSTS_N_INSNS (5), /* mulsi */
758 COSTS_N_INSNS (5), /* mulsi_const */
759 COSTS_N_INSNS (5), /* mulsi_const9 */
760 COSTS_N_INSNS (5), /* muldi */
761 COSTS_N_INSNS (18), /* divsi */
762 COSTS_N_INSNS (18), /* divdi */
763 COSTS_N_INSNS (10), /* fp */
764 COSTS_N_INSNS (10), /* dmul */
765 COSTS_N_INSNS (46), /* sdiv */
766 COSTS_N_INSNS (72), /* ddiv */
767 32, /* cache line size */
768 32, /* l1 cache */
769 512, /* l2 cache */
770 1, /* prefetch streams /*/
773 /* Instruction costs on POWER4 and POWER5 processors. */
774 static const
775 struct processor_costs power4_cost = {
776 COSTS_N_INSNS (3), /* mulsi */
777 COSTS_N_INSNS (2), /* mulsi_const */
778 COSTS_N_INSNS (2), /* mulsi_const9 */
779 COSTS_N_INSNS (4), /* muldi */
780 COSTS_N_INSNS (18), /* divsi */
781 COSTS_N_INSNS (34), /* divdi */
782 COSTS_N_INSNS (3), /* fp */
783 COSTS_N_INSNS (3), /* dmul */
784 COSTS_N_INSNS (17), /* sdiv */
785 COSTS_N_INSNS (17), /* ddiv */
786 128, /* cache line size */
787 32, /* l1 cache */
788 1024, /* l2 cache */
789 8, /* prefetch streams /*/
792 /* Instruction costs on POWER6 processors. */
793 static const
794 struct processor_costs power6_cost = {
795 COSTS_N_INSNS (8), /* mulsi */
796 COSTS_N_INSNS (8), /* mulsi_const */
797 COSTS_N_INSNS (8), /* mulsi_const9 */
798 COSTS_N_INSNS (8), /* muldi */
799 COSTS_N_INSNS (22), /* divsi */
800 COSTS_N_INSNS (28), /* divdi */
801 COSTS_N_INSNS (3), /* fp */
802 COSTS_N_INSNS (3), /* dmul */
803 COSTS_N_INSNS (13), /* sdiv */
804 COSTS_N_INSNS (16), /* ddiv */
805 128, /* cache line size */
806 64, /* l1 cache */
807 2048, /* l2 cache */
808 16, /* prefetch streams */
811 /* Instruction costs on POWER7 processors. */
812 static const
813 struct processor_costs power7_cost = {
814 COSTS_N_INSNS (2), /* mulsi */
815 COSTS_N_INSNS (2), /* mulsi_const */
816 COSTS_N_INSNS (2), /* mulsi_const9 */
817 COSTS_N_INSNS (2), /* muldi */
818 COSTS_N_INSNS (18), /* divsi */
819 COSTS_N_INSNS (34), /* divdi */
820 COSTS_N_INSNS (3), /* fp */
821 COSTS_N_INSNS (3), /* dmul */
822 COSTS_N_INSNS (13), /* sdiv */
823 COSTS_N_INSNS (16), /* ddiv */
824 128, /* cache line size */
825 32, /* l1 cache */
826 256, /* l2 cache */
827 12, /* prefetch streams */
830 /* Instruction costs on POWER A2 processors. */
831 static const
832 struct processor_costs ppca2_cost = {
833 COSTS_N_INSNS (16), /* mulsi */
834 COSTS_N_INSNS (16), /* mulsi_const */
835 COSTS_N_INSNS (16), /* mulsi_const9 */
836 COSTS_N_INSNS (16), /* muldi */
837 COSTS_N_INSNS (22), /* divsi */
838 COSTS_N_INSNS (28), /* divdi */
839 COSTS_N_INSNS (3), /* fp */
840 COSTS_N_INSNS (3), /* dmul */
841 COSTS_N_INSNS (59), /* sdiv */
842 COSTS_N_INSNS (72), /* ddiv */
844 16, /* l1 cache */
845 2048, /* l2 cache */
846 16, /* prefetch streams */
850 /* Table that classifies rs6000 builtin functions (pure, const, etc.). */
851 #undef RS6000_BUILTIN_1
852 #undef RS6000_BUILTIN_2
853 #undef RS6000_BUILTIN_3
854 #undef RS6000_BUILTIN_A
855 #undef RS6000_BUILTIN_D
856 #undef RS6000_BUILTIN_E
857 #undef RS6000_BUILTIN_P
858 #undef RS6000_BUILTIN_Q
859 #undef RS6000_BUILTIN_S
860 #undef RS6000_BUILTIN_X
862 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE) \
863 { NAME, ICODE, MASK, ATTR },
865 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE) \
866 { NAME, ICODE, MASK, ATTR },
868 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE) \
869 { NAME, ICODE, MASK, ATTR },
871 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE) \
872 { NAME, ICODE, MASK, ATTR },
874 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE) \
875 { NAME, ICODE, MASK, ATTR },
877 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE) \
878 { NAME, ICODE, MASK, ATTR },
880 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE) \
881 { NAME, ICODE, MASK, ATTR },
883 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE) \
884 { NAME, ICODE, MASK, ATTR },
886 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE) \
887 { NAME, ICODE, MASK, ATTR },
889 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE) \
890 { NAME, ICODE, MASK, ATTR },
892 struct rs6000_builtin_info_type {
893 const char *name;
894 const enum insn_code icode;
895 const unsigned mask;
896 const unsigned attr;
899 static const struct rs6000_builtin_info_type rs6000_builtin_info[] =
901 #include "rs6000-builtin.def"
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_P
911 #undef RS6000_BUILTIN_Q
912 #undef RS6000_BUILTIN_S
913 #undef RS6000_BUILTIN_X
915 /* Support for -mveclibabi=<xxx> to control which vector library to use. */
916 static tree (*rs6000_veclib_handler) (tree, tree, tree);
919 static bool rs6000_debug_legitimate_address_p (enum machine_mode, rtx, bool);
920 static bool spe_func_has_64bit_regs_p (void);
921 static struct machine_function * rs6000_init_machine_status (void);
922 static int rs6000_ra_ever_killed (void);
923 static tree rs6000_handle_longcall_attribute (tree *, tree, tree, int, bool *);
924 static tree rs6000_handle_altivec_attribute (tree *, tree, tree, int, bool *);
925 static tree rs6000_handle_struct_attribute (tree *, tree, tree, int, bool *);
926 static tree rs6000_builtin_vectorized_libmass (tree, tree, tree);
927 static rtx rs6000_emit_set_long_const (rtx, HOST_WIDE_INT, HOST_WIDE_INT);
928 static int rs6000_memory_move_cost (enum machine_mode, reg_class_t, bool);
929 static bool rs6000_debug_rtx_costs (rtx, int, int, int, int *, bool);
930 static int rs6000_debug_address_cost (rtx, bool);
931 static int rs6000_debug_adjust_cost (rtx, rtx, rtx, int);
932 static bool is_microcoded_insn (rtx);
933 static bool is_nonpipeline_insn (rtx);
934 static bool is_cracked_insn (rtx);
935 static bool is_load_insn (rtx, rtx *);
936 static bool is_store_insn (rtx, rtx *);
937 static bool set_to_load_agen (rtx,rtx);
938 static bool insn_terminates_group_p (rtx , enum group_termination);
939 static bool insn_must_be_first_in_group (rtx);
940 static bool insn_must_be_last_in_group (rtx);
941 static void altivec_init_builtins (void);
942 static tree builtin_function_type (enum machine_mode, enum machine_mode,
943 enum machine_mode, enum machine_mode,
944 enum rs6000_builtins, const char *name);
945 static void rs6000_common_init_builtins (void);
946 static void paired_init_builtins (void);
947 static rtx paired_expand_predicate_builtin (enum insn_code, tree, rtx);
948 static void spe_init_builtins (void);
949 static rtx spe_expand_predicate_builtin (enum insn_code, tree, rtx);
950 static rtx spe_expand_evsel_builtin (enum insn_code, tree, rtx);
951 static int rs6000_emit_int_cmove (rtx, rtx, rtx, rtx);
952 static rs6000_stack_t *rs6000_stack_info (void);
953 static void is_altivec_return_reg (rtx, void *);
954 int easy_vector_constant (rtx, enum machine_mode);
955 static rtx rs6000_debug_legitimize_address (rtx, rtx, enum machine_mode);
956 static rtx rs6000_legitimize_tls_address (rtx, enum tls_model);
957 static int rs6000_tls_symbol_ref_1 (rtx *, void *);
958 static int rs6000_get_some_local_dynamic_name_1 (rtx *, void *);
959 static rtx rs6000_darwin64_record_arg (CUMULATIVE_ARGS *, const_tree,
960 bool, bool);
961 #if TARGET_MACHO
962 static void macho_branch_islands (void);
963 #endif
964 static rtx rs6000_legitimize_reload_address (rtx, enum machine_mode, int, int,
965 int, int *);
966 static rtx rs6000_debug_legitimize_reload_address (rtx, enum machine_mode, int,
967 int, int, int *);
968 static bool rs6000_mode_dependent_address (const_rtx);
969 static bool rs6000_debug_mode_dependent_address (const_rtx);
970 static enum reg_class rs6000_secondary_reload_class (enum reg_class,
971 enum machine_mode, rtx);
972 static enum reg_class rs6000_debug_secondary_reload_class (enum reg_class,
973 enum machine_mode,
974 rtx);
975 static enum reg_class rs6000_preferred_reload_class (rtx, enum reg_class);
976 static enum reg_class rs6000_debug_preferred_reload_class (rtx,
977 enum reg_class);
978 static bool rs6000_secondary_memory_needed (enum reg_class, enum reg_class,
979 enum machine_mode);
980 static bool rs6000_debug_secondary_memory_needed (enum reg_class,
981 enum reg_class,
982 enum machine_mode);
983 static bool rs6000_cannot_change_mode_class (enum machine_mode,
984 enum machine_mode,
985 enum reg_class);
986 static bool rs6000_debug_cannot_change_mode_class (enum machine_mode,
987 enum machine_mode,
988 enum reg_class);
989 static bool rs6000_save_toc_in_prologue_p (void);
991 rtx (*rs6000_legitimize_reload_address_ptr) (rtx, enum machine_mode, int, int,
992 int, int *)
993 = rs6000_legitimize_reload_address;
995 static bool (*rs6000_mode_dependent_address_ptr) (const_rtx)
996 = rs6000_mode_dependent_address;
998 enum reg_class (*rs6000_secondary_reload_class_ptr) (enum reg_class,
999 enum machine_mode, rtx)
1000 = rs6000_secondary_reload_class;
1002 enum reg_class (*rs6000_preferred_reload_class_ptr) (rtx, enum reg_class)
1003 = rs6000_preferred_reload_class;
1005 bool (*rs6000_secondary_memory_needed_ptr) (enum reg_class, enum reg_class,
1006 enum machine_mode)
1007 = rs6000_secondary_memory_needed;
1009 bool (*rs6000_cannot_change_mode_class_ptr) (enum machine_mode,
1010 enum machine_mode,
1011 enum reg_class)
1012 = rs6000_cannot_change_mode_class;
1014 const int INSN_NOT_AVAILABLE = -1;
1016 /* Hash table stuff for keeping track of TOC entries. */
1018 struct GTY(()) toc_hash_struct
1020 /* `key' will satisfy CONSTANT_P; in fact, it will satisfy
1021 ASM_OUTPUT_SPECIAL_POOL_ENTRY_P. */
1022 rtx key;
1023 enum machine_mode key_mode;
1024 int labelno;
1027 static GTY ((param_is (struct toc_hash_struct))) htab_t toc_hash_table;
1029 /* Hash table to keep track of the argument types for builtin functions. */
1031 struct GTY(()) builtin_hash_struct
1033 tree type;
1034 enum machine_mode mode[4]; /* return value + 3 arguments. */
1035 unsigned char uns_p[4]; /* and whether the types are unsigned. */
1038 static GTY ((param_is (struct builtin_hash_struct))) htab_t builtin_hash_table;
1041 /* Default register names. */
1042 char rs6000_reg_names[][8] =
1044 "0", "1", "2", "3", "4", "5", "6", "7",
1045 "8", "9", "10", "11", "12", "13", "14", "15",
1046 "16", "17", "18", "19", "20", "21", "22", "23",
1047 "24", "25", "26", "27", "28", "29", "30", "31",
1048 "0", "1", "2", "3", "4", "5", "6", "7",
1049 "8", "9", "10", "11", "12", "13", "14", "15",
1050 "16", "17", "18", "19", "20", "21", "22", "23",
1051 "24", "25", "26", "27", "28", "29", "30", "31",
1052 "mq", "lr", "ctr","ap",
1053 "0", "1", "2", "3", "4", "5", "6", "7",
1054 "ca",
1055 /* AltiVec registers. */
1056 "0", "1", "2", "3", "4", "5", "6", "7",
1057 "8", "9", "10", "11", "12", "13", "14", "15",
1058 "16", "17", "18", "19", "20", "21", "22", "23",
1059 "24", "25", "26", "27", "28", "29", "30", "31",
1060 "vrsave", "vscr",
1061 /* SPE registers. */
1062 "spe_acc", "spefscr",
1063 /* Soft frame pointer. */
1064 "sfp"
1067 #ifdef TARGET_REGNAMES
1068 static const char alt_reg_names[][8] =
1070 "%r0", "%r1", "%r2", "%r3", "%r4", "%r5", "%r6", "%r7",
1071 "%r8", "%r9", "%r10", "%r11", "%r12", "%r13", "%r14", "%r15",
1072 "%r16", "%r17", "%r18", "%r19", "%r20", "%r21", "%r22", "%r23",
1073 "%r24", "%r25", "%r26", "%r27", "%r28", "%r29", "%r30", "%r31",
1074 "%f0", "%f1", "%f2", "%f3", "%f4", "%f5", "%f6", "%f7",
1075 "%f8", "%f9", "%f10", "%f11", "%f12", "%f13", "%f14", "%f15",
1076 "%f16", "%f17", "%f18", "%f19", "%f20", "%f21", "%f22", "%f23",
1077 "%f24", "%f25", "%f26", "%f27", "%f28", "%f29", "%f30", "%f31",
1078 "mq", "lr", "ctr", "ap",
1079 "%cr0", "%cr1", "%cr2", "%cr3", "%cr4", "%cr5", "%cr6", "%cr7",
1080 "ca",
1081 /* AltiVec registers. */
1082 "%v0", "%v1", "%v2", "%v3", "%v4", "%v5", "%v6", "%v7",
1083 "%v8", "%v9", "%v10", "%v11", "%v12", "%v13", "%v14", "%v15",
1084 "%v16", "%v17", "%v18", "%v19", "%v20", "%v21", "%v22", "%v23",
1085 "%v24", "%v25", "%v26", "%v27", "%v28", "%v29", "%v30", "%v31",
1086 "vrsave", "vscr",
1087 /* SPE registers. */
1088 "spe_acc", "spefscr",
1089 /* Soft frame pointer. */
1090 "sfp"
1092 #endif
1094 /* Table of valid machine attributes. */
1096 static const struct attribute_spec rs6000_attribute_table[] =
1098 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
1099 affects_type_identity } */
1100 { "altivec", 1, 1, false, true, false, rs6000_handle_altivec_attribute,
1101 false },
1102 { "longcall", 0, 0, false, true, true, rs6000_handle_longcall_attribute,
1103 false },
1104 { "shortcall", 0, 0, false, true, true, rs6000_handle_longcall_attribute,
1105 false },
1106 { "ms_struct", 0, 0, false, false, false, rs6000_handle_struct_attribute,
1107 false },
1108 { "gcc_struct", 0, 0, false, false, false, rs6000_handle_struct_attribute,
1109 false },
1110 #ifdef SUBTARGET_ATTRIBUTE_TABLE
1111 SUBTARGET_ATTRIBUTE_TABLE,
1112 #endif
1113 { NULL, 0, 0, false, false, false, NULL, false }
1116 #ifndef MASK_STRICT_ALIGN
1117 #define MASK_STRICT_ALIGN 0
1118 #endif
1119 #ifndef TARGET_PROFILE_KERNEL
1120 #define TARGET_PROFILE_KERNEL 0
1121 #endif
1123 /* The VRSAVE bitmask puts bit %v0 as the most significant bit. */
1124 #define ALTIVEC_REG_BIT(REGNO) (0x80000000 >> ((REGNO) - FIRST_ALTIVEC_REGNO))
1126 /* Initialize the GCC target structure. */
1127 #undef TARGET_ATTRIBUTE_TABLE
1128 #define TARGET_ATTRIBUTE_TABLE rs6000_attribute_table
1129 #undef TARGET_SET_DEFAULT_TYPE_ATTRIBUTES
1130 #define TARGET_SET_DEFAULT_TYPE_ATTRIBUTES rs6000_set_default_type_attributes
1131 #undef TARGET_ATTRIBUTE_TAKES_IDENTIFIER_P
1132 #define TARGET_ATTRIBUTE_TAKES_IDENTIFIER_P rs6000_attribute_takes_identifier_p
1134 #undef TARGET_ASM_ALIGNED_DI_OP
1135 #define TARGET_ASM_ALIGNED_DI_OP DOUBLE_INT_ASM_OP
1137 /* Default unaligned ops are only provided for ELF. Find the ops needed
1138 for non-ELF systems. */
1139 #ifndef OBJECT_FORMAT_ELF
1140 #if TARGET_XCOFF
1141 /* For XCOFF. rs6000_assemble_integer will handle unaligned DIs on
1142 64-bit targets. */
1143 #undef TARGET_ASM_UNALIGNED_HI_OP
1144 #define TARGET_ASM_UNALIGNED_HI_OP "\t.vbyte\t2,"
1145 #undef TARGET_ASM_UNALIGNED_SI_OP
1146 #define TARGET_ASM_UNALIGNED_SI_OP "\t.vbyte\t4,"
1147 #undef TARGET_ASM_UNALIGNED_DI_OP
1148 #define TARGET_ASM_UNALIGNED_DI_OP "\t.vbyte\t8,"
1149 #else
1150 /* For Darwin. */
1151 #undef TARGET_ASM_UNALIGNED_HI_OP
1152 #define TARGET_ASM_UNALIGNED_HI_OP "\t.short\t"
1153 #undef TARGET_ASM_UNALIGNED_SI_OP
1154 #define TARGET_ASM_UNALIGNED_SI_OP "\t.long\t"
1155 #undef TARGET_ASM_UNALIGNED_DI_OP
1156 #define TARGET_ASM_UNALIGNED_DI_OP "\t.quad\t"
1157 #undef TARGET_ASM_ALIGNED_DI_OP
1158 #define TARGET_ASM_ALIGNED_DI_OP "\t.quad\t"
1159 #endif
1160 #endif
1162 /* This hook deals with fixups for relocatable code and DI-mode objects
1163 in 64-bit code. */
1164 #undef TARGET_ASM_INTEGER
1165 #define TARGET_ASM_INTEGER rs6000_assemble_integer
1167 #if defined (HAVE_GAS_HIDDEN) && !TARGET_MACHO
1168 #undef TARGET_ASM_ASSEMBLE_VISIBILITY
1169 #define TARGET_ASM_ASSEMBLE_VISIBILITY rs6000_assemble_visibility
1170 #endif
1172 #undef TARGET_SET_UP_BY_PROLOGUE
1173 #define TARGET_SET_UP_BY_PROLOGUE rs6000_set_up_by_prologue
1175 #undef TARGET_HAVE_TLS
1176 #define TARGET_HAVE_TLS HAVE_AS_TLS
1178 #undef TARGET_CANNOT_FORCE_CONST_MEM
1179 #define TARGET_CANNOT_FORCE_CONST_MEM rs6000_cannot_force_const_mem
1181 #undef TARGET_DELEGITIMIZE_ADDRESS
1182 #define TARGET_DELEGITIMIZE_ADDRESS rs6000_delegitimize_address
1184 #undef TARGET_CONST_NOT_OK_FOR_DEBUG_P
1185 #define TARGET_CONST_NOT_OK_FOR_DEBUG_P rs6000_const_not_ok_for_debug_p
1187 #undef TARGET_ASM_FUNCTION_PROLOGUE
1188 #define TARGET_ASM_FUNCTION_PROLOGUE rs6000_output_function_prologue
1189 #undef TARGET_ASM_FUNCTION_EPILOGUE
1190 #define TARGET_ASM_FUNCTION_EPILOGUE rs6000_output_function_epilogue
1192 #undef TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA
1193 #define TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA rs6000_output_addr_const_extra
1195 #undef TARGET_LEGITIMIZE_ADDRESS
1196 #define TARGET_LEGITIMIZE_ADDRESS rs6000_legitimize_address
1198 #undef TARGET_SCHED_VARIABLE_ISSUE
1199 #define TARGET_SCHED_VARIABLE_ISSUE rs6000_variable_issue
1201 #undef TARGET_SCHED_ISSUE_RATE
1202 #define TARGET_SCHED_ISSUE_RATE rs6000_issue_rate
1203 #undef TARGET_SCHED_ADJUST_COST
1204 #define TARGET_SCHED_ADJUST_COST rs6000_adjust_cost
1205 #undef TARGET_SCHED_ADJUST_PRIORITY
1206 #define TARGET_SCHED_ADJUST_PRIORITY rs6000_adjust_priority
1207 #undef TARGET_SCHED_IS_COSTLY_DEPENDENCE
1208 #define TARGET_SCHED_IS_COSTLY_DEPENDENCE rs6000_is_costly_dependence
1209 #undef TARGET_SCHED_INIT
1210 #define TARGET_SCHED_INIT rs6000_sched_init
1211 #undef TARGET_SCHED_FINISH
1212 #define TARGET_SCHED_FINISH rs6000_sched_finish
1213 #undef TARGET_SCHED_REORDER
1214 #define TARGET_SCHED_REORDER rs6000_sched_reorder
1215 #undef TARGET_SCHED_REORDER2
1216 #define TARGET_SCHED_REORDER2 rs6000_sched_reorder2
1218 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD
1219 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD rs6000_use_sched_lookahead
1221 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD_GUARD
1222 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD_GUARD rs6000_use_sched_lookahead_guard
1224 #undef TARGET_SCHED_ALLOC_SCHED_CONTEXT
1225 #define TARGET_SCHED_ALLOC_SCHED_CONTEXT rs6000_alloc_sched_context
1226 #undef TARGET_SCHED_INIT_SCHED_CONTEXT
1227 #define TARGET_SCHED_INIT_SCHED_CONTEXT rs6000_init_sched_context
1228 #undef TARGET_SCHED_SET_SCHED_CONTEXT
1229 #define TARGET_SCHED_SET_SCHED_CONTEXT rs6000_set_sched_context
1230 #undef TARGET_SCHED_FREE_SCHED_CONTEXT
1231 #define TARGET_SCHED_FREE_SCHED_CONTEXT rs6000_free_sched_context
1233 #undef TARGET_VECTORIZE_BUILTIN_MASK_FOR_LOAD
1234 #define TARGET_VECTORIZE_BUILTIN_MASK_FOR_LOAD rs6000_builtin_mask_for_load
1235 #undef TARGET_VECTORIZE_SUPPORT_VECTOR_MISALIGNMENT
1236 #define TARGET_VECTORIZE_SUPPORT_VECTOR_MISALIGNMENT \
1237 rs6000_builtin_support_vector_misalignment
1238 #undef TARGET_VECTORIZE_VECTOR_ALIGNMENT_REACHABLE
1239 #define TARGET_VECTORIZE_VECTOR_ALIGNMENT_REACHABLE rs6000_vector_alignment_reachable
1240 #undef TARGET_VECTORIZE_BUILTIN_VECTORIZATION_COST
1241 #define TARGET_VECTORIZE_BUILTIN_VECTORIZATION_COST \
1242 rs6000_builtin_vectorization_cost
1243 #undef TARGET_VECTORIZE_PREFERRED_SIMD_MODE
1244 #define TARGET_VECTORIZE_PREFERRED_SIMD_MODE \
1245 rs6000_preferred_simd_mode
1246 #undef TARGET_VECTORIZE_INIT_COST
1247 #define TARGET_VECTORIZE_INIT_COST rs6000_init_cost
1248 #undef TARGET_VECTORIZE_ADD_STMT_COST
1249 #define TARGET_VECTORIZE_ADD_STMT_COST rs6000_add_stmt_cost
1250 #undef TARGET_VECTORIZE_FINISH_COST
1251 #define TARGET_VECTORIZE_FINISH_COST rs6000_finish_cost
1252 #undef TARGET_VECTORIZE_DESTROY_COST_DATA
1253 #define TARGET_VECTORIZE_DESTROY_COST_DATA rs6000_destroy_cost_data
1255 #undef TARGET_INIT_BUILTINS
1256 #define TARGET_INIT_BUILTINS rs6000_init_builtins
1257 #undef TARGET_BUILTIN_DECL
1258 #define TARGET_BUILTIN_DECL rs6000_builtin_decl
1260 #undef TARGET_EXPAND_BUILTIN
1261 #define TARGET_EXPAND_BUILTIN rs6000_expand_builtin
1263 #undef TARGET_MANGLE_TYPE
1264 #define TARGET_MANGLE_TYPE rs6000_mangle_type
1266 #undef TARGET_INIT_LIBFUNCS
1267 #define TARGET_INIT_LIBFUNCS rs6000_init_libfuncs
1269 #if TARGET_MACHO
1270 #undef TARGET_BINDS_LOCAL_P
1271 #define TARGET_BINDS_LOCAL_P darwin_binds_local_p
1272 #endif
1274 #undef TARGET_MS_BITFIELD_LAYOUT_P
1275 #define TARGET_MS_BITFIELD_LAYOUT_P rs6000_ms_bitfield_layout_p
1277 #undef TARGET_ASM_OUTPUT_MI_THUNK
1278 #define TARGET_ASM_OUTPUT_MI_THUNK rs6000_output_mi_thunk
1280 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
1281 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK hook_bool_const_tree_hwi_hwi_const_tree_true
1283 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
1284 #define TARGET_FUNCTION_OK_FOR_SIBCALL rs6000_function_ok_for_sibcall
1286 #undef TARGET_INVALID_WITHIN_DOLOOP
1287 #define TARGET_INVALID_WITHIN_DOLOOP rs6000_invalid_within_doloop
1289 #undef TARGET_REGISTER_MOVE_COST
1290 #define TARGET_REGISTER_MOVE_COST rs6000_register_move_cost
1291 #undef TARGET_MEMORY_MOVE_COST
1292 #define TARGET_MEMORY_MOVE_COST rs6000_memory_move_cost
1293 #undef TARGET_RTX_COSTS
1294 #define TARGET_RTX_COSTS rs6000_rtx_costs
1295 #undef TARGET_ADDRESS_COST
1296 #define TARGET_ADDRESS_COST hook_int_rtx_bool_0
1298 #undef TARGET_DWARF_REGISTER_SPAN
1299 #define TARGET_DWARF_REGISTER_SPAN rs6000_dwarf_register_span
1301 #undef TARGET_INIT_DWARF_REG_SIZES_EXTRA
1302 #define TARGET_INIT_DWARF_REG_SIZES_EXTRA rs6000_init_dwarf_reg_sizes_extra
1304 /* On rs6000, function arguments are promoted, as are function return
1305 values. */
1306 #undef TARGET_PROMOTE_FUNCTION_MODE
1307 #define TARGET_PROMOTE_FUNCTION_MODE default_promote_function_mode_always_promote
1309 #undef TARGET_RETURN_IN_MEMORY
1310 #define TARGET_RETURN_IN_MEMORY rs6000_return_in_memory
1312 #undef TARGET_SETUP_INCOMING_VARARGS
1313 #define TARGET_SETUP_INCOMING_VARARGS setup_incoming_varargs
1315 /* Always strict argument naming on rs6000. */
1316 #undef TARGET_STRICT_ARGUMENT_NAMING
1317 #define TARGET_STRICT_ARGUMENT_NAMING hook_bool_CUMULATIVE_ARGS_true
1318 #undef TARGET_PRETEND_OUTGOING_VARARGS_NAMED
1319 #define TARGET_PRETEND_OUTGOING_VARARGS_NAMED hook_bool_CUMULATIVE_ARGS_true
1320 #undef TARGET_SPLIT_COMPLEX_ARG
1321 #define TARGET_SPLIT_COMPLEX_ARG hook_bool_const_tree_true
1322 #undef TARGET_MUST_PASS_IN_STACK
1323 #define TARGET_MUST_PASS_IN_STACK rs6000_must_pass_in_stack
1324 #undef TARGET_PASS_BY_REFERENCE
1325 #define TARGET_PASS_BY_REFERENCE rs6000_pass_by_reference
1326 #undef TARGET_ARG_PARTIAL_BYTES
1327 #define TARGET_ARG_PARTIAL_BYTES rs6000_arg_partial_bytes
1328 #undef TARGET_FUNCTION_ARG_ADVANCE
1329 #define TARGET_FUNCTION_ARG_ADVANCE rs6000_function_arg_advance
1330 #undef TARGET_FUNCTION_ARG
1331 #define TARGET_FUNCTION_ARG rs6000_function_arg
1332 #undef TARGET_FUNCTION_ARG_BOUNDARY
1333 #define TARGET_FUNCTION_ARG_BOUNDARY rs6000_function_arg_boundary
1335 #undef TARGET_BUILD_BUILTIN_VA_LIST
1336 #define TARGET_BUILD_BUILTIN_VA_LIST rs6000_build_builtin_va_list
1338 #undef TARGET_EXPAND_BUILTIN_VA_START
1339 #define TARGET_EXPAND_BUILTIN_VA_START rs6000_va_start
1341 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
1342 #define TARGET_GIMPLIFY_VA_ARG_EXPR rs6000_gimplify_va_arg
1344 #undef TARGET_EH_RETURN_FILTER_MODE
1345 #define TARGET_EH_RETURN_FILTER_MODE rs6000_eh_return_filter_mode
1347 #undef TARGET_SCALAR_MODE_SUPPORTED_P
1348 #define TARGET_SCALAR_MODE_SUPPORTED_P rs6000_scalar_mode_supported_p
1350 #undef TARGET_VECTOR_MODE_SUPPORTED_P
1351 #define TARGET_VECTOR_MODE_SUPPORTED_P rs6000_vector_mode_supported_p
1353 #undef TARGET_INVALID_ARG_FOR_UNPROTOTYPED_FN
1354 #define TARGET_INVALID_ARG_FOR_UNPROTOTYPED_FN invalid_arg_for_unprototyped_fn
1356 #undef TARGET_ASM_LOOP_ALIGN_MAX_SKIP
1357 #define TARGET_ASM_LOOP_ALIGN_MAX_SKIP rs6000_loop_align_max_skip
1359 #undef TARGET_OPTION_OVERRIDE
1360 #define TARGET_OPTION_OVERRIDE rs6000_option_override
1362 #undef TARGET_VECTORIZE_BUILTIN_VECTORIZED_FUNCTION
1363 #define TARGET_VECTORIZE_BUILTIN_VECTORIZED_FUNCTION \
1364 rs6000_builtin_vectorized_function
1366 #if !TARGET_MACHO
1367 #undef TARGET_STACK_PROTECT_FAIL
1368 #define TARGET_STACK_PROTECT_FAIL rs6000_stack_protect_fail
1369 #endif
1371 /* MPC604EUM 3.5.2 Weak Consistency between Multiple Processors
1372 The PowerPC architecture requires only weak consistency among
1373 processors--that is, memory accesses between processors need not be
1374 sequentially consistent and memory accesses among processors can occur
1375 in any order. The ability to order memory accesses weakly provides
1376 opportunities for more efficient use of the system bus. Unless a
1377 dependency exists, the 604e allows read operations to precede store
1378 operations. */
1379 #undef TARGET_RELAXED_ORDERING
1380 #define TARGET_RELAXED_ORDERING true
1382 #ifdef HAVE_AS_TLS
1383 #undef TARGET_ASM_OUTPUT_DWARF_DTPREL
1384 #define TARGET_ASM_OUTPUT_DWARF_DTPREL rs6000_output_dwarf_dtprel
1385 #endif
1387 /* Use a 32-bit anchor range. This leads to sequences like:
1389 addis tmp,anchor,high
1390 add dest,tmp,low
1392 where tmp itself acts as an anchor, and can be shared between
1393 accesses to the same 64k page. */
1394 #undef TARGET_MIN_ANCHOR_OFFSET
1395 #define TARGET_MIN_ANCHOR_OFFSET -0x7fffffff - 1
1396 #undef TARGET_MAX_ANCHOR_OFFSET
1397 #define TARGET_MAX_ANCHOR_OFFSET 0x7fffffff
1398 #undef TARGET_USE_BLOCKS_FOR_CONSTANT_P
1399 #define TARGET_USE_BLOCKS_FOR_CONSTANT_P rs6000_use_blocks_for_constant_p
1401 #undef TARGET_BUILTIN_RECIPROCAL
1402 #define TARGET_BUILTIN_RECIPROCAL rs6000_builtin_reciprocal
1404 #undef TARGET_EXPAND_TO_RTL_HOOK
1405 #define TARGET_EXPAND_TO_RTL_HOOK rs6000_alloc_sdmode_stack_slot
1407 #undef TARGET_INSTANTIATE_DECLS
1408 #define TARGET_INSTANTIATE_DECLS rs6000_instantiate_decls
1410 #undef TARGET_SECONDARY_RELOAD
1411 #define TARGET_SECONDARY_RELOAD rs6000_secondary_reload
1413 #undef TARGET_LEGITIMATE_ADDRESS_P
1414 #define TARGET_LEGITIMATE_ADDRESS_P rs6000_legitimate_address_p
1416 #undef TARGET_MODE_DEPENDENT_ADDRESS_P
1417 #define TARGET_MODE_DEPENDENT_ADDRESS_P rs6000_mode_dependent_address_p
1419 #undef TARGET_CAN_ELIMINATE
1420 #define TARGET_CAN_ELIMINATE rs6000_can_eliminate
1422 #undef TARGET_CONDITIONAL_REGISTER_USAGE
1423 #define TARGET_CONDITIONAL_REGISTER_USAGE rs6000_conditional_register_usage
1425 #undef TARGET_TRAMPOLINE_INIT
1426 #define TARGET_TRAMPOLINE_INIT rs6000_trampoline_init
1428 #undef TARGET_FUNCTION_VALUE
1429 #define TARGET_FUNCTION_VALUE rs6000_function_value
1431 #undef TARGET_OPTION_VALID_ATTRIBUTE_P
1432 #define TARGET_OPTION_VALID_ATTRIBUTE_P rs6000_valid_attribute_p
1434 #undef TARGET_OPTION_SAVE
1435 #define TARGET_OPTION_SAVE rs6000_function_specific_save
1437 #undef TARGET_OPTION_RESTORE
1438 #define TARGET_OPTION_RESTORE rs6000_function_specific_restore
1440 #undef TARGET_OPTION_PRINT
1441 #define TARGET_OPTION_PRINT rs6000_function_specific_print
1443 #undef TARGET_CAN_INLINE_P
1444 #define TARGET_CAN_INLINE_P rs6000_can_inline_p
1446 #undef TARGET_SET_CURRENT_FUNCTION
1447 #define TARGET_SET_CURRENT_FUNCTION rs6000_set_current_function
1449 #undef TARGET_LEGITIMATE_CONSTANT_P
1450 #define TARGET_LEGITIMATE_CONSTANT_P rs6000_legitimate_constant_p
1452 #undef TARGET_VECTORIZE_VEC_PERM_CONST_OK
1453 #define TARGET_VECTORIZE_VEC_PERM_CONST_OK rs6000_vectorize_vec_perm_const_ok
1456 /* Simplifications for entries below. */
1458 enum {
1459 POWERPC_BASE_MASK = MASK_POWERPC | MASK_NEW_MNEMONICS,
1460 POWERPC_7400_MASK = POWERPC_BASE_MASK | MASK_PPC_GFXOPT | MASK_ALTIVEC
1463 /* Some OSs don't support saving the high part of 64-bit registers on context
1464 switch. Other OSs don't support saving Altivec registers. On those OSs, we
1465 don't touch the MASK_POWERPC64 or MASK_ALTIVEC settings; if the user wants
1466 either, the user must explicitly specify them and we won't interfere with
1467 the user's specification. */
1469 enum {
1470 POWER_MASKS = MASK_POWER | MASK_POWER2 | MASK_MULTIPLE | MASK_STRING,
1471 POWERPC_MASKS = (POWERPC_BASE_MASK | MASK_PPC_GPOPT | MASK_STRICT_ALIGN
1472 | MASK_PPC_GFXOPT | MASK_POWERPC64 | MASK_ALTIVEC
1473 | MASK_MFCRF | MASK_POPCNTB | MASK_FPRND | MASK_MULHW
1474 | MASK_DLMZB | MASK_CMPB | MASK_MFPGPR | MASK_DFP
1475 | MASK_POPCNTD | MASK_VSX | MASK_ISEL | MASK_NO_UPDATE
1476 | MASK_RECIP_PRECISION)
1479 /* Masks for instructions set at various powerpc ISAs. */
1480 enum {
1481 ISA_2_1_MASKS = MASK_MFCRF,
1482 ISA_2_2_MASKS = (ISA_2_1_MASKS | MASK_POPCNTB),
1483 ISA_2_4_MASKS = (ISA_2_2_MASKS | MASK_FPRND),
1485 /* For ISA 2.05, do not add MFPGPR, since it isn't in ISA 2.06, and don't add
1486 ALTIVEC, since in general it isn't a win on power6. In ISA 2.04, fsel,
1487 fre, fsqrt, etc. were no longer documented as optional. Group masks by
1488 server and embedded. */
1489 ISA_2_5_MASKS_EMBEDDED = (ISA_2_2_MASKS | MASK_CMPB | MASK_RECIP_PRECISION
1490 | MASK_PPC_GFXOPT | MASK_PPC_GPOPT),
1491 ISA_2_5_MASKS_SERVER = (ISA_2_5_MASKS_EMBEDDED | MASK_DFP),
1493 /* For ISA 2.06, don't add ISEL, since in general it isn't a win, but
1494 altivec is a win so enable it. */
1495 ISA_2_6_MASKS_EMBEDDED = (ISA_2_5_MASKS_EMBEDDED | MASK_POPCNTD),
1496 ISA_2_6_MASKS_SERVER = (ISA_2_5_MASKS_SERVER | MASK_POPCNTD | MASK_ALTIVEC
1497 | MASK_VSX)
1500 struct rs6000_ptt
1502 const char *const name; /* Canonical processor name. */
1503 const enum processor_type processor; /* Processor type enum value. */
1504 const int target_enable; /* Target flags to enable. */
1507 static struct rs6000_ptt const processor_target_table[] =
1509 #define RS6000_CPU(NAME, CPU, FLAGS) { NAME, CPU, FLAGS },
1510 #include "rs6000-cpus.def"
1511 #undef RS6000_CPU
1514 /* Look up a processor name for -mcpu=xxx and -mtune=xxx. Return -1 if the
1515 name is invalid. */
1517 static int
1518 rs6000_cpu_name_lookup (const char *name)
1520 size_t i;
1522 if (name != NULL)
1524 for (i = 0; i < ARRAY_SIZE (processor_target_table); i++)
1525 if (! strcmp (name, processor_target_table[i].name))
1526 return (int)i;
1529 return -1;
1533 /* Return number of consecutive hard regs needed starting at reg REGNO
1534 to hold something of mode MODE.
1535 This is ordinarily the length in words of a value of mode MODE
1536 but can be less for certain modes in special long registers.
1538 For the SPE, GPRs are 64 bits but only 32 bits are visible in
1539 scalar instructions. The upper 32 bits are only available to the
1540 SIMD instructions.
1542 POWER and PowerPC GPRs hold 32 bits worth;
1543 PowerPC64 GPRs and FPRs point register holds 64 bits worth. */
1545 static int
1546 rs6000_hard_regno_nregs_internal (int regno, enum machine_mode mode)
1548 unsigned HOST_WIDE_INT reg_size;
1550 if (FP_REGNO_P (regno))
1551 reg_size = (VECTOR_MEM_VSX_P (mode)
1552 ? UNITS_PER_VSX_WORD
1553 : UNITS_PER_FP_WORD);
1555 else if (SPE_SIMD_REGNO_P (regno) && TARGET_SPE && SPE_VECTOR_MODE (mode))
1556 reg_size = UNITS_PER_SPE_WORD;
1558 else if (ALTIVEC_REGNO_P (regno))
1559 reg_size = UNITS_PER_ALTIVEC_WORD;
1561 /* The value returned for SCmode in the E500 double case is 2 for
1562 ABI compatibility; storing an SCmode value in a single register
1563 would require function_arg and rs6000_spe_function_arg to handle
1564 SCmode so as to pass the value correctly in a pair of
1565 registers. */
1566 else if (TARGET_E500_DOUBLE && FLOAT_MODE_P (mode) && mode != SCmode
1567 && !DECIMAL_FLOAT_MODE_P (mode))
1568 reg_size = UNITS_PER_FP_WORD;
1570 else
1571 reg_size = UNITS_PER_WORD;
1573 return (GET_MODE_SIZE (mode) + reg_size - 1) / reg_size;
1576 /* Value is 1 if hard register REGNO can hold a value of machine-mode
1577 MODE. */
1578 static int
1579 rs6000_hard_regno_mode_ok (int regno, enum machine_mode mode)
1581 int last_regno = regno + rs6000_hard_regno_nregs[mode][regno] - 1;
1583 /* VSX registers that overlap the FPR registers are larger than for non-VSX
1584 implementations. Don't allow an item to be split between a FP register
1585 and an Altivec register. */
1586 if (VECTOR_MEM_VSX_P (mode))
1588 if (FP_REGNO_P (regno))
1589 return FP_REGNO_P (last_regno);
1591 if (ALTIVEC_REGNO_P (regno))
1592 return ALTIVEC_REGNO_P (last_regno);
1595 /* The GPRs can hold any mode, but values bigger than one register
1596 cannot go past R31. */
1597 if (INT_REGNO_P (regno))
1598 return INT_REGNO_P (last_regno);
1600 /* The float registers (except for VSX vector modes) can only hold floating
1601 modes and DImode. This excludes the 32-bit decimal float mode for
1602 now. */
1603 if (FP_REGNO_P (regno))
1605 if (SCALAR_FLOAT_MODE_P (mode)
1606 && (mode != TDmode || (regno % 2) == 0)
1607 && FP_REGNO_P (last_regno))
1608 return 1;
1610 if (GET_MODE_CLASS (mode) == MODE_INT
1611 && GET_MODE_SIZE (mode) == UNITS_PER_FP_WORD)
1612 return 1;
1614 if (PAIRED_SIMD_REGNO_P (regno) && TARGET_PAIRED_FLOAT
1615 && PAIRED_VECTOR_MODE (mode))
1616 return 1;
1618 return 0;
1621 /* The CR register can only hold CC modes. */
1622 if (CR_REGNO_P (regno))
1623 return GET_MODE_CLASS (mode) == MODE_CC;
1625 if (CA_REGNO_P (regno))
1626 return mode == BImode;
1628 /* AltiVec only in AldyVec registers. */
1629 if (ALTIVEC_REGNO_P (regno))
1630 return VECTOR_MEM_ALTIVEC_OR_VSX_P (mode);
1632 /* ...but GPRs can hold SIMD data on the SPE in one register. */
1633 if (SPE_SIMD_REGNO_P (regno) && TARGET_SPE && SPE_VECTOR_MODE (mode))
1634 return 1;
1636 /* We cannot put TImode anywhere except general register and it must be able
1637 to fit within the register set. In the future, allow TImode in the
1638 Altivec or VSX registers. */
1640 return GET_MODE_SIZE (mode) <= UNITS_PER_WORD;
1643 /* Print interesting facts about registers. */
1644 static void
1645 rs6000_debug_reg_print (int first_regno, int last_regno, const char *reg_name)
1647 int r, m;
1649 for (r = first_regno; r <= last_regno; ++r)
1651 const char *comma = "";
1652 int len;
1654 if (first_regno == last_regno)
1655 fprintf (stderr, "%s:\t", reg_name);
1656 else
1657 fprintf (stderr, "%s%d:\t", reg_name, r - first_regno);
1659 len = 8;
1660 for (m = 0; m < NUM_MACHINE_MODES; ++m)
1661 if (rs6000_hard_regno_mode_ok_p[m][r] && rs6000_hard_regno_nregs[m][r])
1663 if (len > 70)
1665 fprintf (stderr, ",\n\t");
1666 len = 8;
1667 comma = "";
1670 if (rs6000_hard_regno_nregs[m][r] > 1)
1671 len += fprintf (stderr, "%s%s/%d", comma, GET_MODE_NAME (m),
1672 rs6000_hard_regno_nregs[m][r]);
1673 else
1674 len += fprintf (stderr, "%s%s", comma, GET_MODE_NAME (m));
1676 comma = ", ";
1679 if (call_used_regs[r])
1681 if (len > 70)
1683 fprintf (stderr, ",\n\t");
1684 len = 8;
1685 comma = "";
1688 len += fprintf (stderr, "%s%s", comma, "call-used");
1689 comma = ", ";
1692 if (fixed_regs[r])
1694 if (len > 70)
1696 fprintf (stderr, ",\n\t");
1697 len = 8;
1698 comma = "";
1701 len += fprintf (stderr, "%s%s", comma, "fixed");
1702 comma = ", ";
1705 if (len > 70)
1707 fprintf (stderr, ",\n\t");
1708 comma = "";
1711 fprintf (stderr, "%sregno = %d\n", comma, r);
1715 #define DEBUG_FMT_D "%-32s= %d\n"
1716 #define DEBUG_FMT_X "%-32s= 0x%x\n"
1717 #define DEBUG_FMT_S "%-32s= %s\n"
1719 /* Print various interesting information with -mdebug=reg. */
1720 static void
1721 rs6000_debug_reg_global (void)
1723 static const char *const tf[2] = { "false", "true" };
1724 const char *nl = (const char *)0;
1725 int m;
1726 char costly_num[20];
1727 char nop_num[20];
1728 const char *costly_str;
1729 const char *nop_str;
1730 const char *trace_str;
1731 const char *abi_str;
1732 const char *cmodel_str;
1734 /* Map enum rs6000_vector to string. */
1735 static const char *rs6000_debug_vector_unit[] = {
1736 "none",
1737 "altivec",
1738 "vsx",
1739 "paired",
1740 "spe",
1741 "other"
1744 fprintf (stderr, "Register information: (last virtual reg = %d)\n",
1745 LAST_VIRTUAL_REGISTER);
1746 rs6000_debug_reg_print (0, 31, "gr");
1747 rs6000_debug_reg_print (32, 63, "fp");
1748 rs6000_debug_reg_print (FIRST_ALTIVEC_REGNO,
1749 LAST_ALTIVEC_REGNO,
1750 "vs");
1751 rs6000_debug_reg_print (LR_REGNO, LR_REGNO, "lr");
1752 rs6000_debug_reg_print (CTR_REGNO, CTR_REGNO, "ctr");
1753 rs6000_debug_reg_print (CR0_REGNO, CR7_REGNO, "cr");
1754 rs6000_debug_reg_print (MQ_REGNO, MQ_REGNO, "mq");
1755 rs6000_debug_reg_print (CA_REGNO, CA_REGNO, "ca");
1756 rs6000_debug_reg_print (VRSAVE_REGNO, VRSAVE_REGNO, "vrsave");
1757 rs6000_debug_reg_print (VSCR_REGNO, VSCR_REGNO, "vscr");
1758 rs6000_debug_reg_print (SPE_ACC_REGNO, SPE_ACC_REGNO, "spe_a");
1759 rs6000_debug_reg_print (SPEFSCR_REGNO, SPEFSCR_REGNO, "spe_f");
1761 fprintf (stderr,
1762 "\n"
1763 "d reg_class = %s\n"
1764 "f reg_class = %s\n"
1765 "v reg_class = %s\n"
1766 "wa reg_class = %s\n"
1767 "wd reg_class = %s\n"
1768 "wf reg_class = %s\n"
1769 "ws reg_class = %s\n\n",
1770 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_d]],
1771 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_f]],
1772 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_v]],
1773 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wa]],
1774 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wd]],
1775 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wf]],
1776 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_ws]]);
1778 for (m = 0; m < NUM_MACHINE_MODES; ++m)
1779 if (rs6000_vector_unit[m] || rs6000_vector_mem[m])
1781 nl = "\n";
1782 fprintf (stderr, "Vector mode: %-5s arithmetic: %-8s move: %-8s\n",
1783 GET_MODE_NAME (m),
1784 rs6000_debug_vector_unit[ rs6000_vector_unit[m] ],
1785 rs6000_debug_vector_unit[ rs6000_vector_mem[m] ]);
1788 if (nl)
1789 fputs (nl, stderr);
1791 if (rs6000_recip_control)
1793 fprintf (stderr, "\nReciprocal mask = 0x%x\n", rs6000_recip_control);
1795 for (m = 0; m < NUM_MACHINE_MODES; ++m)
1796 if (rs6000_recip_bits[m])
1798 fprintf (stderr,
1799 "Reciprocal estimate mode: %-5s divide: %s rsqrt: %s\n",
1800 GET_MODE_NAME (m),
1801 (RS6000_RECIP_AUTO_RE_P (m)
1802 ? "auto"
1803 : (RS6000_RECIP_HAVE_RE_P (m) ? "have" : "none")),
1804 (RS6000_RECIP_AUTO_RSQRTE_P (m)
1805 ? "auto"
1806 : (RS6000_RECIP_HAVE_RSQRTE_P (m) ? "have" : "none")));
1809 fputs ("\n", stderr);
1812 if (rs6000_cpu_index >= 0)
1813 fprintf (stderr, DEBUG_FMT_S, "cpu",
1814 processor_target_table[rs6000_cpu_index].name);
1816 if (rs6000_tune_index >= 0)
1817 fprintf (stderr, DEBUG_FMT_S, "tune",
1818 processor_target_table[rs6000_tune_index].name);
1820 switch (rs6000_sched_costly_dep)
1822 case max_dep_latency:
1823 costly_str = "max_dep_latency";
1824 break;
1826 case no_dep_costly:
1827 costly_str = "no_dep_costly";
1828 break;
1830 case all_deps_costly:
1831 costly_str = "all_deps_costly";
1832 break;
1834 case true_store_to_load_dep_costly:
1835 costly_str = "true_store_to_load_dep_costly";
1836 break;
1838 case store_to_load_dep_costly:
1839 costly_str = "store_to_load_dep_costly";
1840 break;
1842 default:
1843 costly_str = costly_num;
1844 sprintf (costly_num, "%d", (int)rs6000_sched_costly_dep);
1845 break;
1848 fprintf (stderr, DEBUG_FMT_S, "sched_costly_dep", costly_str);
1850 switch (rs6000_sched_insert_nops)
1852 case sched_finish_regroup_exact:
1853 nop_str = "sched_finish_regroup_exact";
1854 break;
1856 case sched_finish_pad_groups:
1857 nop_str = "sched_finish_pad_groups";
1858 break;
1860 case sched_finish_none:
1861 nop_str = "sched_finish_none";
1862 break;
1864 default:
1865 nop_str = nop_num;
1866 sprintf (nop_num, "%d", (int)rs6000_sched_insert_nops);
1867 break;
1870 fprintf (stderr, DEBUG_FMT_S, "sched_insert_nops", nop_str);
1872 switch (rs6000_sdata)
1874 default:
1875 case SDATA_NONE:
1876 break;
1878 case SDATA_DATA:
1879 fprintf (stderr, DEBUG_FMT_S, "sdata", "data");
1880 break;
1882 case SDATA_SYSV:
1883 fprintf (stderr, DEBUG_FMT_S, "sdata", "sysv");
1884 break;
1886 case SDATA_EABI:
1887 fprintf (stderr, DEBUG_FMT_S, "sdata", "eabi");
1888 break;
1892 switch (rs6000_traceback)
1894 case traceback_default: trace_str = "default"; break;
1895 case traceback_none: trace_str = "none"; break;
1896 case traceback_part: trace_str = "part"; break;
1897 case traceback_full: trace_str = "full"; break;
1898 default: trace_str = "unknown"; break;
1901 fprintf (stderr, DEBUG_FMT_S, "traceback", trace_str);
1903 switch (rs6000_current_cmodel)
1905 case CMODEL_SMALL: cmodel_str = "small"; break;
1906 case CMODEL_MEDIUM: cmodel_str = "medium"; break;
1907 case CMODEL_LARGE: cmodel_str = "large"; break;
1908 default: cmodel_str = "unknown"; break;
1911 fprintf (stderr, DEBUG_FMT_S, "cmodel", cmodel_str);
1913 switch (rs6000_current_abi)
1915 case ABI_NONE: abi_str = "none"; break;
1916 case ABI_AIX: abi_str = "aix"; break;
1917 case ABI_V4: abi_str = "V4"; break;
1918 case ABI_DARWIN: abi_str = "darwin"; break;
1919 default: abi_str = "unknown"; break;
1922 fprintf (stderr, DEBUG_FMT_S, "abi", abi_str);
1924 if (rs6000_altivec_abi)
1925 fprintf (stderr, DEBUG_FMT_S, "altivec_abi", "true");
1927 if (rs6000_spe_abi)
1928 fprintf (stderr, DEBUG_FMT_S, "spe_abi", "true");
1930 if (rs6000_darwin64_abi)
1931 fprintf (stderr, DEBUG_FMT_S, "darwin64_abi", "true");
1933 if (rs6000_float_gprs)
1934 fprintf (stderr, DEBUG_FMT_S, "float_gprs", "true");
1936 fprintf (stderr, DEBUG_FMT_S, "always_hint", tf[!!rs6000_always_hint]);
1937 fprintf (stderr, DEBUG_FMT_S, "align_branch",
1938 tf[!!rs6000_align_branch_targets]);
1939 fprintf (stderr, DEBUG_FMT_D, "tls_size", rs6000_tls_size);
1940 fprintf (stderr, DEBUG_FMT_D, "long_double_size",
1941 rs6000_long_double_type_size);
1942 fprintf (stderr, DEBUG_FMT_D, "sched_restricted_insns_priority",
1943 (int)rs6000_sched_restricted_insns_priority);
1944 fprintf (stderr, DEBUG_FMT_D, "Number of standard builtins",
1945 (int)END_BUILTINS);
1946 fprintf (stderr, DEBUG_FMT_D, "Number of rs6000 builtins",
1947 (int)RS6000_BUILTIN_COUNT);
1948 fprintf (stderr, DEBUG_FMT_X, "Builtin mask", rs6000_builtin_mask);
1951 /* Initialize the various global tables that are based on register size. */
1952 static void
1953 rs6000_init_hard_regno_mode_ok (bool global_init_p)
1955 int r, m, c;
1956 int align64;
1957 int align32;
1959 /* Precalculate REGNO_REG_CLASS. */
1960 rs6000_regno_regclass[0] = GENERAL_REGS;
1961 for (r = 1; r < 32; ++r)
1962 rs6000_regno_regclass[r] = BASE_REGS;
1964 for (r = 32; r < 64; ++r)
1965 rs6000_regno_regclass[r] = FLOAT_REGS;
1967 for (r = 64; r < FIRST_PSEUDO_REGISTER; ++r)
1968 rs6000_regno_regclass[r] = NO_REGS;
1970 for (r = FIRST_ALTIVEC_REGNO; r <= LAST_ALTIVEC_REGNO; ++r)
1971 rs6000_regno_regclass[r] = ALTIVEC_REGS;
1973 rs6000_regno_regclass[CR0_REGNO] = CR0_REGS;
1974 for (r = CR1_REGNO; r <= CR7_REGNO; ++r)
1975 rs6000_regno_regclass[r] = CR_REGS;
1977 rs6000_regno_regclass[MQ_REGNO] = MQ_REGS;
1978 rs6000_regno_regclass[LR_REGNO] = LINK_REGS;
1979 rs6000_regno_regclass[CTR_REGNO] = CTR_REGS;
1980 rs6000_regno_regclass[CA_REGNO] = CA_REGS;
1981 rs6000_regno_regclass[VRSAVE_REGNO] = VRSAVE_REGS;
1982 rs6000_regno_regclass[VSCR_REGNO] = VRSAVE_REGS;
1983 rs6000_regno_regclass[SPE_ACC_REGNO] = SPE_ACC_REGS;
1984 rs6000_regno_regclass[SPEFSCR_REGNO] = SPEFSCR_REGS;
1985 rs6000_regno_regclass[ARG_POINTER_REGNUM] = BASE_REGS;
1986 rs6000_regno_regclass[FRAME_POINTER_REGNUM] = BASE_REGS;
1988 /* Precalculate vector information, this must be set up before the
1989 rs6000_hard_regno_nregs_internal below. */
1990 for (m = 0; m < NUM_MACHINE_MODES; ++m)
1992 rs6000_vector_unit[m] = rs6000_vector_mem[m] = VECTOR_NONE;
1993 rs6000_vector_reload[m][0] = CODE_FOR_nothing;
1994 rs6000_vector_reload[m][1] = CODE_FOR_nothing;
1997 for (c = 0; c < (int)(int)RS6000_CONSTRAINT_MAX; c++)
1998 rs6000_constraints[c] = NO_REGS;
2000 /* The VSX hardware allows native alignment for vectors, but control whether the compiler
2001 believes it can use native alignment or still uses 128-bit alignment. */
2002 if (TARGET_VSX && !TARGET_VSX_ALIGN_128)
2004 align64 = 64;
2005 align32 = 32;
2007 else
2009 align64 = 128;
2010 align32 = 128;
2013 /* V2DF mode, VSX only. */
2014 if (TARGET_VSX)
2016 rs6000_vector_unit[V2DFmode] = VECTOR_VSX;
2017 rs6000_vector_mem[V2DFmode] = VECTOR_VSX;
2018 rs6000_vector_align[V2DFmode] = align64;
2021 /* V4SF mode, either VSX or Altivec. */
2022 if (TARGET_VSX)
2024 rs6000_vector_unit[V4SFmode] = VECTOR_VSX;
2025 rs6000_vector_mem[V4SFmode] = VECTOR_VSX;
2026 rs6000_vector_align[V4SFmode] = align32;
2028 else if (TARGET_ALTIVEC)
2030 rs6000_vector_unit[V4SFmode] = VECTOR_ALTIVEC;
2031 rs6000_vector_mem[V4SFmode] = VECTOR_ALTIVEC;
2032 rs6000_vector_align[V4SFmode] = align32;
2035 /* V16QImode, V8HImode, V4SImode are Altivec only, but possibly do VSX loads
2036 and stores. */
2037 if (TARGET_ALTIVEC)
2039 rs6000_vector_unit[V4SImode] = VECTOR_ALTIVEC;
2040 rs6000_vector_unit[V8HImode] = VECTOR_ALTIVEC;
2041 rs6000_vector_unit[V16QImode] = VECTOR_ALTIVEC;
2042 rs6000_vector_align[V4SImode] = align32;
2043 rs6000_vector_align[V8HImode] = align32;
2044 rs6000_vector_align[V16QImode] = align32;
2046 if (TARGET_VSX)
2048 rs6000_vector_mem[V4SImode] = VECTOR_VSX;
2049 rs6000_vector_mem[V8HImode] = VECTOR_VSX;
2050 rs6000_vector_mem[V16QImode] = VECTOR_VSX;
2052 else
2054 rs6000_vector_mem[V4SImode] = VECTOR_ALTIVEC;
2055 rs6000_vector_mem[V8HImode] = VECTOR_ALTIVEC;
2056 rs6000_vector_mem[V16QImode] = VECTOR_ALTIVEC;
2060 /* V2DImode, only allow under VSX, which can do V2DI insert/splat/extract.
2061 Altivec doesn't have 64-bit support. */
2062 if (TARGET_VSX)
2064 rs6000_vector_mem[V2DImode] = VECTOR_VSX;
2065 rs6000_vector_unit[V2DImode] = VECTOR_NONE;
2066 rs6000_vector_align[V2DImode] = align64;
2069 /* DFmode, see if we want to use the VSX unit. */
2070 if (TARGET_VSX && TARGET_VSX_SCALAR_DOUBLE)
2072 rs6000_vector_unit[DFmode] = VECTOR_VSX;
2073 rs6000_vector_mem[DFmode]
2074 = (TARGET_VSX_SCALAR_MEMORY ? VECTOR_VSX : VECTOR_NONE);
2075 rs6000_vector_align[DFmode] = align64;
2078 /* TODO add SPE and paired floating point vector support. */
2080 /* Register class constraints for the constraints that depend on compile
2081 switches. */
2082 if (TARGET_HARD_FLOAT && TARGET_FPRS)
2083 rs6000_constraints[RS6000_CONSTRAINT_f] = FLOAT_REGS;
2085 if (TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT)
2086 rs6000_constraints[RS6000_CONSTRAINT_d] = FLOAT_REGS;
2088 if (TARGET_VSX)
2090 /* At present, we just use VSX_REGS, but we have different constraints
2091 based on the use, in case we want to fine tune the default register
2092 class used. wa = any VSX register, wf = register class to use for
2093 V4SF, wd = register class to use for V2DF, and ws = register classs to
2094 use for DF scalars. */
2095 rs6000_constraints[RS6000_CONSTRAINT_wa] = VSX_REGS;
2096 rs6000_constraints[RS6000_CONSTRAINT_wf] = VSX_REGS;
2097 rs6000_constraints[RS6000_CONSTRAINT_wd] = VSX_REGS;
2098 rs6000_constraints[RS6000_CONSTRAINT_ws] = (TARGET_VSX_SCALAR_MEMORY
2099 ? VSX_REGS
2100 : FLOAT_REGS);
2103 if (TARGET_ALTIVEC)
2104 rs6000_constraints[RS6000_CONSTRAINT_v] = ALTIVEC_REGS;
2106 /* Set up the reload helper functions. */
2107 if (TARGET_VSX || TARGET_ALTIVEC)
2109 if (TARGET_64BIT)
2111 rs6000_vector_reload[V16QImode][0] = CODE_FOR_reload_v16qi_di_store;
2112 rs6000_vector_reload[V16QImode][1] = CODE_FOR_reload_v16qi_di_load;
2113 rs6000_vector_reload[V8HImode][0] = CODE_FOR_reload_v8hi_di_store;
2114 rs6000_vector_reload[V8HImode][1] = CODE_FOR_reload_v8hi_di_load;
2115 rs6000_vector_reload[V4SImode][0] = CODE_FOR_reload_v4si_di_store;
2116 rs6000_vector_reload[V4SImode][1] = CODE_FOR_reload_v4si_di_load;
2117 rs6000_vector_reload[V2DImode][0] = CODE_FOR_reload_v2di_di_store;
2118 rs6000_vector_reload[V2DImode][1] = CODE_FOR_reload_v2di_di_load;
2119 rs6000_vector_reload[V4SFmode][0] = CODE_FOR_reload_v4sf_di_store;
2120 rs6000_vector_reload[V4SFmode][1] = CODE_FOR_reload_v4sf_di_load;
2121 rs6000_vector_reload[V2DFmode][0] = CODE_FOR_reload_v2df_di_store;
2122 rs6000_vector_reload[V2DFmode][1] = CODE_FOR_reload_v2df_di_load;
2123 if (TARGET_VSX && TARGET_VSX_SCALAR_MEMORY)
2125 rs6000_vector_reload[DFmode][0] = CODE_FOR_reload_df_di_store;
2126 rs6000_vector_reload[DFmode][1] = CODE_FOR_reload_df_di_load;
2129 else
2131 rs6000_vector_reload[V16QImode][0] = CODE_FOR_reload_v16qi_si_store;
2132 rs6000_vector_reload[V16QImode][1] = CODE_FOR_reload_v16qi_si_load;
2133 rs6000_vector_reload[V8HImode][0] = CODE_FOR_reload_v8hi_si_store;
2134 rs6000_vector_reload[V8HImode][1] = CODE_FOR_reload_v8hi_si_load;
2135 rs6000_vector_reload[V4SImode][0] = CODE_FOR_reload_v4si_si_store;
2136 rs6000_vector_reload[V4SImode][1] = CODE_FOR_reload_v4si_si_load;
2137 rs6000_vector_reload[V2DImode][0] = CODE_FOR_reload_v2di_si_store;
2138 rs6000_vector_reload[V2DImode][1] = CODE_FOR_reload_v2di_si_load;
2139 rs6000_vector_reload[V4SFmode][0] = CODE_FOR_reload_v4sf_si_store;
2140 rs6000_vector_reload[V4SFmode][1] = CODE_FOR_reload_v4sf_si_load;
2141 rs6000_vector_reload[V2DFmode][0] = CODE_FOR_reload_v2df_si_store;
2142 rs6000_vector_reload[V2DFmode][1] = CODE_FOR_reload_v2df_si_load;
2143 if (TARGET_VSX && TARGET_VSX_SCALAR_MEMORY)
2145 rs6000_vector_reload[DFmode][0] = CODE_FOR_reload_df_si_store;
2146 rs6000_vector_reload[DFmode][1] = CODE_FOR_reload_df_si_load;
2151 /* Precalculate HARD_REGNO_NREGS. */
2152 for (r = 0; r < FIRST_PSEUDO_REGISTER; ++r)
2153 for (m = 0; m < NUM_MACHINE_MODES; ++m)
2154 rs6000_hard_regno_nregs[m][r]
2155 = rs6000_hard_regno_nregs_internal (r, (enum machine_mode)m);
2157 /* Precalculate HARD_REGNO_MODE_OK. */
2158 for (r = 0; r < FIRST_PSEUDO_REGISTER; ++r)
2159 for (m = 0; m < NUM_MACHINE_MODES; ++m)
2160 if (rs6000_hard_regno_mode_ok (r, (enum machine_mode)m))
2161 rs6000_hard_regno_mode_ok_p[m][r] = true;
2163 /* Precalculate CLASS_MAX_NREGS sizes. */
2164 for (c = 0; c < LIM_REG_CLASSES; ++c)
2166 int reg_size;
2168 if (TARGET_VSX && VSX_REG_CLASS_P (c))
2169 reg_size = UNITS_PER_VSX_WORD;
2171 else if (c == ALTIVEC_REGS)
2172 reg_size = UNITS_PER_ALTIVEC_WORD;
2174 else if (c == FLOAT_REGS)
2175 reg_size = UNITS_PER_FP_WORD;
2177 else
2178 reg_size = UNITS_PER_WORD;
2180 for (m = 0; m < NUM_MACHINE_MODES; ++m)
2181 rs6000_class_max_nregs[m][c]
2182 = (GET_MODE_SIZE (m) + reg_size - 1) / reg_size;
2185 if (TARGET_E500_DOUBLE)
2186 rs6000_class_max_nregs[DFmode][GENERAL_REGS] = 1;
2188 /* Calculate which modes to automatically generate code to use a the
2189 reciprocal divide and square root instructions. In the future, possibly
2190 automatically generate the instructions even if the user did not specify
2191 -mrecip. The older machines double precision reciprocal sqrt estimate is
2192 not accurate enough. */
2193 memset (rs6000_recip_bits, 0, sizeof (rs6000_recip_bits));
2194 if (TARGET_FRES)
2195 rs6000_recip_bits[SFmode] = RS6000_RECIP_MASK_HAVE_RE;
2196 if (TARGET_FRE)
2197 rs6000_recip_bits[DFmode] = RS6000_RECIP_MASK_HAVE_RE;
2198 if (VECTOR_UNIT_ALTIVEC_OR_VSX_P (V4SFmode))
2199 rs6000_recip_bits[V4SFmode] = RS6000_RECIP_MASK_HAVE_RE;
2200 if (VECTOR_UNIT_VSX_P (V2DFmode))
2201 rs6000_recip_bits[V2DFmode] = RS6000_RECIP_MASK_HAVE_RE;
2203 if (TARGET_FRSQRTES)
2204 rs6000_recip_bits[SFmode] |= RS6000_RECIP_MASK_HAVE_RSQRTE;
2205 if (TARGET_FRSQRTE)
2206 rs6000_recip_bits[DFmode] |= RS6000_RECIP_MASK_HAVE_RSQRTE;
2207 if (VECTOR_UNIT_ALTIVEC_OR_VSX_P (V4SFmode))
2208 rs6000_recip_bits[V4SFmode] |= RS6000_RECIP_MASK_HAVE_RSQRTE;
2209 if (VECTOR_UNIT_VSX_P (V2DFmode))
2210 rs6000_recip_bits[V2DFmode] |= RS6000_RECIP_MASK_HAVE_RSQRTE;
2212 if (rs6000_recip_control)
2214 if (!flag_finite_math_only)
2215 warning (0, "-mrecip requires -ffinite-math or -ffast-math");
2216 if (flag_trapping_math)
2217 warning (0, "-mrecip requires -fno-trapping-math or -ffast-math");
2218 if (!flag_reciprocal_math)
2219 warning (0, "-mrecip requires -freciprocal-math or -ffast-math");
2220 if (flag_finite_math_only && !flag_trapping_math && flag_reciprocal_math)
2222 if (RS6000_RECIP_HAVE_RE_P (SFmode)
2223 && (rs6000_recip_control & RECIP_SF_DIV) != 0)
2224 rs6000_recip_bits[SFmode] |= RS6000_RECIP_MASK_AUTO_RE;
2226 if (RS6000_RECIP_HAVE_RE_P (DFmode)
2227 && (rs6000_recip_control & RECIP_DF_DIV) != 0)
2228 rs6000_recip_bits[DFmode] |= RS6000_RECIP_MASK_AUTO_RE;
2230 if (RS6000_RECIP_HAVE_RE_P (V4SFmode)
2231 && (rs6000_recip_control & RECIP_V4SF_DIV) != 0)
2232 rs6000_recip_bits[V4SFmode] |= RS6000_RECIP_MASK_AUTO_RE;
2234 if (RS6000_RECIP_HAVE_RE_P (V2DFmode)
2235 && (rs6000_recip_control & RECIP_V2DF_DIV) != 0)
2236 rs6000_recip_bits[V2DFmode] |= RS6000_RECIP_MASK_AUTO_RE;
2238 if (RS6000_RECIP_HAVE_RSQRTE_P (SFmode)
2239 && (rs6000_recip_control & RECIP_SF_RSQRT) != 0)
2240 rs6000_recip_bits[SFmode] |= RS6000_RECIP_MASK_AUTO_RSQRTE;
2242 if (RS6000_RECIP_HAVE_RSQRTE_P (DFmode)
2243 && (rs6000_recip_control & RECIP_DF_RSQRT) != 0)
2244 rs6000_recip_bits[DFmode] |= RS6000_RECIP_MASK_AUTO_RSQRTE;
2246 if (RS6000_RECIP_HAVE_RSQRTE_P (V4SFmode)
2247 && (rs6000_recip_control & RECIP_V4SF_RSQRT) != 0)
2248 rs6000_recip_bits[V4SFmode] |= RS6000_RECIP_MASK_AUTO_RSQRTE;
2250 if (RS6000_RECIP_HAVE_RSQRTE_P (V2DFmode)
2251 && (rs6000_recip_control & RECIP_V2DF_RSQRT) != 0)
2252 rs6000_recip_bits[V2DFmode] |= RS6000_RECIP_MASK_AUTO_RSQRTE;
2256 if (global_init_p || TARGET_DEBUG_TARGET)
2258 if (TARGET_DEBUG_REG)
2259 rs6000_debug_reg_global ();
2261 if (TARGET_DEBUG_COST || TARGET_DEBUG_REG)
2262 fprintf (stderr,
2263 "SImode variable mult cost = %d\n"
2264 "SImode constant mult cost = %d\n"
2265 "SImode short constant mult cost = %d\n"
2266 "DImode multipliciation cost = %d\n"
2267 "SImode division cost = %d\n"
2268 "DImode division cost = %d\n"
2269 "Simple fp operation cost = %d\n"
2270 "DFmode multiplication cost = %d\n"
2271 "SFmode division cost = %d\n"
2272 "DFmode division cost = %d\n"
2273 "cache line size = %d\n"
2274 "l1 cache size = %d\n"
2275 "l2 cache size = %d\n"
2276 "simultaneous prefetches = %d\n"
2277 "\n",
2278 rs6000_cost->mulsi,
2279 rs6000_cost->mulsi_const,
2280 rs6000_cost->mulsi_const9,
2281 rs6000_cost->muldi,
2282 rs6000_cost->divsi,
2283 rs6000_cost->divdi,
2284 rs6000_cost->fp,
2285 rs6000_cost->dmul,
2286 rs6000_cost->sdiv,
2287 rs6000_cost->ddiv,
2288 rs6000_cost->cache_line_size,
2289 rs6000_cost->l1_cache_size,
2290 rs6000_cost->l2_cache_size,
2291 rs6000_cost->simultaneous_prefetches);
2295 #if TARGET_MACHO
2296 /* The Darwin version of SUBTARGET_OVERRIDE_OPTIONS. */
2298 static void
2299 darwin_rs6000_override_options (void)
2301 /* The Darwin ABI always includes AltiVec, can't be (validly) turned
2302 off. */
2303 rs6000_altivec_abi = 1;
2304 TARGET_ALTIVEC_VRSAVE = 1;
2305 rs6000_current_abi = ABI_DARWIN;
2307 if (DEFAULT_ABI == ABI_DARWIN
2308 && TARGET_64BIT)
2309 darwin_one_byte_bool = 1;
2311 if (TARGET_64BIT && ! TARGET_POWERPC64)
2313 target_flags |= MASK_POWERPC64;
2314 warning (0, "-m64 requires PowerPC64 architecture, enabling");
2316 if (flag_mkernel)
2318 rs6000_default_long_calls = 1;
2319 target_flags |= MASK_SOFT_FLOAT;
2322 /* Make -m64 imply -maltivec. Darwin's 64-bit ABI includes
2323 Altivec. */
2324 if (!flag_mkernel && !flag_apple_kext
2325 && TARGET_64BIT
2326 && ! (target_flags_explicit & MASK_ALTIVEC))
2327 target_flags |= MASK_ALTIVEC;
2329 /* Unless the user (not the configurer) has explicitly overridden
2330 it with -mcpu=G3 or -mno-altivec, then 10.5+ targets default to
2331 G4 unless targeting the kernel. */
2332 if (!flag_mkernel
2333 && !flag_apple_kext
2334 && strverscmp (darwin_macosx_version_min, "10.5") >= 0
2335 && ! (target_flags_explicit & MASK_ALTIVEC)
2336 && ! global_options_set.x_rs6000_cpu_index)
2338 target_flags |= MASK_ALTIVEC;
2341 #endif
2343 /* If not otherwise specified by a target, make 'long double' equivalent to
2344 'double'. */
2346 #ifndef RS6000_DEFAULT_LONG_DOUBLE_SIZE
2347 #define RS6000_DEFAULT_LONG_DOUBLE_SIZE 64
2348 #endif
2350 /* Return the builtin mask of the various options used that could affect which
2351 builtins were used. In the past we used target_flags, but we've run out of
2352 bits, and some options like SPE and PAIRED are no longer in
2353 target_flags. */
2355 unsigned
2356 rs6000_builtin_mask_calculate (void)
2358 return (((TARGET_ALTIVEC) ? RS6000_BTM_ALTIVEC : 0)
2359 | ((TARGET_VSX) ? RS6000_BTM_VSX : 0)
2360 | ((TARGET_SPE) ? RS6000_BTM_SPE : 0)
2361 | ((TARGET_PAIRED_FLOAT) ? RS6000_BTM_PAIRED : 0)
2362 | ((TARGET_FRE) ? RS6000_BTM_FRE : 0)
2363 | ((TARGET_FRES) ? RS6000_BTM_FRES : 0)
2364 | ((TARGET_FRSQRTE) ? RS6000_BTM_FRSQRTE : 0)
2365 | ((TARGET_FRSQRTES) ? RS6000_BTM_FRSQRTES : 0)
2366 | ((TARGET_POPCNTD) ? RS6000_BTM_POPCNTD : 0)
2367 | ((TARGET_POWERPC) ? RS6000_BTM_POWERPC : 0)
2368 | ((rs6000_cpu == PROCESSOR_CELL) ? RS6000_BTM_CELL : 0));
2371 /* Override command line options. Mostly we process the processor type and
2372 sometimes adjust other TARGET_ options. */
2374 static bool
2375 rs6000_option_override_internal (bool global_init_p)
2377 bool ret = true;
2378 bool have_cpu = false;
2380 /* The default cpu requested at configure time, if any. */
2381 const char *implicit_cpu = OPTION_TARGET_CPU_DEFAULT;
2383 int set_masks;
2384 int cpu_index;
2385 int tune_index;
2386 struct cl_target_option *main_target_opt
2387 = ((global_init_p || target_option_default_node == NULL)
2388 ? NULL : TREE_TARGET_OPTION (target_option_default_node));
2390 /* On 64-bit Darwin, power alignment is ABI-incompatible with some C
2391 library functions, so warn about it. The flag may be useful for
2392 performance studies from time to time though, so don't disable it
2393 entirely. */
2394 if (global_options_set.x_rs6000_alignment_flags
2395 && rs6000_alignment_flags == MASK_ALIGN_POWER
2396 && DEFAULT_ABI == ABI_DARWIN
2397 && TARGET_64BIT)
2398 warning (0, "-malign-power is not supported for 64-bit Darwin;"
2399 " it is incompatible with the installed C and C++ libraries");
2401 /* Numerous experiment shows that IRA based loop pressure
2402 calculation works better for RTL loop invariant motion on targets
2403 with enough (>= 32) registers. It is an expensive optimization.
2404 So it is on only for peak performance. */
2405 if (optimize >= 3 && global_init_p)
2406 flag_ira_loop_pressure = 1;
2408 /* Set the pointer size. */
2409 if (TARGET_64BIT)
2411 rs6000_pmode = (int)DImode;
2412 rs6000_pointer_size = 64;
2414 else
2416 rs6000_pmode = (int)SImode;
2417 rs6000_pointer_size = 32;
2420 set_masks = POWER_MASKS | POWERPC_MASKS | MASK_SOFT_FLOAT;
2421 #ifdef OS_MISSING_POWERPC64
2422 if (OS_MISSING_POWERPC64)
2423 set_masks &= ~MASK_POWERPC64;
2424 #endif
2425 #ifdef OS_MISSING_ALTIVEC
2426 if (OS_MISSING_ALTIVEC)
2427 set_masks &= ~MASK_ALTIVEC;
2428 #endif
2430 /* Don't override by the processor default if given explicitly. */
2431 set_masks &= ~target_flags_explicit;
2433 /* Process the -mcpu=<xxx> and -mtune=<xxx> argument. If the user changed
2434 the cpu in a target attribute or pragma, but did not specify a tuning
2435 option, use the cpu for the tuning option rather than the option specified
2436 with -mtune on the command line. Process a '--with-cpu' configuration
2437 request as an implicit --cpu. */
2438 if (rs6000_cpu_index >= 0)
2440 cpu_index = rs6000_cpu_index;
2441 have_cpu = true;
2443 else if (main_target_opt != NULL && main_target_opt->x_rs6000_cpu_index >= 0)
2445 rs6000_cpu_index = cpu_index = main_target_opt->x_rs6000_cpu_index;
2446 have_cpu = true;
2448 else
2450 const char *default_cpu =
2451 (implicit_cpu ? implicit_cpu
2452 : (TARGET_POWERPC64 ? "powerpc64" : "powerpc"));
2454 rs6000_cpu_index = cpu_index = rs6000_cpu_name_lookup (default_cpu);
2455 have_cpu = implicit_cpu != 0;
2458 gcc_assert (cpu_index >= 0);
2460 target_flags &= ~set_masks;
2461 target_flags |= (processor_target_table[cpu_index].target_enable
2462 & set_masks);
2464 if (rs6000_tune_index >= 0)
2465 tune_index = rs6000_tune_index;
2466 else if (have_cpu)
2467 rs6000_tune_index = tune_index = cpu_index;
2468 else
2470 size_t i;
2471 enum processor_type tune_proc
2472 = (TARGET_POWERPC64 ? PROCESSOR_DEFAULT64 : PROCESSOR_DEFAULT);
2474 tune_index = -1;
2475 for (i = 0; i < ARRAY_SIZE (processor_target_table); i++)
2476 if (processor_target_table[i].processor == tune_proc)
2478 rs6000_tune_index = tune_index = i;
2479 break;
2483 gcc_assert (tune_index >= 0);
2484 rs6000_cpu = processor_target_table[tune_index].processor;
2486 /* Pick defaults for SPE related control flags. Do this early to make sure
2487 that the TARGET_ macros are representative ASAP. */
2489 int spe_capable_cpu =
2490 (rs6000_cpu == PROCESSOR_PPC8540
2491 || rs6000_cpu == PROCESSOR_PPC8548);
2493 if (!global_options_set.x_rs6000_spe_abi)
2494 rs6000_spe_abi = spe_capable_cpu;
2496 if (!global_options_set.x_rs6000_spe)
2497 rs6000_spe = spe_capable_cpu;
2499 if (!global_options_set.x_rs6000_float_gprs)
2500 rs6000_float_gprs =
2501 (rs6000_cpu == PROCESSOR_PPC8540 ? 1
2502 : rs6000_cpu == PROCESSOR_PPC8548 ? 2
2503 : 0);
2506 if (global_options_set.x_rs6000_spe_abi
2507 && rs6000_spe_abi
2508 && !TARGET_SPE_ABI)
2509 error ("not configured for SPE ABI");
2511 if (global_options_set.x_rs6000_spe
2512 && rs6000_spe
2513 && !TARGET_SPE)
2514 error ("not configured for SPE instruction set");
2516 if (main_target_opt != NULL
2517 && ((main_target_opt->x_rs6000_spe_abi != rs6000_spe_abi)
2518 || (main_target_opt->x_rs6000_spe != rs6000_spe)
2519 || (main_target_opt->x_rs6000_float_gprs != rs6000_float_gprs)))
2520 error ("target attribute or pragma changes SPE ABI");
2522 if (rs6000_cpu == PROCESSOR_PPCE300C2 || rs6000_cpu == PROCESSOR_PPCE300C3
2523 || rs6000_cpu == PROCESSOR_PPCE500MC || rs6000_cpu == PROCESSOR_PPCE500MC64
2524 || rs6000_cpu == PROCESSOR_PPCE5500)
2526 if (TARGET_ALTIVEC)
2527 error ("AltiVec not supported in this target");
2528 if (TARGET_SPE)
2529 error ("SPE not supported in this target");
2531 if (rs6000_cpu == PROCESSOR_PPCE6500)
2533 if (TARGET_SPE)
2534 error ("SPE not supported in this target");
2537 /* Disable Cell microcode if we are optimizing for the Cell
2538 and not optimizing for size. */
2539 if (rs6000_gen_cell_microcode == -1)
2540 rs6000_gen_cell_microcode = !(rs6000_cpu == PROCESSOR_CELL
2541 && !optimize_size);
2543 /* If we are optimizing big endian systems for space and it's OK to
2544 use instructions that would be microcoded on the Cell, use the
2545 load/store multiple and string instructions. */
2546 if (BYTES_BIG_ENDIAN && optimize_size && rs6000_gen_cell_microcode)
2547 target_flags |= ~target_flags_explicit & (MASK_MULTIPLE | MASK_STRING);
2549 /* Don't allow -mmultiple or -mstring on little endian systems
2550 unless the cpu is a 750, because the hardware doesn't support the
2551 instructions used in little endian mode, and causes an alignment
2552 trap. The 750 does not cause an alignment trap (except when the
2553 target is unaligned). */
2555 if (!BYTES_BIG_ENDIAN && rs6000_cpu != PROCESSOR_PPC750)
2557 if (TARGET_MULTIPLE)
2559 target_flags &= ~MASK_MULTIPLE;
2560 if ((target_flags_explicit & MASK_MULTIPLE) != 0)
2561 warning (0, "-mmultiple is not supported on little endian systems");
2564 if (TARGET_STRING)
2566 target_flags &= ~MASK_STRING;
2567 if ((target_flags_explicit & MASK_STRING) != 0)
2568 warning (0, "-mstring is not supported on little endian systems");
2572 /* Add some warnings for VSX. */
2573 if (TARGET_VSX)
2575 const char *msg = NULL;
2576 if (!TARGET_HARD_FLOAT || !TARGET_FPRS
2577 || !TARGET_SINGLE_FLOAT || !TARGET_DOUBLE_FLOAT)
2579 if (target_flags_explicit & MASK_VSX)
2580 msg = N_("-mvsx requires hardware floating point");
2581 else
2582 target_flags &= ~ MASK_VSX;
2584 else if (TARGET_PAIRED_FLOAT)
2585 msg = N_("-mvsx and -mpaired are incompatible");
2586 /* The hardware will allow VSX and little endian, but until we make sure
2587 things like vector select, etc. work don't allow VSX on little endian
2588 systems at this point. */
2589 else if (!BYTES_BIG_ENDIAN)
2590 msg = N_("-mvsx used with little endian code");
2591 else if (TARGET_AVOID_XFORM > 0)
2592 msg = N_("-mvsx needs indexed addressing");
2593 else if (!TARGET_ALTIVEC && (target_flags_explicit & MASK_ALTIVEC))
2595 if (target_flags_explicit & MASK_VSX)
2596 msg = N_("-mvsx and -mno-altivec are incompatible");
2597 else
2598 msg = N_("-mno-altivec disables vsx");
2601 if (msg)
2603 warning (0, msg);
2604 target_flags &= ~ MASK_VSX;
2605 target_flags_explicit |= MASK_VSX;
2609 /* For the newer switches (vsx, dfp, etc.) set some of the older options,
2610 unless the user explicitly used the -mno-<option> to disable the code. */
2611 if (TARGET_VSX)
2612 target_flags |= (ISA_2_6_MASKS_SERVER & ~target_flags_explicit);
2613 else if (TARGET_POPCNTD)
2614 target_flags |= (ISA_2_6_MASKS_EMBEDDED & ~target_flags_explicit);
2615 else if (TARGET_DFP)
2616 target_flags |= (ISA_2_5_MASKS_SERVER & ~target_flags_explicit);
2617 else if (TARGET_CMPB)
2618 target_flags |= (ISA_2_5_MASKS_EMBEDDED & ~target_flags_explicit);
2619 else if (TARGET_FPRND)
2620 target_flags |= (ISA_2_4_MASKS & ~target_flags_explicit);
2621 else if (TARGET_POPCNTB)
2622 target_flags |= (ISA_2_2_MASKS & ~target_flags_explicit);
2623 else if (TARGET_ALTIVEC)
2624 target_flags |= (MASK_PPC_GFXOPT & ~target_flags_explicit);
2626 /* E500mc does "better" if we inline more aggressively. Respect the
2627 user's opinion, though. */
2628 if (rs6000_block_move_inline_limit == 0
2629 && (rs6000_cpu == PROCESSOR_PPCE500MC
2630 || rs6000_cpu == PROCESSOR_PPCE500MC64
2631 || rs6000_cpu == PROCESSOR_PPCE5500
2632 || rs6000_cpu == PROCESSOR_PPCE6500))
2633 rs6000_block_move_inline_limit = 128;
2635 /* store_one_arg depends on expand_block_move to handle at least the
2636 size of reg_parm_stack_space. */
2637 if (rs6000_block_move_inline_limit < (TARGET_POWERPC64 ? 64 : 32))
2638 rs6000_block_move_inline_limit = (TARGET_POWERPC64 ? 64 : 32);
2640 if (global_init_p)
2642 /* If the appropriate debug option is enabled, replace the target hooks
2643 with debug versions that call the real version and then prints
2644 debugging information. */
2645 if (TARGET_DEBUG_COST)
2647 targetm.rtx_costs = rs6000_debug_rtx_costs;
2648 targetm.address_cost = rs6000_debug_address_cost;
2649 targetm.sched.adjust_cost = rs6000_debug_adjust_cost;
2652 if (TARGET_DEBUG_ADDR)
2654 targetm.legitimate_address_p = rs6000_debug_legitimate_address_p;
2655 targetm.legitimize_address = rs6000_debug_legitimize_address;
2656 rs6000_secondary_reload_class_ptr
2657 = rs6000_debug_secondary_reload_class;
2658 rs6000_secondary_memory_needed_ptr
2659 = rs6000_debug_secondary_memory_needed;
2660 rs6000_cannot_change_mode_class_ptr
2661 = rs6000_debug_cannot_change_mode_class;
2662 rs6000_preferred_reload_class_ptr
2663 = rs6000_debug_preferred_reload_class;
2664 rs6000_legitimize_reload_address_ptr
2665 = rs6000_debug_legitimize_reload_address;
2666 rs6000_mode_dependent_address_ptr
2667 = rs6000_debug_mode_dependent_address;
2670 if (rs6000_veclibabi_name)
2672 if (strcmp (rs6000_veclibabi_name, "mass") == 0)
2673 rs6000_veclib_handler = rs6000_builtin_vectorized_libmass;
2674 else
2676 error ("unknown vectorization library ABI type (%s) for "
2677 "-mveclibabi= switch", rs6000_veclibabi_name);
2678 ret = false;
2683 if (!global_options_set.x_rs6000_long_double_type_size)
2685 if (main_target_opt != NULL
2686 && (main_target_opt->x_rs6000_long_double_type_size
2687 != RS6000_DEFAULT_LONG_DOUBLE_SIZE))
2688 error ("target attribute or pragma changes long double size");
2689 else
2690 rs6000_long_double_type_size = RS6000_DEFAULT_LONG_DOUBLE_SIZE;
2693 #if !defined (POWERPC_LINUX) && !defined (POWERPC_FREEBSD)
2694 if (!global_options_set.x_rs6000_ieeequad)
2695 rs6000_ieeequad = 1;
2696 #endif
2698 /* Disable VSX and Altivec silently if the user switched cpus to power7 in a
2699 target attribute or pragma which automatically enables both options,
2700 unless the altivec ABI was set. This is set by default for 64-bit, but
2701 not for 32-bit. */
2702 if (main_target_opt != NULL && !main_target_opt->x_rs6000_altivec_abi)
2703 target_flags &= ~((MASK_VSX | MASK_ALTIVEC) & ~target_flags_explicit);
2705 /* Enable Altivec ABI for AIX -maltivec. */
2706 if (TARGET_XCOFF && (TARGET_ALTIVEC || TARGET_VSX))
2708 if (main_target_opt != NULL && !main_target_opt->x_rs6000_altivec_abi)
2709 error ("target attribute or pragma changes AltiVec ABI");
2710 else
2711 rs6000_altivec_abi = 1;
2714 /* The AltiVec ABI is the default for PowerPC-64 GNU/Linux. For
2715 PowerPC-32 GNU/Linux, -maltivec implies the AltiVec ABI. It can
2716 be explicitly overridden in either case. */
2717 if (TARGET_ELF)
2719 if (!global_options_set.x_rs6000_altivec_abi
2720 && (TARGET_64BIT || TARGET_ALTIVEC || TARGET_VSX))
2722 if (main_target_opt != NULL &&
2723 !main_target_opt->x_rs6000_altivec_abi)
2724 error ("target attribute or pragma changes AltiVec ABI");
2725 else
2726 rs6000_altivec_abi = 1;
2729 /* Enable VRSAVE for AltiVec ABI, unless explicitly overridden. */
2730 if (!global_options_set.x_TARGET_ALTIVEC_VRSAVE)
2731 TARGET_ALTIVEC_VRSAVE = rs6000_altivec_abi;
2734 /* Set the Darwin64 ABI as default for 64-bit Darwin.
2735 So far, the only darwin64 targets are also MACH-O. */
2736 if (TARGET_MACHO
2737 && DEFAULT_ABI == ABI_DARWIN
2738 && TARGET_64BIT)
2740 if (main_target_opt != NULL && !main_target_opt->x_rs6000_darwin64_abi)
2741 error ("target attribute or pragma changes darwin64 ABI");
2742 else
2744 rs6000_darwin64_abi = 1;
2745 /* Default to natural alignment, for better performance. */
2746 rs6000_alignment_flags = MASK_ALIGN_NATURAL;
2750 /* Place FP constants in the constant pool instead of TOC
2751 if section anchors enabled. */
2752 if (flag_section_anchors)
2753 TARGET_NO_FP_IN_TOC = 1;
2755 #ifdef SUBTARGET_OVERRIDE_OPTIONS
2756 SUBTARGET_OVERRIDE_OPTIONS;
2757 #endif
2758 #ifdef SUBSUBTARGET_OVERRIDE_OPTIONS
2759 SUBSUBTARGET_OVERRIDE_OPTIONS;
2760 #endif
2761 #ifdef SUB3TARGET_OVERRIDE_OPTIONS
2762 SUB3TARGET_OVERRIDE_OPTIONS;
2763 #endif
2765 /* For the E500 family of cores, reset the single/double FP flags to let us
2766 check that they remain constant across attributes or pragmas. Also,
2767 clear a possible request for string instructions, not supported and which
2768 we might have silently queried above for -Os.
2770 For other families, clear ISEL in case it was set implicitly.
2773 switch (rs6000_cpu)
2775 case PROCESSOR_PPC8540:
2776 case PROCESSOR_PPC8548:
2777 case PROCESSOR_PPCE500MC:
2778 case PROCESSOR_PPCE500MC64:
2779 case PROCESSOR_PPCE5500:
2780 case PROCESSOR_PPCE6500:
2782 rs6000_single_float = TARGET_E500_SINGLE || TARGET_E500_DOUBLE;
2783 rs6000_double_float = TARGET_E500_DOUBLE;
2785 target_flags &= ~MASK_STRING;
2787 break;
2789 default:
2791 if (have_cpu && !(target_flags_explicit & MASK_ISEL))
2792 target_flags &= ~MASK_ISEL;
2794 break;
2797 if (main_target_opt)
2799 if (main_target_opt->x_rs6000_single_float != rs6000_single_float)
2800 error ("target attribute or pragma changes single precision floating "
2801 "point");
2802 if (main_target_opt->x_rs6000_double_float != rs6000_double_float)
2803 error ("target attribute or pragma changes double precision floating "
2804 "point");
2807 /* Detect invalid option combinations with E500. */
2808 CHECK_E500_OPTIONS;
2810 rs6000_always_hint = (rs6000_cpu != PROCESSOR_POWER4
2811 && rs6000_cpu != PROCESSOR_POWER5
2812 && rs6000_cpu != PROCESSOR_POWER6
2813 && rs6000_cpu != PROCESSOR_POWER7
2814 && rs6000_cpu != PROCESSOR_PPCA2
2815 && rs6000_cpu != PROCESSOR_CELL
2816 && rs6000_cpu != PROCESSOR_PPC476);
2817 rs6000_sched_groups = (rs6000_cpu == PROCESSOR_POWER4
2818 || rs6000_cpu == PROCESSOR_POWER5
2819 || rs6000_cpu == PROCESSOR_POWER7);
2820 rs6000_align_branch_targets = (rs6000_cpu == PROCESSOR_POWER4
2821 || rs6000_cpu == PROCESSOR_POWER5
2822 || rs6000_cpu == PROCESSOR_POWER6
2823 || rs6000_cpu == PROCESSOR_POWER7
2824 || rs6000_cpu == PROCESSOR_PPCE500MC
2825 || rs6000_cpu == PROCESSOR_PPCE500MC64
2826 || rs6000_cpu == PROCESSOR_PPCE5500
2827 || rs6000_cpu == PROCESSOR_PPCE6500);
2829 /* Allow debug switches to override the above settings. These are set to -1
2830 in rs6000.opt to indicate the user hasn't directly set the switch. */
2831 if (TARGET_ALWAYS_HINT >= 0)
2832 rs6000_always_hint = TARGET_ALWAYS_HINT;
2834 if (TARGET_SCHED_GROUPS >= 0)
2835 rs6000_sched_groups = TARGET_SCHED_GROUPS;
2837 if (TARGET_ALIGN_BRANCH_TARGETS >= 0)
2838 rs6000_align_branch_targets = TARGET_ALIGN_BRANCH_TARGETS;
2840 rs6000_sched_restricted_insns_priority
2841 = (rs6000_sched_groups ? 1 : 0);
2843 /* Handle -msched-costly-dep option. */
2844 rs6000_sched_costly_dep
2845 = (rs6000_sched_groups ? true_store_to_load_dep_costly : no_dep_costly);
2847 if (rs6000_sched_costly_dep_str)
2849 if (! strcmp (rs6000_sched_costly_dep_str, "no"))
2850 rs6000_sched_costly_dep = no_dep_costly;
2851 else if (! strcmp (rs6000_sched_costly_dep_str, "all"))
2852 rs6000_sched_costly_dep = all_deps_costly;
2853 else if (! strcmp (rs6000_sched_costly_dep_str, "true_store_to_load"))
2854 rs6000_sched_costly_dep = true_store_to_load_dep_costly;
2855 else if (! strcmp (rs6000_sched_costly_dep_str, "store_to_load"))
2856 rs6000_sched_costly_dep = store_to_load_dep_costly;
2857 else
2858 rs6000_sched_costly_dep = ((enum rs6000_dependence_cost)
2859 atoi (rs6000_sched_costly_dep_str));
2862 /* Handle -minsert-sched-nops option. */
2863 rs6000_sched_insert_nops
2864 = (rs6000_sched_groups ? sched_finish_regroup_exact : sched_finish_none);
2866 if (rs6000_sched_insert_nops_str)
2868 if (! strcmp (rs6000_sched_insert_nops_str, "no"))
2869 rs6000_sched_insert_nops = sched_finish_none;
2870 else if (! strcmp (rs6000_sched_insert_nops_str, "pad"))
2871 rs6000_sched_insert_nops = sched_finish_pad_groups;
2872 else if (! strcmp (rs6000_sched_insert_nops_str, "regroup_exact"))
2873 rs6000_sched_insert_nops = sched_finish_regroup_exact;
2874 else
2875 rs6000_sched_insert_nops = ((enum rs6000_nop_insertion)
2876 atoi (rs6000_sched_insert_nops_str));
2879 if (global_init_p)
2881 #ifdef TARGET_REGNAMES
2882 /* If the user desires alternate register names, copy in the
2883 alternate names now. */
2884 if (TARGET_REGNAMES)
2885 memcpy (rs6000_reg_names, alt_reg_names, sizeof (rs6000_reg_names));
2886 #endif
2888 /* Set aix_struct_return last, after the ABI is determined.
2889 If -maix-struct-return or -msvr4-struct-return was explicitly
2890 used, don't override with the ABI default. */
2891 if (!global_options_set.x_aix_struct_return)
2892 aix_struct_return = (DEFAULT_ABI != ABI_V4 || DRAFT_V4_STRUCT_RET);
2894 #if 0
2895 /* IBM XL compiler defaults to unsigned bitfields. */
2896 if (TARGET_XL_COMPAT)
2897 flag_signed_bitfields = 0;
2898 #endif
2900 if (TARGET_LONG_DOUBLE_128 && !TARGET_IEEEQUAD)
2901 REAL_MODE_FORMAT (TFmode) = &ibm_extended_format;
2903 if (TARGET_TOC)
2904 ASM_GENERATE_INTERNAL_LABEL (toc_label_name, "LCTOC", 1);
2906 /* We can only guarantee the availability of DI pseudo-ops when
2907 assembling for 64-bit targets. */
2908 if (!TARGET_64BIT)
2910 targetm.asm_out.aligned_op.di = NULL;
2911 targetm.asm_out.unaligned_op.di = NULL;
2915 /* Set branch target alignment, if not optimizing for size. */
2916 if (!optimize_size)
2918 /* Cell wants to be aligned 8byte for dual issue. Titan wants to be
2919 aligned 8byte to avoid misprediction by the branch predictor. */
2920 if (rs6000_cpu == PROCESSOR_TITAN
2921 || rs6000_cpu == PROCESSOR_CELL)
2923 if (align_functions <= 0)
2924 align_functions = 8;
2925 if (align_jumps <= 0)
2926 align_jumps = 8;
2927 if (align_loops <= 0)
2928 align_loops = 8;
2930 if (rs6000_align_branch_targets)
2932 if (align_functions <= 0)
2933 align_functions = 16;
2934 if (align_jumps <= 0)
2935 align_jumps = 16;
2936 if (align_loops <= 0)
2938 can_override_loop_align = 1;
2939 align_loops = 16;
2942 if (align_jumps_max_skip <= 0)
2943 align_jumps_max_skip = 15;
2944 if (align_loops_max_skip <= 0)
2945 align_loops_max_skip = 15;
2948 /* Arrange to save and restore machine status around nested functions. */
2949 init_machine_status = rs6000_init_machine_status;
2951 /* We should always be splitting complex arguments, but we can't break
2952 Linux and Darwin ABIs at the moment. For now, only AIX is fixed. */
2953 if (DEFAULT_ABI != ABI_AIX)
2954 targetm.calls.split_complex_arg = NULL;
2957 /* Initialize rs6000_cost with the appropriate target costs. */
2958 if (optimize_size)
2959 rs6000_cost = TARGET_POWERPC64 ? &size64_cost : &size32_cost;
2960 else
2961 switch (rs6000_cpu)
2963 case PROCESSOR_RS64A:
2964 rs6000_cost = &rs64a_cost;
2965 break;
2967 case PROCESSOR_MPCCORE:
2968 rs6000_cost = &mpccore_cost;
2969 break;
2971 case PROCESSOR_PPC403:
2972 rs6000_cost = &ppc403_cost;
2973 break;
2975 case PROCESSOR_PPC405:
2976 rs6000_cost = &ppc405_cost;
2977 break;
2979 case PROCESSOR_PPC440:
2980 rs6000_cost = &ppc440_cost;
2981 break;
2983 case PROCESSOR_PPC476:
2984 rs6000_cost = &ppc476_cost;
2985 break;
2987 case PROCESSOR_PPC601:
2988 rs6000_cost = &ppc601_cost;
2989 break;
2991 case PROCESSOR_PPC603:
2992 rs6000_cost = &ppc603_cost;
2993 break;
2995 case PROCESSOR_PPC604:
2996 rs6000_cost = &ppc604_cost;
2997 break;
2999 case PROCESSOR_PPC604e:
3000 rs6000_cost = &ppc604e_cost;
3001 break;
3003 case PROCESSOR_PPC620:
3004 rs6000_cost = &ppc620_cost;
3005 break;
3007 case PROCESSOR_PPC630:
3008 rs6000_cost = &ppc630_cost;
3009 break;
3011 case PROCESSOR_CELL:
3012 rs6000_cost = &ppccell_cost;
3013 break;
3015 case PROCESSOR_PPC750:
3016 case PROCESSOR_PPC7400:
3017 rs6000_cost = &ppc750_cost;
3018 break;
3020 case PROCESSOR_PPC7450:
3021 rs6000_cost = &ppc7450_cost;
3022 break;
3024 case PROCESSOR_PPC8540:
3025 case PROCESSOR_PPC8548:
3026 rs6000_cost = &ppc8540_cost;
3027 break;
3029 case PROCESSOR_PPCE300C2:
3030 case PROCESSOR_PPCE300C3:
3031 rs6000_cost = &ppce300c2c3_cost;
3032 break;
3034 case PROCESSOR_PPCE500MC:
3035 rs6000_cost = &ppce500mc_cost;
3036 break;
3038 case PROCESSOR_PPCE500MC64:
3039 rs6000_cost = &ppce500mc64_cost;
3040 break;
3042 case PROCESSOR_PPCE5500:
3043 rs6000_cost = &ppce5500_cost;
3044 break;
3046 case PROCESSOR_PPCE6500:
3047 rs6000_cost = &ppce6500_cost;
3048 break;
3050 case PROCESSOR_TITAN:
3051 rs6000_cost = &titan_cost;
3052 break;
3054 case PROCESSOR_POWER4:
3055 case PROCESSOR_POWER5:
3056 rs6000_cost = &power4_cost;
3057 break;
3059 case PROCESSOR_POWER6:
3060 rs6000_cost = &power6_cost;
3061 break;
3063 case PROCESSOR_POWER7:
3064 rs6000_cost = &power7_cost;
3065 break;
3067 case PROCESSOR_PPCA2:
3068 rs6000_cost = &ppca2_cost;
3069 break;
3071 default:
3072 gcc_unreachable ();
3075 if (global_init_p)
3077 maybe_set_param_value (PARAM_SIMULTANEOUS_PREFETCHES,
3078 rs6000_cost->simultaneous_prefetches,
3079 global_options.x_param_values,
3080 global_options_set.x_param_values);
3081 maybe_set_param_value (PARAM_L1_CACHE_SIZE, rs6000_cost->l1_cache_size,
3082 global_options.x_param_values,
3083 global_options_set.x_param_values);
3084 maybe_set_param_value (PARAM_L1_CACHE_LINE_SIZE,
3085 rs6000_cost->cache_line_size,
3086 global_options.x_param_values,
3087 global_options_set.x_param_values);
3088 maybe_set_param_value (PARAM_L2_CACHE_SIZE, rs6000_cost->l2_cache_size,
3089 global_options.x_param_values,
3090 global_options_set.x_param_values);
3092 /* If using typedef char *va_list, signal that
3093 __builtin_va_start (&ap, 0) can be optimized to
3094 ap = __builtin_next_arg (0). */
3095 if (DEFAULT_ABI != ABI_V4)
3096 targetm.expand_builtin_va_start = NULL;
3099 /* Set up single/double float flags.
3100 If TARGET_HARD_FLOAT is set, but neither single or double is set,
3101 then set both flags. */
3102 if (TARGET_HARD_FLOAT && TARGET_FPRS
3103 && rs6000_single_float == 0 && rs6000_double_float == 0)
3104 rs6000_single_float = rs6000_double_float = 1;
3106 /* If not explicitly specified via option, decide whether to generate indexed
3107 load/store instructions. */
3108 if (TARGET_AVOID_XFORM == -1)
3109 /* Avoid indexed addressing when targeting Power6 in order to avoid the
3110 DERAT mispredict penalty. However the LVE and STVE altivec instructions
3111 need indexed accesses and the type used is the scalar type of the element
3112 being loaded or stored. */
3113 TARGET_AVOID_XFORM = (rs6000_cpu == PROCESSOR_POWER6 && TARGET_CMPB
3114 && !TARGET_ALTIVEC);
3116 /* Set the -mrecip options. */
3117 if (rs6000_recip_name)
3119 char *p = ASTRDUP (rs6000_recip_name);
3120 char *q;
3121 unsigned int mask, i;
3122 bool invert;
3124 while ((q = strtok (p, ",")) != NULL)
3126 p = NULL;
3127 if (*q == '!')
3129 invert = true;
3130 q++;
3132 else
3133 invert = false;
3135 if (!strcmp (q, "default"))
3136 mask = ((TARGET_RECIP_PRECISION)
3137 ? RECIP_HIGH_PRECISION : RECIP_LOW_PRECISION);
3138 else
3140 for (i = 0; i < ARRAY_SIZE (recip_options); i++)
3141 if (!strcmp (q, recip_options[i].string))
3143 mask = recip_options[i].mask;
3144 break;
3147 if (i == ARRAY_SIZE (recip_options))
3149 error ("unknown option for -mrecip=%s", q);
3150 invert = false;
3151 mask = 0;
3152 ret = false;
3156 if (invert)
3157 rs6000_recip_control &= ~mask;
3158 else
3159 rs6000_recip_control |= mask;
3163 /* Set the builtin mask of the various options used that could affect which
3164 builtins were used. In the past we used target_flags, but we've run out
3165 of bits, and some options like SPE and PAIRED are no longer in
3166 target_flags. */
3167 rs6000_builtin_mask = rs6000_builtin_mask_calculate ();
3168 if (TARGET_DEBUG_BUILTIN || TARGET_DEBUG_TARGET)
3169 fprintf (stderr, "new builtin mask = 0x%x%s%s%s%s\n", rs6000_builtin_mask,
3170 (rs6000_builtin_mask & RS6000_BTM_ALTIVEC) ? ", altivec" : "",
3171 (rs6000_builtin_mask & RS6000_BTM_VSX) ? ", vsx" : "",
3172 (rs6000_builtin_mask & RS6000_BTM_PAIRED) ? ", paired" : "",
3173 (rs6000_builtin_mask & RS6000_BTM_SPE) ? ", spe" : "");
3175 /* Initialize all of the registers. */
3176 rs6000_init_hard_regno_mode_ok (global_init_p);
3178 /* Save the initial options in case the user does function specific options */
3179 if (global_init_p)
3180 target_option_default_node = target_option_current_node
3181 = build_target_option_node ();
3183 /* If not explicitly specified via option, decide whether to generate the
3184 extra blr's required to preserve the link stack on some cpus (eg, 476). */
3185 if (TARGET_LINK_STACK == -1)
3186 SET_TARGET_LINK_STACK (rs6000_cpu == PROCESSOR_PPC476 && flag_pic);
3188 return ret;
3191 /* Implement TARGET_OPTION_OVERRIDE. On the RS/6000 this is used to
3192 define the target cpu type. */
3194 static void
3195 rs6000_option_override (void)
3197 (void) rs6000_option_override_internal (true);
3201 /* Implement targetm.vectorize.builtin_mask_for_load. */
3202 static tree
3203 rs6000_builtin_mask_for_load (void)
3205 if (TARGET_ALTIVEC || TARGET_VSX)
3206 return altivec_builtin_mask_for_load;
3207 else
3208 return 0;
3211 /* Implement LOOP_ALIGN. */
3213 rs6000_loop_align (rtx label)
3215 basic_block bb;
3216 int ninsns;
3218 /* Don't override loop alignment if -falign-loops was specified. */
3219 if (!can_override_loop_align)
3220 return align_loops_log;
3222 bb = BLOCK_FOR_INSN (label);
3223 ninsns = num_loop_insns(bb->loop_father);
3225 /* Align small loops to 32 bytes to fit in an icache sector, otherwise return default. */
3226 if (ninsns > 4 && ninsns <= 8
3227 && (rs6000_cpu == PROCESSOR_POWER4
3228 || rs6000_cpu == PROCESSOR_POWER5
3229 || rs6000_cpu == PROCESSOR_POWER6
3230 || rs6000_cpu == PROCESSOR_POWER7))
3231 return 5;
3232 else
3233 return align_loops_log;
3236 /* Implement TARGET_LOOP_ALIGN_MAX_SKIP. */
3237 static int
3238 rs6000_loop_align_max_skip (rtx label)
3240 return (1 << rs6000_loop_align (label)) - 1;
3243 /* Return true iff, data reference of TYPE can reach vector alignment (16)
3244 after applying N number of iterations. This routine does not determine
3245 how may iterations are required to reach desired alignment. */
3247 static bool
3248 rs6000_vector_alignment_reachable (const_tree type ATTRIBUTE_UNUSED, bool is_packed)
3250 if (is_packed)
3251 return false;
3253 if (TARGET_32BIT)
3255 if (rs6000_alignment_flags == MASK_ALIGN_NATURAL)
3256 return true;
3258 if (rs6000_alignment_flags == MASK_ALIGN_POWER)
3259 return true;
3261 return false;
3263 else
3265 if (TARGET_MACHO)
3266 return false;
3268 /* Assuming that all other types are naturally aligned. CHECKME! */
3269 return true;
3273 /* Return true if the vector misalignment factor is supported by the
3274 target. */
3275 static bool
3276 rs6000_builtin_support_vector_misalignment (enum machine_mode mode,
3277 const_tree type,
3278 int misalignment,
3279 bool is_packed)
3281 if (TARGET_VSX)
3283 /* Return if movmisalign pattern is not supported for this mode. */
3284 if (optab_handler (movmisalign_optab, mode) == CODE_FOR_nothing)
3285 return false;
3287 if (misalignment == -1)
3289 /* Misalignment factor is unknown at compile time but we know
3290 it's word aligned. */
3291 if (rs6000_vector_alignment_reachable (type, is_packed))
3293 int element_size = TREE_INT_CST_LOW (TYPE_SIZE (type));
3295 if (element_size == 64 || element_size == 32)
3296 return true;
3299 return false;
3302 /* VSX supports word-aligned vector. */
3303 if (misalignment % 4 == 0)
3304 return true;
3306 return false;
3309 /* Implement targetm.vectorize.builtin_vectorization_cost. */
3310 static int
3311 rs6000_builtin_vectorization_cost (enum vect_cost_for_stmt type_of_cost,
3312 tree vectype, int misalign)
3314 unsigned elements;
3315 tree elem_type;
3317 switch (type_of_cost)
3319 case scalar_stmt:
3320 case scalar_load:
3321 case scalar_store:
3322 case vector_stmt:
3323 case vector_load:
3324 case vector_store:
3325 case vec_to_scalar:
3326 case scalar_to_vec:
3327 case cond_branch_not_taken:
3328 return 1;
3330 case vec_perm:
3331 if (TARGET_VSX)
3332 return 4;
3333 else
3334 return 1;
3336 case vec_promote_demote:
3337 if (TARGET_VSX)
3338 return 5;
3339 else
3340 return 1;
3342 case cond_branch_taken:
3343 return 3;
3345 case unaligned_load:
3346 if (TARGET_VSX && TARGET_ALLOW_MOVMISALIGN)
3348 elements = TYPE_VECTOR_SUBPARTS (vectype);
3349 if (elements == 2)
3350 /* Double word aligned. */
3351 return 2;
3353 if (elements == 4)
3355 switch (misalign)
3357 case 8:
3358 /* Double word aligned. */
3359 return 2;
3361 case -1:
3362 /* Unknown misalignment. */
3363 case 4:
3364 case 12:
3365 /* Word aligned. */
3366 return 22;
3368 default:
3369 gcc_unreachable ();
3374 if (TARGET_ALTIVEC)
3375 /* Misaligned loads are not supported. */
3376 gcc_unreachable ();
3378 return 2;
3380 case unaligned_store:
3381 if (TARGET_VSX && TARGET_ALLOW_MOVMISALIGN)
3383 elements = TYPE_VECTOR_SUBPARTS (vectype);
3384 if (elements == 2)
3385 /* Double word aligned. */
3386 return 2;
3388 if (elements == 4)
3390 switch (misalign)
3392 case 8:
3393 /* Double word aligned. */
3394 return 2;
3396 case -1:
3397 /* Unknown misalignment. */
3398 case 4:
3399 case 12:
3400 /* Word aligned. */
3401 return 23;
3403 default:
3404 gcc_unreachable ();
3409 if (TARGET_ALTIVEC)
3410 /* Misaligned stores are not supported. */
3411 gcc_unreachable ();
3413 return 2;
3415 case vec_construct:
3416 elements = TYPE_VECTOR_SUBPARTS (vectype);
3417 elem_type = TREE_TYPE (vectype);
3418 /* 32-bit vectors loaded into registers are stored as double
3419 precision, so we need n/2 converts in addition to the usual
3420 n/2 merges to construct a vector of short floats from them. */
3421 if (SCALAR_FLOAT_TYPE_P (elem_type)
3422 && TYPE_PRECISION (elem_type) == 32)
3423 return elements + 1;
3424 else
3425 return elements / 2 + 1;
3427 default:
3428 gcc_unreachable ();
3432 /* Implement targetm.vectorize.preferred_simd_mode. */
3434 static enum machine_mode
3435 rs6000_preferred_simd_mode (enum machine_mode mode)
3437 if (TARGET_VSX)
3438 switch (mode)
3440 case DFmode:
3441 return V2DFmode;
3442 default:;
3444 if (TARGET_ALTIVEC || TARGET_VSX)
3445 switch (mode)
3447 case SFmode:
3448 return V4SFmode;
3449 case DImode:
3450 return V2DImode;
3451 case SImode:
3452 return V4SImode;
3453 case HImode:
3454 return V8HImode;
3455 case QImode:
3456 return V16QImode;
3457 default:;
3459 if (TARGET_SPE)
3460 switch (mode)
3462 case SFmode:
3463 return V2SFmode;
3464 case SImode:
3465 return V2SImode;
3466 default:;
3468 if (TARGET_PAIRED_FLOAT
3469 && mode == SFmode)
3470 return V2SFmode;
3471 return word_mode;
3474 /* Implement targetm.vectorize.init_cost. */
3476 static void *
3477 rs6000_init_cost (struct loop *loop_info ATTRIBUTE_UNUSED)
3479 unsigned *cost = XNEWVEC (unsigned, 3);
3480 cost[vect_prologue] = cost[vect_body] = cost[vect_epilogue] = 0;
3481 return cost;
3484 /* Implement targetm.vectorize.add_stmt_cost. */
3486 static unsigned
3487 rs6000_add_stmt_cost (void *data, int count, enum vect_cost_for_stmt kind,
3488 struct _stmt_vec_info *stmt_info, int misalign,
3489 enum vect_cost_model_location where)
3491 unsigned *cost = (unsigned *) data;
3492 unsigned retval = 0;
3494 if (flag_vect_cost_model)
3496 tree vectype = stmt_info ? stmt_vectype (stmt_info) : NULL_TREE;
3497 int stmt_cost = rs6000_builtin_vectorization_cost (kind, vectype,
3498 misalign);
3499 /* Statements in an inner loop relative to the loop being
3500 vectorized are weighted more heavily. The value here is
3501 arbitrary and could potentially be improved with analysis. */
3502 if (where == vect_body && stmt_info && stmt_in_inner_loop_p (stmt_info))
3503 count *= 50; /* FIXME. */
3505 retval = (unsigned) (count * stmt_cost);
3506 cost[where] += retval;
3509 return retval;
3512 /* Implement targetm.vectorize.finish_cost. */
3514 static void
3515 rs6000_finish_cost (void *data, unsigned *prologue_cost,
3516 unsigned *body_cost, unsigned *epilogue_cost)
3518 unsigned *cost = (unsigned *) data;
3519 *prologue_cost = cost[vect_prologue];
3520 *body_cost = cost[vect_body];
3521 *epilogue_cost = cost[vect_epilogue];
3524 /* Implement targetm.vectorize.destroy_cost_data. */
3526 static void
3527 rs6000_destroy_cost_data (void *data)
3529 free (data);
3532 /* Handler for the Mathematical Acceleration Subsystem (mass) interface to a
3533 library with vectorized intrinsics. */
3535 static tree
3536 rs6000_builtin_vectorized_libmass (tree fndecl, tree type_out, tree type_in)
3538 char name[32];
3539 const char *suffix = NULL;
3540 tree fntype, new_fndecl, bdecl = NULL_TREE;
3541 int n_args = 1;
3542 const char *bname;
3543 enum machine_mode el_mode, in_mode;
3544 int n, in_n;
3546 /* Libmass is suitable for unsafe math only as it does not correctly support
3547 parts of IEEE with the required precision such as denormals. Only support
3548 it if we have VSX to use the simd d2 or f4 functions.
3549 XXX: Add variable length support. */
3550 if (!flag_unsafe_math_optimizations || !TARGET_VSX)
3551 return NULL_TREE;
3553 el_mode = TYPE_MODE (TREE_TYPE (type_out));
3554 n = TYPE_VECTOR_SUBPARTS (type_out);
3555 in_mode = TYPE_MODE (TREE_TYPE (type_in));
3556 in_n = TYPE_VECTOR_SUBPARTS (type_in);
3557 if (el_mode != in_mode
3558 || n != in_n)
3559 return NULL_TREE;
3561 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
3563 enum built_in_function fn = DECL_FUNCTION_CODE (fndecl);
3564 switch (fn)
3566 case BUILT_IN_ATAN2:
3567 case BUILT_IN_HYPOT:
3568 case BUILT_IN_POW:
3569 n_args = 2;
3570 /* fall through */
3572 case BUILT_IN_ACOS:
3573 case BUILT_IN_ACOSH:
3574 case BUILT_IN_ASIN:
3575 case BUILT_IN_ASINH:
3576 case BUILT_IN_ATAN:
3577 case BUILT_IN_ATANH:
3578 case BUILT_IN_CBRT:
3579 case BUILT_IN_COS:
3580 case BUILT_IN_COSH:
3581 case BUILT_IN_ERF:
3582 case BUILT_IN_ERFC:
3583 case BUILT_IN_EXP2:
3584 case BUILT_IN_EXP:
3585 case BUILT_IN_EXPM1:
3586 case BUILT_IN_LGAMMA:
3587 case BUILT_IN_LOG10:
3588 case BUILT_IN_LOG1P:
3589 case BUILT_IN_LOG2:
3590 case BUILT_IN_LOG:
3591 case BUILT_IN_SIN:
3592 case BUILT_IN_SINH:
3593 case BUILT_IN_SQRT:
3594 case BUILT_IN_TAN:
3595 case BUILT_IN_TANH:
3596 bdecl = builtin_decl_implicit (fn);
3597 suffix = "d2"; /* pow -> powd2 */
3598 if (el_mode != DFmode
3599 || n != 2)
3600 return NULL_TREE;
3601 break;
3603 case BUILT_IN_ATAN2F:
3604 case BUILT_IN_HYPOTF:
3605 case BUILT_IN_POWF:
3606 n_args = 2;
3607 /* fall through */
3609 case BUILT_IN_ACOSF:
3610 case BUILT_IN_ACOSHF:
3611 case BUILT_IN_ASINF:
3612 case BUILT_IN_ASINHF:
3613 case BUILT_IN_ATANF:
3614 case BUILT_IN_ATANHF:
3615 case BUILT_IN_CBRTF:
3616 case BUILT_IN_COSF:
3617 case BUILT_IN_COSHF:
3618 case BUILT_IN_ERFF:
3619 case BUILT_IN_ERFCF:
3620 case BUILT_IN_EXP2F:
3621 case BUILT_IN_EXPF:
3622 case BUILT_IN_EXPM1F:
3623 case BUILT_IN_LGAMMAF:
3624 case BUILT_IN_LOG10F:
3625 case BUILT_IN_LOG1PF:
3626 case BUILT_IN_LOG2F:
3627 case BUILT_IN_LOGF:
3628 case BUILT_IN_SINF:
3629 case BUILT_IN_SINHF:
3630 case BUILT_IN_SQRTF:
3631 case BUILT_IN_TANF:
3632 case BUILT_IN_TANHF:
3633 bdecl = builtin_decl_implicit (fn);
3634 suffix = "4"; /* powf -> powf4 */
3635 if (el_mode != SFmode
3636 || n != 4)
3637 return NULL_TREE;
3638 break;
3640 default:
3641 return NULL_TREE;
3644 else
3645 return NULL_TREE;
3647 gcc_assert (suffix != NULL);
3648 bname = IDENTIFIER_POINTER (DECL_NAME (bdecl));
3649 strcpy (name, bname + sizeof ("__builtin_") - 1);
3650 strcat (name, suffix);
3652 if (n_args == 1)
3653 fntype = build_function_type_list (type_out, type_in, NULL);
3654 else if (n_args == 2)
3655 fntype = build_function_type_list (type_out, type_in, type_in, NULL);
3656 else
3657 gcc_unreachable ();
3659 /* Build a function declaration for the vectorized function. */
3660 new_fndecl = build_decl (BUILTINS_LOCATION,
3661 FUNCTION_DECL, get_identifier (name), fntype);
3662 TREE_PUBLIC (new_fndecl) = 1;
3663 DECL_EXTERNAL (new_fndecl) = 1;
3664 DECL_IS_NOVOPS (new_fndecl) = 1;
3665 TREE_READONLY (new_fndecl) = 1;
3667 return new_fndecl;
3670 /* Returns a function decl for a vectorized version of the builtin function
3671 with builtin function code FN and the result vector type TYPE, or NULL_TREE
3672 if it is not available. */
3674 static tree
3675 rs6000_builtin_vectorized_function (tree fndecl, tree type_out,
3676 tree type_in)
3678 enum machine_mode in_mode, out_mode;
3679 int in_n, out_n;
3681 if (TARGET_DEBUG_BUILTIN)
3682 fprintf (stderr, "rs6000_builtin_vectorized_function (%s, %s, %s)\n",
3683 IDENTIFIER_POINTER (DECL_NAME (fndecl)),
3684 GET_MODE_NAME (TYPE_MODE (type_out)),
3685 GET_MODE_NAME (TYPE_MODE (type_in)));
3687 if (TREE_CODE (type_out) != VECTOR_TYPE
3688 || TREE_CODE (type_in) != VECTOR_TYPE
3689 || !TARGET_VECTORIZE_BUILTINS)
3690 return NULL_TREE;
3692 out_mode = TYPE_MODE (TREE_TYPE (type_out));
3693 out_n = TYPE_VECTOR_SUBPARTS (type_out);
3694 in_mode = TYPE_MODE (TREE_TYPE (type_in));
3695 in_n = TYPE_VECTOR_SUBPARTS (type_in);
3697 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
3699 enum built_in_function fn = DECL_FUNCTION_CODE (fndecl);
3700 switch (fn)
3702 case BUILT_IN_COPYSIGN:
3703 if (VECTOR_UNIT_VSX_P (V2DFmode)
3704 && out_mode == DFmode && out_n == 2
3705 && in_mode == DFmode && in_n == 2)
3706 return rs6000_builtin_decls[VSX_BUILTIN_CPSGNDP];
3707 break;
3708 case BUILT_IN_COPYSIGNF:
3709 if (out_mode != SFmode || out_n != 4
3710 || in_mode != SFmode || in_n != 4)
3711 break;
3712 if (VECTOR_UNIT_VSX_P (V4SFmode))
3713 return rs6000_builtin_decls[VSX_BUILTIN_CPSGNSP];
3714 if (VECTOR_UNIT_ALTIVEC_P (V4SFmode))
3715 return rs6000_builtin_decls[ALTIVEC_BUILTIN_COPYSIGN_V4SF];
3716 break;
3717 case BUILT_IN_SQRT:
3718 if (VECTOR_UNIT_VSX_P (V2DFmode)
3719 && out_mode == DFmode && out_n == 2
3720 && in_mode == DFmode && in_n == 2)
3721 return rs6000_builtin_decls[VSX_BUILTIN_XVSQRTDP];
3722 break;
3723 case BUILT_IN_SQRTF:
3724 if (VECTOR_UNIT_VSX_P (V4SFmode)
3725 && out_mode == SFmode && out_n == 4
3726 && in_mode == SFmode && in_n == 4)
3727 return rs6000_builtin_decls[VSX_BUILTIN_XVSQRTSP];
3728 break;
3729 case BUILT_IN_CEIL:
3730 if (VECTOR_UNIT_VSX_P (V2DFmode)
3731 && out_mode == DFmode && out_n == 2
3732 && in_mode == DFmode && in_n == 2)
3733 return rs6000_builtin_decls[VSX_BUILTIN_XVRDPIP];
3734 break;
3735 case BUILT_IN_CEILF:
3736 if (out_mode != SFmode || out_n != 4
3737 || in_mode != SFmode || in_n != 4)
3738 break;
3739 if (VECTOR_UNIT_VSX_P (V4SFmode))
3740 return rs6000_builtin_decls[VSX_BUILTIN_XVRSPIP];
3741 if (VECTOR_UNIT_ALTIVEC_P (V4SFmode))
3742 return rs6000_builtin_decls[ALTIVEC_BUILTIN_VRFIP];
3743 break;
3744 case BUILT_IN_FLOOR:
3745 if (VECTOR_UNIT_VSX_P (V2DFmode)
3746 && out_mode == DFmode && out_n == 2
3747 && in_mode == DFmode && in_n == 2)
3748 return rs6000_builtin_decls[VSX_BUILTIN_XVRDPIM];
3749 break;
3750 case BUILT_IN_FLOORF:
3751 if (out_mode != SFmode || out_n != 4
3752 || in_mode != SFmode || in_n != 4)
3753 break;
3754 if (VECTOR_UNIT_VSX_P (V4SFmode))
3755 return rs6000_builtin_decls[VSX_BUILTIN_XVRSPIM];
3756 if (VECTOR_UNIT_ALTIVEC_P (V4SFmode))
3757 return rs6000_builtin_decls[ALTIVEC_BUILTIN_VRFIM];
3758 break;
3759 case BUILT_IN_FMA:
3760 if (VECTOR_UNIT_VSX_P (V2DFmode)
3761 && out_mode == DFmode && out_n == 2
3762 && in_mode == DFmode && in_n == 2)
3763 return rs6000_builtin_decls[VSX_BUILTIN_XVMADDDP];
3764 break;
3765 case BUILT_IN_FMAF:
3766 if (VECTOR_UNIT_VSX_P (V4SFmode)
3767 && out_mode == SFmode && out_n == 4
3768 && in_mode == SFmode && in_n == 4)
3769 return rs6000_builtin_decls[VSX_BUILTIN_XVMADDSP];
3770 else if (VECTOR_UNIT_ALTIVEC_P (V4SFmode)
3771 && out_mode == SFmode && out_n == 4
3772 && in_mode == SFmode && in_n == 4)
3773 return rs6000_builtin_decls[ALTIVEC_BUILTIN_VMADDFP];
3774 break;
3775 case BUILT_IN_TRUNC:
3776 if (VECTOR_UNIT_VSX_P (V2DFmode)
3777 && out_mode == DFmode && out_n == 2
3778 && in_mode == DFmode && in_n == 2)
3779 return rs6000_builtin_decls[VSX_BUILTIN_XVRDPIZ];
3780 break;
3781 case BUILT_IN_TRUNCF:
3782 if (out_mode != SFmode || out_n != 4
3783 || in_mode != SFmode || in_n != 4)
3784 break;
3785 if (VECTOR_UNIT_VSX_P (V4SFmode))
3786 return rs6000_builtin_decls[VSX_BUILTIN_XVRSPIZ];
3787 if (VECTOR_UNIT_ALTIVEC_P (V4SFmode))
3788 return rs6000_builtin_decls[ALTIVEC_BUILTIN_VRFIZ];
3789 break;
3790 case BUILT_IN_NEARBYINT:
3791 if (VECTOR_UNIT_VSX_P (V2DFmode)
3792 && flag_unsafe_math_optimizations
3793 && out_mode == DFmode && out_n == 2
3794 && in_mode == DFmode && in_n == 2)
3795 return rs6000_builtin_decls[VSX_BUILTIN_XVRDPI];
3796 break;
3797 case BUILT_IN_NEARBYINTF:
3798 if (VECTOR_UNIT_VSX_P (V4SFmode)
3799 && flag_unsafe_math_optimizations
3800 && out_mode == SFmode && out_n == 4
3801 && in_mode == SFmode && in_n == 4)
3802 return rs6000_builtin_decls[VSX_BUILTIN_XVRSPI];
3803 break;
3804 case BUILT_IN_RINT:
3805 if (VECTOR_UNIT_VSX_P (V2DFmode)
3806 && !flag_trapping_math
3807 && out_mode == DFmode && out_n == 2
3808 && in_mode == DFmode && in_n == 2)
3809 return rs6000_builtin_decls[VSX_BUILTIN_XVRDPIC];
3810 break;
3811 case BUILT_IN_RINTF:
3812 if (VECTOR_UNIT_VSX_P (V4SFmode)
3813 && !flag_trapping_math
3814 && out_mode == SFmode && out_n == 4
3815 && in_mode == SFmode && in_n == 4)
3816 return rs6000_builtin_decls[VSX_BUILTIN_XVRSPIC];
3817 break;
3818 default:
3819 break;
3823 else if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
3825 enum rs6000_builtins fn
3826 = (enum rs6000_builtins)DECL_FUNCTION_CODE (fndecl);
3827 switch (fn)
3829 case RS6000_BUILTIN_RSQRTF:
3830 if (VECTOR_UNIT_ALTIVEC_OR_VSX_P (V4SFmode)
3831 && out_mode == SFmode && out_n == 4
3832 && in_mode == SFmode && in_n == 4)
3833 return rs6000_builtin_decls[ALTIVEC_BUILTIN_VRSQRTFP];
3834 break;
3835 case RS6000_BUILTIN_RSQRT:
3836 if (VECTOR_UNIT_VSX_P (V2DFmode)
3837 && out_mode == DFmode && out_n == 2
3838 && in_mode == DFmode && in_n == 2)
3839 return rs6000_builtin_decls[VSX_BUILTIN_RSQRT_2DF];
3840 break;
3841 case RS6000_BUILTIN_RECIPF:
3842 if (VECTOR_UNIT_ALTIVEC_OR_VSX_P (V4SFmode)
3843 && out_mode == SFmode && out_n == 4
3844 && in_mode == SFmode && in_n == 4)
3845 return rs6000_builtin_decls[ALTIVEC_BUILTIN_VRECIPFP];
3846 break;
3847 case RS6000_BUILTIN_RECIP:
3848 if (VECTOR_UNIT_VSX_P (V2DFmode)
3849 && out_mode == DFmode && out_n == 2
3850 && in_mode == DFmode && in_n == 2)
3851 return rs6000_builtin_decls[VSX_BUILTIN_RECIP_V2DF];
3852 break;
3853 default:
3854 break;
3858 /* Generate calls to libmass if appropriate. */
3859 if (rs6000_veclib_handler)
3860 return rs6000_veclib_handler (fndecl, type_out, type_in);
3862 return NULL_TREE;
3865 /* Default CPU string for rs6000*_file_start functions. */
3866 static const char *rs6000_default_cpu;
3868 /* Do anything needed at the start of the asm file. */
3870 static void
3871 rs6000_file_start (void)
3873 char buffer[80];
3874 const char *start = buffer;
3875 FILE *file = asm_out_file;
3877 rs6000_default_cpu = TARGET_CPU_DEFAULT;
3879 default_file_start ();
3881 if (flag_verbose_asm)
3883 sprintf (buffer, "\n%s rs6000/powerpc options:", ASM_COMMENT_START);
3885 if (rs6000_default_cpu != 0 && rs6000_default_cpu[0] != '\0')
3887 fprintf (file, "%s --with-cpu=%s", start, rs6000_default_cpu);
3888 start = "";
3891 if (global_options_set.x_rs6000_cpu_index)
3893 fprintf (file, "%s -mcpu=%s", start,
3894 processor_target_table[rs6000_cpu_index].name);
3895 start = "";
3898 if (global_options_set.x_rs6000_tune_index)
3900 fprintf (file, "%s -mtune=%s", start,
3901 processor_target_table[rs6000_tune_index].name);
3902 start = "";
3905 if (PPC405_ERRATUM77)
3907 fprintf (file, "%s PPC405CR_ERRATUM77", start);
3908 start = "";
3911 #ifdef USING_ELFOS_H
3912 switch (rs6000_sdata)
3914 case SDATA_NONE: fprintf (file, "%s -msdata=none", start); start = ""; break;
3915 case SDATA_DATA: fprintf (file, "%s -msdata=data", start); start = ""; break;
3916 case SDATA_SYSV: fprintf (file, "%s -msdata=sysv", start); start = ""; break;
3917 case SDATA_EABI: fprintf (file, "%s -msdata=eabi", start); start = ""; break;
3920 if (rs6000_sdata && g_switch_value)
3922 fprintf (file, "%s -G %d", start,
3923 g_switch_value);
3924 start = "";
3926 #endif
3928 if (*start == '\0')
3929 putc ('\n', file);
3932 if (DEFAULT_ABI == ABI_AIX || (TARGET_ELF && flag_pic == 2))
3934 switch_to_section (toc_section);
3935 switch_to_section (text_section);
3940 /* Return nonzero if this function is known to have a null epilogue. */
3943 direct_return (void)
3945 if (reload_completed)
3947 rs6000_stack_t *info = rs6000_stack_info ();
3949 if (info->first_gp_reg_save == 32
3950 && info->first_fp_reg_save == 64
3951 && info->first_altivec_reg_save == LAST_ALTIVEC_REGNO + 1
3952 && ! info->lr_save_p
3953 && ! info->cr_save_p
3954 && info->vrsave_mask == 0
3955 && ! info->push_p)
3956 return 1;
3959 return 0;
3962 /* Return the number of instructions it takes to form a constant in an
3963 integer register. */
3966 num_insns_constant_wide (HOST_WIDE_INT value)
3968 /* signed constant loadable with {cal|addi} */
3969 if ((unsigned HOST_WIDE_INT) (value + 0x8000) < 0x10000)
3970 return 1;
3972 /* constant loadable with {cau|addis} */
3973 else if ((value & 0xffff) == 0
3974 && (value >> 31 == -1 || value >> 31 == 0))
3975 return 1;
3977 #if HOST_BITS_PER_WIDE_INT == 64
3978 else if (TARGET_POWERPC64)
3980 HOST_WIDE_INT low = ((value & 0xffffffff) ^ 0x80000000) - 0x80000000;
3981 HOST_WIDE_INT high = value >> 31;
3983 if (high == 0 || high == -1)
3984 return 2;
3986 high >>= 1;
3988 if (low == 0)
3989 return num_insns_constant_wide (high) + 1;
3990 else if (high == 0)
3991 return num_insns_constant_wide (low) + 1;
3992 else
3993 return (num_insns_constant_wide (high)
3994 + num_insns_constant_wide (low) + 1);
3996 #endif
3998 else
3999 return 2;
4003 num_insns_constant (rtx op, enum machine_mode mode)
4005 HOST_WIDE_INT low, high;
4007 switch (GET_CODE (op))
4009 case CONST_INT:
4010 #if HOST_BITS_PER_WIDE_INT == 64
4011 if ((INTVAL (op) >> 31) != 0 && (INTVAL (op) >> 31) != -1
4012 && mask64_operand (op, mode))
4013 return 2;
4014 else
4015 #endif
4016 return num_insns_constant_wide (INTVAL (op));
4018 case CONST_DOUBLE:
4019 if (mode == SFmode || mode == SDmode)
4021 long l;
4022 REAL_VALUE_TYPE rv;
4024 REAL_VALUE_FROM_CONST_DOUBLE (rv, op);
4025 if (DECIMAL_FLOAT_MODE_P (mode))
4026 REAL_VALUE_TO_TARGET_DECIMAL32 (rv, l);
4027 else
4028 REAL_VALUE_TO_TARGET_SINGLE (rv, l);
4029 return num_insns_constant_wide ((HOST_WIDE_INT) l);
4032 if (mode == VOIDmode || mode == DImode)
4034 high = CONST_DOUBLE_HIGH (op);
4035 low = CONST_DOUBLE_LOW (op);
4037 else
4039 long l[2];
4040 REAL_VALUE_TYPE rv;
4042 REAL_VALUE_FROM_CONST_DOUBLE (rv, op);
4043 if (DECIMAL_FLOAT_MODE_P (mode))
4044 REAL_VALUE_TO_TARGET_DECIMAL64 (rv, l);
4045 else
4046 REAL_VALUE_TO_TARGET_DOUBLE (rv, l);
4047 high = l[WORDS_BIG_ENDIAN == 0];
4048 low = l[WORDS_BIG_ENDIAN != 0];
4051 if (TARGET_32BIT)
4052 return (num_insns_constant_wide (low)
4053 + num_insns_constant_wide (high));
4054 else
4056 if ((high == 0 && low >= 0)
4057 || (high == -1 && low < 0))
4058 return num_insns_constant_wide (low);
4060 else if (mask64_operand (op, mode))
4061 return 2;
4063 else if (low == 0)
4064 return num_insns_constant_wide (high) + 1;
4066 else
4067 return (num_insns_constant_wide (high)
4068 + num_insns_constant_wide (low) + 1);
4071 default:
4072 gcc_unreachable ();
4076 /* Interpret element ELT of the CONST_VECTOR OP as an integer value.
4077 If the mode of OP is MODE_VECTOR_INT, this simply returns the
4078 corresponding element of the vector, but for V4SFmode and V2SFmode,
4079 the corresponding "float" is interpreted as an SImode integer. */
4081 HOST_WIDE_INT
4082 const_vector_elt_as_int (rtx op, unsigned int elt)
4084 rtx tmp;
4086 /* We can't handle V2DImode and V2DFmode vector constants here yet. */
4087 gcc_assert (GET_MODE (op) != V2DImode
4088 && GET_MODE (op) != V2DFmode);
4090 tmp = CONST_VECTOR_ELT (op, elt);
4091 if (GET_MODE (op) == V4SFmode
4092 || GET_MODE (op) == V2SFmode)
4093 tmp = gen_lowpart (SImode, tmp);
4094 return INTVAL (tmp);
4097 /* Return true if OP can be synthesized with a particular vspltisb, vspltish
4098 or vspltisw instruction. OP is a CONST_VECTOR. Which instruction is used
4099 depends on STEP and COPIES, one of which will be 1. If COPIES > 1,
4100 all items are set to the same value and contain COPIES replicas of the
4101 vsplt's operand; if STEP > 1, one in STEP elements is set to the vsplt's
4102 operand and the others are set to the value of the operand's msb. */
4104 static bool
4105 vspltis_constant (rtx op, unsigned step, unsigned copies)
4107 enum machine_mode mode = GET_MODE (op);
4108 enum machine_mode inner = GET_MODE_INNER (mode);
4110 unsigned i;
4111 unsigned nunits;
4112 unsigned bitsize;
4113 unsigned mask;
4115 HOST_WIDE_INT val;
4116 HOST_WIDE_INT splat_val;
4117 HOST_WIDE_INT msb_val;
4119 if (mode == V2DImode || mode == V2DFmode)
4120 return false;
4122 nunits = GET_MODE_NUNITS (mode);
4123 bitsize = GET_MODE_BITSIZE (inner);
4124 mask = GET_MODE_MASK (inner);
4126 val = const_vector_elt_as_int (op, nunits - 1);
4127 splat_val = val;
4128 msb_val = val > 0 ? 0 : -1;
4130 /* Construct the value to be splatted, if possible. If not, return 0. */
4131 for (i = 2; i <= copies; i *= 2)
4133 HOST_WIDE_INT small_val;
4134 bitsize /= 2;
4135 small_val = splat_val >> bitsize;
4136 mask >>= bitsize;
4137 if (splat_val != ((small_val << bitsize) | (small_val & mask)))
4138 return false;
4139 splat_val = small_val;
4142 /* Check if SPLAT_VAL can really be the operand of a vspltis[bhw]. */
4143 if (EASY_VECTOR_15 (splat_val))
4146 /* Also check if we can splat, and then add the result to itself. Do so if
4147 the value is positive, of if the splat instruction is using OP's mode;
4148 for splat_val < 0, the splat and the add should use the same mode. */
4149 else if (EASY_VECTOR_15_ADD_SELF (splat_val)
4150 && (splat_val >= 0 || (step == 1 && copies == 1)))
4153 /* Also check if are loading up the most significant bit which can be done by
4154 loading up -1 and shifting the value left by -1. */
4155 else if (EASY_VECTOR_MSB (splat_val, inner))
4158 else
4159 return false;
4161 /* Check if VAL is present in every STEP-th element, and the
4162 other elements are filled with its most significant bit. */
4163 for (i = 0; i < nunits - 1; ++i)
4165 HOST_WIDE_INT desired_val;
4166 if (((i + 1) & (step - 1)) == 0)
4167 desired_val = val;
4168 else
4169 desired_val = msb_val;
4171 if (desired_val != const_vector_elt_as_int (op, i))
4172 return false;
4175 return true;
4179 /* Return true if OP is of the given MODE and can be synthesized
4180 with a vspltisb, vspltish or vspltisw. */
4182 bool
4183 easy_altivec_constant (rtx op, enum machine_mode mode)
4185 unsigned step, copies;
4187 if (mode == VOIDmode)
4188 mode = GET_MODE (op);
4189 else if (mode != GET_MODE (op))
4190 return false;
4192 /* V2DI/V2DF was added with VSX. Only allow 0 and all 1's as easy
4193 constants. */
4194 if (mode == V2DFmode)
4195 return zero_constant (op, mode);
4197 if (mode == V2DImode)
4199 /* In case the compiler is built 32-bit, CONST_DOUBLE constants are not
4200 easy. */
4201 if (GET_CODE (CONST_VECTOR_ELT (op, 0)) != CONST_INT
4202 || GET_CODE (CONST_VECTOR_ELT (op, 1)) != CONST_INT)
4203 return false;
4205 if (zero_constant (op, mode))
4206 return true;
4208 if (INTVAL (CONST_VECTOR_ELT (op, 0)) == -1
4209 && INTVAL (CONST_VECTOR_ELT (op, 1)) == -1)
4210 return true;
4212 return false;
4215 /* Start with a vspltisw. */
4216 step = GET_MODE_NUNITS (mode) / 4;
4217 copies = 1;
4219 if (vspltis_constant (op, step, copies))
4220 return true;
4222 /* Then try with a vspltish. */
4223 if (step == 1)
4224 copies <<= 1;
4225 else
4226 step >>= 1;
4228 if (vspltis_constant (op, step, copies))
4229 return true;
4231 /* And finally a vspltisb. */
4232 if (step == 1)
4233 copies <<= 1;
4234 else
4235 step >>= 1;
4237 if (vspltis_constant (op, step, copies))
4238 return true;
4240 return false;
4243 /* Generate a VEC_DUPLICATE representing a vspltis[bhw] instruction whose
4244 result is OP. Abort if it is not possible. */
4247 gen_easy_altivec_constant (rtx op)
4249 enum machine_mode mode = GET_MODE (op);
4250 int nunits = GET_MODE_NUNITS (mode);
4251 rtx last = CONST_VECTOR_ELT (op, nunits - 1);
4252 unsigned step = nunits / 4;
4253 unsigned copies = 1;
4255 /* Start with a vspltisw. */
4256 if (vspltis_constant (op, step, copies))
4257 return gen_rtx_VEC_DUPLICATE (V4SImode, gen_lowpart (SImode, last));
4259 /* Then try with a vspltish. */
4260 if (step == 1)
4261 copies <<= 1;
4262 else
4263 step >>= 1;
4265 if (vspltis_constant (op, step, copies))
4266 return gen_rtx_VEC_DUPLICATE (V8HImode, gen_lowpart (HImode, last));
4268 /* And finally a vspltisb. */
4269 if (step == 1)
4270 copies <<= 1;
4271 else
4272 step >>= 1;
4274 if (vspltis_constant (op, step, copies))
4275 return gen_rtx_VEC_DUPLICATE (V16QImode, gen_lowpart (QImode, last));
4277 gcc_unreachable ();
4280 const char *
4281 output_vec_const_move (rtx *operands)
4283 int cst, cst2;
4284 enum machine_mode mode;
4285 rtx dest, vec;
4287 dest = operands[0];
4288 vec = operands[1];
4289 mode = GET_MODE (dest);
4291 if (TARGET_VSX)
4293 if (zero_constant (vec, mode))
4294 return "xxlxor %x0,%x0,%x0";
4296 if (mode == V2DImode
4297 && INTVAL (CONST_VECTOR_ELT (vec, 0)) == -1
4298 && INTVAL (CONST_VECTOR_ELT (vec, 1)) == -1)
4299 return "vspltisw %0,-1";
4302 if (TARGET_ALTIVEC)
4304 rtx splat_vec;
4305 if (zero_constant (vec, mode))
4306 return "vxor %0,%0,%0";
4308 splat_vec = gen_easy_altivec_constant (vec);
4309 gcc_assert (GET_CODE (splat_vec) == VEC_DUPLICATE);
4310 operands[1] = XEXP (splat_vec, 0);
4311 if (!EASY_VECTOR_15 (INTVAL (operands[1])))
4312 return "#";
4314 switch (GET_MODE (splat_vec))
4316 case V4SImode:
4317 return "vspltisw %0,%1";
4319 case V8HImode:
4320 return "vspltish %0,%1";
4322 case V16QImode:
4323 return "vspltisb %0,%1";
4325 default:
4326 gcc_unreachable ();
4330 gcc_assert (TARGET_SPE);
4332 /* Vector constant 0 is handled as a splitter of V2SI, and in the
4333 pattern of V1DI, V4HI, and V2SF.
4335 FIXME: We should probably return # and add post reload
4336 splitters for these, but this way is so easy ;-). */
4337 cst = INTVAL (CONST_VECTOR_ELT (vec, 0));
4338 cst2 = INTVAL (CONST_VECTOR_ELT (vec, 1));
4339 operands[1] = CONST_VECTOR_ELT (vec, 0);
4340 operands[2] = CONST_VECTOR_ELT (vec, 1);
4341 if (cst == cst2)
4342 return "li %0,%1\n\tevmergelo %0,%0,%0";
4343 else
4344 return "li %0,%1\n\tevmergelo %0,%0,%0\n\tli %0,%2";
4347 /* Initialize TARGET of vector PAIRED to VALS. */
4349 void
4350 paired_expand_vector_init (rtx target, rtx vals)
4352 enum machine_mode mode = GET_MODE (target);
4353 int n_elts = GET_MODE_NUNITS (mode);
4354 int n_var = 0;
4355 rtx x, new_rtx, tmp, constant_op, op1, op2;
4356 int i;
4358 for (i = 0; i < n_elts; ++i)
4360 x = XVECEXP (vals, 0, i);
4361 if (!(CONST_INT_P (x)
4362 || GET_CODE (x) == CONST_DOUBLE
4363 || GET_CODE (x) == CONST_FIXED))
4364 ++n_var;
4366 if (n_var == 0)
4368 /* Load from constant pool. */
4369 emit_move_insn (target, gen_rtx_CONST_VECTOR (mode, XVEC (vals, 0)));
4370 return;
4373 if (n_var == 2)
4375 /* The vector is initialized only with non-constants. */
4376 new_rtx = gen_rtx_VEC_CONCAT (V2SFmode, XVECEXP (vals, 0, 0),
4377 XVECEXP (vals, 0, 1));
4379 emit_move_insn (target, new_rtx);
4380 return;
4383 /* One field is non-constant and the other one is a constant. Load the
4384 constant from the constant pool and use ps_merge instruction to
4385 construct the whole vector. */
4386 op1 = XVECEXP (vals, 0, 0);
4387 op2 = XVECEXP (vals, 0, 1);
4389 constant_op = (CONSTANT_P (op1)) ? op1 : op2;
4391 tmp = gen_reg_rtx (GET_MODE (constant_op));
4392 emit_move_insn (tmp, constant_op);
4394 if (CONSTANT_P (op1))
4395 new_rtx = gen_rtx_VEC_CONCAT (V2SFmode, tmp, op2);
4396 else
4397 new_rtx = gen_rtx_VEC_CONCAT (V2SFmode, op1, tmp);
4399 emit_move_insn (target, new_rtx);
4402 void
4403 paired_expand_vector_move (rtx operands[])
4405 rtx op0 = operands[0], op1 = operands[1];
4407 emit_move_insn (op0, op1);
4410 /* Emit vector compare for code RCODE. DEST is destination, OP1 and
4411 OP2 are two VEC_COND_EXPR operands, CC_OP0 and CC_OP1 are the two
4412 operands for the relation operation COND. This is a recursive
4413 function. */
4415 static void
4416 paired_emit_vector_compare (enum rtx_code rcode,
4417 rtx dest, rtx op0, rtx op1,
4418 rtx cc_op0, rtx cc_op1)
4420 rtx tmp = gen_reg_rtx (V2SFmode);
4421 rtx tmp1, max, min;
4423 gcc_assert (TARGET_PAIRED_FLOAT);
4424 gcc_assert (GET_MODE (op0) == GET_MODE (op1));
4426 switch (rcode)
4428 case LT:
4429 case LTU:
4430 paired_emit_vector_compare (GE, dest, op1, op0, cc_op0, cc_op1);
4431 return;
4432 case GE:
4433 case GEU:
4434 emit_insn (gen_subv2sf3 (tmp, cc_op0, cc_op1));
4435 emit_insn (gen_selv2sf4 (dest, tmp, op0, op1, CONST0_RTX (SFmode)));
4436 return;
4437 case LE:
4438 case LEU:
4439 paired_emit_vector_compare (GE, dest, op0, op1, cc_op1, cc_op0);
4440 return;
4441 case GT:
4442 paired_emit_vector_compare (LE, dest, op1, op0, cc_op0, cc_op1);
4443 return;
4444 case EQ:
4445 tmp1 = gen_reg_rtx (V2SFmode);
4446 max = gen_reg_rtx (V2SFmode);
4447 min = gen_reg_rtx (V2SFmode);
4448 gen_reg_rtx (V2SFmode);
4450 emit_insn (gen_subv2sf3 (tmp, cc_op0, cc_op1));
4451 emit_insn (gen_selv2sf4
4452 (max, tmp, cc_op0, cc_op1, CONST0_RTX (SFmode)));
4453 emit_insn (gen_subv2sf3 (tmp, cc_op1, cc_op0));
4454 emit_insn (gen_selv2sf4
4455 (min, tmp, cc_op0, cc_op1, CONST0_RTX (SFmode)));
4456 emit_insn (gen_subv2sf3 (tmp1, min, max));
4457 emit_insn (gen_selv2sf4 (dest, tmp1, op0, op1, CONST0_RTX (SFmode)));
4458 return;
4459 case NE:
4460 paired_emit_vector_compare (EQ, dest, op1, op0, cc_op0, cc_op1);
4461 return;
4462 case UNLE:
4463 paired_emit_vector_compare (LE, dest, op1, op0, cc_op0, cc_op1);
4464 return;
4465 case UNLT:
4466 paired_emit_vector_compare (LT, dest, op1, op0, cc_op0, cc_op1);
4467 return;
4468 case UNGE:
4469 paired_emit_vector_compare (GE, dest, op1, op0, cc_op0, cc_op1);
4470 return;
4471 case UNGT:
4472 paired_emit_vector_compare (GT, dest, op1, op0, cc_op0, cc_op1);
4473 return;
4474 default:
4475 gcc_unreachable ();
4478 return;
4481 /* Emit vector conditional expression.
4482 DEST is destination. OP1 and OP2 are two VEC_COND_EXPR operands.
4483 CC_OP0 and CC_OP1 are the two operands for the relation operation COND. */
4486 paired_emit_vector_cond_expr (rtx dest, rtx op1, rtx op2,
4487 rtx cond, rtx cc_op0, rtx cc_op1)
4489 enum rtx_code rcode = GET_CODE (cond);
4491 if (!TARGET_PAIRED_FLOAT)
4492 return 0;
4494 paired_emit_vector_compare (rcode, dest, op1, op2, cc_op0, cc_op1);
4496 return 1;
4499 /* Initialize vector TARGET to VALS. */
4501 void
4502 rs6000_expand_vector_init (rtx target, rtx vals)
4504 enum machine_mode mode = GET_MODE (target);
4505 enum machine_mode inner_mode = GET_MODE_INNER (mode);
4506 int n_elts = GET_MODE_NUNITS (mode);
4507 int n_var = 0, one_var = -1;
4508 bool all_same = true, all_const_zero = true;
4509 rtx x, mem;
4510 int i;
4512 for (i = 0; i < n_elts; ++i)
4514 x = XVECEXP (vals, 0, i);
4515 if (!(CONST_INT_P (x)
4516 || GET_CODE (x) == CONST_DOUBLE
4517 || GET_CODE (x) == CONST_FIXED))
4518 ++n_var, one_var = i;
4519 else if (x != CONST0_RTX (inner_mode))
4520 all_const_zero = false;
4522 if (i > 0 && !rtx_equal_p (x, XVECEXP (vals, 0, 0)))
4523 all_same = false;
4526 if (n_var == 0)
4528 rtx const_vec = gen_rtx_CONST_VECTOR (mode, XVEC (vals, 0));
4529 bool int_vector_p = (GET_MODE_CLASS (mode) == MODE_VECTOR_INT);
4530 if ((int_vector_p || TARGET_VSX) && all_const_zero)
4532 /* Zero register. */
4533 emit_insn (gen_rtx_SET (VOIDmode, target,
4534 gen_rtx_XOR (mode, target, target)));
4535 return;
4537 else if (int_vector_p && easy_vector_constant (const_vec, mode))
4539 /* Splat immediate. */
4540 emit_insn (gen_rtx_SET (VOIDmode, target, const_vec));
4541 return;
4543 else
4545 /* Load from constant pool. */
4546 emit_move_insn (target, const_vec);
4547 return;
4551 /* Double word values on VSX can use xxpermdi or lxvdsx. */
4552 if (VECTOR_MEM_VSX_P (mode) && (mode == V2DFmode || mode == V2DImode))
4554 rtx op0 = XVECEXP (vals, 0, 0);
4555 rtx op1 = XVECEXP (vals, 0, 1);
4556 if (all_same)
4558 if (!MEM_P (op0) && !REG_P (op0))
4559 op0 = force_reg (inner_mode, op0);
4560 if (mode == V2DFmode)
4561 emit_insn (gen_vsx_splat_v2df (target, op0));
4562 else
4563 emit_insn (gen_vsx_splat_v2di (target, op0));
4565 else
4567 op0 = force_reg (inner_mode, op0);
4568 op1 = force_reg (inner_mode, op1);
4569 if (mode == V2DFmode)
4570 emit_insn (gen_vsx_concat_v2df (target, op0, op1));
4571 else
4572 emit_insn (gen_vsx_concat_v2di (target, op0, op1));
4574 return;
4577 /* With single precision floating point on VSX, know that internally single
4578 precision is actually represented as a double, and either make 2 V2DF
4579 vectors, and convert these vectors to single precision, or do one
4580 conversion, and splat the result to the other elements. */
4581 if (mode == V4SFmode && VECTOR_MEM_VSX_P (mode))
4583 if (all_same)
4585 rtx freg = gen_reg_rtx (V4SFmode);
4586 rtx sreg = force_reg (SFmode, XVECEXP (vals, 0, 0));
4588 emit_insn (gen_vsx_xscvdpsp_scalar (freg, sreg));
4589 emit_insn (gen_vsx_xxspltw_v4sf (target, freg, const0_rtx));
4591 else
4593 rtx dbl_even = gen_reg_rtx (V2DFmode);
4594 rtx dbl_odd = gen_reg_rtx (V2DFmode);
4595 rtx flt_even = gen_reg_rtx (V4SFmode);
4596 rtx flt_odd = gen_reg_rtx (V4SFmode);
4597 rtx op0 = force_reg (SFmode, XVECEXP (vals, 0, 0));
4598 rtx op1 = force_reg (SFmode, XVECEXP (vals, 0, 1));
4599 rtx op2 = force_reg (SFmode, XVECEXP (vals, 0, 2));
4600 rtx op3 = force_reg (SFmode, XVECEXP (vals, 0, 3));
4602 emit_insn (gen_vsx_concat_v2sf (dbl_even, op0, op1));
4603 emit_insn (gen_vsx_concat_v2sf (dbl_odd, op2, op3));
4604 emit_insn (gen_vsx_xvcvdpsp (flt_even, dbl_even));
4605 emit_insn (gen_vsx_xvcvdpsp (flt_odd, dbl_odd));
4606 rs6000_expand_extract_even (target, flt_even, flt_odd);
4608 return;
4611 /* Store value to stack temp. Load vector element. Splat. However, splat
4612 of 64-bit items is not supported on Altivec. */
4613 if (all_same && GET_MODE_SIZE (inner_mode) <= 4)
4615 mem = assign_stack_temp (mode, GET_MODE_SIZE (inner_mode));
4616 emit_move_insn (adjust_address_nv (mem, inner_mode, 0),
4617 XVECEXP (vals, 0, 0));
4618 x = gen_rtx_UNSPEC (VOIDmode,
4619 gen_rtvec (1, const0_rtx), UNSPEC_LVE);
4620 emit_insn (gen_rtx_PARALLEL (VOIDmode,
4621 gen_rtvec (2,
4622 gen_rtx_SET (VOIDmode,
4623 target, mem),
4624 x)));
4625 x = gen_rtx_VEC_SELECT (inner_mode, target,
4626 gen_rtx_PARALLEL (VOIDmode,
4627 gen_rtvec (1, const0_rtx)));
4628 emit_insn (gen_rtx_SET (VOIDmode, target,
4629 gen_rtx_VEC_DUPLICATE (mode, x)));
4630 return;
4633 /* One field is non-constant. Load constant then overwrite
4634 varying field. */
4635 if (n_var == 1)
4637 rtx copy = copy_rtx (vals);
4639 /* Load constant part of vector, substitute neighboring value for
4640 varying element. */
4641 XVECEXP (copy, 0, one_var) = XVECEXP (vals, 0, (one_var + 1) % n_elts);
4642 rs6000_expand_vector_init (target, copy);
4644 /* Insert variable. */
4645 rs6000_expand_vector_set (target, XVECEXP (vals, 0, one_var), one_var);
4646 return;
4649 /* Construct the vector in memory one field at a time
4650 and load the whole vector. */
4651 mem = assign_stack_temp (mode, GET_MODE_SIZE (mode));
4652 for (i = 0; i < n_elts; i++)
4653 emit_move_insn (adjust_address_nv (mem, inner_mode,
4654 i * GET_MODE_SIZE (inner_mode)),
4655 XVECEXP (vals, 0, i));
4656 emit_move_insn (target, mem);
4659 /* Set field ELT of TARGET to VAL. */
4661 void
4662 rs6000_expand_vector_set (rtx target, rtx val, int elt)
4664 enum machine_mode mode = GET_MODE (target);
4665 enum machine_mode inner_mode = GET_MODE_INNER (mode);
4666 rtx reg = gen_reg_rtx (mode);
4667 rtx mask, mem, x;
4668 int width = GET_MODE_SIZE (inner_mode);
4669 int i;
4671 if (VECTOR_MEM_VSX_P (mode) && (mode == V2DFmode || mode == V2DImode))
4673 rtx (*set_func) (rtx, rtx, rtx, rtx)
4674 = ((mode == V2DFmode) ? gen_vsx_set_v2df : gen_vsx_set_v2di);
4675 emit_insn (set_func (target, target, val, GEN_INT (elt)));
4676 return;
4679 /* Load single variable value. */
4680 mem = assign_stack_temp (mode, GET_MODE_SIZE (inner_mode));
4681 emit_move_insn (adjust_address_nv (mem, inner_mode, 0), val);
4682 x = gen_rtx_UNSPEC (VOIDmode,
4683 gen_rtvec (1, const0_rtx), UNSPEC_LVE);
4684 emit_insn (gen_rtx_PARALLEL (VOIDmode,
4685 gen_rtvec (2,
4686 gen_rtx_SET (VOIDmode,
4687 reg, mem),
4688 x)));
4690 /* Linear sequence. */
4691 mask = gen_rtx_PARALLEL (V16QImode, rtvec_alloc (16));
4692 for (i = 0; i < 16; ++i)
4693 XVECEXP (mask, 0, i) = GEN_INT (i);
4695 /* Set permute mask to insert element into target. */
4696 for (i = 0; i < width; ++i)
4697 XVECEXP (mask, 0, elt*width + i)
4698 = GEN_INT (i + 0x10);
4699 x = gen_rtx_CONST_VECTOR (V16QImode, XVEC (mask, 0));
4700 x = gen_rtx_UNSPEC (mode,
4701 gen_rtvec (3, target, reg,
4702 force_reg (V16QImode, x)),
4703 UNSPEC_VPERM);
4704 emit_insn (gen_rtx_SET (VOIDmode, target, x));
4707 /* Extract field ELT from VEC into TARGET. */
4709 void
4710 rs6000_expand_vector_extract (rtx target, rtx vec, int elt)
4712 enum machine_mode mode = GET_MODE (vec);
4713 enum machine_mode inner_mode = GET_MODE_INNER (mode);
4714 rtx mem;
4716 if (VECTOR_MEM_VSX_P (mode))
4718 switch (mode)
4720 default:
4721 break;
4722 case V2DFmode:
4723 emit_insn (gen_vsx_extract_v2df (target, vec, GEN_INT (elt)));
4724 return;
4725 case V2DImode:
4726 emit_insn (gen_vsx_extract_v2di (target, vec, GEN_INT (elt)));
4727 return;
4728 case V4SFmode:
4729 emit_insn (gen_vsx_extract_v4sf (target, vec, GEN_INT (elt)));
4730 return;
4734 /* Allocate mode-sized buffer. */
4735 mem = assign_stack_temp (mode, GET_MODE_SIZE (mode));
4737 emit_move_insn (mem, vec);
4739 /* Add offset to field within buffer matching vector element. */
4740 mem = adjust_address_nv (mem, inner_mode, elt * GET_MODE_SIZE (inner_mode));
4742 emit_move_insn (target, adjust_address_nv (mem, inner_mode, 0));
4745 /* Generates shifts and masks for a pair of rldicl or rldicr insns to
4746 implement ANDing by the mask IN. */
4747 void
4748 build_mask64_2_operands (rtx in, rtx *out)
4750 #if HOST_BITS_PER_WIDE_INT >= 64
4751 unsigned HOST_WIDE_INT c, lsb, m1, m2;
4752 int shift;
4754 gcc_assert (GET_CODE (in) == CONST_INT);
4756 c = INTVAL (in);
4757 if (c & 1)
4759 /* Assume c initially something like 0x00fff000000fffff. The idea
4760 is to rotate the word so that the middle ^^^^^^ group of zeros
4761 is at the MS end and can be cleared with an rldicl mask. We then
4762 rotate back and clear off the MS ^^ group of zeros with a
4763 second rldicl. */
4764 c = ~c; /* c == 0xff000ffffff00000 */
4765 lsb = c & -c; /* lsb == 0x0000000000100000 */
4766 m1 = -lsb; /* m1 == 0xfffffffffff00000 */
4767 c = ~c; /* c == 0x00fff000000fffff */
4768 c &= -lsb; /* c == 0x00fff00000000000 */
4769 lsb = c & -c; /* lsb == 0x0000100000000000 */
4770 c = ~c; /* c == 0xff000fffffffffff */
4771 c &= -lsb; /* c == 0xff00000000000000 */
4772 shift = 0;
4773 while ((lsb >>= 1) != 0)
4774 shift++; /* shift == 44 on exit from loop */
4775 m1 <<= 64 - shift; /* m1 == 0xffffff0000000000 */
4776 m1 = ~m1; /* m1 == 0x000000ffffffffff */
4777 m2 = ~c; /* m2 == 0x00ffffffffffffff */
4779 else
4781 /* Assume c initially something like 0xff000f0000000000. The idea
4782 is to rotate the word so that the ^^^ middle group of zeros
4783 is at the LS end and can be cleared with an rldicr mask. We then
4784 rotate back and clear off the LS group of ^^^^^^^^^^ zeros with
4785 a second rldicr. */
4786 lsb = c & -c; /* lsb == 0x0000010000000000 */
4787 m2 = -lsb; /* m2 == 0xffffff0000000000 */
4788 c = ~c; /* c == 0x00fff0ffffffffff */
4789 c &= -lsb; /* c == 0x00fff00000000000 */
4790 lsb = c & -c; /* lsb == 0x0000100000000000 */
4791 c = ~c; /* c == 0xff000fffffffffff */
4792 c &= -lsb; /* c == 0xff00000000000000 */
4793 shift = 0;
4794 while ((lsb >>= 1) != 0)
4795 shift++; /* shift == 44 on exit from loop */
4796 m1 = ~c; /* m1 == 0x00ffffffffffffff */
4797 m1 >>= shift; /* m1 == 0x0000000000000fff */
4798 m1 = ~m1; /* m1 == 0xfffffffffffff000 */
4801 /* Note that when we only have two 0->1 and 1->0 transitions, one of the
4802 masks will be all 1's. We are guaranteed more than one transition. */
4803 out[0] = GEN_INT (64 - shift);
4804 out[1] = GEN_INT (m1);
4805 out[2] = GEN_INT (shift);
4806 out[3] = GEN_INT (m2);
4807 #else
4808 (void)in;
4809 (void)out;
4810 gcc_unreachable ();
4811 #endif
4814 /* Return TRUE if OP is an invalid SUBREG operation on the e500. */
4816 bool
4817 invalid_e500_subreg (rtx op, enum machine_mode mode)
4819 if (TARGET_E500_DOUBLE)
4821 /* Reject (subreg:SI (reg:DF)); likewise with subreg:DI or
4822 subreg:TI and reg:TF. Decimal float modes are like integer
4823 modes (only low part of each register used) for this
4824 purpose. */
4825 if (GET_CODE (op) == SUBREG
4826 && (mode == SImode || mode == DImode || mode == TImode
4827 || mode == DDmode || mode == TDmode)
4828 && REG_P (SUBREG_REG (op))
4829 && (GET_MODE (SUBREG_REG (op)) == DFmode
4830 || GET_MODE (SUBREG_REG (op)) == TFmode))
4831 return true;
4833 /* Reject (subreg:DF (reg:DI)); likewise with subreg:TF and
4834 reg:TI. */
4835 if (GET_CODE (op) == SUBREG
4836 && (mode == DFmode || mode == TFmode)
4837 && REG_P (SUBREG_REG (op))
4838 && (GET_MODE (SUBREG_REG (op)) == DImode
4839 || GET_MODE (SUBREG_REG (op)) == TImode
4840 || GET_MODE (SUBREG_REG (op)) == DDmode
4841 || GET_MODE (SUBREG_REG (op)) == TDmode))
4842 return true;
4845 if (TARGET_SPE
4846 && GET_CODE (op) == SUBREG
4847 && mode == SImode
4848 && REG_P (SUBREG_REG (op))
4849 && SPE_VECTOR_MODE (GET_MODE (SUBREG_REG (op))))
4850 return true;
4852 return false;
4855 /* AIX increases natural record alignment to doubleword if the first
4856 field is an FP double while the FP fields remain word aligned. */
4858 unsigned int
4859 rs6000_special_round_type_align (tree type, unsigned int computed,
4860 unsigned int specified)
4862 unsigned int align = MAX (computed, specified);
4863 tree field = TYPE_FIELDS (type);
4865 /* Skip all non field decls */
4866 while (field != NULL && TREE_CODE (field) != FIELD_DECL)
4867 field = DECL_CHAIN (field);
4869 if (field != NULL && field != type)
4871 type = TREE_TYPE (field);
4872 while (TREE_CODE (type) == ARRAY_TYPE)
4873 type = TREE_TYPE (type);
4875 if (type != error_mark_node && TYPE_MODE (type) == DFmode)
4876 align = MAX (align, 64);
4879 return align;
4882 /* Darwin increases record alignment to the natural alignment of
4883 the first field. */
4885 unsigned int
4886 darwin_rs6000_special_round_type_align (tree type, unsigned int computed,
4887 unsigned int specified)
4889 unsigned int align = MAX (computed, specified);
4891 if (TYPE_PACKED (type))
4892 return align;
4894 /* Find the first field, looking down into aggregates. */
4895 do {
4896 tree field = TYPE_FIELDS (type);
4897 /* Skip all non field decls */
4898 while (field != NULL && TREE_CODE (field) != FIELD_DECL)
4899 field = DECL_CHAIN (field);
4900 if (! field)
4901 break;
4902 /* A packed field does not contribute any extra alignment. */
4903 if (DECL_PACKED (field))
4904 return align;
4905 type = TREE_TYPE (field);
4906 while (TREE_CODE (type) == ARRAY_TYPE)
4907 type = TREE_TYPE (type);
4908 } while (AGGREGATE_TYPE_P (type));
4910 if (! AGGREGATE_TYPE_P (type) && type != error_mark_node)
4911 align = MAX (align, TYPE_ALIGN (type));
4913 return align;
4916 /* Return 1 for an operand in small memory on V.4/eabi. */
4919 small_data_operand (rtx op ATTRIBUTE_UNUSED,
4920 enum machine_mode mode ATTRIBUTE_UNUSED)
4922 #if TARGET_ELF
4923 rtx sym_ref;
4925 if (rs6000_sdata == SDATA_NONE || rs6000_sdata == SDATA_DATA)
4926 return 0;
4928 if (DEFAULT_ABI != ABI_V4)
4929 return 0;
4931 /* Vector and float memory instructions have a limited offset on the
4932 SPE, so using a vector or float variable directly as an operand is
4933 not useful. */
4934 if (TARGET_SPE
4935 && (SPE_VECTOR_MODE (mode) || FLOAT_MODE_P (mode)))
4936 return 0;
4938 if (GET_CODE (op) == SYMBOL_REF)
4939 sym_ref = op;
4941 else if (GET_CODE (op) != CONST
4942 || GET_CODE (XEXP (op, 0)) != PLUS
4943 || GET_CODE (XEXP (XEXP (op, 0), 0)) != SYMBOL_REF
4944 || GET_CODE (XEXP (XEXP (op, 0), 1)) != CONST_INT)
4945 return 0;
4947 else
4949 rtx sum = XEXP (op, 0);
4950 HOST_WIDE_INT summand;
4952 /* We have to be careful here, because it is the referenced address
4953 that must be 32k from _SDA_BASE_, not just the symbol. */
4954 summand = INTVAL (XEXP (sum, 1));
4955 if (summand < 0 || summand > g_switch_value)
4956 return 0;
4958 sym_ref = XEXP (sum, 0);
4961 return SYMBOL_REF_SMALL_P (sym_ref);
4962 #else
4963 return 0;
4964 #endif
4967 /* Return true if either operand is a general purpose register. */
4969 bool
4970 gpr_or_gpr_p (rtx op0, rtx op1)
4972 return ((REG_P (op0) && INT_REGNO_P (REGNO (op0)))
4973 || (REG_P (op1) && INT_REGNO_P (REGNO (op1))));
4976 /* Given an address, return a constant offset term if one exists. */
4978 static rtx
4979 address_offset (rtx op)
4981 if (GET_CODE (op) == PRE_INC
4982 || GET_CODE (op) == PRE_DEC)
4983 op = XEXP (op, 0);
4984 else if (GET_CODE (op) == PRE_MODIFY
4985 || GET_CODE (op) == LO_SUM)
4986 op = XEXP (op, 1);
4988 if (GET_CODE (op) == CONST)
4989 op = XEXP (op, 0);
4991 if (GET_CODE (op) == PLUS)
4992 op = XEXP (op, 1);
4994 if (CONST_INT_P (op))
4995 return op;
4997 return NULL_RTX;
5000 /* Return true if the MEM operand is a memory operand suitable for use
5001 with a (full width, possibly multiple) gpr load/store. On
5002 powerpc64 this means the offset must be divisible by 4.
5003 Implements 'Y' constraint.
5005 Accept direct, indexed, offset, lo_sum and tocref. Since this is
5006 a constraint function we know the operand has satisfied a suitable
5007 memory predicate. Also accept some odd rtl generated by reload
5008 (see rs6000_legitimize_reload_address for various forms). It is
5009 important that reload rtl be accepted by appropriate constraints
5010 but not by the operand predicate.
5012 Offsetting a lo_sum should not be allowed, except where we know by
5013 alignment that a 32k boundary is not crossed, but see the ???
5014 comment in rs6000_legitimize_reload_address. */
5016 bool
5017 mem_operand_gpr (rtx op, enum machine_mode mode)
5019 unsigned HOST_WIDE_INT offset;
5020 int extra;
5022 op = address_offset (XEXP (op, 0));
5023 if (op == NULL_RTX)
5024 return true;
5026 offset = INTVAL (op);
5027 extra = GET_MODE_SIZE (mode) - UNITS_PER_WORD;
5028 if (extra < 0)
5029 extra = 0;
5030 else if (TARGET_POWERPC64 && (offset & 3) != 0)
5031 return false;
5032 return offset + 0x8000 < 0x10000u - extra;
5035 /* Subroutines of rs6000_legitimize_address and rs6000_legitimate_address_p. */
5037 static bool
5038 reg_offset_addressing_ok_p (enum machine_mode mode)
5040 switch (mode)
5042 case V16QImode:
5043 case V8HImode:
5044 case V4SFmode:
5045 case V4SImode:
5046 case V2DFmode:
5047 case V2DImode:
5048 /* AltiVec/VSX vector modes. Only reg+reg addressing is valid. */
5049 if (VECTOR_MEM_ALTIVEC_OR_VSX_P (mode))
5050 return false;
5051 break;
5053 case V4HImode:
5054 case V2SImode:
5055 case V1DImode:
5056 case V2SFmode:
5057 /* Paired vector modes. Only reg+reg addressing is valid. */
5058 if (TARGET_PAIRED_FLOAT)
5059 return false;
5060 break;
5062 default:
5063 break;
5066 return true;
5069 static bool
5070 virtual_stack_registers_memory_p (rtx op)
5072 int regnum;
5074 if (GET_CODE (op) == REG)
5075 regnum = REGNO (op);
5077 else if (GET_CODE (op) == PLUS
5078 && GET_CODE (XEXP (op, 0)) == REG
5079 && GET_CODE (XEXP (op, 1)) == CONST_INT)
5080 regnum = REGNO (XEXP (op, 0));
5082 else
5083 return false;
5085 return (regnum >= FIRST_VIRTUAL_REGISTER
5086 && regnum <= LAST_VIRTUAL_POINTER_REGISTER);
5089 /* Return true if memory accesses to OP are known to never straddle
5090 a 32k boundary. */
5092 static bool
5093 offsettable_ok_by_alignment (rtx op, HOST_WIDE_INT offset,
5094 enum machine_mode mode)
5096 tree decl, type;
5097 unsigned HOST_WIDE_INT dsize, dalign;
5099 if (GET_CODE (op) != SYMBOL_REF)
5100 return false;
5102 decl = SYMBOL_REF_DECL (op);
5103 if (!decl)
5105 if (GET_MODE_SIZE (mode) == 0)
5106 return false;
5108 /* -fsection-anchors loses the original SYMBOL_REF_DECL when
5109 replacing memory addresses with an anchor plus offset. We
5110 could find the decl by rummaging around in the block->objects
5111 VEC for the given offset but that seems like too much work. */
5112 dalign = 1;
5113 if (SYMBOL_REF_HAS_BLOCK_INFO_P (op)
5114 && SYMBOL_REF_ANCHOR_P (op)
5115 && SYMBOL_REF_BLOCK (op) != NULL)
5117 struct object_block *block = SYMBOL_REF_BLOCK (op);
5118 HOST_WIDE_INT lsb, mask;
5120 /* Given the alignment of the block.. */
5121 dalign = block->alignment;
5122 mask = dalign / BITS_PER_UNIT - 1;
5124 /* ..and the combined offset of the anchor and any offset
5125 to this block object.. */
5126 offset += SYMBOL_REF_BLOCK_OFFSET (op);
5127 lsb = offset & -offset;
5129 /* ..find how many bits of the alignment we know for the
5130 object. */
5131 mask &= lsb - 1;
5132 dalign = mask + 1;
5134 return dalign >= GET_MODE_SIZE (mode);
5137 if (DECL_P (decl))
5139 if (TREE_CODE (decl) == FUNCTION_DECL)
5140 return true;
5142 if (!DECL_SIZE_UNIT (decl))
5143 return false;
5145 if (!host_integerp (DECL_SIZE_UNIT (decl), 1))
5146 return false;
5148 dsize = tree_low_cst (DECL_SIZE_UNIT (decl), 1);
5149 if (dsize > 32768)
5150 return false;
5152 dalign = DECL_ALIGN_UNIT (decl);
5153 return dalign >= dsize;
5156 type = TREE_TYPE (decl);
5158 if (TREE_CODE (decl) == STRING_CST)
5159 dsize = TREE_STRING_LENGTH (decl);
5160 else if (TYPE_SIZE_UNIT (type)
5161 && host_integerp (TYPE_SIZE_UNIT (type), 1))
5162 dsize = tree_low_cst (TYPE_SIZE_UNIT (type), 1);
5163 else
5164 return false;
5165 if (dsize > 32768)
5166 return false;
5168 dalign = TYPE_ALIGN (type);
5169 if (CONSTANT_CLASS_P (decl))
5170 dalign = CONSTANT_ALIGNMENT (decl, dalign);
5171 else
5172 dalign = DATA_ALIGNMENT (decl, dalign);
5173 dalign /= BITS_PER_UNIT;
5174 return dalign >= dsize;
5177 static bool
5178 constant_pool_expr_p (rtx op)
5180 rtx base, offset;
5182 split_const (op, &base, &offset);
5183 return (GET_CODE (base) == SYMBOL_REF
5184 && CONSTANT_POOL_ADDRESS_P (base)
5185 && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (base), Pmode));
5188 static const_rtx tocrel_base, tocrel_offset;
5190 /* Return true if OP is a toc pointer relative address (the output
5191 of create_TOC_reference). If STRICT, do not match high part or
5192 non-split -mcmodel=large/medium toc pointer relative addresses. */
5194 bool
5195 toc_relative_expr_p (const_rtx op, bool strict)
5197 if (!TARGET_TOC)
5198 return false;
5200 if (TARGET_CMODEL != CMODEL_SMALL)
5202 /* Only match the low part. */
5203 if (GET_CODE (op) == LO_SUM
5204 && REG_P (XEXP (op, 0))
5205 && INT_REG_OK_FOR_BASE_P (XEXP (op, 0), strict))
5206 op = XEXP (op, 1);
5207 else if (strict)
5208 return false;
5211 tocrel_base = op;
5212 tocrel_offset = const0_rtx;
5213 if (GET_CODE (op) == PLUS && CONST_INT_P (XEXP (op, 1)))
5215 tocrel_base = XEXP (op, 0);
5216 tocrel_offset = XEXP (op, 1);
5219 return (GET_CODE (tocrel_base) == UNSPEC
5220 && XINT (tocrel_base, 1) == UNSPEC_TOCREL);
5223 /* Return true if X is a constant pool address, and also for cmodel=medium
5224 if X is a toc-relative address known to be offsettable within MODE. */
5226 bool
5227 legitimate_constant_pool_address_p (const_rtx x, enum machine_mode mode,
5228 bool strict)
5230 return (toc_relative_expr_p (x, strict)
5231 && (TARGET_CMODEL != CMODEL_MEDIUM
5232 || constant_pool_expr_p (XVECEXP (tocrel_base, 0, 0))
5233 || mode == QImode
5234 || offsettable_ok_by_alignment (XVECEXP (tocrel_base, 0, 0),
5235 INTVAL (tocrel_offset), mode)));
5238 static bool
5239 legitimate_small_data_p (enum machine_mode mode, rtx x)
5241 return (DEFAULT_ABI == ABI_V4
5242 && !flag_pic && !TARGET_TOC
5243 && (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == CONST)
5244 && small_data_operand (x, mode));
5247 /* SPE offset addressing is limited to 5-bits worth of double words. */
5248 #define SPE_CONST_OFFSET_OK(x) (((x) & ~0xf8) == 0)
5250 bool
5251 rs6000_legitimate_offset_address_p (enum machine_mode mode, rtx x,
5252 bool strict, bool worst_case)
5254 unsigned HOST_WIDE_INT offset;
5255 unsigned int extra;
5257 if (GET_CODE (x) != PLUS)
5258 return false;
5259 if (!REG_P (XEXP (x, 0)))
5260 return false;
5261 if (!INT_REG_OK_FOR_BASE_P (XEXP (x, 0), strict))
5262 return false;
5263 if (!reg_offset_addressing_ok_p (mode))
5264 return virtual_stack_registers_memory_p (x);
5265 if (legitimate_constant_pool_address_p (x, mode, strict))
5266 return true;
5267 if (GET_CODE (XEXP (x, 1)) != CONST_INT)
5268 return false;
5270 offset = INTVAL (XEXP (x, 1));
5271 extra = 0;
5272 switch (mode)
5274 case V4HImode:
5275 case V2SImode:
5276 case V1DImode:
5277 case V2SFmode:
5278 /* SPE vector modes. */
5279 return SPE_CONST_OFFSET_OK (offset);
5281 case DFmode:
5282 case DDmode:
5283 case DImode:
5284 /* On e500v2, we may have:
5286 (subreg:DF (mem:DI (plus (reg) (const_int))) 0).
5288 Which gets addressed with evldd instructions. */
5289 if (TARGET_E500_DOUBLE)
5290 return SPE_CONST_OFFSET_OK (offset);
5292 /* If we are using VSX scalar loads, restrict ourselves to reg+reg
5293 addressing. */
5294 if (mode == DFmode && VECTOR_MEM_VSX_P (DFmode))
5295 return false;
5297 if (!worst_case)
5298 break;
5299 if (!TARGET_POWERPC64)
5300 extra = 4;
5301 else if (offset & 3)
5302 return false;
5303 break;
5305 case TFmode:
5306 case TDmode:
5307 case TImode:
5308 if (TARGET_E500_DOUBLE)
5309 return (SPE_CONST_OFFSET_OK (offset)
5310 && SPE_CONST_OFFSET_OK (offset + 8));
5312 extra = 8;
5313 if (!worst_case)
5314 break;
5315 if (!TARGET_POWERPC64)
5316 extra = 12;
5317 else if (offset & 3)
5318 return false;
5319 break;
5321 default:
5322 break;
5325 offset += 0x8000;
5326 return offset < 0x10000 - extra;
5329 bool
5330 legitimate_indexed_address_p (rtx x, int strict)
5332 rtx op0, op1;
5334 if (GET_CODE (x) != PLUS)
5335 return false;
5337 op0 = XEXP (x, 0);
5338 op1 = XEXP (x, 1);
5340 /* Recognize the rtl generated by reload which we know will later be
5341 replaced with proper base and index regs. */
5342 if (!strict
5343 && reload_in_progress
5344 && (REG_P (op0) || GET_CODE (op0) == PLUS)
5345 && REG_P (op1))
5346 return true;
5348 return (REG_P (op0) && REG_P (op1)
5349 && ((INT_REG_OK_FOR_BASE_P (op0, strict)
5350 && INT_REG_OK_FOR_INDEX_P (op1, strict))
5351 || (INT_REG_OK_FOR_BASE_P (op1, strict)
5352 && INT_REG_OK_FOR_INDEX_P (op0, strict))));
5355 bool
5356 avoiding_indexed_address_p (enum machine_mode mode)
5358 /* Avoid indexed addressing for modes that have non-indexed
5359 load/store instruction forms. */
5360 return (TARGET_AVOID_XFORM && VECTOR_MEM_NONE_P (mode));
5363 inline bool
5364 legitimate_indirect_address_p (rtx x, int strict)
5366 return GET_CODE (x) == REG && INT_REG_OK_FOR_BASE_P (x, strict);
5369 bool
5370 macho_lo_sum_memory_operand (rtx x, enum machine_mode mode)
5372 if (!TARGET_MACHO || !flag_pic
5373 || mode != SImode || GET_CODE (x) != MEM)
5374 return false;
5375 x = XEXP (x, 0);
5377 if (GET_CODE (x) != LO_SUM)
5378 return false;
5379 if (GET_CODE (XEXP (x, 0)) != REG)
5380 return false;
5381 if (!INT_REG_OK_FOR_BASE_P (XEXP (x, 0), 0))
5382 return false;
5383 x = XEXP (x, 1);
5385 return CONSTANT_P (x);
5388 static bool
5389 legitimate_lo_sum_address_p (enum machine_mode mode, rtx x, int strict)
5391 if (GET_CODE (x) != LO_SUM)
5392 return false;
5393 if (GET_CODE (XEXP (x, 0)) != REG)
5394 return false;
5395 if (!INT_REG_OK_FOR_BASE_P (XEXP (x, 0), strict))
5396 return false;
5397 /* Restrict addressing for DI because of our SUBREG hackery. */
5398 if (TARGET_E500_DOUBLE && GET_MODE_SIZE (mode) > UNITS_PER_WORD)
5399 return false;
5400 x = XEXP (x, 1);
5402 if (TARGET_ELF || TARGET_MACHO)
5404 if (DEFAULT_ABI != ABI_AIX && DEFAULT_ABI != ABI_DARWIN && flag_pic)
5405 return false;
5406 if (TARGET_TOC)
5407 return false;
5408 if (GET_MODE_NUNITS (mode) != 1)
5409 return false;
5410 if (GET_MODE_SIZE (mode) > UNITS_PER_WORD
5411 && !(/* ??? Assume floating point reg based on mode? */
5412 TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT
5413 && (mode == DFmode || mode == DDmode)))
5414 return false;
5416 return CONSTANT_P (x);
5419 return false;
5423 /* Try machine-dependent ways of modifying an illegitimate address
5424 to be legitimate. If we find one, return the new, valid address.
5425 This is used from only one place: `memory_address' in explow.c.
5427 OLDX is the address as it was before break_out_memory_refs was
5428 called. In some cases it is useful to look at this to decide what
5429 needs to be done.
5431 It is always safe for this function to do nothing. It exists to
5432 recognize opportunities to optimize the output.
5434 On RS/6000, first check for the sum of a register with a constant
5435 integer that is out of range. If so, generate code to add the
5436 constant with the low-order 16 bits masked to the register and force
5437 this result into another register (this can be done with `cau').
5438 Then generate an address of REG+(CONST&0xffff), allowing for the
5439 possibility of bit 16 being a one.
5441 Then check for the sum of a register and something not constant, try to
5442 load the other things into a register and return the sum. */
5444 static rtx
5445 rs6000_legitimize_address (rtx x, rtx oldx ATTRIBUTE_UNUSED,
5446 enum machine_mode mode)
5448 unsigned int extra;
5450 if (!reg_offset_addressing_ok_p (mode))
5452 if (virtual_stack_registers_memory_p (x))
5453 return x;
5455 /* In theory we should not be seeing addresses of the form reg+0,
5456 but just in case it is generated, optimize it away. */
5457 if (GET_CODE (x) == PLUS && XEXP (x, 1) == const0_rtx)
5458 return force_reg (Pmode, XEXP (x, 0));
5460 /* Make sure both operands are registers. */
5461 else if (GET_CODE (x) == PLUS)
5462 return gen_rtx_PLUS (Pmode,
5463 force_reg (Pmode, XEXP (x, 0)),
5464 force_reg (Pmode, XEXP (x, 1)));
5465 else
5466 return force_reg (Pmode, x);
5468 if (GET_CODE (x) == SYMBOL_REF)
5470 enum tls_model model = SYMBOL_REF_TLS_MODEL (x);
5471 if (model != 0)
5472 return rs6000_legitimize_tls_address (x, model);
5475 extra = 0;
5476 switch (mode)
5478 case TFmode:
5479 case TDmode:
5480 case TImode:
5481 /* As in legitimate_offset_address_p we do not assume
5482 worst-case. The mode here is just a hint as to the registers
5483 used. A TImode is usually in gprs, but may actually be in
5484 fprs. Leave worst-case scenario for reload to handle via
5485 insn constraints. */
5486 extra = 8;
5487 break;
5488 default:
5489 break;
5492 if (GET_CODE (x) == PLUS
5493 && GET_CODE (XEXP (x, 0)) == REG
5494 && GET_CODE (XEXP (x, 1)) == CONST_INT
5495 && ((unsigned HOST_WIDE_INT) (INTVAL (XEXP (x, 1)) + 0x8000)
5496 >= 0x10000 - extra)
5497 && !(SPE_VECTOR_MODE (mode)
5498 || (TARGET_E500_DOUBLE && GET_MODE_SIZE (mode) > UNITS_PER_WORD)))
5500 HOST_WIDE_INT high_int, low_int;
5501 rtx sum;
5502 low_int = ((INTVAL (XEXP (x, 1)) & 0xffff) ^ 0x8000) - 0x8000;
5503 if (low_int >= 0x8000 - extra)
5504 low_int = 0;
5505 high_int = INTVAL (XEXP (x, 1)) - low_int;
5506 sum = force_operand (gen_rtx_PLUS (Pmode, XEXP (x, 0),
5507 GEN_INT (high_int)), 0);
5508 return plus_constant (Pmode, sum, low_int);
5510 else if (GET_CODE (x) == PLUS
5511 && GET_CODE (XEXP (x, 0)) == REG
5512 && GET_CODE (XEXP (x, 1)) != CONST_INT
5513 && GET_MODE_NUNITS (mode) == 1
5514 && (GET_MODE_SIZE (mode) <= UNITS_PER_WORD
5515 || (/* ??? Assume floating point reg based on mode? */
5516 (TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT)
5517 && (mode == DFmode || mode == DDmode)))
5518 && !avoiding_indexed_address_p (mode))
5520 return gen_rtx_PLUS (Pmode, XEXP (x, 0),
5521 force_reg (Pmode, force_operand (XEXP (x, 1), 0)));
5523 else if (SPE_VECTOR_MODE (mode)
5524 || (TARGET_E500_DOUBLE && GET_MODE_SIZE (mode) > UNITS_PER_WORD))
5526 if (mode == DImode)
5527 return x;
5528 /* We accept [reg + reg] and [reg + OFFSET]. */
5530 if (GET_CODE (x) == PLUS)
5532 rtx op1 = XEXP (x, 0);
5533 rtx op2 = XEXP (x, 1);
5534 rtx y;
5536 op1 = force_reg (Pmode, op1);
5538 if (GET_CODE (op2) != REG
5539 && (GET_CODE (op2) != CONST_INT
5540 || !SPE_CONST_OFFSET_OK (INTVAL (op2))
5541 || (GET_MODE_SIZE (mode) > 8
5542 && !SPE_CONST_OFFSET_OK (INTVAL (op2) + 8))))
5543 op2 = force_reg (Pmode, op2);
5545 /* We can't always do [reg + reg] for these, because [reg +
5546 reg + offset] is not a legitimate addressing mode. */
5547 y = gen_rtx_PLUS (Pmode, op1, op2);
5549 if ((GET_MODE_SIZE (mode) > 8 || mode == DDmode) && REG_P (op2))
5550 return force_reg (Pmode, y);
5551 else
5552 return y;
5555 return force_reg (Pmode, x);
5557 else if ((TARGET_ELF
5558 #if TARGET_MACHO
5559 || !MACHO_DYNAMIC_NO_PIC_P
5560 #endif
5562 && TARGET_32BIT
5563 && TARGET_NO_TOC
5564 && ! flag_pic
5565 && GET_CODE (x) != CONST_INT
5566 && GET_CODE (x) != CONST_DOUBLE
5567 && CONSTANT_P (x)
5568 && GET_MODE_NUNITS (mode) == 1
5569 && (GET_MODE_SIZE (mode) <= UNITS_PER_WORD
5570 || (/* ??? Assume floating point reg based on mode? */
5571 (TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT)
5572 && (mode == DFmode || mode == DDmode))))
5574 rtx reg = gen_reg_rtx (Pmode);
5575 if (TARGET_ELF)
5576 emit_insn (gen_elf_high (reg, x));
5577 else
5578 emit_insn (gen_macho_high (reg, x));
5579 return gen_rtx_LO_SUM (Pmode, reg, x);
5581 else if (TARGET_TOC
5582 && GET_CODE (x) == SYMBOL_REF
5583 && constant_pool_expr_p (x)
5584 && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (x), Pmode))
5585 return create_TOC_reference (x, NULL_RTX);
5586 else
5587 return x;
5590 /* Debug version of rs6000_legitimize_address. */
5591 static rtx
5592 rs6000_debug_legitimize_address (rtx x, rtx oldx, enum machine_mode mode)
5594 rtx ret;
5595 rtx insns;
5597 start_sequence ();
5598 ret = rs6000_legitimize_address (x, oldx, mode);
5599 insns = get_insns ();
5600 end_sequence ();
5602 if (ret != x)
5604 fprintf (stderr,
5605 "\nrs6000_legitimize_address: mode %s, old code %s, "
5606 "new code %s, modified\n",
5607 GET_MODE_NAME (mode), GET_RTX_NAME (GET_CODE (x)),
5608 GET_RTX_NAME (GET_CODE (ret)));
5610 fprintf (stderr, "Original address:\n");
5611 debug_rtx (x);
5613 fprintf (stderr, "oldx:\n");
5614 debug_rtx (oldx);
5616 fprintf (stderr, "New address:\n");
5617 debug_rtx (ret);
5619 if (insns)
5621 fprintf (stderr, "Insns added:\n");
5622 debug_rtx_list (insns, 20);
5625 else
5627 fprintf (stderr,
5628 "\nrs6000_legitimize_address: mode %s, code %s, no change:\n",
5629 GET_MODE_NAME (mode), GET_RTX_NAME (GET_CODE (x)));
5631 debug_rtx (x);
5634 if (insns)
5635 emit_insn (insns);
5637 return ret;
5640 /* This is called from dwarf2out.c via TARGET_ASM_OUTPUT_DWARF_DTPREL.
5641 We need to emit DTP-relative relocations. */
5643 static void rs6000_output_dwarf_dtprel (FILE *, int, rtx) ATTRIBUTE_UNUSED;
5644 static void
5645 rs6000_output_dwarf_dtprel (FILE *file, int size, rtx x)
5647 switch (size)
5649 case 4:
5650 fputs ("\t.long\t", file);
5651 break;
5652 case 8:
5653 fputs (DOUBLE_INT_ASM_OP, file);
5654 break;
5655 default:
5656 gcc_unreachable ();
5658 output_addr_const (file, x);
5659 fputs ("@dtprel+0x8000", file);
5662 /* In the name of slightly smaller debug output, and to cater to
5663 general assembler lossage, recognize various UNSPEC sequences
5664 and turn them back into a direct symbol reference. */
5666 static rtx
5667 rs6000_delegitimize_address (rtx orig_x)
5669 rtx x, y, offset;
5671 orig_x = delegitimize_mem_from_attrs (orig_x);
5672 x = orig_x;
5673 if (MEM_P (x))
5674 x = XEXP (x, 0);
5676 y = x;
5677 if (TARGET_CMODEL != CMODEL_SMALL
5678 && GET_CODE (y) == LO_SUM)
5679 y = XEXP (y, 1);
5681 offset = NULL_RTX;
5682 if (GET_CODE (y) == PLUS
5683 && GET_MODE (y) == Pmode
5684 && CONST_INT_P (XEXP (y, 1)))
5686 offset = XEXP (y, 1);
5687 y = XEXP (y, 0);
5690 if (GET_CODE (y) == UNSPEC
5691 && XINT (y, 1) == UNSPEC_TOCREL)
5693 #ifdef ENABLE_CHECKING
5694 if (REG_P (XVECEXP (y, 0, 1))
5695 && REGNO (XVECEXP (y, 0, 1)) == TOC_REGISTER)
5697 /* All good. */
5699 else if (GET_CODE (XVECEXP (y, 0, 1)) == DEBUG_EXPR)
5701 /* Weirdness alert. df_note_compute can replace r2 with a
5702 debug_expr when this unspec is in a debug_insn.
5703 Seen in gcc.dg/pr51957-1.c */
5705 else
5707 debug_rtx (orig_x);
5708 abort ();
5710 #endif
5711 y = XVECEXP (y, 0, 0);
5712 if (offset != NULL_RTX)
5713 y = gen_rtx_PLUS (Pmode, y, offset);
5714 if (!MEM_P (orig_x))
5715 return y;
5716 else
5717 return replace_equiv_address_nv (orig_x, y);
5720 if (TARGET_MACHO
5721 && GET_CODE (orig_x) == LO_SUM
5722 && GET_CODE (XEXP (orig_x, 1)) == CONST)
5724 y = XEXP (XEXP (orig_x, 1), 0);
5725 if (GET_CODE (y) == UNSPEC
5726 && XINT (y, 1) == UNSPEC_MACHOPIC_OFFSET)
5727 return XVECEXP (y, 0, 0);
5730 return orig_x;
5733 /* Return true if X shouldn't be emitted into the debug info.
5734 The linker doesn't like .toc section references from
5735 .debug_* sections, so reject .toc section symbols. */
5737 static bool
5738 rs6000_const_not_ok_for_debug_p (rtx x)
5740 if (GET_CODE (x) == SYMBOL_REF
5741 && CONSTANT_POOL_ADDRESS_P (x))
5743 rtx c = get_pool_constant (x);
5744 enum machine_mode cmode = get_pool_mode (x);
5745 if (ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (c, cmode))
5746 return true;
5749 return false;
5752 /* Construct the SYMBOL_REF for the tls_get_addr function. */
5754 static GTY(()) rtx rs6000_tls_symbol;
5755 static rtx
5756 rs6000_tls_get_addr (void)
5758 if (!rs6000_tls_symbol)
5759 rs6000_tls_symbol = init_one_libfunc ("__tls_get_addr");
5761 return rs6000_tls_symbol;
5764 /* Construct the SYMBOL_REF for TLS GOT references. */
5766 static GTY(()) rtx rs6000_got_symbol;
5767 static rtx
5768 rs6000_got_sym (void)
5770 if (!rs6000_got_symbol)
5772 rs6000_got_symbol = gen_rtx_SYMBOL_REF (Pmode, "_GLOBAL_OFFSET_TABLE_");
5773 SYMBOL_REF_FLAGS (rs6000_got_symbol) |= SYMBOL_FLAG_LOCAL;
5774 SYMBOL_REF_FLAGS (rs6000_got_symbol) |= SYMBOL_FLAG_EXTERNAL;
5777 return rs6000_got_symbol;
5780 /* ADDR contains a thread-local SYMBOL_REF. Generate code to compute
5781 this (thread-local) address. */
5783 static rtx
5784 rs6000_legitimize_tls_address (rtx addr, enum tls_model model)
5786 rtx dest, insn;
5788 dest = gen_reg_rtx (Pmode);
5789 if (model == TLS_MODEL_LOCAL_EXEC && rs6000_tls_size == 16)
5791 rtx tlsreg;
5793 if (TARGET_64BIT)
5795 tlsreg = gen_rtx_REG (Pmode, 13);
5796 insn = gen_tls_tprel_64 (dest, tlsreg, addr);
5798 else
5800 tlsreg = gen_rtx_REG (Pmode, 2);
5801 insn = gen_tls_tprel_32 (dest, tlsreg, addr);
5803 emit_insn (insn);
5805 else if (model == TLS_MODEL_LOCAL_EXEC && rs6000_tls_size == 32)
5807 rtx tlsreg, tmp;
5809 tmp = gen_reg_rtx (Pmode);
5810 if (TARGET_64BIT)
5812 tlsreg = gen_rtx_REG (Pmode, 13);
5813 insn = gen_tls_tprel_ha_64 (tmp, tlsreg, addr);
5815 else
5817 tlsreg = gen_rtx_REG (Pmode, 2);
5818 insn = gen_tls_tprel_ha_32 (tmp, tlsreg, addr);
5820 emit_insn (insn);
5821 if (TARGET_64BIT)
5822 insn = gen_tls_tprel_lo_64 (dest, tmp, addr);
5823 else
5824 insn = gen_tls_tprel_lo_32 (dest, tmp, addr);
5825 emit_insn (insn);
5827 else
5829 rtx r3, got, tga, tmp1, tmp2, call_insn;
5831 /* We currently use relocations like @got@tlsgd for tls, which
5832 means the linker will handle allocation of tls entries, placing
5833 them in the .got section. So use a pointer to the .got section,
5834 not one to secondary TOC sections used by 64-bit -mminimal-toc,
5835 or to secondary GOT sections used by 32-bit -fPIC. */
5836 if (TARGET_64BIT)
5837 got = gen_rtx_REG (Pmode, 2);
5838 else
5840 if (flag_pic == 1)
5841 got = gen_rtx_REG (Pmode, RS6000_PIC_OFFSET_TABLE_REGNUM);
5842 else
5844 rtx gsym = rs6000_got_sym ();
5845 got = gen_reg_rtx (Pmode);
5846 if (flag_pic == 0)
5847 rs6000_emit_move (got, gsym, Pmode);
5848 else
5850 rtx mem, lab, last;
5852 tmp1 = gen_reg_rtx (Pmode);
5853 tmp2 = gen_reg_rtx (Pmode);
5854 mem = gen_const_mem (Pmode, tmp1);
5855 lab = gen_label_rtx ();
5856 emit_insn (gen_load_toc_v4_PIC_1b (gsym, lab));
5857 emit_move_insn (tmp1, gen_rtx_REG (Pmode, LR_REGNO));
5858 if (TARGET_LINK_STACK)
5859 emit_insn (gen_addsi3 (tmp1, tmp1, GEN_INT (4)));
5860 emit_move_insn (tmp2, mem);
5861 last = emit_insn (gen_addsi3 (got, tmp1, tmp2));
5862 set_unique_reg_note (last, REG_EQUAL, gsym);
5867 if (model == TLS_MODEL_GLOBAL_DYNAMIC)
5869 tga = rs6000_tls_get_addr ();
5870 emit_library_call_value (tga, dest, LCT_CONST, Pmode,
5871 1, const0_rtx, Pmode);
5873 r3 = gen_rtx_REG (Pmode, 3);
5874 if (DEFAULT_ABI == ABI_AIX && TARGET_64BIT)
5875 insn = gen_tls_gd_aix64 (r3, got, addr, tga, const0_rtx);
5876 else if (DEFAULT_ABI == ABI_AIX && !TARGET_64BIT)
5877 insn = gen_tls_gd_aix32 (r3, got, addr, tga, const0_rtx);
5878 else if (DEFAULT_ABI == ABI_V4)
5879 insn = gen_tls_gd_sysvsi (r3, got, addr, tga, const0_rtx);
5880 else
5881 gcc_unreachable ();
5882 call_insn = last_call_insn ();
5883 PATTERN (call_insn) = insn;
5884 if (DEFAULT_ABI == ABI_V4 && TARGET_SECURE_PLT && flag_pic)
5885 use_reg (&CALL_INSN_FUNCTION_USAGE (call_insn),
5886 pic_offset_table_rtx);
5888 else if (model == TLS_MODEL_LOCAL_DYNAMIC)
5890 tga = rs6000_tls_get_addr ();
5891 tmp1 = gen_reg_rtx (Pmode);
5892 emit_library_call_value (tga, tmp1, LCT_CONST, Pmode,
5893 1, const0_rtx, Pmode);
5895 r3 = gen_rtx_REG (Pmode, 3);
5896 if (DEFAULT_ABI == ABI_AIX && TARGET_64BIT)
5897 insn = gen_tls_ld_aix64 (r3, got, tga, const0_rtx);
5898 else if (DEFAULT_ABI == ABI_AIX && !TARGET_64BIT)
5899 insn = gen_tls_ld_aix32 (r3, got, tga, const0_rtx);
5900 else if (DEFAULT_ABI == ABI_V4)
5901 insn = gen_tls_ld_sysvsi (r3, got, tga, const0_rtx);
5902 else
5903 gcc_unreachable ();
5904 call_insn = last_call_insn ();
5905 PATTERN (call_insn) = insn;
5906 if (DEFAULT_ABI == ABI_V4 && TARGET_SECURE_PLT && flag_pic)
5907 use_reg (&CALL_INSN_FUNCTION_USAGE (call_insn),
5908 pic_offset_table_rtx);
5910 if (rs6000_tls_size == 16)
5912 if (TARGET_64BIT)
5913 insn = gen_tls_dtprel_64 (dest, tmp1, addr);
5914 else
5915 insn = gen_tls_dtprel_32 (dest, tmp1, addr);
5917 else if (rs6000_tls_size == 32)
5919 tmp2 = gen_reg_rtx (Pmode);
5920 if (TARGET_64BIT)
5921 insn = gen_tls_dtprel_ha_64 (tmp2, tmp1, addr);
5922 else
5923 insn = gen_tls_dtprel_ha_32 (tmp2, tmp1, addr);
5924 emit_insn (insn);
5925 if (TARGET_64BIT)
5926 insn = gen_tls_dtprel_lo_64 (dest, tmp2, addr);
5927 else
5928 insn = gen_tls_dtprel_lo_32 (dest, tmp2, addr);
5930 else
5932 tmp2 = gen_reg_rtx (Pmode);
5933 if (TARGET_64BIT)
5934 insn = gen_tls_got_dtprel_64 (tmp2, got, addr);
5935 else
5936 insn = gen_tls_got_dtprel_32 (tmp2, got, addr);
5937 emit_insn (insn);
5938 insn = gen_rtx_SET (Pmode, dest,
5939 gen_rtx_PLUS (Pmode, tmp2, tmp1));
5941 emit_insn (insn);
5943 else
5945 /* IE, or 64-bit offset LE. */
5946 tmp2 = gen_reg_rtx (Pmode);
5947 if (TARGET_64BIT)
5948 insn = gen_tls_got_tprel_64 (tmp2, got, addr);
5949 else
5950 insn = gen_tls_got_tprel_32 (tmp2, got, addr);
5951 emit_insn (insn);
5952 if (TARGET_64BIT)
5953 insn = gen_tls_tls_64 (dest, tmp2, addr);
5954 else
5955 insn = gen_tls_tls_32 (dest, tmp2, addr);
5956 emit_insn (insn);
5960 return dest;
5963 /* Return 1 if X contains a thread-local symbol. */
5965 static bool
5966 rs6000_tls_referenced_p (rtx x)
5968 if (! TARGET_HAVE_TLS)
5969 return false;
5971 return for_each_rtx (&x, &rs6000_tls_symbol_ref_1, 0);
5974 /* Implement TARGET_CANNOT_FORCE_CONST_MEM. */
5976 static bool
5977 rs6000_cannot_force_const_mem (enum machine_mode mode ATTRIBUTE_UNUSED, rtx x)
5979 if (GET_CODE (x) == HIGH
5980 && GET_CODE (XEXP (x, 0)) == UNSPEC)
5981 return true;
5983 return rs6000_tls_referenced_p (x);
5986 /* Return 1 if *X is a thread-local symbol. This is the same as
5987 rs6000_tls_symbol_ref except for the type of the unused argument. */
5989 static int
5990 rs6000_tls_symbol_ref_1 (rtx *x, void *data ATTRIBUTE_UNUSED)
5992 return RS6000_SYMBOL_REF_TLS_P (*x);
5995 /* Return true iff the given SYMBOL_REF refers to a constant pool entry
5996 that we have put in the TOC, or for cmodel=medium, if the SYMBOL_REF
5997 can be addressed relative to the toc pointer. */
5999 static bool
6000 use_toc_relative_ref (rtx sym)
6002 return ((constant_pool_expr_p (sym)
6003 && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (sym),
6004 get_pool_mode (sym)))
6005 || (TARGET_CMODEL == CMODEL_MEDIUM
6006 && !CONSTANT_POOL_ADDRESS_P (sym)
6007 && SYMBOL_REF_LOCAL_P (sym)));
6010 /* Our implementation of LEGITIMIZE_RELOAD_ADDRESS. Returns a value to
6011 replace the input X, or the original X if no replacement is called for.
6012 The output parameter *WIN is 1 if the calling macro should goto WIN,
6013 0 if it should not.
6015 For RS/6000, we wish to handle large displacements off a base
6016 register by splitting the addend across an addiu/addis and the mem insn.
6017 This cuts number of extra insns needed from 3 to 1.
6019 On Darwin, we use this to generate code for floating point constants.
6020 A movsf_low is generated so we wind up with 2 instructions rather than 3.
6021 The Darwin code is inside #if TARGET_MACHO because only then are the
6022 machopic_* functions defined. */
6023 static rtx
6024 rs6000_legitimize_reload_address (rtx x, enum machine_mode mode,
6025 int opnum, int type,
6026 int ind_levels ATTRIBUTE_UNUSED, int *win)
6028 bool reg_offset_p = reg_offset_addressing_ok_p (mode);
6030 /* Nasty hack for vsx_splat_V2DF/V2DI load from mem, which takes a
6031 DFmode/DImode MEM. */
6032 if (reg_offset_p
6033 && opnum == 1
6034 && ((mode == DFmode && recog_data.operand_mode[0] == V2DFmode)
6035 || (mode == DImode && recog_data.operand_mode[0] == V2DImode)))
6036 reg_offset_p = false;
6038 /* We must recognize output that we have already generated ourselves. */
6039 if (GET_CODE (x) == PLUS
6040 && GET_CODE (XEXP (x, 0)) == PLUS
6041 && GET_CODE (XEXP (XEXP (x, 0), 0)) == REG
6042 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6043 && GET_CODE (XEXP (x, 1)) == CONST_INT)
6045 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
6046 BASE_REG_CLASS, GET_MODE (x), VOIDmode, 0, 0,
6047 opnum, (enum reload_type) type);
6048 *win = 1;
6049 return x;
6052 /* Likewise for (lo_sum (high ...) ...) output we have generated. */
6053 if (GET_CODE (x) == LO_SUM
6054 && GET_CODE (XEXP (x, 0)) == HIGH)
6056 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
6057 BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
6058 opnum, (enum reload_type) type);
6059 *win = 1;
6060 return x;
6063 #if TARGET_MACHO
6064 if (DEFAULT_ABI == ABI_DARWIN && flag_pic
6065 && GET_CODE (x) == LO_SUM
6066 && GET_CODE (XEXP (x, 0)) == PLUS
6067 && XEXP (XEXP (x, 0), 0) == pic_offset_table_rtx
6068 && GET_CODE (XEXP (XEXP (x, 0), 1)) == HIGH
6069 && XEXP (XEXP (XEXP (x, 0), 1), 0) == XEXP (x, 1)
6070 && machopic_operand_p (XEXP (x, 1)))
6072 /* Result of previous invocation of this function on Darwin
6073 floating point constant. */
6074 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
6075 BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
6076 opnum, (enum reload_type) type);
6077 *win = 1;
6078 return x;
6080 #endif
6082 if (TARGET_CMODEL != CMODEL_SMALL
6083 && reg_offset_p
6084 && small_toc_ref (x, VOIDmode))
6086 rtx hi = gen_rtx_HIGH (Pmode, copy_rtx (x));
6087 x = gen_rtx_LO_SUM (Pmode, hi, x);
6088 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
6089 BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
6090 opnum, (enum reload_type) type);
6091 *win = 1;
6092 return x;
6095 /* Force ld/std non-word aligned offset into base register by wrapping
6096 in offset 0. */
6097 if (GET_CODE (x) == PLUS
6098 && GET_CODE (XEXP (x, 0)) == REG
6099 && REGNO (XEXP (x, 0)) < 32
6100 && INT_REG_OK_FOR_BASE_P (XEXP (x, 0), 1)
6101 && GET_CODE (XEXP (x, 1)) == CONST_INT
6102 && reg_offset_p
6103 && (INTVAL (XEXP (x, 1)) & 3) != 0
6104 && VECTOR_MEM_NONE_P (mode)
6105 && GET_MODE_SIZE (mode) >= UNITS_PER_WORD
6106 && TARGET_POWERPC64)
6108 x = gen_rtx_PLUS (GET_MODE (x), x, GEN_INT (0));
6109 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
6110 BASE_REG_CLASS, GET_MODE (x), VOIDmode, 0, 0,
6111 opnum, (enum reload_type) type);
6112 *win = 1;
6113 return x;
6116 if (GET_CODE (x) == PLUS
6117 && GET_CODE (XEXP (x, 0)) == REG
6118 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER
6119 && INT_REG_OK_FOR_BASE_P (XEXP (x, 0), 1)
6120 && GET_CODE (XEXP (x, 1)) == CONST_INT
6121 && reg_offset_p
6122 && !SPE_VECTOR_MODE (mode)
6123 && !(TARGET_E500_DOUBLE && (mode == DFmode || mode == TFmode
6124 || mode == DDmode || mode == TDmode
6125 || mode == DImode))
6126 && VECTOR_MEM_NONE_P (mode))
6128 HOST_WIDE_INT val = INTVAL (XEXP (x, 1));
6129 HOST_WIDE_INT low = ((val & 0xffff) ^ 0x8000) - 0x8000;
6130 HOST_WIDE_INT high
6131 = (((val - low) & 0xffffffff) ^ 0x80000000) - 0x80000000;
6133 /* Check for 32-bit overflow. */
6134 if (high + low != val)
6136 *win = 0;
6137 return x;
6140 /* Reload the high part into a base reg; leave the low part
6141 in the mem directly. */
6143 x = gen_rtx_PLUS (GET_MODE (x),
6144 gen_rtx_PLUS (GET_MODE (x), XEXP (x, 0),
6145 GEN_INT (high)),
6146 GEN_INT (low));
6148 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
6149 BASE_REG_CLASS, GET_MODE (x), VOIDmode, 0, 0,
6150 opnum, (enum reload_type) type);
6151 *win = 1;
6152 return x;
6155 if (GET_CODE (x) == SYMBOL_REF
6156 && reg_offset_p
6157 && VECTOR_MEM_NONE_P (mode)
6158 && !SPE_VECTOR_MODE (mode)
6159 #if TARGET_MACHO
6160 && DEFAULT_ABI == ABI_DARWIN
6161 && (flag_pic || MACHO_DYNAMIC_NO_PIC_P)
6162 && machopic_symbol_defined_p (x)
6163 #else
6164 && DEFAULT_ABI == ABI_V4
6165 && !flag_pic
6166 #endif
6167 /* Don't do this for TFmode or TDmode, since the result isn't offsettable.
6168 The same goes for DImode without 64-bit gprs and DFmode and DDmode
6169 without fprs.
6170 ??? Assume floating point reg based on mode? This assumption is
6171 violated by eg. powerpc-linux -m32 compile of gcc.dg/pr28796-2.c
6172 where reload ends up doing a DFmode load of a constant from
6173 mem using two gprs. Unfortunately, at this point reload
6174 hasn't yet selected regs so poking around in reload data
6175 won't help and even if we could figure out the regs reliably,
6176 we'd still want to allow this transformation when the mem is
6177 naturally aligned. Since we say the address is good here, we
6178 can't disable offsets from LO_SUMs in mem_operand_gpr.
6179 FIXME: Allow offset from lo_sum for other modes too, when
6180 mem is sufficiently aligned. */
6181 && mode != TFmode
6182 && mode != TDmode
6183 && (mode != DImode || TARGET_POWERPC64)
6184 && ((mode != DFmode && mode != DDmode) || TARGET_POWERPC64
6185 || (TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT)))
6187 #if TARGET_MACHO
6188 if (flag_pic)
6190 rtx offset = machopic_gen_offset (x);
6191 x = gen_rtx_LO_SUM (GET_MODE (x),
6192 gen_rtx_PLUS (Pmode, pic_offset_table_rtx,
6193 gen_rtx_HIGH (Pmode, offset)), offset);
6195 else
6196 #endif
6197 x = gen_rtx_LO_SUM (GET_MODE (x),
6198 gen_rtx_HIGH (Pmode, x), x);
6200 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
6201 BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
6202 opnum, (enum reload_type) type);
6203 *win = 1;
6204 return x;
6207 /* Reload an offset address wrapped by an AND that represents the
6208 masking of the lower bits. Strip the outer AND and let reload
6209 convert the offset address into an indirect address. For VSX,
6210 force reload to create the address with an AND in a separate
6211 register, because we can't guarantee an altivec register will
6212 be used. */
6213 if (VECTOR_MEM_ALTIVEC_P (mode)
6214 && GET_CODE (x) == AND
6215 && GET_CODE (XEXP (x, 0)) == PLUS
6216 && GET_CODE (XEXP (XEXP (x, 0), 0)) == REG
6217 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6218 && GET_CODE (XEXP (x, 1)) == CONST_INT
6219 && INTVAL (XEXP (x, 1)) == -16)
6221 x = XEXP (x, 0);
6222 *win = 1;
6223 return x;
6226 if (TARGET_TOC
6227 && reg_offset_p
6228 && GET_CODE (x) == SYMBOL_REF
6229 && use_toc_relative_ref (x))
6231 x = create_TOC_reference (x, NULL_RTX);
6232 if (TARGET_CMODEL != CMODEL_SMALL)
6233 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
6234 BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
6235 opnum, (enum reload_type) type);
6236 *win = 1;
6237 return x;
6239 *win = 0;
6240 return x;
6243 /* Debug version of rs6000_legitimize_reload_address. */
6244 static rtx
6245 rs6000_debug_legitimize_reload_address (rtx x, enum machine_mode mode,
6246 int opnum, int type,
6247 int ind_levels, int *win)
6249 rtx ret = rs6000_legitimize_reload_address (x, mode, opnum, type,
6250 ind_levels, win);
6251 fprintf (stderr,
6252 "\nrs6000_legitimize_reload_address: mode = %s, opnum = %d, "
6253 "type = %d, ind_levels = %d, win = %d, original addr:\n",
6254 GET_MODE_NAME (mode), opnum, type, ind_levels, *win);
6255 debug_rtx (x);
6257 if (x == ret)
6258 fprintf (stderr, "Same address returned\n");
6259 else if (!ret)
6260 fprintf (stderr, "NULL returned\n");
6261 else
6263 fprintf (stderr, "New address:\n");
6264 debug_rtx (ret);
6267 return ret;
6270 /* TARGET_LEGITIMATE_ADDRESS_P recognizes an RTL expression
6271 that is a valid memory address for an instruction.
6272 The MODE argument is the machine mode for the MEM expression
6273 that wants to use this address.
6275 On the RS/6000, there are four valid address: a SYMBOL_REF that
6276 refers to a constant pool entry of an address (or the sum of it
6277 plus a constant), a short (16-bit signed) constant plus a register,
6278 the sum of two registers, or a register indirect, possibly with an
6279 auto-increment. For DFmode, DDmode and DImode with a constant plus
6280 register, we must ensure that both words are addressable or PowerPC64
6281 with offset word aligned.
6283 For modes spanning multiple registers (DFmode and DDmode in 32-bit GPRs,
6284 32-bit DImode, TImode, TFmode, TDmode), indexed addressing cannot be used
6285 because adjacent memory cells are accessed by adding word-sized offsets
6286 during assembly output. */
6287 static bool
6288 rs6000_legitimate_address_p (enum machine_mode mode, rtx x, bool reg_ok_strict)
6290 bool reg_offset_p = reg_offset_addressing_ok_p (mode);
6292 /* If this is an unaligned stvx/ldvx type address, discard the outer AND. */
6293 if (VECTOR_MEM_ALTIVEC_P (mode)
6294 && GET_CODE (x) == AND
6295 && GET_CODE (XEXP (x, 1)) == CONST_INT
6296 && INTVAL (XEXP (x, 1)) == -16)
6297 x = XEXP (x, 0);
6299 if (RS6000_SYMBOL_REF_TLS_P (x))
6300 return 0;
6301 if (legitimate_indirect_address_p (x, reg_ok_strict))
6302 return 1;
6303 if ((GET_CODE (x) == PRE_INC || GET_CODE (x) == PRE_DEC)
6304 && !VECTOR_MEM_ALTIVEC_OR_VSX_P (mode)
6305 && !SPE_VECTOR_MODE (mode)
6306 && mode != TFmode
6307 && mode != TDmode
6308 /* Restrict addressing for DI because of our SUBREG hackery. */
6309 && !(TARGET_E500_DOUBLE
6310 && (mode == DFmode || mode == DDmode || mode == DImode))
6311 && TARGET_UPDATE
6312 && legitimate_indirect_address_p (XEXP (x, 0), reg_ok_strict))
6313 return 1;
6314 if (virtual_stack_registers_memory_p (x))
6315 return 1;
6316 if (reg_offset_p && legitimate_small_data_p (mode, x))
6317 return 1;
6318 if (reg_offset_p
6319 && legitimate_constant_pool_address_p (x, mode, reg_ok_strict))
6320 return 1;
6321 /* If not REG_OK_STRICT (before reload) let pass any stack offset. */
6322 if (! reg_ok_strict
6323 && reg_offset_p
6324 && GET_CODE (x) == PLUS
6325 && GET_CODE (XEXP (x, 0)) == REG
6326 && (XEXP (x, 0) == virtual_stack_vars_rtx
6327 || XEXP (x, 0) == arg_pointer_rtx)
6328 && GET_CODE (XEXP (x, 1)) == CONST_INT)
6329 return 1;
6330 if (rs6000_legitimate_offset_address_p (mode, x, reg_ok_strict, false))
6331 return 1;
6332 if (mode != TImode
6333 && mode != TFmode
6334 && mode != TDmode
6335 && ((TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT)
6336 || TARGET_POWERPC64
6337 || (mode != DFmode && mode != DDmode)
6338 || (TARGET_E500_DOUBLE && mode != DDmode))
6339 && (TARGET_POWERPC64 || mode != DImode)
6340 && !avoiding_indexed_address_p (mode)
6341 && legitimate_indexed_address_p (x, reg_ok_strict))
6342 return 1;
6343 if (GET_CODE (x) == PRE_MODIFY
6344 && mode != TImode
6345 && mode != TFmode
6346 && mode != TDmode
6347 && ((TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT)
6348 || TARGET_POWERPC64
6349 || ((mode != DFmode && mode != DDmode) || TARGET_E500_DOUBLE))
6350 && (TARGET_POWERPC64 || mode != DImode)
6351 && !VECTOR_MEM_ALTIVEC_OR_VSX_P (mode)
6352 && !SPE_VECTOR_MODE (mode)
6353 /* Restrict addressing for DI because of our SUBREG hackery. */
6354 && !(TARGET_E500_DOUBLE
6355 && (mode == DFmode || mode == DDmode || mode == DImode))
6356 && TARGET_UPDATE
6357 && legitimate_indirect_address_p (XEXP (x, 0), reg_ok_strict)
6358 && (rs6000_legitimate_offset_address_p (mode, XEXP (x, 1),
6359 reg_ok_strict, false)
6360 || (!avoiding_indexed_address_p (mode)
6361 && legitimate_indexed_address_p (XEXP (x, 1), reg_ok_strict)))
6362 && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
6363 return 1;
6364 if (reg_offset_p && legitimate_lo_sum_address_p (mode, x, reg_ok_strict))
6365 return 1;
6366 return 0;
6369 /* Debug version of rs6000_legitimate_address_p. */
6370 static bool
6371 rs6000_debug_legitimate_address_p (enum machine_mode mode, rtx x,
6372 bool reg_ok_strict)
6374 bool ret = rs6000_legitimate_address_p (mode, x, reg_ok_strict);
6375 fprintf (stderr,
6376 "\nrs6000_legitimate_address_p: return = %s, mode = %s, "
6377 "strict = %d, code = %s\n",
6378 ret ? "true" : "false",
6379 GET_MODE_NAME (mode),
6380 reg_ok_strict,
6381 GET_RTX_NAME (GET_CODE (x)));
6382 debug_rtx (x);
6384 return ret;
6387 /* Implement TARGET_MODE_DEPENDENT_ADDRESS_P. */
6389 static bool
6390 rs6000_mode_dependent_address_p (const_rtx addr)
6392 return rs6000_mode_dependent_address_ptr (addr);
6395 /* Go to LABEL if ADDR (a legitimate address expression)
6396 has an effect that depends on the machine mode it is used for.
6398 On the RS/6000 this is true of all integral offsets (since AltiVec
6399 and VSX modes don't allow them) or is a pre-increment or decrement.
6401 ??? Except that due to conceptual problems in offsettable_address_p
6402 we can't really report the problems of integral offsets. So leave
6403 this assuming that the adjustable offset must be valid for the
6404 sub-words of a TFmode operand, which is what we had before. */
6406 static bool
6407 rs6000_mode_dependent_address (const_rtx addr)
6409 switch (GET_CODE (addr))
6411 case PLUS:
6412 /* Any offset from virtual_stack_vars_rtx and arg_pointer_rtx
6413 is considered a legitimate address before reload, so there
6414 are no offset restrictions in that case. Note that this
6415 condition is safe in strict mode because any address involving
6416 virtual_stack_vars_rtx or arg_pointer_rtx would already have
6417 been rejected as illegitimate. */
6418 if (XEXP (addr, 0) != virtual_stack_vars_rtx
6419 && XEXP (addr, 0) != arg_pointer_rtx
6420 && GET_CODE (XEXP (addr, 1)) == CONST_INT)
6422 unsigned HOST_WIDE_INT val = INTVAL (XEXP (addr, 1));
6423 return val + 0x8000 >= 0x10000 - (TARGET_POWERPC64 ? 8 : 12);
6425 break;
6427 case LO_SUM:
6428 /* Anything in the constant pool is sufficiently aligned that
6429 all bytes have the same high part address. */
6430 return !legitimate_constant_pool_address_p (addr, QImode, false);
6432 /* Auto-increment cases are now treated generically in recog.c. */
6433 case PRE_MODIFY:
6434 return TARGET_UPDATE;
6436 /* AND is only allowed in Altivec loads. */
6437 case AND:
6438 return true;
6440 default:
6441 break;
6444 return false;
6447 /* Debug version of rs6000_mode_dependent_address. */
6448 static bool
6449 rs6000_debug_mode_dependent_address (const_rtx addr)
6451 bool ret = rs6000_mode_dependent_address (addr);
6453 fprintf (stderr, "\nrs6000_mode_dependent_address: ret = %s\n",
6454 ret ? "true" : "false");
6455 debug_rtx (addr);
6457 return ret;
6460 /* Implement FIND_BASE_TERM. */
6463 rs6000_find_base_term (rtx op)
6465 rtx base;
6467 base = op;
6468 if (GET_CODE (base) == CONST)
6469 base = XEXP (base, 0);
6470 if (GET_CODE (base) == PLUS)
6471 base = XEXP (base, 0);
6472 if (GET_CODE (base) == UNSPEC)
6473 switch (XINT (base, 1))
6475 case UNSPEC_TOCREL:
6476 case UNSPEC_MACHOPIC_OFFSET:
6477 /* OP represents SYM [+ OFFSET] - ANCHOR. SYM is the base term
6478 for aliasing purposes. */
6479 return XVECEXP (base, 0, 0);
6482 return op;
6485 /* More elaborate version of recog's offsettable_memref_p predicate
6486 that works around the ??? note of rs6000_mode_dependent_address.
6487 In particular it accepts
6489 (mem:DI (plus:SI (reg/f:SI 31 31) (const_int 32760 [0x7ff8])))
6491 in 32-bit mode, that the recog predicate rejects. */
6493 static bool
6494 rs6000_offsettable_memref_p (rtx op, enum machine_mode reg_mode)
6496 bool worst_case;
6498 if (!MEM_P (op))
6499 return false;
6501 /* First mimic offsettable_memref_p. */
6502 if (offsettable_address_p (true, GET_MODE (op), XEXP (op, 0)))
6503 return true;
6505 /* offsettable_address_p invokes rs6000_mode_dependent_address, but
6506 the latter predicate knows nothing about the mode of the memory
6507 reference and, therefore, assumes that it is the largest supported
6508 mode (TFmode). As a consequence, legitimate offsettable memory
6509 references are rejected. rs6000_legitimate_offset_address_p contains
6510 the correct logic for the PLUS case of rs6000_mode_dependent_address,
6511 at least with a little bit of help here given that we know the
6512 actual registers used. */
6513 worst_case = ((TARGET_POWERPC64 && GET_MODE_CLASS (reg_mode) == MODE_INT)
6514 || GET_MODE_SIZE (reg_mode) == 4);
6515 return rs6000_legitimate_offset_address_p (GET_MODE (op), XEXP (op, 0),
6516 true, worst_case);
6519 /* Change register usage conditional on target flags. */
6520 static void
6521 rs6000_conditional_register_usage (void)
6523 int i;
6525 if (TARGET_DEBUG_TARGET)
6526 fprintf (stderr, "rs6000_conditional_register_usage called\n");
6528 /* Set MQ register fixed (already call_used) if not POWER
6529 architecture (PPC601) so that it will not be allocated. */
6530 if (! TARGET_POWER)
6531 fixed_regs[64] = 1;
6533 /* 64-bit AIX and Linux reserve GPR13 for thread-private data. */
6534 if (TARGET_64BIT)
6535 fixed_regs[13] = call_used_regs[13]
6536 = call_really_used_regs[13] = 1;
6538 /* Conditionally disable FPRs. */
6539 if (TARGET_SOFT_FLOAT || !TARGET_FPRS)
6540 for (i = 32; i < 64; i++)
6541 fixed_regs[i] = call_used_regs[i]
6542 = call_really_used_regs[i] = 1;
6544 /* The TOC register is not killed across calls in a way that is
6545 visible to the compiler. */
6546 if (DEFAULT_ABI == ABI_AIX)
6547 call_really_used_regs[2] = 0;
6549 if (DEFAULT_ABI == ABI_V4
6550 && PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
6551 && flag_pic == 2)
6552 fixed_regs[RS6000_PIC_OFFSET_TABLE_REGNUM] = 1;
6554 if (DEFAULT_ABI == ABI_V4
6555 && PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
6556 && flag_pic == 1)
6557 fixed_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
6558 = call_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
6559 = call_really_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM] = 1;
6561 if (DEFAULT_ABI == ABI_DARWIN
6562 && PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM)
6563 fixed_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
6564 = call_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
6565 = call_really_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM] = 1;
6567 if (TARGET_TOC && TARGET_MINIMAL_TOC)
6568 fixed_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
6569 = call_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM] = 1;
6571 if (TARGET_SPE)
6573 global_regs[SPEFSCR_REGNO] = 1;
6574 /* We used to use r14 as FIXED_SCRATCH to address SPE 64-bit
6575 registers in prologues and epilogues. We no longer use r14
6576 for FIXED_SCRATCH, but we're keeping r14 out of the allocation
6577 pool for link-compatibility with older versions of GCC. Once
6578 "old" code has died out, we can return r14 to the allocation
6579 pool. */
6580 fixed_regs[14]
6581 = call_used_regs[14]
6582 = call_really_used_regs[14] = 1;
6585 if (!TARGET_ALTIVEC && !TARGET_VSX)
6587 for (i = FIRST_ALTIVEC_REGNO; i <= LAST_ALTIVEC_REGNO; ++i)
6588 fixed_regs[i] = call_used_regs[i] = call_really_used_regs[i] = 1;
6589 call_really_used_regs[VRSAVE_REGNO] = 1;
6592 if (TARGET_ALTIVEC || TARGET_VSX)
6593 global_regs[VSCR_REGNO] = 1;
6595 if (TARGET_ALTIVEC_ABI)
6597 for (i = FIRST_ALTIVEC_REGNO; i < FIRST_ALTIVEC_REGNO + 20; ++i)
6598 call_used_regs[i] = call_really_used_regs[i] = 1;
6600 /* AIX reserves VR20:31 in non-extended ABI mode. */
6601 if (TARGET_XCOFF)
6602 for (i = FIRST_ALTIVEC_REGNO + 20; i < FIRST_ALTIVEC_REGNO + 32; ++i)
6603 fixed_regs[i] = call_used_regs[i] = call_really_used_regs[i] = 1;
6607 /* Try to output insns to set TARGET equal to the constant C if it can
6608 be done in less than N insns. Do all computations in MODE.
6609 Returns the place where the output has been placed if it can be
6610 done and the insns have been emitted. If it would take more than N
6611 insns, zero is returned and no insns and emitted. */
6614 rs6000_emit_set_const (rtx dest, enum machine_mode mode,
6615 rtx source, int n ATTRIBUTE_UNUSED)
6617 rtx result, insn, set;
6618 HOST_WIDE_INT c0, c1;
6620 switch (mode)
6622 case QImode:
6623 case HImode:
6624 if (dest == NULL)
6625 dest = gen_reg_rtx (mode);
6626 emit_insn (gen_rtx_SET (VOIDmode, dest, source));
6627 return dest;
6629 case SImode:
6630 result = !can_create_pseudo_p () ? dest : gen_reg_rtx (SImode);
6632 emit_insn (gen_rtx_SET (VOIDmode, copy_rtx (result),
6633 GEN_INT (INTVAL (source)
6634 & (~ (HOST_WIDE_INT) 0xffff))));
6635 emit_insn (gen_rtx_SET (VOIDmode, dest,
6636 gen_rtx_IOR (SImode, copy_rtx (result),
6637 GEN_INT (INTVAL (source) & 0xffff))));
6638 result = dest;
6639 break;
6641 case DImode:
6642 switch (GET_CODE (source))
6644 case CONST_INT:
6645 c0 = INTVAL (source);
6646 c1 = -(c0 < 0);
6647 break;
6649 case CONST_DOUBLE:
6650 #if HOST_BITS_PER_WIDE_INT >= 64
6651 c0 = CONST_DOUBLE_LOW (source);
6652 c1 = -(c0 < 0);
6653 #else
6654 c0 = CONST_DOUBLE_LOW (source);
6655 c1 = CONST_DOUBLE_HIGH (source);
6656 #endif
6657 break;
6659 default:
6660 gcc_unreachable ();
6663 result = rs6000_emit_set_long_const (dest, c0, c1);
6664 break;
6666 default:
6667 gcc_unreachable ();
6670 insn = get_last_insn ();
6671 set = single_set (insn);
6672 if (! CONSTANT_P (SET_SRC (set)))
6673 set_unique_reg_note (insn, REG_EQUAL, source);
6675 return result;
6678 /* Having failed to find a 3 insn sequence in rs6000_emit_set_const,
6679 fall back to a straight forward decomposition. We do this to avoid
6680 exponential run times encountered when looking for longer sequences
6681 with rs6000_emit_set_const. */
6682 static rtx
6683 rs6000_emit_set_long_const (rtx dest, HOST_WIDE_INT c1, HOST_WIDE_INT c2)
6685 if (!TARGET_POWERPC64)
6687 rtx operand1, operand2;
6689 operand1 = operand_subword_force (dest, WORDS_BIG_ENDIAN == 0,
6690 DImode);
6691 operand2 = operand_subword_force (copy_rtx (dest), WORDS_BIG_ENDIAN != 0,
6692 DImode);
6693 emit_move_insn (operand1, GEN_INT (c1));
6694 emit_move_insn (operand2, GEN_INT (c2));
6696 else
6698 HOST_WIDE_INT ud1, ud2, ud3, ud4;
6700 ud1 = c1 & 0xffff;
6701 ud2 = (c1 & 0xffff0000) >> 16;
6702 #if HOST_BITS_PER_WIDE_INT >= 64
6703 c2 = c1 >> 32;
6704 #endif
6705 ud3 = c2 & 0xffff;
6706 ud4 = (c2 & 0xffff0000) >> 16;
6708 if ((ud4 == 0xffff && ud3 == 0xffff && ud2 == 0xffff && (ud1 & 0x8000))
6709 || (ud4 == 0 && ud3 == 0 && ud2 == 0 && ! (ud1 & 0x8000)))
6711 if (ud1 & 0x8000)
6712 emit_move_insn (dest, GEN_INT (((ud1 ^ 0x8000) - 0x8000)));
6713 else
6714 emit_move_insn (dest, GEN_INT (ud1));
6717 else if ((ud4 == 0xffff && ud3 == 0xffff && (ud2 & 0x8000))
6718 || (ud4 == 0 && ud3 == 0 && ! (ud2 & 0x8000)))
6720 if (ud2 & 0x8000)
6721 emit_move_insn (dest, GEN_INT (((ud2 << 16) ^ 0x80000000)
6722 - 0x80000000));
6723 else
6724 emit_move_insn (dest, GEN_INT (ud2 << 16));
6725 if (ud1 != 0)
6726 emit_move_insn (copy_rtx (dest),
6727 gen_rtx_IOR (DImode, copy_rtx (dest),
6728 GEN_INT (ud1)));
6730 else if (ud3 == 0 && ud4 == 0)
6732 gcc_assert (ud2 & 0x8000);
6733 emit_move_insn (dest, GEN_INT (((ud2 << 16) ^ 0x80000000)
6734 - 0x80000000));
6735 if (ud1 != 0)
6736 emit_move_insn (copy_rtx (dest),
6737 gen_rtx_IOR (DImode, copy_rtx (dest),
6738 GEN_INT (ud1)));
6739 emit_move_insn (copy_rtx (dest),
6740 gen_rtx_ZERO_EXTEND (DImode,
6741 gen_lowpart (SImode,
6742 copy_rtx (dest))));
6744 else if ((ud4 == 0xffff && (ud3 & 0x8000))
6745 || (ud4 == 0 && ! (ud3 & 0x8000)))
6747 if (ud3 & 0x8000)
6748 emit_move_insn (dest, GEN_INT (((ud3 << 16) ^ 0x80000000)
6749 - 0x80000000));
6750 else
6751 emit_move_insn (dest, GEN_INT (ud3 << 16));
6753 if (ud2 != 0)
6754 emit_move_insn (copy_rtx (dest),
6755 gen_rtx_IOR (DImode, copy_rtx (dest),
6756 GEN_INT (ud2)));
6757 emit_move_insn (copy_rtx (dest),
6758 gen_rtx_ASHIFT (DImode, copy_rtx (dest),
6759 GEN_INT (16)));
6760 if (ud1 != 0)
6761 emit_move_insn (copy_rtx (dest),
6762 gen_rtx_IOR (DImode, copy_rtx (dest),
6763 GEN_INT (ud1)));
6765 else
6767 if (ud4 & 0x8000)
6768 emit_move_insn (dest, GEN_INT (((ud4 << 16) ^ 0x80000000)
6769 - 0x80000000));
6770 else
6771 emit_move_insn (dest, GEN_INT (ud4 << 16));
6773 if (ud3 != 0)
6774 emit_move_insn (copy_rtx (dest),
6775 gen_rtx_IOR (DImode, copy_rtx (dest),
6776 GEN_INT (ud3)));
6778 emit_move_insn (copy_rtx (dest),
6779 gen_rtx_ASHIFT (DImode, copy_rtx (dest),
6780 GEN_INT (32)));
6781 if (ud2 != 0)
6782 emit_move_insn (copy_rtx (dest),
6783 gen_rtx_IOR (DImode, copy_rtx (dest),
6784 GEN_INT (ud2 << 16)));
6785 if (ud1 != 0)
6786 emit_move_insn (copy_rtx (dest),
6787 gen_rtx_IOR (DImode, copy_rtx (dest), GEN_INT (ud1)));
6790 return dest;
6793 /* Helper for the following. Get rid of [r+r] memory refs
6794 in cases where it won't work (TImode, TFmode, TDmode). */
6796 static void
6797 rs6000_eliminate_indexed_memrefs (rtx operands[2])
6799 if (reload_in_progress)
6800 return;
6802 if (GET_CODE (operands[0]) == MEM
6803 && GET_CODE (XEXP (operands[0], 0)) != REG
6804 && ! legitimate_constant_pool_address_p (XEXP (operands[0], 0),
6805 GET_MODE (operands[0]), false))
6806 operands[0]
6807 = replace_equiv_address (operands[0],
6808 copy_addr_to_reg (XEXP (operands[0], 0)));
6810 if (GET_CODE (operands[1]) == MEM
6811 && GET_CODE (XEXP (operands[1], 0)) != REG
6812 && ! legitimate_constant_pool_address_p (XEXP (operands[1], 0),
6813 GET_MODE (operands[1]), false))
6814 operands[1]
6815 = replace_equiv_address (operands[1],
6816 copy_addr_to_reg (XEXP (operands[1], 0)));
6819 /* Emit a move from SOURCE to DEST in mode MODE. */
6820 void
6821 rs6000_emit_move (rtx dest, rtx source, enum machine_mode mode)
6823 rtx operands[2];
6824 operands[0] = dest;
6825 operands[1] = source;
6827 if (TARGET_DEBUG_ADDR)
6829 fprintf (stderr,
6830 "\nrs6000_emit_move: mode = %s, reload_in_progress = %d, "
6831 "reload_completed = %d, can_create_pseudos = %d.\ndest:\n",
6832 GET_MODE_NAME (mode),
6833 reload_in_progress,
6834 reload_completed,
6835 can_create_pseudo_p ());
6836 debug_rtx (dest);
6837 fprintf (stderr, "source:\n");
6838 debug_rtx (source);
6841 /* Sanity checks. Check that we get CONST_DOUBLE only when we should. */
6842 if (GET_CODE (operands[1]) == CONST_DOUBLE
6843 && ! FLOAT_MODE_P (mode)
6844 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
6846 /* FIXME. This should never happen. */
6847 /* Since it seems that it does, do the safe thing and convert
6848 to a CONST_INT. */
6849 operands[1] = gen_int_mode (CONST_DOUBLE_LOW (operands[1]), mode);
6851 gcc_assert (GET_CODE (operands[1]) != CONST_DOUBLE
6852 || FLOAT_MODE_P (mode)
6853 || ((CONST_DOUBLE_HIGH (operands[1]) != 0
6854 || CONST_DOUBLE_LOW (operands[1]) < 0)
6855 && (CONST_DOUBLE_HIGH (operands[1]) != -1
6856 || CONST_DOUBLE_LOW (operands[1]) >= 0)));
6858 /* Check if GCC is setting up a block move that will end up using FP
6859 registers as temporaries. We must make sure this is acceptable. */
6860 if (GET_CODE (operands[0]) == MEM
6861 && GET_CODE (operands[1]) == MEM
6862 && mode == DImode
6863 && (SLOW_UNALIGNED_ACCESS (DImode, MEM_ALIGN (operands[0]))
6864 || SLOW_UNALIGNED_ACCESS (DImode, MEM_ALIGN (operands[1])))
6865 && ! (SLOW_UNALIGNED_ACCESS (SImode, (MEM_ALIGN (operands[0]) > 32
6866 ? 32 : MEM_ALIGN (operands[0])))
6867 || SLOW_UNALIGNED_ACCESS (SImode, (MEM_ALIGN (operands[1]) > 32
6868 ? 32
6869 : MEM_ALIGN (operands[1]))))
6870 && ! MEM_VOLATILE_P (operands [0])
6871 && ! MEM_VOLATILE_P (operands [1]))
6873 emit_move_insn (adjust_address (operands[0], SImode, 0),
6874 adjust_address (operands[1], SImode, 0));
6875 emit_move_insn (adjust_address (copy_rtx (operands[0]), SImode, 4),
6876 adjust_address (copy_rtx (operands[1]), SImode, 4));
6877 return;
6880 if (can_create_pseudo_p () && GET_CODE (operands[0]) == MEM
6881 && !gpc_reg_operand (operands[1], mode))
6882 operands[1] = force_reg (mode, operands[1]);
6884 if (mode == SFmode && ! TARGET_POWERPC
6885 && TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT
6886 && GET_CODE (operands[0]) == MEM)
6888 int regnum;
6890 if (reload_in_progress || reload_completed)
6891 regnum = true_regnum (operands[1]);
6892 else if (GET_CODE (operands[1]) == REG)
6893 regnum = REGNO (operands[1]);
6894 else
6895 regnum = -1;
6897 /* If operands[1] is a register, on POWER it may have
6898 double-precision data in it, so truncate it to single
6899 precision. */
6900 if (FP_REGNO_P (regnum) || regnum >= FIRST_PSEUDO_REGISTER)
6902 rtx newreg;
6903 newreg = (!can_create_pseudo_p () ? copy_rtx (operands[1])
6904 : gen_reg_rtx (mode));
6905 emit_insn (gen_aux_truncdfsf2 (newreg, operands[1]));
6906 operands[1] = newreg;
6910 /* Recognize the case where operand[1] is a reference to thread-local
6911 data and load its address to a register. */
6912 if (rs6000_tls_referenced_p (operands[1]))
6914 enum tls_model model;
6915 rtx tmp = operands[1];
6916 rtx addend = NULL;
6918 if (GET_CODE (tmp) == CONST && GET_CODE (XEXP (tmp, 0)) == PLUS)
6920 addend = XEXP (XEXP (tmp, 0), 1);
6921 tmp = XEXP (XEXP (tmp, 0), 0);
6924 gcc_assert (GET_CODE (tmp) == SYMBOL_REF);
6925 model = SYMBOL_REF_TLS_MODEL (tmp);
6926 gcc_assert (model != 0);
6928 tmp = rs6000_legitimize_tls_address (tmp, model);
6929 if (addend)
6931 tmp = gen_rtx_PLUS (mode, tmp, addend);
6932 tmp = force_operand (tmp, operands[0]);
6934 operands[1] = tmp;
6937 /* Handle the case where reload calls us with an invalid address. */
6938 if (reload_in_progress && mode == Pmode
6939 && (! general_operand (operands[1], mode)
6940 || ! nonimmediate_operand (operands[0], mode)))
6941 goto emit_set;
6943 /* 128-bit constant floating-point values on Darwin should really be
6944 loaded as two parts. */
6945 if (!TARGET_IEEEQUAD && TARGET_LONG_DOUBLE_128
6946 && mode == TFmode && GET_CODE (operands[1]) == CONST_DOUBLE)
6948 rs6000_emit_move (simplify_gen_subreg (DFmode, operands[0], mode, 0),
6949 simplify_gen_subreg (DFmode, operands[1], mode, 0),
6950 DFmode);
6951 rs6000_emit_move (simplify_gen_subreg (DFmode, operands[0], mode,
6952 GET_MODE_SIZE (DFmode)),
6953 simplify_gen_subreg (DFmode, operands[1], mode,
6954 GET_MODE_SIZE (DFmode)),
6955 DFmode);
6956 return;
6959 if (reload_in_progress && cfun->machine->sdmode_stack_slot != NULL_RTX)
6960 cfun->machine->sdmode_stack_slot =
6961 eliminate_regs (cfun->machine->sdmode_stack_slot, VOIDmode, NULL_RTX);
6963 if (reload_in_progress
6964 && mode == SDmode
6965 && MEM_P (operands[0])
6966 && rtx_equal_p (operands[0], cfun->machine->sdmode_stack_slot)
6967 && REG_P (operands[1]))
6969 if (FP_REGNO_P (REGNO (operands[1])))
6971 rtx mem = adjust_address_nv (operands[0], DDmode, 0);
6972 mem = eliminate_regs (mem, VOIDmode, NULL_RTX);
6973 emit_insn (gen_movsd_store (mem, operands[1]));
6975 else if (INT_REGNO_P (REGNO (operands[1])))
6977 rtx mem = adjust_address_nv (operands[0], mode, 4);
6978 mem = eliminate_regs (mem, VOIDmode, NULL_RTX);
6979 emit_insn (gen_movsd_hardfloat (mem, operands[1]));
6981 else
6982 gcc_unreachable();
6983 return;
6985 if (reload_in_progress
6986 && mode == SDmode
6987 && REG_P (operands[0])
6988 && MEM_P (operands[1])
6989 && rtx_equal_p (operands[1], cfun->machine->sdmode_stack_slot))
6991 if (FP_REGNO_P (REGNO (operands[0])))
6993 rtx mem = adjust_address_nv (operands[1], DDmode, 0);
6994 mem = eliminate_regs (mem, VOIDmode, NULL_RTX);
6995 emit_insn (gen_movsd_load (operands[0], mem));
6997 else if (INT_REGNO_P (REGNO (operands[0])))
6999 rtx mem = adjust_address_nv (operands[1], mode, 4);
7000 mem = eliminate_regs (mem, VOIDmode, NULL_RTX);
7001 emit_insn (gen_movsd_hardfloat (operands[0], mem));
7003 else
7004 gcc_unreachable();
7005 return;
7008 /* FIXME: In the long term, this switch statement should go away
7009 and be replaced by a sequence of tests based on things like
7010 mode == Pmode. */
7011 switch (mode)
7013 case HImode:
7014 case QImode:
7015 if (CONSTANT_P (operands[1])
7016 && GET_CODE (operands[1]) != CONST_INT)
7017 operands[1] = force_const_mem (mode, operands[1]);
7018 break;
7020 case TFmode:
7021 case TDmode:
7022 rs6000_eliminate_indexed_memrefs (operands);
7023 /* fall through */
7025 case DFmode:
7026 case DDmode:
7027 case SFmode:
7028 case SDmode:
7029 if (CONSTANT_P (operands[1])
7030 && ! easy_fp_constant (operands[1], mode))
7031 operands[1] = force_const_mem (mode, operands[1]);
7032 break;
7034 case V16QImode:
7035 case V8HImode:
7036 case V4SFmode:
7037 case V4SImode:
7038 case V4HImode:
7039 case V2SFmode:
7040 case V2SImode:
7041 case V1DImode:
7042 case V2DFmode:
7043 case V2DImode:
7044 if (CONSTANT_P (operands[1])
7045 && !easy_vector_constant (operands[1], mode))
7046 operands[1] = force_const_mem (mode, operands[1]);
7047 break;
7049 case SImode:
7050 case DImode:
7051 /* Use default pattern for address of ELF small data */
7052 if (TARGET_ELF
7053 && mode == Pmode
7054 && DEFAULT_ABI == ABI_V4
7055 && (GET_CODE (operands[1]) == SYMBOL_REF
7056 || GET_CODE (operands[1]) == CONST)
7057 && small_data_operand (operands[1], mode))
7059 emit_insn (gen_rtx_SET (VOIDmode, operands[0], operands[1]));
7060 return;
7063 if (DEFAULT_ABI == ABI_V4
7064 && mode == Pmode && mode == SImode
7065 && flag_pic == 1 && got_operand (operands[1], mode))
7067 emit_insn (gen_movsi_got (operands[0], operands[1]));
7068 return;
7071 if ((TARGET_ELF || DEFAULT_ABI == ABI_DARWIN)
7072 && TARGET_NO_TOC
7073 && ! flag_pic
7074 && mode == Pmode
7075 && CONSTANT_P (operands[1])
7076 && GET_CODE (operands[1]) != HIGH
7077 && GET_CODE (operands[1]) != CONST_INT)
7079 rtx target = (!can_create_pseudo_p ()
7080 ? operands[0]
7081 : gen_reg_rtx (mode));
7083 /* If this is a function address on -mcall-aixdesc,
7084 convert it to the address of the descriptor. */
7085 if (DEFAULT_ABI == ABI_AIX
7086 && GET_CODE (operands[1]) == SYMBOL_REF
7087 && XSTR (operands[1], 0)[0] == '.')
7089 const char *name = XSTR (operands[1], 0);
7090 rtx new_ref;
7091 while (*name == '.')
7092 name++;
7093 new_ref = gen_rtx_SYMBOL_REF (Pmode, name);
7094 CONSTANT_POOL_ADDRESS_P (new_ref)
7095 = CONSTANT_POOL_ADDRESS_P (operands[1]);
7096 SYMBOL_REF_FLAGS (new_ref) = SYMBOL_REF_FLAGS (operands[1]);
7097 SYMBOL_REF_USED (new_ref) = SYMBOL_REF_USED (operands[1]);
7098 SYMBOL_REF_DATA (new_ref) = SYMBOL_REF_DATA (operands[1]);
7099 operands[1] = new_ref;
7102 if (DEFAULT_ABI == ABI_DARWIN)
7104 #if TARGET_MACHO
7105 if (MACHO_DYNAMIC_NO_PIC_P)
7107 /* Take care of any required data indirection. */
7108 operands[1] = rs6000_machopic_legitimize_pic_address (
7109 operands[1], mode, operands[0]);
7110 if (operands[0] != operands[1])
7111 emit_insn (gen_rtx_SET (VOIDmode,
7112 operands[0], operands[1]));
7113 return;
7115 #endif
7116 emit_insn (gen_macho_high (target, operands[1]));
7117 emit_insn (gen_macho_low (operands[0], target, operands[1]));
7118 return;
7121 emit_insn (gen_elf_high (target, operands[1]));
7122 emit_insn (gen_elf_low (operands[0], target, operands[1]));
7123 return;
7126 /* If this is a SYMBOL_REF that refers to a constant pool entry,
7127 and we have put it in the TOC, we just need to make a TOC-relative
7128 reference to it. */
7129 if (TARGET_TOC
7130 && GET_CODE (operands[1]) == SYMBOL_REF
7131 && use_toc_relative_ref (operands[1]))
7132 operands[1] = create_TOC_reference (operands[1], operands[0]);
7133 else if (mode == Pmode
7134 && CONSTANT_P (operands[1])
7135 && GET_CODE (operands[1]) != HIGH
7136 && ((GET_CODE (operands[1]) != CONST_INT
7137 && ! easy_fp_constant (operands[1], mode))
7138 || (GET_CODE (operands[1]) == CONST_INT
7139 && (num_insns_constant (operands[1], mode)
7140 > (TARGET_CMODEL != CMODEL_SMALL ? 3 : 2)))
7141 || (GET_CODE (operands[0]) == REG
7142 && FP_REGNO_P (REGNO (operands[0]))))
7143 && !toc_relative_expr_p (operands[1], false)
7144 && (TARGET_CMODEL == CMODEL_SMALL
7145 || can_create_pseudo_p ()
7146 || (REG_P (operands[0])
7147 && INT_REG_OK_FOR_BASE_P (operands[0], true))))
7150 #if TARGET_MACHO
7151 /* Darwin uses a special PIC legitimizer. */
7152 if (DEFAULT_ABI == ABI_DARWIN && MACHOPIC_INDIRECT)
7154 operands[1] =
7155 rs6000_machopic_legitimize_pic_address (operands[1], mode,
7156 operands[0]);
7157 if (operands[0] != operands[1])
7158 emit_insn (gen_rtx_SET (VOIDmode, operands[0], operands[1]));
7159 return;
7161 #endif
7163 /* If we are to limit the number of things we put in the TOC and
7164 this is a symbol plus a constant we can add in one insn,
7165 just put the symbol in the TOC and add the constant. Don't do
7166 this if reload is in progress. */
7167 if (GET_CODE (operands[1]) == CONST
7168 && TARGET_NO_SUM_IN_TOC && ! reload_in_progress
7169 && GET_CODE (XEXP (operands[1], 0)) == PLUS
7170 && add_operand (XEXP (XEXP (operands[1], 0), 1), mode)
7171 && (GET_CODE (XEXP (XEXP (operands[1], 0), 0)) == LABEL_REF
7172 || GET_CODE (XEXP (XEXP (operands[1], 0), 0)) == SYMBOL_REF)
7173 && ! side_effects_p (operands[0]))
7175 rtx sym =
7176 force_const_mem (mode, XEXP (XEXP (operands[1], 0), 0));
7177 rtx other = XEXP (XEXP (operands[1], 0), 1);
7179 sym = force_reg (mode, sym);
7180 emit_insn (gen_add3_insn (operands[0], sym, other));
7181 return;
7184 operands[1] = force_const_mem (mode, operands[1]);
7186 if (TARGET_TOC
7187 && GET_CODE (XEXP (operands[1], 0)) == SYMBOL_REF
7188 && constant_pool_expr_p (XEXP (operands[1], 0))
7189 && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (
7190 get_pool_constant (XEXP (operands[1], 0)),
7191 get_pool_mode (XEXP (operands[1], 0))))
7193 rtx tocref = create_TOC_reference (XEXP (operands[1], 0),
7194 operands[0]);
7195 operands[1] = gen_const_mem (mode, tocref);
7196 set_mem_alias_set (operands[1], get_TOC_alias_set ());
7199 break;
7201 case TImode:
7202 rs6000_eliminate_indexed_memrefs (operands);
7204 if (TARGET_POWER)
7206 emit_insn (gen_rtx_PARALLEL (VOIDmode,
7207 gen_rtvec (2,
7208 gen_rtx_SET (VOIDmode,
7209 operands[0], operands[1]),
7210 gen_rtx_CLOBBER (VOIDmode,
7211 gen_rtx_SCRATCH (SImode)))));
7212 return;
7214 break;
7216 default:
7217 fatal_insn ("bad move", gen_rtx_SET (VOIDmode, dest, source));
7220 /* Above, we may have called force_const_mem which may have returned
7221 an invalid address. If we can, fix this up; otherwise, reload will
7222 have to deal with it. */
7223 if (GET_CODE (operands[1]) == MEM && ! reload_in_progress)
7224 operands[1] = validize_mem (operands[1]);
7226 emit_set:
7227 emit_insn (gen_rtx_SET (VOIDmode, operands[0], operands[1]));
7230 /* Nonzero if we can use a floating-point register to pass this arg. */
7231 #define USE_FP_FOR_ARG_P(CUM,MODE,TYPE) \
7232 (SCALAR_FLOAT_MODE_P (MODE) \
7233 && (CUM)->fregno <= FP_ARG_MAX_REG \
7234 && TARGET_HARD_FLOAT && TARGET_FPRS)
7236 /* Nonzero if we can use an AltiVec register to pass this arg. */
7237 #define USE_ALTIVEC_FOR_ARG_P(CUM,MODE,TYPE,NAMED) \
7238 (ALTIVEC_OR_VSX_VECTOR_MODE (MODE) \
7239 && (CUM)->vregno <= ALTIVEC_ARG_MAX_REG \
7240 && TARGET_ALTIVEC_ABI \
7241 && (NAMED))
7243 /* Return a nonzero value to say to return the function value in
7244 memory, just as large structures are always returned. TYPE will be
7245 the data type of the value, and FNTYPE will be the type of the
7246 function doing the returning, or @code{NULL} for libcalls.
7248 The AIX ABI for the RS/6000 specifies that all structures are
7249 returned in memory. The Darwin ABI does the same.
7251 For the Darwin 64 Bit ABI, a function result can be returned in
7252 registers or in memory, depending on the size of the return data
7253 type. If it is returned in registers, the value occupies the same
7254 registers as it would if it were the first and only function
7255 argument. Otherwise, the function places its result in memory at
7256 the location pointed to by GPR3.
7258 The SVR4 ABI specifies that structures <= 8 bytes are returned in r3/r4,
7259 but a draft put them in memory, and GCC used to implement the draft
7260 instead of the final standard. Therefore, aix_struct_return
7261 controls this instead of DEFAULT_ABI; V.4 targets needing backward
7262 compatibility can change DRAFT_V4_STRUCT_RET to override the
7263 default, and -m switches get the final word. See
7264 rs6000_option_override_internal for more details.
7266 The PPC32 SVR4 ABI uses IEEE double extended for long double, if 128-bit
7267 long double support is enabled. These values are returned in memory.
7269 int_size_in_bytes returns -1 for variable size objects, which go in
7270 memory always. The cast to unsigned makes -1 > 8. */
7272 static bool
7273 rs6000_return_in_memory (const_tree type, const_tree fntype ATTRIBUTE_UNUSED)
7275 /* For the Darwin64 ABI, test if we can fit the return value in regs. */
7276 if (TARGET_MACHO
7277 && rs6000_darwin64_abi
7278 && TREE_CODE (type) == RECORD_TYPE
7279 && int_size_in_bytes (type) > 0)
7281 CUMULATIVE_ARGS valcum;
7282 rtx valret;
7284 valcum.words = 0;
7285 valcum.fregno = FP_ARG_MIN_REG;
7286 valcum.vregno = ALTIVEC_ARG_MIN_REG;
7287 /* Do a trial code generation as if this were going to be passed
7288 as an argument; if any part goes in memory, we return NULL. */
7289 valret = rs6000_darwin64_record_arg (&valcum, type, true, true);
7290 if (valret)
7291 return false;
7292 /* Otherwise fall through to more conventional ABI rules. */
7295 if (AGGREGATE_TYPE_P (type)
7296 && (aix_struct_return
7297 || (unsigned HOST_WIDE_INT) int_size_in_bytes (type) > 8))
7298 return true;
7300 /* Allow -maltivec -mabi=no-altivec without warning. Altivec vector
7301 modes only exist for GCC vector types if -maltivec. */
7302 if (TARGET_32BIT && !TARGET_ALTIVEC_ABI
7303 && ALTIVEC_VECTOR_MODE (TYPE_MODE (type)))
7304 return false;
7306 /* Return synthetic vectors in memory. */
7307 if (TREE_CODE (type) == VECTOR_TYPE
7308 && int_size_in_bytes (type) > (TARGET_ALTIVEC_ABI ? 16 : 8))
7310 static bool warned_for_return_big_vectors = false;
7311 if (!warned_for_return_big_vectors)
7313 warning (0, "GCC vector returned by reference: "
7314 "non-standard ABI extension with no compatibility guarantee");
7315 warned_for_return_big_vectors = true;
7317 return true;
7320 if (DEFAULT_ABI == ABI_V4 && TARGET_IEEEQUAD && TYPE_MODE (type) == TFmode)
7321 return true;
7323 return false;
7326 #ifdef HAVE_AS_GNU_ATTRIBUTE
7327 /* Return TRUE if a call to function FNDECL may be one that
7328 potentially affects the function calling ABI of the object file. */
7330 static bool
7331 call_ABI_of_interest (tree fndecl)
7333 if (cgraph_state == CGRAPH_STATE_EXPANSION)
7335 struct cgraph_node *c_node;
7337 /* Libcalls are always interesting. */
7338 if (fndecl == NULL_TREE)
7339 return true;
7341 /* Any call to an external function is interesting. */
7342 if (DECL_EXTERNAL (fndecl))
7343 return true;
7345 /* Interesting functions that we are emitting in this object file. */
7346 c_node = cgraph_get_node (fndecl);
7347 c_node = cgraph_function_or_thunk_node (c_node, NULL);
7348 return !cgraph_only_called_directly_p (c_node);
7350 return false;
7352 #endif
7354 /* Initialize a variable CUM of type CUMULATIVE_ARGS
7355 for a call to a function whose data type is FNTYPE.
7356 For a library call, FNTYPE is 0 and RETURN_MODE the return value mode.
7358 For incoming args we set the number of arguments in the prototype large
7359 so we never return a PARALLEL. */
7361 void
7362 init_cumulative_args (CUMULATIVE_ARGS *cum, tree fntype,
7363 rtx libname ATTRIBUTE_UNUSED, int incoming,
7364 int libcall, int n_named_args,
7365 tree fndecl ATTRIBUTE_UNUSED,
7366 enum machine_mode return_mode ATTRIBUTE_UNUSED)
7368 static CUMULATIVE_ARGS zero_cumulative;
7370 *cum = zero_cumulative;
7371 cum->words = 0;
7372 cum->fregno = FP_ARG_MIN_REG;
7373 cum->vregno = ALTIVEC_ARG_MIN_REG;
7374 cum->prototype = (fntype && prototype_p (fntype));
7375 cum->call_cookie = ((DEFAULT_ABI == ABI_V4 && libcall)
7376 ? CALL_LIBCALL : CALL_NORMAL);
7377 cum->sysv_gregno = GP_ARG_MIN_REG;
7378 cum->stdarg = stdarg_p (fntype);
7380 cum->nargs_prototype = 0;
7381 if (incoming || cum->prototype)
7382 cum->nargs_prototype = n_named_args;
7384 /* Check for a longcall attribute. */
7385 if ((!fntype && rs6000_default_long_calls)
7386 || (fntype
7387 && lookup_attribute ("longcall", TYPE_ATTRIBUTES (fntype))
7388 && !lookup_attribute ("shortcall", TYPE_ATTRIBUTES (fntype))))
7389 cum->call_cookie |= CALL_LONG;
7391 if (TARGET_DEBUG_ARG)
7393 fprintf (stderr, "\ninit_cumulative_args:");
7394 if (fntype)
7396 tree ret_type = TREE_TYPE (fntype);
7397 fprintf (stderr, " ret code = %s,",
7398 tree_code_name[ (int)TREE_CODE (ret_type) ]);
7401 if (cum->call_cookie & CALL_LONG)
7402 fprintf (stderr, " longcall,");
7404 fprintf (stderr, " proto = %d, nargs = %d\n",
7405 cum->prototype, cum->nargs_prototype);
7408 #ifdef HAVE_AS_GNU_ATTRIBUTE
7409 if (DEFAULT_ABI == ABI_V4)
7411 cum->escapes = call_ABI_of_interest (fndecl);
7412 if (cum->escapes)
7414 tree return_type;
7416 if (fntype)
7418 return_type = TREE_TYPE (fntype);
7419 return_mode = TYPE_MODE (return_type);
7421 else
7422 return_type = lang_hooks.types.type_for_mode (return_mode, 0);
7424 if (return_type != NULL)
7426 if (TREE_CODE (return_type) == RECORD_TYPE
7427 && TYPE_TRANSPARENT_AGGR (return_type))
7429 return_type = TREE_TYPE (first_field (return_type));
7430 return_mode = TYPE_MODE (return_type);
7432 if (AGGREGATE_TYPE_P (return_type)
7433 && ((unsigned HOST_WIDE_INT) int_size_in_bytes (return_type)
7434 <= 8))
7435 rs6000_returns_struct = true;
7437 if (SCALAR_FLOAT_MODE_P (return_mode))
7438 rs6000_passes_float = true;
7439 else if (ALTIVEC_OR_VSX_VECTOR_MODE (return_mode)
7440 || SPE_VECTOR_MODE (return_mode))
7441 rs6000_passes_vector = true;
7444 #endif
7446 if (fntype
7447 && !TARGET_ALTIVEC
7448 && TARGET_ALTIVEC_ABI
7449 && ALTIVEC_VECTOR_MODE (TYPE_MODE (TREE_TYPE (fntype))))
7451 error ("cannot return value in vector register because"
7452 " altivec instructions are disabled, use -maltivec"
7453 " to enable them");
7457 /* Return true if TYPE must be passed on the stack and not in registers. */
7459 static bool
7460 rs6000_must_pass_in_stack (enum machine_mode mode, const_tree type)
7462 if (DEFAULT_ABI == ABI_AIX || TARGET_64BIT)
7463 return must_pass_in_stack_var_size (mode, type);
7464 else
7465 return must_pass_in_stack_var_size_or_pad (mode, type);
7468 /* If defined, a C expression which determines whether, and in which
7469 direction, to pad out an argument with extra space. The value
7470 should be of type `enum direction': either `upward' to pad above
7471 the argument, `downward' to pad below, or `none' to inhibit
7472 padding.
7474 For the AIX ABI structs are always stored left shifted in their
7475 argument slot. */
7477 enum direction
7478 function_arg_padding (enum machine_mode mode, const_tree type)
7480 #ifndef AGGREGATE_PADDING_FIXED
7481 #define AGGREGATE_PADDING_FIXED 0
7482 #endif
7483 #ifndef AGGREGATES_PAD_UPWARD_ALWAYS
7484 #define AGGREGATES_PAD_UPWARD_ALWAYS 0
7485 #endif
7487 if (!AGGREGATE_PADDING_FIXED)
7489 /* GCC used to pass structures of the same size as integer types as
7490 if they were in fact integers, ignoring FUNCTION_ARG_PADDING.
7491 i.e. Structures of size 1 or 2 (or 4 when TARGET_64BIT) were
7492 passed padded downward, except that -mstrict-align further
7493 muddied the water in that multi-component structures of 2 and 4
7494 bytes in size were passed padded upward.
7496 The following arranges for best compatibility with previous
7497 versions of gcc, but removes the -mstrict-align dependency. */
7498 if (BYTES_BIG_ENDIAN)
7500 HOST_WIDE_INT size = 0;
7502 if (mode == BLKmode)
7504 if (type && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
7505 size = int_size_in_bytes (type);
7507 else
7508 size = GET_MODE_SIZE (mode);
7510 if (size == 1 || size == 2 || size == 4)
7511 return downward;
7513 return upward;
7516 if (AGGREGATES_PAD_UPWARD_ALWAYS)
7518 if (type != 0 && AGGREGATE_TYPE_P (type))
7519 return upward;
7522 /* Fall back to the default. */
7523 return DEFAULT_FUNCTION_ARG_PADDING (mode, type);
7526 /* If defined, a C expression that gives the alignment boundary, in bits,
7527 of an argument with the specified mode and type. If it is not defined,
7528 PARM_BOUNDARY is used for all arguments.
7530 V.4 wants long longs and doubles to be double word aligned. Just
7531 testing the mode size is a boneheaded way to do this as it means
7532 that other types such as complex int are also double word aligned.
7533 However, we're stuck with this because changing the ABI might break
7534 existing library interfaces.
7536 Doubleword align SPE vectors.
7537 Quadword align Altivec/VSX vectors.
7538 Quadword align large synthetic vector types. */
7540 static unsigned int
7541 rs6000_function_arg_boundary (enum machine_mode mode, const_tree type)
7543 if (DEFAULT_ABI == ABI_V4
7544 && (GET_MODE_SIZE (mode) == 8
7545 || (TARGET_HARD_FLOAT
7546 && TARGET_FPRS
7547 && (mode == TFmode || mode == TDmode))))
7548 return 64;
7549 else if (SPE_VECTOR_MODE (mode)
7550 || (type && TREE_CODE (type) == VECTOR_TYPE
7551 && int_size_in_bytes (type) >= 8
7552 && int_size_in_bytes (type) < 16))
7553 return 64;
7554 else if (ALTIVEC_OR_VSX_VECTOR_MODE (mode)
7555 || (type && TREE_CODE (type) == VECTOR_TYPE
7556 && int_size_in_bytes (type) >= 16))
7557 return 128;
7558 else if (TARGET_MACHO
7559 && rs6000_darwin64_abi
7560 && mode == BLKmode
7561 && type && TYPE_ALIGN (type) > 64)
7562 return 128;
7563 else
7564 return PARM_BOUNDARY;
7567 /* For a function parm of MODE and TYPE, return the starting word in
7568 the parameter area. NWORDS of the parameter area are already used. */
7570 static unsigned int
7571 rs6000_parm_start (enum machine_mode mode, const_tree type,
7572 unsigned int nwords)
7574 unsigned int align;
7575 unsigned int parm_offset;
7577 align = rs6000_function_arg_boundary (mode, type) / PARM_BOUNDARY - 1;
7578 parm_offset = DEFAULT_ABI == ABI_V4 ? 2 : 6;
7579 return nwords + (-(parm_offset + nwords) & align);
7582 /* Compute the size (in words) of a function argument. */
7584 static unsigned long
7585 rs6000_arg_size (enum machine_mode mode, const_tree type)
7587 unsigned long size;
7589 if (mode != BLKmode)
7590 size = GET_MODE_SIZE (mode);
7591 else
7592 size = int_size_in_bytes (type);
7594 if (TARGET_32BIT)
7595 return (size + 3) >> 2;
7596 else
7597 return (size + 7) >> 3;
7600 /* Use this to flush pending int fields. */
7602 static void
7603 rs6000_darwin64_record_arg_advance_flush (CUMULATIVE_ARGS *cum,
7604 HOST_WIDE_INT bitpos, int final)
7606 unsigned int startbit, endbit;
7607 int intregs, intoffset;
7608 enum machine_mode mode;
7610 /* Handle the situations where a float is taking up the first half
7611 of the GPR, and the other half is empty (typically due to
7612 alignment restrictions). We can detect this by a 8-byte-aligned
7613 int field, or by seeing that this is the final flush for this
7614 argument. Count the word and continue on. */
7615 if (cum->floats_in_gpr == 1
7616 && (cum->intoffset % 64 == 0
7617 || (cum->intoffset == -1 && final)))
7619 cum->words++;
7620 cum->floats_in_gpr = 0;
7623 if (cum->intoffset == -1)
7624 return;
7626 intoffset = cum->intoffset;
7627 cum->intoffset = -1;
7628 cum->floats_in_gpr = 0;
7630 if (intoffset % BITS_PER_WORD != 0)
7632 mode = mode_for_size (BITS_PER_WORD - intoffset % BITS_PER_WORD,
7633 MODE_INT, 0);
7634 if (mode == BLKmode)
7636 /* We couldn't find an appropriate mode, which happens,
7637 e.g., in packed structs when there are 3 bytes to load.
7638 Back intoffset back to the beginning of the word in this
7639 case. */
7640 intoffset = intoffset & -BITS_PER_WORD;
7644 startbit = intoffset & -BITS_PER_WORD;
7645 endbit = (bitpos + BITS_PER_WORD - 1) & -BITS_PER_WORD;
7646 intregs = (endbit - startbit) / BITS_PER_WORD;
7647 cum->words += intregs;
7648 /* words should be unsigned. */
7649 if ((unsigned)cum->words < (endbit/BITS_PER_WORD))
7651 int pad = (endbit/BITS_PER_WORD) - cum->words;
7652 cum->words += pad;
7656 /* The darwin64 ABI calls for us to recurse down through structs,
7657 looking for elements passed in registers. Unfortunately, we have
7658 to track int register count here also because of misalignments
7659 in powerpc alignment mode. */
7661 static void
7662 rs6000_darwin64_record_arg_advance_recurse (CUMULATIVE_ARGS *cum,
7663 const_tree type,
7664 HOST_WIDE_INT startbitpos)
7666 tree f;
7668 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
7669 if (TREE_CODE (f) == FIELD_DECL)
7671 HOST_WIDE_INT bitpos = startbitpos;
7672 tree ftype = TREE_TYPE (f);
7673 enum machine_mode mode;
7674 if (ftype == error_mark_node)
7675 continue;
7676 mode = TYPE_MODE (ftype);
7678 if (DECL_SIZE (f) != 0
7679 && host_integerp (bit_position (f), 1))
7680 bitpos += int_bit_position (f);
7682 /* ??? FIXME: else assume zero offset. */
7684 if (TREE_CODE (ftype) == RECORD_TYPE)
7685 rs6000_darwin64_record_arg_advance_recurse (cum, ftype, bitpos);
7686 else if (USE_FP_FOR_ARG_P (cum, mode, ftype))
7688 unsigned n_fpregs = (GET_MODE_SIZE (mode) + 7) >> 3;
7689 rs6000_darwin64_record_arg_advance_flush (cum, bitpos, 0);
7690 cum->fregno += n_fpregs;
7691 /* Single-precision floats present a special problem for
7692 us, because they are smaller than an 8-byte GPR, and so
7693 the structure-packing rules combined with the standard
7694 varargs behavior mean that we want to pack float/float
7695 and float/int combinations into a single register's
7696 space. This is complicated by the arg advance flushing,
7697 which works on arbitrarily large groups of int-type
7698 fields. */
7699 if (mode == SFmode)
7701 if (cum->floats_in_gpr == 1)
7703 /* Two floats in a word; count the word and reset
7704 the float count. */
7705 cum->words++;
7706 cum->floats_in_gpr = 0;
7708 else if (bitpos % 64 == 0)
7710 /* A float at the beginning of an 8-byte word;
7711 count it and put off adjusting cum->words until
7712 we see if a arg advance flush is going to do it
7713 for us. */
7714 cum->floats_in_gpr++;
7716 else
7718 /* The float is at the end of a word, preceded
7719 by integer fields, so the arg advance flush
7720 just above has already set cum->words and
7721 everything is taken care of. */
7724 else
7725 cum->words += n_fpregs;
7727 else if (USE_ALTIVEC_FOR_ARG_P (cum, mode, type, 1))
7729 rs6000_darwin64_record_arg_advance_flush (cum, bitpos, 0);
7730 cum->vregno++;
7731 cum->words += 2;
7733 else if (cum->intoffset == -1)
7734 cum->intoffset = bitpos;
7738 /* Check for an item that needs to be considered specially under the darwin 64
7739 bit ABI. These are record types where the mode is BLK or the structure is
7740 8 bytes in size. */
7741 static int
7742 rs6000_darwin64_struct_check_p (enum machine_mode mode, const_tree type)
7744 return rs6000_darwin64_abi
7745 && ((mode == BLKmode
7746 && TREE_CODE (type) == RECORD_TYPE
7747 && int_size_in_bytes (type) > 0)
7748 || (type && TREE_CODE (type) == RECORD_TYPE
7749 && int_size_in_bytes (type) == 8)) ? 1 : 0;
7752 /* Update the data in CUM to advance over an argument
7753 of mode MODE and data type TYPE.
7754 (TYPE is null for libcalls where that information may not be available.)
7756 Note that for args passed by reference, function_arg will be called
7757 with MODE and TYPE set to that of the pointer to the arg, not the arg
7758 itself. */
7760 static void
7761 rs6000_function_arg_advance_1 (CUMULATIVE_ARGS *cum, enum machine_mode mode,
7762 const_tree type, bool named, int depth)
7764 /* Only tick off an argument if we're not recursing. */
7765 if (depth == 0)
7766 cum->nargs_prototype--;
7768 #ifdef HAVE_AS_GNU_ATTRIBUTE
7769 if (DEFAULT_ABI == ABI_V4
7770 && cum->escapes)
7772 if (SCALAR_FLOAT_MODE_P (mode))
7773 rs6000_passes_float = true;
7774 else if (named && ALTIVEC_OR_VSX_VECTOR_MODE (mode))
7775 rs6000_passes_vector = true;
7776 else if (SPE_VECTOR_MODE (mode)
7777 && !cum->stdarg
7778 && cum->sysv_gregno <= GP_ARG_MAX_REG)
7779 rs6000_passes_vector = true;
7781 #endif
7783 if (TARGET_ALTIVEC_ABI
7784 && (ALTIVEC_OR_VSX_VECTOR_MODE (mode)
7785 || (type && TREE_CODE (type) == VECTOR_TYPE
7786 && int_size_in_bytes (type) == 16)))
7788 bool stack = false;
7790 if (USE_ALTIVEC_FOR_ARG_P (cum, mode, type, named))
7792 cum->vregno++;
7793 if (!TARGET_ALTIVEC)
7794 error ("cannot pass argument in vector register because"
7795 " altivec instructions are disabled, use -maltivec"
7796 " to enable them");
7798 /* PowerPC64 Linux and AIX allocate GPRs for a vector argument
7799 even if it is going to be passed in a vector register.
7800 Darwin does the same for variable-argument functions. */
7801 if ((DEFAULT_ABI == ABI_AIX && TARGET_64BIT)
7802 || (cum->stdarg && DEFAULT_ABI != ABI_V4))
7803 stack = true;
7805 else
7806 stack = true;
7808 if (stack)
7810 int align;
7812 /* Vector parameters must be 16-byte aligned. This places
7813 them at 2 mod 4 in terms of words in 32-bit mode, since
7814 the parameter save area starts at offset 24 from the
7815 stack. In 64-bit mode, they just have to start on an
7816 even word, since the parameter save area is 16-byte
7817 aligned. Space for GPRs is reserved even if the argument
7818 will be passed in memory. */
7819 if (TARGET_32BIT)
7820 align = (2 - cum->words) & 3;
7821 else
7822 align = cum->words & 1;
7823 cum->words += align + rs6000_arg_size (mode, type);
7825 if (TARGET_DEBUG_ARG)
7827 fprintf (stderr, "function_adv: words = %2d, align=%d, ",
7828 cum->words, align);
7829 fprintf (stderr, "nargs = %4d, proto = %d, mode = %4s\n",
7830 cum->nargs_prototype, cum->prototype,
7831 GET_MODE_NAME (mode));
7835 else if (TARGET_SPE_ABI && TARGET_SPE && SPE_VECTOR_MODE (mode)
7836 && !cum->stdarg
7837 && cum->sysv_gregno <= GP_ARG_MAX_REG)
7838 cum->sysv_gregno++;
7840 else if (TARGET_MACHO && rs6000_darwin64_struct_check_p (mode, type))
7842 int size = int_size_in_bytes (type);
7843 /* Variable sized types have size == -1 and are
7844 treated as if consisting entirely of ints.
7845 Pad to 16 byte boundary if needed. */
7846 if (TYPE_ALIGN (type) >= 2 * BITS_PER_WORD
7847 && (cum->words % 2) != 0)
7848 cum->words++;
7849 /* For varargs, we can just go up by the size of the struct. */
7850 if (!named)
7851 cum->words += (size + 7) / 8;
7852 else
7854 /* It is tempting to say int register count just goes up by
7855 sizeof(type)/8, but this is wrong in a case such as
7856 { int; double; int; } [powerpc alignment]. We have to
7857 grovel through the fields for these too. */
7858 cum->intoffset = 0;
7859 cum->floats_in_gpr = 0;
7860 rs6000_darwin64_record_arg_advance_recurse (cum, type, 0);
7861 rs6000_darwin64_record_arg_advance_flush (cum,
7862 size * BITS_PER_UNIT, 1);
7864 if (TARGET_DEBUG_ARG)
7866 fprintf (stderr, "function_adv: words = %2d, align=%d, size=%d",
7867 cum->words, TYPE_ALIGN (type), size);
7868 fprintf (stderr,
7869 "nargs = %4d, proto = %d, mode = %4s (darwin64 abi)\n",
7870 cum->nargs_prototype, cum->prototype,
7871 GET_MODE_NAME (mode));
7874 else if (DEFAULT_ABI == ABI_V4)
7876 if (TARGET_HARD_FLOAT && TARGET_FPRS
7877 && ((TARGET_SINGLE_FLOAT && mode == SFmode)
7878 || (TARGET_DOUBLE_FLOAT && mode == DFmode)
7879 || (mode == TFmode && !TARGET_IEEEQUAD)
7880 || mode == SDmode || mode == DDmode || mode == TDmode))
7882 /* _Decimal128 must use an even/odd register pair. This assumes
7883 that the register number is odd when fregno is odd. */
7884 if (mode == TDmode && (cum->fregno % 2) == 1)
7885 cum->fregno++;
7887 if (cum->fregno + (mode == TFmode || mode == TDmode ? 1 : 0)
7888 <= FP_ARG_V4_MAX_REG)
7889 cum->fregno += (GET_MODE_SIZE (mode) + 7) >> 3;
7890 else
7892 cum->fregno = FP_ARG_V4_MAX_REG + 1;
7893 if (mode == DFmode || mode == TFmode
7894 || mode == DDmode || mode == TDmode)
7895 cum->words += cum->words & 1;
7896 cum->words += rs6000_arg_size (mode, type);
7899 else
7901 int n_words = rs6000_arg_size (mode, type);
7902 int gregno = cum->sysv_gregno;
7904 /* Long long and SPE vectors are put in (r3,r4), (r5,r6),
7905 (r7,r8) or (r9,r10). As does any other 2 word item such
7906 as complex int due to a historical mistake. */
7907 if (n_words == 2)
7908 gregno += (1 - gregno) & 1;
7910 /* Multi-reg args are not split between registers and stack. */
7911 if (gregno + n_words - 1 > GP_ARG_MAX_REG)
7913 /* Long long and SPE vectors are aligned on the stack.
7914 So are other 2 word items such as complex int due to
7915 a historical mistake. */
7916 if (n_words == 2)
7917 cum->words += cum->words & 1;
7918 cum->words += n_words;
7921 /* Note: continuing to accumulate gregno past when we've started
7922 spilling to the stack indicates the fact that we've started
7923 spilling to the stack to expand_builtin_saveregs. */
7924 cum->sysv_gregno = gregno + n_words;
7927 if (TARGET_DEBUG_ARG)
7929 fprintf (stderr, "function_adv: words = %2d, fregno = %2d, ",
7930 cum->words, cum->fregno);
7931 fprintf (stderr, "gregno = %2d, nargs = %4d, proto = %d, ",
7932 cum->sysv_gregno, cum->nargs_prototype, cum->prototype);
7933 fprintf (stderr, "mode = %4s, named = %d\n",
7934 GET_MODE_NAME (mode), named);
7937 else
7939 int n_words = rs6000_arg_size (mode, type);
7940 int start_words = cum->words;
7941 int align_words = rs6000_parm_start (mode, type, start_words);
7943 cum->words = align_words + n_words;
7945 if (SCALAR_FLOAT_MODE_P (mode)
7946 && TARGET_HARD_FLOAT && TARGET_FPRS)
7948 /* _Decimal128 must be passed in an even/odd float register pair.
7949 This assumes that the register number is odd when fregno is
7950 odd. */
7951 if (mode == TDmode && (cum->fregno % 2) == 1)
7952 cum->fregno++;
7953 cum->fregno += (GET_MODE_SIZE (mode) + 7) >> 3;
7956 if (TARGET_DEBUG_ARG)
7958 fprintf (stderr, "function_adv: words = %2d, fregno = %2d, ",
7959 cum->words, cum->fregno);
7960 fprintf (stderr, "nargs = %4d, proto = %d, mode = %4s, ",
7961 cum->nargs_prototype, cum->prototype, GET_MODE_NAME (mode));
7962 fprintf (stderr, "named = %d, align = %d, depth = %d\n",
7963 named, align_words - start_words, depth);
7968 static void
7969 rs6000_function_arg_advance (cumulative_args_t cum, enum machine_mode mode,
7970 const_tree type, bool named)
7972 rs6000_function_arg_advance_1 (get_cumulative_args (cum), mode, type, named,
7976 static rtx
7977 spe_build_register_parallel (enum machine_mode mode, int gregno)
7979 rtx r1, r3, r5, r7;
7981 switch (mode)
7983 case DFmode:
7984 r1 = gen_rtx_REG (DImode, gregno);
7985 r1 = gen_rtx_EXPR_LIST (VOIDmode, r1, const0_rtx);
7986 return gen_rtx_PARALLEL (mode, gen_rtvec (1, r1));
7988 case DCmode:
7989 case TFmode:
7990 r1 = gen_rtx_REG (DImode, gregno);
7991 r1 = gen_rtx_EXPR_LIST (VOIDmode, r1, const0_rtx);
7992 r3 = gen_rtx_REG (DImode, gregno + 2);
7993 r3 = gen_rtx_EXPR_LIST (VOIDmode, r3, GEN_INT (8));
7994 return gen_rtx_PARALLEL (mode, gen_rtvec (2, r1, r3));
7996 case TCmode:
7997 r1 = gen_rtx_REG (DImode, gregno);
7998 r1 = gen_rtx_EXPR_LIST (VOIDmode, r1, const0_rtx);
7999 r3 = gen_rtx_REG (DImode, gregno + 2);
8000 r3 = gen_rtx_EXPR_LIST (VOIDmode, r3, GEN_INT (8));
8001 r5 = gen_rtx_REG (DImode, gregno + 4);
8002 r5 = gen_rtx_EXPR_LIST (VOIDmode, r5, GEN_INT (16));
8003 r7 = gen_rtx_REG (DImode, gregno + 6);
8004 r7 = gen_rtx_EXPR_LIST (VOIDmode, r7, GEN_INT (24));
8005 return gen_rtx_PARALLEL (mode, gen_rtvec (4, r1, r3, r5, r7));
8007 default:
8008 gcc_unreachable ();
8012 /* Determine where to put a SIMD argument on the SPE. */
8013 static rtx
8014 rs6000_spe_function_arg (const CUMULATIVE_ARGS *cum, enum machine_mode mode,
8015 const_tree type)
8017 int gregno = cum->sysv_gregno;
8019 /* On E500 v2, double arithmetic is done on the full 64-bit GPR, but
8020 are passed and returned in a pair of GPRs for ABI compatibility. */
8021 if (TARGET_E500_DOUBLE && (mode == DFmode || mode == TFmode
8022 || mode == DCmode || mode == TCmode))
8024 int n_words = rs6000_arg_size (mode, type);
8026 /* Doubles go in an odd/even register pair (r5/r6, etc). */
8027 if (mode == DFmode)
8028 gregno += (1 - gregno) & 1;
8030 /* Multi-reg args are not split between registers and stack. */
8031 if (gregno + n_words - 1 > GP_ARG_MAX_REG)
8032 return NULL_RTX;
8034 return spe_build_register_parallel (mode, gregno);
8036 if (cum->stdarg)
8038 int n_words = rs6000_arg_size (mode, type);
8040 /* SPE vectors are put in odd registers. */
8041 if (n_words == 2 && (gregno & 1) == 0)
8042 gregno += 1;
8044 if (gregno + n_words - 1 <= GP_ARG_MAX_REG)
8046 rtx r1, r2;
8047 enum machine_mode m = SImode;
8049 r1 = gen_rtx_REG (m, gregno);
8050 r1 = gen_rtx_EXPR_LIST (m, r1, const0_rtx);
8051 r2 = gen_rtx_REG (m, gregno + 1);
8052 r2 = gen_rtx_EXPR_LIST (m, r2, GEN_INT (4));
8053 return gen_rtx_PARALLEL (mode, gen_rtvec (2, r1, r2));
8055 else
8056 return NULL_RTX;
8058 else
8060 if (gregno <= GP_ARG_MAX_REG)
8061 return gen_rtx_REG (mode, gregno);
8062 else
8063 return NULL_RTX;
8067 /* A subroutine of rs6000_darwin64_record_arg. Assign the bits of the
8068 structure between cum->intoffset and bitpos to integer registers. */
8070 static void
8071 rs6000_darwin64_record_arg_flush (CUMULATIVE_ARGS *cum,
8072 HOST_WIDE_INT bitpos, rtx rvec[], int *k)
8074 enum machine_mode mode;
8075 unsigned int regno;
8076 unsigned int startbit, endbit;
8077 int this_regno, intregs, intoffset;
8078 rtx reg;
8080 if (cum->intoffset == -1)
8081 return;
8083 intoffset = cum->intoffset;
8084 cum->intoffset = -1;
8086 /* If this is the trailing part of a word, try to only load that
8087 much into the register. Otherwise load the whole register. Note
8088 that in the latter case we may pick up unwanted bits. It's not a
8089 problem at the moment but may wish to revisit. */
8091 if (intoffset % BITS_PER_WORD != 0)
8093 mode = mode_for_size (BITS_PER_WORD - intoffset % BITS_PER_WORD,
8094 MODE_INT, 0);
8095 if (mode == BLKmode)
8097 /* We couldn't find an appropriate mode, which happens,
8098 e.g., in packed structs when there are 3 bytes to load.
8099 Back intoffset back to the beginning of the word in this
8100 case. */
8101 intoffset = intoffset & -BITS_PER_WORD;
8102 mode = word_mode;
8105 else
8106 mode = word_mode;
8108 startbit = intoffset & -BITS_PER_WORD;
8109 endbit = (bitpos + BITS_PER_WORD - 1) & -BITS_PER_WORD;
8110 intregs = (endbit - startbit) / BITS_PER_WORD;
8111 this_regno = cum->words + intoffset / BITS_PER_WORD;
8113 if (intregs > 0 && intregs > GP_ARG_NUM_REG - this_regno)
8114 cum->use_stack = 1;
8116 intregs = MIN (intregs, GP_ARG_NUM_REG - this_regno);
8117 if (intregs <= 0)
8118 return;
8120 intoffset /= BITS_PER_UNIT;
8123 regno = GP_ARG_MIN_REG + this_regno;
8124 reg = gen_rtx_REG (mode, regno);
8125 rvec[(*k)++] =
8126 gen_rtx_EXPR_LIST (VOIDmode, reg, GEN_INT (intoffset));
8128 this_regno += 1;
8129 intoffset = (intoffset | (UNITS_PER_WORD-1)) + 1;
8130 mode = word_mode;
8131 intregs -= 1;
8133 while (intregs > 0);
8136 /* Recursive workhorse for the following. */
8138 static void
8139 rs6000_darwin64_record_arg_recurse (CUMULATIVE_ARGS *cum, const_tree type,
8140 HOST_WIDE_INT startbitpos, rtx rvec[],
8141 int *k)
8143 tree f;
8145 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
8146 if (TREE_CODE (f) == FIELD_DECL)
8148 HOST_WIDE_INT bitpos = startbitpos;
8149 tree ftype = TREE_TYPE (f);
8150 enum machine_mode mode;
8151 if (ftype == error_mark_node)
8152 continue;
8153 mode = TYPE_MODE (ftype);
8155 if (DECL_SIZE (f) != 0
8156 && host_integerp (bit_position (f), 1))
8157 bitpos += int_bit_position (f);
8159 /* ??? FIXME: else assume zero offset. */
8161 if (TREE_CODE (ftype) == RECORD_TYPE)
8162 rs6000_darwin64_record_arg_recurse (cum, ftype, bitpos, rvec, k);
8163 else if (cum->named && USE_FP_FOR_ARG_P (cum, mode, ftype))
8165 unsigned n_fpreg = (GET_MODE_SIZE (mode) + 7) >> 3;
8166 #if 0
8167 switch (mode)
8169 case SCmode: mode = SFmode; break;
8170 case DCmode: mode = DFmode; break;
8171 case TCmode: mode = TFmode; break;
8172 default: break;
8174 #endif
8175 rs6000_darwin64_record_arg_flush (cum, bitpos, rvec, k);
8176 if (cum->fregno + n_fpreg > FP_ARG_MAX_REG + 1)
8178 gcc_assert (cum->fregno == FP_ARG_MAX_REG
8179 && (mode == TFmode || mode == TDmode));
8180 /* Long double or _Decimal128 split over regs and memory. */
8181 mode = DECIMAL_FLOAT_MODE_P (mode) ? DDmode : DFmode;
8182 cum->use_stack=1;
8184 rvec[(*k)++]
8185 = gen_rtx_EXPR_LIST (VOIDmode,
8186 gen_rtx_REG (mode, cum->fregno++),
8187 GEN_INT (bitpos / BITS_PER_UNIT));
8188 if (mode == TFmode || mode == TDmode)
8189 cum->fregno++;
8191 else if (cum->named && USE_ALTIVEC_FOR_ARG_P (cum, mode, ftype, 1))
8193 rs6000_darwin64_record_arg_flush (cum, bitpos, rvec, k);
8194 rvec[(*k)++]
8195 = gen_rtx_EXPR_LIST (VOIDmode,
8196 gen_rtx_REG (mode, cum->vregno++),
8197 GEN_INT (bitpos / BITS_PER_UNIT));
8199 else if (cum->intoffset == -1)
8200 cum->intoffset = bitpos;
8204 /* For the darwin64 ABI, we want to construct a PARALLEL consisting of
8205 the register(s) to be used for each field and subfield of a struct
8206 being passed by value, along with the offset of where the
8207 register's value may be found in the block. FP fields go in FP
8208 register, vector fields go in vector registers, and everything
8209 else goes in int registers, packed as in memory.
8211 This code is also used for function return values. RETVAL indicates
8212 whether this is the case.
8214 Much of this is taken from the SPARC V9 port, which has a similar
8215 calling convention. */
8217 static rtx
8218 rs6000_darwin64_record_arg (CUMULATIVE_ARGS *orig_cum, const_tree type,
8219 bool named, bool retval)
8221 rtx rvec[FIRST_PSEUDO_REGISTER];
8222 int k = 1, kbase = 1;
8223 HOST_WIDE_INT typesize = int_size_in_bytes (type);
8224 /* This is a copy; modifications are not visible to our caller. */
8225 CUMULATIVE_ARGS copy_cum = *orig_cum;
8226 CUMULATIVE_ARGS *cum = &copy_cum;
8228 /* Pad to 16 byte boundary if needed. */
8229 if (!retval && TYPE_ALIGN (type) >= 2 * BITS_PER_WORD
8230 && (cum->words % 2) != 0)
8231 cum->words++;
8233 cum->intoffset = 0;
8234 cum->use_stack = 0;
8235 cum->named = named;
8237 /* Put entries into rvec[] for individual FP and vector fields, and
8238 for the chunks of memory that go in int regs. Note we start at
8239 element 1; 0 is reserved for an indication of using memory, and
8240 may or may not be filled in below. */
8241 rs6000_darwin64_record_arg_recurse (cum, type, /* startbit pos= */ 0, rvec, &k);
8242 rs6000_darwin64_record_arg_flush (cum, typesize * BITS_PER_UNIT, rvec, &k);
8244 /* If any part of the struct went on the stack put all of it there.
8245 This hack is because the generic code for
8246 FUNCTION_ARG_PARTIAL_NREGS cannot handle cases where the register
8247 parts of the struct are not at the beginning. */
8248 if (cum->use_stack)
8250 if (retval)
8251 return NULL_RTX; /* doesn't go in registers at all */
8252 kbase = 0;
8253 rvec[0] = gen_rtx_EXPR_LIST (VOIDmode, NULL_RTX, const0_rtx);
8255 if (k > 1 || cum->use_stack)
8256 return gen_rtx_PARALLEL (BLKmode, gen_rtvec_v (k - kbase, &rvec[kbase]));
8257 else
8258 return NULL_RTX;
8261 /* Determine where to place an argument in 64-bit mode with 32-bit ABI. */
8263 static rtx
8264 rs6000_mixed_function_arg (enum machine_mode mode, const_tree type,
8265 int align_words)
8267 int n_units;
8268 int i, k;
8269 rtx rvec[GP_ARG_NUM_REG + 1];
8271 if (align_words >= GP_ARG_NUM_REG)
8272 return NULL_RTX;
8274 n_units = rs6000_arg_size (mode, type);
8276 /* Optimize the simple case where the arg fits in one gpr, except in
8277 the case of BLKmode due to assign_parms assuming that registers are
8278 BITS_PER_WORD wide. */
8279 if (n_units == 0
8280 || (n_units == 1 && mode != BLKmode))
8281 return gen_rtx_REG (mode, GP_ARG_MIN_REG + align_words);
8283 k = 0;
8284 if (align_words + n_units > GP_ARG_NUM_REG)
8285 /* Not all of the arg fits in gprs. Say that it goes in memory too,
8286 using a magic NULL_RTX component.
8287 This is not strictly correct. Only some of the arg belongs in
8288 memory, not all of it. However, the normal scheme using
8289 function_arg_partial_nregs can result in unusual subregs, eg.
8290 (subreg:SI (reg:DF) 4), which are not handled well. The code to
8291 store the whole arg to memory is often more efficient than code
8292 to store pieces, and we know that space is available in the right
8293 place for the whole arg. */
8294 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, NULL_RTX, const0_rtx);
8296 i = 0;
8299 rtx r = gen_rtx_REG (SImode, GP_ARG_MIN_REG + align_words);
8300 rtx off = GEN_INT (i++ * 4);
8301 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, off);
8303 while (++align_words < GP_ARG_NUM_REG && --n_units != 0);
8305 return gen_rtx_PARALLEL (mode, gen_rtvec_v (k, rvec));
8308 /* Determine where to put an argument to a function.
8309 Value is zero to push the argument on the stack,
8310 or a hard register in which to store the argument.
8312 MODE is the argument's machine mode.
8313 TYPE is the data type of the argument (as a tree).
8314 This is null for libcalls where that information may
8315 not be available.
8316 CUM is a variable of type CUMULATIVE_ARGS which gives info about
8317 the preceding args and about the function being called. It is
8318 not modified in this routine.
8319 NAMED is nonzero if this argument is a named parameter
8320 (otherwise it is an extra parameter matching an ellipsis).
8322 On RS/6000 the first eight words of non-FP are normally in registers
8323 and the rest are pushed. Under AIX, the first 13 FP args are in registers.
8324 Under V.4, the first 8 FP args are in registers.
8326 If this is floating-point and no prototype is specified, we use
8327 both an FP and integer register (or possibly FP reg and stack). Library
8328 functions (when CALL_LIBCALL is set) always have the proper types for args,
8329 so we can pass the FP value just in one register. emit_library_function
8330 doesn't support PARALLEL anyway.
8332 Note that for args passed by reference, function_arg will be called
8333 with MODE and TYPE set to that of the pointer to the arg, not the arg
8334 itself. */
8336 static rtx
8337 rs6000_function_arg (cumulative_args_t cum_v, enum machine_mode mode,
8338 const_tree type, bool named)
8340 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
8341 enum rs6000_abi abi = DEFAULT_ABI;
8343 /* Return a marker to indicate whether CR1 needs to set or clear the
8344 bit that V.4 uses to say fp args were passed in registers.
8345 Assume that we don't need the marker for software floating point,
8346 or compiler generated library calls. */
8347 if (mode == VOIDmode)
8349 if (abi == ABI_V4
8350 && (cum->call_cookie & CALL_LIBCALL) == 0
8351 && (cum->stdarg
8352 || (cum->nargs_prototype < 0
8353 && (cum->prototype || TARGET_NO_PROTOTYPE))))
8355 /* For the SPE, we need to crxor CR6 always. */
8356 if (TARGET_SPE_ABI)
8357 return GEN_INT (cum->call_cookie | CALL_V4_SET_FP_ARGS);
8358 else if (TARGET_HARD_FLOAT && TARGET_FPRS)
8359 return GEN_INT (cum->call_cookie
8360 | ((cum->fregno == FP_ARG_MIN_REG)
8361 ? CALL_V4_SET_FP_ARGS
8362 : CALL_V4_CLEAR_FP_ARGS));
8365 return GEN_INT (cum->call_cookie & ~CALL_LIBCALL);
8368 if (TARGET_MACHO && rs6000_darwin64_struct_check_p (mode, type))
8370 rtx rslt = rs6000_darwin64_record_arg (cum, type, named, /*retval= */false);
8371 if (rslt != NULL_RTX)
8372 return rslt;
8373 /* Else fall through to usual handling. */
8376 if (USE_ALTIVEC_FOR_ARG_P (cum, mode, type, named))
8377 if (TARGET_64BIT && ! cum->prototype)
8379 /* Vector parameters get passed in vector register
8380 and also in GPRs or memory, in absence of prototype. */
8381 int align_words;
8382 rtx slot;
8383 align_words = (cum->words + 1) & ~1;
8385 if (align_words >= GP_ARG_NUM_REG)
8387 slot = NULL_RTX;
8389 else
8391 slot = gen_rtx_REG (mode, GP_ARG_MIN_REG + align_words);
8393 return gen_rtx_PARALLEL (mode,
8394 gen_rtvec (2,
8395 gen_rtx_EXPR_LIST (VOIDmode,
8396 slot, const0_rtx),
8397 gen_rtx_EXPR_LIST (VOIDmode,
8398 gen_rtx_REG (mode, cum->vregno),
8399 const0_rtx)));
8401 else
8402 return gen_rtx_REG (mode, cum->vregno);
8403 else if (TARGET_ALTIVEC_ABI
8404 && (ALTIVEC_OR_VSX_VECTOR_MODE (mode)
8405 || (type && TREE_CODE (type) == VECTOR_TYPE
8406 && int_size_in_bytes (type) == 16)))
8408 if (named || abi == ABI_V4)
8409 return NULL_RTX;
8410 else
8412 /* Vector parameters to varargs functions under AIX or Darwin
8413 get passed in memory and possibly also in GPRs. */
8414 int align, align_words, n_words;
8415 enum machine_mode part_mode;
8417 /* Vector parameters must be 16-byte aligned. This places them at
8418 2 mod 4 in terms of words in 32-bit mode, since the parameter
8419 save area starts at offset 24 from the stack. In 64-bit mode,
8420 they just have to start on an even word, since the parameter
8421 save area is 16-byte aligned. */
8422 if (TARGET_32BIT)
8423 align = (2 - cum->words) & 3;
8424 else
8425 align = cum->words & 1;
8426 align_words = cum->words + align;
8428 /* Out of registers? Memory, then. */
8429 if (align_words >= GP_ARG_NUM_REG)
8430 return NULL_RTX;
8432 if (TARGET_32BIT && TARGET_POWERPC64)
8433 return rs6000_mixed_function_arg (mode, type, align_words);
8435 /* The vector value goes in GPRs. Only the part of the
8436 value in GPRs is reported here. */
8437 part_mode = mode;
8438 n_words = rs6000_arg_size (mode, type);
8439 if (align_words + n_words > GP_ARG_NUM_REG)
8440 /* Fortunately, there are only two possibilities, the value
8441 is either wholly in GPRs or half in GPRs and half not. */
8442 part_mode = DImode;
8444 return gen_rtx_REG (part_mode, GP_ARG_MIN_REG + align_words);
8447 else if (TARGET_SPE_ABI && TARGET_SPE
8448 && (SPE_VECTOR_MODE (mode)
8449 || (TARGET_E500_DOUBLE && (mode == DFmode
8450 || mode == DCmode
8451 || mode == TFmode
8452 || mode == TCmode))))
8453 return rs6000_spe_function_arg (cum, mode, type);
8455 else if (abi == ABI_V4)
8457 if (TARGET_HARD_FLOAT && TARGET_FPRS
8458 && ((TARGET_SINGLE_FLOAT && mode == SFmode)
8459 || (TARGET_DOUBLE_FLOAT && mode == DFmode)
8460 || (mode == TFmode && !TARGET_IEEEQUAD)
8461 || mode == SDmode || mode == DDmode || mode == TDmode))
8463 /* _Decimal128 must use an even/odd register pair. This assumes
8464 that the register number is odd when fregno is odd. */
8465 if (mode == TDmode && (cum->fregno % 2) == 1)
8466 cum->fregno++;
8468 if (cum->fregno + (mode == TFmode || mode == TDmode ? 1 : 0)
8469 <= FP_ARG_V4_MAX_REG)
8470 return gen_rtx_REG (mode, cum->fregno);
8471 else
8472 return NULL_RTX;
8474 else
8476 int n_words = rs6000_arg_size (mode, type);
8477 int gregno = cum->sysv_gregno;
8479 /* Long long and SPE vectors are put in (r3,r4), (r5,r6),
8480 (r7,r8) or (r9,r10). As does any other 2 word item such
8481 as complex int due to a historical mistake. */
8482 if (n_words == 2)
8483 gregno += (1 - gregno) & 1;
8485 /* Multi-reg args are not split between registers and stack. */
8486 if (gregno + n_words - 1 > GP_ARG_MAX_REG)
8487 return NULL_RTX;
8489 if (TARGET_32BIT && TARGET_POWERPC64)
8490 return rs6000_mixed_function_arg (mode, type,
8491 gregno - GP_ARG_MIN_REG);
8492 return gen_rtx_REG (mode, gregno);
8495 else
8497 int align_words = rs6000_parm_start (mode, type, cum->words);
8499 /* _Decimal128 must be passed in an even/odd float register pair.
8500 This assumes that the register number is odd when fregno is odd. */
8501 if (mode == TDmode && (cum->fregno % 2) == 1)
8502 cum->fregno++;
8504 if (USE_FP_FOR_ARG_P (cum, mode, type))
8506 rtx rvec[GP_ARG_NUM_REG + 1];
8507 rtx r;
8508 int k;
8509 bool needs_psave;
8510 enum machine_mode fmode = mode;
8511 unsigned long n_fpreg = (GET_MODE_SIZE (mode) + 7) >> 3;
8513 if (cum->fregno + n_fpreg > FP_ARG_MAX_REG + 1)
8515 /* Currently, we only ever need one reg here because complex
8516 doubles are split. */
8517 gcc_assert (cum->fregno == FP_ARG_MAX_REG
8518 && (fmode == TFmode || fmode == TDmode));
8520 /* Long double or _Decimal128 split over regs and memory. */
8521 fmode = DECIMAL_FLOAT_MODE_P (fmode) ? DDmode : DFmode;
8524 /* Do we also need to pass this arg in the parameter save
8525 area? */
8526 needs_psave = (type
8527 && (cum->nargs_prototype <= 0
8528 || (DEFAULT_ABI == ABI_AIX
8529 && TARGET_XL_COMPAT
8530 && align_words >= GP_ARG_NUM_REG)));
8532 if (!needs_psave && mode == fmode)
8533 return gen_rtx_REG (fmode, cum->fregno);
8535 k = 0;
8536 if (needs_psave)
8538 /* Describe the part that goes in gprs or the stack.
8539 This piece must come first, before the fprs. */
8540 if (align_words < GP_ARG_NUM_REG)
8542 unsigned long n_words = rs6000_arg_size (mode, type);
8544 if (align_words + n_words > GP_ARG_NUM_REG
8545 || (TARGET_32BIT && TARGET_POWERPC64))
8547 /* If this is partially on the stack, then we only
8548 include the portion actually in registers here. */
8549 enum machine_mode rmode = TARGET_32BIT ? SImode : DImode;
8550 rtx off;
8551 int i = 0;
8552 if (align_words + n_words > GP_ARG_NUM_REG)
8553 /* Not all of the arg fits in gprs. Say that it
8554 goes in memory too, using a magic NULL_RTX
8555 component. Also see comment in
8556 rs6000_mixed_function_arg for why the normal
8557 function_arg_partial_nregs scheme doesn't work
8558 in this case. */
8559 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, NULL_RTX,
8560 const0_rtx);
8563 r = gen_rtx_REG (rmode,
8564 GP_ARG_MIN_REG + align_words);
8565 off = GEN_INT (i++ * GET_MODE_SIZE (rmode));
8566 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, off);
8568 while (++align_words < GP_ARG_NUM_REG && --n_words != 0);
8570 else
8572 /* The whole arg fits in gprs. */
8573 r = gen_rtx_REG (mode, GP_ARG_MIN_REG + align_words);
8574 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, const0_rtx);
8577 else
8578 /* It's entirely in memory. */
8579 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, NULL_RTX, const0_rtx);
8582 /* Describe where this piece goes in the fprs. */
8583 r = gen_rtx_REG (fmode, cum->fregno);
8584 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, const0_rtx);
8586 return gen_rtx_PARALLEL (mode, gen_rtvec_v (k, rvec));
8588 else if (align_words < GP_ARG_NUM_REG)
8590 if (TARGET_32BIT && TARGET_POWERPC64)
8591 return rs6000_mixed_function_arg (mode, type, align_words);
8593 if (mode == BLKmode)
8594 mode = Pmode;
8596 return gen_rtx_REG (mode, GP_ARG_MIN_REG + align_words);
8598 else
8599 return NULL_RTX;
8603 /* For an arg passed partly in registers and partly in memory, this is
8604 the number of bytes passed in registers. For args passed entirely in
8605 registers or entirely in memory, zero. When an arg is described by a
8606 PARALLEL, perhaps using more than one register type, this function
8607 returns the number of bytes used by the first element of the PARALLEL. */
8609 static int
8610 rs6000_arg_partial_bytes (cumulative_args_t cum_v, enum machine_mode mode,
8611 tree type, bool named)
8613 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
8614 int ret = 0;
8615 int align_words;
8617 if (DEFAULT_ABI == ABI_V4)
8618 return 0;
8620 if (USE_ALTIVEC_FOR_ARG_P (cum, mode, type, named)
8621 && cum->nargs_prototype >= 0)
8622 return 0;
8624 /* In this complicated case we just disable the partial_nregs code. */
8625 if (TARGET_MACHO && rs6000_darwin64_struct_check_p (mode, type))
8626 return 0;
8628 align_words = rs6000_parm_start (mode, type, cum->words);
8630 if (USE_FP_FOR_ARG_P (cum, mode, type))
8632 /* If we are passing this arg in the fixed parameter save area
8633 (gprs or memory) as well as fprs, then this function should
8634 return the number of partial bytes passed in the parameter
8635 save area rather than partial bytes passed in fprs. */
8636 if (type
8637 && (cum->nargs_prototype <= 0
8638 || (DEFAULT_ABI == ABI_AIX
8639 && TARGET_XL_COMPAT
8640 && align_words >= GP_ARG_NUM_REG)))
8641 return 0;
8642 else if (cum->fregno + ((GET_MODE_SIZE (mode) + 7) >> 3)
8643 > FP_ARG_MAX_REG + 1)
8644 ret = (FP_ARG_MAX_REG + 1 - cum->fregno) * 8;
8645 else if (cum->nargs_prototype >= 0)
8646 return 0;
8649 if (align_words < GP_ARG_NUM_REG
8650 && GP_ARG_NUM_REG < align_words + rs6000_arg_size (mode, type))
8651 ret = (GP_ARG_NUM_REG - align_words) * (TARGET_32BIT ? 4 : 8);
8653 if (ret != 0 && TARGET_DEBUG_ARG)
8654 fprintf (stderr, "rs6000_arg_partial_bytes: %d\n", ret);
8656 return ret;
8659 /* A C expression that indicates when an argument must be passed by
8660 reference. If nonzero for an argument, a copy of that argument is
8661 made in memory and a pointer to the argument is passed instead of
8662 the argument itself. The pointer is passed in whatever way is
8663 appropriate for passing a pointer to that type.
8665 Under V.4, aggregates and long double are passed by reference.
8667 As an extension to all 32-bit ABIs, AltiVec vectors are passed by
8668 reference unless the AltiVec vector extension ABI is in force.
8670 As an extension to all ABIs, variable sized types are passed by
8671 reference. */
8673 static bool
8674 rs6000_pass_by_reference (cumulative_args_t cum ATTRIBUTE_UNUSED,
8675 enum machine_mode mode, const_tree type,
8676 bool named ATTRIBUTE_UNUSED)
8678 if (DEFAULT_ABI == ABI_V4 && TARGET_IEEEQUAD && mode == TFmode)
8680 if (TARGET_DEBUG_ARG)
8681 fprintf (stderr, "function_arg_pass_by_reference: V4 long double\n");
8682 return 1;
8685 if (!type)
8686 return 0;
8688 if (DEFAULT_ABI == ABI_V4 && AGGREGATE_TYPE_P (type))
8690 if (TARGET_DEBUG_ARG)
8691 fprintf (stderr, "function_arg_pass_by_reference: V4 aggregate\n");
8692 return 1;
8695 if (int_size_in_bytes (type) < 0)
8697 if (TARGET_DEBUG_ARG)
8698 fprintf (stderr, "function_arg_pass_by_reference: variable size\n");
8699 return 1;
8702 /* Allow -maltivec -mabi=no-altivec without warning. Altivec vector
8703 modes only exist for GCC vector types if -maltivec. */
8704 if (TARGET_32BIT && !TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (mode))
8706 if (TARGET_DEBUG_ARG)
8707 fprintf (stderr, "function_arg_pass_by_reference: AltiVec\n");
8708 return 1;
8711 /* Pass synthetic vectors in memory. */
8712 if (TREE_CODE (type) == VECTOR_TYPE
8713 && int_size_in_bytes (type) > (TARGET_ALTIVEC_ABI ? 16 : 8))
8715 static bool warned_for_pass_big_vectors = false;
8716 if (TARGET_DEBUG_ARG)
8717 fprintf (stderr, "function_arg_pass_by_reference: synthetic vector\n");
8718 if (!warned_for_pass_big_vectors)
8720 warning (0, "GCC vector passed by reference: "
8721 "non-standard ABI extension with no compatibility guarantee");
8722 warned_for_pass_big_vectors = true;
8724 return 1;
8727 return 0;
8730 static void
8731 rs6000_move_block_from_reg (int regno, rtx x, int nregs)
8733 int i;
8734 enum machine_mode reg_mode = TARGET_32BIT ? SImode : DImode;
8736 if (nregs == 0)
8737 return;
8739 for (i = 0; i < nregs; i++)
8741 rtx tem = adjust_address_nv (x, reg_mode, i * GET_MODE_SIZE (reg_mode));
8742 if (reload_completed)
8744 if (! strict_memory_address_p (reg_mode, XEXP (tem, 0)))
8745 tem = NULL_RTX;
8746 else
8747 tem = simplify_gen_subreg (reg_mode, x, BLKmode,
8748 i * GET_MODE_SIZE (reg_mode));
8750 else
8751 tem = replace_equiv_address (tem, XEXP (tem, 0));
8753 gcc_assert (tem);
8755 emit_move_insn (tem, gen_rtx_REG (reg_mode, regno + i));
8759 /* Perform any needed actions needed for a function that is receiving a
8760 variable number of arguments.
8762 CUM is as above.
8764 MODE and TYPE are the mode and type of the current parameter.
8766 PRETEND_SIZE is a variable that should be set to the amount of stack
8767 that must be pushed by the prolog to pretend that our caller pushed
8770 Normally, this macro will push all remaining incoming registers on the
8771 stack and set PRETEND_SIZE to the length of the registers pushed. */
8773 static void
8774 setup_incoming_varargs (cumulative_args_t cum, enum machine_mode mode,
8775 tree type, int *pretend_size ATTRIBUTE_UNUSED,
8776 int no_rtl)
8778 CUMULATIVE_ARGS next_cum;
8779 int reg_size = TARGET_32BIT ? 4 : 8;
8780 rtx save_area = NULL_RTX, mem;
8781 int first_reg_offset;
8782 alias_set_type set;
8784 /* Skip the last named argument. */
8785 next_cum = *get_cumulative_args (cum);
8786 rs6000_function_arg_advance_1 (&next_cum, mode, type, true, 0);
8788 if (DEFAULT_ABI == ABI_V4)
8790 first_reg_offset = next_cum.sysv_gregno - GP_ARG_MIN_REG;
8792 if (! no_rtl)
8794 int gpr_reg_num = 0, gpr_size = 0, fpr_size = 0;
8795 HOST_WIDE_INT offset = 0;
8797 /* Try to optimize the size of the varargs save area.
8798 The ABI requires that ap.reg_save_area is doubleword
8799 aligned, but we don't need to allocate space for all
8800 the bytes, only those to which we actually will save
8801 anything. */
8802 if (cfun->va_list_gpr_size && first_reg_offset < GP_ARG_NUM_REG)
8803 gpr_reg_num = GP_ARG_NUM_REG - first_reg_offset;
8804 if (TARGET_HARD_FLOAT && TARGET_FPRS
8805 && next_cum.fregno <= FP_ARG_V4_MAX_REG
8806 && cfun->va_list_fpr_size)
8808 if (gpr_reg_num)
8809 fpr_size = (next_cum.fregno - FP_ARG_MIN_REG)
8810 * UNITS_PER_FP_WORD;
8811 if (cfun->va_list_fpr_size
8812 < FP_ARG_V4_MAX_REG + 1 - next_cum.fregno)
8813 fpr_size += cfun->va_list_fpr_size * UNITS_PER_FP_WORD;
8814 else
8815 fpr_size += (FP_ARG_V4_MAX_REG + 1 - next_cum.fregno)
8816 * UNITS_PER_FP_WORD;
8818 if (gpr_reg_num)
8820 offset = -((first_reg_offset * reg_size) & ~7);
8821 if (!fpr_size && gpr_reg_num > cfun->va_list_gpr_size)
8823 gpr_reg_num = cfun->va_list_gpr_size;
8824 if (reg_size == 4 && (first_reg_offset & 1))
8825 gpr_reg_num++;
8827 gpr_size = (gpr_reg_num * reg_size + 7) & ~7;
8829 else if (fpr_size)
8830 offset = - (int) (next_cum.fregno - FP_ARG_MIN_REG)
8831 * UNITS_PER_FP_WORD
8832 - (int) (GP_ARG_NUM_REG * reg_size);
8834 if (gpr_size + fpr_size)
8836 rtx reg_save_area
8837 = assign_stack_local (BLKmode, gpr_size + fpr_size, 64);
8838 gcc_assert (GET_CODE (reg_save_area) == MEM);
8839 reg_save_area = XEXP (reg_save_area, 0);
8840 if (GET_CODE (reg_save_area) == PLUS)
8842 gcc_assert (XEXP (reg_save_area, 0)
8843 == virtual_stack_vars_rtx);
8844 gcc_assert (GET_CODE (XEXP (reg_save_area, 1)) == CONST_INT);
8845 offset += INTVAL (XEXP (reg_save_area, 1));
8847 else
8848 gcc_assert (reg_save_area == virtual_stack_vars_rtx);
8851 cfun->machine->varargs_save_offset = offset;
8852 save_area = plus_constant (Pmode, virtual_stack_vars_rtx, offset);
8855 else
8857 first_reg_offset = next_cum.words;
8858 save_area = virtual_incoming_args_rtx;
8860 if (targetm.calls.must_pass_in_stack (mode, type))
8861 first_reg_offset += rs6000_arg_size (TYPE_MODE (type), type);
8864 set = get_varargs_alias_set ();
8865 if (! no_rtl && first_reg_offset < GP_ARG_NUM_REG
8866 && cfun->va_list_gpr_size)
8868 int nregs = GP_ARG_NUM_REG - first_reg_offset;
8870 if (va_list_gpr_counter_field)
8872 /* V4 va_list_gpr_size counts number of registers needed. */
8873 if (nregs > cfun->va_list_gpr_size)
8874 nregs = cfun->va_list_gpr_size;
8876 else
8878 /* char * va_list instead counts number of bytes needed. */
8879 if (nregs > cfun->va_list_gpr_size / reg_size)
8880 nregs = cfun->va_list_gpr_size / reg_size;
8883 mem = gen_rtx_MEM (BLKmode,
8884 plus_constant (Pmode, save_area,
8885 first_reg_offset * reg_size));
8886 MEM_NOTRAP_P (mem) = 1;
8887 set_mem_alias_set (mem, set);
8888 set_mem_align (mem, BITS_PER_WORD);
8890 rs6000_move_block_from_reg (GP_ARG_MIN_REG + first_reg_offset, mem,
8891 nregs);
8894 /* Save FP registers if needed. */
8895 if (DEFAULT_ABI == ABI_V4
8896 && TARGET_HARD_FLOAT && TARGET_FPRS
8897 && ! no_rtl
8898 && next_cum.fregno <= FP_ARG_V4_MAX_REG
8899 && cfun->va_list_fpr_size)
8901 int fregno = next_cum.fregno, nregs;
8902 rtx cr1 = gen_rtx_REG (CCmode, CR1_REGNO);
8903 rtx lab = gen_label_rtx ();
8904 int off = (GP_ARG_NUM_REG * reg_size) + ((fregno - FP_ARG_MIN_REG)
8905 * UNITS_PER_FP_WORD);
8907 emit_jump_insn
8908 (gen_rtx_SET (VOIDmode,
8909 pc_rtx,
8910 gen_rtx_IF_THEN_ELSE (VOIDmode,
8911 gen_rtx_NE (VOIDmode, cr1,
8912 const0_rtx),
8913 gen_rtx_LABEL_REF (VOIDmode, lab),
8914 pc_rtx)));
8916 for (nregs = 0;
8917 fregno <= FP_ARG_V4_MAX_REG && nregs < cfun->va_list_fpr_size;
8918 fregno++, off += UNITS_PER_FP_WORD, nregs++)
8920 mem = gen_rtx_MEM ((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT)
8921 ? DFmode : SFmode,
8922 plus_constant (Pmode, save_area, off));
8923 MEM_NOTRAP_P (mem) = 1;
8924 set_mem_alias_set (mem, set);
8925 set_mem_align (mem, GET_MODE_ALIGNMENT (
8926 (TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT)
8927 ? DFmode : SFmode));
8928 emit_move_insn (mem, gen_rtx_REG (
8929 (TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT)
8930 ? DFmode : SFmode, fregno));
8933 emit_label (lab);
8937 /* Create the va_list data type. */
8939 static tree
8940 rs6000_build_builtin_va_list (void)
8942 tree f_gpr, f_fpr, f_res, f_ovf, f_sav, record, type_decl;
8944 /* For AIX, prefer 'char *' because that's what the system
8945 header files like. */
8946 if (DEFAULT_ABI != ABI_V4)
8947 return build_pointer_type (char_type_node);
8949 record = (*lang_hooks.types.make_type) (RECORD_TYPE);
8950 type_decl = build_decl (BUILTINS_LOCATION, TYPE_DECL,
8951 get_identifier ("__va_list_tag"), record);
8953 f_gpr = build_decl (BUILTINS_LOCATION, FIELD_DECL, get_identifier ("gpr"),
8954 unsigned_char_type_node);
8955 f_fpr = build_decl (BUILTINS_LOCATION, FIELD_DECL, get_identifier ("fpr"),
8956 unsigned_char_type_node);
8957 /* Give the two bytes of padding a name, so that -Wpadded won't warn on
8958 every user file. */
8959 f_res = build_decl (BUILTINS_LOCATION, FIELD_DECL,
8960 get_identifier ("reserved"), short_unsigned_type_node);
8961 f_ovf = build_decl (BUILTINS_LOCATION, FIELD_DECL,
8962 get_identifier ("overflow_arg_area"),
8963 ptr_type_node);
8964 f_sav = build_decl (BUILTINS_LOCATION, FIELD_DECL,
8965 get_identifier ("reg_save_area"),
8966 ptr_type_node);
8968 va_list_gpr_counter_field = f_gpr;
8969 va_list_fpr_counter_field = f_fpr;
8971 DECL_FIELD_CONTEXT (f_gpr) = record;
8972 DECL_FIELD_CONTEXT (f_fpr) = record;
8973 DECL_FIELD_CONTEXT (f_res) = record;
8974 DECL_FIELD_CONTEXT (f_ovf) = record;
8975 DECL_FIELD_CONTEXT (f_sav) = record;
8977 TYPE_STUB_DECL (record) = type_decl;
8978 TYPE_NAME (record) = type_decl;
8979 TYPE_FIELDS (record) = f_gpr;
8980 DECL_CHAIN (f_gpr) = f_fpr;
8981 DECL_CHAIN (f_fpr) = f_res;
8982 DECL_CHAIN (f_res) = f_ovf;
8983 DECL_CHAIN (f_ovf) = f_sav;
8985 layout_type (record);
8987 /* The correct type is an array type of one element. */
8988 return build_array_type (record, build_index_type (size_zero_node));
8991 /* Implement va_start. */
8993 static void
8994 rs6000_va_start (tree valist, rtx nextarg)
8996 HOST_WIDE_INT words, n_gpr, n_fpr;
8997 tree f_gpr, f_fpr, f_res, f_ovf, f_sav;
8998 tree gpr, fpr, ovf, sav, t;
9000 /* Only SVR4 needs something special. */
9001 if (DEFAULT_ABI != ABI_V4)
9003 std_expand_builtin_va_start (valist, nextarg);
9004 return;
9007 f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
9008 f_fpr = DECL_CHAIN (f_gpr);
9009 f_res = DECL_CHAIN (f_fpr);
9010 f_ovf = DECL_CHAIN (f_res);
9011 f_sav = DECL_CHAIN (f_ovf);
9013 valist = build_simple_mem_ref (valist);
9014 gpr = build3 (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr, NULL_TREE);
9015 fpr = build3 (COMPONENT_REF, TREE_TYPE (f_fpr), unshare_expr (valist),
9016 f_fpr, NULL_TREE);
9017 ovf = build3 (COMPONENT_REF, TREE_TYPE (f_ovf), unshare_expr (valist),
9018 f_ovf, NULL_TREE);
9019 sav = build3 (COMPONENT_REF, TREE_TYPE (f_sav), unshare_expr (valist),
9020 f_sav, NULL_TREE);
9022 /* Count number of gp and fp argument registers used. */
9023 words = crtl->args.info.words;
9024 n_gpr = MIN (crtl->args.info.sysv_gregno - GP_ARG_MIN_REG,
9025 GP_ARG_NUM_REG);
9026 n_fpr = MIN (crtl->args.info.fregno - FP_ARG_MIN_REG,
9027 FP_ARG_NUM_REG);
9029 if (TARGET_DEBUG_ARG)
9030 fprintf (stderr, "va_start: words = "HOST_WIDE_INT_PRINT_DEC", n_gpr = "
9031 HOST_WIDE_INT_PRINT_DEC", n_fpr = "HOST_WIDE_INT_PRINT_DEC"\n",
9032 words, n_gpr, n_fpr);
9034 if (cfun->va_list_gpr_size)
9036 t = build2 (MODIFY_EXPR, TREE_TYPE (gpr), gpr,
9037 build_int_cst (NULL_TREE, n_gpr));
9038 TREE_SIDE_EFFECTS (t) = 1;
9039 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
9042 if (cfun->va_list_fpr_size)
9044 t = build2 (MODIFY_EXPR, TREE_TYPE (fpr), fpr,
9045 build_int_cst (NULL_TREE, n_fpr));
9046 TREE_SIDE_EFFECTS (t) = 1;
9047 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
9049 #ifdef HAVE_AS_GNU_ATTRIBUTE
9050 if (call_ABI_of_interest (cfun->decl))
9051 rs6000_passes_float = true;
9052 #endif
9055 /* Find the overflow area. */
9056 t = make_tree (TREE_TYPE (ovf), virtual_incoming_args_rtx);
9057 if (words != 0)
9058 t = fold_build_pointer_plus_hwi (t, words * UNITS_PER_WORD);
9059 t = build2 (MODIFY_EXPR, TREE_TYPE (ovf), ovf, t);
9060 TREE_SIDE_EFFECTS (t) = 1;
9061 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
9063 /* If there were no va_arg invocations, don't set up the register
9064 save area. */
9065 if (!cfun->va_list_gpr_size
9066 && !cfun->va_list_fpr_size
9067 && n_gpr < GP_ARG_NUM_REG
9068 && n_fpr < FP_ARG_V4_MAX_REG)
9069 return;
9071 /* Find the register save area. */
9072 t = make_tree (TREE_TYPE (sav), virtual_stack_vars_rtx);
9073 if (cfun->machine->varargs_save_offset)
9074 t = fold_build_pointer_plus_hwi (t, cfun->machine->varargs_save_offset);
9075 t = build2 (MODIFY_EXPR, TREE_TYPE (sav), sav, t);
9076 TREE_SIDE_EFFECTS (t) = 1;
9077 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
9080 /* Implement va_arg. */
9082 static tree
9083 rs6000_gimplify_va_arg (tree valist, tree type, gimple_seq *pre_p,
9084 gimple_seq *post_p)
9086 tree f_gpr, f_fpr, f_res, f_ovf, f_sav;
9087 tree gpr, fpr, ovf, sav, reg, t, u;
9088 int size, rsize, n_reg, sav_ofs, sav_scale;
9089 tree lab_false, lab_over, addr;
9090 int align;
9091 tree ptrtype = build_pointer_type_for_mode (type, ptr_mode, true);
9092 int regalign = 0;
9093 gimple stmt;
9095 if (pass_by_reference (NULL, TYPE_MODE (type), type, false))
9097 t = rs6000_gimplify_va_arg (valist, ptrtype, pre_p, post_p);
9098 return build_va_arg_indirect_ref (t);
9101 /* We need to deal with the fact that the darwin ppc64 ABI is defined by an
9102 earlier version of gcc, with the property that it always applied alignment
9103 adjustments to the va-args (even for zero-sized types). The cheapest way
9104 to deal with this is to replicate the effect of the part of
9105 std_gimplify_va_arg_expr that carries out the align adjust, for the case
9106 of relevance.
9107 We don't need to check for pass-by-reference because of the test above.
9108 We can return a simplifed answer, since we know there's no offset to add. */
9110 if (TARGET_MACHO
9111 && rs6000_darwin64_abi
9112 && integer_zerop (TYPE_SIZE (type)))
9114 unsigned HOST_WIDE_INT align, boundary;
9115 tree valist_tmp = get_initialized_tmp_var (valist, pre_p, NULL);
9116 align = PARM_BOUNDARY / BITS_PER_UNIT;
9117 boundary = rs6000_function_arg_boundary (TYPE_MODE (type), type);
9118 if (boundary > MAX_SUPPORTED_STACK_ALIGNMENT)
9119 boundary = MAX_SUPPORTED_STACK_ALIGNMENT;
9120 boundary /= BITS_PER_UNIT;
9121 if (boundary > align)
9123 tree t ;
9124 /* This updates arg ptr by the amount that would be necessary
9125 to align the zero-sized (but not zero-alignment) item. */
9126 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
9127 fold_build_pointer_plus_hwi (valist_tmp, boundary - 1));
9128 gimplify_and_add (t, pre_p);
9130 t = fold_convert (sizetype, valist_tmp);
9131 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
9132 fold_convert (TREE_TYPE (valist),
9133 fold_build2 (BIT_AND_EXPR, sizetype, t,
9134 size_int (-boundary))));
9135 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist, t);
9136 gimplify_and_add (t, pre_p);
9138 /* Since it is zero-sized there's no increment for the item itself. */
9139 valist_tmp = fold_convert (build_pointer_type (type), valist_tmp);
9140 return build_va_arg_indirect_ref (valist_tmp);
9143 if (DEFAULT_ABI != ABI_V4)
9145 if (targetm.calls.split_complex_arg && TREE_CODE (type) == COMPLEX_TYPE)
9147 tree elem_type = TREE_TYPE (type);
9148 enum machine_mode elem_mode = TYPE_MODE (elem_type);
9149 int elem_size = GET_MODE_SIZE (elem_mode);
9151 if (elem_size < UNITS_PER_WORD)
9153 tree real_part, imag_part;
9154 gimple_seq post = NULL;
9156 real_part = rs6000_gimplify_va_arg (valist, elem_type, pre_p,
9157 &post);
9158 /* Copy the value into a temporary, lest the formal temporary
9159 be reused out from under us. */
9160 real_part = get_initialized_tmp_var (real_part, pre_p, &post);
9161 gimple_seq_add_seq (pre_p, post);
9163 imag_part = rs6000_gimplify_va_arg (valist, elem_type, pre_p,
9164 post_p);
9166 return build2 (COMPLEX_EXPR, type, real_part, imag_part);
9170 return std_gimplify_va_arg_expr (valist, type, pre_p, post_p);
9173 f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
9174 f_fpr = DECL_CHAIN (f_gpr);
9175 f_res = DECL_CHAIN (f_fpr);
9176 f_ovf = DECL_CHAIN (f_res);
9177 f_sav = DECL_CHAIN (f_ovf);
9179 valist = build_va_arg_indirect_ref (valist);
9180 gpr = build3 (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr, NULL_TREE);
9181 fpr = build3 (COMPONENT_REF, TREE_TYPE (f_fpr), unshare_expr (valist),
9182 f_fpr, NULL_TREE);
9183 ovf = build3 (COMPONENT_REF, TREE_TYPE (f_ovf), unshare_expr (valist),
9184 f_ovf, NULL_TREE);
9185 sav = build3 (COMPONENT_REF, TREE_TYPE (f_sav), unshare_expr (valist),
9186 f_sav, NULL_TREE);
9188 size = int_size_in_bytes (type);
9189 rsize = (size + 3) / 4;
9190 align = 1;
9192 if (TARGET_HARD_FLOAT && TARGET_FPRS
9193 && ((TARGET_SINGLE_FLOAT && TYPE_MODE (type) == SFmode)
9194 || (TARGET_DOUBLE_FLOAT
9195 && (TYPE_MODE (type) == DFmode
9196 || TYPE_MODE (type) == TFmode
9197 || TYPE_MODE (type) == SDmode
9198 || TYPE_MODE (type) == DDmode
9199 || TYPE_MODE (type) == TDmode))))
9201 /* FP args go in FP registers, if present. */
9202 reg = fpr;
9203 n_reg = (size + 7) / 8;
9204 sav_ofs = ((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT) ? 8 : 4) * 4;
9205 sav_scale = ((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT) ? 8 : 4);
9206 if (TYPE_MODE (type) != SFmode && TYPE_MODE (type) != SDmode)
9207 align = 8;
9209 else
9211 /* Otherwise into GP registers. */
9212 reg = gpr;
9213 n_reg = rsize;
9214 sav_ofs = 0;
9215 sav_scale = 4;
9216 if (n_reg == 2)
9217 align = 8;
9220 /* Pull the value out of the saved registers.... */
9222 lab_over = NULL;
9223 addr = create_tmp_var (ptr_type_node, "addr");
9225 /* AltiVec vectors never go in registers when -mabi=altivec. */
9226 if (TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (TYPE_MODE (type)))
9227 align = 16;
9228 else
9230 lab_false = create_artificial_label (input_location);
9231 lab_over = create_artificial_label (input_location);
9233 /* Long long and SPE vectors are aligned in the registers.
9234 As are any other 2 gpr item such as complex int due to a
9235 historical mistake. */
9236 u = reg;
9237 if (n_reg == 2 && reg == gpr)
9239 regalign = 1;
9240 u = build2 (BIT_AND_EXPR, TREE_TYPE (reg), unshare_expr (reg),
9241 build_int_cst (TREE_TYPE (reg), n_reg - 1));
9242 u = build2 (POSTINCREMENT_EXPR, TREE_TYPE (reg),
9243 unshare_expr (reg), u);
9245 /* _Decimal128 is passed in even/odd fpr pairs; the stored
9246 reg number is 0 for f1, so we want to make it odd. */
9247 else if (reg == fpr && TYPE_MODE (type) == TDmode)
9249 t = build2 (BIT_IOR_EXPR, TREE_TYPE (reg), unshare_expr (reg),
9250 build_int_cst (TREE_TYPE (reg), 1));
9251 u = build2 (MODIFY_EXPR, void_type_node, unshare_expr (reg), t);
9254 t = fold_convert (TREE_TYPE (reg), size_int (8 - n_reg + 1));
9255 t = build2 (GE_EXPR, boolean_type_node, u, t);
9256 u = build1 (GOTO_EXPR, void_type_node, lab_false);
9257 t = build3 (COND_EXPR, void_type_node, t, u, NULL_TREE);
9258 gimplify_and_add (t, pre_p);
9260 t = sav;
9261 if (sav_ofs)
9262 t = fold_build_pointer_plus_hwi (sav, sav_ofs);
9264 u = build2 (POSTINCREMENT_EXPR, TREE_TYPE (reg), unshare_expr (reg),
9265 build_int_cst (TREE_TYPE (reg), n_reg));
9266 u = fold_convert (sizetype, u);
9267 u = build2 (MULT_EXPR, sizetype, u, size_int (sav_scale));
9268 t = fold_build_pointer_plus (t, u);
9270 /* _Decimal32 varargs are located in the second word of the 64-bit
9271 FP register for 32-bit binaries. */
9272 if (!TARGET_POWERPC64
9273 && TARGET_HARD_FLOAT && TARGET_FPRS
9274 && TYPE_MODE (type) == SDmode)
9275 t = fold_build_pointer_plus_hwi (t, size);
9277 gimplify_assign (addr, t, pre_p);
9279 gimple_seq_add_stmt (pre_p, gimple_build_goto (lab_over));
9281 stmt = gimple_build_label (lab_false);
9282 gimple_seq_add_stmt (pre_p, stmt);
9284 if ((n_reg == 2 && !regalign) || n_reg > 2)
9286 /* Ensure that we don't find any more args in regs.
9287 Alignment has taken care of for special cases. */
9288 gimplify_assign (reg, build_int_cst (TREE_TYPE (reg), 8), pre_p);
9292 /* ... otherwise out of the overflow area. */
9294 /* Care for on-stack alignment if needed. */
9295 t = ovf;
9296 if (align != 1)
9298 t = fold_build_pointer_plus_hwi (t, align - 1);
9299 t = build2 (BIT_AND_EXPR, TREE_TYPE (t), t,
9300 build_int_cst (TREE_TYPE (t), -align));
9302 gimplify_expr (&t, pre_p, NULL, is_gimple_val, fb_rvalue);
9304 gimplify_assign (unshare_expr (addr), t, pre_p);
9306 t = fold_build_pointer_plus_hwi (t, size);
9307 gimplify_assign (unshare_expr (ovf), t, pre_p);
9309 if (lab_over)
9311 stmt = gimple_build_label (lab_over);
9312 gimple_seq_add_stmt (pre_p, stmt);
9315 if (STRICT_ALIGNMENT
9316 && (TYPE_ALIGN (type)
9317 > (unsigned) BITS_PER_UNIT * (align < 4 ? 4 : align)))
9319 /* The value (of type complex double, for example) may not be
9320 aligned in memory in the saved registers, so copy via a
9321 temporary. (This is the same code as used for SPARC.) */
9322 tree tmp = create_tmp_var (type, "va_arg_tmp");
9323 tree dest_addr = build_fold_addr_expr (tmp);
9325 tree copy = build_call_expr (builtin_decl_implicit (BUILT_IN_MEMCPY),
9326 3, dest_addr, addr, size_int (rsize * 4));
9328 gimplify_and_add (copy, pre_p);
9329 addr = dest_addr;
9332 addr = fold_convert (ptrtype, addr);
9333 return build_va_arg_indirect_ref (addr);
9336 /* Builtins. */
9338 static void
9339 def_builtin (const char *name, tree type, enum rs6000_builtins code)
9341 tree t;
9342 unsigned classify = rs6000_builtin_info[(int)code].attr;
9343 const char *attr_string = "";
9345 gcc_assert (name != NULL);
9346 gcc_assert (IN_RANGE ((int)code, 0, (int)RS6000_BUILTIN_COUNT));
9348 if (rs6000_builtin_decls[(int)code])
9349 fatal_error ("internal error: builtin function %s already processed", name);
9351 rs6000_builtin_decls[(int)code] = t =
9352 add_builtin_function (name, type, (int)code, BUILT_IN_MD, NULL, NULL_TREE);
9354 /* Set any special attributes. */
9355 if ((classify & RS6000_BTC_CONST) != 0)
9357 /* const function, function only depends on the inputs. */
9358 TREE_READONLY (t) = 1;
9359 TREE_NOTHROW (t) = 1;
9360 attr_string = ", pure";
9362 else if ((classify & RS6000_BTC_PURE) != 0)
9364 /* pure function, function can read global memory, but does not set any
9365 external state. */
9366 DECL_PURE_P (t) = 1;
9367 TREE_NOTHROW (t) = 1;
9368 attr_string = ", const";
9370 else if ((classify & RS6000_BTC_FP) != 0)
9372 /* Function is a math function. If rounding mode is on, then treat the
9373 function as not reading global memory, but it can have arbitrary side
9374 effects. If it is off, then assume the function is a const function.
9375 This mimics the ATTR_MATHFN_FPROUNDING attribute in
9376 builtin-attribute.def that is used for the math functions. */
9377 TREE_NOTHROW (t) = 1;
9378 if (flag_rounding_math)
9380 DECL_PURE_P (t) = 1;
9381 DECL_IS_NOVOPS (t) = 1;
9382 attr_string = ", fp, pure";
9384 else
9386 TREE_READONLY (t) = 1;
9387 attr_string = ", fp, const";
9390 else if ((classify & RS6000_BTC_ATTR_MASK) != 0)
9391 gcc_unreachable ();
9393 if (TARGET_DEBUG_BUILTIN)
9394 fprintf (stderr, "rs6000_builtin, code = %4d, %s%s\n",
9395 (int)code, name, attr_string);
9398 /* Simple ternary operations: VECd = foo (VECa, VECb, VECc). */
9400 #undef RS6000_BUILTIN_1
9401 #undef RS6000_BUILTIN_2
9402 #undef RS6000_BUILTIN_3
9403 #undef RS6000_BUILTIN_A
9404 #undef RS6000_BUILTIN_D
9405 #undef RS6000_BUILTIN_E
9406 #undef RS6000_BUILTIN_P
9407 #undef RS6000_BUILTIN_Q
9408 #undef RS6000_BUILTIN_S
9409 #undef RS6000_BUILTIN_X
9411 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
9412 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
9413 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE) \
9414 { MASK, ICODE, NAME, ENUM },
9416 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
9417 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
9418 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
9419 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
9420 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
9421 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
9422 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
9424 static const struct builtin_description bdesc_3arg[] =
9426 #include "rs6000-builtin.def"
9429 /* DST operations: void foo (void *, const int, const char). */
9431 #undef RS6000_BUILTIN_1
9432 #undef RS6000_BUILTIN_2
9433 #undef RS6000_BUILTIN_3
9434 #undef RS6000_BUILTIN_A
9435 #undef RS6000_BUILTIN_D
9436 #undef RS6000_BUILTIN_E
9437 #undef RS6000_BUILTIN_P
9438 #undef RS6000_BUILTIN_Q
9439 #undef RS6000_BUILTIN_S
9440 #undef RS6000_BUILTIN_X
9442 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
9443 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
9444 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
9445 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
9446 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE) \
9447 { MASK, ICODE, NAME, ENUM },
9449 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
9450 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
9451 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
9452 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
9453 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
9455 static const struct builtin_description bdesc_dst[] =
9457 #include "rs6000-builtin.def"
9460 /* Simple binary operations: VECc = foo (VECa, VECb). */
9462 #undef RS6000_BUILTIN_1
9463 #undef RS6000_BUILTIN_2
9464 #undef RS6000_BUILTIN_3
9465 #undef RS6000_BUILTIN_A
9466 #undef RS6000_BUILTIN_D
9467 #undef RS6000_BUILTIN_E
9468 #undef RS6000_BUILTIN_P
9469 #undef RS6000_BUILTIN_Q
9470 #undef RS6000_BUILTIN_S
9471 #undef RS6000_BUILTIN_X
9473 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
9474 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE) \
9475 { MASK, ICODE, NAME, ENUM },
9477 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
9478 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
9479 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
9480 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
9481 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
9482 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
9483 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
9484 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
9486 static const struct builtin_description bdesc_2arg[] =
9488 #include "rs6000-builtin.def"
9491 #undef RS6000_BUILTIN_1
9492 #undef RS6000_BUILTIN_2
9493 #undef RS6000_BUILTIN_3
9494 #undef RS6000_BUILTIN_A
9495 #undef RS6000_BUILTIN_D
9496 #undef RS6000_BUILTIN_E
9497 #undef RS6000_BUILTIN_P
9498 #undef RS6000_BUILTIN_Q
9499 #undef RS6000_BUILTIN_S
9500 #undef RS6000_BUILTIN_X
9502 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
9503 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
9504 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
9505 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
9506 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
9507 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
9508 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE) \
9509 { MASK, ICODE, NAME, ENUM },
9511 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
9512 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
9513 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
9515 /* AltiVec predicates. */
9517 static const struct builtin_description bdesc_altivec_preds[] =
9519 #include "rs6000-builtin.def"
9522 /* SPE predicates. */
9523 #undef RS6000_BUILTIN_1
9524 #undef RS6000_BUILTIN_2
9525 #undef RS6000_BUILTIN_3
9526 #undef RS6000_BUILTIN_A
9527 #undef RS6000_BUILTIN_D
9528 #undef RS6000_BUILTIN_E
9529 #undef RS6000_BUILTIN_P
9530 #undef RS6000_BUILTIN_Q
9531 #undef RS6000_BUILTIN_S
9532 #undef RS6000_BUILTIN_X
9534 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
9535 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
9536 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
9537 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
9538 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
9539 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
9540 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
9541 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
9542 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE) \
9543 { MASK, ICODE, NAME, ENUM },
9545 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
9547 static const struct builtin_description bdesc_spe_predicates[] =
9549 #include "rs6000-builtin.def"
9552 /* SPE evsel predicates. */
9553 #undef RS6000_BUILTIN_1
9554 #undef RS6000_BUILTIN_2
9555 #undef RS6000_BUILTIN_3
9556 #undef RS6000_BUILTIN_A
9557 #undef RS6000_BUILTIN_D
9558 #undef RS6000_BUILTIN_E
9559 #undef RS6000_BUILTIN_P
9560 #undef RS6000_BUILTIN_Q
9561 #undef RS6000_BUILTIN_S
9562 #undef RS6000_BUILTIN_X
9564 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
9565 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
9566 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
9567 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
9568 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
9569 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE) \
9570 { MASK, ICODE, NAME, ENUM },
9572 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
9573 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
9574 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
9575 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
9577 static const struct builtin_description bdesc_spe_evsel[] =
9579 #include "rs6000-builtin.def"
9582 /* PAIRED predicates. */
9583 #undef RS6000_BUILTIN_1
9584 #undef RS6000_BUILTIN_2
9585 #undef RS6000_BUILTIN_3
9586 #undef RS6000_BUILTIN_A
9587 #undef RS6000_BUILTIN_D
9588 #undef RS6000_BUILTIN_E
9589 #undef RS6000_BUILTIN_P
9590 #undef RS6000_BUILTIN_Q
9591 #undef RS6000_BUILTIN_S
9592 #undef RS6000_BUILTIN_X
9594 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
9595 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
9596 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
9597 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
9598 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
9599 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
9600 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
9601 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE) \
9602 { MASK, ICODE, NAME, ENUM },
9604 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
9605 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
9607 static const struct builtin_description bdesc_paired_preds[] =
9609 #include "rs6000-builtin.def"
9612 /* ABS* operations. */
9614 #undef RS6000_BUILTIN_1
9615 #undef RS6000_BUILTIN_2
9616 #undef RS6000_BUILTIN_3
9617 #undef RS6000_BUILTIN_A
9618 #undef RS6000_BUILTIN_D
9619 #undef RS6000_BUILTIN_E
9620 #undef RS6000_BUILTIN_P
9621 #undef RS6000_BUILTIN_Q
9622 #undef RS6000_BUILTIN_S
9623 #undef RS6000_BUILTIN_X
9625 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
9626 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
9627 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
9628 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE) \
9629 { MASK, ICODE, NAME, ENUM },
9631 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
9632 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
9633 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
9634 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
9635 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
9636 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
9638 static const struct builtin_description bdesc_abs[] =
9640 #include "rs6000-builtin.def"
9643 /* Simple unary operations: VECb = foo (unsigned literal) or VECb =
9644 foo (VECa). */
9646 #undef RS6000_BUILTIN_1
9647 #undef RS6000_BUILTIN_2
9648 #undef RS6000_BUILTIN_3
9649 #undef RS6000_BUILTIN_A
9650 #undef RS6000_BUILTIN_E
9651 #undef RS6000_BUILTIN_D
9652 #undef RS6000_BUILTIN_P
9653 #undef RS6000_BUILTIN_Q
9654 #undef RS6000_BUILTIN_S
9655 #undef RS6000_BUILTIN_X
9657 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE) \
9658 { MASK, ICODE, NAME, ENUM },
9660 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
9661 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
9662 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
9663 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
9664 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
9665 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
9666 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
9667 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
9668 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
9670 static const struct builtin_description bdesc_1arg[] =
9672 #include "rs6000-builtin.def"
9675 #undef RS6000_BUILTIN_1
9676 #undef RS6000_BUILTIN_2
9677 #undef RS6000_BUILTIN_3
9678 #undef RS6000_BUILTIN_A
9679 #undef RS6000_BUILTIN_D
9680 #undef RS6000_BUILTIN_E
9681 #undef RS6000_BUILTIN_P
9682 #undef RS6000_BUILTIN_Q
9683 #undef RS6000_BUILTIN_S
9684 #undef RS6000_BUILTIN_X
9686 /* Return true if a builtin function is overloaded. */
9687 bool
9688 rs6000_overloaded_builtin_p (enum rs6000_builtins fncode)
9690 return (rs6000_builtin_info[(int)fncode].attr & RS6000_BTC_OVERLOADED) != 0;
9694 static rtx
9695 rs6000_expand_unop_builtin (enum insn_code icode, tree exp, rtx target)
9697 rtx pat;
9698 tree arg0 = CALL_EXPR_ARG (exp, 0);
9699 rtx op0 = expand_normal (arg0);
9700 enum machine_mode tmode = insn_data[icode].operand[0].mode;
9701 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
9703 if (icode == CODE_FOR_nothing)
9704 /* Builtin not supported on this processor. */
9705 return 0;
9707 /* If we got invalid arguments bail out before generating bad rtl. */
9708 if (arg0 == error_mark_node)
9709 return const0_rtx;
9711 if (icode == CODE_FOR_altivec_vspltisb
9712 || icode == CODE_FOR_altivec_vspltish
9713 || icode == CODE_FOR_altivec_vspltisw
9714 || icode == CODE_FOR_spe_evsplatfi
9715 || icode == CODE_FOR_spe_evsplati)
9717 /* Only allow 5-bit *signed* literals. */
9718 if (GET_CODE (op0) != CONST_INT
9719 || INTVAL (op0) > 15
9720 || INTVAL (op0) < -16)
9722 error ("argument 1 must be a 5-bit signed literal");
9723 return const0_rtx;
9727 if (target == 0
9728 || GET_MODE (target) != tmode
9729 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
9730 target = gen_reg_rtx (tmode);
9732 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
9733 op0 = copy_to_mode_reg (mode0, op0);
9735 pat = GEN_FCN (icode) (target, op0);
9736 if (! pat)
9737 return 0;
9738 emit_insn (pat);
9740 return target;
9743 static rtx
9744 altivec_expand_abs_builtin (enum insn_code icode, tree exp, rtx target)
9746 rtx pat, scratch1, scratch2;
9747 tree arg0 = CALL_EXPR_ARG (exp, 0);
9748 rtx op0 = expand_normal (arg0);
9749 enum machine_mode tmode = insn_data[icode].operand[0].mode;
9750 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
9752 /* If we have invalid arguments, bail out before generating bad rtl. */
9753 if (arg0 == error_mark_node)
9754 return const0_rtx;
9756 if (target == 0
9757 || GET_MODE (target) != tmode
9758 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
9759 target = gen_reg_rtx (tmode);
9761 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
9762 op0 = copy_to_mode_reg (mode0, op0);
9764 scratch1 = gen_reg_rtx (mode0);
9765 scratch2 = gen_reg_rtx (mode0);
9767 pat = GEN_FCN (icode) (target, op0, scratch1, scratch2);
9768 if (! pat)
9769 return 0;
9770 emit_insn (pat);
9772 return target;
9775 static rtx
9776 rs6000_expand_binop_builtin (enum insn_code icode, tree exp, rtx target)
9778 rtx pat;
9779 tree arg0 = CALL_EXPR_ARG (exp, 0);
9780 tree arg1 = CALL_EXPR_ARG (exp, 1);
9781 rtx op0 = expand_normal (arg0);
9782 rtx op1 = expand_normal (arg1);
9783 enum machine_mode tmode = insn_data[icode].operand[0].mode;
9784 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
9785 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
9787 if (icode == CODE_FOR_nothing)
9788 /* Builtin not supported on this processor. */
9789 return 0;
9791 /* If we got invalid arguments bail out before generating bad rtl. */
9792 if (arg0 == error_mark_node || arg1 == error_mark_node)
9793 return const0_rtx;
9795 if (icode == CODE_FOR_altivec_vcfux
9796 || icode == CODE_FOR_altivec_vcfsx
9797 || icode == CODE_FOR_altivec_vctsxs
9798 || icode == CODE_FOR_altivec_vctuxs
9799 || icode == CODE_FOR_altivec_vspltb
9800 || icode == CODE_FOR_altivec_vsplth
9801 || icode == CODE_FOR_altivec_vspltw
9802 || icode == CODE_FOR_spe_evaddiw
9803 || icode == CODE_FOR_spe_evldd
9804 || icode == CODE_FOR_spe_evldh
9805 || icode == CODE_FOR_spe_evldw
9806 || icode == CODE_FOR_spe_evlhhesplat
9807 || icode == CODE_FOR_spe_evlhhossplat
9808 || icode == CODE_FOR_spe_evlhhousplat
9809 || icode == CODE_FOR_spe_evlwhe
9810 || icode == CODE_FOR_spe_evlwhos
9811 || icode == CODE_FOR_spe_evlwhou
9812 || icode == CODE_FOR_spe_evlwhsplat
9813 || icode == CODE_FOR_spe_evlwwsplat
9814 || icode == CODE_FOR_spe_evrlwi
9815 || icode == CODE_FOR_spe_evslwi
9816 || icode == CODE_FOR_spe_evsrwis
9817 || icode == CODE_FOR_spe_evsubifw
9818 || icode == CODE_FOR_spe_evsrwiu)
9820 /* Only allow 5-bit unsigned literals. */
9821 STRIP_NOPS (arg1);
9822 if (TREE_CODE (arg1) != INTEGER_CST
9823 || TREE_INT_CST_LOW (arg1) & ~0x1f)
9825 error ("argument 2 must be a 5-bit unsigned literal");
9826 return const0_rtx;
9830 if (target == 0
9831 || GET_MODE (target) != tmode
9832 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
9833 target = gen_reg_rtx (tmode);
9835 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
9836 op0 = copy_to_mode_reg (mode0, op0);
9837 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
9838 op1 = copy_to_mode_reg (mode1, op1);
9840 pat = GEN_FCN (icode) (target, op0, op1);
9841 if (! pat)
9842 return 0;
9843 emit_insn (pat);
9845 return target;
9848 static rtx
9849 altivec_expand_predicate_builtin (enum insn_code icode, tree exp, rtx target)
9851 rtx pat, scratch;
9852 tree cr6_form = CALL_EXPR_ARG (exp, 0);
9853 tree arg0 = CALL_EXPR_ARG (exp, 1);
9854 tree arg1 = CALL_EXPR_ARG (exp, 2);
9855 rtx op0 = expand_normal (arg0);
9856 rtx op1 = expand_normal (arg1);
9857 enum machine_mode tmode = SImode;
9858 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
9859 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
9860 int cr6_form_int;
9862 if (TREE_CODE (cr6_form) != INTEGER_CST)
9864 error ("argument 1 of __builtin_altivec_predicate must be a constant");
9865 return const0_rtx;
9867 else
9868 cr6_form_int = TREE_INT_CST_LOW (cr6_form);
9870 gcc_assert (mode0 == mode1);
9872 /* If we have invalid arguments, bail out before generating bad rtl. */
9873 if (arg0 == error_mark_node || arg1 == error_mark_node)
9874 return const0_rtx;
9876 if (target == 0
9877 || GET_MODE (target) != tmode
9878 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
9879 target = gen_reg_rtx (tmode);
9881 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
9882 op0 = copy_to_mode_reg (mode0, op0);
9883 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
9884 op1 = copy_to_mode_reg (mode1, op1);
9886 scratch = gen_reg_rtx (mode0);
9888 pat = GEN_FCN (icode) (scratch, op0, op1);
9889 if (! pat)
9890 return 0;
9891 emit_insn (pat);
9893 /* The vec_any* and vec_all* predicates use the same opcodes for two
9894 different operations, but the bits in CR6 will be different
9895 depending on what information we want. So we have to play tricks
9896 with CR6 to get the right bits out.
9898 If you think this is disgusting, look at the specs for the
9899 AltiVec predicates. */
9901 switch (cr6_form_int)
9903 case 0:
9904 emit_insn (gen_cr6_test_for_zero (target));
9905 break;
9906 case 1:
9907 emit_insn (gen_cr6_test_for_zero_reverse (target));
9908 break;
9909 case 2:
9910 emit_insn (gen_cr6_test_for_lt (target));
9911 break;
9912 case 3:
9913 emit_insn (gen_cr6_test_for_lt_reverse (target));
9914 break;
9915 default:
9916 error ("argument 1 of __builtin_altivec_predicate is out of range");
9917 break;
9920 return target;
9923 static rtx
9924 paired_expand_lv_builtin (enum insn_code icode, tree exp, rtx target)
9926 rtx pat, addr;
9927 tree arg0 = CALL_EXPR_ARG (exp, 0);
9928 tree arg1 = CALL_EXPR_ARG (exp, 1);
9929 enum machine_mode tmode = insn_data[icode].operand[0].mode;
9930 enum machine_mode mode0 = Pmode;
9931 enum machine_mode mode1 = Pmode;
9932 rtx op0 = expand_normal (arg0);
9933 rtx op1 = expand_normal (arg1);
9935 if (icode == CODE_FOR_nothing)
9936 /* Builtin not supported on this processor. */
9937 return 0;
9939 /* If we got invalid arguments bail out before generating bad rtl. */
9940 if (arg0 == error_mark_node || arg1 == error_mark_node)
9941 return const0_rtx;
9943 if (target == 0
9944 || GET_MODE (target) != tmode
9945 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
9946 target = gen_reg_rtx (tmode);
9948 op1 = copy_to_mode_reg (mode1, op1);
9950 if (op0 == const0_rtx)
9952 addr = gen_rtx_MEM (tmode, op1);
9954 else
9956 op0 = copy_to_mode_reg (mode0, op0);
9957 addr = gen_rtx_MEM (tmode, gen_rtx_PLUS (Pmode, op0, op1));
9960 pat = GEN_FCN (icode) (target, addr);
9962 if (! pat)
9963 return 0;
9964 emit_insn (pat);
9966 return target;
9969 static rtx
9970 altivec_expand_lv_builtin (enum insn_code icode, tree exp, rtx target, bool blk)
9972 rtx pat, addr;
9973 tree arg0 = CALL_EXPR_ARG (exp, 0);
9974 tree arg1 = CALL_EXPR_ARG (exp, 1);
9975 enum machine_mode tmode = insn_data[icode].operand[0].mode;
9976 enum machine_mode mode0 = Pmode;
9977 enum machine_mode mode1 = Pmode;
9978 rtx op0 = expand_normal (arg0);
9979 rtx op1 = expand_normal (arg1);
9981 if (icode == CODE_FOR_nothing)
9982 /* Builtin not supported on this processor. */
9983 return 0;
9985 /* If we got invalid arguments bail out before generating bad rtl. */
9986 if (arg0 == error_mark_node || arg1 == error_mark_node)
9987 return const0_rtx;
9989 if (target == 0
9990 || GET_MODE (target) != tmode
9991 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
9992 target = gen_reg_rtx (tmode);
9994 op1 = copy_to_mode_reg (mode1, op1);
9996 if (op0 == const0_rtx)
9998 addr = gen_rtx_MEM (blk ? BLKmode : tmode, op1);
10000 else
10002 op0 = copy_to_mode_reg (mode0, op0);
10003 addr = gen_rtx_MEM (blk ? BLKmode : tmode, gen_rtx_PLUS (Pmode, op0, op1));
10006 pat = GEN_FCN (icode) (target, addr);
10008 if (! pat)
10009 return 0;
10010 emit_insn (pat);
10012 return target;
10015 static rtx
10016 spe_expand_stv_builtin (enum insn_code icode, tree exp)
10018 tree arg0 = CALL_EXPR_ARG (exp, 0);
10019 tree arg1 = CALL_EXPR_ARG (exp, 1);
10020 tree arg2 = CALL_EXPR_ARG (exp, 2);
10021 rtx op0 = expand_normal (arg0);
10022 rtx op1 = expand_normal (arg1);
10023 rtx op2 = expand_normal (arg2);
10024 rtx pat;
10025 enum machine_mode mode0 = insn_data[icode].operand[0].mode;
10026 enum machine_mode mode1 = insn_data[icode].operand[1].mode;
10027 enum machine_mode mode2 = insn_data[icode].operand[2].mode;
10029 /* Invalid arguments. Bail before doing anything stoopid! */
10030 if (arg0 == error_mark_node
10031 || arg1 == error_mark_node
10032 || arg2 == error_mark_node)
10033 return const0_rtx;
10035 if (! (*insn_data[icode].operand[2].predicate) (op0, mode2))
10036 op0 = copy_to_mode_reg (mode2, op0);
10037 if (! (*insn_data[icode].operand[0].predicate) (op1, mode0))
10038 op1 = copy_to_mode_reg (mode0, op1);
10039 if (! (*insn_data[icode].operand[1].predicate) (op2, mode1))
10040 op2 = copy_to_mode_reg (mode1, op2);
10042 pat = GEN_FCN (icode) (op1, op2, op0);
10043 if (pat)
10044 emit_insn (pat);
10045 return NULL_RTX;
10048 static rtx
10049 paired_expand_stv_builtin (enum insn_code icode, tree exp)
10051 tree arg0 = CALL_EXPR_ARG (exp, 0);
10052 tree arg1 = CALL_EXPR_ARG (exp, 1);
10053 tree arg2 = CALL_EXPR_ARG (exp, 2);
10054 rtx op0 = expand_normal (arg0);
10055 rtx op1 = expand_normal (arg1);
10056 rtx op2 = expand_normal (arg2);
10057 rtx pat, addr;
10058 enum machine_mode tmode = insn_data[icode].operand[0].mode;
10059 enum machine_mode mode1 = Pmode;
10060 enum machine_mode mode2 = Pmode;
10062 /* Invalid arguments. Bail before doing anything stoopid! */
10063 if (arg0 == error_mark_node
10064 || arg1 == error_mark_node
10065 || arg2 == error_mark_node)
10066 return const0_rtx;
10068 if (! (*insn_data[icode].operand[1].predicate) (op0, tmode))
10069 op0 = copy_to_mode_reg (tmode, op0);
10071 op2 = copy_to_mode_reg (mode2, op2);
10073 if (op1 == const0_rtx)
10075 addr = gen_rtx_MEM (tmode, op2);
10077 else
10079 op1 = copy_to_mode_reg (mode1, op1);
10080 addr = gen_rtx_MEM (tmode, gen_rtx_PLUS (Pmode, op1, op2));
10083 pat = GEN_FCN (icode) (addr, op0);
10084 if (pat)
10085 emit_insn (pat);
10086 return NULL_RTX;
10089 static rtx
10090 altivec_expand_stv_builtin (enum insn_code icode, tree exp)
10092 tree arg0 = CALL_EXPR_ARG (exp, 0);
10093 tree arg1 = CALL_EXPR_ARG (exp, 1);
10094 tree arg2 = CALL_EXPR_ARG (exp, 2);
10095 rtx op0 = expand_normal (arg0);
10096 rtx op1 = expand_normal (arg1);
10097 rtx op2 = expand_normal (arg2);
10098 rtx pat, addr;
10099 enum machine_mode tmode = insn_data[icode].operand[0].mode;
10100 enum machine_mode smode = insn_data[icode].operand[1].mode;
10101 enum machine_mode mode1 = Pmode;
10102 enum machine_mode mode2 = Pmode;
10104 /* Invalid arguments. Bail before doing anything stoopid! */
10105 if (arg0 == error_mark_node
10106 || arg1 == error_mark_node
10107 || arg2 == error_mark_node)
10108 return const0_rtx;
10110 if (! (*insn_data[icode].operand[1].predicate) (op0, smode))
10111 op0 = copy_to_mode_reg (smode, op0);
10113 op2 = copy_to_mode_reg (mode2, op2);
10115 if (op1 == const0_rtx)
10117 addr = gen_rtx_MEM (tmode, op2);
10119 else
10121 op1 = copy_to_mode_reg (mode1, op1);
10122 addr = gen_rtx_MEM (tmode, gen_rtx_PLUS (Pmode, op1, op2));
10125 pat = GEN_FCN (icode) (addr, op0);
10126 if (pat)
10127 emit_insn (pat);
10128 return NULL_RTX;
10131 static rtx
10132 rs6000_expand_ternop_builtin (enum insn_code icode, tree exp, rtx target)
10134 rtx pat;
10135 tree arg0 = CALL_EXPR_ARG (exp, 0);
10136 tree arg1 = CALL_EXPR_ARG (exp, 1);
10137 tree arg2 = CALL_EXPR_ARG (exp, 2);
10138 rtx op0 = expand_normal (arg0);
10139 rtx op1 = expand_normal (arg1);
10140 rtx op2 = expand_normal (arg2);
10141 enum machine_mode tmode = insn_data[icode].operand[0].mode;
10142 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
10143 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
10144 enum machine_mode mode2 = insn_data[icode].operand[3].mode;
10146 if (icode == CODE_FOR_nothing)
10147 /* Builtin not supported on this processor. */
10148 return 0;
10150 /* If we got invalid arguments bail out before generating bad rtl. */
10151 if (arg0 == error_mark_node
10152 || arg1 == error_mark_node
10153 || arg2 == error_mark_node)
10154 return const0_rtx;
10156 /* Check and prepare argument depending on the instruction code.
10158 Note that a switch statement instead of the sequence of tests
10159 would be incorrect as many of the CODE_FOR values could be
10160 CODE_FOR_nothing and that would yield multiple alternatives
10161 with identical values. We'd never reach here at runtime in
10162 this case. */
10163 if (icode == CODE_FOR_altivec_vsldoi_v4sf
10164 || icode == CODE_FOR_altivec_vsldoi_v4si
10165 || icode == CODE_FOR_altivec_vsldoi_v8hi
10166 || icode == CODE_FOR_altivec_vsldoi_v16qi)
10168 /* Only allow 4-bit unsigned literals. */
10169 STRIP_NOPS (arg2);
10170 if (TREE_CODE (arg2) != INTEGER_CST
10171 || TREE_INT_CST_LOW (arg2) & ~0xf)
10173 error ("argument 3 must be a 4-bit unsigned literal");
10174 return const0_rtx;
10177 else if (icode == CODE_FOR_vsx_xxpermdi_v2df
10178 || icode == CODE_FOR_vsx_xxpermdi_v2di
10179 || icode == CODE_FOR_vsx_xxsldwi_v16qi
10180 || icode == CODE_FOR_vsx_xxsldwi_v8hi
10181 || icode == CODE_FOR_vsx_xxsldwi_v4si
10182 || icode == CODE_FOR_vsx_xxsldwi_v4sf
10183 || icode == CODE_FOR_vsx_xxsldwi_v2di
10184 || icode == CODE_FOR_vsx_xxsldwi_v2df)
10186 /* Only allow 2-bit unsigned literals. */
10187 STRIP_NOPS (arg2);
10188 if (TREE_CODE (arg2) != INTEGER_CST
10189 || TREE_INT_CST_LOW (arg2) & ~0x3)
10191 error ("argument 3 must be a 2-bit unsigned literal");
10192 return const0_rtx;
10195 else if (icode == CODE_FOR_vsx_set_v2df
10196 || icode == CODE_FOR_vsx_set_v2di)
10198 /* Only allow 1-bit unsigned literals. */
10199 STRIP_NOPS (arg2);
10200 if (TREE_CODE (arg2) != INTEGER_CST
10201 || TREE_INT_CST_LOW (arg2) & ~0x1)
10203 error ("argument 3 must be a 1-bit unsigned literal");
10204 return const0_rtx;
10208 if (target == 0
10209 || GET_MODE (target) != tmode
10210 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
10211 target = gen_reg_rtx (tmode);
10213 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
10214 op0 = copy_to_mode_reg (mode0, op0);
10215 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
10216 op1 = copy_to_mode_reg (mode1, op1);
10217 if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
10218 op2 = copy_to_mode_reg (mode2, op2);
10220 if (TARGET_PAIRED_FLOAT && icode == CODE_FOR_selv2sf4)
10221 pat = GEN_FCN (icode) (target, op0, op1, op2, CONST0_RTX (SFmode));
10222 else
10223 pat = GEN_FCN (icode) (target, op0, op1, op2);
10224 if (! pat)
10225 return 0;
10226 emit_insn (pat);
10228 return target;
10231 /* Expand the lvx builtins. */
10232 static rtx
10233 altivec_expand_ld_builtin (tree exp, rtx target, bool *expandedp)
10235 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
10236 unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
10237 tree arg0;
10238 enum machine_mode tmode, mode0;
10239 rtx pat, op0;
10240 enum insn_code icode;
10242 switch (fcode)
10244 case ALTIVEC_BUILTIN_LD_INTERNAL_16qi:
10245 icode = CODE_FOR_vector_altivec_load_v16qi;
10246 break;
10247 case ALTIVEC_BUILTIN_LD_INTERNAL_8hi:
10248 icode = CODE_FOR_vector_altivec_load_v8hi;
10249 break;
10250 case ALTIVEC_BUILTIN_LD_INTERNAL_4si:
10251 icode = CODE_FOR_vector_altivec_load_v4si;
10252 break;
10253 case ALTIVEC_BUILTIN_LD_INTERNAL_4sf:
10254 icode = CODE_FOR_vector_altivec_load_v4sf;
10255 break;
10256 case ALTIVEC_BUILTIN_LD_INTERNAL_2df:
10257 icode = CODE_FOR_vector_altivec_load_v2df;
10258 break;
10259 case ALTIVEC_BUILTIN_LD_INTERNAL_2di:
10260 icode = CODE_FOR_vector_altivec_load_v2di;
10261 break;
10262 default:
10263 *expandedp = false;
10264 return NULL_RTX;
10267 *expandedp = true;
10269 arg0 = CALL_EXPR_ARG (exp, 0);
10270 op0 = expand_normal (arg0);
10271 tmode = insn_data[icode].operand[0].mode;
10272 mode0 = insn_data[icode].operand[1].mode;
10274 if (target == 0
10275 || GET_MODE (target) != tmode
10276 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
10277 target = gen_reg_rtx (tmode);
10279 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
10280 op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
10282 pat = GEN_FCN (icode) (target, op0);
10283 if (! pat)
10284 return 0;
10285 emit_insn (pat);
10286 return target;
10289 /* Expand the stvx builtins. */
10290 static rtx
10291 altivec_expand_st_builtin (tree exp, rtx target ATTRIBUTE_UNUSED,
10292 bool *expandedp)
10294 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
10295 unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
10296 tree arg0, arg1;
10297 enum machine_mode mode0, mode1;
10298 rtx pat, op0, op1;
10299 enum insn_code icode;
10301 switch (fcode)
10303 case ALTIVEC_BUILTIN_ST_INTERNAL_16qi:
10304 icode = CODE_FOR_vector_altivec_store_v16qi;
10305 break;
10306 case ALTIVEC_BUILTIN_ST_INTERNAL_8hi:
10307 icode = CODE_FOR_vector_altivec_store_v8hi;
10308 break;
10309 case ALTIVEC_BUILTIN_ST_INTERNAL_4si:
10310 icode = CODE_FOR_vector_altivec_store_v4si;
10311 break;
10312 case ALTIVEC_BUILTIN_ST_INTERNAL_4sf:
10313 icode = CODE_FOR_vector_altivec_store_v4sf;
10314 break;
10315 case ALTIVEC_BUILTIN_ST_INTERNAL_2df:
10316 icode = CODE_FOR_vector_altivec_store_v2df;
10317 break;
10318 case ALTIVEC_BUILTIN_ST_INTERNAL_2di:
10319 icode = CODE_FOR_vector_altivec_store_v2di;
10320 break;
10321 default:
10322 *expandedp = false;
10323 return NULL_RTX;
10326 arg0 = CALL_EXPR_ARG (exp, 0);
10327 arg1 = CALL_EXPR_ARG (exp, 1);
10328 op0 = expand_normal (arg0);
10329 op1 = expand_normal (arg1);
10330 mode0 = insn_data[icode].operand[0].mode;
10331 mode1 = insn_data[icode].operand[1].mode;
10333 if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
10334 op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
10335 if (! (*insn_data[icode].operand[1].predicate) (op1, mode1))
10336 op1 = copy_to_mode_reg (mode1, op1);
10338 pat = GEN_FCN (icode) (op0, op1);
10339 if (pat)
10340 emit_insn (pat);
10342 *expandedp = true;
10343 return NULL_RTX;
10346 /* Expand the dst builtins. */
10347 static rtx
10348 altivec_expand_dst_builtin (tree exp, rtx target ATTRIBUTE_UNUSED,
10349 bool *expandedp)
10351 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
10352 unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
10353 tree arg0, arg1, arg2;
10354 enum machine_mode mode0, mode1;
10355 rtx pat, op0, op1, op2;
10356 const struct builtin_description *d;
10357 size_t i;
10359 *expandedp = false;
10361 /* Handle DST variants. */
10362 d = bdesc_dst;
10363 for (i = 0; i < ARRAY_SIZE (bdesc_dst); i++, d++)
10364 if (d->code == fcode)
10366 arg0 = CALL_EXPR_ARG (exp, 0);
10367 arg1 = CALL_EXPR_ARG (exp, 1);
10368 arg2 = CALL_EXPR_ARG (exp, 2);
10369 op0 = expand_normal (arg0);
10370 op1 = expand_normal (arg1);
10371 op2 = expand_normal (arg2);
10372 mode0 = insn_data[d->icode].operand[0].mode;
10373 mode1 = insn_data[d->icode].operand[1].mode;
10375 /* Invalid arguments, bail out before generating bad rtl. */
10376 if (arg0 == error_mark_node
10377 || arg1 == error_mark_node
10378 || arg2 == error_mark_node)
10379 return const0_rtx;
10381 *expandedp = true;
10382 STRIP_NOPS (arg2);
10383 if (TREE_CODE (arg2) != INTEGER_CST
10384 || TREE_INT_CST_LOW (arg2) & ~0x3)
10386 error ("argument to %qs must be a 2-bit unsigned literal", d->name);
10387 return const0_rtx;
10390 if (! (*insn_data[d->icode].operand[0].predicate) (op0, mode0))
10391 op0 = copy_to_mode_reg (Pmode, op0);
10392 if (! (*insn_data[d->icode].operand[1].predicate) (op1, mode1))
10393 op1 = copy_to_mode_reg (mode1, op1);
10395 pat = GEN_FCN (d->icode) (op0, op1, op2);
10396 if (pat != 0)
10397 emit_insn (pat);
10399 return NULL_RTX;
10402 return NULL_RTX;
10405 /* Expand vec_init builtin. */
10406 static rtx
10407 altivec_expand_vec_init_builtin (tree type, tree exp, rtx target)
10409 enum machine_mode tmode = TYPE_MODE (type);
10410 enum machine_mode inner_mode = GET_MODE_INNER (tmode);
10411 int i, n_elt = GET_MODE_NUNITS (tmode);
10412 rtvec v = rtvec_alloc (n_elt);
10414 gcc_assert (VECTOR_MODE_P (tmode));
10415 gcc_assert (n_elt == call_expr_nargs (exp));
10417 for (i = 0; i < n_elt; ++i)
10419 rtx x = expand_normal (CALL_EXPR_ARG (exp, i));
10420 RTVEC_ELT (v, i) = gen_lowpart (inner_mode, x);
10423 if (!target || !register_operand (target, tmode))
10424 target = gen_reg_rtx (tmode);
10426 rs6000_expand_vector_init (target, gen_rtx_PARALLEL (tmode, v));
10427 return target;
10430 /* Return the integer constant in ARG. Constrain it to be in the range
10431 of the subparts of VEC_TYPE; issue an error if not. */
10433 static int
10434 get_element_number (tree vec_type, tree arg)
10436 unsigned HOST_WIDE_INT elt, max = TYPE_VECTOR_SUBPARTS (vec_type) - 1;
10438 if (!host_integerp (arg, 1)
10439 || (elt = tree_low_cst (arg, 1), elt > max))
10441 error ("selector must be an integer constant in the range 0..%wi", max);
10442 return 0;
10445 return elt;
10448 /* Expand vec_set builtin. */
10449 static rtx
10450 altivec_expand_vec_set_builtin (tree exp)
10452 enum machine_mode tmode, mode1;
10453 tree arg0, arg1, arg2;
10454 int elt;
10455 rtx op0, op1;
10457 arg0 = CALL_EXPR_ARG (exp, 0);
10458 arg1 = CALL_EXPR_ARG (exp, 1);
10459 arg2 = CALL_EXPR_ARG (exp, 2);
10461 tmode = TYPE_MODE (TREE_TYPE (arg0));
10462 mode1 = TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0)));
10463 gcc_assert (VECTOR_MODE_P (tmode));
10465 op0 = expand_expr (arg0, NULL_RTX, tmode, EXPAND_NORMAL);
10466 op1 = expand_expr (arg1, NULL_RTX, mode1, EXPAND_NORMAL);
10467 elt = get_element_number (TREE_TYPE (arg0), arg2);
10469 if (GET_MODE (op1) != mode1 && GET_MODE (op1) != VOIDmode)
10470 op1 = convert_modes (mode1, GET_MODE (op1), op1, true);
10472 op0 = force_reg (tmode, op0);
10473 op1 = force_reg (mode1, op1);
10475 rs6000_expand_vector_set (op0, op1, elt);
10477 return op0;
10480 /* Expand vec_ext builtin. */
10481 static rtx
10482 altivec_expand_vec_ext_builtin (tree exp, rtx target)
10484 enum machine_mode tmode, mode0;
10485 tree arg0, arg1;
10486 int elt;
10487 rtx op0;
10489 arg0 = CALL_EXPR_ARG (exp, 0);
10490 arg1 = CALL_EXPR_ARG (exp, 1);
10492 op0 = expand_normal (arg0);
10493 elt = get_element_number (TREE_TYPE (arg0), arg1);
10495 tmode = TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0)));
10496 mode0 = TYPE_MODE (TREE_TYPE (arg0));
10497 gcc_assert (VECTOR_MODE_P (mode0));
10499 op0 = force_reg (mode0, op0);
10501 if (optimize || !target || !register_operand (target, tmode))
10502 target = gen_reg_rtx (tmode);
10504 rs6000_expand_vector_extract (target, op0, elt);
10506 return target;
10509 /* Expand the builtin in EXP and store the result in TARGET. Store
10510 true in *EXPANDEDP if we found a builtin to expand. */
10511 static rtx
10512 altivec_expand_builtin (tree exp, rtx target, bool *expandedp)
10514 const struct builtin_description *d;
10515 size_t i;
10516 enum insn_code icode;
10517 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
10518 tree arg0;
10519 rtx op0, pat;
10520 enum machine_mode tmode, mode0;
10521 enum rs6000_builtins fcode
10522 = (enum rs6000_builtins) DECL_FUNCTION_CODE (fndecl);
10524 if (rs6000_overloaded_builtin_p (fcode))
10526 *expandedp = true;
10527 error ("unresolved overload for Altivec builtin %qF", fndecl);
10529 /* Given it is invalid, just generate a normal call. */
10530 return expand_call (exp, target, false);
10533 target = altivec_expand_ld_builtin (exp, target, expandedp);
10534 if (*expandedp)
10535 return target;
10537 target = altivec_expand_st_builtin (exp, target, expandedp);
10538 if (*expandedp)
10539 return target;
10541 target = altivec_expand_dst_builtin (exp, target, expandedp);
10542 if (*expandedp)
10543 return target;
10545 *expandedp = true;
10547 switch (fcode)
10549 case ALTIVEC_BUILTIN_STVX:
10550 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvx_v4si, exp);
10551 case ALTIVEC_BUILTIN_STVEBX:
10552 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvebx, exp);
10553 case ALTIVEC_BUILTIN_STVEHX:
10554 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvehx, exp);
10555 case ALTIVEC_BUILTIN_STVEWX:
10556 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvewx, exp);
10557 case ALTIVEC_BUILTIN_STVXL:
10558 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvxl, exp);
10560 case ALTIVEC_BUILTIN_STVLX:
10561 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvlx, exp);
10562 case ALTIVEC_BUILTIN_STVLXL:
10563 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvlxl, exp);
10564 case ALTIVEC_BUILTIN_STVRX:
10565 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvrx, exp);
10566 case ALTIVEC_BUILTIN_STVRXL:
10567 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvrxl, exp);
10569 case VSX_BUILTIN_STXVD2X_V2DF:
10570 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v2df, exp);
10571 case VSX_BUILTIN_STXVD2X_V2DI:
10572 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v2di, exp);
10573 case VSX_BUILTIN_STXVW4X_V4SF:
10574 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v4sf, exp);
10575 case VSX_BUILTIN_STXVW4X_V4SI:
10576 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v4si, exp);
10577 case VSX_BUILTIN_STXVW4X_V8HI:
10578 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v8hi, exp);
10579 case VSX_BUILTIN_STXVW4X_V16QI:
10580 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v16qi, exp);
10582 case ALTIVEC_BUILTIN_MFVSCR:
10583 icode = CODE_FOR_altivec_mfvscr;
10584 tmode = insn_data[icode].operand[0].mode;
10586 if (target == 0
10587 || GET_MODE (target) != tmode
10588 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
10589 target = gen_reg_rtx (tmode);
10591 pat = GEN_FCN (icode) (target);
10592 if (! pat)
10593 return 0;
10594 emit_insn (pat);
10595 return target;
10597 case ALTIVEC_BUILTIN_MTVSCR:
10598 icode = CODE_FOR_altivec_mtvscr;
10599 arg0 = CALL_EXPR_ARG (exp, 0);
10600 op0 = expand_normal (arg0);
10601 mode0 = insn_data[icode].operand[0].mode;
10603 /* If we got invalid arguments bail out before generating bad rtl. */
10604 if (arg0 == error_mark_node)
10605 return const0_rtx;
10607 if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
10608 op0 = copy_to_mode_reg (mode0, op0);
10610 pat = GEN_FCN (icode) (op0);
10611 if (pat)
10612 emit_insn (pat);
10613 return NULL_RTX;
10615 case ALTIVEC_BUILTIN_DSSALL:
10616 emit_insn (gen_altivec_dssall ());
10617 return NULL_RTX;
10619 case ALTIVEC_BUILTIN_DSS:
10620 icode = CODE_FOR_altivec_dss;
10621 arg0 = CALL_EXPR_ARG (exp, 0);
10622 STRIP_NOPS (arg0);
10623 op0 = expand_normal (arg0);
10624 mode0 = insn_data[icode].operand[0].mode;
10626 /* If we got invalid arguments bail out before generating bad rtl. */
10627 if (arg0 == error_mark_node)
10628 return const0_rtx;
10630 if (TREE_CODE (arg0) != INTEGER_CST
10631 || TREE_INT_CST_LOW (arg0) & ~0x3)
10633 error ("argument to dss must be a 2-bit unsigned literal");
10634 return const0_rtx;
10637 if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
10638 op0 = copy_to_mode_reg (mode0, op0);
10640 emit_insn (gen_altivec_dss (op0));
10641 return NULL_RTX;
10643 case ALTIVEC_BUILTIN_VEC_INIT_V4SI:
10644 case ALTIVEC_BUILTIN_VEC_INIT_V8HI:
10645 case ALTIVEC_BUILTIN_VEC_INIT_V16QI:
10646 case ALTIVEC_BUILTIN_VEC_INIT_V4SF:
10647 case VSX_BUILTIN_VEC_INIT_V2DF:
10648 case VSX_BUILTIN_VEC_INIT_V2DI:
10649 return altivec_expand_vec_init_builtin (TREE_TYPE (exp), exp, target);
10651 case ALTIVEC_BUILTIN_VEC_SET_V4SI:
10652 case ALTIVEC_BUILTIN_VEC_SET_V8HI:
10653 case ALTIVEC_BUILTIN_VEC_SET_V16QI:
10654 case ALTIVEC_BUILTIN_VEC_SET_V4SF:
10655 case VSX_BUILTIN_VEC_SET_V2DF:
10656 case VSX_BUILTIN_VEC_SET_V2DI:
10657 return altivec_expand_vec_set_builtin (exp);
10659 case ALTIVEC_BUILTIN_VEC_EXT_V4SI:
10660 case ALTIVEC_BUILTIN_VEC_EXT_V8HI:
10661 case ALTIVEC_BUILTIN_VEC_EXT_V16QI:
10662 case ALTIVEC_BUILTIN_VEC_EXT_V4SF:
10663 case VSX_BUILTIN_VEC_EXT_V2DF:
10664 case VSX_BUILTIN_VEC_EXT_V2DI:
10665 return altivec_expand_vec_ext_builtin (exp, target);
10667 default:
10668 break;
10669 /* Fall through. */
10672 /* Expand abs* operations. */
10673 d = bdesc_abs;
10674 for (i = 0; i < ARRAY_SIZE (bdesc_abs); i++, d++)
10675 if (d->code == fcode)
10676 return altivec_expand_abs_builtin (d->icode, exp, target);
10678 /* Expand the AltiVec predicates. */
10679 d = bdesc_altivec_preds;
10680 for (i = 0; i < ARRAY_SIZE (bdesc_altivec_preds); i++, d++)
10681 if (d->code == fcode)
10682 return altivec_expand_predicate_builtin (d->icode, exp, target);
10684 /* LV* are funky. We initialized them differently. */
10685 switch (fcode)
10687 case ALTIVEC_BUILTIN_LVSL:
10688 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvsl,
10689 exp, target, false);
10690 case ALTIVEC_BUILTIN_LVSR:
10691 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvsr,
10692 exp, target, false);
10693 case ALTIVEC_BUILTIN_LVEBX:
10694 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvebx,
10695 exp, target, false);
10696 case ALTIVEC_BUILTIN_LVEHX:
10697 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvehx,
10698 exp, target, false);
10699 case ALTIVEC_BUILTIN_LVEWX:
10700 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvewx,
10701 exp, target, false);
10702 case ALTIVEC_BUILTIN_LVXL:
10703 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvxl,
10704 exp, target, false);
10705 case ALTIVEC_BUILTIN_LVX:
10706 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvx_v4si,
10707 exp, target, false);
10708 case ALTIVEC_BUILTIN_LVLX:
10709 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvlx,
10710 exp, target, true);
10711 case ALTIVEC_BUILTIN_LVLXL:
10712 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvlxl,
10713 exp, target, true);
10714 case ALTIVEC_BUILTIN_LVRX:
10715 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvrx,
10716 exp, target, true);
10717 case ALTIVEC_BUILTIN_LVRXL:
10718 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvrxl,
10719 exp, target, true);
10720 case VSX_BUILTIN_LXVD2X_V2DF:
10721 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v2df,
10722 exp, target, false);
10723 case VSX_BUILTIN_LXVD2X_V2DI:
10724 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v2di,
10725 exp, target, false);
10726 case VSX_BUILTIN_LXVW4X_V4SF:
10727 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v4sf,
10728 exp, target, false);
10729 case VSX_BUILTIN_LXVW4X_V4SI:
10730 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v4si,
10731 exp, target, false);
10732 case VSX_BUILTIN_LXVW4X_V8HI:
10733 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v8hi,
10734 exp, target, false);
10735 case VSX_BUILTIN_LXVW4X_V16QI:
10736 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v16qi,
10737 exp, target, false);
10738 break;
10739 default:
10740 break;
10741 /* Fall through. */
10744 *expandedp = false;
10745 return NULL_RTX;
10748 /* Expand the builtin in EXP and store the result in TARGET. Store
10749 true in *EXPANDEDP if we found a builtin to expand. */
10750 static rtx
10751 paired_expand_builtin (tree exp, rtx target, bool * expandedp)
10753 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
10754 unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
10755 const struct builtin_description *d;
10756 size_t i;
10758 *expandedp = true;
10760 switch (fcode)
10762 case PAIRED_BUILTIN_STX:
10763 return paired_expand_stv_builtin (CODE_FOR_paired_stx, exp);
10764 case PAIRED_BUILTIN_LX:
10765 return paired_expand_lv_builtin (CODE_FOR_paired_lx, exp, target);
10766 default:
10767 break;
10768 /* Fall through. */
10771 /* Expand the paired predicates. */
10772 d = bdesc_paired_preds;
10773 for (i = 0; i < ARRAY_SIZE (bdesc_paired_preds); i++, d++)
10774 if (d->code == fcode)
10775 return paired_expand_predicate_builtin (d->icode, exp, target);
10777 *expandedp = false;
10778 return NULL_RTX;
10781 /* Binops that need to be initialized manually, but can be expanded
10782 automagically by rs6000_expand_binop_builtin. */
10783 static const struct builtin_description bdesc_2arg_spe[] =
10785 { RS6000_BTM_SPE, CODE_FOR_spe_evlddx, "__builtin_spe_evlddx", SPE_BUILTIN_EVLDDX },
10786 { RS6000_BTM_SPE, CODE_FOR_spe_evldwx, "__builtin_spe_evldwx", SPE_BUILTIN_EVLDWX },
10787 { RS6000_BTM_SPE, CODE_FOR_spe_evldhx, "__builtin_spe_evldhx", SPE_BUILTIN_EVLDHX },
10788 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhex, "__builtin_spe_evlwhex", SPE_BUILTIN_EVLWHEX },
10789 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhoux, "__builtin_spe_evlwhoux", SPE_BUILTIN_EVLWHOUX },
10790 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhosx, "__builtin_spe_evlwhosx", SPE_BUILTIN_EVLWHOSX },
10791 { RS6000_BTM_SPE, CODE_FOR_spe_evlwwsplatx, "__builtin_spe_evlwwsplatx", SPE_BUILTIN_EVLWWSPLATX },
10792 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhsplatx, "__builtin_spe_evlwhsplatx", SPE_BUILTIN_EVLWHSPLATX },
10793 { RS6000_BTM_SPE, CODE_FOR_spe_evlhhesplatx, "__builtin_spe_evlhhesplatx", SPE_BUILTIN_EVLHHESPLATX },
10794 { RS6000_BTM_SPE, CODE_FOR_spe_evlhhousplatx, "__builtin_spe_evlhhousplatx", SPE_BUILTIN_EVLHHOUSPLATX },
10795 { RS6000_BTM_SPE, CODE_FOR_spe_evlhhossplatx, "__builtin_spe_evlhhossplatx", SPE_BUILTIN_EVLHHOSSPLATX },
10796 { RS6000_BTM_SPE, CODE_FOR_spe_evldd, "__builtin_spe_evldd", SPE_BUILTIN_EVLDD },
10797 { RS6000_BTM_SPE, CODE_FOR_spe_evldw, "__builtin_spe_evldw", SPE_BUILTIN_EVLDW },
10798 { RS6000_BTM_SPE, CODE_FOR_spe_evldh, "__builtin_spe_evldh", SPE_BUILTIN_EVLDH },
10799 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhe, "__builtin_spe_evlwhe", SPE_BUILTIN_EVLWHE },
10800 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhou, "__builtin_spe_evlwhou", SPE_BUILTIN_EVLWHOU },
10801 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhos, "__builtin_spe_evlwhos", SPE_BUILTIN_EVLWHOS },
10802 { RS6000_BTM_SPE, CODE_FOR_spe_evlwwsplat, "__builtin_spe_evlwwsplat", SPE_BUILTIN_EVLWWSPLAT },
10803 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhsplat, "__builtin_spe_evlwhsplat", SPE_BUILTIN_EVLWHSPLAT },
10804 { RS6000_BTM_SPE, CODE_FOR_spe_evlhhesplat, "__builtin_spe_evlhhesplat", SPE_BUILTIN_EVLHHESPLAT },
10805 { RS6000_BTM_SPE, CODE_FOR_spe_evlhhousplat, "__builtin_spe_evlhhousplat", SPE_BUILTIN_EVLHHOUSPLAT },
10806 { RS6000_BTM_SPE, CODE_FOR_spe_evlhhossplat, "__builtin_spe_evlhhossplat", SPE_BUILTIN_EVLHHOSSPLAT }
10809 /* Expand the builtin in EXP and store the result in TARGET. Store
10810 true in *EXPANDEDP if we found a builtin to expand.
10812 This expands the SPE builtins that are not simple unary and binary
10813 operations. */
10814 static rtx
10815 spe_expand_builtin (tree exp, rtx target, bool *expandedp)
10817 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
10818 tree arg1, arg0;
10819 unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
10820 enum insn_code icode;
10821 enum machine_mode tmode, mode0;
10822 rtx pat, op0;
10823 const struct builtin_description *d;
10824 size_t i;
10826 *expandedp = true;
10828 /* Syntax check for a 5-bit unsigned immediate. */
10829 switch (fcode)
10831 case SPE_BUILTIN_EVSTDD:
10832 case SPE_BUILTIN_EVSTDH:
10833 case SPE_BUILTIN_EVSTDW:
10834 case SPE_BUILTIN_EVSTWHE:
10835 case SPE_BUILTIN_EVSTWHO:
10836 case SPE_BUILTIN_EVSTWWE:
10837 case SPE_BUILTIN_EVSTWWO:
10838 arg1 = CALL_EXPR_ARG (exp, 2);
10839 if (TREE_CODE (arg1) != INTEGER_CST
10840 || TREE_INT_CST_LOW (arg1) & ~0x1f)
10842 error ("argument 2 must be a 5-bit unsigned literal");
10843 return const0_rtx;
10845 break;
10846 default:
10847 break;
10850 /* The evsplat*i instructions are not quite generic. */
10851 switch (fcode)
10853 case SPE_BUILTIN_EVSPLATFI:
10854 return rs6000_expand_unop_builtin (CODE_FOR_spe_evsplatfi,
10855 exp, target);
10856 case SPE_BUILTIN_EVSPLATI:
10857 return rs6000_expand_unop_builtin (CODE_FOR_spe_evsplati,
10858 exp, target);
10859 default:
10860 break;
10863 d = bdesc_2arg_spe;
10864 for (i = 0; i < ARRAY_SIZE (bdesc_2arg_spe); ++i, ++d)
10865 if (d->code == fcode)
10866 return rs6000_expand_binop_builtin (d->icode, exp, target);
10868 d = bdesc_spe_predicates;
10869 for (i = 0; i < ARRAY_SIZE (bdesc_spe_predicates); ++i, ++d)
10870 if (d->code == fcode)
10871 return spe_expand_predicate_builtin (d->icode, exp, target);
10873 d = bdesc_spe_evsel;
10874 for (i = 0; i < ARRAY_SIZE (bdesc_spe_evsel); ++i, ++d)
10875 if (d->code == fcode)
10876 return spe_expand_evsel_builtin (d->icode, exp, target);
10878 switch (fcode)
10880 case SPE_BUILTIN_EVSTDDX:
10881 return spe_expand_stv_builtin (CODE_FOR_spe_evstddx, exp);
10882 case SPE_BUILTIN_EVSTDHX:
10883 return spe_expand_stv_builtin (CODE_FOR_spe_evstdhx, exp);
10884 case SPE_BUILTIN_EVSTDWX:
10885 return spe_expand_stv_builtin (CODE_FOR_spe_evstdwx, exp);
10886 case SPE_BUILTIN_EVSTWHEX:
10887 return spe_expand_stv_builtin (CODE_FOR_spe_evstwhex, exp);
10888 case SPE_BUILTIN_EVSTWHOX:
10889 return spe_expand_stv_builtin (CODE_FOR_spe_evstwhox, exp);
10890 case SPE_BUILTIN_EVSTWWEX:
10891 return spe_expand_stv_builtin (CODE_FOR_spe_evstwwex, exp);
10892 case SPE_BUILTIN_EVSTWWOX:
10893 return spe_expand_stv_builtin (CODE_FOR_spe_evstwwox, exp);
10894 case SPE_BUILTIN_EVSTDD:
10895 return spe_expand_stv_builtin (CODE_FOR_spe_evstdd, exp);
10896 case SPE_BUILTIN_EVSTDH:
10897 return spe_expand_stv_builtin (CODE_FOR_spe_evstdh, exp);
10898 case SPE_BUILTIN_EVSTDW:
10899 return spe_expand_stv_builtin (CODE_FOR_spe_evstdw, exp);
10900 case SPE_BUILTIN_EVSTWHE:
10901 return spe_expand_stv_builtin (CODE_FOR_spe_evstwhe, exp);
10902 case SPE_BUILTIN_EVSTWHO:
10903 return spe_expand_stv_builtin (CODE_FOR_spe_evstwho, exp);
10904 case SPE_BUILTIN_EVSTWWE:
10905 return spe_expand_stv_builtin (CODE_FOR_spe_evstwwe, exp);
10906 case SPE_BUILTIN_EVSTWWO:
10907 return spe_expand_stv_builtin (CODE_FOR_spe_evstwwo, exp);
10908 case SPE_BUILTIN_MFSPEFSCR:
10909 icode = CODE_FOR_spe_mfspefscr;
10910 tmode = insn_data[icode].operand[0].mode;
10912 if (target == 0
10913 || GET_MODE (target) != tmode
10914 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
10915 target = gen_reg_rtx (tmode);
10917 pat = GEN_FCN (icode) (target);
10918 if (! pat)
10919 return 0;
10920 emit_insn (pat);
10921 return target;
10922 case SPE_BUILTIN_MTSPEFSCR:
10923 icode = CODE_FOR_spe_mtspefscr;
10924 arg0 = CALL_EXPR_ARG (exp, 0);
10925 op0 = expand_normal (arg0);
10926 mode0 = insn_data[icode].operand[0].mode;
10928 if (arg0 == error_mark_node)
10929 return const0_rtx;
10931 if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
10932 op0 = copy_to_mode_reg (mode0, op0);
10934 pat = GEN_FCN (icode) (op0);
10935 if (pat)
10936 emit_insn (pat);
10937 return NULL_RTX;
10938 default:
10939 break;
10942 *expandedp = false;
10943 return NULL_RTX;
10946 static rtx
10947 paired_expand_predicate_builtin (enum insn_code icode, tree exp, rtx target)
10949 rtx pat, scratch, tmp;
10950 tree form = CALL_EXPR_ARG (exp, 0);
10951 tree arg0 = CALL_EXPR_ARG (exp, 1);
10952 tree arg1 = CALL_EXPR_ARG (exp, 2);
10953 rtx op0 = expand_normal (arg0);
10954 rtx op1 = expand_normal (arg1);
10955 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
10956 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
10957 int form_int;
10958 enum rtx_code code;
10960 if (TREE_CODE (form) != INTEGER_CST)
10962 error ("argument 1 of __builtin_paired_predicate must be a constant");
10963 return const0_rtx;
10965 else
10966 form_int = TREE_INT_CST_LOW (form);
10968 gcc_assert (mode0 == mode1);
10970 if (arg0 == error_mark_node || arg1 == error_mark_node)
10971 return const0_rtx;
10973 if (target == 0
10974 || GET_MODE (target) != SImode
10975 || !(*insn_data[icode].operand[0].predicate) (target, SImode))
10976 target = gen_reg_rtx (SImode);
10977 if (!(*insn_data[icode].operand[1].predicate) (op0, mode0))
10978 op0 = copy_to_mode_reg (mode0, op0);
10979 if (!(*insn_data[icode].operand[2].predicate) (op1, mode1))
10980 op1 = copy_to_mode_reg (mode1, op1);
10982 scratch = gen_reg_rtx (CCFPmode);
10984 pat = GEN_FCN (icode) (scratch, op0, op1);
10985 if (!pat)
10986 return const0_rtx;
10988 emit_insn (pat);
10990 switch (form_int)
10992 /* LT bit. */
10993 case 0:
10994 code = LT;
10995 break;
10996 /* GT bit. */
10997 case 1:
10998 code = GT;
10999 break;
11000 /* EQ bit. */
11001 case 2:
11002 code = EQ;
11003 break;
11004 /* UN bit. */
11005 case 3:
11006 emit_insn (gen_move_from_CR_ov_bit (target, scratch));
11007 return target;
11008 default:
11009 error ("argument 1 of __builtin_paired_predicate is out of range");
11010 return const0_rtx;
11013 tmp = gen_rtx_fmt_ee (code, SImode, scratch, const0_rtx);
11014 emit_move_insn (target, tmp);
11015 return target;
11018 static rtx
11019 spe_expand_predicate_builtin (enum insn_code icode, tree exp, rtx target)
11021 rtx pat, scratch, tmp;
11022 tree form = CALL_EXPR_ARG (exp, 0);
11023 tree arg0 = CALL_EXPR_ARG (exp, 1);
11024 tree arg1 = CALL_EXPR_ARG (exp, 2);
11025 rtx op0 = expand_normal (arg0);
11026 rtx op1 = expand_normal (arg1);
11027 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
11028 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
11029 int form_int;
11030 enum rtx_code code;
11032 if (TREE_CODE (form) != INTEGER_CST)
11034 error ("argument 1 of __builtin_spe_predicate must be a constant");
11035 return const0_rtx;
11037 else
11038 form_int = TREE_INT_CST_LOW (form);
11040 gcc_assert (mode0 == mode1);
11042 if (arg0 == error_mark_node || arg1 == error_mark_node)
11043 return const0_rtx;
11045 if (target == 0
11046 || GET_MODE (target) != SImode
11047 || ! (*insn_data[icode].operand[0].predicate) (target, SImode))
11048 target = gen_reg_rtx (SImode);
11050 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
11051 op0 = copy_to_mode_reg (mode0, op0);
11052 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
11053 op1 = copy_to_mode_reg (mode1, op1);
11055 scratch = gen_reg_rtx (CCmode);
11057 pat = GEN_FCN (icode) (scratch, op0, op1);
11058 if (! pat)
11059 return const0_rtx;
11060 emit_insn (pat);
11062 /* There are 4 variants for each predicate: _any_, _all_, _upper_,
11063 _lower_. We use one compare, but look in different bits of the
11064 CR for each variant.
11066 There are 2 elements in each SPE simd type (upper/lower). The CR
11067 bits are set as follows:
11069 BIT0 | BIT 1 | BIT 2 | BIT 3
11070 U | L | (U | L) | (U & L)
11072 So, for an "all" relationship, BIT 3 would be set.
11073 For an "any" relationship, BIT 2 would be set. Etc.
11075 Following traditional nomenclature, these bits map to:
11077 BIT0 | BIT 1 | BIT 2 | BIT 3
11078 LT | GT | EQ | OV
11080 Later, we will generate rtl to look in the LT/EQ/EQ/OV bits.
11083 switch (form_int)
11085 /* All variant. OV bit. */
11086 case 0:
11087 /* We need to get to the OV bit, which is the ORDERED bit. We
11088 could generate (ordered:SI (reg:CC xx) (const_int 0)), but
11089 that's ugly and will make validate_condition_mode die.
11090 So let's just use another pattern. */
11091 emit_insn (gen_move_from_CR_ov_bit (target, scratch));
11092 return target;
11093 /* Any variant. EQ bit. */
11094 case 1:
11095 code = EQ;
11096 break;
11097 /* Upper variant. LT bit. */
11098 case 2:
11099 code = LT;
11100 break;
11101 /* Lower variant. GT bit. */
11102 case 3:
11103 code = GT;
11104 break;
11105 default:
11106 error ("argument 1 of __builtin_spe_predicate is out of range");
11107 return const0_rtx;
11110 tmp = gen_rtx_fmt_ee (code, SImode, scratch, const0_rtx);
11111 emit_move_insn (target, tmp);
11113 return target;
11116 /* The evsel builtins look like this:
11118 e = __builtin_spe_evsel_OP (a, b, c, d);
11120 and work like this:
11122 e[upper] = a[upper] *OP* b[upper] ? c[upper] : d[upper];
11123 e[lower] = a[lower] *OP* b[lower] ? c[lower] : d[lower];
11126 static rtx
11127 spe_expand_evsel_builtin (enum insn_code icode, tree exp, rtx target)
11129 rtx pat, scratch;
11130 tree arg0 = CALL_EXPR_ARG (exp, 0);
11131 tree arg1 = CALL_EXPR_ARG (exp, 1);
11132 tree arg2 = CALL_EXPR_ARG (exp, 2);
11133 tree arg3 = CALL_EXPR_ARG (exp, 3);
11134 rtx op0 = expand_normal (arg0);
11135 rtx op1 = expand_normal (arg1);
11136 rtx op2 = expand_normal (arg2);
11137 rtx op3 = expand_normal (arg3);
11138 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
11139 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
11141 gcc_assert (mode0 == mode1);
11143 if (arg0 == error_mark_node || arg1 == error_mark_node
11144 || arg2 == error_mark_node || arg3 == error_mark_node)
11145 return const0_rtx;
11147 if (target == 0
11148 || GET_MODE (target) != mode0
11149 || ! (*insn_data[icode].operand[0].predicate) (target, mode0))
11150 target = gen_reg_rtx (mode0);
11152 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
11153 op0 = copy_to_mode_reg (mode0, op0);
11154 if (! (*insn_data[icode].operand[1].predicate) (op1, mode1))
11155 op1 = copy_to_mode_reg (mode0, op1);
11156 if (! (*insn_data[icode].operand[1].predicate) (op2, mode1))
11157 op2 = copy_to_mode_reg (mode0, op2);
11158 if (! (*insn_data[icode].operand[1].predicate) (op3, mode1))
11159 op3 = copy_to_mode_reg (mode0, op3);
11161 /* Generate the compare. */
11162 scratch = gen_reg_rtx (CCmode);
11163 pat = GEN_FCN (icode) (scratch, op0, op1);
11164 if (! pat)
11165 return const0_rtx;
11166 emit_insn (pat);
11168 if (mode0 == V2SImode)
11169 emit_insn (gen_spe_evsel (target, op2, op3, scratch));
11170 else
11171 emit_insn (gen_spe_evsel_fs (target, op2, op3, scratch));
11173 return target;
11176 /* Raise an error message for a builtin function that is called without the
11177 appropriate target options being set. */
11179 static void
11180 rs6000_invalid_builtin (enum rs6000_builtins fncode)
11182 size_t uns_fncode = (size_t)fncode;
11183 const char *name = rs6000_builtin_info[uns_fncode].name;
11184 unsigned fnmask = rs6000_builtin_info[uns_fncode].mask;
11186 gcc_assert (name != NULL);
11187 if ((fnmask & RS6000_BTM_CELL) != 0)
11188 error ("Builtin function %s is only valid for the cell processor", name);
11189 else if ((fnmask & RS6000_BTM_VSX) != 0)
11190 error ("Builtin function %s requires the -mvsx option", name);
11191 else if ((fnmask & RS6000_BTM_ALTIVEC) != 0)
11192 error ("Builtin function %s requires the -maltivec option", name);
11193 else if ((fnmask & RS6000_BTM_PAIRED) != 0)
11194 error ("Builtin function %s requires the -mpaired option", name);
11195 else if ((fnmask & RS6000_BTM_SPE) != 0)
11196 error ("Builtin function %s requires the -mspe option", name);
11197 else
11198 error ("Builtin function %s is not supported with the current options",
11199 name);
11202 /* Expand an expression EXP that calls a built-in function,
11203 with result going to TARGET if that's convenient
11204 (and in mode MODE if that's convenient).
11205 SUBTARGET may be used as the target for computing one of EXP's operands.
11206 IGNORE is nonzero if the value is to be ignored. */
11208 static rtx
11209 rs6000_expand_builtin (tree exp, rtx target, rtx subtarget ATTRIBUTE_UNUSED,
11210 enum machine_mode mode ATTRIBUTE_UNUSED,
11211 int ignore ATTRIBUTE_UNUSED)
11213 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
11214 enum rs6000_builtins fcode
11215 = (enum rs6000_builtins)DECL_FUNCTION_CODE (fndecl);
11216 size_t uns_fcode = (size_t)fcode;
11217 const struct builtin_description *d;
11218 size_t i;
11219 rtx ret;
11220 bool success;
11221 unsigned mask = rs6000_builtin_info[uns_fcode].mask;
11222 bool func_valid_p = ((rs6000_builtin_mask & mask) == mask);
11224 if (TARGET_DEBUG_BUILTIN)
11226 enum insn_code icode = rs6000_builtin_info[uns_fcode].icode;
11227 const char *name1 = rs6000_builtin_info[uns_fcode].name;
11228 const char *name2 = ((icode != CODE_FOR_nothing)
11229 ? get_insn_name ((int)icode)
11230 : "nothing");
11231 const char *name3;
11233 switch (rs6000_builtin_info[uns_fcode].attr & RS6000_BTC_TYPE_MASK)
11235 default: name3 = "unknown"; break;
11236 case RS6000_BTC_SPECIAL: name3 = "special"; break;
11237 case RS6000_BTC_UNARY: name3 = "unary"; break;
11238 case RS6000_BTC_BINARY: name3 = "binary"; break;
11239 case RS6000_BTC_TERNARY: name3 = "ternary"; break;
11240 case RS6000_BTC_PREDICATE: name3 = "predicate"; break;
11241 case RS6000_BTC_ABS: name3 = "abs"; break;
11242 case RS6000_BTC_EVSEL: name3 = "evsel"; break;
11243 case RS6000_BTC_DST: name3 = "dst"; break;
11247 fprintf (stderr,
11248 "rs6000_expand_builtin, %s (%d), insn = %s (%d), type=%s%s\n",
11249 (name1) ? name1 : "---", fcode,
11250 (name2) ? name2 : "---", (int)icode,
11251 name3,
11252 func_valid_p ? "" : ", not valid");
11255 if (!func_valid_p)
11257 rs6000_invalid_builtin (fcode);
11259 /* Given it is invalid, just generate a normal call. */
11260 return expand_call (exp, target, ignore);
11263 switch (fcode)
11265 case RS6000_BUILTIN_RECIP:
11266 return rs6000_expand_binop_builtin (CODE_FOR_recipdf3, exp, target);
11268 case RS6000_BUILTIN_RECIPF:
11269 return rs6000_expand_binop_builtin (CODE_FOR_recipsf3, exp, target);
11271 case RS6000_BUILTIN_RSQRTF:
11272 return rs6000_expand_unop_builtin (CODE_FOR_rsqrtsf2, exp, target);
11274 case RS6000_BUILTIN_RSQRT:
11275 return rs6000_expand_unop_builtin (CODE_FOR_rsqrtdf2, exp, target);
11277 case POWER7_BUILTIN_BPERMD:
11278 return rs6000_expand_binop_builtin (((TARGET_64BIT)
11279 ? CODE_FOR_bpermd_di
11280 : CODE_FOR_bpermd_si), exp, target);
11282 case ALTIVEC_BUILTIN_MASK_FOR_LOAD:
11283 case ALTIVEC_BUILTIN_MASK_FOR_STORE:
11285 int icode = (int) CODE_FOR_altivec_lvsr;
11286 enum machine_mode tmode = insn_data[icode].operand[0].mode;
11287 enum machine_mode mode = insn_data[icode].operand[1].mode;
11288 tree arg;
11289 rtx op, addr, pat;
11291 gcc_assert (TARGET_ALTIVEC);
11293 arg = CALL_EXPR_ARG (exp, 0);
11294 gcc_assert (POINTER_TYPE_P (TREE_TYPE (arg)));
11295 op = expand_expr (arg, NULL_RTX, Pmode, EXPAND_NORMAL);
11296 addr = memory_address (mode, op);
11297 if (fcode == ALTIVEC_BUILTIN_MASK_FOR_STORE)
11298 op = addr;
11299 else
11301 /* For the load case need to negate the address. */
11302 op = gen_reg_rtx (GET_MODE (addr));
11303 emit_insn (gen_rtx_SET (VOIDmode, op,
11304 gen_rtx_NEG (GET_MODE (addr), addr)));
11306 op = gen_rtx_MEM (mode, op);
11308 if (target == 0
11309 || GET_MODE (target) != tmode
11310 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
11311 target = gen_reg_rtx (tmode);
11313 /*pat = gen_altivec_lvsr (target, op);*/
11314 pat = GEN_FCN (icode) (target, op);
11315 if (!pat)
11316 return 0;
11317 emit_insn (pat);
11319 return target;
11322 case ALTIVEC_BUILTIN_VCFUX:
11323 case ALTIVEC_BUILTIN_VCFSX:
11324 case ALTIVEC_BUILTIN_VCTUXS:
11325 case ALTIVEC_BUILTIN_VCTSXS:
11326 /* FIXME: There's got to be a nicer way to handle this case than
11327 constructing a new CALL_EXPR. */
11328 if (call_expr_nargs (exp) == 1)
11330 exp = build_call_nary (TREE_TYPE (exp), CALL_EXPR_FN (exp),
11331 2, CALL_EXPR_ARG (exp, 0), integer_zero_node);
11333 break;
11335 default:
11336 break;
11339 if (TARGET_ALTIVEC)
11341 ret = altivec_expand_builtin (exp, target, &success);
11343 if (success)
11344 return ret;
11346 if (TARGET_SPE)
11348 ret = spe_expand_builtin (exp, target, &success);
11350 if (success)
11351 return ret;
11353 if (TARGET_PAIRED_FLOAT)
11355 ret = paired_expand_builtin (exp, target, &success);
11357 if (success)
11358 return ret;
11361 gcc_assert (TARGET_ALTIVEC || TARGET_VSX || TARGET_SPE || TARGET_PAIRED_FLOAT);
11363 /* Handle simple unary operations. */
11364 d = bdesc_1arg;
11365 for (i = 0; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
11366 if (d->code == fcode)
11367 return rs6000_expand_unop_builtin (d->icode, exp, target);
11369 /* Handle simple binary operations. */
11370 d = bdesc_2arg;
11371 for (i = 0; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
11372 if (d->code == fcode)
11373 return rs6000_expand_binop_builtin (d->icode, exp, target);
11375 /* Handle simple ternary operations. */
11376 d = bdesc_3arg;
11377 for (i = 0; i < ARRAY_SIZE (bdesc_3arg); i++, d++)
11378 if (d->code == fcode)
11379 return rs6000_expand_ternop_builtin (d->icode, exp, target);
11381 gcc_unreachable ();
11384 static void
11385 rs6000_init_builtins (void)
11387 tree tdecl;
11388 tree ftype;
11389 enum machine_mode mode;
11391 if (TARGET_DEBUG_BUILTIN)
11392 fprintf (stderr, "rs6000_init_builtins%s%s%s%s\n",
11393 (TARGET_PAIRED_FLOAT) ? ", paired" : "",
11394 (TARGET_SPE) ? ", spe" : "",
11395 (TARGET_ALTIVEC) ? ", altivec" : "",
11396 (TARGET_VSX) ? ", vsx" : "");
11398 V2SI_type_node = build_vector_type (intSI_type_node, 2);
11399 V2SF_type_node = build_vector_type (float_type_node, 2);
11400 V2DI_type_node = build_vector_type (intDI_type_node, 2);
11401 V2DF_type_node = build_vector_type (double_type_node, 2);
11402 V4HI_type_node = build_vector_type (intHI_type_node, 4);
11403 V4SI_type_node = build_vector_type (intSI_type_node, 4);
11404 V4SF_type_node = build_vector_type (float_type_node, 4);
11405 V8HI_type_node = build_vector_type (intHI_type_node, 8);
11406 V16QI_type_node = build_vector_type (intQI_type_node, 16);
11408 unsigned_V16QI_type_node = build_vector_type (unsigned_intQI_type_node, 16);
11409 unsigned_V8HI_type_node = build_vector_type (unsigned_intHI_type_node, 8);
11410 unsigned_V4SI_type_node = build_vector_type (unsigned_intSI_type_node, 4);
11411 unsigned_V2DI_type_node = build_vector_type (unsigned_intDI_type_node, 2);
11413 opaque_V2SF_type_node = build_opaque_vector_type (float_type_node, 2);
11414 opaque_V2SI_type_node = build_opaque_vector_type (intSI_type_node, 2);
11415 opaque_p_V2SI_type_node = build_pointer_type (opaque_V2SI_type_node);
11416 opaque_V4SI_type_node = build_opaque_vector_type (intSI_type_node, 4);
11418 /* The 'vector bool ...' types must be kept distinct from 'vector unsigned ...'
11419 types, especially in C++ land. Similarly, 'vector pixel' is distinct from
11420 'vector unsigned short'. */
11422 bool_char_type_node = build_distinct_type_copy (unsigned_intQI_type_node);
11423 bool_short_type_node = build_distinct_type_copy (unsigned_intHI_type_node);
11424 bool_int_type_node = build_distinct_type_copy (unsigned_intSI_type_node);
11425 bool_long_type_node = build_distinct_type_copy (unsigned_intDI_type_node);
11426 pixel_type_node = build_distinct_type_copy (unsigned_intHI_type_node);
11428 long_integer_type_internal_node = long_integer_type_node;
11429 long_unsigned_type_internal_node = long_unsigned_type_node;
11430 long_long_integer_type_internal_node = long_long_integer_type_node;
11431 long_long_unsigned_type_internal_node = long_long_unsigned_type_node;
11432 intQI_type_internal_node = intQI_type_node;
11433 uintQI_type_internal_node = unsigned_intQI_type_node;
11434 intHI_type_internal_node = intHI_type_node;
11435 uintHI_type_internal_node = unsigned_intHI_type_node;
11436 intSI_type_internal_node = intSI_type_node;
11437 uintSI_type_internal_node = unsigned_intSI_type_node;
11438 intDI_type_internal_node = intDI_type_node;
11439 uintDI_type_internal_node = unsigned_intDI_type_node;
11440 float_type_internal_node = float_type_node;
11441 double_type_internal_node = double_type_node;
11442 void_type_internal_node = void_type_node;
11444 /* Initialize the modes for builtin_function_type, mapping a machine mode to
11445 tree type node. */
11446 builtin_mode_to_type[QImode][0] = integer_type_node;
11447 builtin_mode_to_type[HImode][0] = integer_type_node;
11448 builtin_mode_to_type[SImode][0] = intSI_type_node;
11449 builtin_mode_to_type[SImode][1] = unsigned_intSI_type_node;
11450 builtin_mode_to_type[DImode][0] = intDI_type_node;
11451 builtin_mode_to_type[DImode][1] = unsigned_intDI_type_node;
11452 builtin_mode_to_type[SFmode][0] = float_type_node;
11453 builtin_mode_to_type[DFmode][0] = double_type_node;
11454 builtin_mode_to_type[V2SImode][0] = V2SI_type_node;
11455 builtin_mode_to_type[V2SFmode][0] = V2SF_type_node;
11456 builtin_mode_to_type[V2DImode][0] = V2DI_type_node;
11457 builtin_mode_to_type[V2DImode][1] = unsigned_V2DI_type_node;
11458 builtin_mode_to_type[V2DFmode][0] = V2DF_type_node;
11459 builtin_mode_to_type[V4HImode][0] = V4HI_type_node;
11460 builtin_mode_to_type[V4SImode][0] = V4SI_type_node;
11461 builtin_mode_to_type[V4SImode][1] = unsigned_V4SI_type_node;
11462 builtin_mode_to_type[V4SFmode][0] = V4SF_type_node;
11463 builtin_mode_to_type[V8HImode][0] = V8HI_type_node;
11464 builtin_mode_to_type[V8HImode][1] = unsigned_V8HI_type_node;
11465 builtin_mode_to_type[V16QImode][0] = V16QI_type_node;
11466 builtin_mode_to_type[V16QImode][1] = unsigned_V16QI_type_node;
11468 tdecl = add_builtin_type ("__bool char", bool_char_type_node);
11469 TYPE_NAME (bool_char_type_node) = tdecl;
11471 tdecl = add_builtin_type ("__bool short", bool_short_type_node);
11472 TYPE_NAME (bool_short_type_node) = tdecl;
11474 tdecl = add_builtin_type ("__bool int", bool_int_type_node);
11475 TYPE_NAME (bool_int_type_node) = tdecl;
11477 tdecl = add_builtin_type ("__pixel", pixel_type_node);
11478 TYPE_NAME (pixel_type_node) = tdecl;
11480 bool_V16QI_type_node = build_vector_type (bool_char_type_node, 16);
11481 bool_V8HI_type_node = build_vector_type (bool_short_type_node, 8);
11482 bool_V4SI_type_node = build_vector_type (bool_int_type_node, 4);
11483 bool_V2DI_type_node = build_vector_type (bool_long_type_node, 2);
11484 pixel_V8HI_type_node = build_vector_type (pixel_type_node, 8);
11486 tdecl = add_builtin_type ("__vector unsigned char", unsigned_V16QI_type_node);
11487 TYPE_NAME (unsigned_V16QI_type_node) = tdecl;
11489 tdecl = add_builtin_type ("__vector signed char", V16QI_type_node);
11490 TYPE_NAME (V16QI_type_node) = tdecl;
11492 tdecl = add_builtin_type ("__vector __bool char", bool_V16QI_type_node);
11493 TYPE_NAME ( bool_V16QI_type_node) = tdecl;
11495 tdecl = add_builtin_type ("__vector unsigned short", unsigned_V8HI_type_node);
11496 TYPE_NAME (unsigned_V8HI_type_node) = tdecl;
11498 tdecl = add_builtin_type ("__vector signed short", V8HI_type_node);
11499 TYPE_NAME (V8HI_type_node) = tdecl;
11501 tdecl = add_builtin_type ("__vector __bool short", bool_V8HI_type_node);
11502 TYPE_NAME (bool_V8HI_type_node) = tdecl;
11504 tdecl = add_builtin_type ("__vector unsigned int", unsigned_V4SI_type_node);
11505 TYPE_NAME (unsigned_V4SI_type_node) = tdecl;
11507 tdecl = add_builtin_type ("__vector signed int", V4SI_type_node);
11508 TYPE_NAME (V4SI_type_node) = tdecl;
11510 tdecl = add_builtin_type ("__vector __bool int", bool_V4SI_type_node);
11511 TYPE_NAME (bool_V4SI_type_node) = tdecl;
11513 tdecl = add_builtin_type ("__vector float", V4SF_type_node);
11514 TYPE_NAME (V4SF_type_node) = tdecl;
11516 tdecl = add_builtin_type ("__vector __pixel", pixel_V8HI_type_node);
11517 TYPE_NAME (pixel_V8HI_type_node) = tdecl;
11519 tdecl = add_builtin_type ("__vector double", V2DF_type_node);
11520 TYPE_NAME (V2DF_type_node) = tdecl;
11522 tdecl = add_builtin_type ("__vector long", V2DI_type_node);
11523 TYPE_NAME (V2DI_type_node) = tdecl;
11525 tdecl = add_builtin_type ("__vector unsigned long", unsigned_V2DI_type_node);
11526 TYPE_NAME (unsigned_V2DI_type_node) = tdecl;
11528 tdecl = add_builtin_type ("__vector __bool long", bool_V2DI_type_node);
11529 TYPE_NAME (bool_V2DI_type_node) = tdecl;
11531 /* Paired and SPE builtins are only available if you build a compiler with
11532 the appropriate options, so only create those builtins with the
11533 appropriate compiler option. Create Altivec and VSX builtins on machines
11534 with at least the general purpose extensions (970 and newer) to allow the
11535 use of the target attribute. */
11536 if (TARGET_PAIRED_FLOAT)
11537 paired_init_builtins ();
11538 if (TARGET_SPE)
11539 spe_init_builtins ();
11540 if (TARGET_EXTRA_BUILTINS)
11541 altivec_init_builtins ();
11542 if (TARGET_EXTRA_BUILTINS || TARGET_SPE || TARGET_PAIRED_FLOAT)
11543 rs6000_common_init_builtins ();
11545 ftype = builtin_function_type (DFmode, DFmode, DFmode, VOIDmode,
11546 RS6000_BUILTIN_RECIP, "__builtin_recipdiv");
11547 def_builtin ("__builtin_recipdiv", ftype, RS6000_BUILTIN_RECIP);
11549 ftype = builtin_function_type (SFmode, SFmode, SFmode, VOIDmode,
11550 RS6000_BUILTIN_RECIPF, "__builtin_recipdivf");
11551 def_builtin ("__builtin_recipdivf", ftype, RS6000_BUILTIN_RECIPF);
11553 ftype = builtin_function_type (DFmode, DFmode, VOIDmode, VOIDmode,
11554 RS6000_BUILTIN_RSQRT, "__builtin_rsqrt");
11555 def_builtin ("__builtin_rsqrt", ftype, RS6000_BUILTIN_RSQRT);
11557 ftype = builtin_function_type (SFmode, SFmode, VOIDmode, VOIDmode,
11558 RS6000_BUILTIN_RSQRTF, "__builtin_rsqrtf");
11559 def_builtin ("__builtin_rsqrtf", ftype, RS6000_BUILTIN_RSQRTF);
11561 mode = (TARGET_64BIT) ? DImode : SImode;
11562 ftype = builtin_function_type (mode, mode, mode, VOIDmode,
11563 POWER7_BUILTIN_BPERMD, "__builtin_bpermd");
11564 def_builtin ("__builtin_bpermd", ftype, POWER7_BUILTIN_BPERMD);
11566 #if TARGET_XCOFF
11567 /* AIX libm provides clog as __clog. */
11568 if ((tdecl = builtin_decl_explicit (BUILT_IN_CLOG)) != NULL_TREE)
11569 set_user_assembler_name (tdecl, "__clog");
11570 #endif
11572 #ifdef SUBTARGET_INIT_BUILTINS
11573 SUBTARGET_INIT_BUILTINS;
11574 #endif
11577 /* Returns the rs6000 builtin decl for CODE. */
11579 static tree
11580 rs6000_builtin_decl (unsigned code, bool initialize_p ATTRIBUTE_UNUSED)
11582 unsigned fnmask;
11584 if (code >= RS6000_BUILTIN_COUNT)
11585 return error_mark_node;
11587 fnmask = rs6000_builtin_info[code].mask;
11588 if ((fnmask & rs6000_builtin_mask) != fnmask)
11590 rs6000_invalid_builtin ((enum rs6000_builtins)code);
11591 return error_mark_node;
11594 return rs6000_builtin_decls[code];
11597 static void
11598 spe_init_builtins (void)
11600 tree puint_type_node = build_pointer_type (unsigned_type_node);
11601 tree pushort_type_node = build_pointer_type (short_unsigned_type_node);
11602 const struct builtin_description *d;
11603 size_t i;
11605 tree v2si_ftype_4_v2si
11606 = build_function_type_list (opaque_V2SI_type_node,
11607 opaque_V2SI_type_node,
11608 opaque_V2SI_type_node,
11609 opaque_V2SI_type_node,
11610 opaque_V2SI_type_node,
11611 NULL_TREE);
11613 tree v2sf_ftype_4_v2sf
11614 = build_function_type_list (opaque_V2SF_type_node,
11615 opaque_V2SF_type_node,
11616 opaque_V2SF_type_node,
11617 opaque_V2SF_type_node,
11618 opaque_V2SF_type_node,
11619 NULL_TREE);
11621 tree int_ftype_int_v2si_v2si
11622 = build_function_type_list (integer_type_node,
11623 integer_type_node,
11624 opaque_V2SI_type_node,
11625 opaque_V2SI_type_node,
11626 NULL_TREE);
11628 tree int_ftype_int_v2sf_v2sf
11629 = build_function_type_list (integer_type_node,
11630 integer_type_node,
11631 opaque_V2SF_type_node,
11632 opaque_V2SF_type_node,
11633 NULL_TREE);
11635 tree void_ftype_v2si_puint_int
11636 = build_function_type_list (void_type_node,
11637 opaque_V2SI_type_node,
11638 puint_type_node,
11639 integer_type_node,
11640 NULL_TREE);
11642 tree void_ftype_v2si_puint_char
11643 = build_function_type_list (void_type_node,
11644 opaque_V2SI_type_node,
11645 puint_type_node,
11646 char_type_node,
11647 NULL_TREE);
11649 tree void_ftype_v2si_pv2si_int
11650 = build_function_type_list (void_type_node,
11651 opaque_V2SI_type_node,
11652 opaque_p_V2SI_type_node,
11653 integer_type_node,
11654 NULL_TREE);
11656 tree void_ftype_v2si_pv2si_char
11657 = build_function_type_list (void_type_node,
11658 opaque_V2SI_type_node,
11659 opaque_p_V2SI_type_node,
11660 char_type_node,
11661 NULL_TREE);
11663 tree void_ftype_int
11664 = build_function_type_list (void_type_node, integer_type_node, NULL_TREE);
11666 tree int_ftype_void
11667 = build_function_type_list (integer_type_node, NULL_TREE);
11669 tree v2si_ftype_pv2si_int
11670 = build_function_type_list (opaque_V2SI_type_node,
11671 opaque_p_V2SI_type_node,
11672 integer_type_node,
11673 NULL_TREE);
11675 tree v2si_ftype_puint_int
11676 = build_function_type_list (opaque_V2SI_type_node,
11677 puint_type_node,
11678 integer_type_node,
11679 NULL_TREE);
11681 tree v2si_ftype_pushort_int
11682 = build_function_type_list (opaque_V2SI_type_node,
11683 pushort_type_node,
11684 integer_type_node,
11685 NULL_TREE);
11687 tree v2si_ftype_signed_char
11688 = build_function_type_list (opaque_V2SI_type_node,
11689 signed_char_type_node,
11690 NULL_TREE);
11692 add_builtin_type ("__ev64_opaque__", opaque_V2SI_type_node);
11694 /* Initialize irregular SPE builtins. */
11696 def_builtin ("__builtin_spe_mtspefscr", void_ftype_int, SPE_BUILTIN_MTSPEFSCR);
11697 def_builtin ("__builtin_spe_mfspefscr", int_ftype_void, SPE_BUILTIN_MFSPEFSCR);
11698 def_builtin ("__builtin_spe_evstddx", void_ftype_v2si_pv2si_int, SPE_BUILTIN_EVSTDDX);
11699 def_builtin ("__builtin_spe_evstdhx", void_ftype_v2si_pv2si_int, SPE_BUILTIN_EVSTDHX);
11700 def_builtin ("__builtin_spe_evstdwx", void_ftype_v2si_pv2si_int, SPE_BUILTIN_EVSTDWX);
11701 def_builtin ("__builtin_spe_evstwhex", void_ftype_v2si_puint_int, SPE_BUILTIN_EVSTWHEX);
11702 def_builtin ("__builtin_spe_evstwhox", void_ftype_v2si_puint_int, SPE_BUILTIN_EVSTWHOX);
11703 def_builtin ("__builtin_spe_evstwwex", void_ftype_v2si_puint_int, SPE_BUILTIN_EVSTWWEX);
11704 def_builtin ("__builtin_spe_evstwwox", void_ftype_v2si_puint_int, SPE_BUILTIN_EVSTWWOX);
11705 def_builtin ("__builtin_spe_evstdd", void_ftype_v2si_pv2si_char, SPE_BUILTIN_EVSTDD);
11706 def_builtin ("__builtin_spe_evstdh", void_ftype_v2si_pv2si_char, SPE_BUILTIN_EVSTDH);
11707 def_builtin ("__builtin_spe_evstdw", void_ftype_v2si_pv2si_char, SPE_BUILTIN_EVSTDW);
11708 def_builtin ("__builtin_spe_evstwhe", void_ftype_v2si_puint_char, SPE_BUILTIN_EVSTWHE);
11709 def_builtin ("__builtin_spe_evstwho", void_ftype_v2si_puint_char, SPE_BUILTIN_EVSTWHO);
11710 def_builtin ("__builtin_spe_evstwwe", void_ftype_v2si_puint_char, SPE_BUILTIN_EVSTWWE);
11711 def_builtin ("__builtin_spe_evstwwo", void_ftype_v2si_puint_char, SPE_BUILTIN_EVSTWWO);
11712 def_builtin ("__builtin_spe_evsplatfi", v2si_ftype_signed_char, SPE_BUILTIN_EVSPLATFI);
11713 def_builtin ("__builtin_spe_evsplati", v2si_ftype_signed_char, SPE_BUILTIN_EVSPLATI);
11715 /* Loads. */
11716 def_builtin ("__builtin_spe_evlddx", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDDX);
11717 def_builtin ("__builtin_spe_evldwx", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDWX);
11718 def_builtin ("__builtin_spe_evldhx", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDHX);
11719 def_builtin ("__builtin_spe_evlwhex", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHEX);
11720 def_builtin ("__builtin_spe_evlwhoux", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHOUX);
11721 def_builtin ("__builtin_spe_evlwhosx", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHOSX);
11722 def_builtin ("__builtin_spe_evlwwsplatx", v2si_ftype_puint_int, SPE_BUILTIN_EVLWWSPLATX);
11723 def_builtin ("__builtin_spe_evlwhsplatx", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHSPLATX);
11724 def_builtin ("__builtin_spe_evlhhesplatx", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHESPLATX);
11725 def_builtin ("__builtin_spe_evlhhousplatx", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHOUSPLATX);
11726 def_builtin ("__builtin_spe_evlhhossplatx", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHOSSPLATX);
11727 def_builtin ("__builtin_spe_evldd", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDD);
11728 def_builtin ("__builtin_spe_evldw", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDW);
11729 def_builtin ("__builtin_spe_evldh", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDH);
11730 def_builtin ("__builtin_spe_evlhhesplat", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHESPLAT);
11731 def_builtin ("__builtin_spe_evlhhossplat", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHOSSPLAT);
11732 def_builtin ("__builtin_spe_evlhhousplat", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHOUSPLAT);
11733 def_builtin ("__builtin_spe_evlwhe", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHE);
11734 def_builtin ("__builtin_spe_evlwhos", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHOS);
11735 def_builtin ("__builtin_spe_evlwhou", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHOU);
11736 def_builtin ("__builtin_spe_evlwhsplat", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHSPLAT);
11737 def_builtin ("__builtin_spe_evlwwsplat", v2si_ftype_puint_int, SPE_BUILTIN_EVLWWSPLAT);
11739 /* Predicates. */
11740 d = bdesc_spe_predicates;
11741 for (i = 0; i < ARRAY_SIZE (bdesc_spe_predicates); ++i, d++)
11743 tree type;
11745 switch (insn_data[d->icode].operand[1].mode)
11747 case V2SImode:
11748 type = int_ftype_int_v2si_v2si;
11749 break;
11750 case V2SFmode:
11751 type = int_ftype_int_v2sf_v2sf;
11752 break;
11753 default:
11754 gcc_unreachable ();
11757 def_builtin (d->name, type, d->code);
11760 /* Evsel predicates. */
11761 d = bdesc_spe_evsel;
11762 for (i = 0; i < ARRAY_SIZE (bdesc_spe_evsel); ++i, d++)
11764 tree type;
11766 switch (insn_data[d->icode].operand[1].mode)
11768 case V2SImode:
11769 type = v2si_ftype_4_v2si;
11770 break;
11771 case V2SFmode:
11772 type = v2sf_ftype_4_v2sf;
11773 break;
11774 default:
11775 gcc_unreachable ();
11778 def_builtin (d->name, type, d->code);
11782 static void
11783 paired_init_builtins (void)
11785 const struct builtin_description *d;
11786 size_t i;
11788 tree int_ftype_int_v2sf_v2sf
11789 = build_function_type_list (integer_type_node,
11790 integer_type_node,
11791 V2SF_type_node,
11792 V2SF_type_node,
11793 NULL_TREE);
11794 tree pcfloat_type_node =
11795 build_pointer_type (build_qualified_type
11796 (float_type_node, TYPE_QUAL_CONST));
11798 tree v2sf_ftype_long_pcfloat = build_function_type_list (V2SF_type_node,
11799 long_integer_type_node,
11800 pcfloat_type_node,
11801 NULL_TREE);
11802 tree void_ftype_v2sf_long_pcfloat =
11803 build_function_type_list (void_type_node,
11804 V2SF_type_node,
11805 long_integer_type_node,
11806 pcfloat_type_node,
11807 NULL_TREE);
11810 def_builtin ("__builtin_paired_lx", v2sf_ftype_long_pcfloat,
11811 PAIRED_BUILTIN_LX);
11814 def_builtin ("__builtin_paired_stx", void_ftype_v2sf_long_pcfloat,
11815 PAIRED_BUILTIN_STX);
11817 /* Predicates. */
11818 d = bdesc_paired_preds;
11819 for (i = 0; i < ARRAY_SIZE (bdesc_paired_preds); ++i, d++)
11821 tree type;
11823 if (TARGET_DEBUG_BUILTIN)
11824 fprintf (stderr, "paired pred #%d, insn = %s [%d], mode = %s\n",
11825 (int)i, get_insn_name (d->icode), (int)d->icode,
11826 GET_MODE_NAME (insn_data[d->icode].operand[1].mode));
11828 switch (insn_data[d->icode].operand[1].mode)
11830 case V2SFmode:
11831 type = int_ftype_int_v2sf_v2sf;
11832 break;
11833 default:
11834 gcc_unreachable ();
11837 def_builtin (d->name, type, d->code);
11841 static void
11842 altivec_init_builtins (void)
11844 const struct builtin_description *d;
11845 size_t i;
11846 tree ftype;
11847 tree decl;
11849 tree pvoid_type_node = build_pointer_type (void_type_node);
11851 tree pcvoid_type_node
11852 = build_pointer_type (build_qualified_type (void_type_node,
11853 TYPE_QUAL_CONST));
11855 tree int_ftype_opaque
11856 = build_function_type_list (integer_type_node,
11857 opaque_V4SI_type_node, NULL_TREE);
11858 tree opaque_ftype_opaque
11859 = build_function_type_list (integer_type_node, NULL_TREE);
11860 tree opaque_ftype_opaque_int
11861 = build_function_type_list (opaque_V4SI_type_node,
11862 opaque_V4SI_type_node, integer_type_node, NULL_TREE);
11863 tree opaque_ftype_opaque_opaque_int
11864 = build_function_type_list (opaque_V4SI_type_node,
11865 opaque_V4SI_type_node, opaque_V4SI_type_node,
11866 integer_type_node, NULL_TREE);
11867 tree int_ftype_int_opaque_opaque
11868 = build_function_type_list (integer_type_node,
11869 integer_type_node, opaque_V4SI_type_node,
11870 opaque_V4SI_type_node, NULL_TREE);
11871 tree int_ftype_int_v4si_v4si
11872 = build_function_type_list (integer_type_node,
11873 integer_type_node, V4SI_type_node,
11874 V4SI_type_node, NULL_TREE);
11875 tree void_ftype_v4si
11876 = build_function_type_list (void_type_node, V4SI_type_node, NULL_TREE);
11877 tree v8hi_ftype_void
11878 = build_function_type_list (V8HI_type_node, NULL_TREE);
11879 tree void_ftype_void
11880 = build_function_type_list (void_type_node, NULL_TREE);
11881 tree void_ftype_int
11882 = build_function_type_list (void_type_node, integer_type_node, NULL_TREE);
11884 tree opaque_ftype_long_pcvoid
11885 = build_function_type_list (opaque_V4SI_type_node,
11886 long_integer_type_node, pcvoid_type_node,
11887 NULL_TREE);
11888 tree v16qi_ftype_long_pcvoid
11889 = build_function_type_list (V16QI_type_node,
11890 long_integer_type_node, pcvoid_type_node,
11891 NULL_TREE);
11892 tree v8hi_ftype_long_pcvoid
11893 = build_function_type_list (V8HI_type_node,
11894 long_integer_type_node, pcvoid_type_node,
11895 NULL_TREE);
11896 tree v4si_ftype_long_pcvoid
11897 = build_function_type_list (V4SI_type_node,
11898 long_integer_type_node, pcvoid_type_node,
11899 NULL_TREE);
11900 tree v4sf_ftype_long_pcvoid
11901 = build_function_type_list (V4SF_type_node,
11902 long_integer_type_node, pcvoid_type_node,
11903 NULL_TREE);
11904 tree v2df_ftype_long_pcvoid
11905 = build_function_type_list (V2DF_type_node,
11906 long_integer_type_node, pcvoid_type_node,
11907 NULL_TREE);
11908 tree v2di_ftype_long_pcvoid
11909 = build_function_type_list (V2DI_type_node,
11910 long_integer_type_node, pcvoid_type_node,
11911 NULL_TREE);
11913 tree void_ftype_opaque_long_pvoid
11914 = build_function_type_list (void_type_node,
11915 opaque_V4SI_type_node, long_integer_type_node,
11916 pvoid_type_node, NULL_TREE);
11917 tree void_ftype_v4si_long_pvoid
11918 = build_function_type_list (void_type_node,
11919 V4SI_type_node, long_integer_type_node,
11920 pvoid_type_node, NULL_TREE);
11921 tree void_ftype_v16qi_long_pvoid
11922 = build_function_type_list (void_type_node,
11923 V16QI_type_node, long_integer_type_node,
11924 pvoid_type_node, NULL_TREE);
11925 tree void_ftype_v8hi_long_pvoid
11926 = build_function_type_list (void_type_node,
11927 V8HI_type_node, long_integer_type_node,
11928 pvoid_type_node, NULL_TREE);
11929 tree void_ftype_v4sf_long_pvoid
11930 = build_function_type_list (void_type_node,
11931 V4SF_type_node, long_integer_type_node,
11932 pvoid_type_node, NULL_TREE);
11933 tree void_ftype_v2df_long_pvoid
11934 = build_function_type_list (void_type_node,
11935 V2DF_type_node, long_integer_type_node,
11936 pvoid_type_node, NULL_TREE);
11937 tree void_ftype_v2di_long_pvoid
11938 = build_function_type_list (void_type_node,
11939 V2DI_type_node, long_integer_type_node,
11940 pvoid_type_node, NULL_TREE);
11941 tree int_ftype_int_v8hi_v8hi
11942 = build_function_type_list (integer_type_node,
11943 integer_type_node, V8HI_type_node,
11944 V8HI_type_node, NULL_TREE);
11945 tree int_ftype_int_v16qi_v16qi
11946 = build_function_type_list (integer_type_node,
11947 integer_type_node, V16QI_type_node,
11948 V16QI_type_node, NULL_TREE);
11949 tree int_ftype_int_v4sf_v4sf
11950 = build_function_type_list (integer_type_node,
11951 integer_type_node, V4SF_type_node,
11952 V4SF_type_node, NULL_TREE);
11953 tree int_ftype_int_v2df_v2df
11954 = build_function_type_list (integer_type_node,
11955 integer_type_node, V2DF_type_node,
11956 V2DF_type_node, NULL_TREE);
11957 tree v4si_ftype_v4si
11958 = build_function_type_list (V4SI_type_node, V4SI_type_node, NULL_TREE);
11959 tree v8hi_ftype_v8hi
11960 = build_function_type_list (V8HI_type_node, V8HI_type_node, NULL_TREE);
11961 tree v16qi_ftype_v16qi
11962 = build_function_type_list (V16QI_type_node, V16QI_type_node, NULL_TREE);
11963 tree v4sf_ftype_v4sf
11964 = build_function_type_list (V4SF_type_node, V4SF_type_node, NULL_TREE);
11965 tree v2df_ftype_v2df
11966 = build_function_type_list (V2DF_type_node, V2DF_type_node, NULL_TREE);
11967 tree void_ftype_pcvoid_int_int
11968 = build_function_type_list (void_type_node,
11969 pcvoid_type_node, integer_type_node,
11970 integer_type_node, NULL_TREE);
11972 def_builtin ("__builtin_altivec_mtvscr", void_ftype_v4si, ALTIVEC_BUILTIN_MTVSCR);
11973 def_builtin ("__builtin_altivec_mfvscr", v8hi_ftype_void, ALTIVEC_BUILTIN_MFVSCR);
11974 def_builtin ("__builtin_altivec_dssall", void_ftype_void, ALTIVEC_BUILTIN_DSSALL);
11975 def_builtin ("__builtin_altivec_dss", void_ftype_int, ALTIVEC_BUILTIN_DSS);
11976 def_builtin ("__builtin_altivec_lvsl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVSL);
11977 def_builtin ("__builtin_altivec_lvsr", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVSR);
11978 def_builtin ("__builtin_altivec_lvebx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVEBX);
11979 def_builtin ("__builtin_altivec_lvehx", v8hi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVEHX);
11980 def_builtin ("__builtin_altivec_lvewx", v4si_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVEWX);
11981 def_builtin ("__builtin_altivec_lvxl", v4si_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVXL);
11982 def_builtin ("__builtin_altivec_lvx", v4si_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVX);
11983 def_builtin ("__builtin_altivec_stvx", void_ftype_v4si_long_pvoid, ALTIVEC_BUILTIN_STVX);
11984 def_builtin ("__builtin_altivec_stvewx", void_ftype_v4si_long_pvoid, ALTIVEC_BUILTIN_STVEWX);
11985 def_builtin ("__builtin_altivec_stvxl", void_ftype_v4si_long_pvoid, ALTIVEC_BUILTIN_STVXL);
11986 def_builtin ("__builtin_altivec_stvebx", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_STVEBX);
11987 def_builtin ("__builtin_altivec_stvehx", void_ftype_v8hi_long_pvoid, ALTIVEC_BUILTIN_STVEHX);
11988 def_builtin ("__builtin_vec_ld", opaque_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LD);
11989 def_builtin ("__builtin_vec_lde", opaque_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LDE);
11990 def_builtin ("__builtin_vec_ldl", opaque_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LDL);
11991 def_builtin ("__builtin_vec_lvsl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVSL);
11992 def_builtin ("__builtin_vec_lvsr", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVSR);
11993 def_builtin ("__builtin_vec_lvebx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVEBX);
11994 def_builtin ("__builtin_vec_lvehx", v8hi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVEHX);
11995 def_builtin ("__builtin_vec_lvewx", v4si_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVEWX);
11996 def_builtin ("__builtin_vec_st", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_ST);
11997 def_builtin ("__builtin_vec_ste", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_STE);
11998 def_builtin ("__builtin_vec_stl", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_STL);
11999 def_builtin ("__builtin_vec_stvewx", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_STVEWX);
12000 def_builtin ("__builtin_vec_stvebx", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_STVEBX);
12001 def_builtin ("__builtin_vec_stvehx", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_STVEHX);
12003 def_builtin ("__builtin_vsx_lxvd2x_v2df", v2df_ftype_long_pcvoid,
12004 VSX_BUILTIN_LXVD2X_V2DF);
12005 def_builtin ("__builtin_vsx_lxvd2x_v2di", v2di_ftype_long_pcvoid,
12006 VSX_BUILTIN_LXVD2X_V2DI);
12007 def_builtin ("__builtin_vsx_lxvw4x_v4sf", v4sf_ftype_long_pcvoid,
12008 VSX_BUILTIN_LXVW4X_V4SF);
12009 def_builtin ("__builtin_vsx_lxvw4x_v4si", v4si_ftype_long_pcvoid,
12010 VSX_BUILTIN_LXVW4X_V4SI);
12011 def_builtin ("__builtin_vsx_lxvw4x_v8hi", v8hi_ftype_long_pcvoid,
12012 VSX_BUILTIN_LXVW4X_V8HI);
12013 def_builtin ("__builtin_vsx_lxvw4x_v16qi", v16qi_ftype_long_pcvoid,
12014 VSX_BUILTIN_LXVW4X_V16QI);
12015 def_builtin ("__builtin_vsx_stxvd2x_v2df", void_ftype_v2df_long_pvoid,
12016 VSX_BUILTIN_STXVD2X_V2DF);
12017 def_builtin ("__builtin_vsx_stxvd2x_v2di", void_ftype_v2di_long_pvoid,
12018 VSX_BUILTIN_STXVD2X_V2DI);
12019 def_builtin ("__builtin_vsx_stxvw4x_v4sf", void_ftype_v4sf_long_pvoid,
12020 VSX_BUILTIN_STXVW4X_V4SF);
12021 def_builtin ("__builtin_vsx_stxvw4x_v4si", void_ftype_v4si_long_pvoid,
12022 VSX_BUILTIN_STXVW4X_V4SI);
12023 def_builtin ("__builtin_vsx_stxvw4x_v8hi", void_ftype_v8hi_long_pvoid,
12024 VSX_BUILTIN_STXVW4X_V8HI);
12025 def_builtin ("__builtin_vsx_stxvw4x_v16qi", void_ftype_v16qi_long_pvoid,
12026 VSX_BUILTIN_STXVW4X_V16QI);
12027 def_builtin ("__builtin_vec_vsx_ld", opaque_ftype_long_pcvoid,
12028 VSX_BUILTIN_VEC_LD);
12029 def_builtin ("__builtin_vec_vsx_st", void_ftype_opaque_long_pvoid,
12030 VSX_BUILTIN_VEC_ST);
12032 def_builtin ("__builtin_vec_step", int_ftype_opaque, ALTIVEC_BUILTIN_VEC_STEP);
12033 def_builtin ("__builtin_vec_splats", opaque_ftype_opaque, ALTIVEC_BUILTIN_VEC_SPLATS);
12034 def_builtin ("__builtin_vec_promote", opaque_ftype_opaque, ALTIVEC_BUILTIN_VEC_PROMOTE);
12036 def_builtin ("__builtin_vec_sld", opaque_ftype_opaque_opaque_int, ALTIVEC_BUILTIN_VEC_SLD);
12037 def_builtin ("__builtin_vec_splat", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_SPLAT);
12038 def_builtin ("__builtin_vec_extract", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_EXTRACT);
12039 def_builtin ("__builtin_vec_insert", opaque_ftype_opaque_opaque_int, ALTIVEC_BUILTIN_VEC_INSERT);
12040 def_builtin ("__builtin_vec_vspltw", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_VSPLTW);
12041 def_builtin ("__builtin_vec_vsplth", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_VSPLTH);
12042 def_builtin ("__builtin_vec_vspltb", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_VSPLTB);
12043 def_builtin ("__builtin_vec_ctf", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_CTF);
12044 def_builtin ("__builtin_vec_vcfsx", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_VCFSX);
12045 def_builtin ("__builtin_vec_vcfux", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_VCFUX);
12046 def_builtin ("__builtin_vec_cts", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_CTS);
12047 def_builtin ("__builtin_vec_ctu", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_CTU);
12049 /* Cell builtins. */
12050 def_builtin ("__builtin_altivec_lvlx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVLX);
12051 def_builtin ("__builtin_altivec_lvlxl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVLXL);
12052 def_builtin ("__builtin_altivec_lvrx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVRX);
12053 def_builtin ("__builtin_altivec_lvrxl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVRXL);
12055 def_builtin ("__builtin_vec_lvlx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVLX);
12056 def_builtin ("__builtin_vec_lvlxl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVLXL);
12057 def_builtin ("__builtin_vec_lvrx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVRX);
12058 def_builtin ("__builtin_vec_lvrxl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVRXL);
12060 def_builtin ("__builtin_altivec_stvlx", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_STVLX);
12061 def_builtin ("__builtin_altivec_stvlxl", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_STVLXL);
12062 def_builtin ("__builtin_altivec_stvrx", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_STVRX);
12063 def_builtin ("__builtin_altivec_stvrxl", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_STVRXL);
12065 def_builtin ("__builtin_vec_stvlx", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_VEC_STVLX);
12066 def_builtin ("__builtin_vec_stvlxl", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_VEC_STVLXL);
12067 def_builtin ("__builtin_vec_stvrx", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_VEC_STVRX);
12068 def_builtin ("__builtin_vec_stvrxl", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_VEC_STVRXL);
12070 /* Add the DST variants. */
12071 d = bdesc_dst;
12072 for (i = 0; i < ARRAY_SIZE (bdesc_dst); i++, d++)
12073 def_builtin (d->name, void_ftype_pcvoid_int_int, d->code);
12075 /* Initialize the predicates. */
12076 d = bdesc_altivec_preds;
12077 for (i = 0; i < ARRAY_SIZE (bdesc_altivec_preds); i++, d++)
12079 enum machine_mode mode1;
12080 tree type;
12082 if (rs6000_overloaded_builtin_p (d->code))
12083 mode1 = VOIDmode;
12084 else
12085 mode1 = insn_data[d->icode].operand[1].mode;
12087 switch (mode1)
12089 case VOIDmode:
12090 type = int_ftype_int_opaque_opaque;
12091 break;
12092 case V4SImode:
12093 type = int_ftype_int_v4si_v4si;
12094 break;
12095 case V8HImode:
12096 type = int_ftype_int_v8hi_v8hi;
12097 break;
12098 case V16QImode:
12099 type = int_ftype_int_v16qi_v16qi;
12100 break;
12101 case V4SFmode:
12102 type = int_ftype_int_v4sf_v4sf;
12103 break;
12104 case V2DFmode:
12105 type = int_ftype_int_v2df_v2df;
12106 break;
12107 default:
12108 gcc_unreachable ();
12111 def_builtin (d->name, type, d->code);
12114 /* Initialize the abs* operators. */
12115 d = bdesc_abs;
12116 for (i = 0; i < ARRAY_SIZE (bdesc_abs); i++, d++)
12118 enum machine_mode mode0;
12119 tree type;
12121 mode0 = insn_data[d->icode].operand[0].mode;
12123 switch (mode0)
12125 case V4SImode:
12126 type = v4si_ftype_v4si;
12127 break;
12128 case V8HImode:
12129 type = v8hi_ftype_v8hi;
12130 break;
12131 case V16QImode:
12132 type = v16qi_ftype_v16qi;
12133 break;
12134 case V4SFmode:
12135 type = v4sf_ftype_v4sf;
12136 break;
12137 case V2DFmode:
12138 type = v2df_ftype_v2df;
12139 break;
12140 default:
12141 gcc_unreachable ();
12144 def_builtin (d->name, type, d->code);
12147 /* Initialize target builtin that implements
12148 targetm.vectorize.builtin_mask_for_load. */
12150 decl = add_builtin_function ("__builtin_altivec_mask_for_load",
12151 v16qi_ftype_long_pcvoid,
12152 ALTIVEC_BUILTIN_MASK_FOR_LOAD,
12153 BUILT_IN_MD, NULL, NULL_TREE);
12154 TREE_READONLY (decl) = 1;
12155 /* Record the decl. Will be used by rs6000_builtin_mask_for_load. */
12156 altivec_builtin_mask_for_load = decl;
12158 /* Access to the vec_init patterns. */
12159 ftype = build_function_type_list (V4SI_type_node, integer_type_node,
12160 integer_type_node, integer_type_node,
12161 integer_type_node, NULL_TREE);
12162 def_builtin ("__builtin_vec_init_v4si", ftype, ALTIVEC_BUILTIN_VEC_INIT_V4SI);
12164 ftype = build_function_type_list (V8HI_type_node, short_integer_type_node,
12165 short_integer_type_node,
12166 short_integer_type_node,
12167 short_integer_type_node,
12168 short_integer_type_node,
12169 short_integer_type_node,
12170 short_integer_type_node,
12171 short_integer_type_node, NULL_TREE);
12172 def_builtin ("__builtin_vec_init_v8hi", ftype, ALTIVEC_BUILTIN_VEC_INIT_V8HI);
12174 ftype = build_function_type_list (V16QI_type_node, char_type_node,
12175 char_type_node, char_type_node,
12176 char_type_node, char_type_node,
12177 char_type_node, char_type_node,
12178 char_type_node, char_type_node,
12179 char_type_node, char_type_node,
12180 char_type_node, char_type_node,
12181 char_type_node, char_type_node,
12182 char_type_node, NULL_TREE);
12183 def_builtin ("__builtin_vec_init_v16qi", ftype,
12184 ALTIVEC_BUILTIN_VEC_INIT_V16QI);
12186 ftype = build_function_type_list (V4SF_type_node, float_type_node,
12187 float_type_node, float_type_node,
12188 float_type_node, NULL_TREE);
12189 def_builtin ("__builtin_vec_init_v4sf", ftype, ALTIVEC_BUILTIN_VEC_INIT_V4SF);
12191 /* VSX builtins. */
12192 ftype = build_function_type_list (V2DF_type_node, double_type_node,
12193 double_type_node, NULL_TREE);
12194 def_builtin ("__builtin_vec_init_v2df", ftype, VSX_BUILTIN_VEC_INIT_V2DF);
12196 ftype = build_function_type_list (V2DI_type_node, intDI_type_node,
12197 intDI_type_node, NULL_TREE);
12198 def_builtin ("__builtin_vec_init_v2di", ftype, VSX_BUILTIN_VEC_INIT_V2DI);
12200 /* Access to the vec_set patterns. */
12201 ftype = build_function_type_list (V4SI_type_node, V4SI_type_node,
12202 intSI_type_node,
12203 integer_type_node, NULL_TREE);
12204 def_builtin ("__builtin_vec_set_v4si", ftype, ALTIVEC_BUILTIN_VEC_SET_V4SI);
12206 ftype = build_function_type_list (V8HI_type_node, V8HI_type_node,
12207 intHI_type_node,
12208 integer_type_node, NULL_TREE);
12209 def_builtin ("__builtin_vec_set_v8hi", ftype, ALTIVEC_BUILTIN_VEC_SET_V8HI);
12211 ftype = build_function_type_list (V16QI_type_node, V16QI_type_node,
12212 intQI_type_node,
12213 integer_type_node, NULL_TREE);
12214 def_builtin ("__builtin_vec_set_v16qi", ftype, ALTIVEC_BUILTIN_VEC_SET_V16QI);
12216 ftype = build_function_type_list (V4SF_type_node, V4SF_type_node,
12217 float_type_node,
12218 integer_type_node, NULL_TREE);
12219 def_builtin ("__builtin_vec_set_v4sf", ftype, ALTIVEC_BUILTIN_VEC_SET_V4SF);
12221 ftype = build_function_type_list (V2DF_type_node, V2DF_type_node,
12222 double_type_node,
12223 integer_type_node, NULL_TREE);
12224 def_builtin ("__builtin_vec_set_v2df", ftype, VSX_BUILTIN_VEC_SET_V2DF);
12226 ftype = build_function_type_list (V2DI_type_node, V2DI_type_node,
12227 intDI_type_node,
12228 integer_type_node, NULL_TREE);
12229 def_builtin ("__builtin_vec_set_v2di", ftype, VSX_BUILTIN_VEC_SET_V2DI);
12231 /* Access to the vec_extract patterns. */
12232 ftype = build_function_type_list (intSI_type_node, V4SI_type_node,
12233 integer_type_node, NULL_TREE);
12234 def_builtin ("__builtin_vec_ext_v4si", ftype, ALTIVEC_BUILTIN_VEC_EXT_V4SI);
12236 ftype = build_function_type_list (intHI_type_node, V8HI_type_node,
12237 integer_type_node, NULL_TREE);
12238 def_builtin ("__builtin_vec_ext_v8hi", ftype, ALTIVEC_BUILTIN_VEC_EXT_V8HI);
12240 ftype = build_function_type_list (intQI_type_node, V16QI_type_node,
12241 integer_type_node, NULL_TREE);
12242 def_builtin ("__builtin_vec_ext_v16qi", ftype, ALTIVEC_BUILTIN_VEC_EXT_V16QI);
12244 ftype = build_function_type_list (float_type_node, V4SF_type_node,
12245 integer_type_node, NULL_TREE);
12246 def_builtin ("__builtin_vec_ext_v4sf", ftype, ALTIVEC_BUILTIN_VEC_EXT_V4SF);
12248 ftype = build_function_type_list (double_type_node, V2DF_type_node,
12249 integer_type_node, NULL_TREE);
12250 def_builtin ("__builtin_vec_ext_v2df", ftype, VSX_BUILTIN_VEC_EXT_V2DF);
12252 ftype = build_function_type_list (intDI_type_node, V2DI_type_node,
12253 integer_type_node, NULL_TREE);
12254 def_builtin ("__builtin_vec_ext_v2di", ftype, VSX_BUILTIN_VEC_EXT_V2DI);
12257 /* Hash function for builtin functions with up to 3 arguments and a return
12258 type. */
12259 static unsigned
12260 builtin_hash_function (const void *hash_entry)
12262 unsigned ret = 0;
12263 int i;
12264 const struct builtin_hash_struct *bh =
12265 (const struct builtin_hash_struct *) hash_entry;
12267 for (i = 0; i < 4; i++)
12269 ret = (ret * (unsigned)MAX_MACHINE_MODE) + ((unsigned)bh->mode[i]);
12270 ret = (ret * 2) + bh->uns_p[i];
12273 return ret;
12276 /* Compare builtin hash entries H1 and H2 for equivalence. */
12277 static int
12278 builtin_hash_eq (const void *h1, const void *h2)
12280 const struct builtin_hash_struct *p1 = (const struct builtin_hash_struct *) h1;
12281 const struct builtin_hash_struct *p2 = (const struct builtin_hash_struct *) h2;
12283 return ((p1->mode[0] == p2->mode[0])
12284 && (p1->mode[1] == p2->mode[1])
12285 && (p1->mode[2] == p2->mode[2])
12286 && (p1->mode[3] == p2->mode[3])
12287 && (p1->uns_p[0] == p2->uns_p[0])
12288 && (p1->uns_p[1] == p2->uns_p[1])
12289 && (p1->uns_p[2] == p2->uns_p[2])
12290 && (p1->uns_p[3] == p2->uns_p[3]));
12293 /* Map types for builtin functions with an explicit return type and up to 3
12294 arguments. Functions with fewer than 3 arguments use VOIDmode as the type
12295 of the argument. */
12296 static tree
12297 builtin_function_type (enum machine_mode mode_ret, enum machine_mode mode_arg0,
12298 enum machine_mode mode_arg1, enum machine_mode mode_arg2,
12299 enum rs6000_builtins builtin, const char *name)
12301 struct builtin_hash_struct h;
12302 struct builtin_hash_struct *h2;
12303 void **found;
12304 int num_args = 3;
12305 int i;
12306 tree ret_type = NULL_TREE;
12307 tree arg_type[3] = { NULL_TREE, NULL_TREE, NULL_TREE };
12309 /* Create builtin_hash_table. */
12310 if (builtin_hash_table == NULL)
12311 builtin_hash_table = htab_create_ggc (1500, builtin_hash_function,
12312 builtin_hash_eq, NULL);
12314 h.type = NULL_TREE;
12315 h.mode[0] = mode_ret;
12316 h.mode[1] = mode_arg0;
12317 h.mode[2] = mode_arg1;
12318 h.mode[3] = mode_arg2;
12319 h.uns_p[0] = 0;
12320 h.uns_p[1] = 0;
12321 h.uns_p[2] = 0;
12322 h.uns_p[3] = 0;
12324 /* If the builtin is a type that produces unsigned results or takes unsigned
12325 arguments, and it is returned as a decl for the vectorizer (such as
12326 widening multiplies, permute), make sure the arguments and return value
12327 are type correct. */
12328 switch (builtin)
12330 /* unsigned 2 argument functions. */
12331 case ALTIVEC_BUILTIN_VMULEUB_UNS:
12332 case ALTIVEC_BUILTIN_VMULEUH_UNS:
12333 case ALTIVEC_BUILTIN_VMULOUB_UNS:
12334 case ALTIVEC_BUILTIN_VMULOUH_UNS:
12335 h.uns_p[0] = 1;
12336 h.uns_p[1] = 1;
12337 h.uns_p[2] = 1;
12338 break;
12340 /* unsigned 3 argument functions. */
12341 case ALTIVEC_BUILTIN_VPERM_16QI_UNS:
12342 case ALTIVEC_BUILTIN_VPERM_8HI_UNS:
12343 case ALTIVEC_BUILTIN_VPERM_4SI_UNS:
12344 case ALTIVEC_BUILTIN_VPERM_2DI_UNS:
12345 case ALTIVEC_BUILTIN_VSEL_16QI_UNS:
12346 case ALTIVEC_BUILTIN_VSEL_8HI_UNS:
12347 case ALTIVEC_BUILTIN_VSEL_4SI_UNS:
12348 case ALTIVEC_BUILTIN_VSEL_2DI_UNS:
12349 case VSX_BUILTIN_VPERM_16QI_UNS:
12350 case VSX_BUILTIN_VPERM_8HI_UNS:
12351 case VSX_BUILTIN_VPERM_4SI_UNS:
12352 case VSX_BUILTIN_VPERM_2DI_UNS:
12353 case VSX_BUILTIN_XXSEL_16QI_UNS:
12354 case VSX_BUILTIN_XXSEL_8HI_UNS:
12355 case VSX_BUILTIN_XXSEL_4SI_UNS:
12356 case VSX_BUILTIN_XXSEL_2DI_UNS:
12357 h.uns_p[0] = 1;
12358 h.uns_p[1] = 1;
12359 h.uns_p[2] = 1;
12360 h.uns_p[3] = 1;
12361 break;
12363 /* signed permute functions with unsigned char mask. */
12364 case ALTIVEC_BUILTIN_VPERM_16QI:
12365 case ALTIVEC_BUILTIN_VPERM_8HI:
12366 case ALTIVEC_BUILTIN_VPERM_4SI:
12367 case ALTIVEC_BUILTIN_VPERM_4SF:
12368 case ALTIVEC_BUILTIN_VPERM_2DI:
12369 case ALTIVEC_BUILTIN_VPERM_2DF:
12370 case VSX_BUILTIN_VPERM_16QI:
12371 case VSX_BUILTIN_VPERM_8HI:
12372 case VSX_BUILTIN_VPERM_4SI:
12373 case VSX_BUILTIN_VPERM_4SF:
12374 case VSX_BUILTIN_VPERM_2DI:
12375 case VSX_BUILTIN_VPERM_2DF:
12376 h.uns_p[3] = 1;
12377 break;
12379 /* unsigned args, signed return. */
12380 case VSX_BUILTIN_XVCVUXDDP_UNS:
12381 case ALTIVEC_BUILTIN_UNSFLOAT_V4SI_V4SF:
12382 h.uns_p[1] = 1;
12383 break;
12385 /* signed args, unsigned return. */
12386 case VSX_BUILTIN_XVCVDPUXDS_UNS:
12387 case ALTIVEC_BUILTIN_FIXUNS_V4SF_V4SI:
12388 h.uns_p[0] = 1;
12389 break;
12391 default:
12392 break;
12395 /* Figure out how many args are present. */
12396 while (num_args > 0 && h.mode[num_args] == VOIDmode)
12397 num_args--;
12399 if (num_args == 0)
12400 fatal_error ("internal error: builtin function %s had no type", name);
12402 ret_type = builtin_mode_to_type[h.mode[0]][h.uns_p[0]];
12403 if (!ret_type && h.uns_p[0])
12404 ret_type = builtin_mode_to_type[h.mode[0]][0];
12406 if (!ret_type)
12407 fatal_error ("internal error: builtin function %s had an unexpected "
12408 "return type %s", name, GET_MODE_NAME (h.mode[0]));
12410 for (i = 0; i < (int) ARRAY_SIZE (arg_type); i++)
12411 arg_type[i] = NULL_TREE;
12413 for (i = 0; i < num_args; i++)
12415 int m = (int) h.mode[i+1];
12416 int uns_p = h.uns_p[i+1];
12418 arg_type[i] = builtin_mode_to_type[m][uns_p];
12419 if (!arg_type[i] && uns_p)
12420 arg_type[i] = builtin_mode_to_type[m][0];
12422 if (!arg_type[i])
12423 fatal_error ("internal error: builtin function %s, argument %d "
12424 "had unexpected argument type %s", name, i,
12425 GET_MODE_NAME (m));
12428 found = htab_find_slot (builtin_hash_table, &h, INSERT);
12429 if (*found == NULL)
12431 h2 = ggc_alloc_builtin_hash_struct ();
12432 *h2 = h;
12433 *found = (void *)h2;
12435 h2->type = build_function_type_list (ret_type, arg_type[0], arg_type[1],
12436 arg_type[2], NULL_TREE);
12439 return ((struct builtin_hash_struct *)(*found))->type;
12442 static void
12443 rs6000_common_init_builtins (void)
12445 const struct builtin_description *d;
12446 size_t i;
12448 tree opaque_ftype_opaque = NULL_TREE;
12449 tree opaque_ftype_opaque_opaque = NULL_TREE;
12450 tree opaque_ftype_opaque_opaque_opaque = NULL_TREE;
12451 tree v2si_ftype_qi = NULL_TREE;
12452 tree v2si_ftype_v2si_qi = NULL_TREE;
12453 tree v2si_ftype_int_qi = NULL_TREE;
12454 unsigned builtin_mask = rs6000_builtin_mask;
12456 if (!TARGET_PAIRED_FLOAT)
12458 builtin_mode_to_type[V2SImode][0] = opaque_V2SI_type_node;
12459 builtin_mode_to_type[V2SFmode][0] = opaque_V2SF_type_node;
12462 /* Paired and SPE builtins are only available if you build a compiler with
12463 the appropriate options, so only create those builtins with the
12464 appropriate compiler option. Create Altivec and VSX builtins on machines
12465 with at least the general purpose extensions (970 and newer) to allow the
12466 use of the target attribute.. */
12468 if (TARGET_EXTRA_BUILTINS)
12469 builtin_mask |= RS6000_BTM_COMMON;
12471 /* Add the ternary operators. */
12472 d = bdesc_3arg;
12473 for (i = 0; i < ARRAY_SIZE (bdesc_3arg); i++, d++)
12475 tree type;
12476 unsigned mask = d->mask;
12478 if ((mask & builtin_mask) != mask)
12480 if (TARGET_DEBUG_BUILTIN)
12481 fprintf (stderr, "rs6000_builtin, skip ternary %s\n", d->name);
12482 continue;
12485 if (rs6000_overloaded_builtin_p (d->code))
12487 if (! (type = opaque_ftype_opaque_opaque_opaque))
12488 type = opaque_ftype_opaque_opaque_opaque
12489 = build_function_type_list (opaque_V4SI_type_node,
12490 opaque_V4SI_type_node,
12491 opaque_V4SI_type_node,
12492 opaque_V4SI_type_node,
12493 NULL_TREE);
12495 else
12497 enum insn_code icode = d->icode;
12498 if (d->name == 0 || icode == CODE_FOR_nothing)
12499 continue;
12501 type = builtin_function_type (insn_data[icode].operand[0].mode,
12502 insn_data[icode].operand[1].mode,
12503 insn_data[icode].operand[2].mode,
12504 insn_data[icode].operand[3].mode,
12505 d->code, d->name);
12508 def_builtin (d->name, type, d->code);
12511 /* Add the binary operators. */
12512 d = bdesc_2arg;
12513 for (i = 0; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
12515 enum machine_mode mode0, mode1, mode2;
12516 tree type;
12517 unsigned mask = d->mask;
12519 if ((mask & builtin_mask) != mask)
12521 if (TARGET_DEBUG_BUILTIN)
12522 fprintf (stderr, "rs6000_builtin, skip binary %s\n", d->name);
12523 continue;
12526 if (rs6000_overloaded_builtin_p (d->code))
12528 if (! (type = opaque_ftype_opaque_opaque))
12529 type = opaque_ftype_opaque_opaque
12530 = build_function_type_list (opaque_V4SI_type_node,
12531 opaque_V4SI_type_node,
12532 opaque_V4SI_type_node,
12533 NULL_TREE);
12535 else
12537 enum insn_code icode = d->icode;
12538 if (d->name == 0 || icode == CODE_FOR_nothing)
12539 continue;
12541 mode0 = insn_data[icode].operand[0].mode;
12542 mode1 = insn_data[icode].operand[1].mode;
12543 mode2 = insn_data[icode].operand[2].mode;
12545 if (mode0 == V2SImode && mode1 == V2SImode && mode2 == QImode)
12547 if (! (type = v2si_ftype_v2si_qi))
12548 type = v2si_ftype_v2si_qi
12549 = build_function_type_list (opaque_V2SI_type_node,
12550 opaque_V2SI_type_node,
12551 char_type_node,
12552 NULL_TREE);
12555 else if (mode0 == V2SImode && GET_MODE_CLASS (mode1) == MODE_INT
12556 && mode2 == QImode)
12558 if (! (type = v2si_ftype_int_qi))
12559 type = v2si_ftype_int_qi
12560 = build_function_type_list (opaque_V2SI_type_node,
12561 integer_type_node,
12562 char_type_node,
12563 NULL_TREE);
12566 else
12567 type = builtin_function_type (mode0, mode1, mode2, VOIDmode,
12568 d->code, d->name);
12571 def_builtin (d->name, type, d->code);
12574 /* Add the simple unary operators. */
12575 d = bdesc_1arg;
12576 for (i = 0; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
12578 enum machine_mode mode0, mode1;
12579 tree type;
12580 unsigned mask = d->mask;
12582 if ((mask & builtin_mask) != mask)
12584 if (TARGET_DEBUG_BUILTIN)
12585 fprintf (stderr, "rs6000_builtin, skip unary %s\n", d->name);
12586 continue;
12589 if (rs6000_overloaded_builtin_p (d->code))
12591 if (! (type = opaque_ftype_opaque))
12592 type = opaque_ftype_opaque
12593 = build_function_type_list (opaque_V4SI_type_node,
12594 opaque_V4SI_type_node,
12595 NULL_TREE);
12597 else
12599 enum insn_code icode = d->icode;
12600 if (d->name == 0 || icode == CODE_FOR_nothing)
12601 continue;
12603 mode0 = insn_data[icode].operand[0].mode;
12604 mode1 = insn_data[icode].operand[1].mode;
12606 if (mode0 == V2SImode && mode1 == QImode)
12608 if (! (type = v2si_ftype_qi))
12609 type = v2si_ftype_qi
12610 = build_function_type_list (opaque_V2SI_type_node,
12611 char_type_node,
12612 NULL_TREE);
12615 else
12616 type = builtin_function_type (mode0, mode1, VOIDmode, VOIDmode,
12617 d->code, d->name);
12620 def_builtin (d->name, type, d->code);
12624 static void
12625 rs6000_init_libfuncs (void)
12627 if (DEFAULT_ABI != ABI_V4 && TARGET_XCOFF
12628 && !TARGET_POWER2 && !TARGET_POWERPC)
12630 /* AIX library routines for float->int conversion. */
12631 set_conv_libfunc (sfix_optab, SImode, DFmode, "__itrunc");
12632 set_conv_libfunc (ufix_optab, SImode, DFmode, "__uitrunc");
12633 set_conv_libfunc (sfix_optab, SImode, TFmode, "_qitrunc");
12634 set_conv_libfunc (ufix_optab, SImode, TFmode, "_quitrunc");
12637 if (!TARGET_IEEEQUAD)
12638 /* AIX/Darwin/64-bit Linux quad floating point routines. */
12639 if (!TARGET_XL_COMPAT)
12641 set_optab_libfunc (add_optab, TFmode, "__gcc_qadd");
12642 set_optab_libfunc (sub_optab, TFmode, "__gcc_qsub");
12643 set_optab_libfunc (smul_optab, TFmode, "__gcc_qmul");
12644 set_optab_libfunc (sdiv_optab, TFmode, "__gcc_qdiv");
12646 if (!(TARGET_HARD_FLOAT && (TARGET_FPRS || TARGET_E500_DOUBLE)))
12648 set_optab_libfunc (neg_optab, TFmode, "__gcc_qneg");
12649 set_optab_libfunc (eq_optab, TFmode, "__gcc_qeq");
12650 set_optab_libfunc (ne_optab, TFmode, "__gcc_qne");
12651 set_optab_libfunc (gt_optab, TFmode, "__gcc_qgt");
12652 set_optab_libfunc (ge_optab, TFmode, "__gcc_qge");
12653 set_optab_libfunc (lt_optab, TFmode, "__gcc_qlt");
12654 set_optab_libfunc (le_optab, TFmode, "__gcc_qle");
12656 set_conv_libfunc (sext_optab, TFmode, SFmode, "__gcc_stoq");
12657 set_conv_libfunc (sext_optab, TFmode, DFmode, "__gcc_dtoq");
12658 set_conv_libfunc (trunc_optab, SFmode, TFmode, "__gcc_qtos");
12659 set_conv_libfunc (trunc_optab, DFmode, TFmode, "__gcc_qtod");
12660 set_conv_libfunc (sfix_optab, SImode, TFmode, "__gcc_qtoi");
12661 set_conv_libfunc (ufix_optab, SImode, TFmode, "__gcc_qtou");
12662 set_conv_libfunc (sfloat_optab, TFmode, SImode, "__gcc_itoq");
12663 set_conv_libfunc (ufloat_optab, TFmode, SImode, "__gcc_utoq");
12666 if (!(TARGET_HARD_FLOAT && TARGET_FPRS))
12667 set_optab_libfunc (unord_optab, TFmode, "__gcc_qunord");
12669 else
12671 set_optab_libfunc (add_optab, TFmode, "_xlqadd");
12672 set_optab_libfunc (sub_optab, TFmode, "_xlqsub");
12673 set_optab_libfunc (smul_optab, TFmode, "_xlqmul");
12674 set_optab_libfunc (sdiv_optab, TFmode, "_xlqdiv");
12676 else
12678 /* 32-bit SVR4 quad floating point routines. */
12680 set_optab_libfunc (add_optab, TFmode, "_q_add");
12681 set_optab_libfunc (sub_optab, TFmode, "_q_sub");
12682 set_optab_libfunc (neg_optab, TFmode, "_q_neg");
12683 set_optab_libfunc (smul_optab, TFmode, "_q_mul");
12684 set_optab_libfunc (sdiv_optab, TFmode, "_q_div");
12685 if (TARGET_PPC_GPOPT || TARGET_POWER2)
12686 set_optab_libfunc (sqrt_optab, TFmode, "_q_sqrt");
12688 set_optab_libfunc (eq_optab, TFmode, "_q_feq");
12689 set_optab_libfunc (ne_optab, TFmode, "_q_fne");
12690 set_optab_libfunc (gt_optab, TFmode, "_q_fgt");
12691 set_optab_libfunc (ge_optab, TFmode, "_q_fge");
12692 set_optab_libfunc (lt_optab, TFmode, "_q_flt");
12693 set_optab_libfunc (le_optab, TFmode, "_q_fle");
12695 set_conv_libfunc (sext_optab, TFmode, SFmode, "_q_stoq");
12696 set_conv_libfunc (sext_optab, TFmode, DFmode, "_q_dtoq");
12697 set_conv_libfunc (trunc_optab, SFmode, TFmode, "_q_qtos");
12698 set_conv_libfunc (trunc_optab, DFmode, TFmode, "_q_qtod");
12699 set_conv_libfunc (sfix_optab, SImode, TFmode, "_q_qtoi");
12700 set_conv_libfunc (ufix_optab, SImode, TFmode, "_q_qtou");
12701 set_conv_libfunc (sfloat_optab, TFmode, SImode, "_q_itoq");
12702 set_conv_libfunc (ufloat_optab, TFmode, SImode, "_q_utoq");
12707 /* Expand a block clear operation, and return 1 if successful. Return 0
12708 if we should let the compiler generate normal code.
12710 operands[0] is the destination
12711 operands[1] is the length
12712 operands[3] is the alignment */
12715 expand_block_clear (rtx operands[])
12717 rtx orig_dest = operands[0];
12718 rtx bytes_rtx = operands[1];
12719 rtx align_rtx = operands[3];
12720 bool constp = (GET_CODE (bytes_rtx) == CONST_INT);
12721 HOST_WIDE_INT align;
12722 HOST_WIDE_INT bytes;
12723 int offset;
12724 int clear_bytes;
12725 int clear_step;
12727 /* If this is not a fixed size move, just call memcpy */
12728 if (! constp)
12729 return 0;
12731 /* This must be a fixed size alignment */
12732 gcc_assert (GET_CODE (align_rtx) == CONST_INT);
12733 align = INTVAL (align_rtx) * BITS_PER_UNIT;
12735 /* Anything to clear? */
12736 bytes = INTVAL (bytes_rtx);
12737 if (bytes <= 0)
12738 return 1;
12740 /* Use the builtin memset after a point, to avoid huge code bloat.
12741 When optimize_size, avoid any significant code bloat; calling
12742 memset is about 4 instructions, so allow for one instruction to
12743 load zero and three to do clearing. */
12744 if (TARGET_ALTIVEC && align >= 128)
12745 clear_step = 16;
12746 else if (TARGET_POWERPC64 && align >= 32)
12747 clear_step = 8;
12748 else if (TARGET_SPE && align >= 64)
12749 clear_step = 8;
12750 else
12751 clear_step = 4;
12753 if (optimize_size && bytes > 3 * clear_step)
12754 return 0;
12755 if (! optimize_size && bytes > 8 * clear_step)
12756 return 0;
12758 for (offset = 0; bytes > 0; offset += clear_bytes, bytes -= clear_bytes)
12760 enum machine_mode mode = BLKmode;
12761 rtx dest;
12763 if (bytes >= 16 && TARGET_ALTIVEC && align >= 128)
12765 clear_bytes = 16;
12766 mode = V4SImode;
12768 else if (bytes >= 8 && TARGET_SPE && align >= 64)
12770 clear_bytes = 8;
12771 mode = V2SImode;
12773 else if (bytes >= 8 && TARGET_POWERPC64
12774 /* 64-bit loads and stores require word-aligned
12775 displacements. */
12776 && (align >= 64 || (!STRICT_ALIGNMENT && align >= 32)))
12778 clear_bytes = 8;
12779 mode = DImode;
12781 else if (bytes >= 4 && (align >= 32 || !STRICT_ALIGNMENT))
12782 { /* move 4 bytes */
12783 clear_bytes = 4;
12784 mode = SImode;
12786 else if (bytes >= 2 && (align >= 16 || !STRICT_ALIGNMENT))
12787 { /* move 2 bytes */
12788 clear_bytes = 2;
12789 mode = HImode;
12791 else /* move 1 byte at a time */
12793 clear_bytes = 1;
12794 mode = QImode;
12797 dest = adjust_address (orig_dest, mode, offset);
12799 emit_move_insn (dest, CONST0_RTX (mode));
12802 return 1;
12806 /* Expand a block move operation, and return 1 if successful. Return 0
12807 if we should let the compiler generate normal code.
12809 operands[0] is the destination
12810 operands[1] is the source
12811 operands[2] is the length
12812 operands[3] is the alignment */
12814 #define MAX_MOVE_REG 4
12817 expand_block_move (rtx operands[])
12819 rtx orig_dest = operands[0];
12820 rtx orig_src = operands[1];
12821 rtx bytes_rtx = operands[2];
12822 rtx align_rtx = operands[3];
12823 int constp = (GET_CODE (bytes_rtx) == CONST_INT);
12824 int align;
12825 int bytes;
12826 int offset;
12827 int move_bytes;
12828 rtx stores[MAX_MOVE_REG];
12829 int num_reg = 0;
12831 /* If this is not a fixed size move, just call memcpy */
12832 if (! constp)
12833 return 0;
12835 /* This must be a fixed size alignment */
12836 gcc_assert (GET_CODE (align_rtx) == CONST_INT);
12837 align = INTVAL (align_rtx) * BITS_PER_UNIT;
12839 /* Anything to move? */
12840 bytes = INTVAL (bytes_rtx);
12841 if (bytes <= 0)
12842 return 1;
12844 if (bytes > rs6000_block_move_inline_limit)
12845 return 0;
12847 for (offset = 0; bytes > 0; offset += move_bytes, bytes -= move_bytes)
12849 union {
12850 rtx (*movmemsi) (rtx, rtx, rtx, rtx);
12851 rtx (*mov) (rtx, rtx);
12852 } gen_func;
12853 enum machine_mode mode = BLKmode;
12854 rtx src, dest;
12856 /* Altivec first, since it will be faster than a string move
12857 when it applies, and usually not significantly larger. */
12858 if (TARGET_ALTIVEC && bytes >= 16 && align >= 128)
12860 move_bytes = 16;
12861 mode = V4SImode;
12862 gen_func.mov = gen_movv4si;
12864 else if (TARGET_SPE && bytes >= 8 && align >= 64)
12866 move_bytes = 8;
12867 mode = V2SImode;
12868 gen_func.mov = gen_movv2si;
12870 else if (TARGET_STRING
12871 && bytes > 24 /* move up to 32 bytes at a time */
12872 && ! fixed_regs[5]
12873 && ! fixed_regs[6]
12874 && ! fixed_regs[7]
12875 && ! fixed_regs[8]
12876 && ! fixed_regs[9]
12877 && ! fixed_regs[10]
12878 && ! fixed_regs[11]
12879 && ! fixed_regs[12])
12881 move_bytes = (bytes > 32) ? 32 : bytes;
12882 gen_func.movmemsi = gen_movmemsi_8reg;
12884 else if (TARGET_STRING
12885 && bytes > 16 /* move up to 24 bytes at a time */
12886 && ! fixed_regs[5]
12887 && ! fixed_regs[6]
12888 && ! fixed_regs[7]
12889 && ! fixed_regs[8]
12890 && ! fixed_regs[9]
12891 && ! fixed_regs[10])
12893 move_bytes = (bytes > 24) ? 24 : bytes;
12894 gen_func.movmemsi = gen_movmemsi_6reg;
12896 else if (TARGET_STRING
12897 && bytes > 8 /* move up to 16 bytes at a time */
12898 && ! fixed_regs[5]
12899 && ! fixed_regs[6]
12900 && ! fixed_regs[7]
12901 && ! fixed_regs[8])
12903 move_bytes = (bytes > 16) ? 16 : bytes;
12904 gen_func.movmemsi = gen_movmemsi_4reg;
12906 else if (bytes >= 8 && TARGET_POWERPC64
12907 /* 64-bit loads and stores require word-aligned
12908 displacements. */
12909 && (align >= 64 || (!STRICT_ALIGNMENT && align >= 32)))
12911 move_bytes = 8;
12912 mode = DImode;
12913 gen_func.mov = gen_movdi;
12915 else if (TARGET_STRING && bytes > 4 && !TARGET_POWERPC64)
12916 { /* move up to 8 bytes at a time */
12917 move_bytes = (bytes > 8) ? 8 : bytes;
12918 gen_func.movmemsi = gen_movmemsi_2reg;
12920 else if (bytes >= 4 && (align >= 32 || !STRICT_ALIGNMENT))
12921 { /* move 4 bytes */
12922 move_bytes = 4;
12923 mode = SImode;
12924 gen_func.mov = gen_movsi;
12926 else if (bytes >= 2 && (align >= 16 || !STRICT_ALIGNMENT))
12927 { /* move 2 bytes */
12928 move_bytes = 2;
12929 mode = HImode;
12930 gen_func.mov = gen_movhi;
12932 else if (TARGET_STRING && bytes > 1)
12933 { /* move up to 4 bytes at a time */
12934 move_bytes = (bytes > 4) ? 4 : bytes;
12935 gen_func.movmemsi = gen_movmemsi_1reg;
12937 else /* move 1 byte at a time */
12939 move_bytes = 1;
12940 mode = QImode;
12941 gen_func.mov = gen_movqi;
12944 src = adjust_address (orig_src, mode, offset);
12945 dest = adjust_address (orig_dest, mode, offset);
12947 if (mode != BLKmode)
12949 rtx tmp_reg = gen_reg_rtx (mode);
12951 emit_insn ((*gen_func.mov) (tmp_reg, src));
12952 stores[num_reg++] = (*gen_func.mov) (dest, tmp_reg);
12955 if (mode == BLKmode || num_reg >= MAX_MOVE_REG || bytes == move_bytes)
12957 int i;
12958 for (i = 0; i < num_reg; i++)
12959 emit_insn (stores[i]);
12960 num_reg = 0;
12963 if (mode == BLKmode)
12965 /* Move the address into scratch registers. The movmemsi
12966 patterns require zero offset. */
12967 if (!REG_P (XEXP (src, 0)))
12969 rtx src_reg = copy_addr_to_reg (XEXP (src, 0));
12970 src = replace_equiv_address (src, src_reg);
12972 set_mem_size (src, move_bytes);
12974 if (!REG_P (XEXP (dest, 0)))
12976 rtx dest_reg = copy_addr_to_reg (XEXP (dest, 0));
12977 dest = replace_equiv_address (dest, dest_reg);
12979 set_mem_size (dest, move_bytes);
12981 emit_insn ((*gen_func.movmemsi) (dest, src,
12982 GEN_INT (move_bytes & 31),
12983 align_rtx));
12987 return 1;
12991 /* Return a string to perform a load_multiple operation.
12992 operands[0] is the vector.
12993 operands[1] is the source address.
12994 operands[2] is the first destination register. */
12996 const char *
12997 rs6000_output_load_multiple (rtx operands[3])
12999 /* We have to handle the case where the pseudo used to contain the address
13000 is assigned to one of the output registers. */
13001 int i, j;
13002 int words = XVECLEN (operands[0], 0);
13003 rtx xop[10];
13005 if (XVECLEN (operands[0], 0) == 1)
13006 return "{l|lwz} %2,0(%1)";
13008 for (i = 0; i < words; i++)
13009 if (refers_to_regno_p (REGNO (operands[2]) + i,
13010 REGNO (operands[2]) + i + 1, operands[1], 0))
13012 if (i == words-1)
13014 xop[0] = GEN_INT (4 * (words-1));
13015 xop[1] = operands[1];
13016 xop[2] = operands[2];
13017 output_asm_insn ("{lsi|lswi} %2,%1,%0\n\t{l|lwz} %1,%0(%1)", xop);
13018 return "";
13020 else if (i == 0)
13022 xop[0] = GEN_INT (4 * (words-1));
13023 xop[1] = operands[1];
13024 xop[2] = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
13025 output_asm_insn ("{cal %1,4(%1)|addi %1,%1,4}\n\t{lsi|lswi} %2,%1,%0\n\t{l|lwz} %1,-4(%1)", xop);
13026 return "";
13028 else
13030 for (j = 0; j < words; j++)
13031 if (j != i)
13033 xop[0] = GEN_INT (j * 4);
13034 xop[1] = operands[1];
13035 xop[2] = gen_rtx_REG (SImode, REGNO (operands[2]) + j);
13036 output_asm_insn ("{l|lwz} %2,%0(%1)", xop);
13038 xop[0] = GEN_INT (i * 4);
13039 xop[1] = operands[1];
13040 output_asm_insn ("{l|lwz} %1,%0(%1)", xop);
13041 return "";
13045 return "{lsi|lswi} %2,%1,%N0";
13049 /* A validation routine: say whether CODE, a condition code, and MODE
13050 match. The other alternatives either don't make sense or should
13051 never be generated. */
13053 void
13054 validate_condition_mode (enum rtx_code code, enum machine_mode mode)
13056 gcc_assert ((GET_RTX_CLASS (code) == RTX_COMPARE
13057 || GET_RTX_CLASS (code) == RTX_COMM_COMPARE)
13058 && GET_MODE_CLASS (mode) == MODE_CC);
13060 /* These don't make sense. */
13061 gcc_assert ((code != GT && code != LT && code != GE && code != LE)
13062 || mode != CCUNSmode);
13064 gcc_assert ((code != GTU && code != LTU && code != GEU && code != LEU)
13065 || mode == CCUNSmode);
13067 gcc_assert (mode == CCFPmode
13068 || (code != ORDERED && code != UNORDERED
13069 && code != UNEQ && code != LTGT
13070 && code != UNGT && code != UNLT
13071 && code != UNGE && code != UNLE));
13073 /* These should never be generated except for
13074 flag_finite_math_only. */
13075 gcc_assert (mode != CCFPmode
13076 || flag_finite_math_only
13077 || (code != LE && code != GE
13078 && code != UNEQ && code != LTGT
13079 && code != UNGT && code != UNLT));
13081 /* These are invalid; the information is not there. */
13082 gcc_assert (mode != CCEQmode || code == EQ || code == NE);
13086 /* Return 1 if ANDOP is a mask that has no bits on that are not in the
13087 mask required to convert the result of a rotate insn into a shift
13088 left insn of SHIFTOP bits. Both are known to be SImode CONST_INT. */
13091 includes_lshift_p (rtx shiftop, rtx andop)
13093 unsigned HOST_WIDE_INT shift_mask = ~(unsigned HOST_WIDE_INT) 0;
13095 shift_mask <<= INTVAL (shiftop);
13097 return (INTVAL (andop) & 0xffffffff & ~shift_mask) == 0;
13100 /* Similar, but for right shift. */
13103 includes_rshift_p (rtx shiftop, rtx andop)
13105 unsigned HOST_WIDE_INT shift_mask = ~(unsigned HOST_WIDE_INT) 0;
13107 shift_mask >>= INTVAL (shiftop);
13109 return (INTVAL (andop) & 0xffffffff & ~shift_mask) == 0;
13112 /* Return 1 if ANDOP is a mask suitable for use with an rldic insn
13113 to perform a left shift. It must have exactly SHIFTOP least
13114 significant 0's, then one or more 1's, then zero or more 0's. */
13117 includes_rldic_lshift_p (rtx shiftop, rtx andop)
13119 if (GET_CODE (andop) == CONST_INT)
13121 HOST_WIDE_INT c, lsb, shift_mask;
13123 c = INTVAL (andop);
13124 if (c == 0 || c == ~0)
13125 return 0;
13127 shift_mask = ~0;
13128 shift_mask <<= INTVAL (shiftop);
13130 /* Find the least significant one bit. */
13131 lsb = c & -c;
13133 /* It must coincide with the LSB of the shift mask. */
13134 if (-lsb != shift_mask)
13135 return 0;
13137 /* Invert to look for the next transition (if any). */
13138 c = ~c;
13140 /* Remove the low group of ones (originally low group of zeros). */
13141 c &= -lsb;
13143 /* Again find the lsb, and check we have all 1's above. */
13144 lsb = c & -c;
13145 return c == -lsb;
13147 else if (GET_CODE (andop) == CONST_DOUBLE
13148 && (GET_MODE (andop) == VOIDmode || GET_MODE (andop) == DImode))
13150 HOST_WIDE_INT low, high, lsb;
13151 HOST_WIDE_INT shift_mask_low, shift_mask_high;
13153 low = CONST_DOUBLE_LOW (andop);
13154 if (HOST_BITS_PER_WIDE_INT < 64)
13155 high = CONST_DOUBLE_HIGH (andop);
13157 if ((low == 0 && (HOST_BITS_PER_WIDE_INT >= 64 || high == 0))
13158 || (low == ~0 && (HOST_BITS_PER_WIDE_INT >= 64 || high == ~0)))
13159 return 0;
13161 if (HOST_BITS_PER_WIDE_INT < 64 && low == 0)
13163 shift_mask_high = ~0;
13164 if (INTVAL (shiftop) > 32)
13165 shift_mask_high <<= INTVAL (shiftop) - 32;
13167 lsb = high & -high;
13169 if (-lsb != shift_mask_high || INTVAL (shiftop) < 32)
13170 return 0;
13172 high = ~high;
13173 high &= -lsb;
13175 lsb = high & -high;
13176 return high == -lsb;
13179 shift_mask_low = ~0;
13180 shift_mask_low <<= INTVAL (shiftop);
13182 lsb = low & -low;
13184 if (-lsb != shift_mask_low)
13185 return 0;
13187 if (HOST_BITS_PER_WIDE_INT < 64)
13188 high = ~high;
13189 low = ~low;
13190 low &= -lsb;
13192 if (HOST_BITS_PER_WIDE_INT < 64 && low == 0)
13194 lsb = high & -high;
13195 return high == -lsb;
13198 lsb = low & -low;
13199 return low == -lsb && (HOST_BITS_PER_WIDE_INT >= 64 || high == ~0);
13201 else
13202 return 0;
13205 /* Return 1 if ANDOP is a mask suitable for use with an rldicr insn
13206 to perform a left shift. It must have SHIFTOP or more least
13207 significant 0's, with the remainder of the word 1's. */
13210 includes_rldicr_lshift_p (rtx shiftop, rtx andop)
13212 if (GET_CODE (andop) == CONST_INT)
13214 HOST_WIDE_INT c, lsb, shift_mask;
13216 shift_mask = ~0;
13217 shift_mask <<= INTVAL (shiftop);
13218 c = INTVAL (andop);
13220 /* Find the least significant one bit. */
13221 lsb = c & -c;
13223 /* It must be covered by the shift mask.
13224 This test also rejects c == 0. */
13225 if ((lsb & shift_mask) == 0)
13226 return 0;
13228 /* Check we have all 1's above the transition, and reject all 1's. */
13229 return c == -lsb && lsb != 1;
13231 else if (GET_CODE (andop) == CONST_DOUBLE
13232 && (GET_MODE (andop) == VOIDmode || GET_MODE (andop) == DImode))
13234 HOST_WIDE_INT low, lsb, shift_mask_low;
13236 low = CONST_DOUBLE_LOW (andop);
13238 if (HOST_BITS_PER_WIDE_INT < 64)
13240 HOST_WIDE_INT high, shift_mask_high;
13242 high = CONST_DOUBLE_HIGH (andop);
13244 if (low == 0)
13246 shift_mask_high = ~0;
13247 if (INTVAL (shiftop) > 32)
13248 shift_mask_high <<= INTVAL (shiftop) - 32;
13250 lsb = high & -high;
13252 if ((lsb & shift_mask_high) == 0)
13253 return 0;
13255 return high == -lsb;
13257 if (high != ~0)
13258 return 0;
13261 shift_mask_low = ~0;
13262 shift_mask_low <<= INTVAL (shiftop);
13264 lsb = low & -low;
13266 if ((lsb & shift_mask_low) == 0)
13267 return 0;
13269 return low == -lsb && lsb != 1;
13271 else
13272 return 0;
13275 /* Return 1 if operands will generate a valid arguments to rlwimi
13276 instruction for insert with right shift in 64-bit mode. The mask may
13277 not start on the first bit or stop on the last bit because wrap-around
13278 effects of instruction do not correspond to semantics of RTL insn. */
13281 insvdi_rshift_rlwimi_p (rtx sizeop, rtx startop, rtx shiftop)
13283 if (INTVAL (startop) > 32
13284 && INTVAL (startop) < 64
13285 && INTVAL (sizeop) > 1
13286 && INTVAL (sizeop) + INTVAL (startop) < 64
13287 && INTVAL (shiftop) > 0
13288 && INTVAL (sizeop) + INTVAL (shiftop) < 32
13289 && (64 - (INTVAL (shiftop) & 63)) >= INTVAL (sizeop))
13290 return 1;
13292 return 0;
13295 /* Return 1 if REGNO (reg1) == REGNO (reg2) - 1 making them candidates
13296 for lfq and stfq insns iff the registers are hard registers. */
13299 registers_ok_for_quad_peep (rtx reg1, rtx reg2)
13301 /* We might have been passed a SUBREG. */
13302 if (GET_CODE (reg1) != REG || GET_CODE (reg2) != REG)
13303 return 0;
13305 /* We might have been passed non floating point registers. */
13306 if (!FP_REGNO_P (REGNO (reg1))
13307 || !FP_REGNO_P (REGNO (reg2)))
13308 return 0;
13310 return (REGNO (reg1) == REGNO (reg2) - 1);
13313 /* Return 1 if addr1 and addr2 are suitable for lfq or stfq insn.
13314 addr1 and addr2 must be in consecutive memory locations
13315 (addr2 == addr1 + 8). */
13318 mems_ok_for_quad_peep (rtx mem1, rtx mem2)
13320 rtx addr1, addr2;
13321 unsigned int reg1, reg2;
13322 int offset1, offset2;
13324 /* The mems cannot be volatile. */
13325 if (MEM_VOLATILE_P (mem1) || MEM_VOLATILE_P (mem2))
13326 return 0;
13328 addr1 = XEXP (mem1, 0);
13329 addr2 = XEXP (mem2, 0);
13331 /* Extract an offset (if used) from the first addr. */
13332 if (GET_CODE (addr1) == PLUS)
13334 /* If not a REG, return zero. */
13335 if (GET_CODE (XEXP (addr1, 0)) != REG)
13336 return 0;
13337 else
13339 reg1 = REGNO (XEXP (addr1, 0));
13340 /* The offset must be constant! */
13341 if (GET_CODE (XEXP (addr1, 1)) != CONST_INT)
13342 return 0;
13343 offset1 = INTVAL (XEXP (addr1, 1));
13346 else if (GET_CODE (addr1) != REG)
13347 return 0;
13348 else
13350 reg1 = REGNO (addr1);
13351 /* This was a simple (mem (reg)) expression. Offset is 0. */
13352 offset1 = 0;
13355 /* And now for the second addr. */
13356 if (GET_CODE (addr2) == PLUS)
13358 /* If not a REG, return zero. */
13359 if (GET_CODE (XEXP (addr2, 0)) != REG)
13360 return 0;
13361 else
13363 reg2 = REGNO (XEXP (addr2, 0));
13364 /* The offset must be constant. */
13365 if (GET_CODE (XEXP (addr2, 1)) != CONST_INT)
13366 return 0;
13367 offset2 = INTVAL (XEXP (addr2, 1));
13370 else if (GET_CODE (addr2) != REG)
13371 return 0;
13372 else
13374 reg2 = REGNO (addr2);
13375 /* This was a simple (mem (reg)) expression. Offset is 0. */
13376 offset2 = 0;
13379 /* Both of these must have the same base register. */
13380 if (reg1 != reg2)
13381 return 0;
13383 /* The offset for the second addr must be 8 more than the first addr. */
13384 if (offset2 != offset1 + 8)
13385 return 0;
13387 /* All the tests passed. addr1 and addr2 are valid for lfq or stfq
13388 instructions. */
13389 return 1;
13394 rs6000_secondary_memory_needed_rtx (enum machine_mode mode)
13396 static bool eliminated = false;
13397 rtx ret;
13399 if (mode != SDmode)
13400 ret = assign_stack_local (mode, GET_MODE_SIZE (mode), 0);
13401 else
13403 rtx mem = cfun->machine->sdmode_stack_slot;
13404 gcc_assert (mem != NULL_RTX);
13406 if (!eliminated)
13408 mem = eliminate_regs (mem, VOIDmode, NULL_RTX);
13409 cfun->machine->sdmode_stack_slot = mem;
13410 eliminated = true;
13412 ret = mem;
13415 if (TARGET_DEBUG_ADDR)
13417 fprintf (stderr, "\nrs6000_secondary_memory_needed_rtx, mode %s, rtx:\n",
13418 GET_MODE_NAME (mode));
13419 if (!ret)
13420 fprintf (stderr, "\tNULL_RTX\n");
13421 else
13422 debug_rtx (ret);
13425 return ret;
13428 static tree
13429 rs6000_check_sdmode (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
13431 /* Don't walk into types. */
13432 if (*tp == NULL_TREE || *tp == error_mark_node || TYPE_P (*tp))
13434 *walk_subtrees = 0;
13435 return NULL_TREE;
13438 switch (TREE_CODE (*tp))
13440 case VAR_DECL:
13441 case PARM_DECL:
13442 case FIELD_DECL:
13443 case RESULT_DECL:
13444 case SSA_NAME:
13445 case REAL_CST:
13446 case MEM_REF:
13447 case VIEW_CONVERT_EXPR:
13448 if (TYPE_MODE (TREE_TYPE (*tp)) == SDmode)
13449 return *tp;
13450 break;
13451 default:
13452 break;
13455 return NULL_TREE;
13458 enum reload_reg_type {
13459 GPR_REGISTER_TYPE,
13460 VECTOR_REGISTER_TYPE,
13461 OTHER_REGISTER_TYPE
13464 static enum reload_reg_type
13465 rs6000_reload_register_type (enum reg_class rclass)
13467 switch (rclass)
13469 case GENERAL_REGS:
13470 case BASE_REGS:
13471 return GPR_REGISTER_TYPE;
13473 case FLOAT_REGS:
13474 case ALTIVEC_REGS:
13475 case VSX_REGS:
13476 return VECTOR_REGISTER_TYPE;
13478 default:
13479 return OTHER_REGISTER_TYPE;
13483 /* Inform reload about cases where moving X with a mode MODE to a register in
13484 RCLASS requires an extra scratch or immediate register. Return the class
13485 needed for the immediate register.
13487 For VSX and Altivec, we may need a register to convert sp+offset into
13488 reg+sp.
13490 For misaligned 64-bit gpr loads and stores we need a register to
13491 convert an offset address to indirect. */
13493 static reg_class_t
13494 rs6000_secondary_reload (bool in_p,
13495 rtx x,
13496 reg_class_t rclass_i,
13497 enum machine_mode mode,
13498 secondary_reload_info *sri)
13500 enum reg_class rclass = (enum reg_class) rclass_i;
13501 reg_class_t ret = ALL_REGS;
13502 enum insn_code icode;
13503 bool default_p = false;
13505 sri->icode = CODE_FOR_nothing;
13507 /* Convert vector loads and stores into gprs to use an additional base
13508 register. */
13509 icode = rs6000_vector_reload[mode][in_p != false];
13510 if (icode != CODE_FOR_nothing)
13512 ret = NO_REGS;
13513 sri->icode = CODE_FOR_nothing;
13514 sri->extra_cost = 0;
13516 if (GET_CODE (x) == MEM)
13518 rtx addr = XEXP (x, 0);
13520 /* Loads to and stores from gprs can do reg+offset, and wouldn't need
13521 an extra register in that case, but it would need an extra
13522 register if the addressing is reg+reg or (reg+reg)&(-16). */
13523 if (rclass == GENERAL_REGS || rclass == BASE_REGS)
13525 if (!legitimate_indirect_address_p (addr, false)
13526 && !rs6000_legitimate_offset_address_p (TImode, addr,
13527 false, true))
13529 sri->icode = icode;
13530 /* account for splitting the loads, and converting the
13531 address from reg+reg to reg. */
13532 sri->extra_cost = (((TARGET_64BIT) ? 3 : 5)
13533 + ((GET_CODE (addr) == AND) ? 1 : 0));
13536 /* Loads to and stores from vector registers can only do reg+reg
13537 addressing. Altivec registers can also do (reg+reg)&(-16). */
13538 else if (rclass == VSX_REGS || rclass == ALTIVEC_REGS
13539 || rclass == FLOAT_REGS || rclass == NO_REGS)
13541 if (!VECTOR_MEM_ALTIVEC_P (mode)
13542 && GET_CODE (addr) == AND
13543 && GET_CODE (XEXP (addr, 1)) == CONST_INT
13544 && INTVAL (XEXP (addr, 1)) == -16
13545 && (legitimate_indirect_address_p (XEXP (addr, 0), false)
13546 || legitimate_indexed_address_p (XEXP (addr, 0), false)))
13548 sri->icode = icode;
13549 sri->extra_cost = ((GET_CODE (XEXP (addr, 0)) == PLUS)
13550 ? 2 : 1);
13552 else if (!legitimate_indirect_address_p (addr, false)
13553 && (rclass == NO_REGS
13554 || !legitimate_indexed_address_p (addr, false)))
13556 sri->icode = icode;
13557 sri->extra_cost = 1;
13559 else
13560 icode = CODE_FOR_nothing;
13562 /* Any other loads, including to pseudo registers which haven't been
13563 assigned to a register yet, default to require a scratch
13564 register. */
13565 else
13567 sri->icode = icode;
13568 sri->extra_cost = 2;
13571 else if (REG_P (x))
13573 int regno = true_regnum (x);
13575 icode = CODE_FOR_nothing;
13576 if (regno < 0 || regno >= FIRST_PSEUDO_REGISTER)
13577 default_p = true;
13578 else
13580 enum reg_class xclass = REGNO_REG_CLASS (regno);
13581 enum reload_reg_type rtype1 = rs6000_reload_register_type (rclass);
13582 enum reload_reg_type rtype2 = rs6000_reload_register_type (xclass);
13584 /* If memory is needed, use default_secondary_reload to create the
13585 stack slot. */
13586 if (rtype1 != rtype2 || rtype1 == OTHER_REGISTER_TYPE)
13587 default_p = true;
13588 else
13589 ret = NO_REGS;
13592 else
13593 default_p = true;
13595 else if (TARGET_POWERPC64
13596 && rs6000_reload_register_type (rclass) == GPR_REGISTER_TYPE
13597 && MEM_P (x)
13598 && GET_MODE_SIZE (GET_MODE (x)) >= UNITS_PER_WORD)
13600 rtx off = address_offset (XEXP (x, 0));
13602 if (off != NULL_RTX && (INTVAL (off) & 3) != 0)
13604 if (in_p)
13605 sri->icode = CODE_FOR_reload_di_load;
13606 else
13607 sri->icode = CODE_FOR_reload_di_store;
13608 sri->extra_cost = 2;
13609 ret = NO_REGS;
13611 else
13612 default_p = true;
13614 else if (!TARGET_POWERPC64
13615 && rs6000_reload_register_type (rclass) == GPR_REGISTER_TYPE
13616 && MEM_P (x)
13617 && GET_MODE_SIZE (GET_MODE (x)) > UNITS_PER_WORD)
13619 rtx off = address_offset (XEXP (x, 0));
13621 if (off != NULL_RTX
13622 && ((unsigned HOST_WIDE_INT) INTVAL (off) + 0x8000
13623 >= 0x1000u - (GET_MODE_SIZE (GET_MODE (x)) - UNITS_PER_WORD)))
13625 if (in_p)
13626 sri->icode = CODE_FOR_reload_si_load;
13627 else
13628 sri->icode = CODE_FOR_reload_si_store;
13629 sri->extra_cost = 2;
13630 ret = NO_REGS;
13632 else
13633 default_p = true;
13635 else
13636 default_p = true;
13638 if (default_p)
13639 ret = default_secondary_reload (in_p, x, rclass, mode, sri);
13641 gcc_assert (ret != ALL_REGS);
13643 if (TARGET_DEBUG_ADDR)
13645 fprintf (stderr,
13646 "\nrs6000_secondary_reload, return %s, in_p = %s, rclass = %s, "
13647 "mode = %s",
13648 reg_class_names[ret],
13649 in_p ? "true" : "false",
13650 reg_class_names[rclass],
13651 GET_MODE_NAME (mode));
13653 if (default_p)
13654 fprintf (stderr, ", default secondary reload");
13656 if (sri->icode != CODE_FOR_nothing)
13657 fprintf (stderr, ", reload func = %s, extra cost = %d\n",
13658 insn_data[sri->icode].name, sri->extra_cost);
13659 else
13660 fprintf (stderr, "\n");
13662 debug_rtx (x);
13665 return ret;
13668 /* Fixup reload addresses for Altivec or VSX loads/stores to change SP+offset
13669 to SP+reg addressing. */
13671 void
13672 rs6000_secondary_reload_inner (rtx reg, rtx mem, rtx scratch, bool store_p)
13674 int regno = true_regnum (reg);
13675 enum machine_mode mode = GET_MODE (reg);
13676 enum reg_class rclass;
13677 rtx addr;
13678 rtx and_op2 = NULL_RTX;
13679 rtx addr_op1;
13680 rtx addr_op2;
13681 rtx scratch_or_premodify = scratch;
13682 rtx and_rtx;
13683 rtx cc_clobber;
13685 if (TARGET_DEBUG_ADDR)
13687 fprintf (stderr, "\nrs6000_secondary_reload_inner, type = %s\n",
13688 store_p ? "store" : "load");
13689 fprintf (stderr, "reg:\n");
13690 debug_rtx (reg);
13691 fprintf (stderr, "mem:\n");
13692 debug_rtx (mem);
13693 fprintf (stderr, "scratch:\n");
13694 debug_rtx (scratch);
13697 gcc_assert (regno >= 0 && regno < FIRST_PSEUDO_REGISTER);
13698 gcc_assert (GET_CODE (mem) == MEM);
13699 rclass = REGNO_REG_CLASS (regno);
13700 addr = XEXP (mem, 0);
13702 switch (rclass)
13704 /* GPRs can handle reg + small constant, all other addresses need to use
13705 the scratch register. */
13706 case GENERAL_REGS:
13707 case BASE_REGS:
13708 if (GET_CODE (addr) == AND)
13710 and_op2 = XEXP (addr, 1);
13711 addr = XEXP (addr, 0);
13714 if (GET_CODE (addr) == PRE_MODIFY)
13716 scratch_or_premodify = XEXP (addr, 0);
13717 gcc_assert (REG_P (scratch_or_premodify));
13718 gcc_assert (GET_CODE (XEXP (addr, 1)) == PLUS);
13719 addr = XEXP (addr, 1);
13722 if (GET_CODE (addr) == PLUS
13723 && (and_op2 != NULL_RTX
13724 || !rs6000_legitimate_offset_address_p (TImode, addr,
13725 false, true)))
13727 addr_op1 = XEXP (addr, 0);
13728 addr_op2 = XEXP (addr, 1);
13729 gcc_assert (legitimate_indirect_address_p (addr_op1, false));
13731 if (!REG_P (addr_op2)
13732 && (GET_CODE (addr_op2) != CONST_INT
13733 || !satisfies_constraint_I (addr_op2)))
13735 if (TARGET_DEBUG_ADDR)
13737 fprintf (stderr,
13738 "\nMove plus addr to register %s, mode = %s: ",
13739 rs6000_reg_names[REGNO (scratch)],
13740 GET_MODE_NAME (mode));
13741 debug_rtx (addr_op2);
13743 rs6000_emit_move (scratch, addr_op2, Pmode);
13744 addr_op2 = scratch;
13747 emit_insn (gen_rtx_SET (VOIDmode,
13748 scratch_or_premodify,
13749 gen_rtx_PLUS (Pmode,
13750 addr_op1,
13751 addr_op2)));
13753 addr = scratch_or_premodify;
13754 scratch_or_premodify = scratch;
13756 else if (!legitimate_indirect_address_p (addr, false)
13757 && !rs6000_legitimate_offset_address_p (TImode, addr,
13758 false, true))
13760 if (TARGET_DEBUG_ADDR)
13762 fprintf (stderr, "\nMove addr to register %s, mode = %s: ",
13763 rs6000_reg_names[REGNO (scratch_or_premodify)],
13764 GET_MODE_NAME (mode));
13765 debug_rtx (addr);
13767 rs6000_emit_move (scratch_or_premodify, addr, Pmode);
13768 addr = scratch_or_premodify;
13769 scratch_or_premodify = scratch;
13771 break;
13773 /* Float/Altivec registers can only handle reg+reg addressing. Move
13774 other addresses into a scratch register. */
13775 case FLOAT_REGS:
13776 case VSX_REGS:
13777 case ALTIVEC_REGS:
13779 /* With float regs, we need to handle the AND ourselves, since we can't
13780 use the Altivec instruction with an implicit AND -16. Allow scalar
13781 loads to float registers to use reg+offset even if VSX. */
13782 if (GET_CODE (addr) == AND
13783 && (rclass != ALTIVEC_REGS || GET_MODE_SIZE (mode) != 16
13784 || GET_CODE (XEXP (addr, 1)) != CONST_INT
13785 || INTVAL (XEXP (addr, 1)) != -16
13786 || !VECTOR_MEM_ALTIVEC_P (mode)))
13788 and_op2 = XEXP (addr, 1);
13789 addr = XEXP (addr, 0);
13792 /* If we aren't using a VSX load, save the PRE_MODIFY register and use it
13793 as the address later. */
13794 if (GET_CODE (addr) == PRE_MODIFY
13795 && (!VECTOR_MEM_VSX_P (mode)
13796 || and_op2 != NULL_RTX
13797 || !legitimate_indexed_address_p (XEXP (addr, 1), false)))
13799 scratch_or_premodify = XEXP (addr, 0);
13800 gcc_assert (legitimate_indirect_address_p (scratch_or_premodify,
13801 false));
13802 gcc_assert (GET_CODE (XEXP (addr, 1)) == PLUS);
13803 addr = XEXP (addr, 1);
13806 if (legitimate_indirect_address_p (addr, false) /* reg */
13807 || legitimate_indexed_address_p (addr, false) /* reg+reg */
13808 || GET_CODE (addr) == PRE_MODIFY /* VSX pre-modify */
13809 || (GET_CODE (addr) == AND /* Altivec memory */
13810 && GET_CODE (XEXP (addr, 1)) == CONST_INT
13811 && INTVAL (XEXP (addr, 1)) == -16
13812 && VECTOR_MEM_ALTIVEC_P (mode))
13813 || (rclass == FLOAT_REGS /* legacy float mem */
13814 && GET_MODE_SIZE (mode) == 8
13815 && and_op2 == NULL_RTX
13816 && scratch_or_premodify == scratch
13817 && rs6000_legitimate_offset_address_p (mode, addr, false, false)))
13820 else if (GET_CODE (addr) == PLUS)
13822 addr_op1 = XEXP (addr, 0);
13823 addr_op2 = XEXP (addr, 1);
13824 gcc_assert (REG_P (addr_op1));
13826 if (TARGET_DEBUG_ADDR)
13828 fprintf (stderr, "\nMove plus addr to register %s, mode = %s: ",
13829 rs6000_reg_names[REGNO (scratch)], GET_MODE_NAME (mode));
13830 debug_rtx (addr_op2);
13832 rs6000_emit_move (scratch, addr_op2, Pmode);
13833 emit_insn (gen_rtx_SET (VOIDmode,
13834 scratch_or_premodify,
13835 gen_rtx_PLUS (Pmode,
13836 addr_op1,
13837 scratch)));
13838 addr = scratch_or_premodify;
13839 scratch_or_premodify = scratch;
13842 else if (GET_CODE (addr) == SYMBOL_REF || GET_CODE (addr) == CONST
13843 || GET_CODE (addr) == CONST_INT || REG_P (addr))
13845 if (TARGET_DEBUG_ADDR)
13847 fprintf (stderr, "\nMove addr to register %s, mode = %s: ",
13848 rs6000_reg_names[REGNO (scratch_or_premodify)],
13849 GET_MODE_NAME (mode));
13850 debug_rtx (addr);
13853 rs6000_emit_move (scratch_or_premodify, addr, Pmode);
13854 addr = scratch_or_premodify;
13855 scratch_or_premodify = scratch;
13858 else
13859 gcc_unreachable ();
13861 break;
13863 default:
13864 gcc_unreachable ();
13867 /* If the original address involved a pre-modify that we couldn't use the VSX
13868 memory instruction with update, and we haven't taken care of already,
13869 store the address in the pre-modify register and use that as the
13870 address. */
13871 if (scratch_or_premodify != scratch && scratch_or_premodify != addr)
13873 emit_insn (gen_rtx_SET (VOIDmode, scratch_or_premodify, addr));
13874 addr = scratch_or_premodify;
13877 /* If the original address involved an AND -16 and we couldn't use an ALTIVEC
13878 memory instruction, recreate the AND now, including the clobber which is
13879 generated by the general ANDSI3/ANDDI3 patterns for the
13880 andi. instruction. */
13881 if (and_op2 != NULL_RTX)
13883 if (! legitimate_indirect_address_p (addr, false))
13885 emit_insn (gen_rtx_SET (VOIDmode, scratch, addr));
13886 addr = scratch;
13889 if (TARGET_DEBUG_ADDR)
13891 fprintf (stderr, "\nAnd addr to register %s, mode = %s: ",
13892 rs6000_reg_names[REGNO (scratch)], GET_MODE_NAME (mode));
13893 debug_rtx (and_op2);
13896 and_rtx = gen_rtx_SET (VOIDmode,
13897 scratch,
13898 gen_rtx_AND (Pmode,
13899 addr,
13900 and_op2));
13902 cc_clobber = gen_rtx_CLOBBER (CCmode, gen_rtx_SCRATCH (CCmode));
13903 emit_insn (gen_rtx_PARALLEL (VOIDmode,
13904 gen_rtvec (2, and_rtx, cc_clobber)));
13905 addr = scratch;
13908 /* Adjust the address if it changed. */
13909 if (addr != XEXP (mem, 0))
13911 mem = change_address (mem, mode, addr);
13912 if (TARGET_DEBUG_ADDR)
13913 fprintf (stderr, "\nrs6000_secondary_reload_inner, mem adjusted.\n");
13916 /* Now create the move. */
13917 if (store_p)
13918 emit_insn (gen_rtx_SET (VOIDmode, mem, reg));
13919 else
13920 emit_insn (gen_rtx_SET (VOIDmode, reg, mem));
13922 return;
13925 /* Convert reloads involving 64-bit gprs and misaligned offset
13926 addressing, or multiple 32-bit gprs and offsets that are too large,
13927 to use indirect addressing. */
13929 void
13930 rs6000_secondary_reload_gpr (rtx reg, rtx mem, rtx scratch, bool store_p)
13932 int regno = true_regnum (reg);
13933 enum reg_class rclass;
13934 rtx addr;
13935 rtx scratch_or_premodify = scratch;
13937 if (TARGET_DEBUG_ADDR)
13939 fprintf (stderr, "\nrs6000_secondary_reload_gpr, type = %s\n",
13940 store_p ? "store" : "load");
13941 fprintf (stderr, "reg:\n");
13942 debug_rtx (reg);
13943 fprintf (stderr, "mem:\n");
13944 debug_rtx (mem);
13945 fprintf (stderr, "scratch:\n");
13946 debug_rtx (scratch);
13949 gcc_assert (regno >= 0 && regno < FIRST_PSEUDO_REGISTER);
13950 gcc_assert (GET_CODE (mem) == MEM);
13951 rclass = REGNO_REG_CLASS (regno);
13952 gcc_assert (rclass == GENERAL_REGS || rclass == BASE_REGS);
13953 addr = XEXP (mem, 0);
13955 if (GET_CODE (addr) == PRE_MODIFY)
13957 scratch_or_premodify = XEXP (addr, 0);
13958 gcc_assert (REG_P (scratch_or_premodify));
13959 addr = XEXP (addr, 1);
13961 gcc_assert (GET_CODE (addr) == PLUS || GET_CODE (addr) == LO_SUM);
13963 rs6000_emit_move (scratch_or_premodify, addr, Pmode);
13965 mem = replace_equiv_address_nv (mem, scratch_or_premodify);
13967 /* Now create the move. */
13968 if (store_p)
13969 emit_insn (gen_rtx_SET (VOIDmode, mem, reg));
13970 else
13971 emit_insn (gen_rtx_SET (VOIDmode, reg, mem));
13973 return;
13976 /* Allocate a 64-bit stack slot to be used for copying SDmode
13977 values through if this function has any SDmode references. */
13979 static void
13980 rs6000_alloc_sdmode_stack_slot (void)
13982 tree t;
13983 basic_block bb;
13984 gimple_stmt_iterator gsi;
13986 gcc_assert (cfun->machine->sdmode_stack_slot == NULL_RTX);
13988 FOR_EACH_BB (bb)
13989 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
13991 tree ret = walk_gimple_op (gsi_stmt (gsi), rs6000_check_sdmode, NULL);
13992 if (ret)
13994 rtx stack = assign_stack_local (DDmode, GET_MODE_SIZE (DDmode), 0);
13995 cfun->machine->sdmode_stack_slot = adjust_address_nv (stack,
13996 SDmode, 0);
13997 return;
14001 /* Check for any SDmode parameters of the function. */
14002 for (t = DECL_ARGUMENTS (cfun->decl); t; t = DECL_CHAIN (t))
14004 if (TREE_TYPE (t) == error_mark_node)
14005 continue;
14007 if (TYPE_MODE (TREE_TYPE (t)) == SDmode
14008 || TYPE_MODE (DECL_ARG_TYPE (t)) == SDmode)
14010 rtx stack = assign_stack_local (DDmode, GET_MODE_SIZE (DDmode), 0);
14011 cfun->machine->sdmode_stack_slot = adjust_address_nv (stack,
14012 SDmode, 0);
14013 return;
14018 static void
14019 rs6000_instantiate_decls (void)
14021 if (cfun->machine->sdmode_stack_slot != NULL_RTX)
14022 instantiate_decl_rtl (cfun->machine->sdmode_stack_slot);
14025 /* Given an rtx X being reloaded into a reg required to be
14026 in class CLASS, return the class of reg to actually use.
14027 In general this is just CLASS; but on some machines
14028 in some cases it is preferable to use a more restrictive class.
14030 On the RS/6000, we have to return NO_REGS when we want to reload a
14031 floating-point CONST_DOUBLE to force it to be copied to memory.
14033 We also don't want to reload integer values into floating-point
14034 registers if we can at all help it. In fact, this can
14035 cause reload to die, if it tries to generate a reload of CTR
14036 into a FP register and discovers it doesn't have the memory location
14037 required.
14039 ??? Would it be a good idea to have reload do the converse, that is
14040 try to reload floating modes into FP registers if possible?
14043 static enum reg_class
14044 rs6000_preferred_reload_class (rtx x, enum reg_class rclass)
14046 enum machine_mode mode = GET_MODE (x);
14048 if (VECTOR_UNIT_VSX_P (mode)
14049 && x == CONST0_RTX (mode) && VSX_REG_CLASS_P (rclass))
14050 return rclass;
14052 if (VECTOR_UNIT_ALTIVEC_OR_VSX_P (mode)
14053 && (rclass == ALTIVEC_REGS || rclass == VSX_REGS)
14054 && easy_vector_constant (x, mode))
14055 return ALTIVEC_REGS;
14057 if (CONSTANT_P (x) && reg_classes_intersect_p (rclass, FLOAT_REGS))
14058 return NO_REGS;
14060 if (GET_MODE_CLASS (mode) == MODE_INT && rclass == NON_SPECIAL_REGS)
14061 return GENERAL_REGS;
14063 /* For VSX, prefer the traditional registers for 64-bit values because we can
14064 use the non-VSX loads. Prefer the Altivec registers if Altivec is
14065 handling the vector operations (i.e. V16QI, V8HI, and V4SI), or if we
14066 prefer Altivec loads.. */
14067 if (rclass == VSX_REGS)
14069 if (GET_MODE_SIZE (mode) <= 8)
14070 return FLOAT_REGS;
14072 if (VECTOR_UNIT_ALTIVEC_P (mode) || VECTOR_MEM_ALTIVEC_P (mode))
14073 return ALTIVEC_REGS;
14075 return rclass;
14078 return rclass;
14081 /* Debug version of rs6000_preferred_reload_class. */
14082 static enum reg_class
14083 rs6000_debug_preferred_reload_class (rtx x, enum reg_class rclass)
14085 enum reg_class ret = rs6000_preferred_reload_class (x, rclass);
14087 fprintf (stderr,
14088 "\nrs6000_preferred_reload_class, return %s, rclass = %s, "
14089 "mode = %s, x:\n",
14090 reg_class_names[ret], reg_class_names[rclass],
14091 GET_MODE_NAME (GET_MODE (x)));
14092 debug_rtx (x);
14094 return ret;
14097 /* If we are copying between FP or AltiVec registers and anything else, we need
14098 a memory location. The exception is when we are targeting ppc64 and the
14099 move to/from fpr to gpr instructions are available. Also, under VSX, you
14100 can copy vector registers from the FP register set to the Altivec register
14101 set and vice versa. */
14103 static bool
14104 rs6000_secondary_memory_needed (enum reg_class class1,
14105 enum reg_class class2,
14106 enum machine_mode mode)
14108 if (class1 == class2)
14109 return false;
14111 /* Under VSX, there are 3 register classes that values could be in (VSX_REGS,
14112 ALTIVEC_REGS, and FLOAT_REGS). We don't need to use memory to copy
14113 between these classes. But we need memory for other things that can go in
14114 FLOAT_REGS like SFmode. */
14115 if (TARGET_VSX
14116 && (VECTOR_MEM_VSX_P (mode) || VECTOR_UNIT_VSX_P (mode))
14117 && (class1 == VSX_REGS || class1 == ALTIVEC_REGS
14118 || class1 == FLOAT_REGS))
14119 return (class2 != VSX_REGS && class2 != ALTIVEC_REGS
14120 && class2 != FLOAT_REGS);
14122 if (class1 == VSX_REGS || class2 == VSX_REGS)
14123 return true;
14125 if (class1 == FLOAT_REGS
14126 && (!TARGET_MFPGPR || !TARGET_POWERPC64
14127 || ((mode != DFmode)
14128 && (mode != DDmode)
14129 && (mode != DImode))))
14130 return true;
14132 if (class2 == FLOAT_REGS
14133 && (!TARGET_MFPGPR || !TARGET_POWERPC64
14134 || ((mode != DFmode)
14135 && (mode != DDmode)
14136 && (mode != DImode))))
14137 return true;
14139 if (class1 == ALTIVEC_REGS || class2 == ALTIVEC_REGS)
14140 return true;
14142 return false;
14145 /* Debug version of rs6000_secondary_memory_needed. */
14146 static bool
14147 rs6000_debug_secondary_memory_needed (enum reg_class class1,
14148 enum reg_class class2,
14149 enum machine_mode mode)
14151 bool ret = rs6000_secondary_memory_needed (class1, class2, mode);
14153 fprintf (stderr,
14154 "rs6000_secondary_memory_needed, return: %s, class1 = %s, "
14155 "class2 = %s, mode = %s\n",
14156 ret ? "true" : "false", reg_class_names[class1],
14157 reg_class_names[class2], GET_MODE_NAME (mode));
14159 return ret;
14162 /* Return the register class of a scratch register needed to copy IN into
14163 or out of a register in RCLASS in MODE. If it can be done directly,
14164 NO_REGS is returned. */
14166 static enum reg_class
14167 rs6000_secondary_reload_class (enum reg_class rclass, enum machine_mode mode,
14168 rtx in)
14170 int regno;
14172 if (TARGET_ELF || (DEFAULT_ABI == ABI_DARWIN
14173 #if TARGET_MACHO
14174 && MACHOPIC_INDIRECT
14175 #endif
14178 /* We cannot copy a symbolic operand directly into anything
14179 other than BASE_REGS for TARGET_ELF. So indicate that a
14180 register from BASE_REGS is needed as an intermediate
14181 register.
14183 On Darwin, pic addresses require a load from memory, which
14184 needs a base register. */
14185 if (rclass != BASE_REGS
14186 && (GET_CODE (in) == SYMBOL_REF
14187 || GET_CODE (in) == HIGH
14188 || GET_CODE (in) == LABEL_REF
14189 || GET_CODE (in) == CONST))
14190 return BASE_REGS;
14193 if (GET_CODE (in) == REG)
14195 regno = REGNO (in);
14196 if (regno >= FIRST_PSEUDO_REGISTER)
14198 regno = true_regnum (in);
14199 if (regno >= FIRST_PSEUDO_REGISTER)
14200 regno = -1;
14203 else if (GET_CODE (in) == SUBREG)
14205 regno = true_regnum (in);
14206 if (regno >= FIRST_PSEUDO_REGISTER)
14207 regno = -1;
14209 else
14210 regno = -1;
14212 /* We can place anything into GENERAL_REGS and can put GENERAL_REGS
14213 into anything. */
14214 if (rclass == GENERAL_REGS || rclass == BASE_REGS
14215 || (regno >= 0 && INT_REGNO_P (regno)))
14216 return NO_REGS;
14218 /* Constants, memory, and FP registers can go into FP registers. */
14219 if ((regno == -1 || FP_REGNO_P (regno))
14220 && (rclass == FLOAT_REGS || rclass == NON_SPECIAL_REGS))
14221 return (mode != SDmode) ? NO_REGS : GENERAL_REGS;
14223 /* Memory, and FP/altivec registers can go into fp/altivec registers under
14224 VSX. */
14225 if (TARGET_VSX
14226 && (regno == -1 || VSX_REGNO_P (regno))
14227 && VSX_REG_CLASS_P (rclass))
14228 return NO_REGS;
14230 /* Memory, and AltiVec registers can go into AltiVec registers. */
14231 if ((regno == -1 || ALTIVEC_REGNO_P (regno))
14232 && rclass == ALTIVEC_REGS)
14233 return NO_REGS;
14235 /* We can copy among the CR registers. */
14236 if ((rclass == CR_REGS || rclass == CR0_REGS)
14237 && regno >= 0 && CR_REGNO_P (regno))
14238 return NO_REGS;
14240 /* Otherwise, we need GENERAL_REGS. */
14241 return GENERAL_REGS;
14244 /* Debug version of rs6000_secondary_reload_class. */
14245 static enum reg_class
14246 rs6000_debug_secondary_reload_class (enum reg_class rclass,
14247 enum machine_mode mode, rtx in)
14249 enum reg_class ret = rs6000_secondary_reload_class (rclass, mode, in);
14250 fprintf (stderr,
14251 "\nrs6000_secondary_reload_class, return %s, rclass = %s, "
14252 "mode = %s, input rtx:\n",
14253 reg_class_names[ret], reg_class_names[rclass],
14254 GET_MODE_NAME (mode));
14255 debug_rtx (in);
14257 return ret;
14260 /* Return nonzero if for CLASS a mode change from FROM to TO is invalid. */
14262 static bool
14263 rs6000_cannot_change_mode_class (enum machine_mode from,
14264 enum machine_mode to,
14265 enum reg_class rclass)
14267 unsigned from_size = GET_MODE_SIZE (from);
14268 unsigned to_size = GET_MODE_SIZE (to);
14270 if (from_size != to_size)
14272 enum reg_class xclass = (TARGET_VSX) ? VSX_REGS : FLOAT_REGS;
14273 return ((from_size < 8 || to_size < 8 || TARGET_IEEEQUAD)
14274 && reg_classes_intersect_p (xclass, rclass));
14277 if (TARGET_E500_DOUBLE
14278 && ((((to) == DFmode) + ((from) == DFmode)) == 1
14279 || (((to) == TFmode) + ((from) == TFmode)) == 1
14280 || (((to) == DDmode) + ((from) == DDmode)) == 1
14281 || (((to) == TDmode) + ((from) == TDmode)) == 1
14282 || (((to) == DImode) + ((from) == DImode)) == 1))
14283 return true;
14285 /* Since the VSX register set includes traditional floating point registers
14286 and altivec registers, just check for the size being different instead of
14287 trying to check whether the modes are vector modes. Otherwise it won't
14288 allow say DF and DI to change classes. */
14289 if (TARGET_VSX && VSX_REG_CLASS_P (rclass))
14290 return (from_size != 8 && from_size != 16);
14292 if (TARGET_ALTIVEC && rclass == ALTIVEC_REGS
14293 && (ALTIVEC_VECTOR_MODE (from) + ALTIVEC_VECTOR_MODE (to)) == 1)
14294 return true;
14296 if (TARGET_SPE && (SPE_VECTOR_MODE (from) + SPE_VECTOR_MODE (to)) == 1
14297 && reg_classes_intersect_p (GENERAL_REGS, rclass))
14298 return true;
14300 return false;
14303 /* Debug version of rs6000_cannot_change_mode_class. */
14304 static bool
14305 rs6000_debug_cannot_change_mode_class (enum machine_mode from,
14306 enum machine_mode to,
14307 enum reg_class rclass)
14309 bool ret = rs6000_cannot_change_mode_class (from, to, rclass);
14311 fprintf (stderr,
14312 "rs6000_cannot_change_mode_class, return %s, from = %s, "
14313 "to = %s, rclass = %s\n",
14314 ret ? "true" : "false",
14315 GET_MODE_NAME (from), GET_MODE_NAME (to),
14316 reg_class_names[rclass]);
14318 return ret;
14321 /* Given a comparison operation, return the bit number in CCR to test. We
14322 know this is a valid comparison.
14324 SCC_P is 1 if this is for an scc. That means that %D will have been
14325 used instead of %C, so the bits will be in different places.
14327 Return -1 if OP isn't a valid comparison for some reason. */
14330 ccr_bit (rtx op, int scc_p)
14332 enum rtx_code code = GET_CODE (op);
14333 enum machine_mode cc_mode;
14334 int cc_regnum;
14335 int base_bit;
14336 rtx reg;
14338 if (!COMPARISON_P (op))
14339 return -1;
14341 reg = XEXP (op, 0);
14343 gcc_assert (GET_CODE (reg) == REG && CR_REGNO_P (REGNO (reg)));
14345 cc_mode = GET_MODE (reg);
14346 cc_regnum = REGNO (reg);
14347 base_bit = 4 * (cc_regnum - CR0_REGNO);
14349 validate_condition_mode (code, cc_mode);
14351 /* When generating a sCOND operation, only positive conditions are
14352 allowed. */
14353 gcc_assert (!scc_p
14354 || code == EQ || code == GT || code == LT || code == UNORDERED
14355 || code == GTU || code == LTU);
14357 switch (code)
14359 case NE:
14360 return scc_p ? base_bit + 3 : base_bit + 2;
14361 case EQ:
14362 return base_bit + 2;
14363 case GT: case GTU: case UNLE:
14364 return base_bit + 1;
14365 case LT: case LTU: case UNGE:
14366 return base_bit;
14367 case ORDERED: case UNORDERED:
14368 return base_bit + 3;
14370 case GE: case GEU:
14371 /* If scc, we will have done a cror to put the bit in the
14372 unordered position. So test that bit. For integer, this is ! LT
14373 unless this is an scc insn. */
14374 return scc_p ? base_bit + 3 : base_bit;
14376 case LE: case LEU:
14377 return scc_p ? base_bit + 3 : base_bit + 1;
14379 default:
14380 gcc_unreachable ();
14384 /* Return the GOT register. */
14387 rs6000_got_register (rtx value ATTRIBUTE_UNUSED)
14389 /* The second flow pass currently (June 1999) can't update
14390 regs_ever_live without disturbing other parts of the compiler, so
14391 update it here to make the prolog/epilogue code happy. */
14392 if (!can_create_pseudo_p ()
14393 && !df_regs_ever_live_p (RS6000_PIC_OFFSET_TABLE_REGNUM))
14394 df_set_regs_ever_live (RS6000_PIC_OFFSET_TABLE_REGNUM, true);
14396 crtl->uses_pic_offset_table = 1;
14398 return pic_offset_table_rtx;
14401 static rs6000_stack_t stack_info;
14403 /* Function to init struct machine_function.
14404 This will be called, via a pointer variable,
14405 from push_function_context. */
14407 static struct machine_function *
14408 rs6000_init_machine_status (void)
14410 stack_info.reload_completed = 0;
14411 return ggc_alloc_cleared_machine_function ();
14414 /* These macros test for integers and extract the low-order bits. */
14415 #define INT_P(X) \
14416 ((GET_CODE (X) == CONST_INT || GET_CODE (X) == CONST_DOUBLE) \
14417 && GET_MODE (X) == VOIDmode)
14419 #define INT_LOWPART(X) \
14420 (GET_CODE (X) == CONST_INT ? INTVAL (X) : CONST_DOUBLE_LOW (X))
14423 extract_MB (rtx op)
14425 int i;
14426 unsigned long val = INT_LOWPART (op);
14428 /* If the high bit is zero, the value is the first 1 bit we find
14429 from the left. */
14430 if ((val & 0x80000000) == 0)
14432 gcc_assert (val & 0xffffffff);
14434 i = 1;
14435 while (((val <<= 1) & 0x80000000) == 0)
14436 ++i;
14437 return i;
14440 /* If the high bit is set and the low bit is not, or the mask is all
14441 1's, the value is zero. */
14442 if ((val & 1) == 0 || (val & 0xffffffff) == 0xffffffff)
14443 return 0;
14445 /* Otherwise we have a wrap-around mask. Look for the first 0 bit
14446 from the right. */
14447 i = 31;
14448 while (((val >>= 1) & 1) != 0)
14449 --i;
14451 return i;
14455 extract_ME (rtx op)
14457 int i;
14458 unsigned long val = INT_LOWPART (op);
14460 /* If the low bit is zero, the value is the first 1 bit we find from
14461 the right. */
14462 if ((val & 1) == 0)
14464 gcc_assert (val & 0xffffffff);
14466 i = 30;
14467 while (((val >>= 1) & 1) == 0)
14468 --i;
14470 return i;
14473 /* If the low bit is set and the high bit is not, or the mask is all
14474 1's, the value is 31. */
14475 if ((val & 0x80000000) == 0 || (val & 0xffffffff) == 0xffffffff)
14476 return 31;
14478 /* Otherwise we have a wrap-around mask. Look for the first 0 bit
14479 from the left. */
14480 i = 0;
14481 while (((val <<= 1) & 0x80000000) != 0)
14482 ++i;
14484 return i;
14487 /* Locate some local-dynamic symbol still in use by this function
14488 so that we can print its name in some tls_ld pattern. */
14490 static const char *
14491 rs6000_get_some_local_dynamic_name (void)
14493 rtx insn;
14495 if (cfun->machine->some_ld_name)
14496 return cfun->machine->some_ld_name;
14498 for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
14499 if (INSN_P (insn)
14500 && for_each_rtx (&PATTERN (insn),
14501 rs6000_get_some_local_dynamic_name_1, 0))
14502 return cfun->machine->some_ld_name;
14504 gcc_unreachable ();
14507 /* Helper function for rs6000_get_some_local_dynamic_name. */
14509 static int
14510 rs6000_get_some_local_dynamic_name_1 (rtx *px, void *data ATTRIBUTE_UNUSED)
14512 rtx x = *px;
14514 if (GET_CODE (x) == SYMBOL_REF)
14516 const char *str = XSTR (x, 0);
14517 if (SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_DYNAMIC)
14519 cfun->machine->some_ld_name = str;
14520 return 1;
14524 return 0;
14527 /* Write out a function code label. */
14529 void
14530 rs6000_output_function_entry (FILE *file, const char *fname)
14532 if (fname[0] != '.')
14534 switch (DEFAULT_ABI)
14536 default:
14537 gcc_unreachable ();
14539 case ABI_AIX:
14540 if (DOT_SYMBOLS)
14541 putc ('.', file);
14542 else
14543 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "L.");
14544 break;
14546 case ABI_V4:
14547 case ABI_DARWIN:
14548 break;
14552 RS6000_OUTPUT_BASENAME (file, fname);
14555 /* Print an operand. Recognize special options, documented below. */
14557 #if TARGET_ELF
14558 #define SMALL_DATA_RELOC ((rs6000_sdata == SDATA_EABI) ? "sda21" : "sdarel")
14559 #define SMALL_DATA_REG ((rs6000_sdata == SDATA_EABI) ? 0 : 13)
14560 #else
14561 #define SMALL_DATA_RELOC "sda21"
14562 #define SMALL_DATA_REG 0
14563 #endif
14565 void
14566 print_operand (FILE *file, rtx x, int code)
14568 int i;
14569 unsigned HOST_WIDE_INT uval;
14571 switch (code)
14573 case '.':
14574 /* Write out an instruction after the call which may be replaced
14575 with glue code by the loader. This depends on the AIX version. */
14576 asm_fprintf (file, RS6000_CALL_GLUE);
14577 return;
14579 /* %a is output_address. */
14581 case 'A':
14582 /* If X is a constant integer whose low-order 5 bits are zero,
14583 write 'l'. Otherwise, write 'r'. This is a kludge to fix a bug
14584 in the AIX assembler where "sri" with a zero shift count
14585 writes a trash instruction. */
14586 if (GET_CODE (x) == CONST_INT && (INTVAL (x) & 31) == 0)
14587 putc ('l', file);
14588 else
14589 putc ('r', file);
14590 return;
14592 case 'b':
14593 /* If constant, low-order 16 bits of constant, unsigned.
14594 Otherwise, write normally. */
14595 if (INT_P (x))
14596 fprintf (file, HOST_WIDE_INT_PRINT_DEC, INT_LOWPART (x) & 0xffff);
14597 else
14598 print_operand (file, x, 0);
14599 return;
14601 case 'B':
14602 /* If the low-order bit is zero, write 'r'; otherwise, write 'l'
14603 for 64-bit mask direction. */
14604 putc (((INT_LOWPART (x) & 1) == 0 ? 'r' : 'l'), file);
14605 return;
14607 /* %c is output_addr_const if a CONSTANT_ADDRESS_P, otherwise
14608 output_operand. */
14610 case 'c':
14611 /* X is a CR register. Print the number of the GT bit of the CR. */
14612 if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
14613 output_operand_lossage ("invalid %%c value");
14614 else
14615 fprintf (file, "%d", 4 * (REGNO (x) - CR0_REGNO) + 1);
14616 return;
14618 case 'D':
14619 /* Like 'J' but get to the GT bit only. */
14620 gcc_assert (REG_P (x));
14622 /* Bit 1 is GT bit. */
14623 i = 4 * (REGNO (x) - CR0_REGNO) + 1;
14625 /* Add one for shift count in rlinm for scc. */
14626 fprintf (file, "%d", i + 1);
14627 return;
14629 case 'E':
14630 /* X is a CR register. Print the number of the EQ bit of the CR */
14631 if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
14632 output_operand_lossage ("invalid %%E value");
14633 else
14634 fprintf (file, "%d", 4 * (REGNO (x) - CR0_REGNO) + 2);
14635 return;
14637 case 'f':
14638 /* X is a CR register. Print the shift count needed to move it
14639 to the high-order four bits. */
14640 if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
14641 output_operand_lossage ("invalid %%f value");
14642 else
14643 fprintf (file, "%d", 4 * (REGNO (x) - CR0_REGNO));
14644 return;
14646 case 'F':
14647 /* Similar, but print the count for the rotate in the opposite
14648 direction. */
14649 if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
14650 output_operand_lossage ("invalid %%F value");
14651 else
14652 fprintf (file, "%d", 32 - 4 * (REGNO (x) - CR0_REGNO));
14653 return;
14655 case 'G':
14656 /* X is a constant integer. If it is negative, print "m",
14657 otherwise print "z". This is to make an aze or ame insn. */
14658 if (GET_CODE (x) != CONST_INT)
14659 output_operand_lossage ("invalid %%G value");
14660 else if (INTVAL (x) >= 0)
14661 putc ('z', file);
14662 else
14663 putc ('m', file);
14664 return;
14666 case 'h':
14667 /* If constant, output low-order five bits. Otherwise, write
14668 normally. */
14669 if (INT_P (x))
14670 fprintf (file, HOST_WIDE_INT_PRINT_DEC, INT_LOWPART (x) & 31);
14671 else
14672 print_operand (file, x, 0);
14673 return;
14675 case 'H':
14676 /* If constant, output low-order six bits. Otherwise, write
14677 normally. */
14678 if (INT_P (x))
14679 fprintf (file, HOST_WIDE_INT_PRINT_DEC, INT_LOWPART (x) & 63);
14680 else
14681 print_operand (file, x, 0);
14682 return;
14684 case 'I':
14685 /* Print `i' if this is a constant, else nothing. */
14686 if (INT_P (x))
14687 putc ('i', file);
14688 return;
14690 case 'j':
14691 /* Write the bit number in CCR for jump. */
14692 i = ccr_bit (x, 0);
14693 if (i == -1)
14694 output_operand_lossage ("invalid %%j code");
14695 else
14696 fprintf (file, "%d", i);
14697 return;
14699 case 'J':
14700 /* Similar, but add one for shift count in rlinm for scc and pass
14701 scc flag to `ccr_bit'. */
14702 i = ccr_bit (x, 1);
14703 if (i == -1)
14704 output_operand_lossage ("invalid %%J code");
14705 else
14706 /* If we want bit 31, write a shift count of zero, not 32. */
14707 fprintf (file, "%d", i == 31 ? 0 : i + 1);
14708 return;
14710 case 'k':
14711 /* X must be a constant. Write the 1's complement of the
14712 constant. */
14713 if (! INT_P (x))
14714 output_operand_lossage ("invalid %%k value");
14715 else
14716 fprintf (file, HOST_WIDE_INT_PRINT_DEC, ~ INT_LOWPART (x));
14717 return;
14719 case 'K':
14720 /* X must be a symbolic constant on ELF. Write an
14721 expression suitable for an 'addi' that adds in the low 16
14722 bits of the MEM. */
14723 if (GET_CODE (x) == CONST)
14725 if (GET_CODE (XEXP (x, 0)) != PLUS
14726 || (GET_CODE (XEXP (XEXP (x, 0), 0)) != SYMBOL_REF
14727 && GET_CODE (XEXP (XEXP (x, 0), 0)) != LABEL_REF)
14728 || GET_CODE (XEXP (XEXP (x, 0), 1)) != CONST_INT)
14729 output_operand_lossage ("invalid %%K value");
14731 print_operand_address (file, x);
14732 fputs ("@l", file);
14733 return;
14735 /* %l is output_asm_label. */
14737 case 'L':
14738 /* Write second word of DImode or DFmode reference. Works on register
14739 or non-indexed memory only. */
14740 if (REG_P (x))
14741 fputs (reg_names[REGNO (x) + 1], file);
14742 else if (MEM_P (x))
14744 /* Handle possible auto-increment. Since it is pre-increment and
14745 we have already done it, we can just use an offset of word. */
14746 if (GET_CODE (XEXP (x, 0)) == PRE_INC
14747 || GET_CODE (XEXP (x, 0)) == PRE_DEC)
14748 output_address (plus_constant (Pmode, XEXP (XEXP (x, 0), 0),
14749 UNITS_PER_WORD));
14750 else if (GET_CODE (XEXP (x, 0)) == PRE_MODIFY)
14751 output_address (plus_constant (Pmode, XEXP (XEXP (x, 0), 0),
14752 UNITS_PER_WORD));
14753 else
14754 output_address (XEXP (adjust_address_nv (x, SImode,
14755 UNITS_PER_WORD),
14756 0));
14758 if (small_data_operand (x, GET_MODE (x)))
14759 fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
14760 reg_names[SMALL_DATA_REG]);
14762 return;
14764 case 'm':
14765 /* MB value for a mask operand. */
14766 if (! mask_operand (x, SImode))
14767 output_operand_lossage ("invalid %%m value");
14769 fprintf (file, "%d", extract_MB (x));
14770 return;
14772 case 'M':
14773 /* ME value for a mask operand. */
14774 if (! mask_operand (x, SImode))
14775 output_operand_lossage ("invalid %%M value");
14777 fprintf (file, "%d", extract_ME (x));
14778 return;
14780 /* %n outputs the negative of its operand. */
14782 case 'N':
14783 /* Write the number of elements in the vector times 4. */
14784 if (GET_CODE (x) != PARALLEL)
14785 output_operand_lossage ("invalid %%N value");
14786 else
14787 fprintf (file, "%d", XVECLEN (x, 0) * 4);
14788 return;
14790 case 'O':
14791 /* Similar, but subtract 1 first. */
14792 if (GET_CODE (x) != PARALLEL)
14793 output_operand_lossage ("invalid %%O value");
14794 else
14795 fprintf (file, "%d", (XVECLEN (x, 0) - 1) * 4);
14796 return;
14798 case 'p':
14799 /* X is a CONST_INT that is a power of two. Output the logarithm. */
14800 if (! INT_P (x)
14801 || INT_LOWPART (x) < 0
14802 || (i = exact_log2 (INT_LOWPART (x))) < 0)
14803 output_operand_lossage ("invalid %%p value");
14804 else
14805 fprintf (file, "%d", i);
14806 return;
14808 case 'P':
14809 /* The operand must be an indirect memory reference. The result
14810 is the register name. */
14811 if (GET_CODE (x) != MEM || GET_CODE (XEXP (x, 0)) != REG
14812 || REGNO (XEXP (x, 0)) >= 32)
14813 output_operand_lossage ("invalid %%P value");
14814 else
14815 fputs (reg_names[REGNO (XEXP (x, 0))], file);
14816 return;
14818 case 'q':
14819 /* This outputs the logical code corresponding to a boolean
14820 expression. The expression may have one or both operands
14821 negated (if one, only the first one). For condition register
14822 logical operations, it will also treat the negated
14823 CR codes as NOTs, but not handle NOTs of them. */
14825 const char *const *t = 0;
14826 const char *s;
14827 enum rtx_code code = GET_CODE (x);
14828 static const char * const tbl[3][3] = {
14829 { "and", "andc", "nor" },
14830 { "or", "orc", "nand" },
14831 { "xor", "eqv", "xor" } };
14833 if (code == AND)
14834 t = tbl[0];
14835 else if (code == IOR)
14836 t = tbl[1];
14837 else if (code == XOR)
14838 t = tbl[2];
14839 else
14840 output_operand_lossage ("invalid %%q value");
14842 if (GET_CODE (XEXP (x, 0)) != NOT)
14843 s = t[0];
14844 else
14846 if (GET_CODE (XEXP (x, 1)) == NOT)
14847 s = t[2];
14848 else
14849 s = t[1];
14852 fputs (s, file);
14854 return;
14856 case 'Q':
14857 if (TARGET_MFCRF)
14858 fputc (',', file);
14859 /* FALLTHRU */
14860 else
14861 return;
14863 case 'R':
14864 /* X is a CR register. Print the mask for `mtcrf'. */
14865 if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
14866 output_operand_lossage ("invalid %%R value");
14867 else
14868 fprintf (file, "%d", 128 >> (REGNO (x) - CR0_REGNO));
14869 return;
14871 case 's':
14872 /* Low 5 bits of 32 - value */
14873 if (! INT_P (x))
14874 output_operand_lossage ("invalid %%s value");
14875 else
14876 fprintf (file, HOST_WIDE_INT_PRINT_DEC, (32 - INT_LOWPART (x)) & 31);
14877 return;
14879 case 'S':
14880 /* PowerPC64 mask position. All 0's is excluded.
14881 CONST_INT 32-bit mask is considered sign-extended so any
14882 transition must occur within the CONST_INT, not on the boundary. */
14883 if (! mask64_operand (x, DImode))
14884 output_operand_lossage ("invalid %%S value");
14886 uval = INT_LOWPART (x);
14888 if (uval & 1) /* Clear Left */
14890 #if HOST_BITS_PER_WIDE_INT > 64
14891 uval &= ((unsigned HOST_WIDE_INT) 1 << 64) - 1;
14892 #endif
14893 i = 64;
14895 else /* Clear Right */
14897 uval = ~uval;
14898 #if HOST_BITS_PER_WIDE_INT > 64
14899 uval &= ((unsigned HOST_WIDE_INT) 1 << 64) - 1;
14900 #endif
14901 i = 63;
14903 while (uval != 0)
14904 --i, uval >>= 1;
14905 gcc_assert (i >= 0);
14906 fprintf (file, "%d", i);
14907 return;
14909 case 't':
14910 /* Like 'J' but get to the OVERFLOW/UNORDERED bit. */
14911 gcc_assert (REG_P (x) && GET_MODE (x) == CCmode);
14913 /* Bit 3 is OV bit. */
14914 i = 4 * (REGNO (x) - CR0_REGNO) + 3;
14916 /* If we want bit 31, write a shift count of zero, not 32. */
14917 fprintf (file, "%d", i == 31 ? 0 : i + 1);
14918 return;
14920 case 'T':
14921 /* Print the symbolic name of a branch target register. */
14922 if (GET_CODE (x) != REG || (REGNO (x) != LR_REGNO
14923 && REGNO (x) != CTR_REGNO))
14924 output_operand_lossage ("invalid %%T value");
14925 else if (REGNO (x) == LR_REGNO)
14926 fputs (TARGET_NEW_MNEMONICS ? "lr" : "r", file);
14927 else
14928 fputs ("ctr", file);
14929 return;
14931 case 'u':
14932 /* High-order 16 bits of constant for use in unsigned operand. */
14933 if (! INT_P (x))
14934 output_operand_lossage ("invalid %%u value");
14935 else
14936 fprintf (file, HOST_WIDE_INT_PRINT_HEX,
14937 (INT_LOWPART (x) >> 16) & 0xffff);
14938 return;
14940 case 'v':
14941 /* High-order 16 bits of constant for use in signed operand. */
14942 if (! INT_P (x))
14943 output_operand_lossage ("invalid %%v value");
14944 else
14945 fprintf (file, HOST_WIDE_INT_PRINT_HEX,
14946 (INT_LOWPART (x) >> 16) & 0xffff);
14947 return;
14949 case 'U':
14950 /* Print `u' if this has an auto-increment or auto-decrement. */
14951 if (MEM_P (x)
14952 && (GET_CODE (XEXP (x, 0)) == PRE_INC
14953 || GET_CODE (XEXP (x, 0)) == PRE_DEC
14954 || GET_CODE (XEXP (x, 0)) == PRE_MODIFY))
14955 putc ('u', file);
14956 return;
14958 case 'V':
14959 /* Print the trap code for this operand. */
14960 switch (GET_CODE (x))
14962 case EQ:
14963 fputs ("eq", file); /* 4 */
14964 break;
14965 case NE:
14966 fputs ("ne", file); /* 24 */
14967 break;
14968 case LT:
14969 fputs ("lt", file); /* 16 */
14970 break;
14971 case LE:
14972 fputs ("le", file); /* 20 */
14973 break;
14974 case GT:
14975 fputs ("gt", file); /* 8 */
14976 break;
14977 case GE:
14978 fputs ("ge", file); /* 12 */
14979 break;
14980 case LTU:
14981 fputs ("llt", file); /* 2 */
14982 break;
14983 case LEU:
14984 fputs ("lle", file); /* 6 */
14985 break;
14986 case GTU:
14987 fputs ("lgt", file); /* 1 */
14988 break;
14989 case GEU:
14990 fputs ("lge", file); /* 5 */
14991 break;
14992 default:
14993 gcc_unreachable ();
14995 break;
14997 case 'w':
14998 /* If constant, low-order 16 bits of constant, signed. Otherwise, write
14999 normally. */
15000 if (INT_P (x))
15001 fprintf (file, HOST_WIDE_INT_PRINT_DEC,
15002 ((INT_LOWPART (x) & 0xffff) ^ 0x8000) - 0x8000);
15003 else
15004 print_operand (file, x, 0);
15005 return;
15007 case 'W':
15008 /* MB value for a PowerPC64 rldic operand. */
15009 i = clz_hwi (GET_CODE (x) == CONST_INT
15010 ? INTVAL (x) : CONST_DOUBLE_HIGH (x));
15012 #if HOST_BITS_PER_WIDE_INT == 32
15013 if (GET_CODE (x) == CONST_INT && i > 0)
15014 i += 32; /* zero-extend high-part was all 0's */
15015 else if (GET_CODE (x) == CONST_DOUBLE && i == 32)
15016 i = clz_hwi (CONST_DOUBLE_LOW (x)) + 32;
15017 #endif
15019 fprintf (file, "%d", i);
15020 return;
15022 case 'x':
15023 /* X is a FPR or Altivec register used in a VSX context. */
15024 if (GET_CODE (x) != REG || !VSX_REGNO_P (REGNO (x)))
15025 output_operand_lossage ("invalid %%x value");
15026 else
15028 int reg = REGNO (x);
15029 int vsx_reg = (FP_REGNO_P (reg)
15030 ? reg - 32
15031 : reg - FIRST_ALTIVEC_REGNO + 32);
15033 #ifdef TARGET_REGNAMES
15034 if (TARGET_REGNAMES)
15035 fprintf (file, "%%vs%d", vsx_reg);
15036 else
15037 #endif
15038 fprintf (file, "%d", vsx_reg);
15040 return;
15042 case 'X':
15043 if (MEM_P (x)
15044 && (legitimate_indexed_address_p (XEXP (x, 0), 0)
15045 || (GET_CODE (XEXP (x, 0)) == PRE_MODIFY
15046 && legitimate_indexed_address_p (XEXP (XEXP (x, 0), 1), 0))))
15047 putc ('x', file);
15048 return;
15050 case 'Y':
15051 /* Like 'L', for third word of TImode */
15052 if (REG_P (x))
15053 fputs (reg_names[REGNO (x) + 2], file);
15054 else if (MEM_P (x))
15056 if (GET_CODE (XEXP (x, 0)) == PRE_INC
15057 || GET_CODE (XEXP (x, 0)) == PRE_DEC)
15058 output_address (plus_constant (Pmode, XEXP (XEXP (x, 0), 0), 8));
15059 else if (GET_CODE (XEXP (x, 0)) == PRE_MODIFY)
15060 output_address (plus_constant (Pmode, XEXP (XEXP (x, 0), 0), 8));
15061 else
15062 output_address (XEXP (adjust_address_nv (x, SImode, 8), 0));
15063 if (small_data_operand (x, GET_MODE (x)))
15064 fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
15065 reg_names[SMALL_DATA_REG]);
15067 return;
15069 case 'z':
15070 /* X is a SYMBOL_REF. Write out the name preceded by a
15071 period and without any trailing data in brackets. Used for function
15072 names. If we are configured for System V (or the embedded ABI) on
15073 the PowerPC, do not emit the period, since those systems do not use
15074 TOCs and the like. */
15075 gcc_assert (GET_CODE (x) == SYMBOL_REF);
15077 /* Mark the decl as referenced so that cgraph will output the
15078 function. */
15079 if (SYMBOL_REF_DECL (x))
15080 mark_decl_referenced (SYMBOL_REF_DECL (x));
15082 /* For macho, check to see if we need a stub. */
15083 if (TARGET_MACHO)
15085 const char *name = XSTR (x, 0);
15086 #if TARGET_MACHO
15087 if (darwin_emit_branch_islands
15088 && MACHOPIC_INDIRECT
15089 && machopic_classify_symbol (x) == MACHOPIC_UNDEFINED_FUNCTION)
15090 name = machopic_indirection_name (x, /*stub_p=*/true);
15091 #endif
15092 assemble_name (file, name);
15094 else if (!DOT_SYMBOLS)
15095 assemble_name (file, XSTR (x, 0));
15096 else
15097 rs6000_output_function_entry (file, XSTR (x, 0));
15098 return;
15100 case 'Z':
15101 /* Like 'L', for last word of TImode. */
15102 if (REG_P (x))
15103 fputs (reg_names[REGNO (x) + 3], file);
15104 else if (MEM_P (x))
15106 if (GET_CODE (XEXP (x, 0)) == PRE_INC
15107 || GET_CODE (XEXP (x, 0)) == PRE_DEC)
15108 output_address (plus_constant (Pmode, XEXP (XEXP (x, 0), 0), 12));
15109 else if (GET_CODE (XEXP (x, 0)) == PRE_MODIFY)
15110 output_address (plus_constant (Pmode, XEXP (XEXP (x, 0), 0), 12));
15111 else
15112 output_address (XEXP (adjust_address_nv (x, SImode, 12), 0));
15113 if (small_data_operand (x, GET_MODE (x)))
15114 fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
15115 reg_names[SMALL_DATA_REG]);
15117 return;
15119 /* Print AltiVec or SPE memory operand. */
15120 case 'y':
15122 rtx tmp;
15124 gcc_assert (MEM_P (x));
15126 tmp = XEXP (x, 0);
15128 /* Ugly hack because %y is overloaded. */
15129 if ((TARGET_SPE || TARGET_E500_DOUBLE)
15130 && (GET_MODE_SIZE (GET_MODE (x)) == 8
15131 || GET_MODE (x) == TFmode
15132 || GET_MODE (x) == TImode))
15134 /* Handle [reg]. */
15135 if (REG_P (tmp))
15137 fprintf (file, "0(%s)", reg_names[REGNO (tmp)]);
15138 break;
15140 /* Handle [reg+UIMM]. */
15141 else if (GET_CODE (tmp) == PLUS &&
15142 GET_CODE (XEXP (tmp, 1)) == CONST_INT)
15144 int x;
15146 gcc_assert (REG_P (XEXP (tmp, 0)));
15148 x = INTVAL (XEXP (tmp, 1));
15149 fprintf (file, "%d(%s)", x, reg_names[REGNO (XEXP (tmp, 0))]);
15150 break;
15153 /* Fall through. Must be [reg+reg]. */
15155 if (VECTOR_MEM_ALTIVEC_P (GET_MODE (x))
15156 && GET_CODE (tmp) == AND
15157 && GET_CODE (XEXP (tmp, 1)) == CONST_INT
15158 && INTVAL (XEXP (tmp, 1)) == -16)
15159 tmp = XEXP (tmp, 0);
15160 else if (VECTOR_MEM_VSX_P (GET_MODE (x))
15161 && GET_CODE (tmp) == PRE_MODIFY)
15162 tmp = XEXP (tmp, 1);
15163 if (REG_P (tmp))
15164 fprintf (file, "0,%s", reg_names[REGNO (tmp)]);
15165 else
15167 if (!GET_CODE (tmp) == PLUS
15168 || !REG_P (XEXP (tmp, 0))
15169 || !REG_P (XEXP (tmp, 1)))
15171 output_operand_lossage ("invalid %%y value, try using the 'Z' constraint");
15172 break;
15175 if (REGNO (XEXP (tmp, 0)) == 0)
15176 fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (tmp, 1)) ],
15177 reg_names[ REGNO (XEXP (tmp, 0)) ]);
15178 else
15179 fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (tmp, 0)) ],
15180 reg_names[ REGNO (XEXP (tmp, 1)) ]);
15182 break;
15185 case 0:
15186 if (REG_P (x))
15187 fprintf (file, "%s", reg_names[REGNO (x)]);
15188 else if (MEM_P (x))
15190 /* We need to handle PRE_INC and PRE_DEC here, since we need to
15191 know the width from the mode. */
15192 if (GET_CODE (XEXP (x, 0)) == PRE_INC)
15193 fprintf (file, "%d(%s)", GET_MODE_SIZE (GET_MODE (x)),
15194 reg_names[REGNO (XEXP (XEXP (x, 0), 0))]);
15195 else if (GET_CODE (XEXP (x, 0)) == PRE_DEC)
15196 fprintf (file, "%d(%s)", - GET_MODE_SIZE (GET_MODE (x)),
15197 reg_names[REGNO (XEXP (XEXP (x, 0), 0))]);
15198 else if (GET_CODE (XEXP (x, 0)) == PRE_MODIFY)
15199 output_address (XEXP (XEXP (x, 0), 1));
15200 else
15201 output_address (XEXP (x, 0));
15203 else
15205 if (toc_relative_expr_p (x, false))
15206 /* This hack along with a corresponding hack in
15207 rs6000_output_addr_const_extra arranges to output addends
15208 where the assembler expects to find them. eg.
15209 (plus (unspec [(symbol_ref ("x")) (reg 2)] tocrel) 4)
15210 without this hack would be output as "x@toc+4". We
15211 want "x+4@toc". */
15212 output_addr_const (file, CONST_CAST_RTX (tocrel_base));
15213 else
15214 output_addr_const (file, x);
15216 return;
15218 case '&':
15219 assemble_name (file, rs6000_get_some_local_dynamic_name ());
15220 return;
15222 default:
15223 output_operand_lossage ("invalid %%xn code");
15227 /* Print the address of an operand. */
15229 void
15230 print_operand_address (FILE *file, rtx x)
15232 if (REG_P (x))
15233 fprintf (file, "0(%s)", reg_names[ REGNO (x) ]);
15234 else if (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == CONST
15235 || GET_CODE (x) == LABEL_REF)
15237 output_addr_const (file, x);
15238 if (small_data_operand (x, GET_MODE (x)))
15239 fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
15240 reg_names[SMALL_DATA_REG]);
15241 else
15242 gcc_assert (!TARGET_TOC);
15244 else if (GET_CODE (x) == PLUS && REG_P (XEXP (x, 0))
15245 && REG_P (XEXP (x, 1)))
15247 if (REGNO (XEXP (x, 0)) == 0)
15248 fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (x, 1)) ],
15249 reg_names[ REGNO (XEXP (x, 0)) ]);
15250 else
15251 fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (x, 0)) ],
15252 reg_names[ REGNO (XEXP (x, 1)) ]);
15254 else if (GET_CODE (x) == PLUS && REG_P (XEXP (x, 0))
15255 && GET_CODE (XEXP (x, 1)) == CONST_INT)
15256 fprintf (file, HOST_WIDE_INT_PRINT_DEC "(%s)",
15257 INTVAL (XEXP (x, 1)), reg_names[ REGNO (XEXP (x, 0)) ]);
15258 #if TARGET_MACHO
15259 else if (GET_CODE (x) == LO_SUM && REG_P (XEXP (x, 0))
15260 && CONSTANT_P (XEXP (x, 1)))
15262 fprintf (file, "lo16(");
15263 output_addr_const (file, XEXP (x, 1));
15264 fprintf (file, ")(%s)", reg_names[ REGNO (XEXP (x, 0)) ]);
15266 #endif
15267 #if TARGET_ELF
15268 else if (GET_CODE (x) == LO_SUM && REG_P (XEXP (x, 0))
15269 && CONSTANT_P (XEXP (x, 1)))
15271 output_addr_const (file, XEXP (x, 1));
15272 fprintf (file, "@l(%s)", reg_names[ REGNO (XEXP (x, 0)) ]);
15274 #endif
15275 else if (toc_relative_expr_p (x, false))
15277 /* This hack along with a corresponding hack in
15278 rs6000_output_addr_const_extra arranges to output addends
15279 where the assembler expects to find them. eg.
15280 (lo_sum (reg 9)
15281 . (plus (unspec [(symbol_ref ("x")) (reg 2)] tocrel) 8))
15282 without this hack would be output as "x@toc+8@l(9)". We
15283 want "x+8@toc@l(9)". */
15284 output_addr_const (file, CONST_CAST_RTX (tocrel_base));
15285 if (GET_CODE (x) == LO_SUM)
15286 fprintf (file, "@l(%s)", reg_names[REGNO (XEXP (x, 0))]);
15287 else
15288 fprintf (file, "(%s)", reg_names[REGNO (XVECEXP (tocrel_base, 0, 1))]);
15290 else
15291 gcc_unreachable ();
15294 /* Implement TARGET_OUTPUT_ADDR_CONST_EXTRA. */
15296 static bool
15297 rs6000_output_addr_const_extra (FILE *file, rtx x)
15299 if (GET_CODE (x) == UNSPEC)
15300 switch (XINT (x, 1))
15302 case UNSPEC_TOCREL:
15303 gcc_checking_assert (GET_CODE (XVECEXP (x, 0, 0)) == SYMBOL_REF
15304 && REG_P (XVECEXP (x, 0, 1))
15305 && REGNO (XVECEXP (x, 0, 1)) == TOC_REGISTER);
15306 output_addr_const (file, XVECEXP (x, 0, 0));
15307 if (x == tocrel_base && tocrel_offset != const0_rtx)
15309 if (INTVAL (tocrel_offset) >= 0)
15310 fprintf (file, "+");
15311 output_addr_const (file, CONST_CAST_RTX (tocrel_offset));
15313 if (!TARGET_AIX || (TARGET_ELF && TARGET_MINIMAL_TOC))
15315 putc ('-', file);
15316 assemble_name (file, toc_label_name);
15318 else if (TARGET_ELF)
15319 fputs ("@toc", file);
15320 return true;
15322 #if TARGET_MACHO
15323 case UNSPEC_MACHOPIC_OFFSET:
15324 output_addr_const (file, XVECEXP (x, 0, 0));
15325 putc ('-', file);
15326 machopic_output_function_base_name (file);
15327 return true;
15328 #endif
15330 return false;
15333 /* Target hook for assembling integer objects. The PowerPC version has
15334 to handle fixup entries for relocatable code if RELOCATABLE_NEEDS_FIXUP
15335 is defined. It also needs to handle DI-mode objects on 64-bit
15336 targets. */
15338 static bool
15339 rs6000_assemble_integer (rtx x, unsigned int size, int aligned_p)
15341 #ifdef RELOCATABLE_NEEDS_FIXUP
15342 /* Special handling for SI values. */
15343 if (RELOCATABLE_NEEDS_FIXUP && size == 4 && aligned_p)
15345 static int recurse = 0;
15347 /* For -mrelocatable, we mark all addresses that need to be fixed up in
15348 the .fixup section. Since the TOC section is already relocated, we
15349 don't need to mark it here. We used to skip the text section, but it
15350 should never be valid for relocated addresses to be placed in the text
15351 section. */
15352 if (TARGET_RELOCATABLE
15353 && in_section != toc_section
15354 && !recurse
15355 && GET_CODE (x) != CONST_INT
15356 && GET_CODE (x) != CONST_DOUBLE
15357 && CONSTANT_P (x))
15359 char buf[256];
15361 recurse = 1;
15362 ASM_GENERATE_INTERNAL_LABEL (buf, "LCP", fixuplabelno);
15363 fixuplabelno++;
15364 ASM_OUTPUT_LABEL (asm_out_file, buf);
15365 fprintf (asm_out_file, "\t.long\t(");
15366 output_addr_const (asm_out_file, x);
15367 fprintf (asm_out_file, ")@fixup\n");
15368 fprintf (asm_out_file, "\t.section\t\".fixup\",\"aw\"\n");
15369 ASM_OUTPUT_ALIGN (asm_out_file, 2);
15370 fprintf (asm_out_file, "\t.long\t");
15371 assemble_name (asm_out_file, buf);
15372 fprintf (asm_out_file, "\n\t.previous\n");
15373 recurse = 0;
15374 return true;
15376 /* Remove initial .'s to turn a -mcall-aixdesc function
15377 address into the address of the descriptor, not the function
15378 itself. */
15379 else if (GET_CODE (x) == SYMBOL_REF
15380 && XSTR (x, 0)[0] == '.'
15381 && DEFAULT_ABI == ABI_AIX)
15383 const char *name = XSTR (x, 0);
15384 while (*name == '.')
15385 name++;
15387 fprintf (asm_out_file, "\t.long\t%s\n", name);
15388 return true;
15391 #endif /* RELOCATABLE_NEEDS_FIXUP */
15392 return default_assemble_integer (x, size, aligned_p);
15395 #if defined (HAVE_GAS_HIDDEN) && !TARGET_MACHO
15396 /* Emit an assembler directive to set symbol visibility for DECL to
15397 VISIBILITY_TYPE. */
15399 static void
15400 rs6000_assemble_visibility (tree decl, int vis)
15402 /* Functions need to have their entry point symbol visibility set as
15403 well as their descriptor symbol visibility. */
15404 if (DEFAULT_ABI == ABI_AIX
15405 && DOT_SYMBOLS
15406 && TREE_CODE (decl) == FUNCTION_DECL)
15408 static const char * const visibility_types[] = {
15409 NULL, "internal", "hidden", "protected"
15412 const char *name, *type;
15414 name = ((* targetm.strip_name_encoding)
15415 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl))));
15416 type = visibility_types[vis];
15418 fprintf (asm_out_file, "\t.%s\t%s\n", type, name);
15419 fprintf (asm_out_file, "\t.%s\t.%s\n", type, name);
15421 else
15422 default_assemble_visibility (decl, vis);
15424 #endif
15426 enum rtx_code
15427 rs6000_reverse_condition (enum machine_mode mode, enum rtx_code code)
15429 /* Reversal of FP compares takes care -- an ordered compare
15430 becomes an unordered compare and vice versa. */
15431 if (mode == CCFPmode
15432 && (!flag_finite_math_only
15433 || code == UNLT || code == UNLE || code == UNGT || code == UNGE
15434 || code == UNEQ || code == LTGT))
15435 return reverse_condition_maybe_unordered (code);
15436 else
15437 return reverse_condition (code);
15440 /* Generate a compare for CODE. Return a brand-new rtx that
15441 represents the result of the compare. */
15443 static rtx
15444 rs6000_generate_compare (rtx cmp, enum machine_mode mode)
15446 enum machine_mode comp_mode;
15447 rtx compare_result;
15448 enum rtx_code code = GET_CODE (cmp);
15449 rtx op0 = XEXP (cmp, 0);
15450 rtx op1 = XEXP (cmp, 1);
15452 if (FLOAT_MODE_P (mode))
15453 comp_mode = CCFPmode;
15454 else if (code == GTU || code == LTU
15455 || code == GEU || code == LEU)
15456 comp_mode = CCUNSmode;
15457 else if ((code == EQ || code == NE)
15458 && unsigned_reg_p (op0)
15459 && (unsigned_reg_p (op1)
15460 || (CONST_INT_P (op1) && INTVAL (op1) != 0)))
15461 /* These are unsigned values, perhaps there will be a later
15462 ordering compare that can be shared with this one. */
15463 comp_mode = CCUNSmode;
15464 else
15465 comp_mode = CCmode;
15467 /* If we have an unsigned compare, make sure we don't have a signed value as
15468 an immediate. */
15469 if (comp_mode == CCUNSmode && GET_CODE (op1) == CONST_INT
15470 && INTVAL (op1) < 0)
15472 op0 = copy_rtx_if_shared (op0);
15473 op1 = force_reg (GET_MODE (op0), op1);
15474 cmp = gen_rtx_fmt_ee (code, GET_MODE (cmp), op0, op1);
15477 /* First, the compare. */
15478 compare_result = gen_reg_rtx (comp_mode);
15480 /* E500 FP compare instructions on the GPRs. Yuck! */
15481 if ((!TARGET_FPRS && TARGET_HARD_FLOAT)
15482 && FLOAT_MODE_P (mode))
15484 rtx cmp, or_result, compare_result2;
15485 enum machine_mode op_mode = GET_MODE (op0);
15487 if (op_mode == VOIDmode)
15488 op_mode = GET_MODE (op1);
15490 /* The E500 FP compare instructions toggle the GT bit (CR bit 1) only.
15491 This explains the following mess. */
15493 switch (code)
15495 case EQ: case UNEQ: case NE: case LTGT:
15496 switch (op_mode)
15498 case SFmode:
15499 cmp = (flag_finite_math_only && !flag_trapping_math)
15500 ? gen_tstsfeq_gpr (compare_result, op0, op1)
15501 : gen_cmpsfeq_gpr (compare_result, op0, op1);
15502 break;
15504 case DFmode:
15505 cmp = (flag_finite_math_only && !flag_trapping_math)
15506 ? gen_tstdfeq_gpr (compare_result, op0, op1)
15507 : gen_cmpdfeq_gpr (compare_result, op0, op1);
15508 break;
15510 case TFmode:
15511 cmp = (flag_finite_math_only && !flag_trapping_math)
15512 ? gen_tsttfeq_gpr (compare_result, op0, op1)
15513 : gen_cmptfeq_gpr (compare_result, op0, op1);
15514 break;
15516 default:
15517 gcc_unreachable ();
15519 break;
15521 case GT: case GTU: case UNGT: case UNGE: case GE: case GEU:
15522 switch (op_mode)
15524 case SFmode:
15525 cmp = (flag_finite_math_only && !flag_trapping_math)
15526 ? gen_tstsfgt_gpr (compare_result, op0, op1)
15527 : gen_cmpsfgt_gpr (compare_result, op0, op1);
15528 break;
15530 case DFmode:
15531 cmp = (flag_finite_math_only && !flag_trapping_math)
15532 ? gen_tstdfgt_gpr (compare_result, op0, op1)
15533 : gen_cmpdfgt_gpr (compare_result, op0, op1);
15534 break;
15536 case TFmode:
15537 cmp = (flag_finite_math_only && !flag_trapping_math)
15538 ? gen_tsttfgt_gpr (compare_result, op0, op1)
15539 : gen_cmptfgt_gpr (compare_result, op0, op1);
15540 break;
15542 default:
15543 gcc_unreachable ();
15545 break;
15547 case LT: case LTU: case UNLT: case UNLE: case LE: case LEU:
15548 switch (op_mode)
15550 case SFmode:
15551 cmp = (flag_finite_math_only && !flag_trapping_math)
15552 ? gen_tstsflt_gpr (compare_result, op0, op1)
15553 : gen_cmpsflt_gpr (compare_result, op0, op1);
15554 break;
15556 case DFmode:
15557 cmp = (flag_finite_math_only && !flag_trapping_math)
15558 ? gen_tstdflt_gpr (compare_result, op0, op1)
15559 : gen_cmpdflt_gpr (compare_result, op0, op1);
15560 break;
15562 case TFmode:
15563 cmp = (flag_finite_math_only && !flag_trapping_math)
15564 ? gen_tsttflt_gpr (compare_result, op0, op1)
15565 : gen_cmptflt_gpr (compare_result, op0, op1);
15566 break;
15568 default:
15569 gcc_unreachable ();
15571 break;
15572 default:
15573 gcc_unreachable ();
15576 /* Synthesize LE and GE from LT/GT || EQ. */
15577 if (code == LE || code == GE || code == LEU || code == GEU)
15579 emit_insn (cmp);
15581 switch (code)
15583 case LE: code = LT; break;
15584 case GE: code = GT; break;
15585 case LEU: code = LT; break;
15586 case GEU: code = GT; break;
15587 default: gcc_unreachable ();
15590 compare_result2 = gen_reg_rtx (CCFPmode);
15592 /* Do the EQ. */
15593 switch (op_mode)
15595 case SFmode:
15596 cmp = (flag_finite_math_only && !flag_trapping_math)
15597 ? gen_tstsfeq_gpr (compare_result2, op0, op1)
15598 : gen_cmpsfeq_gpr (compare_result2, op0, op1);
15599 break;
15601 case DFmode:
15602 cmp = (flag_finite_math_only && !flag_trapping_math)
15603 ? gen_tstdfeq_gpr (compare_result2, op0, op1)
15604 : gen_cmpdfeq_gpr (compare_result2, op0, op1);
15605 break;
15607 case TFmode:
15608 cmp = (flag_finite_math_only && !flag_trapping_math)
15609 ? gen_tsttfeq_gpr (compare_result2, op0, op1)
15610 : gen_cmptfeq_gpr (compare_result2, op0, op1);
15611 break;
15613 default:
15614 gcc_unreachable ();
15616 emit_insn (cmp);
15618 /* OR them together. */
15619 or_result = gen_reg_rtx (CCFPmode);
15620 cmp = gen_e500_cr_ior_compare (or_result, compare_result,
15621 compare_result2);
15622 compare_result = or_result;
15623 code = EQ;
15625 else
15627 if (code == NE || code == LTGT)
15628 code = NE;
15629 else
15630 code = EQ;
15633 emit_insn (cmp);
15635 else
15637 /* Generate XLC-compatible TFmode compare as PARALLEL with extra
15638 CLOBBERs to match cmptf_internal2 pattern. */
15639 if (comp_mode == CCFPmode && TARGET_XL_COMPAT
15640 && GET_MODE (op0) == TFmode
15641 && !TARGET_IEEEQUAD
15642 && TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_LONG_DOUBLE_128)
15643 emit_insn (gen_rtx_PARALLEL (VOIDmode,
15644 gen_rtvec (10,
15645 gen_rtx_SET (VOIDmode,
15646 compare_result,
15647 gen_rtx_COMPARE (comp_mode, op0, op1)),
15648 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
15649 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
15650 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
15651 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
15652 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
15653 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
15654 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
15655 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
15656 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (Pmode)))));
15657 else if (GET_CODE (op1) == UNSPEC
15658 && XINT (op1, 1) == UNSPEC_SP_TEST)
15660 rtx op1b = XVECEXP (op1, 0, 0);
15661 comp_mode = CCEQmode;
15662 compare_result = gen_reg_rtx (CCEQmode);
15663 if (TARGET_64BIT)
15664 emit_insn (gen_stack_protect_testdi (compare_result, op0, op1b));
15665 else
15666 emit_insn (gen_stack_protect_testsi (compare_result, op0, op1b));
15668 else
15669 emit_insn (gen_rtx_SET (VOIDmode, compare_result,
15670 gen_rtx_COMPARE (comp_mode, op0, op1)));
15673 /* Some kinds of FP comparisons need an OR operation;
15674 under flag_finite_math_only we don't bother. */
15675 if (FLOAT_MODE_P (mode)
15676 && !flag_finite_math_only
15677 && !(TARGET_HARD_FLOAT && !TARGET_FPRS)
15678 && (code == LE || code == GE
15679 || code == UNEQ || code == LTGT
15680 || code == UNGT || code == UNLT))
15682 enum rtx_code or1, or2;
15683 rtx or1_rtx, or2_rtx, compare2_rtx;
15684 rtx or_result = gen_reg_rtx (CCEQmode);
15686 switch (code)
15688 case LE: or1 = LT; or2 = EQ; break;
15689 case GE: or1 = GT; or2 = EQ; break;
15690 case UNEQ: or1 = UNORDERED; or2 = EQ; break;
15691 case LTGT: or1 = LT; or2 = GT; break;
15692 case UNGT: or1 = UNORDERED; or2 = GT; break;
15693 case UNLT: or1 = UNORDERED; or2 = LT; break;
15694 default: gcc_unreachable ();
15696 validate_condition_mode (or1, comp_mode);
15697 validate_condition_mode (or2, comp_mode);
15698 or1_rtx = gen_rtx_fmt_ee (or1, SImode, compare_result, const0_rtx);
15699 or2_rtx = gen_rtx_fmt_ee (or2, SImode, compare_result, const0_rtx);
15700 compare2_rtx = gen_rtx_COMPARE (CCEQmode,
15701 gen_rtx_IOR (SImode, or1_rtx, or2_rtx),
15702 const_true_rtx);
15703 emit_insn (gen_rtx_SET (VOIDmode, or_result, compare2_rtx));
15705 compare_result = or_result;
15706 code = EQ;
15709 validate_condition_mode (code, GET_MODE (compare_result));
15711 return gen_rtx_fmt_ee (code, VOIDmode, compare_result, const0_rtx);
15715 /* Emit the RTL for an sISEL pattern. */
15717 void
15718 rs6000_emit_sISEL (enum machine_mode mode ATTRIBUTE_UNUSED, rtx operands[])
15720 rs6000_emit_int_cmove (operands[0], operands[1], const1_rtx, const0_rtx);
15723 void
15724 rs6000_emit_sCOND (enum machine_mode mode, rtx operands[])
15726 rtx condition_rtx;
15727 enum machine_mode op_mode;
15728 enum rtx_code cond_code;
15729 rtx result = operands[0];
15731 if (TARGET_ISEL && (mode == SImode || mode == DImode))
15733 rs6000_emit_sISEL (mode, operands);
15734 return;
15737 condition_rtx = rs6000_generate_compare (operands[1], mode);
15738 cond_code = GET_CODE (condition_rtx);
15740 if (FLOAT_MODE_P (mode)
15741 && !TARGET_FPRS && TARGET_HARD_FLOAT)
15743 rtx t;
15745 PUT_MODE (condition_rtx, SImode);
15746 t = XEXP (condition_rtx, 0);
15748 gcc_assert (cond_code == NE || cond_code == EQ);
15750 if (cond_code == NE)
15751 emit_insn (gen_e500_flip_gt_bit (t, t));
15753 emit_insn (gen_move_from_CR_gt_bit (result, t));
15754 return;
15757 if (cond_code == NE
15758 || cond_code == GE || cond_code == LE
15759 || cond_code == GEU || cond_code == LEU
15760 || cond_code == ORDERED || cond_code == UNGE || cond_code == UNLE)
15762 rtx not_result = gen_reg_rtx (CCEQmode);
15763 rtx not_op, rev_cond_rtx;
15764 enum machine_mode cc_mode;
15766 cc_mode = GET_MODE (XEXP (condition_rtx, 0));
15768 rev_cond_rtx = gen_rtx_fmt_ee (rs6000_reverse_condition (cc_mode, cond_code),
15769 SImode, XEXP (condition_rtx, 0), const0_rtx);
15770 not_op = gen_rtx_COMPARE (CCEQmode, rev_cond_rtx, const0_rtx);
15771 emit_insn (gen_rtx_SET (VOIDmode, not_result, not_op));
15772 condition_rtx = gen_rtx_EQ (VOIDmode, not_result, const0_rtx);
15775 op_mode = GET_MODE (XEXP (operands[1], 0));
15776 if (op_mode == VOIDmode)
15777 op_mode = GET_MODE (XEXP (operands[1], 1));
15779 if (TARGET_POWERPC64 && (op_mode == DImode || FLOAT_MODE_P (mode)))
15781 PUT_MODE (condition_rtx, DImode);
15782 convert_move (result, condition_rtx, 0);
15784 else
15786 PUT_MODE (condition_rtx, SImode);
15787 emit_insn (gen_rtx_SET (VOIDmode, result, condition_rtx));
15791 /* Emit a branch of kind CODE to location LOC. */
15793 void
15794 rs6000_emit_cbranch (enum machine_mode mode, rtx operands[])
15796 rtx condition_rtx, loc_ref;
15798 condition_rtx = rs6000_generate_compare (operands[0], mode);
15799 loc_ref = gen_rtx_LABEL_REF (VOIDmode, operands[3]);
15800 emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx,
15801 gen_rtx_IF_THEN_ELSE (VOIDmode, condition_rtx,
15802 loc_ref, pc_rtx)));
15805 /* Return the string to output a conditional branch to LABEL, which is
15806 the operand number of the label, or -1 if the branch is really a
15807 conditional return.
15809 OP is the conditional expression. XEXP (OP, 0) is assumed to be a
15810 condition code register and its mode specifies what kind of
15811 comparison we made.
15813 REVERSED is nonzero if we should reverse the sense of the comparison.
15815 INSN is the insn. */
15817 char *
15818 output_cbranch (rtx op, const char *label, int reversed, rtx insn)
15820 static char string[64];
15821 enum rtx_code code = GET_CODE (op);
15822 rtx cc_reg = XEXP (op, 0);
15823 enum machine_mode mode = GET_MODE (cc_reg);
15824 int cc_regno = REGNO (cc_reg) - CR0_REGNO;
15825 int need_longbranch = label != NULL && get_attr_length (insn) == 8;
15826 int really_reversed = reversed ^ need_longbranch;
15827 char *s = string;
15828 const char *ccode;
15829 const char *pred;
15830 rtx note;
15832 validate_condition_mode (code, mode);
15834 /* Work out which way this really branches. We could use
15835 reverse_condition_maybe_unordered here always but this
15836 makes the resulting assembler clearer. */
15837 if (really_reversed)
15839 /* Reversal of FP compares takes care -- an ordered compare
15840 becomes an unordered compare and vice versa. */
15841 if (mode == CCFPmode)
15842 code = reverse_condition_maybe_unordered (code);
15843 else
15844 code = reverse_condition (code);
15847 if ((!TARGET_FPRS && TARGET_HARD_FLOAT) && mode == CCFPmode)
15849 /* The efscmp/tst* instructions twiddle bit 2, which maps nicely
15850 to the GT bit. */
15851 switch (code)
15853 case EQ:
15854 /* Opposite of GT. */
15855 code = GT;
15856 break;
15858 case NE:
15859 code = UNLE;
15860 break;
15862 default:
15863 gcc_unreachable ();
15867 switch (code)
15869 /* Not all of these are actually distinct opcodes, but
15870 we distinguish them for clarity of the resulting assembler. */
15871 case NE: case LTGT:
15872 ccode = "ne"; break;
15873 case EQ: case UNEQ:
15874 ccode = "eq"; break;
15875 case GE: case GEU:
15876 ccode = "ge"; break;
15877 case GT: case GTU: case UNGT:
15878 ccode = "gt"; break;
15879 case LE: case LEU:
15880 ccode = "le"; break;
15881 case LT: case LTU: case UNLT:
15882 ccode = "lt"; break;
15883 case UNORDERED: ccode = "un"; break;
15884 case ORDERED: ccode = "nu"; break;
15885 case UNGE: ccode = "nl"; break;
15886 case UNLE: ccode = "ng"; break;
15887 default:
15888 gcc_unreachable ();
15891 /* Maybe we have a guess as to how likely the branch is.
15892 The old mnemonics don't have a way to specify this information. */
15893 pred = "";
15894 note = find_reg_note (insn, REG_BR_PROB, NULL_RTX);
15895 if (note != NULL_RTX)
15897 /* PROB is the difference from 50%. */
15898 int prob = INTVAL (XEXP (note, 0)) - REG_BR_PROB_BASE / 2;
15900 /* Only hint for highly probable/improbable branches on newer
15901 cpus as static prediction overrides processor dynamic
15902 prediction. For older cpus we may as well always hint, but
15903 assume not taken for branches that are very close to 50% as a
15904 mispredicted taken branch is more expensive than a
15905 mispredicted not-taken branch. */
15906 if (rs6000_always_hint
15907 || (abs (prob) > REG_BR_PROB_BASE / 100 * 48
15908 && br_prob_note_reliable_p (note)))
15910 if (abs (prob) > REG_BR_PROB_BASE / 20
15911 && ((prob > 0) ^ need_longbranch))
15912 pred = "+";
15913 else
15914 pred = "-";
15918 if (label == NULL)
15919 s += sprintf (s, "{b%sr|b%slr%s} ", ccode, ccode, pred);
15920 else
15921 s += sprintf (s, "{b%s|b%s%s} ", ccode, ccode, pred);
15923 /* We need to escape any '%' characters in the reg_names string.
15924 Assume they'd only be the first character.... */
15925 if (reg_names[cc_regno + CR0_REGNO][0] == '%')
15926 *s++ = '%';
15927 s += sprintf (s, "%s", reg_names[cc_regno + CR0_REGNO]);
15929 if (label != NULL)
15931 /* If the branch distance was too far, we may have to use an
15932 unconditional branch to go the distance. */
15933 if (need_longbranch)
15934 s += sprintf (s, ",$+8\n\tb %s", label);
15935 else
15936 s += sprintf (s, ",%s", label);
15939 return string;
15942 /* Return the string to flip the GT bit on a CR. */
15943 char *
15944 output_e500_flip_gt_bit (rtx dst, rtx src)
15946 static char string[64];
15947 int a, b;
15949 gcc_assert (GET_CODE (dst) == REG && CR_REGNO_P (REGNO (dst))
15950 && GET_CODE (src) == REG && CR_REGNO_P (REGNO (src)));
15952 /* GT bit. */
15953 a = 4 * (REGNO (dst) - CR0_REGNO) + 1;
15954 b = 4 * (REGNO (src) - CR0_REGNO) + 1;
15956 sprintf (string, "crnot %d,%d", a, b);
15957 return string;
15960 /* Return insn for VSX or Altivec comparisons. */
15962 static rtx
15963 rs6000_emit_vector_compare_inner (enum rtx_code code, rtx op0, rtx op1)
15965 rtx mask;
15966 enum machine_mode mode = GET_MODE (op0);
15968 switch (code)
15970 default:
15971 break;
15973 case GE:
15974 if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
15975 return NULL_RTX;
15977 case EQ:
15978 case GT:
15979 case GTU:
15980 case ORDERED:
15981 case UNORDERED:
15982 case UNEQ:
15983 case LTGT:
15984 mask = gen_reg_rtx (mode);
15985 emit_insn (gen_rtx_SET (VOIDmode,
15986 mask,
15987 gen_rtx_fmt_ee (code, mode, op0, op1)));
15988 return mask;
15991 return NULL_RTX;
15994 /* Emit vector compare for operands OP0 and OP1 using code RCODE.
15995 DMODE is expected destination mode. This is a recursive function. */
15997 static rtx
15998 rs6000_emit_vector_compare (enum rtx_code rcode,
15999 rtx op0, rtx op1,
16000 enum machine_mode dmode)
16002 rtx mask;
16003 bool swap_operands = false;
16004 bool try_again = false;
16006 gcc_assert (VECTOR_UNIT_ALTIVEC_OR_VSX_P (dmode));
16007 gcc_assert (GET_MODE (op0) == GET_MODE (op1));
16009 /* See if the comparison works as is. */
16010 mask = rs6000_emit_vector_compare_inner (rcode, op0, op1);
16011 if (mask)
16012 return mask;
16014 switch (rcode)
16016 case LT:
16017 rcode = GT;
16018 swap_operands = true;
16019 try_again = true;
16020 break;
16021 case LTU:
16022 rcode = GTU;
16023 swap_operands = true;
16024 try_again = true;
16025 break;
16026 case NE:
16027 case UNLE:
16028 case UNLT:
16029 case UNGE:
16030 case UNGT:
16031 /* Invert condition and try again.
16032 e.g., A != B becomes ~(A==B). */
16034 enum rtx_code rev_code;
16035 enum insn_code nor_code;
16036 rtx mask2;
16038 rev_code = reverse_condition_maybe_unordered (rcode);
16039 if (rev_code == UNKNOWN)
16040 return NULL_RTX;
16042 nor_code = optab_handler (one_cmpl_optab, dmode);
16043 if (nor_code == CODE_FOR_nothing)
16044 return NULL_RTX;
16046 mask2 = rs6000_emit_vector_compare (rev_code, op0, op1, dmode);
16047 if (!mask2)
16048 return NULL_RTX;
16050 mask = gen_reg_rtx (dmode);
16051 emit_insn (GEN_FCN (nor_code) (mask, mask2));
16052 return mask;
16054 break;
16055 case GE:
16056 case GEU:
16057 case LE:
16058 case LEU:
16059 /* Try GT/GTU/LT/LTU OR EQ */
16061 rtx c_rtx, eq_rtx;
16062 enum insn_code ior_code;
16063 enum rtx_code new_code;
16065 switch (rcode)
16067 case GE:
16068 new_code = GT;
16069 break;
16071 case GEU:
16072 new_code = GTU;
16073 break;
16075 case LE:
16076 new_code = LT;
16077 break;
16079 case LEU:
16080 new_code = LTU;
16081 break;
16083 default:
16084 gcc_unreachable ();
16087 ior_code = optab_handler (ior_optab, dmode);
16088 if (ior_code == CODE_FOR_nothing)
16089 return NULL_RTX;
16091 c_rtx = rs6000_emit_vector_compare (new_code, op0, op1, dmode);
16092 if (!c_rtx)
16093 return NULL_RTX;
16095 eq_rtx = rs6000_emit_vector_compare (EQ, op0, op1, dmode);
16096 if (!eq_rtx)
16097 return NULL_RTX;
16099 mask = gen_reg_rtx (dmode);
16100 emit_insn (GEN_FCN (ior_code) (mask, c_rtx, eq_rtx));
16101 return mask;
16103 break;
16104 default:
16105 return NULL_RTX;
16108 if (try_again)
16110 if (swap_operands)
16112 rtx tmp;
16113 tmp = op0;
16114 op0 = op1;
16115 op1 = tmp;
16118 mask = rs6000_emit_vector_compare_inner (rcode, op0, op1);
16119 if (mask)
16120 return mask;
16123 /* You only get two chances. */
16124 return NULL_RTX;
16127 /* Emit vector conditional expression. DEST is destination. OP_TRUE and
16128 OP_FALSE are two VEC_COND_EXPR operands. CC_OP0 and CC_OP1 are the two
16129 operands for the relation operation COND. */
16132 rs6000_emit_vector_cond_expr (rtx dest, rtx op_true, rtx op_false,
16133 rtx cond, rtx cc_op0, rtx cc_op1)
16135 enum machine_mode dest_mode = GET_MODE (dest);
16136 enum machine_mode mask_mode = GET_MODE (cc_op0);
16137 enum rtx_code rcode = GET_CODE (cond);
16138 enum machine_mode cc_mode = CCmode;
16139 rtx mask;
16140 rtx cond2;
16141 rtx tmp;
16142 bool invert_move = false;
16144 if (VECTOR_UNIT_NONE_P (dest_mode))
16145 return 0;
16147 gcc_assert (GET_MODE_SIZE (dest_mode) == GET_MODE_SIZE (mask_mode)
16148 && GET_MODE_NUNITS (dest_mode) == GET_MODE_NUNITS (mask_mode));
16150 switch (rcode)
16152 /* Swap operands if we can, and fall back to doing the operation as
16153 specified, and doing a NOR to invert the test. */
16154 case NE:
16155 case UNLE:
16156 case UNLT:
16157 case UNGE:
16158 case UNGT:
16159 /* Invert condition and try again.
16160 e.g., A = (B != C) ? D : E becomes A = (B == C) ? E : D. */
16161 invert_move = true;
16162 rcode = reverse_condition_maybe_unordered (rcode);
16163 if (rcode == UNKNOWN)
16164 return 0;
16165 break;
16167 /* Mark unsigned tests with CCUNSmode. */
16168 case GTU:
16169 case GEU:
16170 case LTU:
16171 case LEU:
16172 cc_mode = CCUNSmode;
16173 break;
16175 default:
16176 break;
16179 /* Get the vector mask for the given relational operations. */
16180 mask = rs6000_emit_vector_compare (rcode, cc_op0, cc_op1, mask_mode);
16182 if (!mask)
16183 return 0;
16185 if (invert_move)
16187 tmp = op_true;
16188 op_true = op_false;
16189 op_false = tmp;
16192 cond2 = gen_rtx_fmt_ee (NE, cc_mode, gen_lowpart (dest_mode, mask),
16193 CONST0_RTX (dest_mode));
16194 emit_insn (gen_rtx_SET (VOIDmode,
16195 dest,
16196 gen_rtx_IF_THEN_ELSE (dest_mode,
16197 cond2,
16198 op_true,
16199 op_false)));
16200 return 1;
16203 /* Emit a conditional move: move TRUE_COND to DEST if OP of the
16204 operands of the last comparison is nonzero/true, FALSE_COND if it
16205 is zero/false. Return 0 if the hardware has no such operation. */
16208 rs6000_emit_cmove (rtx dest, rtx op, rtx true_cond, rtx false_cond)
16210 enum rtx_code code = GET_CODE (op);
16211 rtx op0 = XEXP (op, 0);
16212 rtx op1 = XEXP (op, 1);
16213 REAL_VALUE_TYPE c1;
16214 enum machine_mode compare_mode = GET_MODE (op0);
16215 enum machine_mode result_mode = GET_MODE (dest);
16216 rtx temp;
16217 bool is_against_zero;
16219 /* These modes should always match. */
16220 if (GET_MODE (op1) != compare_mode
16221 /* In the isel case however, we can use a compare immediate, so
16222 op1 may be a small constant. */
16223 && (!TARGET_ISEL || !short_cint_operand (op1, VOIDmode)))
16224 return 0;
16225 if (GET_MODE (true_cond) != result_mode)
16226 return 0;
16227 if (GET_MODE (false_cond) != result_mode)
16228 return 0;
16230 /* Don't allow using floating point comparisons for integer results for
16231 now. */
16232 if (FLOAT_MODE_P (compare_mode) && !FLOAT_MODE_P (result_mode))
16233 return 0;
16235 /* First, work out if the hardware can do this at all, or
16236 if it's too slow.... */
16237 if (!FLOAT_MODE_P (compare_mode))
16239 if (TARGET_ISEL)
16240 return rs6000_emit_int_cmove (dest, op, true_cond, false_cond);
16241 return 0;
16243 else if (TARGET_HARD_FLOAT && !TARGET_FPRS
16244 && SCALAR_FLOAT_MODE_P (compare_mode))
16245 return 0;
16247 is_against_zero = op1 == CONST0_RTX (compare_mode);
16249 /* A floating-point subtract might overflow, underflow, or produce
16250 an inexact result, thus changing the floating-point flags, so it
16251 can't be generated if we care about that. It's safe if one side
16252 of the construct is zero, since then no subtract will be
16253 generated. */
16254 if (SCALAR_FLOAT_MODE_P (compare_mode)
16255 && flag_trapping_math && ! is_against_zero)
16256 return 0;
16258 /* Eliminate half of the comparisons by switching operands, this
16259 makes the remaining code simpler. */
16260 if (code == UNLT || code == UNGT || code == UNORDERED || code == NE
16261 || code == LTGT || code == LT || code == UNLE)
16263 code = reverse_condition_maybe_unordered (code);
16264 temp = true_cond;
16265 true_cond = false_cond;
16266 false_cond = temp;
16269 /* UNEQ and LTGT take four instructions for a comparison with zero,
16270 it'll probably be faster to use a branch here too. */
16271 if (code == UNEQ && HONOR_NANS (compare_mode))
16272 return 0;
16274 if (GET_CODE (op1) == CONST_DOUBLE)
16275 REAL_VALUE_FROM_CONST_DOUBLE (c1, op1);
16277 /* We're going to try to implement comparisons by performing
16278 a subtract, then comparing against zero. Unfortunately,
16279 Inf - Inf is NaN which is not zero, and so if we don't
16280 know that the operand is finite and the comparison
16281 would treat EQ different to UNORDERED, we can't do it. */
16282 if (HONOR_INFINITIES (compare_mode)
16283 && code != GT && code != UNGE
16284 && (GET_CODE (op1) != CONST_DOUBLE || real_isinf (&c1))
16285 /* Constructs of the form (a OP b ? a : b) are safe. */
16286 && ((! rtx_equal_p (op0, false_cond) && ! rtx_equal_p (op1, false_cond))
16287 || (! rtx_equal_p (op0, true_cond)
16288 && ! rtx_equal_p (op1, true_cond))))
16289 return 0;
16291 /* At this point we know we can use fsel. */
16293 /* Reduce the comparison to a comparison against zero. */
16294 if (! is_against_zero)
16296 temp = gen_reg_rtx (compare_mode);
16297 emit_insn (gen_rtx_SET (VOIDmode, temp,
16298 gen_rtx_MINUS (compare_mode, op0, op1)));
16299 op0 = temp;
16300 op1 = CONST0_RTX (compare_mode);
16303 /* If we don't care about NaNs we can reduce some of the comparisons
16304 down to faster ones. */
16305 if (! HONOR_NANS (compare_mode))
16306 switch (code)
16308 case GT:
16309 code = LE;
16310 temp = true_cond;
16311 true_cond = false_cond;
16312 false_cond = temp;
16313 break;
16314 case UNGE:
16315 code = GE;
16316 break;
16317 case UNEQ:
16318 code = EQ;
16319 break;
16320 default:
16321 break;
16324 /* Now, reduce everything down to a GE. */
16325 switch (code)
16327 case GE:
16328 break;
16330 case LE:
16331 temp = gen_reg_rtx (compare_mode);
16332 emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_NEG (compare_mode, op0)));
16333 op0 = temp;
16334 break;
16336 case ORDERED:
16337 temp = gen_reg_rtx (compare_mode);
16338 emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_ABS (compare_mode, op0)));
16339 op0 = temp;
16340 break;
16342 case EQ:
16343 temp = gen_reg_rtx (compare_mode);
16344 emit_insn (gen_rtx_SET (VOIDmode, temp,
16345 gen_rtx_NEG (compare_mode,
16346 gen_rtx_ABS (compare_mode, op0))));
16347 op0 = temp;
16348 break;
16350 case UNGE:
16351 /* a UNGE 0 <-> (a GE 0 || -a UNLT 0) */
16352 temp = gen_reg_rtx (result_mode);
16353 emit_insn (gen_rtx_SET (VOIDmode, temp,
16354 gen_rtx_IF_THEN_ELSE (result_mode,
16355 gen_rtx_GE (VOIDmode,
16356 op0, op1),
16357 true_cond, false_cond)));
16358 false_cond = true_cond;
16359 true_cond = temp;
16361 temp = gen_reg_rtx (compare_mode);
16362 emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_NEG (compare_mode, op0)));
16363 op0 = temp;
16364 break;
16366 case GT:
16367 /* a GT 0 <-> (a GE 0 && -a UNLT 0) */
16368 temp = gen_reg_rtx (result_mode);
16369 emit_insn (gen_rtx_SET (VOIDmode, temp,
16370 gen_rtx_IF_THEN_ELSE (result_mode,
16371 gen_rtx_GE (VOIDmode,
16372 op0, op1),
16373 true_cond, false_cond)));
16374 true_cond = false_cond;
16375 false_cond = temp;
16377 temp = gen_reg_rtx (compare_mode);
16378 emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_NEG (compare_mode, op0)));
16379 op0 = temp;
16380 break;
16382 default:
16383 gcc_unreachable ();
16386 emit_insn (gen_rtx_SET (VOIDmode, dest,
16387 gen_rtx_IF_THEN_ELSE (result_mode,
16388 gen_rtx_GE (VOIDmode,
16389 op0, op1),
16390 true_cond, false_cond)));
16391 return 1;
16394 /* Same as above, but for ints (isel). */
16396 static int
16397 rs6000_emit_int_cmove (rtx dest, rtx op, rtx true_cond, rtx false_cond)
16399 rtx condition_rtx, cr;
16400 enum machine_mode mode = GET_MODE (dest);
16401 enum rtx_code cond_code;
16402 rtx (*isel_func) (rtx, rtx, rtx, rtx, rtx);
16403 bool signedp;
16405 if (mode != SImode && (!TARGET_POWERPC64 || mode != DImode))
16406 return 0;
16408 /* We still have to do the compare, because isel doesn't do a
16409 compare, it just looks at the CRx bits set by a previous compare
16410 instruction. */
16411 condition_rtx = rs6000_generate_compare (op, mode);
16412 cond_code = GET_CODE (condition_rtx);
16413 cr = XEXP (condition_rtx, 0);
16414 signedp = GET_MODE (cr) == CCmode;
16416 isel_func = (mode == SImode
16417 ? (signedp ? gen_isel_signed_si : gen_isel_unsigned_si)
16418 : (signedp ? gen_isel_signed_di : gen_isel_unsigned_di));
16420 switch (cond_code)
16422 case LT: case GT: case LTU: case GTU: case EQ:
16423 /* isel handles these directly. */
16424 break;
16426 default:
16427 /* We need to swap the sense of the comparison. */
16429 rtx t = true_cond;
16430 true_cond = false_cond;
16431 false_cond = t;
16432 PUT_CODE (condition_rtx, reverse_condition (cond_code));
16434 break;
16437 false_cond = force_reg (mode, false_cond);
16438 if (true_cond != const0_rtx)
16439 true_cond = force_reg (mode, true_cond);
16441 emit_insn (isel_func (dest, condition_rtx, true_cond, false_cond, cr));
16443 return 1;
16446 const char *
16447 output_isel (rtx *operands)
16449 enum rtx_code code;
16451 code = GET_CODE (operands[1]);
16453 if (code == GE || code == GEU || code == LE || code == LEU || code == NE)
16455 gcc_assert (GET_CODE (operands[2]) == REG
16456 && GET_CODE (operands[3]) == REG);
16457 PUT_CODE (operands[1], reverse_condition (code));
16458 return "isel %0,%3,%2,%j1";
16461 return "isel %0,%2,%3,%j1";
16464 void
16465 rs6000_emit_minmax (rtx dest, enum rtx_code code, rtx op0, rtx op1)
16467 enum machine_mode mode = GET_MODE (op0);
16468 enum rtx_code c;
16469 rtx target;
16471 /* VSX/altivec have direct min/max insns. */
16472 if ((code == SMAX || code == SMIN)
16473 && (VECTOR_UNIT_ALTIVEC_OR_VSX_P (mode)
16474 || (mode == SFmode && VECTOR_UNIT_VSX_P (DFmode))))
16476 emit_insn (gen_rtx_SET (VOIDmode,
16477 dest,
16478 gen_rtx_fmt_ee (code, mode, op0, op1)));
16479 return;
16482 if (code == SMAX || code == SMIN)
16483 c = GE;
16484 else
16485 c = GEU;
16487 if (code == SMAX || code == UMAX)
16488 target = emit_conditional_move (dest, c, op0, op1, mode,
16489 op0, op1, mode, 0);
16490 else
16491 target = emit_conditional_move (dest, c, op0, op1, mode,
16492 op1, op0, mode, 0);
16493 gcc_assert (target);
16494 if (target != dest)
16495 emit_move_insn (dest, target);
16498 /* A subroutine of the atomic operation splitters. Jump to LABEL if
16499 COND is true. Mark the jump as unlikely to be taken. */
16501 static void
16502 emit_unlikely_jump (rtx cond, rtx label)
16504 rtx very_unlikely = GEN_INT (REG_BR_PROB_BASE / 100 - 1);
16505 rtx x;
16507 x = gen_rtx_IF_THEN_ELSE (VOIDmode, cond, label, pc_rtx);
16508 x = emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, x));
16509 add_reg_note (x, REG_BR_PROB, very_unlikely);
16512 /* A subroutine of the atomic operation splitters. Emit a load-locked
16513 instruction in MODE. */
16515 static void
16516 emit_load_locked (enum machine_mode mode, rtx reg, rtx mem)
16518 rtx (*fn) (rtx, rtx) = NULL;
16520 switch (mode)
16522 case SImode:
16523 fn = gen_load_lockedsi;
16524 break;
16525 case DImode:
16526 fn = gen_load_lockeddi;
16527 break;
16528 default:
16529 gcc_unreachable ();
16531 emit_insn (fn (reg, mem));
16534 /* A subroutine of the atomic operation splitters. Emit a store-conditional
16535 instruction in MODE. */
16537 static void
16538 emit_store_conditional (enum machine_mode mode, rtx res, rtx mem, rtx val)
16540 rtx (*fn) (rtx, rtx, rtx) = NULL;
16542 switch (mode)
16544 case SImode:
16545 fn = gen_store_conditionalsi;
16546 break;
16547 case DImode:
16548 fn = gen_store_conditionaldi;
16549 break;
16550 default:
16551 gcc_unreachable ();
16554 /* Emit sync before stwcx. to address PPC405 Erratum. */
16555 if (PPC405_ERRATUM77)
16556 emit_insn (gen_hwsync ());
16558 emit_insn (fn (res, mem, val));
16561 /* Expand barriers before and after a load_locked/store_cond sequence. */
16563 static rtx
16564 rs6000_pre_atomic_barrier (rtx mem, enum memmodel model)
16566 rtx addr = XEXP (mem, 0);
16567 int strict_p = (reload_in_progress || reload_completed);
16569 if (!legitimate_indirect_address_p (addr, strict_p)
16570 && !legitimate_indexed_address_p (addr, strict_p))
16572 addr = force_reg (Pmode, addr);
16573 mem = replace_equiv_address_nv (mem, addr);
16576 switch (model)
16578 case MEMMODEL_RELAXED:
16579 case MEMMODEL_CONSUME:
16580 case MEMMODEL_ACQUIRE:
16581 break;
16582 case MEMMODEL_RELEASE:
16583 case MEMMODEL_ACQ_REL:
16584 emit_insn (gen_lwsync ());
16585 break;
16586 case MEMMODEL_SEQ_CST:
16587 emit_insn (gen_hwsync ());
16588 break;
16589 default:
16590 gcc_unreachable ();
16592 return mem;
16595 static void
16596 rs6000_post_atomic_barrier (enum memmodel model)
16598 switch (model)
16600 case MEMMODEL_RELAXED:
16601 case MEMMODEL_CONSUME:
16602 case MEMMODEL_RELEASE:
16603 break;
16604 case MEMMODEL_ACQUIRE:
16605 case MEMMODEL_ACQ_REL:
16606 case MEMMODEL_SEQ_CST:
16607 emit_insn (gen_isync ());
16608 break;
16609 default:
16610 gcc_unreachable ();
16614 /* A subroutine of the various atomic expanders. For sub-word operations,
16615 we must adjust things to operate on SImode. Given the original MEM,
16616 return a new aligned memory. Also build and return the quantities by
16617 which to shift and mask. */
16619 static rtx
16620 rs6000_adjust_atomic_subword (rtx orig_mem, rtx *pshift, rtx *pmask)
16622 rtx addr, align, shift, mask, mem;
16623 HOST_WIDE_INT shift_mask;
16624 enum machine_mode mode = GET_MODE (orig_mem);
16626 /* For smaller modes, we have to implement this via SImode. */
16627 shift_mask = (mode == QImode ? 0x18 : 0x10);
16629 addr = XEXP (orig_mem, 0);
16630 addr = force_reg (GET_MODE (addr), addr);
16632 /* Aligned memory containing subword. Generate a new memory. We
16633 do not want any of the existing MEM_ATTR data, as we're now
16634 accessing memory outside the original object. */
16635 align = expand_simple_binop (Pmode, AND, addr, GEN_INT (-4),
16636 NULL_RTX, 1, OPTAB_LIB_WIDEN);
16637 mem = gen_rtx_MEM (SImode, align);
16638 MEM_VOLATILE_P (mem) = MEM_VOLATILE_P (orig_mem);
16639 if (MEM_ALIAS_SET (orig_mem) == ALIAS_SET_MEMORY_BARRIER)
16640 set_mem_alias_set (mem, ALIAS_SET_MEMORY_BARRIER);
16642 /* Shift amount for subword relative to aligned word. */
16643 shift = gen_reg_rtx (SImode);
16644 addr = gen_lowpart (SImode, addr);
16645 emit_insn (gen_rlwinm (shift, addr, GEN_INT (3), GEN_INT (shift_mask)));
16646 shift = expand_simple_binop (SImode, XOR, shift, GEN_INT (shift_mask),
16647 shift, 1, OPTAB_LIB_WIDEN);
16648 *pshift = shift;
16650 /* Mask for insertion. */
16651 mask = expand_simple_binop (SImode, ASHIFT, GEN_INT (GET_MODE_MASK (mode)),
16652 shift, NULL_RTX, 1, OPTAB_LIB_WIDEN);
16653 *pmask = mask;
16655 return mem;
16658 /* A subroutine of the various atomic expanders. For sub-word operands,
16659 combine OLDVAL and NEWVAL via MASK. Returns a new pseduo. */
16661 static rtx
16662 rs6000_mask_atomic_subword (rtx oldval, rtx newval, rtx mask)
16664 rtx x;
16666 x = gen_reg_rtx (SImode);
16667 emit_insn (gen_rtx_SET (VOIDmode, x,
16668 gen_rtx_AND (SImode,
16669 gen_rtx_NOT (SImode, mask),
16670 oldval)));
16672 x = expand_simple_binop (SImode, IOR, newval, x, x, 1, OPTAB_LIB_WIDEN);
16674 return x;
16677 /* A subroutine of the various atomic expanders. For sub-word operands,
16678 extract WIDE to NARROW via SHIFT. */
16680 static void
16681 rs6000_finish_atomic_subword (rtx narrow, rtx wide, rtx shift)
16683 wide = expand_simple_binop (SImode, LSHIFTRT, wide, shift,
16684 wide, 1, OPTAB_LIB_WIDEN);
16685 emit_move_insn (narrow, gen_lowpart (GET_MODE (narrow), wide));
16688 /* Expand an atomic compare and swap operation. */
16690 void
16691 rs6000_expand_atomic_compare_and_swap (rtx operands[])
16693 rtx boolval, retval, mem, oldval, newval, cond;
16694 rtx label1, label2, x, mask, shift;
16695 enum machine_mode mode;
16696 enum memmodel mod_s, mod_f;
16697 bool is_weak;
16699 boolval = operands[0];
16700 retval = operands[1];
16701 mem = operands[2];
16702 oldval = operands[3];
16703 newval = operands[4];
16704 is_weak = (INTVAL (operands[5]) != 0);
16705 mod_s = (enum memmodel) INTVAL (operands[6]);
16706 mod_f = (enum memmodel) INTVAL (operands[7]);
16707 mode = GET_MODE (mem);
16709 mask = shift = NULL_RTX;
16710 if (mode == QImode || mode == HImode)
16712 mem = rs6000_adjust_atomic_subword (mem, &shift, &mask);
16714 /* Shift and mask OLDVAL into position with the word. */
16715 oldval = convert_modes (SImode, mode, oldval, 1);
16716 oldval = expand_simple_binop (SImode, ASHIFT, oldval, shift,
16717 NULL_RTX, 1, OPTAB_LIB_WIDEN);
16719 /* Shift and mask NEWVAL into position within the word. */
16720 newval = convert_modes (SImode, mode, newval, 1);
16721 newval = expand_simple_binop (SImode, ASHIFT, newval, shift,
16722 NULL_RTX, 1, OPTAB_LIB_WIDEN);
16724 /* Prepare to adjust the return value. */
16725 retval = gen_reg_rtx (SImode);
16726 mode = SImode;
16728 else if (reg_overlap_mentioned_p (retval, oldval))
16729 oldval = copy_to_reg (oldval);
16731 mem = rs6000_pre_atomic_barrier (mem, mod_s);
16733 label1 = NULL_RTX;
16734 if (!is_weak)
16736 label1 = gen_rtx_LABEL_REF (VOIDmode, gen_label_rtx ());
16737 emit_label (XEXP (label1, 0));
16739 label2 = gen_rtx_LABEL_REF (VOIDmode, gen_label_rtx ());
16741 emit_load_locked (mode, retval, mem);
16743 x = retval;
16744 if (mask)
16746 x = expand_simple_binop (SImode, AND, retval, mask,
16747 NULL_RTX, 1, OPTAB_LIB_WIDEN);
16750 cond = gen_reg_rtx (CCmode);
16751 x = gen_rtx_COMPARE (CCmode, x, oldval);
16752 emit_insn (gen_rtx_SET (VOIDmode, cond, x));
16754 x = gen_rtx_NE (VOIDmode, cond, const0_rtx);
16755 emit_unlikely_jump (x, label2);
16757 x = newval;
16758 if (mask)
16759 x = rs6000_mask_atomic_subword (retval, newval, mask);
16761 emit_store_conditional (mode, cond, mem, x);
16763 if (!is_weak)
16765 x = gen_rtx_NE (VOIDmode, cond, const0_rtx);
16766 emit_unlikely_jump (x, label1);
16769 if (mod_f != MEMMODEL_RELAXED)
16770 emit_label (XEXP (label2, 0));
16772 rs6000_post_atomic_barrier (mod_s);
16774 if (mod_f == MEMMODEL_RELAXED)
16775 emit_label (XEXP (label2, 0));
16777 if (shift)
16778 rs6000_finish_atomic_subword (operands[1], retval, shift);
16780 /* In all cases, CR0 contains EQ on success, and NE on failure. */
16781 x = gen_rtx_EQ (SImode, cond, const0_rtx);
16782 emit_insn (gen_rtx_SET (VOIDmode, boolval, x));
16785 /* Expand an atomic exchange operation. */
16787 void
16788 rs6000_expand_atomic_exchange (rtx operands[])
16790 rtx retval, mem, val, cond;
16791 enum machine_mode mode;
16792 enum memmodel model;
16793 rtx label, x, mask, shift;
16795 retval = operands[0];
16796 mem = operands[1];
16797 val = operands[2];
16798 model = (enum memmodel) INTVAL (operands[3]);
16799 mode = GET_MODE (mem);
16801 mask = shift = NULL_RTX;
16802 if (mode == QImode || mode == HImode)
16804 mem = rs6000_adjust_atomic_subword (mem, &shift, &mask);
16806 /* Shift and mask VAL into position with the word. */
16807 val = convert_modes (SImode, mode, val, 1);
16808 val = expand_simple_binop (SImode, ASHIFT, val, shift,
16809 NULL_RTX, 1, OPTAB_LIB_WIDEN);
16811 /* Prepare to adjust the return value. */
16812 retval = gen_reg_rtx (SImode);
16813 mode = SImode;
16816 mem = rs6000_pre_atomic_barrier (mem, model);
16818 label = gen_rtx_LABEL_REF (VOIDmode, gen_label_rtx ());
16819 emit_label (XEXP (label, 0));
16821 emit_load_locked (mode, retval, mem);
16823 x = val;
16824 if (mask)
16825 x = rs6000_mask_atomic_subword (retval, val, mask);
16827 cond = gen_reg_rtx (CCmode);
16828 emit_store_conditional (mode, cond, mem, x);
16830 x = gen_rtx_NE (VOIDmode, cond, const0_rtx);
16831 emit_unlikely_jump (x, label);
16833 rs6000_post_atomic_barrier (model);
16835 if (shift)
16836 rs6000_finish_atomic_subword (operands[0], retval, shift);
16839 /* Expand an atomic fetch-and-operate pattern. CODE is the binary operation
16840 to perform. MEM is the memory on which to operate. VAL is the second
16841 operand of the binary operator. BEFORE and AFTER are optional locations to
16842 return the value of MEM either before of after the operation. MODEL_RTX
16843 is a CONST_INT containing the memory model to use. */
16845 void
16846 rs6000_expand_atomic_op (enum rtx_code code, rtx mem, rtx val,
16847 rtx orig_before, rtx orig_after, rtx model_rtx)
16849 enum memmodel model = (enum memmodel) INTVAL (model_rtx);
16850 enum machine_mode mode = GET_MODE (mem);
16851 rtx label, x, cond, mask, shift;
16852 rtx before = orig_before, after = orig_after;
16854 mask = shift = NULL_RTX;
16855 if (mode == QImode || mode == HImode)
16857 mem = rs6000_adjust_atomic_subword (mem, &shift, &mask);
16859 /* Shift and mask VAL into position with the word. */
16860 val = convert_modes (SImode, mode, val, 1);
16861 val = expand_simple_binop (SImode, ASHIFT, val, shift,
16862 NULL_RTX, 1, OPTAB_LIB_WIDEN);
16864 switch (code)
16866 case IOR:
16867 case XOR:
16868 /* We've already zero-extended VAL. That is sufficient to
16869 make certain that it does not affect other bits. */
16870 mask = NULL;
16871 break;
16873 case AND:
16874 /* If we make certain that all of the other bits in VAL are
16875 set, that will be sufficient to not affect other bits. */
16876 x = gen_rtx_NOT (SImode, mask);
16877 x = gen_rtx_IOR (SImode, x, val);
16878 emit_insn (gen_rtx_SET (VOIDmode, val, x));
16879 mask = NULL;
16880 break;
16882 case NOT:
16883 case PLUS:
16884 case MINUS:
16885 /* These will all affect bits outside the field and need
16886 adjustment via MASK within the loop. */
16887 break;
16889 default:
16890 gcc_unreachable ();
16893 /* Prepare to adjust the return value. */
16894 before = gen_reg_rtx (SImode);
16895 if (after)
16896 after = gen_reg_rtx (SImode);
16897 mode = SImode;
16900 mem = rs6000_pre_atomic_barrier (mem, model);
16902 label = gen_label_rtx ();
16903 emit_label (label);
16904 label = gen_rtx_LABEL_REF (VOIDmode, label);
16906 if (before == NULL_RTX)
16907 before = gen_reg_rtx (mode);
16909 emit_load_locked (mode, before, mem);
16911 if (code == NOT)
16913 x = expand_simple_binop (mode, AND, before, val,
16914 NULL_RTX, 1, OPTAB_LIB_WIDEN);
16915 after = expand_simple_unop (mode, NOT, x, after, 1);
16917 else
16919 after = expand_simple_binop (mode, code, before, val,
16920 after, 1, OPTAB_LIB_WIDEN);
16923 x = after;
16924 if (mask)
16926 x = expand_simple_binop (SImode, AND, after, mask,
16927 NULL_RTX, 1, OPTAB_LIB_WIDEN);
16928 x = rs6000_mask_atomic_subword (before, x, mask);
16931 cond = gen_reg_rtx (CCmode);
16932 emit_store_conditional (mode, cond, mem, x);
16934 x = gen_rtx_NE (VOIDmode, cond, const0_rtx);
16935 emit_unlikely_jump (x, label);
16937 rs6000_post_atomic_barrier (model);
16939 if (shift)
16941 if (orig_before)
16942 rs6000_finish_atomic_subword (orig_before, before, shift);
16943 if (orig_after)
16944 rs6000_finish_atomic_subword (orig_after, after, shift);
16946 else if (orig_after && after != orig_after)
16947 emit_move_insn (orig_after, after);
16950 /* Emit instructions to move SRC to DST. Called by splitters for
16951 multi-register moves. It will emit at most one instruction for
16952 each register that is accessed; that is, it won't emit li/lis pairs
16953 (or equivalent for 64-bit code). One of SRC or DST must be a hard
16954 register. */
16956 void
16957 rs6000_split_multireg_move (rtx dst, rtx src)
16959 /* The register number of the first register being moved. */
16960 int reg;
16961 /* The mode that is to be moved. */
16962 enum machine_mode mode;
16963 /* The mode that the move is being done in, and its size. */
16964 enum machine_mode reg_mode;
16965 int reg_mode_size;
16966 /* The number of registers that will be moved. */
16967 int nregs;
16969 reg = REG_P (dst) ? REGNO (dst) : REGNO (src);
16970 mode = GET_MODE (dst);
16971 nregs = hard_regno_nregs[reg][mode];
16972 if (FP_REGNO_P (reg))
16973 reg_mode = DECIMAL_FLOAT_MODE_P (mode) ? DDmode :
16974 ((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT) ? DFmode : SFmode);
16975 else if (ALTIVEC_REGNO_P (reg))
16976 reg_mode = V16QImode;
16977 else if (TARGET_E500_DOUBLE && mode == TFmode)
16978 reg_mode = DFmode;
16979 else
16980 reg_mode = word_mode;
16981 reg_mode_size = GET_MODE_SIZE (reg_mode);
16983 gcc_assert (reg_mode_size * nregs == GET_MODE_SIZE (mode));
16985 if (REG_P (src) && REG_P (dst) && (REGNO (src) < REGNO (dst)))
16987 /* Move register range backwards, if we might have destructive
16988 overlap. */
16989 int i;
16990 for (i = nregs - 1; i >= 0; i--)
16991 emit_insn (gen_rtx_SET (VOIDmode,
16992 simplify_gen_subreg (reg_mode, dst, mode,
16993 i * reg_mode_size),
16994 simplify_gen_subreg (reg_mode, src, mode,
16995 i * reg_mode_size)));
16997 else
16999 int i;
17000 int j = -1;
17001 bool used_update = false;
17002 rtx restore_basereg = NULL_RTX;
17004 if (MEM_P (src) && INT_REGNO_P (reg))
17006 rtx breg;
17008 if (GET_CODE (XEXP (src, 0)) == PRE_INC
17009 || GET_CODE (XEXP (src, 0)) == PRE_DEC)
17011 rtx delta_rtx;
17012 breg = XEXP (XEXP (src, 0), 0);
17013 delta_rtx = (GET_CODE (XEXP (src, 0)) == PRE_INC
17014 ? GEN_INT (GET_MODE_SIZE (GET_MODE (src)))
17015 : GEN_INT (-GET_MODE_SIZE (GET_MODE (src))));
17016 emit_insn (gen_add3_insn (breg, breg, delta_rtx));
17017 src = replace_equiv_address (src, breg);
17019 else if (! rs6000_offsettable_memref_p (src, reg_mode))
17021 if (GET_CODE (XEXP (src, 0)) == PRE_MODIFY)
17023 rtx basereg = XEXP (XEXP (src, 0), 0);
17024 if (TARGET_UPDATE)
17026 rtx ndst = simplify_gen_subreg (reg_mode, dst, mode, 0);
17027 emit_insn (gen_rtx_SET (VOIDmode, ndst,
17028 gen_rtx_MEM (reg_mode, XEXP (src, 0))));
17029 used_update = true;
17031 else
17032 emit_insn (gen_rtx_SET (VOIDmode, basereg,
17033 XEXP (XEXP (src, 0), 1)));
17034 src = replace_equiv_address (src, basereg);
17036 else
17038 rtx basereg = gen_rtx_REG (Pmode, reg);
17039 emit_insn (gen_rtx_SET (VOIDmode, basereg, XEXP (src, 0)));
17040 src = replace_equiv_address (src, basereg);
17044 breg = XEXP (src, 0);
17045 if (GET_CODE (breg) == PLUS || GET_CODE (breg) == LO_SUM)
17046 breg = XEXP (breg, 0);
17048 /* If the base register we are using to address memory is
17049 also a destination reg, then change that register last. */
17050 if (REG_P (breg)
17051 && REGNO (breg) >= REGNO (dst)
17052 && REGNO (breg) < REGNO (dst) + nregs)
17053 j = REGNO (breg) - REGNO (dst);
17055 else if (MEM_P (dst) && INT_REGNO_P (reg))
17057 rtx breg;
17059 if (GET_CODE (XEXP (dst, 0)) == PRE_INC
17060 || GET_CODE (XEXP (dst, 0)) == PRE_DEC)
17062 rtx delta_rtx;
17063 breg = XEXP (XEXP (dst, 0), 0);
17064 delta_rtx = (GET_CODE (XEXP (dst, 0)) == PRE_INC
17065 ? GEN_INT (GET_MODE_SIZE (GET_MODE (dst)))
17066 : GEN_INT (-GET_MODE_SIZE (GET_MODE (dst))));
17068 /* We have to update the breg before doing the store.
17069 Use store with update, if available. */
17071 if (TARGET_UPDATE)
17073 rtx nsrc = simplify_gen_subreg (reg_mode, src, mode, 0);
17074 emit_insn (TARGET_32BIT
17075 ? (TARGET_POWERPC64
17076 ? gen_movdi_si_update (breg, breg, delta_rtx, nsrc)
17077 : gen_movsi_update (breg, breg, delta_rtx, nsrc))
17078 : gen_movdi_di_update (breg, breg, delta_rtx, nsrc));
17079 used_update = true;
17081 else
17082 emit_insn (gen_add3_insn (breg, breg, delta_rtx));
17083 dst = replace_equiv_address (dst, breg);
17085 else if (!rs6000_offsettable_memref_p (dst, reg_mode)
17086 && GET_CODE (XEXP (dst, 0)) != LO_SUM)
17088 if (GET_CODE (XEXP (dst, 0)) == PRE_MODIFY)
17090 rtx basereg = XEXP (XEXP (dst, 0), 0);
17091 if (TARGET_UPDATE)
17093 rtx nsrc = simplify_gen_subreg (reg_mode, src, mode, 0);
17094 emit_insn (gen_rtx_SET (VOIDmode,
17095 gen_rtx_MEM (reg_mode, XEXP (dst, 0)), nsrc));
17096 used_update = true;
17098 else
17099 emit_insn (gen_rtx_SET (VOIDmode, basereg,
17100 XEXP (XEXP (dst, 0), 1)));
17101 dst = replace_equiv_address (dst, basereg);
17103 else
17105 rtx basereg = XEXP (XEXP (dst, 0), 0);
17106 rtx offsetreg = XEXP (XEXP (dst, 0), 1);
17107 gcc_assert (GET_CODE (XEXP (dst, 0)) == PLUS
17108 && REG_P (basereg)
17109 && REG_P (offsetreg)
17110 && REGNO (basereg) != REGNO (offsetreg));
17111 if (REGNO (basereg) == 0)
17113 rtx tmp = offsetreg;
17114 offsetreg = basereg;
17115 basereg = tmp;
17117 emit_insn (gen_add3_insn (basereg, basereg, offsetreg));
17118 restore_basereg = gen_sub3_insn (basereg, basereg, offsetreg);
17119 dst = replace_equiv_address (dst, basereg);
17122 else if (GET_CODE (XEXP (dst, 0)) != LO_SUM)
17123 gcc_assert (rs6000_offsettable_memref_p (dst, reg_mode));
17126 for (i = 0; i < nregs; i++)
17128 /* Calculate index to next subword. */
17129 ++j;
17130 if (j == nregs)
17131 j = 0;
17133 /* If compiler already emitted move of first word by
17134 store with update, no need to do anything. */
17135 if (j == 0 && used_update)
17136 continue;
17138 emit_insn (gen_rtx_SET (VOIDmode,
17139 simplify_gen_subreg (reg_mode, dst, mode,
17140 j * reg_mode_size),
17141 simplify_gen_subreg (reg_mode, src, mode,
17142 j * reg_mode_size)));
17144 if (restore_basereg != NULL_RTX)
17145 emit_insn (restore_basereg);
17150 /* This page contains routines that are used to determine what the
17151 function prologue and epilogue code will do and write them out. */
17153 static inline bool
17154 save_reg_p (int r)
17156 return !call_used_regs[r] && df_regs_ever_live_p (r);
17159 /* Return the first fixed-point register that is required to be
17160 saved. 32 if none. */
17163 first_reg_to_save (void)
17165 int first_reg;
17167 /* Find lowest numbered live register. */
17168 for (first_reg = 13; first_reg <= 31; first_reg++)
17169 if (save_reg_p (first_reg))
17170 break;
17172 if (first_reg > RS6000_PIC_OFFSET_TABLE_REGNUM
17173 && ((DEFAULT_ABI == ABI_V4 && flag_pic != 0)
17174 || (DEFAULT_ABI == ABI_DARWIN && flag_pic)
17175 || (TARGET_TOC && TARGET_MINIMAL_TOC))
17176 && df_regs_ever_live_p (RS6000_PIC_OFFSET_TABLE_REGNUM))
17177 first_reg = RS6000_PIC_OFFSET_TABLE_REGNUM;
17179 #if TARGET_MACHO
17180 if (flag_pic
17181 && crtl->uses_pic_offset_table
17182 && first_reg > RS6000_PIC_OFFSET_TABLE_REGNUM)
17183 return RS6000_PIC_OFFSET_TABLE_REGNUM;
17184 #endif
17186 return first_reg;
17189 /* Similar, for FP regs. */
17192 first_fp_reg_to_save (void)
17194 int first_reg;
17196 /* Find lowest numbered live register. */
17197 for (first_reg = 14 + 32; first_reg <= 63; first_reg++)
17198 if (save_reg_p (first_reg))
17199 break;
17201 return first_reg;
17204 /* Similar, for AltiVec regs. */
17206 static int
17207 first_altivec_reg_to_save (void)
17209 int i;
17211 /* Stack frame remains as is unless we are in AltiVec ABI. */
17212 if (! TARGET_ALTIVEC_ABI)
17213 return LAST_ALTIVEC_REGNO + 1;
17215 /* On Darwin, the unwind routines are compiled without
17216 TARGET_ALTIVEC, and use save_world to save/restore the
17217 altivec registers when necessary. */
17218 if (DEFAULT_ABI == ABI_DARWIN && crtl->calls_eh_return
17219 && ! TARGET_ALTIVEC)
17220 return FIRST_ALTIVEC_REGNO + 20;
17222 /* Find lowest numbered live register. */
17223 for (i = FIRST_ALTIVEC_REGNO + 20; i <= LAST_ALTIVEC_REGNO; ++i)
17224 if (save_reg_p (i))
17225 break;
17227 return i;
17230 /* Return a 32-bit mask of the AltiVec registers we need to set in
17231 VRSAVE. Bit n of the return value is 1 if Vn is live. The MSB in
17232 the 32-bit word is 0. */
17234 static unsigned int
17235 compute_vrsave_mask (void)
17237 unsigned int i, mask = 0;
17239 /* On Darwin, the unwind routines are compiled without
17240 TARGET_ALTIVEC, and use save_world to save/restore the
17241 call-saved altivec registers when necessary. */
17242 if (DEFAULT_ABI == ABI_DARWIN && crtl->calls_eh_return
17243 && ! TARGET_ALTIVEC)
17244 mask |= 0xFFF;
17246 /* First, find out if we use _any_ altivec registers. */
17247 for (i = FIRST_ALTIVEC_REGNO; i <= LAST_ALTIVEC_REGNO; ++i)
17248 if (df_regs_ever_live_p (i))
17249 mask |= ALTIVEC_REG_BIT (i);
17251 if (mask == 0)
17252 return mask;
17254 /* Next, remove the argument registers from the set. These must
17255 be in the VRSAVE mask set by the caller, so we don't need to add
17256 them in again. More importantly, the mask we compute here is
17257 used to generate CLOBBERs in the set_vrsave insn, and we do not
17258 wish the argument registers to die. */
17259 for (i = crtl->args.info.vregno - 1; i >= ALTIVEC_ARG_MIN_REG; --i)
17260 mask &= ~ALTIVEC_REG_BIT (i);
17262 /* Similarly, remove the return value from the set. */
17264 bool yes = false;
17265 diddle_return_value (is_altivec_return_reg, &yes);
17266 if (yes)
17267 mask &= ~ALTIVEC_REG_BIT (ALTIVEC_ARG_RETURN);
17270 return mask;
17273 /* For a very restricted set of circumstances, we can cut down the
17274 size of prologues/epilogues by calling our own save/restore-the-world
17275 routines. */
17277 static void
17278 compute_save_world_info (rs6000_stack_t *info_ptr)
17280 info_ptr->world_save_p = 1;
17281 info_ptr->world_save_p
17282 = (WORLD_SAVE_P (info_ptr)
17283 && DEFAULT_ABI == ABI_DARWIN
17284 && !cfun->has_nonlocal_label
17285 && info_ptr->first_fp_reg_save == FIRST_SAVED_FP_REGNO
17286 && info_ptr->first_gp_reg_save == FIRST_SAVED_GP_REGNO
17287 && info_ptr->first_altivec_reg_save == FIRST_SAVED_ALTIVEC_REGNO
17288 && info_ptr->cr_save_p);
17290 /* This will not work in conjunction with sibcalls. Make sure there
17291 are none. (This check is expensive, but seldom executed.) */
17292 if (WORLD_SAVE_P (info_ptr))
17294 rtx insn;
17295 for ( insn = get_last_insn_anywhere (); insn; insn = PREV_INSN (insn))
17296 if ( GET_CODE (insn) == CALL_INSN
17297 && SIBLING_CALL_P (insn))
17299 info_ptr->world_save_p = 0;
17300 break;
17304 if (WORLD_SAVE_P (info_ptr))
17306 /* Even if we're not touching VRsave, make sure there's room on the
17307 stack for it, if it looks like we're calling SAVE_WORLD, which
17308 will attempt to save it. */
17309 info_ptr->vrsave_size = 4;
17311 /* If we are going to save the world, we need to save the link register too. */
17312 info_ptr->lr_save_p = 1;
17314 /* "Save" the VRsave register too if we're saving the world. */
17315 if (info_ptr->vrsave_mask == 0)
17316 info_ptr->vrsave_mask = compute_vrsave_mask ();
17318 /* Because the Darwin register save/restore routines only handle
17319 F14 .. F31 and V20 .. V31 as per the ABI, perform a consistency
17320 check. */
17321 gcc_assert (info_ptr->first_fp_reg_save >= FIRST_SAVED_FP_REGNO
17322 && (info_ptr->first_altivec_reg_save
17323 >= FIRST_SAVED_ALTIVEC_REGNO));
17325 return;
17329 static void
17330 is_altivec_return_reg (rtx reg, void *xyes)
17332 bool *yes = (bool *) xyes;
17333 if (REGNO (reg) == ALTIVEC_ARG_RETURN)
17334 *yes = true;
17338 /* Look for user-defined global regs in the range FIRST to LAST-1.
17339 We should not restore these, and so cannot use lmw or out-of-line
17340 restore functions if there are any. We also can't save them
17341 (well, emit frame notes for them), because frame unwinding during
17342 exception handling will restore saved registers. */
17344 static bool
17345 global_regs_p (unsigned first, unsigned last)
17347 while (first < last)
17348 if (global_regs[first++])
17349 return true;
17350 return false;
17353 /* Determine the strategy for savings/restoring registers. */
17355 enum {
17356 SAVRES_MULTIPLE = 0x1,
17357 SAVE_INLINE_FPRS = 0x2,
17358 SAVE_INLINE_GPRS = 0x4,
17359 REST_INLINE_FPRS = 0x8,
17360 REST_INLINE_GPRS = 0x10,
17361 SAVE_NOINLINE_GPRS_SAVES_LR = 0x20,
17362 SAVE_NOINLINE_FPRS_SAVES_LR = 0x40,
17363 REST_NOINLINE_FPRS_DOESNT_RESTORE_LR = 0x80,
17364 SAVE_INLINE_VRS = 0x100,
17365 REST_INLINE_VRS = 0x200
17368 static int
17369 rs6000_savres_strategy (rs6000_stack_t *info,
17370 bool using_static_chain_p)
17372 int strategy = 0;
17373 bool lr_save_p;
17375 if (TARGET_MULTIPLE
17376 && !TARGET_POWERPC64
17377 && !(TARGET_SPE_ABI && info->spe_64bit_regs_used)
17378 && info->first_gp_reg_save < 31
17379 && !global_regs_p (info->first_gp_reg_save, 32))
17380 strategy |= SAVRES_MULTIPLE;
17382 if (crtl->calls_eh_return
17383 || cfun->machine->ra_need_lr)
17384 strategy |= (SAVE_INLINE_FPRS | REST_INLINE_FPRS
17385 | SAVE_INLINE_GPRS | REST_INLINE_GPRS
17386 | SAVE_INLINE_VRS | REST_INLINE_VRS);
17388 if (info->first_fp_reg_save == 64
17389 /* The out-of-line FP routines use double-precision stores;
17390 we can't use those routines if we don't have such stores. */
17391 || (TARGET_HARD_FLOAT && !TARGET_DOUBLE_FLOAT)
17392 || global_regs_p (info->first_fp_reg_save, 64))
17393 strategy |= SAVE_INLINE_FPRS | REST_INLINE_FPRS;
17395 if (info->first_gp_reg_save == 32
17396 || (!(strategy & SAVRES_MULTIPLE)
17397 && global_regs_p (info->first_gp_reg_save, 32)))
17398 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
17400 if (info->first_altivec_reg_save == LAST_ALTIVEC_REGNO + 1
17401 || global_regs_p (info->first_altivec_reg_save, LAST_ALTIVEC_REGNO + 1))
17402 strategy |= SAVE_INLINE_VRS | REST_INLINE_VRS;
17404 /* Define cutoff for using out-of-line functions to save registers. */
17405 if (DEFAULT_ABI == ABI_V4 || TARGET_ELF)
17407 if (!optimize_size)
17409 strategy |= SAVE_INLINE_FPRS | REST_INLINE_FPRS;
17410 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
17411 strategy |= SAVE_INLINE_VRS | REST_INLINE_VRS;
17413 else
17415 /* Prefer out-of-line restore if it will exit. */
17416 if (info->first_fp_reg_save > 61)
17417 strategy |= SAVE_INLINE_FPRS;
17418 if (info->first_gp_reg_save > 29)
17420 if (info->first_fp_reg_save == 64)
17421 strategy |= SAVE_INLINE_GPRS;
17422 else
17423 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
17425 if (info->first_altivec_reg_save == LAST_ALTIVEC_REGNO)
17426 strategy |= SAVE_INLINE_VRS | REST_INLINE_VRS;
17429 else if (DEFAULT_ABI == ABI_DARWIN)
17431 if (info->first_fp_reg_save > 60)
17432 strategy |= SAVE_INLINE_FPRS | REST_INLINE_FPRS;
17433 if (info->first_gp_reg_save > 29)
17434 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
17435 strategy |= SAVE_INLINE_VRS | REST_INLINE_VRS;
17437 else
17439 gcc_checking_assert (DEFAULT_ABI == ABI_AIX);
17440 if (info->first_fp_reg_save > 61)
17441 strategy |= SAVE_INLINE_FPRS | REST_INLINE_FPRS;
17442 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
17443 strategy |= SAVE_INLINE_VRS | REST_INLINE_VRS;
17446 /* Don't bother to try to save things out-of-line if r11 is occupied
17447 by the static chain. It would require too much fiddling and the
17448 static chain is rarely used anyway. FPRs are saved w.r.t the stack
17449 pointer on Darwin, and AIX uses r1 or r12. */
17450 if (using_static_chain_p && DEFAULT_ABI != ABI_AIX)
17451 strategy |= ((DEFAULT_ABI == ABI_DARWIN ? 0 : SAVE_INLINE_FPRS)
17452 | SAVE_INLINE_GPRS
17453 | SAVE_INLINE_VRS | REST_INLINE_VRS);
17455 /* We can only use the out-of-line routines to restore if we've
17456 saved all the registers from first_fp_reg_save in the prologue.
17457 Otherwise, we risk loading garbage. */
17458 if ((strategy & (SAVE_INLINE_FPRS | REST_INLINE_FPRS)) == SAVE_INLINE_FPRS)
17460 int i;
17462 for (i = info->first_fp_reg_save; i < 64; i++)
17463 if (!save_reg_p (i))
17465 strategy |= REST_INLINE_FPRS;
17466 break;
17470 /* If we are going to use store multiple, then don't even bother
17471 with the out-of-line routines, since the store-multiple
17472 instruction will always be smaller. */
17473 if ((strategy & SAVRES_MULTIPLE))
17474 strategy |= SAVE_INLINE_GPRS;
17476 /* info->lr_save_p isn't yet set if the only reason lr needs to be
17477 saved is an out-of-line save or restore. Set up the value for
17478 the next test (excluding out-of-line gpr restore). */
17479 lr_save_p = (info->lr_save_p
17480 || !(strategy & SAVE_INLINE_GPRS)
17481 || !(strategy & SAVE_INLINE_FPRS)
17482 || !(strategy & SAVE_INLINE_VRS)
17483 || !(strategy & REST_INLINE_FPRS)
17484 || !(strategy & REST_INLINE_VRS));
17486 /* The situation is more complicated with load multiple. We'd
17487 prefer to use the out-of-line routines for restores, since the
17488 "exit" out-of-line routines can handle the restore of LR and the
17489 frame teardown. However if doesn't make sense to use the
17490 out-of-line routine if that is the only reason we'd need to save
17491 LR, and we can't use the "exit" out-of-line gpr restore if we
17492 have saved some fprs; In those cases it is advantageous to use
17493 load multiple when available. */
17494 if ((strategy & SAVRES_MULTIPLE)
17495 && (!lr_save_p
17496 || info->first_fp_reg_save != 64))
17497 strategy |= REST_INLINE_GPRS;
17499 /* Saving CR interferes with the exit routines used on the SPE, so
17500 just punt here. */
17501 if (TARGET_SPE_ABI
17502 && info->spe_64bit_regs_used
17503 && info->cr_save_p)
17504 strategy |= REST_INLINE_GPRS;
17506 /* We can only use load multiple or the out-of-line routines to
17507 restore if we've used store multiple or out-of-line routines
17508 in the prologue, i.e. if we've saved all the registers from
17509 first_gp_reg_save. Otherwise, we risk loading garbage. */
17510 if ((strategy & (SAVE_INLINE_GPRS | REST_INLINE_GPRS | SAVRES_MULTIPLE))
17511 == SAVE_INLINE_GPRS)
17513 int i;
17515 for (i = info->first_gp_reg_save; i < 32; i++)
17516 if (!save_reg_p (i))
17518 strategy |= REST_INLINE_GPRS;
17519 break;
17523 if (TARGET_ELF && TARGET_64BIT)
17525 if (!(strategy & SAVE_INLINE_FPRS))
17526 strategy |= SAVE_NOINLINE_FPRS_SAVES_LR;
17527 else if (!(strategy & SAVE_INLINE_GPRS)
17528 && info->first_fp_reg_save == 64)
17529 strategy |= SAVE_NOINLINE_GPRS_SAVES_LR;
17531 else if (TARGET_AIX && !(strategy & REST_INLINE_FPRS))
17532 strategy |= REST_NOINLINE_FPRS_DOESNT_RESTORE_LR;
17534 if (TARGET_MACHO && !(strategy & SAVE_INLINE_FPRS))
17535 strategy |= SAVE_NOINLINE_FPRS_SAVES_LR;
17537 return strategy;
17540 /* Calculate the stack information for the current function. This is
17541 complicated by having two separate calling sequences, the AIX calling
17542 sequence and the V.4 calling sequence.
17544 AIX (and Darwin/Mac OS X) stack frames look like:
17545 32-bit 64-bit
17546 SP----> +---------------------------------------+
17547 | back chain to caller | 0 0
17548 +---------------------------------------+
17549 | saved CR | 4 8 (8-11)
17550 +---------------------------------------+
17551 | saved LR | 8 16
17552 +---------------------------------------+
17553 | reserved for compilers | 12 24
17554 +---------------------------------------+
17555 | reserved for binders | 16 32
17556 +---------------------------------------+
17557 | saved TOC pointer | 20 40
17558 +---------------------------------------+
17559 | Parameter save area (P) | 24 48
17560 +---------------------------------------+
17561 | Alloca space (A) | 24+P etc.
17562 +---------------------------------------+
17563 | Local variable space (L) | 24+P+A
17564 +---------------------------------------+
17565 | Float/int conversion temporary (X) | 24+P+A+L
17566 +---------------------------------------+
17567 | Save area for AltiVec registers (W) | 24+P+A+L+X
17568 +---------------------------------------+
17569 | AltiVec alignment padding (Y) | 24+P+A+L+X+W
17570 +---------------------------------------+
17571 | Save area for VRSAVE register (Z) | 24+P+A+L+X+W+Y
17572 +---------------------------------------+
17573 | Save area for GP registers (G) | 24+P+A+X+L+X+W+Y+Z
17574 +---------------------------------------+
17575 | Save area for FP registers (F) | 24+P+A+X+L+X+W+Y+Z+G
17576 +---------------------------------------+
17577 old SP->| back chain to caller's caller |
17578 +---------------------------------------+
17580 The required alignment for AIX configurations is two words (i.e., 8
17581 or 16 bytes).
17584 V.4 stack frames look like:
17586 SP----> +---------------------------------------+
17587 | back chain to caller | 0
17588 +---------------------------------------+
17589 | caller's saved LR | 4
17590 +---------------------------------------+
17591 | Parameter save area (P) | 8
17592 +---------------------------------------+
17593 | Alloca space (A) | 8+P
17594 +---------------------------------------+
17595 | Varargs save area (V) | 8+P+A
17596 +---------------------------------------+
17597 | Local variable space (L) | 8+P+A+V
17598 +---------------------------------------+
17599 | Float/int conversion temporary (X) | 8+P+A+V+L
17600 +---------------------------------------+
17601 | Save area for AltiVec registers (W) | 8+P+A+V+L+X
17602 +---------------------------------------+
17603 | AltiVec alignment padding (Y) | 8+P+A+V+L+X+W
17604 +---------------------------------------+
17605 | Save area for VRSAVE register (Z) | 8+P+A+V+L+X+W+Y
17606 +---------------------------------------+
17607 | SPE: area for 64-bit GP registers |
17608 +---------------------------------------+
17609 | SPE alignment padding |
17610 +---------------------------------------+
17611 | saved CR (C) | 8+P+A+V+L+X+W+Y+Z
17612 +---------------------------------------+
17613 | Save area for GP registers (G) | 8+P+A+V+L+X+W+Y+Z+C
17614 +---------------------------------------+
17615 | Save area for FP registers (F) | 8+P+A+V+L+X+W+Y+Z+C+G
17616 +---------------------------------------+
17617 old SP->| back chain to caller's caller |
17618 +---------------------------------------+
17620 The required alignment for V.4 is 16 bytes, or 8 bytes if -meabi is
17621 given. (But note below and in sysv4.h that we require only 8 and
17622 may round up the size of our stack frame anyways. The historical
17623 reason is early versions of powerpc-linux which didn't properly
17624 align the stack at program startup. A happy side-effect is that
17625 -mno-eabi libraries can be used with -meabi programs.)
17627 The EABI configuration defaults to the V.4 layout. However,
17628 the stack alignment requirements may differ. If -mno-eabi is not
17629 given, the required stack alignment is 8 bytes; if -mno-eabi is
17630 given, the required alignment is 16 bytes. (But see V.4 comment
17631 above.) */
17633 #ifndef ABI_STACK_BOUNDARY
17634 #define ABI_STACK_BOUNDARY STACK_BOUNDARY
17635 #endif
17637 static rs6000_stack_t *
17638 rs6000_stack_info (void)
17640 rs6000_stack_t *info_ptr = &stack_info;
17641 int reg_size = TARGET_32BIT ? 4 : 8;
17642 int ehrd_size;
17643 int save_align;
17644 int first_gp;
17645 HOST_WIDE_INT non_fixed_size;
17646 bool using_static_chain_p;
17648 if (reload_completed && info_ptr->reload_completed)
17649 return info_ptr;
17651 memset (info_ptr, 0, sizeof (*info_ptr));
17652 info_ptr->reload_completed = reload_completed;
17654 if (TARGET_SPE)
17656 /* Cache value so we don't rescan instruction chain over and over. */
17657 if (cfun->machine->insn_chain_scanned_p == 0)
17658 cfun->machine->insn_chain_scanned_p
17659 = spe_func_has_64bit_regs_p () + 1;
17660 info_ptr->spe_64bit_regs_used = cfun->machine->insn_chain_scanned_p - 1;
17663 /* Select which calling sequence. */
17664 info_ptr->abi = DEFAULT_ABI;
17666 /* Calculate which registers need to be saved & save area size. */
17667 info_ptr->first_gp_reg_save = first_reg_to_save ();
17668 /* Assume that we will have to save RS6000_PIC_OFFSET_TABLE_REGNUM,
17669 even if it currently looks like we won't. Reload may need it to
17670 get at a constant; if so, it will have already created a constant
17671 pool entry for it. */
17672 if (((TARGET_TOC && TARGET_MINIMAL_TOC)
17673 || (flag_pic == 1 && DEFAULT_ABI == ABI_V4)
17674 || (flag_pic && DEFAULT_ABI == ABI_DARWIN))
17675 && crtl->uses_const_pool
17676 && info_ptr->first_gp_reg_save > RS6000_PIC_OFFSET_TABLE_REGNUM)
17677 first_gp = RS6000_PIC_OFFSET_TABLE_REGNUM;
17678 else
17679 first_gp = info_ptr->first_gp_reg_save;
17681 info_ptr->gp_size = reg_size * (32 - first_gp);
17683 /* For the SPE, we have an additional upper 32-bits on each GPR.
17684 Ideally we should save the entire 64-bits only when the upper
17685 half is used in SIMD instructions. Since we only record
17686 registers live (not the size they are used in), this proves
17687 difficult because we'd have to traverse the instruction chain at
17688 the right time, taking reload into account. This is a real pain,
17689 so we opt to save the GPRs in 64-bits always if but one register
17690 gets used in 64-bits. Otherwise, all the registers in the frame
17691 get saved in 32-bits.
17693 So... since when we save all GPRs (except the SP) in 64-bits, the
17694 traditional GP save area will be empty. */
17695 if (TARGET_SPE_ABI && info_ptr->spe_64bit_regs_used != 0)
17696 info_ptr->gp_size = 0;
17698 info_ptr->first_fp_reg_save = first_fp_reg_to_save ();
17699 info_ptr->fp_size = 8 * (64 - info_ptr->first_fp_reg_save);
17701 info_ptr->first_altivec_reg_save = first_altivec_reg_to_save ();
17702 info_ptr->altivec_size = 16 * (LAST_ALTIVEC_REGNO + 1
17703 - info_ptr->first_altivec_reg_save);
17705 /* Does this function call anything? */
17706 info_ptr->calls_p = (! crtl->is_leaf
17707 || cfun->machine->ra_needs_full_frame);
17709 /* Determine if we need to save the condition code registers. */
17710 if (df_regs_ever_live_p (CR2_REGNO)
17711 || df_regs_ever_live_p (CR3_REGNO)
17712 || df_regs_ever_live_p (CR4_REGNO))
17714 info_ptr->cr_save_p = 1;
17715 if (DEFAULT_ABI == ABI_V4)
17716 info_ptr->cr_size = reg_size;
17719 /* If the current function calls __builtin_eh_return, then we need
17720 to allocate stack space for registers that will hold data for
17721 the exception handler. */
17722 if (crtl->calls_eh_return)
17724 unsigned int i;
17725 for (i = 0; EH_RETURN_DATA_REGNO (i) != INVALID_REGNUM; ++i)
17726 continue;
17728 /* SPE saves EH registers in 64-bits. */
17729 ehrd_size = i * (TARGET_SPE_ABI
17730 && info_ptr->spe_64bit_regs_used != 0
17731 ? UNITS_PER_SPE_WORD : UNITS_PER_WORD);
17733 else
17734 ehrd_size = 0;
17736 /* Determine various sizes. */
17737 info_ptr->reg_size = reg_size;
17738 info_ptr->fixed_size = RS6000_SAVE_AREA;
17739 info_ptr->vars_size = RS6000_ALIGN (get_frame_size (), 8);
17740 info_ptr->parm_size = RS6000_ALIGN (crtl->outgoing_args_size,
17741 TARGET_ALTIVEC ? 16 : 8);
17742 if (FRAME_GROWS_DOWNWARD)
17743 info_ptr->vars_size
17744 += RS6000_ALIGN (info_ptr->fixed_size + info_ptr->vars_size
17745 + info_ptr->parm_size,
17746 ABI_STACK_BOUNDARY / BITS_PER_UNIT)
17747 - (info_ptr->fixed_size + info_ptr->vars_size
17748 + info_ptr->parm_size);
17750 if (TARGET_SPE_ABI && info_ptr->spe_64bit_regs_used != 0)
17751 info_ptr->spe_gp_size = 8 * (32 - first_gp);
17752 else
17753 info_ptr->spe_gp_size = 0;
17755 if (TARGET_ALTIVEC_ABI)
17756 info_ptr->vrsave_mask = compute_vrsave_mask ();
17757 else
17758 info_ptr->vrsave_mask = 0;
17760 if (TARGET_ALTIVEC_VRSAVE && info_ptr->vrsave_mask)
17761 info_ptr->vrsave_size = 4;
17762 else
17763 info_ptr->vrsave_size = 0;
17765 compute_save_world_info (info_ptr);
17767 /* Calculate the offsets. */
17768 switch (DEFAULT_ABI)
17770 case ABI_NONE:
17771 default:
17772 gcc_unreachable ();
17774 case ABI_AIX:
17775 case ABI_DARWIN:
17776 info_ptr->fp_save_offset = - info_ptr->fp_size;
17777 info_ptr->gp_save_offset = info_ptr->fp_save_offset - info_ptr->gp_size;
17779 if (TARGET_ALTIVEC_ABI)
17781 info_ptr->vrsave_save_offset
17782 = info_ptr->gp_save_offset - info_ptr->vrsave_size;
17784 /* Align stack so vector save area is on a quadword boundary.
17785 The padding goes above the vectors. */
17786 if (info_ptr->altivec_size != 0)
17787 info_ptr->altivec_padding_size
17788 = info_ptr->vrsave_save_offset & 0xF;
17789 else
17790 info_ptr->altivec_padding_size = 0;
17792 info_ptr->altivec_save_offset
17793 = info_ptr->vrsave_save_offset
17794 - info_ptr->altivec_padding_size
17795 - info_ptr->altivec_size;
17796 gcc_assert (info_ptr->altivec_size == 0
17797 || info_ptr->altivec_save_offset % 16 == 0);
17799 /* Adjust for AltiVec case. */
17800 info_ptr->ehrd_offset = info_ptr->altivec_save_offset - ehrd_size;
17802 else
17803 info_ptr->ehrd_offset = info_ptr->gp_save_offset - ehrd_size;
17804 info_ptr->cr_save_offset = reg_size; /* first word when 64-bit. */
17805 info_ptr->lr_save_offset = 2*reg_size;
17806 break;
17808 case ABI_V4:
17809 info_ptr->fp_save_offset = - info_ptr->fp_size;
17810 info_ptr->gp_save_offset = info_ptr->fp_save_offset - info_ptr->gp_size;
17811 info_ptr->cr_save_offset = info_ptr->gp_save_offset - info_ptr->cr_size;
17813 if (TARGET_SPE_ABI && info_ptr->spe_64bit_regs_used != 0)
17815 /* Align stack so SPE GPR save area is aligned on a
17816 double-word boundary. */
17817 if (info_ptr->spe_gp_size != 0 && info_ptr->cr_save_offset != 0)
17818 info_ptr->spe_padding_size
17819 = 8 - (-info_ptr->cr_save_offset % 8);
17820 else
17821 info_ptr->spe_padding_size = 0;
17823 info_ptr->spe_gp_save_offset
17824 = info_ptr->cr_save_offset
17825 - info_ptr->spe_padding_size
17826 - info_ptr->spe_gp_size;
17828 /* Adjust for SPE case. */
17829 info_ptr->ehrd_offset = info_ptr->spe_gp_save_offset;
17831 else if (TARGET_ALTIVEC_ABI)
17833 info_ptr->vrsave_save_offset
17834 = info_ptr->cr_save_offset - info_ptr->vrsave_size;
17836 /* Align stack so vector save area is on a quadword boundary. */
17837 if (info_ptr->altivec_size != 0)
17838 info_ptr->altivec_padding_size
17839 = 16 - (-info_ptr->vrsave_save_offset % 16);
17840 else
17841 info_ptr->altivec_padding_size = 0;
17843 info_ptr->altivec_save_offset
17844 = info_ptr->vrsave_save_offset
17845 - info_ptr->altivec_padding_size
17846 - info_ptr->altivec_size;
17848 /* Adjust for AltiVec case. */
17849 info_ptr->ehrd_offset = info_ptr->altivec_save_offset;
17851 else
17852 info_ptr->ehrd_offset = info_ptr->cr_save_offset;
17853 info_ptr->ehrd_offset -= ehrd_size;
17854 info_ptr->lr_save_offset = reg_size;
17855 break;
17858 save_align = (TARGET_ALTIVEC_ABI || DEFAULT_ABI == ABI_DARWIN) ? 16 : 8;
17859 info_ptr->save_size = RS6000_ALIGN (info_ptr->fp_size
17860 + info_ptr->gp_size
17861 + info_ptr->altivec_size
17862 + info_ptr->altivec_padding_size
17863 + info_ptr->spe_gp_size
17864 + info_ptr->spe_padding_size
17865 + ehrd_size
17866 + info_ptr->cr_size
17867 + info_ptr->vrsave_size,
17868 save_align);
17870 non_fixed_size = (info_ptr->vars_size
17871 + info_ptr->parm_size
17872 + info_ptr->save_size);
17874 info_ptr->total_size = RS6000_ALIGN (non_fixed_size + info_ptr->fixed_size,
17875 ABI_STACK_BOUNDARY / BITS_PER_UNIT);
17877 /* Determine if we need to save the link register. */
17878 if (info_ptr->calls_p
17879 || (DEFAULT_ABI == ABI_AIX
17880 && crtl->profile
17881 && !TARGET_PROFILE_KERNEL)
17882 || (DEFAULT_ABI == ABI_V4 && cfun->calls_alloca)
17883 #ifdef TARGET_RELOCATABLE
17884 || (TARGET_RELOCATABLE && (get_pool_size () != 0))
17885 #endif
17886 || rs6000_ra_ever_killed ())
17887 info_ptr->lr_save_p = 1;
17889 using_static_chain_p = (cfun->static_chain_decl != NULL_TREE
17890 && df_regs_ever_live_p (STATIC_CHAIN_REGNUM)
17891 && call_used_regs[STATIC_CHAIN_REGNUM]);
17892 info_ptr->savres_strategy = rs6000_savres_strategy (info_ptr,
17893 using_static_chain_p);
17895 if (!(info_ptr->savres_strategy & SAVE_INLINE_GPRS)
17896 || !(info_ptr->savres_strategy & SAVE_INLINE_FPRS)
17897 || !(info_ptr->savres_strategy & SAVE_INLINE_VRS)
17898 || !(info_ptr->savres_strategy & REST_INLINE_GPRS)
17899 || !(info_ptr->savres_strategy & REST_INLINE_FPRS)
17900 || !(info_ptr->savres_strategy & REST_INLINE_VRS))
17901 info_ptr->lr_save_p = 1;
17903 if (info_ptr->lr_save_p)
17904 df_set_regs_ever_live (LR_REGNO, true);
17906 /* Determine if we need to allocate any stack frame:
17908 For AIX we need to push the stack if a frame pointer is needed
17909 (because the stack might be dynamically adjusted), if we are
17910 debugging, if we make calls, or if the sum of fp_save, gp_save,
17911 and local variables are more than the space needed to save all
17912 non-volatile registers: 32-bit: 18*8 + 19*4 = 220 or 64-bit: 18*8
17913 + 18*8 = 288 (GPR13 reserved).
17915 For V.4 we don't have the stack cushion that AIX uses, but assume
17916 that the debugger can handle stackless frames. */
17918 if (info_ptr->calls_p)
17919 info_ptr->push_p = 1;
17921 else if (DEFAULT_ABI == ABI_V4)
17922 info_ptr->push_p = non_fixed_size != 0;
17924 else if (frame_pointer_needed)
17925 info_ptr->push_p = 1;
17927 else if (TARGET_XCOFF && write_symbols != NO_DEBUG)
17928 info_ptr->push_p = 1;
17930 else
17931 info_ptr->push_p = non_fixed_size > (TARGET_32BIT ? 220 : 288);
17933 /* Zero offsets if we're not saving those registers. */
17934 if (info_ptr->fp_size == 0)
17935 info_ptr->fp_save_offset = 0;
17937 if (info_ptr->gp_size == 0)
17938 info_ptr->gp_save_offset = 0;
17940 if (! TARGET_ALTIVEC_ABI || info_ptr->altivec_size == 0)
17941 info_ptr->altivec_save_offset = 0;
17943 if (! TARGET_ALTIVEC_ABI || info_ptr->vrsave_mask == 0)
17944 info_ptr->vrsave_save_offset = 0;
17946 if (! TARGET_SPE_ABI
17947 || info_ptr->spe_64bit_regs_used == 0
17948 || info_ptr->spe_gp_size == 0)
17949 info_ptr->spe_gp_save_offset = 0;
17951 if (! info_ptr->lr_save_p)
17952 info_ptr->lr_save_offset = 0;
17954 if (! info_ptr->cr_save_p)
17955 info_ptr->cr_save_offset = 0;
17957 return info_ptr;
17960 /* Return true if the current function uses any GPRs in 64-bit SIMD
17961 mode. */
17963 static bool
17964 spe_func_has_64bit_regs_p (void)
17966 rtx insns, insn;
17968 /* Functions that save and restore all the call-saved registers will
17969 need to save/restore the registers in 64-bits. */
17970 if (crtl->calls_eh_return
17971 || cfun->calls_setjmp
17972 || crtl->has_nonlocal_goto)
17973 return true;
17975 insns = get_insns ();
17977 for (insn = NEXT_INSN (insns); insn != NULL_RTX; insn = NEXT_INSN (insn))
17979 if (INSN_P (insn))
17981 rtx i;
17983 /* FIXME: This should be implemented with attributes...
17985 (set_attr "spe64" "true")....then,
17986 if (get_spe64(insn)) return true;
17988 It's the only reliable way to do the stuff below. */
17990 i = PATTERN (insn);
17991 if (GET_CODE (i) == SET)
17993 enum machine_mode mode = GET_MODE (SET_SRC (i));
17995 if (SPE_VECTOR_MODE (mode))
17996 return true;
17997 if (TARGET_E500_DOUBLE && (mode == DFmode || mode == TFmode))
17998 return true;
18003 return false;
18006 static void
18007 debug_stack_info (rs6000_stack_t *info)
18009 const char *abi_string;
18011 if (! info)
18012 info = rs6000_stack_info ();
18014 fprintf (stderr, "\nStack information for function %s:\n",
18015 ((current_function_decl && DECL_NAME (current_function_decl))
18016 ? IDENTIFIER_POINTER (DECL_NAME (current_function_decl))
18017 : "<unknown>"));
18019 switch (info->abi)
18021 default: abi_string = "Unknown"; break;
18022 case ABI_NONE: abi_string = "NONE"; break;
18023 case ABI_AIX: abi_string = "AIX"; break;
18024 case ABI_DARWIN: abi_string = "Darwin"; break;
18025 case ABI_V4: abi_string = "V.4"; break;
18028 fprintf (stderr, "\tABI = %5s\n", abi_string);
18030 if (TARGET_ALTIVEC_ABI)
18031 fprintf (stderr, "\tALTIVEC ABI extensions enabled.\n");
18033 if (TARGET_SPE_ABI)
18034 fprintf (stderr, "\tSPE ABI extensions enabled.\n");
18036 if (info->first_gp_reg_save != 32)
18037 fprintf (stderr, "\tfirst_gp_reg_save = %5d\n", info->first_gp_reg_save);
18039 if (info->first_fp_reg_save != 64)
18040 fprintf (stderr, "\tfirst_fp_reg_save = %5d\n", info->first_fp_reg_save);
18042 if (info->first_altivec_reg_save <= LAST_ALTIVEC_REGNO)
18043 fprintf (stderr, "\tfirst_altivec_reg_save = %5d\n",
18044 info->first_altivec_reg_save);
18046 if (info->lr_save_p)
18047 fprintf (stderr, "\tlr_save_p = %5d\n", info->lr_save_p);
18049 if (info->cr_save_p)
18050 fprintf (stderr, "\tcr_save_p = %5d\n", info->cr_save_p);
18052 if (info->vrsave_mask)
18053 fprintf (stderr, "\tvrsave_mask = 0x%x\n", info->vrsave_mask);
18055 if (info->push_p)
18056 fprintf (stderr, "\tpush_p = %5d\n", info->push_p);
18058 if (info->calls_p)
18059 fprintf (stderr, "\tcalls_p = %5d\n", info->calls_p);
18061 if (info->gp_save_offset)
18062 fprintf (stderr, "\tgp_save_offset = %5d\n", info->gp_save_offset);
18064 if (info->fp_save_offset)
18065 fprintf (stderr, "\tfp_save_offset = %5d\n", info->fp_save_offset);
18067 if (info->altivec_save_offset)
18068 fprintf (stderr, "\taltivec_save_offset = %5d\n",
18069 info->altivec_save_offset);
18071 if (info->spe_gp_save_offset)
18072 fprintf (stderr, "\tspe_gp_save_offset = %5d\n",
18073 info->spe_gp_save_offset);
18075 if (info->vrsave_save_offset)
18076 fprintf (stderr, "\tvrsave_save_offset = %5d\n",
18077 info->vrsave_save_offset);
18079 if (info->lr_save_offset)
18080 fprintf (stderr, "\tlr_save_offset = %5d\n", info->lr_save_offset);
18082 if (info->cr_save_offset)
18083 fprintf (stderr, "\tcr_save_offset = %5d\n", info->cr_save_offset);
18085 if (info->varargs_save_offset)
18086 fprintf (stderr, "\tvarargs_save_offset = %5d\n", info->varargs_save_offset);
18088 if (info->total_size)
18089 fprintf (stderr, "\ttotal_size = "HOST_WIDE_INT_PRINT_DEC"\n",
18090 info->total_size);
18092 if (info->vars_size)
18093 fprintf (stderr, "\tvars_size = "HOST_WIDE_INT_PRINT_DEC"\n",
18094 info->vars_size);
18096 if (info->parm_size)
18097 fprintf (stderr, "\tparm_size = %5d\n", info->parm_size);
18099 if (info->fixed_size)
18100 fprintf (stderr, "\tfixed_size = %5d\n", info->fixed_size);
18102 if (info->gp_size)
18103 fprintf (stderr, "\tgp_size = %5d\n", info->gp_size);
18105 if (info->spe_gp_size)
18106 fprintf (stderr, "\tspe_gp_size = %5d\n", info->spe_gp_size);
18108 if (info->fp_size)
18109 fprintf (stderr, "\tfp_size = %5d\n", info->fp_size);
18111 if (info->altivec_size)
18112 fprintf (stderr, "\taltivec_size = %5d\n", info->altivec_size);
18114 if (info->vrsave_size)
18115 fprintf (stderr, "\tvrsave_size = %5d\n", info->vrsave_size);
18117 if (info->altivec_padding_size)
18118 fprintf (stderr, "\taltivec_padding_size= %5d\n",
18119 info->altivec_padding_size);
18121 if (info->spe_padding_size)
18122 fprintf (stderr, "\tspe_padding_size = %5d\n",
18123 info->spe_padding_size);
18125 if (info->cr_size)
18126 fprintf (stderr, "\tcr_size = %5d\n", info->cr_size);
18128 if (info->save_size)
18129 fprintf (stderr, "\tsave_size = %5d\n", info->save_size);
18131 if (info->reg_size != 4)
18132 fprintf (stderr, "\treg_size = %5d\n", info->reg_size);
18134 fprintf (stderr, "\tsave-strategy = %04x\n", info->savres_strategy);
18136 fprintf (stderr, "\n");
18140 rs6000_return_addr (int count, rtx frame)
18142 /* Currently we don't optimize very well between prolog and body
18143 code and for PIC code the code can be actually quite bad, so
18144 don't try to be too clever here. */
18145 if (count != 0 || (DEFAULT_ABI != ABI_AIX && flag_pic))
18147 cfun->machine->ra_needs_full_frame = 1;
18149 return
18150 gen_rtx_MEM
18151 (Pmode,
18152 memory_address
18153 (Pmode,
18154 plus_constant (Pmode,
18155 copy_to_reg
18156 (gen_rtx_MEM (Pmode,
18157 memory_address (Pmode, frame))),
18158 RETURN_ADDRESS_OFFSET)));
18161 cfun->machine->ra_need_lr = 1;
18162 return get_hard_reg_initial_val (Pmode, LR_REGNO);
18165 /* Say whether a function is a candidate for sibcall handling or not. */
18167 static bool
18168 rs6000_function_ok_for_sibcall (tree decl, tree exp)
18170 tree fntype;
18172 if (decl)
18173 fntype = TREE_TYPE (decl);
18174 else
18175 fntype = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (exp)));
18177 /* We can't do it if the called function has more vector parameters
18178 than the current function; there's nowhere to put the VRsave code. */
18179 if (TARGET_ALTIVEC_ABI
18180 && TARGET_ALTIVEC_VRSAVE
18181 && !(decl && decl == current_function_decl))
18183 function_args_iterator args_iter;
18184 tree type;
18185 int nvreg = 0;
18187 /* Functions with vector parameters are required to have a
18188 prototype, so the argument type info must be available
18189 here. */
18190 FOREACH_FUNCTION_ARGS(fntype, type, args_iter)
18191 if (TREE_CODE (type) == VECTOR_TYPE
18192 && ALTIVEC_OR_VSX_VECTOR_MODE (TYPE_MODE (type)))
18193 nvreg++;
18195 FOREACH_FUNCTION_ARGS(TREE_TYPE (current_function_decl), type, args_iter)
18196 if (TREE_CODE (type) == VECTOR_TYPE
18197 && ALTIVEC_OR_VSX_VECTOR_MODE (TYPE_MODE (type)))
18198 nvreg--;
18200 if (nvreg > 0)
18201 return false;
18204 /* Under the AIX ABI we can't allow calls to non-local functions,
18205 because the callee may have a different TOC pointer to the
18206 caller and there's no way to ensure we restore the TOC when we
18207 return. With the secure-plt SYSV ABI we can't make non-local
18208 calls when -fpic/PIC because the plt call stubs use r30. */
18209 if (DEFAULT_ABI == ABI_DARWIN
18210 || (DEFAULT_ABI == ABI_AIX
18211 && decl
18212 && !DECL_EXTERNAL (decl)
18213 && (*targetm.binds_local_p) (decl))
18214 || (DEFAULT_ABI == ABI_V4
18215 && (!TARGET_SECURE_PLT
18216 || !flag_pic
18217 || (decl
18218 && (*targetm.binds_local_p) (decl)))))
18220 tree attr_list = TYPE_ATTRIBUTES (fntype);
18222 if (!lookup_attribute ("longcall", attr_list)
18223 || lookup_attribute ("shortcall", attr_list))
18224 return true;
18227 return false;
18230 /* NULL if INSN insn is valid within a low-overhead loop.
18231 Otherwise return why doloop cannot be applied.
18232 PowerPC uses the COUNT register for branch on table instructions. */
18234 static const char *
18235 rs6000_invalid_within_doloop (const_rtx insn)
18237 if (CALL_P (insn))
18238 return "Function call in the loop.";
18240 if (JUMP_P (insn)
18241 && (GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC
18242 || GET_CODE (PATTERN (insn)) == ADDR_VEC))
18243 return "Computed branch in the loop.";
18245 return NULL;
18248 static int
18249 rs6000_ra_ever_killed (void)
18251 rtx top;
18252 rtx reg;
18253 rtx insn;
18255 if (cfun->is_thunk)
18256 return 0;
18258 if (cfun->machine->lr_save_state)
18259 return cfun->machine->lr_save_state - 1;
18261 /* regs_ever_live has LR marked as used if any sibcalls are present,
18262 but this should not force saving and restoring in the
18263 pro/epilogue. Likewise, reg_set_between_p thinks a sibcall
18264 clobbers LR, so that is inappropriate. */
18266 /* Also, the prologue can generate a store into LR that
18267 doesn't really count, like this:
18269 move LR->R0
18270 bcl to set PIC register
18271 move LR->R31
18272 move R0->LR
18274 When we're called from the epilogue, we need to avoid counting
18275 this as a store. */
18277 push_topmost_sequence ();
18278 top = get_insns ();
18279 pop_topmost_sequence ();
18280 reg = gen_rtx_REG (Pmode, LR_REGNO);
18282 for (insn = NEXT_INSN (top); insn != NULL_RTX; insn = NEXT_INSN (insn))
18284 if (INSN_P (insn))
18286 if (CALL_P (insn))
18288 if (!SIBLING_CALL_P (insn))
18289 return 1;
18291 else if (find_regno_note (insn, REG_INC, LR_REGNO))
18292 return 1;
18293 else if (set_of (reg, insn) != NULL_RTX
18294 && !prologue_epilogue_contains (insn))
18295 return 1;
18298 return 0;
18301 /* Emit instructions needed to load the TOC register.
18302 This is only needed when TARGET_TOC, TARGET_MINIMAL_TOC, and there is
18303 a constant pool; or for SVR4 -fpic. */
18305 void
18306 rs6000_emit_load_toc_table (int fromprolog)
18308 rtx dest;
18309 dest = gen_rtx_REG (Pmode, RS6000_PIC_OFFSET_TABLE_REGNUM);
18311 if (TARGET_ELF && TARGET_SECURE_PLT && DEFAULT_ABI != ABI_AIX && flag_pic)
18313 char buf[30];
18314 rtx lab, tmp1, tmp2, got;
18316 lab = gen_label_rtx ();
18317 ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (lab));
18318 lab = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
18319 if (flag_pic == 2)
18320 got = gen_rtx_SYMBOL_REF (Pmode, toc_label_name);
18321 else
18322 got = rs6000_got_sym ();
18323 tmp1 = tmp2 = dest;
18324 if (!fromprolog)
18326 tmp1 = gen_reg_rtx (Pmode);
18327 tmp2 = gen_reg_rtx (Pmode);
18329 emit_insn (gen_load_toc_v4_PIC_1 (lab));
18330 emit_move_insn (tmp1, gen_rtx_REG (Pmode, LR_REGNO));
18331 emit_insn (gen_load_toc_v4_PIC_3b (tmp2, tmp1, got, lab));
18332 emit_insn (gen_load_toc_v4_PIC_3c (dest, tmp2, got, lab));
18334 else if (TARGET_ELF && DEFAULT_ABI == ABI_V4 && flag_pic == 1)
18336 emit_insn (gen_load_toc_v4_pic_si ());
18337 emit_move_insn (dest, gen_rtx_REG (Pmode, LR_REGNO));
18339 else if (TARGET_ELF && DEFAULT_ABI != ABI_AIX && flag_pic == 2)
18341 char buf[30];
18342 rtx temp0 = (fromprolog
18343 ? gen_rtx_REG (Pmode, 0)
18344 : gen_reg_rtx (Pmode));
18346 if (fromprolog)
18348 rtx symF, symL;
18350 ASM_GENERATE_INTERNAL_LABEL (buf, "LCF", rs6000_pic_labelno);
18351 symF = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
18353 ASM_GENERATE_INTERNAL_LABEL (buf, "LCL", rs6000_pic_labelno);
18354 symL = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
18356 emit_insn (gen_load_toc_v4_PIC_1 (symF));
18357 emit_move_insn (dest, gen_rtx_REG (Pmode, LR_REGNO));
18358 emit_insn (gen_load_toc_v4_PIC_2 (temp0, dest, symL, symF));
18360 else
18362 rtx tocsym, lab;
18364 tocsym = gen_rtx_SYMBOL_REF (Pmode, toc_label_name);
18365 lab = gen_label_rtx ();
18366 emit_insn (gen_load_toc_v4_PIC_1b (tocsym, lab));
18367 emit_move_insn (dest, gen_rtx_REG (Pmode, LR_REGNO));
18368 if (TARGET_LINK_STACK)
18369 emit_insn (gen_addsi3 (dest, dest, GEN_INT (4)));
18370 emit_move_insn (temp0, gen_rtx_MEM (Pmode, dest));
18372 emit_insn (gen_addsi3 (dest, temp0, dest));
18374 else if (TARGET_ELF && !TARGET_AIX && flag_pic == 0 && TARGET_MINIMAL_TOC)
18376 /* This is for AIX code running in non-PIC ELF32. */
18377 char buf[30];
18378 rtx realsym;
18379 ASM_GENERATE_INTERNAL_LABEL (buf, "LCTOC", 1);
18380 realsym = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
18382 emit_insn (gen_elf_high (dest, realsym));
18383 emit_insn (gen_elf_low (dest, dest, realsym));
18385 else
18387 gcc_assert (DEFAULT_ABI == ABI_AIX);
18389 if (TARGET_32BIT)
18390 emit_insn (gen_load_toc_aix_si (dest));
18391 else
18392 emit_insn (gen_load_toc_aix_di (dest));
18396 /* Emit instructions to restore the link register after determining where
18397 its value has been stored. */
18399 void
18400 rs6000_emit_eh_reg_restore (rtx source, rtx scratch)
18402 rs6000_stack_t *info = rs6000_stack_info ();
18403 rtx operands[2];
18405 operands[0] = source;
18406 operands[1] = scratch;
18408 if (info->lr_save_p)
18410 rtx frame_rtx = stack_pointer_rtx;
18411 HOST_WIDE_INT sp_offset = 0;
18412 rtx tmp;
18414 if (frame_pointer_needed
18415 || cfun->calls_alloca
18416 || info->total_size > 32767)
18418 tmp = gen_frame_mem (Pmode, frame_rtx);
18419 emit_move_insn (operands[1], tmp);
18420 frame_rtx = operands[1];
18422 else if (info->push_p)
18423 sp_offset = info->total_size;
18425 tmp = plus_constant (Pmode, frame_rtx,
18426 info->lr_save_offset + sp_offset);
18427 tmp = gen_frame_mem (Pmode, tmp);
18428 emit_move_insn (tmp, operands[0]);
18430 else
18431 emit_move_insn (gen_rtx_REG (Pmode, LR_REGNO), operands[0]);
18433 /* Freeze lr_save_p. We've just emitted rtl that depends on the
18434 state of lr_save_p so any change from here on would be a bug. In
18435 particular, stop rs6000_ra_ever_killed from considering the SET
18436 of lr we may have added just above. */
18437 cfun->machine->lr_save_state = info->lr_save_p + 1;
18440 static GTY(()) alias_set_type set = -1;
18442 alias_set_type
18443 get_TOC_alias_set (void)
18445 if (set == -1)
18446 set = new_alias_set ();
18447 return set;
18450 /* This returns nonzero if the current function uses the TOC. This is
18451 determined by the presence of (use (unspec ... UNSPEC_TOC)), which
18452 is generated by the ABI_V4 load_toc_* patterns. */
18453 #if TARGET_ELF
18454 static int
18455 uses_TOC (void)
18457 rtx insn;
18459 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
18460 if (INSN_P (insn))
18462 rtx pat = PATTERN (insn);
18463 int i;
18465 if (GET_CODE (pat) == PARALLEL)
18466 for (i = 0; i < XVECLEN (pat, 0); i++)
18468 rtx sub = XVECEXP (pat, 0, i);
18469 if (GET_CODE (sub) == USE)
18471 sub = XEXP (sub, 0);
18472 if (GET_CODE (sub) == UNSPEC
18473 && XINT (sub, 1) == UNSPEC_TOC)
18474 return 1;
18478 return 0;
18480 #endif
18483 create_TOC_reference (rtx symbol, rtx largetoc_reg)
18485 rtx tocrel, tocreg, hi;
18487 if (TARGET_DEBUG_ADDR)
18489 if (GET_CODE (symbol) == SYMBOL_REF)
18490 fprintf (stderr, "\ncreate_TOC_reference, (symbol_ref %s)\n",
18491 XSTR (symbol, 0));
18492 else
18494 fprintf (stderr, "\ncreate_TOC_reference, code %s:\n",
18495 GET_RTX_NAME (GET_CODE (symbol)));
18496 debug_rtx (symbol);
18500 if (!can_create_pseudo_p ())
18501 df_set_regs_ever_live (TOC_REGISTER, true);
18503 tocreg = gen_rtx_REG (Pmode, TOC_REGISTER);
18504 tocrel = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, symbol, tocreg), UNSPEC_TOCREL);
18505 if (TARGET_CMODEL == CMODEL_SMALL || can_create_pseudo_p ())
18506 return tocrel;
18508 hi = gen_rtx_HIGH (Pmode, copy_rtx (tocrel));
18509 if (largetoc_reg != NULL)
18511 emit_move_insn (largetoc_reg, hi);
18512 hi = largetoc_reg;
18514 return gen_rtx_LO_SUM (Pmode, hi, tocrel);
18517 /* Issue assembly directives that create a reference to the given DWARF
18518 FRAME_TABLE_LABEL from the current function section. */
18519 void
18520 rs6000_aix_asm_output_dwarf_table_ref (char * frame_table_label)
18522 fprintf (asm_out_file, "\t.ref %s\n",
18523 (* targetm.strip_name_encoding) (frame_table_label));
18526 /* This ties together stack memory (MEM with an alias set of frame_alias_set)
18527 and the change to the stack pointer. */
18529 static void
18530 rs6000_emit_stack_tie (rtx fp, bool hard_frame_needed)
18532 rtvec p;
18533 int i;
18534 rtx regs[3];
18536 i = 0;
18537 regs[i++] = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
18538 if (hard_frame_needed)
18539 regs[i++] = gen_rtx_REG (Pmode, HARD_FRAME_POINTER_REGNUM);
18540 if (!(REGNO (fp) == STACK_POINTER_REGNUM
18541 || (hard_frame_needed
18542 && REGNO (fp) == HARD_FRAME_POINTER_REGNUM)))
18543 regs[i++] = fp;
18545 p = rtvec_alloc (i);
18546 while (--i >= 0)
18548 rtx mem = gen_frame_mem (BLKmode, regs[i]);
18549 RTVEC_ELT (p, i) = gen_rtx_SET (VOIDmode, mem, const0_rtx);
18552 emit_insn (gen_stack_tie (gen_rtx_PARALLEL (VOIDmode, p)));
18555 /* Emit the correct code for allocating stack space, as insns.
18556 If COPY_REG, make sure a copy of the old frame is left there.
18557 The generated code may use hard register 0 as a temporary. */
18559 static void
18560 rs6000_emit_allocate_stack (HOST_WIDE_INT size, rtx copy_reg, int copy_off)
18562 rtx insn;
18563 rtx stack_reg = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
18564 rtx tmp_reg = gen_rtx_REG (Pmode, 0);
18565 rtx todec = gen_int_mode (-size, Pmode);
18566 rtx par, set, mem;
18568 if (INTVAL (todec) != -size)
18570 warning (0, "stack frame too large");
18571 emit_insn (gen_trap ());
18572 return;
18575 if (crtl->limit_stack)
18577 if (REG_P (stack_limit_rtx)
18578 && REGNO (stack_limit_rtx) > 1
18579 && REGNO (stack_limit_rtx) <= 31)
18581 emit_insn (gen_add3_insn (tmp_reg, stack_limit_rtx, GEN_INT (size)));
18582 emit_insn (gen_cond_trap (LTU, stack_reg, tmp_reg,
18583 const0_rtx));
18585 else if (GET_CODE (stack_limit_rtx) == SYMBOL_REF
18586 && TARGET_32BIT
18587 && DEFAULT_ABI == ABI_V4)
18589 rtx toload = gen_rtx_CONST (VOIDmode,
18590 gen_rtx_PLUS (Pmode,
18591 stack_limit_rtx,
18592 GEN_INT (size)));
18594 emit_insn (gen_elf_high (tmp_reg, toload));
18595 emit_insn (gen_elf_low (tmp_reg, tmp_reg, toload));
18596 emit_insn (gen_cond_trap (LTU, stack_reg, tmp_reg,
18597 const0_rtx));
18599 else
18600 warning (0, "stack limit expression is not supported");
18603 if (copy_reg)
18605 if (copy_off != 0)
18606 emit_insn (gen_add3_insn (copy_reg, stack_reg, GEN_INT (copy_off)));
18607 else
18608 emit_move_insn (copy_reg, stack_reg);
18611 if (size > 32767)
18613 /* Need a note here so that try_split doesn't get confused. */
18614 if (get_last_insn () == NULL_RTX)
18615 emit_note (NOTE_INSN_DELETED);
18616 insn = emit_move_insn (tmp_reg, todec);
18617 try_split (PATTERN (insn), insn, 0);
18618 todec = tmp_reg;
18621 insn = emit_insn (TARGET_32BIT
18622 ? gen_movsi_update_stack (stack_reg, stack_reg,
18623 todec, stack_reg)
18624 : gen_movdi_di_update_stack (stack_reg, stack_reg,
18625 todec, stack_reg));
18626 /* Since we didn't use gen_frame_mem to generate the MEM, grab
18627 it now and set the alias set/attributes. The above gen_*_update
18628 calls will generate a PARALLEL with the MEM set being the first
18629 operation. */
18630 par = PATTERN (insn);
18631 gcc_assert (GET_CODE (par) == PARALLEL);
18632 set = XVECEXP (par, 0, 0);
18633 gcc_assert (GET_CODE (set) == SET);
18634 mem = SET_DEST (set);
18635 gcc_assert (MEM_P (mem));
18636 MEM_NOTRAP_P (mem) = 1;
18637 set_mem_alias_set (mem, get_frame_alias_set ());
18639 RTX_FRAME_RELATED_P (insn) = 1;
18640 add_reg_note (insn, REG_FRAME_RELATED_EXPR,
18641 gen_rtx_SET (VOIDmode, stack_reg,
18642 gen_rtx_PLUS (Pmode, stack_reg,
18643 GEN_INT (-size))));
18646 #define PROBE_INTERVAL (1 << STACK_CHECK_PROBE_INTERVAL_EXP)
18648 #if PROBE_INTERVAL > 32768
18649 #error Cannot use indexed addressing mode for stack probing
18650 #endif
18652 /* Emit code to probe a range of stack addresses from FIRST to FIRST+SIZE,
18653 inclusive. These are offsets from the current stack pointer. */
18655 static void
18656 rs6000_emit_probe_stack_range (HOST_WIDE_INT first, HOST_WIDE_INT size)
18658 /* See if we have a constant small number of probes to generate. If so,
18659 that's the easy case. */
18660 if (first + size <= 32768)
18662 HOST_WIDE_INT i;
18664 /* Probe at FIRST + N * PROBE_INTERVAL for values of N from 1 until
18665 it exceeds SIZE. If only one probe is needed, this will not
18666 generate any code. Then probe at FIRST + SIZE. */
18667 for (i = PROBE_INTERVAL; i < size; i += PROBE_INTERVAL)
18668 emit_stack_probe (plus_constant (Pmode, stack_pointer_rtx,
18669 -(first + i)));
18671 emit_stack_probe (plus_constant (Pmode, stack_pointer_rtx,
18672 -(first + size)));
18675 /* Otherwise, do the same as above, but in a loop. Note that we must be
18676 extra careful with variables wrapping around because we might be at
18677 the very top (or the very bottom) of the address space and we have
18678 to be able to handle this case properly; in particular, we use an
18679 equality test for the loop condition. */
18680 else
18682 HOST_WIDE_INT rounded_size;
18683 rtx r12 = gen_rtx_REG (Pmode, 12);
18684 rtx r0 = gen_rtx_REG (Pmode, 0);
18686 /* Sanity check for the addressing mode we're going to use. */
18687 gcc_assert (first <= 32768);
18689 /* Step 1: round SIZE to the previous multiple of the interval. */
18691 rounded_size = size & -PROBE_INTERVAL;
18694 /* Step 2: compute initial and final value of the loop counter. */
18696 /* TEST_ADDR = SP + FIRST. */
18697 emit_insn (gen_rtx_SET (VOIDmode, r12,
18698 plus_constant (Pmode, stack_pointer_rtx,
18699 -first)));
18701 /* LAST_ADDR = SP + FIRST + ROUNDED_SIZE. */
18702 if (rounded_size > 32768)
18704 emit_move_insn (r0, GEN_INT (-rounded_size));
18705 emit_insn (gen_rtx_SET (VOIDmode, r0,
18706 gen_rtx_PLUS (Pmode, r12, r0)));
18708 else
18709 emit_insn (gen_rtx_SET (VOIDmode, r0,
18710 plus_constant (Pmode, r12, -rounded_size)));
18713 /* Step 3: the loop
18715 while (TEST_ADDR != LAST_ADDR)
18717 TEST_ADDR = TEST_ADDR + PROBE_INTERVAL
18718 probe at TEST_ADDR
18721 probes at FIRST + N * PROBE_INTERVAL for values of N from 1
18722 until it is equal to ROUNDED_SIZE. */
18724 if (TARGET_64BIT)
18725 emit_insn (gen_probe_stack_rangedi (r12, r12, r0));
18726 else
18727 emit_insn (gen_probe_stack_rangesi (r12, r12, r0));
18730 /* Step 4: probe at FIRST + SIZE if we cannot assert at compile-time
18731 that SIZE is equal to ROUNDED_SIZE. */
18733 if (size != rounded_size)
18734 emit_stack_probe (plus_constant (Pmode, r12, rounded_size - size));
18738 /* Probe a range of stack addresses from REG1 to REG2 inclusive. These are
18739 absolute addresses. */
18741 const char *
18742 output_probe_stack_range (rtx reg1, rtx reg2)
18744 static int labelno = 0;
18745 char loop_lab[32], end_lab[32];
18746 rtx xops[2];
18748 ASM_GENERATE_INTERNAL_LABEL (loop_lab, "LPSRL", labelno);
18749 ASM_GENERATE_INTERNAL_LABEL (end_lab, "LPSRE", labelno++);
18751 ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, loop_lab);
18753 /* Jump to END_LAB if TEST_ADDR == LAST_ADDR. */
18754 xops[0] = reg1;
18755 xops[1] = reg2;
18756 if (TARGET_64BIT)
18757 output_asm_insn ("{cmp|cmpd} 0,%0,%1", xops);
18758 else
18759 output_asm_insn ("{cmp|cmpw} 0,%0,%1", xops);
18761 fputs ("\tbeq 0,", asm_out_file);
18762 assemble_name_raw (asm_out_file, end_lab);
18763 fputc ('\n', asm_out_file);
18765 /* TEST_ADDR = TEST_ADDR + PROBE_INTERVAL. */
18766 xops[1] = GEN_INT (-PROBE_INTERVAL);
18767 output_asm_insn ("{cal %0,%1(%0)|addi %0,%0,%1}", xops);
18769 /* Probe at TEST_ADDR and branch. */
18770 xops[1] = gen_rtx_REG (Pmode, 0);
18771 output_asm_insn ("{st|stw} %1,0(%0)", xops);
18772 fprintf (asm_out_file, "\tb ");
18773 assemble_name_raw (asm_out_file, loop_lab);
18774 fputc ('\n', asm_out_file);
18776 ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, end_lab);
18778 return "";
18781 /* Add to 'insn' a note which is PATTERN (INSN) but with REG replaced
18782 with (plus:P (reg 1) VAL), and with REG2 replaced with RREG if REG2
18783 is not NULL. It would be nice if dwarf2out_frame_debug_expr could
18784 deduce these equivalences by itself so it wasn't necessary to hold
18785 its hand so much. Don't be tempted to always supply d2_f_d_e with
18786 the actual cfa register, ie. r31 when we are using a hard frame
18787 pointer. That fails when saving regs off r1, and sched moves the
18788 r31 setup past the reg saves. */
18790 static rtx
18791 rs6000_frame_related (rtx insn, rtx reg, HOST_WIDE_INT val,
18792 rtx reg2, rtx rreg)
18794 rtx real, temp;
18796 if (REGNO (reg) == STACK_POINTER_REGNUM && reg2 == NULL_RTX)
18798 /* No need for any replacement. Just set RTX_FRAME_RELATED_P. */
18799 int i;
18801 gcc_checking_assert (val == 0);
18802 real = PATTERN (insn);
18803 if (GET_CODE (real) == PARALLEL)
18804 for (i = 0; i < XVECLEN (real, 0); i++)
18805 if (GET_CODE (XVECEXP (real, 0, i)) == SET)
18807 rtx set = XVECEXP (real, 0, i);
18809 RTX_FRAME_RELATED_P (set) = 1;
18811 RTX_FRAME_RELATED_P (insn) = 1;
18812 return insn;
18815 /* copy_rtx will not make unique copies of registers, so we need to
18816 ensure we don't have unwanted sharing here. */
18817 if (reg == reg2)
18818 reg = gen_raw_REG (GET_MODE (reg), REGNO (reg));
18820 if (reg == rreg)
18821 reg = gen_raw_REG (GET_MODE (reg), REGNO (reg));
18823 real = copy_rtx (PATTERN (insn));
18825 if (reg2 != NULL_RTX)
18826 real = replace_rtx (real, reg2, rreg);
18828 if (REGNO (reg) == STACK_POINTER_REGNUM)
18829 gcc_checking_assert (val == 0);
18830 else
18831 real = replace_rtx (real, reg,
18832 gen_rtx_PLUS (Pmode, gen_rtx_REG (Pmode,
18833 STACK_POINTER_REGNUM),
18834 GEN_INT (val)));
18836 /* We expect that 'real' is either a SET or a PARALLEL containing
18837 SETs (and possibly other stuff). In a PARALLEL, all the SETs
18838 are important so they all have to be marked RTX_FRAME_RELATED_P. */
18840 if (GET_CODE (real) == SET)
18842 rtx set = real;
18844 temp = simplify_rtx (SET_SRC (set));
18845 if (temp)
18846 SET_SRC (set) = temp;
18847 temp = simplify_rtx (SET_DEST (set));
18848 if (temp)
18849 SET_DEST (set) = temp;
18850 if (GET_CODE (SET_DEST (set)) == MEM)
18852 temp = simplify_rtx (XEXP (SET_DEST (set), 0));
18853 if (temp)
18854 XEXP (SET_DEST (set), 0) = temp;
18857 else
18859 int i;
18861 gcc_assert (GET_CODE (real) == PARALLEL);
18862 for (i = 0; i < XVECLEN (real, 0); i++)
18863 if (GET_CODE (XVECEXP (real, 0, i)) == SET)
18865 rtx set = XVECEXP (real, 0, i);
18867 temp = simplify_rtx (SET_SRC (set));
18868 if (temp)
18869 SET_SRC (set) = temp;
18870 temp = simplify_rtx (SET_DEST (set));
18871 if (temp)
18872 SET_DEST (set) = temp;
18873 if (GET_CODE (SET_DEST (set)) == MEM)
18875 temp = simplify_rtx (XEXP (SET_DEST (set), 0));
18876 if (temp)
18877 XEXP (SET_DEST (set), 0) = temp;
18879 RTX_FRAME_RELATED_P (set) = 1;
18883 RTX_FRAME_RELATED_P (insn) = 1;
18884 add_reg_note (insn, REG_FRAME_RELATED_EXPR, real);
18886 return insn;
18889 /* Returns an insn that has a vrsave set operation with the
18890 appropriate CLOBBERs. */
18892 static rtx
18893 generate_set_vrsave (rtx reg, rs6000_stack_t *info, int epiloguep)
18895 int nclobs, i;
18896 rtx insn, clobs[TOTAL_ALTIVEC_REGS + 1];
18897 rtx vrsave = gen_rtx_REG (SImode, VRSAVE_REGNO);
18899 clobs[0]
18900 = gen_rtx_SET (VOIDmode,
18901 vrsave,
18902 gen_rtx_UNSPEC_VOLATILE (SImode,
18903 gen_rtvec (2, reg, vrsave),
18904 UNSPECV_SET_VRSAVE));
18906 nclobs = 1;
18908 /* We need to clobber the registers in the mask so the scheduler
18909 does not move sets to VRSAVE before sets of AltiVec registers.
18911 However, if the function receives nonlocal gotos, reload will set
18912 all call saved registers live. We will end up with:
18914 (set (reg 999) (mem))
18915 (parallel [ (set (reg vrsave) (unspec blah))
18916 (clobber (reg 999))])
18918 The clobber will cause the store into reg 999 to be dead, and
18919 flow will attempt to delete an epilogue insn. In this case, we
18920 need an unspec use/set of the register. */
18922 for (i = FIRST_ALTIVEC_REGNO; i <= LAST_ALTIVEC_REGNO; ++i)
18923 if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
18925 if (!epiloguep || call_used_regs [i])
18926 clobs[nclobs++] = gen_rtx_CLOBBER (VOIDmode,
18927 gen_rtx_REG (V4SImode, i));
18928 else
18930 rtx reg = gen_rtx_REG (V4SImode, i);
18932 clobs[nclobs++]
18933 = gen_rtx_SET (VOIDmode,
18934 reg,
18935 gen_rtx_UNSPEC (V4SImode,
18936 gen_rtvec (1, reg), 27));
18940 insn = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (nclobs));
18942 for (i = 0; i < nclobs; ++i)
18943 XVECEXP (insn, 0, i) = clobs[i];
18945 return insn;
18948 static rtx
18949 gen_frame_set (rtx reg, rtx frame_reg, int offset, bool store)
18951 rtx addr, mem;
18953 addr = gen_rtx_PLUS (Pmode, frame_reg, GEN_INT (offset));
18954 mem = gen_frame_mem (GET_MODE (reg), addr);
18955 return gen_rtx_SET (VOIDmode, store ? mem : reg, store ? reg : mem);
18958 static rtx
18959 gen_frame_load (rtx reg, rtx frame_reg, int offset)
18961 return gen_frame_set (reg, frame_reg, offset, false);
18964 static rtx
18965 gen_frame_store (rtx reg, rtx frame_reg, int offset)
18967 return gen_frame_set (reg, frame_reg, offset, true);
18970 /* Save a register into the frame, and emit RTX_FRAME_RELATED_P notes.
18971 Save REGNO into [FRAME_REG + OFFSET] in mode MODE. */
18973 static rtx
18974 emit_frame_save (rtx frame_reg, enum machine_mode mode,
18975 unsigned int regno, int offset, HOST_WIDE_INT frame_reg_to_sp)
18977 rtx reg, insn;
18979 /* Some cases that need register indexed addressing. */
18980 gcc_checking_assert (!((TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (mode))
18981 || (TARGET_VSX && ALTIVEC_OR_VSX_VECTOR_MODE (mode))
18982 || (TARGET_E500_DOUBLE && mode == DFmode)
18983 || (TARGET_SPE_ABI
18984 && SPE_VECTOR_MODE (mode)
18985 && !SPE_CONST_OFFSET_OK (offset))));
18987 reg = gen_rtx_REG (mode, regno);
18988 insn = emit_insn (gen_frame_store (reg, frame_reg, offset));
18989 return rs6000_frame_related (insn, frame_reg, frame_reg_to_sp,
18990 NULL_RTX, NULL_RTX);
18993 /* Emit an offset memory reference suitable for a frame store, while
18994 converting to a valid addressing mode. */
18996 static rtx
18997 gen_frame_mem_offset (enum machine_mode mode, rtx reg, int offset)
18999 rtx int_rtx, offset_rtx;
19001 int_rtx = GEN_INT (offset);
19003 if ((TARGET_SPE_ABI && SPE_VECTOR_MODE (mode) && !SPE_CONST_OFFSET_OK (offset))
19004 || (TARGET_E500_DOUBLE && mode == DFmode))
19006 offset_rtx = gen_rtx_REG (Pmode, FIXED_SCRATCH);
19007 emit_move_insn (offset_rtx, int_rtx);
19009 else
19010 offset_rtx = int_rtx;
19012 return gen_frame_mem (mode, gen_rtx_PLUS (Pmode, reg, offset_rtx));
19015 #ifndef TARGET_FIX_AND_CONTINUE
19016 #define TARGET_FIX_AND_CONTINUE 0
19017 #endif
19019 /* It's really GPR 13 or 14, FPR 14 and VR 20. We need the smallest. */
19020 #define FIRST_SAVRES_REGISTER FIRST_SAVED_GP_REGNO
19021 #define LAST_SAVRES_REGISTER 31
19022 #define N_SAVRES_REGISTERS (LAST_SAVRES_REGISTER - FIRST_SAVRES_REGISTER + 1)
19024 enum {
19025 SAVRES_LR = 0x1,
19026 SAVRES_SAVE = 0x2,
19027 SAVRES_REG = 0x0c,
19028 SAVRES_GPR = 0,
19029 SAVRES_FPR = 4,
19030 SAVRES_VR = 8
19033 static GTY(()) rtx savres_routine_syms[N_SAVRES_REGISTERS][12];
19035 /* Temporary holding space for an out-of-line register save/restore
19036 routine name. */
19037 static char savres_routine_name[30];
19039 /* Return the name for an out-of-line register save/restore routine.
19040 We are saving/restoring GPRs if GPR is true. */
19042 static char *
19043 rs6000_savres_routine_name (rs6000_stack_t *info, int regno, int sel)
19045 const char *prefix = "";
19046 const char *suffix = "";
19048 /* Different targets are supposed to define
19049 {SAVE,RESTORE}_FP_{PREFIX,SUFFIX} with the idea that the needed
19050 routine name could be defined with:
19052 sprintf (name, "%s%d%s", SAVE_FP_PREFIX, regno, SAVE_FP_SUFFIX)
19054 This is a nice idea in practice, but in reality, things are
19055 complicated in several ways:
19057 - ELF targets have save/restore routines for GPRs.
19059 - SPE targets use different prefixes for 32/64-bit registers, and
19060 neither of them fit neatly in the FOO_{PREFIX,SUFFIX} regimen.
19062 - PPC64 ELF targets have routines for save/restore of GPRs that
19063 differ in what they do with the link register, so having a set
19064 prefix doesn't work. (We only use one of the save routines at
19065 the moment, though.)
19067 - PPC32 elf targets have "exit" versions of the restore routines
19068 that restore the link register and can save some extra space.
19069 These require an extra suffix. (There are also "tail" versions
19070 of the restore routines and "GOT" versions of the save routines,
19071 but we don't generate those at present. Same problems apply,
19072 though.)
19074 We deal with all this by synthesizing our own prefix/suffix and
19075 using that for the simple sprintf call shown above. */
19076 if (TARGET_SPE)
19078 /* No floating point saves on the SPE. */
19079 gcc_assert ((sel & SAVRES_REG) == SAVRES_GPR);
19081 if ((sel & SAVRES_SAVE))
19082 prefix = info->spe_64bit_regs_used ? "_save64gpr_" : "_save32gpr_";
19083 else
19084 prefix = info->spe_64bit_regs_used ? "_rest64gpr_" : "_rest32gpr_";
19086 if ((sel & SAVRES_LR))
19087 suffix = "_x";
19089 else if (DEFAULT_ABI == ABI_V4)
19091 if (TARGET_64BIT)
19092 goto aix_names;
19094 if ((sel & SAVRES_REG) == SAVRES_GPR)
19095 prefix = (sel & SAVRES_SAVE) ? "_savegpr_" : "_restgpr_";
19096 else if ((sel & SAVRES_REG) == SAVRES_FPR)
19097 prefix = (sel & SAVRES_SAVE) ? "_savefpr_" : "_restfpr_";
19098 else if ((sel & SAVRES_REG) == SAVRES_VR)
19099 prefix = (sel & SAVRES_SAVE) ? "_savevr_" : "_restvr_";
19100 else
19101 abort ();
19103 if ((sel & SAVRES_LR))
19104 suffix = "_x";
19106 else if (DEFAULT_ABI == ABI_AIX)
19108 #if !defined (POWERPC_LINUX) && !defined (POWERPC_FREEBSD)
19109 /* No out-of-line save/restore routines for GPRs on AIX. */
19110 gcc_assert (!TARGET_AIX || (sel & SAVRES_REG) != SAVRES_GPR);
19111 #endif
19113 aix_names:
19114 if ((sel & SAVRES_REG) == SAVRES_GPR)
19115 prefix = ((sel & SAVRES_SAVE)
19116 ? ((sel & SAVRES_LR) ? "_savegpr0_" : "_savegpr1_")
19117 : ((sel & SAVRES_LR) ? "_restgpr0_" : "_restgpr1_"));
19118 else if ((sel & SAVRES_REG) == SAVRES_FPR)
19120 #if defined (POWERPC_LINUX) || defined (POWERPC_FREEBSD)
19121 if ((sel & SAVRES_LR))
19122 prefix = ((sel & SAVRES_SAVE) ? "_savefpr_" : "_restfpr_");
19123 else
19124 #endif
19126 prefix = (sel & SAVRES_SAVE) ? SAVE_FP_PREFIX : RESTORE_FP_PREFIX;
19127 suffix = (sel & SAVRES_SAVE) ? SAVE_FP_SUFFIX : RESTORE_FP_SUFFIX;
19130 else if ((sel & SAVRES_REG) == SAVRES_VR)
19131 prefix = (sel & SAVRES_SAVE) ? "_savevr_" : "_restvr_";
19132 else
19133 abort ();
19136 if (DEFAULT_ABI == ABI_DARWIN)
19138 /* The Darwin approach is (slightly) different, in order to be
19139 compatible with code generated by the system toolchain. There is a
19140 single symbol for the start of save sequence, and the code here
19141 embeds an offset into that code on the basis of the first register
19142 to be saved. */
19143 prefix = (sel & SAVRES_SAVE) ? "save" : "rest" ;
19144 if ((sel & SAVRES_REG) == SAVRES_GPR)
19145 sprintf (savres_routine_name, "*%sGPR%s%s%.0d ; %s r%d-r31", prefix,
19146 ((sel & SAVRES_LR) ? "x" : ""), (regno == 13 ? "" : "+"),
19147 (regno - 13) * 4, prefix, regno);
19148 else if ((sel & SAVRES_REG) == SAVRES_FPR)
19149 sprintf (savres_routine_name, "*%sFP%s%.0d ; %s f%d-f31", prefix,
19150 (regno == 14 ? "" : "+"), (regno - 14) * 4, prefix, regno);
19151 else if ((sel & SAVRES_REG) == SAVRES_VR)
19152 sprintf (savres_routine_name, "*%sVEC%s%.0d ; %s v%d-v31", prefix,
19153 (regno == 20 ? "" : "+"), (regno - 20) * 8, prefix, regno);
19154 else
19155 abort ();
19157 else
19158 sprintf (savres_routine_name, "%s%d%s", prefix, regno, suffix);
19160 return savres_routine_name;
19163 /* Return an RTL SYMBOL_REF for an out-of-line register save/restore routine.
19164 We are saving/restoring GPRs if GPR is true. */
19166 static rtx
19167 rs6000_savres_routine_sym (rs6000_stack_t *info, int sel)
19169 int regno = ((sel & SAVRES_REG) == SAVRES_GPR
19170 ? info->first_gp_reg_save
19171 : (sel & SAVRES_REG) == SAVRES_FPR
19172 ? info->first_fp_reg_save - 32
19173 : (sel & SAVRES_REG) == SAVRES_VR
19174 ? info->first_altivec_reg_save - FIRST_ALTIVEC_REGNO
19175 : -1);
19176 rtx sym;
19177 int select = sel;
19179 /* On the SPE, we never have any FPRs, but we do have 32/64-bit
19180 versions of the gpr routines. */
19181 if (TARGET_SPE_ABI && (sel & SAVRES_REG) == SAVRES_GPR
19182 && info->spe_64bit_regs_used)
19183 select ^= SAVRES_FPR ^ SAVRES_GPR;
19185 /* Don't generate bogus routine names. */
19186 gcc_assert (FIRST_SAVRES_REGISTER <= regno
19187 && regno <= LAST_SAVRES_REGISTER
19188 && select >= 0 && select <= 12);
19190 sym = savres_routine_syms[regno-FIRST_SAVRES_REGISTER][select];
19192 if (sym == NULL)
19194 char *name;
19196 name = rs6000_savres_routine_name (info, regno, sel);
19198 sym = savres_routine_syms[regno-FIRST_SAVRES_REGISTER][select]
19199 = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (name));
19200 SYMBOL_REF_FLAGS (sym) |= SYMBOL_FLAG_FUNCTION;
19203 return sym;
19206 /* Emit a sequence of insns, including a stack tie if needed, for
19207 resetting the stack pointer. If UPDT_REGNO is not 1, then don't
19208 reset the stack pointer, but move the base of the frame into
19209 reg UPDT_REGNO for use by out-of-line register restore routines. */
19211 static rtx
19212 rs6000_emit_stack_reset (rs6000_stack_t *info,
19213 rtx frame_reg_rtx, HOST_WIDE_INT frame_off,
19214 unsigned updt_regno)
19216 rtx updt_reg_rtx;
19218 /* This blockage is needed so that sched doesn't decide to move
19219 the sp change before the register restores. */
19220 if (DEFAULT_ABI == ABI_V4
19221 || (TARGET_SPE_ABI
19222 && info->spe_64bit_regs_used != 0
19223 && info->first_gp_reg_save != 32))
19224 rs6000_emit_stack_tie (frame_reg_rtx, frame_pointer_needed);
19226 /* If we are restoring registers out-of-line, we will be using the
19227 "exit" variants of the restore routines, which will reset the
19228 stack for us. But we do need to point updt_reg into the
19229 right place for those routines. */
19230 updt_reg_rtx = gen_rtx_REG (Pmode, updt_regno);
19232 if (frame_off != 0)
19233 return emit_insn (gen_add3_insn (updt_reg_rtx,
19234 frame_reg_rtx, GEN_INT (frame_off)));
19235 else if (REGNO (frame_reg_rtx) != updt_regno)
19236 return emit_move_insn (updt_reg_rtx, frame_reg_rtx);
19238 return NULL_RTX;
19241 /* Return the register number used as a pointer by out-of-line
19242 save/restore functions. */
19244 static inline unsigned
19245 ptr_regno_for_savres (int sel)
19247 if (DEFAULT_ABI == ABI_AIX)
19248 return (sel & SAVRES_REG) == SAVRES_FPR || (sel & SAVRES_LR) ? 1 : 12;
19249 return DEFAULT_ABI == ABI_DARWIN && (sel & SAVRES_REG) == SAVRES_FPR ? 1 : 11;
19252 /* Construct a parallel rtx describing the effect of a call to an
19253 out-of-line register save/restore routine, and emit the insn
19254 or jump_insn as appropriate. */
19256 static rtx
19257 rs6000_emit_savres_rtx (rs6000_stack_t *info,
19258 rtx frame_reg_rtx, int save_area_offset, int lr_offset,
19259 enum machine_mode reg_mode, int sel)
19261 int i;
19262 int offset, start_reg, end_reg, n_regs, use_reg;
19263 int reg_size = GET_MODE_SIZE (reg_mode);
19264 rtx sym;
19265 rtvec p;
19266 rtx par, insn;
19268 offset = 0;
19269 start_reg = ((sel & SAVRES_REG) == SAVRES_GPR
19270 ? info->first_gp_reg_save
19271 : (sel & SAVRES_REG) == SAVRES_FPR
19272 ? info->first_fp_reg_save
19273 : (sel & SAVRES_REG) == SAVRES_VR
19274 ? info->first_altivec_reg_save
19275 : -1);
19276 end_reg = ((sel & SAVRES_REG) == SAVRES_GPR
19277 ? 32
19278 : (sel & SAVRES_REG) == SAVRES_FPR
19279 ? 64
19280 : (sel & SAVRES_REG) == SAVRES_VR
19281 ? LAST_ALTIVEC_REGNO + 1
19282 : -1);
19283 n_regs = end_reg - start_reg;
19284 p = rtvec_alloc (3 + ((sel & SAVRES_LR) ? 1 : 0)
19285 + ((sel & SAVRES_REG) == SAVRES_VR ? 1 : 0)
19286 + n_regs);
19288 if (!(sel & SAVRES_SAVE) && (sel & SAVRES_LR))
19289 RTVEC_ELT (p, offset++) = ret_rtx;
19291 RTVEC_ELT (p, offset++)
19292 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, LR_REGNO));
19294 sym = rs6000_savres_routine_sym (info, sel);
19295 RTVEC_ELT (p, offset++) = gen_rtx_USE (VOIDmode, sym);
19297 use_reg = ptr_regno_for_savres (sel);
19298 if ((sel & SAVRES_REG) == SAVRES_VR)
19300 /* Vector regs are saved/restored using [reg+reg] addressing. */
19301 RTVEC_ELT (p, offset++)
19302 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, use_reg));
19303 RTVEC_ELT (p, offset++)
19304 = gen_rtx_USE (VOIDmode, gen_rtx_REG (Pmode, 0));
19306 else
19307 RTVEC_ELT (p, offset++)
19308 = gen_rtx_USE (VOIDmode, gen_rtx_REG (Pmode, use_reg));
19310 for (i = 0; i < end_reg - start_reg; i++)
19311 RTVEC_ELT (p, i + offset)
19312 = gen_frame_set (gen_rtx_REG (reg_mode, start_reg + i),
19313 frame_reg_rtx, save_area_offset + reg_size * i,
19314 (sel & SAVRES_SAVE) != 0);
19316 if ((sel & SAVRES_SAVE) && (sel & SAVRES_LR))
19317 RTVEC_ELT (p, i + offset)
19318 = gen_frame_store (gen_rtx_REG (Pmode, 0), frame_reg_rtx, lr_offset);
19320 par = gen_rtx_PARALLEL (VOIDmode, p);
19322 if (!(sel & SAVRES_SAVE) && (sel & SAVRES_LR))
19324 insn = emit_jump_insn (par);
19325 JUMP_LABEL (insn) = ret_rtx;
19327 else
19328 insn = emit_insn (par);
19329 return insn;
19332 /* Determine whether the gp REG is really used. */
19334 static bool
19335 rs6000_reg_live_or_pic_offset_p (int reg)
19337 /* If the function calls eh_return, claim used all the registers that would
19338 be checked for liveness otherwise. This is required for the PIC offset
19339 register with -mminimal-toc on AIX, as it is advertised as "fixed" for
19340 register allocation purposes in this case. */
19342 return (((crtl->calls_eh_return || df_regs_ever_live_p (reg))
19343 && (!call_used_regs[reg]
19344 || (reg == RS6000_PIC_OFFSET_TABLE_REGNUM
19345 && !TARGET_SINGLE_PIC_BASE
19346 && TARGET_TOC && TARGET_MINIMAL_TOC)))
19347 || (reg == RS6000_PIC_OFFSET_TABLE_REGNUM
19348 && !TARGET_SINGLE_PIC_BASE
19349 && ((DEFAULT_ABI == ABI_V4 && flag_pic != 0)
19350 || (DEFAULT_ABI == ABI_DARWIN && flag_pic))));
19353 /* Emit function prologue as insns. */
19355 void
19356 rs6000_emit_prologue (void)
19358 rs6000_stack_t *info = rs6000_stack_info ();
19359 enum machine_mode reg_mode = Pmode;
19360 int reg_size = TARGET_32BIT ? 4 : 8;
19361 rtx sp_reg_rtx = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
19362 rtx frame_reg_rtx = sp_reg_rtx;
19363 unsigned int cr_save_regno;
19364 rtx cr_save_rtx = NULL_RTX;
19365 rtx insn;
19366 int strategy;
19367 int using_static_chain_p = (cfun->static_chain_decl != NULL_TREE
19368 && df_regs_ever_live_p (STATIC_CHAIN_REGNUM)
19369 && call_used_regs[STATIC_CHAIN_REGNUM]);
19370 /* Offset to top of frame for frame_reg and sp respectively. */
19371 HOST_WIDE_INT frame_off = 0;
19372 HOST_WIDE_INT sp_off = 0;
19374 #ifdef ENABLE_CHECKING
19375 /* Track and check usage of r0, r11, r12. */
19376 int reg_inuse = using_static_chain_p ? 1 << 11 : 0;
19377 #define START_USE(R) do \
19379 gcc_assert ((reg_inuse & (1 << (R))) == 0); \
19380 reg_inuse |= 1 << (R); \
19381 } while (0)
19382 #define END_USE(R) do \
19384 gcc_assert ((reg_inuse & (1 << (R))) != 0); \
19385 reg_inuse &= ~(1 << (R)); \
19386 } while (0)
19387 #define NOT_INUSE(R) do \
19389 gcc_assert ((reg_inuse & (1 << (R))) == 0); \
19390 } while (0)
19391 #else
19392 #define START_USE(R) do {} while (0)
19393 #define END_USE(R) do {} while (0)
19394 #define NOT_INUSE(R) do {} while (0)
19395 #endif
19397 if (flag_stack_usage_info)
19398 current_function_static_stack_size = info->total_size;
19400 if (flag_stack_check == STATIC_BUILTIN_STACK_CHECK && info->total_size)
19401 rs6000_emit_probe_stack_range (STACK_CHECK_PROTECT, info->total_size);
19403 if (TARGET_FIX_AND_CONTINUE)
19405 /* gdb on darwin arranges to forward a function from the old
19406 address by modifying the first 5 instructions of the function
19407 to branch to the overriding function. This is necessary to
19408 permit function pointers that point to the old function to
19409 actually forward to the new function. */
19410 emit_insn (gen_nop ());
19411 emit_insn (gen_nop ());
19412 emit_insn (gen_nop ());
19413 emit_insn (gen_nop ());
19414 emit_insn (gen_nop ());
19417 if (TARGET_SPE_ABI && info->spe_64bit_regs_used != 0)
19419 reg_mode = V2SImode;
19420 reg_size = 8;
19423 /* Handle world saves specially here. */
19424 if (WORLD_SAVE_P (info))
19426 int i, j, sz;
19427 rtx treg;
19428 rtvec p;
19429 rtx reg0;
19431 /* save_world expects lr in r0. */
19432 reg0 = gen_rtx_REG (Pmode, 0);
19433 if (info->lr_save_p)
19435 insn = emit_move_insn (reg0,
19436 gen_rtx_REG (Pmode, LR_REGNO));
19437 RTX_FRAME_RELATED_P (insn) = 1;
19440 /* The SAVE_WORLD and RESTORE_WORLD routines make a number of
19441 assumptions about the offsets of various bits of the stack
19442 frame. */
19443 gcc_assert (info->gp_save_offset == -220
19444 && info->fp_save_offset == -144
19445 && info->lr_save_offset == 8
19446 && info->cr_save_offset == 4
19447 && info->push_p
19448 && info->lr_save_p
19449 && (!crtl->calls_eh_return
19450 || info->ehrd_offset == -432)
19451 && info->vrsave_save_offset == -224
19452 && info->altivec_save_offset == -416);
19454 treg = gen_rtx_REG (SImode, 11);
19455 emit_move_insn (treg, GEN_INT (-info->total_size));
19457 /* SAVE_WORLD takes the caller's LR in R0 and the frame size
19458 in R11. It also clobbers R12, so beware! */
19460 /* Preserve CR2 for save_world prologues */
19461 sz = 5;
19462 sz += 32 - info->first_gp_reg_save;
19463 sz += 64 - info->first_fp_reg_save;
19464 sz += LAST_ALTIVEC_REGNO - info->first_altivec_reg_save + 1;
19465 p = rtvec_alloc (sz);
19466 j = 0;
19467 RTVEC_ELT (p, j++) = gen_rtx_CLOBBER (VOIDmode,
19468 gen_rtx_REG (SImode,
19469 LR_REGNO));
19470 RTVEC_ELT (p, j++) = gen_rtx_USE (VOIDmode,
19471 gen_rtx_SYMBOL_REF (Pmode,
19472 "*save_world"));
19473 /* We do floats first so that the instruction pattern matches
19474 properly. */
19475 for (i = 0; i < 64 - info->first_fp_reg_save; i++)
19476 RTVEC_ELT (p, j++)
19477 = gen_frame_store (gen_rtx_REG (TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT
19478 ? DFmode : SFmode,
19479 info->first_fp_reg_save + i),
19480 frame_reg_rtx,
19481 info->fp_save_offset + frame_off + 8 * i);
19482 for (i = 0; info->first_altivec_reg_save + i <= LAST_ALTIVEC_REGNO; i++)
19483 RTVEC_ELT (p, j++)
19484 = gen_frame_store (gen_rtx_REG (V4SImode,
19485 info->first_altivec_reg_save + i),
19486 frame_reg_rtx,
19487 info->altivec_save_offset + frame_off + 16 * i);
19488 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
19489 RTVEC_ELT (p, j++)
19490 = gen_frame_store (gen_rtx_REG (reg_mode, info->first_gp_reg_save + i),
19491 frame_reg_rtx,
19492 info->gp_save_offset + frame_off + reg_size * i);
19494 /* CR register traditionally saved as CR2. */
19495 RTVEC_ELT (p, j++)
19496 = gen_frame_store (gen_rtx_REG (SImode, CR2_REGNO),
19497 frame_reg_rtx, info->cr_save_offset + frame_off);
19498 /* Explain about use of R0. */
19499 if (info->lr_save_p)
19500 RTVEC_ELT (p, j++)
19501 = gen_frame_store (reg0,
19502 frame_reg_rtx, info->lr_save_offset + frame_off);
19503 /* Explain what happens to the stack pointer. */
19505 rtx newval = gen_rtx_PLUS (Pmode, sp_reg_rtx, treg);
19506 RTVEC_ELT (p, j++) = gen_rtx_SET (VOIDmode, sp_reg_rtx, newval);
19509 insn = emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
19510 rs6000_frame_related (insn, frame_reg_rtx, sp_off - frame_off,
19511 treg, GEN_INT (-info->total_size));
19512 sp_off = frame_off = info->total_size;
19515 strategy = info->savres_strategy;
19517 /* For V.4, update stack before we do any saving and set back pointer. */
19518 if (! WORLD_SAVE_P (info)
19519 && info->push_p
19520 && (DEFAULT_ABI == ABI_V4
19521 || crtl->calls_eh_return))
19523 bool need_r11 = (TARGET_SPE
19524 ? (!(strategy & SAVE_INLINE_GPRS)
19525 && info->spe_64bit_regs_used == 0)
19526 : (!(strategy & SAVE_INLINE_FPRS)
19527 || !(strategy & SAVE_INLINE_GPRS)
19528 || !(strategy & SAVE_INLINE_VRS)));
19529 int ptr_regno = -1;
19530 rtx ptr_reg = NULL_RTX;
19531 int ptr_off = 0;
19533 if (info->total_size < 32767)
19534 frame_off = info->total_size;
19535 else if (need_r11)
19536 ptr_regno = 11;
19537 else if (info->cr_save_p
19538 || info->lr_save_p
19539 || info->first_fp_reg_save < 64
19540 || info->first_gp_reg_save < 32
19541 || info->altivec_size != 0
19542 || info->vrsave_mask != 0
19543 || crtl->calls_eh_return)
19544 ptr_regno = 12;
19545 else
19547 /* The prologue won't be saving any regs so there is no need
19548 to set up a frame register to access any frame save area.
19549 We also won't be using frame_off anywhere below, but set
19550 the correct value anyway to protect against future
19551 changes to this function. */
19552 frame_off = info->total_size;
19554 if (ptr_regno != -1)
19556 /* Set up the frame offset to that needed by the first
19557 out-of-line save function. */
19558 START_USE (ptr_regno);
19559 ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
19560 frame_reg_rtx = ptr_reg;
19561 if (!(strategy & SAVE_INLINE_FPRS) && info->fp_size != 0)
19562 gcc_checking_assert (info->fp_save_offset + info->fp_size == 0);
19563 else if (!(strategy & SAVE_INLINE_GPRS) && info->first_gp_reg_save < 32)
19564 ptr_off = info->gp_save_offset + info->gp_size;
19565 else if (!(strategy & SAVE_INLINE_VRS) && info->altivec_size != 0)
19566 ptr_off = info->altivec_save_offset + info->altivec_size;
19567 frame_off = -ptr_off;
19569 rs6000_emit_allocate_stack (info->total_size, ptr_reg, ptr_off);
19570 sp_off = info->total_size;
19571 if (frame_reg_rtx != sp_reg_rtx)
19572 rs6000_emit_stack_tie (frame_reg_rtx, false);
19575 /* If we use the link register, get it into r0. */
19576 if (!WORLD_SAVE_P (info) && info->lr_save_p)
19578 rtx addr, reg, mem;
19580 reg = gen_rtx_REG (Pmode, 0);
19581 START_USE (0);
19582 insn = emit_move_insn (reg, gen_rtx_REG (Pmode, LR_REGNO));
19583 RTX_FRAME_RELATED_P (insn) = 1;
19585 if (!(strategy & (SAVE_NOINLINE_GPRS_SAVES_LR
19586 | SAVE_NOINLINE_FPRS_SAVES_LR)))
19588 addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
19589 GEN_INT (info->lr_save_offset + frame_off));
19590 mem = gen_rtx_MEM (Pmode, addr);
19591 /* This should not be of rs6000_sr_alias_set, because of
19592 __builtin_return_address. */
19594 insn = emit_move_insn (mem, reg);
19595 rs6000_frame_related (insn, frame_reg_rtx, sp_off - frame_off,
19596 NULL_RTX, NULL_RTX);
19597 END_USE (0);
19601 /* If we need to save CR, put it into r12 or r11. Choose r12 except when
19602 r12 will be needed by out-of-line gpr restore. */
19603 cr_save_regno = (DEFAULT_ABI == ABI_AIX
19604 && !(strategy & (SAVE_INLINE_GPRS
19605 | SAVE_NOINLINE_GPRS_SAVES_LR))
19606 ? 11 : 12);
19607 if (!WORLD_SAVE_P (info)
19608 && info->cr_save_p
19609 && REGNO (frame_reg_rtx) != cr_save_regno
19610 && !(using_static_chain_p && cr_save_regno == 11))
19612 rtx set;
19614 cr_save_rtx = gen_rtx_REG (SImode, cr_save_regno);
19615 START_USE (cr_save_regno);
19616 insn = emit_insn (gen_movesi_from_cr (cr_save_rtx));
19617 RTX_FRAME_RELATED_P (insn) = 1;
19618 /* Now, there's no way that dwarf2out_frame_debug_expr is going
19619 to understand '(unspec:SI [(reg:CC 68) ...] UNSPEC_MOVESI_FROM_CR)'.
19620 But that's OK. All we have to do is specify that _one_ condition
19621 code register is saved in this stack slot. The thrower's epilogue
19622 will then restore all the call-saved registers.
19623 We use CR2_REGNO (70) to be compatible with gcc-2.95 on Linux. */
19624 set = gen_rtx_SET (VOIDmode, cr_save_rtx,
19625 gen_rtx_REG (SImode, CR2_REGNO));
19626 add_reg_note (insn, REG_FRAME_RELATED_EXPR, set);
19629 /* Do any required saving of fpr's. If only one or two to save, do
19630 it ourselves. Otherwise, call function. */
19631 if (!WORLD_SAVE_P (info) && (strategy & SAVE_INLINE_FPRS))
19633 int i;
19634 for (i = 0; i < 64 - info->first_fp_reg_save; i++)
19635 if (save_reg_p (info->first_fp_reg_save + i))
19636 emit_frame_save (frame_reg_rtx,
19637 (TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT
19638 ? DFmode : SFmode),
19639 info->first_fp_reg_save + i,
19640 info->fp_save_offset + frame_off + 8 * i,
19641 sp_off - frame_off);
19643 else if (!WORLD_SAVE_P (info) && info->first_fp_reg_save != 64)
19645 bool lr = (strategy & SAVE_NOINLINE_FPRS_SAVES_LR) != 0;
19646 int sel = SAVRES_SAVE | SAVRES_FPR | (lr ? SAVRES_LR : 0);
19647 unsigned ptr_regno = ptr_regno_for_savres (sel);
19648 rtx ptr_reg = frame_reg_rtx;
19650 if (REGNO (frame_reg_rtx) == ptr_regno)
19651 gcc_checking_assert (frame_off == 0);
19652 else
19654 ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
19655 NOT_INUSE (ptr_regno);
19656 emit_insn (gen_add3_insn (ptr_reg,
19657 frame_reg_rtx, GEN_INT (frame_off)));
19659 insn = rs6000_emit_savres_rtx (info, ptr_reg,
19660 info->fp_save_offset,
19661 info->lr_save_offset,
19662 DFmode, sel);
19663 rs6000_frame_related (insn, ptr_reg, sp_off,
19664 NULL_RTX, NULL_RTX);
19665 if (lr)
19666 END_USE (0);
19669 /* Save GPRs. This is done as a PARALLEL if we are using
19670 the store-multiple instructions. */
19671 if (!WORLD_SAVE_P (info)
19672 && TARGET_SPE_ABI
19673 && info->spe_64bit_regs_used != 0
19674 && info->first_gp_reg_save != 32)
19676 int i;
19677 rtx spe_save_area_ptr;
19678 HOST_WIDE_INT save_off;
19679 int ool_adjust = 0;
19681 /* Determine whether we can address all of the registers that need
19682 to be saved with an offset from frame_reg_rtx that fits in
19683 the small const field for SPE memory instructions. */
19684 int spe_regs_addressable
19685 = (SPE_CONST_OFFSET_OK (info->spe_gp_save_offset + frame_off
19686 + reg_size * (32 - info->first_gp_reg_save - 1))
19687 && (strategy & SAVE_INLINE_GPRS));
19689 if (spe_regs_addressable)
19691 spe_save_area_ptr = frame_reg_rtx;
19692 save_off = frame_off;
19694 else
19696 /* Make r11 point to the start of the SPE save area. We need
19697 to be careful here if r11 is holding the static chain. If
19698 it is, then temporarily save it in r0. */
19699 HOST_WIDE_INT offset;
19701 if (!(strategy & SAVE_INLINE_GPRS))
19702 ool_adjust = 8 * (info->first_gp_reg_save
19703 - (FIRST_SAVRES_REGISTER + 1));
19704 offset = info->spe_gp_save_offset + frame_off - ool_adjust;
19705 spe_save_area_ptr = gen_rtx_REG (Pmode, 11);
19706 save_off = frame_off - offset;
19708 if (using_static_chain_p)
19710 rtx r0 = gen_rtx_REG (Pmode, 0);
19712 START_USE (0);
19713 gcc_assert (info->first_gp_reg_save > 11);
19715 emit_move_insn (r0, spe_save_area_ptr);
19717 else if (REGNO (frame_reg_rtx) != 11)
19718 START_USE (11);
19720 emit_insn (gen_addsi3 (spe_save_area_ptr,
19721 frame_reg_rtx, GEN_INT (offset)));
19722 if (!using_static_chain_p && REGNO (frame_reg_rtx) == 11)
19723 frame_off = -info->spe_gp_save_offset + ool_adjust;
19726 if ((strategy & SAVE_INLINE_GPRS))
19728 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
19729 if (rs6000_reg_live_or_pic_offset_p (info->first_gp_reg_save + i))
19730 emit_frame_save (spe_save_area_ptr, reg_mode,
19731 info->first_gp_reg_save + i,
19732 (info->spe_gp_save_offset + save_off
19733 + reg_size * i),
19734 sp_off - save_off);
19736 else
19738 insn = rs6000_emit_savres_rtx (info, spe_save_area_ptr,
19739 info->spe_gp_save_offset + save_off,
19740 0, reg_mode,
19741 SAVRES_SAVE | SAVRES_GPR);
19743 rs6000_frame_related (insn, spe_save_area_ptr, sp_off - save_off,
19744 NULL_RTX, NULL_RTX);
19747 /* Move the static chain pointer back. */
19748 if (!spe_regs_addressable)
19750 if (using_static_chain_p)
19752 emit_move_insn (spe_save_area_ptr, gen_rtx_REG (Pmode, 0));
19753 END_USE (0);
19755 else if (REGNO (frame_reg_rtx) != 11)
19756 END_USE (11);
19759 else if (!WORLD_SAVE_P (info) && !(strategy & SAVE_INLINE_GPRS))
19761 bool lr = (strategy & SAVE_NOINLINE_GPRS_SAVES_LR) != 0;
19762 int sel = SAVRES_SAVE | SAVRES_GPR | (lr ? SAVRES_LR : 0);
19763 unsigned ptr_regno = ptr_regno_for_savres (sel);
19764 rtx ptr_reg = frame_reg_rtx;
19765 bool ptr_set_up = REGNO (ptr_reg) == ptr_regno;
19766 int end_save = info->gp_save_offset + info->gp_size;
19767 int ptr_off;
19769 if (!ptr_set_up)
19770 ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
19772 /* Need to adjust r11 (r12) if we saved any FPRs. */
19773 if (end_save + frame_off != 0)
19775 rtx offset = GEN_INT (end_save + frame_off);
19777 if (ptr_set_up)
19778 frame_off = -end_save;
19779 else
19780 NOT_INUSE (ptr_regno);
19781 emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx, offset));
19783 else if (!ptr_set_up)
19785 NOT_INUSE (ptr_regno);
19786 emit_move_insn (ptr_reg, frame_reg_rtx);
19788 ptr_off = -end_save;
19789 insn = rs6000_emit_savres_rtx (info, ptr_reg,
19790 info->gp_save_offset + ptr_off,
19791 info->lr_save_offset + ptr_off,
19792 reg_mode, sel);
19793 rs6000_frame_related (insn, ptr_reg, sp_off - ptr_off,
19794 NULL_RTX, NULL_RTX);
19795 if (lr)
19796 END_USE (0);
19798 else if (!WORLD_SAVE_P (info) && (strategy & SAVRES_MULTIPLE))
19800 rtvec p;
19801 int i;
19802 p = rtvec_alloc (32 - info->first_gp_reg_save);
19803 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
19804 RTVEC_ELT (p, i)
19805 = gen_frame_store (gen_rtx_REG (reg_mode, info->first_gp_reg_save + i),
19806 frame_reg_rtx,
19807 info->gp_save_offset + frame_off + reg_size * i);
19808 insn = emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
19809 rs6000_frame_related (insn, frame_reg_rtx, sp_off - frame_off,
19810 NULL_RTX, NULL_RTX);
19812 else if (!WORLD_SAVE_P (info))
19814 int i;
19815 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
19816 if (rs6000_reg_live_or_pic_offset_p (info->first_gp_reg_save + i))
19817 emit_frame_save (frame_reg_rtx, reg_mode,
19818 info->first_gp_reg_save + i,
19819 info->gp_save_offset + frame_off + reg_size * i,
19820 sp_off - frame_off);
19823 if (crtl->calls_eh_return)
19825 unsigned int i;
19826 rtvec p;
19828 for (i = 0; ; ++i)
19830 unsigned int regno = EH_RETURN_DATA_REGNO (i);
19831 if (regno == INVALID_REGNUM)
19832 break;
19835 p = rtvec_alloc (i);
19837 for (i = 0; ; ++i)
19839 unsigned int regno = EH_RETURN_DATA_REGNO (i);
19840 if (regno == INVALID_REGNUM)
19841 break;
19843 insn
19844 = gen_frame_store (gen_rtx_REG (reg_mode, regno),
19845 sp_reg_rtx,
19846 info->ehrd_offset + sp_off + reg_size * (int) i);
19847 RTVEC_ELT (p, i) = insn;
19848 RTX_FRAME_RELATED_P (insn) = 1;
19851 insn = emit_insn (gen_blockage ());
19852 RTX_FRAME_RELATED_P (insn) = 1;
19853 add_reg_note (insn, REG_FRAME_RELATED_EXPR, gen_rtx_PARALLEL (VOIDmode, p));
19856 /* In AIX ABI we need to make sure r2 is really saved. */
19857 if (TARGET_AIX && crtl->calls_eh_return)
19859 rtx tmp_reg, tmp_reg_si, hi, lo, compare_result, toc_save_done, jump;
19860 rtx save_insn, join_insn, note;
19861 long toc_restore_insn;
19863 tmp_reg = gen_rtx_REG (Pmode, 11);
19864 tmp_reg_si = gen_rtx_REG (SImode, 11);
19865 if (using_static_chain_p)
19867 START_USE (0);
19868 emit_move_insn (gen_rtx_REG (Pmode, 0), tmp_reg);
19870 else
19871 START_USE (11);
19872 emit_move_insn (tmp_reg, gen_rtx_REG (Pmode, LR_REGNO));
19873 /* Peek at instruction to which this function returns. If it's
19874 restoring r2, then we know we've already saved r2. We can't
19875 unconditionally save r2 because the value we have will already
19876 be updated if we arrived at this function via a plt call or
19877 toc adjusting stub. */
19878 emit_move_insn (tmp_reg_si, gen_rtx_MEM (SImode, tmp_reg));
19879 toc_restore_insn = TARGET_32BIT ? 0x80410014 : 0xE8410028;
19880 hi = gen_int_mode (toc_restore_insn & ~0xffff, SImode);
19881 emit_insn (gen_xorsi3 (tmp_reg_si, tmp_reg_si, hi));
19882 compare_result = gen_rtx_REG (CCUNSmode, CR0_REGNO);
19883 validate_condition_mode (EQ, CCUNSmode);
19884 lo = gen_int_mode (toc_restore_insn & 0xffff, SImode);
19885 emit_insn (gen_rtx_SET (VOIDmode, compare_result,
19886 gen_rtx_COMPARE (CCUNSmode, tmp_reg_si, lo)));
19887 toc_save_done = gen_label_rtx ();
19888 jump = gen_rtx_IF_THEN_ELSE (VOIDmode,
19889 gen_rtx_EQ (VOIDmode, compare_result,
19890 const0_rtx),
19891 gen_rtx_LABEL_REF (VOIDmode, toc_save_done),
19892 pc_rtx);
19893 jump = emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, jump));
19894 JUMP_LABEL (jump) = toc_save_done;
19895 LABEL_NUSES (toc_save_done) += 1;
19897 save_insn = emit_frame_save (frame_reg_rtx, reg_mode,
19898 TOC_REGNUM, frame_off + 5 * reg_size,
19899 sp_off - frame_off);
19901 emit_label (toc_save_done);
19903 /* ??? If we leave SAVE_INSN as marked as saving R2, then we'll
19904 have a CFG that has different saves along different paths.
19905 Move the note to a dummy blockage insn, which describes that
19906 R2 is unconditionally saved after the label. */
19907 /* ??? An alternate representation might be a special insn pattern
19908 containing both the branch and the store. That might let the
19909 code that minimizes the number of DW_CFA_advance opcodes better
19910 freedom in placing the annotations. */
19911 note = find_reg_note (save_insn, REG_FRAME_RELATED_EXPR, NULL);
19912 if (note)
19913 remove_note (save_insn, note);
19914 else
19915 note = alloc_reg_note (REG_FRAME_RELATED_EXPR,
19916 copy_rtx (PATTERN (save_insn)), NULL_RTX);
19917 RTX_FRAME_RELATED_P (save_insn) = 0;
19919 join_insn = emit_insn (gen_blockage ());
19920 REG_NOTES (join_insn) = note;
19921 RTX_FRAME_RELATED_P (join_insn) = 1;
19923 if (using_static_chain_p)
19925 emit_move_insn (tmp_reg, gen_rtx_REG (Pmode, 0));
19926 END_USE (0);
19928 else
19929 END_USE (11);
19932 /* Save CR if we use any that must be preserved. */
19933 if (!WORLD_SAVE_P (info) && info->cr_save_p)
19935 rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
19936 GEN_INT (info->cr_save_offset + frame_off));
19937 rtx mem = gen_frame_mem (SImode, addr);
19938 /* See the large comment above about why CR2_REGNO is used. */
19939 rtx magic_eh_cr_reg = gen_rtx_REG (SImode, CR2_REGNO);
19941 /* If we didn't copy cr before, do so now using r0. */
19942 if (cr_save_rtx == NULL_RTX)
19944 rtx set;
19946 START_USE (0);
19947 cr_save_rtx = gen_rtx_REG (SImode, 0);
19948 insn = emit_insn (gen_movesi_from_cr (cr_save_rtx));
19949 RTX_FRAME_RELATED_P (insn) = 1;
19950 set = gen_rtx_SET (VOIDmode, cr_save_rtx, magic_eh_cr_reg);
19951 add_reg_note (insn, REG_FRAME_RELATED_EXPR, set);
19953 insn = emit_move_insn (mem, cr_save_rtx);
19954 END_USE (REGNO (cr_save_rtx));
19956 rs6000_frame_related (insn, frame_reg_rtx, sp_off - frame_off,
19957 NULL_RTX, NULL_RTX);
19960 /* Update stack and set back pointer unless this is V.4,
19961 for which it was done previously. */
19962 if (!WORLD_SAVE_P (info) && info->push_p
19963 && !(DEFAULT_ABI == ABI_V4 || crtl->calls_eh_return))
19965 rtx ptr_reg = NULL;
19966 int ptr_off = 0;
19968 /* If saving altivec regs we need to be able to address all save
19969 locations using a 16-bit offset. */
19970 if ((strategy & SAVE_INLINE_VRS) == 0
19971 || (info->altivec_size != 0
19972 && (info->altivec_save_offset + info->altivec_size - 16
19973 + info->total_size - frame_off) > 32767)
19974 || (info->vrsave_mask != 0
19975 && (info->vrsave_save_offset
19976 + info->total_size - frame_off) > 32767))
19978 int sel = SAVRES_SAVE | SAVRES_VR;
19979 unsigned ptr_regno = ptr_regno_for_savres (sel);
19981 if (using_static_chain_p
19982 && ptr_regno == STATIC_CHAIN_REGNUM)
19983 ptr_regno = 12;
19984 if (REGNO (frame_reg_rtx) != ptr_regno)
19985 START_USE (ptr_regno);
19986 ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
19987 frame_reg_rtx = ptr_reg;
19988 ptr_off = info->altivec_save_offset + info->altivec_size;
19989 frame_off = -ptr_off;
19991 else if (REGNO (frame_reg_rtx) == 1)
19992 frame_off = info->total_size;
19993 rs6000_emit_allocate_stack (info->total_size, ptr_reg, ptr_off);
19994 sp_off = info->total_size;
19995 if (frame_reg_rtx != sp_reg_rtx)
19996 rs6000_emit_stack_tie (frame_reg_rtx, false);
19999 /* Set frame pointer, if needed. */
20000 if (frame_pointer_needed)
20002 insn = emit_move_insn (gen_rtx_REG (Pmode, HARD_FRAME_POINTER_REGNUM),
20003 sp_reg_rtx);
20004 RTX_FRAME_RELATED_P (insn) = 1;
20007 /* Save AltiVec registers if needed. Save here because the red zone does
20008 not always include AltiVec registers. */
20009 if (!WORLD_SAVE_P (info) && TARGET_ALTIVEC_ABI
20010 && info->altivec_size != 0 && (strategy & SAVE_INLINE_VRS) == 0)
20012 int end_save = info->altivec_save_offset + info->altivec_size;
20013 int ptr_off;
20014 /* Oddly, the vector save/restore functions point r0 at the end
20015 of the save area, then use r11 or r12 to load offsets for
20016 [reg+reg] addressing. */
20017 rtx ptr_reg = gen_rtx_REG (Pmode, 0);
20018 int scratch_regno = ptr_regno_for_savres (SAVRES_SAVE | SAVRES_VR);
20019 rtx scratch_reg = gen_rtx_REG (Pmode, scratch_regno);
20021 gcc_checking_assert (scratch_regno == 11 || scratch_regno == 12);
20022 NOT_INUSE (0);
20023 if (end_save + frame_off != 0)
20025 rtx offset = GEN_INT (end_save + frame_off);
20027 emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx, offset));
20029 else
20030 emit_move_insn (ptr_reg, frame_reg_rtx);
20032 ptr_off = -end_save;
20033 insn = rs6000_emit_savres_rtx (info, scratch_reg,
20034 info->altivec_save_offset + ptr_off,
20035 0, V4SImode, SAVRES_SAVE | SAVRES_VR);
20036 rs6000_frame_related (insn, scratch_reg, sp_off - ptr_off,
20037 NULL_RTX, NULL_RTX);
20038 if (REGNO (frame_reg_rtx) == REGNO (scratch_reg))
20040 /* The oddity mentioned above clobbered our frame reg. */
20041 emit_move_insn (frame_reg_rtx, ptr_reg);
20042 frame_off = ptr_off;
20045 else if (!WORLD_SAVE_P (info) && TARGET_ALTIVEC_ABI
20046 && info->altivec_size != 0)
20048 int i;
20050 for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
20051 if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
20053 rtx areg, savereg, mem;
20054 int offset;
20056 offset = (info->altivec_save_offset + frame_off
20057 + 16 * (i - info->first_altivec_reg_save));
20059 savereg = gen_rtx_REG (V4SImode, i);
20061 NOT_INUSE (0);
20062 areg = gen_rtx_REG (Pmode, 0);
20063 emit_move_insn (areg, GEN_INT (offset));
20065 /* AltiVec addressing mode is [reg+reg]. */
20066 mem = gen_frame_mem (V4SImode,
20067 gen_rtx_PLUS (Pmode, frame_reg_rtx, areg));
20069 insn = emit_move_insn (mem, savereg);
20071 rs6000_frame_related (insn, frame_reg_rtx, sp_off - frame_off,
20072 areg, GEN_INT (offset));
20076 /* VRSAVE is a bit vector representing which AltiVec registers
20077 are used. The OS uses this to determine which vector
20078 registers to save on a context switch. We need to save
20079 VRSAVE on the stack frame, add whatever AltiVec registers we
20080 used in this function, and do the corresponding magic in the
20081 epilogue. */
20083 if (!WORLD_SAVE_P (info)
20084 && TARGET_ALTIVEC
20085 && TARGET_ALTIVEC_VRSAVE
20086 && info->vrsave_mask != 0)
20088 rtx reg, vrsave;
20089 int offset;
20090 int save_regno;
20092 /* Get VRSAVE onto a GPR. Note that ABI_V4 and ABI_DARWIN might
20093 be using r12 as frame_reg_rtx and r11 as the static chain
20094 pointer for nested functions. */
20095 save_regno = 12;
20096 if (DEFAULT_ABI == ABI_AIX && !using_static_chain_p)
20097 save_regno = 11;
20098 else if (REGNO (frame_reg_rtx) == 12)
20100 save_regno = 11;
20101 if (using_static_chain_p)
20102 save_regno = 0;
20105 NOT_INUSE (save_regno);
20106 reg = gen_rtx_REG (SImode, save_regno);
20107 vrsave = gen_rtx_REG (SImode, VRSAVE_REGNO);
20108 if (TARGET_MACHO)
20109 emit_insn (gen_get_vrsave_internal (reg));
20110 else
20111 emit_insn (gen_rtx_SET (VOIDmode, reg, vrsave));
20113 /* Save VRSAVE. */
20114 offset = info->vrsave_save_offset + frame_off;
20115 insn = emit_insn (gen_frame_store (reg, frame_reg_rtx, offset));
20117 /* Include the registers in the mask. */
20118 emit_insn (gen_iorsi3 (reg, reg, GEN_INT ((int) info->vrsave_mask)));
20120 insn = emit_insn (generate_set_vrsave (reg, info, 0));
20123 /* If we are using RS6000_PIC_OFFSET_TABLE_REGNUM, we need to set it up. */
20124 if (!TARGET_SINGLE_PIC_BASE
20125 && ((TARGET_TOC && TARGET_MINIMAL_TOC && get_pool_size () != 0)
20126 || (DEFAULT_ABI == ABI_V4
20127 && (flag_pic == 1 || (flag_pic && TARGET_SECURE_PLT))
20128 && df_regs_ever_live_p (RS6000_PIC_OFFSET_TABLE_REGNUM))))
20130 /* If emit_load_toc_table will use the link register, we need to save
20131 it. We use R12 for this purpose because emit_load_toc_table
20132 can use register 0. This allows us to use a plain 'blr' to return
20133 from the procedure more often. */
20134 int save_LR_around_toc_setup = (TARGET_ELF
20135 && DEFAULT_ABI != ABI_AIX
20136 && flag_pic
20137 && ! info->lr_save_p
20138 && EDGE_COUNT (EXIT_BLOCK_PTR->preds) > 0);
20139 if (save_LR_around_toc_setup)
20141 rtx lr = gen_rtx_REG (Pmode, LR_REGNO);
20142 rtx tmp = gen_rtx_REG (Pmode, 12);
20144 insn = emit_move_insn (tmp, lr);
20145 RTX_FRAME_RELATED_P (insn) = 1;
20147 rs6000_emit_load_toc_table (TRUE);
20149 insn = emit_move_insn (lr, tmp);
20150 add_reg_note (insn, REG_CFA_RESTORE, lr);
20151 RTX_FRAME_RELATED_P (insn) = 1;
20153 else
20154 rs6000_emit_load_toc_table (TRUE);
20157 #if TARGET_MACHO
20158 if (!TARGET_SINGLE_PIC_BASE
20159 && DEFAULT_ABI == ABI_DARWIN
20160 && flag_pic && crtl->uses_pic_offset_table)
20162 rtx lr = gen_rtx_REG (Pmode, LR_REGNO);
20163 rtx src = gen_rtx_SYMBOL_REF (Pmode, MACHOPIC_FUNCTION_BASE_NAME);
20165 /* Save and restore LR locally around this call (in R0). */
20166 if (!info->lr_save_p)
20167 emit_move_insn (gen_rtx_REG (Pmode, 0), lr);
20169 emit_insn (gen_load_macho_picbase (src));
20171 emit_move_insn (gen_rtx_REG (Pmode,
20172 RS6000_PIC_OFFSET_TABLE_REGNUM),
20173 lr);
20175 if (!info->lr_save_p)
20176 emit_move_insn (lr, gen_rtx_REG (Pmode, 0));
20178 #endif
20180 /* If we need to, save the TOC register after doing the stack setup.
20181 Do not emit eh frame info for this save. The unwinder wants info,
20182 conceptually attached to instructions in this function, about
20183 register values in the caller of this function. This R2 may have
20184 already been changed from the value in the caller.
20185 We don't attempt to write accurate DWARF EH frame info for R2
20186 because code emitted by gcc for a (non-pointer) function call
20187 doesn't save and restore R2. Instead, R2 is managed out-of-line
20188 by a linker generated plt call stub when the function resides in
20189 a shared library. This behaviour is costly to describe in DWARF,
20190 both in terms of the size of DWARF info and the time taken in the
20191 unwinder to interpret it. R2 changes, apart from the
20192 calls_eh_return case earlier in this function, are handled by
20193 linux-unwind.h frob_update_context. */
20194 if (rs6000_save_toc_in_prologue_p ())
20196 rtx reg = gen_rtx_REG (reg_mode, TOC_REGNUM);
20197 emit_insn (gen_frame_store (reg, sp_reg_rtx, 5 * reg_size));
20201 /* Write function prologue. */
20203 static void
20204 rs6000_output_function_prologue (FILE *file,
20205 HOST_WIDE_INT size ATTRIBUTE_UNUSED)
20207 rs6000_stack_t *info = rs6000_stack_info ();
20209 if (TARGET_DEBUG_STACK)
20210 debug_stack_info (info);
20212 /* Write .extern for any function we will call to save and restore
20213 fp values. */
20214 if (info->first_fp_reg_save < 64
20215 && !TARGET_MACHO
20216 && !TARGET_ELF)
20218 char *name;
20219 int regno = info->first_fp_reg_save - 32;
20221 if ((info->savres_strategy & SAVE_INLINE_FPRS) == 0)
20223 bool lr = (info->savres_strategy & SAVE_NOINLINE_FPRS_SAVES_LR) != 0;
20224 int sel = SAVRES_SAVE | SAVRES_FPR | (lr ? SAVRES_LR : 0);
20225 name = rs6000_savres_routine_name (info, regno, sel);
20226 fprintf (file, "\t.extern %s\n", name);
20228 if ((info->savres_strategy & REST_INLINE_FPRS) == 0)
20230 bool lr = (info->savres_strategy
20231 & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR) == 0;
20232 int sel = SAVRES_FPR | (lr ? SAVRES_LR : 0);
20233 name = rs6000_savres_routine_name (info, regno, sel);
20234 fprintf (file, "\t.extern %s\n", name);
20238 /* Write .extern for AIX common mode routines, if needed. */
20239 if (! TARGET_POWER && ! TARGET_POWERPC && ! common_mode_defined)
20241 fputs ("\t.extern __mulh\n", file);
20242 fputs ("\t.extern __mull\n", file);
20243 fputs ("\t.extern __divss\n", file);
20244 fputs ("\t.extern __divus\n", file);
20245 fputs ("\t.extern __quoss\n", file);
20246 fputs ("\t.extern __quous\n", file);
20247 common_mode_defined = 1;
20250 rs6000_pic_labelno++;
20253 /* Non-zero if vmx regs are restored before the frame pop, zero if
20254 we restore after the pop when possible. */
20255 #define ALWAYS_RESTORE_ALTIVEC_BEFORE_POP 0
20257 /* Restoring cr is a two step process: loading a reg from the frame
20258 save, then moving the reg to cr. For ABI_V4 we must let the
20259 unwinder know that the stack location is no longer valid at or
20260 before the stack deallocation, but we can't emit a cfa_restore for
20261 cr at the stack deallocation like we do for other registers.
20262 The trouble is that it is possible for the move to cr to be
20263 scheduled after the stack deallocation. So say exactly where cr
20264 is located on each of the two insns. */
20266 static rtx
20267 load_cr_save (int regno, rtx frame_reg_rtx, int offset, bool exit_func)
20269 rtx mem = gen_frame_mem_offset (SImode, frame_reg_rtx, offset);
20270 rtx reg = gen_rtx_REG (SImode, regno);
20271 rtx insn = emit_move_insn (reg, mem);
20273 if (!exit_func && DEFAULT_ABI == ABI_V4)
20275 rtx cr = gen_rtx_REG (SImode, CR2_REGNO);
20276 rtx set = gen_rtx_SET (VOIDmode, reg, cr);
20278 add_reg_note (insn, REG_CFA_REGISTER, set);
20279 RTX_FRAME_RELATED_P (insn) = 1;
20281 return reg;
20284 /* Reload CR from REG. */
20286 static void
20287 restore_saved_cr (rtx reg, int using_mfcr_multiple, bool exit_func)
20289 int count = 0;
20290 int i;
20292 if (using_mfcr_multiple)
20294 for (i = 0; i < 8; i++)
20295 if (save_reg_p (CR0_REGNO + i))
20296 count++;
20297 gcc_assert (count);
20300 if (using_mfcr_multiple && count > 1)
20302 rtvec p;
20303 int ndx;
20305 p = rtvec_alloc (count);
20307 ndx = 0;
20308 for (i = 0; i < 8; i++)
20309 if (save_reg_p (CR0_REGNO + i))
20311 rtvec r = rtvec_alloc (2);
20312 RTVEC_ELT (r, 0) = reg;
20313 RTVEC_ELT (r, 1) = GEN_INT (1 << (7-i));
20314 RTVEC_ELT (p, ndx) =
20315 gen_rtx_SET (VOIDmode, gen_rtx_REG (CCmode, CR0_REGNO + i),
20316 gen_rtx_UNSPEC (CCmode, r, UNSPEC_MOVESI_TO_CR));
20317 ndx++;
20319 emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
20320 gcc_assert (ndx == count);
20322 else
20323 for (i = 0; i < 8; i++)
20324 if (save_reg_p (CR0_REGNO + i))
20325 emit_insn (gen_movsi_to_cr_one (gen_rtx_REG (CCmode, CR0_REGNO + i),
20326 reg));
20328 if (!exit_func && (DEFAULT_ABI == ABI_V4 || flag_shrink_wrap))
20330 rtx insn = get_last_insn ();
20331 rtx cr = gen_rtx_REG (SImode, CR2_REGNO);
20333 add_reg_note (insn, REG_CFA_RESTORE, cr);
20334 RTX_FRAME_RELATED_P (insn) = 1;
20338 /* Like cr, the move to lr instruction can be scheduled after the
20339 stack deallocation, but unlike cr, its stack frame save is still
20340 valid. So we only need to emit the cfa_restore on the correct
20341 instruction. */
20343 static void
20344 load_lr_save (int regno, rtx frame_reg_rtx, int offset)
20346 rtx mem = gen_frame_mem_offset (Pmode, frame_reg_rtx, offset);
20347 rtx reg = gen_rtx_REG (Pmode, regno);
20349 emit_move_insn (reg, mem);
20352 static void
20353 restore_saved_lr (int regno, bool exit_func)
20355 rtx reg = gen_rtx_REG (Pmode, regno);
20356 rtx lr = gen_rtx_REG (Pmode, LR_REGNO);
20357 rtx insn = emit_move_insn (lr, reg);
20359 if (!exit_func && flag_shrink_wrap)
20361 add_reg_note (insn, REG_CFA_RESTORE, lr);
20362 RTX_FRAME_RELATED_P (insn) = 1;
20366 static rtx
20367 add_crlr_cfa_restore (const rs6000_stack_t *info, rtx cfa_restores)
20369 if (info->cr_save_p)
20370 cfa_restores = alloc_reg_note (REG_CFA_RESTORE,
20371 gen_rtx_REG (SImode, CR2_REGNO),
20372 cfa_restores);
20373 if (info->lr_save_p)
20374 cfa_restores = alloc_reg_note (REG_CFA_RESTORE,
20375 gen_rtx_REG (Pmode, LR_REGNO),
20376 cfa_restores);
20377 return cfa_restores;
20380 /* Return true if OFFSET from stack pointer can be clobbered by signals.
20381 V.4 doesn't have any stack cushion, AIX ABIs have 220 or 288 bytes
20382 below stack pointer not cloberred by signals. */
20384 static inline bool
20385 offset_below_red_zone_p (HOST_WIDE_INT offset)
20387 return offset < (DEFAULT_ABI == ABI_V4
20389 : TARGET_32BIT ? -220 : -288);
20392 /* Append CFA_RESTORES to any existing REG_NOTES on the last insn. */
20394 static void
20395 emit_cfa_restores (rtx cfa_restores)
20397 rtx insn = get_last_insn ();
20398 rtx *loc = &REG_NOTES (insn);
20400 while (*loc)
20401 loc = &XEXP (*loc, 1);
20402 *loc = cfa_restores;
20403 RTX_FRAME_RELATED_P (insn) = 1;
20406 /* Emit function epilogue as insns. */
20408 void
20409 rs6000_emit_epilogue (int sibcall)
20411 rs6000_stack_t *info;
20412 int restoring_GPRs_inline;
20413 int restoring_FPRs_inline;
20414 int using_load_multiple;
20415 int using_mtcr_multiple;
20416 int use_backchain_to_restore_sp;
20417 int restore_lr;
20418 int strategy;
20419 HOST_WIDE_INT frame_off = 0;
20420 rtx sp_reg_rtx = gen_rtx_REG (Pmode, 1);
20421 rtx frame_reg_rtx = sp_reg_rtx;
20422 rtx cfa_restores = NULL_RTX;
20423 rtx insn;
20424 rtx cr_save_reg = NULL_RTX;
20425 enum machine_mode reg_mode = Pmode;
20426 int reg_size = TARGET_32BIT ? 4 : 8;
20427 int i;
20428 bool exit_func;
20429 unsigned ptr_regno;
20431 info = rs6000_stack_info ();
20433 if (TARGET_SPE_ABI && info->spe_64bit_regs_used != 0)
20435 reg_mode = V2SImode;
20436 reg_size = 8;
20439 strategy = info->savres_strategy;
20440 using_load_multiple = strategy & SAVRES_MULTIPLE;
20441 restoring_FPRs_inline = sibcall || (strategy & REST_INLINE_FPRS);
20442 restoring_GPRs_inline = sibcall || (strategy & REST_INLINE_GPRS);
20443 using_mtcr_multiple = (rs6000_cpu == PROCESSOR_PPC601
20444 || rs6000_cpu == PROCESSOR_PPC603
20445 || rs6000_cpu == PROCESSOR_PPC750
20446 || optimize_size);
20447 /* Restore via the backchain when we have a large frame, since this
20448 is more efficient than an addis, addi pair. The second condition
20449 here will not trigger at the moment; We don't actually need a
20450 frame pointer for alloca, but the generic parts of the compiler
20451 give us one anyway. */
20452 use_backchain_to_restore_sp = (info->total_size > 32767 - info->lr_save_offset
20453 || (cfun->calls_alloca
20454 && !frame_pointer_needed));
20455 restore_lr = (info->lr_save_p
20456 && (restoring_FPRs_inline
20457 || (strategy & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR))
20458 && (restoring_GPRs_inline
20459 || info->first_fp_reg_save < 64));
20461 if (WORLD_SAVE_P (info))
20463 int i, j;
20464 char rname[30];
20465 const char *alloc_rname;
20466 rtvec p;
20468 /* eh_rest_world_r10 will return to the location saved in the LR
20469 stack slot (which is not likely to be our caller.)
20470 Input: R10 -- stack adjustment. Clobbers R0, R11, R12, R7, R8.
20471 rest_world is similar, except any R10 parameter is ignored.
20472 The exception-handling stuff that was here in 2.95 is no
20473 longer necessary. */
20475 p = rtvec_alloc (9
20477 + 32 - info->first_gp_reg_save
20478 + LAST_ALTIVEC_REGNO + 1 - info->first_altivec_reg_save
20479 + 63 + 1 - info->first_fp_reg_save);
20481 strcpy (rname, ((crtl->calls_eh_return) ?
20482 "*eh_rest_world_r10" : "*rest_world"));
20483 alloc_rname = ggc_strdup (rname);
20485 j = 0;
20486 RTVEC_ELT (p, j++) = ret_rtx;
20487 RTVEC_ELT (p, j++) = gen_rtx_USE (VOIDmode,
20488 gen_rtx_REG (Pmode,
20489 LR_REGNO));
20490 RTVEC_ELT (p, j++)
20491 = gen_rtx_USE (VOIDmode, gen_rtx_SYMBOL_REF (Pmode, alloc_rname));
20492 /* The instruction pattern requires a clobber here;
20493 it is shared with the restVEC helper. */
20494 RTVEC_ELT (p, j++)
20495 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, 11));
20498 /* CR register traditionally saved as CR2. */
20499 rtx reg = gen_rtx_REG (SImode, CR2_REGNO);
20500 RTVEC_ELT (p, j++)
20501 = gen_frame_load (reg, frame_reg_rtx, info->cr_save_offset);
20502 if (flag_shrink_wrap)
20504 cfa_restores = alloc_reg_note (REG_CFA_RESTORE,
20505 gen_rtx_REG (Pmode, LR_REGNO),
20506 cfa_restores);
20507 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
20511 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
20513 rtx reg = gen_rtx_REG (reg_mode, info->first_gp_reg_save + i);
20514 RTVEC_ELT (p, j++)
20515 = gen_frame_load (reg,
20516 frame_reg_rtx, info->gp_save_offset + reg_size * i);
20517 if (flag_shrink_wrap)
20518 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
20520 for (i = 0; info->first_altivec_reg_save + i <= LAST_ALTIVEC_REGNO; i++)
20522 rtx reg = gen_rtx_REG (V4SImode, info->first_altivec_reg_save + i);
20523 RTVEC_ELT (p, j++)
20524 = gen_frame_load (reg,
20525 frame_reg_rtx, info->altivec_save_offset + 16 * i);
20526 if (flag_shrink_wrap)
20527 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
20529 for (i = 0; info->first_fp_reg_save + i <= 63; i++)
20531 rtx reg = gen_rtx_REG ((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT
20532 ? DFmode : SFmode),
20533 info->first_fp_reg_save + i);
20534 RTVEC_ELT (p, j++)
20535 = gen_frame_load (reg, frame_reg_rtx, info->fp_save_offset + 8 * i);
20536 if (flag_shrink_wrap)
20537 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
20539 RTVEC_ELT (p, j++)
20540 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, 0));
20541 RTVEC_ELT (p, j++)
20542 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (SImode, 12));
20543 RTVEC_ELT (p, j++)
20544 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (SImode, 7));
20545 RTVEC_ELT (p, j++)
20546 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (SImode, 8));
20547 RTVEC_ELT (p, j++)
20548 = gen_rtx_USE (VOIDmode, gen_rtx_REG (SImode, 10));
20549 insn = emit_jump_insn (gen_rtx_PARALLEL (VOIDmode, p));
20551 if (flag_shrink_wrap)
20553 REG_NOTES (insn) = cfa_restores;
20554 add_reg_note (insn, REG_CFA_DEF_CFA, sp_reg_rtx);
20555 RTX_FRAME_RELATED_P (insn) = 1;
20557 return;
20560 /* frame_reg_rtx + frame_off points to the top of this stack frame. */
20561 if (info->push_p)
20562 frame_off = info->total_size;
20564 /* Restore AltiVec registers if we must do so before adjusting the
20565 stack. */
20566 if (TARGET_ALTIVEC_ABI
20567 && info->altivec_size != 0
20568 && (ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
20569 || (DEFAULT_ABI != ABI_V4
20570 && offset_below_red_zone_p (info->altivec_save_offset))))
20572 int i;
20573 int scratch_regno = ptr_regno_for_savres (SAVRES_VR);
20575 gcc_checking_assert (scratch_regno == 11 || scratch_regno == 12);
20576 if (use_backchain_to_restore_sp)
20578 int frame_regno = 11;
20580 if ((strategy & REST_INLINE_VRS) == 0)
20582 /* Of r11 and r12, select the one not clobbered by an
20583 out-of-line restore function for the frame register. */
20584 frame_regno = 11 + 12 - scratch_regno;
20586 frame_reg_rtx = gen_rtx_REG (Pmode, frame_regno);
20587 emit_move_insn (frame_reg_rtx,
20588 gen_rtx_MEM (Pmode, sp_reg_rtx));
20589 frame_off = 0;
20591 else if (frame_pointer_needed)
20592 frame_reg_rtx = hard_frame_pointer_rtx;
20594 if ((strategy & REST_INLINE_VRS) == 0)
20596 int end_save = info->altivec_save_offset + info->altivec_size;
20597 int ptr_off;
20598 rtx ptr_reg = gen_rtx_REG (Pmode, 0);
20599 rtx scratch_reg = gen_rtx_REG (Pmode, scratch_regno);
20601 if (end_save + frame_off != 0)
20603 rtx offset = GEN_INT (end_save + frame_off);
20605 emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx, offset));
20607 else
20608 emit_move_insn (ptr_reg, frame_reg_rtx);
20610 ptr_off = -end_save;
20611 insn = rs6000_emit_savres_rtx (info, scratch_reg,
20612 info->altivec_save_offset + ptr_off,
20613 0, V4SImode, SAVRES_VR);
20615 else
20617 for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
20618 if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
20620 rtx addr, areg, mem, reg;
20622 areg = gen_rtx_REG (Pmode, 0);
20623 emit_move_insn
20624 (areg, GEN_INT (info->altivec_save_offset
20625 + frame_off
20626 + 16 * (i - info->first_altivec_reg_save)));
20628 /* AltiVec addressing mode is [reg+reg]. */
20629 addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, areg);
20630 mem = gen_frame_mem (V4SImode, addr);
20632 reg = gen_rtx_REG (V4SImode, i);
20633 emit_move_insn (reg, mem);
20637 for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
20638 if (((strategy & REST_INLINE_VRS) == 0
20639 || (info->vrsave_mask & ALTIVEC_REG_BIT (i)) != 0)
20640 && (flag_shrink_wrap
20641 || (offset_below_red_zone_p
20642 (info->altivec_save_offset
20643 + 16 * (i - info->first_altivec_reg_save)))))
20645 rtx reg = gen_rtx_REG (V4SImode, i);
20646 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
20650 /* Restore VRSAVE if we must do so before adjusting the stack. */
20651 if (TARGET_ALTIVEC
20652 && TARGET_ALTIVEC_VRSAVE
20653 && info->vrsave_mask != 0
20654 && (ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
20655 || (DEFAULT_ABI != ABI_V4
20656 && offset_below_red_zone_p (info->vrsave_save_offset))))
20658 rtx reg;
20660 if (frame_reg_rtx == sp_reg_rtx)
20662 if (use_backchain_to_restore_sp)
20664 frame_reg_rtx = gen_rtx_REG (Pmode, 11);
20665 emit_move_insn (frame_reg_rtx,
20666 gen_rtx_MEM (Pmode, sp_reg_rtx));
20667 frame_off = 0;
20669 else if (frame_pointer_needed)
20670 frame_reg_rtx = hard_frame_pointer_rtx;
20673 reg = gen_rtx_REG (SImode, 12);
20674 emit_insn (gen_frame_load (reg, frame_reg_rtx,
20675 info->vrsave_save_offset + frame_off));
20677 emit_insn (generate_set_vrsave (reg, info, 1));
20680 insn = NULL_RTX;
20681 /* If we have a large stack frame, restore the old stack pointer
20682 using the backchain. */
20683 if (use_backchain_to_restore_sp)
20685 if (frame_reg_rtx == sp_reg_rtx)
20687 /* Under V.4, don't reset the stack pointer until after we're done
20688 loading the saved registers. */
20689 if (DEFAULT_ABI == ABI_V4)
20690 frame_reg_rtx = gen_rtx_REG (Pmode, 11);
20692 insn = emit_move_insn (frame_reg_rtx,
20693 gen_rtx_MEM (Pmode, sp_reg_rtx));
20694 frame_off = 0;
20696 else if (ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
20697 && DEFAULT_ABI == ABI_V4)
20698 /* frame_reg_rtx has been set up by the altivec restore. */
20700 else
20702 insn = emit_move_insn (sp_reg_rtx, frame_reg_rtx);
20703 frame_reg_rtx = sp_reg_rtx;
20706 /* If we have a frame pointer, we can restore the old stack pointer
20707 from it. */
20708 else if (frame_pointer_needed)
20710 frame_reg_rtx = sp_reg_rtx;
20711 if (DEFAULT_ABI == ABI_V4)
20712 frame_reg_rtx = gen_rtx_REG (Pmode, 11);
20713 /* Prevent reordering memory accesses against stack pointer restore. */
20714 else if (cfun->calls_alloca
20715 || offset_below_red_zone_p (-info->total_size))
20716 rs6000_emit_stack_tie (frame_reg_rtx, true);
20718 insn = emit_insn (gen_add3_insn (frame_reg_rtx, hard_frame_pointer_rtx,
20719 GEN_INT (info->total_size)));
20720 frame_off = 0;
20722 else if (info->push_p
20723 && DEFAULT_ABI != ABI_V4
20724 && !crtl->calls_eh_return)
20726 /* Prevent reordering memory accesses against stack pointer restore. */
20727 if (cfun->calls_alloca
20728 || offset_below_red_zone_p (-info->total_size))
20729 rs6000_emit_stack_tie (frame_reg_rtx, false);
20730 insn = emit_insn (gen_add3_insn (sp_reg_rtx, sp_reg_rtx,
20731 GEN_INT (info->total_size)));
20732 frame_off = 0;
20734 if (insn && frame_reg_rtx == sp_reg_rtx)
20736 if (cfa_restores)
20738 REG_NOTES (insn) = cfa_restores;
20739 cfa_restores = NULL_RTX;
20741 add_reg_note (insn, REG_CFA_DEF_CFA, sp_reg_rtx);
20742 RTX_FRAME_RELATED_P (insn) = 1;
20745 /* Restore AltiVec registers if we have not done so already. */
20746 if (!ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
20747 && TARGET_ALTIVEC_ABI
20748 && info->altivec_size != 0
20749 && (DEFAULT_ABI == ABI_V4
20750 || !offset_below_red_zone_p (info->altivec_save_offset)))
20752 int i;
20754 if ((strategy & REST_INLINE_VRS) == 0)
20756 int end_save = info->altivec_save_offset + info->altivec_size;
20757 int ptr_off;
20758 rtx ptr_reg = gen_rtx_REG (Pmode, 0);
20759 int scratch_regno = ptr_regno_for_savres (SAVRES_VR);
20760 rtx scratch_reg = gen_rtx_REG (Pmode, scratch_regno);
20762 if (end_save + frame_off != 0)
20764 rtx offset = GEN_INT (end_save + frame_off);
20766 emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx, offset));
20768 else
20769 emit_move_insn (ptr_reg, frame_reg_rtx);
20771 ptr_off = -end_save;
20772 insn = rs6000_emit_savres_rtx (info, scratch_reg,
20773 info->altivec_save_offset + ptr_off,
20774 0, V4SImode, SAVRES_VR);
20775 if (REGNO (frame_reg_rtx) == REGNO (scratch_reg))
20777 /* Frame reg was clobbered by out-of-line save. Restore it
20778 from ptr_reg, and if we are calling out-of-line gpr or
20779 fpr restore set up the correct pointer and offset. */
20780 unsigned newptr_regno = 1;
20781 if (!restoring_GPRs_inline)
20783 bool lr = info->gp_save_offset + info->gp_size == 0;
20784 int sel = SAVRES_GPR | (lr ? SAVRES_LR : 0);
20785 newptr_regno = ptr_regno_for_savres (sel);
20786 end_save = info->gp_save_offset + info->gp_size;
20788 else if (!restoring_FPRs_inline)
20790 bool lr = !(strategy & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR);
20791 int sel = SAVRES_FPR | (lr ? SAVRES_LR : 0);
20792 newptr_regno = ptr_regno_for_savres (sel);
20793 end_save = info->gp_save_offset + info->gp_size;
20796 if (newptr_regno != 1 && REGNO (frame_reg_rtx) != newptr_regno)
20797 frame_reg_rtx = gen_rtx_REG (Pmode, newptr_regno);
20799 if (end_save + ptr_off != 0)
20801 rtx offset = GEN_INT (end_save + ptr_off);
20803 frame_off = -end_save;
20804 emit_insn (gen_add3_insn (frame_reg_rtx, ptr_reg, offset));
20806 else
20808 frame_off = ptr_off;
20809 emit_move_insn (frame_reg_rtx, ptr_reg);
20813 else
20815 for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
20816 if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
20818 rtx addr, areg, mem, reg;
20820 areg = gen_rtx_REG (Pmode, 0);
20821 emit_move_insn
20822 (areg, GEN_INT (info->altivec_save_offset
20823 + frame_off
20824 + 16 * (i - info->first_altivec_reg_save)));
20826 /* AltiVec addressing mode is [reg+reg]. */
20827 addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, areg);
20828 mem = gen_frame_mem (V4SImode, addr);
20830 reg = gen_rtx_REG (V4SImode, i);
20831 emit_move_insn (reg, mem);
20835 for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
20836 if (((strategy & REST_INLINE_VRS) == 0
20837 || (info->vrsave_mask & ALTIVEC_REG_BIT (i)) != 0)
20838 && (DEFAULT_ABI == ABI_V4 || flag_shrink_wrap))
20840 rtx reg = gen_rtx_REG (V4SImode, i);
20841 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
20845 /* Restore VRSAVE if we have not done so already. */
20846 if (!ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
20847 && TARGET_ALTIVEC
20848 && TARGET_ALTIVEC_VRSAVE
20849 && info->vrsave_mask != 0
20850 && (DEFAULT_ABI == ABI_V4
20851 || !offset_below_red_zone_p (info->vrsave_save_offset)))
20853 rtx reg;
20855 reg = gen_rtx_REG (SImode, 12);
20856 emit_insn (gen_frame_load (reg, frame_reg_rtx,
20857 info->vrsave_save_offset + frame_off));
20859 emit_insn (generate_set_vrsave (reg, info, 1));
20862 /* If we exit by an out-of-line restore function on ABI_V4 then that
20863 function will deallocate the stack, so we don't need to worry
20864 about the unwinder restoring cr from an invalid stack frame
20865 location. */
20866 exit_func = (!restoring_FPRs_inline
20867 || (!restoring_GPRs_inline
20868 && info->first_fp_reg_save == 64));
20870 /* Get the old lr if we saved it. If we are restoring registers
20871 out-of-line, then the out-of-line routines can do this for us. */
20872 if (restore_lr && restoring_GPRs_inline)
20873 load_lr_save (0, frame_reg_rtx, info->lr_save_offset + frame_off);
20875 /* Get the old cr if we saved it. */
20876 if (info->cr_save_p)
20878 unsigned cr_save_regno = 12;
20880 if (!restoring_GPRs_inline)
20882 /* Ensure we don't use the register used by the out-of-line
20883 gpr register restore below. */
20884 bool lr = info->gp_save_offset + info->gp_size == 0;
20885 int sel = SAVRES_GPR | (lr ? SAVRES_LR : 0);
20886 int gpr_ptr_regno = ptr_regno_for_savres (sel);
20888 if (gpr_ptr_regno == 12)
20889 cr_save_regno = 11;
20890 gcc_checking_assert (REGNO (frame_reg_rtx) != cr_save_regno);
20892 else if (REGNO (frame_reg_rtx) == 12)
20893 cr_save_regno = 11;
20895 cr_save_reg = load_cr_save (cr_save_regno, frame_reg_rtx,
20896 info->cr_save_offset + frame_off,
20897 exit_func);
20900 /* Set LR here to try to overlap restores below. */
20901 if (restore_lr && restoring_GPRs_inline)
20902 restore_saved_lr (0, exit_func);
20904 /* Load exception handler data registers, if needed. */
20905 if (crtl->calls_eh_return)
20907 unsigned int i, regno;
20909 if (TARGET_AIX)
20911 rtx reg = gen_rtx_REG (reg_mode, 2);
20912 emit_insn (gen_frame_load (reg, frame_reg_rtx,
20913 frame_off + 5 * reg_size));
20916 for (i = 0; ; ++i)
20918 rtx mem;
20920 regno = EH_RETURN_DATA_REGNO (i);
20921 if (regno == INVALID_REGNUM)
20922 break;
20924 /* Note: possible use of r0 here to address SPE regs. */
20925 mem = gen_frame_mem_offset (reg_mode, frame_reg_rtx,
20926 info->ehrd_offset + frame_off
20927 + reg_size * (int) i);
20929 emit_move_insn (gen_rtx_REG (reg_mode, regno), mem);
20933 /* Restore GPRs. This is done as a PARALLEL if we are using
20934 the load-multiple instructions. */
20935 if (TARGET_SPE_ABI
20936 && info->spe_64bit_regs_used
20937 && info->first_gp_reg_save != 32)
20939 /* Determine whether we can address all of the registers that need
20940 to be saved with an offset from frame_reg_rtx that fits in
20941 the small const field for SPE memory instructions. */
20942 int spe_regs_addressable
20943 = (SPE_CONST_OFFSET_OK (info->spe_gp_save_offset + frame_off
20944 + reg_size * (32 - info->first_gp_reg_save - 1))
20945 && restoring_GPRs_inline);
20947 if (!spe_regs_addressable)
20949 int ool_adjust = 0;
20950 rtx old_frame_reg_rtx = frame_reg_rtx;
20951 /* Make r11 point to the start of the SPE save area. We worried about
20952 not clobbering it when we were saving registers in the prologue.
20953 There's no need to worry here because the static chain is passed
20954 anew to every function. */
20956 if (!restoring_GPRs_inline)
20957 ool_adjust = 8 * (info->first_gp_reg_save
20958 - (FIRST_SAVRES_REGISTER + 1));
20959 frame_reg_rtx = gen_rtx_REG (Pmode, 11);
20960 emit_insn (gen_addsi3 (frame_reg_rtx, old_frame_reg_rtx,
20961 GEN_INT (info->spe_gp_save_offset
20962 + frame_off
20963 - ool_adjust)));
20964 /* Keep the invariant that frame_reg_rtx + frame_off points
20965 at the top of the stack frame. */
20966 frame_off = -info->spe_gp_save_offset + ool_adjust;
20969 if (restoring_GPRs_inline)
20971 HOST_WIDE_INT spe_offset = info->spe_gp_save_offset + frame_off;
20973 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
20974 if (rs6000_reg_live_or_pic_offset_p (info->first_gp_reg_save + i))
20976 rtx offset, addr, mem, reg;
20978 /* We're doing all this to ensure that the immediate offset
20979 fits into the immediate field of 'evldd'. */
20980 gcc_assert (SPE_CONST_OFFSET_OK (spe_offset + reg_size * i));
20982 offset = GEN_INT (spe_offset + reg_size * i);
20983 addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, offset);
20984 mem = gen_rtx_MEM (V2SImode, addr);
20985 reg = gen_rtx_REG (reg_mode, info->first_gp_reg_save + i);
20987 emit_move_insn (reg, mem);
20990 else
20991 rs6000_emit_savres_rtx (info, frame_reg_rtx,
20992 info->spe_gp_save_offset + frame_off,
20993 info->lr_save_offset + frame_off,
20994 reg_mode,
20995 SAVRES_GPR | SAVRES_LR);
20997 else if (!restoring_GPRs_inline)
20999 /* We are jumping to an out-of-line function. */
21000 rtx ptr_reg;
21001 int end_save = info->gp_save_offset + info->gp_size;
21002 bool can_use_exit = end_save == 0;
21003 int sel = SAVRES_GPR | (can_use_exit ? SAVRES_LR : 0);
21004 int ptr_off;
21006 /* Emit stack reset code if we need it. */
21007 ptr_regno = ptr_regno_for_savres (sel);
21008 ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
21009 if (can_use_exit)
21010 rs6000_emit_stack_reset (info, frame_reg_rtx, frame_off, ptr_regno);
21011 else if (end_save + frame_off != 0)
21012 emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx,
21013 GEN_INT (end_save + frame_off)));
21014 else if (REGNO (frame_reg_rtx) != ptr_regno)
21015 emit_move_insn (ptr_reg, frame_reg_rtx);
21016 if (REGNO (frame_reg_rtx) == ptr_regno)
21017 frame_off = -end_save;
21019 if (can_use_exit && info->cr_save_p)
21020 restore_saved_cr (cr_save_reg, using_mtcr_multiple, true);
21022 ptr_off = -end_save;
21023 rs6000_emit_savres_rtx (info, ptr_reg,
21024 info->gp_save_offset + ptr_off,
21025 info->lr_save_offset + ptr_off,
21026 reg_mode, sel);
21028 else if (using_load_multiple)
21030 rtvec p;
21031 p = rtvec_alloc (32 - info->first_gp_reg_save);
21032 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
21033 RTVEC_ELT (p, i)
21034 = gen_frame_load (gen_rtx_REG (reg_mode, info->first_gp_reg_save + i),
21035 frame_reg_rtx,
21036 info->gp_save_offset + frame_off + reg_size * i);
21037 emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
21039 else
21041 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
21042 if (rs6000_reg_live_or_pic_offset_p (info->first_gp_reg_save + i))
21043 emit_insn (gen_frame_load
21044 (gen_rtx_REG (reg_mode, info->first_gp_reg_save + i),
21045 frame_reg_rtx,
21046 info->gp_save_offset + frame_off + reg_size * i));
21049 if (DEFAULT_ABI == ABI_V4 || flag_shrink_wrap)
21051 /* If the frame pointer was used then we can't delay emitting
21052 a REG_CFA_DEF_CFA note. This must happen on the insn that
21053 restores the frame pointer, r31. We may have already emitted
21054 a REG_CFA_DEF_CFA note, but that's OK; A duplicate is
21055 discarded by dwarf2cfi.c/dwarf2out.c, and in any case would
21056 be harmless if emitted. */
21057 if (frame_pointer_needed)
21059 insn = get_last_insn ();
21060 add_reg_note (insn, REG_CFA_DEF_CFA,
21061 plus_constant (Pmode, frame_reg_rtx, frame_off));
21062 RTX_FRAME_RELATED_P (insn) = 1;
21065 /* Set up cfa_restores. We always need these when
21066 shrink-wrapping. If not shrink-wrapping then we only need
21067 the cfa_restore when the stack location is no longer valid.
21068 The cfa_restores must be emitted on or before the insn that
21069 invalidates the stack, and of course must not be emitted
21070 before the insn that actually does the restore. The latter
21071 is why it is a bad idea to emit the cfa_restores as a group
21072 on the last instruction here that actually does a restore:
21073 That insn may be reordered with respect to others doing
21074 restores. */
21075 if (flag_shrink_wrap
21076 && !restoring_GPRs_inline
21077 && info->first_fp_reg_save == 64)
21078 cfa_restores = add_crlr_cfa_restore (info, cfa_restores);
21080 for (i = info->first_gp_reg_save; i < 32; i++)
21081 if (!restoring_GPRs_inline
21082 || using_load_multiple
21083 || rs6000_reg_live_or_pic_offset_p (i))
21085 rtx reg = gen_rtx_REG (reg_mode, i);
21087 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
21091 if (!restoring_GPRs_inline
21092 && info->first_fp_reg_save == 64)
21094 /* We are jumping to an out-of-line function. */
21095 if (cfa_restores)
21096 emit_cfa_restores (cfa_restores);
21097 return;
21100 if (restore_lr && !restoring_GPRs_inline)
21102 load_lr_save (0, frame_reg_rtx, info->lr_save_offset + frame_off);
21103 restore_saved_lr (0, exit_func);
21106 /* Restore fpr's if we need to do it without calling a function. */
21107 if (restoring_FPRs_inline)
21108 for (i = 0; i < 64 - info->first_fp_reg_save; i++)
21109 if (save_reg_p (info->first_fp_reg_save + i))
21111 rtx reg = gen_rtx_REG ((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT
21112 ? DFmode : SFmode),
21113 info->first_fp_reg_save + i);
21114 emit_insn (gen_frame_load (reg, frame_reg_rtx,
21115 info->fp_save_offset + frame_off + 8 * i));
21116 if (DEFAULT_ABI == ABI_V4 || flag_shrink_wrap)
21117 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
21120 /* If we saved cr, restore it here. Just those that were used. */
21121 if (info->cr_save_p)
21122 restore_saved_cr (cr_save_reg, using_mtcr_multiple, exit_func);
21124 /* If this is V.4, unwind the stack pointer after all of the loads
21125 have been done, or set up r11 if we are restoring fp out of line. */
21126 ptr_regno = 1;
21127 if (!restoring_FPRs_inline)
21129 bool lr = (strategy & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR) == 0;
21130 int sel = SAVRES_FPR | (lr ? SAVRES_LR : 0);
21131 ptr_regno = ptr_regno_for_savres (sel);
21134 insn = rs6000_emit_stack_reset (info, frame_reg_rtx, frame_off, ptr_regno);
21135 if (REGNO (frame_reg_rtx) == ptr_regno)
21136 frame_off = 0;
21138 if (insn && restoring_FPRs_inline)
21140 if (cfa_restores)
21142 REG_NOTES (insn) = cfa_restores;
21143 cfa_restores = NULL_RTX;
21145 add_reg_note (insn, REG_CFA_DEF_CFA, sp_reg_rtx);
21146 RTX_FRAME_RELATED_P (insn) = 1;
21149 if (crtl->calls_eh_return)
21151 rtx sa = EH_RETURN_STACKADJ_RTX;
21152 emit_insn (gen_add3_insn (sp_reg_rtx, sp_reg_rtx, sa));
21155 if (!sibcall)
21157 rtvec p;
21158 bool lr = (strategy & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR) == 0;
21159 if (! restoring_FPRs_inline)
21161 p = rtvec_alloc (4 + 64 - info->first_fp_reg_save);
21162 RTVEC_ELT (p, 0) = ret_rtx;
21164 else
21166 if (cfa_restores)
21168 /* We can't hang the cfa_restores off a simple return,
21169 since the shrink-wrap code sometimes uses an existing
21170 return. This means there might be a path from
21171 pre-prologue code to this return, and dwarf2cfi code
21172 wants the eh_frame unwinder state to be the same on
21173 all paths to any point. So we need to emit the
21174 cfa_restores before the return. For -m64 we really
21175 don't need epilogue cfa_restores at all, except for
21176 this irritating dwarf2cfi with shrink-wrap
21177 requirement; The stack red-zone means eh_frame info
21178 from the prologue telling the unwinder to restore
21179 from the stack is perfectly good right to the end of
21180 the function. */
21181 emit_insn (gen_blockage ());
21182 emit_cfa_restores (cfa_restores);
21183 cfa_restores = NULL_RTX;
21185 p = rtvec_alloc (2);
21186 RTVEC_ELT (p, 0) = simple_return_rtx;
21189 RTVEC_ELT (p, 1) = ((restoring_FPRs_inline || !lr)
21190 ? gen_rtx_USE (VOIDmode,
21191 gen_rtx_REG (Pmode, LR_REGNO))
21192 : gen_rtx_CLOBBER (VOIDmode,
21193 gen_rtx_REG (Pmode, LR_REGNO)));
21195 /* If we have to restore more than two FP registers, branch to the
21196 restore function. It will return to our caller. */
21197 if (! restoring_FPRs_inline)
21199 int i;
21200 rtx sym;
21202 if (flag_shrink_wrap)
21203 cfa_restores = add_crlr_cfa_restore (info, cfa_restores);
21205 sym = rs6000_savres_routine_sym (info,
21206 SAVRES_FPR | (lr ? SAVRES_LR : 0));
21207 RTVEC_ELT (p, 2) = gen_rtx_USE (VOIDmode, sym);
21208 RTVEC_ELT (p, 3) = gen_rtx_USE (VOIDmode,
21209 gen_rtx_REG (Pmode,
21210 DEFAULT_ABI == ABI_AIX
21211 ? 1 : 11));
21212 for (i = 0; i < 64 - info->first_fp_reg_save; i++)
21214 rtx reg = gen_rtx_REG (DFmode, info->first_fp_reg_save + i);
21216 RTVEC_ELT (p, i + 4)
21217 = gen_frame_load (reg, sp_reg_rtx, info->fp_save_offset + 8 * i);
21218 if (flag_shrink_wrap)
21219 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg,
21220 cfa_restores);
21224 emit_jump_insn (gen_rtx_PARALLEL (VOIDmode, p));
21227 if (cfa_restores)
21229 if (sibcall)
21230 /* Ensure the cfa_restores are hung off an insn that won't
21231 be reordered above other restores. */
21232 emit_insn (gen_blockage ());
21234 emit_cfa_restores (cfa_restores);
21238 /* Write function epilogue. */
21240 static void
21241 rs6000_output_function_epilogue (FILE *file,
21242 HOST_WIDE_INT size ATTRIBUTE_UNUSED)
21244 #if TARGET_MACHO
21245 macho_branch_islands ();
21246 /* Mach-O doesn't support labels at the end of objects, so if
21247 it looks like we might want one, insert a NOP. */
21249 rtx insn = get_last_insn ();
21250 rtx deleted_debug_label = NULL_RTX;
21251 while (insn
21252 && NOTE_P (insn)
21253 && NOTE_KIND (insn) != NOTE_INSN_DELETED_LABEL)
21255 /* Don't insert a nop for NOTE_INSN_DELETED_DEBUG_LABEL
21256 notes only, instead set their CODE_LABEL_NUMBER to -1,
21257 otherwise there would be code generation differences
21258 in between -g and -g0. */
21259 if (NOTE_P (insn) && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL)
21260 deleted_debug_label = insn;
21261 insn = PREV_INSN (insn);
21263 if (insn
21264 && (LABEL_P (insn)
21265 || (NOTE_P (insn)
21266 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL)))
21267 fputs ("\tnop\n", file);
21268 else if (deleted_debug_label)
21269 for (insn = deleted_debug_label; insn; insn = NEXT_INSN (insn))
21270 if (NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL)
21271 CODE_LABEL_NUMBER (insn) = -1;
21273 #endif
21275 /* Output a traceback table here. See /usr/include/sys/debug.h for info
21276 on its format.
21278 We don't output a traceback table if -finhibit-size-directive was
21279 used. The documentation for -finhibit-size-directive reads
21280 ``don't output a @code{.size} assembler directive, or anything
21281 else that would cause trouble if the function is split in the
21282 middle, and the two halves are placed at locations far apart in
21283 memory.'' The traceback table has this property, since it
21284 includes the offset from the start of the function to the
21285 traceback table itself.
21287 System V.4 Powerpc's (and the embedded ABI derived from it) use a
21288 different traceback table. */
21289 if (DEFAULT_ABI == ABI_AIX && ! flag_inhibit_size_directive
21290 && rs6000_traceback != traceback_none && !cfun->is_thunk)
21292 const char *fname = NULL;
21293 const char *language_string = lang_hooks.name;
21294 int fixed_parms = 0, float_parms = 0, parm_info = 0;
21295 int i;
21296 int optional_tbtab;
21297 rs6000_stack_t *info = rs6000_stack_info ();
21299 if (rs6000_traceback == traceback_full)
21300 optional_tbtab = 1;
21301 else if (rs6000_traceback == traceback_part)
21302 optional_tbtab = 0;
21303 else
21304 optional_tbtab = !optimize_size && !TARGET_ELF;
21306 if (optional_tbtab)
21308 fname = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
21309 while (*fname == '.') /* V.4 encodes . in the name */
21310 fname++;
21312 /* Need label immediately before tbtab, so we can compute
21313 its offset from the function start. */
21314 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LT");
21315 ASM_OUTPUT_LABEL (file, fname);
21318 /* The .tbtab pseudo-op can only be used for the first eight
21319 expressions, since it can't handle the possibly variable
21320 length fields that follow. However, if you omit the optional
21321 fields, the assembler outputs zeros for all optional fields
21322 anyways, giving each variable length field is minimum length
21323 (as defined in sys/debug.h). Thus we can not use the .tbtab
21324 pseudo-op at all. */
21326 /* An all-zero word flags the start of the tbtab, for debuggers
21327 that have to find it by searching forward from the entry
21328 point or from the current pc. */
21329 fputs ("\t.long 0\n", file);
21331 /* Tbtab format type. Use format type 0. */
21332 fputs ("\t.byte 0,", file);
21334 /* Language type. Unfortunately, there does not seem to be any
21335 official way to discover the language being compiled, so we
21336 use language_string.
21337 C is 0. Fortran is 1. Pascal is 2. Ada is 3. C++ is 9.
21338 Java is 13. Objective-C is 14. Objective-C++ isn't assigned
21339 a number, so for now use 9. LTO and Go aren't assigned numbers
21340 either, so for now use 0. */
21341 if (! strcmp (language_string, "GNU C")
21342 || ! strcmp (language_string, "GNU GIMPLE")
21343 || ! strcmp (language_string, "GNU Go"))
21344 i = 0;
21345 else if (! strcmp (language_string, "GNU F77")
21346 || ! strcmp (language_string, "GNU Fortran"))
21347 i = 1;
21348 else if (! strcmp (language_string, "GNU Pascal"))
21349 i = 2;
21350 else if (! strcmp (language_string, "GNU Ada"))
21351 i = 3;
21352 else if (! strcmp (language_string, "GNU C++")
21353 || ! strcmp (language_string, "GNU Objective-C++"))
21354 i = 9;
21355 else if (! strcmp (language_string, "GNU Java"))
21356 i = 13;
21357 else if (! strcmp (language_string, "GNU Objective-C"))
21358 i = 14;
21359 else
21360 gcc_unreachable ();
21361 fprintf (file, "%d,", i);
21363 /* 8 single bit fields: global linkage (not set for C extern linkage,
21364 apparently a PL/I convention?), out-of-line epilogue/prologue, offset
21365 from start of procedure stored in tbtab, internal function, function
21366 has controlled storage, function has no toc, function uses fp,
21367 function logs/aborts fp operations. */
21368 /* Assume that fp operations are used if any fp reg must be saved. */
21369 fprintf (file, "%d,",
21370 (optional_tbtab << 5) | ((info->first_fp_reg_save != 64) << 1));
21372 /* 6 bitfields: function is interrupt handler, name present in
21373 proc table, function calls alloca, on condition directives
21374 (controls stack walks, 3 bits), saves condition reg, saves
21375 link reg. */
21376 /* The `function calls alloca' bit seems to be set whenever reg 31 is
21377 set up as a frame pointer, even when there is no alloca call. */
21378 fprintf (file, "%d,",
21379 ((optional_tbtab << 6)
21380 | ((optional_tbtab & frame_pointer_needed) << 5)
21381 | (info->cr_save_p << 1)
21382 | (info->lr_save_p)));
21384 /* 3 bitfields: saves backchain, fixup code, number of fpr saved
21385 (6 bits). */
21386 fprintf (file, "%d,",
21387 (info->push_p << 7) | (64 - info->first_fp_reg_save));
21389 /* 2 bitfields: spare bits (2 bits), number of gpr saved (6 bits). */
21390 fprintf (file, "%d,", (32 - first_reg_to_save ()));
21392 if (optional_tbtab)
21394 /* Compute the parameter info from the function decl argument
21395 list. */
21396 tree decl;
21397 int next_parm_info_bit = 31;
21399 for (decl = DECL_ARGUMENTS (current_function_decl);
21400 decl; decl = DECL_CHAIN (decl))
21402 rtx parameter = DECL_INCOMING_RTL (decl);
21403 enum machine_mode mode = GET_MODE (parameter);
21405 if (GET_CODE (parameter) == REG)
21407 if (SCALAR_FLOAT_MODE_P (mode))
21409 int bits;
21411 float_parms++;
21413 switch (mode)
21415 case SFmode:
21416 case SDmode:
21417 bits = 0x2;
21418 break;
21420 case DFmode:
21421 case DDmode:
21422 case TFmode:
21423 case TDmode:
21424 bits = 0x3;
21425 break;
21427 default:
21428 gcc_unreachable ();
21431 /* If only one bit will fit, don't or in this entry. */
21432 if (next_parm_info_bit > 0)
21433 parm_info |= (bits << (next_parm_info_bit - 1));
21434 next_parm_info_bit -= 2;
21436 else
21438 fixed_parms += ((GET_MODE_SIZE (mode)
21439 + (UNITS_PER_WORD - 1))
21440 / UNITS_PER_WORD);
21441 next_parm_info_bit -= 1;
21447 /* Number of fixed point parameters. */
21448 /* This is actually the number of words of fixed point parameters; thus
21449 an 8 byte struct counts as 2; and thus the maximum value is 8. */
21450 fprintf (file, "%d,", fixed_parms);
21452 /* 2 bitfields: number of floating point parameters (7 bits), parameters
21453 all on stack. */
21454 /* This is actually the number of fp registers that hold parameters;
21455 and thus the maximum value is 13. */
21456 /* Set parameters on stack bit if parameters are not in their original
21457 registers, regardless of whether they are on the stack? Xlc
21458 seems to set the bit when not optimizing. */
21459 fprintf (file, "%d\n", ((float_parms << 1) | (! optimize)));
21461 if (! optional_tbtab)
21462 return;
21464 /* Optional fields follow. Some are variable length. */
21466 /* Parameter types, left adjusted bit fields: 0 fixed, 10 single float,
21467 11 double float. */
21468 /* There is an entry for each parameter in a register, in the order that
21469 they occur in the parameter list. Any intervening arguments on the
21470 stack are ignored. If the list overflows a long (max possible length
21471 34 bits) then completely leave off all elements that don't fit. */
21472 /* Only emit this long if there was at least one parameter. */
21473 if (fixed_parms || float_parms)
21474 fprintf (file, "\t.long %d\n", parm_info);
21476 /* Offset from start of code to tb table. */
21477 fputs ("\t.long ", file);
21478 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LT");
21479 RS6000_OUTPUT_BASENAME (file, fname);
21480 putc ('-', file);
21481 rs6000_output_function_entry (file, fname);
21482 putc ('\n', file);
21484 /* Interrupt handler mask. */
21485 /* Omit this long, since we never set the interrupt handler bit
21486 above. */
21488 /* Number of CTL (controlled storage) anchors. */
21489 /* Omit this long, since the has_ctl bit is never set above. */
21491 /* Displacement into stack of each CTL anchor. */
21492 /* Omit this list of longs, because there are no CTL anchors. */
21494 /* Length of function name. */
21495 if (*fname == '*')
21496 ++fname;
21497 fprintf (file, "\t.short %d\n", (int) strlen (fname));
21499 /* Function name. */
21500 assemble_string (fname, strlen (fname));
21502 /* Register for alloca automatic storage; this is always reg 31.
21503 Only emit this if the alloca bit was set above. */
21504 if (frame_pointer_needed)
21505 fputs ("\t.byte 31\n", file);
21507 fputs ("\t.align 2\n", file);
21511 /* A C compound statement that outputs the assembler code for a thunk
21512 function, used to implement C++ virtual function calls with
21513 multiple inheritance. The thunk acts as a wrapper around a virtual
21514 function, adjusting the implicit object parameter before handing
21515 control off to the real function.
21517 First, emit code to add the integer DELTA to the location that
21518 contains the incoming first argument. Assume that this argument
21519 contains a pointer, and is the one used to pass the `this' pointer
21520 in C++. This is the incoming argument *before* the function
21521 prologue, e.g. `%o0' on a sparc. The addition must preserve the
21522 values of all other incoming arguments.
21524 After the addition, emit code to jump to FUNCTION, which is a
21525 `FUNCTION_DECL'. This is a direct pure jump, not a call, and does
21526 not touch the return address. Hence returning from FUNCTION will
21527 return to whoever called the current `thunk'.
21529 The effect must be as if FUNCTION had been called directly with the
21530 adjusted first argument. This macro is responsible for emitting
21531 all of the code for a thunk function; output_function_prologue()
21532 and output_function_epilogue() are not invoked.
21534 The THUNK_FNDECL is redundant. (DELTA and FUNCTION have already
21535 been extracted from it.) It might possibly be useful on some
21536 targets, but probably not.
21538 If you do not define this macro, the target-independent code in the
21539 C++ frontend will generate a less efficient heavyweight thunk that
21540 calls FUNCTION instead of jumping to it. The generic approach does
21541 not support varargs. */
21543 static void
21544 rs6000_output_mi_thunk (FILE *file, tree thunk_fndecl ATTRIBUTE_UNUSED,
21545 HOST_WIDE_INT delta, HOST_WIDE_INT vcall_offset,
21546 tree function)
21548 rtx this_rtx, insn, funexp;
21550 reload_completed = 1;
21551 epilogue_completed = 1;
21553 /* Mark the end of the (empty) prologue. */
21554 emit_note (NOTE_INSN_PROLOGUE_END);
21556 /* Find the "this" pointer. If the function returns a structure,
21557 the structure return pointer is in r3. */
21558 if (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function))
21559 this_rtx = gen_rtx_REG (Pmode, 4);
21560 else
21561 this_rtx = gen_rtx_REG (Pmode, 3);
21563 /* Apply the constant offset, if required. */
21564 if (delta)
21565 emit_insn (gen_add3_insn (this_rtx, this_rtx, GEN_INT (delta)));
21567 /* Apply the offset from the vtable, if required. */
21568 if (vcall_offset)
21570 rtx vcall_offset_rtx = GEN_INT (vcall_offset);
21571 rtx tmp = gen_rtx_REG (Pmode, 12);
21573 emit_move_insn (tmp, gen_rtx_MEM (Pmode, this_rtx));
21574 if (((unsigned HOST_WIDE_INT) vcall_offset) + 0x8000 >= 0x10000)
21576 emit_insn (gen_add3_insn (tmp, tmp, vcall_offset_rtx));
21577 emit_move_insn (tmp, gen_rtx_MEM (Pmode, tmp));
21579 else
21581 rtx loc = gen_rtx_PLUS (Pmode, tmp, vcall_offset_rtx);
21583 emit_move_insn (tmp, gen_rtx_MEM (Pmode, loc));
21585 emit_insn (gen_add3_insn (this_rtx, this_rtx, tmp));
21588 /* Generate a tail call to the target function. */
21589 if (!TREE_USED (function))
21591 assemble_external (function);
21592 TREE_USED (function) = 1;
21594 funexp = XEXP (DECL_RTL (function), 0);
21595 funexp = gen_rtx_MEM (FUNCTION_MODE, funexp);
21597 #if TARGET_MACHO
21598 if (MACHOPIC_INDIRECT)
21599 funexp = machopic_indirect_call_target (funexp);
21600 #endif
21602 /* gen_sibcall expects reload to convert scratch pseudo to LR so we must
21603 generate sibcall RTL explicitly. */
21604 insn = emit_call_insn (
21605 gen_rtx_PARALLEL (VOIDmode,
21606 gen_rtvec (4,
21607 gen_rtx_CALL (VOIDmode,
21608 funexp, const0_rtx),
21609 gen_rtx_USE (VOIDmode, const0_rtx),
21610 gen_rtx_USE (VOIDmode,
21611 gen_rtx_REG (SImode,
21612 LR_REGNO)),
21613 simple_return_rtx)));
21614 SIBLING_CALL_P (insn) = 1;
21615 emit_barrier ();
21617 /* Run just enough of rest_of_compilation to get the insns emitted.
21618 There's not really enough bulk here to make other passes such as
21619 instruction scheduling worth while. Note that use_thunk calls
21620 assemble_start_function and assemble_end_function. */
21621 insn = get_insns ();
21622 insn_locators_alloc ();
21623 shorten_branches (insn);
21624 final_start_function (insn, file, 1);
21625 final (insn, file, 1);
21626 final_end_function ();
21628 reload_completed = 0;
21629 epilogue_completed = 0;
21632 /* A quick summary of the various types of 'constant-pool tables'
21633 under PowerPC:
21635 Target Flags Name One table per
21636 AIX (none) AIX TOC object file
21637 AIX -mfull-toc AIX TOC object file
21638 AIX -mminimal-toc AIX minimal TOC translation unit
21639 SVR4/EABI (none) SVR4 SDATA object file
21640 SVR4/EABI -fpic SVR4 pic object file
21641 SVR4/EABI -fPIC SVR4 PIC translation unit
21642 SVR4/EABI -mrelocatable EABI TOC function
21643 SVR4/EABI -maix AIX TOC object file
21644 SVR4/EABI -maix -mminimal-toc
21645 AIX minimal TOC translation unit
21647 Name Reg. Set by entries contains:
21648 made by addrs? fp? sum?
21650 AIX TOC 2 crt0 as Y option option
21651 AIX minimal TOC 30 prolog gcc Y Y option
21652 SVR4 SDATA 13 crt0 gcc N Y N
21653 SVR4 pic 30 prolog ld Y not yet N
21654 SVR4 PIC 30 prolog gcc Y option option
21655 EABI TOC 30 prolog gcc Y option option
21659 /* Hash functions for the hash table. */
21661 static unsigned
21662 rs6000_hash_constant (rtx k)
21664 enum rtx_code code = GET_CODE (k);
21665 enum machine_mode mode = GET_MODE (k);
21666 unsigned result = (code << 3) ^ mode;
21667 const char *format;
21668 int flen, fidx;
21670 format = GET_RTX_FORMAT (code);
21671 flen = strlen (format);
21672 fidx = 0;
21674 switch (code)
21676 case LABEL_REF:
21677 return result * 1231 + (unsigned) INSN_UID (XEXP (k, 0));
21679 case CONST_DOUBLE:
21680 if (mode != VOIDmode)
21681 return real_hash (CONST_DOUBLE_REAL_VALUE (k)) * result;
21682 flen = 2;
21683 break;
21685 case CODE_LABEL:
21686 fidx = 3;
21687 break;
21689 default:
21690 break;
21693 for (; fidx < flen; fidx++)
21694 switch (format[fidx])
21696 case 's':
21698 unsigned i, len;
21699 const char *str = XSTR (k, fidx);
21700 len = strlen (str);
21701 result = result * 613 + len;
21702 for (i = 0; i < len; i++)
21703 result = result * 613 + (unsigned) str[i];
21704 break;
21706 case 'u':
21707 case 'e':
21708 result = result * 1231 + rs6000_hash_constant (XEXP (k, fidx));
21709 break;
21710 case 'i':
21711 case 'n':
21712 result = result * 613 + (unsigned) XINT (k, fidx);
21713 break;
21714 case 'w':
21715 if (sizeof (unsigned) >= sizeof (HOST_WIDE_INT))
21716 result = result * 613 + (unsigned) XWINT (k, fidx);
21717 else
21719 size_t i;
21720 for (i = 0; i < sizeof (HOST_WIDE_INT) / sizeof (unsigned); i++)
21721 result = result * 613 + (unsigned) (XWINT (k, fidx)
21722 >> CHAR_BIT * i);
21724 break;
21725 case '0':
21726 break;
21727 default:
21728 gcc_unreachable ();
21731 return result;
21734 static unsigned
21735 toc_hash_function (const void *hash_entry)
21737 const struct toc_hash_struct *thc =
21738 (const struct toc_hash_struct *) hash_entry;
21739 return rs6000_hash_constant (thc->key) ^ thc->key_mode;
21742 /* Compare H1 and H2 for equivalence. */
21744 static int
21745 toc_hash_eq (const void *h1, const void *h2)
21747 rtx r1 = ((const struct toc_hash_struct *) h1)->key;
21748 rtx r2 = ((const struct toc_hash_struct *) h2)->key;
21750 if (((const struct toc_hash_struct *) h1)->key_mode
21751 != ((const struct toc_hash_struct *) h2)->key_mode)
21752 return 0;
21754 return rtx_equal_p (r1, r2);
21757 /* These are the names given by the C++ front-end to vtables, and
21758 vtable-like objects. Ideally, this logic should not be here;
21759 instead, there should be some programmatic way of inquiring as
21760 to whether or not an object is a vtable. */
21762 #define VTABLE_NAME_P(NAME) \
21763 (strncmp ("_vt.", name, strlen ("_vt.")) == 0 \
21764 || strncmp ("_ZTV", name, strlen ("_ZTV")) == 0 \
21765 || strncmp ("_ZTT", name, strlen ("_ZTT")) == 0 \
21766 || strncmp ("_ZTI", name, strlen ("_ZTI")) == 0 \
21767 || strncmp ("_ZTC", name, strlen ("_ZTC")) == 0)
21769 #ifdef NO_DOLLAR_IN_LABEL
21770 /* Return a GGC-allocated character string translating dollar signs in
21771 input NAME to underscores. Used by XCOFF ASM_OUTPUT_LABELREF. */
21773 const char *
21774 rs6000_xcoff_strip_dollar (const char *name)
21776 char *strip, *p;
21777 const char *q;
21778 size_t len;
21780 q = (const char *) strchr (name, '$');
21782 if (q == 0 || q == name)
21783 return name;
21785 len = strlen (name);
21786 strip = XALLOCAVEC (char, len + 1);
21787 strcpy (strip, name);
21788 p = strip + (q - name);
21789 while (p)
21791 *p = '_';
21792 p = strchr (p + 1, '$');
21795 return ggc_alloc_string (strip, len);
21797 #endif
21799 void
21800 rs6000_output_symbol_ref (FILE *file, rtx x)
21802 /* Currently C++ toc references to vtables can be emitted before it
21803 is decided whether the vtable is public or private. If this is
21804 the case, then the linker will eventually complain that there is
21805 a reference to an unknown section. Thus, for vtables only,
21806 we emit the TOC reference to reference the symbol and not the
21807 section. */
21808 const char *name = XSTR (x, 0);
21810 if (VTABLE_NAME_P (name))
21812 RS6000_OUTPUT_BASENAME (file, name);
21814 else
21815 assemble_name (file, name);
21818 /* Output a TOC entry. We derive the entry name from what is being
21819 written. */
21821 void
21822 output_toc (FILE *file, rtx x, int labelno, enum machine_mode mode)
21824 char buf[256];
21825 const char *name = buf;
21826 rtx base = x;
21827 HOST_WIDE_INT offset = 0;
21829 gcc_assert (!TARGET_NO_TOC);
21831 /* When the linker won't eliminate them, don't output duplicate
21832 TOC entries (this happens on AIX if there is any kind of TOC,
21833 and on SVR4 under -fPIC or -mrelocatable). Don't do this for
21834 CODE_LABELs. */
21835 if (TARGET_TOC && GET_CODE (x) != LABEL_REF)
21837 struct toc_hash_struct *h;
21838 void * * found;
21840 /* Create toc_hash_table. This can't be done at TARGET_OPTION_OVERRIDE
21841 time because GGC is not initialized at that point. */
21842 if (toc_hash_table == NULL)
21843 toc_hash_table = htab_create_ggc (1021, toc_hash_function,
21844 toc_hash_eq, NULL);
21846 h = ggc_alloc_toc_hash_struct ();
21847 h->key = x;
21848 h->key_mode = mode;
21849 h->labelno = labelno;
21851 found = htab_find_slot (toc_hash_table, h, INSERT);
21852 if (*found == NULL)
21853 *found = h;
21854 else /* This is indeed a duplicate.
21855 Set this label equal to that label. */
21857 fputs ("\t.set ", file);
21858 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LC");
21859 fprintf (file, "%d,", labelno);
21860 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LC");
21861 fprintf (file, "%d\n", ((*(const struct toc_hash_struct **)
21862 found)->labelno));
21863 return;
21867 /* If we're going to put a double constant in the TOC, make sure it's
21868 aligned properly when strict alignment is on. */
21869 if (GET_CODE (x) == CONST_DOUBLE
21870 && STRICT_ALIGNMENT
21871 && GET_MODE_BITSIZE (mode) >= 64
21872 && ! (TARGET_NO_FP_IN_TOC && ! TARGET_MINIMAL_TOC)) {
21873 ASM_OUTPUT_ALIGN (file, 3);
21876 (*targetm.asm_out.internal_label) (file, "LC", labelno);
21878 /* Handle FP constants specially. Note that if we have a minimal
21879 TOC, things we put here aren't actually in the TOC, so we can allow
21880 FP constants. */
21881 if (GET_CODE (x) == CONST_DOUBLE &&
21882 (GET_MODE (x) == TFmode || GET_MODE (x) == TDmode))
21884 REAL_VALUE_TYPE rv;
21885 long k[4];
21887 REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
21888 if (DECIMAL_FLOAT_MODE_P (GET_MODE (x)))
21889 REAL_VALUE_TO_TARGET_DECIMAL128 (rv, k);
21890 else
21891 REAL_VALUE_TO_TARGET_LONG_DOUBLE (rv, k);
21893 if (TARGET_64BIT)
21895 if (TARGET_MINIMAL_TOC)
21896 fputs (DOUBLE_INT_ASM_OP, file);
21897 else
21898 fprintf (file, "\t.tc FT_%lx_%lx_%lx_%lx[TC],",
21899 k[0] & 0xffffffff, k[1] & 0xffffffff,
21900 k[2] & 0xffffffff, k[3] & 0xffffffff);
21901 fprintf (file, "0x%lx%08lx,0x%lx%08lx\n",
21902 k[0] & 0xffffffff, k[1] & 0xffffffff,
21903 k[2] & 0xffffffff, k[3] & 0xffffffff);
21904 return;
21906 else
21908 if (TARGET_MINIMAL_TOC)
21909 fputs ("\t.long ", file);
21910 else
21911 fprintf (file, "\t.tc FT_%lx_%lx_%lx_%lx[TC],",
21912 k[0] & 0xffffffff, k[1] & 0xffffffff,
21913 k[2] & 0xffffffff, k[3] & 0xffffffff);
21914 fprintf (file, "0x%lx,0x%lx,0x%lx,0x%lx\n",
21915 k[0] & 0xffffffff, k[1] & 0xffffffff,
21916 k[2] & 0xffffffff, k[3] & 0xffffffff);
21917 return;
21920 else if (GET_CODE (x) == CONST_DOUBLE &&
21921 (GET_MODE (x) == DFmode || GET_MODE (x) == DDmode))
21923 REAL_VALUE_TYPE rv;
21924 long k[2];
21926 REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
21928 if (DECIMAL_FLOAT_MODE_P (GET_MODE (x)))
21929 REAL_VALUE_TO_TARGET_DECIMAL64 (rv, k);
21930 else
21931 REAL_VALUE_TO_TARGET_DOUBLE (rv, k);
21933 if (TARGET_64BIT)
21935 if (TARGET_MINIMAL_TOC)
21936 fputs (DOUBLE_INT_ASM_OP, file);
21937 else
21938 fprintf (file, "\t.tc FD_%lx_%lx[TC],",
21939 k[0] & 0xffffffff, k[1] & 0xffffffff);
21940 fprintf (file, "0x%lx%08lx\n",
21941 k[0] & 0xffffffff, k[1] & 0xffffffff);
21942 return;
21944 else
21946 if (TARGET_MINIMAL_TOC)
21947 fputs ("\t.long ", file);
21948 else
21949 fprintf (file, "\t.tc FD_%lx_%lx[TC],",
21950 k[0] & 0xffffffff, k[1] & 0xffffffff);
21951 fprintf (file, "0x%lx,0x%lx\n",
21952 k[0] & 0xffffffff, k[1] & 0xffffffff);
21953 return;
21956 else if (GET_CODE (x) == CONST_DOUBLE &&
21957 (GET_MODE (x) == SFmode || GET_MODE (x) == SDmode))
21959 REAL_VALUE_TYPE rv;
21960 long l;
21962 REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
21963 if (DECIMAL_FLOAT_MODE_P (GET_MODE (x)))
21964 REAL_VALUE_TO_TARGET_DECIMAL32 (rv, l);
21965 else
21966 REAL_VALUE_TO_TARGET_SINGLE (rv, l);
21968 if (TARGET_64BIT)
21970 if (TARGET_MINIMAL_TOC)
21971 fputs (DOUBLE_INT_ASM_OP, file);
21972 else
21973 fprintf (file, "\t.tc FS_%lx[TC],", l & 0xffffffff);
21974 fprintf (file, "0x%lx00000000\n", l & 0xffffffff);
21975 return;
21977 else
21979 if (TARGET_MINIMAL_TOC)
21980 fputs ("\t.long ", file);
21981 else
21982 fprintf (file, "\t.tc FS_%lx[TC],", l & 0xffffffff);
21983 fprintf (file, "0x%lx\n", l & 0xffffffff);
21984 return;
21987 else if (GET_MODE (x) == VOIDmode
21988 && (GET_CODE (x) == CONST_INT || GET_CODE (x) == CONST_DOUBLE))
21990 unsigned HOST_WIDE_INT low;
21991 HOST_WIDE_INT high;
21993 if (GET_CODE (x) == CONST_DOUBLE)
21995 low = CONST_DOUBLE_LOW (x);
21996 high = CONST_DOUBLE_HIGH (x);
21998 else
21999 #if HOST_BITS_PER_WIDE_INT == 32
22001 low = INTVAL (x);
22002 high = (low & 0x80000000) ? ~0 : 0;
22004 #else
22006 low = INTVAL (x) & 0xffffffff;
22007 high = (HOST_WIDE_INT) INTVAL (x) >> 32;
22009 #endif
22011 /* TOC entries are always Pmode-sized, but since this
22012 is a bigendian machine then if we're putting smaller
22013 integer constants in the TOC we have to pad them.
22014 (This is still a win over putting the constants in
22015 a separate constant pool, because then we'd have
22016 to have both a TOC entry _and_ the actual constant.)
22018 For a 32-bit target, CONST_INT values are loaded and shifted
22019 entirely within `low' and can be stored in one TOC entry. */
22021 /* It would be easy to make this work, but it doesn't now. */
22022 gcc_assert (!TARGET_64BIT || POINTER_SIZE >= GET_MODE_BITSIZE (mode));
22024 if (POINTER_SIZE > GET_MODE_BITSIZE (mode))
22026 #if HOST_BITS_PER_WIDE_INT == 32
22027 lshift_double (low, high, POINTER_SIZE - GET_MODE_BITSIZE (mode),
22028 POINTER_SIZE, &low, &high, 0);
22029 #else
22030 low |= high << 32;
22031 low <<= POINTER_SIZE - GET_MODE_BITSIZE (mode);
22032 high = (HOST_WIDE_INT) low >> 32;
22033 low &= 0xffffffff;
22034 #endif
22037 if (TARGET_64BIT)
22039 if (TARGET_MINIMAL_TOC)
22040 fputs (DOUBLE_INT_ASM_OP, file);
22041 else
22042 fprintf (file, "\t.tc ID_%lx_%lx[TC],",
22043 (long) high & 0xffffffff, (long) low & 0xffffffff);
22044 fprintf (file, "0x%lx%08lx\n",
22045 (long) high & 0xffffffff, (long) low & 0xffffffff);
22046 return;
22048 else
22050 if (POINTER_SIZE < GET_MODE_BITSIZE (mode))
22052 if (TARGET_MINIMAL_TOC)
22053 fputs ("\t.long ", file);
22054 else
22055 fprintf (file, "\t.tc ID_%lx_%lx[TC],",
22056 (long) high & 0xffffffff, (long) low & 0xffffffff);
22057 fprintf (file, "0x%lx,0x%lx\n",
22058 (long) high & 0xffffffff, (long) low & 0xffffffff);
22060 else
22062 if (TARGET_MINIMAL_TOC)
22063 fputs ("\t.long ", file);
22064 else
22065 fprintf (file, "\t.tc IS_%lx[TC],", (long) low & 0xffffffff);
22066 fprintf (file, "0x%lx\n", (long) low & 0xffffffff);
22068 return;
22072 if (GET_CODE (x) == CONST)
22074 gcc_assert (GET_CODE (XEXP (x, 0)) == PLUS
22075 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT);
22077 base = XEXP (XEXP (x, 0), 0);
22078 offset = INTVAL (XEXP (XEXP (x, 0), 1));
22081 switch (GET_CODE (base))
22083 case SYMBOL_REF:
22084 name = XSTR (base, 0);
22085 break;
22087 case LABEL_REF:
22088 ASM_GENERATE_INTERNAL_LABEL (buf, "L",
22089 CODE_LABEL_NUMBER (XEXP (base, 0)));
22090 break;
22092 case CODE_LABEL:
22093 ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (base));
22094 break;
22096 default:
22097 gcc_unreachable ();
22100 if (TARGET_MINIMAL_TOC)
22101 fputs (TARGET_32BIT ? "\t.long " : DOUBLE_INT_ASM_OP, file);
22102 else
22104 fputs ("\t.tc ", file);
22105 RS6000_OUTPUT_BASENAME (file, name);
22107 if (offset < 0)
22108 fprintf (file, ".N" HOST_WIDE_INT_PRINT_UNSIGNED, - offset);
22109 else if (offset)
22110 fprintf (file, ".P" HOST_WIDE_INT_PRINT_UNSIGNED, offset);
22112 fputs ("[TC],", file);
22115 /* Currently C++ toc references to vtables can be emitted before it
22116 is decided whether the vtable is public or private. If this is
22117 the case, then the linker will eventually complain that there is
22118 a TOC reference to an unknown section. Thus, for vtables only,
22119 we emit the TOC reference to reference the symbol and not the
22120 section. */
22121 if (VTABLE_NAME_P (name))
22123 RS6000_OUTPUT_BASENAME (file, name);
22124 if (offset < 0)
22125 fprintf (file, HOST_WIDE_INT_PRINT_DEC, offset);
22126 else if (offset > 0)
22127 fprintf (file, "+" HOST_WIDE_INT_PRINT_DEC, offset);
22129 else
22130 output_addr_const (file, x);
22131 putc ('\n', file);
22134 /* Output an assembler pseudo-op to write an ASCII string of N characters
22135 starting at P to FILE.
22137 On the RS/6000, we have to do this using the .byte operation and
22138 write out special characters outside the quoted string.
22139 Also, the assembler is broken; very long strings are truncated,
22140 so we must artificially break them up early. */
22142 void
22143 output_ascii (FILE *file, const char *p, int n)
22145 char c;
22146 int i, count_string;
22147 const char *for_string = "\t.byte \"";
22148 const char *for_decimal = "\t.byte ";
22149 const char *to_close = NULL;
22151 count_string = 0;
22152 for (i = 0; i < n; i++)
22154 c = *p++;
22155 if (c >= ' ' && c < 0177)
22157 if (for_string)
22158 fputs (for_string, file);
22159 putc (c, file);
22161 /* Write two quotes to get one. */
22162 if (c == '"')
22164 putc (c, file);
22165 ++count_string;
22168 for_string = NULL;
22169 for_decimal = "\"\n\t.byte ";
22170 to_close = "\"\n";
22171 ++count_string;
22173 if (count_string >= 512)
22175 fputs (to_close, file);
22177 for_string = "\t.byte \"";
22178 for_decimal = "\t.byte ";
22179 to_close = NULL;
22180 count_string = 0;
22183 else
22185 if (for_decimal)
22186 fputs (for_decimal, file);
22187 fprintf (file, "%d", c);
22189 for_string = "\n\t.byte \"";
22190 for_decimal = ", ";
22191 to_close = "\n";
22192 count_string = 0;
22196 /* Now close the string if we have written one. Then end the line. */
22197 if (to_close)
22198 fputs (to_close, file);
22201 /* Generate a unique section name for FILENAME for a section type
22202 represented by SECTION_DESC. Output goes into BUF.
22204 SECTION_DESC can be any string, as long as it is different for each
22205 possible section type.
22207 We name the section in the same manner as xlc. The name begins with an
22208 underscore followed by the filename (after stripping any leading directory
22209 names) with the last period replaced by the string SECTION_DESC. If
22210 FILENAME does not contain a period, SECTION_DESC is appended to the end of
22211 the name. */
22213 void
22214 rs6000_gen_section_name (char **buf, const char *filename,
22215 const char *section_desc)
22217 const char *q, *after_last_slash, *last_period = 0;
22218 char *p;
22219 int len;
22221 after_last_slash = filename;
22222 for (q = filename; *q; q++)
22224 if (*q == '/')
22225 after_last_slash = q + 1;
22226 else if (*q == '.')
22227 last_period = q;
22230 len = strlen (after_last_slash) + strlen (section_desc) + 2;
22231 *buf = (char *) xmalloc (len);
22233 p = *buf;
22234 *p++ = '_';
22236 for (q = after_last_slash; *q; q++)
22238 if (q == last_period)
22240 strcpy (p, section_desc);
22241 p += strlen (section_desc);
22242 break;
22245 else if (ISALNUM (*q))
22246 *p++ = *q;
22249 if (last_period == 0)
22250 strcpy (p, section_desc);
22251 else
22252 *p = '\0';
22255 /* Emit profile function. */
22257 void
22258 output_profile_hook (int labelno ATTRIBUTE_UNUSED)
22260 /* Non-standard profiling for kernels, which just saves LR then calls
22261 _mcount without worrying about arg saves. The idea is to change
22262 the function prologue as little as possible as it isn't easy to
22263 account for arg save/restore code added just for _mcount. */
22264 if (TARGET_PROFILE_KERNEL)
22265 return;
22267 if (DEFAULT_ABI == ABI_AIX)
22269 #ifndef NO_PROFILE_COUNTERS
22270 # define NO_PROFILE_COUNTERS 0
22271 #endif
22272 if (NO_PROFILE_COUNTERS)
22273 emit_library_call (init_one_libfunc (RS6000_MCOUNT),
22274 LCT_NORMAL, VOIDmode, 0);
22275 else
22277 char buf[30];
22278 const char *label_name;
22279 rtx fun;
22281 ASM_GENERATE_INTERNAL_LABEL (buf, "LP", labelno);
22282 label_name = ggc_strdup ((*targetm.strip_name_encoding) (buf));
22283 fun = gen_rtx_SYMBOL_REF (Pmode, label_name);
22285 emit_library_call (init_one_libfunc (RS6000_MCOUNT),
22286 LCT_NORMAL, VOIDmode, 1, fun, Pmode);
22289 else if (DEFAULT_ABI == ABI_DARWIN)
22291 const char *mcount_name = RS6000_MCOUNT;
22292 int caller_addr_regno = LR_REGNO;
22294 /* Be conservative and always set this, at least for now. */
22295 crtl->uses_pic_offset_table = 1;
22297 #if TARGET_MACHO
22298 /* For PIC code, set up a stub and collect the caller's address
22299 from r0, which is where the prologue puts it. */
22300 if (MACHOPIC_INDIRECT
22301 && crtl->uses_pic_offset_table)
22302 caller_addr_regno = 0;
22303 #endif
22304 emit_library_call (gen_rtx_SYMBOL_REF (Pmode, mcount_name),
22305 LCT_NORMAL, VOIDmode, 1,
22306 gen_rtx_REG (Pmode, caller_addr_regno), Pmode);
22310 /* Write function profiler code. */
22312 void
22313 output_function_profiler (FILE *file, int labelno)
22315 char buf[100];
22317 switch (DEFAULT_ABI)
22319 default:
22320 gcc_unreachable ();
22322 case ABI_V4:
22323 if (!TARGET_32BIT)
22325 warning (0, "no profiling of 64-bit code for this ABI");
22326 return;
22328 ASM_GENERATE_INTERNAL_LABEL (buf, "LP", labelno);
22329 fprintf (file, "\tmflr %s\n", reg_names[0]);
22330 if (NO_PROFILE_COUNTERS)
22332 asm_fprintf (file, "\t{st|stw} %s,4(%s)\n",
22333 reg_names[0], reg_names[1]);
22335 else if (TARGET_SECURE_PLT && flag_pic)
22337 if (TARGET_LINK_STACK)
22339 char name[32];
22340 get_ppc476_thunk_name (name);
22341 asm_fprintf (file, "\tbl %s\n", name);
22343 else
22344 asm_fprintf (file, "\tbcl 20,31,1f\n1:\n");
22345 asm_fprintf (file, "\t{st|stw} %s,4(%s)\n",
22346 reg_names[0], reg_names[1]);
22347 asm_fprintf (file, "\tmflr %s\n", reg_names[12]);
22348 asm_fprintf (file, "\t{cau|addis} %s,%s,",
22349 reg_names[12], reg_names[12]);
22350 assemble_name (file, buf);
22351 asm_fprintf (file, "-1b@ha\n\t{cal|la} %s,", reg_names[0]);
22352 assemble_name (file, buf);
22353 asm_fprintf (file, "-1b@l(%s)\n", reg_names[12]);
22355 else if (flag_pic == 1)
22357 fputs ("\tbl _GLOBAL_OFFSET_TABLE_@local-4\n", file);
22358 asm_fprintf (file, "\t{st|stw} %s,4(%s)\n",
22359 reg_names[0], reg_names[1]);
22360 asm_fprintf (file, "\tmflr %s\n", reg_names[12]);
22361 asm_fprintf (file, "\t{l|lwz} %s,", reg_names[0]);
22362 assemble_name (file, buf);
22363 asm_fprintf (file, "@got(%s)\n", reg_names[12]);
22365 else if (flag_pic > 1)
22367 asm_fprintf (file, "\t{st|stw} %s,4(%s)\n",
22368 reg_names[0], reg_names[1]);
22369 /* Now, we need to get the address of the label. */
22370 if (TARGET_LINK_STACK)
22372 char name[32];
22373 get_ppc476_thunk_name (name);
22374 asm_fprintf (file, "\tbl %s\n\tb 1f\n\t.long ", name);
22375 assemble_name (file, buf);
22376 fputs ("-.\n1:", file);
22377 asm_fprintf (file, "\tmflr %s\n", reg_names[11]);
22378 asm_fprintf (file, "\taddi %s,%s,4\n",
22379 reg_names[11], reg_names[11]);
22381 else
22383 fputs ("\tbcl 20,31,1f\n\t.long ", file);
22384 assemble_name (file, buf);
22385 fputs ("-.\n1:", file);
22386 asm_fprintf (file, "\tmflr %s\n", reg_names[11]);
22388 asm_fprintf (file, "\t{l|lwz} %s,0(%s)\n",
22389 reg_names[0], reg_names[11]);
22390 asm_fprintf (file, "\t{cax|add} %s,%s,%s\n",
22391 reg_names[0], reg_names[0], reg_names[11]);
22393 else
22395 asm_fprintf (file, "\t{liu|lis} %s,", reg_names[12]);
22396 assemble_name (file, buf);
22397 fputs ("@ha\n", file);
22398 asm_fprintf (file, "\t{st|stw} %s,4(%s)\n",
22399 reg_names[0], reg_names[1]);
22400 asm_fprintf (file, "\t{cal|la} %s,", reg_names[0]);
22401 assemble_name (file, buf);
22402 asm_fprintf (file, "@l(%s)\n", reg_names[12]);
22405 /* ABI_V4 saves the static chain reg with ASM_OUTPUT_REG_PUSH. */
22406 fprintf (file, "\tbl %s%s\n",
22407 RS6000_MCOUNT, flag_pic ? "@plt" : "");
22408 break;
22410 case ABI_AIX:
22411 case ABI_DARWIN:
22412 if (!TARGET_PROFILE_KERNEL)
22414 /* Don't do anything, done in output_profile_hook (). */
22416 else
22418 gcc_assert (!TARGET_32BIT);
22420 asm_fprintf (file, "\tmflr %s\n", reg_names[0]);
22421 asm_fprintf (file, "\tstd %s,16(%s)\n", reg_names[0], reg_names[1]);
22423 if (cfun->static_chain_decl != NULL)
22425 asm_fprintf (file, "\tstd %s,24(%s)\n",
22426 reg_names[STATIC_CHAIN_REGNUM], reg_names[1]);
22427 fprintf (file, "\tbl %s\n", RS6000_MCOUNT);
22428 asm_fprintf (file, "\tld %s,24(%s)\n",
22429 reg_names[STATIC_CHAIN_REGNUM], reg_names[1]);
22431 else
22432 fprintf (file, "\tbl %s\n", RS6000_MCOUNT);
22434 break;
22440 /* The following variable value is the last issued insn. */
22442 static rtx last_scheduled_insn;
22444 /* The following variable helps to balance issuing of load and
22445 store instructions */
22447 static int load_store_pendulum;
22449 /* Power4 load update and store update instructions are cracked into a
22450 load or store and an integer insn which are executed in the same cycle.
22451 Branches have their own dispatch slot which does not count against the
22452 GCC issue rate, but it changes the program flow so there are no other
22453 instructions to issue in this cycle. */
22455 static int
22456 rs6000_variable_issue_1 (rtx insn, int more)
22458 last_scheduled_insn = insn;
22459 if (GET_CODE (PATTERN (insn)) == USE
22460 || GET_CODE (PATTERN (insn)) == CLOBBER)
22462 cached_can_issue_more = more;
22463 return cached_can_issue_more;
22466 if (insn_terminates_group_p (insn, current_group))
22468 cached_can_issue_more = 0;
22469 return cached_can_issue_more;
22472 /* If no reservation, but reach here */
22473 if (recog_memoized (insn) < 0)
22474 return more;
22476 if (rs6000_sched_groups)
22478 if (is_microcoded_insn (insn))
22479 cached_can_issue_more = 0;
22480 else if (is_cracked_insn (insn))
22481 cached_can_issue_more = more > 2 ? more - 2 : 0;
22482 else
22483 cached_can_issue_more = more - 1;
22485 return cached_can_issue_more;
22488 if (rs6000_cpu_attr == CPU_CELL && is_nonpipeline_insn (insn))
22489 return 0;
22491 cached_can_issue_more = more - 1;
22492 return cached_can_issue_more;
22495 static int
22496 rs6000_variable_issue (FILE *stream, int verbose, rtx insn, int more)
22498 int r = rs6000_variable_issue_1 (insn, more);
22499 if (verbose)
22500 fprintf (stream, "// rs6000_variable_issue (more = %d) = %d\n", more, r);
22501 return r;
22504 /* Adjust the cost of a scheduling dependency. Return the new cost of
22505 a dependency LINK or INSN on DEP_INSN. COST is the current cost. */
22507 static int
22508 rs6000_adjust_cost (rtx insn, rtx link, rtx dep_insn, int cost)
22510 enum attr_type attr_type;
22512 if (! recog_memoized (insn))
22513 return 0;
22515 switch (REG_NOTE_KIND (link))
22517 case REG_DEP_TRUE:
22519 /* Data dependency; DEP_INSN writes a register that INSN reads
22520 some cycles later. */
22522 /* Separate a load from a narrower, dependent store. */
22523 if (rs6000_sched_groups
22524 && GET_CODE (PATTERN (insn)) == SET
22525 && GET_CODE (PATTERN (dep_insn)) == SET
22526 && GET_CODE (XEXP (PATTERN (insn), 1)) == MEM
22527 && GET_CODE (XEXP (PATTERN (dep_insn), 0)) == MEM
22528 && (GET_MODE_SIZE (GET_MODE (XEXP (PATTERN (insn), 1)))
22529 > GET_MODE_SIZE (GET_MODE (XEXP (PATTERN (dep_insn), 0)))))
22530 return cost + 14;
22532 attr_type = get_attr_type (insn);
22534 switch (attr_type)
22536 case TYPE_JMPREG:
22537 /* Tell the first scheduling pass about the latency between
22538 a mtctr and bctr (and mtlr and br/blr). The first
22539 scheduling pass will not know about this latency since
22540 the mtctr instruction, which has the latency associated
22541 to it, will be generated by reload. */
22542 return TARGET_POWER ? 5 : 4;
22543 case TYPE_BRANCH:
22544 /* Leave some extra cycles between a compare and its
22545 dependent branch, to inhibit expensive mispredicts. */
22546 if ((rs6000_cpu_attr == CPU_PPC603
22547 || rs6000_cpu_attr == CPU_PPC604
22548 || rs6000_cpu_attr == CPU_PPC604E
22549 || rs6000_cpu_attr == CPU_PPC620
22550 || rs6000_cpu_attr == CPU_PPC630
22551 || rs6000_cpu_attr == CPU_PPC750
22552 || rs6000_cpu_attr == CPU_PPC7400
22553 || rs6000_cpu_attr == CPU_PPC7450
22554 || rs6000_cpu_attr == CPU_PPCE5500
22555 || rs6000_cpu_attr == CPU_PPCE6500
22556 || rs6000_cpu_attr == CPU_POWER4
22557 || rs6000_cpu_attr == CPU_POWER5
22558 || rs6000_cpu_attr == CPU_POWER7
22559 || rs6000_cpu_attr == CPU_CELL)
22560 && recog_memoized (dep_insn)
22561 && (INSN_CODE (dep_insn) >= 0))
22563 switch (get_attr_type (dep_insn))
22565 case TYPE_CMP:
22566 case TYPE_COMPARE:
22567 case TYPE_DELAYED_COMPARE:
22568 case TYPE_IMUL_COMPARE:
22569 case TYPE_LMUL_COMPARE:
22570 case TYPE_FPCOMPARE:
22571 case TYPE_CR_LOGICAL:
22572 case TYPE_DELAYED_CR:
22573 return cost + 2;
22574 default:
22575 break;
22577 break;
22579 case TYPE_STORE:
22580 case TYPE_STORE_U:
22581 case TYPE_STORE_UX:
22582 case TYPE_FPSTORE:
22583 case TYPE_FPSTORE_U:
22584 case TYPE_FPSTORE_UX:
22585 if ((rs6000_cpu == PROCESSOR_POWER6)
22586 && recog_memoized (dep_insn)
22587 && (INSN_CODE (dep_insn) >= 0))
22590 if (GET_CODE (PATTERN (insn)) != SET)
22591 /* If this happens, we have to extend this to schedule
22592 optimally. Return default for now. */
22593 return cost;
22595 /* Adjust the cost for the case where the value written
22596 by a fixed point operation is used as the address
22597 gen value on a store. */
22598 switch (get_attr_type (dep_insn))
22600 case TYPE_LOAD:
22601 case TYPE_LOAD_U:
22602 case TYPE_LOAD_UX:
22603 case TYPE_CNTLZ:
22605 if (! store_data_bypass_p (dep_insn, insn))
22606 return 4;
22607 break;
22609 case TYPE_LOAD_EXT:
22610 case TYPE_LOAD_EXT_U:
22611 case TYPE_LOAD_EXT_UX:
22612 case TYPE_VAR_SHIFT_ROTATE:
22613 case TYPE_VAR_DELAYED_COMPARE:
22615 if (! store_data_bypass_p (dep_insn, insn))
22616 return 6;
22617 break;
22619 case TYPE_INTEGER:
22620 case TYPE_COMPARE:
22621 case TYPE_FAST_COMPARE:
22622 case TYPE_EXTS:
22623 case TYPE_SHIFT:
22624 case TYPE_INSERT_WORD:
22625 case TYPE_INSERT_DWORD:
22626 case TYPE_FPLOAD_U:
22627 case TYPE_FPLOAD_UX:
22628 case TYPE_STORE_U:
22629 case TYPE_STORE_UX:
22630 case TYPE_FPSTORE_U:
22631 case TYPE_FPSTORE_UX:
22633 if (! store_data_bypass_p (dep_insn, insn))
22634 return 3;
22635 break;
22637 case TYPE_IMUL:
22638 case TYPE_IMUL2:
22639 case TYPE_IMUL3:
22640 case TYPE_LMUL:
22641 case TYPE_IMUL_COMPARE:
22642 case TYPE_LMUL_COMPARE:
22644 if (! store_data_bypass_p (dep_insn, insn))
22645 return 17;
22646 break;
22648 case TYPE_IDIV:
22650 if (! store_data_bypass_p (dep_insn, insn))
22651 return 45;
22652 break;
22654 case TYPE_LDIV:
22656 if (! store_data_bypass_p (dep_insn, insn))
22657 return 57;
22658 break;
22660 default:
22661 break;
22664 break;
22666 case TYPE_LOAD:
22667 case TYPE_LOAD_U:
22668 case TYPE_LOAD_UX:
22669 case TYPE_LOAD_EXT:
22670 case TYPE_LOAD_EXT_U:
22671 case TYPE_LOAD_EXT_UX:
22672 if ((rs6000_cpu == PROCESSOR_POWER6)
22673 && recog_memoized (dep_insn)
22674 && (INSN_CODE (dep_insn) >= 0))
22677 /* Adjust the cost for the case where the value written
22678 by a fixed point instruction is used within the address
22679 gen portion of a subsequent load(u)(x) */
22680 switch (get_attr_type (dep_insn))
22682 case TYPE_LOAD:
22683 case TYPE_LOAD_U:
22684 case TYPE_LOAD_UX:
22685 case TYPE_CNTLZ:
22687 if (set_to_load_agen (dep_insn, insn))
22688 return 4;
22689 break;
22691 case TYPE_LOAD_EXT:
22692 case TYPE_LOAD_EXT_U:
22693 case TYPE_LOAD_EXT_UX:
22694 case TYPE_VAR_SHIFT_ROTATE:
22695 case TYPE_VAR_DELAYED_COMPARE:
22697 if (set_to_load_agen (dep_insn, insn))
22698 return 6;
22699 break;
22701 case TYPE_INTEGER:
22702 case TYPE_COMPARE:
22703 case TYPE_FAST_COMPARE:
22704 case TYPE_EXTS:
22705 case TYPE_SHIFT:
22706 case TYPE_INSERT_WORD:
22707 case TYPE_INSERT_DWORD:
22708 case TYPE_FPLOAD_U:
22709 case TYPE_FPLOAD_UX:
22710 case TYPE_STORE_U:
22711 case TYPE_STORE_UX:
22712 case TYPE_FPSTORE_U:
22713 case TYPE_FPSTORE_UX:
22715 if (set_to_load_agen (dep_insn, insn))
22716 return 3;
22717 break;
22719 case TYPE_IMUL:
22720 case TYPE_IMUL2:
22721 case TYPE_IMUL3:
22722 case TYPE_LMUL:
22723 case TYPE_IMUL_COMPARE:
22724 case TYPE_LMUL_COMPARE:
22726 if (set_to_load_agen (dep_insn, insn))
22727 return 17;
22728 break;
22730 case TYPE_IDIV:
22732 if (set_to_load_agen (dep_insn, insn))
22733 return 45;
22734 break;
22736 case TYPE_LDIV:
22738 if (set_to_load_agen (dep_insn, insn))
22739 return 57;
22740 break;
22742 default:
22743 break;
22746 break;
22748 case TYPE_FPLOAD:
22749 if ((rs6000_cpu == PROCESSOR_POWER6)
22750 && recog_memoized (dep_insn)
22751 && (INSN_CODE (dep_insn) >= 0)
22752 && (get_attr_type (dep_insn) == TYPE_MFFGPR))
22753 return 2;
22755 default:
22756 break;
22759 /* Fall out to return default cost. */
22761 break;
22763 case REG_DEP_OUTPUT:
22764 /* Output dependency; DEP_INSN writes a register that INSN writes some
22765 cycles later. */
22766 if ((rs6000_cpu == PROCESSOR_POWER6)
22767 && recog_memoized (dep_insn)
22768 && (INSN_CODE (dep_insn) >= 0))
22770 attr_type = get_attr_type (insn);
22772 switch (attr_type)
22774 case TYPE_FP:
22775 if (get_attr_type (dep_insn) == TYPE_FP)
22776 return 1;
22777 break;
22778 case TYPE_FPLOAD:
22779 if (get_attr_type (dep_insn) == TYPE_MFFGPR)
22780 return 2;
22781 break;
22782 default:
22783 break;
22786 case REG_DEP_ANTI:
22787 /* Anti dependency; DEP_INSN reads a register that INSN writes some
22788 cycles later. */
22789 return 0;
22791 default:
22792 gcc_unreachable ();
22795 return cost;
22798 /* Debug version of rs6000_adjust_cost. */
22800 static int
22801 rs6000_debug_adjust_cost (rtx insn, rtx link, rtx dep_insn, int cost)
22803 int ret = rs6000_adjust_cost (insn, link, dep_insn, cost);
22805 if (ret != cost)
22807 const char *dep;
22809 switch (REG_NOTE_KIND (link))
22811 default: dep = "unknown depencency"; break;
22812 case REG_DEP_TRUE: dep = "data dependency"; break;
22813 case REG_DEP_OUTPUT: dep = "output dependency"; break;
22814 case REG_DEP_ANTI: dep = "anti depencency"; break;
22817 fprintf (stderr,
22818 "\nrs6000_adjust_cost, final cost = %d, orig cost = %d, "
22819 "%s, insn:\n", ret, cost, dep);
22821 debug_rtx (insn);
22824 return ret;
22827 /* The function returns a true if INSN is microcoded.
22828 Return false otherwise. */
22830 static bool
22831 is_microcoded_insn (rtx insn)
22833 if (!insn || !NONDEBUG_INSN_P (insn)
22834 || GET_CODE (PATTERN (insn)) == USE
22835 || GET_CODE (PATTERN (insn)) == CLOBBER)
22836 return false;
22838 if (rs6000_cpu_attr == CPU_CELL)
22839 return get_attr_cell_micro (insn) == CELL_MICRO_ALWAYS;
22841 if (rs6000_sched_groups)
22843 enum attr_type type = get_attr_type (insn);
22844 if (type == TYPE_LOAD_EXT_U
22845 || type == TYPE_LOAD_EXT_UX
22846 || type == TYPE_LOAD_UX
22847 || type == TYPE_STORE_UX
22848 || type == TYPE_MFCR)
22849 return true;
22852 return false;
22855 /* The function returns true if INSN is cracked into 2 instructions
22856 by the processor (and therefore occupies 2 issue slots). */
22858 static bool
22859 is_cracked_insn (rtx insn)
22861 if (!insn || !NONDEBUG_INSN_P (insn)
22862 || GET_CODE (PATTERN (insn)) == USE
22863 || GET_CODE (PATTERN (insn)) == CLOBBER)
22864 return false;
22866 if (rs6000_sched_groups)
22868 enum attr_type type = get_attr_type (insn);
22869 if (type == TYPE_LOAD_U || type == TYPE_STORE_U
22870 || type == TYPE_FPLOAD_U || type == TYPE_FPSTORE_U
22871 || type == TYPE_FPLOAD_UX || type == TYPE_FPSTORE_UX
22872 || type == TYPE_LOAD_EXT || type == TYPE_DELAYED_CR
22873 || type == TYPE_COMPARE || type == TYPE_DELAYED_COMPARE
22874 || type == TYPE_IMUL_COMPARE || type == TYPE_LMUL_COMPARE
22875 || type == TYPE_IDIV || type == TYPE_LDIV
22876 || type == TYPE_INSERT_WORD)
22877 return true;
22880 return false;
22883 /* The function returns true if INSN can be issued only from
22884 the branch slot. */
22886 static bool
22887 is_branch_slot_insn (rtx insn)
22889 if (!insn || !NONDEBUG_INSN_P (insn)
22890 || GET_CODE (PATTERN (insn)) == USE
22891 || GET_CODE (PATTERN (insn)) == CLOBBER)
22892 return false;
22894 if (rs6000_sched_groups)
22896 enum attr_type type = get_attr_type (insn);
22897 if (type == TYPE_BRANCH || type == TYPE_JMPREG)
22898 return true;
22899 return false;
22902 return false;
22905 /* The function returns true if out_inst sets a value that is
22906 used in the address generation computation of in_insn */
22907 static bool
22908 set_to_load_agen (rtx out_insn, rtx in_insn)
22910 rtx out_set, in_set;
22912 /* For performance reasons, only handle the simple case where
22913 both loads are a single_set. */
22914 out_set = single_set (out_insn);
22915 if (out_set)
22917 in_set = single_set (in_insn);
22918 if (in_set)
22919 return reg_mentioned_p (SET_DEST (out_set), SET_SRC (in_set));
22922 return false;
22925 /* Try to determine base/offset/size parts of the given MEM.
22926 Return true if successful, false if all the values couldn't
22927 be determined.
22929 This function only looks for REG or REG+CONST address forms.
22930 REG+REG address form will return false. */
22932 static bool
22933 get_memref_parts (rtx mem, rtx *base, HOST_WIDE_INT *offset,
22934 HOST_WIDE_INT *size)
22936 rtx addr_rtx;
22937 if MEM_SIZE_KNOWN_P (mem)
22938 *size = MEM_SIZE (mem);
22939 else
22940 return false;
22942 if (GET_CODE (XEXP (mem, 0)) == PRE_MODIFY)
22943 addr_rtx = XEXP (XEXP (mem, 0), 1);
22944 else
22945 addr_rtx = (XEXP (mem, 0));
22947 if (GET_CODE (addr_rtx) == REG)
22949 *base = addr_rtx;
22950 *offset = 0;
22952 else if (GET_CODE (addr_rtx) == PLUS
22953 && CONST_INT_P (XEXP (addr_rtx, 1)))
22955 *base = XEXP (addr_rtx, 0);
22956 *offset = INTVAL (XEXP (addr_rtx, 1));
22958 else
22959 return false;
22961 return true;
22964 /* The function returns true if the target storage location of
22965 mem1 is adjacent to the target storage location of mem2 */
22966 /* Return 1 if memory locations are adjacent. */
22968 static bool
22969 adjacent_mem_locations (rtx mem1, rtx mem2)
22971 rtx reg1, reg2;
22972 HOST_WIDE_INT off1, size1, off2, size2;
22974 if (get_memref_parts (mem1, &reg1, &off1, &size1)
22975 && get_memref_parts (mem2, &reg2, &off2, &size2))
22976 return ((REGNO (reg1) == REGNO (reg2))
22977 && ((off1 + size1 == off2)
22978 || (off2 + size2 == off1)));
22980 return false;
22983 /* This function returns true if it can be determined that the two MEM
22984 locations overlap by at least 1 byte based on base reg/offset/size. */
22986 static bool
22987 mem_locations_overlap (rtx mem1, rtx mem2)
22989 rtx reg1, reg2;
22990 HOST_WIDE_INT off1, size1, off2, size2;
22992 if (get_memref_parts (mem1, &reg1, &off1, &size1)
22993 && get_memref_parts (mem2, &reg2, &off2, &size2))
22994 return ((REGNO (reg1) == REGNO (reg2))
22995 && (((off1 <= off2) && (off1 + size1 > off2))
22996 || ((off2 <= off1) && (off2 + size2 > off1))));
22998 return false;
23001 /* A C statement (sans semicolon) to update the integer scheduling
23002 priority INSN_PRIORITY (INSN). Increase the priority to execute the
23003 INSN earlier, reduce the priority to execute INSN later. Do not
23004 define this macro if you do not need to adjust the scheduling
23005 priorities of insns. */
23007 static int
23008 rs6000_adjust_priority (rtx insn ATTRIBUTE_UNUSED, int priority)
23010 rtx load_mem, str_mem;
23011 /* On machines (like the 750) which have asymmetric integer units,
23012 where one integer unit can do multiply and divides and the other
23013 can't, reduce the priority of multiply/divide so it is scheduled
23014 before other integer operations. */
23016 #if 0
23017 if (! INSN_P (insn))
23018 return priority;
23020 if (GET_CODE (PATTERN (insn)) == USE)
23021 return priority;
23023 switch (rs6000_cpu_attr) {
23024 case CPU_PPC750:
23025 switch (get_attr_type (insn))
23027 default:
23028 break;
23030 case TYPE_IMUL:
23031 case TYPE_IDIV:
23032 fprintf (stderr, "priority was %#x (%d) before adjustment\n",
23033 priority, priority);
23034 if (priority >= 0 && priority < 0x01000000)
23035 priority >>= 3;
23036 break;
23039 #endif
23041 if (insn_must_be_first_in_group (insn)
23042 && reload_completed
23043 && current_sched_info->sched_max_insns_priority
23044 && rs6000_sched_restricted_insns_priority)
23047 /* Prioritize insns that can be dispatched only in the first
23048 dispatch slot. */
23049 if (rs6000_sched_restricted_insns_priority == 1)
23050 /* Attach highest priority to insn. This means that in
23051 haifa-sched.c:ready_sort(), dispatch-slot restriction considerations
23052 precede 'priority' (critical path) considerations. */
23053 return current_sched_info->sched_max_insns_priority;
23054 else if (rs6000_sched_restricted_insns_priority == 2)
23055 /* Increase priority of insn by a minimal amount. This means that in
23056 haifa-sched.c:ready_sort(), only 'priority' (critical path)
23057 considerations precede dispatch-slot restriction considerations. */
23058 return (priority + 1);
23061 if (rs6000_cpu == PROCESSOR_POWER6
23062 && ((load_store_pendulum == -2 && is_load_insn (insn, &load_mem))
23063 || (load_store_pendulum == 2 && is_store_insn (insn, &str_mem))))
23064 /* Attach highest priority to insn if the scheduler has just issued two
23065 stores and this instruction is a load, or two loads and this instruction
23066 is a store. Power6 wants loads and stores scheduled alternately
23067 when possible */
23068 return current_sched_info->sched_max_insns_priority;
23070 return priority;
23073 /* Return true if the instruction is nonpipelined on the Cell. */
23074 static bool
23075 is_nonpipeline_insn (rtx insn)
23077 enum attr_type type;
23078 if (!insn || !NONDEBUG_INSN_P (insn)
23079 || GET_CODE (PATTERN (insn)) == USE
23080 || GET_CODE (PATTERN (insn)) == CLOBBER)
23081 return false;
23083 type = get_attr_type (insn);
23084 if (type == TYPE_IMUL
23085 || type == TYPE_IMUL2
23086 || type == TYPE_IMUL3
23087 || type == TYPE_LMUL
23088 || type == TYPE_IDIV
23089 || type == TYPE_LDIV
23090 || type == TYPE_SDIV
23091 || type == TYPE_DDIV
23092 || type == TYPE_SSQRT
23093 || type == TYPE_DSQRT
23094 || type == TYPE_MFCR
23095 || type == TYPE_MFCRF
23096 || type == TYPE_MFJMPR)
23098 return true;
23100 return false;
23104 /* Return how many instructions the machine can issue per cycle. */
23106 static int
23107 rs6000_issue_rate (void)
23109 /* Unless scheduling for register pressure, use issue rate of 1 for
23110 first scheduling pass to decrease degradation. */
23111 if (!reload_completed && !flag_sched_pressure)
23112 return 1;
23114 switch (rs6000_cpu_attr) {
23115 case CPU_RS64A:
23116 case CPU_PPC601: /* ? */
23117 case CPU_PPC7450:
23118 return 3;
23119 case CPU_PPC440:
23120 case CPU_PPC603:
23121 case CPU_PPC750:
23122 case CPU_PPC7400:
23123 case CPU_PPC8540:
23124 case CPU_PPC8548:
23125 case CPU_CELL:
23126 case CPU_PPCE300C2:
23127 case CPU_PPCE300C3:
23128 case CPU_PPCE500MC:
23129 case CPU_PPCE500MC64:
23130 case CPU_PPCE5500:
23131 case CPU_PPCE6500:
23132 case CPU_TITAN:
23133 return 2;
23134 case CPU_PPC476:
23135 case CPU_PPC604:
23136 case CPU_PPC604E:
23137 case CPU_PPC620:
23138 case CPU_PPC630:
23139 return 4;
23140 case CPU_POWER4:
23141 case CPU_POWER5:
23142 case CPU_POWER6:
23143 case CPU_POWER7:
23144 return 5;
23145 default:
23146 return 1;
23150 /* Return how many instructions to look ahead for better insn
23151 scheduling. */
23153 static int
23154 rs6000_use_sched_lookahead (void)
23156 switch (rs6000_cpu_attr)
23158 case CPU_PPC8540:
23159 case CPU_PPC8548:
23160 return 4;
23162 case CPU_CELL:
23163 return (reload_completed ? 8 : 0);
23165 default:
23166 return 0;
23170 /* We are choosing insn from the ready queue. Return nonzero if INSN can be chosen. */
23171 static int
23172 rs6000_use_sched_lookahead_guard (rtx insn)
23174 if (rs6000_cpu_attr != CPU_CELL)
23175 return 1;
23177 if (insn == NULL_RTX || !INSN_P (insn))
23178 abort ();
23180 if (!reload_completed
23181 || is_nonpipeline_insn (insn)
23182 || is_microcoded_insn (insn))
23183 return 0;
23185 return 1;
23188 /* Determine if PAT refers to memory. If so, set MEM_REF to the MEM rtx
23189 and return true. */
23191 static bool
23192 find_mem_ref (rtx pat, rtx *mem_ref)
23194 const char * fmt;
23195 int i, j;
23197 /* stack_tie does not produce any real memory traffic. */
23198 if (tie_operand (pat, VOIDmode))
23199 return false;
23201 if (GET_CODE (pat) == MEM)
23203 *mem_ref = pat;
23204 return true;
23207 /* Recursively process the pattern. */
23208 fmt = GET_RTX_FORMAT (GET_CODE (pat));
23210 for (i = GET_RTX_LENGTH (GET_CODE (pat)) - 1; i >= 0; i--)
23212 if (fmt[i] == 'e')
23214 if (find_mem_ref (XEXP (pat, i), mem_ref))
23215 return true;
23217 else if (fmt[i] == 'E')
23218 for (j = XVECLEN (pat, i) - 1; j >= 0; j--)
23220 if (find_mem_ref (XVECEXP (pat, i, j), mem_ref))
23221 return true;
23225 return false;
23228 /* Determine if PAT is a PATTERN of a load insn. */
23230 static bool
23231 is_load_insn1 (rtx pat, rtx *load_mem)
23233 if (!pat || pat == NULL_RTX)
23234 return false;
23236 if (GET_CODE (pat) == SET)
23237 return find_mem_ref (SET_SRC (pat), load_mem);
23239 if (GET_CODE (pat) == PARALLEL)
23241 int i;
23243 for (i = 0; i < XVECLEN (pat, 0); i++)
23244 if (is_load_insn1 (XVECEXP (pat, 0, i), load_mem))
23245 return true;
23248 return false;
23251 /* Determine if INSN loads from memory. */
23253 static bool
23254 is_load_insn (rtx insn, rtx *load_mem)
23256 if (!insn || !INSN_P (insn))
23257 return false;
23259 if (GET_CODE (insn) == CALL_INSN)
23260 return false;
23262 return is_load_insn1 (PATTERN (insn), load_mem);
23265 /* Determine if PAT is a PATTERN of a store insn. */
23267 static bool
23268 is_store_insn1 (rtx pat, rtx *str_mem)
23270 if (!pat || pat == NULL_RTX)
23271 return false;
23273 if (GET_CODE (pat) == SET)
23274 return find_mem_ref (SET_DEST (pat), str_mem);
23276 if (GET_CODE (pat) == PARALLEL)
23278 int i;
23280 for (i = 0; i < XVECLEN (pat, 0); i++)
23281 if (is_store_insn1 (XVECEXP (pat, 0, i), str_mem))
23282 return true;
23285 return false;
23288 /* Determine if INSN stores to memory. */
23290 static bool
23291 is_store_insn (rtx insn, rtx *str_mem)
23293 if (!insn || !INSN_P (insn))
23294 return false;
23296 return is_store_insn1 (PATTERN (insn), str_mem);
23299 /* Returns whether the dependence between INSN and NEXT is considered
23300 costly by the given target. */
23302 static bool
23303 rs6000_is_costly_dependence (dep_t dep, int cost, int distance)
23305 rtx insn;
23306 rtx next;
23307 rtx load_mem, str_mem;
23309 /* If the flag is not enabled - no dependence is considered costly;
23310 allow all dependent insns in the same group.
23311 This is the most aggressive option. */
23312 if (rs6000_sched_costly_dep == no_dep_costly)
23313 return false;
23315 /* If the flag is set to 1 - a dependence is always considered costly;
23316 do not allow dependent instructions in the same group.
23317 This is the most conservative option. */
23318 if (rs6000_sched_costly_dep == all_deps_costly)
23319 return true;
23321 insn = DEP_PRO (dep);
23322 next = DEP_CON (dep);
23324 if (rs6000_sched_costly_dep == store_to_load_dep_costly
23325 && is_load_insn (next, &load_mem)
23326 && is_store_insn (insn, &str_mem))
23327 /* Prevent load after store in the same group. */
23328 return true;
23330 if (rs6000_sched_costly_dep == true_store_to_load_dep_costly
23331 && is_load_insn (next, &load_mem)
23332 && is_store_insn (insn, &str_mem)
23333 && DEP_TYPE (dep) == REG_DEP_TRUE
23334 && mem_locations_overlap(str_mem, load_mem))
23335 /* Prevent load after store in the same group if it is a true
23336 dependence. */
23337 return true;
23339 /* The flag is set to X; dependences with latency >= X are considered costly,
23340 and will not be scheduled in the same group. */
23341 if (rs6000_sched_costly_dep <= max_dep_latency
23342 && ((cost - distance) >= (int)rs6000_sched_costly_dep))
23343 return true;
23345 return false;
23348 /* Return the next insn after INSN that is found before TAIL is reached,
23349 skipping any "non-active" insns - insns that will not actually occupy
23350 an issue slot. Return NULL_RTX if such an insn is not found. */
23352 static rtx
23353 get_next_active_insn (rtx insn, rtx tail)
23355 if (insn == NULL_RTX || insn == tail)
23356 return NULL_RTX;
23358 while (1)
23360 insn = NEXT_INSN (insn);
23361 if (insn == NULL_RTX || insn == tail)
23362 return NULL_RTX;
23364 if (CALL_P (insn)
23365 || JUMP_P (insn)
23366 || (NONJUMP_INSN_P (insn)
23367 && GET_CODE (PATTERN (insn)) != USE
23368 && GET_CODE (PATTERN (insn)) != CLOBBER
23369 && INSN_CODE (insn) != CODE_FOR_stack_tie))
23370 break;
23372 return insn;
23375 /* We are about to begin issuing insns for this clock cycle. */
23377 static int
23378 rs6000_sched_reorder (FILE *dump ATTRIBUTE_UNUSED, int sched_verbose,
23379 rtx *ready ATTRIBUTE_UNUSED,
23380 int *pn_ready ATTRIBUTE_UNUSED,
23381 int clock_var ATTRIBUTE_UNUSED)
23383 int n_ready = *pn_ready;
23385 if (sched_verbose)
23386 fprintf (dump, "// rs6000_sched_reorder :\n");
23388 /* Reorder the ready list, if the second to last ready insn
23389 is a nonepipeline insn. */
23390 if (rs6000_cpu_attr == CPU_CELL && n_ready > 1)
23392 if (is_nonpipeline_insn (ready[n_ready - 1])
23393 && (recog_memoized (ready[n_ready - 2]) > 0))
23394 /* Simply swap first two insns. */
23396 rtx tmp = ready[n_ready - 1];
23397 ready[n_ready - 1] = ready[n_ready - 2];
23398 ready[n_ready - 2] = tmp;
23402 if (rs6000_cpu == PROCESSOR_POWER6)
23403 load_store_pendulum = 0;
23405 return rs6000_issue_rate ();
23408 /* Like rs6000_sched_reorder, but called after issuing each insn. */
23410 static int
23411 rs6000_sched_reorder2 (FILE *dump, int sched_verbose, rtx *ready,
23412 int *pn_ready, int clock_var ATTRIBUTE_UNUSED)
23414 if (sched_verbose)
23415 fprintf (dump, "// rs6000_sched_reorder2 :\n");
23417 /* For Power6, we need to handle some special cases to try and keep the
23418 store queue from overflowing and triggering expensive flushes.
23420 This code monitors how load and store instructions are being issued
23421 and skews the ready list one way or the other to increase the likelihood
23422 that a desired instruction is issued at the proper time.
23424 A couple of things are done. First, we maintain a "load_store_pendulum"
23425 to track the current state of load/store issue.
23427 - If the pendulum is at zero, then no loads or stores have been
23428 issued in the current cycle so we do nothing.
23430 - If the pendulum is 1, then a single load has been issued in this
23431 cycle and we attempt to locate another load in the ready list to
23432 issue with it.
23434 - If the pendulum is -2, then two stores have already been
23435 issued in this cycle, so we increase the priority of the first load
23436 in the ready list to increase it's likelihood of being chosen first
23437 in the next cycle.
23439 - If the pendulum is -1, then a single store has been issued in this
23440 cycle and we attempt to locate another store in the ready list to
23441 issue with it, preferring a store to an adjacent memory location to
23442 facilitate store pairing in the store queue.
23444 - If the pendulum is 2, then two loads have already been
23445 issued in this cycle, so we increase the priority of the first store
23446 in the ready list to increase it's likelihood of being chosen first
23447 in the next cycle.
23449 - If the pendulum < -2 or > 2, then do nothing.
23451 Note: This code covers the most common scenarios. There exist non
23452 load/store instructions which make use of the LSU and which
23453 would need to be accounted for to strictly model the behavior
23454 of the machine. Those instructions are currently unaccounted
23455 for to help minimize compile time overhead of this code.
23457 if (rs6000_cpu == PROCESSOR_POWER6 && last_scheduled_insn)
23459 int pos;
23460 int i;
23461 rtx tmp, load_mem, str_mem;
23463 if (is_store_insn (last_scheduled_insn, &str_mem))
23464 /* Issuing a store, swing the load_store_pendulum to the left */
23465 load_store_pendulum--;
23466 else if (is_load_insn (last_scheduled_insn, &load_mem))
23467 /* Issuing a load, swing the load_store_pendulum to the right */
23468 load_store_pendulum++;
23469 else
23470 return cached_can_issue_more;
23472 /* If the pendulum is balanced, or there is only one instruction on
23473 the ready list, then all is well, so return. */
23474 if ((load_store_pendulum == 0) || (*pn_ready <= 1))
23475 return cached_can_issue_more;
23477 if (load_store_pendulum == 1)
23479 /* A load has been issued in this cycle. Scan the ready list
23480 for another load to issue with it */
23481 pos = *pn_ready-1;
23483 while (pos >= 0)
23485 if (is_load_insn (ready[pos], &load_mem))
23487 /* Found a load. Move it to the head of the ready list,
23488 and adjust it's priority so that it is more likely to
23489 stay there */
23490 tmp = ready[pos];
23491 for (i=pos; i<*pn_ready-1; i++)
23492 ready[i] = ready[i + 1];
23493 ready[*pn_ready-1] = tmp;
23495 if (!sel_sched_p () && INSN_PRIORITY_KNOWN (tmp))
23496 INSN_PRIORITY (tmp)++;
23497 break;
23499 pos--;
23502 else if (load_store_pendulum == -2)
23504 /* Two stores have been issued in this cycle. Increase the
23505 priority of the first load in the ready list to favor it for
23506 issuing in the next cycle. */
23507 pos = *pn_ready-1;
23509 while (pos >= 0)
23511 if (is_load_insn (ready[pos], &load_mem)
23512 && !sel_sched_p ()
23513 && INSN_PRIORITY_KNOWN (ready[pos]))
23515 INSN_PRIORITY (ready[pos])++;
23517 /* Adjust the pendulum to account for the fact that a load
23518 was found and increased in priority. This is to prevent
23519 increasing the priority of multiple loads */
23520 load_store_pendulum--;
23522 break;
23524 pos--;
23527 else if (load_store_pendulum == -1)
23529 /* A store has been issued in this cycle. Scan the ready list for
23530 another store to issue with it, preferring a store to an adjacent
23531 memory location */
23532 int first_store_pos = -1;
23534 pos = *pn_ready-1;
23536 while (pos >= 0)
23538 if (is_store_insn (ready[pos], &str_mem))
23540 rtx str_mem2;
23541 /* Maintain the index of the first store found on the
23542 list */
23543 if (first_store_pos == -1)
23544 first_store_pos = pos;
23546 if (is_store_insn (last_scheduled_insn, &str_mem2)
23547 && adjacent_mem_locations (str_mem, str_mem2))
23549 /* Found an adjacent store. Move it to the head of the
23550 ready list, and adjust it's priority so that it is
23551 more likely to stay there */
23552 tmp = ready[pos];
23553 for (i=pos; i<*pn_ready-1; i++)
23554 ready[i] = ready[i + 1];
23555 ready[*pn_ready-1] = tmp;
23557 if (!sel_sched_p () && INSN_PRIORITY_KNOWN (tmp))
23558 INSN_PRIORITY (tmp)++;
23560 first_store_pos = -1;
23562 break;
23565 pos--;
23568 if (first_store_pos >= 0)
23570 /* An adjacent store wasn't found, but a non-adjacent store was,
23571 so move the non-adjacent store to the front of the ready
23572 list, and adjust its priority so that it is more likely to
23573 stay there. */
23574 tmp = ready[first_store_pos];
23575 for (i=first_store_pos; i<*pn_ready-1; i++)
23576 ready[i] = ready[i + 1];
23577 ready[*pn_ready-1] = tmp;
23578 if (!sel_sched_p () && INSN_PRIORITY_KNOWN (tmp))
23579 INSN_PRIORITY (tmp)++;
23582 else if (load_store_pendulum == 2)
23584 /* Two loads have been issued in this cycle. Increase the priority
23585 of the first store in the ready list to favor it for issuing in
23586 the next cycle. */
23587 pos = *pn_ready-1;
23589 while (pos >= 0)
23591 if (is_store_insn (ready[pos], &str_mem)
23592 && !sel_sched_p ()
23593 && INSN_PRIORITY_KNOWN (ready[pos]))
23595 INSN_PRIORITY (ready[pos])++;
23597 /* Adjust the pendulum to account for the fact that a store
23598 was found and increased in priority. This is to prevent
23599 increasing the priority of multiple stores */
23600 load_store_pendulum++;
23602 break;
23604 pos--;
23609 return cached_can_issue_more;
23612 /* Return whether the presence of INSN causes a dispatch group termination
23613 of group WHICH_GROUP.
23615 If WHICH_GROUP == current_group, this function will return true if INSN
23616 causes the termination of the current group (i.e, the dispatch group to
23617 which INSN belongs). This means that INSN will be the last insn in the
23618 group it belongs to.
23620 If WHICH_GROUP == previous_group, this function will return true if INSN
23621 causes the termination of the previous group (i.e, the dispatch group that
23622 precedes the group to which INSN belongs). This means that INSN will be
23623 the first insn in the group it belongs to). */
23625 static bool
23626 insn_terminates_group_p (rtx insn, enum group_termination which_group)
23628 bool first, last;
23630 if (! insn)
23631 return false;
23633 first = insn_must_be_first_in_group (insn);
23634 last = insn_must_be_last_in_group (insn);
23636 if (first && last)
23637 return true;
23639 if (which_group == current_group)
23640 return last;
23641 else if (which_group == previous_group)
23642 return first;
23644 return false;
23648 static bool
23649 insn_must_be_first_in_group (rtx insn)
23651 enum attr_type type;
23653 if (!insn
23654 || GET_CODE (insn) == NOTE
23655 || DEBUG_INSN_P (insn)
23656 || GET_CODE (PATTERN (insn)) == USE
23657 || GET_CODE (PATTERN (insn)) == CLOBBER)
23658 return false;
23660 switch (rs6000_cpu)
23662 case PROCESSOR_POWER5:
23663 if (is_cracked_insn (insn))
23664 return true;
23665 case PROCESSOR_POWER4:
23666 if (is_microcoded_insn (insn))
23667 return true;
23669 if (!rs6000_sched_groups)
23670 return false;
23672 type = get_attr_type (insn);
23674 switch (type)
23676 case TYPE_MFCR:
23677 case TYPE_MFCRF:
23678 case TYPE_MTCR:
23679 case TYPE_DELAYED_CR:
23680 case TYPE_CR_LOGICAL:
23681 case TYPE_MTJMPR:
23682 case TYPE_MFJMPR:
23683 case TYPE_IDIV:
23684 case TYPE_LDIV:
23685 case TYPE_LOAD_L:
23686 case TYPE_STORE_C:
23687 case TYPE_ISYNC:
23688 case TYPE_SYNC:
23689 return true;
23690 default:
23691 break;
23693 break;
23694 case PROCESSOR_POWER6:
23695 type = get_attr_type (insn);
23697 switch (type)
23699 case TYPE_INSERT_DWORD:
23700 case TYPE_EXTS:
23701 case TYPE_CNTLZ:
23702 case TYPE_SHIFT:
23703 case TYPE_VAR_SHIFT_ROTATE:
23704 case TYPE_TRAP:
23705 case TYPE_IMUL:
23706 case TYPE_IMUL2:
23707 case TYPE_IMUL3:
23708 case TYPE_LMUL:
23709 case TYPE_IDIV:
23710 case TYPE_INSERT_WORD:
23711 case TYPE_DELAYED_COMPARE:
23712 case TYPE_IMUL_COMPARE:
23713 case TYPE_LMUL_COMPARE:
23714 case TYPE_FPCOMPARE:
23715 case TYPE_MFCR:
23716 case TYPE_MTCR:
23717 case TYPE_MFJMPR:
23718 case TYPE_MTJMPR:
23719 case TYPE_ISYNC:
23720 case TYPE_SYNC:
23721 case TYPE_LOAD_L:
23722 case TYPE_STORE_C:
23723 case TYPE_LOAD_U:
23724 case TYPE_LOAD_UX:
23725 case TYPE_LOAD_EXT_UX:
23726 case TYPE_STORE_U:
23727 case TYPE_STORE_UX:
23728 case TYPE_FPLOAD_U:
23729 case TYPE_FPLOAD_UX:
23730 case TYPE_FPSTORE_U:
23731 case TYPE_FPSTORE_UX:
23732 return true;
23733 default:
23734 break;
23736 break;
23737 case PROCESSOR_POWER7:
23738 type = get_attr_type (insn);
23740 switch (type)
23742 case TYPE_CR_LOGICAL:
23743 case TYPE_MFCR:
23744 case TYPE_MFCRF:
23745 case TYPE_MTCR:
23746 case TYPE_IDIV:
23747 case TYPE_LDIV:
23748 case TYPE_COMPARE:
23749 case TYPE_DELAYED_COMPARE:
23750 case TYPE_VAR_DELAYED_COMPARE:
23751 case TYPE_ISYNC:
23752 case TYPE_LOAD_L:
23753 case TYPE_STORE_C:
23754 case TYPE_LOAD_U:
23755 case TYPE_LOAD_UX:
23756 case TYPE_LOAD_EXT:
23757 case TYPE_LOAD_EXT_U:
23758 case TYPE_LOAD_EXT_UX:
23759 case TYPE_STORE_U:
23760 case TYPE_STORE_UX:
23761 case TYPE_FPLOAD_U:
23762 case TYPE_FPLOAD_UX:
23763 case TYPE_FPSTORE_U:
23764 case TYPE_FPSTORE_UX:
23765 case TYPE_MFJMPR:
23766 case TYPE_MTJMPR:
23767 return true;
23768 default:
23769 break;
23771 break;
23772 default:
23773 break;
23776 return false;
23779 static bool
23780 insn_must_be_last_in_group (rtx insn)
23782 enum attr_type type;
23784 if (!insn
23785 || GET_CODE (insn) == NOTE
23786 || DEBUG_INSN_P (insn)
23787 || GET_CODE (PATTERN (insn)) == USE
23788 || GET_CODE (PATTERN (insn)) == CLOBBER)
23789 return false;
23791 switch (rs6000_cpu) {
23792 case PROCESSOR_POWER4:
23793 case PROCESSOR_POWER5:
23794 if (is_microcoded_insn (insn))
23795 return true;
23797 if (is_branch_slot_insn (insn))
23798 return true;
23800 break;
23801 case PROCESSOR_POWER6:
23802 type = get_attr_type (insn);
23804 switch (type)
23806 case TYPE_EXTS:
23807 case TYPE_CNTLZ:
23808 case TYPE_SHIFT:
23809 case TYPE_VAR_SHIFT_ROTATE:
23810 case TYPE_TRAP:
23811 case TYPE_IMUL:
23812 case TYPE_IMUL2:
23813 case TYPE_IMUL3:
23814 case TYPE_LMUL:
23815 case TYPE_IDIV:
23816 case TYPE_DELAYED_COMPARE:
23817 case TYPE_IMUL_COMPARE:
23818 case TYPE_LMUL_COMPARE:
23819 case TYPE_FPCOMPARE:
23820 case TYPE_MFCR:
23821 case TYPE_MTCR:
23822 case TYPE_MFJMPR:
23823 case TYPE_MTJMPR:
23824 case TYPE_ISYNC:
23825 case TYPE_SYNC:
23826 case TYPE_LOAD_L:
23827 case TYPE_STORE_C:
23828 return true;
23829 default:
23830 break;
23832 break;
23833 case PROCESSOR_POWER7:
23834 type = get_attr_type (insn);
23836 switch (type)
23838 case TYPE_ISYNC:
23839 case TYPE_SYNC:
23840 case TYPE_LOAD_L:
23841 case TYPE_STORE_C:
23842 case TYPE_LOAD_EXT_U:
23843 case TYPE_LOAD_EXT_UX:
23844 case TYPE_STORE_UX:
23845 return true;
23846 default:
23847 break;
23849 break;
23850 default:
23851 break;
23854 return false;
23857 /* Return true if it is recommended to keep NEXT_INSN "far" (in a separate
23858 dispatch group) from the insns in GROUP_INSNS. Return false otherwise. */
23860 static bool
23861 is_costly_group (rtx *group_insns, rtx next_insn)
23863 int i;
23864 int issue_rate = rs6000_issue_rate ();
23866 for (i = 0; i < issue_rate; i++)
23868 sd_iterator_def sd_it;
23869 dep_t dep;
23870 rtx insn = group_insns[i];
23872 if (!insn)
23873 continue;
23875 FOR_EACH_DEP (insn, SD_LIST_RES_FORW, sd_it, dep)
23877 rtx next = DEP_CON (dep);
23879 if (next == next_insn
23880 && rs6000_is_costly_dependence (dep, dep_cost (dep), 0))
23881 return true;
23885 return false;
23888 /* Utility of the function redefine_groups.
23889 Check if it is too costly to schedule NEXT_INSN together with GROUP_INSNS
23890 in the same dispatch group. If so, insert nops before NEXT_INSN, in order
23891 to keep it "far" (in a separate group) from GROUP_INSNS, following
23892 one of the following schemes, depending on the value of the flag
23893 -minsert_sched_nops = X:
23894 (1) X == sched_finish_regroup_exact: insert exactly as many nops as needed
23895 in order to force NEXT_INSN into a separate group.
23896 (2) X < sched_finish_regroup_exact: insert exactly X nops.
23897 GROUP_END, CAN_ISSUE_MORE and GROUP_COUNT record the state after nop
23898 insertion (has a group just ended, how many vacant issue slots remain in the
23899 last group, and how many dispatch groups were encountered so far). */
23901 static int
23902 force_new_group (int sched_verbose, FILE *dump, rtx *group_insns,
23903 rtx next_insn, bool *group_end, int can_issue_more,
23904 int *group_count)
23906 rtx nop;
23907 bool force;
23908 int issue_rate = rs6000_issue_rate ();
23909 bool end = *group_end;
23910 int i;
23912 if (next_insn == NULL_RTX || DEBUG_INSN_P (next_insn))
23913 return can_issue_more;
23915 if (rs6000_sched_insert_nops > sched_finish_regroup_exact)
23916 return can_issue_more;
23918 force = is_costly_group (group_insns, next_insn);
23919 if (!force)
23920 return can_issue_more;
23922 if (sched_verbose > 6)
23923 fprintf (dump,"force: group count = %d, can_issue_more = %d\n",
23924 *group_count ,can_issue_more);
23926 if (rs6000_sched_insert_nops == sched_finish_regroup_exact)
23928 if (*group_end)
23929 can_issue_more = 0;
23931 /* Since only a branch can be issued in the last issue_slot, it is
23932 sufficient to insert 'can_issue_more - 1' nops if next_insn is not
23933 a branch. If next_insn is a branch, we insert 'can_issue_more' nops;
23934 in this case the last nop will start a new group and the branch
23935 will be forced to the new group. */
23936 if (can_issue_more && !is_branch_slot_insn (next_insn))
23937 can_issue_more--;
23939 /* Power6 and Power7 have special group ending nop. */
23940 if (rs6000_cpu_attr == CPU_POWER6 || rs6000_cpu_attr == CPU_POWER7)
23942 nop = gen_group_ending_nop ();
23943 emit_insn_before (nop, next_insn);
23944 can_issue_more = 0;
23946 else
23947 while (can_issue_more > 0)
23949 nop = gen_nop ();
23950 emit_insn_before (nop, next_insn);
23951 can_issue_more--;
23954 *group_end = true;
23955 return 0;
23958 if (rs6000_sched_insert_nops < sched_finish_regroup_exact)
23960 int n_nops = rs6000_sched_insert_nops;
23962 /* Nops can't be issued from the branch slot, so the effective
23963 issue_rate for nops is 'issue_rate - 1'. */
23964 if (can_issue_more == 0)
23965 can_issue_more = issue_rate;
23966 can_issue_more--;
23967 if (can_issue_more == 0)
23969 can_issue_more = issue_rate - 1;
23970 (*group_count)++;
23971 end = true;
23972 for (i = 0; i < issue_rate; i++)
23974 group_insns[i] = 0;
23978 while (n_nops > 0)
23980 nop = gen_nop ();
23981 emit_insn_before (nop, next_insn);
23982 if (can_issue_more == issue_rate - 1) /* new group begins */
23983 end = false;
23984 can_issue_more--;
23985 if (can_issue_more == 0)
23987 can_issue_more = issue_rate - 1;
23988 (*group_count)++;
23989 end = true;
23990 for (i = 0; i < issue_rate; i++)
23992 group_insns[i] = 0;
23995 n_nops--;
23998 /* Scale back relative to 'issue_rate' (instead of 'issue_rate - 1'). */
23999 can_issue_more++;
24001 /* Is next_insn going to start a new group? */
24002 *group_end
24003 = (end
24004 || (can_issue_more == 1 && !is_branch_slot_insn (next_insn))
24005 || (can_issue_more <= 2 && is_cracked_insn (next_insn))
24006 || (can_issue_more < issue_rate &&
24007 insn_terminates_group_p (next_insn, previous_group)));
24008 if (*group_end && end)
24009 (*group_count)--;
24011 if (sched_verbose > 6)
24012 fprintf (dump, "done force: group count = %d, can_issue_more = %d\n",
24013 *group_count, can_issue_more);
24014 return can_issue_more;
24017 return can_issue_more;
24020 /* This function tries to synch the dispatch groups that the compiler "sees"
24021 with the dispatch groups that the processor dispatcher is expected to
24022 form in practice. It tries to achieve this synchronization by forcing the
24023 estimated processor grouping on the compiler (as opposed to the function
24024 'pad_goups' which tries to force the scheduler's grouping on the processor).
24026 The function scans the insn sequence between PREV_HEAD_INSN and TAIL and
24027 examines the (estimated) dispatch groups that will be formed by the processor
24028 dispatcher. It marks these group boundaries to reflect the estimated
24029 processor grouping, overriding the grouping that the scheduler had marked.
24030 Depending on the value of the flag '-minsert-sched-nops' this function can
24031 force certain insns into separate groups or force a certain distance between
24032 them by inserting nops, for example, if there exists a "costly dependence"
24033 between the insns.
24035 The function estimates the group boundaries that the processor will form as
24036 follows: It keeps track of how many vacant issue slots are available after
24037 each insn. A subsequent insn will start a new group if one of the following
24038 4 cases applies:
24039 - no more vacant issue slots remain in the current dispatch group.
24040 - only the last issue slot, which is the branch slot, is vacant, but the next
24041 insn is not a branch.
24042 - only the last 2 or less issue slots, including the branch slot, are vacant,
24043 which means that a cracked insn (which occupies two issue slots) can't be
24044 issued in this group.
24045 - less than 'issue_rate' slots are vacant, and the next insn always needs to
24046 start a new group. */
24048 static int
24049 redefine_groups (FILE *dump, int sched_verbose, rtx prev_head_insn, rtx tail)
24051 rtx insn, next_insn;
24052 int issue_rate;
24053 int can_issue_more;
24054 int slot, i;
24055 bool group_end;
24056 int group_count = 0;
24057 rtx *group_insns;
24059 /* Initialize. */
24060 issue_rate = rs6000_issue_rate ();
24061 group_insns = XALLOCAVEC (rtx, issue_rate);
24062 for (i = 0; i < issue_rate; i++)
24064 group_insns[i] = 0;
24066 can_issue_more = issue_rate;
24067 slot = 0;
24068 insn = get_next_active_insn (prev_head_insn, tail);
24069 group_end = false;
24071 while (insn != NULL_RTX)
24073 slot = (issue_rate - can_issue_more);
24074 group_insns[slot] = insn;
24075 can_issue_more =
24076 rs6000_variable_issue (dump, sched_verbose, insn, can_issue_more);
24077 if (insn_terminates_group_p (insn, current_group))
24078 can_issue_more = 0;
24080 next_insn = get_next_active_insn (insn, tail);
24081 if (next_insn == NULL_RTX)
24082 return group_count + 1;
24084 /* Is next_insn going to start a new group? */
24085 group_end
24086 = (can_issue_more == 0
24087 || (can_issue_more == 1 && !is_branch_slot_insn (next_insn))
24088 || (can_issue_more <= 2 && is_cracked_insn (next_insn))
24089 || (can_issue_more < issue_rate &&
24090 insn_terminates_group_p (next_insn, previous_group)));
24092 can_issue_more = force_new_group (sched_verbose, dump, group_insns,
24093 next_insn, &group_end, can_issue_more,
24094 &group_count);
24096 if (group_end)
24098 group_count++;
24099 can_issue_more = 0;
24100 for (i = 0; i < issue_rate; i++)
24102 group_insns[i] = 0;
24106 if (GET_MODE (next_insn) == TImode && can_issue_more)
24107 PUT_MODE (next_insn, VOIDmode);
24108 else if (!can_issue_more && GET_MODE (next_insn) != TImode)
24109 PUT_MODE (next_insn, TImode);
24111 insn = next_insn;
24112 if (can_issue_more == 0)
24113 can_issue_more = issue_rate;
24114 } /* while */
24116 return group_count;
24119 /* Scan the insn sequence between PREV_HEAD_INSN and TAIL and examine the
24120 dispatch group boundaries that the scheduler had marked. Pad with nops
24121 any dispatch groups which have vacant issue slots, in order to force the
24122 scheduler's grouping on the processor dispatcher. The function
24123 returns the number of dispatch groups found. */
24125 static int
24126 pad_groups (FILE *dump, int sched_verbose, rtx prev_head_insn, rtx tail)
24128 rtx insn, next_insn;
24129 rtx nop;
24130 int issue_rate;
24131 int can_issue_more;
24132 int group_end;
24133 int group_count = 0;
24135 /* Initialize issue_rate. */
24136 issue_rate = rs6000_issue_rate ();
24137 can_issue_more = issue_rate;
24139 insn = get_next_active_insn (prev_head_insn, tail);
24140 next_insn = get_next_active_insn (insn, tail);
24142 while (insn != NULL_RTX)
24144 can_issue_more =
24145 rs6000_variable_issue (dump, sched_verbose, insn, can_issue_more);
24147 group_end = (next_insn == NULL_RTX || GET_MODE (next_insn) == TImode);
24149 if (next_insn == NULL_RTX)
24150 break;
24152 if (group_end)
24154 /* If the scheduler had marked group termination at this location
24155 (between insn and next_insn), and neither insn nor next_insn will
24156 force group termination, pad the group with nops to force group
24157 termination. */
24158 if (can_issue_more
24159 && (rs6000_sched_insert_nops == sched_finish_pad_groups)
24160 && !insn_terminates_group_p (insn, current_group)
24161 && !insn_terminates_group_p (next_insn, previous_group))
24163 if (!is_branch_slot_insn (next_insn))
24164 can_issue_more--;
24166 while (can_issue_more)
24168 nop = gen_nop ();
24169 emit_insn_before (nop, next_insn);
24170 can_issue_more--;
24174 can_issue_more = issue_rate;
24175 group_count++;
24178 insn = next_insn;
24179 next_insn = get_next_active_insn (insn, tail);
24182 return group_count;
24185 /* We're beginning a new block. Initialize data structures as necessary. */
24187 static void
24188 rs6000_sched_init (FILE *dump ATTRIBUTE_UNUSED,
24189 int sched_verbose ATTRIBUTE_UNUSED,
24190 int max_ready ATTRIBUTE_UNUSED)
24192 last_scheduled_insn = NULL_RTX;
24193 load_store_pendulum = 0;
24196 /* The following function is called at the end of scheduling BB.
24197 After reload, it inserts nops at insn group bundling. */
24199 static void
24200 rs6000_sched_finish (FILE *dump, int sched_verbose)
24202 int n_groups;
24204 if (sched_verbose)
24205 fprintf (dump, "=== Finishing schedule.\n");
24207 if (reload_completed && rs6000_sched_groups)
24209 /* Do not run sched_finish hook when selective scheduling enabled. */
24210 if (sel_sched_p ())
24211 return;
24213 if (rs6000_sched_insert_nops == sched_finish_none)
24214 return;
24216 if (rs6000_sched_insert_nops == sched_finish_pad_groups)
24217 n_groups = pad_groups (dump, sched_verbose,
24218 current_sched_info->prev_head,
24219 current_sched_info->next_tail);
24220 else
24221 n_groups = redefine_groups (dump, sched_verbose,
24222 current_sched_info->prev_head,
24223 current_sched_info->next_tail);
24225 if (sched_verbose >= 6)
24227 fprintf (dump, "ngroups = %d\n", n_groups);
24228 print_rtl (dump, current_sched_info->prev_head);
24229 fprintf (dump, "Done finish_sched\n");
24234 struct _rs6000_sched_context
24236 short cached_can_issue_more;
24237 rtx last_scheduled_insn;
24238 int load_store_pendulum;
24241 typedef struct _rs6000_sched_context rs6000_sched_context_def;
24242 typedef rs6000_sched_context_def *rs6000_sched_context_t;
24244 /* Allocate store for new scheduling context. */
24245 static void *
24246 rs6000_alloc_sched_context (void)
24248 return xmalloc (sizeof (rs6000_sched_context_def));
24251 /* If CLEAN_P is true then initializes _SC with clean data,
24252 and from the global context otherwise. */
24253 static void
24254 rs6000_init_sched_context (void *_sc, bool clean_p)
24256 rs6000_sched_context_t sc = (rs6000_sched_context_t) _sc;
24258 if (clean_p)
24260 sc->cached_can_issue_more = 0;
24261 sc->last_scheduled_insn = NULL_RTX;
24262 sc->load_store_pendulum = 0;
24264 else
24266 sc->cached_can_issue_more = cached_can_issue_more;
24267 sc->last_scheduled_insn = last_scheduled_insn;
24268 sc->load_store_pendulum = load_store_pendulum;
24272 /* Sets the global scheduling context to the one pointed to by _SC. */
24273 static void
24274 rs6000_set_sched_context (void *_sc)
24276 rs6000_sched_context_t sc = (rs6000_sched_context_t) _sc;
24278 gcc_assert (sc != NULL);
24280 cached_can_issue_more = sc->cached_can_issue_more;
24281 last_scheduled_insn = sc->last_scheduled_insn;
24282 load_store_pendulum = sc->load_store_pendulum;
24285 /* Free _SC. */
24286 static void
24287 rs6000_free_sched_context (void *_sc)
24289 gcc_assert (_sc != NULL);
24291 free (_sc);
24295 /* Length in units of the trampoline for entering a nested function. */
24298 rs6000_trampoline_size (void)
24300 int ret = 0;
24302 switch (DEFAULT_ABI)
24304 default:
24305 gcc_unreachable ();
24307 case ABI_AIX:
24308 ret = (TARGET_32BIT) ? 12 : 24;
24309 break;
24311 case ABI_DARWIN:
24312 case ABI_V4:
24313 ret = (TARGET_32BIT) ? 40 : 48;
24314 break;
24317 return ret;
24320 /* Emit RTL insns to initialize the variable parts of a trampoline.
24321 FNADDR is an RTX for the address of the function's pure code.
24322 CXT is an RTX for the static chain value for the function. */
24324 static void
24325 rs6000_trampoline_init (rtx m_tramp, tree fndecl, rtx cxt)
24327 int regsize = (TARGET_32BIT) ? 4 : 8;
24328 rtx fnaddr = XEXP (DECL_RTL (fndecl), 0);
24329 rtx ctx_reg = force_reg (Pmode, cxt);
24330 rtx addr = force_reg (Pmode, XEXP (m_tramp, 0));
24332 switch (DEFAULT_ABI)
24334 default:
24335 gcc_unreachable ();
24337 /* Under AIX, just build the 3 word function descriptor */
24338 case ABI_AIX:
24340 rtx fnmem, fn_reg, toc_reg;
24342 if (!TARGET_POINTERS_TO_NESTED_FUNCTIONS)
24343 error ("You cannot take the address of a nested function if you use "
24344 "the -mno-pointers-to-nested-functions option.");
24346 fnmem = gen_const_mem (Pmode, force_reg (Pmode, fnaddr));
24347 fn_reg = gen_reg_rtx (Pmode);
24348 toc_reg = gen_reg_rtx (Pmode);
24350 /* Macro to shorten the code expansions below. */
24351 # define MEM_PLUS(MEM, OFFSET) adjust_address (MEM, Pmode, OFFSET)
24353 m_tramp = replace_equiv_address (m_tramp, addr);
24355 emit_move_insn (fn_reg, MEM_PLUS (fnmem, 0));
24356 emit_move_insn (toc_reg, MEM_PLUS (fnmem, regsize));
24357 emit_move_insn (MEM_PLUS (m_tramp, 0), fn_reg);
24358 emit_move_insn (MEM_PLUS (m_tramp, regsize), toc_reg);
24359 emit_move_insn (MEM_PLUS (m_tramp, 2*regsize), ctx_reg);
24361 # undef MEM_PLUS
24363 break;
24365 /* Under V.4/eabi/darwin, __trampoline_setup does the real work. */
24366 case ABI_DARWIN:
24367 case ABI_V4:
24368 emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "__trampoline_setup"),
24369 LCT_NORMAL, VOIDmode, 4,
24370 addr, Pmode,
24371 GEN_INT (rs6000_trampoline_size ()), SImode,
24372 fnaddr, Pmode,
24373 ctx_reg, Pmode);
24374 break;
24379 /* Returns TRUE iff the target attribute indicated by ATTR_ID takes a plain
24380 identifier as an argument, so the front end shouldn't look it up. */
24382 static bool
24383 rs6000_attribute_takes_identifier_p (const_tree attr_id)
24385 return is_attribute_p ("altivec", attr_id);
24388 /* Handle the "altivec" attribute. The attribute may have
24389 arguments as follows:
24391 __attribute__((altivec(vector__)))
24392 __attribute__((altivec(pixel__))) (always followed by 'unsigned short')
24393 __attribute__((altivec(bool__))) (always followed by 'unsigned')
24395 and may appear more than once (e.g., 'vector bool char') in a
24396 given declaration. */
24398 static tree
24399 rs6000_handle_altivec_attribute (tree *node,
24400 tree name ATTRIBUTE_UNUSED,
24401 tree args,
24402 int flags ATTRIBUTE_UNUSED,
24403 bool *no_add_attrs)
24405 tree type = *node, result = NULL_TREE;
24406 enum machine_mode mode;
24407 int unsigned_p;
24408 char altivec_type
24409 = ((args && TREE_CODE (args) == TREE_LIST && TREE_VALUE (args)
24410 && TREE_CODE (TREE_VALUE (args)) == IDENTIFIER_NODE)
24411 ? *IDENTIFIER_POINTER (TREE_VALUE (args))
24412 : '?');
24414 while (POINTER_TYPE_P (type)
24415 || TREE_CODE (type) == FUNCTION_TYPE
24416 || TREE_CODE (type) == METHOD_TYPE
24417 || TREE_CODE (type) == ARRAY_TYPE)
24418 type = TREE_TYPE (type);
24420 mode = TYPE_MODE (type);
24422 /* Check for invalid AltiVec type qualifiers. */
24423 if (type == long_double_type_node)
24424 error ("use of %<long double%> in AltiVec types is invalid");
24425 else if (type == boolean_type_node)
24426 error ("use of boolean types in AltiVec types is invalid");
24427 else if (TREE_CODE (type) == COMPLEX_TYPE)
24428 error ("use of %<complex%> in AltiVec types is invalid");
24429 else if (DECIMAL_FLOAT_MODE_P (mode))
24430 error ("use of decimal floating point types in AltiVec types is invalid");
24431 else if (!TARGET_VSX)
24433 if (type == long_unsigned_type_node || type == long_integer_type_node)
24435 if (TARGET_64BIT)
24436 error ("use of %<long%> in AltiVec types is invalid for "
24437 "64-bit code without -mvsx");
24438 else if (rs6000_warn_altivec_long)
24439 warning (0, "use of %<long%> in AltiVec types is deprecated; "
24440 "use %<int%>");
24442 else if (type == long_long_unsigned_type_node
24443 || type == long_long_integer_type_node)
24444 error ("use of %<long long%> in AltiVec types is invalid without "
24445 "-mvsx");
24446 else if (type == double_type_node)
24447 error ("use of %<double%> in AltiVec types is invalid without -mvsx");
24450 switch (altivec_type)
24452 case 'v':
24453 unsigned_p = TYPE_UNSIGNED (type);
24454 switch (mode)
24456 case DImode:
24457 result = (unsigned_p ? unsigned_V2DI_type_node : V2DI_type_node);
24458 break;
24459 case SImode:
24460 result = (unsigned_p ? unsigned_V4SI_type_node : V4SI_type_node);
24461 break;
24462 case HImode:
24463 result = (unsigned_p ? unsigned_V8HI_type_node : V8HI_type_node);
24464 break;
24465 case QImode:
24466 result = (unsigned_p ? unsigned_V16QI_type_node : V16QI_type_node);
24467 break;
24468 case SFmode: result = V4SF_type_node; break;
24469 case DFmode: result = V2DF_type_node; break;
24470 /* If the user says 'vector int bool', we may be handed the 'bool'
24471 attribute _before_ the 'vector' attribute, and so select the
24472 proper type in the 'b' case below. */
24473 case V4SImode: case V8HImode: case V16QImode: case V4SFmode:
24474 case V2DImode: case V2DFmode:
24475 result = type;
24476 default: break;
24478 break;
24479 case 'b':
24480 switch (mode)
24482 case DImode: case V2DImode: result = bool_V2DI_type_node; break;
24483 case SImode: case V4SImode: result = bool_V4SI_type_node; break;
24484 case HImode: case V8HImode: result = bool_V8HI_type_node; break;
24485 case QImode: case V16QImode: result = bool_V16QI_type_node;
24486 default: break;
24488 break;
24489 case 'p':
24490 switch (mode)
24492 case V8HImode: result = pixel_V8HI_type_node;
24493 default: break;
24495 default: break;
24498 /* Propagate qualifiers attached to the element type
24499 onto the vector type. */
24500 if (result && result != type && TYPE_QUALS (type))
24501 result = build_qualified_type (result, TYPE_QUALS (type));
24503 *no_add_attrs = true; /* No need to hang on to the attribute. */
24505 if (result)
24506 *node = lang_hooks.types.reconstruct_complex_type (*node, result);
24508 return NULL_TREE;
24511 /* AltiVec defines four built-in scalar types that serve as vector
24512 elements; we must teach the compiler how to mangle them. */
24514 static const char *
24515 rs6000_mangle_type (const_tree type)
24517 type = TYPE_MAIN_VARIANT (type);
24519 if (TREE_CODE (type) != VOID_TYPE && TREE_CODE (type) != BOOLEAN_TYPE
24520 && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
24521 return NULL;
24523 if (type == bool_char_type_node) return "U6__boolc";
24524 if (type == bool_short_type_node) return "U6__bools";
24525 if (type == pixel_type_node) return "u7__pixel";
24526 if (type == bool_int_type_node) return "U6__booli";
24527 if (type == bool_long_type_node) return "U6__booll";
24529 /* Mangle IBM extended float long double as `g' (__float128) on
24530 powerpc*-linux where long-double-64 previously was the default. */
24531 if (TYPE_MAIN_VARIANT (type) == long_double_type_node
24532 && TARGET_ELF
24533 && TARGET_LONG_DOUBLE_128
24534 && !TARGET_IEEEQUAD)
24535 return "g";
24537 /* For all other types, use normal C++ mangling. */
24538 return NULL;
24541 /* Handle a "longcall" or "shortcall" attribute; arguments as in
24542 struct attribute_spec.handler. */
24544 static tree
24545 rs6000_handle_longcall_attribute (tree *node, tree name,
24546 tree args ATTRIBUTE_UNUSED,
24547 int flags ATTRIBUTE_UNUSED,
24548 bool *no_add_attrs)
24550 if (TREE_CODE (*node) != FUNCTION_TYPE
24551 && TREE_CODE (*node) != FIELD_DECL
24552 && TREE_CODE (*node) != TYPE_DECL)
24554 warning (OPT_Wattributes, "%qE attribute only applies to functions",
24555 name);
24556 *no_add_attrs = true;
24559 return NULL_TREE;
24562 /* Set longcall attributes on all functions declared when
24563 rs6000_default_long_calls is true. */
24564 static void
24565 rs6000_set_default_type_attributes (tree type)
24567 if (rs6000_default_long_calls
24568 && (TREE_CODE (type) == FUNCTION_TYPE
24569 || TREE_CODE (type) == METHOD_TYPE))
24570 TYPE_ATTRIBUTES (type) = tree_cons (get_identifier ("longcall"),
24571 NULL_TREE,
24572 TYPE_ATTRIBUTES (type));
24574 #if TARGET_MACHO
24575 darwin_set_default_type_attributes (type);
24576 #endif
24579 /* Return a reference suitable for calling a function with the
24580 longcall attribute. */
24583 rs6000_longcall_ref (rtx call_ref)
24585 const char *call_name;
24586 tree node;
24588 if (GET_CODE (call_ref) != SYMBOL_REF)
24589 return call_ref;
24591 /* System V adds '.' to the internal name, so skip them. */
24592 call_name = XSTR (call_ref, 0);
24593 if (*call_name == '.')
24595 while (*call_name == '.')
24596 call_name++;
24598 node = get_identifier (call_name);
24599 call_ref = gen_rtx_SYMBOL_REF (VOIDmode, IDENTIFIER_POINTER (node));
24602 return force_reg (Pmode, call_ref);
24605 #ifndef TARGET_USE_MS_BITFIELD_LAYOUT
24606 #define TARGET_USE_MS_BITFIELD_LAYOUT 0
24607 #endif
24609 /* Handle a "ms_struct" or "gcc_struct" attribute; arguments as in
24610 struct attribute_spec.handler. */
24611 static tree
24612 rs6000_handle_struct_attribute (tree *node, tree name,
24613 tree args ATTRIBUTE_UNUSED,
24614 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
24616 tree *type = NULL;
24617 if (DECL_P (*node))
24619 if (TREE_CODE (*node) == TYPE_DECL)
24620 type = &TREE_TYPE (*node);
24622 else
24623 type = node;
24625 if (!(type && (TREE_CODE (*type) == RECORD_TYPE
24626 || TREE_CODE (*type) == UNION_TYPE)))
24628 warning (OPT_Wattributes, "%qE attribute ignored", name);
24629 *no_add_attrs = true;
24632 else if ((is_attribute_p ("ms_struct", name)
24633 && lookup_attribute ("gcc_struct", TYPE_ATTRIBUTES (*type)))
24634 || ((is_attribute_p ("gcc_struct", name)
24635 && lookup_attribute ("ms_struct", TYPE_ATTRIBUTES (*type)))))
24637 warning (OPT_Wattributes, "%qE incompatible attribute ignored",
24638 name);
24639 *no_add_attrs = true;
24642 return NULL_TREE;
24645 static bool
24646 rs6000_ms_bitfield_layout_p (const_tree record_type)
24648 return (TARGET_USE_MS_BITFIELD_LAYOUT &&
24649 !lookup_attribute ("gcc_struct", TYPE_ATTRIBUTES (record_type)))
24650 || lookup_attribute ("ms_struct", TYPE_ATTRIBUTES (record_type));
24653 #ifdef USING_ELFOS_H
24655 /* A get_unnamed_section callback, used for switching to toc_section. */
24657 static void
24658 rs6000_elf_output_toc_section_asm_op (const void *data ATTRIBUTE_UNUSED)
24660 if (DEFAULT_ABI == ABI_AIX
24661 && TARGET_MINIMAL_TOC
24662 && !TARGET_RELOCATABLE)
24664 if (!toc_initialized)
24666 toc_initialized = 1;
24667 fprintf (asm_out_file, "%s\n", TOC_SECTION_ASM_OP);
24668 (*targetm.asm_out.internal_label) (asm_out_file, "LCTOC", 0);
24669 fprintf (asm_out_file, "\t.tc ");
24670 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (asm_out_file, "LCTOC1[TC],");
24671 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (asm_out_file, "LCTOC1");
24672 fprintf (asm_out_file, "\n");
24674 fprintf (asm_out_file, "%s\n", MINIMAL_TOC_SECTION_ASM_OP);
24675 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (asm_out_file, "LCTOC1");
24676 fprintf (asm_out_file, " = .+32768\n");
24678 else
24679 fprintf (asm_out_file, "%s\n", MINIMAL_TOC_SECTION_ASM_OP);
24681 else if (DEFAULT_ABI == ABI_AIX && !TARGET_RELOCATABLE)
24682 fprintf (asm_out_file, "%s\n", TOC_SECTION_ASM_OP);
24683 else
24685 fprintf (asm_out_file, "%s\n", MINIMAL_TOC_SECTION_ASM_OP);
24686 if (!toc_initialized)
24688 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (asm_out_file, "LCTOC1");
24689 fprintf (asm_out_file, " = .+32768\n");
24690 toc_initialized = 1;
24695 /* Implement TARGET_ASM_INIT_SECTIONS. */
24697 static void
24698 rs6000_elf_asm_init_sections (void)
24700 toc_section
24701 = get_unnamed_section (0, rs6000_elf_output_toc_section_asm_op, NULL);
24703 sdata2_section
24704 = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
24705 SDATA2_SECTION_ASM_OP);
24708 /* Implement TARGET_SELECT_RTX_SECTION. */
24710 static section *
24711 rs6000_elf_select_rtx_section (enum machine_mode mode, rtx x,
24712 unsigned HOST_WIDE_INT align)
24714 if (ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (x, mode))
24715 return toc_section;
24716 else
24717 return default_elf_select_rtx_section (mode, x, align);
24720 /* For a SYMBOL_REF, set generic flags and then perform some
24721 target-specific processing.
24723 When the AIX ABI is requested on a non-AIX system, replace the
24724 function name with the real name (with a leading .) rather than the
24725 function descriptor name. This saves a lot of overriding code to
24726 read the prefixes. */
24728 static void rs6000_elf_encode_section_info (tree, rtx, int) ATTRIBUTE_UNUSED;
24729 static void
24730 rs6000_elf_encode_section_info (tree decl, rtx rtl, int first)
24732 default_encode_section_info (decl, rtl, first);
24734 if (first
24735 && TREE_CODE (decl) == FUNCTION_DECL
24736 && !TARGET_AIX
24737 && DEFAULT_ABI == ABI_AIX)
24739 rtx sym_ref = XEXP (rtl, 0);
24740 size_t len = strlen (XSTR (sym_ref, 0));
24741 char *str = XALLOCAVEC (char, len + 2);
24742 str[0] = '.';
24743 memcpy (str + 1, XSTR (sym_ref, 0), len + 1);
24744 XSTR (sym_ref, 0) = ggc_alloc_string (str, len + 1);
24748 static inline bool
24749 compare_section_name (const char *section, const char *templ)
24751 int len;
24753 len = strlen (templ);
24754 return (strncmp (section, templ, len) == 0
24755 && (section[len] == 0 || section[len] == '.'));
24758 bool
24759 rs6000_elf_in_small_data_p (const_tree decl)
24761 if (rs6000_sdata == SDATA_NONE)
24762 return false;
24764 /* We want to merge strings, so we never consider them small data. */
24765 if (TREE_CODE (decl) == STRING_CST)
24766 return false;
24768 /* Functions are never in the small data area. */
24769 if (TREE_CODE (decl) == FUNCTION_DECL)
24770 return false;
24772 if (TREE_CODE (decl) == VAR_DECL && DECL_SECTION_NAME (decl))
24774 const char *section = TREE_STRING_POINTER (DECL_SECTION_NAME (decl));
24775 if (compare_section_name (section, ".sdata")
24776 || compare_section_name (section, ".sdata2")
24777 || compare_section_name (section, ".gnu.linkonce.s")
24778 || compare_section_name (section, ".sbss")
24779 || compare_section_name (section, ".sbss2")
24780 || compare_section_name (section, ".gnu.linkonce.sb")
24781 || strcmp (section, ".PPC.EMB.sdata0") == 0
24782 || strcmp (section, ".PPC.EMB.sbss0") == 0)
24783 return true;
24785 else
24787 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (decl));
24789 if (size > 0
24790 && size <= g_switch_value
24791 /* If it's not public, and we're not going to reference it there,
24792 there's no need to put it in the small data section. */
24793 && (rs6000_sdata != SDATA_DATA || TREE_PUBLIC (decl)))
24794 return true;
24797 return false;
24800 #endif /* USING_ELFOS_H */
24802 /* Implement TARGET_USE_BLOCKS_FOR_CONSTANT_P. */
24804 static bool
24805 rs6000_use_blocks_for_constant_p (enum machine_mode mode, const_rtx x)
24807 return !ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (x, mode);
24810 /* Return a REG that occurs in ADDR with coefficient 1.
24811 ADDR can be effectively incremented by incrementing REG.
24813 r0 is special and we must not select it as an address
24814 register by this routine since our caller will try to
24815 increment the returned register via an "la" instruction. */
24818 find_addr_reg (rtx addr)
24820 while (GET_CODE (addr) == PLUS)
24822 if (GET_CODE (XEXP (addr, 0)) == REG
24823 && REGNO (XEXP (addr, 0)) != 0)
24824 addr = XEXP (addr, 0);
24825 else if (GET_CODE (XEXP (addr, 1)) == REG
24826 && REGNO (XEXP (addr, 1)) != 0)
24827 addr = XEXP (addr, 1);
24828 else if (CONSTANT_P (XEXP (addr, 0)))
24829 addr = XEXP (addr, 1);
24830 else if (CONSTANT_P (XEXP (addr, 1)))
24831 addr = XEXP (addr, 0);
24832 else
24833 gcc_unreachable ();
24835 gcc_assert (GET_CODE (addr) == REG && REGNO (addr) != 0);
24836 return addr;
24839 void
24840 rs6000_fatal_bad_address (rtx op)
24842 fatal_insn ("bad address", op);
24845 #if TARGET_MACHO
24847 typedef struct branch_island_d {
24848 tree function_name;
24849 tree label_name;
24850 int line_number;
24851 } branch_island;
24853 DEF_VEC_O(branch_island);
24854 DEF_VEC_ALLOC_O(branch_island,gc);
24856 static VEC(branch_island,gc) *branch_islands;
24858 /* Remember to generate a branch island for far calls to the given
24859 function. */
24861 static void
24862 add_compiler_branch_island (tree label_name, tree function_name,
24863 int line_number)
24865 branch_island *bi = VEC_safe_push (branch_island, gc, branch_islands, NULL);
24867 bi->function_name = function_name;
24868 bi->label_name = label_name;
24869 bi->line_number = line_number;
24872 /* Generate far-jump branch islands for everything recorded in
24873 branch_islands. Invoked immediately after the last instruction of
24874 the epilogue has been emitted; the branch islands must be appended
24875 to, and contiguous with, the function body. Mach-O stubs are
24876 generated in machopic_output_stub(). */
24878 static void
24879 macho_branch_islands (void)
24881 char tmp_buf[512];
24883 while (!VEC_empty (branch_island, branch_islands))
24885 branch_island *bi = VEC_last (branch_island, branch_islands);
24886 const char *label = IDENTIFIER_POINTER (bi->label_name);
24887 const char *name = IDENTIFIER_POINTER (bi->function_name);
24888 char name_buf[512];
24889 /* Cheap copy of the details from the Darwin ASM_OUTPUT_LABELREF(). */
24890 if (name[0] == '*' || name[0] == '&')
24891 strcpy (name_buf, name+1);
24892 else
24894 name_buf[0] = '_';
24895 strcpy (name_buf+1, name);
24897 strcpy (tmp_buf, "\n");
24898 strcat (tmp_buf, label);
24899 #if defined (DBX_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)
24900 if (write_symbols == DBX_DEBUG || write_symbols == XCOFF_DEBUG)
24901 dbxout_stabd (N_SLINE, bi->line_number);
24902 #endif /* DBX_DEBUGGING_INFO || XCOFF_DEBUGGING_INFO */
24903 if (flag_pic)
24905 if (TARGET_LINK_STACK)
24907 char name[32];
24908 get_ppc476_thunk_name (name);
24909 strcat (tmp_buf, ":\n\tmflr r0\n\tbl ");
24910 strcat (tmp_buf, name);
24911 strcat (tmp_buf, "\n");
24912 strcat (tmp_buf, label);
24913 strcat (tmp_buf, "_pic:\n\tmflr r11\n");
24915 else
24917 strcat (tmp_buf, ":\n\tmflr r0\n\tbcl 20,31,");
24918 strcat (tmp_buf, label);
24919 strcat (tmp_buf, "_pic\n");
24920 strcat (tmp_buf, label);
24921 strcat (tmp_buf, "_pic:\n\tmflr r11\n");
24924 strcat (tmp_buf, "\taddis r11,r11,ha16(");
24925 strcat (tmp_buf, name_buf);
24926 strcat (tmp_buf, " - ");
24927 strcat (tmp_buf, label);
24928 strcat (tmp_buf, "_pic)\n");
24930 strcat (tmp_buf, "\tmtlr r0\n");
24932 strcat (tmp_buf, "\taddi r12,r11,lo16(");
24933 strcat (tmp_buf, name_buf);
24934 strcat (tmp_buf, " - ");
24935 strcat (tmp_buf, label);
24936 strcat (tmp_buf, "_pic)\n");
24938 strcat (tmp_buf, "\tmtctr r12\n\tbctr\n");
24940 else
24942 strcat (tmp_buf, ":\nlis r12,hi16(");
24943 strcat (tmp_buf, name_buf);
24944 strcat (tmp_buf, ")\n\tori r12,r12,lo16(");
24945 strcat (tmp_buf, name_buf);
24946 strcat (tmp_buf, ")\n\tmtctr r12\n\tbctr");
24948 output_asm_insn (tmp_buf, 0);
24949 #if defined (DBX_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)
24950 if (write_symbols == DBX_DEBUG || write_symbols == XCOFF_DEBUG)
24951 dbxout_stabd (N_SLINE, bi->line_number);
24952 #endif /* DBX_DEBUGGING_INFO || XCOFF_DEBUGGING_INFO */
24953 VEC_pop (branch_island, branch_islands);
24957 /* NO_PREVIOUS_DEF checks in the link list whether the function name is
24958 already there or not. */
24960 static int
24961 no_previous_def (tree function_name)
24963 branch_island *bi;
24964 unsigned ix;
24966 FOR_EACH_VEC_ELT (branch_island, branch_islands, ix, bi)
24967 if (function_name == bi->function_name)
24968 return 0;
24969 return 1;
24972 /* GET_PREV_LABEL gets the label name from the previous definition of
24973 the function. */
24975 static tree
24976 get_prev_label (tree function_name)
24978 branch_island *bi;
24979 unsigned ix;
24981 FOR_EACH_VEC_ELT (branch_island, branch_islands, ix, bi)
24982 if (function_name == bi->function_name)
24983 return bi->label_name;
24984 return NULL_TREE;
24987 /* INSN is either a function call or a millicode call. It may have an
24988 unconditional jump in its delay slot.
24990 CALL_DEST is the routine we are calling. */
24992 char *
24993 output_call (rtx insn, rtx *operands, int dest_operand_number,
24994 int cookie_operand_number)
24996 static char buf[256];
24997 if (darwin_emit_branch_islands
24998 && GET_CODE (operands[dest_operand_number]) == SYMBOL_REF
24999 && (INTVAL (operands[cookie_operand_number]) & CALL_LONG))
25001 tree labelname;
25002 tree funname = get_identifier (XSTR (operands[dest_operand_number], 0));
25004 if (no_previous_def (funname))
25006 rtx label_rtx = gen_label_rtx ();
25007 char *label_buf, temp_buf[256];
25008 ASM_GENERATE_INTERNAL_LABEL (temp_buf, "L",
25009 CODE_LABEL_NUMBER (label_rtx));
25010 label_buf = temp_buf[0] == '*' ? temp_buf + 1 : temp_buf;
25011 labelname = get_identifier (label_buf);
25012 add_compiler_branch_island (labelname, funname, insn_line (insn));
25014 else
25015 labelname = get_prev_label (funname);
25017 /* "jbsr foo, L42" is Mach-O for "Link as 'bl foo' if a 'bl'
25018 instruction will reach 'foo', otherwise link as 'bl L42'".
25019 "L42" should be a 'branch island', that will do a far jump to
25020 'foo'. Branch islands are generated in
25021 macho_branch_islands(). */
25022 sprintf (buf, "jbsr %%z%d,%.246s",
25023 dest_operand_number, IDENTIFIER_POINTER (labelname));
25025 else
25026 sprintf (buf, "bl %%z%d", dest_operand_number);
25027 return buf;
25030 /* Generate PIC and indirect symbol stubs. */
25032 void
25033 machopic_output_stub (FILE *file, const char *symb, const char *stub)
25035 unsigned int length;
25036 char *symbol_name, *lazy_ptr_name;
25037 char *local_label_0;
25038 static int label = 0;
25040 /* Lose our funky encoding stuff so it doesn't contaminate the stub. */
25041 symb = (*targetm.strip_name_encoding) (symb);
25044 length = strlen (symb);
25045 symbol_name = XALLOCAVEC (char, length + 32);
25046 GEN_SYMBOL_NAME_FOR_SYMBOL (symbol_name, symb, length);
25048 lazy_ptr_name = XALLOCAVEC (char, length + 32);
25049 GEN_LAZY_PTR_NAME_FOR_SYMBOL (lazy_ptr_name, symb, length);
25051 if (flag_pic == 2)
25052 switch_to_section (darwin_sections[machopic_picsymbol_stub1_section]);
25053 else
25054 switch_to_section (darwin_sections[machopic_symbol_stub1_section]);
25056 if (flag_pic == 2)
25058 fprintf (file, "\t.align 5\n");
25060 fprintf (file, "%s:\n", stub);
25061 fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
25063 label++;
25064 local_label_0 = XALLOCAVEC (char, sizeof ("\"L00000000000$spb\""));
25065 sprintf (local_label_0, "\"L%011d$spb\"", label);
25067 fprintf (file, "\tmflr r0\n");
25068 if (TARGET_LINK_STACK)
25070 char name[32];
25071 get_ppc476_thunk_name (name);
25072 fprintf (file, "\tbl %s\n", name);
25073 fprintf (file, "%s:\n\tmflr r11\n", local_label_0);
25075 else
25077 fprintf (file, "\tbcl 20,31,%s\n", local_label_0);
25078 fprintf (file, "%s:\n\tmflr r11\n", local_label_0);
25080 fprintf (file, "\taddis r11,r11,ha16(%s-%s)\n",
25081 lazy_ptr_name, local_label_0);
25082 fprintf (file, "\tmtlr r0\n");
25083 fprintf (file, "\t%s r12,lo16(%s-%s)(r11)\n",
25084 (TARGET_64BIT ? "ldu" : "lwzu"),
25085 lazy_ptr_name, local_label_0);
25086 fprintf (file, "\tmtctr r12\n");
25087 fprintf (file, "\tbctr\n");
25089 else
25091 fprintf (file, "\t.align 4\n");
25093 fprintf (file, "%s:\n", stub);
25094 fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
25096 fprintf (file, "\tlis r11,ha16(%s)\n", lazy_ptr_name);
25097 fprintf (file, "\t%s r12,lo16(%s)(r11)\n",
25098 (TARGET_64BIT ? "ldu" : "lwzu"),
25099 lazy_ptr_name);
25100 fprintf (file, "\tmtctr r12\n");
25101 fprintf (file, "\tbctr\n");
25104 switch_to_section (darwin_sections[machopic_lazy_symbol_ptr_section]);
25105 fprintf (file, "%s:\n", lazy_ptr_name);
25106 fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
25107 fprintf (file, "%sdyld_stub_binding_helper\n",
25108 (TARGET_64BIT ? DOUBLE_INT_ASM_OP : "\t.long\t"));
25111 /* Legitimize PIC addresses. If the address is already
25112 position-independent, we return ORIG. Newly generated
25113 position-independent addresses go into a reg. This is REG if non
25114 zero, otherwise we allocate register(s) as necessary. */
25116 #define SMALL_INT(X) ((UINTVAL (X) + 0x8000) < 0x10000)
25119 rs6000_machopic_legitimize_pic_address (rtx orig, enum machine_mode mode,
25120 rtx reg)
25122 rtx base, offset;
25124 if (reg == NULL && ! reload_in_progress && ! reload_completed)
25125 reg = gen_reg_rtx (Pmode);
25127 if (GET_CODE (orig) == CONST)
25129 rtx reg_temp;
25131 if (GET_CODE (XEXP (orig, 0)) == PLUS
25132 && XEXP (XEXP (orig, 0), 0) == pic_offset_table_rtx)
25133 return orig;
25135 gcc_assert (GET_CODE (XEXP (orig, 0)) == PLUS);
25137 /* Use a different reg for the intermediate value, as
25138 it will be marked UNCHANGING. */
25139 reg_temp = !can_create_pseudo_p () ? reg : gen_reg_rtx (Pmode);
25140 base = rs6000_machopic_legitimize_pic_address (XEXP (XEXP (orig, 0), 0),
25141 Pmode, reg_temp);
25142 offset =
25143 rs6000_machopic_legitimize_pic_address (XEXP (XEXP (orig, 0), 1),
25144 Pmode, reg);
25146 if (GET_CODE (offset) == CONST_INT)
25148 if (SMALL_INT (offset))
25149 return plus_constant (Pmode, base, INTVAL (offset));
25150 else if (! reload_in_progress && ! reload_completed)
25151 offset = force_reg (Pmode, offset);
25152 else
25154 rtx mem = force_const_mem (Pmode, orig);
25155 return machopic_legitimize_pic_address (mem, Pmode, reg);
25158 return gen_rtx_PLUS (Pmode, base, offset);
25161 /* Fall back on generic machopic code. */
25162 return machopic_legitimize_pic_address (orig, mode, reg);
25165 /* Output a .machine directive for the Darwin assembler, and call
25166 the generic start_file routine. */
25168 static void
25169 rs6000_darwin_file_start (void)
25171 static const struct
25173 const char *arg;
25174 const char *name;
25175 int if_set;
25176 } mapping[] = {
25177 { "ppc64", "ppc64", MASK_64BIT },
25178 { "970", "ppc970", MASK_PPC_GPOPT | MASK_MFCRF | MASK_POWERPC64 },
25179 { "power4", "ppc970", 0 },
25180 { "G5", "ppc970", 0 },
25181 { "7450", "ppc7450", 0 },
25182 { "7400", "ppc7400", MASK_ALTIVEC },
25183 { "G4", "ppc7400", 0 },
25184 { "750", "ppc750", 0 },
25185 { "740", "ppc750", 0 },
25186 { "G3", "ppc750", 0 },
25187 { "604e", "ppc604e", 0 },
25188 { "604", "ppc604", 0 },
25189 { "603e", "ppc603", 0 },
25190 { "603", "ppc603", 0 },
25191 { "601", "ppc601", 0 },
25192 { NULL, "ppc", 0 } };
25193 const char *cpu_id = "";
25194 size_t i;
25196 rs6000_file_start ();
25197 darwin_file_start ();
25199 /* Determine the argument to -mcpu=. Default to G3 if not specified. */
25201 if (rs6000_default_cpu != 0 && rs6000_default_cpu[0] != '\0')
25202 cpu_id = rs6000_default_cpu;
25204 if (global_options_set.x_rs6000_cpu_index)
25205 cpu_id = processor_target_table[rs6000_cpu_index].name;
25207 /* Look through the mapping array. Pick the first name that either
25208 matches the argument, has a bit set in IF_SET that is also set
25209 in the target flags, or has a NULL name. */
25211 i = 0;
25212 while (mapping[i].arg != NULL
25213 && strcmp (mapping[i].arg, cpu_id) != 0
25214 && (mapping[i].if_set & target_flags) == 0)
25215 i++;
25217 fprintf (asm_out_file, "\t.machine %s\n", mapping[i].name);
25220 #endif /* TARGET_MACHO */
25222 #if TARGET_ELF
25223 static int
25224 rs6000_elf_reloc_rw_mask (void)
25226 if (flag_pic)
25227 return 3;
25228 else if (DEFAULT_ABI == ABI_AIX)
25229 return 2;
25230 else
25231 return 0;
25234 /* Record an element in the table of global constructors. SYMBOL is
25235 a SYMBOL_REF of the function to be called; PRIORITY is a number
25236 between 0 and MAX_INIT_PRIORITY.
25238 This differs from default_named_section_asm_out_constructor in
25239 that we have special handling for -mrelocatable. */
25241 static void rs6000_elf_asm_out_constructor (rtx, int) ATTRIBUTE_UNUSED;
25242 static void
25243 rs6000_elf_asm_out_constructor (rtx symbol, int priority)
25245 const char *section = ".ctors";
25246 char buf[16];
25248 if (priority != DEFAULT_INIT_PRIORITY)
25250 sprintf (buf, ".ctors.%.5u",
25251 /* Invert the numbering so the linker puts us in the proper
25252 order; constructors are run from right to left, and the
25253 linker sorts in increasing order. */
25254 MAX_INIT_PRIORITY - priority);
25255 section = buf;
25258 switch_to_section (get_section (section, SECTION_WRITE, NULL));
25259 assemble_align (POINTER_SIZE);
25261 if (TARGET_RELOCATABLE)
25263 fputs ("\t.long (", asm_out_file);
25264 output_addr_const (asm_out_file, symbol);
25265 fputs (")@fixup\n", asm_out_file);
25267 else
25268 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
25271 static void rs6000_elf_asm_out_destructor (rtx, int) ATTRIBUTE_UNUSED;
25272 static void
25273 rs6000_elf_asm_out_destructor (rtx symbol, int priority)
25275 const char *section = ".dtors";
25276 char buf[16];
25278 if (priority != DEFAULT_INIT_PRIORITY)
25280 sprintf (buf, ".dtors.%.5u",
25281 /* Invert the numbering so the linker puts us in the proper
25282 order; constructors are run from right to left, and the
25283 linker sorts in increasing order. */
25284 MAX_INIT_PRIORITY - priority);
25285 section = buf;
25288 switch_to_section (get_section (section, SECTION_WRITE, NULL));
25289 assemble_align (POINTER_SIZE);
25291 if (TARGET_RELOCATABLE)
25293 fputs ("\t.long (", asm_out_file);
25294 output_addr_const (asm_out_file, symbol);
25295 fputs (")@fixup\n", asm_out_file);
25297 else
25298 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
25301 void
25302 rs6000_elf_declare_function_name (FILE *file, const char *name, tree decl)
25304 if (TARGET_64BIT)
25306 fputs ("\t.section\t\".opd\",\"aw\"\n\t.align 3\n", file);
25307 ASM_OUTPUT_LABEL (file, name);
25308 fputs (DOUBLE_INT_ASM_OP, file);
25309 rs6000_output_function_entry (file, name);
25310 fputs (",.TOC.@tocbase,0\n\t.previous\n", file);
25311 if (DOT_SYMBOLS)
25313 fputs ("\t.size\t", file);
25314 assemble_name (file, name);
25315 fputs (",24\n\t.type\t.", file);
25316 assemble_name (file, name);
25317 fputs (",@function\n", file);
25318 if (TREE_PUBLIC (decl) && ! DECL_WEAK (decl))
25320 fputs ("\t.globl\t.", file);
25321 assemble_name (file, name);
25322 putc ('\n', file);
25325 else
25326 ASM_OUTPUT_TYPE_DIRECTIVE (file, name, "function");
25327 ASM_DECLARE_RESULT (file, DECL_RESULT (decl));
25328 rs6000_output_function_entry (file, name);
25329 fputs (":\n", file);
25330 return;
25333 if (TARGET_RELOCATABLE
25334 && !TARGET_SECURE_PLT
25335 && (get_pool_size () != 0 || crtl->profile)
25336 && uses_TOC ())
25338 char buf[256];
25340 (*targetm.asm_out.internal_label) (file, "LCL", rs6000_pic_labelno);
25342 ASM_GENERATE_INTERNAL_LABEL (buf, "LCTOC", 1);
25343 fprintf (file, "\t.long ");
25344 assemble_name (file, buf);
25345 putc ('-', file);
25346 ASM_GENERATE_INTERNAL_LABEL (buf, "LCF", rs6000_pic_labelno);
25347 assemble_name (file, buf);
25348 putc ('\n', file);
25351 ASM_OUTPUT_TYPE_DIRECTIVE (file, name, "function");
25352 ASM_DECLARE_RESULT (file, DECL_RESULT (decl));
25354 if (DEFAULT_ABI == ABI_AIX)
25356 const char *desc_name, *orig_name;
25358 orig_name = (*targetm.strip_name_encoding) (name);
25359 desc_name = orig_name;
25360 while (*desc_name == '.')
25361 desc_name++;
25363 if (TREE_PUBLIC (decl))
25364 fprintf (file, "\t.globl %s\n", desc_name);
25366 fprintf (file, "%s\n", MINIMAL_TOC_SECTION_ASM_OP);
25367 fprintf (file, "%s:\n", desc_name);
25368 fprintf (file, "\t.long %s\n", orig_name);
25369 fputs ("\t.long _GLOBAL_OFFSET_TABLE_\n", file);
25370 if (DEFAULT_ABI == ABI_AIX)
25371 fputs ("\t.long 0\n", file);
25372 fprintf (file, "\t.previous\n");
25374 ASM_OUTPUT_LABEL (file, name);
25377 static void rs6000_elf_file_end (void) ATTRIBUTE_UNUSED;
25378 static void
25379 rs6000_elf_file_end (void)
25381 #ifdef HAVE_AS_GNU_ATTRIBUTE
25382 if (TARGET_32BIT && DEFAULT_ABI == ABI_V4)
25384 if (rs6000_passes_float)
25385 fprintf (asm_out_file, "\t.gnu_attribute 4, %d\n",
25386 ((TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT) ? 1
25387 : (TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_SINGLE_FLOAT) ? 3
25388 : 2));
25389 if (rs6000_passes_vector)
25390 fprintf (asm_out_file, "\t.gnu_attribute 8, %d\n",
25391 (TARGET_ALTIVEC_ABI ? 2
25392 : TARGET_SPE_ABI ? 3
25393 : 1));
25394 if (rs6000_returns_struct)
25395 fprintf (asm_out_file, "\t.gnu_attribute 12, %d\n",
25396 aix_struct_return ? 2 : 1);
25398 #endif
25399 #if defined (POWERPC_LINUX) || defined (POWERPC_FREEBSD)
25400 if (TARGET_32BIT)
25401 file_end_indicate_exec_stack ();
25402 #endif
25404 #endif
25406 #if TARGET_XCOFF
25407 static void
25408 rs6000_xcoff_asm_output_anchor (rtx symbol)
25410 char buffer[100];
25412 sprintf (buffer, "$ + " HOST_WIDE_INT_PRINT_DEC,
25413 SYMBOL_REF_BLOCK_OFFSET (symbol));
25414 ASM_OUTPUT_DEF (asm_out_file, XSTR (symbol, 0), buffer);
25417 static void
25418 rs6000_xcoff_asm_globalize_label (FILE *stream, const char *name)
25420 fputs (GLOBAL_ASM_OP, stream);
25421 RS6000_OUTPUT_BASENAME (stream, name);
25422 putc ('\n', stream);
25425 /* A get_unnamed_decl callback, used for read-only sections. PTR
25426 points to the section string variable. */
25428 static void
25429 rs6000_xcoff_output_readonly_section_asm_op (const void *directive)
25431 fprintf (asm_out_file, "\t.csect %s[RO],%s\n",
25432 *(const char *const *) directive,
25433 XCOFF_CSECT_DEFAULT_ALIGNMENT_STR);
25436 /* Likewise for read-write sections. */
25438 static void
25439 rs6000_xcoff_output_readwrite_section_asm_op (const void *directive)
25441 fprintf (asm_out_file, "\t.csect %s[RW],%s\n",
25442 *(const char *const *) directive,
25443 XCOFF_CSECT_DEFAULT_ALIGNMENT_STR);
25446 /* A get_unnamed_section callback, used for switching to toc_section. */
25448 static void
25449 rs6000_xcoff_output_toc_section_asm_op (const void *data ATTRIBUTE_UNUSED)
25451 if (TARGET_MINIMAL_TOC)
25453 /* toc_section is always selected at least once from
25454 rs6000_xcoff_file_start, so this is guaranteed to
25455 always be defined once and only once in each file. */
25456 if (!toc_initialized)
25458 fputs ("\t.toc\nLCTOC..1:\n", asm_out_file);
25459 fputs ("\t.tc toc_table[TC],toc_table[RW]\n", asm_out_file);
25460 toc_initialized = 1;
25462 fprintf (asm_out_file, "\t.csect toc_table[RW]%s\n",
25463 (TARGET_32BIT ? "" : ",3"));
25465 else
25466 fputs ("\t.toc\n", asm_out_file);
25469 /* Implement TARGET_ASM_INIT_SECTIONS. */
25471 static void
25472 rs6000_xcoff_asm_init_sections (void)
25474 read_only_data_section
25475 = get_unnamed_section (0, rs6000_xcoff_output_readonly_section_asm_op,
25476 &xcoff_read_only_section_name);
25478 private_data_section
25479 = get_unnamed_section (SECTION_WRITE,
25480 rs6000_xcoff_output_readwrite_section_asm_op,
25481 &xcoff_private_data_section_name);
25483 read_only_private_data_section
25484 = get_unnamed_section (0, rs6000_xcoff_output_readonly_section_asm_op,
25485 &xcoff_private_data_section_name);
25487 toc_section
25488 = get_unnamed_section (0, rs6000_xcoff_output_toc_section_asm_op, NULL);
25490 readonly_data_section = read_only_data_section;
25491 exception_section = data_section;
25494 static int
25495 rs6000_xcoff_reloc_rw_mask (void)
25497 return 3;
25500 static void
25501 rs6000_xcoff_asm_named_section (const char *name, unsigned int flags,
25502 tree decl ATTRIBUTE_UNUSED)
25504 int smclass;
25505 static const char * const suffix[3] = { "PR", "RO", "RW" };
25507 if (flags & SECTION_CODE)
25508 smclass = 0;
25509 else if (flags & SECTION_WRITE)
25510 smclass = 2;
25511 else
25512 smclass = 1;
25514 fprintf (asm_out_file, "\t.csect %s%s[%s],%u\n",
25515 (flags & SECTION_CODE) ? "." : "",
25516 name, suffix[smclass], flags & SECTION_ENTSIZE);
25519 static section *
25520 rs6000_xcoff_select_section (tree decl, int reloc,
25521 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
25523 if (decl_readonly_section (decl, reloc))
25525 if (TREE_PUBLIC (decl))
25526 return read_only_data_section;
25527 else
25528 return read_only_private_data_section;
25530 else
25532 if (TREE_PUBLIC (decl))
25533 return data_section;
25534 else
25535 return private_data_section;
25539 static void
25540 rs6000_xcoff_unique_section (tree decl, int reloc ATTRIBUTE_UNUSED)
25542 const char *name;
25544 /* Use select_section for private and uninitialized data. */
25545 if (!TREE_PUBLIC (decl)
25546 || DECL_COMMON (decl)
25547 || DECL_INITIAL (decl) == NULL_TREE
25548 || DECL_INITIAL (decl) == error_mark_node
25549 || (flag_zero_initialized_in_bss
25550 && initializer_zerop (DECL_INITIAL (decl))))
25551 return;
25553 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
25554 name = (*targetm.strip_name_encoding) (name);
25555 DECL_SECTION_NAME (decl) = build_string (strlen (name), name);
25558 /* Select section for constant in constant pool.
25560 On RS/6000, all constants are in the private read-only data area.
25561 However, if this is being placed in the TOC it must be output as a
25562 toc entry. */
25564 static section *
25565 rs6000_xcoff_select_rtx_section (enum machine_mode mode, rtx x,
25566 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
25568 if (ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (x, mode))
25569 return toc_section;
25570 else
25571 return read_only_private_data_section;
25574 /* Remove any trailing [DS] or the like from the symbol name. */
25576 static const char *
25577 rs6000_xcoff_strip_name_encoding (const char *name)
25579 size_t len;
25580 if (*name == '*')
25581 name++;
25582 len = strlen (name);
25583 if (name[len - 1] == ']')
25584 return ggc_alloc_string (name, len - 4);
25585 else
25586 return name;
25589 /* Section attributes. AIX is always PIC. */
25591 static unsigned int
25592 rs6000_xcoff_section_type_flags (tree decl, const char *name, int reloc)
25594 unsigned int align;
25595 unsigned int flags = default_section_type_flags (decl, name, reloc);
25597 /* Align to at least UNIT size. */
25598 if (flags & SECTION_CODE || !decl)
25599 align = MIN_UNITS_PER_WORD;
25600 else
25601 /* Increase alignment of large objects if not already stricter. */
25602 align = MAX ((DECL_ALIGN (decl) / BITS_PER_UNIT),
25603 int_size_in_bytes (TREE_TYPE (decl)) > MIN_UNITS_PER_WORD
25604 ? UNITS_PER_FP_WORD : MIN_UNITS_PER_WORD);
25606 return flags | (exact_log2 (align) & SECTION_ENTSIZE);
25609 /* Output at beginning of assembler file.
25611 Initialize the section names for the RS/6000 at this point.
25613 Specify filename, including full path, to assembler.
25615 We want to go into the TOC section so at least one .toc will be emitted.
25616 Also, in order to output proper .bs/.es pairs, we need at least one static
25617 [RW] section emitted.
25619 Finally, declare mcount when profiling to make the assembler happy. */
25621 static void
25622 rs6000_xcoff_file_start (void)
25624 rs6000_gen_section_name (&xcoff_bss_section_name,
25625 main_input_filename, ".bss_");
25626 rs6000_gen_section_name (&xcoff_private_data_section_name,
25627 main_input_filename, ".rw_");
25628 rs6000_gen_section_name (&xcoff_read_only_section_name,
25629 main_input_filename, ".ro_");
25631 fputs ("\t.file\t", asm_out_file);
25632 output_quoted_string (asm_out_file, main_input_filename);
25633 fputc ('\n', asm_out_file);
25634 if (write_symbols != NO_DEBUG)
25635 switch_to_section (private_data_section);
25636 switch_to_section (text_section);
25637 if (profile_flag)
25638 fprintf (asm_out_file, "\t.extern %s\n", RS6000_MCOUNT);
25639 rs6000_file_start ();
25642 /* Output at end of assembler file.
25643 On the RS/6000, referencing data should automatically pull in text. */
25645 static void
25646 rs6000_xcoff_file_end (void)
25648 switch_to_section (text_section);
25649 fputs ("_section_.text:\n", asm_out_file);
25650 switch_to_section (data_section);
25651 fputs (TARGET_32BIT
25652 ? "\t.long _section_.text\n" : "\t.llong _section_.text\n",
25653 asm_out_file);
25655 #endif /* TARGET_XCOFF */
25657 /* Compute a (partial) cost for rtx X. Return true if the complete
25658 cost has been computed, and false if subexpressions should be
25659 scanned. In either case, *TOTAL contains the cost result. */
25661 static bool
25662 rs6000_rtx_costs (rtx x, int code, int outer_code, int opno ATTRIBUTE_UNUSED,
25663 int *total, bool speed)
25665 enum machine_mode mode = GET_MODE (x);
25667 switch (code)
25669 /* On the RS/6000, if it is valid in the insn, it is free. */
25670 case CONST_INT:
25671 if (((outer_code == SET
25672 || outer_code == PLUS
25673 || outer_code == MINUS)
25674 && (satisfies_constraint_I (x)
25675 || satisfies_constraint_L (x)))
25676 || (outer_code == AND
25677 && (satisfies_constraint_K (x)
25678 || (mode == SImode
25679 ? satisfies_constraint_L (x)
25680 : satisfies_constraint_J (x))
25681 || mask_operand (x, mode)
25682 || (mode == DImode
25683 && mask64_operand (x, DImode))))
25684 || ((outer_code == IOR || outer_code == XOR)
25685 && (satisfies_constraint_K (x)
25686 || (mode == SImode
25687 ? satisfies_constraint_L (x)
25688 : satisfies_constraint_J (x))))
25689 || outer_code == ASHIFT
25690 || outer_code == ASHIFTRT
25691 || outer_code == LSHIFTRT
25692 || outer_code == ROTATE
25693 || outer_code == ROTATERT
25694 || outer_code == ZERO_EXTRACT
25695 || (outer_code == MULT
25696 && satisfies_constraint_I (x))
25697 || ((outer_code == DIV || outer_code == UDIV
25698 || outer_code == MOD || outer_code == UMOD)
25699 && exact_log2 (INTVAL (x)) >= 0)
25700 || (outer_code == COMPARE
25701 && (satisfies_constraint_I (x)
25702 || satisfies_constraint_K (x)))
25703 || ((outer_code == EQ || outer_code == NE)
25704 && (satisfies_constraint_I (x)
25705 || satisfies_constraint_K (x)
25706 || (mode == SImode
25707 ? satisfies_constraint_L (x)
25708 : satisfies_constraint_J (x))))
25709 || (outer_code == GTU
25710 && satisfies_constraint_I (x))
25711 || (outer_code == LTU
25712 && satisfies_constraint_P (x)))
25714 *total = 0;
25715 return true;
25717 else if ((outer_code == PLUS
25718 && reg_or_add_cint_operand (x, VOIDmode))
25719 || (outer_code == MINUS
25720 && reg_or_sub_cint_operand (x, VOIDmode))
25721 || ((outer_code == SET
25722 || outer_code == IOR
25723 || outer_code == XOR)
25724 && (INTVAL (x)
25725 & ~ (unsigned HOST_WIDE_INT) 0xffffffff) == 0))
25727 *total = COSTS_N_INSNS (1);
25728 return true;
25730 /* FALLTHRU */
25732 case CONST_DOUBLE:
25733 if (mode == DImode && code == CONST_DOUBLE)
25735 if ((outer_code == IOR || outer_code == XOR)
25736 && CONST_DOUBLE_HIGH (x) == 0
25737 && (CONST_DOUBLE_LOW (x)
25738 & ~ (unsigned HOST_WIDE_INT) 0xffff) == 0)
25740 *total = 0;
25741 return true;
25743 else if ((outer_code == AND && and64_2_operand (x, DImode))
25744 || ((outer_code == SET
25745 || outer_code == IOR
25746 || outer_code == XOR)
25747 && CONST_DOUBLE_HIGH (x) == 0))
25749 *total = COSTS_N_INSNS (1);
25750 return true;
25753 /* FALLTHRU */
25755 case CONST:
25756 case HIGH:
25757 case SYMBOL_REF:
25758 case MEM:
25759 /* When optimizing for size, MEM should be slightly more expensive
25760 than generating address, e.g., (plus (reg) (const)).
25761 L1 cache latency is about two instructions. */
25762 *total = !speed ? COSTS_N_INSNS (1) + 1 : COSTS_N_INSNS (2);
25763 return true;
25765 case LABEL_REF:
25766 *total = 0;
25767 return true;
25769 case PLUS:
25770 case MINUS:
25771 if (FLOAT_MODE_P (mode))
25772 *total = rs6000_cost->fp;
25773 else
25774 *total = COSTS_N_INSNS (1);
25775 return false;
25777 case MULT:
25778 if (GET_CODE (XEXP (x, 1)) == CONST_INT
25779 && satisfies_constraint_I (XEXP (x, 1)))
25781 if (INTVAL (XEXP (x, 1)) >= -256
25782 && INTVAL (XEXP (x, 1)) <= 255)
25783 *total = rs6000_cost->mulsi_const9;
25784 else
25785 *total = rs6000_cost->mulsi_const;
25787 else if (mode == SFmode)
25788 *total = rs6000_cost->fp;
25789 else if (FLOAT_MODE_P (mode))
25790 *total = rs6000_cost->dmul;
25791 else if (mode == DImode)
25792 *total = rs6000_cost->muldi;
25793 else
25794 *total = rs6000_cost->mulsi;
25795 return false;
25797 case FMA:
25798 if (mode == SFmode)
25799 *total = rs6000_cost->fp;
25800 else
25801 *total = rs6000_cost->dmul;
25802 break;
25804 case DIV:
25805 case MOD:
25806 if (FLOAT_MODE_P (mode))
25808 *total = mode == DFmode ? rs6000_cost->ddiv
25809 : rs6000_cost->sdiv;
25810 return false;
25812 /* FALLTHRU */
25814 case UDIV:
25815 case UMOD:
25816 if (GET_CODE (XEXP (x, 1)) == CONST_INT
25817 && exact_log2 (INTVAL (XEXP (x, 1))) >= 0)
25819 if (code == DIV || code == MOD)
25820 /* Shift, addze */
25821 *total = COSTS_N_INSNS (2);
25822 else
25823 /* Shift */
25824 *total = COSTS_N_INSNS (1);
25826 else
25828 if (GET_MODE (XEXP (x, 1)) == DImode)
25829 *total = rs6000_cost->divdi;
25830 else
25831 *total = rs6000_cost->divsi;
25833 /* Add in shift and subtract for MOD. */
25834 if (code == MOD || code == UMOD)
25835 *total += COSTS_N_INSNS (2);
25836 return false;
25838 case CTZ:
25839 case FFS:
25840 *total = COSTS_N_INSNS (4);
25841 return false;
25843 case POPCOUNT:
25844 *total = COSTS_N_INSNS (TARGET_POPCNTD ? 1 : 6);
25845 return false;
25847 case PARITY:
25848 *total = COSTS_N_INSNS (TARGET_CMPB ? 2 : 6);
25849 return false;
25851 case NOT:
25852 if (outer_code == AND || outer_code == IOR || outer_code == XOR)
25854 *total = 0;
25855 return false;
25857 /* FALLTHRU */
25859 case AND:
25860 case CLZ:
25861 case IOR:
25862 case XOR:
25863 case ZERO_EXTRACT:
25864 *total = COSTS_N_INSNS (1);
25865 return false;
25867 case ASHIFT:
25868 case ASHIFTRT:
25869 case LSHIFTRT:
25870 case ROTATE:
25871 case ROTATERT:
25872 /* Handle mul_highpart. */
25873 if (outer_code == TRUNCATE
25874 && GET_CODE (XEXP (x, 0)) == MULT)
25876 if (mode == DImode)
25877 *total = rs6000_cost->muldi;
25878 else
25879 *total = rs6000_cost->mulsi;
25880 return true;
25882 else if (outer_code == AND)
25883 *total = 0;
25884 else
25885 *total = COSTS_N_INSNS (1);
25886 return false;
25888 case SIGN_EXTEND:
25889 case ZERO_EXTEND:
25890 if (GET_CODE (XEXP (x, 0)) == MEM)
25891 *total = 0;
25892 else
25893 *total = COSTS_N_INSNS (1);
25894 return false;
25896 case COMPARE:
25897 case NEG:
25898 case ABS:
25899 if (!FLOAT_MODE_P (mode))
25901 *total = COSTS_N_INSNS (1);
25902 return false;
25904 /* FALLTHRU */
25906 case FLOAT:
25907 case UNSIGNED_FLOAT:
25908 case FIX:
25909 case UNSIGNED_FIX:
25910 case FLOAT_TRUNCATE:
25911 *total = rs6000_cost->fp;
25912 return false;
25914 case FLOAT_EXTEND:
25915 if (mode == DFmode)
25916 *total = 0;
25917 else
25918 *total = rs6000_cost->fp;
25919 return false;
25921 case UNSPEC:
25922 switch (XINT (x, 1))
25924 case UNSPEC_FRSP:
25925 *total = rs6000_cost->fp;
25926 return true;
25928 default:
25929 break;
25931 break;
25933 case CALL:
25934 case IF_THEN_ELSE:
25935 if (!speed)
25937 *total = COSTS_N_INSNS (1);
25938 return true;
25940 else if (FLOAT_MODE_P (mode)
25941 && TARGET_PPC_GFXOPT && TARGET_HARD_FLOAT && TARGET_FPRS)
25943 *total = rs6000_cost->fp;
25944 return false;
25946 break;
25948 case EQ:
25949 case GTU:
25950 case LTU:
25951 /* Carry bit requires mode == Pmode.
25952 NEG or PLUS already counted so only add one. */
25953 if (mode == Pmode
25954 && (outer_code == NEG || outer_code == PLUS))
25956 *total = COSTS_N_INSNS (1);
25957 return true;
25959 if (outer_code == SET)
25961 if (XEXP (x, 1) == const0_rtx)
25963 if (TARGET_ISEL && !TARGET_MFCRF)
25964 *total = COSTS_N_INSNS (8);
25965 else
25966 *total = COSTS_N_INSNS (2);
25967 return true;
25969 else if (mode == Pmode)
25971 *total = COSTS_N_INSNS (3);
25972 return false;
25975 /* FALLTHRU */
25977 case GT:
25978 case LT:
25979 case UNORDERED:
25980 if (outer_code == SET && (XEXP (x, 1) == const0_rtx))
25982 if (TARGET_ISEL && !TARGET_MFCRF)
25983 *total = COSTS_N_INSNS (8);
25984 else
25985 *total = COSTS_N_INSNS (2);
25986 return true;
25988 /* CC COMPARE. */
25989 if (outer_code == COMPARE)
25991 *total = 0;
25992 return true;
25994 break;
25996 default:
25997 break;
26000 return false;
26003 /* Debug form of r6000_rtx_costs that is selected if -mdebug=cost. */
26005 static bool
26006 rs6000_debug_rtx_costs (rtx x, int code, int outer_code, int opno, int *total,
26007 bool speed)
26009 bool ret = rs6000_rtx_costs (x, code, outer_code, opno, total, speed);
26011 fprintf (stderr,
26012 "\nrs6000_rtx_costs, return = %s, code = %s, outer_code = %s, "
26013 "opno = %d, total = %d, speed = %s, x:\n",
26014 ret ? "complete" : "scan inner",
26015 GET_RTX_NAME (code),
26016 GET_RTX_NAME (outer_code),
26017 opno,
26018 *total,
26019 speed ? "true" : "false");
26021 debug_rtx (x);
26023 return ret;
26026 /* Debug form of ADDRESS_COST that is selected if -mdebug=cost. */
26028 static int
26029 rs6000_debug_address_cost (rtx x, bool speed)
26031 int ret = TARGET_ADDRESS_COST (x, speed);
26033 fprintf (stderr, "\nrs6000_address_cost, return = %d, speed = %s, x:\n",
26034 ret, speed ? "true" : "false");
26035 debug_rtx (x);
26037 return ret;
26041 /* A C expression returning the cost of moving data from a register of class
26042 CLASS1 to one of CLASS2. */
26044 static int
26045 rs6000_register_move_cost (enum machine_mode mode,
26046 reg_class_t from, reg_class_t to)
26048 int ret;
26050 if (TARGET_DEBUG_COST)
26051 dbg_cost_ctrl++;
26053 /* Moves from/to GENERAL_REGS. */
26054 if (reg_classes_intersect_p (to, GENERAL_REGS)
26055 || reg_classes_intersect_p (from, GENERAL_REGS))
26057 reg_class_t rclass = from;
26059 if (! reg_classes_intersect_p (to, GENERAL_REGS))
26060 rclass = to;
26062 if (rclass == FLOAT_REGS || rclass == ALTIVEC_REGS || rclass == VSX_REGS)
26063 ret = (rs6000_memory_move_cost (mode, rclass, false)
26064 + rs6000_memory_move_cost (mode, GENERAL_REGS, false));
26066 /* It's more expensive to move CR_REGS than CR0_REGS because of the
26067 shift. */
26068 else if (rclass == CR_REGS)
26069 ret = 4;
26071 /* For those processors that have slow LR/CTR moves, make them more
26072 expensive than memory in order to bias spills to memory .*/
26073 else if ((rs6000_cpu == PROCESSOR_POWER6
26074 || rs6000_cpu == PROCESSOR_POWER7)
26075 && reg_classes_intersect_p (rclass, LINK_OR_CTR_REGS))
26076 ret = 6 * hard_regno_nregs[0][mode];
26078 else
26079 /* A move will cost one instruction per GPR moved. */
26080 ret = 2 * hard_regno_nregs[0][mode];
26083 /* If we have VSX, we can easily move between FPR or Altivec registers. */
26084 else if (VECTOR_UNIT_VSX_P (mode)
26085 && reg_classes_intersect_p (to, VSX_REGS)
26086 && reg_classes_intersect_p (from, VSX_REGS))
26087 ret = 2 * hard_regno_nregs[32][mode];
26089 /* Moving between two similar registers is just one instruction. */
26090 else if (reg_classes_intersect_p (to, from))
26091 ret = (mode == TFmode || mode == TDmode) ? 4 : 2;
26093 /* Everything else has to go through GENERAL_REGS. */
26094 else
26095 ret = (rs6000_register_move_cost (mode, GENERAL_REGS, to)
26096 + rs6000_register_move_cost (mode, from, GENERAL_REGS));
26098 if (TARGET_DEBUG_COST)
26100 if (dbg_cost_ctrl == 1)
26101 fprintf (stderr,
26102 "rs6000_register_move_cost:, ret=%d, mode=%s, from=%s, to=%s\n",
26103 ret, GET_MODE_NAME (mode), reg_class_names[from],
26104 reg_class_names[to]);
26105 dbg_cost_ctrl--;
26108 return ret;
26111 /* A C expressions returning the cost of moving data of MODE from a register to
26112 or from memory. */
26114 static int
26115 rs6000_memory_move_cost (enum machine_mode mode, reg_class_t rclass,
26116 bool in ATTRIBUTE_UNUSED)
26118 int ret;
26120 if (TARGET_DEBUG_COST)
26121 dbg_cost_ctrl++;
26123 if (reg_classes_intersect_p (rclass, GENERAL_REGS))
26124 ret = 4 * hard_regno_nregs[0][mode];
26125 else if (reg_classes_intersect_p (rclass, FLOAT_REGS))
26126 ret = 4 * hard_regno_nregs[32][mode];
26127 else if (reg_classes_intersect_p (rclass, ALTIVEC_REGS))
26128 ret = 4 * hard_regno_nregs[FIRST_ALTIVEC_REGNO][mode];
26129 else
26130 ret = 4 + rs6000_register_move_cost (mode, rclass, GENERAL_REGS);
26132 if (TARGET_DEBUG_COST)
26134 if (dbg_cost_ctrl == 1)
26135 fprintf (stderr,
26136 "rs6000_memory_move_cost: ret=%d, mode=%s, rclass=%s, in=%d\n",
26137 ret, GET_MODE_NAME (mode), reg_class_names[rclass], in);
26138 dbg_cost_ctrl--;
26141 return ret;
26144 /* Returns a code for a target-specific builtin that implements
26145 reciprocal of the function, or NULL_TREE if not available. */
26147 static tree
26148 rs6000_builtin_reciprocal (unsigned int fn, bool md_fn,
26149 bool sqrt ATTRIBUTE_UNUSED)
26151 if (optimize_insn_for_size_p ())
26152 return NULL_TREE;
26154 if (md_fn)
26155 switch (fn)
26157 case VSX_BUILTIN_XVSQRTDP:
26158 if (!RS6000_RECIP_AUTO_RSQRTE_P (V2DFmode))
26159 return NULL_TREE;
26161 return rs6000_builtin_decls[VSX_BUILTIN_RSQRT_2DF];
26163 case VSX_BUILTIN_XVSQRTSP:
26164 if (!RS6000_RECIP_AUTO_RSQRTE_P (V4SFmode))
26165 return NULL_TREE;
26167 return rs6000_builtin_decls[VSX_BUILTIN_RSQRT_4SF];
26169 default:
26170 return NULL_TREE;
26173 else
26174 switch (fn)
26176 case BUILT_IN_SQRT:
26177 if (!RS6000_RECIP_AUTO_RSQRTE_P (DFmode))
26178 return NULL_TREE;
26180 return rs6000_builtin_decls[RS6000_BUILTIN_RSQRT];
26182 case BUILT_IN_SQRTF:
26183 if (!RS6000_RECIP_AUTO_RSQRTE_P (SFmode))
26184 return NULL_TREE;
26186 return rs6000_builtin_decls[RS6000_BUILTIN_RSQRTF];
26188 default:
26189 return NULL_TREE;
26193 /* Load up a constant. If the mode is a vector mode, splat the value across
26194 all of the vector elements. */
26196 static rtx
26197 rs6000_load_constant_and_splat (enum machine_mode mode, REAL_VALUE_TYPE dconst)
26199 rtx reg;
26201 if (mode == SFmode || mode == DFmode)
26203 rtx d = CONST_DOUBLE_FROM_REAL_VALUE (dconst, mode);
26204 reg = force_reg (mode, d);
26206 else if (mode == V4SFmode)
26208 rtx d = CONST_DOUBLE_FROM_REAL_VALUE (dconst, SFmode);
26209 rtvec v = gen_rtvec (4, d, d, d, d);
26210 reg = gen_reg_rtx (mode);
26211 rs6000_expand_vector_init (reg, gen_rtx_PARALLEL (mode, v));
26213 else if (mode == V2DFmode)
26215 rtx d = CONST_DOUBLE_FROM_REAL_VALUE (dconst, DFmode);
26216 rtvec v = gen_rtvec (2, d, d);
26217 reg = gen_reg_rtx (mode);
26218 rs6000_expand_vector_init (reg, gen_rtx_PARALLEL (mode, v));
26220 else
26221 gcc_unreachable ();
26223 return reg;
26226 /* Generate an FMA instruction. */
26228 static void
26229 rs6000_emit_madd (rtx target, rtx m1, rtx m2, rtx a)
26231 enum machine_mode mode = GET_MODE (target);
26232 rtx dst;
26234 dst = expand_ternary_op (mode, fma_optab, m1, m2, a, target, 0);
26235 gcc_assert (dst != NULL);
26237 if (dst != target)
26238 emit_move_insn (target, dst);
26241 /* Generate a FMSUB instruction: dst = fma(m1, m2, -a). */
26243 static void
26244 rs6000_emit_msub (rtx target, rtx m1, rtx m2, rtx a)
26246 enum machine_mode mode = GET_MODE (target);
26247 rtx dst;
26249 /* Altivec does not support fms directly;
26250 generate in terms of fma in that case. */
26251 if (optab_handler (fms_optab, mode) != CODE_FOR_nothing)
26252 dst = expand_ternary_op (mode, fms_optab, m1, m2, a, target, 0);
26253 else
26255 a = expand_unop (mode, neg_optab, a, NULL_RTX, 0);
26256 dst = expand_ternary_op (mode, fma_optab, m1, m2, a, target, 0);
26258 gcc_assert (dst != NULL);
26260 if (dst != target)
26261 emit_move_insn (target, dst);
26264 /* Generate a FNMSUB instruction: dst = -fma(m1, m2, -a). */
26266 static void
26267 rs6000_emit_nmsub (rtx dst, rtx m1, rtx m2, rtx a)
26269 enum machine_mode mode = GET_MODE (dst);
26270 rtx r;
26272 /* This is a tad more complicated, since the fnma_optab is for
26273 a different expression: fma(-m1, m2, a), which is the same
26274 thing except in the case of signed zeros.
26276 Fortunately we know that if FMA is supported that FNMSUB is
26277 also supported in the ISA. Just expand it directly. */
26279 gcc_assert (optab_handler (fma_optab, mode) != CODE_FOR_nothing);
26281 r = gen_rtx_NEG (mode, a);
26282 r = gen_rtx_FMA (mode, m1, m2, r);
26283 r = gen_rtx_NEG (mode, r);
26284 emit_insn (gen_rtx_SET (VOIDmode, dst, r));
26287 /* Newton-Raphson approximation of floating point divide with just 2 passes
26288 (either single precision floating point, or newer machines with higher
26289 accuracy estimates). Support both scalar and vector divide. Assumes no
26290 trapping math and finite arguments. */
26292 static void
26293 rs6000_emit_swdiv_high_precision (rtx dst, rtx n, rtx d)
26295 enum machine_mode mode = GET_MODE (dst);
26296 rtx x0, e0, e1, y1, u0, v0;
26297 enum insn_code code = optab_handler (smul_optab, mode);
26298 gen_2arg_fn_t gen_mul = (gen_2arg_fn_t) GEN_FCN (code);
26299 rtx one = rs6000_load_constant_and_splat (mode, dconst1);
26301 gcc_assert (code != CODE_FOR_nothing);
26303 /* x0 = 1./d estimate */
26304 x0 = gen_reg_rtx (mode);
26305 emit_insn (gen_rtx_SET (VOIDmode, x0,
26306 gen_rtx_UNSPEC (mode, gen_rtvec (1, d),
26307 UNSPEC_FRES)));
26309 e0 = gen_reg_rtx (mode);
26310 rs6000_emit_nmsub (e0, d, x0, one); /* e0 = 1. - (d * x0) */
26312 e1 = gen_reg_rtx (mode);
26313 rs6000_emit_madd (e1, e0, e0, e0); /* e1 = (e0 * e0) + e0 */
26315 y1 = gen_reg_rtx (mode);
26316 rs6000_emit_madd (y1, e1, x0, x0); /* y1 = (e1 * x0) + x0 */
26318 u0 = gen_reg_rtx (mode);
26319 emit_insn (gen_mul (u0, n, y1)); /* u0 = n * y1 */
26321 v0 = gen_reg_rtx (mode);
26322 rs6000_emit_nmsub (v0, d, u0, n); /* v0 = n - (d * u0) */
26324 rs6000_emit_madd (dst, v0, y1, u0); /* dst = (v0 * y1) + u0 */
26327 /* Newton-Raphson approximation of floating point divide that has a low
26328 precision estimate. Assumes no trapping math and finite arguments. */
26330 static void
26331 rs6000_emit_swdiv_low_precision (rtx dst, rtx n, rtx d)
26333 enum machine_mode mode = GET_MODE (dst);
26334 rtx x0, e0, e1, e2, y1, y2, y3, u0, v0, one;
26335 enum insn_code code = optab_handler (smul_optab, mode);
26336 gen_2arg_fn_t gen_mul = (gen_2arg_fn_t) GEN_FCN (code);
26338 gcc_assert (code != CODE_FOR_nothing);
26340 one = rs6000_load_constant_and_splat (mode, dconst1);
26342 /* x0 = 1./d estimate */
26343 x0 = gen_reg_rtx (mode);
26344 emit_insn (gen_rtx_SET (VOIDmode, x0,
26345 gen_rtx_UNSPEC (mode, gen_rtvec (1, d),
26346 UNSPEC_FRES)));
26348 e0 = gen_reg_rtx (mode);
26349 rs6000_emit_nmsub (e0, d, x0, one); /* e0 = 1. - d * x0 */
26351 y1 = gen_reg_rtx (mode);
26352 rs6000_emit_madd (y1, e0, x0, x0); /* y1 = x0 + e0 * x0 */
26354 e1 = gen_reg_rtx (mode);
26355 emit_insn (gen_mul (e1, e0, e0)); /* e1 = e0 * e0 */
26357 y2 = gen_reg_rtx (mode);
26358 rs6000_emit_madd (y2, e1, y1, y1); /* y2 = y1 + e1 * y1 */
26360 e2 = gen_reg_rtx (mode);
26361 emit_insn (gen_mul (e2, e1, e1)); /* e2 = e1 * e1 */
26363 y3 = gen_reg_rtx (mode);
26364 rs6000_emit_madd (y3, e2, y2, y2); /* y3 = y2 + e2 * y2 */
26366 u0 = gen_reg_rtx (mode);
26367 emit_insn (gen_mul (u0, n, y3)); /* u0 = n * y3 */
26369 v0 = gen_reg_rtx (mode);
26370 rs6000_emit_nmsub (v0, d, u0, n); /* v0 = n - d * u0 */
26372 rs6000_emit_madd (dst, v0, y3, u0); /* dst = u0 + v0 * y3 */
26375 /* Newton-Raphson approximation of floating point divide DST = N/D. If NOTE_P,
26376 add a reg_note saying that this was a division. Support both scalar and
26377 vector divide. Assumes no trapping math and finite arguments. */
26379 void
26380 rs6000_emit_swdiv (rtx dst, rtx n, rtx d, bool note_p)
26382 enum machine_mode mode = GET_MODE (dst);
26384 if (RS6000_RECIP_HIGH_PRECISION_P (mode))
26385 rs6000_emit_swdiv_high_precision (dst, n, d);
26386 else
26387 rs6000_emit_swdiv_low_precision (dst, n, d);
26389 if (note_p)
26390 add_reg_note (get_last_insn (), REG_EQUAL, gen_rtx_DIV (mode, n, d));
26393 /* Newton-Raphson approximation of single/double-precision floating point
26394 rsqrt. Assumes no trapping math and finite arguments. */
26396 void
26397 rs6000_emit_swrsqrt (rtx dst, rtx src)
26399 enum machine_mode mode = GET_MODE (src);
26400 rtx x0 = gen_reg_rtx (mode);
26401 rtx y = gen_reg_rtx (mode);
26402 int passes = (TARGET_RECIP_PRECISION) ? 2 : 3;
26403 REAL_VALUE_TYPE dconst3_2;
26404 int i;
26405 rtx halfthree;
26406 enum insn_code code = optab_handler (smul_optab, mode);
26407 gen_2arg_fn_t gen_mul = (gen_2arg_fn_t) GEN_FCN (code);
26409 gcc_assert (code != CODE_FOR_nothing);
26411 /* Load up the constant 1.5 either as a scalar, or as a vector. */
26412 real_from_integer (&dconst3_2, VOIDmode, 3, 0, 0);
26413 SET_REAL_EXP (&dconst3_2, REAL_EXP (&dconst3_2) - 1);
26415 halfthree = rs6000_load_constant_and_splat (mode, dconst3_2);
26417 /* x0 = rsqrt estimate */
26418 emit_insn (gen_rtx_SET (VOIDmode, x0,
26419 gen_rtx_UNSPEC (mode, gen_rtvec (1, src),
26420 UNSPEC_RSQRT)));
26422 /* y = 0.5 * src = 1.5 * src - src -> fewer constants */
26423 rs6000_emit_msub (y, src, halfthree, src);
26425 for (i = 0; i < passes; i++)
26427 rtx x1 = gen_reg_rtx (mode);
26428 rtx u = gen_reg_rtx (mode);
26429 rtx v = gen_reg_rtx (mode);
26431 /* x1 = x0 * (1.5 - y * (x0 * x0)) */
26432 emit_insn (gen_mul (u, x0, x0));
26433 rs6000_emit_nmsub (v, y, u, halfthree);
26434 emit_insn (gen_mul (x1, x0, v));
26435 x0 = x1;
26438 emit_move_insn (dst, x0);
26439 return;
26442 /* Emit popcount intrinsic on TARGET_POPCNTB (Power5) and TARGET_POPCNTD
26443 (Power7) targets. DST is the target, and SRC is the argument operand. */
26445 void
26446 rs6000_emit_popcount (rtx dst, rtx src)
26448 enum machine_mode mode = GET_MODE (dst);
26449 rtx tmp1, tmp2;
26451 /* Use the PPC ISA 2.06 popcnt{w,d} instruction if we can. */
26452 if (TARGET_POPCNTD)
26454 if (mode == SImode)
26455 emit_insn (gen_popcntdsi2 (dst, src));
26456 else
26457 emit_insn (gen_popcntddi2 (dst, src));
26458 return;
26461 tmp1 = gen_reg_rtx (mode);
26463 if (mode == SImode)
26465 emit_insn (gen_popcntbsi2 (tmp1, src));
26466 tmp2 = expand_mult (SImode, tmp1, GEN_INT (0x01010101),
26467 NULL_RTX, 0);
26468 tmp2 = force_reg (SImode, tmp2);
26469 emit_insn (gen_lshrsi3 (dst, tmp2, GEN_INT (24)));
26471 else
26473 emit_insn (gen_popcntbdi2 (tmp1, src));
26474 tmp2 = expand_mult (DImode, tmp1,
26475 GEN_INT ((HOST_WIDE_INT)
26476 0x01010101 << 32 | 0x01010101),
26477 NULL_RTX, 0);
26478 tmp2 = force_reg (DImode, tmp2);
26479 emit_insn (gen_lshrdi3 (dst, tmp2, GEN_INT (56)));
26484 /* Emit parity intrinsic on TARGET_POPCNTB targets. DST is the
26485 target, and SRC is the argument operand. */
26487 void
26488 rs6000_emit_parity (rtx dst, rtx src)
26490 enum machine_mode mode = GET_MODE (dst);
26491 rtx tmp;
26493 tmp = gen_reg_rtx (mode);
26495 /* Use the PPC ISA 2.05 prtyw/prtyd instruction if we can. */
26496 if (TARGET_CMPB)
26498 if (mode == SImode)
26500 emit_insn (gen_popcntbsi2 (tmp, src));
26501 emit_insn (gen_paritysi2_cmpb (dst, tmp));
26503 else
26505 emit_insn (gen_popcntbdi2 (tmp, src));
26506 emit_insn (gen_paritydi2_cmpb (dst, tmp));
26508 return;
26511 if (mode == SImode)
26513 /* Is mult+shift >= shift+xor+shift+xor? */
26514 if (rs6000_cost->mulsi_const >= COSTS_N_INSNS (3))
26516 rtx tmp1, tmp2, tmp3, tmp4;
26518 tmp1 = gen_reg_rtx (SImode);
26519 emit_insn (gen_popcntbsi2 (tmp1, src));
26521 tmp2 = gen_reg_rtx (SImode);
26522 emit_insn (gen_lshrsi3 (tmp2, tmp1, GEN_INT (16)));
26523 tmp3 = gen_reg_rtx (SImode);
26524 emit_insn (gen_xorsi3 (tmp3, tmp1, tmp2));
26526 tmp4 = gen_reg_rtx (SImode);
26527 emit_insn (gen_lshrsi3 (tmp4, tmp3, GEN_INT (8)));
26528 emit_insn (gen_xorsi3 (tmp, tmp3, tmp4));
26530 else
26531 rs6000_emit_popcount (tmp, src);
26532 emit_insn (gen_andsi3 (dst, tmp, const1_rtx));
26534 else
26536 /* Is mult+shift >= shift+xor+shift+xor+shift+xor? */
26537 if (rs6000_cost->muldi >= COSTS_N_INSNS (5))
26539 rtx tmp1, tmp2, tmp3, tmp4, tmp5, tmp6;
26541 tmp1 = gen_reg_rtx (DImode);
26542 emit_insn (gen_popcntbdi2 (tmp1, src));
26544 tmp2 = gen_reg_rtx (DImode);
26545 emit_insn (gen_lshrdi3 (tmp2, tmp1, GEN_INT (32)));
26546 tmp3 = gen_reg_rtx (DImode);
26547 emit_insn (gen_xordi3 (tmp3, tmp1, tmp2));
26549 tmp4 = gen_reg_rtx (DImode);
26550 emit_insn (gen_lshrdi3 (tmp4, tmp3, GEN_INT (16)));
26551 tmp5 = gen_reg_rtx (DImode);
26552 emit_insn (gen_xordi3 (tmp5, tmp3, tmp4));
26554 tmp6 = gen_reg_rtx (DImode);
26555 emit_insn (gen_lshrdi3 (tmp6, tmp5, GEN_INT (8)));
26556 emit_insn (gen_xordi3 (tmp, tmp5, tmp6));
26558 else
26559 rs6000_emit_popcount (tmp, src);
26560 emit_insn (gen_anddi3 (dst, tmp, const1_rtx));
26564 /* Expand an Altivec constant permutation. Return true if we match
26565 an efficient implementation; false to fall back to VPERM. */
26567 bool
26568 altivec_expand_vec_perm_const (rtx operands[4])
26570 struct altivec_perm_insn {
26571 enum insn_code impl;
26572 unsigned char perm[16];
26574 static const struct altivec_perm_insn patterns[] = {
26575 { CODE_FOR_altivec_vpkuhum,
26576 { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31 } },
26577 { CODE_FOR_altivec_vpkuwum,
26578 { 2, 3, 6, 7, 10, 11, 14, 15, 18, 19, 22, 23, 26, 27, 30, 31 } },
26579 { CODE_FOR_altivec_vmrghb,
26580 { 0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23 } },
26581 { CODE_FOR_altivec_vmrghh,
26582 { 0, 1, 16, 17, 2, 3, 18, 19, 4, 5, 20, 21, 6, 7, 22, 23 } },
26583 { CODE_FOR_altivec_vmrghw,
26584 { 0, 1, 2, 3, 16, 17, 18, 19, 4, 5, 6, 7, 20, 21, 22, 23 } },
26585 { CODE_FOR_altivec_vmrglb,
26586 { 8, 24, 9, 25, 10, 26, 11, 27, 12, 28, 13, 29, 14, 30, 15, 31 } },
26587 { CODE_FOR_altivec_vmrglh,
26588 { 8, 9, 24, 25, 10, 11, 26, 27, 12, 13, 28, 29, 14, 15, 30, 31 } },
26589 { CODE_FOR_altivec_vmrglw,
26590 { 8, 9, 10, 11, 24, 25, 26, 27, 12, 13, 14, 15, 28, 29, 30, 31 } }
26593 unsigned int i, j, elt, which;
26594 unsigned char perm[16];
26595 rtx target, op0, op1, sel, x;
26596 bool one_vec;
26598 target = operands[0];
26599 op0 = operands[1];
26600 op1 = operands[2];
26601 sel = operands[3];
26603 /* Unpack the constant selector. */
26604 for (i = which = 0; i < 16; ++i)
26606 rtx e = XVECEXP (sel, 0, i);
26607 elt = INTVAL (e) & 31;
26608 which |= (elt < 16 ? 1 : 2);
26609 perm[i] = elt;
26612 /* Simplify the constant selector based on operands. */
26613 switch (which)
26615 default:
26616 gcc_unreachable ();
26618 case 3:
26619 one_vec = false;
26620 if (!rtx_equal_p (op0, op1))
26621 break;
26622 /* FALLTHRU */
26624 case 2:
26625 for (i = 0; i < 16; ++i)
26626 perm[i] &= 15;
26627 op0 = op1;
26628 one_vec = true;
26629 break;
26631 case 1:
26632 op1 = op0;
26633 one_vec = true;
26634 break;
26637 /* Look for splat patterns. */
26638 if (one_vec)
26640 elt = perm[0];
26642 for (i = 0; i < 16; ++i)
26643 if (perm[i] != elt)
26644 break;
26645 if (i == 16)
26647 emit_insn (gen_altivec_vspltb (target, op0, GEN_INT (elt)));
26648 return true;
26651 if (elt % 2 == 0)
26653 for (i = 0; i < 16; i += 2)
26654 if (perm[i] != elt || perm[i + 1] != elt + 1)
26655 break;
26656 if (i == 16)
26658 x = gen_reg_rtx (V8HImode);
26659 emit_insn (gen_altivec_vsplth (x, gen_lowpart (V8HImode, op0),
26660 GEN_INT (elt / 2)));
26661 emit_move_insn (target, gen_lowpart (V16QImode, x));
26662 return true;
26666 if (elt % 4 == 0)
26668 for (i = 0; i < 16; i += 4)
26669 if (perm[i] != elt
26670 || perm[i + 1] != elt + 1
26671 || perm[i + 2] != elt + 2
26672 || perm[i + 3] != elt + 3)
26673 break;
26674 if (i == 16)
26676 x = gen_reg_rtx (V4SImode);
26677 emit_insn (gen_altivec_vspltw (x, gen_lowpart (V4SImode, op0),
26678 GEN_INT (elt / 4)));
26679 emit_move_insn (target, gen_lowpart (V16QImode, x));
26680 return true;
26685 /* Look for merge and pack patterns. */
26686 for (j = 0; j < ARRAY_SIZE (patterns); ++j)
26688 bool swapped;
26690 elt = patterns[j].perm[0];
26691 if (perm[0] == elt)
26692 swapped = false;
26693 else if (perm[0] == elt + 16)
26694 swapped = true;
26695 else
26696 continue;
26697 for (i = 1; i < 16; ++i)
26699 elt = patterns[j].perm[i];
26700 if (swapped)
26701 elt = (elt >= 16 ? elt - 16 : elt + 16);
26702 else if (one_vec && elt >= 16)
26703 elt -= 16;
26704 if (perm[i] != elt)
26705 break;
26707 if (i == 16)
26709 enum insn_code icode = patterns[j].impl;
26710 enum machine_mode omode = insn_data[icode].operand[0].mode;
26711 enum machine_mode imode = insn_data[icode].operand[1].mode;
26713 if (swapped)
26714 x = op0, op0 = op1, op1 = x;
26715 if (imode != V16QImode)
26717 op0 = gen_lowpart (imode, op0);
26718 op1 = gen_lowpart (imode, op1);
26720 if (omode == V16QImode)
26721 x = target;
26722 else
26723 x = gen_reg_rtx (omode);
26724 emit_insn (GEN_FCN (icode) (x, op0, op1));
26725 if (omode != V16QImode)
26726 emit_move_insn (target, gen_lowpart (V16QImode, x));
26727 return true;
26731 return false;
26734 /* Expand a Paired Single, VSX Permute Doubleword, or SPE constant permutation.
26735 Return true if we match an efficient implementation. */
26737 static bool
26738 rs6000_expand_vec_perm_const_1 (rtx target, rtx op0, rtx op1,
26739 unsigned char perm0, unsigned char perm1)
26741 rtx x;
26743 /* If both selectors come from the same operand, fold to single op. */
26744 if ((perm0 & 2) == (perm1 & 2))
26746 if (perm0 & 2)
26747 op0 = op1;
26748 else
26749 op1 = op0;
26751 /* If both operands are equal, fold to simpler permutation. */
26752 if (rtx_equal_p (op0, op1))
26754 perm0 = perm0 & 1;
26755 perm1 = (perm1 & 1) + 2;
26757 /* If the first selector comes from the second operand, swap. */
26758 else if (perm0 & 2)
26760 if (perm1 & 2)
26761 return false;
26762 perm0 -= 2;
26763 perm1 += 2;
26764 x = op0, op0 = op1, op1 = x;
26766 /* If the second selector does not come from the second operand, fail. */
26767 else if ((perm1 & 2) == 0)
26768 return false;
26770 /* Success! */
26771 if (target != NULL)
26773 enum machine_mode vmode, dmode;
26774 rtvec v;
26776 vmode = GET_MODE (target);
26777 gcc_assert (GET_MODE_NUNITS (vmode) == 2);
26778 dmode = mode_for_vector (GET_MODE_INNER (vmode), 4);
26780 x = gen_rtx_VEC_CONCAT (dmode, op0, op1);
26781 v = gen_rtvec (2, GEN_INT (perm0), GEN_INT (perm1));
26782 x = gen_rtx_VEC_SELECT (vmode, x, gen_rtx_PARALLEL (VOIDmode, v));
26783 emit_insn (gen_rtx_SET (VOIDmode, target, x));
26785 return true;
26788 bool
26789 rs6000_expand_vec_perm_const (rtx operands[4])
26791 rtx target, op0, op1, sel;
26792 unsigned char perm0, perm1;
26794 target = operands[0];
26795 op0 = operands[1];
26796 op1 = operands[2];
26797 sel = operands[3];
26799 /* Unpack the constant selector. */
26800 perm0 = INTVAL (XVECEXP (sel, 0, 0)) & 3;
26801 perm1 = INTVAL (XVECEXP (sel, 0, 1)) & 3;
26803 return rs6000_expand_vec_perm_const_1 (target, op0, op1, perm0, perm1);
26806 /* Test whether a constant permutation is supported. */
26808 static bool
26809 rs6000_vectorize_vec_perm_const_ok (enum machine_mode vmode,
26810 const unsigned char *sel)
26812 /* AltiVec (and thus VSX) can handle arbitrary permutations. */
26813 if (TARGET_ALTIVEC)
26814 return true;
26816 /* Check for ps_merge* or evmerge* insns. */
26817 if ((TARGET_PAIRED_FLOAT && vmode == V2SFmode)
26818 || (TARGET_SPE && vmode == V2SImode))
26820 rtx op0 = gen_raw_REG (vmode, LAST_VIRTUAL_REGISTER + 1);
26821 rtx op1 = gen_raw_REG (vmode, LAST_VIRTUAL_REGISTER + 2);
26822 return rs6000_expand_vec_perm_const_1 (NULL, op0, op1, sel[0], sel[1]);
26825 return false;
26828 /* A subroutine for rs6000_expand_extract_even & rs6000_expand_interleave. */
26830 static void
26831 rs6000_do_expand_vec_perm (rtx target, rtx op0, rtx op1,
26832 enum machine_mode vmode, unsigned nelt, rtx perm[])
26834 enum machine_mode imode;
26835 rtx x;
26837 imode = vmode;
26838 if (GET_MODE_CLASS (vmode) != MODE_VECTOR_INT)
26840 imode = GET_MODE_INNER (vmode);
26841 imode = mode_for_size (GET_MODE_BITSIZE (imode), MODE_INT, 0);
26842 imode = mode_for_vector (imode, nelt);
26845 x = gen_rtx_CONST_VECTOR (imode, gen_rtvec_v (nelt, perm));
26846 x = expand_vec_perm (vmode, op0, op1, x, target);
26847 if (x != target)
26848 emit_move_insn (target, x);
26851 /* Expand an extract even operation. */
26853 void
26854 rs6000_expand_extract_even (rtx target, rtx op0, rtx op1)
26856 enum machine_mode vmode = GET_MODE (target);
26857 unsigned i, nelt = GET_MODE_NUNITS (vmode);
26858 rtx perm[16];
26860 for (i = 0; i < nelt; i++)
26861 perm[i] = GEN_INT (i * 2);
26863 rs6000_do_expand_vec_perm (target, op0, op1, vmode, nelt, perm);
26866 /* Expand a vector interleave operation. */
26868 void
26869 rs6000_expand_interleave (rtx target, rtx op0, rtx op1, bool highp)
26871 enum machine_mode vmode = GET_MODE (target);
26872 unsigned i, high, nelt = GET_MODE_NUNITS (vmode);
26873 rtx perm[16];
26875 high = (highp == BYTES_BIG_ENDIAN ? 0 : nelt / 2);
26876 for (i = 0; i < nelt / 2; i++)
26878 perm[i * 2] = GEN_INT (i + high);
26879 perm[i * 2 + 1] = GEN_INT (i + nelt + high);
26882 rs6000_do_expand_vec_perm (target, op0, op1, vmode, nelt, perm);
26885 /* Return an RTX representing where to find the function value of a
26886 function returning MODE. */
26887 static rtx
26888 rs6000_complex_function_value (enum machine_mode mode)
26890 unsigned int regno;
26891 rtx r1, r2;
26892 enum machine_mode inner = GET_MODE_INNER (mode);
26893 unsigned int inner_bytes = GET_MODE_SIZE (inner);
26895 if (FLOAT_MODE_P (mode) && TARGET_HARD_FLOAT && TARGET_FPRS)
26896 regno = FP_ARG_RETURN;
26897 else
26899 regno = GP_ARG_RETURN;
26901 /* 32-bit is OK since it'll go in r3/r4. */
26902 if (TARGET_32BIT && inner_bytes >= 4)
26903 return gen_rtx_REG (mode, regno);
26906 if (inner_bytes >= 8)
26907 return gen_rtx_REG (mode, regno);
26909 r1 = gen_rtx_EXPR_LIST (inner, gen_rtx_REG (inner, regno),
26910 const0_rtx);
26911 r2 = gen_rtx_EXPR_LIST (inner, gen_rtx_REG (inner, regno + 1),
26912 GEN_INT (inner_bytes));
26913 return gen_rtx_PARALLEL (mode, gen_rtvec (2, r1, r2));
26916 /* Target hook for TARGET_FUNCTION_VALUE.
26918 On the SPE, both FPs and vectors are returned in r3.
26920 On RS/6000 an integer value is in r3 and a floating-point value is in
26921 fp1, unless -msoft-float. */
26923 static rtx
26924 rs6000_function_value (const_tree valtype,
26925 const_tree fn_decl_or_type ATTRIBUTE_UNUSED,
26926 bool outgoing ATTRIBUTE_UNUSED)
26928 enum machine_mode mode;
26929 unsigned int regno;
26931 /* Special handling for structs in darwin64. */
26932 if (TARGET_MACHO
26933 && rs6000_darwin64_struct_check_p (TYPE_MODE (valtype), valtype))
26935 CUMULATIVE_ARGS valcum;
26936 rtx valret;
26938 valcum.words = 0;
26939 valcum.fregno = FP_ARG_MIN_REG;
26940 valcum.vregno = ALTIVEC_ARG_MIN_REG;
26941 /* Do a trial code generation as if this were going to be passed as
26942 an argument; if any part goes in memory, we return NULL. */
26943 valret = rs6000_darwin64_record_arg (&valcum, valtype, true, /* retval= */ true);
26944 if (valret)
26945 return valret;
26946 /* Otherwise fall through to standard ABI rules. */
26949 if (TARGET_32BIT && TARGET_POWERPC64 && TYPE_MODE (valtype) == DImode)
26951 /* Long long return value need be split in -mpowerpc64, 32bit ABI. */
26952 return gen_rtx_PARALLEL (DImode,
26953 gen_rtvec (2,
26954 gen_rtx_EXPR_LIST (VOIDmode,
26955 gen_rtx_REG (SImode, GP_ARG_RETURN),
26956 const0_rtx),
26957 gen_rtx_EXPR_LIST (VOIDmode,
26958 gen_rtx_REG (SImode,
26959 GP_ARG_RETURN + 1),
26960 GEN_INT (4))));
26962 if (TARGET_32BIT && TARGET_POWERPC64 && TYPE_MODE (valtype) == DCmode)
26964 return gen_rtx_PARALLEL (DCmode,
26965 gen_rtvec (4,
26966 gen_rtx_EXPR_LIST (VOIDmode,
26967 gen_rtx_REG (SImode, GP_ARG_RETURN),
26968 const0_rtx),
26969 gen_rtx_EXPR_LIST (VOIDmode,
26970 gen_rtx_REG (SImode,
26971 GP_ARG_RETURN + 1),
26972 GEN_INT (4)),
26973 gen_rtx_EXPR_LIST (VOIDmode,
26974 gen_rtx_REG (SImode,
26975 GP_ARG_RETURN + 2),
26976 GEN_INT (8)),
26977 gen_rtx_EXPR_LIST (VOIDmode,
26978 gen_rtx_REG (SImode,
26979 GP_ARG_RETURN + 3),
26980 GEN_INT (12))));
26983 mode = TYPE_MODE (valtype);
26984 if ((INTEGRAL_TYPE_P (valtype) && GET_MODE_BITSIZE (mode) < BITS_PER_WORD)
26985 || POINTER_TYPE_P (valtype))
26986 mode = TARGET_32BIT ? SImode : DImode;
26988 if (DECIMAL_FLOAT_MODE_P (mode) && TARGET_HARD_FLOAT && TARGET_FPRS)
26989 /* _Decimal128 must use an even/odd register pair. */
26990 regno = (mode == TDmode) ? FP_ARG_RETURN + 1 : FP_ARG_RETURN;
26991 else if (SCALAR_FLOAT_TYPE_P (valtype) && TARGET_HARD_FLOAT && TARGET_FPRS
26992 && ((TARGET_SINGLE_FLOAT && (mode == SFmode)) || TARGET_DOUBLE_FLOAT))
26993 regno = FP_ARG_RETURN;
26994 else if (TREE_CODE (valtype) == COMPLEX_TYPE
26995 && targetm.calls.split_complex_arg)
26996 return rs6000_complex_function_value (mode);
26997 /* VSX is a superset of Altivec and adds V2DImode/V2DFmode. Since the same
26998 return register is used in both cases, and we won't see V2DImode/V2DFmode
26999 for pure altivec, combine the two cases. */
27000 else if (TREE_CODE (valtype) == VECTOR_TYPE
27001 && TARGET_ALTIVEC && TARGET_ALTIVEC_ABI
27002 && ALTIVEC_OR_VSX_VECTOR_MODE (mode))
27003 regno = ALTIVEC_ARG_RETURN;
27004 else if (TARGET_E500_DOUBLE && TARGET_HARD_FLOAT
27005 && (mode == DFmode || mode == DCmode
27006 || mode == TFmode || mode == TCmode))
27007 return spe_build_register_parallel (mode, GP_ARG_RETURN);
27008 else
27009 regno = GP_ARG_RETURN;
27011 return gen_rtx_REG (mode, regno);
27014 /* Define how to find the value returned by a library function
27015 assuming the value has mode MODE. */
27017 rs6000_libcall_value (enum machine_mode mode)
27019 unsigned int regno;
27021 if (TARGET_32BIT && TARGET_POWERPC64 && mode == DImode)
27023 /* Long long return value need be split in -mpowerpc64, 32bit ABI. */
27024 return gen_rtx_PARALLEL (DImode,
27025 gen_rtvec (2,
27026 gen_rtx_EXPR_LIST (VOIDmode,
27027 gen_rtx_REG (SImode, GP_ARG_RETURN),
27028 const0_rtx),
27029 gen_rtx_EXPR_LIST (VOIDmode,
27030 gen_rtx_REG (SImode,
27031 GP_ARG_RETURN + 1),
27032 GEN_INT (4))));
27035 if (DECIMAL_FLOAT_MODE_P (mode) && TARGET_HARD_FLOAT && TARGET_FPRS)
27036 /* _Decimal128 must use an even/odd register pair. */
27037 regno = (mode == TDmode) ? FP_ARG_RETURN + 1 : FP_ARG_RETURN;
27038 else if (SCALAR_FLOAT_MODE_P (mode)
27039 && TARGET_HARD_FLOAT && TARGET_FPRS
27040 && ((TARGET_SINGLE_FLOAT && mode == SFmode) || TARGET_DOUBLE_FLOAT))
27041 regno = FP_ARG_RETURN;
27042 /* VSX is a superset of Altivec and adds V2DImode/V2DFmode. Since the same
27043 return register is used in both cases, and we won't see V2DImode/V2DFmode
27044 for pure altivec, combine the two cases. */
27045 else if (ALTIVEC_OR_VSX_VECTOR_MODE (mode)
27046 && TARGET_ALTIVEC && TARGET_ALTIVEC_ABI)
27047 regno = ALTIVEC_ARG_RETURN;
27048 else if (COMPLEX_MODE_P (mode) && targetm.calls.split_complex_arg)
27049 return rs6000_complex_function_value (mode);
27050 else if (TARGET_E500_DOUBLE && TARGET_HARD_FLOAT
27051 && (mode == DFmode || mode == DCmode
27052 || mode == TFmode || mode == TCmode))
27053 return spe_build_register_parallel (mode, GP_ARG_RETURN);
27054 else
27055 regno = GP_ARG_RETURN;
27057 return gen_rtx_REG (mode, regno);
27061 /* Given FROM and TO register numbers, say whether this elimination is allowed.
27062 Frame pointer elimination is automatically handled.
27064 For the RS/6000, if frame pointer elimination is being done, we would like
27065 to convert ap into fp, not sp.
27067 We need r30 if -mminimal-toc was specified, and there are constant pool
27068 references. */
27070 static bool
27071 rs6000_can_eliminate (const int from, const int to)
27073 return (from == ARG_POINTER_REGNUM && to == STACK_POINTER_REGNUM
27074 ? ! frame_pointer_needed
27075 : from == RS6000_PIC_OFFSET_TABLE_REGNUM
27076 ? ! TARGET_MINIMAL_TOC || TARGET_NO_TOC || get_pool_size () == 0
27077 : true);
27080 /* Define the offset between two registers, FROM to be eliminated and its
27081 replacement TO, at the start of a routine. */
27082 HOST_WIDE_INT
27083 rs6000_initial_elimination_offset (int from, int to)
27085 rs6000_stack_t *info = rs6000_stack_info ();
27086 HOST_WIDE_INT offset;
27088 if (from == HARD_FRAME_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
27089 offset = info->push_p ? 0 : -info->total_size;
27090 else if (from == FRAME_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
27092 offset = info->push_p ? 0 : -info->total_size;
27093 if (FRAME_GROWS_DOWNWARD)
27094 offset += info->fixed_size + info->vars_size + info->parm_size;
27096 else if (from == FRAME_POINTER_REGNUM && to == HARD_FRAME_POINTER_REGNUM)
27097 offset = FRAME_GROWS_DOWNWARD
27098 ? info->fixed_size + info->vars_size + info->parm_size
27099 : 0;
27100 else if (from == ARG_POINTER_REGNUM && to == HARD_FRAME_POINTER_REGNUM)
27101 offset = info->total_size;
27102 else if (from == ARG_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
27103 offset = info->push_p ? info->total_size : 0;
27104 else if (from == RS6000_PIC_OFFSET_TABLE_REGNUM)
27105 offset = 0;
27106 else
27107 gcc_unreachable ();
27109 return offset;
27112 static rtx
27113 rs6000_dwarf_register_span (rtx reg)
27115 rtx parts[8];
27116 int i, words;
27117 unsigned regno = REGNO (reg);
27118 enum machine_mode mode = GET_MODE (reg);
27120 if (TARGET_SPE
27121 && regno < 32
27122 && (SPE_VECTOR_MODE (GET_MODE (reg))
27123 || (TARGET_E500_DOUBLE && FLOAT_MODE_P (mode)
27124 && mode != SFmode && mode != SDmode && mode != SCmode)))
27126 else
27127 return NULL_RTX;
27129 regno = REGNO (reg);
27131 /* The duality of the SPE register size wreaks all kinds of havoc.
27132 This is a way of distinguishing r0 in 32-bits from r0 in
27133 64-bits. */
27134 words = (GET_MODE_SIZE (mode) + UNITS_PER_FP_WORD - 1) / UNITS_PER_FP_WORD;
27135 gcc_assert (words <= 4);
27136 for (i = 0; i < words; i++, regno++)
27138 if (BYTES_BIG_ENDIAN)
27140 parts[2 * i] = gen_rtx_REG (SImode, regno + 1200);
27141 parts[2 * i + 1] = gen_rtx_REG (SImode, regno);
27143 else
27145 parts[2 * i] = gen_rtx_REG (SImode, regno);
27146 parts[2 * i + 1] = gen_rtx_REG (SImode, regno + 1200);
27150 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (words * 2, parts));
27153 /* Fill in sizes for SPE register high parts in table used by unwinder. */
27155 static void
27156 rs6000_init_dwarf_reg_sizes_extra (tree address)
27158 if (TARGET_SPE)
27160 int i;
27161 enum machine_mode mode = TYPE_MODE (char_type_node);
27162 rtx addr = expand_expr (address, NULL_RTX, VOIDmode, EXPAND_NORMAL);
27163 rtx mem = gen_rtx_MEM (BLKmode, addr);
27164 rtx value = gen_int_mode (4, mode);
27166 for (i = 1201; i < 1232; i++)
27168 int column = DWARF_REG_TO_UNWIND_COLUMN (i);
27169 HOST_WIDE_INT offset
27170 = DWARF_FRAME_REGNUM (column) * GET_MODE_SIZE (mode);
27172 emit_move_insn (adjust_address (mem, mode, offset), value);
27177 /* Map internal gcc register numbers to DWARF2 register numbers. */
27179 unsigned int
27180 rs6000_dbx_register_number (unsigned int regno)
27182 if (regno <= 63 || write_symbols != DWARF2_DEBUG)
27183 return regno;
27184 if (regno == MQ_REGNO)
27185 return 100;
27186 if (regno == LR_REGNO)
27187 return 108;
27188 if (regno == CTR_REGNO)
27189 return 109;
27190 if (CR_REGNO_P (regno))
27191 return regno - CR0_REGNO + 86;
27192 if (regno == CA_REGNO)
27193 return 101; /* XER */
27194 if (ALTIVEC_REGNO_P (regno))
27195 return regno - FIRST_ALTIVEC_REGNO + 1124;
27196 if (regno == VRSAVE_REGNO)
27197 return 356;
27198 if (regno == VSCR_REGNO)
27199 return 67;
27200 if (regno == SPE_ACC_REGNO)
27201 return 99;
27202 if (regno == SPEFSCR_REGNO)
27203 return 612;
27204 /* SPE high reg number. We get these values of regno from
27205 rs6000_dwarf_register_span. */
27206 gcc_assert (regno >= 1200 && regno < 1232);
27207 return regno;
27210 /* target hook eh_return_filter_mode */
27211 static enum machine_mode
27212 rs6000_eh_return_filter_mode (void)
27214 return TARGET_32BIT ? SImode : word_mode;
27217 /* Target hook for scalar_mode_supported_p. */
27218 static bool
27219 rs6000_scalar_mode_supported_p (enum machine_mode mode)
27221 if (DECIMAL_FLOAT_MODE_P (mode))
27222 return default_decimal_float_supported_p ();
27223 else
27224 return default_scalar_mode_supported_p (mode);
27227 /* Target hook for vector_mode_supported_p. */
27228 static bool
27229 rs6000_vector_mode_supported_p (enum machine_mode mode)
27232 if (TARGET_PAIRED_FLOAT && PAIRED_VECTOR_MODE (mode))
27233 return true;
27235 if (TARGET_SPE && SPE_VECTOR_MODE (mode))
27236 return true;
27238 else if (VECTOR_MEM_ALTIVEC_OR_VSX_P (mode))
27239 return true;
27241 else
27242 return false;
27245 /* Target hook for invalid_arg_for_unprototyped_fn. */
27246 static const char *
27247 invalid_arg_for_unprototyped_fn (const_tree typelist, const_tree funcdecl, const_tree val)
27249 return (!rs6000_darwin64_abi
27250 && typelist == 0
27251 && TREE_CODE (TREE_TYPE (val)) == VECTOR_TYPE
27252 && (funcdecl == NULL_TREE
27253 || (TREE_CODE (funcdecl) == FUNCTION_DECL
27254 && DECL_BUILT_IN_CLASS (funcdecl) != BUILT_IN_MD)))
27255 ? N_("AltiVec argument passed to unprototyped function")
27256 : NULL;
27259 /* For TARGET_SECURE_PLT 32-bit PIC code we can save PIC register
27260 setup by using __stack_chk_fail_local hidden function instead of
27261 calling __stack_chk_fail directly. Otherwise it is better to call
27262 __stack_chk_fail directly. */
27264 static tree ATTRIBUTE_UNUSED
27265 rs6000_stack_protect_fail (void)
27267 return (DEFAULT_ABI == ABI_V4 && TARGET_SECURE_PLT && flag_pic)
27268 ? default_hidden_stack_protect_fail ()
27269 : default_external_stack_protect_fail ();
27272 void
27273 rs6000_final_prescan_insn (rtx insn, rtx *operand ATTRIBUTE_UNUSED,
27274 int num_operands ATTRIBUTE_UNUSED)
27276 if (rs6000_warn_cell_microcode)
27278 const char *temp;
27279 int insn_code_number = recog_memoized (insn);
27280 location_t location = locator_location (INSN_LOCATOR (insn));
27282 /* Punt on insns we cannot recognize. */
27283 if (insn_code_number < 0)
27284 return;
27286 temp = get_insn_template (insn_code_number, insn);
27288 if (get_attr_cell_micro (insn) == CELL_MICRO_ALWAYS)
27289 warning_at (location, OPT_mwarn_cell_microcode,
27290 "emitting microcode insn %s\t[%s] #%d",
27291 temp, insn_data[INSN_CODE (insn)].name, INSN_UID (insn));
27292 else if (get_attr_cell_micro (insn) == CELL_MICRO_CONDITIONAL)
27293 warning_at (location, OPT_mwarn_cell_microcode,
27294 "emitting conditional microcode insn %s\t[%s] #%d",
27295 temp, insn_data[INSN_CODE (insn)].name, INSN_UID (insn));
27300 /* Mask options that we want to support inside of attribute((target)) and
27301 #pragma GCC target operations. Note, we do not include things like
27302 64/32-bit, endianess, hard/soft floating point, etc. that would have
27303 different calling sequences. */
27305 struct rs6000_opt_mask {
27306 const char *name; /* option name */
27307 int mask; /* mask to set */
27308 bool invert; /* invert sense of mask */
27309 bool valid_target; /* option is a target option */
27312 static struct rs6000_opt_mask const rs6000_opt_masks[] =
27314 { "altivec", MASK_ALTIVEC, false, true },
27315 { "cmpb", MASK_CMPB, false, true },
27316 { "dlmzb", MASK_DLMZB, false, true },
27317 { "fprnd", MASK_FPRND, false, true },
27318 { "hard-dfp", MASK_DFP, false, true },
27319 { "isel", MASK_ISEL, false, true },
27320 { "mfcrf", MASK_MFCRF, false, true },
27321 { "mfpgpr", MASK_MFPGPR, false, true },
27322 { "mulhw", MASK_MULHW, false, true },
27323 { "multiple", MASK_MULTIPLE, false, true },
27324 { "update", MASK_NO_UPDATE, true , true },
27325 { "popcntb", MASK_POPCNTB, false, true },
27326 { "popcntd", MASK_POPCNTD, false, true },
27327 { "powerpc-gfxopt", MASK_PPC_GFXOPT, false, true },
27328 { "powerpc-gpopt", MASK_PPC_GPOPT, false, true },
27329 { "recip-precision", MASK_RECIP_PRECISION, false, true },
27330 { "string", MASK_STRING, false, true },
27331 { "vsx", MASK_VSX, false, true },
27332 #ifdef MASK_64BIT
27333 #if TARGET_AIX_OS
27334 { "aix64", MASK_64BIT, false, false },
27335 { "aix32", MASK_64BIT, true, false },
27336 #else
27337 { "64", MASK_64BIT, false, false },
27338 { "32", MASK_64BIT, true, false },
27339 #endif
27340 #endif
27341 #ifdef MASK_EABI
27342 { "eabi", MASK_EABI, false, false },
27343 #endif
27344 #ifdef MASK_LITTLE_ENDIAN
27345 { "little", MASK_LITTLE_ENDIAN, false, false },
27346 { "big", MASK_LITTLE_ENDIAN, true, false },
27347 #endif
27348 #ifdef MASK_RELOCATABLE
27349 { "relocatable", MASK_RELOCATABLE, false, false },
27350 #endif
27351 #ifdef MASK_STRICT_ALIGN
27352 { "strict-align", MASK_STRICT_ALIGN, false, false },
27353 #endif
27354 { "power", MASK_POWER, false, false },
27355 { "power2", MASK_POWER2, false, false },
27356 { "powerpc", MASK_POWERPC, false, false },
27357 { "soft-float", MASK_SOFT_FLOAT, false, false },
27358 { "string", MASK_STRING, false, false },
27361 /* Builtin mask mapping for printing the flags. */
27362 static struct rs6000_opt_mask const rs6000_builtin_mask_names[] =
27364 { "altivec", RS6000_BTM_ALTIVEC, false, false },
27365 { "vsx", RS6000_BTM_VSX, false, false },
27366 { "spe", RS6000_BTM_SPE, false, false },
27367 { "paired", RS6000_BTM_PAIRED, false, false },
27368 { "fre", RS6000_BTM_FRE, false, false },
27369 { "fres", RS6000_BTM_FRES, false, false },
27370 { "frsqrte", RS6000_BTM_FRSQRTE, false, false },
27371 { "frsqrtes", RS6000_BTM_FRSQRTES, false, false },
27372 { "popcntd", RS6000_BTM_POPCNTD, false, false },
27373 { "powerpc", RS6000_BTM_POWERPC, false, false },
27374 { "cell", RS6000_BTM_CELL, false, false },
27377 /* Option variables that we want to support inside attribute((target)) and
27378 #pragma GCC target operations. */
27380 struct rs6000_opt_var {
27381 const char *name; /* option name */
27382 size_t global_offset; /* offset of the option in global_options. */
27383 size_t target_offset; /* offset of the option in target optiosn. */
27386 static struct rs6000_opt_var const rs6000_opt_vars[] =
27388 { "friz",
27389 offsetof (struct gcc_options, x_TARGET_FRIZ),
27390 offsetof (struct cl_target_option, x_TARGET_FRIZ), },
27391 { "avoid-indexed-addresses",
27392 offsetof (struct gcc_options, x_TARGET_AVOID_XFORM),
27393 offsetof (struct cl_target_option, x_TARGET_AVOID_XFORM) },
27394 { "paired",
27395 offsetof (struct gcc_options, x_rs6000_paired_float),
27396 offsetof (struct cl_target_option, x_rs6000_paired_float), },
27397 { "longcall",
27398 offsetof (struct gcc_options, x_rs6000_default_long_calls),
27399 offsetof (struct cl_target_option, x_rs6000_default_long_calls), },
27402 /* Inner function to handle attribute((target("..."))) and #pragma GCC target
27403 parsing. Return true if there were no errors. */
27405 static bool
27406 rs6000_inner_target_options (tree args, bool attr_p)
27408 bool ret = true;
27410 if (args == NULL_TREE)
27413 else if (TREE_CODE (args) == STRING_CST)
27415 char *p = ASTRDUP (TREE_STRING_POINTER (args));
27416 char *q;
27418 while ((q = strtok (p, ",")) != NULL)
27420 bool error_p = false;
27421 bool not_valid_p = false;
27422 const char *cpu_opt = NULL;
27424 p = NULL;
27425 if (strncmp (q, "cpu=", 4) == 0)
27427 int cpu_index = rs6000_cpu_name_lookup (q+4);
27428 if (cpu_index >= 0)
27429 rs6000_cpu_index = cpu_index;
27430 else
27432 error_p = true;
27433 cpu_opt = q+4;
27436 else if (strncmp (q, "tune=", 5) == 0)
27438 int tune_index = rs6000_cpu_name_lookup (q+5);
27439 if (tune_index >= 0)
27440 rs6000_tune_index = tune_index;
27441 else
27443 error_p = true;
27444 cpu_opt = q+5;
27447 else
27449 size_t i;
27450 bool invert = false;
27451 char *r = q;
27453 error_p = true;
27454 if (strncmp (r, "no-", 3) == 0)
27456 invert = true;
27457 r += 3;
27460 for (i = 0; i < ARRAY_SIZE (rs6000_opt_masks); i++)
27461 if (strcmp (r, rs6000_opt_masks[i].name) == 0)
27463 int mask = rs6000_opt_masks[i].mask;
27465 if (!rs6000_opt_masks[i].valid_target)
27466 not_valid_p = true;
27467 else
27469 error_p = false;
27470 target_flags_explicit |= mask;
27472 /* VSX needs altivec, so -mvsx automagically sets
27473 altivec. */
27474 if (mask == MASK_VSX && !invert)
27475 mask |= MASK_ALTIVEC;
27477 if (rs6000_opt_masks[i].invert)
27478 invert = !invert;
27480 if (invert)
27481 target_flags &= ~mask;
27482 else
27483 target_flags |= mask;
27485 break;
27488 if (error_p && !not_valid_p)
27490 for (i = 0; i < ARRAY_SIZE (rs6000_opt_vars); i++)
27491 if (strcmp (r, rs6000_opt_vars[i].name) == 0)
27493 size_t j = rs6000_opt_vars[i].global_offset;
27494 *((int *) ((char *)&global_options + j)) = !invert;
27495 error_p = false;
27496 break;
27501 if (error_p)
27503 const char *eprefix, *esuffix;
27505 ret = false;
27506 if (attr_p)
27508 eprefix = "__attribute__((__target__(";
27509 esuffix = ")))";
27511 else
27513 eprefix = "#pragma GCC target ";
27514 esuffix = "";
27517 if (cpu_opt)
27518 error ("invalid cpu \"%s\" for %s\"%s\"%s", cpu_opt, eprefix,
27519 q, esuffix);
27520 else if (not_valid_p)
27521 error ("%s\"%s\"%s is not allowed", eprefix, q, esuffix);
27522 else
27523 error ("%s\"%s\"%s is invalid", eprefix, q, esuffix);
27528 else if (TREE_CODE (args) == TREE_LIST)
27532 tree value = TREE_VALUE (args);
27533 if (value)
27535 bool ret2 = rs6000_inner_target_options (value, attr_p);
27536 if (!ret2)
27537 ret = false;
27539 args = TREE_CHAIN (args);
27541 while (args != NULL_TREE);
27544 else
27545 gcc_unreachable ();
27547 return ret;
27550 /* Print out the target options as a list for -mdebug=target. */
27552 static void
27553 rs6000_debug_target_options (tree args, const char *prefix)
27555 if (args == NULL_TREE)
27556 fprintf (stderr, "%s<NULL>", prefix);
27558 else if (TREE_CODE (args) == STRING_CST)
27560 char *p = ASTRDUP (TREE_STRING_POINTER (args));
27561 char *q;
27563 while ((q = strtok (p, ",")) != NULL)
27565 p = NULL;
27566 fprintf (stderr, "%s\"%s\"", prefix, q);
27567 prefix = ", ";
27571 else if (TREE_CODE (args) == TREE_LIST)
27575 tree value = TREE_VALUE (args);
27576 if (value)
27578 rs6000_debug_target_options (value, prefix);
27579 prefix = ", ";
27581 args = TREE_CHAIN (args);
27583 while (args != NULL_TREE);
27586 else
27587 gcc_unreachable ();
27589 return;
27593 /* Hook to validate attribute((target("..."))). */
27595 static bool
27596 rs6000_valid_attribute_p (tree fndecl,
27597 tree ARG_UNUSED (name),
27598 tree args,
27599 int flags)
27601 struct cl_target_option cur_target;
27602 bool ret;
27603 tree old_optimize = build_optimization_node ();
27604 tree new_target, new_optimize;
27605 tree func_optimize = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl);
27607 gcc_assert ((fndecl != NULL_TREE) && (args != NULL_TREE));
27609 if (TARGET_DEBUG_TARGET)
27611 tree tname = DECL_NAME (fndecl);
27612 fprintf (stderr, "\n==================== rs6000_valid_attribute_p:\n");
27613 if (tname)
27614 fprintf (stderr, "function: %.*s\n",
27615 (int) IDENTIFIER_LENGTH (tname),
27616 IDENTIFIER_POINTER (tname));
27617 else
27618 fprintf (stderr, "function: unknown\n");
27620 fprintf (stderr, "args:");
27621 rs6000_debug_target_options (args, " ");
27622 fprintf (stderr, "\n");
27624 if (flags)
27625 fprintf (stderr, "flags: 0x%x\n", flags);
27627 fprintf (stderr, "--------------------\n");
27630 old_optimize = build_optimization_node ();
27631 func_optimize = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl);
27633 /* If the function changed the optimization levels as well as setting target
27634 options, start with the optimizations specified. */
27635 if (func_optimize && func_optimize != old_optimize)
27636 cl_optimization_restore (&global_options,
27637 TREE_OPTIMIZATION (func_optimize));
27639 /* The target attributes may also change some optimization flags, so update
27640 the optimization options if necessary. */
27641 cl_target_option_save (&cur_target, &global_options);
27642 rs6000_cpu_index = rs6000_tune_index = -1;
27643 ret = rs6000_inner_target_options (args, true);
27645 /* Set up any additional state. */
27646 if (ret)
27648 ret = rs6000_option_override_internal (false);
27649 new_target = build_target_option_node ();
27651 else
27652 new_target = NULL;
27654 new_optimize = build_optimization_node ();
27656 if (!new_target)
27657 ret = false;
27659 else if (fndecl)
27661 DECL_FUNCTION_SPECIFIC_TARGET (fndecl) = new_target;
27663 if (old_optimize != new_optimize)
27664 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl) = new_optimize;
27667 cl_target_option_restore (&global_options, &cur_target);
27669 if (old_optimize != new_optimize)
27670 cl_optimization_restore (&global_options,
27671 TREE_OPTIMIZATION (old_optimize));
27673 return ret;
27677 /* Hook to validate the current #pragma GCC target and set the state, and
27678 update the macros based on what was changed. If ARGS is NULL, then
27679 POP_TARGET is used to reset the options. */
27681 bool
27682 rs6000_pragma_target_parse (tree args, tree pop_target)
27684 tree prev_tree = build_target_option_node ();
27685 tree cur_tree;
27686 struct cl_target_option *prev_opt, *cur_opt;
27687 unsigned prev_bumask, cur_bumask, diff_bumask;
27688 int prev_flags, cur_flags, diff_flags;
27690 if (TARGET_DEBUG_TARGET)
27692 fprintf (stderr, "\n==================== rs6000_pragma_target_parse\n");
27693 fprintf (stderr, "args:");
27694 rs6000_debug_target_options (args, " ");
27695 fprintf (stderr, "\n");
27697 if (pop_target)
27699 fprintf (stderr, "pop_target:\n");
27700 debug_tree (pop_target);
27702 else
27703 fprintf (stderr, "pop_target: <NULL>\n");
27705 fprintf (stderr, "--------------------\n");
27708 if (! args)
27710 cur_tree = ((pop_target)
27711 ? pop_target
27712 : target_option_default_node);
27713 cl_target_option_restore (&global_options,
27714 TREE_TARGET_OPTION (cur_tree));
27716 else
27718 rs6000_cpu_index = rs6000_tune_index = -1;
27719 if (!rs6000_inner_target_options (args, false)
27720 || !rs6000_option_override_internal (false)
27721 || (cur_tree = build_target_option_node ()) == NULL_TREE)
27723 if (TARGET_DEBUG_BUILTIN || TARGET_DEBUG_TARGET)
27724 fprintf (stderr, "invalid pragma\n");
27726 return false;
27730 target_option_current_node = cur_tree;
27732 /* If we have the preprocessor linked in (i.e. C or C++ languages), possibly
27733 change the macros that are defined. */
27734 if (rs6000_target_modify_macros_ptr)
27736 prev_opt = TREE_TARGET_OPTION (prev_tree);
27737 prev_bumask = prev_opt->x_rs6000_builtin_mask;
27738 prev_flags = prev_opt->x_target_flags;
27740 cur_opt = TREE_TARGET_OPTION (cur_tree);
27741 cur_flags = cur_opt->x_target_flags;
27742 cur_bumask = cur_opt->x_rs6000_builtin_mask;
27744 diff_bumask = (prev_bumask ^ cur_bumask);
27745 diff_flags = (prev_flags ^ cur_flags);
27747 if ((diff_flags != 0) || (diff_bumask != 0))
27749 /* Delete old macros. */
27750 rs6000_target_modify_macros_ptr (false,
27751 prev_flags & diff_flags,
27752 prev_bumask & diff_bumask);
27754 /* Define new macros. */
27755 rs6000_target_modify_macros_ptr (true,
27756 cur_flags & diff_flags,
27757 cur_bumask & diff_bumask);
27761 return true;
27765 /* Remember the last target of rs6000_set_current_function. */
27766 static GTY(()) tree rs6000_previous_fndecl;
27768 /* Establish appropriate back-end context for processing the function
27769 FNDECL. The argument might be NULL to indicate processing at top
27770 level, outside of any function scope. */
27771 static void
27772 rs6000_set_current_function (tree fndecl)
27774 tree old_tree = (rs6000_previous_fndecl
27775 ? DECL_FUNCTION_SPECIFIC_TARGET (rs6000_previous_fndecl)
27776 : NULL_TREE);
27778 tree new_tree = (fndecl
27779 ? DECL_FUNCTION_SPECIFIC_TARGET (fndecl)
27780 : NULL_TREE);
27782 if (TARGET_DEBUG_TARGET)
27784 bool print_final = false;
27785 fprintf (stderr, "\n==================== rs6000_set_current_function");
27787 if (fndecl)
27788 fprintf (stderr, ", fndecl %s (%p)",
27789 (DECL_NAME (fndecl)
27790 ? IDENTIFIER_POINTER (DECL_NAME (fndecl))
27791 : "<unknown>"), (void *)fndecl);
27793 if (rs6000_previous_fndecl)
27794 fprintf (stderr, ", prev_fndecl (%p)", (void *)rs6000_previous_fndecl);
27796 fprintf (stderr, "\n");
27797 if (new_tree)
27799 fprintf (stderr, "\nnew fndecl target specific options:\n");
27800 debug_tree (new_tree);
27801 print_final = true;
27804 if (old_tree)
27806 fprintf (stderr, "\nold fndecl target specific options:\n");
27807 debug_tree (old_tree);
27808 print_final = true;
27811 if (print_final)
27812 fprintf (stderr, "--------------------\n");
27815 /* Only change the context if the function changes. This hook is called
27816 several times in the course of compiling a function, and we don't want to
27817 slow things down too much or call target_reinit when it isn't safe. */
27818 if (fndecl && fndecl != rs6000_previous_fndecl)
27820 rs6000_previous_fndecl = fndecl;
27821 if (old_tree == new_tree)
27824 else if (new_tree)
27826 cl_target_option_restore (&global_options,
27827 TREE_TARGET_OPTION (new_tree));
27828 target_reinit ();
27831 else if (old_tree)
27833 struct cl_target_option *def
27834 = TREE_TARGET_OPTION (target_option_current_node);
27836 cl_target_option_restore (&global_options, def);
27837 target_reinit ();
27843 /* Save the current options */
27845 static void
27846 rs6000_function_specific_save (struct cl_target_option *ptr)
27848 ptr->rs6000_target_flags_explicit = target_flags_explicit;
27851 /* Restore the current options */
27853 static void
27854 rs6000_function_specific_restore (struct cl_target_option *ptr)
27856 target_flags_explicit = ptr->rs6000_target_flags_explicit;
27857 (void) rs6000_option_override_internal (false);
27860 /* Print the current options */
27862 static void
27863 rs6000_function_specific_print (FILE *file, int indent,
27864 struct cl_target_option *ptr)
27866 size_t i;
27867 int flags = ptr->x_target_flags;
27868 unsigned bu_mask = ptr->x_rs6000_builtin_mask;
27870 /* Print the various mask options. */
27871 for (i = 0; i < ARRAY_SIZE (rs6000_opt_masks); i++)
27872 if ((flags & rs6000_opt_masks[i].mask) != 0)
27874 flags &= ~ rs6000_opt_masks[i].mask;
27875 fprintf (file, "%*s-m%s%s\n", indent, "",
27876 rs6000_opt_masks[i].invert ? "no-" : "",
27877 rs6000_opt_masks[i].name);
27880 /* Print the various options that are variables. */
27881 for (i = 0; i < ARRAY_SIZE (rs6000_opt_vars); i++)
27883 size_t j = rs6000_opt_vars[i].target_offset;
27884 if (((signed char *) ptr)[j])
27885 fprintf (file, "%*s-m%s\n", indent, "",
27886 rs6000_opt_vars[i].name);
27889 /* Print the various builtin flags. */
27890 fprintf (file, "%*sbuiltin mask = 0x%x\n", indent, "", bu_mask);
27891 for (i = 0; i < ARRAY_SIZE (rs6000_builtin_mask_names); i++)
27892 if ((bu_mask & rs6000_builtin_mask_names[i].mask) != 0)
27894 fprintf (file, "%*s%s builtins supported\n", indent, "",
27895 rs6000_builtin_mask_names[i].name);
27900 /* Hook to determine if one function can safely inline another. */
27902 static bool
27903 rs6000_can_inline_p (tree caller, tree callee)
27905 bool ret = false;
27906 tree caller_tree = DECL_FUNCTION_SPECIFIC_TARGET (caller);
27907 tree callee_tree = DECL_FUNCTION_SPECIFIC_TARGET (callee);
27909 /* If callee has no option attributes, then it is ok to inline. */
27910 if (!callee_tree)
27911 ret = true;
27913 /* If caller has no option attributes, but callee does then it is not ok to
27914 inline. */
27915 else if (!caller_tree)
27916 ret = false;
27918 else
27920 struct cl_target_option *caller_opts = TREE_TARGET_OPTION (caller_tree);
27921 struct cl_target_option *callee_opts = TREE_TARGET_OPTION (callee_tree);
27923 /* Callee's options should a subset of the caller's, i.e. a vsx function
27924 can inline an altivec function but a non-vsx function can't inline a
27925 vsx function. */
27926 if ((caller_opts->x_target_flags & callee_opts->x_target_flags)
27927 == callee_opts->x_target_flags)
27928 ret = true;
27931 if (TARGET_DEBUG_TARGET)
27932 fprintf (stderr, "rs6000_can_inline_p:, caller %s, callee %s, %s inline\n",
27933 (DECL_NAME (caller)
27934 ? IDENTIFIER_POINTER (DECL_NAME (caller))
27935 : "<unknown>"),
27936 (DECL_NAME (callee)
27937 ? IDENTIFIER_POINTER (DECL_NAME (callee))
27938 : "<unknown>"),
27939 (ret ? "can" : "cannot"));
27941 return ret;
27944 /* Allocate a stack temp and fixup the address so it meets the particular
27945 memory requirements (either offetable or REG+REG addressing). */
27948 rs6000_allocate_stack_temp (enum machine_mode mode,
27949 bool offsettable_p,
27950 bool reg_reg_p)
27952 rtx stack = assign_stack_temp (mode, GET_MODE_SIZE (mode));
27953 rtx addr = XEXP (stack, 0);
27954 int strict_p = (reload_in_progress || reload_completed);
27956 if (!legitimate_indirect_address_p (addr, strict_p))
27958 if (offsettable_p
27959 && !rs6000_legitimate_offset_address_p (mode, addr, strict_p, true))
27960 stack = replace_equiv_address (stack, copy_addr_to_reg (addr));
27962 else if (reg_reg_p && !legitimate_indexed_address_p (addr, strict_p))
27963 stack = replace_equiv_address (stack, copy_addr_to_reg (addr));
27966 return stack;
27969 /* Given a memory reference, if it is not a reg or reg+reg addressing, convert
27970 to such a form to deal with memory reference instructions like STFIWX that
27971 only take reg+reg addressing. */
27974 rs6000_address_for_fpconvert (rtx x)
27976 int strict_p = (reload_in_progress || reload_completed);
27977 rtx addr;
27979 gcc_assert (MEM_P (x));
27980 addr = XEXP (x, 0);
27981 if (! legitimate_indirect_address_p (addr, strict_p)
27982 && ! legitimate_indexed_address_p (addr, strict_p))
27984 if (GET_CODE (addr) == PRE_INC || GET_CODE (addr) == PRE_DEC)
27986 rtx reg = XEXP (addr, 0);
27987 HOST_WIDE_INT size = GET_MODE_SIZE (GET_MODE (x));
27988 rtx size_rtx = GEN_INT ((GET_CODE (addr) == PRE_DEC) ? -size : size);
27989 gcc_assert (REG_P (reg));
27990 emit_insn (gen_add3_insn (reg, reg, size_rtx));
27991 addr = reg;
27993 else if (GET_CODE (addr) == PRE_MODIFY)
27995 rtx reg = XEXP (addr, 0);
27996 rtx expr = XEXP (addr, 1);
27997 gcc_assert (REG_P (reg));
27998 gcc_assert (GET_CODE (expr) == PLUS);
27999 emit_insn (gen_add3_insn (reg, XEXP (expr, 0), XEXP (expr, 1)));
28000 addr = reg;
28003 x = replace_equiv_address (x, copy_addr_to_reg (addr));
28006 return x;
28009 /* Given a memory reference, if it is not in the form for altivec memory
28010 reference instructions (i.e. reg or reg+reg addressing with AND of -16),
28011 convert to the altivec format. */
28014 rs6000_address_for_altivec (rtx x)
28016 gcc_assert (MEM_P (x));
28017 if (!altivec_indexed_or_indirect_operand (x, GET_MODE (x)))
28019 rtx addr = XEXP (x, 0);
28020 int strict_p = (reload_in_progress || reload_completed);
28022 if (!legitimate_indexed_address_p (addr, strict_p)
28023 && !legitimate_indirect_address_p (addr, strict_p))
28024 addr = copy_to_mode_reg (Pmode, addr);
28026 addr = gen_rtx_AND (Pmode, addr, GEN_INT (-16));
28027 x = change_address (x, GET_MODE (x), addr);
28030 return x;
28033 /* Implement TARGET_LEGITIMATE_CONSTANT_P.
28035 On the RS/6000, all integer constants are acceptable, most won't be valid
28036 for particular insns, though. Only easy FP constants are acceptable. */
28038 static bool
28039 rs6000_legitimate_constant_p (enum machine_mode mode, rtx x)
28041 if (rs6000_tls_referenced_p (x))
28042 return false;
28044 return ((GET_CODE (x) != CONST_DOUBLE && GET_CODE (x) != CONST_VECTOR)
28045 || GET_MODE (x) == VOIDmode
28046 || (TARGET_POWERPC64 && mode == DImode)
28047 || easy_fp_constant (x, mode)
28048 || easy_vector_constant (x, mode));
28052 /* A function pointer under AIX is a pointer to a data area whose first word
28053 contains the actual address of the function, whose second word contains a
28054 pointer to its TOC, and whose third word contains a value to place in the
28055 static chain register (r11). Note that if we load the static chain, our
28056 "trampoline" need not have any executable code. */
28058 void
28059 rs6000_call_indirect_aix (rtx value, rtx func_desc, rtx flag)
28061 rtx func_addr;
28062 rtx toc_reg;
28063 rtx sc_reg;
28064 rtx stack_ptr;
28065 rtx stack_toc_offset;
28066 rtx stack_toc_mem;
28067 rtx func_toc_offset;
28068 rtx func_toc_mem;
28069 rtx func_sc_offset;
28070 rtx func_sc_mem;
28071 rtx insn;
28072 rtx (*call_func) (rtx, rtx, rtx, rtx);
28073 rtx (*call_value_func) (rtx, rtx, rtx, rtx, rtx);
28075 stack_ptr = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
28076 toc_reg = gen_rtx_REG (Pmode, TOC_REGNUM);
28078 /* Load up address of the actual function. */
28079 func_desc = force_reg (Pmode, func_desc);
28080 func_addr = gen_reg_rtx (Pmode);
28081 emit_move_insn (func_addr, gen_rtx_MEM (Pmode, func_desc));
28083 if (TARGET_32BIT)
28086 stack_toc_offset = GEN_INT (TOC_SAVE_OFFSET_32BIT);
28087 func_toc_offset = GEN_INT (AIX_FUNC_DESC_TOC_32BIT);
28088 func_sc_offset = GEN_INT (AIX_FUNC_DESC_SC_32BIT);
28089 if (TARGET_POINTERS_TO_NESTED_FUNCTIONS)
28091 call_func = gen_call_indirect_aix32bit;
28092 call_value_func = gen_call_value_indirect_aix32bit;
28094 else
28096 call_func = gen_call_indirect_aix32bit_nor11;
28097 call_value_func = gen_call_value_indirect_aix32bit_nor11;
28100 else
28102 stack_toc_offset = GEN_INT (TOC_SAVE_OFFSET_64BIT);
28103 func_toc_offset = GEN_INT (AIX_FUNC_DESC_TOC_64BIT);
28104 func_sc_offset = GEN_INT (AIX_FUNC_DESC_SC_64BIT);
28105 if (TARGET_POINTERS_TO_NESTED_FUNCTIONS)
28107 call_func = gen_call_indirect_aix64bit;
28108 call_value_func = gen_call_value_indirect_aix64bit;
28110 else
28112 call_func = gen_call_indirect_aix64bit_nor11;
28113 call_value_func = gen_call_value_indirect_aix64bit_nor11;
28117 /* Reserved spot to store the TOC. */
28118 stack_toc_mem = gen_frame_mem (Pmode,
28119 gen_rtx_PLUS (Pmode,
28120 stack_ptr,
28121 stack_toc_offset));
28123 gcc_assert (cfun);
28124 gcc_assert (cfun->machine);
28126 /* Can we optimize saving the TOC in the prologue or do we need to do it at
28127 every call? */
28128 if (TARGET_SAVE_TOC_INDIRECT && !cfun->calls_alloca)
28129 cfun->machine->save_toc_in_prologue = true;
28131 else
28133 MEM_VOLATILE_P (stack_toc_mem) = 1;
28134 emit_move_insn (stack_toc_mem, toc_reg);
28137 /* Calculate the address to load the TOC of the called function. We don't
28138 actually load this until the split after reload. */
28139 func_toc_mem = gen_rtx_MEM (Pmode,
28140 gen_rtx_PLUS (Pmode,
28141 func_desc,
28142 func_toc_offset));
28144 /* If we have a static chain, load it up. */
28145 if (TARGET_POINTERS_TO_NESTED_FUNCTIONS)
28147 func_sc_mem = gen_rtx_MEM (Pmode,
28148 gen_rtx_PLUS (Pmode,
28149 func_desc,
28150 func_sc_offset));
28152 sc_reg = gen_rtx_REG (Pmode, STATIC_CHAIN_REGNUM);
28153 emit_move_insn (sc_reg, func_sc_mem);
28156 /* Create the call. */
28157 if (value)
28158 insn = call_value_func (value, func_addr, flag, func_toc_mem,
28159 stack_toc_mem);
28160 else
28161 insn = call_func (func_addr, flag, func_toc_mem, stack_toc_mem);
28163 emit_call_insn (insn);
28166 /* Return whether we need to always update the saved TOC pointer when we update
28167 the stack pointer. */
28169 static bool
28170 rs6000_save_toc_in_prologue_p (void)
28172 return (cfun && cfun->machine && cfun->machine->save_toc_in_prologue);
28175 #ifdef HAVE_GAS_HIDDEN
28176 # define USE_HIDDEN_LINKONCE 1
28177 #else
28178 # define USE_HIDDEN_LINKONCE 0
28179 #endif
28181 /* Fills in the label name that should be used for a 476 link stack thunk. */
28183 void
28184 get_ppc476_thunk_name (char name[32])
28186 gcc_assert (TARGET_LINK_STACK);
28188 if (USE_HIDDEN_LINKONCE)
28189 sprintf (name, "__ppc476.get_thunk");
28190 else
28191 ASM_GENERATE_INTERNAL_LABEL (name, "LPPC476_", 0);
28194 /* This function emits the simple thunk routine that is used to preserve
28195 the link stack on the 476 cpu. */
28197 static void rs6000_code_end (void) ATTRIBUTE_UNUSED;
28198 static void
28199 rs6000_code_end (void)
28201 char name[32];
28202 tree decl;
28204 if (!TARGET_LINK_STACK)
28205 return;
28207 get_ppc476_thunk_name (name);
28209 decl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL, get_identifier (name),
28210 build_function_type_list (void_type_node, NULL_TREE));
28211 DECL_RESULT (decl) = build_decl (BUILTINS_LOCATION, RESULT_DECL,
28212 NULL_TREE, void_type_node);
28213 TREE_PUBLIC (decl) = 1;
28214 TREE_STATIC (decl) = 1;
28216 if (USE_HIDDEN_LINKONCE)
28218 DECL_COMDAT_GROUP (decl) = DECL_ASSEMBLER_NAME (decl);
28219 targetm.asm_out.unique_section (decl, 0);
28220 switch_to_section (get_named_section (decl, NULL, 0));
28221 DECL_WEAK (decl) = 1;
28222 ASM_WEAKEN_DECL (asm_out_file, decl, name, 0);
28223 targetm.asm_out.globalize_label (asm_out_file, name);
28224 targetm.asm_out.assemble_visibility (decl, VISIBILITY_HIDDEN);
28225 ASM_DECLARE_FUNCTION_NAME (asm_out_file, name, decl);
28227 else
28229 switch_to_section (text_section);
28230 ASM_OUTPUT_LABEL (asm_out_file, name);
28233 DECL_INITIAL (decl) = make_node (BLOCK);
28234 current_function_decl = decl;
28235 init_function_start (decl);
28236 first_function_block_is_cold = false;
28237 /* Make sure unwind info is emitted for the thunk if needed. */
28238 final_start_function (emit_barrier (), asm_out_file, 1);
28240 fputs ("\tblr\n", asm_out_file);
28242 final_end_function ();
28243 init_insn_lengths ();
28244 free_after_compilation (cfun);
28245 set_cfun (NULL);
28246 current_function_decl = NULL;
28249 /* Add r30 to hard reg set if the prologue sets it up and it is not
28250 pic_offset_table_rtx. */
28252 static void
28253 rs6000_set_up_by_prologue (struct hard_reg_set_container *set)
28255 if (!TARGET_SINGLE_PIC_BASE
28256 && TARGET_TOC
28257 && TARGET_MINIMAL_TOC
28258 && get_pool_size () != 0)
28259 add_to_hard_reg_set (&set->set, Pmode, RS6000_PIC_OFFSET_TABLE_REGNUM);
28262 struct gcc_target targetm = TARGET_INITIALIZER;
28264 #include "gt-rs6000.h"