* config/rs6000/predicates.md (reg_or_add_cint_operand,
[official-gcc.git] / gcc / config / rs6000 / rs6000.c
blob28f316080f8b75896b1342b83c0bed098bdece97
1 /* Subroutines used for code generation on IBM RS/6000.
2 Copyright (C) 1991-2013 Free Software Foundation, Inc.
3 Contributed by Richard Kenner (kenner@vlsi1.ultra.nyu.edu)
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published
9 by the Free Software Foundation; either version 3, or (at your
10 option) any later version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
15 License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "rtl.h"
26 #include "regs.h"
27 #include "hard-reg-set.h"
28 #include "insn-config.h"
29 #include "conditions.h"
30 #include "insn-attr.h"
31 #include "flags.h"
32 #include "recog.h"
33 #include "obstack.h"
34 #include "tree.h"
35 #include "expr.h"
36 #include "optabs.h"
37 #include "except.h"
38 #include "function.h"
39 #include "output.h"
40 #include "dbxout.h"
41 #include "basic-block.h"
42 #include "diagnostic-core.h"
43 #include "toplev.h"
44 #include "ggc.h"
45 #include "hashtab.h"
46 #include "tm_p.h"
47 #include "target.h"
48 #include "target-def.h"
49 #include "common/common-target.h"
50 #include "langhooks.h"
51 #include "reload.h"
52 #include "cfgloop.h"
53 #include "sched-int.h"
54 #include "gimple.h"
55 #include "tree-flow.h"
56 #include "intl.h"
57 #include "params.h"
58 #include "tm-constrs.h"
59 #include "opts.h"
60 #include "tree-vectorizer.h"
61 #include "dumpfile.h"
62 #if TARGET_XCOFF
63 #include "xcoffout.h" /* get declarations of xcoff_*_section_name */
64 #endif
65 #if TARGET_MACHO
66 #include "gstab.h" /* for N_SLINE */
67 #endif
69 #ifndef TARGET_NO_PROTOTYPE
70 #define TARGET_NO_PROTOTYPE 0
71 #endif
73 #define min(A,B) ((A) < (B) ? (A) : (B))
74 #define max(A,B) ((A) > (B) ? (A) : (B))
76 /* Structure used to define the rs6000 stack */
77 typedef struct rs6000_stack {
78 int reload_completed; /* stack info won't change from here on */
79 int first_gp_reg_save; /* first callee saved GP register used */
80 int first_fp_reg_save; /* first callee saved FP register used */
81 int first_altivec_reg_save; /* first callee saved AltiVec register used */
82 int lr_save_p; /* true if the link reg needs to be saved */
83 int cr_save_p; /* true if the CR reg needs to be saved */
84 unsigned int vrsave_mask; /* mask of vec registers to save */
85 int push_p; /* true if we need to allocate stack space */
86 int calls_p; /* true if the function makes any calls */
87 int world_save_p; /* true if we're saving *everything*:
88 r13-r31, cr, f14-f31, vrsave, v20-v31 */
89 enum rs6000_abi abi; /* which ABI to use */
90 int gp_save_offset; /* offset to save GP regs from initial SP */
91 int fp_save_offset; /* offset to save FP regs from initial SP */
92 int altivec_save_offset; /* offset to save AltiVec regs from initial SP */
93 int lr_save_offset; /* offset to save LR from initial SP */
94 int cr_save_offset; /* offset to save CR from initial SP */
95 int vrsave_save_offset; /* offset to save VRSAVE from initial SP */
96 int spe_gp_save_offset; /* offset to save spe 64-bit gprs */
97 int varargs_save_offset; /* offset to save the varargs registers */
98 int ehrd_offset; /* offset to EH return data */
99 int reg_size; /* register size (4 or 8) */
100 HOST_WIDE_INT vars_size; /* variable save area size */
101 int parm_size; /* outgoing parameter size */
102 int save_size; /* save area size */
103 int fixed_size; /* fixed size of stack frame */
104 int gp_size; /* size of saved GP registers */
105 int fp_size; /* size of saved FP registers */
106 int altivec_size; /* size of saved AltiVec registers */
107 int cr_size; /* size to hold CR if not in save_size */
108 int vrsave_size; /* size to hold VRSAVE if not in save_size */
109 int altivec_padding_size; /* size of altivec alignment padding if
110 not in save_size */
111 int spe_gp_size; /* size of 64-bit GPR save size for SPE */
112 int spe_padding_size;
113 HOST_WIDE_INT total_size; /* total bytes allocated for stack */
114 int spe_64bit_regs_used;
115 int savres_strategy;
116 } rs6000_stack_t;
118 /* A C structure for machine-specific, per-function data.
119 This is added to the cfun structure. */
120 typedef struct GTY(()) machine_function
122 /* Some local-dynamic symbol. */
123 const char *some_ld_name;
124 /* Whether the instruction chain has been scanned already. */
125 int insn_chain_scanned_p;
126 /* Flags if __builtin_return_address (n) with n >= 1 was used. */
127 int ra_needs_full_frame;
128 /* Flags if __builtin_return_address (0) was used. */
129 int ra_need_lr;
130 /* Cache lr_save_p after expansion of builtin_eh_return. */
131 int lr_save_state;
132 /* Whether we need to save the TOC to the reserved stack location in the
133 function prologue. */
134 bool save_toc_in_prologue;
135 /* Offset from virtual_stack_vars_rtx to the start of the ABI_V4
136 varargs save area. */
137 HOST_WIDE_INT varargs_save_offset;
138 /* Temporary stack slot to use for SDmode copies. This slot is
139 64-bits wide and is allocated early enough so that the offset
140 does not overflow the 16-bit load/store offset field. */
141 rtx sdmode_stack_slot;
142 } machine_function;
144 /* Support targetm.vectorize.builtin_mask_for_load. */
145 static GTY(()) tree altivec_builtin_mask_for_load;
147 /* Set to nonzero once AIX common-mode calls have been defined. */
148 static GTY(()) int common_mode_defined;
150 /* Label number of label created for -mrelocatable, to call to so we can
151 get the address of the GOT section */
152 static int rs6000_pic_labelno;
154 #ifdef USING_ELFOS_H
155 /* Counter for labels which are to be placed in .fixup. */
156 int fixuplabelno = 0;
157 #endif
159 /* Whether to use variant of AIX ABI for PowerPC64 Linux. */
160 int dot_symbols;
162 /* Specify the machine mode that pointers have. After generation of rtl, the
163 compiler makes no further distinction between pointers and any other objects
164 of this machine mode. The type is unsigned since not all things that
165 include rs6000.h also include machmode.h. */
166 unsigned rs6000_pmode;
168 /* Width in bits of a pointer. */
169 unsigned rs6000_pointer_size;
171 #ifdef HAVE_AS_GNU_ATTRIBUTE
172 /* Flag whether floating point values have been passed/returned. */
173 static bool rs6000_passes_float;
174 /* Flag whether vector values have been passed/returned. */
175 static bool rs6000_passes_vector;
176 /* Flag whether small (<= 8 byte) structures have been returned. */
177 static bool rs6000_returns_struct;
178 #endif
180 /* Value is TRUE if register/mode pair is acceptable. */
181 bool rs6000_hard_regno_mode_ok_p[NUM_MACHINE_MODES][FIRST_PSEUDO_REGISTER];
183 /* Maximum number of registers needed for a given register class and mode. */
184 unsigned char rs6000_class_max_nregs[NUM_MACHINE_MODES][LIM_REG_CLASSES];
186 /* How many registers are needed for a given register and mode. */
187 unsigned char rs6000_hard_regno_nregs[NUM_MACHINE_MODES][FIRST_PSEUDO_REGISTER];
189 /* Map register number to register class. */
190 enum reg_class rs6000_regno_regclass[FIRST_PSEUDO_REGISTER];
192 /* Reload functions based on the type and the vector unit. */
193 static enum insn_code rs6000_vector_reload[NUM_MACHINE_MODES][2];
195 static int dbg_cost_ctrl;
197 /* Built in types. */
198 tree rs6000_builtin_types[RS6000_BTI_MAX];
199 tree rs6000_builtin_decls[RS6000_BUILTIN_COUNT];
201 /* Flag to say the TOC is initialized */
202 int toc_initialized;
203 char toc_label_name[10];
205 /* Cached value of rs6000_variable_issue. This is cached in
206 rs6000_variable_issue hook and returned from rs6000_sched_reorder2. */
207 static short cached_can_issue_more;
209 static GTY(()) section *read_only_data_section;
210 static GTY(()) section *private_data_section;
211 static GTY(()) section *tls_data_section;
212 static GTY(()) section *tls_private_data_section;
213 static GTY(()) section *read_only_private_data_section;
214 static GTY(()) section *sdata2_section;
215 static GTY(()) section *toc_section;
217 struct builtin_description
219 const HOST_WIDE_INT mask;
220 const enum insn_code icode;
221 const char *const name;
222 const enum rs6000_builtins code;
225 /* Describe the vector unit used for modes. */
226 enum rs6000_vector rs6000_vector_unit[NUM_MACHINE_MODES];
227 enum rs6000_vector rs6000_vector_mem[NUM_MACHINE_MODES];
229 /* Register classes for various constraints that are based on the target
230 switches. */
231 enum reg_class rs6000_constraints[RS6000_CONSTRAINT_MAX];
233 /* Describe the alignment of a vector. */
234 int rs6000_vector_align[NUM_MACHINE_MODES];
236 /* Map selected modes to types for builtins. */
237 static GTY(()) tree builtin_mode_to_type[MAX_MACHINE_MODE][2];
239 /* What modes to automatically generate reciprocal divide estimate (fre) and
240 reciprocal sqrt (frsqrte) for. */
241 unsigned char rs6000_recip_bits[MAX_MACHINE_MODE];
243 /* Masks to determine which reciprocal esitmate instructions to generate
244 automatically. */
245 enum rs6000_recip_mask {
246 RECIP_SF_DIV = 0x001, /* Use divide estimate */
247 RECIP_DF_DIV = 0x002,
248 RECIP_V4SF_DIV = 0x004,
249 RECIP_V2DF_DIV = 0x008,
251 RECIP_SF_RSQRT = 0x010, /* Use reciprocal sqrt estimate. */
252 RECIP_DF_RSQRT = 0x020,
253 RECIP_V4SF_RSQRT = 0x040,
254 RECIP_V2DF_RSQRT = 0x080,
256 /* Various combination of flags for -mrecip=xxx. */
257 RECIP_NONE = 0,
258 RECIP_ALL = (RECIP_SF_DIV | RECIP_DF_DIV | RECIP_V4SF_DIV
259 | RECIP_V2DF_DIV | RECIP_SF_RSQRT | RECIP_DF_RSQRT
260 | RECIP_V4SF_RSQRT | RECIP_V2DF_RSQRT),
262 RECIP_HIGH_PRECISION = RECIP_ALL,
264 /* On low precision machines like the power5, don't enable double precision
265 reciprocal square root estimate, since it isn't accurate enough. */
266 RECIP_LOW_PRECISION = (RECIP_ALL & ~(RECIP_DF_RSQRT | RECIP_V2DF_RSQRT))
269 /* -mrecip options. */
270 static struct
272 const char *string; /* option name */
273 unsigned int mask; /* mask bits to set */
274 } recip_options[] = {
275 { "all", RECIP_ALL },
276 { "none", RECIP_NONE },
277 { "div", (RECIP_SF_DIV | RECIP_DF_DIV | RECIP_V4SF_DIV
278 | RECIP_V2DF_DIV) },
279 { "divf", (RECIP_SF_DIV | RECIP_V4SF_DIV) },
280 { "divd", (RECIP_DF_DIV | RECIP_V2DF_DIV) },
281 { "rsqrt", (RECIP_SF_RSQRT | RECIP_DF_RSQRT | RECIP_V4SF_RSQRT
282 | RECIP_V2DF_RSQRT) },
283 { "rsqrtf", (RECIP_SF_RSQRT | RECIP_V4SF_RSQRT) },
284 { "rsqrtd", (RECIP_DF_RSQRT | RECIP_V2DF_RSQRT) },
287 /* 2 argument gen function typedef. */
288 typedef rtx (*gen_2arg_fn_t) (rtx, rtx, rtx);
290 /* Pointer to function (in rs6000-c.c) that can define or undefine target
291 macros that have changed. Languages that don't support the preprocessor
292 don't link in rs6000-c.c, so we can't call it directly. */
293 void (*rs6000_target_modify_macros_ptr) (bool, HOST_WIDE_INT, HOST_WIDE_INT);
296 /* Target cpu costs. */
298 struct processor_costs {
299 const int mulsi; /* cost of SImode multiplication. */
300 const int mulsi_const; /* cost of SImode multiplication by constant. */
301 const int mulsi_const9; /* cost of SImode mult by short constant. */
302 const int muldi; /* cost of DImode multiplication. */
303 const int divsi; /* cost of SImode division. */
304 const int divdi; /* cost of DImode division. */
305 const int fp; /* cost of simple SFmode and DFmode insns. */
306 const int dmul; /* cost of DFmode multiplication (and fmadd). */
307 const int sdiv; /* cost of SFmode division (fdivs). */
308 const int ddiv; /* cost of DFmode division (fdiv). */
309 const int cache_line_size; /* cache line size in bytes. */
310 const int l1_cache_size; /* size of l1 cache, in kilobytes. */
311 const int l2_cache_size; /* size of l2 cache, in kilobytes. */
312 const int simultaneous_prefetches; /* number of parallel prefetch
313 operations. */
316 const struct processor_costs *rs6000_cost;
318 /* Processor costs (relative to an add) */
320 /* Instruction size costs on 32bit processors. */
321 static const
322 struct processor_costs size32_cost = {
323 COSTS_N_INSNS (1), /* mulsi */
324 COSTS_N_INSNS (1), /* mulsi_const */
325 COSTS_N_INSNS (1), /* mulsi_const9 */
326 COSTS_N_INSNS (1), /* muldi */
327 COSTS_N_INSNS (1), /* divsi */
328 COSTS_N_INSNS (1), /* divdi */
329 COSTS_N_INSNS (1), /* fp */
330 COSTS_N_INSNS (1), /* dmul */
331 COSTS_N_INSNS (1), /* sdiv */
332 COSTS_N_INSNS (1), /* ddiv */
339 /* Instruction size costs on 64bit processors. */
340 static const
341 struct processor_costs size64_cost = {
342 COSTS_N_INSNS (1), /* mulsi */
343 COSTS_N_INSNS (1), /* mulsi_const */
344 COSTS_N_INSNS (1), /* mulsi_const9 */
345 COSTS_N_INSNS (1), /* muldi */
346 COSTS_N_INSNS (1), /* divsi */
347 COSTS_N_INSNS (1), /* divdi */
348 COSTS_N_INSNS (1), /* fp */
349 COSTS_N_INSNS (1), /* dmul */
350 COSTS_N_INSNS (1), /* sdiv */
351 COSTS_N_INSNS (1), /* ddiv */
352 128,
358 /* Instruction costs on RS64A processors. */
359 static const
360 struct processor_costs rs64a_cost = {
361 COSTS_N_INSNS (20), /* mulsi */
362 COSTS_N_INSNS (12), /* mulsi_const */
363 COSTS_N_INSNS (8), /* mulsi_const9 */
364 COSTS_N_INSNS (34), /* muldi */
365 COSTS_N_INSNS (65), /* divsi */
366 COSTS_N_INSNS (67), /* divdi */
367 COSTS_N_INSNS (4), /* fp */
368 COSTS_N_INSNS (4), /* dmul */
369 COSTS_N_INSNS (31), /* sdiv */
370 COSTS_N_INSNS (31), /* ddiv */
371 128, /* cache line size */
372 128, /* l1 cache */
373 2048, /* l2 cache */
374 1, /* streams */
377 /* Instruction costs on MPCCORE processors. */
378 static const
379 struct processor_costs mpccore_cost = {
380 COSTS_N_INSNS (2), /* mulsi */
381 COSTS_N_INSNS (2), /* mulsi_const */
382 COSTS_N_INSNS (2), /* mulsi_const9 */
383 COSTS_N_INSNS (2), /* muldi */
384 COSTS_N_INSNS (6), /* divsi */
385 COSTS_N_INSNS (6), /* divdi */
386 COSTS_N_INSNS (4), /* fp */
387 COSTS_N_INSNS (5), /* dmul */
388 COSTS_N_INSNS (10), /* sdiv */
389 COSTS_N_INSNS (17), /* ddiv */
390 32, /* cache line size */
391 4, /* l1 cache */
392 16, /* l2 cache */
393 1, /* streams */
396 /* Instruction costs on PPC403 processors. */
397 static const
398 struct processor_costs ppc403_cost = {
399 COSTS_N_INSNS (4), /* mulsi */
400 COSTS_N_INSNS (4), /* mulsi_const */
401 COSTS_N_INSNS (4), /* mulsi_const9 */
402 COSTS_N_INSNS (4), /* muldi */
403 COSTS_N_INSNS (33), /* divsi */
404 COSTS_N_INSNS (33), /* divdi */
405 COSTS_N_INSNS (11), /* fp */
406 COSTS_N_INSNS (11), /* dmul */
407 COSTS_N_INSNS (11), /* sdiv */
408 COSTS_N_INSNS (11), /* ddiv */
409 32, /* cache line size */
410 4, /* l1 cache */
411 16, /* l2 cache */
412 1, /* streams */
415 /* Instruction costs on PPC405 processors. */
416 static const
417 struct processor_costs ppc405_cost = {
418 COSTS_N_INSNS (5), /* mulsi */
419 COSTS_N_INSNS (4), /* mulsi_const */
420 COSTS_N_INSNS (3), /* mulsi_const9 */
421 COSTS_N_INSNS (5), /* muldi */
422 COSTS_N_INSNS (35), /* divsi */
423 COSTS_N_INSNS (35), /* divdi */
424 COSTS_N_INSNS (11), /* fp */
425 COSTS_N_INSNS (11), /* dmul */
426 COSTS_N_INSNS (11), /* sdiv */
427 COSTS_N_INSNS (11), /* ddiv */
428 32, /* cache line size */
429 16, /* l1 cache */
430 128, /* l2 cache */
431 1, /* streams */
434 /* Instruction costs on PPC440 processors. */
435 static const
436 struct processor_costs ppc440_cost = {
437 COSTS_N_INSNS (3), /* mulsi */
438 COSTS_N_INSNS (2), /* mulsi_const */
439 COSTS_N_INSNS (2), /* mulsi_const9 */
440 COSTS_N_INSNS (3), /* muldi */
441 COSTS_N_INSNS (34), /* divsi */
442 COSTS_N_INSNS (34), /* divdi */
443 COSTS_N_INSNS (5), /* fp */
444 COSTS_N_INSNS (5), /* dmul */
445 COSTS_N_INSNS (19), /* sdiv */
446 COSTS_N_INSNS (33), /* ddiv */
447 32, /* cache line size */
448 32, /* l1 cache */
449 256, /* l2 cache */
450 1, /* streams */
453 /* Instruction costs on PPC476 processors. */
454 static const
455 struct processor_costs ppc476_cost = {
456 COSTS_N_INSNS (4), /* mulsi */
457 COSTS_N_INSNS (4), /* mulsi_const */
458 COSTS_N_INSNS (4), /* mulsi_const9 */
459 COSTS_N_INSNS (4), /* muldi */
460 COSTS_N_INSNS (11), /* divsi */
461 COSTS_N_INSNS (11), /* divdi */
462 COSTS_N_INSNS (6), /* fp */
463 COSTS_N_INSNS (6), /* dmul */
464 COSTS_N_INSNS (19), /* sdiv */
465 COSTS_N_INSNS (33), /* ddiv */
466 32, /* l1 cache line size */
467 32, /* l1 cache */
468 512, /* l2 cache */
469 1, /* streams */
472 /* Instruction costs on PPC601 processors. */
473 static const
474 struct processor_costs ppc601_cost = {
475 COSTS_N_INSNS (5), /* mulsi */
476 COSTS_N_INSNS (5), /* mulsi_const */
477 COSTS_N_INSNS (5), /* mulsi_const9 */
478 COSTS_N_INSNS (5), /* muldi */
479 COSTS_N_INSNS (36), /* divsi */
480 COSTS_N_INSNS (36), /* divdi */
481 COSTS_N_INSNS (4), /* fp */
482 COSTS_N_INSNS (5), /* dmul */
483 COSTS_N_INSNS (17), /* sdiv */
484 COSTS_N_INSNS (31), /* ddiv */
485 32, /* cache line size */
486 32, /* l1 cache */
487 256, /* l2 cache */
488 1, /* streams */
491 /* Instruction costs on PPC603 processors. */
492 static const
493 struct processor_costs ppc603_cost = {
494 COSTS_N_INSNS (5), /* mulsi */
495 COSTS_N_INSNS (3), /* mulsi_const */
496 COSTS_N_INSNS (2), /* mulsi_const9 */
497 COSTS_N_INSNS (5), /* muldi */
498 COSTS_N_INSNS (37), /* divsi */
499 COSTS_N_INSNS (37), /* divdi */
500 COSTS_N_INSNS (3), /* fp */
501 COSTS_N_INSNS (4), /* dmul */
502 COSTS_N_INSNS (18), /* sdiv */
503 COSTS_N_INSNS (33), /* ddiv */
504 32, /* cache line size */
505 8, /* l1 cache */
506 64, /* l2 cache */
507 1, /* streams */
510 /* Instruction costs on PPC604 processors. */
511 static const
512 struct processor_costs ppc604_cost = {
513 COSTS_N_INSNS (4), /* mulsi */
514 COSTS_N_INSNS (4), /* mulsi_const */
515 COSTS_N_INSNS (4), /* mulsi_const9 */
516 COSTS_N_INSNS (4), /* muldi */
517 COSTS_N_INSNS (20), /* divsi */
518 COSTS_N_INSNS (20), /* divdi */
519 COSTS_N_INSNS (3), /* fp */
520 COSTS_N_INSNS (3), /* dmul */
521 COSTS_N_INSNS (18), /* sdiv */
522 COSTS_N_INSNS (32), /* ddiv */
523 32, /* cache line size */
524 16, /* l1 cache */
525 512, /* l2 cache */
526 1, /* streams */
529 /* Instruction costs on PPC604e processors. */
530 static const
531 struct processor_costs ppc604e_cost = {
532 COSTS_N_INSNS (2), /* mulsi */
533 COSTS_N_INSNS (2), /* mulsi_const */
534 COSTS_N_INSNS (2), /* mulsi_const9 */
535 COSTS_N_INSNS (2), /* muldi */
536 COSTS_N_INSNS (20), /* divsi */
537 COSTS_N_INSNS (20), /* divdi */
538 COSTS_N_INSNS (3), /* fp */
539 COSTS_N_INSNS (3), /* dmul */
540 COSTS_N_INSNS (18), /* sdiv */
541 COSTS_N_INSNS (32), /* ddiv */
542 32, /* cache line size */
543 32, /* l1 cache */
544 1024, /* l2 cache */
545 1, /* streams */
548 /* Instruction costs on PPC620 processors. */
549 static const
550 struct processor_costs ppc620_cost = {
551 COSTS_N_INSNS (5), /* mulsi */
552 COSTS_N_INSNS (4), /* mulsi_const */
553 COSTS_N_INSNS (3), /* mulsi_const9 */
554 COSTS_N_INSNS (7), /* muldi */
555 COSTS_N_INSNS (21), /* divsi */
556 COSTS_N_INSNS (37), /* divdi */
557 COSTS_N_INSNS (3), /* fp */
558 COSTS_N_INSNS (3), /* dmul */
559 COSTS_N_INSNS (18), /* sdiv */
560 COSTS_N_INSNS (32), /* ddiv */
561 128, /* cache line size */
562 32, /* l1 cache */
563 1024, /* l2 cache */
564 1, /* streams */
567 /* Instruction costs on PPC630 processors. */
568 static const
569 struct processor_costs ppc630_cost = {
570 COSTS_N_INSNS (5), /* mulsi */
571 COSTS_N_INSNS (4), /* mulsi_const */
572 COSTS_N_INSNS (3), /* mulsi_const9 */
573 COSTS_N_INSNS (7), /* muldi */
574 COSTS_N_INSNS (21), /* divsi */
575 COSTS_N_INSNS (37), /* divdi */
576 COSTS_N_INSNS (3), /* fp */
577 COSTS_N_INSNS (3), /* dmul */
578 COSTS_N_INSNS (17), /* sdiv */
579 COSTS_N_INSNS (21), /* ddiv */
580 128, /* cache line size */
581 64, /* l1 cache */
582 1024, /* l2 cache */
583 1, /* streams */
586 /* Instruction costs on Cell processor. */
587 /* COSTS_N_INSNS (1) ~ one add. */
588 static const
589 struct processor_costs ppccell_cost = {
590 COSTS_N_INSNS (9/2)+2, /* mulsi */
591 COSTS_N_INSNS (6/2), /* mulsi_const */
592 COSTS_N_INSNS (6/2), /* mulsi_const9 */
593 COSTS_N_INSNS (15/2)+2, /* muldi */
594 COSTS_N_INSNS (38/2), /* divsi */
595 COSTS_N_INSNS (70/2), /* divdi */
596 COSTS_N_INSNS (10/2), /* fp */
597 COSTS_N_INSNS (10/2), /* dmul */
598 COSTS_N_INSNS (74/2), /* sdiv */
599 COSTS_N_INSNS (74/2), /* ddiv */
600 128, /* cache line size */
601 32, /* l1 cache */
602 512, /* l2 cache */
603 6, /* streams */
606 /* Instruction costs on PPC750 and PPC7400 processors. */
607 static const
608 struct processor_costs ppc750_cost = {
609 COSTS_N_INSNS (5), /* mulsi */
610 COSTS_N_INSNS (3), /* mulsi_const */
611 COSTS_N_INSNS (2), /* mulsi_const9 */
612 COSTS_N_INSNS (5), /* muldi */
613 COSTS_N_INSNS (17), /* divsi */
614 COSTS_N_INSNS (17), /* divdi */
615 COSTS_N_INSNS (3), /* fp */
616 COSTS_N_INSNS (3), /* dmul */
617 COSTS_N_INSNS (17), /* sdiv */
618 COSTS_N_INSNS (31), /* ddiv */
619 32, /* cache line size */
620 32, /* l1 cache */
621 512, /* l2 cache */
622 1, /* streams */
625 /* Instruction costs on PPC7450 processors. */
626 static const
627 struct processor_costs ppc7450_cost = {
628 COSTS_N_INSNS (4), /* mulsi */
629 COSTS_N_INSNS (3), /* mulsi_const */
630 COSTS_N_INSNS (3), /* mulsi_const9 */
631 COSTS_N_INSNS (4), /* muldi */
632 COSTS_N_INSNS (23), /* divsi */
633 COSTS_N_INSNS (23), /* divdi */
634 COSTS_N_INSNS (5), /* fp */
635 COSTS_N_INSNS (5), /* dmul */
636 COSTS_N_INSNS (21), /* sdiv */
637 COSTS_N_INSNS (35), /* ddiv */
638 32, /* cache line size */
639 32, /* l1 cache */
640 1024, /* l2 cache */
641 1, /* streams */
644 /* Instruction costs on PPC8540 processors. */
645 static const
646 struct processor_costs ppc8540_cost = {
647 COSTS_N_INSNS (4), /* mulsi */
648 COSTS_N_INSNS (4), /* mulsi_const */
649 COSTS_N_INSNS (4), /* mulsi_const9 */
650 COSTS_N_INSNS (4), /* muldi */
651 COSTS_N_INSNS (19), /* divsi */
652 COSTS_N_INSNS (19), /* divdi */
653 COSTS_N_INSNS (4), /* fp */
654 COSTS_N_INSNS (4), /* dmul */
655 COSTS_N_INSNS (29), /* sdiv */
656 COSTS_N_INSNS (29), /* ddiv */
657 32, /* cache line size */
658 32, /* l1 cache */
659 256, /* l2 cache */
660 1, /* prefetch streams /*/
663 /* Instruction costs on E300C2 and E300C3 cores. */
664 static const
665 struct processor_costs ppce300c2c3_cost = {
666 COSTS_N_INSNS (4), /* mulsi */
667 COSTS_N_INSNS (4), /* mulsi_const */
668 COSTS_N_INSNS (4), /* mulsi_const9 */
669 COSTS_N_INSNS (4), /* muldi */
670 COSTS_N_INSNS (19), /* divsi */
671 COSTS_N_INSNS (19), /* divdi */
672 COSTS_N_INSNS (3), /* fp */
673 COSTS_N_INSNS (4), /* dmul */
674 COSTS_N_INSNS (18), /* sdiv */
675 COSTS_N_INSNS (33), /* ddiv */
677 16, /* l1 cache */
678 16, /* l2 cache */
679 1, /* prefetch streams /*/
682 /* Instruction costs on PPCE500MC processors. */
683 static const
684 struct processor_costs ppce500mc_cost = {
685 COSTS_N_INSNS (4), /* mulsi */
686 COSTS_N_INSNS (4), /* mulsi_const */
687 COSTS_N_INSNS (4), /* mulsi_const9 */
688 COSTS_N_INSNS (4), /* muldi */
689 COSTS_N_INSNS (14), /* divsi */
690 COSTS_N_INSNS (14), /* divdi */
691 COSTS_N_INSNS (8), /* fp */
692 COSTS_N_INSNS (10), /* dmul */
693 COSTS_N_INSNS (36), /* sdiv */
694 COSTS_N_INSNS (66), /* ddiv */
695 64, /* cache line size */
696 32, /* l1 cache */
697 128, /* l2 cache */
698 1, /* prefetch streams /*/
701 /* Instruction costs on PPCE500MC64 processors. */
702 static const
703 struct processor_costs ppce500mc64_cost = {
704 COSTS_N_INSNS (4), /* mulsi */
705 COSTS_N_INSNS (4), /* mulsi_const */
706 COSTS_N_INSNS (4), /* mulsi_const9 */
707 COSTS_N_INSNS (4), /* muldi */
708 COSTS_N_INSNS (14), /* divsi */
709 COSTS_N_INSNS (14), /* divdi */
710 COSTS_N_INSNS (4), /* fp */
711 COSTS_N_INSNS (10), /* dmul */
712 COSTS_N_INSNS (36), /* sdiv */
713 COSTS_N_INSNS (66), /* ddiv */
714 64, /* cache line size */
715 32, /* l1 cache */
716 128, /* l2 cache */
717 1, /* prefetch streams /*/
720 /* Instruction costs on PPCE5500 processors. */
721 static const
722 struct processor_costs ppce5500_cost = {
723 COSTS_N_INSNS (5), /* mulsi */
724 COSTS_N_INSNS (5), /* mulsi_const */
725 COSTS_N_INSNS (4), /* mulsi_const9 */
726 COSTS_N_INSNS (5), /* muldi */
727 COSTS_N_INSNS (14), /* divsi */
728 COSTS_N_INSNS (14), /* divdi */
729 COSTS_N_INSNS (7), /* fp */
730 COSTS_N_INSNS (10), /* dmul */
731 COSTS_N_INSNS (36), /* sdiv */
732 COSTS_N_INSNS (66), /* ddiv */
733 64, /* cache line size */
734 32, /* l1 cache */
735 128, /* l2 cache */
736 1, /* prefetch streams /*/
739 /* Instruction costs on PPCE6500 processors. */
740 static const
741 struct processor_costs ppce6500_cost = {
742 COSTS_N_INSNS (5), /* mulsi */
743 COSTS_N_INSNS (5), /* mulsi_const */
744 COSTS_N_INSNS (4), /* mulsi_const9 */
745 COSTS_N_INSNS (5), /* muldi */
746 COSTS_N_INSNS (14), /* divsi */
747 COSTS_N_INSNS (14), /* divdi */
748 COSTS_N_INSNS (7), /* fp */
749 COSTS_N_INSNS (10), /* dmul */
750 COSTS_N_INSNS (36), /* sdiv */
751 COSTS_N_INSNS (66), /* ddiv */
752 64, /* cache line size */
753 32, /* l1 cache */
754 128, /* l2 cache */
755 1, /* prefetch streams /*/
758 /* Instruction costs on AppliedMicro Titan processors. */
759 static const
760 struct processor_costs titan_cost = {
761 COSTS_N_INSNS (5), /* mulsi */
762 COSTS_N_INSNS (5), /* mulsi_const */
763 COSTS_N_INSNS (5), /* mulsi_const9 */
764 COSTS_N_INSNS (5), /* muldi */
765 COSTS_N_INSNS (18), /* divsi */
766 COSTS_N_INSNS (18), /* divdi */
767 COSTS_N_INSNS (10), /* fp */
768 COSTS_N_INSNS (10), /* dmul */
769 COSTS_N_INSNS (46), /* sdiv */
770 COSTS_N_INSNS (72), /* ddiv */
771 32, /* cache line size */
772 32, /* l1 cache */
773 512, /* l2 cache */
774 1, /* prefetch streams /*/
777 /* Instruction costs on POWER4 and POWER5 processors. */
778 static const
779 struct processor_costs power4_cost = {
780 COSTS_N_INSNS (3), /* mulsi */
781 COSTS_N_INSNS (2), /* mulsi_const */
782 COSTS_N_INSNS (2), /* mulsi_const9 */
783 COSTS_N_INSNS (4), /* muldi */
784 COSTS_N_INSNS (18), /* divsi */
785 COSTS_N_INSNS (34), /* divdi */
786 COSTS_N_INSNS (3), /* fp */
787 COSTS_N_INSNS (3), /* dmul */
788 COSTS_N_INSNS (17), /* sdiv */
789 COSTS_N_INSNS (17), /* ddiv */
790 128, /* cache line size */
791 32, /* l1 cache */
792 1024, /* l2 cache */
793 8, /* prefetch streams /*/
796 /* Instruction costs on POWER6 processors. */
797 static const
798 struct processor_costs power6_cost = {
799 COSTS_N_INSNS (8), /* mulsi */
800 COSTS_N_INSNS (8), /* mulsi_const */
801 COSTS_N_INSNS (8), /* mulsi_const9 */
802 COSTS_N_INSNS (8), /* muldi */
803 COSTS_N_INSNS (22), /* divsi */
804 COSTS_N_INSNS (28), /* divdi */
805 COSTS_N_INSNS (3), /* fp */
806 COSTS_N_INSNS (3), /* dmul */
807 COSTS_N_INSNS (13), /* sdiv */
808 COSTS_N_INSNS (16), /* ddiv */
809 128, /* cache line size */
810 64, /* l1 cache */
811 2048, /* l2 cache */
812 16, /* prefetch streams */
815 /* Instruction costs on POWER7 processors. */
816 static const
817 struct processor_costs power7_cost = {
818 COSTS_N_INSNS (2), /* mulsi */
819 COSTS_N_INSNS (2), /* mulsi_const */
820 COSTS_N_INSNS (2), /* mulsi_const9 */
821 COSTS_N_INSNS (2), /* muldi */
822 COSTS_N_INSNS (18), /* divsi */
823 COSTS_N_INSNS (34), /* divdi */
824 COSTS_N_INSNS (3), /* fp */
825 COSTS_N_INSNS (3), /* dmul */
826 COSTS_N_INSNS (13), /* sdiv */
827 COSTS_N_INSNS (16), /* ddiv */
828 128, /* cache line size */
829 32, /* l1 cache */
830 256, /* l2 cache */
831 12, /* prefetch streams */
834 /* Instruction costs on POWER A2 processors. */
835 static const
836 struct processor_costs ppca2_cost = {
837 COSTS_N_INSNS (16), /* mulsi */
838 COSTS_N_INSNS (16), /* mulsi_const */
839 COSTS_N_INSNS (16), /* mulsi_const9 */
840 COSTS_N_INSNS (16), /* muldi */
841 COSTS_N_INSNS (22), /* divsi */
842 COSTS_N_INSNS (28), /* divdi */
843 COSTS_N_INSNS (3), /* fp */
844 COSTS_N_INSNS (3), /* dmul */
845 COSTS_N_INSNS (59), /* sdiv */
846 COSTS_N_INSNS (72), /* ddiv */
848 16, /* l1 cache */
849 2048, /* l2 cache */
850 16, /* prefetch streams */
854 /* Table that classifies rs6000 builtin functions (pure, const, etc.). */
855 #undef RS6000_BUILTIN_1
856 #undef RS6000_BUILTIN_2
857 #undef RS6000_BUILTIN_3
858 #undef RS6000_BUILTIN_A
859 #undef RS6000_BUILTIN_D
860 #undef RS6000_BUILTIN_E
861 #undef RS6000_BUILTIN_P
862 #undef RS6000_BUILTIN_Q
863 #undef RS6000_BUILTIN_S
864 #undef RS6000_BUILTIN_X
866 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE) \
867 { NAME, ICODE, MASK, ATTR },
869 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE) \
870 { NAME, ICODE, MASK, ATTR },
872 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE) \
873 { NAME, ICODE, MASK, ATTR },
875 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE) \
876 { NAME, ICODE, MASK, ATTR },
878 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE) \
879 { NAME, ICODE, MASK, ATTR },
881 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE) \
882 { NAME, ICODE, MASK, ATTR },
884 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE) \
885 { NAME, ICODE, MASK, ATTR },
887 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE) \
888 { NAME, ICODE, MASK, ATTR },
890 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE) \
891 { NAME, ICODE, MASK, ATTR },
893 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE) \
894 { NAME, ICODE, MASK, ATTR },
896 struct rs6000_builtin_info_type {
897 const char *name;
898 const enum insn_code icode;
899 const HOST_WIDE_INT mask;
900 const unsigned attr;
903 static const struct rs6000_builtin_info_type rs6000_builtin_info[] =
905 #include "rs6000-builtin.def"
908 #undef RS6000_BUILTIN_1
909 #undef RS6000_BUILTIN_2
910 #undef RS6000_BUILTIN_3
911 #undef RS6000_BUILTIN_A
912 #undef RS6000_BUILTIN_D
913 #undef RS6000_BUILTIN_E
914 #undef RS6000_BUILTIN_P
915 #undef RS6000_BUILTIN_Q
916 #undef RS6000_BUILTIN_S
917 #undef RS6000_BUILTIN_X
919 /* Support for -mveclibabi=<xxx> to control which vector library to use. */
920 static tree (*rs6000_veclib_handler) (tree, tree, tree);
923 static bool rs6000_debug_legitimate_address_p (enum machine_mode, rtx, bool);
924 static bool spe_func_has_64bit_regs_p (void);
925 static struct machine_function * rs6000_init_machine_status (void);
926 static int rs6000_ra_ever_killed (void);
927 static tree rs6000_handle_longcall_attribute (tree *, tree, tree, int, bool *);
928 static tree rs6000_handle_altivec_attribute (tree *, tree, tree, int, bool *);
929 static tree rs6000_handle_struct_attribute (tree *, tree, tree, int, bool *);
930 static tree rs6000_builtin_vectorized_libmass (tree, tree, tree);
931 static rtx rs6000_emit_set_long_const (rtx, HOST_WIDE_INT, HOST_WIDE_INT);
932 static int rs6000_memory_move_cost (enum machine_mode, reg_class_t, bool);
933 static bool rs6000_debug_rtx_costs (rtx, int, int, int, int *, bool);
934 static int rs6000_debug_address_cost (rtx, enum machine_mode, addr_space_t,
935 bool);
936 static int rs6000_debug_adjust_cost (rtx, rtx, rtx, int);
937 static bool is_microcoded_insn (rtx);
938 static bool is_nonpipeline_insn (rtx);
939 static bool is_cracked_insn (rtx);
940 static bool is_load_insn (rtx, rtx *);
941 static bool is_store_insn (rtx, rtx *);
942 static bool set_to_load_agen (rtx,rtx);
943 static bool insn_terminates_group_p (rtx , enum group_termination);
944 static bool insn_must_be_first_in_group (rtx);
945 static bool insn_must_be_last_in_group (rtx);
946 static void altivec_init_builtins (void);
947 static tree builtin_function_type (enum machine_mode, enum machine_mode,
948 enum machine_mode, enum machine_mode,
949 enum rs6000_builtins, const char *name);
950 static void rs6000_common_init_builtins (void);
951 static void paired_init_builtins (void);
952 static rtx paired_expand_predicate_builtin (enum insn_code, tree, rtx);
953 static void spe_init_builtins (void);
954 static rtx spe_expand_predicate_builtin (enum insn_code, tree, rtx);
955 static rtx spe_expand_evsel_builtin (enum insn_code, tree, rtx);
956 static int rs6000_emit_int_cmove (rtx, rtx, rtx, rtx);
957 static rs6000_stack_t *rs6000_stack_info (void);
958 static void is_altivec_return_reg (rtx, void *);
959 int easy_vector_constant (rtx, enum machine_mode);
960 static rtx rs6000_debug_legitimize_address (rtx, rtx, enum machine_mode);
961 static rtx rs6000_legitimize_tls_address (rtx, enum tls_model);
962 static int rs6000_tls_symbol_ref_1 (rtx *, void *);
963 static int rs6000_get_some_local_dynamic_name_1 (rtx *, void *);
964 static rtx rs6000_darwin64_record_arg (CUMULATIVE_ARGS *, const_tree,
965 bool, bool);
966 #if TARGET_MACHO
967 static void macho_branch_islands (void);
968 #endif
969 static rtx rs6000_legitimize_reload_address (rtx, enum machine_mode, int, int,
970 int, int *);
971 static rtx rs6000_debug_legitimize_reload_address (rtx, enum machine_mode, int,
972 int, int, int *);
973 static bool rs6000_mode_dependent_address (const_rtx);
974 static bool rs6000_debug_mode_dependent_address (const_rtx);
975 static enum reg_class rs6000_secondary_reload_class (enum reg_class,
976 enum machine_mode, rtx);
977 static enum reg_class rs6000_debug_secondary_reload_class (enum reg_class,
978 enum machine_mode,
979 rtx);
980 static enum reg_class rs6000_preferred_reload_class (rtx, enum reg_class);
981 static enum reg_class rs6000_debug_preferred_reload_class (rtx,
982 enum reg_class);
983 static bool rs6000_secondary_memory_needed (enum reg_class, enum reg_class,
984 enum machine_mode);
985 static bool rs6000_debug_secondary_memory_needed (enum reg_class,
986 enum reg_class,
987 enum machine_mode);
988 static bool rs6000_cannot_change_mode_class (enum machine_mode,
989 enum machine_mode,
990 enum reg_class);
991 static bool rs6000_debug_cannot_change_mode_class (enum machine_mode,
992 enum machine_mode,
993 enum reg_class);
994 static bool rs6000_save_toc_in_prologue_p (void);
996 rtx (*rs6000_legitimize_reload_address_ptr) (rtx, enum machine_mode, int, int,
997 int, int *)
998 = rs6000_legitimize_reload_address;
1000 static bool (*rs6000_mode_dependent_address_ptr) (const_rtx)
1001 = rs6000_mode_dependent_address;
1003 enum reg_class (*rs6000_secondary_reload_class_ptr) (enum reg_class,
1004 enum machine_mode, rtx)
1005 = rs6000_secondary_reload_class;
1007 enum reg_class (*rs6000_preferred_reload_class_ptr) (rtx, enum reg_class)
1008 = rs6000_preferred_reload_class;
1010 bool (*rs6000_secondary_memory_needed_ptr) (enum reg_class, enum reg_class,
1011 enum machine_mode)
1012 = rs6000_secondary_memory_needed;
1014 bool (*rs6000_cannot_change_mode_class_ptr) (enum machine_mode,
1015 enum machine_mode,
1016 enum reg_class)
1017 = rs6000_cannot_change_mode_class;
1019 const int INSN_NOT_AVAILABLE = -1;
1021 static void rs6000_print_isa_options (FILE *, int, const char *,
1022 HOST_WIDE_INT);
1023 static void rs6000_print_builtin_options (FILE *, int, const char *,
1024 HOST_WIDE_INT);
1026 /* Hash table stuff for keeping track of TOC entries. */
1028 struct GTY(()) toc_hash_struct
1030 /* `key' will satisfy CONSTANT_P; in fact, it will satisfy
1031 ASM_OUTPUT_SPECIAL_POOL_ENTRY_P. */
1032 rtx key;
1033 enum machine_mode key_mode;
1034 int labelno;
1037 static GTY ((param_is (struct toc_hash_struct))) htab_t toc_hash_table;
1039 /* Hash table to keep track of the argument types for builtin functions. */
1041 struct GTY(()) builtin_hash_struct
1043 tree type;
1044 enum machine_mode mode[4]; /* return value + 3 arguments. */
1045 unsigned char uns_p[4]; /* and whether the types are unsigned. */
1048 static GTY ((param_is (struct builtin_hash_struct))) htab_t builtin_hash_table;
1051 /* Default register names. */
1052 char rs6000_reg_names[][8] =
1054 "0", "1", "2", "3", "4", "5", "6", "7",
1055 "8", "9", "10", "11", "12", "13", "14", "15",
1056 "16", "17", "18", "19", "20", "21", "22", "23",
1057 "24", "25", "26", "27", "28", "29", "30", "31",
1058 "0", "1", "2", "3", "4", "5", "6", "7",
1059 "8", "9", "10", "11", "12", "13", "14", "15",
1060 "16", "17", "18", "19", "20", "21", "22", "23",
1061 "24", "25", "26", "27", "28", "29", "30", "31",
1062 "mq", "lr", "ctr","ap",
1063 "0", "1", "2", "3", "4", "5", "6", "7",
1064 "ca",
1065 /* AltiVec registers. */
1066 "0", "1", "2", "3", "4", "5", "6", "7",
1067 "8", "9", "10", "11", "12", "13", "14", "15",
1068 "16", "17", "18", "19", "20", "21", "22", "23",
1069 "24", "25", "26", "27", "28", "29", "30", "31",
1070 "vrsave", "vscr",
1071 /* SPE registers. */
1072 "spe_acc", "spefscr",
1073 /* Soft frame pointer. */
1074 "sfp"
1077 #ifdef TARGET_REGNAMES
1078 static const char alt_reg_names[][8] =
1080 "%r0", "%r1", "%r2", "%r3", "%r4", "%r5", "%r6", "%r7",
1081 "%r8", "%r9", "%r10", "%r11", "%r12", "%r13", "%r14", "%r15",
1082 "%r16", "%r17", "%r18", "%r19", "%r20", "%r21", "%r22", "%r23",
1083 "%r24", "%r25", "%r26", "%r27", "%r28", "%r29", "%r30", "%r31",
1084 "%f0", "%f1", "%f2", "%f3", "%f4", "%f5", "%f6", "%f7",
1085 "%f8", "%f9", "%f10", "%f11", "%f12", "%f13", "%f14", "%f15",
1086 "%f16", "%f17", "%f18", "%f19", "%f20", "%f21", "%f22", "%f23",
1087 "%f24", "%f25", "%f26", "%f27", "%f28", "%f29", "%f30", "%f31",
1088 "mq", "lr", "ctr", "ap",
1089 "%cr0", "%cr1", "%cr2", "%cr3", "%cr4", "%cr5", "%cr6", "%cr7",
1090 "ca",
1091 /* AltiVec registers. */
1092 "%v0", "%v1", "%v2", "%v3", "%v4", "%v5", "%v6", "%v7",
1093 "%v8", "%v9", "%v10", "%v11", "%v12", "%v13", "%v14", "%v15",
1094 "%v16", "%v17", "%v18", "%v19", "%v20", "%v21", "%v22", "%v23",
1095 "%v24", "%v25", "%v26", "%v27", "%v28", "%v29", "%v30", "%v31",
1096 "vrsave", "vscr",
1097 /* SPE registers. */
1098 "spe_acc", "spefscr",
1099 /* Soft frame pointer. */
1100 "sfp"
1102 #endif
1104 /* Table of valid machine attributes. */
1106 static const struct attribute_spec rs6000_attribute_table[] =
1108 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
1109 affects_type_identity } */
1110 { "altivec", 1, 1, false, true, false, rs6000_handle_altivec_attribute,
1111 false },
1112 { "longcall", 0, 0, false, true, true, rs6000_handle_longcall_attribute,
1113 false },
1114 { "shortcall", 0, 0, false, true, true, rs6000_handle_longcall_attribute,
1115 false },
1116 { "ms_struct", 0, 0, false, false, false, rs6000_handle_struct_attribute,
1117 false },
1118 { "gcc_struct", 0, 0, false, false, false, rs6000_handle_struct_attribute,
1119 false },
1120 #ifdef SUBTARGET_ATTRIBUTE_TABLE
1121 SUBTARGET_ATTRIBUTE_TABLE,
1122 #endif
1123 { NULL, 0, 0, false, false, false, NULL, false }
1126 #ifndef TARGET_PROFILE_KERNEL
1127 #define TARGET_PROFILE_KERNEL 0
1128 #endif
1130 /* The VRSAVE bitmask puts bit %v0 as the most significant bit. */
1131 #define ALTIVEC_REG_BIT(REGNO) (0x80000000 >> ((REGNO) - FIRST_ALTIVEC_REGNO))
1133 /* Initialize the GCC target structure. */
1134 #undef TARGET_ATTRIBUTE_TABLE
1135 #define TARGET_ATTRIBUTE_TABLE rs6000_attribute_table
1136 #undef TARGET_SET_DEFAULT_TYPE_ATTRIBUTES
1137 #define TARGET_SET_DEFAULT_TYPE_ATTRIBUTES rs6000_set_default_type_attributes
1138 #undef TARGET_ATTRIBUTE_TAKES_IDENTIFIER_P
1139 #define TARGET_ATTRIBUTE_TAKES_IDENTIFIER_P rs6000_attribute_takes_identifier_p
1141 #undef TARGET_ASM_ALIGNED_DI_OP
1142 #define TARGET_ASM_ALIGNED_DI_OP DOUBLE_INT_ASM_OP
1144 /* Default unaligned ops are only provided for ELF. Find the ops needed
1145 for non-ELF systems. */
1146 #ifndef OBJECT_FORMAT_ELF
1147 #if TARGET_XCOFF
1148 /* For XCOFF. rs6000_assemble_integer will handle unaligned DIs on
1149 64-bit targets. */
1150 #undef TARGET_ASM_UNALIGNED_HI_OP
1151 #define TARGET_ASM_UNALIGNED_HI_OP "\t.vbyte\t2,"
1152 #undef TARGET_ASM_UNALIGNED_SI_OP
1153 #define TARGET_ASM_UNALIGNED_SI_OP "\t.vbyte\t4,"
1154 #undef TARGET_ASM_UNALIGNED_DI_OP
1155 #define TARGET_ASM_UNALIGNED_DI_OP "\t.vbyte\t8,"
1156 #else
1157 /* For Darwin. */
1158 #undef TARGET_ASM_UNALIGNED_HI_OP
1159 #define TARGET_ASM_UNALIGNED_HI_OP "\t.short\t"
1160 #undef TARGET_ASM_UNALIGNED_SI_OP
1161 #define TARGET_ASM_UNALIGNED_SI_OP "\t.long\t"
1162 #undef TARGET_ASM_UNALIGNED_DI_OP
1163 #define TARGET_ASM_UNALIGNED_DI_OP "\t.quad\t"
1164 #undef TARGET_ASM_ALIGNED_DI_OP
1165 #define TARGET_ASM_ALIGNED_DI_OP "\t.quad\t"
1166 #endif
1167 #endif
1169 /* This hook deals with fixups for relocatable code and DI-mode objects
1170 in 64-bit code. */
1171 #undef TARGET_ASM_INTEGER
1172 #define TARGET_ASM_INTEGER rs6000_assemble_integer
1174 #if defined (HAVE_GAS_HIDDEN) && !TARGET_MACHO
1175 #undef TARGET_ASM_ASSEMBLE_VISIBILITY
1176 #define TARGET_ASM_ASSEMBLE_VISIBILITY rs6000_assemble_visibility
1177 #endif
1179 #undef TARGET_SET_UP_BY_PROLOGUE
1180 #define TARGET_SET_UP_BY_PROLOGUE rs6000_set_up_by_prologue
1182 #undef TARGET_HAVE_TLS
1183 #define TARGET_HAVE_TLS HAVE_AS_TLS
1185 #undef TARGET_CANNOT_FORCE_CONST_MEM
1186 #define TARGET_CANNOT_FORCE_CONST_MEM rs6000_cannot_force_const_mem
1188 #undef TARGET_DELEGITIMIZE_ADDRESS
1189 #define TARGET_DELEGITIMIZE_ADDRESS rs6000_delegitimize_address
1191 #undef TARGET_CONST_NOT_OK_FOR_DEBUG_P
1192 #define TARGET_CONST_NOT_OK_FOR_DEBUG_P rs6000_const_not_ok_for_debug_p
1194 #undef TARGET_ASM_FUNCTION_PROLOGUE
1195 #define TARGET_ASM_FUNCTION_PROLOGUE rs6000_output_function_prologue
1196 #undef TARGET_ASM_FUNCTION_EPILOGUE
1197 #define TARGET_ASM_FUNCTION_EPILOGUE rs6000_output_function_epilogue
1199 #undef TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA
1200 #define TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA rs6000_output_addr_const_extra
1202 #undef TARGET_LEGITIMIZE_ADDRESS
1203 #define TARGET_LEGITIMIZE_ADDRESS rs6000_legitimize_address
1205 #undef TARGET_SCHED_VARIABLE_ISSUE
1206 #define TARGET_SCHED_VARIABLE_ISSUE rs6000_variable_issue
1208 #undef TARGET_SCHED_ISSUE_RATE
1209 #define TARGET_SCHED_ISSUE_RATE rs6000_issue_rate
1210 #undef TARGET_SCHED_ADJUST_COST
1211 #define TARGET_SCHED_ADJUST_COST rs6000_adjust_cost
1212 #undef TARGET_SCHED_ADJUST_PRIORITY
1213 #define TARGET_SCHED_ADJUST_PRIORITY rs6000_adjust_priority
1214 #undef TARGET_SCHED_IS_COSTLY_DEPENDENCE
1215 #define TARGET_SCHED_IS_COSTLY_DEPENDENCE rs6000_is_costly_dependence
1216 #undef TARGET_SCHED_INIT
1217 #define TARGET_SCHED_INIT rs6000_sched_init
1218 #undef TARGET_SCHED_FINISH
1219 #define TARGET_SCHED_FINISH rs6000_sched_finish
1220 #undef TARGET_SCHED_REORDER
1221 #define TARGET_SCHED_REORDER rs6000_sched_reorder
1222 #undef TARGET_SCHED_REORDER2
1223 #define TARGET_SCHED_REORDER2 rs6000_sched_reorder2
1225 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD
1226 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD rs6000_use_sched_lookahead
1228 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD_GUARD
1229 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD_GUARD rs6000_use_sched_lookahead_guard
1231 #undef TARGET_SCHED_ALLOC_SCHED_CONTEXT
1232 #define TARGET_SCHED_ALLOC_SCHED_CONTEXT rs6000_alloc_sched_context
1233 #undef TARGET_SCHED_INIT_SCHED_CONTEXT
1234 #define TARGET_SCHED_INIT_SCHED_CONTEXT rs6000_init_sched_context
1235 #undef TARGET_SCHED_SET_SCHED_CONTEXT
1236 #define TARGET_SCHED_SET_SCHED_CONTEXT rs6000_set_sched_context
1237 #undef TARGET_SCHED_FREE_SCHED_CONTEXT
1238 #define TARGET_SCHED_FREE_SCHED_CONTEXT rs6000_free_sched_context
1240 #undef TARGET_VECTORIZE_BUILTIN_MASK_FOR_LOAD
1241 #define TARGET_VECTORIZE_BUILTIN_MASK_FOR_LOAD rs6000_builtin_mask_for_load
1242 #undef TARGET_VECTORIZE_SUPPORT_VECTOR_MISALIGNMENT
1243 #define TARGET_VECTORIZE_SUPPORT_VECTOR_MISALIGNMENT \
1244 rs6000_builtin_support_vector_misalignment
1245 #undef TARGET_VECTORIZE_VECTOR_ALIGNMENT_REACHABLE
1246 #define TARGET_VECTORIZE_VECTOR_ALIGNMENT_REACHABLE rs6000_vector_alignment_reachable
1247 #undef TARGET_VECTORIZE_BUILTIN_VECTORIZATION_COST
1248 #define TARGET_VECTORIZE_BUILTIN_VECTORIZATION_COST \
1249 rs6000_builtin_vectorization_cost
1250 #undef TARGET_VECTORIZE_PREFERRED_SIMD_MODE
1251 #define TARGET_VECTORIZE_PREFERRED_SIMD_MODE \
1252 rs6000_preferred_simd_mode
1253 #undef TARGET_VECTORIZE_INIT_COST
1254 #define TARGET_VECTORIZE_INIT_COST rs6000_init_cost
1255 #undef TARGET_VECTORIZE_ADD_STMT_COST
1256 #define TARGET_VECTORIZE_ADD_STMT_COST rs6000_add_stmt_cost
1257 #undef TARGET_VECTORIZE_FINISH_COST
1258 #define TARGET_VECTORIZE_FINISH_COST rs6000_finish_cost
1259 #undef TARGET_VECTORIZE_DESTROY_COST_DATA
1260 #define TARGET_VECTORIZE_DESTROY_COST_DATA rs6000_destroy_cost_data
1262 #undef TARGET_INIT_BUILTINS
1263 #define TARGET_INIT_BUILTINS rs6000_init_builtins
1264 #undef TARGET_BUILTIN_DECL
1265 #define TARGET_BUILTIN_DECL rs6000_builtin_decl
1267 #undef TARGET_EXPAND_BUILTIN
1268 #define TARGET_EXPAND_BUILTIN rs6000_expand_builtin
1270 #undef TARGET_MANGLE_TYPE
1271 #define TARGET_MANGLE_TYPE rs6000_mangle_type
1273 #undef TARGET_INIT_LIBFUNCS
1274 #define TARGET_INIT_LIBFUNCS rs6000_init_libfuncs
1276 #if TARGET_MACHO
1277 #undef TARGET_BINDS_LOCAL_P
1278 #define TARGET_BINDS_LOCAL_P darwin_binds_local_p
1279 #endif
1281 #undef TARGET_MS_BITFIELD_LAYOUT_P
1282 #define TARGET_MS_BITFIELD_LAYOUT_P rs6000_ms_bitfield_layout_p
1284 #undef TARGET_ASM_OUTPUT_MI_THUNK
1285 #define TARGET_ASM_OUTPUT_MI_THUNK rs6000_output_mi_thunk
1287 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
1288 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK hook_bool_const_tree_hwi_hwi_const_tree_true
1290 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
1291 #define TARGET_FUNCTION_OK_FOR_SIBCALL rs6000_function_ok_for_sibcall
1293 #undef TARGET_REGISTER_MOVE_COST
1294 #define TARGET_REGISTER_MOVE_COST rs6000_register_move_cost
1295 #undef TARGET_MEMORY_MOVE_COST
1296 #define TARGET_MEMORY_MOVE_COST rs6000_memory_move_cost
1297 #undef TARGET_RTX_COSTS
1298 #define TARGET_RTX_COSTS rs6000_rtx_costs
1299 #undef TARGET_ADDRESS_COST
1300 #define TARGET_ADDRESS_COST hook_int_rtx_mode_as_bool_0
1302 #undef TARGET_DWARF_REGISTER_SPAN
1303 #define TARGET_DWARF_REGISTER_SPAN rs6000_dwarf_register_span
1305 #undef TARGET_INIT_DWARF_REG_SIZES_EXTRA
1306 #define TARGET_INIT_DWARF_REG_SIZES_EXTRA rs6000_init_dwarf_reg_sizes_extra
1308 #undef TARGET_MEMBER_TYPE_FORCES_BLK
1309 #define TARGET_MEMBER_TYPE_FORCES_BLK rs6000_member_type_forces_blk
1311 /* On rs6000, function arguments are promoted, as are function return
1312 values. */
1313 #undef TARGET_PROMOTE_FUNCTION_MODE
1314 #define TARGET_PROMOTE_FUNCTION_MODE default_promote_function_mode_always_promote
1316 #undef TARGET_RETURN_IN_MEMORY
1317 #define TARGET_RETURN_IN_MEMORY rs6000_return_in_memory
1319 #undef TARGET_SETUP_INCOMING_VARARGS
1320 #define TARGET_SETUP_INCOMING_VARARGS setup_incoming_varargs
1322 /* Always strict argument naming on rs6000. */
1323 #undef TARGET_STRICT_ARGUMENT_NAMING
1324 #define TARGET_STRICT_ARGUMENT_NAMING hook_bool_CUMULATIVE_ARGS_true
1325 #undef TARGET_PRETEND_OUTGOING_VARARGS_NAMED
1326 #define TARGET_PRETEND_OUTGOING_VARARGS_NAMED hook_bool_CUMULATIVE_ARGS_true
1327 #undef TARGET_SPLIT_COMPLEX_ARG
1328 #define TARGET_SPLIT_COMPLEX_ARG hook_bool_const_tree_true
1329 #undef TARGET_MUST_PASS_IN_STACK
1330 #define TARGET_MUST_PASS_IN_STACK rs6000_must_pass_in_stack
1331 #undef TARGET_PASS_BY_REFERENCE
1332 #define TARGET_PASS_BY_REFERENCE rs6000_pass_by_reference
1333 #undef TARGET_ARG_PARTIAL_BYTES
1334 #define TARGET_ARG_PARTIAL_BYTES rs6000_arg_partial_bytes
1335 #undef TARGET_FUNCTION_ARG_ADVANCE
1336 #define TARGET_FUNCTION_ARG_ADVANCE rs6000_function_arg_advance
1337 #undef TARGET_FUNCTION_ARG
1338 #define TARGET_FUNCTION_ARG rs6000_function_arg
1339 #undef TARGET_FUNCTION_ARG_BOUNDARY
1340 #define TARGET_FUNCTION_ARG_BOUNDARY rs6000_function_arg_boundary
1342 #undef TARGET_BUILD_BUILTIN_VA_LIST
1343 #define TARGET_BUILD_BUILTIN_VA_LIST rs6000_build_builtin_va_list
1345 #undef TARGET_EXPAND_BUILTIN_VA_START
1346 #define TARGET_EXPAND_BUILTIN_VA_START rs6000_va_start
1348 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
1349 #define TARGET_GIMPLIFY_VA_ARG_EXPR rs6000_gimplify_va_arg
1351 #undef TARGET_EH_RETURN_FILTER_MODE
1352 #define TARGET_EH_RETURN_FILTER_MODE rs6000_eh_return_filter_mode
1354 #undef TARGET_SCALAR_MODE_SUPPORTED_P
1355 #define TARGET_SCALAR_MODE_SUPPORTED_P rs6000_scalar_mode_supported_p
1357 #undef TARGET_VECTOR_MODE_SUPPORTED_P
1358 #define TARGET_VECTOR_MODE_SUPPORTED_P rs6000_vector_mode_supported_p
1360 #undef TARGET_INVALID_ARG_FOR_UNPROTOTYPED_FN
1361 #define TARGET_INVALID_ARG_FOR_UNPROTOTYPED_FN invalid_arg_for_unprototyped_fn
1363 #undef TARGET_ASM_LOOP_ALIGN_MAX_SKIP
1364 #define TARGET_ASM_LOOP_ALIGN_MAX_SKIP rs6000_loop_align_max_skip
1366 #undef TARGET_OPTION_OVERRIDE
1367 #define TARGET_OPTION_OVERRIDE rs6000_option_override
1369 #undef TARGET_VECTORIZE_BUILTIN_VECTORIZED_FUNCTION
1370 #define TARGET_VECTORIZE_BUILTIN_VECTORIZED_FUNCTION \
1371 rs6000_builtin_vectorized_function
1373 #if !TARGET_MACHO
1374 #undef TARGET_STACK_PROTECT_FAIL
1375 #define TARGET_STACK_PROTECT_FAIL rs6000_stack_protect_fail
1376 #endif
1378 /* MPC604EUM 3.5.2 Weak Consistency between Multiple Processors
1379 The PowerPC architecture requires only weak consistency among
1380 processors--that is, memory accesses between processors need not be
1381 sequentially consistent and memory accesses among processors can occur
1382 in any order. The ability to order memory accesses weakly provides
1383 opportunities for more efficient use of the system bus. Unless a
1384 dependency exists, the 604e allows read operations to precede store
1385 operations. */
1386 #undef TARGET_RELAXED_ORDERING
1387 #define TARGET_RELAXED_ORDERING true
1389 #ifdef HAVE_AS_TLS
1390 #undef TARGET_ASM_OUTPUT_DWARF_DTPREL
1391 #define TARGET_ASM_OUTPUT_DWARF_DTPREL rs6000_output_dwarf_dtprel
1392 #endif
1394 /* Use a 32-bit anchor range. This leads to sequences like:
1396 addis tmp,anchor,high
1397 add dest,tmp,low
1399 where tmp itself acts as an anchor, and can be shared between
1400 accesses to the same 64k page. */
1401 #undef TARGET_MIN_ANCHOR_OFFSET
1402 #define TARGET_MIN_ANCHOR_OFFSET -0x7fffffff - 1
1403 #undef TARGET_MAX_ANCHOR_OFFSET
1404 #define TARGET_MAX_ANCHOR_OFFSET 0x7fffffff
1405 #undef TARGET_USE_BLOCKS_FOR_CONSTANT_P
1406 #define TARGET_USE_BLOCKS_FOR_CONSTANT_P rs6000_use_blocks_for_constant_p
1407 #undef TARGET_USE_BLOCKS_FOR_DECL_P
1408 #define TARGET_USE_BLOCKS_FOR_DECL_P rs6000_use_blocks_for_decl_p
1410 #undef TARGET_BUILTIN_RECIPROCAL
1411 #define TARGET_BUILTIN_RECIPROCAL rs6000_builtin_reciprocal
1413 #undef TARGET_EXPAND_TO_RTL_HOOK
1414 #define TARGET_EXPAND_TO_RTL_HOOK rs6000_alloc_sdmode_stack_slot
1416 #undef TARGET_INSTANTIATE_DECLS
1417 #define TARGET_INSTANTIATE_DECLS rs6000_instantiate_decls
1419 #undef TARGET_SECONDARY_RELOAD
1420 #define TARGET_SECONDARY_RELOAD rs6000_secondary_reload
1422 #undef TARGET_LEGITIMATE_ADDRESS_P
1423 #define TARGET_LEGITIMATE_ADDRESS_P rs6000_legitimate_address_p
1425 #undef TARGET_MODE_DEPENDENT_ADDRESS_P
1426 #define TARGET_MODE_DEPENDENT_ADDRESS_P rs6000_mode_dependent_address_p
1428 #undef TARGET_CAN_ELIMINATE
1429 #define TARGET_CAN_ELIMINATE rs6000_can_eliminate
1431 #undef TARGET_CONDITIONAL_REGISTER_USAGE
1432 #define TARGET_CONDITIONAL_REGISTER_USAGE rs6000_conditional_register_usage
1434 #undef TARGET_TRAMPOLINE_INIT
1435 #define TARGET_TRAMPOLINE_INIT rs6000_trampoline_init
1437 #undef TARGET_FUNCTION_VALUE
1438 #define TARGET_FUNCTION_VALUE rs6000_function_value
1440 #undef TARGET_OPTION_VALID_ATTRIBUTE_P
1441 #define TARGET_OPTION_VALID_ATTRIBUTE_P rs6000_valid_attribute_p
1443 #undef TARGET_OPTION_SAVE
1444 #define TARGET_OPTION_SAVE rs6000_function_specific_save
1446 #undef TARGET_OPTION_RESTORE
1447 #define TARGET_OPTION_RESTORE rs6000_function_specific_restore
1449 #undef TARGET_OPTION_PRINT
1450 #define TARGET_OPTION_PRINT rs6000_function_specific_print
1452 #undef TARGET_CAN_INLINE_P
1453 #define TARGET_CAN_INLINE_P rs6000_can_inline_p
1455 #undef TARGET_SET_CURRENT_FUNCTION
1456 #define TARGET_SET_CURRENT_FUNCTION rs6000_set_current_function
1458 #undef TARGET_LEGITIMATE_CONSTANT_P
1459 #define TARGET_LEGITIMATE_CONSTANT_P rs6000_legitimate_constant_p
1461 #undef TARGET_VECTORIZE_VEC_PERM_CONST_OK
1462 #define TARGET_VECTORIZE_VEC_PERM_CONST_OK rs6000_vectorize_vec_perm_const_ok
1465 /* Processor table. */
1466 struct rs6000_ptt
1468 const char *const name; /* Canonical processor name. */
1469 const enum processor_type processor; /* Processor type enum value. */
1470 const HOST_WIDE_INT target_enable; /* Target flags to enable. */
1473 static struct rs6000_ptt const processor_target_table[] =
1475 #define RS6000_CPU(NAME, CPU, FLAGS) { NAME, CPU, FLAGS },
1476 #include "rs6000-cpus.def"
1477 #undef RS6000_CPU
1480 /* Look up a processor name for -mcpu=xxx and -mtune=xxx. Return -1 if the
1481 name is invalid. */
1483 static int
1484 rs6000_cpu_name_lookup (const char *name)
1486 size_t i;
1488 if (name != NULL)
1490 for (i = 0; i < ARRAY_SIZE (processor_target_table); i++)
1491 if (! strcmp (name, processor_target_table[i].name))
1492 return (int)i;
1495 return -1;
1499 /* Return number of consecutive hard regs needed starting at reg REGNO
1500 to hold something of mode MODE.
1501 This is ordinarily the length in words of a value of mode MODE
1502 but can be less for certain modes in special long registers.
1504 For the SPE, GPRs are 64 bits but only 32 bits are visible in
1505 scalar instructions. The upper 32 bits are only available to the
1506 SIMD instructions.
1508 POWER and PowerPC GPRs hold 32 bits worth;
1509 PowerPC64 GPRs and FPRs point register holds 64 bits worth. */
1511 static int
1512 rs6000_hard_regno_nregs_internal (int regno, enum machine_mode mode)
1514 unsigned HOST_WIDE_INT reg_size;
1516 /* TF/TD modes are special in that they always take 2 registers. */
1517 if (FP_REGNO_P (regno))
1518 reg_size = ((VECTOR_MEM_VSX_P (mode) && mode != TDmode && mode != TFmode)
1519 ? UNITS_PER_VSX_WORD
1520 : UNITS_PER_FP_WORD);
1522 else if (SPE_SIMD_REGNO_P (regno) && TARGET_SPE && SPE_VECTOR_MODE (mode))
1523 reg_size = UNITS_PER_SPE_WORD;
1525 else if (ALTIVEC_REGNO_P (regno))
1526 reg_size = UNITS_PER_ALTIVEC_WORD;
1528 /* The value returned for SCmode in the E500 double case is 2 for
1529 ABI compatibility; storing an SCmode value in a single register
1530 would require function_arg and rs6000_spe_function_arg to handle
1531 SCmode so as to pass the value correctly in a pair of
1532 registers. */
1533 else if (TARGET_E500_DOUBLE && FLOAT_MODE_P (mode) && mode != SCmode
1534 && !DECIMAL_FLOAT_MODE_P (mode))
1535 reg_size = UNITS_PER_FP_WORD;
1537 else
1538 reg_size = UNITS_PER_WORD;
1540 return (GET_MODE_SIZE (mode) + reg_size - 1) / reg_size;
1543 /* Value is 1 if hard register REGNO can hold a value of machine-mode
1544 MODE. */
1545 static int
1546 rs6000_hard_regno_mode_ok (int regno, enum machine_mode mode)
1548 int last_regno = regno + rs6000_hard_regno_nregs[mode][regno] - 1;
1550 /* VSX registers that overlap the FPR registers are larger than for non-VSX
1551 implementations. Don't allow an item to be split between a FP register
1552 and an Altivec register. */
1553 if (VECTOR_MEM_VSX_P (mode))
1555 if (FP_REGNO_P (regno))
1556 return FP_REGNO_P (last_regno);
1558 if (ALTIVEC_REGNO_P (regno))
1559 return ALTIVEC_REGNO_P (last_regno);
1562 /* Allow TImode in all VSX registers if the user asked for it. Note, PTImode
1563 can only go in GPRs. */
1564 if (mode == TImode && TARGET_VSX_TIMODE && VSX_REGNO_P (regno))
1565 return 1;
1567 /* The GPRs can hold any mode, but values bigger than one register
1568 cannot go past R31. */
1569 if (INT_REGNO_P (regno))
1570 return INT_REGNO_P (last_regno);
1572 /* The float registers (except for VSX vector modes) can only hold floating
1573 modes and DImode. */
1574 if (FP_REGNO_P (regno))
1576 if (SCALAR_FLOAT_MODE_P (mode)
1577 && (mode != TDmode || (regno % 2) == 0)
1578 && FP_REGNO_P (last_regno))
1579 return 1;
1581 if (GET_MODE_CLASS (mode) == MODE_INT
1582 && GET_MODE_SIZE (mode) == UNITS_PER_FP_WORD)
1583 return 1;
1585 if (PAIRED_SIMD_REGNO_P (regno) && TARGET_PAIRED_FLOAT
1586 && PAIRED_VECTOR_MODE (mode))
1587 return 1;
1589 return 0;
1592 /* The CR register can only hold CC modes. */
1593 if (CR_REGNO_P (regno))
1594 return GET_MODE_CLASS (mode) == MODE_CC;
1596 if (CA_REGNO_P (regno))
1597 return mode == BImode;
1599 /* AltiVec only in AldyVec registers. */
1600 if (ALTIVEC_REGNO_P (regno))
1601 return VECTOR_MEM_ALTIVEC_OR_VSX_P (mode);
1603 /* ...but GPRs can hold SIMD data on the SPE in one register. */
1604 if (SPE_SIMD_REGNO_P (regno) && TARGET_SPE && SPE_VECTOR_MODE (mode))
1605 return 1;
1607 /* We cannot put non-VSX TImode or PTImode anywhere except general register
1608 and it must be able to fit within the register set. */
1610 return GET_MODE_SIZE (mode) <= UNITS_PER_WORD;
1613 /* Print interesting facts about registers. */
1614 static void
1615 rs6000_debug_reg_print (int first_regno, int last_regno, const char *reg_name)
1617 int r, m;
1619 for (r = first_regno; r <= last_regno; ++r)
1621 const char *comma = "";
1622 int len;
1624 if (first_regno == last_regno)
1625 fprintf (stderr, "%s:\t", reg_name);
1626 else
1627 fprintf (stderr, "%s%d:\t", reg_name, r - first_regno);
1629 len = 8;
1630 for (m = 0; m < NUM_MACHINE_MODES; ++m)
1631 if (rs6000_hard_regno_mode_ok_p[m][r] && rs6000_hard_regno_nregs[m][r])
1633 if (len > 70)
1635 fprintf (stderr, ",\n\t");
1636 len = 8;
1637 comma = "";
1640 if (rs6000_hard_regno_nregs[m][r] > 1)
1641 len += fprintf (stderr, "%s%s/%d", comma, GET_MODE_NAME (m),
1642 rs6000_hard_regno_nregs[m][r]);
1643 else
1644 len += fprintf (stderr, "%s%s", comma, GET_MODE_NAME (m));
1646 comma = ", ";
1649 if (call_used_regs[r])
1651 if (len > 70)
1653 fprintf (stderr, ",\n\t");
1654 len = 8;
1655 comma = "";
1658 len += fprintf (stderr, "%s%s", comma, "call-used");
1659 comma = ", ";
1662 if (fixed_regs[r])
1664 if (len > 70)
1666 fprintf (stderr, ",\n\t");
1667 len = 8;
1668 comma = "";
1671 len += fprintf (stderr, "%s%s", comma, "fixed");
1672 comma = ", ";
1675 if (len > 70)
1677 fprintf (stderr, ",\n\t");
1678 comma = "";
1681 fprintf (stderr, "%sregno = %d\n", comma, r);
1685 #define DEBUG_FMT_ID "%-32s= "
1686 #define DEBUG_FMT_D DEBUG_FMT_ID "%d\n"
1687 #define DEBUG_FMT_WX DEBUG_FMT_ID "%#.12" HOST_WIDE_INT_PRINT "x: "
1688 #define DEBUG_FMT_S DEBUG_FMT_ID "%s\n"
1690 /* Print various interesting information with -mdebug=reg. */
1691 static void
1692 rs6000_debug_reg_global (void)
1694 static const char *const tf[2] = { "false", "true" };
1695 const char *nl = (const char *)0;
1696 int m;
1697 size_t m1, m2, v;
1698 char costly_num[20];
1699 char nop_num[20];
1700 char flags_buffer[40];
1701 const char *costly_str;
1702 const char *nop_str;
1703 const char *trace_str;
1704 const char *abi_str;
1705 const char *cmodel_str;
1706 struct cl_target_option cl_opts;
1708 /* Map enum rs6000_vector to string. */
1709 static const char *rs6000_debug_vector_unit[] = {
1710 "none",
1711 "altivec",
1712 "vsx",
1713 "paired",
1714 "spe",
1715 "other"
1718 /* Modes we want tieable information on. */
1719 static const enum machine_mode print_tieable_modes[] = {
1720 QImode,
1721 HImode,
1722 SImode,
1723 DImode,
1724 TImode,
1725 PTImode,
1726 SFmode,
1727 DFmode,
1728 TFmode,
1729 SDmode,
1730 DDmode,
1731 TDmode,
1732 V8QImode,
1733 V4HImode,
1734 V2SImode,
1735 V16QImode,
1736 V8HImode,
1737 V4SImode,
1738 V2DImode,
1739 V32QImode,
1740 V16HImode,
1741 V8SImode,
1742 V4DImode,
1743 V2SFmode,
1744 V4SFmode,
1745 V2DFmode,
1746 V8SFmode,
1747 V4DFmode,
1748 CCmode,
1749 CCUNSmode,
1750 CCEQmode,
1753 /* Virtual regs we are interested in. */
1754 const static struct {
1755 int regno; /* register number. */
1756 const char *name; /* register name. */
1757 } virtual_regs[] = {
1758 { STACK_POINTER_REGNUM, "stack pointer:" },
1759 { TOC_REGNUM, "toc: " },
1760 { STATIC_CHAIN_REGNUM, "static chain: " },
1761 { RS6000_PIC_OFFSET_TABLE_REGNUM, "pic offset: " },
1762 { HARD_FRAME_POINTER_REGNUM, "hard frame: " },
1763 { ARG_POINTER_REGNUM, "arg pointer: " },
1764 { FRAME_POINTER_REGNUM, "frame pointer:" },
1765 { FIRST_PSEUDO_REGISTER, "first pseudo: " },
1766 { FIRST_VIRTUAL_REGISTER, "first virtual:" },
1767 { VIRTUAL_INCOMING_ARGS_REGNUM, "incoming_args:" },
1768 { VIRTUAL_STACK_VARS_REGNUM, "stack_vars: " },
1769 { VIRTUAL_STACK_DYNAMIC_REGNUM, "stack_dynamic:" },
1770 { VIRTUAL_OUTGOING_ARGS_REGNUM, "outgoing_args:" },
1771 { VIRTUAL_CFA_REGNUM, "cfa (frame): " },
1772 { VIRTUAL_PREFERRED_STACK_BOUNDARY_REGNUM, "stack boundry:" },
1773 { LAST_VIRTUAL_REGISTER, "last virtual: " },
1776 fputs ("\nHard register information:\n", stderr);
1777 rs6000_debug_reg_print (FIRST_GPR_REGNO, LAST_GPR_REGNO, "gr");
1778 rs6000_debug_reg_print (FIRST_FPR_REGNO, LAST_FPR_REGNO, "fp");
1779 rs6000_debug_reg_print (FIRST_ALTIVEC_REGNO,
1780 LAST_ALTIVEC_REGNO,
1781 "vs");
1782 rs6000_debug_reg_print (LR_REGNO, LR_REGNO, "lr");
1783 rs6000_debug_reg_print (CTR_REGNO, CTR_REGNO, "ctr");
1784 rs6000_debug_reg_print (CR0_REGNO, CR7_REGNO, "cr");
1785 rs6000_debug_reg_print (CA_REGNO, CA_REGNO, "ca");
1786 rs6000_debug_reg_print (VRSAVE_REGNO, VRSAVE_REGNO, "vrsave");
1787 rs6000_debug_reg_print (VSCR_REGNO, VSCR_REGNO, "vscr");
1788 rs6000_debug_reg_print (SPE_ACC_REGNO, SPE_ACC_REGNO, "spe_a");
1789 rs6000_debug_reg_print (SPEFSCR_REGNO, SPEFSCR_REGNO, "spe_f");
1791 fputs ("\nVirtual/stack/frame registers:\n", stderr);
1792 for (v = 0; v < ARRAY_SIZE (virtual_regs); v++)
1793 fprintf (stderr, "%s regno = %3d\n", virtual_regs[v].name, virtual_regs[v].regno);
1795 fprintf (stderr,
1796 "\n"
1797 "d reg_class = %s\n"
1798 "f reg_class = %s\n"
1799 "v reg_class = %s\n"
1800 "wa reg_class = %s\n"
1801 "wd reg_class = %s\n"
1802 "wf reg_class = %s\n"
1803 "wg reg_class = %s\n"
1804 "wl reg_class = %s\n"
1805 "ws reg_class = %s\n"
1806 "wt reg_class = %s\n"
1807 "wx reg_class = %s\n"
1808 "wz reg_class = %s\n"
1809 "\n",
1810 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_d]],
1811 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_f]],
1812 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_v]],
1813 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wa]],
1814 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wd]],
1815 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wf]],
1816 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wg]],
1817 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wl]],
1818 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_ws]],
1819 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wt]],
1820 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wx]],
1821 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wz]]);
1823 for (m = 0; m < NUM_MACHINE_MODES; ++m)
1824 if (rs6000_vector_unit[m] || rs6000_vector_mem[m]
1825 || (rs6000_vector_reload[m][0] != CODE_FOR_nothing)
1826 || (rs6000_vector_reload[m][1] != CODE_FOR_nothing))
1828 nl = "\n";
1829 fprintf (stderr,
1830 "Vector mode: %-5s arithmetic: %-10s move: %-10s "
1831 "reload-out: %c reload-in: %c\n",
1832 GET_MODE_NAME (m),
1833 rs6000_debug_vector_unit[ rs6000_vector_unit[m] ],
1834 rs6000_debug_vector_unit[ rs6000_vector_mem[m] ],
1835 (rs6000_vector_reload[m][0] != CODE_FOR_nothing) ? 'y' : 'n',
1836 (rs6000_vector_reload[m][1] != CODE_FOR_nothing) ? 'y' : 'n');
1839 if (nl)
1840 fputs (nl, stderr);
1842 for (m1 = 0; m1 < ARRAY_SIZE (print_tieable_modes); m1++)
1844 enum machine_mode mode1 = print_tieable_modes[m1];
1845 bool first_time = true;
1847 nl = (const char *)0;
1848 for (m2 = 0; m2 < ARRAY_SIZE (print_tieable_modes); m2++)
1850 enum machine_mode mode2 = print_tieable_modes[m2];
1851 if (mode1 != mode2 && MODES_TIEABLE_P (mode1, mode2))
1853 if (first_time)
1855 fprintf (stderr, "Tieable modes %s:", GET_MODE_NAME (mode1));
1856 nl = "\n";
1857 first_time = false;
1860 fprintf (stderr, " %s", GET_MODE_NAME (mode2));
1864 if (!first_time)
1865 fputs ("\n", stderr);
1868 if (nl)
1869 fputs (nl, stderr);
1871 if (rs6000_recip_control)
1873 fprintf (stderr, "\nReciprocal mask = 0x%x\n", rs6000_recip_control);
1875 for (m = 0; m < NUM_MACHINE_MODES; ++m)
1876 if (rs6000_recip_bits[m])
1878 fprintf (stderr,
1879 "Reciprocal estimate mode: %-5s divide: %s rsqrt: %s\n",
1880 GET_MODE_NAME (m),
1881 (RS6000_RECIP_AUTO_RE_P (m)
1882 ? "auto"
1883 : (RS6000_RECIP_HAVE_RE_P (m) ? "have" : "none")),
1884 (RS6000_RECIP_AUTO_RSQRTE_P (m)
1885 ? "auto"
1886 : (RS6000_RECIP_HAVE_RSQRTE_P (m) ? "have" : "none")));
1889 fputs ("\n", stderr);
1892 if (rs6000_cpu_index >= 0)
1894 const char *name = processor_target_table[rs6000_cpu_index].name;
1895 HOST_WIDE_INT flags
1896 = processor_target_table[rs6000_cpu_index].target_enable;
1898 sprintf (flags_buffer, "-mcpu=%s flags", name);
1899 rs6000_print_isa_options (stderr, 0, flags_buffer, flags);
1901 else
1902 fprintf (stderr, DEBUG_FMT_S, "cpu", "<none>");
1904 if (rs6000_tune_index >= 0)
1906 const char *name = processor_target_table[rs6000_tune_index].name;
1907 HOST_WIDE_INT flags
1908 = processor_target_table[rs6000_tune_index].target_enable;
1910 sprintf (flags_buffer, "-mtune=%s flags", name);
1911 rs6000_print_isa_options (stderr, 0, flags_buffer, flags);
1913 else
1914 fprintf (stderr, DEBUG_FMT_S, "tune", "<none>");
1916 cl_target_option_save (&cl_opts, &global_options);
1917 rs6000_print_isa_options (stderr, 0, "rs6000_isa_flags",
1918 rs6000_isa_flags);
1920 rs6000_print_isa_options (stderr, 0, "rs6000_isa_flags_explicit",
1921 rs6000_isa_flags_explicit);
1923 rs6000_print_builtin_options (stderr, 0, "rs6000_builtin_mask",
1924 rs6000_builtin_mask);
1926 rs6000_print_isa_options (stderr, 0, "TARGET_DEFAULT", TARGET_DEFAULT);
1928 fprintf (stderr, DEBUG_FMT_S, "--with-cpu default",
1929 OPTION_TARGET_CPU_DEFAULT ? OPTION_TARGET_CPU_DEFAULT : "<none>");
1931 switch (rs6000_sched_costly_dep)
1933 case max_dep_latency:
1934 costly_str = "max_dep_latency";
1935 break;
1937 case no_dep_costly:
1938 costly_str = "no_dep_costly";
1939 break;
1941 case all_deps_costly:
1942 costly_str = "all_deps_costly";
1943 break;
1945 case true_store_to_load_dep_costly:
1946 costly_str = "true_store_to_load_dep_costly";
1947 break;
1949 case store_to_load_dep_costly:
1950 costly_str = "store_to_load_dep_costly";
1951 break;
1953 default:
1954 costly_str = costly_num;
1955 sprintf (costly_num, "%d", (int)rs6000_sched_costly_dep);
1956 break;
1959 fprintf (stderr, DEBUG_FMT_S, "sched_costly_dep", costly_str);
1961 switch (rs6000_sched_insert_nops)
1963 case sched_finish_regroup_exact:
1964 nop_str = "sched_finish_regroup_exact";
1965 break;
1967 case sched_finish_pad_groups:
1968 nop_str = "sched_finish_pad_groups";
1969 break;
1971 case sched_finish_none:
1972 nop_str = "sched_finish_none";
1973 break;
1975 default:
1976 nop_str = nop_num;
1977 sprintf (nop_num, "%d", (int)rs6000_sched_insert_nops);
1978 break;
1981 fprintf (stderr, DEBUG_FMT_S, "sched_insert_nops", nop_str);
1983 switch (rs6000_sdata)
1985 default:
1986 case SDATA_NONE:
1987 break;
1989 case SDATA_DATA:
1990 fprintf (stderr, DEBUG_FMT_S, "sdata", "data");
1991 break;
1993 case SDATA_SYSV:
1994 fprintf (stderr, DEBUG_FMT_S, "sdata", "sysv");
1995 break;
1997 case SDATA_EABI:
1998 fprintf (stderr, DEBUG_FMT_S, "sdata", "eabi");
1999 break;
2003 switch (rs6000_traceback)
2005 case traceback_default: trace_str = "default"; break;
2006 case traceback_none: trace_str = "none"; break;
2007 case traceback_part: trace_str = "part"; break;
2008 case traceback_full: trace_str = "full"; break;
2009 default: trace_str = "unknown"; break;
2012 fprintf (stderr, DEBUG_FMT_S, "traceback", trace_str);
2014 switch (rs6000_current_cmodel)
2016 case CMODEL_SMALL: cmodel_str = "small"; break;
2017 case CMODEL_MEDIUM: cmodel_str = "medium"; break;
2018 case CMODEL_LARGE: cmodel_str = "large"; break;
2019 default: cmodel_str = "unknown"; break;
2022 fprintf (stderr, DEBUG_FMT_S, "cmodel", cmodel_str);
2024 switch (rs6000_current_abi)
2026 case ABI_NONE: abi_str = "none"; break;
2027 case ABI_AIX: abi_str = "aix"; break;
2028 case ABI_V4: abi_str = "V4"; break;
2029 case ABI_DARWIN: abi_str = "darwin"; break;
2030 default: abi_str = "unknown"; break;
2033 fprintf (stderr, DEBUG_FMT_S, "abi", abi_str);
2035 if (rs6000_altivec_abi)
2036 fprintf (stderr, DEBUG_FMT_S, "altivec_abi", "true");
2038 if (rs6000_spe_abi)
2039 fprintf (stderr, DEBUG_FMT_S, "spe_abi", "true");
2041 if (rs6000_darwin64_abi)
2042 fprintf (stderr, DEBUG_FMT_S, "darwin64_abi", "true");
2044 if (rs6000_float_gprs)
2045 fprintf (stderr, DEBUG_FMT_S, "float_gprs", "true");
2047 if (TARGET_LINK_STACK)
2048 fprintf (stderr, DEBUG_FMT_S, "link_stack", "true");
2050 if (targetm.lra_p ())
2051 fprintf (stderr, DEBUG_FMT_S, "lra", "true");
2053 fprintf (stderr, DEBUG_FMT_S, "plt-format",
2054 TARGET_SECURE_PLT ? "secure" : "bss");
2055 fprintf (stderr, DEBUG_FMT_S, "struct-return",
2056 aix_struct_return ? "aix" : "sysv");
2057 fprintf (stderr, DEBUG_FMT_S, "always_hint", tf[!!rs6000_always_hint]);
2058 fprintf (stderr, DEBUG_FMT_S, "sched_groups", tf[!!rs6000_sched_groups]);
2059 fprintf (stderr, DEBUG_FMT_S, "align_branch",
2060 tf[!!rs6000_align_branch_targets]);
2061 fprintf (stderr, DEBUG_FMT_D, "tls_size", rs6000_tls_size);
2062 fprintf (stderr, DEBUG_FMT_D, "long_double_size",
2063 rs6000_long_double_type_size);
2064 fprintf (stderr, DEBUG_FMT_D, "sched_restricted_insns_priority",
2065 (int)rs6000_sched_restricted_insns_priority);
2066 fprintf (stderr, DEBUG_FMT_D, "Number of standard builtins",
2067 (int)END_BUILTINS);
2068 fprintf (stderr, DEBUG_FMT_D, "Number of rs6000 builtins",
2069 (int)RS6000_BUILTIN_COUNT);
2072 /* Initialize the various global tables that are based on register size. */
2073 static void
2074 rs6000_init_hard_regno_mode_ok (bool global_init_p)
2076 int r, m, c;
2077 int align64;
2078 int align32;
2080 /* Precalculate REGNO_REG_CLASS. */
2081 rs6000_regno_regclass[0] = GENERAL_REGS;
2082 for (r = 1; r < 32; ++r)
2083 rs6000_regno_regclass[r] = BASE_REGS;
2085 for (r = 32; r < 64; ++r)
2086 rs6000_regno_regclass[r] = FLOAT_REGS;
2088 for (r = 64; r < FIRST_PSEUDO_REGISTER; ++r)
2089 rs6000_regno_regclass[r] = NO_REGS;
2091 for (r = FIRST_ALTIVEC_REGNO; r <= LAST_ALTIVEC_REGNO; ++r)
2092 rs6000_regno_regclass[r] = ALTIVEC_REGS;
2094 rs6000_regno_regclass[CR0_REGNO] = CR0_REGS;
2095 for (r = CR1_REGNO; r <= CR7_REGNO; ++r)
2096 rs6000_regno_regclass[r] = CR_REGS;
2098 rs6000_regno_regclass[LR_REGNO] = LINK_REGS;
2099 rs6000_regno_regclass[CTR_REGNO] = CTR_REGS;
2100 rs6000_regno_regclass[CA_REGNO] = CA_REGS;
2101 rs6000_regno_regclass[VRSAVE_REGNO] = VRSAVE_REGS;
2102 rs6000_regno_regclass[VSCR_REGNO] = VRSAVE_REGS;
2103 rs6000_regno_regclass[SPE_ACC_REGNO] = SPE_ACC_REGS;
2104 rs6000_regno_regclass[SPEFSCR_REGNO] = SPEFSCR_REGS;
2105 rs6000_regno_regclass[ARG_POINTER_REGNUM] = BASE_REGS;
2106 rs6000_regno_regclass[FRAME_POINTER_REGNUM] = BASE_REGS;
2108 /* Precalculate vector information, this must be set up before the
2109 rs6000_hard_regno_nregs_internal below. */
2110 for (m = 0; m < NUM_MACHINE_MODES; ++m)
2112 rs6000_vector_unit[m] = rs6000_vector_mem[m] = VECTOR_NONE;
2113 rs6000_vector_reload[m][0] = CODE_FOR_nothing;
2114 rs6000_vector_reload[m][1] = CODE_FOR_nothing;
2117 for (c = 0; c < (int)(int)RS6000_CONSTRAINT_MAX; c++)
2118 rs6000_constraints[c] = NO_REGS;
2120 /* The VSX hardware allows native alignment for vectors, but control whether the compiler
2121 believes it can use native alignment or still uses 128-bit alignment. */
2122 if (TARGET_VSX && !TARGET_VSX_ALIGN_128)
2124 align64 = 64;
2125 align32 = 32;
2127 else
2129 align64 = 128;
2130 align32 = 128;
2133 /* V2DF mode, VSX only. */
2134 if (TARGET_VSX)
2136 rs6000_vector_unit[V2DFmode] = VECTOR_VSX;
2137 rs6000_vector_mem[V2DFmode] = VECTOR_VSX;
2138 rs6000_vector_align[V2DFmode] = align64;
2141 /* V4SF mode, either VSX or Altivec. */
2142 if (TARGET_VSX)
2144 rs6000_vector_unit[V4SFmode] = VECTOR_VSX;
2145 rs6000_vector_mem[V4SFmode] = VECTOR_VSX;
2146 rs6000_vector_align[V4SFmode] = align32;
2148 else if (TARGET_ALTIVEC)
2150 rs6000_vector_unit[V4SFmode] = VECTOR_ALTIVEC;
2151 rs6000_vector_mem[V4SFmode] = VECTOR_ALTIVEC;
2152 rs6000_vector_align[V4SFmode] = align32;
2155 /* V16QImode, V8HImode, V4SImode are Altivec only, but possibly do VSX loads
2156 and stores. */
2157 if (TARGET_ALTIVEC)
2159 rs6000_vector_unit[V4SImode] = VECTOR_ALTIVEC;
2160 rs6000_vector_unit[V8HImode] = VECTOR_ALTIVEC;
2161 rs6000_vector_unit[V16QImode] = VECTOR_ALTIVEC;
2162 rs6000_vector_align[V4SImode] = align32;
2163 rs6000_vector_align[V8HImode] = align32;
2164 rs6000_vector_align[V16QImode] = align32;
2166 if (TARGET_VSX)
2168 rs6000_vector_mem[V4SImode] = VECTOR_VSX;
2169 rs6000_vector_mem[V8HImode] = VECTOR_VSX;
2170 rs6000_vector_mem[V16QImode] = VECTOR_VSX;
2172 else
2174 rs6000_vector_mem[V4SImode] = VECTOR_ALTIVEC;
2175 rs6000_vector_mem[V8HImode] = VECTOR_ALTIVEC;
2176 rs6000_vector_mem[V16QImode] = VECTOR_ALTIVEC;
2180 /* V2DImode, only allow under VSX, which can do V2DI insert/splat/extract.
2181 Altivec doesn't have 64-bit support. */
2182 if (TARGET_VSX)
2184 rs6000_vector_mem[V2DImode] = VECTOR_VSX;
2185 rs6000_vector_unit[V2DImode] = VECTOR_NONE;
2186 rs6000_vector_align[V2DImode] = align64;
2189 /* DFmode, see if we want to use the VSX unit. */
2190 if (TARGET_VSX && TARGET_VSX_SCALAR_DOUBLE)
2192 rs6000_vector_unit[DFmode] = VECTOR_VSX;
2193 rs6000_vector_mem[DFmode]
2194 = (TARGET_VSX_SCALAR_MEMORY ? VECTOR_VSX : VECTOR_NONE);
2195 rs6000_vector_align[DFmode] = align64;
2198 /* Allow TImode in VSX register and set the VSX memory macros. */
2199 if (TARGET_VSX && TARGET_VSX_TIMODE)
2201 rs6000_vector_mem[TImode] = VECTOR_VSX;
2202 rs6000_vector_align[TImode] = align64;
2205 /* TODO add SPE and paired floating point vector support. */
2207 /* Register class constraints for the constraints that depend on compile
2208 switches. */
2209 if (TARGET_HARD_FLOAT && TARGET_FPRS)
2210 rs6000_constraints[RS6000_CONSTRAINT_f] = FLOAT_REGS;
2212 if (TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT)
2213 rs6000_constraints[RS6000_CONSTRAINT_d] = FLOAT_REGS;
2215 if (TARGET_VSX)
2217 /* At present, we just use VSX_REGS, but we have different constraints
2218 based on the use, in case we want to fine tune the default register
2219 class used. wa = any VSX register, wf = register class to use for
2220 V4SF, wd = register class to use for V2DF, and ws = register classs to
2221 use for DF scalars. */
2222 rs6000_constraints[RS6000_CONSTRAINT_wa] = VSX_REGS;
2223 rs6000_constraints[RS6000_CONSTRAINT_wf] = VSX_REGS;
2224 rs6000_constraints[RS6000_CONSTRAINT_wd] = VSX_REGS;
2225 rs6000_constraints[RS6000_CONSTRAINT_ws] = (TARGET_VSX_SCALAR_MEMORY
2226 ? VSX_REGS
2227 : FLOAT_REGS);
2228 if (TARGET_VSX_TIMODE)
2229 rs6000_constraints[RS6000_CONSTRAINT_wt] = VSX_REGS;
2232 /* Add conditional constraints based on various options, to allow us to
2233 collapse multiple insn patterns. */
2234 if (TARGET_ALTIVEC)
2235 rs6000_constraints[RS6000_CONSTRAINT_v] = ALTIVEC_REGS;
2237 if (TARGET_MFPGPR)
2238 rs6000_constraints[RS6000_CONSTRAINT_wg] = FLOAT_REGS;
2240 if (TARGET_LFIWAX)
2241 rs6000_constraints[RS6000_CONSTRAINT_wl] = FLOAT_REGS;
2243 if (TARGET_STFIWX)
2244 rs6000_constraints[RS6000_CONSTRAINT_wx] = FLOAT_REGS;
2246 if (TARGET_LFIWZX)
2247 rs6000_constraints[RS6000_CONSTRAINT_wz] = FLOAT_REGS;
2249 /* Set up the reload helper functions. */
2250 if (TARGET_VSX || TARGET_ALTIVEC)
2252 if (TARGET_64BIT)
2254 rs6000_vector_reload[V16QImode][0] = CODE_FOR_reload_v16qi_di_store;
2255 rs6000_vector_reload[V16QImode][1] = CODE_FOR_reload_v16qi_di_load;
2256 rs6000_vector_reload[V8HImode][0] = CODE_FOR_reload_v8hi_di_store;
2257 rs6000_vector_reload[V8HImode][1] = CODE_FOR_reload_v8hi_di_load;
2258 rs6000_vector_reload[V4SImode][0] = CODE_FOR_reload_v4si_di_store;
2259 rs6000_vector_reload[V4SImode][1] = CODE_FOR_reload_v4si_di_load;
2260 rs6000_vector_reload[V2DImode][0] = CODE_FOR_reload_v2di_di_store;
2261 rs6000_vector_reload[V2DImode][1] = CODE_FOR_reload_v2di_di_load;
2262 rs6000_vector_reload[V4SFmode][0] = CODE_FOR_reload_v4sf_di_store;
2263 rs6000_vector_reload[V4SFmode][1] = CODE_FOR_reload_v4sf_di_load;
2264 rs6000_vector_reload[V2DFmode][0] = CODE_FOR_reload_v2df_di_store;
2265 rs6000_vector_reload[V2DFmode][1] = CODE_FOR_reload_v2df_di_load;
2266 if (TARGET_VSX && TARGET_VSX_SCALAR_MEMORY)
2268 rs6000_vector_reload[DFmode][0] = CODE_FOR_reload_df_di_store;
2269 rs6000_vector_reload[DFmode][1] = CODE_FOR_reload_df_di_load;
2270 rs6000_vector_reload[DDmode][0] = CODE_FOR_reload_dd_di_store;
2271 rs6000_vector_reload[DDmode][1] = CODE_FOR_reload_dd_di_load;
2273 if (TARGET_VSX_TIMODE)
2275 rs6000_vector_reload[TImode][0] = CODE_FOR_reload_ti_di_store;
2276 rs6000_vector_reload[TImode][1] = CODE_FOR_reload_ti_di_load;
2279 else
2281 rs6000_vector_reload[V16QImode][0] = CODE_FOR_reload_v16qi_si_store;
2282 rs6000_vector_reload[V16QImode][1] = CODE_FOR_reload_v16qi_si_load;
2283 rs6000_vector_reload[V8HImode][0] = CODE_FOR_reload_v8hi_si_store;
2284 rs6000_vector_reload[V8HImode][1] = CODE_FOR_reload_v8hi_si_load;
2285 rs6000_vector_reload[V4SImode][0] = CODE_FOR_reload_v4si_si_store;
2286 rs6000_vector_reload[V4SImode][1] = CODE_FOR_reload_v4si_si_load;
2287 rs6000_vector_reload[V2DImode][0] = CODE_FOR_reload_v2di_si_store;
2288 rs6000_vector_reload[V2DImode][1] = CODE_FOR_reload_v2di_si_load;
2289 rs6000_vector_reload[V4SFmode][0] = CODE_FOR_reload_v4sf_si_store;
2290 rs6000_vector_reload[V4SFmode][1] = CODE_FOR_reload_v4sf_si_load;
2291 rs6000_vector_reload[V2DFmode][0] = CODE_FOR_reload_v2df_si_store;
2292 rs6000_vector_reload[V2DFmode][1] = CODE_FOR_reload_v2df_si_load;
2293 if (TARGET_VSX && TARGET_VSX_SCALAR_MEMORY)
2295 rs6000_vector_reload[DFmode][0] = CODE_FOR_reload_df_si_store;
2296 rs6000_vector_reload[DFmode][1] = CODE_FOR_reload_df_si_load;
2297 rs6000_vector_reload[DDmode][0] = CODE_FOR_reload_dd_si_store;
2298 rs6000_vector_reload[DDmode][1] = CODE_FOR_reload_dd_si_load;
2300 if (TARGET_VSX_TIMODE)
2302 rs6000_vector_reload[TImode][0] = CODE_FOR_reload_ti_si_store;
2303 rs6000_vector_reload[TImode][1] = CODE_FOR_reload_ti_si_load;
2308 /* Precalculate HARD_REGNO_NREGS. */
2309 for (r = 0; r < FIRST_PSEUDO_REGISTER; ++r)
2310 for (m = 0; m < NUM_MACHINE_MODES; ++m)
2311 rs6000_hard_regno_nregs[m][r]
2312 = rs6000_hard_regno_nregs_internal (r, (enum machine_mode)m);
2314 /* Precalculate HARD_REGNO_MODE_OK. */
2315 for (r = 0; r < FIRST_PSEUDO_REGISTER; ++r)
2316 for (m = 0; m < NUM_MACHINE_MODES; ++m)
2317 if (rs6000_hard_regno_mode_ok (r, (enum machine_mode)m))
2318 rs6000_hard_regno_mode_ok_p[m][r] = true;
2320 /* Precalculate CLASS_MAX_NREGS sizes. */
2321 for (c = 0; c < LIM_REG_CLASSES; ++c)
2323 int reg_size;
2325 if (TARGET_VSX && VSX_REG_CLASS_P (c))
2326 reg_size = UNITS_PER_VSX_WORD;
2328 else if (c == ALTIVEC_REGS)
2329 reg_size = UNITS_PER_ALTIVEC_WORD;
2331 else if (c == FLOAT_REGS)
2332 reg_size = UNITS_PER_FP_WORD;
2334 else
2335 reg_size = UNITS_PER_WORD;
2337 for (m = 0; m < NUM_MACHINE_MODES; ++m)
2339 int reg_size2 = reg_size;
2341 /* TFmode/TDmode always takes 2 registers, even in VSX. */
2342 if (m == TDmode || m == TFmode)
2343 reg_size2 = UNITS_PER_FP_WORD;
2345 rs6000_class_max_nregs[m][c]
2346 = (GET_MODE_SIZE (m) + reg_size2 - 1) / reg_size2;
2350 if (TARGET_E500_DOUBLE)
2351 rs6000_class_max_nregs[DFmode][GENERAL_REGS] = 1;
2353 /* Calculate which modes to automatically generate code to use a the
2354 reciprocal divide and square root instructions. In the future, possibly
2355 automatically generate the instructions even if the user did not specify
2356 -mrecip. The older machines double precision reciprocal sqrt estimate is
2357 not accurate enough. */
2358 memset (rs6000_recip_bits, 0, sizeof (rs6000_recip_bits));
2359 if (TARGET_FRES)
2360 rs6000_recip_bits[SFmode] = RS6000_RECIP_MASK_HAVE_RE;
2361 if (TARGET_FRE)
2362 rs6000_recip_bits[DFmode] = RS6000_RECIP_MASK_HAVE_RE;
2363 if (VECTOR_UNIT_ALTIVEC_OR_VSX_P (V4SFmode))
2364 rs6000_recip_bits[V4SFmode] = RS6000_RECIP_MASK_HAVE_RE;
2365 if (VECTOR_UNIT_VSX_P (V2DFmode))
2366 rs6000_recip_bits[V2DFmode] = RS6000_RECIP_MASK_HAVE_RE;
2368 if (TARGET_FRSQRTES)
2369 rs6000_recip_bits[SFmode] |= RS6000_RECIP_MASK_HAVE_RSQRTE;
2370 if (TARGET_FRSQRTE)
2371 rs6000_recip_bits[DFmode] |= RS6000_RECIP_MASK_HAVE_RSQRTE;
2372 if (VECTOR_UNIT_ALTIVEC_OR_VSX_P (V4SFmode))
2373 rs6000_recip_bits[V4SFmode] |= RS6000_RECIP_MASK_HAVE_RSQRTE;
2374 if (VECTOR_UNIT_VSX_P (V2DFmode))
2375 rs6000_recip_bits[V2DFmode] |= RS6000_RECIP_MASK_HAVE_RSQRTE;
2377 if (rs6000_recip_control)
2379 if (!flag_finite_math_only)
2380 warning (0, "-mrecip requires -ffinite-math or -ffast-math");
2381 if (flag_trapping_math)
2382 warning (0, "-mrecip requires -fno-trapping-math or -ffast-math");
2383 if (!flag_reciprocal_math)
2384 warning (0, "-mrecip requires -freciprocal-math or -ffast-math");
2385 if (flag_finite_math_only && !flag_trapping_math && flag_reciprocal_math)
2387 if (RS6000_RECIP_HAVE_RE_P (SFmode)
2388 && (rs6000_recip_control & RECIP_SF_DIV) != 0)
2389 rs6000_recip_bits[SFmode] |= RS6000_RECIP_MASK_AUTO_RE;
2391 if (RS6000_RECIP_HAVE_RE_P (DFmode)
2392 && (rs6000_recip_control & RECIP_DF_DIV) != 0)
2393 rs6000_recip_bits[DFmode] |= RS6000_RECIP_MASK_AUTO_RE;
2395 if (RS6000_RECIP_HAVE_RE_P (V4SFmode)
2396 && (rs6000_recip_control & RECIP_V4SF_DIV) != 0)
2397 rs6000_recip_bits[V4SFmode] |= RS6000_RECIP_MASK_AUTO_RE;
2399 if (RS6000_RECIP_HAVE_RE_P (V2DFmode)
2400 && (rs6000_recip_control & RECIP_V2DF_DIV) != 0)
2401 rs6000_recip_bits[V2DFmode] |= RS6000_RECIP_MASK_AUTO_RE;
2403 if (RS6000_RECIP_HAVE_RSQRTE_P (SFmode)
2404 && (rs6000_recip_control & RECIP_SF_RSQRT) != 0)
2405 rs6000_recip_bits[SFmode] |= RS6000_RECIP_MASK_AUTO_RSQRTE;
2407 if (RS6000_RECIP_HAVE_RSQRTE_P (DFmode)
2408 && (rs6000_recip_control & RECIP_DF_RSQRT) != 0)
2409 rs6000_recip_bits[DFmode] |= RS6000_RECIP_MASK_AUTO_RSQRTE;
2411 if (RS6000_RECIP_HAVE_RSQRTE_P (V4SFmode)
2412 && (rs6000_recip_control & RECIP_V4SF_RSQRT) != 0)
2413 rs6000_recip_bits[V4SFmode] |= RS6000_RECIP_MASK_AUTO_RSQRTE;
2415 if (RS6000_RECIP_HAVE_RSQRTE_P (V2DFmode)
2416 && (rs6000_recip_control & RECIP_V2DF_RSQRT) != 0)
2417 rs6000_recip_bits[V2DFmode] |= RS6000_RECIP_MASK_AUTO_RSQRTE;
2421 if (global_init_p || TARGET_DEBUG_TARGET)
2423 if (TARGET_DEBUG_REG)
2424 rs6000_debug_reg_global ();
2426 if (TARGET_DEBUG_COST || TARGET_DEBUG_REG)
2427 fprintf (stderr,
2428 "SImode variable mult cost = %d\n"
2429 "SImode constant mult cost = %d\n"
2430 "SImode short constant mult cost = %d\n"
2431 "DImode multipliciation cost = %d\n"
2432 "SImode division cost = %d\n"
2433 "DImode division cost = %d\n"
2434 "Simple fp operation cost = %d\n"
2435 "DFmode multiplication cost = %d\n"
2436 "SFmode division cost = %d\n"
2437 "DFmode division cost = %d\n"
2438 "cache line size = %d\n"
2439 "l1 cache size = %d\n"
2440 "l2 cache size = %d\n"
2441 "simultaneous prefetches = %d\n"
2442 "\n",
2443 rs6000_cost->mulsi,
2444 rs6000_cost->mulsi_const,
2445 rs6000_cost->mulsi_const9,
2446 rs6000_cost->muldi,
2447 rs6000_cost->divsi,
2448 rs6000_cost->divdi,
2449 rs6000_cost->fp,
2450 rs6000_cost->dmul,
2451 rs6000_cost->sdiv,
2452 rs6000_cost->ddiv,
2453 rs6000_cost->cache_line_size,
2454 rs6000_cost->l1_cache_size,
2455 rs6000_cost->l2_cache_size,
2456 rs6000_cost->simultaneous_prefetches);
2460 #if TARGET_MACHO
2461 /* The Darwin version of SUBTARGET_OVERRIDE_OPTIONS. */
2463 static void
2464 darwin_rs6000_override_options (void)
2466 /* The Darwin ABI always includes AltiVec, can't be (validly) turned
2467 off. */
2468 rs6000_altivec_abi = 1;
2469 TARGET_ALTIVEC_VRSAVE = 1;
2470 rs6000_current_abi = ABI_DARWIN;
2472 if (DEFAULT_ABI == ABI_DARWIN
2473 && TARGET_64BIT)
2474 darwin_one_byte_bool = 1;
2476 if (TARGET_64BIT && ! TARGET_POWERPC64)
2478 rs6000_isa_flags |= OPTION_MASK_POWERPC64;
2479 warning (0, "-m64 requires PowerPC64 architecture, enabling");
2481 if (flag_mkernel)
2483 rs6000_default_long_calls = 1;
2484 rs6000_isa_flags |= OPTION_MASK_SOFT_FLOAT;
2487 /* Make -m64 imply -maltivec. Darwin's 64-bit ABI includes
2488 Altivec. */
2489 if (!flag_mkernel && !flag_apple_kext
2490 && TARGET_64BIT
2491 && ! (rs6000_isa_flags_explicit & OPTION_MASK_ALTIVEC))
2492 rs6000_isa_flags |= OPTION_MASK_ALTIVEC;
2494 /* Unless the user (not the configurer) has explicitly overridden
2495 it with -mcpu=G3 or -mno-altivec, then 10.5+ targets default to
2496 G4 unless targeting the kernel. */
2497 if (!flag_mkernel
2498 && !flag_apple_kext
2499 && strverscmp (darwin_macosx_version_min, "10.5") >= 0
2500 && ! (rs6000_isa_flags_explicit & OPTION_MASK_ALTIVEC)
2501 && ! global_options_set.x_rs6000_cpu_index)
2503 rs6000_isa_flags |= OPTION_MASK_ALTIVEC;
2506 #endif
2508 /* If not otherwise specified by a target, make 'long double' equivalent to
2509 'double'. */
2511 #ifndef RS6000_DEFAULT_LONG_DOUBLE_SIZE
2512 #define RS6000_DEFAULT_LONG_DOUBLE_SIZE 64
2513 #endif
2515 /* Return the builtin mask of the various options used that could affect which
2516 builtins were used. In the past we used target_flags, but we've run out of
2517 bits, and some options like SPE and PAIRED are no longer in
2518 target_flags. */
2520 HOST_WIDE_INT
2521 rs6000_builtin_mask_calculate (void)
2523 return (((TARGET_ALTIVEC) ? RS6000_BTM_ALTIVEC : 0)
2524 | ((TARGET_VSX) ? RS6000_BTM_VSX : 0)
2525 | ((TARGET_SPE) ? RS6000_BTM_SPE : 0)
2526 | ((TARGET_PAIRED_FLOAT) ? RS6000_BTM_PAIRED : 0)
2527 | ((TARGET_FRE) ? RS6000_BTM_FRE : 0)
2528 | ((TARGET_FRES) ? RS6000_BTM_FRES : 0)
2529 | ((TARGET_FRSQRTE) ? RS6000_BTM_FRSQRTE : 0)
2530 | ((TARGET_FRSQRTES) ? RS6000_BTM_FRSQRTES : 0)
2531 | ((TARGET_POPCNTD) ? RS6000_BTM_POPCNTD : 0)
2532 | ((rs6000_cpu == PROCESSOR_CELL) ? RS6000_BTM_CELL : 0));
2535 /* Override command line options. Mostly we process the processor type and
2536 sometimes adjust other TARGET_ options. */
2538 static bool
2539 rs6000_option_override_internal (bool global_init_p)
2541 bool ret = true;
2542 bool have_cpu = false;
2544 /* The default cpu requested at configure time, if any. */
2545 const char *implicit_cpu = OPTION_TARGET_CPU_DEFAULT;
2547 HOST_WIDE_INT set_masks;
2548 int cpu_index;
2549 int tune_index;
2550 struct cl_target_option *main_target_opt
2551 = ((global_init_p || target_option_default_node == NULL)
2552 ? NULL : TREE_TARGET_OPTION (target_option_default_node));
2554 /* On 64-bit Darwin, power alignment is ABI-incompatible with some C
2555 library functions, so warn about it. The flag may be useful for
2556 performance studies from time to time though, so don't disable it
2557 entirely. */
2558 if (global_options_set.x_rs6000_alignment_flags
2559 && rs6000_alignment_flags == MASK_ALIGN_POWER
2560 && DEFAULT_ABI == ABI_DARWIN
2561 && TARGET_64BIT)
2562 warning (0, "-malign-power is not supported for 64-bit Darwin;"
2563 " it is incompatible with the installed C and C++ libraries");
2565 /* Numerous experiment shows that IRA based loop pressure
2566 calculation works better for RTL loop invariant motion on targets
2567 with enough (>= 32) registers. It is an expensive optimization.
2568 So it is on only for peak performance. */
2569 if (optimize >= 3 && global_init_p)
2570 flag_ira_loop_pressure = 1;
2572 /* Set the pointer size. */
2573 if (TARGET_64BIT)
2575 rs6000_pmode = (int)DImode;
2576 rs6000_pointer_size = 64;
2578 else
2580 rs6000_pmode = (int)SImode;
2581 rs6000_pointer_size = 32;
2584 /* Some OSs don't support saving the high part of 64-bit registers on context
2585 switch. Other OSs don't support saving Altivec registers. On those OSs,
2586 we don't touch the OPTION_MASK_POWERPC64 or OPTION_MASK_ALTIVEC settings;
2587 if the user wants either, the user must explicitly specify them and we
2588 won't interfere with the user's specification. */
2590 set_masks = POWERPC_MASKS;
2591 #ifdef OS_MISSING_POWERPC64
2592 if (OS_MISSING_POWERPC64)
2593 set_masks &= ~OPTION_MASK_POWERPC64;
2594 #endif
2595 #ifdef OS_MISSING_ALTIVEC
2596 if (OS_MISSING_ALTIVEC)
2597 set_masks &= ~(OPTION_MASK_ALTIVEC | OPTION_MASK_VSX);
2598 #endif
2600 /* Don't override by the processor default if given explicitly. */
2601 set_masks &= ~rs6000_isa_flags_explicit;
2603 /* Process the -mcpu=<xxx> and -mtune=<xxx> argument. If the user changed
2604 the cpu in a target attribute or pragma, but did not specify a tuning
2605 option, use the cpu for the tuning option rather than the option specified
2606 with -mtune on the command line. Process a '--with-cpu' configuration
2607 request as an implicit --cpu. */
2608 if (rs6000_cpu_index >= 0)
2610 cpu_index = rs6000_cpu_index;
2611 have_cpu = true;
2613 else if (main_target_opt != NULL && main_target_opt->x_rs6000_cpu_index >= 0)
2615 rs6000_cpu_index = cpu_index = main_target_opt->x_rs6000_cpu_index;
2616 have_cpu = true;
2618 else if (implicit_cpu)
2620 rs6000_cpu_index = cpu_index = rs6000_cpu_name_lookup (implicit_cpu);
2621 have_cpu = true;
2623 else
2625 const char *default_cpu = (TARGET_POWERPC64 ? "powerpc64" : "powerpc");
2626 rs6000_cpu_index = cpu_index = rs6000_cpu_name_lookup (default_cpu);
2627 have_cpu = false;
2630 gcc_assert (cpu_index >= 0);
2632 /* If we have a cpu, either through an explicit -mcpu=<xxx> or if the
2633 compiler was configured with --with-cpu=<xxx>, replace all of the ISA bits
2634 with those from the cpu, except for options that were explicitly set. If
2635 we don't have a cpu, do not override the target bits set in
2636 TARGET_DEFAULT. */
2637 if (have_cpu)
2639 rs6000_isa_flags &= ~set_masks;
2640 rs6000_isa_flags |= (processor_target_table[cpu_index].target_enable
2641 & set_masks);
2643 else
2644 rs6000_isa_flags |= (processor_target_table[cpu_index].target_enable
2645 & ~rs6000_isa_flags_explicit);
2647 /* If no -mcpu=<xxx>, inherit any default options that were cleared via
2648 POWERPC_MASKS. Originally, TARGET_DEFAULT was used to initialize
2649 target_flags via the TARGET_DEFAULT_TARGET_FLAGS hook. When we switched
2650 to using rs6000_isa_flags, we need to do the initialization here. */
2651 if (!have_cpu)
2652 rs6000_isa_flags |= (TARGET_DEFAULT & ~rs6000_isa_flags_explicit);
2654 if (rs6000_tune_index >= 0)
2655 tune_index = rs6000_tune_index;
2656 else if (have_cpu)
2657 rs6000_tune_index = tune_index = cpu_index;
2658 else
2660 size_t i;
2661 enum processor_type tune_proc
2662 = (TARGET_POWERPC64 ? PROCESSOR_DEFAULT64 : PROCESSOR_DEFAULT);
2664 tune_index = -1;
2665 for (i = 0; i < ARRAY_SIZE (processor_target_table); i++)
2666 if (processor_target_table[i].processor == tune_proc)
2668 rs6000_tune_index = tune_index = i;
2669 break;
2673 gcc_assert (tune_index >= 0);
2674 rs6000_cpu = processor_target_table[tune_index].processor;
2676 /* Pick defaults for SPE related control flags. Do this early to make sure
2677 that the TARGET_ macros are representative ASAP. */
2679 int spe_capable_cpu =
2680 (rs6000_cpu == PROCESSOR_PPC8540
2681 || rs6000_cpu == PROCESSOR_PPC8548);
2683 if (!global_options_set.x_rs6000_spe_abi)
2684 rs6000_spe_abi = spe_capable_cpu;
2686 if (!global_options_set.x_rs6000_spe)
2687 rs6000_spe = spe_capable_cpu;
2689 if (!global_options_set.x_rs6000_float_gprs)
2690 rs6000_float_gprs =
2691 (rs6000_cpu == PROCESSOR_PPC8540 ? 1
2692 : rs6000_cpu == PROCESSOR_PPC8548 ? 2
2693 : 0);
2696 if (global_options_set.x_rs6000_spe_abi
2697 && rs6000_spe_abi
2698 && !TARGET_SPE_ABI)
2699 error ("not configured for SPE ABI");
2701 if (global_options_set.x_rs6000_spe
2702 && rs6000_spe
2703 && !TARGET_SPE)
2704 error ("not configured for SPE instruction set");
2706 if (main_target_opt != NULL
2707 && ((main_target_opt->x_rs6000_spe_abi != rs6000_spe_abi)
2708 || (main_target_opt->x_rs6000_spe != rs6000_spe)
2709 || (main_target_opt->x_rs6000_float_gprs != rs6000_float_gprs)))
2710 error ("target attribute or pragma changes SPE ABI");
2712 if (rs6000_cpu == PROCESSOR_PPCE300C2 || rs6000_cpu == PROCESSOR_PPCE300C3
2713 || rs6000_cpu == PROCESSOR_PPCE500MC || rs6000_cpu == PROCESSOR_PPCE500MC64
2714 || rs6000_cpu == PROCESSOR_PPCE5500)
2716 if (TARGET_ALTIVEC)
2717 error ("AltiVec not supported in this target");
2718 if (TARGET_SPE)
2719 error ("SPE not supported in this target");
2721 if (rs6000_cpu == PROCESSOR_PPCE6500)
2723 if (TARGET_SPE)
2724 error ("SPE not supported in this target");
2727 /* Disable Cell microcode if we are optimizing for the Cell
2728 and not optimizing for size. */
2729 if (rs6000_gen_cell_microcode == -1)
2730 rs6000_gen_cell_microcode = !(rs6000_cpu == PROCESSOR_CELL
2731 && !optimize_size);
2733 /* If we are optimizing big endian systems for space and it's OK to
2734 use instructions that would be microcoded on the Cell, use the
2735 load/store multiple and string instructions. */
2736 if (BYTES_BIG_ENDIAN && optimize_size && rs6000_gen_cell_microcode)
2737 rs6000_isa_flags |= ~rs6000_isa_flags_explicit & (OPTION_MASK_MULTIPLE
2738 | OPTION_MASK_STRING);
2740 /* Don't allow -mmultiple or -mstring on little endian systems
2741 unless the cpu is a 750, because the hardware doesn't support the
2742 instructions used in little endian mode, and causes an alignment
2743 trap. The 750 does not cause an alignment trap (except when the
2744 target is unaligned). */
2746 if (!BYTES_BIG_ENDIAN && rs6000_cpu != PROCESSOR_PPC750)
2748 if (TARGET_MULTIPLE)
2750 rs6000_isa_flags &= ~OPTION_MASK_MULTIPLE;
2751 if ((rs6000_isa_flags_explicit & OPTION_MASK_MULTIPLE) != 0)
2752 warning (0, "-mmultiple is not supported on little endian systems");
2755 if (TARGET_STRING)
2757 rs6000_isa_flags &= ~OPTION_MASK_STRING;
2758 if ((rs6000_isa_flags_explicit & OPTION_MASK_STRING) != 0)
2759 warning (0, "-mstring is not supported on little endian systems");
2763 /* Add some warnings for VSX. */
2764 if (TARGET_VSX)
2766 const char *msg = NULL;
2767 if (!TARGET_HARD_FLOAT || !TARGET_FPRS
2768 || !TARGET_SINGLE_FLOAT || !TARGET_DOUBLE_FLOAT)
2770 if (rs6000_isa_flags_explicit & OPTION_MASK_VSX)
2771 msg = N_("-mvsx requires hardware floating point");
2772 else
2773 rs6000_isa_flags &= ~ OPTION_MASK_VSX;
2775 else if (TARGET_PAIRED_FLOAT)
2776 msg = N_("-mvsx and -mpaired are incompatible");
2777 /* The hardware will allow VSX and little endian, but until we make sure
2778 things like vector select, etc. work don't allow VSX on little endian
2779 systems at this point. */
2780 else if (!BYTES_BIG_ENDIAN)
2781 msg = N_("-mvsx used with little endian code");
2782 else if (TARGET_AVOID_XFORM > 0)
2783 msg = N_("-mvsx needs indexed addressing");
2784 else if (!TARGET_ALTIVEC && (rs6000_isa_flags_explicit
2785 & OPTION_MASK_ALTIVEC))
2787 if (rs6000_isa_flags_explicit & OPTION_MASK_VSX)
2788 msg = N_("-mvsx and -mno-altivec are incompatible");
2789 else
2790 msg = N_("-mno-altivec disables vsx");
2793 if (msg)
2795 warning (0, msg);
2796 rs6000_isa_flags &= ~ OPTION_MASK_VSX;
2797 rs6000_isa_flags_explicit |= OPTION_MASK_VSX;
2801 if (TARGET_DEBUG_REG || TARGET_DEBUG_TARGET)
2802 rs6000_print_isa_options (stderr, 0, "before defaults", rs6000_isa_flags);
2804 /* For the newer switches (vsx, dfp, etc.) set some of the older options,
2805 unless the user explicitly used the -mno-<option> to disable the code. */
2806 if (TARGET_VSX)
2807 rs6000_isa_flags |= (ISA_2_6_MASKS_SERVER & ~rs6000_isa_flags_explicit);
2808 else if (TARGET_POPCNTD)
2809 rs6000_isa_flags |= (ISA_2_6_MASKS_EMBEDDED & ~rs6000_isa_flags_explicit);
2810 else if (TARGET_DFP)
2811 rs6000_isa_flags |= (ISA_2_5_MASKS_SERVER & ~rs6000_isa_flags_explicit);
2812 else if (TARGET_CMPB)
2813 rs6000_isa_flags |= (ISA_2_5_MASKS_EMBEDDED & ~rs6000_isa_flags_explicit);
2814 else if (TARGET_FPRND)
2815 rs6000_isa_flags |= (ISA_2_4_MASKS & ~rs6000_isa_flags_explicit);
2816 else if (TARGET_POPCNTB)
2817 rs6000_isa_flags |= (ISA_2_2_MASKS & ~rs6000_isa_flags_explicit);
2818 else if (TARGET_ALTIVEC)
2819 rs6000_isa_flags |= (OPTION_MASK_PPC_GFXOPT & ~rs6000_isa_flags_explicit);
2821 if (TARGET_VSX_TIMODE && !TARGET_VSX)
2823 if (rs6000_isa_flags_explicit & OPTION_MASK_VSX_TIMODE)
2824 error ("-mvsx-timode requires -mvsx");
2825 rs6000_isa_flags &= ~OPTION_MASK_VSX_TIMODE;
2828 if (TARGET_DEBUG_REG || TARGET_DEBUG_TARGET)
2829 rs6000_print_isa_options (stderr, 0, "after defaults", rs6000_isa_flags);
2831 /* E500mc does "better" if we inline more aggressively. Respect the
2832 user's opinion, though. */
2833 if (rs6000_block_move_inline_limit == 0
2834 && (rs6000_cpu == PROCESSOR_PPCE500MC
2835 || rs6000_cpu == PROCESSOR_PPCE500MC64
2836 || rs6000_cpu == PROCESSOR_PPCE5500
2837 || rs6000_cpu == PROCESSOR_PPCE6500))
2838 rs6000_block_move_inline_limit = 128;
2840 /* store_one_arg depends on expand_block_move to handle at least the
2841 size of reg_parm_stack_space. */
2842 if (rs6000_block_move_inline_limit < (TARGET_POWERPC64 ? 64 : 32))
2843 rs6000_block_move_inline_limit = (TARGET_POWERPC64 ? 64 : 32);
2845 if (global_init_p)
2847 /* If the appropriate debug option is enabled, replace the target hooks
2848 with debug versions that call the real version and then prints
2849 debugging information. */
2850 if (TARGET_DEBUG_COST)
2852 targetm.rtx_costs = rs6000_debug_rtx_costs;
2853 targetm.address_cost = rs6000_debug_address_cost;
2854 targetm.sched.adjust_cost = rs6000_debug_adjust_cost;
2857 if (TARGET_DEBUG_ADDR)
2859 targetm.legitimate_address_p = rs6000_debug_legitimate_address_p;
2860 targetm.legitimize_address = rs6000_debug_legitimize_address;
2861 rs6000_secondary_reload_class_ptr
2862 = rs6000_debug_secondary_reload_class;
2863 rs6000_secondary_memory_needed_ptr
2864 = rs6000_debug_secondary_memory_needed;
2865 rs6000_cannot_change_mode_class_ptr
2866 = rs6000_debug_cannot_change_mode_class;
2867 rs6000_preferred_reload_class_ptr
2868 = rs6000_debug_preferred_reload_class;
2869 rs6000_legitimize_reload_address_ptr
2870 = rs6000_debug_legitimize_reload_address;
2871 rs6000_mode_dependent_address_ptr
2872 = rs6000_debug_mode_dependent_address;
2875 if (rs6000_veclibabi_name)
2877 if (strcmp (rs6000_veclibabi_name, "mass") == 0)
2878 rs6000_veclib_handler = rs6000_builtin_vectorized_libmass;
2879 else
2881 error ("unknown vectorization library ABI type (%s) for "
2882 "-mveclibabi= switch", rs6000_veclibabi_name);
2883 ret = false;
2888 if (!global_options_set.x_rs6000_long_double_type_size)
2890 if (main_target_opt != NULL
2891 && (main_target_opt->x_rs6000_long_double_type_size
2892 != RS6000_DEFAULT_LONG_DOUBLE_SIZE))
2893 error ("target attribute or pragma changes long double size");
2894 else
2895 rs6000_long_double_type_size = RS6000_DEFAULT_LONG_DOUBLE_SIZE;
2898 #if !defined (POWERPC_LINUX) && !defined (POWERPC_FREEBSD)
2899 if (!global_options_set.x_rs6000_ieeequad)
2900 rs6000_ieeequad = 1;
2901 #endif
2903 /* Disable VSX and Altivec silently if the user switched cpus to power7 in a
2904 target attribute or pragma which automatically enables both options,
2905 unless the altivec ABI was set. This is set by default for 64-bit, but
2906 not for 32-bit. */
2907 if (main_target_opt != NULL && !main_target_opt->x_rs6000_altivec_abi)
2908 rs6000_isa_flags &= ~((OPTION_MASK_VSX | OPTION_MASK_ALTIVEC)
2909 & ~rs6000_isa_flags_explicit);
2911 /* Enable Altivec ABI for AIX -maltivec. */
2912 if (TARGET_XCOFF && (TARGET_ALTIVEC || TARGET_VSX))
2914 if (main_target_opt != NULL && !main_target_opt->x_rs6000_altivec_abi)
2915 error ("target attribute or pragma changes AltiVec ABI");
2916 else
2917 rs6000_altivec_abi = 1;
2920 /* The AltiVec ABI is the default for PowerPC-64 GNU/Linux. For
2921 PowerPC-32 GNU/Linux, -maltivec implies the AltiVec ABI. It can
2922 be explicitly overridden in either case. */
2923 if (TARGET_ELF)
2925 if (!global_options_set.x_rs6000_altivec_abi
2926 && (TARGET_64BIT || TARGET_ALTIVEC || TARGET_VSX))
2928 if (main_target_opt != NULL &&
2929 !main_target_opt->x_rs6000_altivec_abi)
2930 error ("target attribute or pragma changes AltiVec ABI");
2931 else
2932 rs6000_altivec_abi = 1;
2936 /* Set the Darwin64 ABI as default for 64-bit Darwin.
2937 So far, the only darwin64 targets are also MACH-O. */
2938 if (TARGET_MACHO
2939 && DEFAULT_ABI == ABI_DARWIN
2940 && TARGET_64BIT)
2942 if (main_target_opt != NULL && !main_target_opt->x_rs6000_darwin64_abi)
2943 error ("target attribute or pragma changes darwin64 ABI");
2944 else
2946 rs6000_darwin64_abi = 1;
2947 /* Default to natural alignment, for better performance. */
2948 rs6000_alignment_flags = MASK_ALIGN_NATURAL;
2952 /* Place FP constants in the constant pool instead of TOC
2953 if section anchors enabled. */
2954 if (flag_section_anchors)
2955 TARGET_NO_FP_IN_TOC = 1;
2957 if (TARGET_DEBUG_REG || TARGET_DEBUG_TARGET)
2958 rs6000_print_isa_options (stderr, 0, "before subtarget", rs6000_isa_flags);
2960 #ifdef SUBTARGET_OVERRIDE_OPTIONS
2961 SUBTARGET_OVERRIDE_OPTIONS;
2962 #endif
2963 #ifdef SUBSUBTARGET_OVERRIDE_OPTIONS
2964 SUBSUBTARGET_OVERRIDE_OPTIONS;
2965 #endif
2966 #ifdef SUB3TARGET_OVERRIDE_OPTIONS
2967 SUB3TARGET_OVERRIDE_OPTIONS;
2968 #endif
2970 if (TARGET_DEBUG_REG || TARGET_DEBUG_TARGET)
2971 rs6000_print_isa_options (stderr, 0, "after subtarget", rs6000_isa_flags);
2973 /* For the E500 family of cores, reset the single/double FP flags to let us
2974 check that they remain constant across attributes or pragmas. Also,
2975 clear a possible request for string instructions, not supported and which
2976 we might have silently queried above for -Os.
2978 For other families, clear ISEL in case it was set implicitly.
2981 switch (rs6000_cpu)
2983 case PROCESSOR_PPC8540:
2984 case PROCESSOR_PPC8548:
2985 case PROCESSOR_PPCE500MC:
2986 case PROCESSOR_PPCE500MC64:
2987 case PROCESSOR_PPCE5500:
2988 case PROCESSOR_PPCE6500:
2990 rs6000_single_float = TARGET_E500_SINGLE || TARGET_E500_DOUBLE;
2991 rs6000_double_float = TARGET_E500_DOUBLE;
2993 rs6000_isa_flags &= ~OPTION_MASK_STRING;
2995 break;
2997 default:
2999 if (have_cpu && !(rs6000_isa_flags_explicit & OPTION_MASK_ISEL))
3000 rs6000_isa_flags &= ~OPTION_MASK_ISEL;
3002 break;
3005 if (main_target_opt)
3007 if (main_target_opt->x_rs6000_single_float != rs6000_single_float)
3008 error ("target attribute or pragma changes single precision floating "
3009 "point");
3010 if (main_target_opt->x_rs6000_double_float != rs6000_double_float)
3011 error ("target attribute or pragma changes double precision floating "
3012 "point");
3015 /* Detect invalid option combinations with E500. */
3016 CHECK_E500_OPTIONS;
3018 rs6000_always_hint = (rs6000_cpu != PROCESSOR_POWER4
3019 && rs6000_cpu != PROCESSOR_POWER5
3020 && rs6000_cpu != PROCESSOR_POWER6
3021 && rs6000_cpu != PROCESSOR_POWER7
3022 && rs6000_cpu != PROCESSOR_PPCA2
3023 && rs6000_cpu != PROCESSOR_CELL
3024 && rs6000_cpu != PROCESSOR_PPC476);
3025 rs6000_sched_groups = (rs6000_cpu == PROCESSOR_POWER4
3026 || rs6000_cpu == PROCESSOR_POWER5
3027 || rs6000_cpu == PROCESSOR_POWER7);
3028 rs6000_align_branch_targets = (rs6000_cpu == PROCESSOR_POWER4
3029 || rs6000_cpu == PROCESSOR_POWER5
3030 || rs6000_cpu == PROCESSOR_POWER6
3031 || rs6000_cpu == PROCESSOR_POWER7
3032 || rs6000_cpu == PROCESSOR_PPCE500MC
3033 || rs6000_cpu == PROCESSOR_PPCE500MC64
3034 || rs6000_cpu == PROCESSOR_PPCE5500
3035 || rs6000_cpu == PROCESSOR_PPCE6500);
3037 /* Allow debug switches to override the above settings. These are set to -1
3038 in rs6000.opt to indicate the user hasn't directly set the switch. */
3039 if (TARGET_ALWAYS_HINT >= 0)
3040 rs6000_always_hint = TARGET_ALWAYS_HINT;
3042 if (TARGET_SCHED_GROUPS >= 0)
3043 rs6000_sched_groups = TARGET_SCHED_GROUPS;
3045 if (TARGET_ALIGN_BRANCH_TARGETS >= 0)
3046 rs6000_align_branch_targets = TARGET_ALIGN_BRANCH_TARGETS;
3048 rs6000_sched_restricted_insns_priority
3049 = (rs6000_sched_groups ? 1 : 0);
3051 /* Handle -msched-costly-dep option. */
3052 rs6000_sched_costly_dep
3053 = (rs6000_sched_groups ? true_store_to_load_dep_costly : no_dep_costly);
3055 if (rs6000_sched_costly_dep_str)
3057 if (! strcmp (rs6000_sched_costly_dep_str, "no"))
3058 rs6000_sched_costly_dep = no_dep_costly;
3059 else if (! strcmp (rs6000_sched_costly_dep_str, "all"))
3060 rs6000_sched_costly_dep = all_deps_costly;
3061 else if (! strcmp (rs6000_sched_costly_dep_str, "true_store_to_load"))
3062 rs6000_sched_costly_dep = true_store_to_load_dep_costly;
3063 else if (! strcmp (rs6000_sched_costly_dep_str, "store_to_load"))
3064 rs6000_sched_costly_dep = store_to_load_dep_costly;
3065 else
3066 rs6000_sched_costly_dep = ((enum rs6000_dependence_cost)
3067 atoi (rs6000_sched_costly_dep_str));
3070 /* Handle -minsert-sched-nops option. */
3071 rs6000_sched_insert_nops
3072 = (rs6000_sched_groups ? sched_finish_regroup_exact : sched_finish_none);
3074 if (rs6000_sched_insert_nops_str)
3076 if (! strcmp (rs6000_sched_insert_nops_str, "no"))
3077 rs6000_sched_insert_nops = sched_finish_none;
3078 else if (! strcmp (rs6000_sched_insert_nops_str, "pad"))
3079 rs6000_sched_insert_nops = sched_finish_pad_groups;
3080 else if (! strcmp (rs6000_sched_insert_nops_str, "regroup_exact"))
3081 rs6000_sched_insert_nops = sched_finish_regroup_exact;
3082 else
3083 rs6000_sched_insert_nops = ((enum rs6000_nop_insertion)
3084 atoi (rs6000_sched_insert_nops_str));
3087 if (global_init_p)
3089 #ifdef TARGET_REGNAMES
3090 /* If the user desires alternate register names, copy in the
3091 alternate names now. */
3092 if (TARGET_REGNAMES)
3093 memcpy (rs6000_reg_names, alt_reg_names, sizeof (rs6000_reg_names));
3094 #endif
3096 /* Set aix_struct_return last, after the ABI is determined.
3097 If -maix-struct-return or -msvr4-struct-return was explicitly
3098 used, don't override with the ABI default. */
3099 if (!global_options_set.x_aix_struct_return)
3100 aix_struct_return = (DEFAULT_ABI != ABI_V4 || DRAFT_V4_STRUCT_RET);
3102 #if 0
3103 /* IBM XL compiler defaults to unsigned bitfields. */
3104 if (TARGET_XL_COMPAT)
3105 flag_signed_bitfields = 0;
3106 #endif
3108 if (TARGET_LONG_DOUBLE_128 && !TARGET_IEEEQUAD)
3109 REAL_MODE_FORMAT (TFmode) = &ibm_extended_format;
3111 if (TARGET_TOC)
3112 ASM_GENERATE_INTERNAL_LABEL (toc_label_name, "LCTOC", 1);
3114 /* We can only guarantee the availability of DI pseudo-ops when
3115 assembling for 64-bit targets. */
3116 if (!TARGET_64BIT)
3118 targetm.asm_out.aligned_op.di = NULL;
3119 targetm.asm_out.unaligned_op.di = NULL;
3123 /* Set branch target alignment, if not optimizing for size. */
3124 if (!optimize_size)
3126 /* Cell wants to be aligned 8byte for dual issue. Titan wants to be
3127 aligned 8byte to avoid misprediction by the branch predictor. */
3128 if (rs6000_cpu == PROCESSOR_TITAN
3129 || rs6000_cpu == PROCESSOR_CELL)
3131 if (align_functions <= 0)
3132 align_functions = 8;
3133 if (align_jumps <= 0)
3134 align_jumps = 8;
3135 if (align_loops <= 0)
3136 align_loops = 8;
3138 if (rs6000_align_branch_targets)
3140 if (align_functions <= 0)
3141 align_functions = 16;
3142 if (align_jumps <= 0)
3143 align_jumps = 16;
3144 if (align_loops <= 0)
3146 can_override_loop_align = 1;
3147 align_loops = 16;
3150 if (align_jumps_max_skip <= 0)
3151 align_jumps_max_skip = 15;
3152 if (align_loops_max_skip <= 0)
3153 align_loops_max_skip = 15;
3156 /* Arrange to save and restore machine status around nested functions. */
3157 init_machine_status = rs6000_init_machine_status;
3159 /* We should always be splitting complex arguments, but we can't break
3160 Linux and Darwin ABIs at the moment. For now, only AIX is fixed. */
3161 if (DEFAULT_ABI != ABI_AIX)
3162 targetm.calls.split_complex_arg = NULL;
3165 /* Initialize rs6000_cost with the appropriate target costs. */
3166 if (optimize_size)
3167 rs6000_cost = TARGET_POWERPC64 ? &size64_cost : &size32_cost;
3168 else
3169 switch (rs6000_cpu)
3171 case PROCESSOR_RS64A:
3172 rs6000_cost = &rs64a_cost;
3173 break;
3175 case PROCESSOR_MPCCORE:
3176 rs6000_cost = &mpccore_cost;
3177 break;
3179 case PROCESSOR_PPC403:
3180 rs6000_cost = &ppc403_cost;
3181 break;
3183 case PROCESSOR_PPC405:
3184 rs6000_cost = &ppc405_cost;
3185 break;
3187 case PROCESSOR_PPC440:
3188 rs6000_cost = &ppc440_cost;
3189 break;
3191 case PROCESSOR_PPC476:
3192 rs6000_cost = &ppc476_cost;
3193 break;
3195 case PROCESSOR_PPC601:
3196 rs6000_cost = &ppc601_cost;
3197 break;
3199 case PROCESSOR_PPC603:
3200 rs6000_cost = &ppc603_cost;
3201 break;
3203 case PROCESSOR_PPC604:
3204 rs6000_cost = &ppc604_cost;
3205 break;
3207 case PROCESSOR_PPC604e:
3208 rs6000_cost = &ppc604e_cost;
3209 break;
3211 case PROCESSOR_PPC620:
3212 rs6000_cost = &ppc620_cost;
3213 break;
3215 case PROCESSOR_PPC630:
3216 rs6000_cost = &ppc630_cost;
3217 break;
3219 case PROCESSOR_CELL:
3220 rs6000_cost = &ppccell_cost;
3221 break;
3223 case PROCESSOR_PPC750:
3224 case PROCESSOR_PPC7400:
3225 rs6000_cost = &ppc750_cost;
3226 break;
3228 case PROCESSOR_PPC7450:
3229 rs6000_cost = &ppc7450_cost;
3230 break;
3232 case PROCESSOR_PPC8540:
3233 case PROCESSOR_PPC8548:
3234 rs6000_cost = &ppc8540_cost;
3235 break;
3237 case PROCESSOR_PPCE300C2:
3238 case PROCESSOR_PPCE300C3:
3239 rs6000_cost = &ppce300c2c3_cost;
3240 break;
3242 case PROCESSOR_PPCE500MC:
3243 rs6000_cost = &ppce500mc_cost;
3244 break;
3246 case PROCESSOR_PPCE500MC64:
3247 rs6000_cost = &ppce500mc64_cost;
3248 break;
3250 case PROCESSOR_PPCE5500:
3251 rs6000_cost = &ppce5500_cost;
3252 break;
3254 case PROCESSOR_PPCE6500:
3255 rs6000_cost = &ppce6500_cost;
3256 break;
3258 case PROCESSOR_TITAN:
3259 rs6000_cost = &titan_cost;
3260 break;
3262 case PROCESSOR_POWER4:
3263 case PROCESSOR_POWER5:
3264 rs6000_cost = &power4_cost;
3265 break;
3267 case PROCESSOR_POWER6:
3268 rs6000_cost = &power6_cost;
3269 break;
3271 case PROCESSOR_POWER7:
3272 rs6000_cost = &power7_cost;
3273 break;
3275 case PROCESSOR_PPCA2:
3276 rs6000_cost = &ppca2_cost;
3277 break;
3279 default:
3280 gcc_unreachable ();
3283 if (global_init_p)
3285 maybe_set_param_value (PARAM_SIMULTANEOUS_PREFETCHES,
3286 rs6000_cost->simultaneous_prefetches,
3287 global_options.x_param_values,
3288 global_options_set.x_param_values);
3289 maybe_set_param_value (PARAM_L1_CACHE_SIZE, rs6000_cost->l1_cache_size,
3290 global_options.x_param_values,
3291 global_options_set.x_param_values);
3292 maybe_set_param_value (PARAM_L1_CACHE_LINE_SIZE,
3293 rs6000_cost->cache_line_size,
3294 global_options.x_param_values,
3295 global_options_set.x_param_values);
3296 maybe_set_param_value (PARAM_L2_CACHE_SIZE, rs6000_cost->l2_cache_size,
3297 global_options.x_param_values,
3298 global_options_set.x_param_values);
3300 /* Increase loop peeling limits based on performance analysis. */
3301 maybe_set_param_value (PARAM_MAX_PEELED_INSNS, 400,
3302 global_options.x_param_values,
3303 global_options_set.x_param_values);
3304 maybe_set_param_value (PARAM_MAX_COMPLETELY_PEELED_INSNS, 400,
3305 global_options.x_param_values,
3306 global_options_set.x_param_values);
3308 /* If using typedef char *va_list, signal that
3309 __builtin_va_start (&ap, 0) can be optimized to
3310 ap = __builtin_next_arg (0). */
3311 if (DEFAULT_ABI != ABI_V4)
3312 targetm.expand_builtin_va_start = NULL;
3315 /* Set up single/double float flags.
3316 If TARGET_HARD_FLOAT is set, but neither single or double is set,
3317 then set both flags. */
3318 if (TARGET_HARD_FLOAT && TARGET_FPRS
3319 && rs6000_single_float == 0 && rs6000_double_float == 0)
3320 rs6000_single_float = rs6000_double_float = 1;
3322 /* If not explicitly specified via option, decide whether to generate indexed
3323 load/store instructions. */
3324 if (TARGET_AVOID_XFORM == -1)
3325 /* Avoid indexed addressing when targeting Power6 in order to avoid the
3326 DERAT mispredict penalty. However the LVE and STVE altivec instructions
3327 need indexed accesses and the type used is the scalar type of the element
3328 being loaded or stored. */
3329 TARGET_AVOID_XFORM = (rs6000_cpu == PROCESSOR_POWER6 && TARGET_CMPB
3330 && !TARGET_ALTIVEC);
3332 /* Set the -mrecip options. */
3333 if (rs6000_recip_name)
3335 char *p = ASTRDUP (rs6000_recip_name);
3336 char *q;
3337 unsigned int mask, i;
3338 bool invert;
3340 while ((q = strtok (p, ",")) != NULL)
3342 p = NULL;
3343 if (*q == '!')
3345 invert = true;
3346 q++;
3348 else
3349 invert = false;
3351 if (!strcmp (q, "default"))
3352 mask = ((TARGET_RECIP_PRECISION)
3353 ? RECIP_HIGH_PRECISION : RECIP_LOW_PRECISION);
3354 else
3356 for (i = 0; i < ARRAY_SIZE (recip_options); i++)
3357 if (!strcmp (q, recip_options[i].string))
3359 mask = recip_options[i].mask;
3360 break;
3363 if (i == ARRAY_SIZE (recip_options))
3365 error ("unknown option for -mrecip=%s", q);
3366 invert = false;
3367 mask = 0;
3368 ret = false;
3372 if (invert)
3373 rs6000_recip_control &= ~mask;
3374 else
3375 rs6000_recip_control |= mask;
3379 /* Set the builtin mask of the various options used that could affect which
3380 builtins were used. In the past we used target_flags, but we've run out
3381 of bits, and some options like SPE and PAIRED are no longer in
3382 target_flags. */
3383 rs6000_builtin_mask = rs6000_builtin_mask_calculate ();
3384 if (TARGET_DEBUG_BUILTIN || TARGET_DEBUG_TARGET)
3386 fprintf (stderr,
3387 "new builtin mask = " HOST_WIDE_INT_PRINT_HEX ", ",
3388 rs6000_builtin_mask);
3389 rs6000_print_builtin_options (stderr, 0, NULL, rs6000_builtin_mask);
3392 /* Initialize all of the registers. */
3393 rs6000_init_hard_regno_mode_ok (global_init_p);
3395 /* Save the initial options in case the user does function specific options */
3396 if (global_init_p)
3397 target_option_default_node = target_option_current_node
3398 = build_target_option_node ();
3400 /* If not explicitly specified via option, decide whether to generate the
3401 extra blr's required to preserve the link stack on some cpus (eg, 476). */
3402 if (TARGET_LINK_STACK == -1)
3403 SET_TARGET_LINK_STACK (rs6000_cpu == PROCESSOR_PPC476 && flag_pic);
3405 return ret;
3408 /* Implement TARGET_OPTION_OVERRIDE. On the RS/6000 this is used to
3409 define the target cpu type. */
3411 static void
3412 rs6000_option_override (void)
3414 (void) rs6000_option_override_internal (true);
3418 /* Implement targetm.vectorize.builtin_mask_for_load. */
3419 static tree
3420 rs6000_builtin_mask_for_load (void)
3422 if (TARGET_ALTIVEC || TARGET_VSX)
3423 return altivec_builtin_mask_for_load;
3424 else
3425 return 0;
3428 /* Implement LOOP_ALIGN. */
3430 rs6000_loop_align (rtx label)
3432 basic_block bb;
3433 int ninsns;
3435 /* Don't override loop alignment if -falign-loops was specified. */
3436 if (!can_override_loop_align)
3437 return align_loops_log;
3439 bb = BLOCK_FOR_INSN (label);
3440 ninsns = num_loop_insns(bb->loop_father);
3442 /* Align small loops to 32 bytes to fit in an icache sector, otherwise return default. */
3443 if (ninsns > 4 && ninsns <= 8
3444 && (rs6000_cpu == PROCESSOR_POWER4
3445 || rs6000_cpu == PROCESSOR_POWER5
3446 || rs6000_cpu == PROCESSOR_POWER6
3447 || rs6000_cpu == PROCESSOR_POWER7))
3448 return 5;
3449 else
3450 return align_loops_log;
3453 /* Implement TARGET_LOOP_ALIGN_MAX_SKIP. */
3454 static int
3455 rs6000_loop_align_max_skip (rtx label)
3457 return (1 << rs6000_loop_align (label)) - 1;
3460 /* Return true iff, data reference of TYPE can reach vector alignment (16)
3461 after applying N number of iterations. This routine does not determine
3462 how may iterations are required to reach desired alignment. */
3464 static bool
3465 rs6000_vector_alignment_reachable (const_tree type ATTRIBUTE_UNUSED, bool is_packed)
3467 if (is_packed)
3468 return false;
3470 if (TARGET_32BIT)
3472 if (rs6000_alignment_flags == MASK_ALIGN_NATURAL)
3473 return true;
3475 if (rs6000_alignment_flags == MASK_ALIGN_POWER)
3476 return true;
3478 return false;
3480 else
3482 if (TARGET_MACHO)
3483 return false;
3485 /* Assuming that all other types are naturally aligned. CHECKME! */
3486 return true;
3490 /* Return true if the vector misalignment factor is supported by the
3491 target. */
3492 static bool
3493 rs6000_builtin_support_vector_misalignment (enum machine_mode mode,
3494 const_tree type,
3495 int misalignment,
3496 bool is_packed)
3498 if (TARGET_VSX)
3500 /* Return if movmisalign pattern is not supported for this mode. */
3501 if (optab_handler (movmisalign_optab, mode) == CODE_FOR_nothing)
3502 return false;
3504 if (misalignment == -1)
3506 /* Misalignment factor is unknown at compile time but we know
3507 it's word aligned. */
3508 if (rs6000_vector_alignment_reachable (type, is_packed))
3510 int element_size = TREE_INT_CST_LOW (TYPE_SIZE (type));
3512 if (element_size == 64 || element_size == 32)
3513 return true;
3516 return false;
3519 /* VSX supports word-aligned vector. */
3520 if (misalignment % 4 == 0)
3521 return true;
3523 return false;
3526 /* Implement targetm.vectorize.builtin_vectorization_cost. */
3527 static int
3528 rs6000_builtin_vectorization_cost (enum vect_cost_for_stmt type_of_cost,
3529 tree vectype, int misalign)
3531 unsigned elements;
3532 tree elem_type;
3534 switch (type_of_cost)
3536 case scalar_stmt:
3537 case scalar_load:
3538 case scalar_store:
3539 case vector_stmt:
3540 case vector_load:
3541 case vector_store:
3542 case vec_to_scalar:
3543 case scalar_to_vec:
3544 case cond_branch_not_taken:
3545 return 1;
3547 case vec_perm:
3548 if (TARGET_VSX)
3549 return 3;
3550 else
3551 return 1;
3553 case vec_promote_demote:
3554 if (TARGET_VSX)
3555 return 4;
3556 else
3557 return 1;
3559 case cond_branch_taken:
3560 return 3;
3562 case unaligned_load:
3563 if (TARGET_VSX && TARGET_ALLOW_MOVMISALIGN)
3565 elements = TYPE_VECTOR_SUBPARTS (vectype);
3566 if (elements == 2)
3567 /* Double word aligned. */
3568 return 2;
3570 if (elements == 4)
3572 switch (misalign)
3574 case 8:
3575 /* Double word aligned. */
3576 return 2;
3578 case -1:
3579 /* Unknown misalignment. */
3580 case 4:
3581 case 12:
3582 /* Word aligned. */
3583 return 22;
3585 default:
3586 gcc_unreachable ();
3591 if (TARGET_ALTIVEC)
3592 /* Misaligned loads are not supported. */
3593 gcc_unreachable ();
3595 return 2;
3597 case unaligned_store:
3598 if (TARGET_VSX && TARGET_ALLOW_MOVMISALIGN)
3600 elements = TYPE_VECTOR_SUBPARTS (vectype);
3601 if (elements == 2)
3602 /* Double word aligned. */
3603 return 2;
3605 if (elements == 4)
3607 switch (misalign)
3609 case 8:
3610 /* Double word aligned. */
3611 return 2;
3613 case -1:
3614 /* Unknown misalignment. */
3615 case 4:
3616 case 12:
3617 /* Word aligned. */
3618 return 23;
3620 default:
3621 gcc_unreachable ();
3626 if (TARGET_ALTIVEC)
3627 /* Misaligned stores are not supported. */
3628 gcc_unreachable ();
3630 return 2;
3632 case vec_construct:
3633 elements = TYPE_VECTOR_SUBPARTS (vectype);
3634 elem_type = TREE_TYPE (vectype);
3635 /* 32-bit vectors loaded into registers are stored as double
3636 precision, so we need n/2 converts in addition to the usual
3637 n/2 merges to construct a vector of short floats from them. */
3638 if (SCALAR_FLOAT_TYPE_P (elem_type)
3639 && TYPE_PRECISION (elem_type) == 32)
3640 return elements + 1;
3641 else
3642 return elements / 2 + 1;
3644 default:
3645 gcc_unreachable ();
3649 /* Implement targetm.vectorize.preferred_simd_mode. */
3651 static enum machine_mode
3652 rs6000_preferred_simd_mode (enum machine_mode mode)
3654 if (TARGET_VSX)
3655 switch (mode)
3657 case DFmode:
3658 return V2DFmode;
3659 default:;
3661 if (TARGET_ALTIVEC || TARGET_VSX)
3662 switch (mode)
3664 case SFmode:
3665 return V4SFmode;
3666 case DImode:
3667 return V2DImode;
3668 case SImode:
3669 return V4SImode;
3670 case HImode:
3671 return V8HImode;
3672 case QImode:
3673 return V16QImode;
3674 default:;
3676 if (TARGET_SPE)
3677 switch (mode)
3679 case SFmode:
3680 return V2SFmode;
3681 case SImode:
3682 return V2SImode;
3683 default:;
3685 if (TARGET_PAIRED_FLOAT
3686 && mode == SFmode)
3687 return V2SFmode;
3688 return word_mode;
3691 typedef struct _rs6000_cost_data
3693 struct loop *loop_info;
3694 unsigned cost[3];
3695 } rs6000_cost_data;
3697 /* Test for likely overcommitment of vector hardware resources. If a
3698 loop iteration is relatively large, and too large a percentage of
3699 instructions in the loop are vectorized, the cost model may not
3700 adequately reflect delays from unavailable vector resources.
3701 Penalize the loop body cost for this case. */
3703 static void
3704 rs6000_density_test (rs6000_cost_data *data)
3706 const int DENSITY_PCT_THRESHOLD = 85;
3707 const int DENSITY_SIZE_THRESHOLD = 70;
3708 const int DENSITY_PENALTY = 10;
3709 struct loop *loop = data->loop_info;
3710 basic_block *bbs = get_loop_body (loop);
3711 int nbbs = loop->num_nodes;
3712 int vec_cost = data->cost[vect_body], not_vec_cost = 0;
3713 int i, density_pct;
3715 for (i = 0; i < nbbs; i++)
3717 basic_block bb = bbs[i];
3718 gimple_stmt_iterator gsi;
3720 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
3722 gimple stmt = gsi_stmt (gsi);
3723 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
3725 if (!STMT_VINFO_RELEVANT_P (stmt_info)
3726 && !STMT_VINFO_IN_PATTERN_P (stmt_info))
3727 not_vec_cost++;
3731 free (bbs);
3732 density_pct = (vec_cost * 100) / (vec_cost + not_vec_cost);
3734 if (density_pct > DENSITY_PCT_THRESHOLD
3735 && vec_cost + not_vec_cost > DENSITY_SIZE_THRESHOLD)
3737 data->cost[vect_body] = vec_cost * (100 + DENSITY_PENALTY) / 100;
3738 if (dump_enabled_p ())
3739 dump_printf_loc (MSG_NOTE, vect_location,
3740 "density %d%%, cost %d exceeds threshold, penalizing "
3741 "loop body cost by %d%%", density_pct,
3742 vec_cost + not_vec_cost, DENSITY_PENALTY);
3746 /* Implement targetm.vectorize.init_cost. */
3748 static void *
3749 rs6000_init_cost (struct loop *loop_info)
3751 rs6000_cost_data *data = XNEW (struct _rs6000_cost_data);
3752 data->loop_info = loop_info;
3753 data->cost[vect_prologue] = 0;
3754 data->cost[vect_body] = 0;
3755 data->cost[vect_epilogue] = 0;
3756 return data;
3759 /* Implement targetm.vectorize.add_stmt_cost. */
3761 static unsigned
3762 rs6000_add_stmt_cost (void *data, int count, enum vect_cost_for_stmt kind,
3763 struct _stmt_vec_info *stmt_info, int misalign,
3764 enum vect_cost_model_location where)
3766 rs6000_cost_data *cost_data = (rs6000_cost_data*) data;
3767 unsigned retval = 0;
3769 if (flag_vect_cost_model)
3771 tree vectype = stmt_info ? stmt_vectype (stmt_info) : NULL_TREE;
3772 int stmt_cost = rs6000_builtin_vectorization_cost (kind, vectype,
3773 misalign);
3774 /* Statements in an inner loop relative to the loop being
3775 vectorized are weighted more heavily. The value here is
3776 arbitrary and could potentially be improved with analysis. */
3777 if (where == vect_body && stmt_info && stmt_in_inner_loop_p (stmt_info))
3778 count *= 50; /* FIXME. */
3780 retval = (unsigned) (count * stmt_cost);
3781 cost_data->cost[where] += retval;
3784 return retval;
3787 /* Implement targetm.vectorize.finish_cost. */
3789 static void
3790 rs6000_finish_cost (void *data, unsigned *prologue_cost,
3791 unsigned *body_cost, unsigned *epilogue_cost)
3793 rs6000_cost_data *cost_data = (rs6000_cost_data*) data;
3795 if (cost_data->loop_info)
3796 rs6000_density_test (cost_data);
3798 *prologue_cost = cost_data->cost[vect_prologue];
3799 *body_cost = cost_data->cost[vect_body];
3800 *epilogue_cost = cost_data->cost[vect_epilogue];
3803 /* Implement targetm.vectorize.destroy_cost_data. */
3805 static void
3806 rs6000_destroy_cost_data (void *data)
3808 free (data);
3811 /* Handler for the Mathematical Acceleration Subsystem (mass) interface to a
3812 library with vectorized intrinsics. */
3814 static tree
3815 rs6000_builtin_vectorized_libmass (tree fndecl, tree type_out, tree type_in)
3817 char name[32];
3818 const char *suffix = NULL;
3819 tree fntype, new_fndecl, bdecl = NULL_TREE;
3820 int n_args = 1;
3821 const char *bname;
3822 enum machine_mode el_mode, in_mode;
3823 int n, in_n;
3825 /* Libmass is suitable for unsafe math only as it does not correctly support
3826 parts of IEEE with the required precision such as denormals. Only support
3827 it if we have VSX to use the simd d2 or f4 functions.
3828 XXX: Add variable length support. */
3829 if (!flag_unsafe_math_optimizations || !TARGET_VSX)
3830 return NULL_TREE;
3832 el_mode = TYPE_MODE (TREE_TYPE (type_out));
3833 n = TYPE_VECTOR_SUBPARTS (type_out);
3834 in_mode = TYPE_MODE (TREE_TYPE (type_in));
3835 in_n = TYPE_VECTOR_SUBPARTS (type_in);
3836 if (el_mode != in_mode
3837 || n != in_n)
3838 return NULL_TREE;
3840 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
3842 enum built_in_function fn = DECL_FUNCTION_CODE (fndecl);
3843 switch (fn)
3845 case BUILT_IN_ATAN2:
3846 case BUILT_IN_HYPOT:
3847 case BUILT_IN_POW:
3848 n_args = 2;
3849 /* fall through */
3851 case BUILT_IN_ACOS:
3852 case BUILT_IN_ACOSH:
3853 case BUILT_IN_ASIN:
3854 case BUILT_IN_ASINH:
3855 case BUILT_IN_ATAN:
3856 case BUILT_IN_ATANH:
3857 case BUILT_IN_CBRT:
3858 case BUILT_IN_COS:
3859 case BUILT_IN_COSH:
3860 case BUILT_IN_ERF:
3861 case BUILT_IN_ERFC:
3862 case BUILT_IN_EXP2:
3863 case BUILT_IN_EXP:
3864 case BUILT_IN_EXPM1:
3865 case BUILT_IN_LGAMMA:
3866 case BUILT_IN_LOG10:
3867 case BUILT_IN_LOG1P:
3868 case BUILT_IN_LOG2:
3869 case BUILT_IN_LOG:
3870 case BUILT_IN_SIN:
3871 case BUILT_IN_SINH:
3872 case BUILT_IN_SQRT:
3873 case BUILT_IN_TAN:
3874 case BUILT_IN_TANH:
3875 bdecl = builtin_decl_implicit (fn);
3876 suffix = "d2"; /* pow -> powd2 */
3877 if (el_mode != DFmode
3878 || n != 2
3879 || !bdecl)
3880 return NULL_TREE;
3881 break;
3883 case BUILT_IN_ATAN2F:
3884 case BUILT_IN_HYPOTF:
3885 case BUILT_IN_POWF:
3886 n_args = 2;
3887 /* fall through */
3889 case BUILT_IN_ACOSF:
3890 case BUILT_IN_ACOSHF:
3891 case BUILT_IN_ASINF:
3892 case BUILT_IN_ASINHF:
3893 case BUILT_IN_ATANF:
3894 case BUILT_IN_ATANHF:
3895 case BUILT_IN_CBRTF:
3896 case BUILT_IN_COSF:
3897 case BUILT_IN_COSHF:
3898 case BUILT_IN_ERFF:
3899 case BUILT_IN_ERFCF:
3900 case BUILT_IN_EXP2F:
3901 case BUILT_IN_EXPF:
3902 case BUILT_IN_EXPM1F:
3903 case BUILT_IN_LGAMMAF:
3904 case BUILT_IN_LOG10F:
3905 case BUILT_IN_LOG1PF:
3906 case BUILT_IN_LOG2F:
3907 case BUILT_IN_LOGF:
3908 case BUILT_IN_SINF:
3909 case BUILT_IN_SINHF:
3910 case BUILT_IN_SQRTF:
3911 case BUILT_IN_TANF:
3912 case BUILT_IN_TANHF:
3913 bdecl = builtin_decl_implicit (fn);
3914 suffix = "4"; /* powf -> powf4 */
3915 if (el_mode != SFmode
3916 || n != 4
3917 || !bdecl)
3918 return NULL_TREE;
3919 break;
3921 default:
3922 return NULL_TREE;
3925 else
3926 return NULL_TREE;
3928 gcc_assert (suffix != NULL);
3929 bname = IDENTIFIER_POINTER (DECL_NAME (bdecl));
3930 if (!bname)
3931 return NULL_TREE;
3933 strcpy (name, bname + sizeof ("__builtin_") - 1);
3934 strcat (name, suffix);
3936 if (n_args == 1)
3937 fntype = build_function_type_list (type_out, type_in, NULL);
3938 else if (n_args == 2)
3939 fntype = build_function_type_list (type_out, type_in, type_in, NULL);
3940 else
3941 gcc_unreachable ();
3943 /* Build a function declaration for the vectorized function. */
3944 new_fndecl = build_decl (BUILTINS_LOCATION,
3945 FUNCTION_DECL, get_identifier (name), fntype);
3946 TREE_PUBLIC (new_fndecl) = 1;
3947 DECL_EXTERNAL (new_fndecl) = 1;
3948 DECL_IS_NOVOPS (new_fndecl) = 1;
3949 TREE_READONLY (new_fndecl) = 1;
3951 return new_fndecl;
3954 /* Returns a function decl for a vectorized version of the builtin function
3955 with builtin function code FN and the result vector type TYPE, or NULL_TREE
3956 if it is not available. */
3958 static tree
3959 rs6000_builtin_vectorized_function (tree fndecl, tree type_out,
3960 tree type_in)
3962 enum machine_mode in_mode, out_mode;
3963 int in_n, out_n;
3965 if (TARGET_DEBUG_BUILTIN)
3966 fprintf (stderr, "rs6000_builtin_vectorized_function (%s, %s, %s)\n",
3967 IDENTIFIER_POINTER (DECL_NAME (fndecl)),
3968 GET_MODE_NAME (TYPE_MODE (type_out)),
3969 GET_MODE_NAME (TYPE_MODE (type_in)));
3971 if (TREE_CODE (type_out) != VECTOR_TYPE
3972 || TREE_CODE (type_in) != VECTOR_TYPE
3973 || !TARGET_VECTORIZE_BUILTINS)
3974 return NULL_TREE;
3976 out_mode = TYPE_MODE (TREE_TYPE (type_out));
3977 out_n = TYPE_VECTOR_SUBPARTS (type_out);
3978 in_mode = TYPE_MODE (TREE_TYPE (type_in));
3979 in_n = TYPE_VECTOR_SUBPARTS (type_in);
3981 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
3983 enum built_in_function fn = DECL_FUNCTION_CODE (fndecl);
3984 switch (fn)
3986 case BUILT_IN_COPYSIGN:
3987 if (VECTOR_UNIT_VSX_P (V2DFmode)
3988 && out_mode == DFmode && out_n == 2
3989 && in_mode == DFmode && in_n == 2)
3990 return rs6000_builtin_decls[VSX_BUILTIN_CPSGNDP];
3991 break;
3992 case BUILT_IN_COPYSIGNF:
3993 if (out_mode != SFmode || out_n != 4
3994 || in_mode != SFmode || in_n != 4)
3995 break;
3996 if (VECTOR_UNIT_VSX_P (V4SFmode))
3997 return rs6000_builtin_decls[VSX_BUILTIN_CPSGNSP];
3998 if (VECTOR_UNIT_ALTIVEC_P (V4SFmode))
3999 return rs6000_builtin_decls[ALTIVEC_BUILTIN_COPYSIGN_V4SF];
4000 break;
4001 case BUILT_IN_SQRT:
4002 if (VECTOR_UNIT_VSX_P (V2DFmode)
4003 && out_mode == DFmode && out_n == 2
4004 && in_mode == DFmode && in_n == 2)
4005 return rs6000_builtin_decls[VSX_BUILTIN_XVSQRTDP];
4006 break;
4007 case BUILT_IN_SQRTF:
4008 if (VECTOR_UNIT_VSX_P (V4SFmode)
4009 && out_mode == SFmode && out_n == 4
4010 && in_mode == SFmode && in_n == 4)
4011 return rs6000_builtin_decls[VSX_BUILTIN_XVSQRTSP];
4012 break;
4013 case BUILT_IN_CEIL:
4014 if (VECTOR_UNIT_VSX_P (V2DFmode)
4015 && out_mode == DFmode && out_n == 2
4016 && in_mode == DFmode && in_n == 2)
4017 return rs6000_builtin_decls[VSX_BUILTIN_XVRDPIP];
4018 break;
4019 case BUILT_IN_CEILF:
4020 if (out_mode != SFmode || out_n != 4
4021 || in_mode != SFmode || in_n != 4)
4022 break;
4023 if (VECTOR_UNIT_VSX_P (V4SFmode))
4024 return rs6000_builtin_decls[VSX_BUILTIN_XVRSPIP];
4025 if (VECTOR_UNIT_ALTIVEC_P (V4SFmode))
4026 return rs6000_builtin_decls[ALTIVEC_BUILTIN_VRFIP];
4027 break;
4028 case BUILT_IN_FLOOR:
4029 if (VECTOR_UNIT_VSX_P (V2DFmode)
4030 && out_mode == DFmode && out_n == 2
4031 && in_mode == DFmode && in_n == 2)
4032 return rs6000_builtin_decls[VSX_BUILTIN_XVRDPIM];
4033 break;
4034 case BUILT_IN_FLOORF:
4035 if (out_mode != SFmode || out_n != 4
4036 || in_mode != SFmode || in_n != 4)
4037 break;
4038 if (VECTOR_UNIT_VSX_P (V4SFmode))
4039 return rs6000_builtin_decls[VSX_BUILTIN_XVRSPIM];
4040 if (VECTOR_UNIT_ALTIVEC_P (V4SFmode))
4041 return rs6000_builtin_decls[ALTIVEC_BUILTIN_VRFIM];
4042 break;
4043 case BUILT_IN_FMA:
4044 if (VECTOR_UNIT_VSX_P (V2DFmode)
4045 && out_mode == DFmode && out_n == 2
4046 && in_mode == DFmode && in_n == 2)
4047 return rs6000_builtin_decls[VSX_BUILTIN_XVMADDDP];
4048 break;
4049 case BUILT_IN_FMAF:
4050 if (VECTOR_UNIT_VSX_P (V4SFmode)
4051 && out_mode == SFmode && out_n == 4
4052 && in_mode == SFmode && in_n == 4)
4053 return rs6000_builtin_decls[VSX_BUILTIN_XVMADDSP];
4054 else if (VECTOR_UNIT_ALTIVEC_P (V4SFmode)
4055 && out_mode == SFmode && out_n == 4
4056 && in_mode == SFmode && in_n == 4)
4057 return rs6000_builtin_decls[ALTIVEC_BUILTIN_VMADDFP];
4058 break;
4059 case BUILT_IN_TRUNC:
4060 if (VECTOR_UNIT_VSX_P (V2DFmode)
4061 && out_mode == DFmode && out_n == 2
4062 && in_mode == DFmode && in_n == 2)
4063 return rs6000_builtin_decls[VSX_BUILTIN_XVRDPIZ];
4064 break;
4065 case BUILT_IN_TRUNCF:
4066 if (out_mode != SFmode || out_n != 4
4067 || in_mode != SFmode || in_n != 4)
4068 break;
4069 if (VECTOR_UNIT_VSX_P (V4SFmode))
4070 return rs6000_builtin_decls[VSX_BUILTIN_XVRSPIZ];
4071 if (VECTOR_UNIT_ALTIVEC_P (V4SFmode))
4072 return rs6000_builtin_decls[ALTIVEC_BUILTIN_VRFIZ];
4073 break;
4074 case BUILT_IN_NEARBYINT:
4075 if (VECTOR_UNIT_VSX_P (V2DFmode)
4076 && flag_unsafe_math_optimizations
4077 && out_mode == DFmode && out_n == 2
4078 && in_mode == DFmode && in_n == 2)
4079 return rs6000_builtin_decls[VSX_BUILTIN_XVRDPI];
4080 break;
4081 case BUILT_IN_NEARBYINTF:
4082 if (VECTOR_UNIT_VSX_P (V4SFmode)
4083 && flag_unsafe_math_optimizations
4084 && out_mode == SFmode && out_n == 4
4085 && in_mode == SFmode && in_n == 4)
4086 return rs6000_builtin_decls[VSX_BUILTIN_XVRSPI];
4087 break;
4088 case BUILT_IN_RINT:
4089 if (VECTOR_UNIT_VSX_P (V2DFmode)
4090 && !flag_trapping_math
4091 && out_mode == DFmode && out_n == 2
4092 && in_mode == DFmode && in_n == 2)
4093 return rs6000_builtin_decls[VSX_BUILTIN_XVRDPIC];
4094 break;
4095 case BUILT_IN_RINTF:
4096 if (VECTOR_UNIT_VSX_P (V4SFmode)
4097 && !flag_trapping_math
4098 && out_mode == SFmode && out_n == 4
4099 && in_mode == SFmode && in_n == 4)
4100 return rs6000_builtin_decls[VSX_BUILTIN_XVRSPIC];
4101 break;
4102 default:
4103 break;
4107 else if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
4109 enum rs6000_builtins fn
4110 = (enum rs6000_builtins)DECL_FUNCTION_CODE (fndecl);
4111 switch (fn)
4113 case RS6000_BUILTIN_RSQRTF:
4114 if (VECTOR_UNIT_ALTIVEC_OR_VSX_P (V4SFmode)
4115 && out_mode == SFmode && out_n == 4
4116 && in_mode == SFmode && in_n == 4)
4117 return rs6000_builtin_decls[ALTIVEC_BUILTIN_VRSQRTFP];
4118 break;
4119 case RS6000_BUILTIN_RSQRT:
4120 if (VECTOR_UNIT_VSX_P (V2DFmode)
4121 && out_mode == DFmode && out_n == 2
4122 && in_mode == DFmode && in_n == 2)
4123 return rs6000_builtin_decls[VSX_BUILTIN_RSQRT_2DF];
4124 break;
4125 case RS6000_BUILTIN_RECIPF:
4126 if (VECTOR_UNIT_ALTIVEC_OR_VSX_P (V4SFmode)
4127 && out_mode == SFmode && out_n == 4
4128 && in_mode == SFmode && in_n == 4)
4129 return rs6000_builtin_decls[ALTIVEC_BUILTIN_VRECIPFP];
4130 break;
4131 case RS6000_BUILTIN_RECIP:
4132 if (VECTOR_UNIT_VSX_P (V2DFmode)
4133 && out_mode == DFmode && out_n == 2
4134 && in_mode == DFmode && in_n == 2)
4135 return rs6000_builtin_decls[VSX_BUILTIN_RECIP_V2DF];
4136 break;
4137 default:
4138 break;
4142 /* Generate calls to libmass if appropriate. */
4143 if (rs6000_veclib_handler)
4144 return rs6000_veclib_handler (fndecl, type_out, type_in);
4146 return NULL_TREE;
4149 /* Default CPU string for rs6000*_file_start functions. */
4150 static const char *rs6000_default_cpu;
4152 /* Do anything needed at the start of the asm file. */
4154 static void
4155 rs6000_file_start (void)
4157 char buffer[80];
4158 const char *start = buffer;
4159 FILE *file = asm_out_file;
4161 rs6000_default_cpu = TARGET_CPU_DEFAULT;
4163 default_file_start ();
4165 if (flag_verbose_asm)
4167 sprintf (buffer, "\n%s rs6000/powerpc options:", ASM_COMMENT_START);
4169 if (rs6000_default_cpu != 0 && rs6000_default_cpu[0] != '\0')
4171 fprintf (file, "%s --with-cpu=%s", start, rs6000_default_cpu);
4172 start = "";
4175 if (global_options_set.x_rs6000_cpu_index)
4177 fprintf (file, "%s -mcpu=%s", start,
4178 processor_target_table[rs6000_cpu_index].name);
4179 start = "";
4182 if (global_options_set.x_rs6000_tune_index)
4184 fprintf (file, "%s -mtune=%s", start,
4185 processor_target_table[rs6000_tune_index].name);
4186 start = "";
4189 if (PPC405_ERRATUM77)
4191 fprintf (file, "%s PPC405CR_ERRATUM77", start);
4192 start = "";
4195 #ifdef USING_ELFOS_H
4196 switch (rs6000_sdata)
4198 case SDATA_NONE: fprintf (file, "%s -msdata=none", start); start = ""; break;
4199 case SDATA_DATA: fprintf (file, "%s -msdata=data", start); start = ""; break;
4200 case SDATA_SYSV: fprintf (file, "%s -msdata=sysv", start); start = ""; break;
4201 case SDATA_EABI: fprintf (file, "%s -msdata=eabi", start); start = ""; break;
4204 if (rs6000_sdata && g_switch_value)
4206 fprintf (file, "%s -G %d", start,
4207 g_switch_value);
4208 start = "";
4210 #endif
4212 if (*start == '\0')
4213 putc ('\n', file);
4216 if (DEFAULT_ABI == ABI_AIX || (TARGET_ELF && flag_pic == 2))
4218 switch_to_section (toc_section);
4219 switch_to_section (text_section);
4224 /* Return nonzero if this function is known to have a null epilogue. */
4227 direct_return (void)
4229 if (reload_completed)
4231 rs6000_stack_t *info = rs6000_stack_info ();
4233 if (info->first_gp_reg_save == 32
4234 && info->first_fp_reg_save == 64
4235 && info->first_altivec_reg_save == LAST_ALTIVEC_REGNO + 1
4236 && ! info->lr_save_p
4237 && ! info->cr_save_p
4238 && info->vrsave_mask == 0
4239 && ! info->push_p)
4240 return 1;
4243 return 0;
4246 /* Return the number of instructions it takes to form a constant in an
4247 integer register. */
4250 num_insns_constant_wide (HOST_WIDE_INT value)
4252 /* signed constant loadable with addi */
4253 if ((unsigned HOST_WIDE_INT) (value + 0x8000) < 0x10000)
4254 return 1;
4256 /* constant loadable with addis */
4257 else if ((value & 0xffff) == 0
4258 && (value >> 31 == -1 || value >> 31 == 0))
4259 return 1;
4261 else if (TARGET_POWERPC64)
4263 HOST_WIDE_INT low = ((value & 0xffffffff) ^ 0x80000000) - 0x80000000;
4264 HOST_WIDE_INT high = value >> 31;
4266 if (high == 0 || high == -1)
4267 return 2;
4269 high >>= 1;
4271 if (low == 0)
4272 return num_insns_constant_wide (high) + 1;
4273 else if (high == 0)
4274 return num_insns_constant_wide (low) + 1;
4275 else
4276 return (num_insns_constant_wide (high)
4277 + num_insns_constant_wide (low) + 1);
4280 else
4281 return 2;
4285 num_insns_constant (rtx op, enum machine_mode mode)
4287 HOST_WIDE_INT low, high;
4289 switch (GET_CODE (op))
4291 case CONST_INT:
4292 if ((INTVAL (op) >> 31) != 0 && (INTVAL (op) >> 31) != -1
4293 && mask64_operand (op, mode))
4294 return 2;
4295 else
4296 return num_insns_constant_wide (INTVAL (op));
4298 case CONST_DOUBLE:
4299 if (mode == SFmode || mode == SDmode)
4301 long l;
4302 REAL_VALUE_TYPE rv;
4304 REAL_VALUE_FROM_CONST_DOUBLE (rv, op);
4305 if (DECIMAL_FLOAT_MODE_P (mode))
4306 REAL_VALUE_TO_TARGET_DECIMAL32 (rv, l);
4307 else
4308 REAL_VALUE_TO_TARGET_SINGLE (rv, l);
4309 return num_insns_constant_wide ((HOST_WIDE_INT) l);
4312 long l[2];
4313 REAL_VALUE_TYPE rv;
4315 REAL_VALUE_FROM_CONST_DOUBLE (rv, op);
4316 if (DECIMAL_FLOAT_MODE_P (mode))
4317 REAL_VALUE_TO_TARGET_DECIMAL64 (rv, l);
4318 else
4319 REAL_VALUE_TO_TARGET_DOUBLE (rv, l);
4320 high = l[WORDS_BIG_ENDIAN == 0];
4321 low = l[WORDS_BIG_ENDIAN != 0];
4323 if (TARGET_32BIT)
4324 return (num_insns_constant_wide (low)
4325 + num_insns_constant_wide (high));
4326 else
4328 if ((high == 0 && low >= 0)
4329 || (high == -1 && low < 0))
4330 return num_insns_constant_wide (low);
4332 else if (mask64_operand (op, mode))
4333 return 2;
4335 else if (low == 0)
4336 return num_insns_constant_wide (high) + 1;
4338 else
4339 return (num_insns_constant_wide (high)
4340 + num_insns_constant_wide (low) + 1);
4343 default:
4344 gcc_unreachable ();
4348 /* Interpret element ELT of the CONST_VECTOR OP as an integer value.
4349 If the mode of OP is MODE_VECTOR_INT, this simply returns the
4350 corresponding element of the vector, but for V4SFmode and V2SFmode,
4351 the corresponding "float" is interpreted as an SImode integer. */
4353 HOST_WIDE_INT
4354 const_vector_elt_as_int (rtx op, unsigned int elt)
4356 rtx tmp;
4358 /* We can't handle V2DImode and V2DFmode vector constants here yet. */
4359 gcc_assert (GET_MODE (op) != V2DImode
4360 && GET_MODE (op) != V2DFmode);
4362 tmp = CONST_VECTOR_ELT (op, elt);
4363 if (GET_MODE (op) == V4SFmode
4364 || GET_MODE (op) == V2SFmode)
4365 tmp = gen_lowpart (SImode, tmp);
4366 return INTVAL (tmp);
4369 /* Return true if OP can be synthesized with a particular vspltisb, vspltish
4370 or vspltisw instruction. OP is a CONST_VECTOR. Which instruction is used
4371 depends on STEP and COPIES, one of which will be 1. If COPIES > 1,
4372 all items are set to the same value and contain COPIES replicas of the
4373 vsplt's operand; if STEP > 1, one in STEP elements is set to the vsplt's
4374 operand and the others are set to the value of the operand's msb. */
4376 static bool
4377 vspltis_constant (rtx op, unsigned step, unsigned copies)
4379 enum machine_mode mode = GET_MODE (op);
4380 enum machine_mode inner = GET_MODE_INNER (mode);
4382 unsigned i;
4383 unsigned nunits;
4384 unsigned bitsize;
4385 unsigned mask;
4387 HOST_WIDE_INT val;
4388 HOST_WIDE_INT splat_val;
4389 HOST_WIDE_INT msb_val;
4391 if (mode == V2DImode || mode == V2DFmode)
4392 return false;
4394 nunits = GET_MODE_NUNITS (mode);
4395 bitsize = GET_MODE_BITSIZE (inner);
4396 mask = GET_MODE_MASK (inner);
4398 val = const_vector_elt_as_int (op, nunits - 1);
4399 splat_val = val;
4400 msb_val = val > 0 ? 0 : -1;
4402 /* Construct the value to be splatted, if possible. If not, return 0. */
4403 for (i = 2; i <= copies; i *= 2)
4405 HOST_WIDE_INT small_val;
4406 bitsize /= 2;
4407 small_val = splat_val >> bitsize;
4408 mask >>= bitsize;
4409 if (splat_val != ((small_val << bitsize) | (small_val & mask)))
4410 return false;
4411 splat_val = small_val;
4414 /* Check if SPLAT_VAL can really be the operand of a vspltis[bhw]. */
4415 if (EASY_VECTOR_15 (splat_val))
4418 /* Also check if we can splat, and then add the result to itself. Do so if
4419 the value is positive, of if the splat instruction is using OP's mode;
4420 for splat_val < 0, the splat and the add should use the same mode. */
4421 else if (EASY_VECTOR_15_ADD_SELF (splat_val)
4422 && (splat_val >= 0 || (step == 1 && copies == 1)))
4425 /* Also check if are loading up the most significant bit which can be done by
4426 loading up -1 and shifting the value left by -1. */
4427 else if (EASY_VECTOR_MSB (splat_val, inner))
4430 else
4431 return false;
4433 /* Check if VAL is present in every STEP-th element, and the
4434 other elements are filled with its most significant bit. */
4435 for (i = 0; i < nunits - 1; ++i)
4437 HOST_WIDE_INT desired_val;
4438 if (((i + 1) & (step - 1)) == 0)
4439 desired_val = val;
4440 else
4441 desired_val = msb_val;
4443 if (desired_val != const_vector_elt_as_int (op, i))
4444 return false;
4447 return true;
4451 /* Return true if OP is of the given MODE and can be synthesized
4452 with a vspltisb, vspltish or vspltisw. */
4454 bool
4455 easy_altivec_constant (rtx op, enum machine_mode mode)
4457 unsigned step, copies;
4459 if (mode == VOIDmode)
4460 mode = GET_MODE (op);
4461 else if (mode != GET_MODE (op))
4462 return false;
4464 /* V2DI/V2DF was added with VSX. Only allow 0 and all 1's as easy
4465 constants. */
4466 if (mode == V2DFmode)
4467 return zero_constant (op, mode);
4469 if (mode == V2DImode)
4471 /* In case the compiler is built 32-bit, CONST_DOUBLE constants are not
4472 easy. */
4473 if (GET_CODE (CONST_VECTOR_ELT (op, 0)) != CONST_INT
4474 || GET_CODE (CONST_VECTOR_ELT (op, 1)) != CONST_INT)
4475 return false;
4477 if (zero_constant (op, mode))
4478 return true;
4480 if (INTVAL (CONST_VECTOR_ELT (op, 0)) == -1
4481 && INTVAL (CONST_VECTOR_ELT (op, 1)) == -1)
4482 return true;
4484 return false;
4487 /* Start with a vspltisw. */
4488 step = GET_MODE_NUNITS (mode) / 4;
4489 copies = 1;
4491 if (vspltis_constant (op, step, copies))
4492 return true;
4494 /* Then try with a vspltish. */
4495 if (step == 1)
4496 copies <<= 1;
4497 else
4498 step >>= 1;
4500 if (vspltis_constant (op, step, copies))
4501 return true;
4503 /* And finally a vspltisb. */
4504 if (step == 1)
4505 copies <<= 1;
4506 else
4507 step >>= 1;
4509 if (vspltis_constant (op, step, copies))
4510 return true;
4512 return false;
4515 /* Generate a VEC_DUPLICATE representing a vspltis[bhw] instruction whose
4516 result is OP. Abort if it is not possible. */
4519 gen_easy_altivec_constant (rtx op)
4521 enum machine_mode mode = GET_MODE (op);
4522 int nunits = GET_MODE_NUNITS (mode);
4523 rtx last = CONST_VECTOR_ELT (op, nunits - 1);
4524 unsigned step = nunits / 4;
4525 unsigned copies = 1;
4527 /* Start with a vspltisw. */
4528 if (vspltis_constant (op, step, copies))
4529 return gen_rtx_VEC_DUPLICATE (V4SImode, gen_lowpart (SImode, last));
4531 /* Then try with a vspltish. */
4532 if (step == 1)
4533 copies <<= 1;
4534 else
4535 step >>= 1;
4537 if (vspltis_constant (op, step, copies))
4538 return gen_rtx_VEC_DUPLICATE (V8HImode, gen_lowpart (HImode, last));
4540 /* And finally a vspltisb. */
4541 if (step == 1)
4542 copies <<= 1;
4543 else
4544 step >>= 1;
4546 if (vspltis_constant (op, step, copies))
4547 return gen_rtx_VEC_DUPLICATE (V16QImode, gen_lowpart (QImode, last));
4549 gcc_unreachable ();
4552 const char *
4553 output_vec_const_move (rtx *operands)
4555 int cst, cst2;
4556 enum machine_mode mode;
4557 rtx dest, vec;
4559 dest = operands[0];
4560 vec = operands[1];
4561 mode = GET_MODE (dest);
4563 if (TARGET_VSX)
4565 if (zero_constant (vec, mode))
4566 return "xxlxor %x0,%x0,%x0";
4568 if (mode == V2DImode
4569 && INTVAL (CONST_VECTOR_ELT (vec, 0)) == -1
4570 && INTVAL (CONST_VECTOR_ELT (vec, 1)) == -1)
4571 return "vspltisw %0,-1";
4574 if (TARGET_ALTIVEC)
4576 rtx splat_vec;
4577 if (zero_constant (vec, mode))
4578 return "vxor %0,%0,%0";
4580 splat_vec = gen_easy_altivec_constant (vec);
4581 gcc_assert (GET_CODE (splat_vec) == VEC_DUPLICATE);
4582 operands[1] = XEXP (splat_vec, 0);
4583 if (!EASY_VECTOR_15 (INTVAL (operands[1])))
4584 return "#";
4586 switch (GET_MODE (splat_vec))
4588 case V4SImode:
4589 return "vspltisw %0,%1";
4591 case V8HImode:
4592 return "vspltish %0,%1";
4594 case V16QImode:
4595 return "vspltisb %0,%1";
4597 default:
4598 gcc_unreachable ();
4602 gcc_assert (TARGET_SPE);
4604 /* Vector constant 0 is handled as a splitter of V2SI, and in the
4605 pattern of V1DI, V4HI, and V2SF.
4607 FIXME: We should probably return # and add post reload
4608 splitters for these, but this way is so easy ;-). */
4609 cst = INTVAL (CONST_VECTOR_ELT (vec, 0));
4610 cst2 = INTVAL (CONST_VECTOR_ELT (vec, 1));
4611 operands[1] = CONST_VECTOR_ELT (vec, 0);
4612 operands[2] = CONST_VECTOR_ELT (vec, 1);
4613 if (cst == cst2)
4614 return "li %0,%1\n\tevmergelo %0,%0,%0";
4615 else
4616 return "li %0,%1\n\tevmergelo %0,%0,%0\n\tli %0,%2";
4619 /* Initialize TARGET of vector PAIRED to VALS. */
4621 void
4622 paired_expand_vector_init (rtx target, rtx vals)
4624 enum machine_mode mode = GET_MODE (target);
4625 int n_elts = GET_MODE_NUNITS (mode);
4626 int n_var = 0;
4627 rtx x, new_rtx, tmp, constant_op, op1, op2;
4628 int i;
4630 for (i = 0; i < n_elts; ++i)
4632 x = XVECEXP (vals, 0, i);
4633 if (!(CONST_INT_P (x)
4634 || GET_CODE (x) == CONST_DOUBLE
4635 || GET_CODE (x) == CONST_FIXED))
4636 ++n_var;
4638 if (n_var == 0)
4640 /* Load from constant pool. */
4641 emit_move_insn (target, gen_rtx_CONST_VECTOR (mode, XVEC (vals, 0)));
4642 return;
4645 if (n_var == 2)
4647 /* The vector is initialized only with non-constants. */
4648 new_rtx = gen_rtx_VEC_CONCAT (V2SFmode, XVECEXP (vals, 0, 0),
4649 XVECEXP (vals, 0, 1));
4651 emit_move_insn (target, new_rtx);
4652 return;
4655 /* One field is non-constant and the other one is a constant. Load the
4656 constant from the constant pool and use ps_merge instruction to
4657 construct the whole vector. */
4658 op1 = XVECEXP (vals, 0, 0);
4659 op2 = XVECEXP (vals, 0, 1);
4661 constant_op = (CONSTANT_P (op1)) ? op1 : op2;
4663 tmp = gen_reg_rtx (GET_MODE (constant_op));
4664 emit_move_insn (tmp, constant_op);
4666 if (CONSTANT_P (op1))
4667 new_rtx = gen_rtx_VEC_CONCAT (V2SFmode, tmp, op2);
4668 else
4669 new_rtx = gen_rtx_VEC_CONCAT (V2SFmode, op1, tmp);
4671 emit_move_insn (target, new_rtx);
4674 void
4675 paired_expand_vector_move (rtx operands[])
4677 rtx op0 = operands[0], op1 = operands[1];
4679 emit_move_insn (op0, op1);
4682 /* Emit vector compare for code RCODE. DEST is destination, OP1 and
4683 OP2 are two VEC_COND_EXPR operands, CC_OP0 and CC_OP1 are the two
4684 operands for the relation operation COND. This is a recursive
4685 function. */
4687 static void
4688 paired_emit_vector_compare (enum rtx_code rcode,
4689 rtx dest, rtx op0, rtx op1,
4690 rtx cc_op0, rtx cc_op1)
4692 rtx tmp = gen_reg_rtx (V2SFmode);
4693 rtx tmp1, max, min;
4695 gcc_assert (TARGET_PAIRED_FLOAT);
4696 gcc_assert (GET_MODE (op0) == GET_MODE (op1));
4698 switch (rcode)
4700 case LT:
4701 case LTU:
4702 paired_emit_vector_compare (GE, dest, op1, op0, cc_op0, cc_op1);
4703 return;
4704 case GE:
4705 case GEU:
4706 emit_insn (gen_subv2sf3 (tmp, cc_op0, cc_op1));
4707 emit_insn (gen_selv2sf4 (dest, tmp, op0, op1, CONST0_RTX (SFmode)));
4708 return;
4709 case LE:
4710 case LEU:
4711 paired_emit_vector_compare (GE, dest, op0, op1, cc_op1, cc_op0);
4712 return;
4713 case GT:
4714 paired_emit_vector_compare (LE, dest, op1, op0, cc_op0, cc_op1);
4715 return;
4716 case EQ:
4717 tmp1 = gen_reg_rtx (V2SFmode);
4718 max = gen_reg_rtx (V2SFmode);
4719 min = gen_reg_rtx (V2SFmode);
4720 gen_reg_rtx (V2SFmode);
4722 emit_insn (gen_subv2sf3 (tmp, cc_op0, cc_op1));
4723 emit_insn (gen_selv2sf4
4724 (max, tmp, cc_op0, cc_op1, CONST0_RTX (SFmode)));
4725 emit_insn (gen_subv2sf3 (tmp, cc_op1, cc_op0));
4726 emit_insn (gen_selv2sf4
4727 (min, tmp, cc_op0, cc_op1, CONST0_RTX (SFmode)));
4728 emit_insn (gen_subv2sf3 (tmp1, min, max));
4729 emit_insn (gen_selv2sf4 (dest, tmp1, op0, op1, CONST0_RTX (SFmode)));
4730 return;
4731 case NE:
4732 paired_emit_vector_compare (EQ, dest, op1, op0, cc_op0, cc_op1);
4733 return;
4734 case UNLE:
4735 paired_emit_vector_compare (LE, dest, op1, op0, cc_op0, cc_op1);
4736 return;
4737 case UNLT:
4738 paired_emit_vector_compare (LT, dest, op1, op0, cc_op0, cc_op1);
4739 return;
4740 case UNGE:
4741 paired_emit_vector_compare (GE, dest, op1, op0, cc_op0, cc_op1);
4742 return;
4743 case UNGT:
4744 paired_emit_vector_compare (GT, dest, op1, op0, cc_op0, cc_op1);
4745 return;
4746 default:
4747 gcc_unreachable ();
4750 return;
4753 /* Emit vector conditional expression.
4754 DEST is destination. OP1 and OP2 are two VEC_COND_EXPR operands.
4755 CC_OP0 and CC_OP1 are the two operands for the relation operation COND. */
4758 paired_emit_vector_cond_expr (rtx dest, rtx op1, rtx op2,
4759 rtx cond, rtx cc_op0, rtx cc_op1)
4761 enum rtx_code rcode = GET_CODE (cond);
4763 if (!TARGET_PAIRED_FLOAT)
4764 return 0;
4766 paired_emit_vector_compare (rcode, dest, op1, op2, cc_op0, cc_op1);
4768 return 1;
4771 /* Initialize vector TARGET to VALS. */
4773 void
4774 rs6000_expand_vector_init (rtx target, rtx vals)
4776 enum machine_mode mode = GET_MODE (target);
4777 enum machine_mode inner_mode = GET_MODE_INNER (mode);
4778 int n_elts = GET_MODE_NUNITS (mode);
4779 int n_var = 0, one_var = -1;
4780 bool all_same = true, all_const_zero = true;
4781 rtx x, mem;
4782 int i;
4784 for (i = 0; i < n_elts; ++i)
4786 x = XVECEXP (vals, 0, i);
4787 if (!(CONST_INT_P (x)
4788 || GET_CODE (x) == CONST_DOUBLE
4789 || GET_CODE (x) == CONST_FIXED))
4790 ++n_var, one_var = i;
4791 else if (x != CONST0_RTX (inner_mode))
4792 all_const_zero = false;
4794 if (i > 0 && !rtx_equal_p (x, XVECEXP (vals, 0, 0)))
4795 all_same = false;
4798 if (n_var == 0)
4800 rtx const_vec = gen_rtx_CONST_VECTOR (mode, XVEC (vals, 0));
4801 bool int_vector_p = (GET_MODE_CLASS (mode) == MODE_VECTOR_INT);
4802 if ((int_vector_p || TARGET_VSX) && all_const_zero)
4804 /* Zero register. */
4805 emit_insn (gen_rtx_SET (VOIDmode, target,
4806 gen_rtx_XOR (mode, target, target)));
4807 return;
4809 else if (int_vector_p && easy_vector_constant (const_vec, mode))
4811 /* Splat immediate. */
4812 emit_insn (gen_rtx_SET (VOIDmode, target, const_vec));
4813 return;
4815 else
4817 /* Load from constant pool. */
4818 emit_move_insn (target, const_vec);
4819 return;
4823 /* Double word values on VSX can use xxpermdi or lxvdsx. */
4824 if (VECTOR_MEM_VSX_P (mode) && (mode == V2DFmode || mode == V2DImode))
4826 rtx op0 = XVECEXP (vals, 0, 0);
4827 rtx op1 = XVECEXP (vals, 0, 1);
4828 if (all_same)
4830 if (!MEM_P (op0) && !REG_P (op0))
4831 op0 = force_reg (inner_mode, op0);
4832 if (mode == V2DFmode)
4833 emit_insn (gen_vsx_splat_v2df (target, op0));
4834 else
4835 emit_insn (gen_vsx_splat_v2di (target, op0));
4837 else
4839 op0 = force_reg (inner_mode, op0);
4840 op1 = force_reg (inner_mode, op1);
4841 if (mode == V2DFmode)
4842 emit_insn (gen_vsx_concat_v2df (target, op0, op1));
4843 else
4844 emit_insn (gen_vsx_concat_v2di (target, op0, op1));
4846 return;
4849 /* With single precision floating point on VSX, know that internally single
4850 precision is actually represented as a double, and either make 2 V2DF
4851 vectors, and convert these vectors to single precision, or do one
4852 conversion, and splat the result to the other elements. */
4853 if (mode == V4SFmode && VECTOR_MEM_VSX_P (mode))
4855 if (all_same)
4857 rtx freg = gen_reg_rtx (V4SFmode);
4858 rtx sreg = force_reg (SFmode, XVECEXP (vals, 0, 0));
4860 emit_insn (gen_vsx_xscvdpsp_scalar (freg, sreg));
4861 emit_insn (gen_vsx_xxspltw_v4sf (target, freg, const0_rtx));
4863 else
4865 rtx dbl_even = gen_reg_rtx (V2DFmode);
4866 rtx dbl_odd = gen_reg_rtx (V2DFmode);
4867 rtx flt_even = gen_reg_rtx (V4SFmode);
4868 rtx flt_odd = gen_reg_rtx (V4SFmode);
4869 rtx op0 = force_reg (SFmode, XVECEXP (vals, 0, 0));
4870 rtx op1 = force_reg (SFmode, XVECEXP (vals, 0, 1));
4871 rtx op2 = force_reg (SFmode, XVECEXP (vals, 0, 2));
4872 rtx op3 = force_reg (SFmode, XVECEXP (vals, 0, 3));
4874 emit_insn (gen_vsx_concat_v2sf (dbl_even, op0, op1));
4875 emit_insn (gen_vsx_concat_v2sf (dbl_odd, op2, op3));
4876 emit_insn (gen_vsx_xvcvdpsp (flt_even, dbl_even));
4877 emit_insn (gen_vsx_xvcvdpsp (flt_odd, dbl_odd));
4878 rs6000_expand_extract_even (target, flt_even, flt_odd);
4880 return;
4883 /* Store value to stack temp. Load vector element. Splat. However, splat
4884 of 64-bit items is not supported on Altivec. */
4885 if (all_same && GET_MODE_SIZE (inner_mode) <= 4)
4887 mem = assign_stack_temp (mode, GET_MODE_SIZE (inner_mode));
4888 emit_move_insn (adjust_address_nv (mem, inner_mode, 0),
4889 XVECEXP (vals, 0, 0));
4890 x = gen_rtx_UNSPEC (VOIDmode,
4891 gen_rtvec (1, const0_rtx), UNSPEC_LVE);
4892 emit_insn (gen_rtx_PARALLEL (VOIDmode,
4893 gen_rtvec (2,
4894 gen_rtx_SET (VOIDmode,
4895 target, mem),
4896 x)));
4897 x = gen_rtx_VEC_SELECT (inner_mode, target,
4898 gen_rtx_PARALLEL (VOIDmode,
4899 gen_rtvec (1, const0_rtx)));
4900 emit_insn (gen_rtx_SET (VOIDmode, target,
4901 gen_rtx_VEC_DUPLICATE (mode, x)));
4902 return;
4905 /* One field is non-constant. Load constant then overwrite
4906 varying field. */
4907 if (n_var == 1)
4909 rtx copy = copy_rtx (vals);
4911 /* Load constant part of vector, substitute neighboring value for
4912 varying element. */
4913 XVECEXP (copy, 0, one_var) = XVECEXP (vals, 0, (one_var + 1) % n_elts);
4914 rs6000_expand_vector_init (target, copy);
4916 /* Insert variable. */
4917 rs6000_expand_vector_set (target, XVECEXP (vals, 0, one_var), one_var);
4918 return;
4921 /* Construct the vector in memory one field at a time
4922 and load the whole vector. */
4923 mem = assign_stack_temp (mode, GET_MODE_SIZE (mode));
4924 for (i = 0; i < n_elts; i++)
4925 emit_move_insn (adjust_address_nv (mem, inner_mode,
4926 i * GET_MODE_SIZE (inner_mode)),
4927 XVECEXP (vals, 0, i));
4928 emit_move_insn (target, mem);
4931 /* Set field ELT of TARGET to VAL. */
4933 void
4934 rs6000_expand_vector_set (rtx target, rtx val, int elt)
4936 enum machine_mode mode = GET_MODE (target);
4937 enum machine_mode inner_mode = GET_MODE_INNER (mode);
4938 rtx reg = gen_reg_rtx (mode);
4939 rtx mask, mem, x;
4940 int width = GET_MODE_SIZE (inner_mode);
4941 int i;
4943 if (VECTOR_MEM_VSX_P (mode) && (mode == V2DFmode || mode == V2DImode))
4945 rtx (*set_func) (rtx, rtx, rtx, rtx)
4946 = ((mode == V2DFmode) ? gen_vsx_set_v2df : gen_vsx_set_v2di);
4947 emit_insn (set_func (target, target, val, GEN_INT (elt)));
4948 return;
4951 /* Load single variable value. */
4952 mem = assign_stack_temp (mode, GET_MODE_SIZE (inner_mode));
4953 emit_move_insn (adjust_address_nv (mem, inner_mode, 0), val);
4954 x = gen_rtx_UNSPEC (VOIDmode,
4955 gen_rtvec (1, const0_rtx), UNSPEC_LVE);
4956 emit_insn (gen_rtx_PARALLEL (VOIDmode,
4957 gen_rtvec (2,
4958 gen_rtx_SET (VOIDmode,
4959 reg, mem),
4960 x)));
4962 /* Linear sequence. */
4963 mask = gen_rtx_PARALLEL (V16QImode, rtvec_alloc (16));
4964 for (i = 0; i < 16; ++i)
4965 XVECEXP (mask, 0, i) = GEN_INT (i);
4967 /* Set permute mask to insert element into target. */
4968 for (i = 0; i < width; ++i)
4969 XVECEXP (mask, 0, elt*width + i)
4970 = GEN_INT (i + 0x10);
4971 x = gen_rtx_CONST_VECTOR (V16QImode, XVEC (mask, 0));
4972 x = gen_rtx_UNSPEC (mode,
4973 gen_rtvec (3, target, reg,
4974 force_reg (V16QImode, x)),
4975 UNSPEC_VPERM);
4976 emit_insn (gen_rtx_SET (VOIDmode, target, x));
4979 /* Extract field ELT from VEC into TARGET. */
4981 void
4982 rs6000_expand_vector_extract (rtx target, rtx vec, int elt)
4984 enum machine_mode mode = GET_MODE (vec);
4985 enum machine_mode inner_mode = GET_MODE_INNER (mode);
4986 rtx mem;
4988 if (VECTOR_MEM_VSX_P (mode))
4990 switch (mode)
4992 default:
4993 break;
4994 case V2DFmode:
4995 emit_insn (gen_vsx_extract_v2df (target, vec, GEN_INT (elt)));
4996 return;
4997 case V2DImode:
4998 emit_insn (gen_vsx_extract_v2di (target, vec, GEN_INT (elt)));
4999 return;
5000 case V4SFmode:
5001 emit_insn (gen_vsx_extract_v4sf (target, vec, GEN_INT (elt)));
5002 return;
5006 /* Allocate mode-sized buffer. */
5007 mem = assign_stack_temp (mode, GET_MODE_SIZE (mode));
5009 emit_move_insn (mem, vec);
5011 /* Add offset to field within buffer matching vector element. */
5012 mem = adjust_address_nv (mem, inner_mode, elt * GET_MODE_SIZE (inner_mode));
5014 emit_move_insn (target, adjust_address_nv (mem, inner_mode, 0));
5017 /* Generates shifts and masks for a pair of rldicl or rldicr insns to
5018 implement ANDing by the mask IN. */
5019 void
5020 build_mask64_2_operands (rtx in, rtx *out)
5022 unsigned HOST_WIDE_INT c, lsb, m1, m2;
5023 int shift;
5025 gcc_assert (GET_CODE (in) == CONST_INT);
5027 c = INTVAL (in);
5028 if (c & 1)
5030 /* Assume c initially something like 0x00fff000000fffff. The idea
5031 is to rotate the word so that the middle ^^^^^^ group of zeros
5032 is at the MS end and can be cleared with an rldicl mask. We then
5033 rotate back and clear off the MS ^^ group of zeros with a
5034 second rldicl. */
5035 c = ~c; /* c == 0xff000ffffff00000 */
5036 lsb = c & -c; /* lsb == 0x0000000000100000 */
5037 m1 = -lsb; /* m1 == 0xfffffffffff00000 */
5038 c = ~c; /* c == 0x00fff000000fffff */
5039 c &= -lsb; /* c == 0x00fff00000000000 */
5040 lsb = c & -c; /* lsb == 0x0000100000000000 */
5041 c = ~c; /* c == 0xff000fffffffffff */
5042 c &= -lsb; /* c == 0xff00000000000000 */
5043 shift = 0;
5044 while ((lsb >>= 1) != 0)
5045 shift++; /* shift == 44 on exit from loop */
5046 m1 <<= 64 - shift; /* m1 == 0xffffff0000000000 */
5047 m1 = ~m1; /* m1 == 0x000000ffffffffff */
5048 m2 = ~c; /* m2 == 0x00ffffffffffffff */
5050 else
5052 /* Assume c initially something like 0xff000f0000000000. The idea
5053 is to rotate the word so that the ^^^ middle group of zeros
5054 is at the LS end and can be cleared with an rldicr mask. We then
5055 rotate back and clear off the LS group of ^^^^^^^^^^ zeros with
5056 a second rldicr. */
5057 lsb = c & -c; /* lsb == 0x0000010000000000 */
5058 m2 = -lsb; /* m2 == 0xffffff0000000000 */
5059 c = ~c; /* c == 0x00fff0ffffffffff */
5060 c &= -lsb; /* c == 0x00fff00000000000 */
5061 lsb = c & -c; /* lsb == 0x0000100000000000 */
5062 c = ~c; /* c == 0xff000fffffffffff */
5063 c &= -lsb; /* c == 0xff00000000000000 */
5064 shift = 0;
5065 while ((lsb >>= 1) != 0)
5066 shift++; /* shift == 44 on exit from loop */
5067 m1 = ~c; /* m1 == 0x00ffffffffffffff */
5068 m1 >>= shift; /* m1 == 0x0000000000000fff */
5069 m1 = ~m1; /* m1 == 0xfffffffffffff000 */
5072 /* Note that when we only have two 0->1 and 1->0 transitions, one of the
5073 masks will be all 1's. We are guaranteed more than one transition. */
5074 out[0] = GEN_INT (64 - shift);
5075 out[1] = GEN_INT (m1);
5076 out[2] = GEN_INT (shift);
5077 out[3] = GEN_INT (m2);
5080 /* Return TRUE if OP is an invalid SUBREG operation on the e500. */
5082 bool
5083 invalid_e500_subreg (rtx op, enum machine_mode mode)
5085 if (TARGET_E500_DOUBLE)
5087 /* Reject (subreg:SI (reg:DF)); likewise with subreg:DI or
5088 subreg:TI and reg:TF. Decimal float modes are like integer
5089 modes (only low part of each register used) for this
5090 purpose. */
5091 if (GET_CODE (op) == SUBREG
5092 && (mode == SImode || mode == DImode || mode == TImode
5093 || mode == DDmode || mode == TDmode || mode == PTImode)
5094 && REG_P (SUBREG_REG (op))
5095 && (GET_MODE (SUBREG_REG (op)) == DFmode
5096 || GET_MODE (SUBREG_REG (op)) == TFmode))
5097 return true;
5099 /* Reject (subreg:DF (reg:DI)); likewise with subreg:TF and
5100 reg:TI. */
5101 if (GET_CODE (op) == SUBREG
5102 && (mode == DFmode || mode == TFmode)
5103 && REG_P (SUBREG_REG (op))
5104 && (GET_MODE (SUBREG_REG (op)) == DImode
5105 || GET_MODE (SUBREG_REG (op)) == TImode
5106 || GET_MODE (SUBREG_REG (op)) == PTImode
5107 || GET_MODE (SUBREG_REG (op)) == DDmode
5108 || GET_MODE (SUBREG_REG (op)) == TDmode))
5109 return true;
5112 if (TARGET_SPE
5113 && GET_CODE (op) == SUBREG
5114 && mode == SImode
5115 && REG_P (SUBREG_REG (op))
5116 && SPE_VECTOR_MODE (GET_MODE (SUBREG_REG (op))))
5117 return true;
5119 return false;
5122 /* AIX increases natural record alignment to doubleword if the first
5123 field is an FP double while the FP fields remain word aligned. */
5125 unsigned int
5126 rs6000_special_round_type_align (tree type, unsigned int computed,
5127 unsigned int specified)
5129 unsigned int align = MAX (computed, specified);
5130 tree field = TYPE_FIELDS (type);
5132 /* Skip all non field decls */
5133 while (field != NULL && TREE_CODE (field) != FIELD_DECL)
5134 field = DECL_CHAIN (field);
5136 if (field != NULL && field != type)
5138 type = TREE_TYPE (field);
5139 while (TREE_CODE (type) == ARRAY_TYPE)
5140 type = TREE_TYPE (type);
5142 if (type != error_mark_node && TYPE_MODE (type) == DFmode)
5143 align = MAX (align, 64);
5146 return align;
5149 /* Darwin increases record alignment to the natural alignment of
5150 the first field. */
5152 unsigned int
5153 darwin_rs6000_special_round_type_align (tree type, unsigned int computed,
5154 unsigned int specified)
5156 unsigned int align = MAX (computed, specified);
5158 if (TYPE_PACKED (type))
5159 return align;
5161 /* Find the first field, looking down into aggregates. */
5162 do {
5163 tree field = TYPE_FIELDS (type);
5164 /* Skip all non field decls */
5165 while (field != NULL && TREE_CODE (field) != FIELD_DECL)
5166 field = DECL_CHAIN (field);
5167 if (! field)
5168 break;
5169 /* A packed field does not contribute any extra alignment. */
5170 if (DECL_PACKED (field))
5171 return align;
5172 type = TREE_TYPE (field);
5173 while (TREE_CODE (type) == ARRAY_TYPE)
5174 type = TREE_TYPE (type);
5175 } while (AGGREGATE_TYPE_P (type));
5177 if (! AGGREGATE_TYPE_P (type) && type != error_mark_node)
5178 align = MAX (align, TYPE_ALIGN (type));
5180 return align;
5183 /* Return 1 for an operand in small memory on V.4/eabi. */
5186 small_data_operand (rtx op ATTRIBUTE_UNUSED,
5187 enum machine_mode mode ATTRIBUTE_UNUSED)
5189 #if TARGET_ELF
5190 rtx sym_ref;
5192 if (rs6000_sdata == SDATA_NONE || rs6000_sdata == SDATA_DATA)
5193 return 0;
5195 if (DEFAULT_ABI != ABI_V4)
5196 return 0;
5198 /* Vector and float memory instructions have a limited offset on the
5199 SPE, so using a vector or float variable directly as an operand is
5200 not useful. */
5201 if (TARGET_SPE
5202 && (SPE_VECTOR_MODE (mode) || FLOAT_MODE_P (mode)))
5203 return 0;
5205 if (GET_CODE (op) == SYMBOL_REF)
5206 sym_ref = op;
5208 else if (GET_CODE (op) != CONST
5209 || GET_CODE (XEXP (op, 0)) != PLUS
5210 || GET_CODE (XEXP (XEXP (op, 0), 0)) != SYMBOL_REF
5211 || GET_CODE (XEXP (XEXP (op, 0), 1)) != CONST_INT)
5212 return 0;
5214 else
5216 rtx sum = XEXP (op, 0);
5217 HOST_WIDE_INT summand;
5219 /* We have to be careful here, because it is the referenced address
5220 that must be 32k from _SDA_BASE_, not just the symbol. */
5221 summand = INTVAL (XEXP (sum, 1));
5222 if (summand < 0 || summand > g_switch_value)
5223 return 0;
5225 sym_ref = XEXP (sum, 0);
5228 return SYMBOL_REF_SMALL_P (sym_ref);
5229 #else
5230 return 0;
5231 #endif
5234 /* Return true if either operand is a general purpose register. */
5236 bool
5237 gpr_or_gpr_p (rtx op0, rtx op1)
5239 return ((REG_P (op0) && INT_REGNO_P (REGNO (op0)))
5240 || (REG_P (op1) && INT_REGNO_P (REGNO (op1))));
5243 /* Given an address, return a constant offset term if one exists. */
5245 static rtx
5246 address_offset (rtx op)
5248 if (GET_CODE (op) == PRE_INC
5249 || GET_CODE (op) == PRE_DEC)
5250 op = XEXP (op, 0);
5251 else if (GET_CODE (op) == PRE_MODIFY
5252 || GET_CODE (op) == LO_SUM)
5253 op = XEXP (op, 1);
5255 if (GET_CODE (op) == CONST)
5256 op = XEXP (op, 0);
5258 if (GET_CODE (op) == PLUS)
5259 op = XEXP (op, 1);
5261 if (CONST_INT_P (op))
5262 return op;
5264 return NULL_RTX;
5267 /* Return true if the MEM operand is a memory operand suitable for use
5268 with a (full width, possibly multiple) gpr load/store. On
5269 powerpc64 this means the offset must be divisible by 4.
5270 Implements 'Y' constraint.
5272 Accept direct, indexed, offset, lo_sum and tocref. Since this is
5273 a constraint function we know the operand has satisfied a suitable
5274 memory predicate. Also accept some odd rtl generated by reload
5275 (see rs6000_legitimize_reload_address for various forms). It is
5276 important that reload rtl be accepted by appropriate constraints
5277 but not by the operand predicate.
5279 Offsetting a lo_sum should not be allowed, except where we know by
5280 alignment that a 32k boundary is not crossed, but see the ???
5281 comment in rs6000_legitimize_reload_address. Note that by
5282 "offsetting" here we mean a further offset to access parts of the
5283 MEM. It's fine to have a lo_sum where the inner address is offset
5284 from a sym, since the same sym+offset will appear in the high part
5285 of the address calculation. */
5287 bool
5288 mem_operand_gpr (rtx op, enum machine_mode mode)
5290 unsigned HOST_WIDE_INT offset;
5291 int extra;
5292 rtx addr = XEXP (op, 0);
5294 op = address_offset (addr);
5295 if (op == NULL_RTX)
5296 return true;
5298 offset = INTVAL (op);
5299 if (TARGET_POWERPC64 && (offset & 3) != 0)
5300 return false;
5302 extra = GET_MODE_SIZE (mode) - UNITS_PER_WORD;
5303 gcc_assert (extra >= 0);
5305 if (GET_CODE (addr) == LO_SUM)
5306 /* For lo_sum addresses, we must allow any offset except one that
5307 causes a wrap, so test only the low 16 bits. */
5308 offset = ((offset & 0xffff) ^ 0x8000) - 0x8000;
5310 return offset + 0x8000 < 0x10000u - extra;
5313 /* Subroutines of rs6000_legitimize_address and rs6000_legitimate_address_p. */
5315 static bool
5316 reg_offset_addressing_ok_p (enum machine_mode mode)
5318 switch (mode)
5320 case V16QImode:
5321 case V8HImode:
5322 case V4SFmode:
5323 case V4SImode:
5324 case V2DFmode:
5325 case V2DImode:
5326 case TImode:
5327 /* AltiVec/VSX vector modes. Only reg+reg addressing is valid. While
5328 TImode is not a vector mode, if we want to use the VSX registers to
5329 move it around, we need to restrict ourselves to reg+reg
5330 addressing. */
5331 if (VECTOR_MEM_ALTIVEC_OR_VSX_P (mode))
5332 return false;
5333 break;
5335 case V4HImode:
5336 case V2SImode:
5337 case V1DImode:
5338 case V2SFmode:
5339 /* Paired vector modes. Only reg+reg addressing is valid. */
5340 if (TARGET_PAIRED_FLOAT)
5341 return false;
5342 break;
5344 case SDmode:
5345 /* If we can do direct load/stores of SDmode, restrict it to reg+reg
5346 addressing for the LFIWZX and STFIWX instructions. */
5347 if (TARGET_NO_SDMODE_STACK)
5348 return false;
5349 break;
5351 default:
5352 break;
5355 return true;
5358 static bool
5359 virtual_stack_registers_memory_p (rtx op)
5361 int regnum;
5363 if (GET_CODE (op) == REG)
5364 regnum = REGNO (op);
5366 else if (GET_CODE (op) == PLUS
5367 && GET_CODE (XEXP (op, 0)) == REG
5368 && GET_CODE (XEXP (op, 1)) == CONST_INT)
5369 regnum = REGNO (XEXP (op, 0));
5371 else
5372 return false;
5374 return (regnum >= FIRST_VIRTUAL_REGISTER
5375 && regnum <= LAST_VIRTUAL_POINTER_REGISTER);
5378 /* Return true if memory accesses to OP are known to never straddle
5379 a 32k boundary. */
5381 static bool
5382 offsettable_ok_by_alignment (rtx op, HOST_WIDE_INT offset,
5383 enum machine_mode mode)
5385 tree decl, type;
5386 unsigned HOST_WIDE_INT dsize, dalign;
5388 if (GET_CODE (op) != SYMBOL_REF)
5389 return false;
5391 decl = SYMBOL_REF_DECL (op);
5392 if (!decl)
5394 if (GET_MODE_SIZE (mode) == 0)
5395 return false;
5397 /* -fsection-anchors loses the original SYMBOL_REF_DECL when
5398 replacing memory addresses with an anchor plus offset. We
5399 could find the decl by rummaging around in the block->objects
5400 VEC for the given offset but that seems like too much work. */
5401 dalign = 1;
5402 if (SYMBOL_REF_HAS_BLOCK_INFO_P (op)
5403 && SYMBOL_REF_ANCHOR_P (op)
5404 && SYMBOL_REF_BLOCK (op) != NULL)
5406 struct object_block *block = SYMBOL_REF_BLOCK (op);
5407 HOST_WIDE_INT lsb, mask;
5409 /* Given the alignment of the block.. */
5410 dalign = block->alignment;
5411 mask = dalign / BITS_PER_UNIT - 1;
5413 /* ..and the combined offset of the anchor and any offset
5414 to this block object.. */
5415 offset += SYMBOL_REF_BLOCK_OFFSET (op);
5416 lsb = offset & -offset;
5418 /* ..find how many bits of the alignment we know for the
5419 object. */
5420 mask &= lsb - 1;
5421 dalign = mask + 1;
5423 return dalign >= GET_MODE_SIZE (mode);
5426 if (DECL_P (decl))
5428 if (TREE_CODE (decl) == FUNCTION_DECL)
5429 return true;
5431 if (!DECL_SIZE_UNIT (decl))
5432 return false;
5434 if (!host_integerp (DECL_SIZE_UNIT (decl), 1))
5435 return false;
5437 dsize = tree_low_cst (DECL_SIZE_UNIT (decl), 1);
5438 if (dsize > 32768)
5439 return false;
5441 dalign = DECL_ALIGN_UNIT (decl);
5442 return dalign >= dsize;
5445 type = TREE_TYPE (decl);
5447 if (TREE_CODE (decl) == STRING_CST)
5448 dsize = TREE_STRING_LENGTH (decl);
5449 else if (TYPE_SIZE_UNIT (type)
5450 && host_integerp (TYPE_SIZE_UNIT (type), 1))
5451 dsize = tree_low_cst (TYPE_SIZE_UNIT (type), 1);
5452 else
5453 return false;
5454 if (dsize > 32768)
5455 return false;
5457 dalign = TYPE_ALIGN (type);
5458 if (CONSTANT_CLASS_P (decl))
5459 dalign = CONSTANT_ALIGNMENT (decl, dalign);
5460 else
5461 dalign = DATA_ALIGNMENT (decl, dalign);
5462 dalign /= BITS_PER_UNIT;
5463 return dalign >= dsize;
5466 static bool
5467 constant_pool_expr_p (rtx op)
5469 rtx base, offset;
5471 split_const (op, &base, &offset);
5472 return (GET_CODE (base) == SYMBOL_REF
5473 && CONSTANT_POOL_ADDRESS_P (base)
5474 && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (base), Pmode));
5477 static const_rtx tocrel_base, tocrel_offset;
5479 /* Return true if OP is a toc pointer relative address (the output
5480 of create_TOC_reference). If STRICT, do not match high part or
5481 non-split -mcmodel=large/medium toc pointer relative addresses. */
5483 bool
5484 toc_relative_expr_p (const_rtx op, bool strict)
5486 if (!TARGET_TOC)
5487 return false;
5489 if (TARGET_CMODEL != CMODEL_SMALL)
5491 /* Only match the low part. */
5492 if (GET_CODE (op) == LO_SUM
5493 && REG_P (XEXP (op, 0))
5494 && INT_REG_OK_FOR_BASE_P (XEXP (op, 0), strict))
5495 op = XEXP (op, 1);
5496 else if (strict)
5497 return false;
5500 tocrel_base = op;
5501 tocrel_offset = const0_rtx;
5502 if (GET_CODE (op) == PLUS && CONST_INT_P (XEXP (op, 1)))
5504 tocrel_base = XEXP (op, 0);
5505 tocrel_offset = XEXP (op, 1);
5508 return (GET_CODE (tocrel_base) == UNSPEC
5509 && XINT (tocrel_base, 1) == UNSPEC_TOCREL);
5512 /* Return true if X is a constant pool address, and also for cmodel=medium
5513 if X is a toc-relative address known to be offsettable within MODE. */
5515 bool
5516 legitimate_constant_pool_address_p (const_rtx x, enum machine_mode mode,
5517 bool strict)
5519 return (toc_relative_expr_p (x, strict)
5520 && (TARGET_CMODEL != CMODEL_MEDIUM
5521 || constant_pool_expr_p (XVECEXP (tocrel_base, 0, 0))
5522 || mode == QImode
5523 || offsettable_ok_by_alignment (XVECEXP (tocrel_base, 0, 0),
5524 INTVAL (tocrel_offset), mode)));
5527 static bool
5528 legitimate_small_data_p (enum machine_mode mode, rtx x)
5530 return (DEFAULT_ABI == ABI_V4
5531 && !flag_pic && !TARGET_TOC
5532 && (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == CONST)
5533 && small_data_operand (x, mode));
5536 /* SPE offset addressing is limited to 5-bits worth of double words. */
5537 #define SPE_CONST_OFFSET_OK(x) (((x) & ~0xf8) == 0)
5539 bool
5540 rs6000_legitimate_offset_address_p (enum machine_mode mode, rtx x,
5541 bool strict, bool worst_case)
5543 unsigned HOST_WIDE_INT offset;
5544 unsigned int extra;
5546 if (GET_CODE (x) != PLUS)
5547 return false;
5548 if (!REG_P (XEXP (x, 0)))
5549 return false;
5550 if (!INT_REG_OK_FOR_BASE_P (XEXP (x, 0), strict))
5551 return false;
5552 if (!reg_offset_addressing_ok_p (mode))
5553 return virtual_stack_registers_memory_p (x);
5554 if (legitimate_constant_pool_address_p (x, mode, strict))
5555 return true;
5556 if (GET_CODE (XEXP (x, 1)) != CONST_INT)
5557 return false;
5559 offset = INTVAL (XEXP (x, 1));
5560 extra = 0;
5561 switch (mode)
5563 case V4HImode:
5564 case V2SImode:
5565 case V1DImode:
5566 case V2SFmode:
5567 /* SPE vector modes. */
5568 return SPE_CONST_OFFSET_OK (offset);
5570 case DFmode:
5571 case DDmode:
5572 case DImode:
5573 /* On e500v2, we may have:
5575 (subreg:DF (mem:DI (plus (reg) (const_int))) 0).
5577 Which gets addressed with evldd instructions. */
5578 if (TARGET_E500_DOUBLE)
5579 return SPE_CONST_OFFSET_OK (offset);
5581 /* If we are using VSX scalar loads, restrict ourselves to reg+reg
5582 addressing. */
5583 if (VECTOR_MEM_VSX_P (mode))
5584 return false;
5586 if (!worst_case)
5587 break;
5588 if (!TARGET_POWERPC64)
5589 extra = 4;
5590 else if (offset & 3)
5591 return false;
5592 break;
5594 case TFmode:
5595 case TDmode:
5596 case TImode:
5597 case PTImode:
5598 if (TARGET_E500_DOUBLE)
5599 return (SPE_CONST_OFFSET_OK (offset)
5600 && SPE_CONST_OFFSET_OK (offset + 8));
5602 extra = 8;
5603 if (!worst_case)
5604 break;
5605 if (!TARGET_POWERPC64)
5606 extra = 12;
5607 else if (offset & 3)
5608 return false;
5609 break;
5611 default:
5612 break;
5615 offset += 0x8000;
5616 return offset < 0x10000 - extra;
5619 bool
5620 legitimate_indexed_address_p (rtx x, int strict)
5622 rtx op0, op1;
5624 if (GET_CODE (x) != PLUS)
5625 return false;
5627 op0 = XEXP (x, 0);
5628 op1 = XEXP (x, 1);
5630 /* Recognize the rtl generated by reload which we know will later be
5631 replaced with proper base and index regs. */
5632 if (!strict
5633 && reload_in_progress
5634 && (REG_P (op0) || GET_CODE (op0) == PLUS)
5635 && REG_P (op1))
5636 return true;
5638 return (REG_P (op0) && REG_P (op1)
5639 && ((INT_REG_OK_FOR_BASE_P (op0, strict)
5640 && INT_REG_OK_FOR_INDEX_P (op1, strict))
5641 || (INT_REG_OK_FOR_BASE_P (op1, strict)
5642 && INT_REG_OK_FOR_INDEX_P (op0, strict))));
5645 bool
5646 avoiding_indexed_address_p (enum machine_mode mode)
5648 /* Avoid indexed addressing for modes that have non-indexed
5649 load/store instruction forms. */
5650 return (TARGET_AVOID_XFORM && VECTOR_MEM_NONE_P (mode));
5653 bool
5654 legitimate_indirect_address_p (rtx x, int strict)
5656 return GET_CODE (x) == REG && INT_REG_OK_FOR_BASE_P (x, strict);
5659 bool
5660 macho_lo_sum_memory_operand (rtx x, enum machine_mode mode)
5662 if (!TARGET_MACHO || !flag_pic
5663 || mode != SImode || GET_CODE (x) != MEM)
5664 return false;
5665 x = XEXP (x, 0);
5667 if (GET_CODE (x) != LO_SUM)
5668 return false;
5669 if (GET_CODE (XEXP (x, 0)) != REG)
5670 return false;
5671 if (!INT_REG_OK_FOR_BASE_P (XEXP (x, 0), 0))
5672 return false;
5673 x = XEXP (x, 1);
5675 return CONSTANT_P (x);
5678 static bool
5679 legitimate_lo_sum_address_p (enum machine_mode mode, rtx x, int strict)
5681 if (GET_CODE (x) != LO_SUM)
5682 return false;
5683 if (GET_CODE (XEXP (x, 0)) != REG)
5684 return false;
5685 if (!INT_REG_OK_FOR_BASE_P (XEXP (x, 0), strict))
5686 return false;
5687 /* Restrict addressing for DI because of our SUBREG hackery. */
5688 if (TARGET_E500_DOUBLE && GET_MODE_SIZE (mode) > UNITS_PER_WORD)
5689 return false;
5690 x = XEXP (x, 1);
5692 if (TARGET_ELF || TARGET_MACHO)
5694 if (DEFAULT_ABI != ABI_AIX && DEFAULT_ABI != ABI_DARWIN && flag_pic)
5695 return false;
5696 if (TARGET_TOC)
5697 return false;
5698 if (GET_MODE_NUNITS (mode) != 1)
5699 return false;
5700 if (GET_MODE_SIZE (mode) > UNITS_PER_WORD
5701 && !(/* ??? Assume floating point reg based on mode? */
5702 TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT
5703 && (mode == DFmode || mode == DDmode)))
5704 return false;
5706 return CONSTANT_P (x);
5709 return false;
5713 /* Try machine-dependent ways of modifying an illegitimate address
5714 to be legitimate. If we find one, return the new, valid address.
5715 This is used from only one place: `memory_address' in explow.c.
5717 OLDX is the address as it was before break_out_memory_refs was
5718 called. In some cases it is useful to look at this to decide what
5719 needs to be done.
5721 It is always safe for this function to do nothing. It exists to
5722 recognize opportunities to optimize the output.
5724 On RS/6000, first check for the sum of a register with a constant
5725 integer that is out of range. If so, generate code to add the
5726 constant with the low-order 16 bits masked to the register and force
5727 this result into another register (this can be done with `cau').
5728 Then generate an address of REG+(CONST&0xffff), allowing for the
5729 possibility of bit 16 being a one.
5731 Then check for the sum of a register and something not constant, try to
5732 load the other things into a register and return the sum. */
5734 static rtx
5735 rs6000_legitimize_address (rtx x, rtx oldx ATTRIBUTE_UNUSED,
5736 enum machine_mode mode)
5738 unsigned int extra;
5740 if (!reg_offset_addressing_ok_p (mode))
5742 if (virtual_stack_registers_memory_p (x))
5743 return x;
5745 /* In theory we should not be seeing addresses of the form reg+0,
5746 but just in case it is generated, optimize it away. */
5747 if (GET_CODE (x) == PLUS && XEXP (x, 1) == const0_rtx)
5748 return force_reg (Pmode, XEXP (x, 0));
5750 /* Make sure both operands are registers. */
5751 else if (GET_CODE (x) == PLUS)
5752 return gen_rtx_PLUS (Pmode,
5753 force_reg (Pmode, XEXP (x, 0)),
5754 force_reg (Pmode, XEXP (x, 1)));
5755 else
5756 return force_reg (Pmode, x);
5758 if (GET_CODE (x) == SYMBOL_REF)
5760 enum tls_model model = SYMBOL_REF_TLS_MODEL (x);
5761 if (model != 0)
5762 return rs6000_legitimize_tls_address (x, model);
5765 extra = 0;
5766 switch (mode)
5768 case TFmode:
5769 case TDmode:
5770 case TImode:
5771 case PTImode:
5772 /* As in legitimate_offset_address_p we do not assume
5773 worst-case. The mode here is just a hint as to the registers
5774 used. A TImode is usually in gprs, but may actually be in
5775 fprs. Leave worst-case scenario for reload to handle via
5776 insn constraints. PTImode is only GPRs. */
5777 extra = 8;
5778 break;
5779 default:
5780 break;
5783 if (GET_CODE (x) == PLUS
5784 && GET_CODE (XEXP (x, 0)) == REG
5785 && GET_CODE (XEXP (x, 1)) == CONST_INT
5786 && ((unsigned HOST_WIDE_INT) (INTVAL (XEXP (x, 1)) + 0x8000)
5787 >= 0x10000 - extra)
5788 && !(SPE_VECTOR_MODE (mode)
5789 || (TARGET_E500_DOUBLE && GET_MODE_SIZE (mode) > UNITS_PER_WORD)))
5791 HOST_WIDE_INT high_int, low_int;
5792 rtx sum;
5793 low_int = ((INTVAL (XEXP (x, 1)) & 0xffff) ^ 0x8000) - 0x8000;
5794 if (low_int >= 0x8000 - extra)
5795 low_int = 0;
5796 high_int = INTVAL (XEXP (x, 1)) - low_int;
5797 sum = force_operand (gen_rtx_PLUS (Pmode, XEXP (x, 0),
5798 GEN_INT (high_int)), 0);
5799 return plus_constant (Pmode, sum, low_int);
5801 else if (GET_CODE (x) == PLUS
5802 && GET_CODE (XEXP (x, 0)) == REG
5803 && GET_CODE (XEXP (x, 1)) != CONST_INT
5804 && GET_MODE_NUNITS (mode) == 1
5805 && (GET_MODE_SIZE (mode) <= UNITS_PER_WORD
5806 || (/* ??? Assume floating point reg based on mode? */
5807 (TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT)
5808 && (mode == DFmode || mode == DDmode)))
5809 && !avoiding_indexed_address_p (mode))
5811 return gen_rtx_PLUS (Pmode, XEXP (x, 0),
5812 force_reg (Pmode, force_operand (XEXP (x, 1), 0)));
5814 else if (SPE_VECTOR_MODE (mode)
5815 || (TARGET_E500_DOUBLE && GET_MODE_SIZE (mode) > UNITS_PER_WORD))
5817 if (mode == DImode)
5818 return x;
5819 /* We accept [reg + reg] and [reg + OFFSET]. */
5821 if (GET_CODE (x) == PLUS)
5823 rtx op1 = XEXP (x, 0);
5824 rtx op2 = XEXP (x, 1);
5825 rtx y;
5827 op1 = force_reg (Pmode, op1);
5829 if (GET_CODE (op2) != REG
5830 && (GET_CODE (op2) != CONST_INT
5831 || !SPE_CONST_OFFSET_OK (INTVAL (op2))
5832 || (GET_MODE_SIZE (mode) > 8
5833 && !SPE_CONST_OFFSET_OK (INTVAL (op2) + 8))))
5834 op2 = force_reg (Pmode, op2);
5836 /* We can't always do [reg + reg] for these, because [reg +
5837 reg + offset] is not a legitimate addressing mode. */
5838 y = gen_rtx_PLUS (Pmode, op1, op2);
5840 if ((GET_MODE_SIZE (mode) > 8 || mode == DDmode) && REG_P (op2))
5841 return force_reg (Pmode, y);
5842 else
5843 return y;
5846 return force_reg (Pmode, x);
5848 else if ((TARGET_ELF
5849 #if TARGET_MACHO
5850 || !MACHO_DYNAMIC_NO_PIC_P
5851 #endif
5853 && TARGET_32BIT
5854 && TARGET_NO_TOC
5855 && ! flag_pic
5856 && GET_CODE (x) != CONST_INT
5857 && GET_CODE (x) != CONST_DOUBLE
5858 && CONSTANT_P (x)
5859 && GET_MODE_NUNITS (mode) == 1
5860 && (GET_MODE_SIZE (mode) <= UNITS_PER_WORD
5861 || (/* ??? Assume floating point reg based on mode? */
5862 (TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT)
5863 && (mode == DFmode || mode == DDmode))))
5865 rtx reg = gen_reg_rtx (Pmode);
5866 if (TARGET_ELF)
5867 emit_insn (gen_elf_high (reg, x));
5868 else
5869 emit_insn (gen_macho_high (reg, x));
5870 return gen_rtx_LO_SUM (Pmode, reg, x);
5872 else if (TARGET_TOC
5873 && GET_CODE (x) == SYMBOL_REF
5874 && constant_pool_expr_p (x)
5875 && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (x), Pmode))
5876 return create_TOC_reference (x, NULL_RTX);
5877 else
5878 return x;
5881 /* Debug version of rs6000_legitimize_address. */
5882 static rtx
5883 rs6000_debug_legitimize_address (rtx x, rtx oldx, enum machine_mode mode)
5885 rtx ret;
5886 rtx insns;
5888 start_sequence ();
5889 ret = rs6000_legitimize_address (x, oldx, mode);
5890 insns = get_insns ();
5891 end_sequence ();
5893 if (ret != x)
5895 fprintf (stderr,
5896 "\nrs6000_legitimize_address: mode %s, old code %s, "
5897 "new code %s, modified\n",
5898 GET_MODE_NAME (mode), GET_RTX_NAME (GET_CODE (x)),
5899 GET_RTX_NAME (GET_CODE (ret)));
5901 fprintf (stderr, "Original address:\n");
5902 debug_rtx (x);
5904 fprintf (stderr, "oldx:\n");
5905 debug_rtx (oldx);
5907 fprintf (stderr, "New address:\n");
5908 debug_rtx (ret);
5910 if (insns)
5912 fprintf (stderr, "Insns added:\n");
5913 debug_rtx_list (insns, 20);
5916 else
5918 fprintf (stderr,
5919 "\nrs6000_legitimize_address: mode %s, code %s, no change:\n",
5920 GET_MODE_NAME (mode), GET_RTX_NAME (GET_CODE (x)));
5922 debug_rtx (x);
5925 if (insns)
5926 emit_insn (insns);
5928 return ret;
5931 /* This is called from dwarf2out.c via TARGET_ASM_OUTPUT_DWARF_DTPREL.
5932 We need to emit DTP-relative relocations. */
5934 static void rs6000_output_dwarf_dtprel (FILE *, int, rtx) ATTRIBUTE_UNUSED;
5935 static void
5936 rs6000_output_dwarf_dtprel (FILE *file, int size, rtx x)
5938 switch (size)
5940 case 4:
5941 fputs ("\t.long\t", file);
5942 break;
5943 case 8:
5944 fputs (DOUBLE_INT_ASM_OP, file);
5945 break;
5946 default:
5947 gcc_unreachable ();
5949 output_addr_const (file, x);
5950 fputs ("@dtprel+0x8000", file);
5953 /* In the name of slightly smaller debug output, and to cater to
5954 general assembler lossage, recognize various UNSPEC sequences
5955 and turn them back into a direct symbol reference. */
5957 static rtx
5958 rs6000_delegitimize_address (rtx orig_x)
5960 rtx x, y, offset;
5962 orig_x = delegitimize_mem_from_attrs (orig_x);
5963 x = orig_x;
5964 if (MEM_P (x))
5965 x = XEXP (x, 0);
5967 y = x;
5968 if (TARGET_CMODEL != CMODEL_SMALL
5969 && GET_CODE (y) == LO_SUM)
5970 y = XEXP (y, 1);
5972 offset = NULL_RTX;
5973 if (GET_CODE (y) == PLUS
5974 && GET_MODE (y) == Pmode
5975 && CONST_INT_P (XEXP (y, 1)))
5977 offset = XEXP (y, 1);
5978 y = XEXP (y, 0);
5981 if (GET_CODE (y) == UNSPEC
5982 && XINT (y, 1) == UNSPEC_TOCREL)
5984 #ifdef ENABLE_CHECKING
5985 if (REG_P (XVECEXP (y, 0, 1))
5986 && REGNO (XVECEXP (y, 0, 1)) == TOC_REGISTER)
5988 /* All good. */
5990 else if (GET_CODE (XVECEXP (y, 0, 1)) == DEBUG_EXPR)
5992 /* Weirdness alert. df_note_compute can replace r2 with a
5993 debug_expr when this unspec is in a debug_insn.
5994 Seen in gcc.dg/pr51957-1.c */
5996 else
5998 debug_rtx (orig_x);
5999 abort ();
6001 #endif
6002 y = XVECEXP (y, 0, 0);
6004 #ifdef HAVE_AS_TLS
6005 /* Do not associate thread-local symbols with the original
6006 constant pool symbol. */
6007 if (TARGET_XCOFF
6008 && GET_CODE (y) == SYMBOL_REF
6009 && CONSTANT_POOL_ADDRESS_P (y)
6010 && SYMBOL_REF_TLS_MODEL (get_pool_constant (y)) >= TLS_MODEL_REAL)
6011 return orig_x;
6012 #endif
6014 if (offset != NULL_RTX)
6015 y = gen_rtx_PLUS (Pmode, y, offset);
6016 if (!MEM_P (orig_x))
6017 return y;
6018 else
6019 return replace_equiv_address_nv (orig_x, y);
6022 if (TARGET_MACHO
6023 && GET_CODE (orig_x) == LO_SUM
6024 && GET_CODE (XEXP (orig_x, 1)) == CONST)
6026 y = XEXP (XEXP (orig_x, 1), 0);
6027 if (GET_CODE (y) == UNSPEC
6028 && XINT (y, 1) == UNSPEC_MACHOPIC_OFFSET)
6029 return XVECEXP (y, 0, 0);
6032 return orig_x;
6035 /* Return true if X shouldn't be emitted into the debug info.
6036 The linker doesn't like .toc section references from
6037 .debug_* sections, so reject .toc section symbols. */
6039 static bool
6040 rs6000_const_not_ok_for_debug_p (rtx x)
6042 if (GET_CODE (x) == SYMBOL_REF
6043 && CONSTANT_POOL_ADDRESS_P (x))
6045 rtx c = get_pool_constant (x);
6046 enum machine_mode cmode = get_pool_mode (x);
6047 if (ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (c, cmode))
6048 return true;
6051 return false;
6054 /* Construct the SYMBOL_REF for the tls_get_addr function. */
6056 static GTY(()) rtx rs6000_tls_symbol;
6057 static rtx
6058 rs6000_tls_get_addr (void)
6060 if (!rs6000_tls_symbol)
6061 rs6000_tls_symbol = init_one_libfunc ("__tls_get_addr");
6063 return rs6000_tls_symbol;
6066 /* Construct the SYMBOL_REF for TLS GOT references. */
6068 static GTY(()) rtx rs6000_got_symbol;
6069 static rtx
6070 rs6000_got_sym (void)
6072 if (!rs6000_got_symbol)
6074 rs6000_got_symbol = gen_rtx_SYMBOL_REF (Pmode, "_GLOBAL_OFFSET_TABLE_");
6075 SYMBOL_REF_FLAGS (rs6000_got_symbol) |= SYMBOL_FLAG_LOCAL;
6076 SYMBOL_REF_FLAGS (rs6000_got_symbol) |= SYMBOL_FLAG_EXTERNAL;
6079 return rs6000_got_symbol;
6082 /* AIX Thread-Local Address support. */
6084 static rtx
6085 rs6000_legitimize_tls_address_aix (rtx addr, enum tls_model model)
6087 rtx sym, mem, tocref, tlsreg, tmpreg, dest, tlsaddr;
6088 const char *name;
6089 char *tlsname;
6091 name = XSTR (addr, 0);
6092 /* Append TLS CSECT qualifier, unless the symbol already is qualified
6093 or the symbol will be in TLS private data section. */
6094 if (name[strlen (name) - 1] != ']'
6095 && (TREE_PUBLIC (SYMBOL_REF_DECL (addr))
6096 || bss_initializer_p (SYMBOL_REF_DECL (addr))))
6098 tlsname = XALLOCAVEC (char, strlen (name) + 4);
6099 strcpy (tlsname, name);
6100 strcat (tlsname,
6101 bss_initializer_p (SYMBOL_REF_DECL (addr)) ? "[UL]" : "[TL]");
6102 tlsaddr = copy_rtx (addr);
6103 XSTR (tlsaddr, 0) = ggc_strdup (tlsname);
6105 else
6106 tlsaddr = addr;
6108 /* Place addr into TOC constant pool. */
6109 sym = force_const_mem (GET_MODE (tlsaddr), tlsaddr);
6111 /* Output the TOC entry and create the MEM referencing the value. */
6112 if (constant_pool_expr_p (XEXP (sym, 0))
6113 && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (XEXP (sym, 0)), Pmode))
6115 tocref = create_TOC_reference (XEXP (sym, 0), NULL_RTX);
6116 mem = gen_const_mem (Pmode, tocref);
6117 set_mem_alias_set (mem, get_TOC_alias_set ());
6119 else
6120 return sym;
6122 /* Use global-dynamic for local-dynamic. */
6123 if (model == TLS_MODEL_GLOBAL_DYNAMIC
6124 || model == TLS_MODEL_LOCAL_DYNAMIC)
6126 /* Create new TOC reference for @m symbol. */
6127 name = XSTR (XVECEXP (XEXP (mem, 0), 0, 0), 0);
6128 tlsname = XALLOCAVEC (char, strlen (name) + 1);
6129 strcpy (tlsname, "*LCM");
6130 strcat (tlsname, name + 3);
6131 rtx modaddr = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (tlsname));
6132 SYMBOL_REF_FLAGS (modaddr) |= SYMBOL_FLAG_LOCAL;
6133 tocref = create_TOC_reference (modaddr, NULL_RTX);
6134 rtx modmem = gen_const_mem (Pmode, tocref);
6135 set_mem_alias_set (modmem, get_TOC_alias_set ());
6137 rtx modreg = gen_reg_rtx (Pmode);
6138 emit_insn (gen_rtx_SET (VOIDmode, modreg, modmem));
6140 tmpreg = gen_reg_rtx (Pmode);
6141 emit_insn (gen_rtx_SET (VOIDmode, tmpreg, mem));
6143 dest = gen_reg_rtx (Pmode);
6144 if (TARGET_32BIT)
6145 emit_insn (gen_tls_get_addrsi (dest, modreg, tmpreg));
6146 else
6147 emit_insn (gen_tls_get_addrdi (dest, modreg, tmpreg));
6148 return dest;
6150 /* Obtain TLS pointer: 32 bit call or 64 bit GPR 13. */
6151 else if (TARGET_32BIT)
6153 tlsreg = gen_reg_rtx (SImode);
6154 emit_insn (gen_tls_get_tpointer (tlsreg));
6156 else
6157 tlsreg = gen_rtx_REG (DImode, 13);
6159 /* Load the TOC value into temporary register. */
6160 tmpreg = gen_reg_rtx (Pmode);
6161 emit_insn (gen_rtx_SET (VOIDmode, tmpreg, mem));
6162 set_unique_reg_note (get_last_insn (), REG_EQUAL,
6163 gen_rtx_MINUS (Pmode, addr, tlsreg));
6165 /* Add TOC symbol value to TLS pointer. */
6166 dest = force_reg (Pmode, gen_rtx_PLUS (Pmode, tmpreg, tlsreg));
6168 return dest;
6171 /* ADDR contains a thread-local SYMBOL_REF. Generate code to compute
6172 this (thread-local) address. */
6174 static rtx
6175 rs6000_legitimize_tls_address (rtx addr, enum tls_model model)
6177 rtx dest, insn;
6179 if (TARGET_XCOFF)
6180 return rs6000_legitimize_tls_address_aix (addr, model);
6182 dest = gen_reg_rtx (Pmode);
6183 if (model == TLS_MODEL_LOCAL_EXEC && rs6000_tls_size == 16)
6185 rtx tlsreg;
6187 if (TARGET_64BIT)
6189 tlsreg = gen_rtx_REG (Pmode, 13);
6190 insn = gen_tls_tprel_64 (dest, tlsreg, addr);
6192 else
6194 tlsreg = gen_rtx_REG (Pmode, 2);
6195 insn = gen_tls_tprel_32 (dest, tlsreg, addr);
6197 emit_insn (insn);
6199 else if (model == TLS_MODEL_LOCAL_EXEC && rs6000_tls_size == 32)
6201 rtx tlsreg, tmp;
6203 tmp = gen_reg_rtx (Pmode);
6204 if (TARGET_64BIT)
6206 tlsreg = gen_rtx_REG (Pmode, 13);
6207 insn = gen_tls_tprel_ha_64 (tmp, tlsreg, addr);
6209 else
6211 tlsreg = gen_rtx_REG (Pmode, 2);
6212 insn = gen_tls_tprel_ha_32 (tmp, tlsreg, addr);
6214 emit_insn (insn);
6215 if (TARGET_64BIT)
6216 insn = gen_tls_tprel_lo_64 (dest, tmp, addr);
6217 else
6218 insn = gen_tls_tprel_lo_32 (dest, tmp, addr);
6219 emit_insn (insn);
6221 else
6223 rtx r3, got, tga, tmp1, tmp2, call_insn;
6225 /* We currently use relocations like @got@tlsgd for tls, which
6226 means the linker will handle allocation of tls entries, placing
6227 them in the .got section. So use a pointer to the .got section,
6228 not one to secondary TOC sections used by 64-bit -mminimal-toc,
6229 or to secondary GOT sections used by 32-bit -fPIC. */
6230 if (TARGET_64BIT)
6231 got = gen_rtx_REG (Pmode, 2);
6232 else
6234 if (flag_pic == 1)
6235 got = gen_rtx_REG (Pmode, RS6000_PIC_OFFSET_TABLE_REGNUM);
6236 else
6238 rtx gsym = rs6000_got_sym ();
6239 got = gen_reg_rtx (Pmode);
6240 if (flag_pic == 0)
6241 rs6000_emit_move (got, gsym, Pmode);
6242 else
6244 rtx mem, lab, last;
6246 tmp1 = gen_reg_rtx (Pmode);
6247 tmp2 = gen_reg_rtx (Pmode);
6248 mem = gen_const_mem (Pmode, tmp1);
6249 lab = gen_label_rtx ();
6250 emit_insn (gen_load_toc_v4_PIC_1b (gsym, lab));
6251 emit_move_insn (tmp1, gen_rtx_REG (Pmode, LR_REGNO));
6252 if (TARGET_LINK_STACK)
6253 emit_insn (gen_addsi3 (tmp1, tmp1, GEN_INT (4)));
6254 emit_move_insn (tmp2, mem);
6255 last = emit_insn (gen_addsi3 (got, tmp1, tmp2));
6256 set_unique_reg_note (last, REG_EQUAL, gsym);
6261 if (model == TLS_MODEL_GLOBAL_DYNAMIC)
6263 tga = rs6000_tls_get_addr ();
6264 emit_library_call_value (tga, dest, LCT_CONST, Pmode,
6265 1, const0_rtx, Pmode);
6267 r3 = gen_rtx_REG (Pmode, 3);
6268 if (DEFAULT_ABI == ABI_AIX && TARGET_64BIT)
6269 insn = gen_tls_gd_aix64 (r3, got, addr, tga, const0_rtx);
6270 else if (DEFAULT_ABI == ABI_AIX && !TARGET_64BIT)
6271 insn = gen_tls_gd_aix32 (r3, got, addr, tga, const0_rtx);
6272 else if (DEFAULT_ABI == ABI_V4)
6273 insn = gen_tls_gd_sysvsi (r3, got, addr, tga, const0_rtx);
6274 else
6275 gcc_unreachable ();
6276 call_insn = last_call_insn ();
6277 PATTERN (call_insn) = insn;
6278 if (DEFAULT_ABI == ABI_V4 && TARGET_SECURE_PLT && flag_pic)
6279 use_reg (&CALL_INSN_FUNCTION_USAGE (call_insn),
6280 pic_offset_table_rtx);
6282 else if (model == TLS_MODEL_LOCAL_DYNAMIC)
6284 tga = rs6000_tls_get_addr ();
6285 tmp1 = gen_reg_rtx (Pmode);
6286 emit_library_call_value (tga, tmp1, LCT_CONST, Pmode,
6287 1, const0_rtx, Pmode);
6289 r3 = gen_rtx_REG (Pmode, 3);
6290 if (DEFAULT_ABI == ABI_AIX && TARGET_64BIT)
6291 insn = gen_tls_ld_aix64 (r3, got, tga, const0_rtx);
6292 else if (DEFAULT_ABI == ABI_AIX && !TARGET_64BIT)
6293 insn = gen_tls_ld_aix32 (r3, got, tga, const0_rtx);
6294 else if (DEFAULT_ABI == ABI_V4)
6295 insn = gen_tls_ld_sysvsi (r3, got, tga, const0_rtx);
6296 else
6297 gcc_unreachable ();
6298 call_insn = last_call_insn ();
6299 PATTERN (call_insn) = insn;
6300 if (DEFAULT_ABI == ABI_V4 && TARGET_SECURE_PLT && flag_pic)
6301 use_reg (&CALL_INSN_FUNCTION_USAGE (call_insn),
6302 pic_offset_table_rtx);
6304 if (rs6000_tls_size == 16)
6306 if (TARGET_64BIT)
6307 insn = gen_tls_dtprel_64 (dest, tmp1, addr);
6308 else
6309 insn = gen_tls_dtprel_32 (dest, tmp1, addr);
6311 else if (rs6000_tls_size == 32)
6313 tmp2 = gen_reg_rtx (Pmode);
6314 if (TARGET_64BIT)
6315 insn = gen_tls_dtprel_ha_64 (tmp2, tmp1, addr);
6316 else
6317 insn = gen_tls_dtprel_ha_32 (tmp2, tmp1, addr);
6318 emit_insn (insn);
6319 if (TARGET_64BIT)
6320 insn = gen_tls_dtprel_lo_64 (dest, tmp2, addr);
6321 else
6322 insn = gen_tls_dtprel_lo_32 (dest, tmp2, addr);
6324 else
6326 tmp2 = gen_reg_rtx (Pmode);
6327 if (TARGET_64BIT)
6328 insn = gen_tls_got_dtprel_64 (tmp2, got, addr);
6329 else
6330 insn = gen_tls_got_dtprel_32 (tmp2, got, addr);
6331 emit_insn (insn);
6332 insn = gen_rtx_SET (Pmode, dest,
6333 gen_rtx_PLUS (Pmode, tmp2, tmp1));
6335 emit_insn (insn);
6337 else
6339 /* IE, or 64-bit offset LE. */
6340 tmp2 = gen_reg_rtx (Pmode);
6341 if (TARGET_64BIT)
6342 insn = gen_tls_got_tprel_64 (tmp2, got, addr);
6343 else
6344 insn = gen_tls_got_tprel_32 (tmp2, got, addr);
6345 emit_insn (insn);
6346 if (TARGET_64BIT)
6347 insn = gen_tls_tls_64 (dest, tmp2, addr);
6348 else
6349 insn = gen_tls_tls_32 (dest, tmp2, addr);
6350 emit_insn (insn);
6354 return dest;
6357 /* Return 1 if X contains a thread-local symbol. */
6359 static bool
6360 rs6000_tls_referenced_p (rtx x)
6362 if (! TARGET_HAVE_TLS)
6363 return false;
6365 return for_each_rtx (&x, &rs6000_tls_symbol_ref_1, 0);
6368 /* Implement TARGET_CANNOT_FORCE_CONST_MEM. */
6370 static bool
6371 rs6000_cannot_force_const_mem (enum machine_mode mode ATTRIBUTE_UNUSED, rtx x)
6373 if (GET_CODE (x) == HIGH
6374 && GET_CODE (XEXP (x, 0)) == UNSPEC)
6375 return true;
6377 /* A TLS symbol in the TOC cannot contain a sum. */
6378 if (GET_CODE (x) == CONST
6379 && GET_CODE (XEXP (x, 0)) == PLUS
6380 && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF
6381 && SYMBOL_REF_TLS_MODEL (XEXP (XEXP (x, 0), 0)) != 0)
6382 return true;
6384 /* Do not place an ELF TLS symbol in the constant pool. */
6385 return TARGET_ELF && rs6000_tls_referenced_p (x);
6388 /* Return 1 if *X is a thread-local symbol. This is the same as
6389 rs6000_tls_symbol_ref except for the type of the unused argument. */
6391 static int
6392 rs6000_tls_symbol_ref_1 (rtx *x, void *data ATTRIBUTE_UNUSED)
6394 return RS6000_SYMBOL_REF_TLS_P (*x);
6397 /* Return true iff the given SYMBOL_REF refers to a constant pool entry
6398 that we have put in the TOC, or for cmodel=medium, if the SYMBOL_REF
6399 can be addressed relative to the toc pointer. */
6401 static bool
6402 use_toc_relative_ref (rtx sym)
6404 return ((constant_pool_expr_p (sym)
6405 && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (sym),
6406 get_pool_mode (sym)))
6407 || (TARGET_CMODEL == CMODEL_MEDIUM
6408 && !CONSTANT_POOL_ADDRESS_P (sym)
6409 && SYMBOL_REF_LOCAL_P (sym)));
6412 /* Our implementation of LEGITIMIZE_RELOAD_ADDRESS. Returns a value to
6413 replace the input X, or the original X if no replacement is called for.
6414 The output parameter *WIN is 1 if the calling macro should goto WIN,
6415 0 if it should not.
6417 For RS/6000, we wish to handle large displacements off a base
6418 register by splitting the addend across an addiu/addis and the mem insn.
6419 This cuts number of extra insns needed from 3 to 1.
6421 On Darwin, we use this to generate code for floating point constants.
6422 A movsf_low is generated so we wind up with 2 instructions rather than 3.
6423 The Darwin code is inside #if TARGET_MACHO because only then are the
6424 machopic_* functions defined. */
6425 static rtx
6426 rs6000_legitimize_reload_address (rtx x, enum machine_mode mode,
6427 int opnum, int type,
6428 int ind_levels ATTRIBUTE_UNUSED, int *win)
6430 bool reg_offset_p = reg_offset_addressing_ok_p (mode);
6432 /* Nasty hack for vsx_splat_V2DF/V2DI load from mem, which takes a
6433 DFmode/DImode MEM. */
6434 if (reg_offset_p
6435 && opnum == 1
6436 && ((mode == DFmode && recog_data.operand_mode[0] == V2DFmode)
6437 || (mode == DImode && recog_data.operand_mode[0] == V2DImode)))
6438 reg_offset_p = false;
6440 /* We must recognize output that we have already generated ourselves. */
6441 if (GET_CODE (x) == PLUS
6442 && GET_CODE (XEXP (x, 0)) == PLUS
6443 && GET_CODE (XEXP (XEXP (x, 0), 0)) == REG
6444 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6445 && GET_CODE (XEXP (x, 1)) == CONST_INT)
6447 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
6448 BASE_REG_CLASS, GET_MODE (x), VOIDmode, 0, 0,
6449 opnum, (enum reload_type) type);
6450 *win = 1;
6451 return x;
6454 /* Likewise for (lo_sum (high ...) ...) output we have generated. */
6455 if (GET_CODE (x) == LO_SUM
6456 && GET_CODE (XEXP (x, 0)) == HIGH)
6458 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
6459 BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
6460 opnum, (enum reload_type) type);
6461 *win = 1;
6462 return x;
6465 #if TARGET_MACHO
6466 if (DEFAULT_ABI == ABI_DARWIN && flag_pic
6467 && GET_CODE (x) == LO_SUM
6468 && GET_CODE (XEXP (x, 0)) == PLUS
6469 && XEXP (XEXP (x, 0), 0) == pic_offset_table_rtx
6470 && GET_CODE (XEXP (XEXP (x, 0), 1)) == HIGH
6471 && XEXP (XEXP (XEXP (x, 0), 1), 0) == XEXP (x, 1)
6472 && machopic_operand_p (XEXP (x, 1)))
6474 /* Result of previous invocation of this function on Darwin
6475 floating point constant. */
6476 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
6477 BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
6478 opnum, (enum reload_type) type);
6479 *win = 1;
6480 return x;
6482 #endif
6484 if (TARGET_CMODEL != CMODEL_SMALL
6485 && reg_offset_p
6486 && small_toc_ref (x, VOIDmode))
6488 rtx hi = gen_rtx_HIGH (Pmode, copy_rtx (x));
6489 x = gen_rtx_LO_SUM (Pmode, hi, x);
6490 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
6491 BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
6492 opnum, (enum reload_type) type);
6493 *win = 1;
6494 return x;
6497 if (GET_CODE (x) == PLUS
6498 && GET_CODE (XEXP (x, 0)) == REG
6499 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER
6500 && INT_REG_OK_FOR_BASE_P (XEXP (x, 0), 1)
6501 && GET_CODE (XEXP (x, 1)) == CONST_INT
6502 && reg_offset_p
6503 && !SPE_VECTOR_MODE (mode)
6504 && !(TARGET_E500_DOUBLE && (mode == DFmode || mode == TFmode
6505 || mode == DDmode || mode == TDmode
6506 || mode == DImode))
6507 && (!VECTOR_MODE_P (mode) || VECTOR_MEM_NONE_P (mode)))
6509 HOST_WIDE_INT val = INTVAL (XEXP (x, 1));
6510 HOST_WIDE_INT low = ((val & 0xffff) ^ 0x8000) - 0x8000;
6511 HOST_WIDE_INT high
6512 = (((val - low) & 0xffffffff) ^ 0x80000000) - 0x80000000;
6514 /* Check for 32-bit overflow. */
6515 if (high + low != val)
6517 *win = 0;
6518 return x;
6521 /* Reload the high part into a base reg; leave the low part
6522 in the mem directly. */
6524 x = gen_rtx_PLUS (GET_MODE (x),
6525 gen_rtx_PLUS (GET_MODE (x), XEXP (x, 0),
6526 GEN_INT (high)),
6527 GEN_INT (low));
6529 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
6530 BASE_REG_CLASS, GET_MODE (x), VOIDmode, 0, 0,
6531 opnum, (enum reload_type) type);
6532 *win = 1;
6533 return x;
6536 if (GET_CODE (x) == SYMBOL_REF
6537 && reg_offset_p
6538 && (!VECTOR_MODE_P (mode) || VECTOR_MEM_NONE_P (mode))
6539 && !SPE_VECTOR_MODE (mode)
6540 #if TARGET_MACHO
6541 && DEFAULT_ABI == ABI_DARWIN
6542 && (flag_pic || MACHO_DYNAMIC_NO_PIC_P)
6543 && machopic_symbol_defined_p (x)
6544 #else
6545 && DEFAULT_ABI == ABI_V4
6546 && !flag_pic
6547 #endif
6548 /* Don't do this for TFmode or TDmode, since the result isn't offsettable.
6549 The same goes for DImode without 64-bit gprs and DFmode and DDmode
6550 without fprs.
6551 ??? Assume floating point reg based on mode? This assumption is
6552 violated by eg. powerpc-linux -m32 compile of gcc.dg/pr28796-2.c
6553 where reload ends up doing a DFmode load of a constant from
6554 mem using two gprs. Unfortunately, at this point reload
6555 hasn't yet selected regs so poking around in reload data
6556 won't help and even if we could figure out the regs reliably,
6557 we'd still want to allow this transformation when the mem is
6558 naturally aligned. Since we say the address is good here, we
6559 can't disable offsets from LO_SUMs in mem_operand_gpr.
6560 FIXME: Allow offset from lo_sum for other modes too, when
6561 mem is sufficiently aligned. */
6562 && mode != TFmode
6563 && mode != TDmode
6564 && (mode != TImode || !TARGET_VSX_TIMODE)
6565 && mode != PTImode
6566 && (mode != DImode || TARGET_POWERPC64)
6567 && ((mode != DFmode && mode != DDmode) || TARGET_POWERPC64
6568 || (TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT)))
6570 #if TARGET_MACHO
6571 if (flag_pic)
6573 rtx offset = machopic_gen_offset (x);
6574 x = gen_rtx_LO_SUM (GET_MODE (x),
6575 gen_rtx_PLUS (Pmode, pic_offset_table_rtx,
6576 gen_rtx_HIGH (Pmode, offset)), offset);
6578 else
6579 #endif
6580 x = gen_rtx_LO_SUM (GET_MODE (x),
6581 gen_rtx_HIGH (Pmode, x), x);
6583 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
6584 BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
6585 opnum, (enum reload_type) type);
6586 *win = 1;
6587 return x;
6590 /* Reload an offset address wrapped by an AND that represents the
6591 masking of the lower bits. Strip the outer AND and let reload
6592 convert the offset address into an indirect address. For VSX,
6593 force reload to create the address with an AND in a separate
6594 register, because we can't guarantee an altivec register will
6595 be used. */
6596 if (VECTOR_MEM_ALTIVEC_P (mode)
6597 && GET_CODE (x) == AND
6598 && GET_CODE (XEXP (x, 0)) == PLUS
6599 && GET_CODE (XEXP (XEXP (x, 0), 0)) == REG
6600 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6601 && GET_CODE (XEXP (x, 1)) == CONST_INT
6602 && INTVAL (XEXP (x, 1)) == -16)
6604 x = XEXP (x, 0);
6605 *win = 1;
6606 return x;
6609 if (TARGET_TOC
6610 && reg_offset_p
6611 && GET_CODE (x) == SYMBOL_REF
6612 && use_toc_relative_ref (x))
6614 x = create_TOC_reference (x, NULL_RTX);
6615 if (TARGET_CMODEL != CMODEL_SMALL)
6616 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
6617 BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
6618 opnum, (enum reload_type) type);
6619 *win = 1;
6620 return x;
6622 *win = 0;
6623 return x;
6626 /* Debug version of rs6000_legitimize_reload_address. */
6627 static rtx
6628 rs6000_debug_legitimize_reload_address (rtx x, enum machine_mode mode,
6629 int opnum, int type,
6630 int ind_levels, int *win)
6632 rtx ret = rs6000_legitimize_reload_address (x, mode, opnum, type,
6633 ind_levels, win);
6634 fprintf (stderr,
6635 "\nrs6000_legitimize_reload_address: mode = %s, opnum = %d, "
6636 "type = %d, ind_levels = %d, win = %d, original addr:\n",
6637 GET_MODE_NAME (mode), opnum, type, ind_levels, *win);
6638 debug_rtx (x);
6640 if (x == ret)
6641 fprintf (stderr, "Same address returned\n");
6642 else if (!ret)
6643 fprintf (stderr, "NULL returned\n");
6644 else
6646 fprintf (stderr, "New address:\n");
6647 debug_rtx (ret);
6650 return ret;
6653 /* TARGET_LEGITIMATE_ADDRESS_P recognizes an RTL expression
6654 that is a valid memory address for an instruction.
6655 The MODE argument is the machine mode for the MEM expression
6656 that wants to use this address.
6658 On the RS/6000, there are four valid address: a SYMBOL_REF that
6659 refers to a constant pool entry of an address (or the sum of it
6660 plus a constant), a short (16-bit signed) constant plus a register,
6661 the sum of two registers, or a register indirect, possibly with an
6662 auto-increment. For DFmode, DDmode and DImode with a constant plus
6663 register, we must ensure that both words are addressable or PowerPC64
6664 with offset word aligned.
6666 For modes spanning multiple registers (DFmode and DDmode in 32-bit GPRs,
6667 32-bit DImode, TImode, TFmode, TDmode), indexed addressing cannot be used
6668 because adjacent memory cells are accessed by adding word-sized offsets
6669 during assembly output. */
6670 static bool
6671 rs6000_legitimate_address_p (enum machine_mode mode, rtx x, bool reg_ok_strict)
6673 bool reg_offset_p = reg_offset_addressing_ok_p (mode);
6675 /* If this is an unaligned stvx/ldvx type address, discard the outer AND. */
6676 if (VECTOR_MEM_ALTIVEC_P (mode)
6677 && GET_CODE (x) == AND
6678 && GET_CODE (XEXP (x, 1)) == CONST_INT
6679 && INTVAL (XEXP (x, 1)) == -16)
6680 x = XEXP (x, 0);
6682 if (TARGET_ELF && RS6000_SYMBOL_REF_TLS_P (x))
6683 return 0;
6684 if (legitimate_indirect_address_p (x, reg_ok_strict))
6685 return 1;
6686 if ((GET_CODE (x) == PRE_INC || GET_CODE (x) == PRE_DEC)
6687 && !ALTIVEC_OR_VSX_VECTOR_MODE (mode)
6688 && !SPE_VECTOR_MODE (mode)
6689 && mode != TFmode
6690 && mode != TDmode
6691 && mode != TImode
6692 && mode != PTImode
6693 /* Restrict addressing for DI because of our SUBREG hackery. */
6694 && !(TARGET_E500_DOUBLE
6695 && (mode == DFmode || mode == DDmode || mode == DImode))
6696 && TARGET_UPDATE
6697 && legitimate_indirect_address_p (XEXP (x, 0), reg_ok_strict))
6698 return 1;
6699 if (virtual_stack_registers_memory_p (x))
6700 return 1;
6701 if (reg_offset_p && legitimate_small_data_p (mode, x))
6702 return 1;
6703 if (reg_offset_p
6704 && legitimate_constant_pool_address_p (x, mode, reg_ok_strict))
6705 return 1;
6706 /* If not REG_OK_STRICT (before reload) let pass any stack offset. */
6707 if (! reg_ok_strict
6708 && reg_offset_p
6709 && GET_CODE (x) == PLUS
6710 && GET_CODE (XEXP (x, 0)) == REG
6711 && (XEXP (x, 0) == virtual_stack_vars_rtx
6712 || XEXP (x, 0) == arg_pointer_rtx)
6713 && GET_CODE (XEXP (x, 1)) == CONST_INT)
6714 return 1;
6715 if (rs6000_legitimate_offset_address_p (mode, x, reg_ok_strict, false))
6716 return 1;
6717 if (mode != TFmode
6718 && mode != TDmode
6719 && ((TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT)
6720 || TARGET_POWERPC64
6721 || (mode != DFmode && mode != DDmode)
6722 || (TARGET_E500_DOUBLE && mode != DDmode))
6723 && (TARGET_POWERPC64 || mode != DImode)
6724 && (mode != TImode || VECTOR_MEM_VSX_P (TImode))
6725 && mode != PTImode
6726 && !avoiding_indexed_address_p (mode)
6727 && legitimate_indexed_address_p (x, reg_ok_strict))
6728 return 1;
6729 if (GET_CODE (x) == PRE_MODIFY
6730 && mode != TImode
6731 && mode != PTImode
6732 && mode != TFmode
6733 && mode != TDmode
6734 && ((TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT)
6735 || TARGET_POWERPC64
6736 || ((mode != DFmode && mode != DDmode) || TARGET_E500_DOUBLE))
6737 && (TARGET_POWERPC64 || mode != DImode)
6738 && !ALTIVEC_OR_VSX_VECTOR_MODE (mode)
6739 && !SPE_VECTOR_MODE (mode)
6740 /* Restrict addressing for DI because of our SUBREG hackery. */
6741 && !(TARGET_E500_DOUBLE
6742 && (mode == DFmode || mode == DDmode || mode == DImode))
6743 && TARGET_UPDATE
6744 && legitimate_indirect_address_p (XEXP (x, 0), reg_ok_strict)
6745 && (rs6000_legitimate_offset_address_p (mode, XEXP (x, 1),
6746 reg_ok_strict, false)
6747 || (!avoiding_indexed_address_p (mode)
6748 && legitimate_indexed_address_p (XEXP (x, 1), reg_ok_strict)))
6749 && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
6750 return 1;
6751 if (reg_offset_p && legitimate_lo_sum_address_p (mode, x, reg_ok_strict))
6752 return 1;
6753 return 0;
6756 /* Debug version of rs6000_legitimate_address_p. */
6757 static bool
6758 rs6000_debug_legitimate_address_p (enum machine_mode mode, rtx x,
6759 bool reg_ok_strict)
6761 bool ret = rs6000_legitimate_address_p (mode, x, reg_ok_strict);
6762 fprintf (stderr,
6763 "\nrs6000_legitimate_address_p: return = %s, mode = %s, "
6764 "strict = %d, code = %s\n",
6765 ret ? "true" : "false",
6766 GET_MODE_NAME (mode),
6767 reg_ok_strict,
6768 GET_RTX_NAME (GET_CODE (x)));
6769 debug_rtx (x);
6771 return ret;
6774 /* Implement TARGET_MODE_DEPENDENT_ADDRESS_P. */
6776 static bool
6777 rs6000_mode_dependent_address_p (const_rtx addr,
6778 addr_space_t as ATTRIBUTE_UNUSED)
6780 return rs6000_mode_dependent_address_ptr (addr);
6783 /* Go to LABEL if ADDR (a legitimate address expression)
6784 has an effect that depends on the machine mode it is used for.
6786 On the RS/6000 this is true of all integral offsets (since AltiVec
6787 and VSX modes don't allow them) or is a pre-increment or decrement.
6789 ??? Except that due to conceptual problems in offsettable_address_p
6790 we can't really report the problems of integral offsets. So leave
6791 this assuming that the adjustable offset must be valid for the
6792 sub-words of a TFmode operand, which is what we had before. */
6794 static bool
6795 rs6000_mode_dependent_address (const_rtx addr)
6797 switch (GET_CODE (addr))
6799 case PLUS:
6800 /* Any offset from virtual_stack_vars_rtx and arg_pointer_rtx
6801 is considered a legitimate address before reload, so there
6802 are no offset restrictions in that case. Note that this
6803 condition is safe in strict mode because any address involving
6804 virtual_stack_vars_rtx or arg_pointer_rtx would already have
6805 been rejected as illegitimate. */
6806 if (XEXP (addr, 0) != virtual_stack_vars_rtx
6807 && XEXP (addr, 0) != arg_pointer_rtx
6808 && GET_CODE (XEXP (addr, 1)) == CONST_INT)
6810 unsigned HOST_WIDE_INT val = INTVAL (XEXP (addr, 1));
6811 return val + 0x8000 >= 0x10000 - (TARGET_POWERPC64 ? 8 : 12);
6813 break;
6815 case LO_SUM:
6816 /* Anything in the constant pool is sufficiently aligned that
6817 all bytes have the same high part address. */
6818 return !legitimate_constant_pool_address_p (addr, QImode, false);
6820 /* Auto-increment cases are now treated generically in recog.c. */
6821 case PRE_MODIFY:
6822 return TARGET_UPDATE;
6824 /* AND is only allowed in Altivec loads. */
6825 case AND:
6826 return true;
6828 default:
6829 break;
6832 return false;
6835 /* Debug version of rs6000_mode_dependent_address. */
6836 static bool
6837 rs6000_debug_mode_dependent_address (const_rtx addr)
6839 bool ret = rs6000_mode_dependent_address (addr);
6841 fprintf (stderr, "\nrs6000_mode_dependent_address: ret = %s\n",
6842 ret ? "true" : "false");
6843 debug_rtx (addr);
6845 return ret;
6848 /* Implement FIND_BASE_TERM. */
6851 rs6000_find_base_term (rtx op)
6853 rtx base;
6855 base = op;
6856 if (GET_CODE (base) == CONST)
6857 base = XEXP (base, 0);
6858 if (GET_CODE (base) == PLUS)
6859 base = XEXP (base, 0);
6860 if (GET_CODE (base) == UNSPEC)
6861 switch (XINT (base, 1))
6863 case UNSPEC_TOCREL:
6864 case UNSPEC_MACHOPIC_OFFSET:
6865 /* OP represents SYM [+ OFFSET] - ANCHOR. SYM is the base term
6866 for aliasing purposes. */
6867 return XVECEXP (base, 0, 0);
6870 return op;
6873 /* More elaborate version of recog's offsettable_memref_p predicate
6874 that works around the ??? note of rs6000_mode_dependent_address.
6875 In particular it accepts
6877 (mem:DI (plus:SI (reg/f:SI 31 31) (const_int 32760 [0x7ff8])))
6879 in 32-bit mode, that the recog predicate rejects. */
6881 static bool
6882 rs6000_offsettable_memref_p (rtx op, enum machine_mode reg_mode)
6884 bool worst_case;
6886 if (!MEM_P (op))
6887 return false;
6889 /* First mimic offsettable_memref_p. */
6890 if (offsettable_address_p (true, GET_MODE (op), XEXP (op, 0)))
6891 return true;
6893 /* offsettable_address_p invokes rs6000_mode_dependent_address, but
6894 the latter predicate knows nothing about the mode of the memory
6895 reference and, therefore, assumes that it is the largest supported
6896 mode (TFmode). As a consequence, legitimate offsettable memory
6897 references are rejected. rs6000_legitimate_offset_address_p contains
6898 the correct logic for the PLUS case of rs6000_mode_dependent_address,
6899 at least with a little bit of help here given that we know the
6900 actual registers used. */
6901 worst_case = ((TARGET_POWERPC64 && GET_MODE_CLASS (reg_mode) == MODE_INT)
6902 || GET_MODE_SIZE (reg_mode) == 4);
6903 return rs6000_legitimate_offset_address_p (GET_MODE (op), XEXP (op, 0),
6904 true, worst_case);
6907 /* Change register usage conditional on target flags. */
6908 static void
6909 rs6000_conditional_register_usage (void)
6911 int i;
6913 if (TARGET_DEBUG_TARGET)
6914 fprintf (stderr, "rs6000_conditional_register_usage called\n");
6916 /* Set MQ register fixed (already call_used) so that it will not be
6917 allocated. */
6918 fixed_regs[64] = 1;
6920 /* 64-bit AIX and Linux reserve GPR13 for thread-private data. */
6921 if (TARGET_64BIT)
6922 fixed_regs[13] = call_used_regs[13]
6923 = call_really_used_regs[13] = 1;
6925 /* Conditionally disable FPRs. */
6926 if (TARGET_SOFT_FLOAT || !TARGET_FPRS)
6927 for (i = 32; i < 64; i++)
6928 fixed_regs[i] = call_used_regs[i]
6929 = call_really_used_regs[i] = 1;
6931 /* The TOC register is not killed across calls in a way that is
6932 visible to the compiler. */
6933 if (DEFAULT_ABI == ABI_AIX)
6934 call_really_used_regs[2] = 0;
6936 if (DEFAULT_ABI == ABI_V4
6937 && PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
6938 && flag_pic == 2)
6939 fixed_regs[RS6000_PIC_OFFSET_TABLE_REGNUM] = 1;
6941 if (DEFAULT_ABI == ABI_V4
6942 && PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
6943 && flag_pic == 1)
6944 fixed_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
6945 = call_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
6946 = call_really_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM] = 1;
6948 if (DEFAULT_ABI == ABI_DARWIN
6949 && PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM)
6950 fixed_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
6951 = call_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
6952 = call_really_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM] = 1;
6954 if (TARGET_TOC && TARGET_MINIMAL_TOC)
6955 fixed_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
6956 = call_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM] = 1;
6958 if (TARGET_SPE)
6960 global_regs[SPEFSCR_REGNO] = 1;
6961 /* We used to use r14 as FIXED_SCRATCH to address SPE 64-bit
6962 registers in prologues and epilogues. We no longer use r14
6963 for FIXED_SCRATCH, but we're keeping r14 out of the allocation
6964 pool for link-compatibility with older versions of GCC. Once
6965 "old" code has died out, we can return r14 to the allocation
6966 pool. */
6967 fixed_regs[14]
6968 = call_used_regs[14]
6969 = call_really_used_regs[14] = 1;
6972 if (!TARGET_ALTIVEC && !TARGET_VSX)
6974 for (i = FIRST_ALTIVEC_REGNO; i <= LAST_ALTIVEC_REGNO; ++i)
6975 fixed_regs[i] = call_used_regs[i] = call_really_used_regs[i] = 1;
6976 call_really_used_regs[VRSAVE_REGNO] = 1;
6979 if (TARGET_ALTIVEC || TARGET_VSX)
6980 global_regs[VSCR_REGNO] = 1;
6982 if (TARGET_ALTIVEC_ABI)
6984 for (i = FIRST_ALTIVEC_REGNO; i < FIRST_ALTIVEC_REGNO + 20; ++i)
6985 call_used_regs[i] = call_really_used_regs[i] = 1;
6987 /* AIX reserves VR20:31 in non-extended ABI mode. */
6988 if (TARGET_XCOFF)
6989 for (i = FIRST_ALTIVEC_REGNO + 20; i < FIRST_ALTIVEC_REGNO + 32; ++i)
6990 fixed_regs[i] = call_used_regs[i] = call_really_used_regs[i] = 1;
6994 /* Try to output insns to set TARGET equal to the constant C if it can
6995 be done in less than N insns. Do all computations in MODE.
6996 Returns the place where the output has been placed if it can be
6997 done and the insns have been emitted. If it would take more than N
6998 insns, zero is returned and no insns and emitted. */
7001 rs6000_emit_set_const (rtx dest, enum machine_mode mode,
7002 rtx source, int n ATTRIBUTE_UNUSED)
7004 rtx result, insn, set;
7005 HOST_WIDE_INT c0, c1;
7007 switch (mode)
7009 case QImode:
7010 case HImode:
7011 if (dest == NULL)
7012 dest = gen_reg_rtx (mode);
7013 emit_insn (gen_rtx_SET (VOIDmode, dest, source));
7014 return dest;
7016 case SImode:
7017 result = !can_create_pseudo_p () ? dest : gen_reg_rtx (SImode);
7019 emit_insn (gen_rtx_SET (VOIDmode, copy_rtx (result),
7020 GEN_INT (INTVAL (source)
7021 & (~ (HOST_WIDE_INT) 0xffff))));
7022 emit_insn (gen_rtx_SET (VOIDmode, dest,
7023 gen_rtx_IOR (SImode, copy_rtx (result),
7024 GEN_INT (INTVAL (source) & 0xffff))));
7025 result = dest;
7026 break;
7028 case DImode:
7029 switch (GET_CODE (source))
7031 case CONST_INT:
7032 c0 = INTVAL (source);
7033 c1 = -(c0 < 0);
7034 break;
7036 default:
7037 gcc_unreachable ();
7040 result = rs6000_emit_set_long_const (dest, c0, c1);
7041 break;
7043 default:
7044 gcc_unreachable ();
7047 insn = get_last_insn ();
7048 set = single_set (insn);
7049 if (! CONSTANT_P (SET_SRC (set)))
7050 set_unique_reg_note (insn, REG_EQUAL, source);
7052 return result;
7055 /* Having failed to find a 3 insn sequence in rs6000_emit_set_const,
7056 fall back to a straight forward decomposition. We do this to avoid
7057 exponential run times encountered when looking for longer sequences
7058 with rs6000_emit_set_const. */
7059 static rtx
7060 rs6000_emit_set_long_const (rtx dest, HOST_WIDE_INT c1, HOST_WIDE_INT c2)
7062 if (!TARGET_POWERPC64)
7064 rtx operand1, operand2;
7066 operand1 = operand_subword_force (dest, WORDS_BIG_ENDIAN == 0,
7067 DImode);
7068 operand2 = operand_subword_force (copy_rtx (dest), WORDS_BIG_ENDIAN != 0,
7069 DImode);
7070 emit_move_insn (operand1, GEN_INT (c1));
7071 emit_move_insn (operand2, GEN_INT (c2));
7073 else
7075 HOST_WIDE_INT ud1, ud2, ud3, ud4;
7077 ud1 = c1 & 0xffff;
7078 ud2 = (c1 & 0xffff0000) >> 16;
7079 c2 = c1 >> 32;
7080 ud3 = c2 & 0xffff;
7081 ud4 = (c2 & 0xffff0000) >> 16;
7083 if ((ud4 == 0xffff && ud3 == 0xffff && ud2 == 0xffff && (ud1 & 0x8000))
7084 || (ud4 == 0 && ud3 == 0 && ud2 == 0 && ! (ud1 & 0x8000)))
7085 emit_move_insn (dest, GEN_INT ((ud1 ^ 0x8000) - 0x8000));
7087 else if ((ud4 == 0xffff && ud3 == 0xffff && (ud2 & 0x8000))
7088 || (ud4 == 0 && ud3 == 0 && ! (ud2 & 0x8000)))
7090 emit_move_insn (dest, GEN_INT (((ud2 << 16) ^ 0x80000000)
7091 - 0x80000000));
7092 if (ud1 != 0)
7093 emit_move_insn (copy_rtx (dest),
7094 gen_rtx_IOR (DImode, copy_rtx (dest),
7095 GEN_INT (ud1)));
7097 else if (ud3 == 0 && ud4 == 0)
7099 gcc_assert (ud2 & 0x8000);
7100 emit_move_insn (dest, GEN_INT (((ud2 << 16) ^ 0x80000000)
7101 - 0x80000000));
7102 if (ud1 != 0)
7103 emit_move_insn (copy_rtx (dest),
7104 gen_rtx_IOR (DImode, copy_rtx (dest),
7105 GEN_INT (ud1)));
7106 emit_move_insn (copy_rtx (dest),
7107 gen_rtx_ZERO_EXTEND (DImode,
7108 gen_lowpart (SImode,
7109 copy_rtx (dest))));
7111 else if ((ud4 == 0xffff && (ud3 & 0x8000))
7112 || (ud4 == 0 && ! (ud3 & 0x8000)))
7114 emit_move_insn (dest, GEN_INT (((ud3 << 16) ^ 0x80000000)
7115 - 0x80000000));
7116 if (ud2 != 0)
7117 emit_move_insn (copy_rtx (dest),
7118 gen_rtx_IOR (DImode, copy_rtx (dest),
7119 GEN_INT (ud2)));
7120 emit_move_insn (copy_rtx (dest),
7121 gen_rtx_ASHIFT (DImode, copy_rtx (dest),
7122 GEN_INT (16)));
7123 if (ud1 != 0)
7124 emit_move_insn (copy_rtx (dest),
7125 gen_rtx_IOR (DImode, copy_rtx (dest),
7126 GEN_INT (ud1)));
7128 else
7130 emit_move_insn (dest, GEN_INT (((ud4 << 16) ^ 0x80000000)
7131 - 0x80000000));
7132 if (ud3 != 0)
7133 emit_move_insn (copy_rtx (dest),
7134 gen_rtx_IOR (DImode, copy_rtx (dest),
7135 GEN_INT (ud3)));
7137 emit_move_insn (copy_rtx (dest),
7138 gen_rtx_ASHIFT (DImode, copy_rtx (dest),
7139 GEN_INT (32)));
7140 if (ud2 != 0)
7141 emit_move_insn (copy_rtx (dest),
7142 gen_rtx_IOR (DImode, copy_rtx (dest),
7143 GEN_INT (ud2 << 16)));
7144 if (ud1 != 0)
7145 emit_move_insn (copy_rtx (dest),
7146 gen_rtx_IOR (DImode, copy_rtx (dest),
7147 GEN_INT (ud1)));
7150 return dest;
7153 /* Helper for the following. Get rid of [r+r] memory refs
7154 in cases where it won't work (TImode, TFmode, TDmode, PTImode). */
7156 static void
7157 rs6000_eliminate_indexed_memrefs (rtx operands[2])
7159 if (reload_in_progress)
7160 return;
7162 if (GET_CODE (operands[0]) == MEM
7163 && GET_CODE (XEXP (operands[0], 0)) != REG
7164 && ! legitimate_constant_pool_address_p (XEXP (operands[0], 0),
7165 GET_MODE (operands[0]), false))
7166 operands[0]
7167 = replace_equiv_address (operands[0],
7168 copy_addr_to_reg (XEXP (operands[0], 0)));
7170 if (GET_CODE (operands[1]) == MEM
7171 && GET_CODE (XEXP (operands[1], 0)) != REG
7172 && ! legitimate_constant_pool_address_p (XEXP (operands[1], 0),
7173 GET_MODE (operands[1]), false))
7174 operands[1]
7175 = replace_equiv_address (operands[1],
7176 copy_addr_to_reg (XEXP (operands[1], 0)));
7179 /* Emit a move from SOURCE to DEST in mode MODE. */
7180 void
7181 rs6000_emit_move (rtx dest, rtx source, enum machine_mode mode)
7183 rtx operands[2];
7184 operands[0] = dest;
7185 operands[1] = source;
7187 if (TARGET_DEBUG_ADDR)
7189 fprintf (stderr,
7190 "\nrs6000_emit_move: mode = %s, reload_in_progress = %d, "
7191 "reload_completed = %d, can_create_pseudos = %d.\ndest:\n",
7192 GET_MODE_NAME (mode),
7193 reload_in_progress,
7194 reload_completed,
7195 can_create_pseudo_p ());
7196 debug_rtx (dest);
7197 fprintf (stderr, "source:\n");
7198 debug_rtx (source);
7201 /* Sanity checks. Check that we get CONST_DOUBLE only when we should. */
7202 if (GET_CODE (operands[1]) == CONST_DOUBLE
7203 && ! FLOAT_MODE_P (mode)
7204 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
7206 /* FIXME. This should never happen. */
7207 /* Since it seems that it does, do the safe thing and convert
7208 to a CONST_INT. */
7209 operands[1] = gen_int_mode (CONST_DOUBLE_LOW (operands[1]), mode);
7211 gcc_assert (GET_CODE (operands[1]) != CONST_DOUBLE
7212 || FLOAT_MODE_P (mode)
7213 || ((CONST_DOUBLE_HIGH (operands[1]) != 0
7214 || CONST_DOUBLE_LOW (operands[1]) < 0)
7215 && (CONST_DOUBLE_HIGH (operands[1]) != -1
7216 || CONST_DOUBLE_LOW (operands[1]) >= 0)));
7218 /* Check if GCC is setting up a block move that will end up using FP
7219 registers as temporaries. We must make sure this is acceptable. */
7220 if (GET_CODE (operands[0]) == MEM
7221 && GET_CODE (operands[1]) == MEM
7222 && mode == DImode
7223 && (SLOW_UNALIGNED_ACCESS (DImode, MEM_ALIGN (operands[0]))
7224 || SLOW_UNALIGNED_ACCESS (DImode, MEM_ALIGN (operands[1])))
7225 && ! (SLOW_UNALIGNED_ACCESS (SImode, (MEM_ALIGN (operands[0]) > 32
7226 ? 32 : MEM_ALIGN (operands[0])))
7227 || SLOW_UNALIGNED_ACCESS (SImode, (MEM_ALIGN (operands[1]) > 32
7228 ? 32
7229 : MEM_ALIGN (operands[1]))))
7230 && ! MEM_VOLATILE_P (operands [0])
7231 && ! MEM_VOLATILE_P (operands [1]))
7233 emit_move_insn (adjust_address (operands[0], SImode, 0),
7234 adjust_address (operands[1], SImode, 0));
7235 emit_move_insn (adjust_address (copy_rtx (operands[0]), SImode, 4),
7236 adjust_address (copy_rtx (operands[1]), SImode, 4));
7237 return;
7240 if (can_create_pseudo_p () && GET_CODE (operands[0]) == MEM
7241 && !gpc_reg_operand (operands[1], mode))
7242 operands[1] = force_reg (mode, operands[1]);
7244 /* Recognize the case where operand[1] is a reference to thread-local
7245 data and load its address to a register. */
7246 if (rs6000_tls_referenced_p (operands[1]))
7248 enum tls_model model;
7249 rtx tmp = operands[1];
7250 rtx addend = NULL;
7252 if (GET_CODE (tmp) == CONST && GET_CODE (XEXP (tmp, 0)) == PLUS)
7254 addend = XEXP (XEXP (tmp, 0), 1);
7255 tmp = XEXP (XEXP (tmp, 0), 0);
7258 gcc_assert (GET_CODE (tmp) == SYMBOL_REF);
7259 model = SYMBOL_REF_TLS_MODEL (tmp);
7260 gcc_assert (model != 0);
7262 tmp = rs6000_legitimize_tls_address (tmp, model);
7263 if (addend)
7265 tmp = gen_rtx_PLUS (mode, tmp, addend);
7266 tmp = force_operand (tmp, operands[0]);
7268 operands[1] = tmp;
7271 /* Handle the case where reload calls us with an invalid address. */
7272 if (reload_in_progress && mode == Pmode
7273 && (! general_operand (operands[1], mode)
7274 || ! nonimmediate_operand (operands[0], mode)))
7275 goto emit_set;
7277 /* 128-bit constant floating-point values on Darwin should really be
7278 loaded as two parts. */
7279 if (!TARGET_IEEEQUAD && TARGET_LONG_DOUBLE_128
7280 && mode == TFmode && GET_CODE (operands[1]) == CONST_DOUBLE)
7282 rs6000_emit_move (simplify_gen_subreg (DFmode, operands[0], mode, 0),
7283 simplify_gen_subreg (DFmode, operands[1], mode, 0),
7284 DFmode);
7285 rs6000_emit_move (simplify_gen_subreg (DFmode, operands[0], mode,
7286 GET_MODE_SIZE (DFmode)),
7287 simplify_gen_subreg (DFmode, operands[1], mode,
7288 GET_MODE_SIZE (DFmode)),
7289 DFmode);
7290 return;
7293 if (reload_in_progress && cfun->machine->sdmode_stack_slot != NULL_RTX)
7294 cfun->machine->sdmode_stack_slot =
7295 eliminate_regs (cfun->machine->sdmode_stack_slot, VOIDmode, NULL_RTX);
7297 if (reload_in_progress
7298 && mode == SDmode
7299 && cfun->machine->sdmode_stack_slot != NULL_RTX
7300 && MEM_P (operands[0])
7301 && rtx_equal_p (operands[0], cfun->machine->sdmode_stack_slot)
7302 && REG_P (operands[1]))
7304 if (FP_REGNO_P (REGNO (operands[1])))
7306 rtx mem = adjust_address_nv (operands[0], DDmode, 0);
7307 mem = eliminate_regs (mem, VOIDmode, NULL_RTX);
7308 emit_insn (gen_movsd_store (mem, operands[1]));
7310 else if (INT_REGNO_P (REGNO (operands[1])))
7312 rtx mem = adjust_address_nv (operands[0], mode, 4);
7313 mem = eliminate_regs (mem, VOIDmode, NULL_RTX);
7314 emit_insn (gen_movsd_hardfloat (mem, operands[1]));
7316 else
7317 gcc_unreachable();
7318 return;
7320 if (reload_in_progress
7321 && mode == SDmode
7322 && REG_P (operands[0])
7323 && MEM_P (operands[1])
7324 && cfun->machine->sdmode_stack_slot != NULL_RTX
7325 && rtx_equal_p (operands[1], cfun->machine->sdmode_stack_slot))
7327 if (FP_REGNO_P (REGNO (operands[0])))
7329 rtx mem = adjust_address_nv (operands[1], DDmode, 0);
7330 mem = eliminate_regs (mem, VOIDmode, NULL_RTX);
7331 emit_insn (gen_movsd_load (operands[0], mem));
7333 else if (INT_REGNO_P (REGNO (operands[0])))
7335 rtx mem = adjust_address_nv (operands[1], mode, 4);
7336 mem = eliminate_regs (mem, VOIDmode, NULL_RTX);
7337 emit_insn (gen_movsd_hardfloat (operands[0], mem));
7339 else
7340 gcc_unreachable();
7341 return;
7344 /* FIXME: In the long term, this switch statement should go away
7345 and be replaced by a sequence of tests based on things like
7346 mode == Pmode. */
7347 switch (mode)
7349 case HImode:
7350 case QImode:
7351 if (CONSTANT_P (operands[1])
7352 && GET_CODE (operands[1]) != CONST_INT)
7353 operands[1] = force_const_mem (mode, operands[1]);
7354 break;
7356 case TFmode:
7357 case TDmode:
7358 rs6000_eliminate_indexed_memrefs (operands);
7359 /* fall through */
7361 case DFmode:
7362 case DDmode:
7363 case SFmode:
7364 case SDmode:
7365 if (CONSTANT_P (operands[1])
7366 && ! easy_fp_constant (operands[1], mode))
7367 operands[1] = force_const_mem (mode, operands[1]);
7368 break;
7370 case V16QImode:
7371 case V8HImode:
7372 case V4SFmode:
7373 case V4SImode:
7374 case V4HImode:
7375 case V2SFmode:
7376 case V2SImode:
7377 case V1DImode:
7378 case V2DFmode:
7379 case V2DImode:
7380 if (CONSTANT_P (operands[1])
7381 && !easy_vector_constant (operands[1], mode))
7382 operands[1] = force_const_mem (mode, operands[1]);
7383 break;
7385 case SImode:
7386 case DImode:
7387 /* Use default pattern for address of ELF small data */
7388 if (TARGET_ELF
7389 && mode == Pmode
7390 && DEFAULT_ABI == ABI_V4
7391 && (GET_CODE (operands[1]) == SYMBOL_REF
7392 || GET_CODE (operands[1]) == CONST)
7393 && small_data_operand (operands[1], mode))
7395 emit_insn (gen_rtx_SET (VOIDmode, operands[0], operands[1]));
7396 return;
7399 if (DEFAULT_ABI == ABI_V4
7400 && mode == Pmode && mode == SImode
7401 && flag_pic == 1 && got_operand (operands[1], mode))
7403 emit_insn (gen_movsi_got (operands[0], operands[1]));
7404 return;
7407 if ((TARGET_ELF || DEFAULT_ABI == ABI_DARWIN)
7408 && TARGET_NO_TOC
7409 && ! flag_pic
7410 && mode == Pmode
7411 && CONSTANT_P (operands[1])
7412 && GET_CODE (operands[1]) != HIGH
7413 && GET_CODE (operands[1]) != CONST_INT)
7415 rtx target = (!can_create_pseudo_p ()
7416 ? operands[0]
7417 : gen_reg_rtx (mode));
7419 /* If this is a function address on -mcall-aixdesc,
7420 convert it to the address of the descriptor. */
7421 if (DEFAULT_ABI == ABI_AIX
7422 && GET_CODE (operands[1]) == SYMBOL_REF
7423 && XSTR (operands[1], 0)[0] == '.')
7425 const char *name = XSTR (operands[1], 0);
7426 rtx new_ref;
7427 while (*name == '.')
7428 name++;
7429 new_ref = gen_rtx_SYMBOL_REF (Pmode, name);
7430 CONSTANT_POOL_ADDRESS_P (new_ref)
7431 = CONSTANT_POOL_ADDRESS_P (operands[1]);
7432 SYMBOL_REF_FLAGS (new_ref) = SYMBOL_REF_FLAGS (operands[1]);
7433 SYMBOL_REF_USED (new_ref) = SYMBOL_REF_USED (operands[1]);
7434 SYMBOL_REF_DATA (new_ref) = SYMBOL_REF_DATA (operands[1]);
7435 operands[1] = new_ref;
7438 if (DEFAULT_ABI == ABI_DARWIN)
7440 #if TARGET_MACHO
7441 if (MACHO_DYNAMIC_NO_PIC_P)
7443 /* Take care of any required data indirection. */
7444 operands[1] = rs6000_machopic_legitimize_pic_address (
7445 operands[1], mode, operands[0]);
7446 if (operands[0] != operands[1])
7447 emit_insn (gen_rtx_SET (VOIDmode,
7448 operands[0], operands[1]));
7449 return;
7451 #endif
7452 emit_insn (gen_macho_high (target, operands[1]));
7453 emit_insn (gen_macho_low (operands[0], target, operands[1]));
7454 return;
7457 emit_insn (gen_elf_high (target, operands[1]));
7458 emit_insn (gen_elf_low (operands[0], target, operands[1]));
7459 return;
7462 /* If this is a SYMBOL_REF that refers to a constant pool entry,
7463 and we have put it in the TOC, we just need to make a TOC-relative
7464 reference to it. */
7465 if (TARGET_TOC
7466 && GET_CODE (operands[1]) == SYMBOL_REF
7467 && use_toc_relative_ref (operands[1]))
7468 operands[1] = create_TOC_reference (operands[1], operands[0]);
7469 else if (mode == Pmode
7470 && CONSTANT_P (operands[1])
7471 && GET_CODE (operands[1]) != HIGH
7472 && ((GET_CODE (operands[1]) != CONST_INT
7473 && ! easy_fp_constant (operands[1], mode))
7474 || (GET_CODE (operands[1]) == CONST_INT
7475 && (num_insns_constant (operands[1], mode)
7476 > (TARGET_CMODEL != CMODEL_SMALL ? 3 : 2)))
7477 || (GET_CODE (operands[0]) == REG
7478 && FP_REGNO_P (REGNO (operands[0]))))
7479 && !toc_relative_expr_p (operands[1], false)
7480 && (TARGET_CMODEL == CMODEL_SMALL
7481 || can_create_pseudo_p ()
7482 || (REG_P (operands[0])
7483 && INT_REG_OK_FOR_BASE_P (operands[0], true))))
7486 #if TARGET_MACHO
7487 /* Darwin uses a special PIC legitimizer. */
7488 if (DEFAULT_ABI == ABI_DARWIN && MACHOPIC_INDIRECT)
7490 operands[1] =
7491 rs6000_machopic_legitimize_pic_address (operands[1], mode,
7492 operands[0]);
7493 if (operands[0] != operands[1])
7494 emit_insn (gen_rtx_SET (VOIDmode, operands[0], operands[1]));
7495 return;
7497 #endif
7499 /* If we are to limit the number of things we put in the TOC and
7500 this is a symbol plus a constant we can add in one insn,
7501 just put the symbol in the TOC and add the constant. Don't do
7502 this if reload is in progress. */
7503 if (GET_CODE (operands[1]) == CONST
7504 && TARGET_NO_SUM_IN_TOC && ! reload_in_progress
7505 && GET_CODE (XEXP (operands[1], 0)) == PLUS
7506 && add_operand (XEXP (XEXP (operands[1], 0), 1), mode)
7507 && (GET_CODE (XEXP (XEXP (operands[1], 0), 0)) == LABEL_REF
7508 || GET_CODE (XEXP (XEXP (operands[1], 0), 0)) == SYMBOL_REF)
7509 && ! side_effects_p (operands[0]))
7511 rtx sym =
7512 force_const_mem (mode, XEXP (XEXP (operands[1], 0), 0));
7513 rtx other = XEXP (XEXP (operands[1], 0), 1);
7515 sym = force_reg (mode, sym);
7516 emit_insn (gen_add3_insn (operands[0], sym, other));
7517 return;
7520 operands[1] = force_const_mem (mode, operands[1]);
7522 if (TARGET_TOC
7523 && GET_CODE (XEXP (operands[1], 0)) == SYMBOL_REF
7524 && constant_pool_expr_p (XEXP (operands[1], 0))
7525 && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (
7526 get_pool_constant (XEXP (operands[1], 0)),
7527 get_pool_mode (XEXP (operands[1], 0))))
7529 rtx tocref = create_TOC_reference (XEXP (operands[1], 0),
7530 operands[0]);
7531 operands[1] = gen_const_mem (mode, tocref);
7532 set_mem_alias_set (operands[1], get_TOC_alias_set ());
7535 break;
7537 case TImode:
7538 if (!VECTOR_MEM_VSX_P (TImode))
7539 rs6000_eliminate_indexed_memrefs (operands);
7540 break;
7542 case PTImode:
7543 rs6000_eliminate_indexed_memrefs (operands);
7544 break;
7546 default:
7547 fatal_insn ("bad move", gen_rtx_SET (VOIDmode, dest, source));
7550 /* Above, we may have called force_const_mem which may have returned
7551 an invalid address. If we can, fix this up; otherwise, reload will
7552 have to deal with it. */
7553 if (GET_CODE (operands[1]) == MEM && ! reload_in_progress)
7554 operands[1] = validize_mem (operands[1]);
7556 emit_set:
7557 emit_insn (gen_rtx_SET (VOIDmode, operands[0], operands[1]));
7560 /* Return true if a structure, union or array containing FIELD should be
7561 accessed using `BLKMODE'.
7563 For the SPE, simd types are V2SI, and gcc can be tempted to put the
7564 entire thing in a DI and use subregs to access the internals.
7565 store_bit_field() will force (subreg:DI (reg:V2SI x))'s to the
7566 back-end. Because a single GPR can hold a V2SI, but not a DI, the
7567 best thing to do is set structs to BLKmode and avoid Severe Tire
7568 Damage.
7570 On e500 v2, DF and DI modes suffer from the same anomaly. DF can
7571 fit into 1, whereas DI still needs two. */
7573 static bool
7574 rs6000_member_type_forces_blk (const_tree field, enum machine_mode mode)
7576 return ((TARGET_SPE && TREE_CODE (TREE_TYPE (field)) == VECTOR_TYPE)
7577 || (TARGET_E500_DOUBLE && mode == DFmode));
7580 /* Nonzero if we can use a floating-point register to pass this arg. */
7581 #define USE_FP_FOR_ARG_P(CUM,MODE,TYPE) \
7582 (SCALAR_FLOAT_MODE_P (MODE) \
7583 && (CUM)->fregno <= FP_ARG_MAX_REG \
7584 && TARGET_HARD_FLOAT && TARGET_FPRS)
7586 /* Nonzero if we can use an AltiVec register to pass this arg. */
7587 #define USE_ALTIVEC_FOR_ARG_P(CUM,MODE,TYPE,NAMED) \
7588 (ALTIVEC_OR_VSX_VECTOR_MODE (MODE) \
7589 && (CUM)->vregno <= ALTIVEC_ARG_MAX_REG \
7590 && TARGET_ALTIVEC_ABI \
7591 && (NAMED))
7593 /* Return a nonzero value to say to return the function value in
7594 memory, just as large structures are always returned. TYPE will be
7595 the data type of the value, and FNTYPE will be the type of the
7596 function doing the returning, or @code{NULL} for libcalls.
7598 The AIX ABI for the RS/6000 specifies that all structures are
7599 returned in memory. The Darwin ABI does the same.
7601 For the Darwin 64 Bit ABI, a function result can be returned in
7602 registers or in memory, depending on the size of the return data
7603 type. If it is returned in registers, the value occupies the same
7604 registers as it would if it were the first and only function
7605 argument. Otherwise, the function places its result in memory at
7606 the location pointed to by GPR3.
7608 The SVR4 ABI specifies that structures <= 8 bytes are returned in r3/r4,
7609 but a draft put them in memory, and GCC used to implement the draft
7610 instead of the final standard. Therefore, aix_struct_return
7611 controls this instead of DEFAULT_ABI; V.4 targets needing backward
7612 compatibility can change DRAFT_V4_STRUCT_RET to override the
7613 default, and -m switches get the final word. See
7614 rs6000_option_override_internal for more details.
7616 The PPC32 SVR4 ABI uses IEEE double extended for long double, if 128-bit
7617 long double support is enabled. These values are returned in memory.
7619 int_size_in_bytes returns -1 for variable size objects, which go in
7620 memory always. The cast to unsigned makes -1 > 8. */
7622 static bool
7623 rs6000_return_in_memory (const_tree type, const_tree fntype ATTRIBUTE_UNUSED)
7625 /* For the Darwin64 ABI, test if we can fit the return value in regs. */
7626 if (TARGET_MACHO
7627 && rs6000_darwin64_abi
7628 && TREE_CODE (type) == RECORD_TYPE
7629 && int_size_in_bytes (type) > 0)
7631 CUMULATIVE_ARGS valcum;
7632 rtx valret;
7634 valcum.words = 0;
7635 valcum.fregno = FP_ARG_MIN_REG;
7636 valcum.vregno = ALTIVEC_ARG_MIN_REG;
7637 /* Do a trial code generation as if this were going to be passed
7638 as an argument; if any part goes in memory, we return NULL. */
7639 valret = rs6000_darwin64_record_arg (&valcum, type, true, true);
7640 if (valret)
7641 return false;
7642 /* Otherwise fall through to more conventional ABI rules. */
7645 if (AGGREGATE_TYPE_P (type)
7646 && (aix_struct_return
7647 || (unsigned HOST_WIDE_INT) int_size_in_bytes (type) > 8))
7648 return true;
7650 /* Allow -maltivec -mabi=no-altivec without warning. Altivec vector
7651 modes only exist for GCC vector types if -maltivec. */
7652 if (TARGET_32BIT && !TARGET_ALTIVEC_ABI
7653 && ALTIVEC_VECTOR_MODE (TYPE_MODE (type)))
7654 return false;
7656 /* Return synthetic vectors in memory. */
7657 if (TREE_CODE (type) == VECTOR_TYPE
7658 && int_size_in_bytes (type) > (TARGET_ALTIVEC_ABI ? 16 : 8))
7660 static bool warned_for_return_big_vectors = false;
7661 if (!warned_for_return_big_vectors)
7663 warning (0, "GCC vector returned by reference: "
7664 "non-standard ABI extension with no compatibility guarantee");
7665 warned_for_return_big_vectors = true;
7667 return true;
7670 if (DEFAULT_ABI == ABI_V4 && TARGET_IEEEQUAD && TYPE_MODE (type) == TFmode)
7671 return true;
7673 return false;
7676 #ifdef HAVE_AS_GNU_ATTRIBUTE
7677 /* Return TRUE if a call to function FNDECL may be one that
7678 potentially affects the function calling ABI of the object file. */
7680 static bool
7681 call_ABI_of_interest (tree fndecl)
7683 if (cgraph_state == CGRAPH_STATE_EXPANSION)
7685 struct cgraph_node *c_node;
7687 /* Libcalls are always interesting. */
7688 if (fndecl == NULL_TREE)
7689 return true;
7691 /* Any call to an external function is interesting. */
7692 if (DECL_EXTERNAL (fndecl))
7693 return true;
7695 /* Interesting functions that we are emitting in this object file. */
7696 c_node = cgraph_get_node (fndecl);
7697 c_node = cgraph_function_or_thunk_node (c_node, NULL);
7698 return !cgraph_only_called_directly_p (c_node);
7700 return false;
7702 #endif
7704 /* Initialize a variable CUM of type CUMULATIVE_ARGS
7705 for a call to a function whose data type is FNTYPE.
7706 For a library call, FNTYPE is 0 and RETURN_MODE the return value mode.
7708 For incoming args we set the number of arguments in the prototype large
7709 so we never return a PARALLEL. */
7711 void
7712 init_cumulative_args (CUMULATIVE_ARGS *cum, tree fntype,
7713 rtx libname ATTRIBUTE_UNUSED, int incoming,
7714 int libcall, int n_named_args,
7715 tree fndecl ATTRIBUTE_UNUSED,
7716 enum machine_mode return_mode ATTRIBUTE_UNUSED)
7718 static CUMULATIVE_ARGS zero_cumulative;
7720 *cum = zero_cumulative;
7721 cum->words = 0;
7722 cum->fregno = FP_ARG_MIN_REG;
7723 cum->vregno = ALTIVEC_ARG_MIN_REG;
7724 cum->prototype = (fntype && prototype_p (fntype));
7725 cum->call_cookie = ((DEFAULT_ABI == ABI_V4 && libcall)
7726 ? CALL_LIBCALL : CALL_NORMAL);
7727 cum->sysv_gregno = GP_ARG_MIN_REG;
7728 cum->stdarg = stdarg_p (fntype);
7730 cum->nargs_prototype = 0;
7731 if (incoming || cum->prototype)
7732 cum->nargs_prototype = n_named_args;
7734 /* Check for a longcall attribute. */
7735 if ((!fntype && rs6000_default_long_calls)
7736 || (fntype
7737 && lookup_attribute ("longcall", TYPE_ATTRIBUTES (fntype))
7738 && !lookup_attribute ("shortcall", TYPE_ATTRIBUTES (fntype))))
7739 cum->call_cookie |= CALL_LONG;
7741 if (TARGET_DEBUG_ARG)
7743 fprintf (stderr, "\ninit_cumulative_args:");
7744 if (fntype)
7746 tree ret_type = TREE_TYPE (fntype);
7747 fprintf (stderr, " ret code = %s,",
7748 tree_code_name[ (int)TREE_CODE (ret_type) ]);
7751 if (cum->call_cookie & CALL_LONG)
7752 fprintf (stderr, " longcall,");
7754 fprintf (stderr, " proto = %d, nargs = %d\n",
7755 cum->prototype, cum->nargs_prototype);
7758 #ifdef HAVE_AS_GNU_ATTRIBUTE
7759 if (DEFAULT_ABI == ABI_V4)
7761 cum->escapes = call_ABI_of_interest (fndecl);
7762 if (cum->escapes)
7764 tree return_type;
7766 if (fntype)
7768 return_type = TREE_TYPE (fntype);
7769 return_mode = TYPE_MODE (return_type);
7771 else
7772 return_type = lang_hooks.types.type_for_mode (return_mode, 0);
7774 if (return_type != NULL)
7776 if (TREE_CODE (return_type) == RECORD_TYPE
7777 && TYPE_TRANSPARENT_AGGR (return_type))
7779 return_type = TREE_TYPE (first_field (return_type));
7780 return_mode = TYPE_MODE (return_type);
7782 if (AGGREGATE_TYPE_P (return_type)
7783 && ((unsigned HOST_WIDE_INT) int_size_in_bytes (return_type)
7784 <= 8))
7785 rs6000_returns_struct = true;
7787 if (SCALAR_FLOAT_MODE_P (return_mode))
7788 rs6000_passes_float = true;
7789 else if (ALTIVEC_OR_VSX_VECTOR_MODE (return_mode)
7790 || SPE_VECTOR_MODE (return_mode))
7791 rs6000_passes_vector = true;
7794 #endif
7796 if (fntype
7797 && !TARGET_ALTIVEC
7798 && TARGET_ALTIVEC_ABI
7799 && ALTIVEC_VECTOR_MODE (TYPE_MODE (TREE_TYPE (fntype))))
7801 error ("cannot return value in vector register because"
7802 " altivec instructions are disabled, use -maltivec"
7803 " to enable them");
7807 /* Return true if TYPE must be passed on the stack and not in registers. */
7809 static bool
7810 rs6000_must_pass_in_stack (enum machine_mode mode, const_tree type)
7812 if (DEFAULT_ABI == ABI_AIX || TARGET_64BIT)
7813 return must_pass_in_stack_var_size (mode, type);
7814 else
7815 return must_pass_in_stack_var_size_or_pad (mode, type);
7818 /* If defined, a C expression which determines whether, and in which
7819 direction, to pad out an argument with extra space. The value
7820 should be of type `enum direction': either `upward' to pad above
7821 the argument, `downward' to pad below, or `none' to inhibit
7822 padding.
7824 For the AIX ABI structs are always stored left shifted in their
7825 argument slot. */
7827 enum direction
7828 function_arg_padding (enum machine_mode mode, const_tree type)
7830 #ifndef AGGREGATE_PADDING_FIXED
7831 #define AGGREGATE_PADDING_FIXED 0
7832 #endif
7833 #ifndef AGGREGATES_PAD_UPWARD_ALWAYS
7834 #define AGGREGATES_PAD_UPWARD_ALWAYS 0
7835 #endif
7837 if (!AGGREGATE_PADDING_FIXED)
7839 /* GCC used to pass structures of the same size as integer types as
7840 if they were in fact integers, ignoring FUNCTION_ARG_PADDING.
7841 i.e. Structures of size 1 or 2 (or 4 when TARGET_64BIT) were
7842 passed padded downward, except that -mstrict-align further
7843 muddied the water in that multi-component structures of 2 and 4
7844 bytes in size were passed padded upward.
7846 The following arranges for best compatibility with previous
7847 versions of gcc, but removes the -mstrict-align dependency. */
7848 if (BYTES_BIG_ENDIAN)
7850 HOST_WIDE_INT size = 0;
7852 if (mode == BLKmode)
7854 if (type && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
7855 size = int_size_in_bytes (type);
7857 else
7858 size = GET_MODE_SIZE (mode);
7860 if (size == 1 || size == 2 || size == 4)
7861 return downward;
7863 return upward;
7866 if (AGGREGATES_PAD_UPWARD_ALWAYS)
7868 if (type != 0 && AGGREGATE_TYPE_P (type))
7869 return upward;
7872 /* Fall back to the default. */
7873 return DEFAULT_FUNCTION_ARG_PADDING (mode, type);
7876 /* If defined, a C expression that gives the alignment boundary, in bits,
7877 of an argument with the specified mode and type. If it is not defined,
7878 PARM_BOUNDARY is used for all arguments.
7880 V.4 wants long longs and doubles to be double word aligned. Just
7881 testing the mode size is a boneheaded way to do this as it means
7882 that other types such as complex int are also double word aligned.
7883 However, we're stuck with this because changing the ABI might break
7884 existing library interfaces.
7886 Doubleword align SPE vectors.
7887 Quadword align Altivec/VSX vectors.
7888 Quadword align large synthetic vector types. */
7890 static unsigned int
7891 rs6000_function_arg_boundary (enum machine_mode mode, const_tree type)
7893 if (DEFAULT_ABI == ABI_V4
7894 && (GET_MODE_SIZE (mode) == 8
7895 || (TARGET_HARD_FLOAT
7896 && TARGET_FPRS
7897 && (mode == TFmode || mode == TDmode))))
7898 return 64;
7899 else if (SPE_VECTOR_MODE (mode)
7900 || (type && TREE_CODE (type) == VECTOR_TYPE
7901 && int_size_in_bytes (type) >= 8
7902 && int_size_in_bytes (type) < 16))
7903 return 64;
7904 else if (ALTIVEC_OR_VSX_VECTOR_MODE (mode)
7905 || (type && TREE_CODE (type) == VECTOR_TYPE
7906 && int_size_in_bytes (type) >= 16))
7907 return 128;
7908 else if (TARGET_MACHO
7909 && rs6000_darwin64_abi
7910 && mode == BLKmode
7911 && type && TYPE_ALIGN (type) > 64)
7912 return 128;
7913 else
7914 return PARM_BOUNDARY;
7917 /* For a function parm of MODE and TYPE, return the starting word in
7918 the parameter area. NWORDS of the parameter area are already used. */
7920 static unsigned int
7921 rs6000_parm_start (enum machine_mode mode, const_tree type,
7922 unsigned int nwords)
7924 unsigned int align;
7925 unsigned int parm_offset;
7927 align = rs6000_function_arg_boundary (mode, type) / PARM_BOUNDARY - 1;
7928 parm_offset = DEFAULT_ABI == ABI_V4 ? 2 : 6;
7929 return nwords + (-(parm_offset + nwords) & align);
7932 /* Compute the size (in words) of a function argument. */
7934 static unsigned long
7935 rs6000_arg_size (enum machine_mode mode, const_tree type)
7937 unsigned long size;
7939 if (mode != BLKmode)
7940 size = GET_MODE_SIZE (mode);
7941 else
7942 size = int_size_in_bytes (type);
7944 if (TARGET_32BIT)
7945 return (size + 3) >> 2;
7946 else
7947 return (size + 7) >> 3;
7950 /* Use this to flush pending int fields. */
7952 static void
7953 rs6000_darwin64_record_arg_advance_flush (CUMULATIVE_ARGS *cum,
7954 HOST_WIDE_INT bitpos, int final)
7956 unsigned int startbit, endbit;
7957 int intregs, intoffset;
7958 enum machine_mode mode;
7960 /* Handle the situations where a float is taking up the first half
7961 of the GPR, and the other half is empty (typically due to
7962 alignment restrictions). We can detect this by a 8-byte-aligned
7963 int field, or by seeing that this is the final flush for this
7964 argument. Count the word and continue on. */
7965 if (cum->floats_in_gpr == 1
7966 && (cum->intoffset % 64 == 0
7967 || (cum->intoffset == -1 && final)))
7969 cum->words++;
7970 cum->floats_in_gpr = 0;
7973 if (cum->intoffset == -1)
7974 return;
7976 intoffset = cum->intoffset;
7977 cum->intoffset = -1;
7978 cum->floats_in_gpr = 0;
7980 if (intoffset % BITS_PER_WORD != 0)
7982 mode = mode_for_size (BITS_PER_WORD - intoffset % BITS_PER_WORD,
7983 MODE_INT, 0);
7984 if (mode == BLKmode)
7986 /* We couldn't find an appropriate mode, which happens,
7987 e.g., in packed structs when there are 3 bytes to load.
7988 Back intoffset back to the beginning of the word in this
7989 case. */
7990 intoffset = intoffset & -BITS_PER_WORD;
7994 startbit = intoffset & -BITS_PER_WORD;
7995 endbit = (bitpos + BITS_PER_WORD - 1) & -BITS_PER_WORD;
7996 intregs = (endbit - startbit) / BITS_PER_WORD;
7997 cum->words += intregs;
7998 /* words should be unsigned. */
7999 if ((unsigned)cum->words < (endbit/BITS_PER_WORD))
8001 int pad = (endbit/BITS_PER_WORD) - cum->words;
8002 cum->words += pad;
8006 /* The darwin64 ABI calls for us to recurse down through structs,
8007 looking for elements passed in registers. Unfortunately, we have
8008 to track int register count here also because of misalignments
8009 in powerpc alignment mode. */
8011 static void
8012 rs6000_darwin64_record_arg_advance_recurse (CUMULATIVE_ARGS *cum,
8013 const_tree type,
8014 HOST_WIDE_INT startbitpos)
8016 tree f;
8018 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
8019 if (TREE_CODE (f) == FIELD_DECL)
8021 HOST_WIDE_INT bitpos = startbitpos;
8022 tree ftype = TREE_TYPE (f);
8023 enum machine_mode mode;
8024 if (ftype == error_mark_node)
8025 continue;
8026 mode = TYPE_MODE (ftype);
8028 if (DECL_SIZE (f) != 0
8029 && host_integerp (bit_position (f), 1))
8030 bitpos += int_bit_position (f);
8032 /* ??? FIXME: else assume zero offset. */
8034 if (TREE_CODE (ftype) == RECORD_TYPE)
8035 rs6000_darwin64_record_arg_advance_recurse (cum, ftype, bitpos);
8036 else if (USE_FP_FOR_ARG_P (cum, mode, ftype))
8038 unsigned n_fpregs = (GET_MODE_SIZE (mode) + 7) >> 3;
8039 rs6000_darwin64_record_arg_advance_flush (cum, bitpos, 0);
8040 cum->fregno += n_fpregs;
8041 /* Single-precision floats present a special problem for
8042 us, because they are smaller than an 8-byte GPR, and so
8043 the structure-packing rules combined with the standard
8044 varargs behavior mean that we want to pack float/float
8045 and float/int combinations into a single register's
8046 space. This is complicated by the arg advance flushing,
8047 which works on arbitrarily large groups of int-type
8048 fields. */
8049 if (mode == SFmode)
8051 if (cum->floats_in_gpr == 1)
8053 /* Two floats in a word; count the word and reset
8054 the float count. */
8055 cum->words++;
8056 cum->floats_in_gpr = 0;
8058 else if (bitpos % 64 == 0)
8060 /* A float at the beginning of an 8-byte word;
8061 count it and put off adjusting cum->words until
8062 we see if a arg advance flush is going to do it
8063 for us. */
8064 cum->floats_in_gpr++;
8066 else
8068 /* The float is at the end of a word, preceded
8069 by integer fields, so the arg advance flush
8070 just above has already set cum->words and
8071 everything is taken care of. */
8074 else
8075 cum->words += n_fpregs;
8077 else if (USE_ALTIVEC_FOR_ARG_P (cum, mode, type, 1))
8079 rs6000_darwin64_record_arg_advance_flush (cum, bitpos, 0);
8080 cum->vregno++;
8081 cum->words += 2;
8083 else if (cum->intoffset == -1)
8084 cum->intoffset = bitpos;
8088 /* Check for an item that needs to be considered specially under the darwin 64
8089 bit ABI. These are record types where the mode is BLK or the structure is
8090 8 bytes in size. */
8091 static int
8092 rs6000_darwin64_struct_check_p (enum machine_mode mode, const_tree type)
8094 return rs6000_darwin64_abi
8095 && ((mode == BLKmode
8096 && TREE_CODE (type) == RECORD_TYPE
8097 && int_size_in_bytes (type) > 0)
8098 || (type && TREE_CODE (type) == RECORD_TYPE
8099 && int_size_in_bytes (type) == 8)) ? 1 : 0;
8102 /* Update the data in CUM to advance over an argument
8103 of mode MODE and data type TYPE.
8104 (TYPE is null for libcalls where that information may not be available.)
8106 Note that for args passed by reference, function_arg will be called
8107 with MODE and TYPE set to that of the pointer to the arg, not the arg
8108 itself. */
8110 static void
8111 rs6000_function_arg_advance_1 (CUMULATIVE_ARGS *cum, enum machine_mode mode,
8112 const_tree type, bool named, int depth)
8114 /* Only tick off an argument if we're not recursing. */
8115 if (depth == 0)
8116 cum->nargs_prototype--;
8118 #ifdef HAVE_AS_GNU_ATTRIBUTE
8119 if (DEFAULT_ABI == ABI_V4
8120 && cum->escapes)
8122 if (SCALAR_FLOAT_MODE_P (mode))
8123 rs6000_passes_float = true;
8124 else if (named && ALTIVEC_OR_VSX_VECTOR_MODE (mode))
8125 rs6000_passes_vector = true;
8126 else if (SPE_VECTOR_MODE (mode)
8127 && !cum->stdarg
8128 && cum->sysv_gregno <= GP_ARG_MAX_REG)
8129 rs6000_passes_vector = true;
8131 #endif
8133 if (TARGET_ALTIVEC_ABI
8134 && (ALTIVEC_OR_VSX_VECTOR_MODE (mode)
8135 || (type && TREE_CODE (type) == VECTOR_TYPE
8136 && int_size_in_bytes (type) == 16)))
8138 bool stack = false;
8140 if (USE_ALTIVEC_FOR_ARG_P (cum, mode, type, named))
8142 cum->vregno++;
8143 if (!TARGET_ALTIVEC)
8144 error ("cannot pass argument in vector register because"
8145 " altivec instructions are disabled, use -maltivec"
8146 " to enable them");
8148 /* PowerPC64 Linux and AIX allocate GPRs for a vector argument
8149 even if it is going to be passed in a vector register.
8150 Darwin does the same for variable-argument functions. */
8151 if ((DEFAULT_ABI == ABI_AIX && TARGET_64BIT)
8152 || (cum->stdarg && DEFAULT_ABI != ABI_V4))
8153 stack = true;
8155 else
8156 stack = true;
8158 if (stack)
8160 int align;
8162 /* Vector parameters must be 16-byte aligned. This places
8163 them at 2 mod 4 in terms of words in 32-bit mode, since
8164 the parameter save area starts at offset 24 from the
8165 stack. In 64-bit mode, they just have to start on an
8166 even word, since the parameter save area is 16-byte
8167 aligned. Space for GPRs is reserved even if the argument
8168 will be passed in memory. */
8169 if (TARGET_32BIT)
8170 align = (2 - cum->words) & 3;
8171 else
8172 align = cum->words & 1;
8173 cum->words += align + rs6000_arg_size (mode, type);
8175 if (TARGET_DEBUG_ARG)
8177 fprintf (stderr, "function_adv: words = %2d, align=%d, ",
8178 cum->words, align);
8179 fprintf (stderr, "nargs = %4d, proto = %d, mode = %4s\n",
8180 cum->nargs_prototype, cum->prototype,
8181 GET_MODE_NAME (mode));
8185 else if (TARGET_SPE_ABI && TARGET_SPE && SPE_VECTOR_MODE (mode)
8186 && !cum->stdarg
8187 && cum->sysv_gregno <= GP_ARG_MAX_REG)
8188 cum->sysv_gregno++;
8190 else if (TARGET_MACHO && rs6000_darwin64_struct_check_p (mode, type))
8192 int size = int_size_in_bytes (type);
8193 /* Variable sized types have size == -1 and are
8194 treated as if consisting entirely of ints.
8195 Pad to 16 byte boundary if needed. */
8196 if (TYPE_ALIGN (type) >= 2 * BITS_PER_WORD
8197 && (cum->words % 2) != 0)
8198 cum->words++;
8199 /* For varargs, we can just go up by the size of the struct. */
8200 if (!named)
8201 cum->words += (size + 7) / 8;
8202 else
8204 /* It is tempting to say int register count just goes up by
8205 sizeof(type)/8, but this is wrong in a case such as
8206 { int; double; int; } [powerpc alignment]. We have to
8207 grovel through the fields for these too. */
8208 cum->intoffset = 0;
8209 cum->floats_in_gpr = 0;
8210 rs6000_darwin64_record_arg_advance_recurse (cum, type, 0);
8211 rs6000_darwin64_record_arg_advance_flush (cum,
8212 size * BITS_PER_UNIT, 1);
8214 if (TARGET_DEBUG_ARG)
8216 fprintf (stderr, "function_adv: words = %2d, align=%d, size=%d",
8217 cum->words, TYPE_ALIGN (type), size);
8218 fprintf (stderr,
8219 "nargs = %4d, proto = %d, mode = %4s (darwin64 abi)\n",
8220 cum->nargs_prototype, cum->prototype,
8221 GET_MODE_NAME (mode));
8224 else if (DEFAULT_ABI == ABI_V4)
8226 if (TARGET_HARD_FLOAT && TARGET_FPRS
8227 && ((TARGET_SINGLE_FLOAT && mode == SFmode)
8228 || (TARGET_DOUBLE_FLOAT && mode == DFmode)
8229 || (mode == TFmode && !TARGET_IEEEQUAD)
8230 || mode == SDmode || mode == DDmode || mode == TDmode))
8232 /* _Decimal128 must use an even/odd register pair. This assumes
8233 that the register number is odd when fregno is odd. */
8234 if (mode == TDmode && (cum->fregno % 2) == 1)
8235 cum->fregno++;
8237 if (cum->fregno + (mode == TFmode || mode == TDmode ? 1 : 0)
8238 <= FP_ARG_V4_MAX_REG)
8239 cum->fregno += (GET_MODE_SIZE (mode) + 7) >> 3;
8240 else
8242 cum->fregno = FP_ARG_V4_MAX_REG + 1;
8243 if (mode == DFmode || mode == TFmode
8244 || mode == DDmode || mode == TDmode)
8245 cum->words += cum->words & 1;
8246 cum->words += rs6000_arg_size (mode, type);
8249 else
8251 int n_words = rs6000_arg_size (mode, type);
8252 int gregno = cum->sysv_gregno;
8254 /* Long long and SPE vectors are put in (r3,r4), (r5,r6),
8255 (r7,r8) or (r9,r10). As does any other 2 word item such
8256 as complex int due to a historical mistake. */
8257 if (n_words == 2)
8258 gregno += (1 - gregno) & 1;
8260 /* Multi-reg args are not split between registers and stack. */
8261 if (gregno + n_words - 1 > GP_ARG_MAX_REG)
8263 /* Long long and SPE vectors are aligned on the stack.
8264 So are other 2 word items such as complex int due to
8265 a historical mistake. */
8266 if (n_words == 2)
8267 cum->words += cum->words & 1;
8268 cum->words += n_words;
8271 /* Note: continuing to accumulate gregno past when we've started
8272 spilling to the stack indicates the fact that we've started
8273 spilling to the stack to expand_builtin_saveregs. */
8274 cum->sysv_gregno = gregno + n_words;
8277 if (TARGET_DEBUG_ARG)
8279 fprintf (stderr, "function_adv: words = %2d, fregno = %2d, ",
8280 cum->words, cum->fregno);
8281 fprintf (stderr, "gregno = %2d, nargs = %4d, proto = %d, ",
8282 cum->sysv_gregno, cum->nargs_prototype, cum->prototype);
8283 fprintf (stderr, "mode = %4s, named = %d\n",
8284 GET_MODE_NAME (mode), named);
8287 else
8289 int n_words = rs6000_arg_size (mode, type);
8290 int start_words = cum->words;
8291 int align_words = rs6000_parm_start (mode, type, start_words);
8293 cum->words = align_words + n_words;
8295 if (SCALAR_FLOAT_MODE_P (mode)
8296 && TARGET_HARD_FLOAT && TARGET_FPRS)
8298 /* _Decimal128 must be passed in an even/odd float register pair.
8299 This assumes that the register number is odd when fregno is
8300 odd. */
8301 if (mode == TDmode && (cum->fregno % 2) == 1)
8302 cum->fregno++;
8303 cum->fregno += (GET_MODE_SIZE (mode) + 7) >> 3;
8306 if (TARGET_DEBUG_ARG)
8308 fprintf (stderr, "function_adv: words = %2d, fregno = %2d, ",
8309 cum->words, cum->fregno);
8310 fprintf (stderr, "nargs = %4d, proto = %d, mode = %4s, ",
8311 cum->nargs_prototype, cum->prototype, GET_MODE_NAME (mode));
8312 fprintf (stderr, "named = %d, align = %d, depth = %d\n",
8313 named, align_words - start_words, depth);
8318 static void
8319 rs6000_function_arg_advance (cumulative_args_t cum, enum machine_mode mode,
8320 const_tree type, bool named)
8322 rs6000_function_arg_advance_1 (get_cumulative_args (cum), mode, type, named,
8326 static rtx
8327 spe_build_register_parallel (enum machine_mode mode, int gregno)
8329 rtx r1, r3, r5, r7;
8331 switch (mode)
8333 case DFmode:
8334 r1 = gen_rtx_REG (DImode, gregno);
8335 r1 = gen_rtx_EXPR_LIST (VOIDmode, r1, const0_rtx);
8336 return gen_rtx_PARALLEL (mode, gen_rtvec (1, r1));
8338 case DCmode:
8339 case TFmode:
8340 r1 = gen_rtx_REG (DImode, gregno);
8341 r1 = gen_rtx_EXPR_LIST (VOIDmode, r1, const0_rtx);
8342 r3 = gen_rtx_REG (DImode, gregno + 2);
8343 r3 = gen_rtx_EXPR_LIST (VOIDmode, r3, GEN_INT (8));
8344 return gen_rtx_PARALLEL (mode, gen_rtvec (2, r1, r3));
8346 case TCmode:
8347 r1 = gen_rtx_REG (DImode, gregno);
8348 r1 = gen_rtx_EXPR_LIST (VOIDmode, r1, const0_rtx);
8349 r3 = gen_rtx_REG (DImode, gregno + 2);
8350 r3 = gen_rtx_EXPR_LIST (VOIDmode, r3, GEN_INT (8));
8351 r5 = gen_rtx_REG (DImode, gregno + 4);
8352 r5 = gen_rtx_EXPR_LIST (VOIDmode, r5, GEN_INT (16));
8353 r7 = gen_rtx_REG (DImode, gregno + 6);
8354 r7 = gen_rtx_EXPR_LIST (VOIDmode, r7, GEN_INT (24));
8355 return gen_rtx_PARALLEL (mode, gen_rtvec (4, r1, r3, r5, r7));
8357 default:
8358 gcc_unreachable ();
8362 /* Determine where to put a SIMD argument on the SPE. */
8363 static rtx
8364 rs6000_spe_function_arg (const CUMULATIVE_ARGS *cum, enum machine_mode mode,
8365 const_tree type)
8367 int gregno = cum->sysv_gregno;
8369 /* On E500 v2, double arithmetic is done on the full 64-bit GPR, but
8370 are passed and returned in a pair of GPRs for ABI compatibility. */
8371 if (TARGET_E500_DOUBLE && (mode == DFmode || mode == TFmode
8372 || mode == DCmode || mode == TCmode))
8374 int n_words = rs6000_arg_size (mode, type);
8376 /* Doubles go in an odd/even register pair (r5/r6, etc). */
8377 if (mode == DFmode)
8378 gregno += (1 - gregno) & 1;
8380 /* Multi-reg args are not split between registers and stack. */
8381 if (gregno + n_words - 1 > GP_ARG_MAX_REG)
8382 return NULL_RTX;
8384 return spe_build_register_parallel (mode, gregno);
8386 if (cum->stdarg)
8388 int n_words = rs6000_arg_size (mode, type);
8390 /* SPE vectors are put in odd registers. */
8391 if (n_words == 2 && (gregno & 1) == 0)
8392 gregno += 1;
8394 if (gregno + n_words - 1 <= GP_ARG_MAX_REG)
8396 rtx r1, r2;
8397 enum machine_mode m = SImode;
8399 r1 = gen_rtx_REG (m, gregno);
8400 r1 = gen_rtx_EXPR_LIST (m, r1, const0_rtx);
8401 r2 = gen_rtx_REG (m, gregno + 1);
8402 r2 = gen_rtx_EXPR_LIST (m, r2, GEN_INT (4));
8403 return gen_rtx_PARALLEL (mode, gen_rtvec (2, r1, r2));
8405 else
8406 return NULL_RTX;
8408 else
8410 if (gregno <= GP_ARG_MAX_REG)
8411 return gen_rtx_REG (mode, gregno);
8412 else
8413 return NULL_RTX;
8417 /* A subroutine of rs6000_darwin64_record_arg. Assign the bits of the
8418 structure between cum->intoffset and bitpos to integer registers. */
8420 static void
8421 rs6000_darwin64_record_arg_flush (CUMULATIVE_ARGS *cum,
8422 HOST_WIDE_INT bitpos, rtx rvec[], int *k)
8424 enum machine_mode mode;
8425 unsigned int regno;
8426 unsigned int startbit, endbit;
8427 int this_regno, intregs, intoffset;
8428 rtx reg;
8430 if (cum->intoffset == -1)
8431 return;
8433 intoffset = cum->intoffset;
8434 cum->intoffset = -1;
8436 /* If this is the trailing part of a word, try to only load that
8437 much into the register. Otherwise load the whole register. Note
8438 that in the latter case we may pick up unwanted bits. It's not a
8439 problem at the moment but may wish to revisit. */
8441 if (intoffset % BITS_PER_WORD != 0)
8443 mode = mode_for_size (BITS_PER_WORD - intoffset % BITS_PER_WORD,
8444 MODE_INT, 0);
8445 if (mode == BLKmode)
8447 /* We couldn't find an appropriate mode, which happens,
8448 e.g., in packed structs when there are 3 bytes to load.
8449 Back intoffset back to the beginning of the word in this
8450 case. */
8451 intoffset = intoffset & -BITS_PER_WORD;
8452 mode = word_mode;
8455 else
8456 mode = word_mode;
8458 startbit = intoffset & -BITS_PER_WORD;
8459 endbit = (bitpos + BITS_PER_WORD - 1) & -BITS_PER_WORD;
8460 intregs = (endbit - startbit) / BITS_PER_WORD;
8461 this_regno = cum->words + intoffset / BITS_PER_WORD;
8463 if (intregs > 0 && intregs > GP_ARG_NUM_REG - this_regno)
8464 cum->use_stack = 1;
8466 intregs = MIN (intregs, GP_ARG_NUM_REG - this_regno);
8467 if (intregs <= 0)
8468 return;
8470 intoffset /= BITS_PER_UNIT;
8473 regno = GP_ARG_MIN_REG + this_regno;
8474 reg = gen_rtx_REG (mode, regno);
8475 rvec[(*k)++] =
8476 gen_rtx_EXPR_LIST (VOIDmode, reg, GEN_INT (intoffset));
8478 this_regno += 1;
8479 intoffset = (intoffset | (UNITS_PER_WORD-1)) + 1;
8480 mode = word_mode;
8481 intregs -= 1;
8483 while (intregs > 0);
8486 /* Recursive workhorse for the following. */
8488 static void
8489 rs6000_darwin64_record_arg_recurse (CUMULATIVE_ARGS *cum, const_tree type,
8490 HOST_WIDE_INT startbitpos, rtx rvec[],
8491 int *k)
8493 tree f;
8495 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
8496 if (TREE_CODE (f) == FIELD_DECL)
8498 HOST_WIDE_INT bitpos = startbitpos;
8499 tree ftype = TREE_TYPE (f);
8500 enum machine_mode mode;
8501 if (ftype == error_mark_node)
8502 continue;
8503 mode = TYPE_MODE (ftype);
8505 if (DECL_SIZE (f) != 0
8506 && host_integerp (bit_position (f), 1))
8507 bitpos += int_bit_position (f);
8509 /* ??? FIXME: else assume zero offset. */
8511 if (TREE_CODE (ftype) == RECORD_TYPE)
8512 rs6000_darwin64_record_arg_recurse (cum, ftype, bitpos, rvec, k);
8513 else if (cum->named && USE_FP_FOR_ARG_P (cum, mode, ftype))
8515 unsigned n_fpreg = (GET_MODE_SIZE (mode) + 7) >> 3;
8516 #if 0
8517 switch (mode)
8519 case SCmode: mode = SFmode; break;
8520 case DCmode: mode = DFmode; break;
8521 case TCmode: mode = TFmode; break;
8522 default: break;
8524 #endif
8525 rs6000_darwin64_record_arg_flush (cum, bitpos, rvec, k);
8526 if (cum->fregno + n_fpreg > FP_ARG_MAX_REG + 1)
8528 gcc_assert (cum->fregno == FP_ARG_MAX_REG
8529 && (mode == TFmode || mode == TDmode));
8530 /* Long double or _Decimal128 split over regs and memory. */
8531 mode = DECIMAL_FLOAT_MODE_P (mode) ? DDmode : DFmode;
8532 cum->use_stack=1;
8534 rvec[(*k)++]
8535 = gen_rtx_EXPR_LIST (VOIDmode,
8536 gen_rtx_REG (mode, cum->fregno++),
8537 GEN_INT (bitpos / BITS_PER_UNIT));
8538 if (mode == TFmode || mode == TDmode)
8539 cum->fregno++;
8541 else if (cum->named && USE_ALTIVEC_FOR_ARG_P (cum, mode, ftype, 1))
8543 rs6000_darwin64_record_arg_flush (cum, bitpos, rvec, k);
8544 rvec[(*k)++]
8545 = gen_rtx_EXPR_LIST (VOIDmode,
8546 gen_rtx_REG (mode, cum->vregno++),
8547 GEN_INT (bitpos / BITS_PER_UNIT));
8549 else if (cum->intoffset == -1)
8550 cum->intoffset = bitpos;
8554 /* For the darwin64 ABI, we want to construct a PARALLEL consisting of
8555 the register(s) to be used for each field and subfield of a struct
8556 being passed by value, along with the offset of where the
8557 register's value may be found in the block. FP fields go in FP
8558 register, vector fields go in vector registers, and everything
8559 else goes in int registers, packed as in memory.
8561 This code is also used for function return values. RETVAL indicates
8562 whether this is the case.
8564 Much of this is taken from the SPARC V9 port, which has a similar
8565 calling convention. */
8567 static rtx
8568 rs6000_darwin64_record_arg (CUMULATIVE_ARGS *orig_cum, const_tree type,
8569 bool named, bool retval)
8571 rtx rvec[FIRST_PSEUDO_REGISTER];
8572 int k = 1, kbase = 1;
8573 HOST_WIDE_INT typesize = int_size_in_bytes (type);
8574 /* This is a copy; modifications are not visible to our caller. */
8575 CUMULATIVE_ARGS copy_cum = *orig_cum;
8576 CUMULATIVE_ARGS *cum = &copy_cum;
8578 /* Pad to 16 byte boundary if needed. */
8579 if (!retval && TYPE_ALIGN (type) >= 2 * BITS_PER_WORD
8580 && (cum->words % 2) != 0)
8581 cum->words++;
8583 cum->intoffset = 0;
8584 cum->use_stack = 0;
8585 cum->named = named;
8587 /* Put entries into rvec[] for individual FP and vector fields, and
8588 for the chunks of memory that go in int regs. Note we start at
8589 element 1; 0 is reserved for an indication of using memory, and
8590 may or may not be filled in below. */
8591 rs6000_darwin64_record_arg_recurse (cum, type, /* startbit pos= */ 0, rvec, &k);
8592 rs6000_darwin64_record_arg_flush (cum, typesize * BITS_PER_UNIT, rvec, &k);
8594 /* If any part of the struct went on the stack put all of it there.
8595 This hack is because the generic code for
8596 FUNCTION_ARG_PARTIAL_NREGS cannot handle cases where the register
8597 parts of the struct are not at the beginning. */
8598 if (cum->use_stack)
8600 if (retval)
8601 return NULL_RTX; /* doesn't go in registers at all */
8602 kbase = 0;
8603 rvec[0] = gen_rtx_EXPR_LIST (VOIDmode, NULL_RTX, const0_rtx);
8605 if (k > 1 || cum->use_stack)
8606 return gen_rtx_PARALLEL (BLKmode, gen_rtvec_v (k - kbase, &rvec[kbase]));
8607 else
8608 return NULL_RTX;
8611 /* Determine where to place an argument in 64-bit mode with 32-bit ABI. */
8613 static rtx
8614 rs6000_mixed_function_arg (enum machine_mode mode, const_tree type,
8615 int align_words)
8617 int n_units;
8618 int i, k;
8619 rtx rvec[GP_ARG_NUM_REG + 1];
8621 if (align_words >= GP_ARG_NUM_REG)
8622 return NULL_RTX;
8624 n_units = rs6000_arg_size (mode, type);
8626 /* Optimize the simple case where the arg fits in one gpr, except in
8627 the case of BLKmode due to assign_parms assuming that registers are
8628 BITS_PER_WORD wide. */
8629 if (n_units == 0
8630 || (n_units == 1 && mode != BLKmode))
8631 return gen_rtx_REG (mode, GP_ARG_MIN_REG + align_words);
8633 k = 0;
8634 if (align_words + n_units > GP_ARG_NUM_REG)
8635 /* Not all of the arg fits in gprs. Say that it goes in memory too,
8636 using a magic NULL_RTX component.
8637 This is not strictly correct. Only some of the arg belongs in
8638 memory, not all of it. However, the normal scheme using
8639 function_arg_partial_nregs can result in unusual subregs, eg.
8640 (subreg:SI (reg:DF) 4), which are not handled well. The code to
8641 store the whole arg to memory is often more efficient than code
8642 to store pieces, and we know that space is available in the right
8643 place for the whole arg. */
8644 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, NULL_RTX, const0_rtx);
8646 i = 0;
8649 rtx r = gen_rtx_REG (SImode, GP_ARG_MIN_REG + align_words);
8650 rtx off = GEN_INT (i++ * 4);
8651 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, off);
8653 while (++align_words < GP_ARG_NUM_REG && --n_units != 0);
8655 return gen_rtx_PARALLEL (mode, gen_rtvec_v (k, rvec));
8658 /* Determine where to put an argument to a function.
8659 Value is zero to push the argument on the stack,
8660 or a hard register in which to store the argument.
8662 MODE is the argument's machine mode.
8663 TYPE is the data type of the argument (as a tree).
8664 This is null for libcalls where that information may
8665 not be available.
8666 CUM is a variable of type CUMULATIVE_ARGS which gives info about
8667 the preceding args and about the function being called. It is
8668 not modified in this routine.
8669 NAMED is nonzero if this argument is a named parameter
8670 (otherwise it is an extra parameter matching an ellipsis).
8672 On RS/6000 the first eight words of non-FP are normally in registers
8673 and the rest are pushed. Under AIX, the first 13 FP args are in registers.
8674 Under V.4, the first 8 FP args are in registers.
8676 If this is floating-point and no prototype is specified, we use
8677 both an FP and integer register (or possibly FP reg and stack). Library
8678 functions (when CALL_LIBCALL is set) always have the proper types for args,
8679 so we can pass the FP value just in one register. emit_library_function
8680 doesn't support PARALLEL anyway.
8682 Note that for args passed by reference, function_arg will be called
8683 with MODE and TYPE set to that of the pointer to the arg, not the arg
8684 itself. */
8686 static rtx
8687 rs6000_function_arg (cumulative_args_t cum_v, enum machine_mode mode,
8688 const_tree type, bool named)
8690 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
8691 enum rs6000_abi abi = DEFAULT_ABI;
8693 /* Return a marker to indicate whether CR1 needs to set or clear the
8694 bit that V.4 uses to say fp args were passed in registers.
8695 Assume that we don't need the marker for software floating point,
8696 or compiler generated library calls. */
8697 if (mode == VOIDmode)
8699 if (abi == ABI_V4
8700 && (cum->call_cookie & CALL_LIBCALL) == 0
8701 && (cum->stdarg
8702 || (cum->nargs_prototype < 0
8703 && (cum->prototype || TARGET_NO_PROTOTYPE))))
8705 /* For the SPE, we need to crxor CR6 always. */
8706 if (TARGET_SPE_ABI)
8707 return GEN_INT (cum->call_cookie | CALL_V4_SET_FP_ARGS);
8708 else if (TARGET_HARD_FLOAT && TARGET_FPRS)
8709 return GEN_INT (cum->call_cookie
8710 | ((cum->fregno == FP_ARG_MIN_REG)
8711 ? CALL_V4_SET_FP_ARGS
8712 : CALL_V4_CLEAR_FP_ARGS));
8715 return GEN_INT (cum->call_cookie & ~CALL_LIBCALL);
8718 if (TARGET_MACHO && rs6000_darwin64_struct_check_p (mode, type))
8720 rtx rslt = rs6000_darwin64_record_arg (cum, type, named, /*retval= */false);
8721 if (rslt != NULL_RTX)
8722 return rslt;
8723 /* Else fall through to usual handling. */
8726 if (USE_ALTIVEC_FOR_ARG_P (cum, mode, type, named))
8727 if (TARGET_64BIT && ! cum->prototype)
8729 /* Vector parameters get passed in vector register
8730 and also in GPRs or memory, in absence of prototype. */
8731 int align_words;
8732 rtx slot;
8733 align_words = (cum->words + 1) & ~1;
8735 if (align_words >= GP_ARG_NUM_REG)
8737 slot = NULL_RTX;
8739 else
8741 slot = gen_rtx_REG (mode, GP_ARG_MIN_REG + align_words);
8743 return gen_rtx_PARALLEL (mode,
8744 gen_rtvec (2,
8745 gen_rtx_EXPR_LIST (VOIDmode,
8746 slot, const0_rtx),
8747 gen_rtx_EXPR_LIST (VOIDmode,
8748 gen_rtx_REG (mode, cum->vregno),
8749 const0_rtx)));
8751 else
8752 return gen_rtx_REG (mode, cum->vregno);
8753 else if (TARGET_ALTIVEC_ABI
8754 && (ALTIVEC_OR_VSX_VECTOR_MODE (mode)
8755 || (type && TREE_CODE (type) == VECTOR_TYPE
8756 && int_size_in_bytes (type) == 16)))
8758 if (named || abi == ABI_V4)
8759 return NULL_RTX;
8760 else
8762 /* Vector parameters to varargs functions under AIX or Darwin
8763 get passed in memory and possibly also in GPRs. */
8764 int align, align_words, n_words;
8765 enum machine_mode part_mode;
8767 /* Vector parameters must be 16-byte aligned. This places them at
8768 2 mod 4 in terms of words in 32-bit mode, since the parameter
8769 save area starts at offset 24 from the stack. In 64-bit mode,
8770 they just have to start on an even word, since the parameter
8771 save area is 16-byte aligned. */
8772 if (TARGET_32BIT)
8773 align = (2 - cum->words) & 3;
8774 else
8775 align = cum->words & 1;
8776 align_words = cum->words + align;
8778 /* Out of registers? Memory, then. */
8779 if (align_words >= GP_ARG_NUM_REG)
8780 return NULL_RTX;
8782 if (TARGET_32BIT && TARGET_POWERPC64)
8783 return rs6000_mixed_function_arg (mode, type, align_words);
8785 /* The vector value goes in GPRs. Only the part of the
8786 value in GPRs is reported here. */
8787 part_mode = mode;
8788 n_words = rs6000_arg_size (mode, type);
8789 if (align_words + n_words > GP_ARG_NUM_REG)
8790 /* Fortunately, there are only two possibilities, the value
8791 is either wholly in GPRs or half in GPRs and half not. */
8792 part_mode = DImode;
8794 return gen_rtx_REG (part_mode, GP_ARG_MIN_REG + align_words);
8797 else if (TARGET_SPE_ABI && TARGET_SPE
8798 && (SPE_VECTOR_MODE (mode)
8799 || (TARGET_E500_DOUBLE && (mode == DFmode
8800 || mode == DCmode
8801 || mode == TFmode
8802 || mode == TCmode))))
8803 return rs6000_spe_function_arg (cum, mode, type);
8805 else if (abi == ABI_V4)
8807 if (TARGET_HARD_FLOAT && TARGET_FPRS
8808 && ((TARGET_SINGLE_FLOAT && mode == SFmode)
8809 || (TARGET_DOUBLE_FLOAT && mode == DFmode)
8810 || (mode == TFmode && !TARGET_IEEEQUAD)
8811 || mode == SDmode || mode == DDmode || mode == TDmode))
8813 /* _Decimal128 must use an even/odd register pair. This assumes
8814 that the register number is odd when fregno is odd. */
8815 if (mode == TDmode && (cum->fregno % 2) == 1)
8816 cum->fregno++;
8818 if (cum->fregno + (mode == TFmode || mode == TDmode ? 1 : 0)
8819 <= FP_ARG_V4_MAX_REG)
8820 return gen_rtx_REG (mode, cum->fregno);
8821 else
8822 return NULL_RTX;
8824 else
8826 int n_words = rs6000_arg_size (mode, type);
8827 int gregno = cum->sysv_gregno;
8829 /* Long long and SPE vectors are put in (r3,r4), (r5,r6),
8830 (r7,r8) or (r9,r10). As does any other 2 word item such
8831 as complex int due to a historical mistake. */
8832 if (n_words == 2)
8833 gregno += (1 - gregno) & 1;
8835 /* Multi-reg args are not split between registers and stack. */
8836 if (gregno + n_words - 1 > GP_ARG_MAX_REG)
8837 return NULL_RTX;
8839 if (TARGET_32BIT && TARGET_POWERPC64)
8840 return rs6000_mixed_function_arg (mode, type,
8841 gregno - GP_ARG_MIN_REG);
8842 return gen_rtx_REG (mode, gregno);
8845 else
8847 int align_words = rs6000_parm_start (mode, type, cum->words);
8849 /* _Decimal128 must be passed in an even/odd float register pair.
8850 This assumes that the register number is odd when fregno is odd. */
8851 if (mode == TDmode && (cum->fregno % 2) == 1)
8852 cum->fregno++;
8854 if (USE_FP_FOR_ARG_P (cum, mode, type))
8856 rtx rvec[GP_ARG_NUM_REG + 1];
8857 rtx r;
8858 int k;
8859 bool needs_psave;
8860 enum machine_mode fmode = mode;
8861 unsigned long n_fpreg = (GET_MODE_SIZE (mode) + 7) >> 3;
8863 if (cum->fregno + n_fpreg > FP_ARG_MAX_REG + 1)
8865 /* Currently, we only ever need one reg here because complex
8866 doubles are split. */
8867 gcc_assert (cum->fregno == FP_ARG_MAX_REG
8868 && (fmode == TFmode || fmode == TDmode));
8870 /* Long double or _Decimal128 split over regs and memory. */
8871 fmode = DECIMAL_FLOAT_MODE_P (fmode) ? DDmode : DFmode;
8874 /* Do we also need to pass this arg in the parameter save
8875 area? */
8876 needs_psave = (type
8877 && (cum->nargs_prototype <= 0
8878 || (DEFAULT_ABI == ABI_AIX
8879 && TARGET_XL_COMPAT
8880 && align_words >= GP_ARG_NUM_REG)));
8882 if (!needs_psave && mode == fmode)
8883 return gen_rtx_REG (fmode, cum->fregno);
8885 k = 0;
8886 if (needs_psave)
8888 /* Describe the part that goes in gprs or the stack.
8889 This piece must come first, before the fprs. */
8890 if (align_words < GP_ARG_NUM_REG)
8892 unsigned long n_words = rs6000_arg_size (mode, type);
8894 if (align_words + n_words > GP_ARG_NUM_REG
8895 || (TARGET_32BIT && TARGET_POWERPC64))
8897 /* If this is partially on the stack, then we only
8898 include the portion actually in registers here. */
8899 enum machine_mode rmode = TARGET_32BIT ? SImode : DImode;
8900 rtx off;
8901 int i = 0;
8902 if (align_words + n_words > GP_ARG_NUM_REG)
8903 /* Not all of the arg fits in gprs. Say that it
8904 goes in memory too, using a magic NULL_RTX
8905 component. Also see comment in
8906 rs6000_mixed_function_arg for why the normal
8907 function_arg_partial_nregs scheme doesn't work
8908 in this case. */
8909 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, NULL_RTX,
8910 const0_rtx);
8913 r = gen_rtx_REG (rmode,
8914 GP_ARG_MIN_REG + align_words);
8915 off = GEN_INT (i++ * GET_MODE_SIZE (rmode));
8916 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, off);
8918 while (++align_words < GP_ARG_NUM_REG && --n_words != 0);
8920 else
8922 /* The whole arg fits in gprs. */
8923 r = gen_rtx_REG (mode, GP_ARG_MIN_REG + align_words);
8924 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, const0_rtx);
8927 else
8928 /* It's entirely in memory. */
8929 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, NULL_RTX, const0_rtx);
8932 /* Describe where this piece goes in the fprs. */
8933 r = gen_rtx_REG (fmode, cum->fregno);
8934 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, const0_rtx);
8936 return gen_rtx_PARALLEL (mode, gen_rtvec_v (k, rvec));
8938 else if (align_words < GP_ARG_NUM_REG)
8940 if (TARGET_32BIT && TARGET_POWERPC64)
8941 return rs6000_mixed_function_arg (mode, type, align_words);
8943 if (mode == BLKmode)
8944 mode = Pmode;
8946 return gen_rtx_REG (mode, GP_ARG_MIN_REG + align_words);
8948 else
8949 return NULL_RTX;
8953 /* For an arg passed partly in registers and partly in memory, this is
8954 the number of bytes passed in registers. For args passed entirely in
8955 registers or entirely in memory, zero. When an arg is described by a
8956 PARALLEL, perhaps using more than one register type, this function
8957 returns the number of bytes used by the first element of the PARALLEL. */
8959 static int
8960 rs6000_arg_partial_bytes (cumulative_args_t cum_v, enum machine_mode mode,
8961 tree type, bool named)
8963 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
8964 int ret = 0;
8965 int align_words;
8967 if (DEFAULT_ABI == ABI_V4)
8968 return 0;
8970 if (USE_ALTIVEC_FOR_ARG_P (cum, mode, type, named)
8971 && cum->nargs_prototype >= 0)
8972 return 0;
8974 /* In this complicated case we just disable the partial_nregs code. */
8975 if (TARGET_MACHO && rs6000_darwin64_struct_check_p (mode, type))
8976 return 0;
8978 align_words = rs6000_parm_start (mode, type, cum->words);
8980 if (USE_FP_FOR_ARG_P (cum, mode, type))
8982 /* If we are passing this arg in the fixed parameter save area
8983 (gprs or memory) as well as fprs, then this function should
8984 return the number of partial bytes passed in the parameter
8985 save area rather than partial bytes passed in fprs. */
8986 if (type
8987 && (cum->nargs_prototype <= 0
8988 || (DEFAULT_ABI == ABI_AIX
8989 && TARGET_XL_COMPAT
8990 && align_words >= GP_ARG_NUM_REG)))
8991 return 0;
8992 else if (cum->fregno + ((GET_MODE_SIZE (mode) + 7) >> 3)
8993 > FP_ARG_MAX_REG + 1)
8994 ret = (FP_ARG_MAX_REG + 1 - cum->fregno) * 8;
8995 else if (cum->nargs_prototype >= 0)
8996 return 0;
8999 if (align_words < GP_ARG_NUM_REG
9000 && GP_ARG_NUM_REG < align_words + rs6000_arg_size (mode, type))
9001 ret = (GP_ARG_NUM_REG - align_words) * (TARGET_32BIT ? 4 : 8);
9003 if (ret != 0 && TARGET_DEBUG_ARG)
9004 fprintf (stderr, "rs6000_arg_partial_bytes: %d\n", ret);
9006 return ret;
9009 /* A C expression that indicates when an argument must be passed by
9010 reference. If nonzero for an argument, a copy of that argument is
9011 made in memory and a pointer to the argument is passed instead of
9012 the argument itself. The pointer is passed in whatever way is
9013 appropriate for passing a pointer to that type.
9015 Under V.4, aggregates and long double are passed by reference.
9017 As an extension to all 32-bit ABIs, AltiVec vectors are passed by
9018 reference unless the AltiVec vector extension ABI is in force.
9020 As an extension to all ABIs, variable sized types are passed by
9021 reference. */
9023 static bool
9024 rs6000_pass_by_reference (cumulative_args_t cum ATTRIBUTE_UNUSED,
9025 enum machine_mode mode, const_tree type,
9026 bool named ATTRIBUTE_UNUSED)
9028 if (DEFAULT_ABI == ABI_V4 && TARGET_IEEEQUAD && mode == TFmode)
9030 if (TARGET_DEBUG_ARG)
9031 fprintf (stderr, "function_arg_pass_by_reference: V4 long double\n");
9032 return 1;
9035 if (!type)
9036 return 0;
9038 if (DEFAULT_ABI == ABI_V4 && AGGREGATE_TYPE_P (type))
9040 if (TARGET_DEBUG_ARG)
9041 fprintf (stderr, "function_arg_pass_by_reference: V4 aggregate\n");
9042 return 1;
9045 if (int_size_in_bytes (type) < 0)
9047 if (TARGET_DEBUG_ARG)
9048 fprintf (stderr, "function_arg_pass_by_reference: variable size\n");
9049 return 1;
9052 /* Allow -maltivec -mabi=no-altivec without warning. Altivec vector
9053 modes only exist for GCC vector types if -maltivec. */
9054 if (TARGET_32BIT && !TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (mode))
9056 if (TARGET_DEBUG_ARG)
9057 fprintf (stderr, "function_arg_pass_by_reference: AltiVec\n");
9058 return 1;
9061 /* Pass synthetic vectors in memory. */
9062 if (TREE_CODE (type) == VECTOR_TYPE
9063 && int_size_in_bytes (type) > (TARGET_ALTIVEC_ABI ? 16 : 8))
9065 static bool warned_for_pass_big_vectors = false;
9066 if (TARGET_DEBUG_ARG)
9067 fprintf (stderr, "function_arg_pass_by_reference: synthetic vector\n");
9068 if (!warned_for_pass_big_vectors)
9070 warning (0, "GCC vector passed by reference: "
9071 "non-standard ABI extension with no compatibility guarantee");
9072 warned_for_pass_big_vectors = true;
9074 return 1;
9077 return 0;
9080 static void
9081 rs6000_move_block_from_reg (int regno, rtx x, int nregs)
9083 int i;
9084 enum machine_mode reg_mode = TARGET_32BIT ? SImode : DImode;
9086 if (nregs == 0)
9087 return;
9089 for (i = 0; i < nregs; i++)
9091 rtx tem = adjust_address_nv (x, reg_mode, i * GET_MODE_SIZE (reg_mode));
9092 if (reload_completed)
9094 if (! strict_memory_address_p (reg_mode, XEXP (tem, 0)))
9095 tem = NULL_RTX;
9096 else
9097 tem = simplify_gen_subreg (reg_mode, x, BLKmode,
9098 i * GET_MODE_SIZE (reg_mode));
9100 else
9101 tem = replace_equiv_address (tem, XEXP (tem, 0));
9103 gcc_assert (tem);
9105 emit_move_insn (tem, gen_rtx_REG (reg_mode, regno + i));
9109 /* Perform any needed actions needed for a function that is receiving a
9110 variable number of arguments.
9112 CUM is as above.
9114 MODE and TYPE are the mode and type of the current parameter.
9116 PRETEND_SIZE is a variable that should be set to the amount of stack
9117 that must be pushed by the prolog to pretend that our caller pushed
9120 Normally, this macro will push all remaining incoming registers on the
9121 stack and set PRETEND_SIZE to the length of the registers pushed. */
9123 static void
9124 setup_incoming_varargs (cumulative_args_t cum, enum machine_mode mode,
9125 tree type, int *pretend_size ATTRIBUTE_UNUSED,
9126 int no_rtl)
9128 CUMULATIVE_ARGS next_cum;
9129 int reg_size = TARGET_32BIT ? 4 : 8;
9130 rtx save_area = NULL_RTX, mem;
9131 int first_reg_offset;
9132 alias_set_type set;
9134 /* Skip the last named argument. */
9135 next_cum = *get_cumulative_args (cum);
9136 rs6000_function_arg_advance_1 (&next_cum, mode, type, true, 0);
9138 if (DEFAULT_ABI == ABI_V4)
9140 first_reg_offset = next_cum.sysv_gregno - GP_ARG_MIN_REG;
9142 if (! no_rtl)
9144 int gpr_reg_num = 0, gpr_size = 0, fpr_size = 0;
9145 HOST_WIDE_INT offset = 0;
9147 /* Try to optimize the size of the varargs save area.
9148 The ABI requires that ap.reg_save_area is doubleword
9149 aligned, but we don't need to allocate space for all
9150 the bytes, only those to which we actually will save
9151 anything. */
9152 if (cfun->va_list_gpr_size && first_reg_offset < GP_ARG_NUM_REG)
9153 gpr_reg_num = GP_ARG_NUM_REG - first_reg_offset;
9154 if (TARGET_HARD_FLOAT && TARGET_FPRS
9155 && next_cum.fregno <= FP_ARG_V4_MAX_REG
9156 && cfun->va_list_fpr_size)
9158 if (gpr_reg_num)
9159 fpr_size = (next_cum.fregno - FP_ARG_MIN_REG)
9160 * UNITS_PER_FP_WORD;
9161 if (cfun->va_list_fpr_size
9162 < FP_ARG_V4_MAX_REG + 1 - next_cum.fregno)
9163 fpr_size += cfun->va_list_fpr_size * UNITS_PER_FP_WORD;
9164 else
9165 fpr_size += (FP_ARG_V4_MAX_REG + 1 - next_cum.fregno)
9166 * UNITS_PER_FP_WORD;
9168 if (gpr_reg_num)
9170 offset = -((first_reg_offset * reg_size) & ~7);
9171 if (!fpr_size && gpr_reg_num > cfun->va_list_gpr_size)
9173 gpr_reg_num = cfun->va_list_gpr_size;
9174 if (reg_size == 4 && (first_reg_offset & 1))
9175 gpr_reg_num++;
9177 gpr_size = (gpr_reg_num * reg_size + 7) & ~7;
9179 else if (fpr_size)
9180 offset = - (int) (next_cum.fregno - FP_ARG_MIN_REG)
9181 * UNITS_PER_FP_WORD
9182 - (int) (GP_ARG_NUM_REG * reg_size);
9184 if (gpr_size + fpr_size)
9186 rtx reg_save_area
9187 = assign_stack_local (BLKmode, gpr_size + fpr_size, 64);
9188 gcc_assert (GET_CODE (reg_save_area) == MEM);
9189 reg_save_area = XEXP (reg_save_area, 0);
9190 if (GET_CODE (reg_save_area) == PLUS)
9192 gcc_assert (XEXP (reg_save_area, 0)
9193 == virtual_stack_vars_rtx);
9194 gcc_assert (GET_CODE (XEXP (reg_save_area, 1)) == CONST_INT);
9195 offset += INTVAL (XEXP (reg_save_area, 1));
9197 else
9198 gcc_assert (reg_save_area == virtual_stack_vars_rtx);
9201 cfun->machine->varargs_save_offset = offset;
9202 save_area = plus_constant (Pmode, virtual_stack_vars_rtx, offset);
9205 else
9207 first_reg_offset = next_cum.words;
9208 save_area = virtual_incoming_args_rtx;
9210 if (targetm.calls.must_pass_in_stack (mode, type))
9211 first_reg_offset += rs6000_arg_size (TYPE_MODE (type), type);
9214 set = get_varargs_alias_set ();
9215 if (! no_rtl && first_reg_offset < GP_ARG_NUM_REG
9216 && cfun->va_list_gpr_size)
9218 int nregs = GP_ARG_NUM_REG - first_reg_offset;
9220 if (va_list_gpr_counter_field)
9222 /* V4 va_list_gpr_size counts number of registers needed. */
9223 if (nregs > cfun->va_list_gpr_size)
9224 nregs = cfun->va_list_gpr_size;
9226 else
9228 /* char * va_list instead counts number of bytes needed. */
9229 if (nregs > cfun->va_list_gpr_size / reg_size)
9230 nregs = cfun->va_list_gpr_size / reg_size;
9233 mem = gen_rtx_MEM (BLKmode,
9234 plus_constant (Pmode, save_area,
9235 first_reg_offset * reg_size));
9236 MEM_NOTRAP_P (mem) = 1;
9237 set_mem_alias_set (mem, set);
9238 set_mem_align (mem, BITS_PER_WORD);
9240 rs6000_move_block_from_reg (GP_ARG_MIN_REG + first_reg_offset, mem,
9241 nregs);
9244 /* Save FP registers if needed. */
9245 if (DEFAULT_ABI == ABI_V4
9246 && TARGET_HARD_FLOAT && TARGET_FPRS
9247 && ! no_rtl
9248 && next_cum.fregno <= FP_ARG_V4_MAX_REG
9249 && cfun->va_list_fpr_size)
9251 int fregno = next_cum.fregno, nregs;
9252 rtx cr1 = gen_rtx_REG (CCmode, CR1_REGNO);
9253 rtx lab = gen_label_rtx ();
9254 int off = (GP_ARG_NUM_REG * reg_size) + ((fregno - FP_ARG_MIN_REG)
9255 * UNITS_PER_FP_WORD);
9257 emit_jump_insn
9258 (gen_rtx_SET (VOIDmode,
9259 pc_rtx,
9260 gen_rtx_IF_THEN_ELSE (VOIDmode,
9261 gen_rtx_NE (VOIDmode, cr1,
9262 const0_rtx),
9263 gen_rtx_LABEL_REF (VOIDmode, lab),
9264 pc_rtx)));
9266 for (nregs = 0;
9267 fregno <= FP_ARG_V4_MAX_REG && nregs < cfun->va_list_fpr_size;
9268 fregno++, off += UNITS_PER_FP_WORD, nregs++)
9270 mem = gen_rtx_MEM ((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT)
9271 ? DFmode : SFmode,
9272 plus_constant (Pmode, save_area, off));
9273 MEM_NOTRAP_P (mem) = 1;
9274 set_mem_alias_set (mem, set);
9275 set_mem_align (mem, GET_MODE_ALIGNMENT (
9276 (TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT)
9277 ? DFmode : SFmode));
9278 emit_move_insn (mem, gen_rtx_REG (
9279 (TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT)
9280 ? DFmode : SFmode, fregno));
9283 emit_label (lab);
9287 /* Create the va_list data type. */
9289 static tree
9290 rs6000_build_builtin_va_list (void)
9292 tree f_gpr, f_fpr, f_res, f_ovf, f_sav, record, type_decl;
9294 /* For AIX, prefer 'char *' because that's what the system
9295 header files like. */
9296 if (DEFAULT_ABI != ABI_V4)
9297 return build_pointer_type (char_type_node);
9299 record = (*lang_hooks.types.make_type) (RECORD_TYPE);
9300 type_decl = build_decl (BUILTINS_LOCATION, TYPE_DECL,
9301 get_identifier ("__va_list_tag"), record);
9303 f_gpr = build_decl (BUILTINS_LOCATION, FIELD_DECL, get_identifier ("gpr"),
9304 unsigned_char_type_node);
9305 f_fpr = build_decl (BUILTINS_LOCATION, FIELD_DECL, get_identifier ("fpr"),
9306 unsigned_char_type_node);
9307 /* Give the two bytes of padding a name, so that -Wpadded won't warn on
9308 every user file. */
9309 f_res = build_decl (BUILTINS_LOCATION, FIELD_DECL,
9310 get_identifier ("reserved"), short_unsigned_type_node);
9311 f_ovf = build_decl (BUILTINS_LOCATION, FIELD_DECL,
9312 get_identifier ("overflow_arg_area"),
9313 ptr_type_node);
9314 f_sav = build_decl (BUILTINS_LOCATION, FIELD_DECL,
9315 get_identifier ("reg_save_area"),
9316 ptr_type_node);
9318 va_list_gpr_counter_field = f_gpr;
9319 va_list_fpr_counter_field = f_fpr;
9321 DECL_FIELD_CONTEXT (f_gpr) = record;
9322 DECL_FIELD_CONTEXT (f_fpr) = record;
9323 DECL_FIELD_CONTEXT (f_res) = record;
9324 DECL_FIELD_CONTEXT (f_ovf) = record;
9325 DECL_FIELD_CONTEXT (f_sav) = record;
9327 TYPE_STUB_DECL (record) = type_decl;
9328 TYPE_NAME (record) = type_decl;
9329 TYPE_FIELDS (record) = f_gpr;
9330 DECL_CHAIN (f_gpr) = f_fpr;
9331 DECL_CHAIN (f_fpr) = f_res;
9332 DECL_CHAIN (f_res) = f_ovf;
9333 DECL_CHAIN (f_ovf) = f_sav;
9335 layout_type (record);
9337 /* The correct type is an array type of one element. */
9338 return build_array_type (record, build_index_type (size_zero_node));
9341 /* Implement va_start. */
9343 static void
9344 rs6000_va_start (tree valist, rtx nextarg)
9346 HOST_WIDE_INT words, n_gpr, n_fpr;
9347 tree f_gpr, f_fpr, f_res, f_ovf, f_sav;
9348 tree gpr, fpr, ovf, sav, t;
9350 /* Only SVR4 needs something special. */
9351 if (DEFAULT_ABI != ABI_V4)
9353 std_expand_builtin_va_start (valist, nextarg);
9354 return;
9357 f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
9358 f_fpr = DECL_CHAIN (f_gpr);
9359 f_res = DECL_CHAIN (f_fpr);
9360 f_ovf = DECL_CHAIN (f_res);
9361 f_sav = DECL_CHAIN (f_ovf);
9363 valist = build_simple_mem_ref (valist);
9364 gpr = build3 (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr, NULL_TREE);
9365 fpr = build3 (COMPONENT_REF, TREE_TYPE (f_fpr), unshare_expr (valist),
9366 f_fpr, NULL_TREE);
9367 ovf = build3 (COMPONENT_REF, TREE_TYPE (f_ovf), unshare_expr (valist),
9368 f_ovf, NULL_TREE);
9369 sav = build3 (COMPONENT_REF, TREE_TYPE (f_sav), unshare_expr (valist),
9370 f_sav, NULL_TREE);
9372 /* Count number of gp and fp argument registers used. */
9373 words = crtl->args.info.words;
9374 n_gpr = MIN (crtl->args.info.sysv_gregno - GP_ARG_MIN_REG,
9375 GP_ARG_NUM_REG);
9376 n_fpr = MIN (crtl->args.info.fregno - FP_ARG_MIN_REG,
9377 FP_ARG_NUM_REG);
9379 if (TARGET_DEBUG_ARG)
9380 fprintf (stderr, "va_start: words = "HOST_WIDE_INT_PRINT_DEC", n_gpr = "
9381 HOST_WIDE_INT_PRINT_DEC", n_fpr = "HOST_WIDE_INT_PRINT_DEC"\n",
9382 words, n_gpr, n_fpr);
9384 if (cfun->va_list_gpr_size)
9386 t = build2 (MODIFY_EXPR, TREE_TYPE (gpr), gpr,
9387 build_int_cst (NULL_TREE, n_gpr));
9388 TREE_SIDE_EFFECTS (t) = 1;
9389 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
9392 if (cfun->va_list_fpr_size)
9394 t = build2 (MODIFY_EXPR, TREE_TYPE (fpr), fpr,
9395 build_int_cst (NULL_TREE, n_fpr));
9396 TREE_SIDE_EFFECTS (t) = 1;
9397 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
9399 #ifdef HAVE_AS_GNU_ATTRIBUTE
9400 if (call_ABI_of_interest (cfun->decl))
9401 rs6000_passes_float = true;
9402 #endif
9405 /* Find the overflow area. */
9406 t = make_tree (TREE_TYPE (ovf), virtual_incoming_args_rtx);
9407 if (words != 0)
9408 t = fold_build_pointer_plus_hwi (t, words * UNITS_PER_WORD);
9409 t = build2 (MODIFY_EXPR, TREE_TYPE (ovf), ovf, t);
9410 TREE_SIDE_EFFECTS (t) = 1;
9411 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
9413 /* If there were no va_arg invocations, don't set up the register
9414 save area. */
9415 if (!cfun->va_list_gpr_size
9416 && !cfun->va_list_fpr_size
9417 && n_gpr < GP_ARG_NUM_REG
9418 && n_fpr < FP_ARG_V4_MAX_REG)
9419 return;
9421 /* Find the register save area. */
9422 t = make_tree (TREE_TYPE (sav), virtual_stack_vars_rtx);
9423 if (cfun->machine->varargs_save_offset)
9424 t = fold_build_pointer_plus_hwi (t, cfun->machine->varargs_save_offset);
9425 t = build2 (MODIFY_EXPR, TREE_TYPE (sav), sav, t);
9426 TREE_SIDE_EFFECTS (t) = 1;
9427 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
9430 /* Implement va_arg. */
9432 static tree
9433 rs6000_gimplify_va_arg (tree valist, tree type, gimple_seq *pre_p,
9434 gimple_seq *post_p)
9436 tree f_gpr, f_fpr, f_res, f_ovf, f_sav;
9437 tree gpr, fpr, ovf, sav, reg, t, u;
9438 int size, rsize, n_reg, sav_ofs, sav_scale;
9439 tree lab_false, lab_over, addr;
9440 int align;
9441 tree ptrtype = build_pointer_type_for_mode (type, ptr_mode, true);
9442 int regalign = 0;
9443 gimple stmt;
9445 if (pass_by_reference (NULL, TYPE_MODE (type), type, false))
9447 t = rs6000_gimplify_va_arg (valist, ptrtype, pre_p, post_p);
9448 return build_va_arg_indirect_ref (t);
9451 /* We need to deal with the fact that the darwin ppc64 ABI is defined by an
9452 earlier version of gcc, with the property that it always applied alignment
9453 adjustments to the va-args (even for zero-sized types). The cheapest way
9454 to deal with this is to replicate the effect of the part of
9455 std_gimplify_va_arg_expr that carries out the align adjust, for the case
9456 of relevance.
9457 We don't need to check for pass-by-reference because of the test above.
9458 We can return a simplifed answer, since we know there's no offset to add. */
9460 if (TARGET_MACHO
9461 && rs6000_darwin64_abi
9462 && integer_zerop (TYPE_SIZE (type)))
9464 unsigned HOST_WIDE_INT align, boundary;
9465 tree valist_tmp = get_initialized_tmp_var (valist, pre_p, NULL);
9466 align = PARM_BOUNDARY / BITS_PER_UNIT;
9467 boundary = rs6000_function_arg_boundary (TYPE_MODE (type), type);
9468 if (boundary > MAX_SUPPORTED_STACK_ALIGNMENT)
9469 boundary = MAX_SUPPORTED_STACK_ALIGNMENT;
9470 boundary /= BITS_PER_UNIT;
9471 if (boundary > align)
9473 tree t ;
9474 /* This updates arg ptr by the amount that would be necessary
9475 to align the zero-sized (but not zero-alignment) item. */
9476 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
9477 fold_build_pointer_plus_hwi (valist_tmp, boundary - 1));
9478 gimplify_and_add (t, pre_p);
9480 t = fold_convert (sizetype, valist_tmp);
9481 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
9482 fold_convert (TREE_TYPE (valist),
9483 fold_build2 (BIT_AND_EXPR, sizetype, t,
9484 size_int (-boundary))));
9485 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist, t);
9486 gimplify_and_add (t, pre_p);
9488 /* Since it is zero-sized there's no increment for the item itself. */
9489 valist_tmp = fold_convert (build_pointer_type (type), valist_tmp);
9490 return build_va_arg_indirect_ref (valist_tmp);
9493 if (DEFAULT_ABI != ABI_V4)
9495 if (targetm.calls.split_complex_arg && TREE_CODE (type) == COMPLEX_TYPE)
9497 tree elem_type = TREE_TYPE (type);
9498 enum machine_mode elem_mode = TYPE_MODE (elem_type);
9499 int elem_size = GET_MODE_SIZE (elem_mode);
9501 if (elem_size < UNITS_PER_WORD)
9503 tree real_part, imag_part;
9504 gimple_seq post = NULL;
9506 real_part = rs6000_gimplify_va_arg (valist, elem_type, pre_p,
9507 &post);
9508 /* Copy the value into a temporary, lest the formal temporary
9509 be reused out from under us. */
9510 real_part = get_initialized_tmp_var (real_part, pre_p, &post);
9511 gimple_seq_add_seq (pre_p, post);
9513 imag_part = rs6000_gimplify_va_arg (valist, elem_type, pre_p,
9514 post_p);
9516 return build2 (COMPLEX_EXPR, type, real_part, imag_part);
9520 return std_gimplify_va_arg_expr (valist, type, pre_p, post_p);
9523 f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
9524 f_fpr = DECL_CHAIN (f_gpr);
9525 f_res = DECL_CHAIN (f_fpr);
9526 f_ovf = DECL_CHAIN (f_res);
9527 f_sav = DECL_CHAIN (f_ovf);
9529 valist = build_va_arg_indirect_ref (valist);
9530 gpr = build3 (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr, NULL_TREE);
9531 fpr = build3 (COMPONENT_REF, TREE_TYPE (f_fpr), unshare_expr (valist),
9532 f_fpr, NULL_TREE);
9533 ovf = build3 (COMPONENT_REF, TREE_TYPE (f_ovf), unshare_expr (valist),
9534 f_ovf, NULL_TREE);
9535 sav = build3 (COMPONENT_REF, TREE_TYPE (f_sav), unshare_expr (valist),
9536 f_sav, NULL_TREE);
9538 size = int_size_in_bytes (type);
9539 rsize = (size + 3) / 4;
9540 align = 1;
9542 if (TARGET_HARD_FLOAT && TARGET_FPRS
9543 && ((TARGET_SINGLE_FLOAT && TYPE_MODE (type) == SFmode)
9544 || (TARGET_DOUBLE_FLOAT
9545 && (TYPE_MODE (type) == DFmode
9546 || TYPE_MODE (type) == TFmode
9547 || TYPE_MODE (type) == SDmode
9548 || TYPE_MODE (type) == DDmode
9549 || TYPE_MODE (type) == TDmode))))
9551 /* FP args go in FP registers, if present. */
9552 reg = fpr;
9553 n_reg = (size + 7) / 8;
9554 sav_ofs = ((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT) ? 8 : 4) * 4;
9555 sav_scale = ((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT) ? 8 : 4);
9556 if (TYPE_MODE (type) != SFmode && TYPE_MODE (type) != SDmode)
9557 align = 8;
9559 else
9561 /* Otherwise into GP registers. */
9562 reg = gpr;
9563 n_reg = rsize;
9564 sav_ofs = 0;
9565 sav_scale = 4;
9566 if (n_reg == 2)
9567 align = 8;
9570 /* Pull the value out of the saved registers.... */
9572 lab_over = NULL;
9573 addr = create_tmp_var (ptr_type_node, "addr");
9575 /* AltiVec vectors never go in registers when -mabi=altivec. */
9576 if (TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (TYPE_MODE (type)))
9577 align = 16;
9578 else
9580 lab_false = create_artificial_label (input_location);
9581 lab_over = create_artificial_label (input_location);
9583 /* Long long and SPE vectors are aligned in the registers.
9584 As are any other 2 gpr item such as complex int due to a
9585 historical mistake. */
9586 u = reg;
9587 if (n_reg == 2 && reg == gpr)
9589 regalign = 1;
9590 u = build2 (BIT_AND_EXPR, TREE_TYPE (reg), unshare_expr (reg),
9591 build_int_cst (TREE_TYPE (reg), n_reg - 1));
9592 u = build2 (POSTINCREMENT_EXPR, TREE_TYPE (reg),
9593 unshare_expr (reg), u);
9595 /* _Decimal128 is passed in even/odd fpr pairs; the stored
9596 reg number is 0 for f1, so we want to make it odd. */
9597 else if (reg == fpr && TYPE_MODE (type) == TDmode)
9599 t = build2 (BIT_IOR_EXPR, TREE_TYPE (reg), unshare_expr (reg),
9600 build_int_cst (TREE_TYPE (reg), 1));
9601 u = build2 (MODIFY_EXPR, void_type_node, unshare_expr (reg), t);
9604 t = fold_convert (TREE_TYPE (reg), size_int (8 - n_reg + 1));
9605 t = build2 (GE_EXPR, boolean_type_node, u, t);
9606 u = build1 (GOTO_EXPR, void_type_node, lab_false);
9607 t = build3 (COND_EXPR, void_type_node, t, u, NULL_TREE);
9608 gimplify_and_add (t, pre_p);
9610 t = sav;
9611 if (sav_ofs)
9612 t = fold_build_pointer_plus_hwi (sav, sav_ofs);
9614 u = build2 (POSTINCREMENT_EXPR, TREE_TYPE (reg), unshare_expr (reg),
9615 build_int_cst (TREE_TYPE (reg), n_reg));
9616 u = fold_convert (sizetype, u);
9617 u = build2 (MULT_EXPR, sizetype, u, size_int (sav_scale));
9618 t = fold_build_pointer_plus (t, u);
9620 /* _Decimal32 varargs are located in the second word of the 64-bit
9621 FP register for 32-bit binaries. */
9622 if (!TARGET_POWERPC64
9623 && TARGET_HARD_FLOAT && TARGET_FPRS
9624 && TYPE_MODE (type) == SDmode)
9625 t = fold_build_pointer_plus_hwi (t, size);
9627 gimplify_assign (addr, t, pre_p);
9629 gimple_seq_add_stmt (pre_p, gimple_build_goto (lab_over));
9631 stmt = gimple_build_label (lab_false);
9632 gimple_seq_add_stmt (pre_p, stmt);
9634 if ((n_reg == 2 && !regalign) || n_reg > 2)
9636 /* Ensure that we don't find any more args in regs.
9637 Alignment has taken care of for special cases. */
9638 gimplify_assign (reg, build_int_cst (TREE_TYPE (reg), 8), pre_p);
9642 /* ... otherwise out of the overflow area. */
9644 /* Care for on-stack alignment if needed. */
9645 t = ovf;
9646 if (align != 1)
9648 t = fold_build_pointer_plus_hwi (t, align - 1);
9649 t = build2 (BIT_AND_EXPR, TREE_TYPE (t), t,
9650 build_int_cst (TREE_TYPE (t), -align));
9652 gimplify_expr (&t, pre_p, NULL, is_gimple_val, fb_rvalue);
9654 gimplify_assign (unshare_expr (addr), t, pre_p);
9656 t = fold_build_pointer_plus_hwi (t, size);
9657 gimplify_assign (unshare_expr (ovf), t, pre_p);
9659 if (lab_over)
9661 stmt = gimple_build_label (lab_over);
9662 gimple_seq_add_stmt (pre_p, stmt);
9665 if (STRICT_ALIGNMENT
9666 && (TYPE_ALIGN (type)
9667 > (unsigned) BITS_PER_UNIT * (align < 4 ? 4 : align)))
9669 /* The value (of type complex double, for example) may not be
9670 aligned in memory in the saved registers, so copy via a
9671 temporary. (This is the same code as used for SPARC.) */
9672 tree tmp = create_tmp_var (type, "va_arg_tmp");
9673 tree dest_addr = build_fold_addr_expr (tmp);
9675 tree copy = build_call_expr (builtin_decl_implicit (BUILT_IN_MEMCPY),
9676 3, dest_addr, addr, size_int (rsize * 4));
9678 gimplify_and_add (copy, pre_p);
9679 addr = dest_addr;
9682 addr = fold_convert (ptrtype, addr);
9683 return build_va_arg_indirect_ref (addr);
9686 /* Builtins. */
9688 static void
9689 def_builtin (const char *name, tree type, enum rs6000_builtins code)
9691 tree t;
9692 unsigned classify = rs6000_builtin_info[(int)code].attr;
9693 const char *attr_string = "";
9695 gcc_assert (name != NULL);
9696 gcc_assert (IN_RANGE ((int)code, 0, (int)RS6000_BUILTIN_COUNT));
9698 if (rs6000_builtin_decls[(int)code])
9699 fatal_error ("internal error: builtin function %s already processed", name);
9701 rs6000_builtin_decls[(int)code] = t =
9702 add_builtin_function (name, type, (int)code, BUILT_IN_MD, NULL, NULL_TREE);
9704 /* Set any special attributes. */
9705 if ((classify & RS6000_BTC_CONST) != 0)
9707 /* const function, function only depends on the inputs. */
9708 TREE_READONLY (t) = 1;
9709 TREE_NOTHROW (t) = 1;
9710 attr_string = ", pure";
9712 else if ((classify & RS6000_BTC_PURE) != 0)
9714 /* pure function, function can read global memory, but does not set any
9715 external state. */
9716 DECL_PURE_P (t) = 1;
9717 TREE_NOTHROW (t) = 1;
9718 attr_string = ", const";
9720 else if ((classify & RS6000_BTC_FP) != 0)
9722 /* Function is a math function. If rounding mode is on, then treat the
9723 function as not reading global memory, but it can have arbitrary side
9724 effects. If it is off, then assume the function is a const function.
9725 This mimics the ATTR_MATHFN_FPROUNDING attribute in
9726 builtin-attribute.def that is used for the math functions. */
9727 TREE_NOTHROW (t) = 1;
9728 if (flag_rounding_math)
9730 DECL_PURE_P (t) = 1;
9731 DECL_IS_NOVOPS (t) = 1;
9732 attr_string = ", fp, pure";
9734 else
9736 TREE_READONLY (t) = 1;
9737 attr_string = ", fp, const";
9740 else if ((classify & RS6000_BTC_ATTR_MASK) != 0)
9741 gcc_unreachable ();
9743 if (TARGET_DEBUG_BUILTIN)
9744 fprintf (stderr, "rs6000_builtin, code = %4d, %s%s\n",
9745 (int)code, name, attr_string);
9748 /* Simple ternary operations: VECd = foo (VECa, VECb, VECc). */
9750 #undef RS6000_BUILTIN_1
9751 #undef RS6000_BUILTIN_2
9752 #undef RS6000_BUILTIN_3
9753 #undef RS6000_BUILTIN_A
9754 #undef RS6000_BUILTIN_D
9755 #undef RS6000_BUILTIN_E
9756 #undef RS6000_BUILTIN_P
9757 #undef RS6000_BUILTIN_Q
9758 #undef RS6000_BUILTIN_S
9759 #undef RS6000_BUILTIN_X
9761 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
9762 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
9763 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE) \
9764 { MASK, ICODE, NAME, ENUM },
9766 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
9767 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
9768 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
9769 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
9770 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
9771 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
9772 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
9774 static const struct builtin_description bdesc_3arg[] =
9776 #include "rs6000-builtin.def"
9779 /* DST operations: void foo (void *, const int, const char). */
9781 #undef RS6000_BUILTIN_1
9782 #undef RS6000_BUILTIN_2
9783 #undef RS6000_BUILTIN_3
9784 #undef RS6000_BUILTIN_A
9785 #undef RS6000_BUILTIN_D
9786 #undef RS6000_BUILTIN_E
9787 #undef RS6000_BUILTIN_P
9788 #undef RS6000_BUILTIN_Q
9789 #undef RS6000_BUILTIN_S
9790 #undef RS6000_BUILTIN_X
9792 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
9793 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
9794 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
9795 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
9796 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE) \
9797 { MASK, ICODE, NAME, ENUM },
9799 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
9800 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
9801 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
9802 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
9803 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
9805 static const struct builtin_description bdesc_dst[] =
9807 #include "rs6000-builtin.def"
9810 /* Simple binary operations: VECc = foo (VECa, VECb). */
9812 #undef RS6000_BUILTIN_1
9813 #undef RS6000_BUILTIN_2
9814 #undef RS6000_BUILTIN_3
9815 #undef RS6000_BUILTIN_A
9816 #undef RS6000_BUILTIN_D
9817 #undef RS6000_BUILTIN_E
9818 #undef RS6000_BUILTIN_P
9819 #undef RS6000_BUILTIN_Q
9820 #undef RS6000_BUILTIN_S
9821 #undef RS6000_BUILTIN_X
9823 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
9824 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE) \
9825 { MASK, ICODE, NAME, ENUM },
9827 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
9828 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
9829 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
9830 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
9831 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
9832 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
9833 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
9834 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
9836 static const struct builtin_description bdesc_2arg[] =
9838 #include "rs6000-builtin.def"
9841 #undef RS6000_BUILTIN_1
9842 #undef RS6000_BUILTIN_2
9843 #undef RS6000_BUILTIN_3
9844 #undef RS6000_BUILTIN_A
9845 #undef RS6000_BUILTIN_D
9846 #undef RS6000_BUILTIN_E
9847 #undef RS6000_BUILTIN_P
9848 #undef RS6000_BUILTIN_Q
9849 #undef RS6000_BUILTIN_S
9850 #undef RS6000_BUILTIN_X
9852 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
9853 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
9854 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
9855 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
9856 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
9857 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
9858 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE) \
9859 { MASK, ICODE, NAME, ENUM },
9861 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
9862 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
9863 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
9865 /* AltiVec predicates. */
9867 static const struct builtin_description bdesc_altivec_preds[] =
9869 #include "rs6000-builtin.def"
9872 /* SPE predicates. */
9873 #undef RS6000_BUILTIN_1
9874 #undef RS6000_BUILTIN_2
9875 #undef RS6000_BUILTIN_3
9876 #undef RS6000_BUILTIN_A
9877 #undef RS6000_BUILTIN_D
9878 #undef RS6000_BUILTIN_E
9879 #undef RS6000_BUILTIN_P
9880 #undef RS6000_BUILTIN_Q
9881 #undef RS6000_BUILTIN_S
9882 #undef RS6000_BUILTIN_X
9884 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
9885 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
9886 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
9887 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
9888 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
9889 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
9890 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
9891 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
9892 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE) \
9893 { MASK, ICODE, NAME, ENUM },
9895 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
9897 static const struct builtin_description bdesc_spe_predicates[] =
9899 #include "rs6000-builtin.def"
9902 /* SPE evsel predicates. */
9903 #undef RS6000_BUILTIN_1
9904 #undef RS6000_BUILTIN_2
9905 #undef RS6000_BUILTIN_3
9906 #undef RS6000_BUILTIN_A
9907 #undef RS6000_BUILTIN_D
9908 #undef RS6000_BUILTIN_E
9909 #undef RS6000_BUILTIN_P
9910 #undef RS6000_BUILTIN_Q
9911 #undef RS6000_BUILTIN_S
9912 #undef RS6000_BUILTIN_X
9914 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
9915 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
9916 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
9917 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
9918 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
9919 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE) \
9920 { MASK, ICODE, NAME, ENUM },
9922 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
9923 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
9924 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
9925 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
9927 static const struct builtin_description bdesc_spe_evsel[] =
9929 #include "rs6000-builtin.def"
9932 /* PAIRED predicates. */
9933 #undef RS6000_BUILTIN_1
9934 #undef RS6000_BUILTIN_2
9935 #undef RS6000_BUILTIN_3
9936 #undef RS6000_BUILTIN_A
9937 #undef RS6000_BUILTIN_D
9938 #undef RS6000_BUILTIN_E
9939 #undef RS6000_BUILTIN_P
9940 #undef RS6000_BUILTIN_Q
9941 #undef RS6000_BUILTIN_S
9942 #undef RS6000_BUILTIN_X
9944 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
9945 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
9946 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
9947 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
9948 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
9949 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
9950 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
9951 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE) \
9952 { MASK, ICODE, NAME, ENUM },
9954 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
9955 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
9957 static const struct builtin_description bdesc_paired_preds[] =
9959 #include "rs6000-builtin.def"
9962 /* ABS* operations. */
9964 #undef RS6000_BUILTIN_1
9965 #undef RS6000_BUILTIN_2
9966 #undef RS6000_BUILTIN_3
9967 #undef RS6000_BUILTIN_A
9968 #undef RS6000_BUILTIN_D
9969 #undef RS6000_BUILTIN_E
9970 #undef RS6000_BUILTIN_P
9971 #undef RS6000_BUILTIN_Q
9972 #undef RS6000_BUILTIN_S
9973 #undef RS6000_BUILTIN_X
9975 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
9976 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
9977 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
9978 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE) \
9979 { MASK, ICODE, NAME, ENUM },
9981 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
9982 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
9983 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
9984 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
9985 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
9986 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
9988 static const struct builtin_description bdesc_abs[] =
9990 #include "rs6000-builtin.def"
9993 /* Simple unary operations: VECb = foo (unsigned literal) or VECb =
9994 foo (VECa). */
9996 #undef RS6000_BUILTIN_1
9997 #undef RS6000_BUILTIN_2
9998 #undef RS6000_BUILTIN_3
9999 #undef RS6000_BUILTIN_A
10000 #undef RS6000_BUILTIN_E
10001 #undef RS6000_BUILTIN_D
10002 #undef RS6000_BUILTIN_P
10003 #undef RS6000_BUILTIN_Q
10004 #undef RS6000_BUILTIN_S
10005 #undef RS6000_BUILTIN_X
10007 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE) \
10008 { MASK, ICODE, NAME, ENUM },
10010 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
10011 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
10012 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
10013 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
10014 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
10015 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
10016 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
10017 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
10018 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
10020 static const struct builtin_description bdesc_1arg[] =
10022 #include "rs6000-builtin.def"
10025 #undef RS6000_BUILTIN_1
10026 #undef RS6000_BUILTIN_2
10027 #undef RS6000_BUILTIN_3
10028 #undef RS6000_BUILTIN_A
10029 #undef RS6000_BUILTIN_D
10030 #undef RS6000_BUILTIN_E
10031 #undef RS6000_BUILTIN_P
10032 #undef RS6000_BUILTIN_Q
10033 #undef RS6000_BUILTIN_S
10034 #undef RS6000_BUILTIN_X
10036 /* Return true if a builtin function is overloaded. */
10037 bool
10038 rs6000_overloaded_builtin_p (enum rs6000_builtins fncode)
10040 return (rs6000_builtin_info[(int)fncode].attr & RS6000_BTC_OVERLOADED) != 0;
10043 /* Expand an expression EXP that calls a builtin without arguments. */
10044 static rtx
10045 rs6000_expand_zeroop_builtin (enum insn_code icode, rtx target)
10047 rtx pat;
10048 enum machine_mode tmode = insn_data[icode].operand[0].mode;
10050 if (icode == CODE_FOR_nothing)
10051 /* Builtin not supported on this processor. */
10052 return 0;
10054 if (target == 0
10055 || GET_MODE (target) != tmode
10056 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
10057 target = gen_reg_rtx (tmode);
10059 pat = GEN_FCN (icode) (target);
10060 if (! pat)
10061 return 0;
10062 emit_insn (pat);
10064 return target;
10068 static rtx
10069 rs6000_expand_unop_builtin (enum insn_code icode, tree exp, rtx target)
10071 rtx pat;
10072 tree arg0 = CALL_EXPR_ARG (exp, 0);
10073 rtx op0 = expand_normal (arg0);
10074 enum machine_mode tmode = insn_data[icode].operand[0].mode;
10075 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
10077 if (icode == CODE_FOR_nothing)
10078 /* Builtin not supported on this processor. */
10079 return 0;
10081 /* If we got invalid arguments bail out before generating bad rtl. */
10082 if (arg0 == error_mark_node)
10083 return const0_rtx;
10085 if (icode == CODE_FOR_altivec_vspltisb
10086 || icode == CODE_FOR_altivec_vspltish
10087 || icode == CODE_FOR_altivec_vspltisw
10088 || icode == CODE_FOR_spe_evsplatfi
10089 || icode == CODE_FOR_spe_evsplati)
10091 /* Only allow 5-bit *signed* literals. */
10092 if (GET_CODE (op0) != CONST_INT
10093 || INTVAL (op0) > 15
10094 || INTVAL (op0) < -16)
10096 error ("argument 1 must be a 5-bit signed literal");
10097 return const0_rtx;
10101 if (target == 0
10102 || GET_MODE (target) != tmode
10103 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
10104 target = gen_reg_rtx (tmode);
10106 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
10107 op0 = copy_to_mode_reg (mode0, op0);
10109 pat = GEN_FCN (icode) (target, op0);
10110 if (! pat)
10111 return 0;
10112 emit_insn (pat);
10114 return target;
10117 static rtx
10118 altivec_expand_abs_builtin (enum insn_code icode, tree exp, rtx target)
10120 rtx pat, scratch1, scratch2;
10121 tree arg0 = CALL_EXPR_ARG (exp, 0);
10122 rtx op0 = expand_normal (arg0);
10123 enum machine_mode tmode = insn_data[icode].operand[0].mode;
10124 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
10126 /* If we have invalid arguments, bail out before generating bad rtl. */
10127 if (arg0 == error_mark_node)
10128 return const0_rtx;
10130 if (target == 0
10131 || GET_MODE (target) != tmode
10132 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
10133 target = gen_reg_rtx (tmode);
10135 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
10136 op0 = copy_to_mode_reg (mode0, op0);
10138 scratch1 = gen_reg_rtx (mode0);
10139 scratch2 = gen_reg_rtx (mode0);
10141 pat = GEN_FCN (icode) (target, op0, scratch1, scratch2);
10142 if (! pat)
10143 return 0;
10144 emit_insn (pat);
10146 return target;
10149 static rtx
10150 rs6000_expand_binop_builtin (enum insn_code icode, tree exp, rtx target)
10152 rtx pat;
10153 tree arg0 = CALL_EXPR_ARG (exp, 0);
10154 tree arg1 = CALL_EXPR_ARG (exp, 1);
10155 rtx op0 = expand_normal (arg0);
10156 rtx op1 = expand_normal (arg1);
10157 enum machine_mode tmode = insn_data[icode].operand[0].mode;
10158 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
10159 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
10161 if (icode == CODE_FOR_nothing)
10162 /* Builtin not supported on this processor. */
10163 return 0;
10165 /* If we got invalid arguments bail out before generating bad rtl. */
10166 if (arg0 == error_mark_node || arg1 == error_mark_node)
10167 return const0_rtx;
10169 if (icode == CODE_FOR_altivec_vcfux
10170 || icode == CODE_FOR_altivec_vcfsx
10171 || icode == CODE_FOR_altivec_vctsxs
10172 || icode == CODE_FOR_altivec_vctuxs
10173 || icode == CODE_FOR_altivec_vspltb
10174 || icode == CODE_FOR_altivec_vsplth
10175 || icode == CODE_FOR_altivec_vspltw
10176 || icode == CODE_FOR_spe_evaddiw
10177 || icode == CODE_FOR_spe_evldd
10178 || icode == CODE_FOR_spe_evldh
10179 || icode == CODE_FOR_spe_evldw
10180 || icode == CODE_FOR_spe_evlhhesplat
10181 || icode == CODE_FOR_spe_evlhhossplat
10182 || icode == CODE_FOR_spe_evlhhousplat
10183 || icode == CODE_FOR_spe_evlwhe
10184 || icode == CODE_FOR_spe_evlwhos
10185 || icode == CODE_FOR_spe_evlwhou
10186 || icode == CODE_FOR_spe_evlwhsplat
10187 || icode == CODE_FOR_spe_evlwwsplat
10188 || icode == CODE_FOR_spe_evrlwi
10189 || icode == CODE_FOR_spe_evslwi
10190 || icode == CODE_FOR_spe_evsrwis
10191 || icode == CODE_FOR_spe_evsubifw
10192 || icode == CODE_FOR_spe_evsrwiu)
10194 /* Only allow 5-bit unsigned literals. */
10195 STRIP_NOPS (arg1);
10196 if (TREE_CODE (arg1) != INTEGER_CST
10197 || TREE_INT_CST_LOW (arg1) & ~0x1f)
10199 error ("argument 2 must be a 5-bit unsigned literal");
10200 return const0_rtx;
10204 if (target == 0
10205 || GET_MODE (target) != tmode
10206 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
10207 target = gen_reg_rtx (tmode);
10209 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
10210 op0 = copy_to_mode_reg (mode0, op0);
10211 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
10212 op1 = copy_to_mode_reg (mode1, op1);
10214 pat = GEN_FCN (icode) (target, op0, op1);
10215 if (! pat)
10216 return 0;
10217 emit_insn (pat);
10219 return target;
10222 static rtx
10223 altivec_expand_predicate_builtin (enum insn_code icode, tree exp, rtx target)
10225 rtx pat, scratch;
10226 tree cr6_form = CALL_EXPR_ARG (exp, 0);
10227 tree arg0 = CALL_EXPR_ARG (exp, 1);
10228 tree arg1 = CALL_EXPR_ARG (exp, 2);
10229 rtx op0 = expand_normal (arg0);
10230 rtx op1 = expand_normal (arg1);
10231 enum machine_mode tmode = SImode;
10232 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
10233 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
10234 int cr6_form_int;
10236 if (TREE_CODE (cr6_form) != INTEGER_CST)
10238 error ("argument 1 of __builtin_altivec_predicate must be a constant");
10239 return const0_rtx;
10241 else
10242 cr6_form_int = TREE_INT_CST_LOW (cr6_form);
10244 gcc_assert (mode0 == mode1);
10246 /* If we have invalid arguments, bail out before generating bad rtl. */
10247 if (arg0 == error_mark_node || arg1 == error_mark_node)
10248 return const0_rtx;
10250 if (target == 0
10251 || GET_MODE (target) != tmode
10252 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
10253 target = gen_reg_rtx (tmode);
10255 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
10256 op0 = copy_to_mode_reg (mode0, op0);
10257 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
10258 op1 = copy_to_mode_reg (mode1, op1);
10260 scratch = gen_reg_rtx (mode0);
10262 pat = GEN_FCN (icode) (scratch, op0, op1);
10263 if (! pat)
10264 return 0;
10265 emit_insn (pat);
10267 /* The vec_any* and vec_all* predicates use the same opcodes for two
10268 different operations, but the bits in CR6 will be different
10269 depending on what information we want. So we have to play tricks
10270 with CR6 to get the right bits out.
10272 If you think this is disgusting, look at the specs for the
10273 AltiVec predicates. */
10275 switch (cr6_form_int)
10277 case 0:
10278 emit_insn (gen_cr6_test_for_zero (target));
10279 break;
10280 case 1:
10281 emit_insn (gen_cr6_test_for_zero_reverse (target));
10282 break;
10283 case 2:
10284 emit_insn (gen_cr6_test_for_lt (target));
10285 break;
10286 case 3:
10287 emit_insn (gen_cr6_test_for_lt_reverse (target));
10288 break;
10289 default:
10290 error ("argument 1 of __builtin_altivec_predicate is out of range");
10291 break;
10294 return target;
10297 static rtx
10298 paired_expand_lv_builtin (enum insn_code icode, tree exp, rtx target)
10300 rtx pat, addr;
10301 tree arg0 = CALL_EXPR_ARG (exp, 0);
10302 tree arg1 = CALL_EXPR_ARG (exp, 1);
10303 enum machine_mode tmode = insn_data[icode].operand[0].mode;
10304 enum machine_mode mode0 = Pmode;
10305 enum machine_mode mode1 = Pmode;
10306 rtx op0 = expand_normal (arg0);
10307 rtx op1 = expand_normal (arg1);
10309 if (icode == CODE_FOR_nothing)
10310 /* Builtin not supported on this processor. */
10311 return 0;
10313 /* If we got invalid arguments bail out before generating bad rtl. */
10314 if (arg0 == error_mark_node || arg1 == error_mark_node)
10315 return const0_rtx;
10317 if (target == 0
10318 || GET_MODE (target) != tmode
10319 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
10320 target = gen_reg_rtx (tmode);
10322 op1 = copy_to_mode_reg (mode1, op1);
10324 if (op0 == const0_rtx)
10326 addr = gen_rtx_MEM (tmode, op1);
10328 else
10330 op0 = copy_to_mode_reg (mode0, op0);
10331 addr = gen_rtx_MEM (tmode, gen_rtx_PLUS (Pmode, op0, op1));
10334 pat = GEN_FCN (icode) (target, addr);
10336 if (! pat)
10337 return 0;
10338 emit_insn (pat);
10340 return target;
10343 static rtx
10344 altivec_expand_lv_builtin (enum insn_code icode, tree exp, rtx target, bool blk)
10346 rtx pat, addr;
10347 tree arg0 = CALL_EXPR_ARG (exp, 0);
10348 tree arg1 = CALL_EXPR_ARG (exp, 1);
10349 enum machine_mode tmode = insn_data[icode].operand[0].mode;
10350 enum machine_mode mode0 = Pmode;
10351 enum machine_mode mode1 = Pmode;
10352 rtx op0 = expand_normal (arg0);
10353 rtx op1 = expand_normal (arg1);
10355 if (icode == CODE_FOR_nothing)
10356 /* Builtin not supported on this processor. */
10357 return 0;
10359 /* If we got invalid arguments bail out before generating bad rtl. */
10360 if (arg0 == error_mark_node || arg1 == error_mark_node)
10361 return const0_rtx;
10363 if (target == 0
10364 || GET_MODE (target) != tmode
10365 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
10366 target = gen_reg_rtx (tmode);
10368 op1 = copy_to_mode_reg (mode1, op1);
10370 if (op0 == const0_rtx)
10372 addr = gen_rtx_MEM (blk ? BLKmode : tmode, op1);
10374 else
10376 op0 = copy_to_mode_reg (mode0, op0);
10377 addr = gen_rtx_MEM (blk ? BLKmode : tmode, gen_rtx_PLUS (Pmode, op0, op1));
10380 pat = GEN_FCN (icode) (target, addr);
10382 if (! pat)
10383 return 0;
10384 emit_insn (pat);
10386 return target;
10389 static rtx
10390 spe_expand_stv_builtin (enum insn_code icode, tree exp)
10392 tree arg0 = CALL_EXPR_ARG (exp, 0);
10393 tree arg1 = CALL_EXPR_ARG (exp, 1);
10394 tree arg2 = CALL_EXPR_ARG (exp, 2);
10395 rtx op0 = expand_normal (arg0);
10396 rtx op1 = expand_normal (arg1);
10397 rtx op2 = expand_normal (arg2);
10398 rtx pat;
10399 enum machine_mode mode0 = insn_data[icode].operand[0].mode;
10400 enum machine_mode mode1 = insn_data[icode].operand[1].mode;
10401 enum machine_mode mode2 = insn_data[icode].operand[2].mode;
10403 /* Invalid arguments. Bail before doing anything stoopid! */
10404 if (arg0 == error_mark_node
10405 || arg1 == error_mark_node
10406 || arg2 == error_mark_node)
10407 return const0_rtx;
10409 if (! (*insn_data[icode].operand[2].predicate) (op0, mode2))
10410 op0 = copy_to_mode_reg (mode2, op0);
10411 if (! (*insn_data[icode].operand[0].predicate) (op1, mode0))
10412 op1 = copy_to_mode_reg (mode0, op1);
10413 if (! (*insn_data[icode].operand[1].predicate) (op2, mode1))
10414 op2 = copy_to_mode_reg (mode1, op2);
10416 pat = GEN_FCN (icode) (op1, op2, op0);
10417 if (pat)
10418 emit_insn (pat);
10419 return NULL_RTX;
10422 static rtx
10423 paired_expand_stv_builtin (enum insn_code icode, tree exp)
10425 tree arg0 = CALL_EXPR_ARG (exp, 0);
10426 tree arg1 = CALL_EXPR_ARG (exp, 1);
10427 tree arg2 = CALL_EXPR_ARG (exp, 2);
10428 rtx op0 = expand_normal (arg0);
10429 rtx op1 = expand_normal (arg1);
10430 rtx op2 = expand_normal (arg2);
10431 rtx pat, addr;
10432 enum machine_mode tmode = insn_data[icode].operand[0].mode;
10433 enum machine_mode mode1 = Pmode;
10434 enum machine_mode mode2 = Pmode;
10436 /* Invalid arguments. Bail before doing anything stoopid! */
10437 if (arg0 == error_mark_node
10438 || arg1 == error_mark_node
10439 || arg2 == error_mark_node)
10440 return const0_rtx;
10442 if (! (*insn_data[icode].operand[1].predicate) (op0, tmode))
10443 op0 = copy_to_mode_reg (tmode, op0);
10445 op2 = copy_to_mode_reg (mode2, op2);
10447 if (op1 == const0_rtx)
10449 addr = gen_rtx_MEM (tmode, op2);
10451 else
10453 op1 = copy_to_mode_reg (mode1, op1);
10454 addr = gen_rtx_MEM (tmode, gen_rtx_PLUS (Pmode, op1, op2));
10457 pat = GEN_FCN (icode) (addr, op0);
10458 if (pat)
10459 emit_insn (pat);
10460 return NULL_RTX;
10463 static rtx
10464 altivec_expand_stv_builtin (enum insn_code icode, tree exp)
10466 tree arg0 = CALL_EXPR_ARG (exp, 0);
10467 tree arg1 = CALL_EXPR_ARG (exp, 1);
10468 tree arg2 = CALL_EXPR_ARG (exp, 2);
10469 rtx op0 = expand_normal (arg0);
10470 rtx op1 = expand_normal (arg1);
10471 rtx op2 = expand_normal (arg2);
10472 rtx pat, addr;
10473 enum machine_mode tmode = insn_data[icode].operand[0].mode;
10474 enum machine_mode smode = insn_data[icode].operand[1].mode;
10475 enum machine_mode mode1 = Pmode;
10476 enum machine_mode mode2 = Pmode;
10478 /* Invalid arguments. Bail before doing anything stoopid! */
10479 if (arg0 == error_mark_node
10480 || arg1 == error_mark_node
10481 || arg2 == error_mark_node)
10482 return const0_rtx;
10484 if (! (*insn_data[icode].operand[1].predicate) (op0, smode))
10485 op0 = copy_to_mode_reg (smode, op0);
10487 op2 = copy_to_mode_reg (mode2, op2);
10489 if (op1 == const0_rtx)
10491 addr = gen_rtx_MEM (tmode, op2);
10493 else
10495 op1 = copy_to_mode_reg (mode1, op1);
10496 addr = gen_rtx_MEM (tmode, gen_rtx_PLUS (Pmode, op1, op2));
10499 pat = GEN_FCN (icode) (addr, op0);
10500 if (pat)
10501 emit_insn (pat);
10502 return NULL_RTX;
10505 static rtx
10506 rs6000_expand_ternop_builtin (enum insn_code icode, tree exp, rtx target)
10508 rtx pat;
10509 tree arg0 = CALL_EXPR_ARG (exp, 0);
10510 tree arg1 = CALL_EXPR_ARG (exp, 1);
10511 tree arg2 = CALL_EXPR_ARG (exp, 2);
10512 rtx op0 = expand_normal (arg0);
10513 rtx op1 = expand_normal (arg1);
10514 rtx op2 = expand_normal (arg2);
10515 enum machine_mode tmode = insn_data[icode].operand[0].mode;
10516 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
10517 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
10518 enum machine_mode mode2 = insn_data[icode].operand[3].mode;
10520 if (icode == CODE_FOR_nothing)
10521 /* Builtin not supported on this processor. */
10522 return 0;
10524 /* If we got invalid arguments bail out before generating bad rtl. */
10525 if (arg0 == error_mark_node
10526 || arg1 == error_mark_node
10527 || arg2 == error_mark_node)
10528 return const0_rtx;
10530 /* Check and prepare argument depending on the instruction code.
10532 Note that a switch statement instead of the sequence of tests
10533 would be incorrect as many of the CODE_FOR values could be
10534 CODE_FOR_nothing and that would yield multiple alternatives
10535 with identical values. We'd never reach here at runtime in
10536 this case. */
10537 if (icode == CODE_FOR_altivec_vsldoi_v4sf
10538 || icode == CODE_FOR_altivec_vsldoi_v4si
10539 || icode == CODE_FOR_altivec_vsldoi_v8hi
10540 || icode == CODE_FOR_altivec_vsldoi_v16qi)
10542 /* Only allow 4-bit unsigned literals. */
10543 STRIP_NOPS (arg2);
10544 if (TREE_CODE (arg2) != INTEGER_CST
10545 || TREE_INT_CST_LOW (arg2) & ~0xf)
10547 error ("argument 3 must be a 4-bit unsigned literal");
10548 return const0_rtx;
10551 else if (icode == CODE_FOR_vsx_xxpermdi_v2df
10552 || icode == CODE_FOR_vsx_xxpermdi_v2di
10553 || icode == CODE_FOR_vsx_xxsldwi_v16qi
10554 || icode == CODE_FOR_vsx_xxsldwi_v8hi
10555 || icode == CODE_FOR_vsx_xxsldwi_v4si
10556 || icode == CODE_FOR_vsx_xxsldwi_v4sf
10557 || icode == CODE_FOR_vsx_xxsldwi_v2di
10558 || icode == CODE_FOR_vsx_xxsldwi_v2df)
10560 /* Only allow 2-bit unsigned literals. */
10561 STRIP_NOPS (arg2);
10562 if (TREE_CODE (arg2) != INTEGER_CST
10563 || TREE_INT_CST_LOW (arg2) & ~0x3)
10565 error ("argument 3 must be a 2-bit unsigned literal");
10566 return const0_rtx;
10569 else if (icode == CODE_FOR_vsx_set_v2df
10570 || icode == CODE_FOR_vsx_set_v2di)
10572 /* Only allow 1-bit unsigned literals. */
10573 STRIP_NOPS (arg2);
10574 if (TREE_CODE (arg2) != INTEGER_CST
10575 || TREE_INT_CST_LOW (arg2) & ~0x1)
10577 error ("argument 3 must be a 1-bit unsigned literal");
10578 return const0_rtx;
10582 if (target == 0
10583 || GET_MODE (target) != tmode
10584 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
10585 target = gen_reg_rtx (tmode);
10587 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
10588 op0 = copy_to_mode_reg (mode0, op0);
10589 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
10590 op1 = copy_to_mode_reg (mode1, op1);
10591 if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
10592 op2 = copy_to_mode_reg (mode2, op2);
10594 if (TARGET_PAIRED_FLOAT && icode == CODE_FOR_selv2sf4)
10595 pat = GEN_FCN (icode) (target, op0, op1, op2, CONST0_RTX (SFmode));
10596 else
10597 pat = GEN_FCN (icode) (target, op0, op1, op2);
10598 if (! pat)
10599 return 0;
10600 emit_insn (pat);
10602 return target;
10605 /* Expand the lvx builtins. */
10606 static rtx
10607 altivec_expand_ld_builtin (tree exp, rtx target, bool *expandedp)
10609 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
10610 unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
10611 tree arg0;
10612 enum machine_mode tmode, mode0;
10613 rtx pat, op0;
10614 enum insn_code icode;
10616 switch (fcode)
10618 case ALTIVEC_BUILTIN_LD_INTERNAL_16qi:
10619 icode = CODE_FOR_vector_altivec_load_v16qi;
10620 break;
10621 case ALTIVEC_BUILTIN_LD_INTERNAL_8hi:
10622 icode = CODE_FOR_vector_altivec_load_v8hi;
10623 break;
10624 case ALTIVEC_BUILTIN_LD_INTERNAL_4si:
10625 icode = CODE_FOR_vector_altivec_load_v4si;
10626 break;
10627 case ALTIVEC_BUILTIN_LD_INTERNAL_4sf:
10628 icode = CODE_FOR_vector_altivec_load_v4sf;
10629 break;
10630 case ALTIVEC_BUILTIN_LD_INTERNAL_2df:
10631 icode = CODE_FOR_vector_altivec_load_v2df;
10632 break;
10633 case ALTIVEC_BUILTIN_LD_INTERNAL_2di:
10634 icode = CODE_FOR_vector_altivec_load_v2di;
10635 break;
10636 default:
10637 *expandedp = false;
10638 return NULL_RTX;
10641 *expandedp = true;
10643 arg0 = CALL_EXPR_ARG (exp, 0);
10644 op0 = expand_normal (arg0);
10645 tmode = insn_data[icode].operand[0].mode;
10646 mode0 = insn_data[icode].operand[1].mode;
10648 if (target == 0
10649 || GET_MODE (target) != tmode
10650 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
10651 target = gen_reg_rtx (tmode);
10653 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
10654 op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
10656 pat = GEN_FCN (icode) (target, op0);
10657 if (! pat)
10658 return 0;
10659 emit_insn (pat);
10660 return target;
10663 /* Expand the stvx builtins. */
10664 static rtx
10665 altivec_expand_st_builtin (tree exp, rtx target ATTRIBUTE_UNUSED,
10666 bool *expandedp)
10668 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
10669 unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
10670 tree arg0, arg1;
10671 enum machine_mode mode0, mode1;
10672 rtx pat, op0, op1;
10673 enum insn_code icode;
10675 switch (fcode)
10677 case ALTIVEC_BUILTIN_ST_INTERNAL_16qi:
10678 icode = CODE_FOR_vector_altivec_store_v16qi;
10679 break;
10680 case ALTIVEC_BUILTIN_ST_INTERNAL_8hi:
10681 icode = CODE_FOR_vector_altivec_store_v8hi;
10682 break;
10683 case ALTIVEC_BUILTIN_ST_INTERNAL_4si:
10684 icode = CODE_FOR_vector_altivec_store_v4si;
10685 break;
10686 case ALTIVEC_BUILTIN_ST_INTERNAL_4sf:
10687 icode = CODE_FOR_vector_altivec_store_v4sf;
10688 break;
10689 case ALTIVEC_BUILTIN_ST_INTERNAL_2df:
10690 icode = CODE_FOR_vector_altivec_store_v2df;
10691 break;
10692 case ALTIVEC_BUILTIN_ST_INTERNAL_2di:
10693 icode = CODE_FOR_vector_altivec_store_v2di;
10694 break;
10695 default:
10696 *expandedp = false;
10697 return NULL_RTX;
10700 arg0 = CALL_EXPR_ARG (exp, 0);
10701 arg1 = CALL_EXPR_ARG (exp, 1);
10702 op0 = expand_normal (arg0);
10703 op1 = expand_normal (arg1);
10704 mode0 = insn_data[icode].operand[0].mode;
10705 mode1 = insn_data[icode].operand[1].mode;
10707 if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
10708 op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
10709 if (! (*insn_data[icode].operand[1].predicate) (op1, mode1))
10710 op1 = copy_to_mode_reg (mode1, op1);
10712 pat = GEN_FCN (icode) (op0, op1);
10713 if (pat)
10714 emit_insn (pat);
10716 *expandedp = true;
10717 return NULL_RTX;
10720 /* Expand the dst builtins. */
10721 static rtx
10722 altivec_expand_dst_builtin (tree exp, rtx target ATTRIBUTE_UNUSED,
10723 bool *expandedp)
10725 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
10726 enum rs6000_builtins fcode = (enum rs6000_builtins) DECL_FUNCTION_CODE (fndecl);
10727 tree arg0, arg1, arg2;
10728 enum machine_mode mode0, mode1;
10729 rtx pat, op0, op1, op2;
10730 const struct builtin_description *d;
10731 size_t i;
10733 *expandedp = false;
10735 /* Handle DST variants. */
10736 d = bdesc_dst;
10737 for (i = 0; i < ARRAY_SIZE (bdesc_dst); i++, d++)
10738 if (d->code == fcode)
10740 arg0 = CALL_EXPR_ARG (exp, 0);
10741 arg1 = CALL_EXPR_ARG (exp, 1);
10742 arg2 = CALL_EXPR_ARG (exp, 2);
10743 op0 = expand_normal (arg0);
10744 op1 = expand_normal (arg1);
10745 op2 = expand_normal (arg2);
10746 mode0 = insn_data[d->icode].operand[0].mode;
10747 mode1 = insn_data[d->icode].operand[1].mode;
10749 /* Invalid arguments, bail out before generating bad rtl. */
10750 if (arg0 == error_mark_node
10751 || arg1 == error_mark_node
10752 || arg2 == error_mark_node)
10753 return const0_rtx;
10755 *expandedp = true;
10756 STRIP_NOPS (arg2);
10757 if (TREE_CODE (arg2) != INTEGER_CST
10758 || TREE_INT_CST_LOW (arg2) & ~0x3)
10760 error ("argument to %qs must be a 2-bit unsigned literal", d->name);
10761 return const0_rtx;
10764 if (! (*insn_data[d->icode].operand[0].predicate) (op0, mode0))
10765 op0 = copy_to_mode_reg (Pmode, op0);
10766 if (! (*insn_data[d->icode].operand[1].predicate) (op1, mode1))
10767 op1 = copy_to_mode_reg (mode1, op1);
10769 pat = GEN_FCN (d->icode) (op0, op1, op2);
10770 if (pat != 0)
10771 emit_insn (pat);
10773 return NULL_RTX;
10776 return NULL_RTX;
10779 /* Expand vec_init builtin. */
10780 static rtx
10781 altivec_expand_vec_init_builtin (tree type, tree exp, rtx target)
10783 enum machine_mode tmode = TYPE_MODE (type);
10784 enum machine_mode inner_mode = GET_MODE_INNER (tmode);
10785 int i, n_elt = GET_MODE_NUNITS (tmode);
10786 rtvec v = rtvec_alloc (n_elt);
10788 gcc_assert (VECTOR_MODE_P (tmode));
10789 gcc_assert (n_elt == call_expr_nargs (exp));
10791 for (i = 0; i < n_elt; ++i)
10793 rtx x = expand_normal (CALL_EXPR_ARG (exp, i));
10794 RTVEC_ELT (v, i) = gen_lowpart (inner_mode, x);
10797 if (!target || !register_operand (target, tmode))
10798 target = gen_reg_rtx (tmode);
10800 rs6000_expand_vector_init (target, gen_rtx_PARALLEL (tmode, v));
10801 return target;
10804 /* Return the integer constant in ARG. Constrain it to be in the range
10805 of the subparts of VEC_TYPE; issue an error if not. */
10807 static int
10808 get_element_number (tree vec_type, tree arg)
10810 unsigned HOST_WIDE_INT elt, max = TYPE_VECTOR_SUBPARTS (vec_type) - 1;
10812 if (!host_integerp (arg, 1)
10813 || (elt = tree_low_cst (arg, 1), elt > max))
10815 error ("selector must be an integer constant in the range 0..%wi", max);
10816 return 0;
10819 return elt;
10822 /* Expand vec_set builtin. */
10823 static rtx
10824 altivec_expand_vec_set_builtin (tree exp)
10826 enum machine_mode tmode, mode1;
10827 tree arg0, arg1, arg2;
10828 int elt;
10829 rtx op0, op1;
10831 arg0 = CALL_EXPR_ARG (exp, 0);
10832 arg1 = CALL_EXPR_ARG (exp, 1);
10833 arg2 = CALL_EXPR_ARG (exp, 2);
10835 tmode = TYPE_MODE (TREE_TYPE (arg0));
10836 mode1 = TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0)));
10837 gcc_assert (VECTOR_MODE_P (tmode));
10839 op0 = expand_expr (arg0, NULL_RTX, tmode, EXPAND_NORMAL);
10840 op1 = expand_expr (arg1, NULL_RTX, mode1, EXPAND_NORMAL);
10841 elt = get_element_number (TREE_TYPE (arg0), arg2);
10843 if (GET_MODE (op1) != mode1 && GET_MODE (op1) != VOIDmode)
10844 op1 = convert_modes (mode1, GET_MODE (op1), op1, true);
10846 op0 = force_reg (tmode, op0);
10847 op1 = force_reg (mode1, op1);
10849 rs6000_expand_vector_set (op0, op1, elt);
10851 return op0;
10854 /* Expand vec_ext builtin. */
10855 static rtx
10856 altivec_expand_vec_ext_builtin (tree exp, rtx target)
10858 enum machine_mode tmode, mode0;
10859 tree arg0, arg1;
10860 int elt;
10861 rtx op0;
10863 arg0 = CALL_EXPR_ARG (exp, 0);
10864 arg1 = CALL_EXPR_ARG (exp, 1);
10866 op0 = expand_normal (arg0);
10867 elt = get_element_number (TREE_TYPE (arg0), arg1);
10869 tmode = TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0)));
10870 mode0 = TYPE_MODE (TREE_TYPE (arg0));
10871 gcc_assert (VECTOR_MODE_P (mode0));
10873 op0 = force_reg (mode0, op0);
10875 if (optimize || !target || !register_operand (target, tmode))
10876 target = gen_reg_rtx (tmode);
10878 rs6000_expand_vector_extract (target, op0, elt);
10880 return target;
10883 /* Expand the builtin in EXP and store the result in TARGET. Store
10884 true in *EXPANDEDP if we found a builtin to expand. */
10885 static rtx
10886 altivec_expand_builtin (tree exp, rtx target, bool *expandedp)
10888 const struct builtin_description *d;
10889 size_t i;
10890 enum insn_code icode;
10891 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
10892 tree arg0;
10893 rtx op0, pat;
10894 enum machine_mode tmode, mode0;
10895 enum rs6000_builtins fcode
10896 = (enum rs6000_builtins) DECL_FUNCTION_CODE (fndecl);
10898 if (rs6000_overloaded_builtin_p (fcode))
10900 *expandedp = true;
10901 error ("unresolved overload for Altivec builtin %qF", fndecl);
10903 /* Given it is invalid, just generate a normal call. */
10904 return expand_call (exp, target, false);
10907 target = altivec_expand_ld_builtin (exp, target, expandedp);
10908 if (*expandedp)
10909 return target;
10911 target = altivec_expand_st_builtin (exp, target, expandedp);
10912 if (*expandedp)
10913 return target;
10915 target = altivec_expand_dst_builtin (exp, target, expandedp);
10916 if (*expandedp)
10917 return target;
10919 *expandedp = true;
10921 switch (fcode)
10923 case ALTIVEC_BUILTIN_STVX:
10924 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvx_v4si, exp);
10925 case ALTIVEC_BUILTIN_STVEBX:
10926 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvebx, exp);
10927 case ALTIVEC_BUILTIN_STVEHX:
10928 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvehx, exp);
10929 case ALTIVEC_BUILTIN_STVEWX:
10930 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvewx, exp);
10931 case ALTIVEC_BUILTIN_STVXL:
10932 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvxl, exp);
10934 case ALTIVEC_BUILTIN_STVLX:
10935 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvlx, exp);
10936 case ALTIVEC_BUILTIN_STVLXL:
10937 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvlxl, exp);
10938 case ALTIVEC_BUILTIN_STVRX:
10939 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvrx, exp);
10940 case ALTIVEC_BUILTIN_STVRXL:
10941 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvrxl, exp);
10943 case VSX_BUILTIN_STXVD2X_V2DF:
10944 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v2df, exp);
10945 case VSX_BUILTIN_STXVD2X_V2DI:
10946 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v2di, exp);
10947 case VSX_BUILTIN_STXVW4X_V4SF:
10948 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v4sf, exp);
10949 case VSX_BUILTIN_STXVW4X_V4SI:
10950 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v4si, exp);
10951 case VSX_BUILTIN_STXVW4X_V8HI:
10952 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v8hi, exp);
10953 case VSX_BUILTIN_STXVW4X_V16QI:
10954 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v16qi, exp);
10956 case ALTIVEC_BUILTIN_MFVSCR:
10957 icode = CODE_FOR_altivec_mfvscr;
10958 tmode = insn_data[icode].operand[0].mode;
10960 if (target == 0
10961 || GET_MODE (target) != tmode
10962 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
10963 target = gen_reg_rtx (tmode);
10965 pat = GEN_FCN (icode) (target);
10966 if (! pat)
10967 return 0;
10968 emit_insn (pat);
10969 return target;
10971 case ALTIVEC_BUILTIN_MTVSCR:
10972 icode = CODE_FOR_altivec_mtvscr;
10973 arg0 = CALL_EXPR_ARG (exp, 0);
10974 op0 = expand_normal (arg0);
10975 mode0 = insn_data[icode].operand[0].mode;
10977 /* If we got invalid arguments bail out before generating bad rtl. */
10978 if (arg0 == error_mark_node)
10979 return const0_rtx;
10981 if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
10982 op0 = copy_to_mode_reg (mode0, op0);
10984 pat = GEN_FCN (icode) (op0);
10985 if (pat)
10986 emit_insn (pat);
10987 return NULL_RTX;
10989 case ALTIVEC_BUILTIN_DSSALL:
10990 emit_insn (gen_altivec_dssall ());
10991 return NULL_RTX;
10993 case ALTIVEC_BUILTIN_DSS:
10994 icode = CODE_FOR_altivec_dss;
10995 arg0 = CALL_EXPR_ARG (exp, 0);
10996 STRIP_NOPS (arg0);
10997 op0 = expand_normal (arg0);
10998 mode0 = insn_data[icode].operand[0].mode;
11000 /* If we got invalid arguments bail out before generating bad rtl. */
11001 if (arg0 == error_mark_node)
11002 return const0_rtx;
11004 if (TREE_CODE (arg0) != INTEGER_CST
11005 || TREE_INT_CST_LOW (arg0) & ~0x3)
11007 error ("argument to dss must be a 2-bit unsigned literal");
11008 return const0_rtx;
11011 if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
11012 op0 = copy_to_mode_reg (mode0, op0);
11014 emit_insn (gen_altivec_dss (op0));
11015 return NULL_RTX;
11017 case ALTIVEC_BUILTIN_VEC_INIT_V4SI:
11018 case ALTIVEC_BUILTIN_VEC_INIT_V8HI:
11019 case ALTIVEC_BUILTIN_VEC_INIT_V16QI:
11020 case ALTIVEC_BUILTIN_VEC_INIT_V4SF:
11021 case VSX_BUILTIN_VEC_INIT_V2DF:
11022 case VSX_BUILTIN_VEC_INIT_V2DI:
11023 return altivec_expand_vec_init_builtin (TREE_TYPE (exp), exp, target);
11025 case ALTIVEC_BUILTIN_VEC_SET_V4SI:
11026 case ALTIVEC_BUILTIN_VEC_SET_V8HI:
11027 case ALTIVEC_BUILTIN_VEC_SET_V16QI:
11028 case ALTIVEC_BUILTIN_VEC_SET_V4SF:
11029 case VSX_BUILTIN_VEC_SET_V2DF:
11030 case VSX_BUILTIN_VEC_SET_V2DI:
11031 return altivec_expand_vec_set_builtin (exp);
11033 case ALTIVEC_BUILTIN_VEC_EXT_V4SI:
11034 case ALTIVEC_BUILTIN_VEC_EXT_V8HI:
11035 case ALTIVEC_BUILTIN_VEC_EXT_V16QI:
11036 case ALTIVEC_BUILTIN_VEC_EXT_V4SF:
11037 case VSX_BUILTIN_VEC_EXT_V2DF:
11038 case VSX_BUILTIN_VEC_EXT_V2DI:
11039 return altivec_expand_vec_ext_builtin (exp, target);
11041 default:
11042 break;
11043 /* Fall through. */
11046 /* Expand abs* operations. */
11047 d = bdesc_abs;
11048 for (i = 0; i < ARRAY_SIZE (bdesc_abs); i++, d++)
11049 if (d->code == fcode)
11050 return altivec_expand_abs_builtin (d->icode, exp, target);
11052 /* Expand the AltiVec predicates. */
11053 d = bdesc_altivec_preds;
11054 for (i = 0; i < ARRAY_SIZE (bdesc_altivec_preds); i++, d++)
11055 if (d->code == fcode)
11056 return altivec_expand_predicate_builtin (d->icode, exp, target);
11058 /* LV* are funky. We initialized them differently. */
11059 switch (fcode)
11061 case ALTIVEC_BUILTIN_LVSL:
11062 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvsl,
11063 exp, target, false);
11064 case ALTIVEC_BUILTIN_LVSR:
11065 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvsr,
11066 exp, target, false);
11067 case ALTIVEC_BUILTIN_LVEBX:
11068 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvebx,
11069 exp, target, false);
11070 case ALTIVEC_BUILTIN_LVEHX:
11071 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvehx,
11072 exp, target, false);
11073 case ALTIVEC_BUILTIN_LVEWX:
11074 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvewx,
11075 exp, target, false);
11076 case ALTIVEC_BUILTIN_LVXL:
11077 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvxl,
11078 exp, target, false);
11079 case ALTIVEC_BUILTIN_LVX:
11080 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvx_v4si,
11081 exp, target, false);
11082 case ALTIVEC_BUILTIN_LVLX:
11083 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvlx,
11084 exp, target, true);
11085 case ALTIVEC_BUILTIN_LVLXL:
11086 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvlxl,
11087 exp, target, true);
11088 case ALTIVEC_BUILTIN_LVRX:
11089 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvrx,
11090 exp, target, true);
11091 case ALTIVEC_BUILTIN_LVRXL:
11092 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvrxl,
11093 exp, target, true);
11094 case VSX_BUILTIN_LXVD2X_V2DF:
11095 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v2df,
11096 exp, target, false);
11097 case VSX_BUILTIN_LXVD2X_V2DI:
11098 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v2di,
11099 exp, target, false);
11100 case VSX_BUILTIN_LXVW4X_V4SF:
11101 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v4sf,
11102 exp, target, false);
11103 case VSX_BUILTIN_LXVW4X_V4SI:
11104 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v4si,
11105 exp, target, false);
11106 case VSX_BUILTIN_LXVW4X_V8HI:
11107 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v8hi,
11108 exp, target, false);
11109 case VSX_BUILTIN_LXVW4X_V16QI:
11110 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v16qi,
11111 exp, target, false);
11112 break;
11113 default:
11114 break;
11115 /* Fall through. */
11118 *expandedp = false;
11119 return NULL_RTX;
11122 /* Expand the builtin in EXP and store the result in TARGET. Store
11123 true in *EXPANDEDP if we found a builtin to expand. */
11124 static rtx
11125 paired_expand_builtin (tree exp, rtx target, bool * expandedp)
11127 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
11128 enum rs6000_builtins fcode = (enum rs6000_builtins) DECL_FUNCTION_CODE (fndecl);
11129 const struct builtin_description *d;
11130 size_t i;
11132 *expandedp = true;
11134 switch (fcode)
11136 case PAIRED_BUILTIN_STX:
11137 return paired_expand_stv_builtin (CODE_FOR_paired_stx, exp);
11138 case PAIRED_BUILTIN_LX:
11139 return paired_expand_lv_builtin (CODE_FOR_paired_lx, exp, target);
11140 default:
11141 break;
11142 /* Fall through. */
11145 /* Expand the paired predicates. */
11146 d = bdesc_paired_preds;
11147 for (i = 0; i < ARRAY_SIZE (bdesc_paired_preds); i++, d++)
11148 if (d->code == fcode)
11149 return paired_expand_predicate_builtin (d->icode, exp, target);
11151 *expandedp = false;
11152 return NULL_RTX;
11155 /* Binops that need to be initialized manually, but can be expanded
11156 automagically by rs6000_expand_binop_builtin. */
11157 static const struct builtin_description bdesc_2arg_spe[] =
11159 { RS6000_BTM_SPE, CODE_FOR_spe_evlddx, "__builtin_spe_evlddx", SPE_BUILTIN_EVLDDX },
11160 { RS6000_BTM_SPE, CODE_FOR_spe_evldwx, "__builtin_spe_evldwx", SPE_BUILTIN_EVLDWX },
11161 { RS6000_BTM_SPE, CODE_FOR_spe_evldhx, "__builtin_spe_evldhx", SPE_BUILTIN_EVLDHX },
11162 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhex, "__builtin_spe_evlwhex", SPE_BUILTIN_EVLWHEX },
11163 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhoux, "__builtin_spe_evlwhoux", SPE_BUILTIN_EVLWHOUX },
11164 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhosx, "__builtin_spe_evlwhosx", SPE_BUILTIN_EVLWHOSX },
11165 { RS6000_BTM_SPE, CODE_FOR_spe_evlwwsplatx, "__builtin_spe_evlwwsplatx", SPE_BUILTIN_EVLWWSPLATX },
11166 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhsplatx, "__builtin_spe_evlwhsplatx", SPE_BUILTIN_EVLWHSPLATX },
11167 { RS6000_BTM_SPE, CODE_FOR_spe_evlhhesplatx, "__builtin_spe_evlhhesplatx", SPE_BUILTIN_EVLHHESPLATX },
11168 { RS6000_BTM_SPE, CODE_FOR_spe_evlhhousplatx, "__builtin_spe_evlhhousplatx", SPE_BUILTIN_EVLHHOUSPLATX },
11169 { RS6000_BTM_SPE, CODE_FOR_spe_evlhhossplatx, "__builtin_spe_evlhhossplatx", SPE_BUILTIN_EVLHHOSSPLATX },
11170 { RS6000_BTM_SPE, CODE_FOR_spe_evldd, "__builtin_spe_evldd", SPE_BUILTIN_EVLDD },
11171 { RS6000_BTM_SPE, CODE_FOR_spe_evldw, "__builtin_spe_evldw", SPE_BUILTIN_EVLDW },
11172 { RS6000_BTM_SPE, CODE_FOR_spe_evldh, "__builtin_spe_evldh", SPE_BUILTIN_EVLDH },
11173 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhe, "__builtin_spe_evlwhe", SPE_BUILTIN_EVLWHE },
11174 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhou, "__builtin_spe_evlwhou", SPE_BUILTIN_EVLWHOU },
11175 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhos, "__builtin_spe_evlwhos", SPE_BUILTIN_EVLWHOS },
11176 { RS6000_BTM_SPE, CODE_FOR_spe_evlwwsplat, "__builtin_spe_evlwwsplat", SPE_BUILTIN_EVLWWSPLAT },
11177 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhsplat, "__builtin_spe_evlwhsplat", SPE_BUILTIN_EVLWHSPLAT },
11178 { RS6000_BTM_SPE, CODE_FOR_spe_evlhhesplat, "__builtin_spe_evlhhesplat", SPE_BUILTIN_EVLHHESPLAT },
11179 { RS6000_BTM_SPE, CODE_FOR_spe_evlhhousplat, "__builtin_spe_evlhhousplat", SPE_BUILTIN_EVLHHOUSPLAT },
11180 { RS6000_BTM_SPE, CODE_FOR_spe_evlhhossplat, "__builtin_spe_evlhhossplat", SPE_BUILTIN_EVLHHOSSPLAT }
11183 /* Expand the builtin in EXP and store the result in TARGET. Store
11184 true in *EXPANDEDP if we found a builtin to expand.
11186 This expands the SPE builtins that are not simple unary and binary
11187 operations. */
11188 static rtx
11189 spe_expand_builtin (tree exp, rtx target, bool *expandedp)
11191 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
11192 tree arg1, arg0;
11193 enum rs6000_builtins fcode = (enum rs6000_builtins) DECL_FUNCTION_CODE (fndecl);
11194 enum insn_code icode;
11195 enum machine_mode tmode, mode0;
11196 rtx pat, op0;
11197 const struct builtin_description *d;
11198 size_t i;
11200 *expandedp = true;
11202 /* Syntax check for a 5-bit unsigned immediate. */
11203 switch (fcode)
11205 case SPE_BUILTIN_EVSTDD:
11206 case SPE_BUILTIN_EVSTDH:
11207 case SPE_BUILTIN_EVSTDW:
11208 case SPE_BUILTIN_EVSTWHE:
11209 case SPE_BUILTIN_EVSTWHO:
11210 case SPE_BUILTIN_EVSTWWE:
11211 case SPE_BUILTIN_EVSTWWO:
11212 arg1 = CALL_EXPR_ARG (exp, 2);
11213 if (TREE_CODE (arg1) != INTEGER_CST
11214 || TREE_INT_CST_LOW (arg1) & ~0x1f)
11216 error ("argument 2 must be a 5-bit unsigned literal");
11217 return const0_rtx;
11219 break;
11220 default:
11221 break;
11224 /* The evsplat*i instructions are not quite generic. */
11225 switch (fcode)
11227 case SPE_BUILTIN_EVSPLATFI:
11228 return rs6000_expand_unop_builtin (CODE_FOR_spe_evsplatfi,
11229 exp, target);
11230 case SPE_BUILTIN_EVSPLATI:
11231 return rs6000_expand_unop_builtin (CODE_FOR_spe_evsplati,
11232 exp, target);
11233 default:
11234 break;
11237 d = bdesc_2arg_spe;
11238 for (i = 0; i < ARRAY_SIZE (bdesc_2arg_spe); ++i, ++d)
11239 if (d->code == fcode)
11240 return rs6000_expand_binop_builtin (d->icode, exp, target);
11242 d = bdesc_spe_predicates;
11243 for (i = 0; i < ARRAY_SIZE (bdesc_spe_predicates); ++i, ++d)
11244 if (d->code == fcode)
11245 return spe_expand_predicate_builtin (d->icode, exp, target);
11247 d = bdesc_spe_evsel;
11248 for (i = 0; i < ARRAY_SIZE (bdesc_spe_evsel); ++i, ++d)
11249 if (d->code == fcode)
11250 return spe_expand_evsel_builtin (d->icode, exp, target);
11252 switch (fcode)
11254 case SPE_BUILTIN_EVSTDDX:
11255 return spe_expand_stv_builtin (CODE_FOR_spe_evstddx, exp);
11256 case SPE_BUILTIN_EVSTDHX:
11257 return spe_expand_stv_builtin (CODE_FOR_spe_evstdhx, exp);
11258 case SPE_BUILTIN_EVSTDWX:
11259 return spe_expand_stv_builtin (CODE_FOR_spe_evstdwx, exp);
11260 case SPE_BUILTIN_EVSTWHEX:
11261 return spe_expand_stv_builtin (CODE_FOR_spe_evstwhex, exp);
11262 case SPE_BUILTIN_EVSTWHOX:
11263 return spe_expand_stv_builtin (CODE_FOR_spe_evstwhox, exp);
11264 case SPE_BUILTIN_EVSTWWEX:
11265 return spe_expand_stv_builtin (CODE_FOR_spe_evstwwex, exp);
11266 case SPE_BUILTIN_EVSTWWOX:
11267 return spe_expand_stv_builtin (CODE_FOR_spe_evstwwox, exp);
11268 case SPE_BUILTIN_EVSTDD:
11269 return spe_expand_stv_builtin (CODE_FOR_spe_evstdd, exp);
11270 case SPE_BUILTIN_EVSTDH:
11271 return spe_expand_stv_builtin (CODE_FOR_spe_evstdh, exp);
11272 case SPE_BUILTIN_EVSTDW:
11273 return spe_expand_stv_builtin (CODE_FOR_spe_evstdw, exp);
11274 case SPE_BUILTIN_EVSTWHE:
11275 return spe_expand_stv_builtin (CODE_FOR_spe_evstwhe, exp);
11276 case SPE_BUILTIN_EVSTWHO:
11277 return spe_expand_stv_builtin (CODE_FOR_spe_evstwho, exp);
11278 case SPE_BUILTIN_EVSTWWE:
11279 return spe_expand_stv_builtin (CODE_FOR_spe_evstwwe, exp);
11280 case SPE_BUILTIN_EVSTWWO:
11281 return spe_expand_stv_builtin (CODE_FOR_spe_evstwwo, exp);
11282 case SPE_BUILTIN_MFSPEFSCR:
11283 icode = CODE_FOR_spe_mfspefscr;
11284 tmode = insn_data[icode].operand[0].mode;
11286 if (target == 0
11287 || GET_MODE (target) != tmode
11288 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
11289 target = gen_reg_rtx (tmode);
11291 pat = GEN_FCN (icode) (target);
11292 if (! pat)
11293 return 0;
11294 emit_insn (pat);
11295 return target;
11296 case SPE_BUILTIN_MTSPEFSCR:
11297 icode = CODE_FOR_spe_mtspefscr;
11298 arg0 = CALL_EXPR_ARG (exp, 0);
11299 op0 = expand_normal (arg0);
11300 mode0 = insn_data[icode].operand[0].mode;
11302 if (arg0 == error_mark_node)
11303 return const0_rtx;
11305 if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
11306 op0 = copy_to_mode_reg (mode0, op0);
11308 pat = GEN_FCN (icode) (op0);
11309 if (pat)
11310 emit_insn (pat);
11311 return NULL_RTX;
11312 default:
11313 break;
11316 *expandedp = false;
11317 return NULL_RTX;
11320 static rtx
11321 paired_expand_predicate_builtin (enum insn_code icode, tree exp, rtx target)
11323 rtx pat, scratch, tmp;
11324 tree form = CALL_EXPR_ARG (exp, 0);
11325 tree arg0 = CALL_EXPR_ARG (exp, 1);
11326 tree arg1 = CALL_EXPR_ARG (exp, 2);
11327 rtx op0 = expand_normal (arg0);
11328 rtx op1 = expand_normal (arg1);
11329 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
11330 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
11331 int form_int;
11332 enum rtx_code code;
11334 if (TREE_CODE (form) != INTEGER_CST)
11336 error ("argument 1 of __builtin_paired_predicate must be a constant");
11337 return const0_rtx;
11339 else
11340 form_int = TREE_INT_CST_LOW (form);
11342 gcc_assert (mode0 == mode1);
11344 if (arg0 == error_mark_node || arg1 == error_mark_node)
11345 return const0_rtx;
11347 if (target == 0
11348 || GET_MODE (target) != SImode
11349 || !(*insn_data[icode].operand[0].predicate) (target, SImode))
11350 target = gen_reg_rtx (SImode);
11351 if (!(*insn_data[icode].operand[1].predicate) (op0, mode0))
11352 op0 = copy_to_mode_reg (mode0, op0);
11353 if (!(*insn_data[icode].operand[2].predicate) (op1, mode1))
11354 op1 = copy_to_mode_reg (mode1, op1);
11356 scratch = gen_reg_rtx (CCFPmode);
11358 pat = GEN_FCN (icode) (scratch, op0, op1);
11359 if (!pat)
11360 return const0_rtx;
11362 emit_insn (pat);
11364 switch (form_int)
11366 /* LT bit. */
11367 case 0:
11368 code = LT;
11369 break;
11370 /* GT bit. */
11371 case 1:
11372 code = GT;
11373 break;
11374 /* EQ bit. */
11375 case 2:
11376 code = EQ;
11377 break;
11378 /* UN bit. */
11379 case 3:
11380 emit_insn (gen_move_from_CR_ov_bit (target, scratch));
11381 return target;
11382 default:
11383 error ("argument 1 of __builtin_paired_predicate is out of range");
11384 return const0_rtx;
11387 tmp = gen_rtx_fmt_ee (code, SImode, scratch, const0_rtx);
11388 emit_move_insn (target, tmp);
11389 return target;
11392 static rtx
11393 spe_expand_predicate_builtin (enum insn_code icode, tree exp, rtx target)
11395 rtx pat, scratch, tmp;
11396 tree form = CALL_EXPR_ARG (exp, 0);
11397 tree arg0 = CALL_EXPR_ARG (exp, 1);
11398 tree arg1 = CALL_EXPR_ARG (exp, 2);
11399 rtx op0 = expand_normal (arg0);
11400 rtx op1 = expand_normal (arg1);
11401 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
11402 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
11403 int form_int;
11404 enum rtx_code code;
11406 if (TREE_CODE (form) != INTEGER_CST)
11408 error ("argument 1 of __builtin_spe_predicate must be a constant");
11409 return const0_rtx;
11411 else
11412 form_int = TREE_INT_CST_LOW (form);
11414 gcc_assert (mode0 == mode1);
11416 if (arg0 == error_mark_node || arg1 == error_mark_node)
11417 return const0_rtx;
11419 if (target == 0
11420 || GET_MODE (target) != SImode
11421 || ! (*insn_data[icode].operand[0].predicate) (target, SImode))
11422 target = gen_reg_rtx (SImode);
11424 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
11425 op0 = copy_to_mode_reg (mode0, op0);
11426 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
11427 op1 = copy_to_mode_reg (mode1, op1);
11429 scratch = gen_reg_rtx (CCmode);
11431 pat = GEN_FCN (icode) (scratch, op0, op1);
11432 if (! pat)
11433 return const0_rtx;
11434 emit_insn (pat);
11436 /* There are 4 variants for each predicate: _any_, _all_, _upper_,
11437 _lower_. We use one compare, but look in different bits of the
11438 CR for each variant.
11440 There are 2 elements in each SPE simd type (upper/lower). The CR
11441 bits are set as follows:
11443 BIT0 | BIT 1 | BIT 2 | BIT 3
11444 U | L | (U | L) | (U & L)
11446 So, for an "all" relationship, BIT 3 would be set.
11447 For an "any" relationship, BIT 2 would be set. Etc.
11449 Following traditional nomenclature, these bits map to:
11451 BIT0 | BIT 1 | BIT 2 | BIT 3
11452 LT | GT | EQ | OV
11454 Later, we will generate rtl to look in the LT/EQ/EQ/OV bits.
11457 switch (form_int)
11459 /* All variant. OV bit. */
11460 case 0:
11461 /* We need to get to the OV bit, which is the ORDERED bit. We
11462 could generate (ordered:SI (reg:CC xx) (const_int 0)), but
11463 that's ugly and will make validate_condition_mode die.
11464 So let's just use another pattern. */
11465 emit_insn (gen_move_from_CR_ov_bit (target, scratch));
11466 return target;
11467 /* Any variant. EQ bit. */
11468 case 1:
11469 code = EQ;
11470 break;
11471 /* Upper variant. LT bit. */
11472 case 2:
11473 code = LT;
11474 break;
11475 /* Lower variant. GT bit. */
11476 case 3:
11477 code = GT;
11478 break;
11479 default:
11480 error ("argument 1 of __builtin_spe_predicate is out of range");
11481 return const0_rtx;
11484 tmp = gen_rtx_fmt_ee (code, SImode, scratch, const0_rtx);
11485 emit_move_insn (target, tmp);
11487 return target;
11490 /* The evsel builtins look like this:
11492 e = __builtin_spe_evsel_OP (a, b, c, d);
11494 and work like this:
11496 e[upper] = a[upper] *OP* b[upper] ? c[upper] : d[upper];
11497 e[lower] = a[lower] *OP* b[lower] ? c[lower] : d[lower];
11500 static rtx
11501 spe_expand_evsel_builtin (enum insn_code icode, tree exp, rtx target)
11503 rtx pat, scratch;
11504 tree arg0 = CALL_EXPR_ARG (exp, 0);
11505 tree arg1 = CALL_EXPR_ARG (exp, 1);
11506 tree arg2 = CALL_EXPR_ARG (exp, 2);
11507 tree arg3 = CALL_EXPR_ARG (exp, 3);
11508 rtx op0 = expand_normal (arg0);
11509 rtx op1 = expand_normal (arg1);
11510 rtx op2 = expand_normal (arg2);
11511 rtx op3 = expand_normal (arg3);
11512 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
11513 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
11515 gcc_assert (mode0 == mode1);
11517 if (arg0 == error_mark_node || arg1 == error_mark_node
11518 || arg2 == error_mark_node || arg3 == error_mark_node)
11519 return const0_rtx;
11521 if (target == 0
11522 || GET_MODE (target) != mode0
11523 || ! (*insn_data[icode].operand[0].predicate) (target, mode0))
11524 target = gen_reg_rtx (mode0);
11526 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
11527 op0 = copy_to_mode_reg (mode0, op0);
11528 if (! (*insn_data[icode].operand[1].predicate) (op1, mode1))
11529 op1 = copy_to_mode_reg (mode0, op1);
11530 if (! (*insn_data[icode].operand[1].predicate) (op2, mode1))
11531 op2 = copy_to_mode_reg (mode0, op2);
11532 if (! (*insn_data[icode].operand[1].predicate) (op3, mode1))
11533 op3 = copy_to_mode_reg (mode0, op3);
11535 /* Generate the compare. */
11536 scratch = gen_reg_rtx (CCmode);
11537 pat = GEN_FCN (icode) (scratch, op0, op1);
11538 if (! pat)
11539 return const0_rtx;
11540 emit_insn (pat);
11542 if (mode0 == V2SImode)
11543 emit_insn (gen_spe_evsel (target, op2, op3, scratch));
11544 else
11545 emit_insn (gen_spe_evsel_fs (target, op2, op3, scratch));
11547 return target;
11550 /* Raise an error message for a builtin function that is called without the
11551 appropriate target options being set. */
11553 static void
11554 rs6000_invalid_builtin (enum rs6000_builtins fncode)
11556 size_t uns_fncode = (size_t)fncode;
11557 const char *name = rs6000_builtin_info[uns_fncode].name;
11558 HOST_WIDE_INT fnmask = rs6000_builtin_info[uns_fncode].mask;
11560 gcc_assert (name != NULL);
11561 if ((fnmask & RS6000_BTM_CELL) != 0)
11562 error ("Builtin function %s is only valid for the cell processor", name);
11563 else if ((fnmask & RS6000_BTM_VSX) != 0)
11564 error ("Builtin function %s requires the -mvsx option", name);
11565 else if ((fnmask & RS6000_BTM_ALTIVEC) != 0)
11566 error ("Builtin function %s requires the -maltivec option", name);
11567 else if ((fnmask & RS6000_BTM_PAIRED) != 0)
11568 error ("Builtin function %s requires the -mpaired option", name);
11569 else if ((fnmask & RS6000_BTM_SPE) != 0)
11570 error ("Builtin function %s requires the -mspe option", name);
11571 else
11572 error ("Builtin function %s is not supported with the current options",
11573 name);
11576 /* Expand an expression EXP that calls a built-in function,
11577 with result going to TARGET if that's convenient
11578 (and in mode MODE if that's convenient).
11579 SUBTARGET may be used as the target for computing one of EXP's operands.
11580 IGNORE is nonzero if the value is to be ignored. */
11582 static rtx
11583 rs6000_expand_builtin (tree exp, rtx target, rtx subtarget ATTRIBUTE_UNUSED,
11584 enum machine_mode mode ATTRIBUTE_UNUSED,
11585 int ignore ATTRIBUTE_UNUSED)
11587 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
11588 enum rs6000_builtins fcode
11589 = (enum rs6000_builtins)DECL_FUNCTION_CODE (fndecl);
11590 size_t uns_fcode = (size_t)fcode;
11591 const struct builtin_description *d;
11592 size_t i;
11593 rtx ret;
11594 bool success;
11595 HOST_WIDE_INT mask = rs6000_builtin_info[uns_fcode].mask;
11596 bool func_valid_p = ((rs6000_builtin_mask & mask) == mask);
11598 if (TARGET_DEBUG_BUILTIN)
11600 enum insn_code icode = rs6000_builtin_info[uns_fcode].icode;
11601 const char *name1 = rs6000_builtin_info[uns_fcode].name;
11602 const char *name2 = ((icode != CODE_FOR_nothing)
11603 ? get_insn_name ((int)icode)
11604 : "nothing");
11605 const char *name3;
11607 switch (rs6000_builtin_info[uns_fcode].attr & RS6000_BTC_TYPE_MASK)
11609 default: name3 = "unknown"; break;
11610 case RS6000_BTC_SPECIAL: name3 = "special"; break;
11611 case RS6000_BTC_UNARY: name3 = "unary"; break;
11612 case RS6000_BTC_BINARY: name3 = "binary"; break;
11613 case RS6000_BTC_TERNARY: name3 = "ternary"; break;
11614 case RS6000_BTC_PREDICATE: name3 = "predicate"; break;
11615 case RS6000_BTC_ABS: name3 = "abs"; break;
11616 case RS6000_BTC_EVSEL: name3 = "evsel"; break;
11617 case RS6000_BTC_DST: name3 = "dst"; break;
11621 fprintf (stderr,
11622 "rs6000_expand_builtin, %s (%d), insn = %s (%d), type=%s%s\n",
11623 (name1) ? name1 : "---", fcode,
11624 (name2) ? name2 : "---", (int)icode,
11625 name3,
11626 func_valid_p ? "" : ", not valid");
11629 if (!func_valid_p)
11631 rs6000_invalid_builtin (fcode);
11633 /* Given it is invalid, just generate a normal call. */
11634 return expand_call (exp, target, ignore);
11637 switch (fcode)
11639 case RS6000_BUILTIN_RECIP:
11640 return rs6000_expand_binop_builtin (CODE_FOR_recipdf3, exp, target);
11642 case RS6000_BUILTIN_RECIPF:
11643 return rs6000_expand_binop_builtin (CODE_FOR_recipsf3, exp, target);
11645 case RS6000_BUILTIN_RSQRTF:
11646 return rs6000_expand_unop_builtin (CODE_FOR_rsqrtsf2, exp, target);
11648 case RS6000_BUILTIN_RSQRT:
11649 return rs6000_expand_unop_builtin (CODE_FOR_rsqrtdf2, exp, target);
11651 case POWER7_BUILTIN_BPERMD:
11652 return rs6000_expand_binop_builtin (((TARGET_64BIT)
11653 ? CODE_FOR_bpermd_di
11654 : CODE_FOR_bpermd_si), exp, target);
11656 case RS6000_BUILTIN_GET_TB:
11657 return rs6000_expand_zeroop_builtin (CODE_FOR_rs6000_get_timebase,
11658 target);
11660 case RS6000_BUILTIN_MFTB:
11661 return rs6000_expand_zeroop_builtin (((TARGET_64BIT)
11662 ? CODE_FOR_rs6000_mftb_di
11663 : CODE_FOR_rs6000_mftb_si),
11664 target);
11666 case ALTIVEC_BUILTIN_MASK_FOR_LOAD:
11667 case ALTIVEC_BUILTIN_MASK_FOR_STORE:
11669 int icode = (int) CODE_FOR_altivec_lvsr;
11670 enum machine_mode tmode = insn_data[icode].operand[0].mode;
11671 enum machine_mode mode = insn_data[icode].operand[1].mode;
11672 tree arg;
11673 rtx op, addr, pat;
11675 gcc_assert (TARGET_ALTIVEC);
11677 arg = CALL_EXPR_ARG (exp, 0);
11678 gcc_assert (POINTER_TYPE_P (TREE_TYPE (arg)));
11679 op = expand_expr (arg, NULL_RTX, Pmode, EXPAND_NORMAL);
11680 addr = memory_address (mode, op);
11681 if (fcode == ALTIVEC_BUILTIN_MASK_FOR_STORE)
11682 op = addr;
11683 else
11685 /* For the load case need to negate the address. */
11686 op = gen_reg_rtx (GET_MODE (addr));
11687 emit_insn (gen_rtx_SET (VOIDmode, op,
11688 gen_rtx_NEG (GET_MODE (addr), addr)));
11690 op = gen_rtx_MEM (mode, op);
11692 if (target == 0
11693 || GET_MODE (target) != tmode
11694 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
11695 target = gen_reg_rtx (tmode);
11697 /*pat = gen_altivec_lvsr (target, op);*/
11698 pat = GEN_FCN (icode) (target, op);
11699 if (!pat)
11700 return 0;
11701 emit_insn (pat);
11703 return target;
11706 case ALTIVEC_BUILTIN_VCFUX:
11707 case ALTIVEC_BUILTIN_VCFSX:
11708 case ALTIVEC_BUILTIN_VCTUXS:
11709 case ALTIVEC_BUILTIN_VCTSXS:
11710 /* FIXME: There's got to be a nicer way to handle this case than
11711 constructing a new CALL_EXPR. */
11712 if (call_expr_nargs (exp) == 1)
11714 exp = build_call_nary (TREE_TYPE (exp), CALL_EXPR_FN (exp),
11715 2, CALL_EXPR_ARG (exp, 0), integer_zero_node);
11717 break;
11719 default:
11720 break;
11723 if (TARGET_ALTIVEC)
11725 ret = altivec_expand_builtin (exp, target, &success);
11727 if (success)
11728 return ret;
11730 if (TARGET_SPE)
11732 ret = spe_expand_builtin (exp, target, &success);
11734 if (success)
11735 return ret;
11737 if (TARGET_PAIRED_FLOAT)
11739 ret = paired_expand_builtin (exp, target, &success);
11741 if (success)
11742 return ret;
11745 gcc_assert (TARGET_ALTIVEC || TARGET_VSX || TARGET_SPE || TARGET_PAIRED_FLOAT);
11747 /* Handle simple unary operations. */
11748 d = bdesc_1arg;
11749 for (i = 0; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
11750 if (d->code == fcode)
11751 return rs6000_expand_unop_builtin (d->icode, exp, target);
11753 /* Handle simple binary operations. */
11754 d = bdesc_2arg;
11755 for (i = 0; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
11756 if (d->code == fcode)
11757 return rs6000_expand_binop_builtin (d->icode, exp, target);
11759 /* Handle simple ternary operations. */
11760 d = bdesc_3arg;
11761 for (i = 0; i < ARRAY_SIZE (bdesc_3arg); i++, d++)
11762 if (d->code == fcode)
11763 return rs6000_expand_ternop_builtin (d->icode, exp, target);
11765 gcc_unreachable ();
11768 static void
11769 rs6000_init_builtins (void)
11771 tree tdecl;
11772 tree ftype;
11773 enum machine_mode mode;
11775 if (TARGET_DEBUG_BUILTIN)
11776 fprintf (stderr, "rs6000_init_builtins%s%s%s%s\n",
11777 (TARGET_PAIRED_FLOAT) ? ", paired" : "",
11778 (TARGET_SPE) ? ", spe" : "",
11779 (TARGET_ALTIVEC) ? ", altivec" : "",
11780 (TARGET_VSX) ? ", vsx" : "");
11782 V2SI_type_node = build_vector_type (intSI_type_node, 2);
11783 V2SF_type_node = build_vector_type (float_type_node, 2);
11784 V2DI_type_node = build_vector_type (intDI_type_node, 2);
11785 V2DF_type_node = build_vector_type (double_type_node, 2);
11786 V4HI_type_node = build_vector_type (intHI_type_node, 4);
11787 V4SI_type_node = build_vector_type (intSI_type_node, 4);
11788 V4SF_type_node = build_vector_type (float_type_node, 4);
11789 V8HI_type_node = build_vector_type (intHI_type_node, 8);
11790 V16QI_type_node = build_vector_type (intQI_type_node, 16);
11792 unsigned_V16QI_type_node = build_vector_type (unsigned_intQI_type_node, 16);
11793 unsigned_V8HI_type_node = build_vector_type (unsigned_intHI_type_node, 8);
11794 unsigned_V4SI_type_node = build_vector_type (unsigned_intSI_type_node, 4);
11795 unsigned_V2DI_type_node = build_vector_type (unsigned_intDI_type_node, 2);
11797 opaque_V2SF_type_node = build_opaque_vector_type (float_type_node, 2);
11798 opaque_V2SI_type_node = build_opaque_vector_type (intSI_type_node, 2);
11799 opaque_p_V2SI_type_node = build_pointer_type (opaque_V2SI_type_node);
11800 opaque_V4SI_type_node = build_opaque_vector_type (intSI_type_node, 4);
11802 /* The 'vector bool ...' types must be kept distinct from 'vector unsigned ...'
11803 types, especially in C++ land. Similarly, 'vector pixel' is distinct from
11804 'vector unsigned short'. */
11806 bool_char_type_node = build_distinct_type_copy (unsigned_intQI_type_node);
11807 bool_short_type_node = build_distinct_type_copy (unsigned_intHI_type_node);
11808 bool_int_type_node = build_distinct_type_copy (unsigned_intSI_type_node);
11809 bool_long_type_node = build_distinct_type_copy (unsigned_intDI_type_node);
11810 pixel_type_node = build_distinct_type_copy (unsigned_intHI_type_node);
11812 long_integer_type_internal_node = long_integer_type_node;
11813 long_unsigned_type_internal_node = long_unsigned_type_node;
11814 long_long_integer_type_internal_node = long_long_integer_type_node;
11815 long_long_unsigned_type_internal_node = long_long_unsigned_type_node;
11816 intQI_type_internal_node = intQI_type_node;
11817 uintQI_type_internal_node = unsigned_intQI_type_node;
11818 intHI_type_internal_node = intHI_type_node;
11819 uintHI_type_internal_node = unsigned_intHI_type_node;
11820 intSI_type_internal_node = intSI_type_node;
11821 uintSI_type_internal_node = unsigned_intSI_type_node;
11822 intDI_type_internal_node = intDI_type_node;
11823 uintDI_type_internal_node = unsigned_intDI_type_node;
11824 float_type_internal_node = float_type_node;
11825 double_type_internal_node = double_type_node;
11826 void_type_internal_node = void_type_node;
11828 /* Initialize the modes for builtin_function_type, mapping a machine mode to
11829 tree type node. */
11830 builtin_mode_to_type[QImode][0] = integer_type_node;
11831 builtin_mode_to_type[HImode][0] = integer_type_node;
11832 builtin_mode_to_type[SImode][0] = intSI_type_node;
11833 builtin_mode_to_type[SImode][1] = unsigned_intSI_type_node;
11834 builtin_mode_to_type[DImode][0] = intDI_type_node;
11835 builtin_mode_to_type[DImode][1] = unsigned_intDI_type_node;
11836 builtin_mode_to_type[SFmode][0] = float_type_node;
11837 builtin_mode_to_type[DFmode][0] = double_type_node;
11838 builtin_mode_to_type[V2SImode][0] = V2SI_type_node;
11839 builtin_mode_to_type[V2SFmode][0] = V2SF_type_node;
11840 builtin_mode_to_type[V2DImode][0] = V2DI_type_node;
11841 builtin_mode_to_type[V2DImode][1] = unsigned_V2DI_type_node;
11842 builtin_mode_to_type[V2DFmode][0] = V2DF_type_node;
11843 builtin_mode_to_type[V4HImode][0] = V4HI_type_node;
11844 builtin_mode_to_type[V4SImode][0] = V4SI_type_node;
11845 builtin_mode_to_type[V4SImode][1] = unsigned_V4SI_type_node;
11846 builtin_mode_to_type[V4SFmode][0] = V4SF_type_node;
11847 builtin_mode_to_type[V8HImode][0] = V8HI_type_node;
11848 builtin_mode_to_type[V8HImode][1] = unsigned_V8HI_type_node;
11849 builtin_mode_to_type[V16QImode][0] = V16QI_type_node;
11850 builtin_mode_to_type[V16QImode][1] = unsigned_V16QI_type_node;
11852 tdecl = add_builtin_type ("__bool char", bool_char_type_node);
11853 TYPE_NAME (bool_char_type_node) = tdecl;
11855 tdecl = add_builtin_type ("__bool short", bool_short_type_node);
11856 TYPE_NAME (bool_short_type_node) = tdecl;
11858 tdecl = add_builtin_type ("__bool int", bool_int_type_node);
11859 TYPE_NAME (bool_int_type_node) = tdecl;
11861 tdecl = add_builtin_type ("__pixel", pixel_type_node);
11862 TYPE_NAME (pixel_type_node) = tdecl;
11864 bool_V16QI_type_node = build_vector_type (bool_char_type_node, 16);
11865 bool_V8HI_type_node = build_vector_type (bool_short_type_node, 8);
11866 bool_V4SI_type_node = build_vector_type (bool_int_type_node, 4);
11867 bool_V2DI_type_node = build_vector_type (bool_long_type_node, 2);
11868 pixel_V8HI_type_node = build_vector_type (pixel_type_node, 8);
11870 tdecl = add_builtin_type ("__vector unsigned char", unsigned_V16QI_type_node);
11871 TYPE_NAME (unsigned_V16QI_type_node) = tdecl;
11873 tdecl = add_builtin_type ("__vector signed char", V16QI_type_node);
11874 TYPE_NAME (V16QI_type_node) = tdecl;
11876 tdecl = add_builtin_type ("__vector __bool char", bool_V16QI_type_node);
11877 TYPE_NAME ( bool_V16QI_type_node) = tdecl;
11879 tdecl = add_builtin_type ("__vector unsigned short", unsigned_V8HI_type_node);
11880 TYPE_NAME (unsigned_V8HI_type_node) = tdecl;
11882 tdecl = add_builtin_type ("__vector signed short", V8HI_type_node);
11883 TYPE_NAME (V8HI_type_node) = tdecl;
11885 tdecl = add_builtin_type ("__vector __bool short", bool_V8HI_type_node);
11886 TYPE_NAME (bool_V8HI_type_node) = tdecl;
11888 tdecl = add_builtin_type ("__vector unsigned int", unsigned_V4SI_type_node);
11889 TYPE_NAME (unsigned_V4SI_type_node) = tdecl;
11891 tdecl = add_builtin_type ("__vector signed int", V4SI_type_node);
11892 TYPE_NAME (V4SI_type_node) = tdecl;
11894 tdecl = add_builtin_type ("__vector __bool int", bool_V4SI_type_node);
11895 TYPE_NAME (bool_V4SI_type_node) = tdecl;
11897 tdecl = add_builtin_type ("__vector float", V4SF_type_node);
11898 TYPE_NAME (V4SF_type_node) = tdecl;
11900 tdecl = add_builtin_type ("__vector __pixel", pixel_V8HI_type_node);
11901 TYPE_NAME (pixel_V8HI_type_node) = tdecl;
11903 tdecl = add_builtin_type ("__vector double", V2DF_type_node);
11904 TYPE_NAME (V2DF_type_node) = tdecl;
11906 tdecl = add_builtin_type ("__vector long", V2DI_type_node);
11907 TYPE_NAME (V2DI_type_node) = tdecl;
11909 tdecl = add_builtin_type ("__vector unsigned long", unsigned_V2DI_type_node);
11910 TYPE_NAME (unsigned_V2DI_type_node) = tdecl;
11912 tdecl = add_builtin_type ("__vector __bool long", bool_V2DI_type_node);
11913 TYPE_NAME (bool_V2DI_type_node) = tdecl;
11915 /* Paired and SPE builtins are only available if you build a compiler with
11916 the appropriate options, so only create those builtins with the
11917 appropriate compiler option. Create Altivec and VSX builtins on machines
11918 with at least the general purpose extensions (970 and newer) to allow the
11919 use of the target attribute. */
11920 if (TARGET_PAIRED_FLOAT)
11921 paired_init_builtins ();
11922 if (TARGET_SPE)
11923 spe_init_builtins ();
11924 if (TARGET_EXTRA_BUILTINS)
11925 altivec_init_builtins ();
11926 if (TARGET_EXTRA_BUILTINS || TARGET_SPE || TARGET_PAIRED_FLOAT)
11927 rs6000_common_init_builtins ();
11929 ftype = builtin_function_type (DFmode, DFmode, DFmode, VOIDmode,
11930 RS6000_BUILTIN_RECIP, "__builtin_recipdiv");
11931 def_builtin ("__builtin_recipdiv", ftype, RS6000_BUILTIN_RECIP);
11933 ftype = builtin_function_type (SFmode, SFmode, SFmode, VOIDmode,
11934 RS6000_BUILTIN_RECIPF, "__builtin_recipdivf");
11935 def_builtin ("__builtin_recipdivf", ftype, RS6000_BUILTIN_RECIPF);
11937 ftype = builtin_function_type (DFmode, DFmode, VOIDmode, VOIDmode,
11938 RS6000_BUILTIN_RSQRT, "__builtin_rsqrt");
11939 def_builtin ("__builtin_rsqrt", ftype, RS6000_BUILTIN_RSQRT);
11941 ftype = builtin_function_type (SFmode, SFmode, VOIDmode, VOIDmode,
11942 RS6000_BUILTIN_RSQRTF, "__builtin_rsqrtf");
11943 def_builtin ("__builtin_rsqrtf", ftype, RS6000_BUILTIN_RSQRTF);
11945 mode = (TARGET_64BIT) ? DImode : SImode;
11946 ftype = builtin_function_type (mode, mode, mode, VOIDmode,
11947 POWER7_BUILTIN_BPERMD, "__builtin_bpermd");
11948 def_builtin ("__builtin_bpermd", ftype, POWER7_BUILTIN_BPERMD);
11950 ftype = build_function_type_list (unsigned_intDI_type_node,
11951 NULL_TREE);
11952 def_builtin ("__builtin_ppc_get_timebase", ftype, RS6000_BUILTIN_GET_TB);
11954 if (TARGET_64BIT)
11955 ftype = build_function_type_list (unsigned_intDI_type_node,
11956 NULL_TREE);
11957 else
11958 ftype = build_function_type_list (unsigned_intSI_type_node,
11959 NULL_TREE);
11960 def_builtin ("__builtin_ppc_mftb", ftype, RS6000_BUILTIN_MFTB);
11962 #if TARGET_XCOFF
11963 /* AIX libm provides clog as __clog. */
11964 if ((tdecl = builtin_decl_explicit (BUILT_IN_CLOG)) != NULL_TREE)
11965 set_user_assembler_name (tdecl, "__clog");
11966 #endif
11968 #ifdef SUBTARGET_INIT_BUILTINS
11969 SUBTARGET_INIT_BUILTINS;
11970 #endif
11973 /* Returns the rs6000 builtin decl for CODE. */
11975 static tree
11976 rs6000_builtin_decl (unsigned code, bool initialize_p ATTRIBUTE_UNUSED)
11978 HOST_WIDE_INT fnmask;
11980 if (code >= RS6000_BUILTIN_COUNT)
11981 return error_mark_node;
11983 fnmask = rs6000_builtin_info[code].mask;
11984 if ((fnmask & rs6000_builtin_mask) != fnmask)
11986 rs6000_invalid_builtin ((enum rs6000_builtins)code);
11987 return error_mark_node;
11990 return rs6000_builtin_decls[code];
11993 static void
11994 spe_init_builtins (void)
11996 tree puint_type_node = build_pointer_type (unsigned_type_node);
11997 tree pushort_type_node = build_pointer_type (short_unsigned_type_node);
11998 const struct builtin_description *d;
11999 size_t i;
12001 tree v2si_ftype_4_v2si
12002 = build_function_type_list (opaque_V2SI_type_node,
12003 opaque_V2SI_type_node,
12004 opaque_V2SI_type_node,
12005 opaque_V2SI_type_node,
12006 opaque_V2SI_type_node,
12007 NULL_TREE);
12009 tree v2sf_ftype_4_v2sf
12010 = build_function_type_list (opaque_V2SF_type_node,
12011 opaque_V2SF_type_node,
12012 opaque_V2SF_type_node,
12013 opaque_V2SF_type_node,
12014 opaque_V2SF_type_node,
12015 NULL_TREE);
12017 tree int_ftype_int_v2si_v2si
12018 = build_function_type_list (integer_type_node,
12019 integer_type_node,
12020 opaque_V2SI_type_node,
12021 opaque_V2SI_type_node,
12022 NULL_TREE);
12024 tree int_ftype_int_v2sf_v2sf
12025 = build_function_type_list (integer_type_node,
12026 integer_type_node,
12027 opaque_V2SF_type_node,
12028 opaque_V2SF_type_node,
12029 NULL_TREE);
12031 tree void_ftype_v2si_puint_int
12032 = build_function_type_list (void_type_node,
12033 opaque_V2SI_type_node,
12034 puint_type_node,
12035 integer_type_node,
12036 NULL_TREE);
12038 tree void_ftype_v2si_puint_char
12039 = build_function_type_list (void_type_node,
12040 opaque_V2SI_type_node,
12041 puint_type_node,
12042 char_type_node,
12043 NULL_TREE);
12045 tree void_ftype_v2si_pv2si_int
12046 = build_function_type_list (void_type_node,
12047 opaque_V2SI_type_node,
12048 opaque_p_V2SI_type_node,
12049 integer_type_node,
12050 NULL_TREE);
12052 tree void_ftype_v2si_pv2si_char
12053 = build_function_type_list (void_type_node,
12054 opaque_V2SI_type_node,
12055 opaque_p_V2SI_type_node,
12056 char_type_node,
12057 NULL_TREE);
12059 tree void_ftype_int
12060 = build_function_type_list (void_type_node, integer_type_node, NULL_TREE);
12062 tree int_ftype_void
12063 = build_function_type_list (integer_type_node, NULL_TREE);
12065 tree v2si_ftype_pv2si_int
12066 = build_function_type_list (opaque_V2SI_type_node,
12067 opaque_p_V2SI_type_node,
12068 integer_type_node,
12069 NULL_TREE);
12071 tree v2si_ftype_puint_int
12072 = build_function_type_list (opaque_V2SI_type_node,
12073 puint_type_node,
12074 integer_type_node,
12075 NULL_TREE);
12077 tree v2si_ftype_pushort_int
12078 = build_function_type_list (opaque_V2SI_type_node,
12079 pushort_type_node,
12080 integer_type_node,
12081 NULL_TREE);
12083 tree v2si_ftype_signed_char
12084 = build_function_type_list (opaque_V2SI_type_node,
12085 signed_char_type_node,
12086 NULL_TREE);
12088 add_builtin_type ("__ev64_opaque__", opaque_V2SI_type_node);
12090 /* Initialize irregular SPE builtins. */
12092 def_builtin ("__builtin_spe_mtspefscr", void_ftype_int, SPE_BUILTIN_MTSPEFSCR);
12093 def_builtin ("__builtin_spe_mfspefscr", int_ftype_void, SPE_BUILTIN_MFSPEFSCR);
12094 def_builtin ("__builtin_spe_evstddx", void_ftype_v2si_pv2si_int, SPE_BUILTIN_EVSTDDX);
12095 def_builtin ("__builtin_spe_evstdhx", void_ftype_v2si_pv2si_int, SPE_BUILTIN_EVSTDHX);
12096 def_builtin ("__builtin_spe_evstdwx", void_ftype_v2si_pv2si_int, SPE_BUILTIN_EVSTDWX);
12097 def_builtin ("__builtin_spe_evstwhex", void_ftype_v2si_puint_int, SPE_BUILTIN_EVSTWHEX);
12098 def_builtin ("__builtin_spe_evstwhox", void_ftype_v2si_puint_int, SPE_BUILTIN_EVSTWHOX);
12099 def_builtin ("__builtin_spe_evstwwex", void_ftype_v2si_puint_int, SPE_BUILTIN_EVSTWWEX);
12100 def_builtin ("__builtin_spe_evstwwox", void_ftype_v2si_puint_int, SPE_BUILTIN_EVSTWWOX);
12101 def_builtin ("__builtin_spe_evstdd", void_ftype_v2si_pv2si_char, SPE_BUILTIN_EVSTDD);
12102 def_builtin ("__builtin_spe_evstdh", void_ftype_v2si_pv2si_char, SPE_BUILTIN_EVSTDH);
12103 def_builtin ("__builtin_spe_evstdw", void_ftype_v2si_pv2si_char, SPE_BUILTIN_EVSTDW);
12104 def_builtin ("__builtin_spe_evstwhe", void_ftype_v2si_puint_char, SPE_BUILTIN_EVSTWHE);
12105 def_builtin ("__builtin_spe_evstwho", void_ftype_v2si_puint_char, SPE_BUILTIN_EVSTWHO);
12106 def_builtin ("__builtin_spe_evstwwe", void_ftype_v2si_puint_char, SPE_BUILTIN_EVSTWWE);
12107 def_builtin ("__builtin_spe_evstwwo", void_ftype_v2si_puint_char, SPE_BUILTIN_EVSTWWO);
12108 def_builtin ("__builtin_spe_evsplatfi", v2si_ftype_signed_char, SPE_BUILTIN_EVSPLATFI);
12109 def_builtin ("__builtin_spe_evsplati", v2si_ftype_signed_char, SPE_BUILTIN_EVSPLATI);
12111 /* Loads. */
12112 def_builtin ("__builtin_spe_evlddx", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDDX);
12113 def_builtin ("__builtin_spe_evldwx", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDWX);
12114 def_builtin ("__builtin_spe_evldhx", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDHX);
12115 def_builtin ("__builtin_spe_evlwhex", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHEX);
12116 def_builtin ("__builtin_spe_evlwhoux", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHOUX);
12117 def_builtin ("__builtin_spe_evlwhosx", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHOSX);
12118 def_builtin ("__builtin_spe_evlwwsplatx", v2si_ftype_puint_int, SPE_BUILTIN_EVLWWSPLATX);
12119 def_builtin ("__builtin_spe_evlwhsplatx", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHSPLATX);
12120 def_builtin ("__builtin_spe_evlhhesplatx", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHESPLATX);
12121 def_builtin ("__builtin_spe_evlhhousplatx", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHOUSPLATX);
12122 def_builtin ("__builtin_spe_evlhhossplatx", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHOSSPLATX);
12123 def_builtin ("__builtin_spe_evldd", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDD);
12124 def_builtin ("__builtin_spe_evldw", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDW);
12125 def_builtin ("__builtin_spe_evldh", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDH);
12126 def_builtin ("__builtin_spe_evlhhesplat", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHESPLAT);
12127 def_builtin ("__builtin_spe_evlhhossplat", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHOSSPLAT);
12128 def_builtin ("__builtin_spe_evlhhousplat", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHOUSPLAT);
12129 def_builtin ("__builtin_spe_evlwhe", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHE);
12130 def_builtin ("__builtin_spe_evlwhos", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHOS);
12131 def_builtin ("__builtin_spe_evlwhou", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHOU);
12132 def_builtin ("__builtin_spe_evlwhsplat", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHSPLAT);
12133 def_builtin ("__builtin_spe_evlwwsplat", v2si_ftype_puint_int, SPE_BUILTIN_EVLWWSPLAT);
12135 /* Predicates. */
12136 d = bdesc_spe_predicates;
12137 for (i = 0; i < ARRAY_SIZE (bdesc_spe_predicates); ++i, d++)
12139 tree type;
12141 switch (insn_data[d->icode].operand[1].mode)
12143 case V2SImode:
12144 type = int_ftype_int_v2si_v2si;
12145 break;
12146 case V2SFmode:
12147 type = int_ftype_int_v2sf_v2sf;
12148 break;
12149 default:
12150 gcc_unreachable ();
12153 def_builtin (d->name, type, d->code);
12156 /* Evsel predicates. */
12157 d = bdesc_spe_evsel;
12158 for (i = 0; i < ARRAY_SIZE (bdesc_spe_evsel); ++i, d++)
12160 tree type;
12162 switch (insn_data[d->icode].operand[1].mode)
12164 case V2SImode:
12165 type = v2si_ftype_4_v2si;
12166 break;
12167 case V2SFmode:
12168 type = v2sf_ftype_4_v2sf;
12169 break;
12170 default:
12171 gcc_unreachable ();
12174 def_builtin (d->name, type, d->code);
12178 static void
12179 paired_init_builtins (void)
12181 const struct builtin_description *d;
12182 size_t i;
12184 tree int_ftype_int_v2sf_v2sf
12185 = build_function_type_list (integer_type_node,
12186 integer_type_node,
12187 V2SF_type_node,
12188 V2SF_type_node,
12189 NULL_TREE);
12190 tree pcfloat_type_node =
12191 build_pointer_type (build_qualified_type
12192 (float_type_node, TYPE_QUAL_CONST));
12194 tree v2sf_ftype_long_pcfloat = build_function_type_list (V2SF_type_node,
12195 long_integer_type_node,
12196 pcfloat_type_node,
12197 NULL_TREE);
12198 tree void_ftype_v2sf_long_pcfloat =
12199 build_function_type_list (void_type_node,
12200 V2SF_type_node,
12201 long_integer_type_node,
12202 pcfloat_type_node,
12203 NULL_TREE);
12206 def_builtin ("__builtin_paired_lx", v2sf_ftype_long_pcfloat,
12207 PAIRED_BUILTIN_LX);
12210 def_builtin ("__builtin_paired_stx", void_ftype_v2sf_long_pcfloat,
12211 PAIRED_BUILTIN_STX);
12213 /* Predicates. */
12214 d = bdesc_paired_preds;
12215 for (i = 0; i < ARRAY_SIZE (bdesc_paired_preds); ++i, d++)
12217 tree type;
12219 if (TARGET_DEBUG_BUILTIN)
12220 fprintf (stderr, "paired pred #%d, insn = %s [%d], mode = %s\n",
12221 (int)i, get_insn_name (d->icode), (int)d->icode,
12222 GET_MODE_NAME (insn_data[d->icode].operand[1].mode));
12224 switch (insn_data[d->icode].operand[1].mode)
12226 case V2SFmode:
12227 type = int_ftype_int_v2sf_v2sf;
12228 break;
12229 default:
12230 gcc_unreachable ();
12233 def_builtin (d->name, type, d->code);
12237 static void
12238 altivec_init_builtins (void)
12240 const struct builtin_description *d;
12241 size_t i;
12242 tree ftype;
12243 tree decl;
12245 tree pvoid_type_node = build_pointer_type (void_type_node);
12247 tree pcvoid_type_node
12248 = build_pointer_type (build_qualified_type (void_type_node,
12249 TYPE_QUAL_CONST));
12251 tree int_ftype_opaque
12252 = build_function_type_list (integer_type_node,
12253 opaque_V4SI_type_node, NULL_TREE);
12254 tree opaque_ftype_opaque
12255 = build_function_type_list (integer_type_node, NULL_TREE);
12256 tree opaque_ftype_opaque_int
12257 = build_function_type_list (opaque_V4SI_type_node,
12258 opaque_V4SI_type_node, integer_type_node, NULL_TREE);
12259 tree opaque_ftype_opaque_opaque_int
12260 = build_function_type_list (opaque_V4SI_type_node,
12261 opaque_V4SI_type_node, opaque_V4SI_type_node,
12262 integer_type_node, NULL_TREE);
12263 tree int_ftype_int_opaque_opaque
12264 = build_function_type_list (integer_type_node,
12265 integer_type_node, opaque_V4SI_type_node,
12266 opaque_V4SI_type_node, NULL_TREE);
12267 tree int_ftype_int_v4si_v4si
12268 = build_function_type_list (integer_type_node,
12269 integer_type_node, V4SI_type_node,
12270 V4SI_type_node, NULL_TREE);
12271 tree void_ftype_v4si
12272 = build_function_type_list (void_type_node, V4SI_type_node, NULL_TREE);
12273 tree v8hi_ftype_void
12274 = build_function_type_list (V8HI_type_node, NULL_TREE);
12275 tree void_ftype_void
12276 = build_function_type_list (void_type_node, NULL_TREE);
12277 tree void_ftype_int
12278 = build_function_type_list (void_type_node, integer_type_node, NULL_TREE);
12280 tree opaque_ftype_long_pcvoid
12281 = build_function_type_list (opaque_V4SI_type_node,
12282 long_integer_type_node, pcvoid_type_node,
12283 NULL_TREE);
12284 tree v16qi_ftype_long_pcvoid
12285 = build_function_type_list (V16QI_type_node,
12286 long_integer_type_node, pcvoid_type_node,
12287 NULL_TREE);
12288 tree v8hi_ftype_long_pcvoid
12289 = build_function_type_list (V8HI_type_node,
12290 long_integer_type_node, pcvoid_type_node,
12291 NULL_TREE);
12292 tree v4si_ftype_long_pcvoid
12293 = build_function_type_list (V4SI_type_node,
12294 long_integer_type_node, pcvoid_type_node,
12295 NULL_TREE);
12296 tree v4sf_ftype_long_pcvoid
12297 = build_function_type_list (V4SF_type_node,
12298 long_integer_type_node, pcvoid_type_node,
12299 NULL_TREE);
12300 tree v2df_ftype_long_pcvoid
12301 = build_function_type_list (V2DF_type_node,
12302 long_integer_type_node, pcvoid_type_node,
12303 NULL_TREE);
12304 tree v2di_ftype_long_pcvoid
12305 = build_function_type_list (V2DI_type_node,
12306 long_integer_type_node, pcvoid_type_node,
12307 NULL_TREE);
12309 tree void_ftype_opaque_long_pvoid
12310 = build_function_type_list (void_type_node,
12311 opaque_V4SI_type_node, long_integer_type_node,
12312 pvoid_type_node, NULL_TREE);
12313 tree void_ftype_v4si_long_pvoid
12314 = build_function_type_list (void_type_node,
12315 V4SI_type_node, long_integer_type_node,
12316 pvoid_type_node, NULL_TREE);
12317 tree void_ftype_v16qi_long_pvoid
12318 = build_function_type_list (void_type_node,
12319 V16QI_type_node, long_integer_type_node,
12320 pvoid_type_node, NULL_TREE);
12321 tree void_ftype_v8hi_long_pvoid
12322 = build_function_type_list (void_type_node,
12323 V8HI_type_node, long_integer_type_node,
12324 pvoid_type_node, NULL_TREE);
12325 tree void_ftype_v4sf_long_pvoid
12326 = build_function_type_list (void_type_node,
12327 V4SF_type_node, long_integer_type_node,
12328 pvoid_type_node, NULL_TREE);
12329 tree void_ftype_v2df_long_pvoid
12330 = build_function_type_list (void_type_node,
12331 V2DF_type_node, long_integer_type_node,
12332 pvoid_type_node, NULL_TREE);
12333 tree void_ftype_v2di_long_pvoid
12334 = build_function_type_list (void_type_node,
12335 V2DI_type_node, long_integer_type_node,
12336 pvoid_type_node, NULL_TREE);
12337 tree int_ftype_int_v8hi_v8hi
12338 = build_function_type_list (integer_type_node,
12339 integer_type_node, V8HI_type_node,
12340 V8HI_type_node, NULL_TREE);
12341 tree int_ftype_int_v16qi_v16qi
12342 = build_function_type_list (integer_type_node,
12343 integer_type_node, V16QI_type_node,
12344 V16QI_type_node, NULL_TREE);
12345 tree int_ftype_int_v4sf_v4sf
12346 = build_function_type_list (integer_type_node,
12347 integer_type_node, V4SF_type_node,
12348 V4SF_type_node, NULL_TREE);
12349 tree int_ftype_int_v2df_v2df
12350 = build_function_type_list (integer_type_node,
12351 integer_type_node, V2DF_type_node,
12352 V2DF_type_node, NULL_TREE);
12353 tree v4si_ftype_v4si
12354 = build_function_type_list (V4SI_type_node, V4SI_type_node, NULL_TREE);
12355 tree v8hi_ftype_v8hi
12356 = build_function_type_list (V8HI_type_node, V8HI_type_node, NULL_TREE);
12357 tree v16qi_ftype_v16qi
12358 = build_function_type_list (V16QI_type_node, V16QI_type_node, NULL_TREE);
12359 tree v4sf_ftype_v4sf
12360 = build_function_type_list (V4SF_type_node, V4SF_type_node, NULL_TREE);
12361 tree v2df_ftype_v2df
12362 = build_function_type_list (V2DF_type_node, V2DF_type_node, NULL_TREE);
12363 tree void_ftype_pcvoid_int_int
12364 = build_function_type_list (void_type_node,
12365 pcvoid_type_node, integer_type_node,
12366 integer_type_node, NULL_TREE);
12368 def_builtin ("__builtin_altivec_mtvscr", void_ftype_v4si, ALTIVEC_BUILTIN_MTVSCR);
12369 def_builtin ("__builtin_altivec_mfvscr", v8hi_ftype_void, ALTIVEC_BUILTIN_MFVSCR);
12370 def_builtin ("__builtin_altivec_dssall", void_ftype_void, ALTIVEC_BUILTIN_DSSALL);
12371 def_builtin ("__builtin_altivec_dss", void_ftype_int, ALTIVEC_BUILTIN_DSS);
12372 def_builtin ("__builtin_altivec_lvsl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVSL);
12373 def_builtin ("__builtin_altivec_lvsr", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVSR);
12374 def_builtin ("__builtin_altivec_lvebx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVEBX);
12375 def_builtin ("__builtin_altivec_lvehx", v8hi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVEHX);
12376 def_builtin ("__builtin_altivec_lvewx", v4si_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVEWX);
12377 def_builtin ("__builtin_altivec_lvxl", v4si_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVXL);
12378 def_builtin ("__builtin_altivec_lvx", v4si_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVX);
12379 def_builtin ("__builtin_altivec_stvx", void_ftype_v4si_long_pvoid, ALTIVEC_BUILTIN_STVX);
12380 def_builtin ("__builtin_altivec_stvewx", void_ftype_v4si_long_pvoid, ALTIVEC_BUILTIN_STVEWX);
12381 def_builtin ("__builtin_altivec_stvxl", void_ftype_v4si_long_pvoid, ALTIVEC_BUILTIN_STVXL);
12382 def_builtin ("__builtin_altivec_stvebx", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_STVEBX);
12383 def_builtin ("__builtin_altivec_stvehx", void_ftype_v8hi_long_pvoid, ALTIVEC_BUILTIN_STVEHX);
12384 def_builtin ("__builtin_vec_ld", opaque_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LD);
12385 def_builtin ("__builtin_vec_lde", opaque_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LDE);
12386 def_builtin ("__builtin_vec_ldl", opaque_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LDL);
12387 def_builtin ("__builtin_vec_lvsl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVSL);
12388 def_builtin ("__builtin_vec_lvsr", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVSR);
12389 def_builtin ("__builtin_vec_lvebx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVEBX);
12390 def_builtin ("__builtin_vec_lvehx", v8hi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVEHX);
12391 def_builtin ("__builtin_vec_lvewx", v4si_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVEWX);
12392 def_builtin ("__builtin_vec_st", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_ST);
12393 def_builtin ("__builtin_vec_ste", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_STE);
12394 def_builtin ("__builtin_vec_stl", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_STL);
12395 def_builtin ("__builtin_vec_stvewx", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_STVEWX);
12396 def_builtin ("__builtin_vec_stvebx", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_STVEBX);
12397 def_builtin ("__builtin_vec_stvehx", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_STVEHX);
12399 def_builtin ("__builtin_vsx_lxvd2x_v2df", v2df_ftype_long_pcvoid,
12400 VSX_BUILTIN_LXVD2X_V2DF);
12401 def_builtin ("__builtin_vsx_lxvd2x_v2di", v2di_ftype_long_pcvoid,
12402 VSX_BUILTIN_LXVD2X_V2DI);
12403 def_builtin ("__builtin_vsx_lxvw4x_v4sf", v4sf_ftype_long_pcvoid,
12404 VSX_BUILTIN_LXVW4X_V4SF);
12405 def_builtin ("__builtin_vsx_lxvw4x_v4si", v4si_ftype_long_pcvoid,
12406 VSX_BUILTIN_LXVW4X_V4SI);
12407 def_builtin ("__builtin_vsx_lxvw4x_v8hi", v8hi_ftype_long_pcvoid,
12408 VSX_BUILTIN_LXVW4X_V8HI);
12409 def_builtin ("__builtin_vsx_lxvw4x_v16qi", v16qi_ftype_long_pcvoid,
12410 VSX_BUILTIN_LXVW4X_V16QI);
12411 def_builtin ("__builtin_vsx_stxvd2x_v2df", void_ftype_v2df_long_pvoid,
12412 VSX_BUILTIN_STXVD2X_V2DF);
12413 def_builtin ("__builtin_vsx_stxvd2x_v2di", void_ftype_v2di_long_pvoid,
12414 VSX_BUILTIN_STXVD2X_V2DI);
12415 def_builtin ("__builtin_vsx_stxvw4x_v4sf", void_ftype_v4sf_long_pvoid,
12416 VSX_BUILTIN_STXVW4X_V4SF);
12417 def_builtin ("__builtin_vsx_stxvw4x_v4si", void_ftype_v4si_long_pvoid,
12418 VSX_BUILTIN_STXVW4X_V4SI);
12419 def_builtin ("__builtin_vsx_stxvw4x_v8hi", void_ftype_v8hi_long_pvoid,
12420 VSX_BUILTIN_STXVW4X_V8HI);
12421 def_builtin ("__builtin_vsx_stxvw4x_v16qi", void_ftype_v16qi_long_pvoid,
12422 VSX_BUILTIN_STXVW4X_V16QI);
12423 def_builtin ("__builtin_vec_vsx_ld", opaque_ftype_long_pcvoid,
12424 VSX_BUILTIN_VEC_LD);
12425 def_builtin ("__builtin_vec_vsx_st", void_ftype_opaque_long_pvoid,
12426 VSX_BUILTIN_VEC_ST);
12428 def_builtin ("__builtin_vec_step", int_ftype_opaque, ALTIVEC_BUILTIN_VEC_STEP);
12429 def_builtin ("__builtin_vec_splats", opaque_ftype_opaque, ALTIVEC_BUILTIN_VEC_SPLATS);
12430 def_builtin ("__builtin_vec_promote", opaque_ftype_opaque, ALTIVEC_BUILTIN_VEC_PROMOTE);
12432 def_builtin ("__builtin_vec_sld", opaque_ftype_opaque_opaque_int, ALTIVEC_BUILTIN_VEC_SLD);
12433 def_builtin ("__builtin_vec_splat", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_SPLAT);
12434 def_builtin ("__builtin_vec_extract", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_EXTRACT);
12435 def_builtin ("__builtin_vec_insert", opaque_ftype_opaque_opaque_int, ALTIVEC_BUILTIN_VEC_INSERT);
12436 def_builtin ("__builtin_vec_vspltw", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_VSPLTW);
12437 def_builtin ("__builtin_vec_vsplth", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_VSPLTH);
12438 def_builtin ("__builtin_vec_vspltb", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_VSPLTB);
12439 def_builtin ("__builtin_vec_ctf", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_CTF);
12440 def_builtin ("__builtin_vec_vcfsx", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_VCFSX);
12441 def_builtin ("__builtin_vec_vcfux", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_VCFUX);
12442 def_builtin ("__builtin_vec_cts", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_CTS);
12443 def_builtin ("__builtin_vec_ctu", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_CTU);
12445 /* Cell builtins. */
12446 def_builtin ("__builtin_altivec_lvlx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVLX);
12447 def_builtin ("__builtin_altivec_lvlxl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVLXL);
12448 def_builtin ("__builtin_altivec_lvrx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVRX);
12449 def_builtin ("__builtin_altivec_lvrxl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVRXL);
12451 def_builtin ("__builtin_vec_lvlx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVLX);
12452 def_builtin ("__builtin_vec_lvlxl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVLXL);
12453 def_builtin ("__builtin_vec_lvrx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVRX);
12454 def_builtin ("__builtin_vec_lvrxl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVRXL);
12456 def_builtin ("__builtin_altivec_stvlx", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_STVLX);
12457 def_builtin ("__builtin_altivec_stvlxl", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_STVLXL);
12458 def_builtin ("__builtin_altivec_stvrx", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_STVRX);
12459 def_builtin ("__builtin_altivec_stvrxl", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_STVRXL);
12461 def_builtin ("__builtin_vec_stvlx", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_VEC_STVLX);
12462 def_builtin ("__builtin_vec_stvlxl", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_VEC_STVLXL);
12463 def_builtin ("__builtin_vec_stvrx", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_VEC_STVRX);
12464 def_builtin ("__builtin_vec_stvrxl", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_VEC_STVRXL);
12466 /* Add the DST variants. */
12467 d = bdesc_dst;
12468 for (i = 0; i < ARRAY_SIZE (bdesc_dst); i++, d++)
12469 def_builtin (d->name, void_ftype_pcvoid_int_int, d->code);
12471 /* Initialize the predicates. */
12472 d = bdesc_altivec_preds;
12473 for (i = 0; i < ARRAY_SIZE (bdesc_altivec_preds); i++, d++)
12475 enum machine_mode mode1;
12476 tree type;
12478 if (rs6000_overloaded_builtin_p (d->code))
12479 mode1 = VOIDmode;
12480 else
12481 mode1 = insn_data[d->icode].operand[1].mode;
12483 switch (mode1)
12485 case VOIDmode:
12486 type = int_ftype_int_opaque_opaque;
12487 break;
12488 case V4SImode:
12489 type = int_ftype_int_v4si_v4si;
12490 break;
12491 case V8HImode:
12492 type = int_ftype_int_v8hi_v8hi;
12493 break;
12494 case V16QImode:
12495 type = int_ftype_int_v16qi_v16qi;
12496 break;
12497 case V4SFmode:
12498 type = int_ftype_int_v4sf_v4sf;
12499 break;
12500 case V2DFmode:
12501 type = int_ftype_int_v2df_v2df;
12502 break;
12503 default:
12504 gcc_unreachable ();
12507 def_builtin (d->name, type, d->code);
12510 /* Initialize the abs* operators. */
12511 d = bdesc_abs;
12512 for (i = 0; i < ARRAY_SIZE (bdesc_abs); i++, d++)
12514 enum machine_mode mode0;
12515 tree type;
12517 mode0 = insn_data[d->icode].operand[0].mode;
12519 switch (mode0)
12521 case V4SImode:
12522 type = v4si_ftype_v4si;
12523 break;
12524 case V8HImode:
12525 type = v8hi_ftype_v8hi;
12526 break;
12527 case V16QImode:
12528 type = v16qi_ftype_v16qi;
12529 break;
12530 case V4SFmode:
12531 type = v4sf_ftype_v4sf;
12532 break;
12533 case V2DFmode:
12534 type = v2df_ftype_v2df;
12535 break;
12536 default:
12537 gcc_unreachable ();
12540 def_builtin (d->name, type, d->code);
12543 /* Initialize target builtin that implements
12544 targetm.vectorize.builtin_mask_for_load. */
12546 decl = add_builtin_function ("__builtin_altivec_mask_for_load",
12547 v16qi_ftype_long_pcvoid,
12548 ALTIVEC_BUILTIN_MASK_FOR_LOAD,
12549 BUILT_IN_MD, NULL, NULL_TREE);
12550 TREE_READONLY (decl) = 1;
12551 /* Record the decl. Will be used by rs6000_builtin_mask_for_load. */
12552 altivec_builtin_mask_for_load = decl;
12554 /* Access to the vec_init patterns. */
12555 ftype = build_function_type_list (V4SI_type_node, integer_type_node,
12556 integer_type_node, integer_type_node,
12557 integer_type_node, NULL_TREE);
12558 def_builtin ("__builtin_vec_init_v4si", ftype, ALTIVEC_BUILTIN_VEC_INIT_V4SI);
12560 ftype = build_function_type_list (V8HI_type_node, short_integer_type_node,
12561 short_integer_type_node,
12562 short_integer_type_node,
12563 short_integer_type_node,
12564 short_integer_type_node,
12565 short_integer_type_node,
12566 short_integer_type_node,
12567 short_integer_type_node, NULL_TREE);
12568 def_builtin ("__builtin_vec_init_v8hi", ftype, ALTIVEC_BUILTIN_VEC_INIT_V8HI);
12570 ftype = build_function_type_list (V16QI_type_node, char_type_node,
12571 char_type_node, char_type_node,
12572 char_type_node, char_type_node,
12573 char_type_node, char_type_node,
12574 char_type_node, char_type_node,
12575 char_type_node, char_type_node,
12576 char_type_node, char_type_node,
12577 char_type_node, char_type_node,
12578 char_type_node, NULL_TREE);
12579 def_builtin ("__builtin_vec_init_v16qi", ftype,
12580 ALTIVEC_BUILTIN_VEC_INIT_V16QI);
12582 ftype = build_function_type_list (V4SF_type_node, float_type_node,
12583 float_type_node, float_type_node,
12584 float_type_node, NULL_TREE);
12585 def_builtin ("__builtin_vec_init_v4sf", ftype, ALTIVEC_BUILTIN_VEC_INIT_V4SF);
12587 /* VSX builtins. */
12588 ftype = build_function_type_list (V2DF_type_node, double_type_node,
12589 double_type_node, NULL_TREE);
12590 def_builtin ("__builtin_vec_init_v2df", ftype, VSX_BUILTIN_VEC_INIT_V2DF);
12592 ftype = build_function_type_list (V2DI_type_node, intDI_type_node,
12593 intDI_type_node, NULL_TREE);
12594 def_builtin ("__builtin_vec_init_v2di", ftype, VSX_BUILTIN_VEC_INIT_V2DI);
12596 /* Access to the vec_set patterns. */
12597 ftype = build_function_type_list (V4SI_type_node, V4SI_type_node,
12598 intSI_type_node,
12599 integer_type_node, NULL_TREE);
12600 def_builtin ("__builtin_vec_set_v4si", ftype, ALTIVEC_BUILTIN_VEC_SET_V4SI);
12602 ftype = build_function_type_list (V8HI_type_node, V8HI_type_node,
12603 intHI_type_node,
12604 integer_type_node, NULL_TREE);
12605 def_builtin ("__builtin_vec_set_v8hi", ftype, ALTIVEC_BUILTIN_VEC_SET_V8HI);
12607 ftype = build_function_type_list (V16QI_type_node, V16QI_type_node,
12608 intQI_type_node,
12609 integer_type_node, NULL_TREE);
12610 def_builtin ("__builtin_vec_set_v16qi", ftype, ALTIVEC_BUILTIN_VEC_SET_V16QI);
12612 ftype = build_function_type_list (V4SF_type_node, V4SF_type_node,
12613 float_type_node,
12614 integer_type_node, NULL_TREE);
12615 def_builtin ("__builtin_vec_set_v4sf", ftype, ALTIVEC_BUILTIN_VEC_SET_V4SF);
12617 ftype = build_function_type_list (V2DF_type_node, V2DF_type_node,
12618 double_type_node,
12619 integer_type_node, NULL_TREE);
12620 def_builtin ("__builtin_vec_set_v2df", ftype, VSX_BUILTIN_VEC_SET_V2DF);
12622 ftype = build_function_type_list (V2DI_type_node, V2DI_type_node,
12623 intDI_type_node,
12624 integer_type_node, NULL_TREE);
12625 def_builtin ("__builtin_vec_set_v2di", ftype, VSX_BUILTIN_VEC_SET_V2DI);
12627 /* Access to the vec_extract patterns. */
12628 ftype = build_function_type_list (intSI_type_node, V4SI_type_node,
12629 integer_type_node, NULL_TREE);
12630 def_builtin ("__builtin_vec_ext_v4si", ftype, ALTIVEC_BUILTIN_VEC_EXT_V4SI);
12632 ftype = build_function_type_list (intHI_type_node, V8HI_type_node,
12633 integer_type_node, NULL_TREE);
12634 def_builtin ("__builtin_vec_ext_v8hi", ftype, ALTIVEC_BUILTIN_VEC_EXT_V8HI);
12636 ftype = build_function_type_list (intQI_type_node, V16QI_type_node,
12637 integer_type_node, NULL_TREE);
12638 def_builtin ("__builtin_vec_ext_v16qi", ftype, ALTIVEC_BUILTIN_VEC_EXT_V16QI);
12640 ftype = build_function_type_list (float_type_node, V4SF_type_node,
12641 integer_type_node, NULL_TREE);
12642 def_builtin ("__builtin_vec_ext_v4sf", ftype, ALTIVEC_BUILTIN_VEC_EXT_V4SF);
12644 ftype = build_function_type_list (double_type_node, V2DF_type_node,
12645 integer_type_node, NULL_TREE);
12646 def_builtin ("__builtin_vec_ext_v2df", ftype, VSX_BUILTIN_VEC_EXT_V2DF);
12648 ftype = build_function_type_list (intDI_type_node, V2DI_type_node,
12649 integer_type_node, NULL_TREE);
12650 def_builtin ("__builtin_vec_ext_v2di", ftype, VSX_BUILTIN_VEC_EXT_V2DI);
12653 /* Hash function for builtin functions with up to 3 arguments and a return
12654 type. */
12655 static unsigned
12656 builtin_hash_function (const void *hash_entry)
12658 unsigned ret = 0;
12659 int i;
12660 const struct builtin_hash_struct *bh =
12661 (const struct builtin_hash_struct *) hash_entry;
12663 for (i = 0; i < 4; i++)
12665 ret = (ret * (unsigned)MAX_MACHINE_MODE) + ((unsigned)bh->mode[i]);
12666 ret = (ret * 2) + bh->uns_p[i];
12669 return ret;
12672 /* Compare builtin hash entries H1 and H2 for equivalence. */
12673 static int
12674 builtin_hash_eq (const void *h1, const void *h2)
12676 const struct builtin_hash_struct *p1 = (const struct builtin_hash_struct *) h1;
12677 const struct builtin_hash_struct *p2 = (const struct builtin_hash_struct *) h2;
12679 return ((p1->mode[0] == p2->mode[0])
12680 && (p1->mode[1] == p2->mode[1])
12681 && (p1->mode[2] == p2->mode[2])
12682 && (p1->mode[3] == p2->mode[3])
12683 && (p1->uns_p[0] == p2->uns_p[0])
12684 && (p1->uns_p[1] == p2->uns_p[1])
12685 && (p1->uns_p[2] == p2->uns_p[2])
12686 && (p1->uns_p[3] == p2->uns_p[3]));
12689 /* Map types for builtin functions with an explicit return type and up to 3
12690 arguments. Functions with fewer than 3 arguments use VOIDmode as the type
12691 of the argument. */
12692 static tree
12693 builtin_function_type (enum machine_mode mode_ret, enum machine_mode mode_arg0,
12694 enum machine_mode mode_arg1, enum machine_mode mode_arg2,
12695 enum rs6000_builtins builtin, const char *name)
12697 struct builtin_hash_struct h;
12698 struct builtin_hash_struct *h2;
12699 void **found;
12700 int num_args = 3;
12701 int i;
12702 tree ret_type = NULL_TREE;
12703 tree arg_type[3] = { NULL_TREE, NULL_TREE, NULL_TREE };
12705 /* Create builtin_hash_table. */
12706 if (builtin_hash_table == NULL)
12707 builtin_hash_table = htab_create_ggc (1500, builtin_hash_function,
12708 builtin_hash_eq, NULL);
12710 h.type = NULL_TREE;
12711 h.mode[0] = mode_ret;
12712 h.mode[1] = mode_arg0;
12713 h.mode[2] = mode_arg1;
12714 h.mode[3] = mode_arg2;
12715 h.uns_p[0] = 0;
12716 h.uns_p[1] = 0;
12717 h.uns_p[2] = 0;
12718 h.uns_p[3] = 0;
12720 /* If the builtin is a type that produces unsigned results or takes unsigned
12721 arguments, and it is returned as a decl for the vectorizer (such as
12722 widening multiplies, permute), make sure the arguments and return value
12723 are type correct. */
12724 switch (builtin)
12726 /* unsigned 2 argument functions. */
12727 case ALTIVEC_BUILTIN_VMULEUB_UNS:
12728 case ALTIVEC_BUILTIN_VMULEUH_UNS:
12729 case ALTIVEC_BUILTIN_VMULOUB_UNS:
12730 case ALTIVEC_BUILTIN_VMULOUH_UNS:
12731 h.uns_p[0] = 1;
12732 h.uns_p[1] = 1;
12733 h.uns_p[2] = 1;
12734 break;
12736 /* unsigned 3 argument functions. */
12737 case ALTIVEC_BUILTIN_VPERM_16QI_UNS:
12738 case ALTIVEC_BUILTIN_VPERM_8HI_UNS:
12739 case ALTIVEC_BUILTIN_VPERM_4SI_UNS:
12740 case ALTIVEC_BUILTIN_VPERM_2DI_UNS:
12741 case ALTIVEC_BUILTIN_VSEL_16QI_UNS:
12742 case ALTIVEC_BUILTIN_VSEL_8HI_UNS:
12743 case ALTIVEC_BUILTIN_VSEL_4SI_UNS:
12744 case ALTIVEC_BUILTIN_VSEL_2DI_UNS:
12745 case VSX_BUILTIN_VPERM_16QI_UNS:
12746 case VSX_BUILTIN_VPERM_8HI_UNS:
12747 case VSX_BUILTIN_VPERM_4SI_UNS:
12748 case VSX_BUILTIN_VPERM_2DI_UNS:
12749 case VSX_BUILTIN_XXSEL_16QI_UNS:
12750 case VSX_BUILTIN_XXSEL_8HI_UNS:
12751 case VSX_BUILTIN_XXSEL_4SI_UNS:
12752 case VSX_BUILTIN_XXSEL_2DI_UNS:
12753 h.uns_p[0] = 1;
12754 h.uns_p[1] = 1;
12755 h.uns_p[2] = 1;
12756 h.uns_p[3] = 1;
12757 break;
12759 /* signed permute functions with unsigned char mask. */
12760 case ALTIVEC_BUILTIN_VPERM_16QI:
12761 case ALTIVEC_BUILTIN_VPERM_8HI:
12762 case ALTIVEC_BUILTIN_VPERM_4SI:
12763 case ALTIVEC_BUILTIN_VPERM_4SF:
12764 case ALTIVEC_BUILTIN_VPERM_2DI:
12765 case ALTIVEC_BUILTIN_VPERM_2DF:
12766 case VSX_BUILTIN_VPERM_16QI:
12767 case VSX_BUILTIN_VPERM_8HI:
12768 case VSX_BUILTIN_VPERM_4SI:
12769 case VSX_BUILTIN_VPERM_4SF:
12770 case VSX_BUILTIN_VPERM_2DI:
12771 case VSX_BUILTIN_VPERM_2DF:
12772 h.uns_p[3] = 1;
12773 break;
12775 /* unsigned args, signed return. */
12776 case VSX_BUILTIN_XVCVUXDDP_UNS:
12777 case ALTIVEC_BUILTIN_UNSFLOAT_V4SI_V4SF:
12778 h.uns_p[1] = 1;
12779 break;
12781 /* signed args, unsigned return. */
12782 case VSX_BUILTIN_XVCVDPUXDS_UNS:
12783 case ALTIVEC_BUILTIN_FIXUNS_V4SF_V4SI:
12784 h.uns_p[0] = 1;
12785 break;
12787 default:
12788 break;
12791 /* Figure out how many args are present. */
12792 while (num_args > 0 && h.mode[num_args] == VOIDmode)
12793 num_args--;
12795 if (num_args == 0)
12796 fatal_error ("internal error: builtin function %s had no type", name);
12798 ret_type = builtin_mode_to_type[h.mode[0]][h.uns_p[0]];
12799 if (!ret_type && h.uns_p[0])
12800 ret_type = builtin_mode_to_type[h.mode[0]][0];
12802 if (!ret_type)
12803 fatal_error ("internal error: builtin function %s had an unexpected "
12804 "return type %s", name, GET_MODE_NAME (h.mode[0]));
12806 for (i = 0; i < (int) ARRAY_SIZE (arg_type); i++)
12807 arg_type[i] = NULL_TREE;
12809 for (i = 0; i < num_args; i++)
12811 int m = (int) h.mode[i+1];
12812 int uns_p = h.uns_p[i+1];
12814 arg_type[i] = builtin_mode_to_type[m][uns_p];
12815 if (!arg_type[i] && uns_p)
12816 arg_type[i] = builtin_mode_to_type[m][0];
12818 if (!arg_type[i])
12819 fatal_error ("internal error: builtin function %s, argument %d "
12820 "had unexpected argument type %s", name, i,
12821 GET_MODE_NAME (m));
12824 found = htab_find_slot (builtin_hash_table, &h, INSERT);
12825 if (*found == NULL)
12827 h2 = ggc_alloc_builtin_hash_struct ();
12828 *h2 = h;
12829 *found = (void *)h2;
12831 h2->type = build_function_type_list (ret_type, arg_type[0], arg_type[1],
12832 arg_type[2], NULL_TREE);
12835 return ((struct builtin_hash_struct *)(*found))->type;
12838 static void
12839 rs6000_common_init_builtins (void)
12841 const struct builtin_description *d;
12842 size_t i;
12844 tree opaque_ftype_opaque = NULL_TREE;
12845 tree opaque_ftype_opaque_opaque = NULL_TREE;
12846 tree opaque_ftype_opaque_opaque_opaque = NULL_TREE;
12847 tree v2si_ftype_qi = NULL_TREE;
12848 tree v2si_ftype_v2si_qi = NULL_TREE;
12849 tree v2si_ftype_int_qi = NULL_TREE;
12850 HOST_WIDE_INT builtin_mask = rs6000_builtin_mask;
12852 if (!TARGET_PAIRED_FLOAT)
12854 builtin_mode_to_type[V2SImode][0] = opaque_V2SI_type_node;
12855 builtin_mode_to_type[V2SFmode][0] = opaque_V2SF_type_node;
12858 /* Paired and SPE builtins are only available if you build a compiler with
12859 the appropriate options, so only create those builtins with the
12860 appropriate compiler option. Create Altivec and VSX builtins on machines
12861 with at least the general purpose extensions (970 and newer) to allow the
12862 use of the target attribute.. */
12864 if (TARGET_EXTRA_BUILTINS)
12865 builtin_mask |= RS6000_BTM_COMMON;
12867 /* Add the ternary operators. */
12868 d = bdesc_3arg;
12869 for (i = 0; i < ARRAY_SIZE (bdesc_3arg); i++, d++)
12871 tree type;
12872 HOST_WIDE_INT mask = d->mask;
12874 if ((mask & builtin_mask) != mask)
12876 if (TARGET_DEBUG_BUILTIN)
12877 fprintf (stderr, "rs6000_builtin, skip ternary %s\n", d->name);
12878 continue;
12881 if (rs6000_overloaded_builtin_p (d->code))
12883 if (! (type = opaque_ftype_opaque_opaque_opaque))
12884 type = opaque_ftype_opaque_opaque_opaque
12885 = build_function_type_list (opaque_V4SI_type_node,
12886 opaque_V4SI_type_node,
12887 opaque_V4SI_type_node,
12888 opaque_V4SI_type_node,
12889 NULL_TREE);
12891 else
12893 enum insn_code icode = d->icode;
12894 if (d->name == 0 || icode == CODE_FOR_nothing)
12895 continue;
12897 type = builtin_function_type (insn_data[icode].operand[0].mode,
12898 insn_data[icode].operand[1].mode,
12899 insn_data[icode].operand[2].mode,
12900 insn_data[icode].operand[3].mode,
12901 d->code, d->name);
12904 def_builtin (d->name, type, d->code);
12907 /* Add the binary operators. */
12908 d = bdesc_2arg;
12909 for (i = 0; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
12911 enum machine_mode mode0, mode1, mode2;
12912 tree type;
12913 HOST_WIDE_INT mask = d->mask;
12915 if ((mask & builtin_mask) != mask)
12917 if (TARGET_DEBUG_BUILTIN)
12918 fprintf (stderr, "rs6000_builtin, skip binary %s\n", d->name);
12919 continue;
12922 if (rs6000_overloaded_builtin_p (d->code))
12924 if (! (type = opaque_ftype_opaque_opaque))
12925 type = opaque_ftype_opaque_opaque
12926 = build_function_type_list (opaque_V4SI_type_node,
12927 opaque_V4SI_type_node,
12928 opaque_V4SI_type_node,
12929 NULL_TREE);
12931 else
12933 enum insn_code icode = d->icode;
12934 if (d->name == 0 || icode == CODE_FOR_nothing)
12935 continue;
12937 mode0 = insn_data[icode].operand[0].mode;
12938 mode1 = insn_data[icode].operand[1].mode;
12939 mode2 = insn_data[icode].operand[2].mode;
12941 if (mode0 == V2SImode && mode1 == V2SImode && mode2 == QImode)
12943 if (! (type = v2si_ftype_v2si_qi))
12944 type = v2si_ftype_v2si_qi
12945 = build_function_type_list (opaque_V2SI_type_node,
12946 opaque_V2SI_type_node,
12947 char_type_node,
12948 NULL_TREE);
12951 else if (mode0 == V2SImode && GET_MODE_CLASS (mode1) == MODE_INT
12952 && mode2 == QImode)
12954 if (! (type = v2si_ftype_int_qi))
12955 type = v2si_ftype_int_qi
12956 = build_function_type_list (opaque_V2SI_type_node,
12957 integer_type_node,
12958 char_type_node,
12959 NULL_TREE);
12962 else
12963 type = builtin_function_type (mode0, mode1, mode2, VOIDmode,
12964 d->code, d->name);
12967 def_builtin (d->name, type, d->code);
12970 /* Add the simple unary operators. */
12971 d = bdesc_1arg;
12972 for (i = 0; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
12974 enum machine_mode mode0, mode1;
12975 tree type;
12976 HOST_WIDE_INT mask = d->mask;
12978 if ((mask & builtin_mask) != mask)
12980 if (TARGET_DEBUG_BUILTIN)
12981 fprintf (stderr, "rs6000_builtin, skip unary %s\n", d->name);
12982 continue;
12985 if (rs6000_overloaded_builtin_p (d->code))
12987 if (! (type = opaque_ftype_opaque))
12988 type = opaque_ftype_opaque
12989 = build_function_type_list (opaque_V4SI_type_node,
12990 opaque_V4SI_type_node,
12991 NULL_TREE);
12993 else
12995 enum insn_code icode = d->icode;
12996 if (d->name == 0 || icode == CODE_FOR_nothing)
12997 continue;
12999 mode0 = insn_data[icode].operand[0].mode;
13000 mode1 = insn_data[icode].operand[1].mode;
13002 if (mode0 == V2SImode && mode1 == QImode)
13004 if (! (type = v2si_ftype_qi))
13005 type = v2si_ftype_qi
13006 = build_function_type_list (opaque_V2SI_type_node,
13007 char_type_node,
13008 NULL_TREE);
13011 else
13012 type = builtin_function_type (mode0, mode1, VOIDmode, VOIDmode,
13013 d->code, d->name);
13016 def_builtin (d->name, type, d->code);
13020 static void
13021 rs6000_init_libfuncs (void)
13023 if (!TARGET_IEEEQUAD)
13024 /* AIX/Darwin/64-bit Linux quad floating point routines. */
13025 if (!TARGET_XL_COMPAT)
13027 set_optab_libfunc (add_optab, TFmode, "__gcc_qadd");
13028 set_optab_libfunc (sub_optab, TFmode, "__gcc_qsub");
13029 set_optab_libfunc (smul_optab, TFmode, "__gcc_qmul");
13030 set_optab_libfunc (sdiv_optab, TFmode, "__gcc_qdiv");
13032 if (!(TARGET_HARD_FLOAT && (TARGET_FPRS || TARGET_E500_DOUBLE)))
13034 set_optab_libfunc (neg_optab, TFmode, "__gcc_qneg");
13035 set_optab_libfunc (eq_optab, TFmode, "__gcc_qeq");
13036 set_optab_libfunc (ne_optab, TFmode, "__gcc_qne");
13037 set_optab_libfunc (gt_optab, TFmode, "__gcc_qgt");
13038 set_optab_libfunc (ge_optab, TFmode, "__gcc_qge");
13039 set_optab_libfunc (lt_optab, TFmode, "__gcc_qlt");
13040 set_optab_libfunc (le_optab, TFmode, "__gcc_qle");
13042 set_conv_libfunc (sext_optab, TFmode, SFmode, "__gcc_stoq");
13043 set_conv_libfunc (sext_optab, TFmode, DFmode, "__gcc_dtoq");
13044 set_conv_libfunc (trunc_optab, SFmode, TFmode, "__gcc_qtos");
13045 set_conv_libfunc (trunc_optab, DFmode, TFmode, "__gcc_qtod");
13046 set_conv_libfunc (sfix_optab, SImode, TFmode, "__gcc_qtoi");
13047 set_conv_libfunc (ufix_optab, SImode, TFmode, "__gcc_qtou");
13048 set_conv_libfunc (sfloat_optab, TFmode, SImode, "__gcc_itoq");
13049 set_conv_libfunc (ufloat_optab, TFmode, SImode, "__gcc_utoq");
13052 if (!(TARGET_HARD_FLOAT && TARGET_FPRS))
13053 set_optab_libfunc (unord_optab, TFmode, "__gcc_qunord");
13055 else
13057 set_optab_libfunc (add_optab, TFmode, "_xlqadd");
13058 set_optab_libfunc (sub_optab, TFmode, "_xlqsub");
13059 set_optab_libfunc (smul_optab, TFmode, "_xlqmul");
13060 set_optab_libfunc (sdiv_optab, TFmode, "_xlqdiv");
13062 else
13064 /* 32-bit SVR4 quad floating point routines. */
13066 set_optab_libfunc (add_optab, TFmode, "_q_add");
13067 set_optab_libfunc (sub_optab, TFmode, "_q_sub");
13068 set_optab_libfunc (neg_optab, TFmode, "_q_neg");
13069 set_optab_libfunc (smul_optab, TFmode, "_q_mul");
13070 set_optab_libfunc (sdiv_optab, TFmode, "_q_div");
13071 if (TARGET_PPC_GPOPT)
13072 set_optab_libfunc (sqrt_optab, TFmode, "_q_sqrt");
13074 set_optab_libfunc (eq_optab, TFmode, "_q_feq");
13075 set_optab_libfunc (ne_optab, TFmode, "_q_fne");
13076 set_optab_libfunc (gt_optab, TFmode, "_q_fgt");
13077 set_optab_libfunc (ge_optab, TFmode, "_q_fge");
13078 set_optab_libfunc (lt_optab, TFmode, "_q_flt");
13079 set_optab_libfunc (le_optab, TFmode, "_q_fle");
13081 set_conv_libfunc (sext_optab, TFmode, SFmode, "_q_stoq");
13082 set_conv_libfunc (sext_optab, TFmode, DFmode, "_q_dtoq");
13083 set_conv_libfunc (trunc_optab, SFmode, TFmode, "_q_qtos");
13084 set_conv_libfunc (trunc_optab, DFmode, TFmode, "_q_qtod");
13085 set_conv_libfunc (sfix_optab, SImode, TFmode, "_q_qtoi");
13086 set_conv_libfunc (ufix_optab, SImode, TFmode, "_q_qtou");
13087 set_conv_libfunc (sfloat_optab, TFmode, SImode, "_q_itoq");
13088 set_conv_libfunc (ufloat_optab, TFmode, SImode, "_q_utoq");
13093 /* Expand a block clear operation, and return 1 if successful. Return 0
13094 if we should let the compiler generate normal code.
13096 operands[0] is the destination
13097 operands[1] is the length
13098 operands[3] is the alignment */
13101 expand_block_clear (rtx operands[])
13103 rtx orig_dest = operands[0];
13104 rtx bytes_rtx = operands[1];
13105 rtx align_rtx = operands[3];
13106 bool constp = (GET_CODE (bytes_rtx) == CONST_INT);
13107 HOST_WIDE_INT align;
13108 HOST_WIDE_INT bytes;
13109 int offset;
13110 int clear_bytes;
13111 int clear_step;
13113 /* If this is not a fixed size move, just call memcpy */
13114 if (! constp)
13115 return 0;
13117 /* This must be a fixed size alignment */
13118 gcc_assert (GET_CODE (align_rtx) == CONST_INT);
13119 align = INTVAL (align_rtx) * BITS_PER_UNIT;
13121 /* Anything to clear? */
13122 bytes = INTVAL (bytes_rtx);
13123 if (bytes <= 0)
13124 return 1;
13126 /* Use the builtin memset after a point, to avoid huge code bloat.
13127 When optimize_size, avoid any significant code bloat; calling
13128 memset is about 4 instructions, so allow for one instruction to
13129 load zero and three to do clearing. */
13130 if (TARGET_ALTIVEC && align >= 128)
13131 clear_step = 16;
13132 else if (TARGET_POWERPC64 && align >= 32)
13133 clear_step = 8;
13134 else if (TARGET_SPE && align >= 64)
13135 clear_step = 8;
13136 else
13137 clear_step = 4;
13139 if (optimize_size && bytes > 3 * clear_step)
13140 return 0;
13141 if (! optimize_size && bytes > 8 * clear_step)
13142 return 0;
13144 for (offset = 0; bytes > 0; offset += clear_bytes, bytes -= clear_bytes)
13146 enum machine_mode mode = BLKmode;
13147 rtx dest;
13149 if (bytes >= 16 && TARGET_ALTIVEC && align >= 128)
13151 clear_bytes = 16;
13152 mode = V4SImode;
13154 else if (bytes >= 8 && TARGET_SPE && align >= 64)
13156 clear_bytes = 8;
13157 mode = V2SImode;
13159 else if (bytes >= 8 && TARGET_POWERPC64
13160 /* 64-bit loads and stores require word-aligned
13161 displacements. */
13162 && (align >= 64 || (!STRICT_ALIGNMENT && align >= 32)))
13164 clear_bytes = 8;
13165 mode = DImode;
13167 else if (bytes >= 4 && (align >= 32 || !STRICT_ALIGNMENT))
13168 { /* move 4 bytes */
13169 clear_bytes = 4;
13170 mode = SImode;
13172 else if (bytes >= 2 && (align >= 16 || !STRICT_ALIGNMENT))
13173 { /* move 2 bytes */
13174 clear_bytes = 2;
13175 mode = HImode;
13177 else /* move 1 byte at a time */
13179 clear_bytes = 1;
13180 mode = QImode;
13183 dest = adjust_address (orig_dest, mode, offset);
13185 emit_move_insn (dest, CONST0_RTX (mode));
13188 return 1;
13192 /* Expand a block move operation, and return 1 if successful. Return 0
13193 if we should let the compiler generate normal code.
13195 operands[0] is the destination
13196 operands[1] is the source
13197 operands[2] is the length
13198 operands[3] is the alignment */
13200 #define MAX_MOVE_REG 4
13203 expand_block_move (rtx operands[])
13205 rtx orig_dest = operands[0];
13206 rtx orig_src = operands[1];
13207 rtx bytes_rtx = operands[2];
13208 rtx align_rtx = operands[3];
13209 int constp = (GET_CODE (bytes_rtx) == CONST_INT);
13210 int align;
13211 int bytes;
13212 int offset;
13213 int move_bytes;
13214 rtx stores[MAX_MOVE_REG];
13215 int num_reg = 0;
13217 /* If this is not a fixed size move, just call memcpy */
13218 if (! constp)
13219 return 0;
13221 /* This must be a fixed size alignment */
13222 gcc_assert (GET_CODE (align_rtx) == CONST_INT);
13223 align = INTVAL (align_rtx) * BITS_PER_UNIT;
13225 /* Anything to move? */
13226 bytes = INTVAL (bytes_rtx);
13227 if (bytes <= 0)
13228 return 1;
13230 if (bytes > rs6000_block_move_inline_limit)
13231 return 0;
13233 for (offset = 0; bytes > 0; offset += move_bytes, bytes -= move_bytes)
13235 union {
13236 rtx (*movmemsi) (rtx, rtx, rtx, rtx);
13237 rtx (*mov) (rtx, rtx);
13238 } gen_func;
13239 enum machine_mode mode = BLKmode;
13240 rtx src, dest;
13242 /* Altivec first, since it will be faster than a string move
13243 when it applies, and usually not significantly larger. */
13244 if (TARGET_ALTIVEC && bytes >= 16 && align >= 128)
13246 move_bytes = 16;
13247 mode = V4SImode;
13248 gen_func.mov = gen_movv4si;
13250 else if (TARGET_SPE && bytes >= 8 && align >= 64)
13252 move_bytes = 8;
13253 mode = V2SImode;
13254 gen_func.mov = gen_movv2si;
13256 else if (TARGET_STRING
13257 && bytes > 24 /* move up to 32 bytes at a time */
13258 && ! fixed_regs[5]
13259 && ! fixed_regs[6]
13260 && ! fixed_regs[7]
13261 && ! fixed_regs[8]
13262 && ! fixed_regs[9]
13263 && ! fixed_regs[10]
13264 && ! fixed_regs[11]
13265 && ! fixed_regs[12])
13267 move_bytes = (bytes > 32) ? 32 : bytes;
13268 gen_func.movmemsi = gen_movmemsi_8reg;
13270 else if (TARGET_STRING
13271 && bytes > 16 /* move up to 24 bytes at a time */
13272 && ! fixed_regs[5]
13273 && ! fixed_regs[6]
13274 && ! fixed_regs[7]
13275 && ! fixed_regs[8]
13276 && ! fixed_regs[9]
13277 && ! fixed_regs[10])
13279 move_bytes = (bytes > 24) ? 24 : bytes;
13280 gen_func.movmemsi = gen_movmemsi_6reg;
13282 else if (TARGET_STRING
13283 && bytes > 8 /* move up to 16 bytes at a time */
13284 && ! fixed_regs[5]
13285 && ! fixed_regs[6]
13286 && ! fixed_regs[7]
13287 && ! fixed_regs[8])
13289 move_bytes = (bytes > 16) ? 16 : bytes;
13290 gen_func.movmemsi = gen_movmemsi_4reg;
13292 else if (bytes >= 8 && TARGET_POWERPC64
13293 /* 64-bit loads and stores require word-aligned
13294 displacements. */
13295 && (align >= 64 || (!STRICT_ALIGNMENT && align >= 32)))
13297 move_bytes = 8;
13298 mode = DImode;
13299 gen_func.mov = gen_movdi;
13301 else if (TARGET_STRING && bytes > 4 && !TARGET_POWERPC64)
13302 { /* move up to 8 bytes at a time */
13303 move_bytes = (bytes > 8) ? 8 : bytes;
13304 gen_func.movmemsi = gen_movmemsi_2reg;
13306 else if (bytes >= 4 && (align >= 32 || !STRICT_ALIGNMENT))
13307 { /* move 4 bytes */
13308 move_bytes = 4;
13309 mode = SImode;
13310 gen_func.mov = gen_movsi;
13312 else if (bytes >= 2 && (align >= 16 || !STRICT_ALIGNMENT))
13313 { /* move 2 bytes */
13314 move_bytes = 2;
13315 mode = HImode;
13316 gen_func.mov = gen_movhi;
13318 else if (TARGET_STRING && bytes > 1)
13319 { /* move up to 4 bytes at a time */
13320 move_bytes = (bytes > 4) ? 4 : bytes;
13321 gen_func.movmemsi = gen_movmemsi_1reg;
13323 else /* move 1 byte at a time */
13325 move_bytes = 1;
13326 mode = QImode;
13327 gen_func.mov = gen_movqi;
13330 src = adjust_address (orig_src, mode, offset);
13331 dest = adjust_address (orig_dest, mode, offset);
13333 if (mode != BLKmode)
13335 rtx tmp_reg = gen_reg_rtx (mode);
13337 emit_insn ((*gen_func.mov) (tmp_reg, src));
13338 stores[num_reg++] = (*gen_func.mov) (dest, tmp_reg);
13341 if (mode == BLKmode || num_reg >= MAX_MOVE_REG || bytes == move_bytes)
13343 int i;
13344 for (i = 0; i < num_reg; i++)
13345 emit_insn (stores[i]);
13346 num_reg = 0;
13349 if (mode == BLKmode)
13351 /* Move the address into scratch registers. The movmemsi
13352 patterns require zero offset. */
13353 if (!REG_P (XEXP (src, 0)))
13355 rtx src_reg = copy_addr_to_reg (XEXP (src, 0));
13356 src = replace_equiv_address (src, src_reg);
13358 set_mem_size (src, move_bytes);
13360 if (!REG_P (XEXP (dest, 0)))
13362 rtx dest_reg = copy_addr_to_reg (XEXP (dest, 0));
13363 dest = replace_equiv_address (dest, dest_reg);
13365 set_mem_size (dest, move_bytes);
13367 emit_insn ((*gen_func.movmemsi) (dest, src,
13368 GEN_INT (move_bytes & 31),
13369 align_rtx));
13373 return 1;
13377 /* Return a string to perform a load_multiple operation.
13378 operands[0] is the vector.
13379 operands[1] is the source address.
13380 operands[2] is the first destination register. */
13382 const char *
13383 rs6000_output_load_multiple (rtx operands[3])
13385 /* We have to handle the case where the pseudo used to contain the address
13386 is assigned to one of the output registers. */
13387 int i, j;
13388 int words = XVECLEN (operands[0], 0);
13389 rtx xop[10];
13391 if (XVECLEN (operands[0], 0) == 1)
13392 return "lwz %2,0(%1)";
13394 for (i = 0; i < words; i++)
13395 if (refers_to_regno_p (REGNO (operands[2]) + i,
13396 REGNO (operands[2]) + i + 1, operands[1], 0))
13398 if (i == words-1)
13400 xop[0] = GEN_INT (4 * (words-1));
13401 xop[1] = operands[1];
13402 xop[2] = operands[2];
13403 output_asm_insn ("lswi %2,%1,%0\n\tlwz %1,%0(%1)", xop);
13404 return "";
13406 else if (i == 0)
13408 xop[0] = GEN_INT (4 * (words-1));
13409 xop[1] = operands[1];
13410 xop[2] = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
13411 output_asm_insn ("addi %1,%1,4\n\tlswi %2,%1,%0\n\tlwz %1,-4(%1)", xop);
13412 return "";
13414 else
13416 for (j = 0; j < words; j++)
13417 if (j != i)
13419 xop[0] = GEN_INT (j * 4);
13420 xop[1] = operands[1];
13421 xop[2] = gen_rtx_REG (SImode, REGNO (operands[2]) + j);
13422 output_asm_insn ("lwz %2,%0(%1)", xop);
13424 xop[0] = GEN_INT (i * 4);
13425 xop[1] = operands[1];
13426 output_asm_insn ("lwz %1,%0(%1)", xop);
13427 return "";
13431 return "lswi %2,%1,%N0";
13435 /* A validation routine: say whether CODE, a condition code, and MODE
13436 match. The other alternatives either don't make sense or should
13437 never be generated. */
13439 void
13440 validate_condition_mode (enum rtx_code code, enum machine_mode mode)
13442 gcc_assert ((GET_RTX_CLASS (code) == RTX_COMPARE
13443 || GET_RTX_CLASS (code) == RTX_COMM_COMPARE)
13444 && GET_MODE_CLASS (mode) == MODE_CC);
13446 /* These don't make sense. */
13447 gcc_assert ((code != GT && code != LT && code != GE && code != LE)
13448 || mode != CCUNSmode);
13450 gcc_assert ((code != GTU && code != LTU && code != GEU && code != LEU)
13451 || mode == CCUNSmode);
13453 gcc_assert (mode == CCFPmode
13454 || (code != ORDERED && code != UNORDERED
13455 && code != UNEQ && code != LTGT
13456 && code != UNGT && code != UNLT
13457 && code != UNGE && code != UNLE));
13459 /* These should never be generated except for
13460 flag_finite_math_only. */
13461 gcc_assert (mode != CCFPmode
13462 || flag_finite_math_only
13463 || (code != LE && code != GE
13464 && code != UNEQ && code != LTGT
13465 && code != UNGT && code != UNLT));
13467 /* These are invalid; the information is not there. */
13468 gcc_assert (mode != CCEQmode || code == EQ || code == NE);
13472 /* Return 1 if ANDOP is a mask that has no bits on that are not in the
13473 mask required to convert the result of a rotate insn into a shift
13474 left insn of SHIFTOP bits. Both are known to be SImode CONST_INT. */
13477 includes_lshift_p (rtx shiftop, rtx andop)
13479 unsigned HOST_WIDE_INT shift_mask = ~(unsigned HOST_WIDE_INT) 0;
13481 shift_mask <<= INTVAL (shiftop);
13483 return (INTVAL (andop) & 0xffffffff & ~shift_mask) == 0;
13486 /* Similar, but for right shift. */
13489 includes_rshift_p (rtx shiftop, rtx andop)
13491 unsigned HOST_WIDE_INT shift_mask = ~(unsigned HOST_WIDE_INT) 0;
13493 shift_mask >>= INTVAL (shiftop);
13495 return (INTVAL (andop) & 0xffffffff & ~shift_mask) == 0;
13498 /* Return 1 if ANDOP is a mask suitable for use with an rldic insn
13499 to perform a left shift. It must have exactly SHIFTOP least
13500 significant 0's, then one or more 1's, then zero or more 0's. */
13503 includes_rldic_lshift_p (rtx shiftop, rtx andop)
13505 if (GET_CODE (andop) == CONST_INT)
13507 HOST_WIDE_INT c, lsb, shift_mask;
13509 c = INTVAL (andop);
13510 if (c == 0 || c == ~0)
13511 return 0;
13513 shift_mask = ~0;
13514 shift_mask <<= INTVAL (shiftop);
13516 /* Find the least significant one bit. */
13517 lsb = c & -c;
13519 /* It must coincide with the LSB of the shift mask. */
13520 if (-lsb != shift_mask)
13521 return 0;
13523 /* Invert to look for the next transition (if any). */
13524 c = ~c;
13526 /* Remove the low group of ones (originally low group of zeros). */
13527 c &= -lsb;
13529 /* Again find the lsb, and check we have all 1's above. */
13530 lsb = c & -c;
13531 return c == -lsb;
13533 else
13534 return 0;
13537 /* Return 1 if ANDOP is a mask suitable for use with an rldicr insn
13538 to perform a left shift. It must have SHIFTOP or more least
13539 significant 0's, with the remainder of the word 1's. */
13542 includes_rldicr_lshift_p (rtx shiftop, rtx andop)
13544 if (GET_CODE (andop) == CONST_INT)
13546 HOST_WIDE_INT c, lsb, shift_mask;
13548 shift_mask = ~0;
13549 shift_mask <<= INTVAL (shiftop);
13550 c = INTVAL (andop);
13552 /* Find the least significant one bit. */
13553 lsb = c & -c;
13555 /* It must be covered by the shift mask.
13556 This test also rejects c == 0. */
13557 if ((lsb & shift_mask) == 0)
13558 return 0;
13560 /* Check we have all 1's above the transition, and reject all 1's. */
13561 return c == -lsb && lsb != 1;
13563 else
13564 return 0;
13567 /* Return 1 if operands will generate a valid arguments to rlwimi
13568 instruction for insert with right shift in 64-bit mode. The mask may
13569 not start on the first bit or stop on the last bit because wrap-around
13570 effects of instruction do not correspond to semantics of RTL insn. */
13573 insvdi_rshift_rlwimi_p (rtx sizeop, rtx startop, rtx shiftop)
13575 if (INTVAL (startop) > 32
13576 && INTVAL (startop) < 64
13577 && INTVAL (sizeop) > 1
13578 && INTVAL (sizeop) + INTVAL (startop) < 64
13579 && INTVAL (shiftop) > 0
13580 && INTVAL (sizeop) + INTVAL (shiftop) < 32
13581 && (64 - (INTVAL (shiftop) & 63)) >= INTVAL (sizeop))
13582 return 1;
13584 return 0;
13587 /* Return 1 if REGNO (reg1) == REGNO (reg2) - 1 making them candidates
13588 for lfq and stfq insns iff the registers are hard registers. */
13591 registers_ok_for_quad_peep (rtx reg1, rtx reg2)
13593 /* We might have been passed a SUBREG. */
13594 if (GET_CODE (reg1) != REG || GET_CODE (reg2) != REG)
13595 return 0;
13597 /* We might have been passed non floating point registers. */
13598 if (!FP_REGNO_P (REGNO (reg1))
13599 || !FP_REGNO_P (REGNO (reg2)))
13600 return 0;
13602 return (REGNO (reg1) == REGNO (reg2) - 1);
13605 /* Return 1 if addr1 and addr2 are suitable for lfq or stfq insn.
13606 addr1 and addr2 must be in consecutive memory locations
13607 (addr2 == addr1 + 8). */
13610 mems_ok_for_quad_peep (rtx mem1, rtx mem2)
13612 rtx addr1, addr2;
13613 unsigned int reg1, reg2;
13614 int offset1, offset2;
13616 /* The mems cannot be volatile. */
13617 if (MEM_VOLATILE_P (mem1) || MEM_VOLATILE_P (mem2))
13618 return 0;
13620 addr1 = XEXP (mem1, 0);
13621 addr2 = XEXP (mem2, 0);
13623 /* Extract an offset (if used) from the first addr. */
13624 if (GET_CODE (addr1) == PLUS)
13626 /* If not a REG, return zero. */
13627 if (GET_CODE (XEXP (addr1, 0)) != REG)
13628 return 0;
13629 else
13631 reg1 = REGNO (XEXP (addr1, 0));
13632 /* The offset must be constant! */
13633 if (GET_CODE (XEXP (addr1, 1)) != CONST_INT)
13634 return 0;
13635 offset1 = INTVAL (XEXP (addr1, 1));
13638 else if (GET_CODE (addr1) != REG)
13639 return 0;
13640 else
13642 reg1 = REGNO (addr1);
13643 /* This was a simple (mem (reg)) expression. Offset is 0. */
13644 offset1 = 0;
13647 /* And now for the second addr. */
13648 if (GET_CODE (addr2) == PLUS)
13650 /* If not a REG, return zero. */
13651 if (GET_CODE (XEXP (addr2, 0)) != REG)
13652 return 0;
13653 else
13655 reg2 = REGNO (XEXP (addr2, 0));
13656 /* The offset must be constant. */
13657 if (GET_CODE (XEXP (addr2, 1)) != CONST_INT)
13658 return 0;
13659 offset2 = INTVAL (XEXP (addr2, 1));
13662 else if (GET_CODE (addr2) != REG)
13663 return 0;
13664 else
13666 reg2 = REGNO (addr2);
13667 /* This was a simple (mem (reg)) expression. Offset is 0. */
13668 offset2 = 0;
13671 /* Both of these must have the same base register. */
13672 if (reg1 != reg2)
13673 return 0;
13675 /* The offset for the second addr must be 8 more than the first addr. */
13676 if (offset2 != offset1 + 8)
13677 return 0;
13679 /* All the tests passed. addr1 and addr2 are valid for lfq or stfq
13680 instructions. */
13681 return 1;
13686 rs6000_secondary_memory_needed_rtx (enum machine_mode mode)
13688 static bool eliminated = false;
13689 rtx ret;
13691 if (mode != SDmode || TARGET_NO_SDMODE_STACK)
13692 ret = assign_stack_local (mode, GET_MODE_SIZE (mode), 0);
13693 else
13695 rtx mem = cfun->machine->sdmode_stack_slot;
13696 gcc_assert (mem != NULL_RTX);
13698 if (!eliminated)
13700 mem = eliminate_regs (mem, VOIDmode, NULL_RTX);
13701 cfun->machine->sdmode_stack_slot = mem;
13702 eliminated = true;
13704 ret = mem;
13707 if (TARGET_DEBUG_ADDR)
13709 fprintf (stderr, "\nrs6000_secondary_memory_needed_rtx, mode %s, rtx:\n",
13710 GET_MODE_NAME (mode));
13711 if (!ret)
13712 fprintf (stderr, "\tNULL_RTX\n");
13713 else
13714 debug_rtx (ret);
13717 return ret;
13720 static tree
13721 rs6000_check_sdmode (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
13723 /* Don't walk into types. */
13724 if (*tp == NULL_TREE || *tp == error_mark_node || TYPE_P (*tp))
13726 *walk_subtrees = 0;
13727 return NULL_TREE;
13730 switch (TREE_CODE (*tp))
13732 case VAR_DECL:
13733 case PARM_DECL:
13734 case FIELD_DECL:
13735 case RESULT_DECL:
13736 case SSA_NAME:
13737 case REAL_CST:
13738 case MEM_REF:
13739 case VIEW_CONVERT_EXPR:
13740 if (TYPE_MODE (TREE_TYPE (*tp)) == SDmode)
13741 return *tp;
13742 break;
13743 default:
13744 break;
13747 return NULL_TREE;
13750 enum reload_reg_type {
13751 GPR_REGISTER_TYPE,
13752 VECTOR_REGISTER_TYPE,
13753 OTHER_REGISTER_TYPE
13756 static enum reload_reg_type
13757 rs6000_reload_register_type (enum reg_class rclass)
13759 switch (rclass)
13761 case GENERAL_REGS:
13762 case BASE_REGS:
13763 return GPR_REGISTER_TYPE;
13765 case FLOAT_REGS:
13766 case ALTIVEC_REGS:
13767 case VSX_REGS:
13768 return VECTOR_REGISTER_TYPE;
13770 default:
13771 return OTHER_REGISTER_TYPE;
13775 /* Inform reload about cases where moving X with a mode MODE to a register in
13776 RCLASS requires an extra scratch or immediate register. Return the class
13777 needed for the immediate register.
13779 For VSX and Altivec, we may need a register to convert sp+offset into
13780 reg+sp.
13782 For misaligned 64-bit gpr loads and stores we need a register to
13783 convert an offset address to indirect. */
13785 static reg_class_t
13786 rs6000_secondary_reload (bool in_p,
13787 rtx x,
13788 reg_class_t rclass_i,
13789 enum machine_mode mode,
13790 secondary_reload_info *sri)
13792 enum reg_class rclass = (enum reg_class) rclass_i;
13793 reg_class_t ret = ALL_REGS;
13794 enum insn_code icode;
13795 bool default_p = false;
13797 sri->icode = CODE_FOR_nothing;
13799 /* Convert vector loads and stores into gprs to use an additional base
13800 register. */
13801 icode = rs6000_vector_reload[mode][in_p != false];
13802 if (icode != CODE_FOR_nothing)
13804 ret = NO_REGS;
13805 sri->icode = CODE_FOR_nothing;
13806 sri->extra_cost = 0;
13808 if (GET_CODE (x) == MEM)
13810 rtx addr = XEXP (x, 0);
13812 /* Loads to and stores from gprs can do reg+offset, and wouldn't need
13813 an extra register in that case, but it would need an extra
13814 register if the addressing is reg+reg or (reg+reg)&(-16). */
13815 if (rclass == GENERAL_REGS || rclass == BASE_REGS)
13817 if (!legitimate_indirect_address_p (addr, false)
13818 && !rs6000_legitimate_offset_address_p (PTImode, addr,
13819 false, true))
13821 sri->icode = icode;
13822 /* account for splitting the loads, and converting the
13823 address from reg+reg to reg. */
13824 sri->extra_cost = (((TARGET_64BIT) ? 3 : 5)
13825 + ((GET_CODE (addr) == AND) ? 1 : 0));
13828 /* Allow scalar loads to/from the traditional floating point
13829 registers, even if VSX memory is set. */
13830 else if ((rclass == FLOAT_REGS || rclass == NO_REGS)
13831 && (GET_MODE_SIZE (mode) == 4 || GET_MODE_SIZE (mode) == 8)
13832 && (legitimate_indirect_address_p (addr, false)
13833 || legitimate_indirect_address_p (XEXP (addr, 0), false)
13834 || rs6000_legitimate_offset_address_p (mode, addr,
13835 false, true)))
13838 /* Loads to and stores from vector registers can only do reg+reg
13839 addressing. Altivec registers can also do (reg+reg)&(-16). Allow
13840 scalar modes loading up the traditional floating point registers
13841 to use offset addresses. */
13842 else if (rclass == VSX_REGS || rclass == ALTIVEC_REGS
13843 || rclass == FLOAT_REGS || rclass == NO_REGS)
13845 if (!VECTOR_MEM_ALTIVEC_P (mode)
13846 && GET_CODE (addr) == AND
13847 && GET_CODE (XEXP (addr, 1)) == CONST_INT
13848 && INTVAL (XEXP (addr, 1)) == -16
13849 && (legitimate_indirect_address_p (XEXP (addr, 0), false)
13850 || legitimate_indexed_address_p (XEXP (addr, 0), false)))
13852 sri->icode = icode;
13853 sri->extra_cost = ((GET_CODE (XEXP (addr, 0)) == PLUS)
13854 ? 2 : 1);
13856 else if (!legitimate_indirect_address_p (addr, false)
13857 && (rclass == NO_REGS
13858 || !legitimate_indexed_address_p (addr, false)))
13860 sri->icode = icode;
13861 sri->extra_cost = 1;
13863 else
13864 icode = CODE_FOR_nothing;
13866 /* Any other loads, including to pseudo registers which haven't been
13867 assigned to a register yet, default to require a scratch
13868 register. */
13869 else
13871 sri->icode = icode;
13872 sri->extra_cost = 2;
13875 else if (REG_P (x))
13877 int regno = true_regnum (x);
13879 icode = CODE_FOR_nothing;
13880 if (regno < 0 || regno >= FIRST_PSEUDO_REGISTER)
13881 default_p = true;
13882 else
13884 enum reg_class xclass = REGNO_REG_CLASS (regno);
13885 enum reload_reg_type rtype1 = rs6000_reload_register_type (rclass);
13886 enum reload_reg_type rtype2 = rs6000_reload_register_type (xclass);
13888 /* If memory is needed, use default_secondary_reload to create the
13889 stack slot. */
13890 if (rtype1 != rtype2 || rtype1 == OTHER_REGISTER_TYPE)
13891 default_p = true;
13892 else
13893 ret = NO_REGS;
13896 else
13897 default_p = true;
13899 else if (TARGET_POWERPC64
13900 && rs6000_reload_register_type (rclass) == GPR_REGISTER_TYPE
13901 && MEM_P (x)
13902 && GET_MODE_SIZE (GET_MODE (x)) >= UNITS_PER_WORD)
13904 rtx addr = XEXP (x, 0);
13905 rtx off = address_offset (addr);
13907 if (off != NULL_RTX)
13909 unsigned int extra = GET_MODE_SIZE (GET_MODE (x)) - UNITS_PER_WORD;
13910 unsigned HOST_WIDE_INT offset = INTVAL (off);
13912 /* We need a secondary reload when our legitimate_address_p
13913 says the address is good (as otherwise the entire address
13914 will be reloaded), and the offset is not a multiple of
13915 four or we have an address wrap. Address wrap will only
13916 occur for LO_SUMs since legitimate_offset_address_p
13917 rejects addresses for 16-byte mems that will wrap. */
13918 if (GET_CODE (addr) == LO_SUM
13919 ? (1 /* legitimate_address_p allows any offset for lo_sum */
13920 && ((offset & 3) != 0
13921 || ((offset & 0xffff) ^ 0x8000) >= 0x10000 - extra))
13922 : (offset + 0x8000 < 0x10000 - extra /* legitimate_address_p */
13923 && (offset & 3) != 0))
13925 if (in_p)
13926 sri->icode = CODE_FOR_reload_di_load;
13927 else
13928 sri->icode = CODE_FOR_reload_di_store;
13929 sri->extra_cost = 2;
13930 ret = NO_REGS;
13932 else
13933 default_p = true;
13935 else
13936 default_p = true;
13938 else if (!TARGET_POWERPC64
13939 && rs6000_reload_register_type (rclass) == GPR_REGISTER_TYPE
13940 && MEM_P (x)
13941 && GET_MODE_SIZE (GET_MODE (x)) > UNITS_PER_WORD)
13943 rtx addr = XEXP (x, 0);
13944 rtx off = address_offset (addr);
13946 if (off != NULL_RTX)
13948 unsigned int extra = GET_MODE_SIZE (GET_MODE (x)) - UNITS_PER_WORD;
13949 unsigned HOST_WIDE_INT offset = INTVAL (off);
13951 /* We need a secondary reload when our legitimate_address_p
13952 says the address is good (as otherwise the entire address
13953 will be reloaded), and we have a wrap.
13955 legitimate_lo_sum_address_p allows LO_SUM addresses to
13956 have any offset so test for wrap in the low 16 bits.
13958 legitimate_offset_address_p checks for the range
13959 [-0x8000,0x7fff] for mode size of 8 and [-0x8000,0x7ff7]
13960 for mode size of 16. We wrap at [0x7ffc,0x7fff] and
13961 [0x7ff4,0x7fff] respectively, so test for the
13962 intersection of these ranges, [0x7ffc,0x7fff] and
13963 [0x7ff4,0x7ff7] respectively.
13965 Note that the address we see here may have been
13966 manipulated by legitimize_reload_address. */
13967 if (GET_CODE (addr) == LO_SUM
13968 ? ((offset & 0xffff) ^ 0x8000) >= 0x10000 - extra
13969 : offset - (0x8000 - extra) < UNITS_PER_WORD)
13971 if (in_p)
13972 sri->icode = CODE_FOR_reload_si_load;
13973 else
13974 sri->icode = CODE_FOR_reload_si_store;
13975 sri->extra_cost = 2;
13976 ret = NO_REGS;
13978 else
13979 default_p = true;
13981 else
13982 default_p = true;
13984 else
13985 default_p = true;
13987 if (default_p)
13988 ret = default_secondary_reload (in_p, x, rclass, mode, sri);
13990 gcc_assert (ret != ALL_REGS);
13992 if (TARGET_DEBUG_ADDR)
13994 fprintf (stderr,
13995 "\nrs6000_secondary_reload, return %s, in_p = %s, rclass = %s, "
13996 "mode = %s",
13997 reg_class_names[ret],
13998 in_p ? "true" : "false",
13999 reg_class_names[rclass],
14000 GET_MODE_NAME (mode));
14002 if (default_p)
14003 fprintf (stderr, ", default secondary reload");
14005 if (sri->icode != CODE_FOR_nothing)
14006 fprintf (stderr, ", reload func = %s, extra cost = %d\n",
14007 insn_data[sri->icode].name, sri->extra_cost);
14008 else
14009 fprintf (stderr, "\n");
14011 debug_rtx (x);
14014 return ret;
14017 /* Better tracing for rs6000_secondary_reload_inner. */
14019 static void
14020 rs6000_secondary_reload_trace (int line, rtx reg, rtx mem, rtx scratch,
14021 bool store_p)
14023 rtx set, clobber;
14025 gcc_assert (reg != NULL_RTX && mem != NULL_RTX && scratch != NULL_RTX);
14027 fprintf (stderr, "rs6000_secondary_reload_inner:%d, type = %s\n", line,
14028 store_p ? "store" : "load");
14030 if (store_p)
14031 set = gen_rtx_SET (VOIDmode, mem, reg);
14032 else
14033 set = gen_rtx_SET (VOIDmode, reg, mem);
14035 clobber = gen_rtx_CLOBBER (VOIDmode, scratch);
14036 debug_rtx (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, set, clobber)));
14039 static void
14040 rs6000_secondary_reload_fail (int line, rtx reg, rtx mem, rtx scratch,
14041 bool store_p)
14043 rs6000_secondary_reload_trace (line, reg, mem, scratch, store_p);
14044 gcc_unreachable ();
14047 /* Fixup reload addresses for Altivec or VSX loads/stores to change SP+offset
14048 to SP+reg addressing. */
14050 void
14051 rs6000_secondary_reload_inner (rtx reg, rtx mem, rtx scratch, bool store_p)
14053 int regno = true_regnum (reg);
14054 enum machine_mode mode = GET_MODE (reg);
14055 enum reg_class rclass;
14056 rtx addr;
14057 rtx and_op2 = NULL_RTX;
14058 rtx addr_op1;
14059 rtx addr_op2;
14060 rtx scratch_or_premodify = scratch;
14061 rtx and_rtx;
14062 rtx cc_clobber;
14064 if (TARGET_DEBUG_ADDR)
14065 rs6000_secondary_reload_trace (__LINE__, reg, mem, scratch, store_p);
14067 if (regno < 0 || regno >= FIRST_PSEUDO_REGISTER)
14068 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
14070 if (GET_CODE (mem) != MEM)
14071 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
14073 rclass = REGNO_REG_CLASS (regno);
14074 addr = XEXP (mem, 0);
14076 switch (rclass)
14078 /* GPRs can handle reg + small constant, all other addresses need to use
14079 the scratch register. */
14080 case GENERAL_REGS:
14081 case BASE_REGS:
14082 if (GET_CODE (addr) == AND)
14084 and_op2 = XEXP (addr, 1);
14085 addr = XEXP (addr, 0);
14088 if (GET_CODE (addr) == PRE_MODIFY)
14090 scratch_or_premodify = XEXP (addr, 0);
14091 if (!REG_P (scratch_or_premodify))
14092 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
14094 if (GET_CODE (XEXP (addr, 1)) != PLUS)
14095 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
14097 addr = XEXP (addr, 1);
14100 if (GET_CODE (addr) == PLUS
14101 && (and_op2 != NULL_RTX
14102 || !rs6000_legitimate_offset_address_p (PTImode, addr,
14103 false, true)))
14105 addr_op1 = XEXP (addr, 0);
14106 addr_op2 = XEXP (addr, 1);
14107 if (!legitimate_indirect_address_p (addr_op1, false))
14108 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
14110 if (!REG_P (addr_op2)
14111 && (GET_CODE (addr_op2) != CONST_INT
14112 || !satisfies_constraint_I (addr_op2)))
14114 if (TARGET_DEBUG_ADDR)
14116 fprintf (stderr,
14117 "\nMove plus addr to register %s, mode = %s: ",
14118 rs6000_reg_names[REGNO (scratch)],
14119 GET_MODE_NAME (mode));
14120 debug_rtx (addr_op2);
14122 rs6000_emit_move (scratch, addr_op2, Pmode);
14123 addr_op2 = scratch;
14126 emit_insn (gen_rtx_SET (VOIDmode,
14127 scratch_or_premodify,
14128 gen_rtx_PLUS (Pmode,
14129 addr_op1,
14130 addr_op2)));
14132 addr = scratch_or_premodify;
14133 scratch_or_premodify = scratch;
14135 else if (!legitimate_indirect_address_p (addr, false)
14136 && !rs6000_legitimate_offset_address_p (PTImode, addr,
14137 false, true))
14139 if (TARGET_DEBUG_ADDR)
14141 fprintf (stderr, "\nMove addr to register %s, mode = %s: ",
14142 rs6000_reg_names[REGNO (scratch_or_premodify)],
14143 GET_MODE_NAME (mode));
14144 debug_rtx (addr);
14146 rs6000_emit_move (scratch_or_premodify, addr, Pmode);
14147 addr = scratch_or_premodify;
14148 scratch_or_premodify = scratch;
14150 break;
14152 /* Float registers can do offset+reg addressing for scalar types. */
14153 case FLOAT_REGS:
14154 if (legitimate_indirect_address_p (addr, false) /* reg */
14155 || legitimate_indexed_address_p (addr, false) /* reg+reg */
14156 || ((GET_MODE_SIZE (mode) == 4 || GET_MODE_SIZE (mode) == 8)
14157 && and_op2 == NULL_RTX
14158 && scratch_or_premodify == scratch
14159 && rs6000_legitimate_offset_address_p (mode, addr, false, false)))
14160 break;
14162 /* If this isn't a legacy floating point load/store, fall through to the
14163 VSX defaults. */
14165 /* VSX/Altivec registers can only handle reg+reg addressing. Move other
14166 addresses into a scratch register. */
14167 case VSX_REGS:
14168 case ALTIVEC_REGS:
14170 /* With float regs, we need to handle the AND ourselves, since we can't
14171 use the Altivec instruction with an implicit AND -16. Allow scalar
14172 loads to float registers to use reg+offset even if VSX. */
14173 if (GET_CODE (addr) == AND
14174 && (rclass != ALTIVEC_REGS || GET_MODE_SIZE (mode) != 16
14175 || GET_CODE (XEXP (addr, 1)) != CONST_INT
14176 || INTVAL (XEXP (addr, 1)) != -16
14177 || !VECTOR_MEM_ALTIVEC_P (mode)))
14179 and_op2 = XEXP (addr, 1);
14180 addr = XEXP (addr, 0);
14183 /* If we aren't using a VSX load, save the PRE_MODIFY register and use it
14184 as the address later. */
14185 if (GET_CODE (addr) == PRE_MODIFY
14186 && ((ALTIVEC_OR_VSX_VECTOR_MODE (mode)
14187 && (rclass != FLOAT_REGS
14188 || (GET_MODE_SIZE (mode) != 4 && GET_MODE_SIZE (mode) != 8)))
14189 || and_op2 != NULL_RTX
14190 || !legitimate_indexed_address_p (XEXP (addr, 1), false)))
14192 scratch_or_premodify = XEXP (addr, 0);
14193 if (!legitimate_indirect_address_p (scratch_or_premodify, false))
14194 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
14196 if (GET_CODE (XEXP (addr, 1)) != PLUS)
14197 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
14199 addr = XEXP (addr, 1);
14202 if (legitimate_indirect_address_p (addr, false) /* reg */
14203 || legitimate_indexed_address_p (addr, false) /* reg+reg */
14204 || (GET_CODE (addr) == AND /* Altivec memory */
14205 && rclass == ALTIVEC_REGS
14206 && GET_CODE (XEXP (addr, 1)) == CONST_INT
14207 && INTVAL (XEXP (addr, 1)) == -16
14208 && (legitimate_indirect_address_p (XEXP (addr, 0), false)
14209 || legitimate_indexed_address_p (XEXP (addr, 0), false))))
14212 else if (GET_CODE (addr) == PLUS)
14214 addr_op1 = XEXP (addr, 0);
14215 addr_op2 = XEXP (addr, 1);
14216 if (!REG_P (addr_op1))
14217 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
14219 if (TARGET_DEBUG_ADDR)
14221 fprintf (stderr, "\nMove plus addr to register %s, mode = %s: ",
14222 rs6000_reg_names[REGNO (scratch)], GET_MODE_NAME (mode));
14223 debug_rtx (addr_op2);
14225 rs6000_emit_move (scratch, addr_op2, Pmode);
14226 emit_insn (gen_rtx_SET (VOIDmode,
14227 scratch_or_premodify,
14228 gen_rtx_PLUS (Pmode,
14229 addr_op1,
14230 scratch)));
14231 addr = scratch_or_premodify;
14232 scratch_or_premodify = scratch;
14235 else if (GET_CODE (addr) == SYMBOL_REF || GET_CODE (addr) == CONST
14236 || GET_CODE (addr) == CONST_INT || GET_CODE (addr) == LO_SUM
14237 || REG_P (addr))
14239 if (TARGET_DEBUG_ADDR)
14241 fprintf (stderr, "\nMove addr to register %s, mode = %s: ",
14242 rs6000_reg_names[REGNO (scratch_or_premodify)],
14243 GET_MODE_NAME (mode));
14244 debug_rtx (addr);
14247 rs6000_emit_move (scratch_or_premodify, addr, Pmode);
14248 addr = scratch_or_premodify;
14249 scratch_or_premodify = scratch;
14252 else
14253 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
14255 break;
14257 default:
14258 rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
14261 /* If the original address involved a pre-modify that we couldn't use the VSX
14262 memory instruction with update, and we haven't taken care of already,
14263 store the address in the pre-modify register and use that as the
14264 address. */
14265 if (scratch_or_premodify != scratch && scratch_or_premodify != addr)
14267 emit_insn (gen_rtx_SET (VOIDmode, scratch_or_premodify, addr));
14268 addr = scratch_or_premodify;
14271 /* If the original address involved an AND -16 and we couldn't use an ALTIVEC
14272 memory instruction, recreate the AND now, including the clobber which is
14273 generated by the general ANDSI3/ANDDI3 patterns for the
14274 andi. instruction. */
14275 if (and_op2 != NULL_RTX)
14277 if (! legitimate_indirect_address_p (addr, false))
14279 emit_insn (gen_rtx_SET (VOIDmode, scratch, addr));
14280 addr = scratch;
14283 if (TARGET_DEBUG_ADDR)
14285 fprintf (stderr, "\nAnd addr to register %s, mode = %s: ",
14286 rs6000_reg_names[REGNO (scratch)], GET_MODE_NAME (mode));
14287 debug_rtx (and_op2);
14290 and_rtx = gen_rtx_SET (VOIDmode,
14291 scratch,
14292 gen_rtx_AND (Pmode,
14293 addr,
14294 and_op2));
14296 cc_clobber = gen_rtx_CLOBBER (CCmode, gen_rtx_SCRATCH (CCmode));
14297 emit_insn (gen_rtx_PARALLEL (VOIDmode,
14298 gen_rtvec (2, and_rtx, cc_clobber)));
14299 addr = scratch;
14302 /* Adjust the address if it changed. */
14303 if (addr != XEXP (mem, 0))
14305 mem = replace_equiv_address_nv (mem, addr);
14306 if (TARGET_DEBUG_ADDR)
14307 fprintf (stderr, "\nrs6000_secondary_reload_inner, mem adjusted.\n");
14310 /* Now create the move. */
14311 if (store_p)
14312 emit_insn (gen_rtx_SET (VOIDmode, mem, reg));
14313 else
14314 emit_insn (gen_rtx_SET (VOIDmode, reg, mem));
14316 return;
14319 /* Convert reloads involving 64-bit gprs and misaligned offset
14320 addressing, or multiple 32-bit gprs and offsets that are too large,
14321 to use indirect addressing. */
14323 void
14324 rs6000_secondary_reload_gpr (rtx reg, rtx mem, rtx scratch, bool store_p)
14326 int regno = true_regnum (reg);
14327 enum reg_class rclass;
14328 rtx addr;
14329 rtx scratch_or_premodify = scratch;
14331 if (TARGET_DEBUG_ADDR)
14333 fprintf (stderr, "\nrs6000_secondary_reload_gpr, type = %s\n",
14334 store_p ? "store" : "load");
14335 fprintf (stderr, "reg:\n");
14336 debug_rtx (reg);
14337 fprintf (stderr, "mem:\n");
14338 debug_rtx (mem);
14339 fprintf (stderr, "scratch:\n");
14340 debug_rtx (scratch);
14343 gcc_assert (regno >= 0 && regno < FIRST_PSEUDO_REGISTER);
14344 gcc_assert (GET_CODE (mem) == MEM);
14345 rclass = REGNO_REG_CLASS (regno);
14346 gcc_assert (rclass == GENERAL_REGS || rclass == BASE_REGS);
14347 addr = XEXP (mem, 0);
14349 if (GET_CODE (addr) == PRE_MODIFY)
14351 scratch_or_premodify = XEXP (addr, 0);
14352 gcc_assert (REG_P (scratch_or_premodify));
14353 addr = XEXP (addr, 1);
14355 gcc_assert (GET_CODE (addr) == PLUS || GET_CODE (addr) == LO_SUM);
14357 rs6000_emit_move (scratch_or_premodify, addr, Pmode);
14359 mem = replace_equiv_address_nv (mem, scratch_or_premodify);
14361 /* Now create the move. */
14362 if (store_p)
14363 emit_insn (gen_rtx_SET (VOIDmode, mem, reg));
14364 else
14365 emit_insn (gen_rtx_SET (VOIDmode, reg, mem));
14367 return;
14370 /* Allocate a 64-bit stack slot to be used for copying SDmode values through if
14371 this function has any SDmode references. If we are on a power7 or later, we
14372 don't need the 64-bit stack slot since the LFIWZX and STIFWX instructions
14373 can load/store the value. */
14375 static void
14376 rs6000_alloc_sdmode_stack_slot (void)
14378 tree t;
14379 basic_block bb;
14380 gimple_stmt_iterator gsi;
14382 gcc_assert (cfun->machine->sdmode_stack_slot == NULL_RTX);
14384 if (TARGET_NO_SDMODE_STACK)
14385 return;
14387 FOR_EACH_BB (bb)
14388 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
14390 tree ret = walk_gimple_op (gsi_stmt (gsi), rs6000_check_sdmode, NULL);
14391 if (ret)
14393 rtx stack = assign_stack_local (DDmode, GET_MODE_SIZE (DDmode), 0);
14394 cfun->machine->sdmode_stack_slot = adjust_address_nv (stack,
14395 SDmode, 0);
14396 return;
14400 /* Check for any SDmode parameters of the function. */
14401 for (t = DECL_ARGUMENTS (cfun->decl); t; t = DECL_CHAIN (t))
14403 if (TREE_TYPE (t) == error_mark_node)
14404 continue;
14406 if (TYPE_MODE (TREE_TYPE (t)) == SDmode
14407 || TYPE_MODE (DECL_ARG_TYPE (t)) == SDmode)
14409 rtx stack = assign_stack_local (DDmode, GET_MODE_SIZE (DDmode), 0);
14410 cfun->machine->sdmode_stack_slot = adjust_address_nv (stack,
14411 SDmode, 0);
14412 return;
14417 static void
14418 rs6000_instantiate_decls (void)
14420 if (cfun->machine->sdmode_stack_slot != NULL_RTX)
14421 instantiate_decl_rtl (cfun->machine->sdmode_stack_slot);
14424 /* Given an rtx X being reloaded into a reg required to be
14425 in class CLASS, return the class of reg to actually use.
14426 In general this is just CLASS; but on some machines
14427 in some cases it is preferable to use a more restrictive class.
14429 On the RS/6000, we have to return NO_REGS when we want to reload a
14430 floating-point CONST_DOUBLE to force it to be copied to memory.
14432 We also don't want to reload integer values into floating-point
14433 registers if we can at all help it. In fact, this can
14434 cause reload to die, if it tries to generate a reload of CTR
14435 into a FP register and discovers it doesn't have the memory location
14436 required.
14438 ??? Would it be a good idea to have reload do the converse, that is
14439 try to reload floating modes into FP registers if possible?
14442 static enum reg_class
14443 rs6000_preferred_reload_class (rtx x, enum reg_class rclass)
14445 enum machine_mode mode = GET_MODE (x);
14447 if (TARGET_VSX && x == CONST0_RTX (mode) && VSX_REG_CLASS_P (rclass))
14448 return rclass;
14450 if (VECTOR_UNIT_ALTIVEC_OR_VSX_P (mode)
14451 && (rclass == ALTIVEC_REGS || rclass == VSX_REGS)
14452 && easy_vector_constant (x, mode))
14453 return ALTIVEC_REGS;
14455 if (CONSTANT_P (x) && reg_classes_intersect_p (rclass, FLOAT_REGS))
14456 return NO_REGS;
14458 if (GET_MODE_CLASS (mode) == MODE_INT && rclass == NON_SPECIAL_REGS)
14459 return GENERAL_REGS;
14461 /* For VSX, prefer the traditional registers for 64-bit values because we can
14462 use the non-VSX loads. Prefer the Altivec registers if Altivec is
14463 handling the vector operations (i.e. V16QI, V8HI, and V4SI), or if we
14464 prefer Altivec loads.. */
14465 if (rclass == VSX_REGS)
14467 if (GET_MODE_SIZE (mode) <= 8)
14468 return FLOAT_REGS;
14470 if (VECTOR_UNIT_ALTIVEC_P (mode) || VECTOR_MEM_ALTIVEC_P (mode))
14471 return ALTIVEC_REGS;
14473 return rclass;
14476 return rclass;
14479 /* Debug version of rs6000_preferred_reload_class. */
14480 static enum reg_class
14481 rs6000_debug_preferred_reload_class (rtx x, enum reg_class rclass)
14483 enum reg_class ret = rs6000_preferred_reload_class (x, rclass);
14485 fprintf (stderr,
14486 "\nrs6000_preferred_reload_class, return %s, rclass = %s, "
14487 "mode = %s, x:\n",
14488 reg_class_names[ret], reg_class_names[rclass],
14489 GET_MODE_NAME (GET_MODE (x)));
14490 debug_rtx (x);
14492 return ret;
14495 /* If we are copying between FP or AltiVec registers and anything else, we need
14496 a memory location. The exception is when we are targeting ppc64 and the
14497 move to/from fpr to gpr instructions are available. Also, under VSX, you
14498 can copy vector registers from the FP register set to the Altivec register
14499 set and vice versa. */
14501 static bool
14502 rs6000_secondary_memory_needed (enum reg_class class1,
14503 enum reg_class class2,
14504 enum machine_mode mode)
14506 if (class1 == class2)
14507 return false;
14509 /* Under VSX, there are 3 register classes that values could be in (VSX_REGS,
14510 ALTIVEC_REGS, and FLOAT_REGS). We don't need to use memory to copy
14511 between these classes. But we need memory for other things that can go in
14512 FLOAT_REGS like SFmode. */
14513 if (TARGET_VSX
14514 && (VECTOR_MEM_VSX_P (mode) || VECTOR_UNIT_VSX_P (mode))
14515 && (class1 == VSX_REGS || class1 == ALTIVEC_REGS
14516 || class1 == FLOAT_REGS))
14517 return (class2 != VSX_REGS && class2 != ALTIVEC_REGS
14518 && class2 != FLOAT_REGS);
14520 if (class1 == VSX_REGS || class2 == VSX_REGS)
14521 return true;
14523 if (class1 == FLOAT_REGS
14524 && (!TARGET_MFPGPR || !TARGET_POWERPC64
14525 || ((mode != DFmode)
14526 && (mode != DDmode)
14527 && (mode != DImode))))
14528 return true;
14530 if (class2 == FLOAT_REGS
14531 && (!TARGET_MFPGPR || !TARGET_POWERPC64
14532 || ((mode != DFmode)
14533 && (mode != DDmode)
14534 && (mode != DImode))))
14535 return true;
14537 if (class1 == ALTIVEC_REGS || class2 == ALTIVEC_REGS)
14538 return true;
14540 return false;
14543 /* Debug version of rs6000_secondary_memory_needed. */
14544 static bool
14545 rs6000_debug_secondary_memory_needed (enum reg_class class1,
14546 enum reg_class class2,
14547 enum machine_mode mode)
14549 bool ret = rs6000_secondary_memory_needed (class1, class2, mode);
14551 fprintf (stderr,
14552 "rs6000_secondary_memory_needed, return: %s, class1 = %s, "
14553 "class2 = %s, mode = %s\n",
14554 ret ? "true" : "false", reg_class_names[class1],
14555 reg_class_names[class2], GET_MODE_NAME (mode));
14557 return ret;
14560 /* Return the register class of a scratch register needed to copy IN into
14561 or out of a register in RCLASS in MODE. If it can be done directly,
14562 NO_REGS is returned. */
14564 static enum reg_class
14565 rs6000_secondary_reload_class (enum reg_class rclass, enum machine_mode mode,
14566 rtx in)
14568 int regno;
14570 if (TARGET_ELF || (DEFAULT_ABI == ABI_DARWIN
14571 #if TARGET_MACHO
14572 && MACHOPIC_INDIRECT
14573 #endif
14576 /* We cannot copy a symbolic operand directly into anything
14577 other than BASE_REGS for TARGET_ELF. So indicate that a
14578 register from BASE_REGS is needed as an intermediate
14579 register.
14581 On Darwin, pic addresses require a load from memory, which
14582 needs a base register. */
14583 if (rclass != BASE_REGS
14584 && (GET_CODE (in) == SYMBOL_REF
14585 || GET_CODE (in) == HIGH
14586 || GET_CODE (in) == LABEL_REF
14587 || GET_CODE (in) == CONST))
14588 return BASE_REGS;
14591 if (GET_CODE (in) == REG)
14593 regno = REGNO (in);
14594 if (regno >= FIRST_PSEUDO_REGISTER)
14596 regno = true_regnum (in);
14597 if (regno >= FIRST_PSEUDO_REGISTER)
14598 regno = -1;
14601 else if (GET_CODE (in) == SUBREG)
14603 regno = true_regnum (in);
14604 if (regno >= FIRST_PSEUDO_REGISTER)
14605 regno = -1;
14607 else
14608 regno = -1;
14610 /* We can place anything into GENERAL_REGS and can put GENERAL_REGS
14611 into anything. */
14612 if (rclass == GENERAL_REGS || rclass == BASE_REGS
14613 || (regno >= 0 && INT_REGNO_P (regno)))
14614 return NO_REGS;
14616 /* Constants, memory, and FP registers can go into FP registers. */
14617 if ((regno == -1 || FP_REGNO_P (regno))
14618 && (rclass == FLOAT_REGS || rclass == NON_SPECIAL_REGS))
14619 return (mode != SDmode) ? NO_REGS : GENERAL_REGS;
14621 /* Memory, and FP/altivec registers can go into fp/altivec registers under
14622 VSX. However, for scalar variables, use the traditional floating point
14623 registers so that we can use offset+register addressing. */
14624 if (TARGET_VSX
14625 && (regno == -1 || VSX_REGNO_P (regno))
14626 && VSX_REG_CLASS_P (rclass))
14628 if (GET_MODE_SIZE (mode) < 16)
14629 return FLOAT_REGS;
14631 return NO_REGS;
14634 /* Memory, and AltiVec registers can go into AltiVec registers. */
14635 if ((regno == -1 || ALTIVEC_REGNO_P (regno))
14636 && rclass == ALTIVEC_REGS)
14637 return NO_REGS;
14639 /* We can copy among the CR registers. */
14640 if ((rclass == CR_REGS || rclass == CR0_REGS)
14641 && regno >= 0 && CR_REGNO_P (regno))
14642 return NO_REGS;
14644 /* Otherwise, we need GENERAL_REGS. */
14645 return GENERAL_REGS;
14648 /* Debug version of rs6000_secondary_reload_class. */
14649 static enum reg_class
14650 rs6000_debug_secondary_reload_class (enum reg_class rclass,
14651 enum machine_mode mode, rtx in)
14653 enum reg_class ret = rs6000_secondary_reload_class (rclass, mode, in);
14654 fprintf (stderr,
14655 "\nrs6000_secondary_reload_class, return %s, rclass = %s, "
14656 "mode = %s, input rtx:\n",
14657 reg_class_names[ret], reg_class_names[rclass],
14658 GET_MODE_NAME (mode));
14659 debug_rtx (in);
14661 return ret;
14664 /* Return nonzero if for CLASS a mode change from FROM to TO is invalid. */
14666 static bool
14667 rs6000_cannot_change_mode_class (enum machine_mode from,
14668 enum machine_mode to,
14669 enum reg_class rclass)
14671 unsigned from_size = GET_MODE_SIZE (from);
14672 unsigned to_size = GET_MODE_SIZE (to);
14674 if (from_size != to_size)
14676 enum reg_class xclass = (TARGET_VSX) ? VSX_REGS : FLOAT_REGS;
14678 if (reg_classes_intersect_p (xclass, rclass))
14680 unsigned to_nregs = hard_regno_nregs[FIRST_FPR_REGNO][to];
14681 unsigned from_nregs = hard_regno_nregs[FIRST_FPR_REGNO][from];
14683 /* Don't allow 64-bit types to overlap with 128-bit types that take a
14684 single register under VSX because the scalar part of the register
14685 is in the upper 64-bits, and not the lower 64-bits. Types like
14686 TFmode/TDmode that take 2 scalar register can overlap. 128-bit
14687 IEEE floating point can't overlap, and neither can small
14688 values. */
14690 if (TARGET_IEEEQUAD && (to == TFmode || from == TFmode))
14691 return true;
14693 if (from_size < 8 || to_size < 8)
14694 return true;
14696 if (from_size == 8 && (8 * to_nregs) != to_size)
14697 return true;
14699 if (to_size == 8 && (8 * from_nregs) != from_size)
14700 return true;
14702 return false;
14704 else
14705 return false;
14708 if (TARGET_E500_DOUBLE
14709 && ((((to) == DFmode) + ((from) == DFmode)) == 1
14710 || (((to) == TFmode) + ((from) == TFmode)) == 1
14711 || (((to) == DDmode) + ((from) == DDmode)) == 1
14712 || (((to) == TDmode) + ((from) == TDmode)) == 1
14713 || (((to) == DImode) + ((from) == DImode)) == 1))
14714 return true;
14716 /* Since the VSX register set includes traditional floating point registers
14717 and altivec registers, just check for the size being different instead of
14718 trying to check whether the modes are vector modes. Otherwise it won't
14719 allow say DF and DI to change classes. For types like TFmode and TDmode
14720 that take 2 64-bit registers, rather than a single 128-bit register, don't
14721 allow subregs of those types to other 128 bit types. */
14722 if (TARGET_VSX && VSX_REG_CLASS_P (rclass))
14724 unsigned num_regs = (from_size + 15) / 16;
14725 if (hard_regno_nregs[FIRST_FPR_REGNO][to] > num_regs
14726 || hard_regno_nregs[FIRST_FPR_REGNO][from] > num_regs)
14727 return true;
14729 return (from_size != 8 && from_size != 16);
14732 if (TARGET_ALTIVEC && rclass == ALTIVEC_REGS
14733 && (ALTIVEC_VECTOR_MODE (from) + ALTIVEC_VECTOR_MODE (to)) == 1)
14734 return true;
14736 if (TARGET_SPE && (SPE_VECTOR_MODE (from) + SPE_VECTOR_MODE (to)) == 1
14737 && reg_classes_intersect_p (GENERAL_REGS, rclass))
14738 return true;
14740 return false;
14743 /* Debug version of rs6000_cannot_change_mode_class. */
14744 static bool
14745 rs6000_debug_cannot_change_mode_class (enum machine_mode from,
14746 enum machine_mode to,
14747 enum reg_class rclass)
14749 bool ret = rs6000_cannot_change_mode_class (from, to, rclass);
14751 fprintf (stderr,
14752 "rs6000_cannot_change_mode_class, return %s, from = %s, "
14753 "to = %s, rclass = %s\n",
14754 ret ? "true" : "false",
14755 GET_MODE_NAME (from), GET_MODE_NAME (to),
14756 reg_class_names[rclass]);
14758 return ret;
14761 /* Given a comparison operation, return the bit number in CCR to test. We
14762 know this is a valid comparison.
14764 SCC_P is 1 if this is for an scc. That means that %D will have been
14765 used instead of %C, so the bits will be in different places.
14767 Return -1 if OP isn't a valid comparison for some reason. */
14770 ccr_bit (rtx op, int scc_p)
14772 enum rtx_code code = GET_CODE (op);
14773 enum machine_mode cc_mode;
14774 int cc_regnum;
14775 int base_bit;
14776 rtx reg;
14778 if (!COMPARISON_P (op))
14779 return -1;
14781 reg = XEXP (op, 0);
14783 gcc_assert (GET_CODE (reg) == REG && CR_REGNO_P (REGNO (reg)));
14785 cc_mode = GET_MODE (reg);
14786 cc_regnum = REGNO (reg);
14787 base_bit = 4 * (cc_regnum - CR0_REGNO);
14789 validate_condition_mode (code, cc_mode);
14791 /* When generating a sCOND operation, only positive conditions are
14792 allowed. */
14793 gcc_assert (!scc_p
14794 || code == EQ || code == GT || code == LT || code == UNORDERED
14795 || code == GTU || code == LTU);
14797 switch (code)
14799 case NE:
14800 return scc_p ? base_bit + 3 : base_bit + 2;
14801 case EQ:
14802 return base_bit + 2;
14803 case GT: case GTU: case UNLE:
14804 return base_bit + 1;
14805 case LT: case LTU: case UNGE:
14806 return base_bit;
14807 case ORDERED: case UNORDERED:
14808 return base_bit + 3;
14810 case GE: case GEU:
14811 /* If scc, we will have done a cror to put the bit in the
14812 unordered position. So test that bit. For integer, this is ! LT
14813 unless this is an scc insn. */
14814 return scc_p ? base_bit + 3 : base_bit;
14816 case LE: case LEU:
14817 return scc_p ? base_bit + 3 : base_bit + 1;
14819 default:
14820 gcc_unreachable ();
14824 /* Return the GOT register. */
14827 rs6000_got_register (rtx value ATTRIBUTE_UNUSED)
14829 /* The second flow pass currently (June 1999) can't update
14830 regs_ever_live without disturbing other parts of the compiler, so
14831 update it here to make the prolog/epilogue code happy. */
14832 if (!can_create_pseudo_p ()
14833 && !df_regs_ever_live_p (RS6000_PIC_OFFSET_TABLE_REGNUM))
14834 df_set_regs_ever_live (RS6000_PIC_OFFSET_TABLE_REGNUM, true);
14836 crtl->uses_pic_offset_table = 1;
14838 return pic_offset_table_rtx;
14841 static rs6000_stack_t stack_info;
14843 /* Function to init struct machine_function.
14844 This will be called, via a pointer variable,
14845 from push_function_context. */
14847 static struct machine_function *
14848 rs6000_init_machine_status (void)
14850 stack_info.reload_completed = 0;
14851 return ggc_alloc_cleared_machine_function ();
14854 /* These macros test for integers and extract the low-order bits. */
14855 #define INT_P(X) \
14856 (GET_CODE (X) == CONST_INT && GET_MODE (X) == VOIDmode)
14858 #define INT_LOWPART(X) INTVAL (X)
14861 extract_MB (rtx op)
14863 int i;
14864 unsigned long val = INT_LOWPART (op);
14866 /* If the high bit is zero, the value is the first 1 bit we find
14867 from the left. */
14868 if ((val & 0x80000000) == 0)
14870 gcc_assert (val & 0xffffffff);
14872 i = 1;
14873 while (((val <<= 1) & 0x80000000) == 0)
14874 ++i;
14875 return i;
14878 /* If the high bit is set and the low bit is not, or the mask is all
14879 1's, the value is zero. */
14880 if ((val & 1) == 0 || (val & 0xffffffff) == 0xffffffff)
14881 return 0;
14883 /* Otherwise we have a wrap-around mask. Look for the first 0 bit
14884 from the right. */
14885 i = 31;
14886 while (((val >>= 1) & 1) != 0)
14887 --i;
14889 return i;
14893 extract_ME (rtx op)
14895 int i;
14896 unsigned long val = INT_LOWPART (op);
14898 /* If the low bit is zero, the value is the first 1 bit we find from
14899 the right. */
14900 if ((val & 1) == 0)
14902 gcc_assert (val & 0xffffffff);
14904 i = 30;
14905 while (((val >>= 1) & 1) == 0)
14906 --i;
14908 return i;
14911 /* If the low bit is set and the high bit is not, or the mask is all
14912 1's, the value is 31. */
14913 if ((val & 0x80000000) == 0 || (val & 0xffffffff) == 0xffffffff)
14914 return 31;
14916 /* Otherwise we have a wrap-around mask. Look for the first 0 bit
14917 from the left. */
14918 i = 0;
14919 while (((val <<= 1) & 0x80000000) != 0)
14920 ++i;
14922 return i;
14925 /* Locate some local-dynamic symbol still in use by this function
14926 so that we can print its name in some tls_ld pattern. */
14928 static const char *
14929 rs6000_get_some_local_dynamic_name (void)
14931 rtx insn;
14933 if (cfun->machine->some_ld_name)
14934 return cfun->machine->some_ld_name;
14936 for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
14937 if (INSN_P (insn)
14938 && for_each_rtx (&PATTERN (insn),
14939 rs6000_get_some_local_dynamic_name_1, 0))
14940 return cfun->machine->some_ld_name;
14942 gcc_unreachable ();
14945 /* Helper function for rs6000_get_some_local_dynamic_name. */
14947 static int
14948 rs6000_get_some_local_dynamic_name_1 (rtx *px, void *data ATTRIBUTE_UNUSED)
14950 rtx x = *px;
14952 if (GET_CODE (x) == SYMBOL_REF)
14954 const char *str = XSTR (x, 0);
14955 if (SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_DYNAMIC)
14957 cfun->machine->some_ld_name = str;
14958 return 1;
14962 return 0;
14965 /* Write out a function code label. */
14967 void
14968 rs6000_output_function_entry (FILE *file, const char *fname)
14970 if (fname[0] != '.')
14972 switch (DEFAULT_ABI)
14974 default:
14975 gcc_unreachable ();
14977 case ABI_AIX:
14978 if (DOT_SYMBOLS)
14979 putc ('.', file);
14980 else
14981 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "L.");
14982 break;
14984 case ABI_V4:
14985 case ABI_DARWIN:
14986 break;
14990 RS6000_OUTPUT_BASENAME (file, fname);
14993 /* Print an operand. Recognize special options, documented below. */
14995 #if TARGET_ELF
14996 #define SMALL_DATA_RELOC ((rs6000_sdata == SDATA_EABI) ? "sda21" : "sdarel")
14997 #define SMALL_DATA_REG ((rs6000_sdata == SDATA_EABI) ? 0 : 13)
14998 #else
14999 #define SMALL_DATA_RELOC "sda21"
15000 #define SMALL_DATA_REG 0
15001 #endif
15003 void
15004 print_operand (FILE *file, rtx x, int code)
15006 int i;
15007 unsigned HOST_WIDE_INT uval;
15009 switch (code)
15011 /* %a is output_address. */
15013 case 'b':
15014 /* If constant, low-order 16 bits of constant, unsigned.
15015 Otherwise, write normally. */
15016 if (INT_P (x))
15017 fprintf (file, HOST_WIDE_INT_PRINT_DEC, INT_LOWPART (x) & 0xffff);
15018 else
15019 print_operand (file, x, 0);
15020 return;
15022 case 'B':
15023 /* If the low-order bit is zero, write 'r'; otherwise, write 'l'
15024 for 64-bit mask direction. */
15025 putc (((INT_LOWPART (x) & 1) == 0 ? 'r' : 'l'), file);
15026 return;
15028 /* %c is output_addr_const if a CONSTANT_ADDRESS_P, otherwise
15029 output_operand. */
15031 case 'D':
15032 /* Like 'J' but get to the GT bit only. */
15033 gcc_assert (REG_P (x));
15035 /* Bit 1 is GT bit. */
15036 i = 4 * (REGNO (x) - CR0_REGNO) + 1;
15038 /* Add one for shift count in rlinm for scc. */
15039 fprintf (file, "%d", i + 1);
15040 return;
15042 case 'E':
15043 /* X is a CR register. Print the number of the EQ bit of the CR */
15044 if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
15045 output_operand_lossage ("invalid %%E value");
15046 else
15047 fprintf (file, "%d", 4 * (REGNO (x) - CR0_REGNO) + 2);
15048 return;
15050 case 'f':
15051 /* X is a CR register. Print the shift count needed to move it
15052 to the high-order four bits. */
15053 if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
15054 output_operand_lossage ("invalid %%f value");
15055 else
15056 fprintf (file, "%d", 4 * (REGNO (x) - CR0_REGNO));
15057 return;
15059 case 'F':
15060 /* Similar, but print the count for the rotate in the opposite
15061 direction. */
15062 if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
15063 output_operand_lossage ("invalid %%F value");
15064 else
15065 fprintf (file, "%d", 32 - 4 * (REGNO (x) - CR0_REGNO));
15066 return;
15068 case 'G':
15069 /* X is a constant integer. If it is negative, print "m",
15070 otherwise print "z". This is to make an aze or ame insn. */
15071 if (GET_CODE (x) != CONST_INT)
15072 output_operand_lossage ("invalid %%G value");
15073 else if (INTVAL (x) >= 0)
15074 putc ('z', file);
15075 else
15076 putc ('m', file);
15077 return;
15079 case 'h':
15080 /* If constant, output low-order five bits. Otherwise, write
15081 normally. */
15082 if (INT_P (x))
15083 fprintf (file, HOST_WIDE_INT_PRINT_DEC, INT_LOWPART (x) & 31);
15084 else
15085 print_operand (file, x, 0);
15086 return;
15088 case 'H':
15089 /* If constant, output low-order six bits. Otherwise, write
15090 normally. */
15091 if (INT_P (x))
15092 fprintf (file, HOST_WIDE_INT_PRINT_DEC, INT_LOWPART (x) & 63);
15093 else
15094 print_operand (file, x, 0);
15095 return;
15097 case 'I':
15098 /* Print `i' if this is a constant, else nothing. */
15099 if (INT_P (x))
15100 putc ('i', file);
15101 return;
15103 case 'j':
15104 /* Write the bit number in CCR for jump. */
15105 i = ccr_bit (x, 0);
15106 if (i == -1)
15107 output_operand_lossage ("invalid %%j code");
15108 else
15109 fprintf (file, "%d", i);
15110 return;
15112 case 'J':
15113 /* Similar, but add one for shift count in rlinm for scc and pass
15114 scc flag to `ccr_bit'. */
15115 i = ccr_bit (x, 1);
15116 if (i == -1)
15117 output_operand_lossage ("invalid %%J code");
15118 else
15119 /* If we want bit 31, write a shift count of zero, not 32. */
15120 fprintf (file, "%d", i == 31 ? 0 : i + 1);
15121 return;
15123 case 'k':
15124 /* X must be a constant. Write the 1's complement of the
15125 constant. */
15126 if (! INT_P (x))
15127 output_operand_lossage ("invalid %%k value");
15128 else
15129 fprintf (file, HOST_WIDE_INT_PRINT_DEC, ~ INT_LOWPART (x));
15130 return;
15132 case 'K':
15133 /* X must be a symbolic constant on ELF. Write an
15134 expression suitable for an 'addi' that adds in the low 16
15135 bits of the MEM. */
15136 if (GET_CODE (x) == CONST)
15138 if (GET_CODE (XEXP (x, 0)) != PLUS
15139 || (GET_CODE (XEXP (XEXP (x, 0), 0)) != SYMBOL_REF
15140 && GET_CODE (XEXP (XEXP (x, 0), 0)) != LABEL_REF)
15141 || GET_CODE (XEXP (XEXP (x, 0), 1)) != CONST_INT)
15142 output_operand_lossage ("invalid %%K value");
15144 print_operand_address (file, x);
15145 fputs ("@l", file);
15146 return;
15148 /* %l is output_asm_label. */
15150 case 'L':
15151 /* Write second word of DImode or DFmode reference. Works on register
15152 or non-indexed memory only. */
15153 if (REG_P (x))
15154 fputs (reg_names[REGNO (x) + 1], file);
15155 else if (MEM_P (x))
15157 /* Handle possible auto-increment. Since it is pre-increment and
15158 we have already done it, we can just use an offset of word. */
15159 if (GET_CODE (XEXP (x, 0)) == PRE_INC
15160 || GET_CODE (XEXP (x, 0)) == PRE_DEC)
15161 output_address (plus_constant (Pmode, XEXP (XEXP (x, 0), 0),
15162 UNITS_PER_WORD));
15163 else if (GET_CODE (XEXP (x, 0)) == PRE_MODIFY)
15164 output_address (plus_constant (Pmode, XEXP (XEXP (x, 0), 0),
15165 UNITS_PER_WORD));
15166 else
15167 output_address (XEXP (adjust_address_nv (x, SImode,
15168 UNITS_PER_WORD),
15169 0));
15171 if (small_data_operand (x, GET_MODE (x)))
15172 fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
15173 reg_names[SMALL_DATA_REG]);
15175 return;
15177 case 'm':
15178 /* MB value for a mask operand. */
15179 if (! mask_operand (x, SImode))
15180 output_operand_lossage ("invalid %%m value");
15182 fprintf (file, "%d", extract_MB (x));
15183 return;
15185 case 'M':
15186 /* ME value for a mask operand. */
15187 if (! mask_operand (x, SImode))
15188 output_operand_lossage ("invalid %%M value");
15190 fprintf (file, "%d", extract_ME (x));
15191 return;
15193 /* %n outputs the negative of its operand. */
15195 case 'N':
15196 /* Write the number of elements in the vector times 4. */
15197 if (GET_CODE (x) != PARALLEL)
15198 output_operand_lossage ("invalid %%N value");
15199 else
15200 fprintf (file, "%d", XVECLEN (x, 0) * 4);
15201 return;
15203 case 'O':
15204 /* Similar, but subtract 1 first. */
15205 if (GET_CODE (x) != PARALLEL)
15206 output_operand_lossage ("invalid %%O value");
15207 else
15208 fprintf (file, "%d", (XVECLEN (x, 0) - 1) * 4);
15209 return;
15211 case 'p':
15212 /* X is a CONST_INT that is a power of two. Output the logarithm. */
15213 if (! INT_P (x)
15214 || INT_LOWPART (x) < 0
15215 || (i = exact_log2 (INT_LOWPART (x))) < 0)
15216 output_operand_lossage ("invalid %%p value");
15217 else
15218 fprintf (file, "%d", i);
15219 return;
15221 case 'P':
15222 /* The operand must be an indirect memory reference. The result
15223 is the register name. */
15224 if (GET_CODE (x) != MEM || GET_CODE (XEXP (x, 0)) != REG
15225 || REGNO (XEXP (x, 0)) >= 32)
15226 output_operand_lossage ("invalid %%P value");
15227 else
15228 fputs (reg_names[REGNO (XEXP (x, 0))], file);
15229 return;
15231 case 'q':
15232 /* This outputs the logical code corresponding to a boolean
15233 expression. The expression may have one or both operands
15234 negated (if one, only the first one). For condition register
15235 logical operations, it will also treat the negated
15236 CR codes as NOTs, but not handle NOTs of them. */
15238 const char *const *t = 0;
15239 const char *s;
15240 enum rtx_code code = GET_CODE (x);
15241 static const char * const tbl[3][3] = {
15242 { "and", "andc", "nor" },
15243 { "or", "orc", "nand" },
15244 { "xor", "eqv", "xor" } };
15246 if (code == AND)
15247 t = tbl[0];
15248 else if (code == IOR)
15249 t = tbl[1];
15250 else if (code == XOR)
15251 t = tbl[2];
15252 else
15253 output_operand_lossage ("invalid %%q value");
15255 if (GET_CODE (XEXP (x, 0)) != NOT)
15256 s = t[0];
15257 else
15259 if (GET_CODE (XEXP (x, 1)) == NOT)
15260 s = t[2];
15261 else
15262 s = t[1];
15265 fputs (s, file);
15267 return;
15269 case 'Q':
15270 if (! TARGET_MFCRF)
15271 return;
15272 fputc (',', file);
15273 /* FALLTHRU */
15275 case 'R':
15276 /* X is a CR register. Print the mask for `mtcrf'. */
15277 if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
15278 output_operand_lossage ("invalid %%R value");
15279 else
15280 fprintf (file, "%d", 128 >> (REGNO (x) - CR0_REGNO));
15281 return;
15283 case 's':
15284 /* Low 5 bits of 32 - value */
15285 if (! INT_P (x))
15286 output_operand_lossage ("invalid %%s value");
15287 else
15288 fprintf (file, HOST_WIDE_INT_PRINT_DEC, (32 - INT_LOWPART (x)) & 31);
15289 return;
15291 case 'S':
15292 /* PowerPC64 mask position. All 0's is excluded.
15293 CONST_INT 32-bit mask is considered sign-extended so any
15294 transition must occur within the CONST_INT, not on the boundary. */
15295 if (! mask64_operand (x, DImode))
15296 output_operand_lossage ("invalid %%S value");
15298 uval = INT_LOWPART (x);
15300 if (uval & 1) /* Clear Left */
15302 #if HOST_BITS_PER_WIDE_INT > 64
15303 uval &= ((unsigned HOST_WIDE_INT) 1 << 64) - 1;
15304 #endif
15305 i = 64;
15307 else /* Clear Right */
15309 uval = ~uval;
15310 #if HOST_BITS_PER_WIDE_INT > 64
15311 uval &= ((unsigned HOST_WIDE_INT) 1 << 64) - 1;
15312 #endif
15313 i = 63;
15315 while (uval != 0)
15316 --i, uval >>= 1;
15317 gcc_assert (i >= 0);
15318 fprintf (file, "%d", i);
15319 return;
15321 case 't':
15322 /* Like 'J' but get to the OVERFLOW/UNORDERED bit. */
15323 gcc_assert (REG_P (x) && GET_MODE (x) == CCmode);
15325 /* Bit 3 is OV bit. */
15326 i = 4 * (REGNO (x) - CR0_REGNO) + 3;
15328 /* If we want bit 31, write a shift count of zero, not 32. */
15329 fprintf (file, "%d", i == 31 ? 0 : i + 1);
15330 return;
15332 case 'T':
15333 /* Print the symbolic name of a branch target register. */
15334 if (GET_CODE (x) != REG || (REGNO (x) != LR_REGNO
15335 && REGNO (x) != CTR_REGNO))
15336 output_operand_lossage ("invalid %%T value");
15337 else if (REGNO (x) == LR_REGNO)
15338 fputs ("lr", file);
15339 else
15340 fputs ("ctr", file);
15341 return;
15343 case 'u':
15344 /* High-order 16 bits of constant for use in unsigned operand. */
15345 if (! INT_P (x))
15346 output_operand_lossage ("invalid %%u value");
15347 else
15348 fprintf (file, HOST_WIDE_INT_PRINT_HEX,
15349 (INT_LOWPART (x) >> 16) & 0xffff);
15350 return;
15352 case 'v':
15353 /* High-order 16 bits of constant for use in signed operand. */
15354 if (! INT_P (x))
15355 output_operand_lossage ("invalid %%v value");
15356 else
15357 fprintf (file, HOST_WIDE_INT_PRINT_HEX,
15358 (INT_LOWPART (x) >> 16) & 0xffff);
15359 return;
15361 case 'U':
15362 /* Print `u' if this has an auto-increment or auto-decrement. */
15363 if (MEM_P (x)
15364 && (GET_CODE (XEXP (x, 0)) == PRE_INC
15365 || GET_CODE (XEXP (x, 0)) == PRE_DEC
15366 || GET_CODE (XEXP (x, 0)) == PRE_MODIFY))
15367 putc ('u', file);
15368 return;
15370 case 'V':
15371 /* Print the trap code for this operand. */
15372 switch (GET_CODE (x))
15374 case EQ:
15375 fputs ("eq", file); /* 4 */
15376 break;
15377 case NE:
15378 fputs ("ne", file); /* 24 */
15379 break;
15380 case LT:
15381 fputs ("lt", file); /* 16 */
15382 break;
15383 case LE:
15384 fputs ("le", file); /* 20 */
15385 break;
15386 case GT:
15387 fputs ("gt", file); /* 8 */
15388 break;
15389 case GE:
15390 fputs ("ge", file); /* 12 */
15391 break;
15392 case LTU:
15393 fputs ("llt", file); /* 2 */
15394 break;
15395 case LEU:
15396 fputs ("lle", file); /* 6 */
15397 break;
15398 case GTU:
15399 fputs ("lgt", file); /* 1 */
15400 break;
15401 case GEU:
15402 fputs ("lge", file); /* 5 */
15403 break;
15404 default:
15405 gcc_unreachable ();
15407 break;
15409 case 'w':
15410 /* If constant, low-order 16 bits of constant, signed. Otherwise, write
15411 normally. */
15412 if (INT_P (x))
15413 fprintf (file, HOST_WIDE_INT_PRINT_DEC,
15414 ((INT_LOWPART (x) & 0xffff) ^ 0x8000) - 0x8000);
15415 else
15416 print_operand (file, x, 0);
15417 return;
15419 case 'W':
15420 /* MB value for a PowerPC64 rldic operand. */
15421 i = clz_hwi (INTVAL (x));
15423 fprintf (file, "%d", i);
15424 return;
15426 case 'x':
15427 /* X is a FPR or Altivec register used in a VSX context. */
15428 if (GET_CODE (x) != REG || !VSX_REGNO_P (REGNO (x)))
15429 output_operand_lossage ("invalid %%x value");
15430 else
15432 int reg = REGNO (x);
15433 int vsx_reg = (FP_REGNO_P (reg)
15434 ? reg - 32
15435 : reg - FIRST_ALTIVEC_REGNO + 32);
15437 #ifdef TARGET_REGNAMES
15438 if (TARGET_REGNAMES)
15439 fprintf (file, "%%vs%d", vsx_reg);
15440 else
15441 #endif
15442 fprintf (file, "%d", vsx_reg);
15444 return;
15446 case 'X':
15447 if (MEM_P (x)
15448 && (legitimate_indexed_address_p (XEXP (x, 0), 0)
15449 || (GET_CODE (XEXP (x, 0)) == PRE_MODIFY
15450 && legitimate_indexed_address_p (XEXP (XEXP (x, 0), 1), 0))))
15451 putc ('x', file);
15452 return;
15454 case 'Y':
15455 /* Like 'L', for third word of TImode/PTImode */
15456 if (REG_P (x))
15457 fputs (reg_names[REGNO (x) + 2], file);
15458 else if (MEM_P (x))
15460 if (GET_CODE (XEXP (x, 0)) == PRE_INC
15461 || GET_CODE (XEXP (x, 0)) == PRE_DEC)
15462 output_address (plus_constant (Pmode, XEXP (XEXP (x, 0), 0), 8));
15463 else if (GET_CODE (XEXP (x, 0)) == PRE_MODIFY)
15464 output_address (plus_constant (Pmode, XEXP (XEXP (x, 0), 0), 8));
15465 else
15466 output_address (XEXP (adjust_address_nv (x, SImode, 8), 0));
15467 if (small_data_operand (x, GET_MODE (x)))
15468 fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
15469 reg_names[SMALL_DATA_REG]);
15471 return;
15473 case 'z':
15474 /* X is a SYMBOL_REF. Write out the name preceded by a
15475 period and without any trailing data in brackets. Used for function
15476 names. If we are configured for System V (or the embedded ABI) on
15477 the PowerPC, do not emit the period, since those systems do not use
15478 TOCs and the like. */
15479 gcc_assert (GET_CODE (x) == SYMBOL_REF);
15481 /* Mark the decl as referenced so that cgraph will output the
15482 function. */
15483 if (SYMBOL_REF_DECL (x))
15484 mark_decl_referenced (SYMBOL_REF_DECL (x));
15486 /* For macho, check to see if we need a stub. */
15487 if (TARGET_MACHO)
15489 const char *name = XSTR (x, 0);
15490 #if TARGET_MACHO
15491 if (darwin_emit_branch_islands
15492 && MACHOPIC_INDIRECT
15493 && machopic_classify_symbol (x) == MACHOPIC_UNDEFINED_FUNCTION)
15494 name = machopic_indirection_name (x, /*stub_p=*/true);
15495 #endif
15496 assemble_name (file, name);
15498 else if (!DOT_SYMBOLS)
15499 assemble_name (file, XSTR (x, 0));
15500 else
15501 rs6000_output_function_entry (file, XSTR (x, 0));
15502 return;
15504 case 'Z':
15505 /* Like 'L', for last word of TImode/PTImode. */
15506 if (REG_P (x))
15507 fputs (reg_names[REGNO (x) + 3], file);
15508 else if (MEM_P (x))
15510 if (GET_CODE (XEXP (x, 0)) == PRE_INC
15511 || GET_CODE (XEXP (x, 0)) == PRE_DEC)
15512 output_address (plus_constant (Pmode, XEXP (XEXP (x, 0), 0), 12));
15513 else if (GET_CODE (XEXP (x, 0)) == PRE_MODIFY)
15514 output_address (plus_constant (Pmode, XEXP (XEXP (x, 0), 0), 12));
15515 else
15516 output_address (XEXP (adjust_address_nv (x, SImode, 12), 0));
15517 if (small_data_operand (x, GET_MODE (x)))
15518 fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
15519 reg_names[SMALL_DATA_REG]);
15521 return;
15523 /* Print AltiVec or SPE memory operand. */
15524 case 'y':
15526 rtx tmp;
15528 gcc_assert (MEM_P (x));
15530 tmp = XEXP (x, 0);
15532 /* Ugly hack because %y is overloaded. */
15533 if ((TARGET_SPE || TARGET_E500_DOUBLE)
15534 && (GET_MODE_SIZE (GET_MODE (x)) == 8
15535 || GET_MODE (x) == TFmode
15536 || GET_MODE (x) == TImode
15537 || GET_MODE (x) == PTImode))
15539 /* Handle [reg]. */
15540 if (REG_P (tmp))
15542 fprintf (file, "0(%s)", reg_names[REGNO (tmp)]);
15543 break;
15545 /* Handle [reg+UIMM]. */
15546 else if (GET_CODE (tmp) == PLUS &&
15547 GET_CODE (XEXP (tmp, 1)) == CONST_INT)
15549 int x;
15551 gcc_assert (REG_P (XEXP (tmp, 0)));
15553 x = INTVAL (XEXP (tmp, 1));
15554 fprintf (file, "%d(%s)", x, reg_names[REGNO (XEXP (tmp, 0))]);
15555 break;
15558 /* Fall through. Must be [reg+reg]. */
15560 if (VECTOR_MEM_ALTIVEC_P (GET_MODE (x))
15561 && GET_CODE (tmp) == AND
15562 && GET_CODE (XEXP (tmp, 1)) == CONST_INT
15563 && INTVAL (XEXP (tmp, 1)) == -16)
15564 tmp = XEXP (tmp, 0);
15565 else if (VECTOR_MEM_VSX_P (GET_MODE (x))
15566 && GET_CODE (tmp) == PRE_MODIFY)
15567 tmp = XEXP (tmp, 1);
15568 if (REG_P (tmp))
15569 fprintf (file, "0,%s", reg_names[REGNO (tmp)]);
15570 else
15572 if (!GET_CODE (tmp) == PLUS
15573 || !REG_P (XEXP (tmp, 0))
15574 || !REG_P (XEXP (tmp, 1)))
15576 output_operand_lossage ("invalid %%y value, try using the 'Z' constraint");
15577 break;
15580 if (REGNO (XEXP (tmp, 0)) == 0)
15581 fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (tmp, 1)) ],
15582 reg_names[ REGNO (XEXP (tmp, 0)) ]);
15583 else
15584 fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (tmp, 0)) ],
15585 reg_names[ REGNO (XEXP (tmp, 1)) ]);
15587 break;
15590 case 0:
15591 if (REG_P (x))
15592 fprintf (file, "%s", reg_names[REGNO (x)]);
15593 else if (MEM_P (x))
15595 /* We need to handle PRE_INC and PRE_DEC here, since we need to
15596 know the width from the mode. */
15597 if (GET_CODE (XEXP (x, 0)) == PRE_INC)
15598 fprintf (file, "%d(%s)", GET_MODE_SIZE (GET_MODE (x)),
15599 reg_names[REGNO (XEXP (XEXP (x, 0), 0))]);
15600 else if (GET_CODE (XEXP (x, 0)) == PRE_DEC)
15601 fprintf (file, "%d(%s)", - GET_MODE_SIZE (GET_MODE (x)),
15602 reg_names[REGNO (XEXP (XEXP (x, 0), 0))]);
15603 else if (GET_CODE (XEXP (x, 0)) == PRE_MODIFY)
15604 output_address (XEXP (XEXP (x, 0), 1));
15605 else
15606 output_address (XEXP (x, 0));
15608 else
15610 if (toc_relative_expr_p (x, false))
15611 /* This hack along with a corresponding hack in
15612 rs6000_output_addr_const_extra arranges to output addends
15613 where the assembler expects to find them. eg.
15614 (plus (unspec [(symbol_ref ("x")) (reg 2)] tocrel) 4)
15615 without this hack would be output as "x@toc+4". We
15616 want "x+4@toc". */
15617 output_addr_const (file, CONST_CAST_RTX (tocrel_base));
15618 else
15619 output_addr_const (file, x);
15621 return;
15623 case '&':
15624 assemble_name (file, rs6000_get_some_local_dynamic_name ());
15625 return;
15627 default:
15628 output_operand_lossage ("invalid %%xn code");
15632 /* Print the address of an operand. */
15634 void
15635 print_operand_address (FILE *file, rtx x)
15637 if (REG_P (x))
15638 fprintf (file, "0(%s)", reg_names[ REGNO (x) ]);
15639 else if (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == CONST
15640 || GET_CODE (x) == LABEL_REF)
15642 output_addr_const (file, x);
15643 if (small_data_operand (x, GET_MODE (x)))
15644 fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
15645 reg_names[SMALL_DATA_REG]);
15646 else
15647 gcc_assert (!TARGET_TOC);
15649 else if (GET_CODE (x) == PLUS && REG_P (XEXP (x, 0))
15650 && REG_P (XEXP (x, 1)))
15652 if (REGNO (XEXP (x, 0)) == 0)
15653 fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (x, 1)) ],
15654 reg_names[ REGNO (XEXP (x, 0)) ]);
15655 else
15656 fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (x, 0)) ],
15657 reg_names[ REGNO (XEXP (x, 1)) ]);
15659 else if (GET_CODE (x) == PLUS && REG_P (XEXP (x, 0))
15660 && GET_CODE (XEXP (x, 1)) == CONST_INT)
15661 fprintf (file, HOST_WIDE_INT_PRINT_DEC "(%s)",
15662 INTVAL (XEXP (x, 1)), reg_names[ REGNO (XEXP (x, 0)) ]);
15663 #if TARGET_MACHO
15664 else if (GET_CODE (x) == LO_SUM && REG_P (XEXP (x, 0))
15665 && CONSTANT_P (XEXP (x, 1)))
15667 fprintf (file, "lo16(");
15668 output_addr_const (file, XEXP (x, 1));
15669 fprintf (file, ")(%s)", reg_names[ REGNO (XEXP (x, 0)) ]);
15671 #endif
15672 #if TARGET_ELF
15673 else if (GET_CODE (x) == LO_SUM && REG_P (XEXP (x, 0))
15674 && CONSTANT_P (XEXP (x, 1)))
15676 output_addr_const (file, XEXP (x, 1));
15677 fprintf (file, "@l(%s)", reg_names[ REGNO (XEXP (x, 0)) ]);
15679 #endif
15680 else if (toc_relative_expr_p (x, false))
15682 /* This hack along with a corresponding hack in
15683 rs6000_output_addr_const_extra arranges to output addends
15684 where the assembler expects to find them. eg.
15685 (lo_sum (reg 9)
15686 . (plus (unspec [(symbol_ref ("x")) (reg 2)] tocrel) 8))
15687 without this hack would be output as "x@toc+8@l(9)". We
15688 want "x+8@toc@l(9)". */
15689 output_addr_const (file, CONST_CAST_RTX (tocrel_base));
15690 if (GET_CODE (x) == LO_SUM)
15691 fprintf (file, "@l(%s)", reg_names[REGNO (XEXP (x, 0))]);
15692 else
15693 fprintf (file, "(%s)", reg_names[REGNO (XVECEXP (tocrel_base, 0, 1))]);
15695 else
15696 gcc_unreachable ();
15699 /* Implement TARGET_OUTPUT_ADDR_CONST_EXTRA. */
15701 static bool
15702 rs6000_output_addr_const_extra (FILE *file, rtx x)
15704 if (GET_CODE (x) == UNSPEC)
15705 switch (XINT (x, 1))
15707 case UNSPEC_TOCREL:
15708 gcc_checking_assert (GET_CODE (XVECEXP (x, 0, 0)) == SYMBOL_REF
15709 && REG_P (XVECEXP (x, 0, 1))
15710 && REGNO (XVECEXP (x, 0, 1)) == TOC_REGISTER);
15711 output_addr_const (file, XVECEXP (x, 0, 0));
15712 if (x == tocrel_base && tocrel_offset != const0_rtx)
15714 if (INTVAL (tocrel_offset) >= 0)
15715 fprintf (file, "+");
15716 output_addr_const (file, CONST_CAST_RTX (tocrel_offset));
15718 if (!TARGET_AIX || (TARGET_ELF && TARGET_MINIMAL_TOC))
15720 putc ('-', file);
15721 assemble_name (file, toc_label_name);
15723 else if (TARGET_ELF)
15724 fputs ("@toc", file);
15725 return true;
15727 #if TARGET_MACHO
15728 case UNSPEC_MACHOPIC_OFFSET:
15729 output_addr_const (file, XVECEXP (x, 0, 0));
15730 putc ('-', file);
15731 machopic_output_function_base_name (file);
15732 return true;
15733 #endif
15735 return false;
15738 /* Target hook for assembling integer objects. The PowerPC version has
15739 to handle fixup entries for relocatable code if RELOCATABLE_NEEDS_FIXUP
15740 is defined. It also needs to handle DI-mode objects on 64-bit
15741 targets. */
15743 static bool
15744 rs6000_assemble_integer (rtx x, unsigned int size, int aligned_p)
15746 #ifdef RELOCATABLE_NEEDS_FIXUP
15747 /* Special handling for SI values. */
15748 if (RELOCATABLE_NEEDS_FIXUP && size == 4 && aligned_p)
15750 static int recurse = 0;
15752 /* For -mrelocatable, we mark all addresses that need to be fixed up in
15753 the .fixup section. Since the TOC section is already relocated, we
15754 don't need to mark it here. We used to skip the text section, but it
15755 should never be valid for relocated addresses to be placed in the text
15756 section. */
15757 if (TARGET_RELOCATABLE
15758 && in_section != toc_section
15759 && !recurse
15760 && GET_CODE (x) != CONST_INT
15761 && GET_CODE (x) != CONST_DOUBLE
15762 && CONSTANT_P (x))
15764 char buf[256];
15766 recurse = 1;
15767 ASM_GENERATE_INTERNAL_LABEL (buf, "LCP", fixuplabelno);
15768 fixuplabelno++;
15769 ASM_OUTPUT_LABEL (asm_out_file, buf);
15770 fprintf (asm_out_file, "\t.long\t(");
15771 output_addr_const (asm_out_file, x);
15772 fprintf (asm_out_file, ")@fixup\n");
15773 fprintf (asm_out_file, "\t.section\t\".fixup\",\"aw\"\n");
15774 ASM_OUTPUT_ALIGN (asm_out_file, 2);
15775 fprintf (asm_out_file, "\t.long\t");
15776 assemble_name (asm_out_file, buf);
15777 fprintf (asm_out_file, "\n\t.previous\n");
15778 recurse = 0;
15779 return true;
15781 /* Remove initial .'s to turn a -mcall-aixdesc function
15782 address into the address of the descriptor, not the function
15783 itself. */
15784 else if (GET_CODE (x) == SYMBOL_REF
15785 && XSTR (x, 0)[0] == '.'
15786 && DEFAULT_ABI == ABI_AIX)
15788 const char *name = XSTR (x, 0);
15789 while (*name == '.')
15790 name++;
15792 fprintf (asm_out_file, "\t.long\t%s\n", name);
15793 return true;
15796 #endif /* RELOCATABLE_NEEDS_FIXUP */
15797 return default_assemble_integer (x, size, aligned_p);
15800 #if defined (HAVE_GAS_HIDDEN) && !TARGET_MACHO
15801 /* Emit an assembler directive to set symbol visibility for DECL to
15802 VISIBILITY_TYPE. */
15804 static void
15805 rs6000_assemble_visibility (tree decl, int vis)
15807 if (TARGET_XCOFF)
15808 return;
15810 /* Functions need to have their entry point symbol visibility set as
15811 well as their descriptor symbol visibility. */
15812 if (DEFAULT_ABI == ABI_AIX
15813 && DOT_SYMBOLS
15814 && TREE_CODE (decl) == FUNCTION_DECL)
15816 static const char * const visibility_types[] = {
15817 NULL, "internal", "hidden", "protected"
15820 const char *name, *type;
15822 name = ((* targetm.strip_name_encoding)
15823 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl))));
15824 type = visibility_types[vis];
15826 fprintf (asm_out_file, "\t.%s\t%s\n", type, name);
15827 fprintf (asm_out_file, "\t.%s\t.%s\n", type, name);
15829 else
15830 default_assemble_visibility (decl, vis);
15832 #endif
15834 enum rtx_code
15835 rs6000_reverse_condition (enum machine_mode mode, enum rtx_code code)
15837 /* Reversal of FP compares takes care -- an ordered compare
15838 becomes an unordered compare and vice versa. */
15839 if (mode == CCFPmode
15840 && (!flag_finite_math_only
15841 || code == UNLT || code == UNLE || code == UNGT || code == UNGE
15842 || code == UNEQ || code == LTGT))
15843 return reverse_condition_maybe_unordered (code);
15844 else
15845 return reverse_condition (code);
15848 /* Generate a compare for CODE. Return a brand-new rtx that
15849 represents the result of the compare. */
15851 static rtx
15852 rs6000_generate_compare (rtx cmp, enum machine_mode mode)
15854 enum machine_mode comp_mode;
15855 rtx compare_result;
15856 enum rtx_code code = GET_CODE (cmp);
15857 rtx op0 = XEXP (cmp, 0);
15858 rtx op1 = XEXP (cmp, 1);
15860 if (FLOAT_MODE_P (mode))
15861 comp_mode = CCFPmode;
15862 else if (code == GTU || code == LTU
15863 || code == GEU || code == LEU)
15864 comp_mode = CCUNSmode;
15865 else if ((code == EQ || code == NE)
15866 && unsigned_reg_p (op0)
15867 && (unsigned_reg_p (op1)
15868 || (CONST_INT_P (op1) && INTVAL (op1) != 0)))
15869 /* These are unsigned values, perhaps there will be a later
15870 ordering compare that can be shared with this one. */
15871 comp_mode = CCUNSmode;
15872 else
15873 comp_mode = CCmode;
15875 /* If we have an unsigned compare, make sure we don't have a signed value as
15876 an immediate. */
15877 if (comp_mode == CCUNSmode && GET_CODE (op1) == CONST_INT
15878 && INTVAL (op1) < 0)
15880 op0 = copy_rtx_if_shared (op0);
15881 op1 = force_reg (GET_MODE (op0), op1);
15882 cmp = gen_rtx_fmt_ee (code, GET_MODE (cmp), op0, op1);
15885 /* First, the compare. */
15886 compare_result = gen_reg_rtx (comp_mode);
15888 /* E500 FP compare instructions on the GPRs. Yuck! */
15889 if ((!TARGET_FPRS && TARGET_HARD_FLOAT)
15890 && FLOAT_MODE_P (mode))
15892 rtx cmp, or_result, compare_result2;
15893 enum machine_mode op_mode = GET_MODE (op0);
15895 if (op_mode == VOIDmode)
15896 op_mode = GET_MODE (op1);
15898 /* The E500 FP compare instructions toggle the GT bit (CR bit 1) only.
15899 This explains the following mess. */
15901 switch (code)
15903 case EQ: case UNEQ: case NE: case LTGT:
15904 switch (op_mode)
15906 case SFmode:
15907 cmp = (flag_finite_math_only && !flag_trapping_math)
15908 ? gen_tstsfeq_gpr (compare_result, op0, op1)
15909 : gen_cmpsfeq_gpr (compare_result, op0, op1);
15910 break;
15912 case DFmode:
15913 cmp = (flag_finite_math_only && !flag_trapping_math)
15914 ? gen_tstdfeq_gpr (compare_result, op0, op1)
15915 : gen_cmpdfeq_gpr (compare_result, op0, op1);
15916 break;
15918 case TFmode:
15919 cmp = (flag_finite_math_only && !flag_trapping_math)
15920 ? gen_tsttfeq_gpr (compare_result, op0, op1)
15921 : gen_cmptfeq_gpr (compare_result, op0, op1);
15922 break;
15924 default:
15925 gcc_unreachable ();
15927 break;
15929 case GT: case GTU: case UNGT: case UNGE: case GE: case GEU:
15930 switch (op_mode)
15932 case SFmode:
15933 cmp = (flag_finite_math_only && !flag_trapping_math)
15934 ? gen_tstsfgt_gpr (compare_result, op0, op1)
15935 : gen_cmpsfgt_gpr (compare_result, op0, op1);
15936 break;
15938 case DFmode:
15939 cmp = (flag_finite_math_only && !flag_trapping_math)
15940 ? gen_tstdfgt_gpr (compare_result, op0, op1)
15941 : gen_cmpdfgt_gpr (compare_result, op0, op1);
15942 break;
15944 case TFmode:
15945 cmp = (flag_finite_math_only && !flag_trapping_math)
15946 ? gen_tsttfgt_gpr (compare_result, op0, op1)
15947 : gen_cmptfgt_gpr (compare_result, op0, op1);
15948 break;
15950 default:
15951 gcc_unreachable ();
15953 break;
15955 case LT: case LTU: case UNLT: case UNLE: case LE: case LEU:
15956 switch (op_mode)
15958 case SFmode:
15959 cmp = (flag_finite_math_only && !flag_trapping_math)
15960 ? gen_tstsflt_gpr (compare_result, op0, op1)
15961 : gen_cmpsflt_gpr (compare_result, op0, op1);
15962 break;
15964 case DFmode:
15965 cmp = (flag_finite_math_only && !flag_trapping_math)
15966 ? gen_tstdflt_gpr (compare_result, op0, op1)
15967 : gen_cmpdflt_gpr (compare_result, op0, op1);
15968 break;
15970 case TFmode:
15971 cmp = (flag_finite_math_only && !flag_trapping_math)
15972 ? gen_tsttflt_gpr (compare_result, op0, op1)
15973 : gen_cmptflt_gpr (compare_result, op0, op1);
15974 break;
15976 default:
15977 gcc_unreachable ();
15979 break;
15980 default:
15981 gcc_unreachable ();
15984 /* Synthesize LE and GE from LT/GT || EQ. */
15985 if (code == LE || code == GE || code == LEU || code == GEU)
15987 emit_insn (cmp);
15989 switch (code)
15991 case LE: code = LT; break;
15992 case GE: code = GT; break;
15993 case LEU: code = LT; break;
15994 case GEU: code = GT; break;
15995 default: gcc_unreachable ();
15998 compare_result2 = gen_reg_rtx (CCFPmode);
16000 /* Do the EQ. */
16001 switch (op_mode)
16003 case SFmode:
16004 cmp = (flag_finite_math_only && !flag_trapping_math)
16005 ? gen_tstsfeq_gpr (compare_result2, op0, op1)
16006 : gen_cmpsfeq_gpr (compare_result2, op0, op1);
16007 break;
16009 case DFmode:
16010 cmp = (flag_finite_math_only && !flag_trapping_math)
16011 ? gen_tstdfeq_gpr (compare_result2, op0, op1)
16012 : gen_cmpdfeq_gpr (compare_result2, op0, op1);
16013 break;
16015 case TFmode:
16016 cmp = (flag_finite_math_only && !flag_trapping_math)
16017 ? gen_tsttfeq_gpr (compare_result2, op0, op1)
16018 : gen_cmptfeq_gpr (compare_result2, op0, op1);
16019 break;
16021 default:
16022 gcc_unreachable ();
16024 emit_insn (cmp);
16026 /* OR them together. */
16027 or_result = gen_reg_rtx (CCFPmode);
16028 cmp = gen_e500_cr_ior_compare (or_result, compare_result,
16029 compare_result2);
16030 compare_result = or_result;
16031 code = EQ;
16033 else
16035 if (code == NE || code == LTGT)
16036 code = NE;
16037 else
16038 code = EQ;
16041 emit_insn (cmp);
16043 else
16045 /* Generate XLC-compatible TFmode compare as PARALLEL with extra
16046 CLOBBERs to match cmptf_internal2 pattern. */
16047 if (comp_mode == CCFPmode && TARGET_XL_COMPAT
16048 && GET_MODE (op0) == TFmode
16049 && !TARGET_IEEEQUAD
16050 && TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_LONG_DOUBLE_128)
16051 emit_insn (gen_rtx_PARALLEL (VOIDmode,
16052 gen_rtvec (10,
16053 gen_rtx_SET (VOIDmode,
16054 compare_result,
16055 gen_rtx_COMPARE (comp_mode, op0, op1)),
16056 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
16057 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
16058 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
16059 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
16060 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
16061 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
16062 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
16063 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
16064 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (Pmode)))));
16065 else if (GET_CODE (op1) == UNSPEC
16066 && XINT (op1, 1) == UNSPEC_SP_TEST)
16068 rtx op1b = XVECEXP (op1, 0, 0);
16069 comp_mode = CCEQmode;
16070 compare_result = gen_reg_rtx (CCEQmode);
16071 if (TARGET_64BIT)
16072 emit_insn (gen_stack_protect_testdi (compare_result, op0, op1b));
16073 else
16074 emit_insn (gen_stack_protect_testsi (compare_result, op0, op1b));
16076 else
16077 emit_insn (gen_rtx_SET (VOIDmode, compare_result,
16078 gen_rtx_COMPARE (comp_mode, op0, op1)));
16081 /* Some kinds of FP comparisons need an OR operation;
16082 under flag_finite_math_only we don't bother. */
16083 if (FLOAT_MODE_P (mode)
16084 && !flag_finite_math_only
16085 && !(TARGET_HARD_FLOAT && !TARGET_FPRS)
16086 && (code == LE || code == GE
16087 || code == UNEQ || code == LTGT
16088 || code == UNGT || code == UNLT))
16090 enum rtx_code or1, or2;
16091 rtx or1_rtx, or2_rtx, compare2_rtx;
16092 rtx or_result = gen_reg_rtx (CCEQmode);
16094 switch (code)
16096 case LE: or1 = LT; or2 = EQ; break;
16097 case GE: or1 = GT; or2 = EQ; break;
16098 case UNEQ: or1 = UNORDERED; or2 = EQ; break;
16099 case LTGT: or1 = LT; or2 = GT; break;
16100 case UNGT: or1 = UNORDERED; or2 = GT; break;
16101 case UNLT: or1 = UNORDERED; or2 = LT; break;
16102 default: gcc_unreachable ();
16104 validate_condition_mode (or1, comp_mode);
16105 validate_condition_mode (or2, comp_mode);
16106 or1_rtx = gen_rtx_fmt_ee (or1, SImode, compare_result, const0_rtx);
16107 or2_rtx = gen_rtx_fmt_ee (or2, SImode, compare_result, const0_rtx);
16108 compare2_rtx = gen_rtx_COMPARE (CCEQmode,
16109 gen_rtx_IOR (SImode, or1_rtx, or2_rtx),
16110 const_true_rtx);
16111 emit_insn (gen_rtx_SET (VOIDmode, or_result, compare2_rtx));
16113 compare_result = or_result;
16114 code = EQ;
16117 validate_condition_mode (code, GET_MODE (compare_result));
16119 return gen_rtx_fmt_ee (code, VOIDmode, compare_result, const0_rtx);
16123 /* Emit the RTL for an sISEL pattern. */
16125 void
16126 rs6000_emit_sISEL (enum machine_mode mode ATTRIBUTE_UNUSED, rtx operands[])
16128 rs6000_emit_int_cmove (operands[0], operands[1], const1_rtx, const0_rtx);
16131 void
16132 rs6000_emit_sCOND (enum machine_mode mode, rtx operands[])
16134 rtx condition_rtx;
16135 enum machine_mode op_mode;
16136 enum rtx_code cond_code;
16137 rtx result = operands[0];
16139 if (TARGET_ISEL && (mode == SImode || mode == DImode))
16141 rs6000_emit_sISEL (mode, operands);
16142 return;
16145 condition_rtx = rs6000_generate_compare (operands[1], mode);
16146 cond_code = GET_CODE (condition_rtx);
16148 if (FLOAT_MODE_P (mode)
16149 && !TARGET_FPRS && TARGET_HARD_FLOAT)
16151 rtx t;
16153 PUT_MODE (condition_rtx, SImode);
16154 t = XEXP (condition_rtx, 0);
16156 gcc_assert (cond_code == NE || cond_code == EQ);
16158 if (cond_code == NE)
16159 emit_insn (gen_e500_flip_gt_bit (t, t));
16161 emit_insn (gen_move_from_CR_gt_bit (result, t));
16162 return;
16165 if (cond_code == NE
16166 || cond_code == GE || cond_code == LE
16167 || cond_code == GEU || cond_code == LEU
16168 || cond_code == ORDERED || cond_code == UNGE || cond_code == UNLE)
16170 rtx not_result = gen_reg_rtx (CCEQmode);
16171 rtx not_op, rev_cond_rtx;
16172 enum machine_mode cc_mode;
16174 cc_mode = GET_MODE (XEXP (condition_rtx, 0));
16176 rev_cond_rtx = gen_rtx_fmt_ee (rs6000_reverse_condition (cc_mode, cond_code),
16177 SImode, XEXP (condition_rtx, 0), const0_rtx);
16178 not_op = gen_rtx_COMPARE (CCEQmode, rev_cond_rtx, const0_rtx);
16179 emit_insn (gen_rtx_SET (VOIDmode, not_result, not_op));
16180 condition_rtx = gen_rtx_EQ (VOIDmode, not_result, const0_rtx);
16183 op_mode = GET_MODE (XEXP (operands[1], 0));
16184 if (op_mode == VOIDmode)
16185 op_mode = GET_MODE (XEXP (operands[1], 1));
16187 if (TARGET_POWERPC64 && (op_mode == DImode || FLOAT_MODE_P (mode)))
16189 PUT_MODE (condition_rtx, DImode);
16190 convert_move (result, condition_rtx, 0);
16192 else
16194 PUT_MODE (condition_rtx, SImode);
16195 emit_insn (gen_rtx_SET (VOIDmode, result, condition_rtx));
16199 /* Emit a branch of kind CODE to location LOC. */
16201 void
16202 rs6000_emit_cbranch (enum machine_mode mode, rtx operands[])
16204 rtx condition_rtx, loc_ref;
16206 condition_rtx = rs6000_generate_compare (operands[0], mode);
16207 loc_ref = gen_rtx_LABEL_REF (VOIDmode, operands[3]);
16208 emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx,
16209 gen_rtx_IF_THEN_ELSE (VOIDmode, condition_rtx,
16210 loc_ref, pc_rtx)));
16213 /* Return the string to output a conditional branch to LABEL, which is
16214 the operand template of the label, or NULL if the branch is really a
16215 conditional return.
16217 OP is the conditional expression. XEXP (OP, 0) is assumed to be a
16218 condition code register and its mode specifies what kind of
16219 comparison we made.
16221 REVERSED is nonzero if we should reverse the sense of the comparison.
16223 INSN is the insn. */
16225 char *
16226 output_cbranch (rtx op, const char *label, int reversed, rtx insn)
16228 static char string[64];
16229 enum rtx_code code = GET_CODE (op);
16230 rtx cc_reg = XEXP (op, 0);
16231 enum machine_mode mode = GET_MODE (cc_reg);
16232 int cc_regno = REGNO (cc_reg) - CR0_REGNO;
16233 int need_longbranch = label != NULL && get_attr_length (insn) == 8;
16234 int really_reversed = reversed ^ need_longbranch;
16235 char *s = string;
16236 const char *ccode;
16237 const char *pred;
16238 rtx note;
16240 validate_condition_mode (code, mode);
16242 /* Work out which way this really branches. We could use
16243 reverse_condition_maybe_unordered here always but this
16244 makes the resulting assembler clearer. */
16245 if (really_reversed)
16247 /* Reversal of FP compares takes care -- an ordered compare
16248 becomes an unordered compare and vice versa. */
16249 if (mode == CCFPmode)
16250 code = reverse_condition_maybe_unordered (code);
16251 else
16252 code = reverse_condition (code);
16255 if ((!TARGET_FPRS && TARGET_HARD_FLOAT) && mode == CCFPmode)
16257 /* The efscmp/tst* instructions twiddle bit 2, which maps nicely
16258 to the GT bit. */
16259 switch (code)
16261 case EQ:
16262 /* Opposite of GT. */
16263 code = GT;
16264 break;
16266 case NE:
16267 code = UNLE;
16268 break;
16270 default:
16271 gcc_unreachable ();
16275 switch (code)
16277 /* Not all of these are actually distinct opcodes, but
16278 we distinguish them for clarity of the resulting assembler. */
16279 case NE: case LTGT:
16280 ccode = "ne"; break;
16281 case EQ: case UNEQ:
16282 ccode = "eq"; break;
16283 case GE: case GEU:
16284 ccode = "ge"; break;
16285 case GT: case GTU: case UNGT:
16286 ccode = "gt"; break;
16287 case LE: case LEU:
16288 ccode = "le"; break;
16289 case LT: case LTU: case UNLT:
16290 ccode = "lt"; break;
16291 case UNORDERED: ccode = "un"; break;
16292 case ORDERED: ccode = "nu"; break;
16293 case UNGE: ccode = "nl"; break;
16294 case UNLE: ccode = "ng"; break;
16295 default:
16296 gcc_unreachable ();
16299 /* Maybe we have a guess as to how likely the branch is. */
16300 pred = "";
16301 note = find_reg_note (insn, REG_BR_PROB, NULL_RTX);
16302 if (note != NULL_RTX)
16304 /* PROB is the difference from 50%. */
16305 int prob = INTVAL (XEXP (note, 0)) - REG_BR_PROB_BASE / 2;
16307 /* Only hint for highly probable/improbable branches on newer
16308 cpus as static prediction overrides processor dynamic
16309 prediction. For older cpus we may as well always hint, but
16310 assume not taken for branches that are very close to 50% as a
16311 mispredicted taken branch is more expensive than a
16312 mispredicted not-taken branch. */
16313 if (rs6000_always_hint
16314 || (abs (prob) > REG_BR_PROB_BASE / 100 * 48
16315 && br_prob_note_reliable_p (note)))
16317 if (abs (prob) > REG_BR_PROB_BASE / 20
16318 && ((prob > 0) ^ need_longbranch))
16319 pred = "+";
16320 else
16321 pred = "-";
16325 if (label == NULL)
16326 s += sprintf (s, "b%slr%s ", ccode, pred);
16327 else
16328 s += sprintf (s, "b%s%s ", ccode, pred);
16330 /* We need to escape any '%' characters in the reg_names string.
16331 Assume they'd only be the first character.... */
16332 if (reg_names[cc_regno + CR0_REGNO][0] == '%')
16333 *s++ = '%';
16334 s += sprintf (s, "%s", reg_names[cc_regno + CR0_REGNO]);
16336 if (label != NULL)
16338 /* If the branch distance was too far, we may have to use an
16339 unconditional branch to go the distance. */
16340 if (need_longbranch)
16341 s += sprintf (s, ",$+8\n\tb %s", label);
16342 else
16343 s += sprintf (s, ",%s", label);
16346 return string;
16349 /* Return the string to flip the GT bit on a CR. */
16350 char *
16351 output_e500_flip_gt_bit (rtx dst, rtx src)
16353 static char string[64];
16354 int a, b;
16356 gcc_assert (GET_CODE (dst) == REG && CR_REGNO_P (REGNO (dst))
16357 && GET_CODE (src) == REG && CR_REGNO_P (REGNO (src)));
16359 /* GT bit. */
16360 a = 4 * (REGNO (dst) - CR0_REGNO) + 1;
16361 b = 4 * (REGNO (src) - CR0_REGNO) + 1;
16363 sprintf (string, "crnot %d,%d", a, b);
16364 return string;
16367 /* Return insn for VSX or Altivec comparisons. */
16369 static rtx
16370 rs6000_emit_vector_compare_inner (enum rtx_code code, rtx op0, rtx op1)
16372 rtx mask;
16373 enum machine_mode mode = GET_MODE (op0);
16375 switch (code)
16377 default:
16378 break;
16380 case GE:
16381 if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
16382 return NULL_RTX;
16384 case EQ:
16385 case GT:
16386 case GTU:
16387 case ORDERED:
16388 case UNORDERED:
16389 case UNEQ:
16390 case LTGT:
16391 mask = gen_reg_rtx (mode);
16392 emit_insn (gen_rtx_SET (VOIDmode,
16393 mask,
16394 gen_rtx_fmt_ee (code, mode, op0, op1)));
16395 return mask;
16398 return NULL_RTX;
16401 /* Emit vector compare for operands OP0 and OP1 using code RCODE.
16402 DMODE is expected destination mode. This is a recursive function. */
16404 static rtx
16405 rs6000_emit_vector_compare (enum rtx_code rcode,
16406 rtx op0, rtx op1,
16407 enum machine_mode dmode)
16409 rtx mask;
16410 bool swap_operands = false;
16411 bool try_again = false;
16413 gcc_assert (VECTOR_UNIT_ALTIVEC_OR_VSX_P (dmode));
16414 gcc_assert (GET_MODE (op0) == GET_MODE (op1));
16416 /* See if the comparison works as is. */
16417 mask = rs6000_emit_vector_compare_inner (rcode, op0, op1);
16418 if (mask)
16419 return mask;
16421 switch (rcode)
16423 case LT:
16424 rcode = GT;
16425 swap_operands = true;
16426 try_again = true;
16427 break;
16428 case LTU:
16429 rcode = GTU;
16430 swap_operands = true;
16431 try_again = true;
16432 break;
16433 case NE:
16434 case UNLE:
16435 case UNLT:
16436 case UNGE:
16437 case UNGT:
16438 /* Invert condition and try again.
16439 e.g., A != B becomes ~(A==B). */
16441 enum rtx_code rev_code;
16442 enum insn_code nor_code;
16443 rtx mask2;
16445 rev_code = reverse_condition_maybe_unordered (rcode);
16446 if (rev_code == UNKNOWN)
16447 return NULL_RTX;
16449 nor_code = optab_handler (one_cmpl_optab, dmode);
16450 if (nor_code == CODE_FOR_nothing)
16451 return NULL_RTX;
16453 mask2 = rs6000_emit_vector_compare (rev_code, op0, op1, dmode);
16454 if (!mask2)
16455 return NULL_RTX;
16457 mask = gen_reg_rtx (dmode);
16458 emit_insn (GEN_FCN (nor_code) (mask, mask2));
16459 return mask;
16461 break;
16462 case GE:
16463 case GEU:
16464 case LE:
16465 case LEU:
16466 /* Try GT/GTU/LT/LTU OR EQ */
16468 rtx c_rtx, eq_rtx;
16469 enum insn_code ior_code;
16470 enum rtx_code new_code;
16472 switch (rcode)
16474 case GE:
16475 new_code = GT;
16476 break;
16478 case GEU:
16479 new_code = GTU;
16480 break;
16482 case LE:
16483 new_code = LT;
16484 break;
16486 case LEU:
16487 new_code = LTU;
16488 break;
16490 default:
16491 gcc_unreachable ();
16494 ior_code = optab_handler (ior_optab, dmode);
16495 if (ior_code == CODE_FOR_nothing)
16496 return NULL_RTX;
16498 c_rtx = rs6000_emit_vector_compare (new_code, op0, op1, dmode);
16499 if (!c_rtx)
16500 return NULL_RTX;
16502 eq_rtx = rs6000_emit_vector_compare (EQ, op0, op1, dmode);
16503 if (!eq_rtx)
16504 return NULL_RTX;
16506 mask = gen_reg_rtx (dmode);
16507 emit_insn (GEN_FCN (ior_code) (mask, c_rtx, eq_rtx));
16508 return mask;
16510 break;
16511 default:
16512 return NULL_RTX;
16515 if (try_again)
16517 if (swap_operands)
16519 rtx tmp;
16520 tmp = op0;
16521 op0 = op1;
16522 op1 = tmp;
16525 mask = rs6000_emit_vector_compare_inner (rcode, op0, op1);
16526 if (mask)
16527 return mask;
16530 /* You only get two chances. */
16531 return NULL_RTX;
16534 /* Emit vector conditional expression. DEST is destination. OP_TRUE and
16535 OP_FALSE are two VEC_COND_EXPR operands. CC_OP0 and CC_OP1 are the two
16536 operands for the relation operation COND. */
16539 rs6000_emit_vector_cond_expr (rtx dest, rtx op_true, rtx op_false,
16540 rtx cond, rtx cc_op0, rtx cc_op1)
16542 enum machine_mode dest_mode = GET_MODE (dest);
16543 enum machine_mode mask_mode = GET_MODE (cc_op0);
16544 enum rtx_code rcode = GET_CODE (cond);
16545 enum machine_mode cc_mode = CCmode;
16546 rtx mask;
16547 rtx cond2;
16548 rtx tmp;
16549 bool invert_move = false;
16551 if (VECTOR_UNIT_NONE_P (dest_mode))
16552 return 0;
16554 gcc_assert (GET_MODE_SIZE (dest_mode) == GET_MODE_SIZE (mask_mode)
16555 && GET_MODE_NUNITS (dest_mode) == GET_MODE_NUNITS (mask_mode));
16557 switch (rcode)
16559 /* Swap operands if we can, and fall back to doing the operation as
16560 specified, and doing a NOR to invert the test. */
16561 case NE:
16562 case UNLE:
16563 case UNLT:
16564 case UNGE:
16565 case UNGT:
16566 /* Invert condition and try again.
16567 e.g., A = (B != C) ? D : E becomes A = (B == C) ? E : D. */
16568 invert_move = true;
16569 rcode = reverse_condition_maybe_unordered (rcode);
16570 if (rcode == UNKNOWN)
16571 return 0;
16572 break;
16574 /* Mark unsigned tests with CCUNSmode. */
16575 case GTU:
16576 case GEU:
16577 case LTU:
16578 case LEU:
16579 cc_mode = CCUNSmode;
16580 break;
16582 default:
16583 break;
16586 /* Get the vector mask for the given relational operations. */
16587 mask = rs6000_emit_vector_compare (rcode, cc_op0, cc_op1, mask_mode);
16589 if (!mask)
16590 return 0;
16592 if (invert_move)
16594 tmp = op_true;
16595 op_true = op_false;
16596 op_false = tmp;
16599 cond2 = gen_rtx_fmt_ee (NE, cc_mode, gen_lowpart (dest_mode, mask),
16600 CONST0_RTX (dest_mode));
16601 emit_insn (gen_rtx_SET (VOIDmode,
16602 dest,
16603 gen_rtx_IF_THEN_ELSE (dest_mode,
16604 cond2,
16605 op_true,
16606 op_false)));
16607 return 1;
16610 /* Emit a conditional move: move TRUE_COND to DEST if OP of the
16611 operands of the last comparison is nonzero/true, FALSE_COND if it
16612 is zero/false. Return 0 if the hardware has no such operation. */
16615 rs6000_emit_cmove (rtx dest, rtx op, rtx true_cond, rtx false_cond)
16617 enum rtx_code code = GET_CODE (op);
16618 rtx op0 = XEXP (op, 0);
16619 rtx op1 = XEXP (op, 1);
16620 REAL_VALUE_TYPE c1;
16621 enum machine_mode compare_mode = GET_MODE (op0);
16622 enum machine_mode result_mode = GET_MODE (dest);
16623 rtx temp;
16624 bool is_against_zero;
16626 /* These modes should always match. */
16627 if (GET_MODE (op1) != compare_mode
16628 /* In the isel case however, we can use a compare immediate, so
16629 op1 may be a small constant. */
16630 && (!TARGET_ISEL || !short_cint_operand (op1, VOIDmode)))
16631 return 0;
16632 if (GET_MODE (true_cond) != result_mode)
16633 return 0;
16634 if (GET_MODE (false_cond) != result_mode)
16635 return 0;
16637 /* Don't allow using floating point comparisons for integer results for
16638 now. */
16639 if (FLOAT_MODE_P (compare_mode) && !FLOAT_MODE_P (result_mode))
16640 return 0;
16642 /* First, work out if the hardware can do this at all, or
16643 if it's too slow.... */
16644 if (!FLOAT_MODE_P (compare_mode))
16646 if (TARGET_ISEL)
16647 return rs6000_emit_int_cmove (dest, op, true_cond, false_cond);
16648 return 0;
16650 else if (TARGET_HARD_FLOAT && !TARGET_FPRS
16651 && SCALAR_FLOAT_MODE_P (compare_mode))
16652 return 0;
16654 is_against_zero = op1 == CONST0_RTX (compare_mode);
16656 /* A floating-point subtract might overflow, underflow, or produce
16657 an inexact result, thus changing the floating-point flags, so it
16658 can't be generated if we care about that. It's safe if one side
16659 of the construct is zero, since then no subtract will be
16660 generated. */
16661 if (SCALAR_FLOAT_MODE_P (compare_mode)
16662 && flag_trapping_math && ! is_against_zero)
16663 return 0;
16665 /* Eliminate half of the comparisons by switching operands, this
16666 makes the remaining code simpler. */
16667 if (code == UNLT || code == UNGT || code == UNORDERED || code == NE
16668 || code == LTGT || code == LT || code == UNLE)
16670 code = reverse_condition_maybe_unordered (code);
16671 temp = true_cond;
16672 true_cond = false_cond;
16673 false_cond = temp;
16676 /* UNEQ and LTGT take four instructions for a comparison with zero,
16677 it'll probably be faster to use a branch here too. */
16678 if (code == UNEQ && HONOR_NANS (compare_mode))
16679 return 0;
16681 if (GET_CODE (op1) == CONST_DOUBLE)
16682 REAL_VALUE_FROM_CONST_DOUBLE (c1, op1);
16684 /* We're going to try to implement comparisons by performing
16685 a subtract, then comparing against zero. Unfortunately,
16686 Inf - Inf is NaN which is not zero, and so if we don't
16687 know that the operand is finite and the comparison
16688 would treat EQ different to UNORDERED, we can't do it. */
16689 if (HONOR_INFINITIES (compare_mode)
16690 && code != GT && code != UNGE
16691 && (GET_CODE (op1) != CONST_DOUBLE || real_isinf (&c1))
16692 /* Constructs of the form (a OP b ? a : b) are safe. */
16693 && ((! rtx_equal_p (op0, false_cond) && ! rtx_equal_p (op1, false_cond))
16694 || (! rtx_equal_p (op0, true_cond)
16695 && ! rtx_equal_p (op1, true_cond))))
16696 return 0;
16698 /* At this point we know we can use fsel. */
16700 /* Reduce the comparison to a comparison against zero. */
16701 if (! is_against_zero)
16703 temp = gen_reg_rtx (compare_mode);
16704 emit_insn (gen_rtx_SET (VOIDmode, temp,
16705 gen_rtx_MINUS (compare_mode, op0, op1)));
16706 op0 = temp;
16707 op1 = CONST0_RTX (compare_mode);
16710 /* If we don't care about NaNs we can reduce some of the comparisons
16711 down to faster ones. */
16712 if (! HONOR_NANS (compare_mode))
16713 switch (code)
16715 case GT:
16716 code = LE;
16717 temp = true_cond;
16718 true_cond = false_cond;
16719 false_cond = temp;
16720 break;
16721 case UNGE:
16722 code = GE;
16723 break;
16724 case UNEQ:
16725 code = EQ;
16726 break;
16727 default:
16728 break;
16731 /* Now, reduce everything down to a GE. */
16732 switch (code)
16734 case GE:
16735 break;
16737 case LE:
16738 temp = gen_reg_rtx (compare_mode);
16739 emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_NEG (compare_mode, op0)));
16740 op0 = temp;
16741 break;
16743 case ORDERED:
16744 temp = gen_reg_rtx (compare_mode);
16745 emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_ABS (compare_mode, op0)));
16746 op0 = temp;
16747 break;
16749 case EQ:
16750 temp = gen_reg_rtx (compare_mode);
16751 emit_insn (gen_rtx_SET (VOIDmode, temp,
16752 gen_rtx_NEG (compare_mode,
16753 gen_rtx_ABS (compare_mode, op0))));
16754 op0 = temp;
16755 break;
16757 case UNGE:
16758 /* a UNGE 0 <-> (a GE 0 || -a UNLT 0) */
16759 temp = gen_reg_rtx (result_mode);
16760 emit_insn (gen_rtx_SET (VOIDmode, temp,
16761 gen_rtx_IF_THEN_ELSE (result_mode,
16762 gen_rtx_GE (VOIDmode,
16763 op0, op1),
16764 true_cond, false_cond)));
16765 false_cond = true_cond;
16766 true_cond = temp;
16768 temp = gen_reg_rtx (compare_mode);
16769 emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_NEG (compare_mode, op0)));
16770 op0 = temp;
16771 break;
16773 case GT:
16774 /* a GT 0 <-> (a GE 0 && -a UNLT 0) */
16775 temp = gen_reg_rtx (result_mode);
16776 emit_insn (gen_rtx_SET (VOIDmode, temp,
16777 gen_rtx_IF_THEN_ELSE (result_mode,
16778 gen_rtx_GE (VOIDmode,
16779 op0, op1),
16780 true_cond, false_cond)));
16781 true_cond = false_cond;
16782 false_cond = temp;
16784 temp = gen_reg_rtx (compare_mode);
16785 emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_NEG (compare_mode, op0)));
16786 op0 = temp;
16787 break;
16789 default:
16790 gcc_unreachable ();
16793 emit_insn (gen_rtx_SET (VOIDmode, dest,
16794 gen_rtx_IF_THEN_ELSE (result_mode,
16795 gen_rtx_GE (VOIDmode,
16796 op0, op1),
16797 true_cond, false_cond)));
16798 return 1;
16801 /* Same as above, but for ints (isel). */
16803 static int
16804 rs6000_emit_int_cmove (rtx dest, rtx op, rtx true_cond, rtx false_cond)
16806 rtx condition_rtx, cr;
16807 enum machine_mode mode = GET_MODE (dest);
16808 enum rtx_code cond_code;
16809 rtx (*isel_func) (rtx, rtx, rtx, rtx, rtx);
16810 bool signedp;
16812 if (mode != SImode && (!TARGET_POWERPC64 || mode != DImode))
16813 return 0;
16815 /* We still have to do the compare, because isel doesn't do a
16816 compare, it just looks at the CRx bits set by a previous compare
16817 instruction. */
16818 condition_rtx = rs6000_generate_compare (op, mode);
16819 cond_code = GET_CODE (condition_rtx);
16820 cr = XEXP (condition_rtx, 0);
16821 signedp = GET_MODE (cr) == CCmode;
16823 isel_func = (mode == SImode
16824 ? (signedp ? gen_isel_signed_si : gen_isel_unsigned_si)
16825 : (signedp ? gen_isel_signed_di : gen_isel_unsigned_di));
16827 switch (cond_code)
16829 case LT: case GT: case LTU: case GTU: case EQ:
16830 /* isel handles these directly. */
16831 break;
16833 default:
16834 /* We need to swap the sense of the comparison. */
16836 rtx t = true_cond;
16837 true_cond = false_cond;
16838 false_cond = t;
16839 PUT_CODE (condition_rtx, reverse_condition (cond_code));
16841 break;
16844 false_cond = force_reg (mode, false_cond);
16845 if (true_cond != const0_rtx)
16846 true_cond = force_reg (mode, true_cond);
16848 emit_insn (isel_func (dest, condition_rtx, true_cond, false_cond, cr));
16850 return 1;
16853 const char *
16854 output_isel (rtx *operands)
16856 enum rtx_code code;
16858 code = GET_CODE (operands[1]);
16860 if (code == GE || code == GEU || code == LE || code == LEU || code == NE)
16862 gcc_assert (GET_CODE (operands[2]) == REG
16863 && GET_CODE (operands[3]) == REG);
16864 PUT_CODE (operands[1], reverse_condition (code));
16865 return "isel %0,%3,%2,%j1";
16868 return "isel %0,%2,%3,%j1";
16871 void
16872 rs6000_emit_minmax (rtx dest, enum rtx_code code, rtx op0, rtx op1)
16874 enum machine_mode mode = GET_MODE (op0);
16875 enum rtx_code c;
16876 rtx target;
16878 /* VSX/altivec have direct min/max insns. */
16879 if ((code == SMAX || code == SMIN)
16880 && (VECTOR_UNIT_ALTIVEC_OR_VSX_P (mode)
16881 || (mode == SFmode && VECTOR_UNIT_VSX_P (DFmode))))
16883 emit_insn (gen_rtx_SET (VOIDmode,
16884 dest,
16885 gen_rtx_fmt_ee (code, mode, op0, op1)));
16886 return;
16889 if (code == SMAX || code == SMIN)
16890 c = GE;
16891 else
16892 c = GEU;
16894 if (code == SMAX || code == UMAX)
16895 target = emit_conditional_move (dest, c, op0, op1, mode,
16896 op0, op1, mode, 0);
16897 else
16898 target = emit_conditional_move (dest, c, op0, op1, mode,
16899 op1, op0, mode, 0);
16900 gcc_assert (target);
16901 if (target != dest)
16902 emit_move_insn (dest, target);
16905 /* A subroutine of the atomic operation splitters. Jump to LABEL if
16906 COND is true. Mark the jump as unlikely to be taken. */
16908 static void
16909 emit_unlikely_jump (rtx cond, rtx label)
16911 rtx very_unlikely = GEN_INT (REG_BR_PROB_BASE / 100 - 1);
16912 rtx x;
16914 x = gen_rtx_IF_THEN_ELSE (VOIDmode, cond, label, pc_rtx);
16915 x = emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, x));
16916 add_reg_note (x, REG_BR_PROB, very_unlikely);
16919 /* A subroutine of the atomic operation splitters. Emit a load-locked
16920 instruction in MODE. */
16922 static void
16923 emit_load_locked (enum machine_mode mode, rtx reg, rtx mem)
16925 rtx (*fn) (rtx, rtx) = NULL;
16927 switch (mode)
16929 case SImode:
16930 fn = gen_load_lockedsi;
16931 break;
16932 case DImode:
16933 fn = gen_load_lockeddi;
16934 break;
16935 default:
16936 gcc_unreachable ();
16938 emit_insn (fn (reg, mem));
16941 /* A subroutine of the atomic operation splitters. Emit a store-conditional
16942 instruction in MODE. */
16944 static void
16945 emit_store_conditional (enum machine_mode mode, rtx res, rtx mem, rtx val)
16947 rtx (*fn) (rtx, rtx, rtx) = NULL;
16949 switch (mode)
16951 case SImode:
16952 fn = gen_store_conditionalsi;
16953 break;
16954 case DImode:
16955 fn = gen_store_conditionaldi;
16956 break;
16957 default:
16958 gcc_unreachable ();
16961 /* Emit sync before stwcx. to address PPC405 Erratum. */
16962 if (PPC405_ERRATUM77)
16963 emit_insn (gen_hwsync ());
16965 emit_insn (fn (res, mem, val));
16968 /* Expand barriers before and after a load_locked/store_cond sequence. */
16970 static rtx
16971 rs6000_pre_atomic_barrier (rtx mem, enum memmodel model)
16973 rtx addr = XEXP (mem, 0);
16974 int strict_p = (reload_in_progress || reload_completed);
16976 if (!legitimate_indirect_address_p (addr, strict_p)
16977 && !legitimate_indexed_address_p (addr, strict_p))
16979 addr = force_reg (Pmode, addr);
16980 mem = replace_equiv_address_nv (mem, addr);
16983 switch (model)
16985 case MEMMODEL_RELAXED:
16986 case MEMMODEL_CONSUME:
16987 case MEMMODEL_ACQUIRE:
16988 break;
16989 case MEMMODEL_RELEASE:
16990 case MEMMODEL_ACQ_REL:
16991 emit_insn (gen_lwsync ());
16992 break;
16993 case MEMMODEL_SEQ_CST:
16994 emit_insn (gen_hwsync ());
16995 break;
16996 default:
16997 gcc_unreachable ();
16999 return mem;
17002 static void
17003 rs6000_post_atomic_barrier (enum memmodel model)
17005 switch (model)
17007 case MEMMODEL_RELAXED:
17008 case MEMMODEL_CONSUME:
17009 case MEMMODEL_RELEASE:
17010 break;
17011 case MEMMODEL_ACQUIRE:
17012 case MEMMODEL_ACQ_REL:
17013 case MEMMODEL_SEQ_CST:
17014 emit_insn (gen_isync ());
17015 break;
17016 default:
17017 gcc_unreachable ();
17021 /* A subroutine of the various atomic expanders. For sub-word operations,
17022 we must adjust things to operate on SImode. Given the original MEM,
17023 return a new aligned memory. Also build and return the quantities by
17024 which to shift and mask. */
17026 static rtx
17027 rs6000_adjust_atomic_subword (rtx orig_mem, rtx *pshift, rtx *pmask)
17029 rtx addr, align, shift, mask, mem;
17030 HOST_WIDE_INT shift_mask;
17031 enum machine_mode mode = GET_MODE (orig_mem);
17033 /* For smaller modes, we have to implement this via SImode. */
17034 shift_mask = (mode == QImode ? 0x18 : 0x10);
17036 addr = XEXP (orig_mem, 0);
17037 addr = force_reg (GET_MODE (addr), addr);
17039 /* Aligned memory containing subword. Generate a new memory. We
17040 do not want any of the existing MEM_ATTR data, as we're now
17041 accessing memory outside the original object. */
17042 align = expand_simple_binop (Pmode, AND, addr, GEN_INT (-4),
17043 NULL_RTX, 1, OPTAB_LIB_WIDEN);
17044 mem = gen_rtx_MEM (SImode, align);
17045 MEM_VOLATILE_P (mem) = MEM_VOLATILE_P (orig_mem);
17046 if (MEM_ALIAS_SET (orig_mem) == ALIAS_SET_MEMORY_BARRIER)
17047 set_mem_alias_set (mem, ALIAS_SET_MEMORY_BARRIER);
17049 /* Shift amount for subword relative to aligned word. */
17050 shift = gen_reg_rtx (SImode);
17051 addr = gen_lowpart (SImode, addr);
17052 emit_insn (gen_rlwinm (shift, addr, GEN_INT (3), GEN_INT (shift_mask)));
17053 shift = expand_simple_binop (SImode, XOR, shift, GEN_INT (shift_mask),
17054 shift, 1, OPTAB_LIB_WIDEN);
17055 *pshift = shift;
17057 /* Mask for insertion. */
17058 mask = expand_simple_binop (SImode, ASHIFT, GEN_INT (GET_MODE_MASK (mode)),
17059 shift, NULL_RTX, 1, OPTAB_LIB_WIDEN);
17060 *pmask = mask;
17062 return mem;
17065 /* A subroutine of the various atomic expanders. For sub-word operands,
17066 combine OLDVAL and NEWVAL via MASK. Returns a new pseduo. */
17068 static rtx
17069 rs6000_mask_atomic_subword (rtx oldval, rtx newval, rtx mask)
17071 rtx x;
17073 x = gen_reg_rtx (SImode);
17074 emit_insn (gen_rtx_SET (VOIDmode, x,
17075 gen_rtx_AND (SImode,
17076 gen_rtx_NOT (SImode, mask),
17077 oldval)));
17079 x = expand_simple_binop (SImode, IOR, newval, x, x, 1, OPTAB_LIB_WIDEN);
17081 return x;
17084 /* A subroutine of the various atomic expanders. For sub-word operands,
17085 extract WIDE to NARROW via SHIFT. */
17087 static void
17088 rs6000_finish_atomic_subword (rtx narrow, rtx wide, rtx shift)
17090 wide = expand_simple_binop (SImode, LSHIFTRT, wide, shift,
17091 wide, 1, OPTAB_LIB_WIDEN);
17092 emit_move_insn (narrow, gen_lowpart (GET_MODE (narrow), wide));
17095 /* Expand an atomic compare and swap operation. */
17097 void
17098 rs6000_expand_atomic_compare_and_swap (rtx operands[])
17100 rtx boolval, retval, mem, oldval, newval, cond;
17101 rtx label1, label2, x, mask, shift;
17102 enum machine_mode mode;
17103 enum memmodel mod_s, mod_f;
17104 bool is_weak;
17106 boolval = operands[0];
17107 retval = operands[1];
17108 mem = operands[2];
17109 oldval = operands[3];
17110 newval = operands[4];
17111 is_weak = (INTVAL (operands[5]) != 0);
17112 mod_s = (enum memmodel) INTVAL (operands[6]);
17113 mod_f = (enum memmodel) INTVAL (operands[7]);
17114 mode = GET_MODE (mem);
17116 mask = shift = NULL_RTX;
17117 if (mode == QImode || mode == HImode)
17119 mem = rs6000_adjust_atomic_subword (mem, &shift, &mask);
17121 /* Shift and mask OLDVAL into position with the word. */
17122 oldval = convert_modes (SImode, mode, oldval, 1);
17123 oldval = expand_simple_binop (SImode, ASHIFT, oldval, shift,
17124 NULL_RTX, 1, OPTAB_LIB_WIDEN);
17126 /* Shift and mask NEWVAL into position within the word. */
17127 newval = convert_modes (SImode, mode, newval, 1);
17128 newval = expand_simple_binop (SImode, ASHIFT, newval, shift,
17129 NULL_RTX, 1, OPTAB_LIB_WIDEN);
17131 /* Prepare to adjust the return value. */
17132 retval = gen_reg_rtx (SImode);
17133 mode = SImode;
17135 else if (reg_overlap_mentioned_p (retval, oldval))
17136 oldval = copy_to_reg (oldval);
17138 mem = rs6000_pre_atomic_barrier (mem, mod_s);
17140 label1 = NULL_RTX;
17141 if (!is_weak)
17143 label1 = gen_rtx_LABEL_REF (VOIDmode, gen_label_rtx ());
17144 emit_label (XEXP (label1, 0));
17146 label2 = gen_rtx_LABEL_REF (VOIDmode, gen_label_rtx ());
17148 emit_load_locked (mode, retval, mem);
17150 x = retval;
17151 if (mask)
17153 x = expand_simple_binop (SImode, AND, retval, mask,
17154 NULL_RTX, 1, OPTAB_LIB_WIDEN);
17157 cond = gen_reg_rtx (CCmode);
17158 x = gen_rtx_COMPARE (CCmode, x, oldval);
17159 emit_insn (gen_rtx_SET (VOIDmode, cond, x));
17161 x = gen_rtx_NE (VOIDmode, cond, const0_rtx);
17162 emit_unlikely_jump (x, label2);
17164 x = newval;
17165 if (mask)
17166 x = rs6000_mask_atomic_subword (retval, newval, mask);
17168 emit_store_conditional (mode, cond, mem, x);
17170 if (!is_weak)
17172 x = gen_rtx_NE (VOIDmode, cond, const0_rtx);
17173 emit_unlikely_jump (x, label1);
17176 if (mod_f != MEMMODEL_RELAXED)
17177 emit_label (XEXP (label2, 0));
17179 rs6000_post_atomic_barrier (mod_s);
17181 if (mod_f == MEMMODEL_RELAXED)
17182 emit_label (XEXP (label2, 0));
17184 if (shift)
17185 rs6000_finish_atomic_subword (operands[1], retval, shift);
17187 /* In all cases, CR0 contains EQ on success, and NE on failure. */
17188 x = gen_rtx_EQ (SImode, cond, const0_rtx);
17189 emit_insn (gen_rtx_SET (VOIDmode, boolval, x));
17192 /* Expand an atomic exchange operation. */
17194 void
17195 rs6000_expand_atomic_exchange (rtx operands[])
17197 rtx retval, mem, val, cond;
17198 enum machine_mode mode;
17199 enum memmodel model;
17200 rtx label, x, mask, shift;
17202 retval = operands[0];
17203 mem = operands[1];
17204 val = operands[2];
17205 model = (enum memmodel) INTVAL (operands[3]);
17206 mode = GET_MODE (mem);
17208 mask = shift = NULL_RTX;
17209 if (mode == QImode || mode == HImode)
17211 mem = rs6000_adjust_atomic_subword (mem, &shift, &mask);
17213 /* Shift and mask VAL into position with the word. */
17214 val = convert_modes (SImode, mode, val, 1);
17215 val = expand_simple_binop (SImode, ASHIFT, val, shift,
17216 NULL_RTX, 1, OPTAB_LIB_WIDEN);
17218 /* Prepare to adjust the return value. */
17219 retval = gen_reg_rtx (SImode);
17220 mode = SImode;
17223 mem = rs6000_pre_atomic_barrier (mem, model);
17225 label = gen_rtx_LABEL_REF (VOIDmode, gen_label_rtx ());
17226 emit_label (XEXP (label, 0));
17228 emit_load_locked (mode, retval, mem);
17230 x = val;
17231 if (mask)
17232 x = rs6000_mask_atomic_subword (retval, val, mask);
17234 cond = gen_reg_rtx (CCmode);
17235 emit_store_conditional (mode, cond, mem, x);
17237 x = gen_rtx_NE (VOIDmode, cond, const0_rtx);
17238 emit_unlikely_jump (x, label);
17240 rs6000_post_atomic_barrier (model);
17242 if (shift)
17243 rs6000_finish_atomic_subword (operands[0], retval, shift);
17246 /* Expand an atomic fetch-and-operate pattern. CODE is the binary operation
17247 to perform. MEM is the memory on which to operate. VAL is the second
17248 operand of the binary operator. BEFORE and AFTER are optional locations to
17249 return the value of MEM either before of after the operation. MODEL_RTX
17250 is a CONST_INT containing the memory model to use. */
17252 void
17253 rs6000_expand_atomic_op (enum rtx_code code, rtx mem, rtx val,
17254 rtx orig_before, rtx orig_after, rtx model_rtx)
17256 enum memmodel model = (enum memmodel) INTVAL (model_rtx);
17257 enum machine_mode mode = GET_MODE (mem);
17258 rtx label, x, cond, mask, shift;
17259 rtx before = orig_before, after = orig_after;
17261 mask = shift = NULL_RTX;
17262 if (mode == QImode || mode == HImode)
17264 mem = rs6000_adjust_atomic_subword (mem, &shift, &mask);
17266 /* Shift and mask VAL into position with the word. */
17267 val = convert_modes (SImode, mode, val, 1);
17268 val = expand_simple_binop (SImode, ASHIFT, val, shift,
17269 NULL_RTX, 1, OPTAB_LIB_WIDEN);
17271 switch (code)
17273 case IOR:
17274 case XOR:
17275 /* We've already zero-extended VAL. That is sufficient to
17276 make certain that it does not affect other bits. */
17277 mask = NULL;
17278 break;
17280 case AND:
17281 /* If we make certain that all of the other bits in VAL are
17282 set, that will be sufficient to not affect other bits. */
17283 x = gen_rtx_NOT (SImode, mask);
17284 x = gen_rtx_IOR (SImode, x, val);
17285 emit_insn (gen_rtx_SET (VOIDmode, val, x));
17286 mask = NULL;
17287 break;
17289 case NOT:
17290 case PLUS:
17291 case MINUS:
17292 /* These will all affect bits outside the field and need
17293 adjustment via MASK within the loop. */
17294 break;
17296 default:
17297 gcc_unreachable ();
17300 /* Prepare to adjust the return value. */
17301 before = gen_reg_rtx (SImode);
17302 if (after)
17303 after = gen_reg_rtx (SImode);
17304 mode = SImode;
17307 mem = rs6000_pre_atomic_barrier (mem, model);
17309 label = gen_label_rtx ();
17310 emit_label (label);
17311 label = gen_rtx_LABEL_REF (VOIDmode, label);
17313 if (before == NULL_RTX)
17314 before = gen_reg_rtx (mode);
17316 emit_load_locked (mode, before, mem);
17318 if (code == NOT)
17320 x = expand_simple_binop (mode, AND, before, val,
17321 NULL_RTX, 1, OPTAB_LIB_WIDEN);
17322 after = expand_simple_unop (mode, NOT, x, after, 1);
17324 else
17326 after = expand_simple_binop (mode, code, before, val,
17327 after, 1, OPTAB_LIB_WIDEN);
17330 x = after;
17331 if (mask)
17333 x = expand_simple_binop (SImode, AND, after, mask,
17334 NULL_RTX, 1, OPTAB_LIB_WIDEN);
17335 x = rs6000_mask_atomic_subword (before, x, mask);
17338 cond = gen_reg_rtx (CCmode);
17339 emit_store_conditional (mode, cond, mem, x);
17341 x = gen_rtx_NE (VOIDmode, cond, const0_rtx);
17342 emit_unlikely_jump (x, label);
17344 rs6000_post_atomic_barrier (model);
17346 if (shift)
17348 if (orig_before)
17349 rs6000_finish_atomic_subword (orig_before, before, shift);
17350 if (orig_after)
17351 rs6000_finish_atomic_subword (orig_after, after, shift);
17353 else if (orig_after && after != orig_after)
17354 emit_move_insn (orig_after, after);
17357 /* Emit instructions to move SRC to DST. Called by splitters for
17358 multi-register moves. It will emit at most one instruction for
17359 each register that is accessed; that is, it won't emit li/lis pairs
17360 (or equivalent for 64-bit code). One of SRC or DST must be a hard
17361 register. */
17363 void
17364 rs6000_split_multireg_move (rtx dst, rtx src)
17366 /* The register number of the first register being moved. */
17367 int reg;
17368 /* The mode that is to be moved. */
17369 enum machine_mode mode;
17370 /* The mode that the move is being done in, and its size. */
17371 enum machine_mode reg_mode;
17372 int reg_mode_size;
17373 /* The number of registers that will be moved. */
17374 int nregs;
17376 reg = REG_P (dst) ? REGNO (dst) : REGNO (src);
17377 mode = GET_MODE (dst);
17378 nregs = hard_regno_nregs[reg][mode];
17379 if (FP_REGNO_P (reg))
17380 reg_mode = DECIMAL_FLOAT_MODE_P (mode) ? DDmode :
17381 ((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT) ? DFmode : SFmode);
17382 else if (ALTIVEC_REGNO_P (reg))
17383 reg_mode = V16QImode;
17384 else if (TARGET_E500_DOUBLE && mode == TFmode)
17385 reg_mode = DFmode;
17386 else
17387 reg_mode = word_mode;
17388 reg_mode_size = GET_MODE_SIZE (reg_mode);
17390 gcc_assert (reg_mode_size * nregs == GET_MODE_SIZE (mode));
17392 if (REG_P (src) && REG_P (dst) && (REGNO (src) < REGNO (dst)))
17394 /* Move register range backwards, if we might have destructive
17395 overlap. */
17396 int i;
17397 for (i = nregs - 1; i >= 0; i--)
17398 emit_insn (gen_rtx_SET (VOIDmode,
17399 simplify_gen_subreg (reg_mode, dst, mode,
17400 i * reg_mode_size),
17401 simplify_gen_subreg (reg_mode, src, mode,
17402 i * reg_mode_size)));
17404 else
17406 int i;
17407 int j = -1;
17408 bool used_update = false;
17409 rtx restore_basereg = NULL_RTX;
17411 if (MEM_P (src) && INT_REGNO_P (reg))
17413 rtx breg;
17415 if (GET_CODE (XEXP (src, 0)) == PRE_INC
17416 || GET_CODE (XEXP (src, 0)) == PRE_DEC)
17418 rtx delta_rtx;
17419 breg = XEXP (XEXP (src, 0), 0);
17420 delta_rtx = (GET_CODE (XEXP (src, 0)) == PRE_INC
17421 ? GEN_INT (GET_MODE_SIZE (GET_MODE (src)))
17422 : GEN_INT (-GET_MODE_SIZE (GET_MODE (src))));
17423 emit_insn (gen_add3_insn (breg, breg, delta_rtx));
17424 src = replace_equiv_address (src, breg);
17426 else if (! rs6000_offsettable_memref_p (src, reg_mode))
17428 if (GET_CODE (XEXP (src, 0)) == PRE_MODIFY)
17430 rtx basereg = XEXP (XEXP (src, 0), 0);
17431 if (TARGET_UPDATE)
17433 rtx ndst = simplify_gen_subreg (reg_mode, dst, mode, 0);
17434 emit_insn (gen_rtx_SET (VOIDmode, ndst,
17435 gen_rtx_MEM (reg_mode, XEXP (src, 0))));
17436 used_update = true;
17438 else
17439 emit_insn (gen_rtx_SET (VOIDmode, basereg,
17440 XEXP (XEXP (src, 0), 1)));
17441 src = replace_equiv_address (src, basereg);
17443 else
17445 rtx basereg = gen_rtx_REG (Pmode, reg);
17446 emit_insn (gen_rtx_SET (VOIDmode, basereg, XEXP (src, 0)));
17447 src = replace_equiv_address (src, basereg);
17451 breg = XEXP (src, 0);
17452 if (GET_CODE (breg) == PLUS || GET_CODE (breg) == LO_SUM)
17453 breg = XEXP (breg, 0);
17455 /* If the base register we are using to address memory is
17456 also a destination reg, then change that register last. */
17457 if (REG_P (breg)
17458 && REGNO (breg) >= REGNO (dst)
17459 && REGNO (breg) < REGNO (dst) + nregs)
17460 j = REGNO (breg) - REGNO (dst);
17462 else if (MEM_P (dst) && INT_REGNO_P (reg))
17464 rtx breg;
17466 if (GET_CODE (XEXP (dst, 0)) == PRE_INC
17467 || GET_CODE (XEXP (dst, 0)) == PRE_DEC)
17469 rtx delta_rtx;
17470 breg = XEXP (XEXP (dst, 0), 0);
17471 delta_rtx = (GET_CODE (XEXP (dst, 0)) == PRE_INC
17472 ? GEN_INT (GET_MODE_SIZE (GET_MODE (dst)))
17473 : GEN_INT (-GET_MODE_SIZE (GET_MODE (dst))));
17475 /* We have to update the breg before doing the store.
17476 Use store with update, if available. */
17478 if (TARGET_UPDATE)
17480 rtx nsrc = simplify_gen_subreg (reg_mode, src, mode, 0);
17481 emit_insn (TARGET_32BIT
17482 ? (TARGET_POWERPC64
17483 ? gen_movdi_si_update (breg, breg, delta_rtx, nsrc)
17484 : gen_movsi_update (breg, breg, delta_rtx, nsrc))
17485 : gen_movdi_di_update (breg, breg, delta_rtx, nsrc));
17486 used_update = true;
17488 else
17489 emit_insn (gen_add3_insn (breg, breg, delta_rtx));
17490 dst = replace_equiv_address (dst, breg);
17492 else if (!rs6000_offsettable_memref_p (dst, reg_mode)
17493 && GET_CODE (XEXP (dst, 0)) != LO_SUM)
17495 if (GET_CODE (XEXP (dst, 0)) == PRE_MODIFY)
17497 rtx basereg = XEXP (XEXP (dst, 0), 0);
17498 if (TARGET_UPDATE)
17500 rtx nsrc = simplify_gen_subreg (reg_mode, src, mode, 0);
17501 emit_insn (gen_rtx_SET (VOIDmode,
17502 gen_rtx_MEM (reg_mode, XEXP (dst, 0)), nsrc));
17503 used_update = true;
17505 else
17506 emit_insn (gen_rtx_SET (VOIDmode, basereg,
17507 XEXP (XEXP (dst, 0), 1)));
17508 dst = replace_equiv_address (dst, basereg);
17510 else
17512 rtx basereg = XEXP (XEXP (dst, 0), 0);
17513 rtx offsetreg = XEXP (XEXP (dst, 0), 1);
17514 gcc_assert (GET_CODE (XEXP (dst, 0)) == PLUS
17515 && REG_P (basereg)
17516 && REG_P (offsetreg)
17517 && REGNO (basereg) != REGNO (offsetreg));
17518 if (REGNO (basereg) == 0)
17520 rtx tmp = offsetreg;
17521 offsetreg = basereg;
17522 basereg = tmp;
17524 emit_insn (gen_add3_insn (basereg, basereg, offsetreg));
17525 restore_basereg = gen_sub3_insn (basereg, basereg, offsetreg);
17526 dst = replace_equiv_address (dst, basereg);
17529 else if (GET_CODE (XEXP (dst, 0)) != LO_SUM)
17530 gcc_assert (rs6000_offsettable_memref_p (dst, reg_mode));
17533 for (i = 0; i < nregs; i++)
17535 /* Calculate index to next subword. */
17536 ++j;
17537 if (j == nregs)
17538 j = 0;
17540 /* If compiler already emitted move of first word by
17541 store with update, no need to do anything. */
17542 if (j == 0 && used_update)
17543 continue;
17545 emit_insn (gen_rtx_SET (VOIDmode,
17546 simplify_gen_subreg (reg_mode, dst, mode,
17547 j * reg_mode_size),
17548 simplify_gen_subreg (reg_mode, src, mode,
17549 j * reg_mode_size)));
17551 if (restore_basereg != NULL_RTX)
17552 emit_insn (restore_basereg);
17557 /* This page contains routines that are used to determine what the
17558 function prologue and epilogue code will do and write them out. */
17560 static inline bool
17561 save_reg_p (int r)
17563 return !call_used_regs[r] && df_regs_ever_live_p (r);
17566 /* Return the first fixed-point register that is required to be
17567 saved. 32 if none. */
17570 first_reg_to_save (void)
17572 int first_reg;
17574 /* Find lowest numbered live register. */
17575 for (first_reg = 13; first_reg <= 31; first_reg++)
17576 if (save_reg_p (first_reg))
17577 break;
17579 if (first_reg > RS6000_PIC_OFFSET_TABLE_REGNUM
17580 && ((DEFAULT_ABI == ABI_V4 && flag_pic != 0)
17581 || (DEFAULT_ABI == ABI_DARWIN && flag_pic)
17582 || (TARGET_TOC && TARGET_MINIMAL_TOC))
17583 && df_regs_ever_live_p (RS6000_PIC_OFFSET_TABLE_REGNUM))
17584 first_reg = RS6000_PIC_OFFSET_TABLE_REGNUM;
17586 #if TARGET_MACHO
17587 if (flag_pic
17588 && crtl->uses_pic_offset_table
17589 && first_reg > RS6000_PIC_OFFSET_TABLE_REGNUM)
17590 return RS6000_PIC_OFFSET_TABLE_REGNUM;
17591 #endif
17593 return first_reg;
17596 /* Similar, for FP regs. */
17599 first_fp_reg_to_save (void)
17601 int first_reg;
17603 /* Find lowest numbered live register. */
17604 for (first_reg = 14 + 32; first_reg <= 63; first_reg++)
17605 if (save_reg_p (first_reg))
17606 break;
17608 return first_reg;
17611 /* Similar, for AltiVec regs. */
17613 static int
17614 first_altivec_reg_to_save (void)
17616 int i;
17618 /* Stack frame remains as is unless we are in AltiVec ABI. */
17619 if (! TARGET_ALTIVEC_ABI)
17620 return LAST_ALTIVEC_REGNO + 1;
17622 /* On Darwin, the unwind routines are compiled without
17623 TARGET_ALTIVEC, and use save_world to save/restore the
17624 altivec registers when necessary. */
17625 if (DEFAULT_ABI == ABI_DARWIN && crtl->calls_eh_return
17626 && ! TARGET_ALTIVEC)
17627 return FIRST_ALTIVEC_REGNO + 20;
17629 /* Find lowest numbered live register. */
17630 for (i = FIRST_ALTIVEC_REGNO + 20; i <= LAST_ALTIVEC_REGNO; ++i)
17631 if (save_reg_p (i))
17632 break;
17634 return i;
17637 /* Return a 32-bit mask of the AltiVec registers we need to set in
17638 VRSAVE. Bit n of the return value is 1 if Vn is live. The MSB in
17639 the 32-bit word is 0. */
17641 static unsigned int
17642 compute_vrsave_mask (void)
17644 unsigned int i, mask = 0;
17646 /* On Darwin, the unwind routines are compiled without
17647 TARGET_ALTIVEC, and use save_world to save/restore the
17648 call-saved altivec registers when necessary. */
17649 if (DEFAULT_ABI == ABI_DARWIN && crtl->calls_eh_return
17650 && ! TARGET_ALTIVEC)
17651 mask |= 0xFFF;
17653 /* First, find out if we use _any_ altivec registers. */
17654 for (i = FIRST_ALTIVEC_REGNO; i <= LAST_ALTIVEC_REGNO; ++i)
17655 if (df_regs_ever_live_p (i))
17656 mask |= ALTIVEC_REG_BIT (i);
17658 if (mask == 0)
17659 return mask;
17661 /* Next, remove the argument registers from the set. These must
17662 be in the VRSAVE mask set by the caller, so we don't need to add
17663 them in again. More importantly, the mask we compute here is
17664 used to generate CLOBBERs in the set_vrsave insn, and we do not
17665 wish the argument registers to die. */
17666 for (i = crtl->args.info.vregno - 1; i >= ALTIVEC_ARG_MIN_REG; --i)
17667 mask &= ~ALTIVEC_REG_BIT (i);
17669 /* Similarly, remove the return value from the set. */
17671 bool yes = false;
17672 diddle_return_value (is_altivec_return_reg, &yes);
17673 if (yes)
17674 mask &= ~ALTIVEC_REG_BIT (ALTIVEC_ARG_RETURN);
17677 return mask;
17680 /* For a very restricted set of circumstances, we can cut down the
17681 size of prologues/epilogues by calling our own save/restore-the-world
17682 routines. */
17684 static void
17685 compute_save_world_info (rs6000_stack_t *info_ptr)
17687 info_ptr->world_save_p = 1;
17688 info_ptr->world_save_p
17689 = (WORLD_SAVE_P (info_ptr)
17690 && DEFAULT_ABI == ABI_DARWIN
17691 && !cfun->has_nonlocal_label
17692 && info_ptr->first_fp_reg_save == FIRST_SAVED_FP_REGNO
17693 && info_ptr->first_gp_reg_save == FIRST_SAVED_GP_REGNO
17694 && info_ptr->first_altivec_reg_save == FIRST_SAVED_ALTIVEC_REGNO
17695 && info_ptr->cr_save_p);
17697 /* This will not work in conjunction with sibcalls. Make sure there
17698 are none. (This check is expensive, but seldom executed.) */
17699 if (WORLD_SAVE_P (info_ptr))
17701 rtx insn;
17702 for (insn = get_last_insn_anywhere (); insn; insn = PREV_INSN (insn))
17703 if (CALL_P (insn) && SIBLING_CALL_P (insn))
17705 info_ptr->world_save_p = 0;
17706 break;
17710 if (WORLD_SAVE_P (info_ptr))
17712 /* Even if we're not touching VRsave, make sure there's room on the
17713 stack for it, if it looks like we're calling SAVE_WORLD, which
17714 will attempt to save it. */
17715 info_ptr->vrsave_size = 4;
17717 /* If we are going to save the world, we need to save the link register too. */
17718 info_ptr->lr_save_p = 1;
17720 /* "Save" the VRsave register too if we're saving the world. */
17721 if (info_ptr->vrsave_mask == 0)
17722 info_ptr->vrsave_mask = compute_vrsave_mask ();
17724 /* Because the Darwin register save/restore routines only handle
17725 F14 .. F31 and V20 .. V31 as per the ABI, perform a consistency
17726 check. */
17727 gcc_assert (info_ptr->first_fp_reg_save >= FIRST_SAVED_FP_REGNO
17728 && (info_ptr->first_altivec_reg_save
17729 >= FIRST_SAVED_ALTIVEC_REGNO));
17731 return;
17735 static void
17736 is_altivec_return_reg (rtx reg, void *xyes)
17738 bool *yes = (bool *) xyes;
17739 if (REGNO (reg) == ALTIVEC_ARG_RETURN)
17740 *yes = true;
17744 /* Look for user-defined global regs in the range FIRST to LAST-1.
17745 We should not restore these, and so cannot use lmw or out-of-line
17746 restore functions if there are any. We also can't save them
17747 (well, emit frame notes for them), because frame unwinding during
17748 exception handling will restore saved registers. */
17750 static bool
17751 global_regs_p (unsigned first, unsigned last)
17753 while (first < last)
17754 if (global_regs[first++])
17755 return true;
17756 return false;
17759 /* Determine the strategy for savings/restoring registers. */
17761 enum {
17762 SAVRES_MULTIPLE = 0x1,
17763 SAVE_INLINE_FPRS = 0x2,
17764 SAVE_INLINE_GPRS = 0x4,
17765 REST_INLINE_FPRS = 0x8,
17766 REST_INLINE_GPRS = 0x10,
17767 SAVE_NOINLINE_GPRS_SAVES_LR = 0x20,
17768 SAVE_NOINLINE_FPRS_SAVES_LR = 0x40,
17769 REST_NOINLINE_FPRS_DOESNT_RESTORE_LR = 0x80,
17770 SAVE_INLINE_VRS = 0x100,
17771 REST_INLINE_VRS = 0x200
17774 static int
17775 rs6000_savres_strategy (rs6000_stack_t *info,
17776 bool using_static_chain_p)
17778 int strategy = 0;
17779 bool lr_save_p;
17781 if (TARGET_MULTIPLE
17782 && !TARGET_POWERPC64
17783 && !(TARGET_SPE_ABI && info->spe_64bit_regs_used)
17784 && info->first_gp_reg_save < 31
17785 && !global_regs_p (info->first_gp_reg_save, 32))
17786 strategy |= SAVRES_MULTIPLE;
17788 if (crtl->calls_eh_return
17789 || cfun->machine->ra_need_lr)
17790 strategy |= (SAVE_INLINE_FPRS | REST_INLINE_FPRS
17791 | SAVE_INLINE_GPRS | REST_INLINE_GPRS
17792 | SAVE_INLINE_VRS | REST_INLINE_VRS);
17794 if (info->first_fp_reg_save == 64
17795 /* The out-of-line FP routines use double-precision stores;
17796 we can't use those routines if we don't have such stores. */
17797 || (TARGET_HARD_FLOAT && !TARGET_DOUBLE_FLOAT)
17798 || global_regs_p (info->first_fp_reg_save, 64))
17799 strategy |= SAVE_INLINE_FPRS | REST_INLINE_FPRS;
17801 if (info->first_gp_reg_save == 32
17802 || (!(strategy & SAVRES_MULTIPLE)
17803 && global_regs_p (info->first_gp_reg_save, 32)))
17804 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
17806 if (info->first_altivec_reg_save == LAST_ALTIVEC_REGNO + 1
17807 || global_regs_p (info->first_altivec_reg_save, LAST_ALTIVEC_REGNO + 1))
17808 strategy |= SAVE_INLINE_VRS | REST_INLINE_VRS;
17810 /* Define cutoff for using out-of-line functions to save registers. */
17811 if (DEFAULT_ABI == ABI_V4 || TARGET_ELF)
17813 if (!optimize_size)
17815 strategy |= SAVE_INLINE_FPRS | REST_INLINE_FPRS;
17816 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
17817 strategy |= SAVE_INLINE_VRS | REST_INLINE_VRS;
17819 else
17821 /* Prefer out-of-line restore if it will exit. */
17822 if (info->first_fp_reg_save > 61)
17823 strategy |= SAVE_INLINE_FPRS;
17824 if (info->first_gp_reg_save > 29)
17826 if (info->first_fp_reg_save == 64)
17827 strategy |= SAVE_INLINE_GPRS;
17828 else
17829 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
17831 if (info->first_altivec_reg_save == LAST_ALTIVEC_REGNO)
17832 strategy |= SAVE_INLINE_VRS | REST_INLINE_VRS;
17835 else if (DEFAULT_ABI == ABI_DARWIN)
17837 if (info->first_fp_reg_save > 60)
17838 strategy |= SAVE_INLINE_FPRS | REST_INLINE_FPRS;
17839 if (info->first_gp_reg_save > 29)
17840 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
17841 strategy |= SAVE_INLINE_VRS | REST_INLINE_VRS;
17843 else
17845 gcc_checking_assert (DEFAULT_ABI == ABI_AIX);
17846 if (info->first_fp_reg_save > 61)
17847 strategy |= SAVE_INLINE_FPRS | REST_INLINE_FPRS;
17848 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
17849 strategy |= SAVE_INLINE_VRS | REST_INLINE_VRS;
17852 /* Don't bother to try to save things out-of-line if r11 is occupied
17853 by the static chain. It would require too much fiddling and the
17854 static chain is rarely used anyway. FPRs are saved w.r.t the stack
17855 pointer on Darwin, and AIX uses r1 or r12. */
17856 if (using_static_chain_p && DEFAULT_ABI != ABI_AIX)
17857 strategy |= ((DEFAULT_ABI == ABI_DARWIN ? 0 : SAVE_INLINE_FPRS)
17858 | SAVE_INLINE_GPRS
17859 | SAVE_INLINE_VRS | REST_INLINE_VRS);
17861 /* We can only use the out-of-line routines to restore if we've
17862 saved all the registers from first_fp_reg_save in the prologue.
17863 Otherwise, we risk loading garbage. */
17864 if ((strategy & (SAVE_INLINE_FPRS | REST_INLINE_FPRS)) == SAVE_INLINE_FPRS)
17866 int i;
17868 for (i = info->first_fp_reg_save; i < 64; i++)
17869 if (!save_reg_p (i))
17871 strategy |= REST_INLINE_FPRS;
17872 break;
17876 /* If we are going to use store multiple, then don't even bother
17877 with the out-of-line routines, since the store-multiple
17878 instruction will always be smaller. */
17879 if ((strategy & SAVRES_MULTIPLE))
17880 strategy |= SAVE_INLINE_GPRS;
17882 /* info->lr_save_p isn't yet set if the only reason lr needs to be
17883 saved is an out-of-line save or restore. Set up the value for
17884 the next test (excluding out-of-line gpr restore). */
17885 lr_save_p = (info->lr_save_p
17886 || !(strategy & SAVE_INLINE_GPRS)
17887 || !(strategy & SAVE_INLINE_FPRS)
17888 || !(strategy & SAVE_INLINE_VRS)
17889 || !(strategy & REST_INLINE_FPRS)
17890 || !(strategy & REST_INLINE_VRS));
17892 /* The situation is more complicated with load multiple. We'd
17893 prefer to use the out-of-line routines for restores, since the
17894 "exit" out-of-line routines can handle the restore of LR and the
17895 frame teardown. However if doesn't make sense to use the
17896 out-of-line routine if that is the only reason we'd need to save
17897 LR, and we can't use the "exit" out-of-line gpr restore if we
17898 have saved some fprs; In those cases it is advantageous to use
17899 load multiple when available. */
17900 if ((strategy & SAVRES_MULTIPLE)
17901 && (!lr_save_p
17902 || info->first_fp_reg_save != 64))
17903 strategy |= REST_INLINE_GPRS;
17905 /* Saving CR interferes with the exit routines used on the SPE, so
17906 just punt here. */
17907 if (TARGET_SPE_ABI
17908 && info->spe_64bit_regs_used
17909 && info->cr_save_p)
17910 strategy |= REST_INLINE_GPRS;
17912 /* We can only use load multiple or the out-of-line routines to
17913 restore if we've used store multiple or out-of-line routines
17914 in the prologue, i.e. if we've saved all the registers from
17915 first_gp_reg_save. Otherwise, we risk loading garbage. */
17916 if ((strategy & (SAVE_INLINE_GPRS | REST_INLINE_GPRS | SAVRES_MULTIPLE))
17917 == SAVE_INLINE_GPRS)
17919 int i;
17921 for (i = info->first_gp_reg_save; i < 32; i++)
17922 if (!save_reg_p (i))
17924 strategy |= REST_INLINE_GPRS;
17925 break;
17929 if (TARGET_ELF && TARGET_64BIT)
17931 if (!(strategy & SAVE_INLINE_FPRS))
17932 strategy |= SAVE_NOINLINE_FPRS_SAVES_LR;
17933 else if (!(strategy & SAVE_INLINE_GPRS)
17934 && info->first_fp_reg_save == 64)
17935 strategy |= SAVE_NOINLINE_GPRS_SAVES_LR;
17937 else if (TARGET_AIX && !(strategy & REST_INLINE_FPRS))
17938 strategy |= REST_NOINLINE_FPRS_DOESNT_RESTORE_LR;
17940 if (TARGET_MACHO && !(strategy & SAVE_INLINE_FPRS))
17941 strategy |= SAVE_NOINLINE_FPRS_SAVES_LR;
17943 return strategy;
17946 /* Calculate the stack information for the current function. This is
17947 complicated by having two separate calling sequences, the AIX calling
17948 sequence and the V.4 calling sequence.
17950 AIX (and Darwin/Mac OS X) stack frames look like:
17951 32-bit 64-bit
17952 SP----> +---------------------------------------+
17953 | back chain to caller | 0 0
17954 +---------------------------------------+
17955 | saved CR | 4 8 (8-11)
17956 +---------------------------------------+
17957 | saved LR | 8 16
17958 +---------------------------------------+
17959 | reserved for compilers | 12 24
17960 +---------------------------------------+
17961 | reserved for binders | 16 32
17962 +---------------------------------------+
17963 | saved TOC pointer | 20 40
17964 +---------------------------------------+
17965 | Parameter save area (P) | 24 48
17966 +---------------------------------------+
17967 | Alloca space (A) | 24+P etc.
17968 +---------------------------------------+
17969 | Local variable space (L) | 24+P+A
17970 +---------------------------------------+
17971 | Float/int conversion temporary (X) | 24+P+A+L
17972 +---------------------------------------+
17973 | Save area for AltiVec registers (W) | 24+P+A+L+X
17974 +---------------------------------------+
17975 | AltiVec alignment padding (Y) | 24+P+A+L+X+W
17976 +---------------------------------------+
17977 | Save area for VRSAVE register (Z) | 24+P+A+L+X+W+Y
17978 +---------------------------------------+
17979 | Save area for GP registers (G) | 24+P+A+X+L+X+W+Y+Z
17980 +---------------------------------------+
17981 | Save area for FP registers (F) | 24+P+A+X+L+X+W+Y+Z+G
17982 +---------------------------------------+
17983 old SP->| back chain to caller's caller |
17984 +---------------------------------------+
17986 The required alignment for AIX configurations is two words (i.e., 8
17987 or 16 bytes).
17990 V.4 stack frames look like:
17992 SP----> +---------------------------------------+
17993 | back chain to caller | 0
17994 +---------------------------------------+
17995 | caller's saved LR | 4
17996 +---------------------------------------+
17997 | Parameter save area (P) | 8
17998 +---------------------------------------+
17999 | Alloca space (A) | 8+P
18000 +---------------------------------------+
18001 | Varargs save area (V) | 8+P+A
18002 +---------------------------------------+
18003 | Local variable space (L) | 8+P+A+V
18004 +---------------------------------------+
18005 | Float/int conversion temporary (X) | 8+P+A+V+L
18006 +---------------------------------------+
18007 | Save area for AltiVec registers (W) | 8+P+A+V+L+X
18008 +---------------------------------------+
18009 | AltiVec alignment padding (Y) | 8+P+A+V+L+X+W
18010 +---------------------------------------+
18011 | Save area for VRSAVE register (Z) | 8+P+A+V+L+X+W+Y
18012 +---------------------------------------+
18013 | SPE: area for 64-bit GP registers |
18014 +---------------------------------------+
18015 | SPE alignment padding |
18016 +---------------------------------------+
18017 | saved CR (C) | 8+P+A+V+L+X+W+Y+Z
18018 +---------------------------------------+
18019 | Save area for GP registers (G) | 8+P+A+V+L+X+W+Y+Z+C
18020 +---------------------------------------+
18021 | Save area for FP registers (F) | 8+P+A+V+L+X+W+Y+Z+C+G
18022 +---------------------------------------+
18023 old SP->| back chain to caller's caller |
18024 +---------------------------------------+
18026 The required alignment for V.4 is 16 bytes, or 8 bytes if -meabi is
18027 given. (But note below and in sysv4.h that we require only 8 and
18028 may round up the size of our stack frame anyways. The historical
18029 reason is early versions of powerpc-linux which didn't properly
18030 align the stack at program startup. A happy side-effect is that
18031 -mno-eabi libraries can be used with -meabi programs.)
18033 The EABI configuration defaults to the V.4 layout. However,
18034 the stack alignment requirements may differ. If -mno-eabi is not
18035 given, the required stack alignment is 8 bytes; if -mno-eabi is
18036 given, the required alignment is 16 bytes. (But see V.4 comment
18037 above.) */
18039 #ifndef ABI_STACK_BOUNDARY
18040 #define ABI_STACK_BOUNDARY STACK_BOUNDARY
18041 #endif
18043 static rs6000_stack_t *
18044 rs6000_stack_info (void)
18046 rs6000_stack_t *info_ptr = &stack_info;
18047 int reg_size = TARGET_32BIT ? 4 : 8;
18048 int ehrd_size;
18049 int save_align;
18050 int first_gp;
18051 HOST_WIDE_INT non_fixed_size;
18052 bool using_static_chain_p;
18054 if (reload_completed && info_ptr->reload_completed)
18055 return info_ptr;
18057 memset (info_ptr, 0, sizeof (*info_ptr));
18058 info_ptr->reload_completed = reload_completed;
18060 if (TARGET_SPE)
18062 /* Cache value so we don't rescan instruction chain over and over. */
18063 if (cfun->machine->insn_chain_scanned_p == 0)
18064 cfun->machine->insn_chain_scanned_p
18065 = spe_func_has_64bit_regs_p () + 1;
18066 info_ptr->spe_64bit_regs_used = cfun->machine->insn_chain_scanned_p - 1;
18069 /* Select which calling sequence. */
18070 info_ptr->abi = DEFAULT_ABI;
18072 /* Calculate which registers need to be saved & save area size. */
18073 info_ptr->first_gp_reg_save = first_reg_to_save ();
18074 /* Assume that we will have to save RS6000_PIC_OFFSET_TABLE_REGNUM,
18075 even if it currently looks like we won't. Reload may need it to
18076 get at a constant; if so, it will have already created a constant
18077 pool entry for it. */
18078 if (((TARGET_TOC && TARGET_MINIMAL_TOC)
18079 || (flag_pic == 1 && DEFAULT_ABI == ABI_V4)
18080 || (flag_pic && DEFAULT_ABI == ABI_DARWIN))
18081 && crtl->uses_const_pool
18082 && info_ptr->first_gp_reg_save > RS6000_PIC_OFFSET_TABLE_REGNUM)
18083 first_gp = RS6000_PIC_OFFSET_TABLE_REGNUM;
18084 else
18085 first_gp = info_ptr->first_gp_reg_save;
18087 info_ptr->gp_size = reg_size * (32 - first_gp);
18089 /* For the SPE, we have an additional upper 32-bits on each GPR.
18090 Ideally we should save the entire 64-bits only when the upper
18091 half is used in SIMD instructions. Since we only record
18092 registers live (not the size they are used in), this proves
18093 difficult because we'd have to traverse the instruction chain at
18094 the right time, taking reload into account. This is a real pain,
18095 so we opt to save the GPRs in 64-bits always if but one register
18096 gets used in 64-bits. Otherwise, all the registers in the frame
18097 get saved in 32-bits.
18099 So... since when we save all GPRs (except the SP) in 64-bits, the
18100 traditional GP save area will be empty. */
18101 if (TARGET_SPE_ABI && info_ptr->spe_64bit_regs_used != 0)
18102 info_ptr->gp_size = 0;
18104 info_ptr->first_fp_reg_save = first_fp_reg_to_save ();
18105 info_ptr->fp_size = 8 * (64 - info_ptr->first_fp_reg_save);
18107 info_ptr->first_altivec_reg_save = first_altivec_reg_to_save ();
18108 info_ptr->altivec_size = 16 * (LAST_ALTIVEC_REGNO + 1
18109 - info_ptr->first_altivec_reg_save);
18111 /* Does this function call anything? */
18112 info_ptr->calls_p = (! crtl->is_leaf
18113 || cfun->machine->ra_needs_full_frame);
18115 /* Determine if we need to save the condition code registers. */
18116 if (df_regs_ever_live_p (CR2_REGNO)
18117 || df_regs_ever_live_p (CR3_REGNO)
18118 || df_regs_ever_live_p (CR4_REGNO))
18120 info_ptr->cr_save_p = 1;
18121 if (DEFAULT_ABI == ABI_V4)
18122 info_ptr->cr_size = reg_size;
18125 /* If the current function calls __builtin_eh_return, then we need
18126 to allocate stack space for registers that will hold data for
18127 the exception handler. */
18128 if (crtl->calls_eh_return)
18130 unsigned int i;
18131 for (i = 0; EH_RETURN_DATA_REGNO (i) != INVALID_REGNUM; ++i)
18132 continue;
18134 /* SPE saves EH registers in 64-bits. */
18135 ehrd_size = i * (TARGET_SPE_ABI
18136 && info_ptr->spe_64bit_regs_used != 0
18137 ? UNITS_PER_SPE_WORD : UNITS_PER_WORD);
18139 else
18140 ehrd_size = 0;
18142 /* Determine various sizes. */
18143 info_ptr->reg_size = reg_size;
18144 info_ptr->fixed_size = RS6000_SAVE_AREA;
18145 info_ptr->vars_size = RS6000_ALIGN (get_frame_size (), 8);
18146 info_ptr->parm_size = RS6000_ALIGN (crtl->outgoing_args_size,
18147 TARGET_ALTIVEC ? 16 : 8);
18148 if (FRAME_GROWS_DOWNWARD)
18149 info_ptr->vars_size
18150 += RS6000_ALIGN (info_ptr->fixed_size + info_ptr->vars_size
18151 + info_ptr->parm_size,
18152 ABI_STACK_BOUNDARY / BITS_PER_UNIT)
18153 - (info_ptr->fixed_size + info_ptr->vars_size
18154 + info_ptr->parm_size);
18156 if (TARGET_SPE_ABI && info_ptr->spe_64bit_regs_used != 0)
18157 info_ptr->spe_gp_size = 8 * (32 - first_gp);
18158 else
18159 info_ptr->spe_gp_size = 0;
18161 if (TARGET_ALTIVEC_ABI)
18162 info_ptr->vrsave_mask = compute_vrsave_mask ();
18163 else
18164 info_ptr->vrsave_mask = 0;
18166 if (TARGET_ALTIVEC_VRSAVE && info_ptr->vrsave_mask)
18167 info_ptr->vrsave_size = 4;
18168 else
18169 info_ptr->vrsave_size = 0;
18171 compute_save_world_info (info_ptr);
18173 /* Calculate the offsets. */
18174 switch (DEFAULT_ABI)
18176 case ABI_NONE:
18177 default:
18178 gcc_unreachable ();
18180 case ABI_AIX:
18181 case ABI_DARWIN:
18182 info_ptr->fp_save_offset = - info_ptr->fp_size;
18183 info_ptr->gp_save_offset = info_ptr->fp_save_offset - info_ptr->gp_size;
18185 if (TARGET_ALTIVEC_ABI)
18187 info_ptr->vrsave_save_offset
18188 = info_ptr->gp_save_offset - info_ptr->vrsave_size;
18190 /* Align stack so vector save area is on a quadword boundary.
18191 The padding goes above the vectors. */
18192 if (info_ptr->altivec_size != 0)
18193 info_ptr->altivec_padding_size
18194 = info_ptr->vrsave_save_offset & 0xF;
18195 else
18196 info_ptr->altivec_padding_size = 0;
18198 info_ptr->altivec_save_offset
18199 = info_ptr->vrsave_save_offset
18200 - info_ptr->altivec_padding_size
18201 - info_ptr->altivec_size;
18202 gcc_assert (info_ptr->altivec_size == 0
18203 || info_ptr->altivec_save_offset % 16 == 0);
18205 /* Adjust for AltiVec case. */
18206 info_ptr->ehrd_offset = info_ptr->altivec_save_offset - ehrd_size;
18208 else
18209 info_ptr->ehrd_offset = info_ptr->gp_save_offset - ehrd_size;
18210 info_ptr->cr_save_offset = reg_size; /* first word when 64-bit. */
18211 info_ptr->lr_save_offset = 2*reg_size;
18212 break;
18214 case ABI_V4:
18215 info_ptr->fp_save_offset = - info_ptr->fp_size;
18216 info_ptr->gp_save_offset = info_ptr->fp_save_offset - info_ptr->gp_size;
18217 info_ptr->cr_save_offset = info_ptr->gp_save_offset - info_ptr->cr_size;
18219 if (TARGET_SPE_ABI && info_ptr->spe_64bit_regs_used != 0)
18221 /* Align stack so SPE GPR save area is aligned on a
18222 double-word boundary. */
18223 if (info_ptr->spe_gp_size != 0 && info_ptr->cr_save_offset != 0)
18224 info_ptr->spe_padding_size
18225 = 8 - (-info_ptr->cr_save_offset % 8);
18226 else
18227 info_ptr->spe_padding_size = 0;
18229 info_ptr->spe_gp_save_offset
18230 = info_ptr->cr_save_offset
18231 - info_ptr->spe_padding_size
18232 - info_ptr->spe_gp_size;
18234 /* Adjust for SPE case. */
18235 info_ptr->ehrd_offset = info_ptr->spe_gp_save_offset;
18237 else if (TARGET_ALTIVEC_ABI)
18239 info_ptr->vrsave_save_offset
18240 = info_ptr->cr_save_offset - info_ptr->vrsave_size;
18242 /* Align stack so vector save area is on a quadword boundary. */
18243 if (info_ptr->altivec_size != 0)
18244 info_ptr->altivec_padding_size
18245 = 16 - (-info_ptr->vrsave_save_offset % 16);
18246 else
18247 info_ptr->altivec_padding_size = 0;
18249 info_ptr->altivec_save_offset
18250 = info_ptr->vrsave_save_offset
18251 - info_ptr->altivec_padding_size
18252 - info_ptr->altivec_size;
18254 /* Adjust for AltiVec case. */
18255 info_ptr->ehrd_offset = info_ptr->altivec_save_offset;
18257 else
18258 info_ptr->ehrd_offset = info_ptr->cr_save_offset;
18259 info_ptr->ehrd_offset -= ehrd_size;
18260 info_ptr->lr_save_offset = reg_size;
18261 break;
18264 save_align = (TARGET_ALTIVEC_ABI || DEFAULT_ABI == ABI_DARWIN) ? 16 : 8;
18265 info_ptr->save_size = RS6000_ALIGN (info_ptr->fp_size
18266 + info_ptr->gp_size
18267 + info_ptr->altivec_size
18268 + info_ptr->altivec_padding_size
18269 + info_ptr->spe_gp_size
18270 + info_ptr->spe_padding_size
18271 + ehrd_size
18272 + info_ptr->cr_size
18273 + info_ptr->vrsave_size,
18274 save_align);
18276 non_fixed_size = (info_ptr->vars_size
18277 + info_ptr->parm_size
18278 + info_ptr->save_size);
18280 info_ptr->total_size = RS6000_ALIGN (non_fixed_size + info_ptr->fixed_size,
18281 ABI_STACK_BOUNDARY / BITS_PER_UNIT);
18283 /* Determine if we need to save the link register. */
18284 if (info_ptr->calls_p
18285 || (DEFAULT_ABI == ABI_AIX
18286 && crtl->profile
18287 && !TARGET_PROFILE_KERNEL)
18288 || (DEFAULT_ABI == ABI_V4 && cfun->calls_alloca)
18289 #ifdef TARGET_RELOCATABLE
18290 || (TARGET_RELOCATABLE && (get_pool_size () != 0))
18291 #endif
18292 || rs6000_ra_ever_killed ())
18293 info_ptr->lr_save_p = 1;
18295 using_static_chain_p = (cfun->static_chain_decl != NULL_TREE
18296 && df_regs_ever_live_p (STATIC_CHAIN_REGNUM)
18297 && call_used_regs[STATIC_CHAIN_REGNUM]);
18298 info_ptr->savres_strategy = rs6000_savres_strategy (info_ptr,
18299 using_static_chain_p);
18301 if (!(info_ptr->savres_strategy & SAVE_INLINE_GPRS)
18302 || !(info_ptr->savres_strategy & SAVE_INLINE_FPRS)
18303 || !(info_ptr->savres_strategy & SAVE_INLINE_VRS)
18304 || !(info_ptr->savres_strategy & REST_INLINE_GPRS)
18305 || !(info_ptr->savres_strategy & REST_INLINE_FPRS)
18306 || !(info_ptr->savres_strategy & REST_INLINE_VRS))
18307 info_ptr->lr_save_p = 1;
18309 if (info_ptr->lr_save_p)
18310 df_set_regs_ever_live (LR_REGNO, true);
18312 /* Determine if we need to allocate any stack frame:
18314 For AIX we need to push the stack if a frame pointer is needed
18315 (because the stack might be dynamically adjusted), if we are
18316 debugging, if we make calls, or if the sum of fp_save, gp_save,
18317 and local variables are more than the space needed to save all
18318 non-volatile registers: 32-bit: 18*8 + 19*4 = 220 or 64-bit: 18*8
18319 + 18*8 = 288 (GPR13 reserved).
18321 For V.4 we don't have the stack cushion that AIX uses, but assume
18322 that the debugger can handle stackless frames. */
18324 if (info_ptr->calls_p)
18325 info_ptr->push_p = 1;
18327 else if (DEFAULT_ABI == ABI_V4)
18328 info_ptr->push_p = non_fixed_size != 0;
18330 else if (frame_pointer_needed)
18331 info_ptr->push_p = 1;
18333 else if (TARGET_XCOFF && write_symbols != NO_DEBUG)
18334 info_ptr->push_p = 1;
18336 else
18337 info_ptr->push_p = non_fixed_size > (TARGET_32BIT ? 220 : 288);
18339 /* Zero offsets if we're not saving those registers. */
18340 if (info_ptr->fp_size == 0)
18341 info_ptr->fp_save_offset = 0;
18343 if (info_ptr->gp_size == 0)
18344 info_ptr->gp_save_offset = 0;
18346 if (! TARGET_ALTIVEC_ABI || info_ptr->altivec_size == 0)
18347 info_ptr->altivec_save_offset = 0;
18349 /* Zero VRSAVE offset if not saved and restored. */
18350 if (! TARGET_ALTIVEC_VRSAVE || info_ptr->vrsave_mask == 0)
18351 info_ptr->vrsave_save_offset = 0;
18353 if (! TARGET_SPE_ABI
18354 || info_ptr->spe_64bit_regs_used == 0
18355 || info_ptr->spe_gp_size == 0)
18356 info_ptr->spe_gp_save_offset = 0;
18358 if (! info_ptr->lr_save_p)
18359 info_ptr->lr_save_offset = 0;
18361 if (! info_ptr->cr_save_p)
18362 info_ptr->cr_save_offset = 0;
18364 return info_ptr;
18367 /* Return true if the current function uses any GPRs in 64-bit SIMD
18368 mode. */
18370 static bool
18371 spe_func_has_64bit_regs_p (void)
18373 rtx insns, insn;
18375 /* Functions that save and restore all the call-saved registers will
18376 need to save/restore the registers in 64-bits. */
18377 if (crtl->calls_eh_return
18378 || cfun->calls_setjmp
18379 || crtl->has_nonlocal_goto)
18380 return true;
18382 insns = get_insns ();
18384 for (insn = NEXT_INSN (insns); insn != NULL_RTX; insn = NEXT_INSN (insn))
18386 if (INSN_P (insn))
18388 rtx i;
18390 /* FIXME: This should be implemented with attributes...
18392 (set_attr "spe64" "true")....then,
18393 if (get_spe64(insn)) return true;
18395 It's the only reliable way to do the stuff below. */
18397 i = PATTERN (insn);
18398 if (GET_CODE (i) == SET)
18400 enum machine_mode mode = GET_MODE (SET_SRC (i));
18402 if (SPE_VECTOR_MODE (mode))
18403 return true;
18404 if (TARGET_E500_DOUBLE && (mode == DFmode || mode == TFmode))
18405 return true;
18410 return false;
18413 static void
18414 debug_stack_info (rs6000_stack_t *info)
18416 const char *abi_string;
18418 if (! info)
18419 info = rs6000_stack_info ();
18421 fprintf (stderr, "\nStack information for function %s:\n",
18422 ((current_function_decl && DECL_NAME (current_function_decl))
18423 ? IDENTIFIER_POINTER (DECL_NAME (current_function_decl))
18424 : "<unknown>"));
18426 switch (info->abi)
18428 default: abi_string = "Unknown"; break;
18429 case ABI_NONE: abi_string = "NONE"; break;
18430 case ABI_AIX: abi_string = "AIX"; break;
18431 case ABI_DARWIN: abi_string = "Darwin"; break;
18432 case ABI_V4: abi_string = "V.4"; break;
18435 fprintf (stderr, "\tABI = %5s\n", abi_string);
18437 if (TARGET_ALTIVEC_ABI)
18438 fprintf (stderr, "\tALTIVEC ABI extensions enabled.\n");
18440 if (TARGET_SPE_ABI)
18441 fprintf (stderr, "\tSPE ABI extensions enabled.\n");
18443 if (info->first_gp_reg_save != 32)
18444 fprintf (stderr, "\tfirst_gp_reg_save = %5d\n", info->first_gp_reg_save);
18446 if (info->first_fp_reg_save != 64)
18447 fprintf (stderr, "\tfirst_fp_reg_save = %5d\n", info->first_fp_reg_save);
18449 if (info->first_altivec_reg_save <= LAST_ALTIVEC_REGNO)
18450 fprintf (stderr, "\tfirst_altivec_reg_save = %5d\n",
18451 info->first_altivec_reg_save);
18453 if (info->lr_save_p)
18454 fprintf (stderr, "\tlr_save_p = %5d\n", info->lr_save_p);
18456 if (info->cr_save_p)
18457 fprintf (stderr, "\tcr_save_p = %5d\n", info->cr_save_p);
18459 if (info->vrsave_mask)
18460 fprintf (stderr, "\tvrsave_mask = 0x%x\n", info->vrsave_mask);
18462 if (info->push_p)
18463 fprintf (stderr, "\tpush_p = %5d\n", info->push_p);
18465 if (info->calls_p)
18466 fprintf (stderr, "\tcalls_p = %5d\n", info->calls_p);
18468 if (info->gp_save_offset)
18469 fprintf (stderr, "\tgp_save_offset = %5d\n", info->gp_save_offset);
18471 if (info->fp_save_offset)
18472 fprintf (stderr, "\tfp_save_offset = %5d\n", info->fp_save_offset);
18474 if (info->altivec_save_offset)
18475 fprintf (stderr, "\taltivec_save_offset = %5d\n",
18476 info->altivec_save_offset);
18478 if (info->spe_gp_save_offset)
18479 fprintf (stderr, "\tspe_gp_save_offset = %5d\n",
18480 info->spe_gp_save_offset);
18482 if (info->vrsave_save_offset)
18483 fprintf (stderr, "\tvrsave_save_offset = %5d\n",
18484 info->vrsave_save_offset);
18486 if (info->lr_save_offset)
18487 fprintf (stderr, "\tlr_save_offset = %5d\n", info->lr_save_offset);
18489 if (info->cr_save_offset)
18490 fprintf (stderr, "\tcr_save_offset = %5d\n", info->cr_save_offset);
18492 if (info->varargs_save_offset)
18493 fprintf (stderr, "\tvarargs_save_offset = %5d\n", info->varargs_save_offset);
18495 if (info->total_size)
18496 fprintf (stderr, "\ttotal_size = "HOST_WIDE_INT_PRINT_DEC"\n",
18497 info->total_size);
18499 if (info->vars_size)
18500 fprintf (stderr, "\tvars_size = "HOST_WIDE_INT_PRINT_DEC"\n",
18501 info->vars_size);
18503 if (info->parm_size)
18504 fprintf (stderr, "\tparm_size = %5d\n", info->parm_size);
18506 if (info->fixed_size)
18507 fprintf (stderr, "\tfixed_size = %5d\n", info->fixed_size);
18509 if (info->gp_size)
18510 fprintf (stderr, "\tgp_size = %5d\n", info->gp_size);
18512 if (info->spe_gp_size)
18513 fprintf (stderr, "\tspe_gp_size = %5d\n", info->spe_gp_size);
18515 if (info->fp_size)
18516 fprintf (stderr, "\tfp_size = %5d\n", info->fp_size);
18518 if (info->altivec_size)
18519 fprintf (stderr, "\taltivec_size = %5d\n", info->altivec_size);
18521 if (info->vrsave_size)
18522 fprintf (stderr, "\tvrsave_size = %5d\n", info->vrsave_size);
18524 if (info->altivec_padding_size)
18525 fprintf (stderr, "\taltivec_padding_size= %5d\n",
18526 info->altivec_padding_size);
18528 if (info->spe_padding_size)
18529 fprintf (stderr, "\tspe_padding_size = %5d\n",
18530 info->spe_padding_size);
18532 if (info->cr_size)
18533 fprintf (stderr, "\tcr_size = %5d\n", info->cr_size);
18535 if (info->save_size)
18536 fprintf (stderr, "\tsave_size = %5d\n", info->save_size);
18538 if (info->reg_size != 4)
18539 fprintf (stderr, "\treg_size = %5d\n", info->reg_size);
18541 fprintf (stderr, "\tsave-strategy = %04x\n", info->savres_strategy);
18543 fprintf (stderr, "\n");
18547 rs6000_return_addr (int count, rtx frame)
18549 /* Currently we don't optimize very well between prolog and body
18550 code and for PIC code the code can be actually quite bad, so
18551 don't try to be too clever here. */
18552 if (count != 0 || (DEFAULT_ABI != ABI_AIX && flag_pic))
18554 cfun->machine->ra_needs_full_frame = 1;
18556 return
18557 gen_rtx_MEM
18558 (Pmode,
18559 memory_address
18560 (Pmode,
18561 plus_constant (Pmode,
18562 copy_to_reg
18563 (gen_rtx_MEM (Pmode,
18564 memory_address (Pmode, frame))),
18565 RETURN_ADDRESS_OFFSET)));
18568 cfun->machine->ra_need_lr = 1;
18569 return get_hard_reg_initial_val (Pmode, LR_REGNO);
18572 /* Say whether a function is a candidate for sibcall handling or not. */
18574 static bool
18575 rs6000_function_ok_for_sibcall (tree decl, tree exp)
18577 tree fntype;
18579 if (decl)
18580 fntype = TREE_TYPE (decl);
18581 else
18582 fntype = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (exp)));
18584 /* We can't do it if the called function has more vector parameters
18585 than the current function; there's nowhere to put the VRsave code. */
18586 if (TARGET_ALTIVEC_ABI
18587 && TARGET_ALTIVEC_VRSAVE
18588 && !(decl && decl == current_function_decl))
18590 function_args_iterator args_iter;
18591 tree type;
18592 int nvreg = 0;
18594 /* Functions with vector parameters are required to have a
18595 prototype, so the argument type info must be available
18596 here. */
18597 FOREACH_FUNCTION_ARGS(fntype, type, args_iter)
18598 if (TREE_CODE (type) == VECTOR_TYPE
18599 && ALTIVEC_OR_VSX_VECTOR_MODE (TYPE_MODE (type)))
18600 nvreg++;
18602 FOREACH_FUNCTION_ARGS(TREE_TYPE (current_function_decl), type, args_iter)
18603 if (TREE_CODE (type) == VECTOR_TYPE
18604 && ALTIVEC_OR_VSX_VECTOR_MODE (TYPE_MODE (type)))
18605 nvreg--;
18607 if (nvreg > 0)
18608 return false;
18611 /* Under the AIX ABI we can't allow calls to non-local functions,
18612 because the callee may have a different TOC pointer to the
18613 caller and there's no way to ensure we restore the TOC when we
18614 return. With the secure-plt SYSV ABI we can't make non-local
18615 calls when -fpic/PIC because the plt call stubs use r30. */
18616 if (DEFAULT_ABI == ABI_DARWIN
18617 || (DEFAULT_ABI == ABI_AIX
18618 && decl
18619 && !DECL_EXTERNAL (decl)
18620 && (*targetm.binds_local_p) (decl))
18621 || (DEFAULT_ABI == ABI_V4
18622 && (!TARGET_SECURE_PLT
18623 || !flag_pic
18624 || (decl
18625 && (*targetm.binds_local_p) (decl)))))
18627 tree attr_list = TYPE_ATTRIBUTES (fntype);
18629 if (!lookup_attribute ("longcall", attr_list)
18630 || lookup_attribute ("shortcall", attr_list))
18631 return true;
18634 return false;
18637 static int
18638 rs6000_ra_ever_killed (void)
18640 rtx top;
18641 rtx reg;
18642 rtx insn;
18644 if (cfun->is_thunk)
18645 return 0;
18647 if (cfun->machine->lr_save_state)
18648 return cfun->machine->lr_save_state - 1;
18650 /* regs_ever_live has LR marked as used if any sibcalls are present,
18651 but this should not force saving and restoring in the
18652 pro/epilogue. Likewise, reg_set_between_p thinks a sibcall
18653 clobbers LR, so that is inappropriate. */
18655 /* Also, the prologue can generate a store into LR that
18656 doesn't really count, like this:
18658 move LR->R0
18659 bcl to set PIC register
18660 move LR->R31
18661 move R0->LR
18663 When we're called from the epilogue, we need to avoid counting
18664 this as a store. */
18666 push_topmost_sequence ();
18667 top = get_insns ();
18668 pop_topmost_sequence ();
18669 reg = gen_rtx_REG (Pmode, LR_REGNO);
18671 for (insn = NEXT_INSN (top); insn != NULL_RTX; insn = NEXT_INSN (insn))
18673 if (INSN_P (insn))
18675 if (CALL_P (insn))
18677 if (!SIBLING_CALL_P (insn))
18678 return 1;
18680 else if (find_regno_note (insn, REG_INC, LR_REGNO))
18681 return 1;
18682 else if (set_of (reg, insn) != NULL_RTX
18683 && !prologue_epilogue_contains (insn))
18684 return 1;
18687 return 0;
18690 /* Emit instructions needed to load the TOC register.
18691 This is only needed when TARGET_TOC, TARGET_MINIMAL_TOC, and there is
18692 a constant pool; or for SVR4 -fpic. */
18694 void
18695 rs6000_emit_load_toc_table (int fromprolog)
18697 rtx dest;
18698 dest = gen_rtx_REG (Pmode, RS6000_PIC_OFFSET_TABLE_REGNUM);
18700 if (TARGET_ELF && TARGET_SECURE_PLT && DEFAULT_ABI != ABI_AIX && flag_pic)
18702 char buf[30];
18703 rtx lab, tmp1, tmp2, got;
18705 lab = gen_label_rtx ();
18706 ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (lab));
18707 lab = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
18708 if (flag_pic == 2)
18709 got = gen_rtx_SYMBOL_REF (Pmode, toc_label_name);
18710 else
18711 got = rs6000_got_sym ();
18712 tmp1 = tmp2 = dest;
18713 if (!fromprolog)
18715 tmp1 = gen_reg_rtx (Pmode);
18716 tmp2 = gen_reg_rtx (Pmode);
18718 emit_insn (gen_load_toc_v4_PIC_1 (lab));
18719 emit_move_insn (tmp1, gen_rtx_REG (Pmode, LR_REGNO));
18720 emit_insn (gen_load_toc_v4_PIC_3b (tmp2, tmp1, got, lab));
18721 emit_insn (gen_load_toc_v4_PIC_3c (dest, tmp2, got, lab));
18723 else if (TARGET_ELF && DEFAULT_ABI == ABI_V4 && flag_pic == 1)
18725 emit_insn (gen_load_toc_v4_pic_si ());
18726 emit_move_insn (dest, gen_rtx_REG (Pmode, LR_REGNO));
18728 else if (TARGET_ELF && DEFAULT_ABI != ABI_AIX && flag_pic == 2)
18730 char buf[30];
18731 rtx temp0 = (fromprolog
18732 ? gen_rtx_REG (Pmode, 0)
18733 : gen_reg_rtx (Pmode));
18735 if (fromprolog)
18737 rtx symF, symL;
18739 ASM_GENERATE_INTERNAL_LABEL (buf, "LCF", rs6000_pic_labelno);
18740 symF = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
18742 ASM_GENERATE_INTERNAL_LABEL (buf, "LCL", rs6000_pic_labelno);
18743 symL = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
18745 emit_insn (gen_load_toc_v4_PIC_1 (symF));
18746 emit_move_insn (dest, gen_rtx_REG (Pmode, LR_REGNO));
18747 emit_insn (gen_load_toc_v4_PIC_2 (temp0, dest, symL, symF));
18749 else
18751 rtx tocsym, lab;
18753 tocsym = gen_rtx_SYMBOL_REF (Pmode, toc_label_name);
18754 lab = gen_label_rtx ();
18755 emit_insn (gen_load_toc_v4_PIC_1b (tocsym, lab));
18756 emit_move_insn (dest, gen_rtx_REG (Pmode, LR_REGNO));
18757 if (TARGET_LINK_STACK)
18758 emit_insn (gen_addsi3 (dest, dest, GEN_INT (4)));
18759 emit_move_insn (temp0, gen_rtx_MEM (Pmode, dest));
18761 emit_insn (gen_addsi3 (dest, temp0, dest));
18763 else if (TARGET_ELF && !TARGET_AIX && flag_pic == 0 && TARGET_MINIMAL_TOC)
18765 /* This is for AIX code running in non-PIC ELF32. */
18766 char buf[30];
18767 rtx realsym;
18768 ASM_GENERATE_INTERNAL_LABEL (buf, "LCTOC", 1);
18769 realsym = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
18771 emit_insn (gen_elf_high (dest, realsym));
18772 emit_insn (gen_elf_low (dest, dest, realsym));
18774 else
18776 gcc_assert (DEFAULT_ABI == ABI_AIX);
18778 if (TARGET_32BIT)
18779 emit_insn (gen_load_toc_aix_si (dest));
18780 else
18781 emit_insn (gen_load_toc_aix_di (dest));
18785 /* Emit instructions to restore the link register after determining where
18786 its value has been stored. */
18788 void
18789 rs6000_emit_eh_reg_restore (rtx source, rtx scratch)
18791 rs6000_stack_t *info = rs6000_stack_info ();
18792 rtx operands[2];
18794 operands[0] = source;
18795 operands[1] = scratch;
18797 if (info->lr_save_p)
18799 rtx frame_rtx = stack_pointer_rtx;
18800 HOST_WIDE_INT sp_offset = 0;
18801 rtx tmp;
18803 if (frame_pointer_needed
18804 || cfun->calls_alloca
18805 || info->total_size > 32767)
18807 tmp = gen_frame_mem (Pmode, frame_rtx);
18808 emit_move_insn (operands[1], tmp);
18809 frame_rtx = operands[1];
18811 else if (info->push_p)
18812 sp_offset = info->total_size;
18814 tmp = plus_constant (Pmode, frame_rtx,
18815 info->lr_save_offset + sp_offset);
18816 tmp = gen_frame_mem (Pmode, tmp);
18817 emit_move_insn (tmp, operands[0]);
18819 else
18820 emit_move_insn (gen_rtx_REG (Pmode, LR_REGNO), operands[0]);
18822 /* Freeze lr_save_p. We've just emitted rtl that depends on the
18823 state of lr_save_p so any change from here on would be a bug. In
18824 particular, stop rs6000_ra_ever_killed from considering the SET
18825 of lr we may have added just above. */
18826 cfun->machine->lr_save_state = info->lr_save_p + 1;
18829 static GTY(()) alias_set_type set = -1;
18831 alias_set_type
18832 get_TOC_alias_set (void)
18834 if (set == -1)
18835 set = new_alias_set ();
18836 return set;
18839 /* This returns nonzero if the current function uses the TOC. This is
18840 determined by the presence of (use (unspec ... UNSPEC_TOC)), which
18841 is generated by the ABI_V4 load_toc_* patterns. */
18842 #if TARGET_ELF
18843 static int
18844 uses_TOC (void)
18846 rtx insn;
18848 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
18849 if (INSN_P (insn))
18851 rtx pat = PATTERN (insn);
18852 int i;
18854 if (GET_CODE (pat) == PARALLEL)
18855 for (i = 0; i < XVECLEN (pat, 0); i++)
18857 rtx sub = XVECEXP (pat, 0, i);
18858 if (GET_CODE (sub) == USE)
18860 sub = XEXP (sub, 0);
18861 if (GET_CODE (sub) == UNSPEC
18862 && XINT (sub, 1) == UNSPEC_TOC)
18863 return 1;
18867 return 0;
18869 #endif
18872 create_TOC_reference (rtx symbol, rtx largetoc_reg)
18874 rtx tocrel, tocreg, hi;
18876 if (TARGET_DEBUG_ADDR)
18878 if (GET_CODE (symbol) == SYMBOL_REF)
18879 fprintf (stderr, "\ncreate_TOC_reference, (symbol_ref %s)\n",
18880 XSTR (symbol, 0));
18881 else
18883 fprintf (stderr, "\ncreate_TOC_reference, code %s:\n",
18884 GET_RTX_NAME (GET_CODE (symbol)));
18885 debug_rtx (symbol);
18889 if (!can_create_pseudo_p ())
18890 df_set_regs_ever_live (TOC_REGISTER, true);
18892 tocreg = gen_rtx_REG (Pmode, TOC_REGISTER);
18893 tocrel = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, symbol, tocreg), UNSPEC_TOCREL);
18894 if (TARGET_CMODEL == CMODEL_SMALL || can_create_pseudo_p ())
18895 return tocrel;
18897 hi = gen_rtx_HIGH (Pmode, copy_rtx (tocrel));
18898 if (largetoc_reg != NULL)
18900 emit_move_insn (largetoc_reg, hi);
18901 hi = largetoc_reg;
18903 return gen_rtx_LO_SUM (Pmode, hi, tocrel);
18906 /* Issue assembly directives that create a reference to the given DWARF
18907 FRAME_TABLE_LABEL from the current function section. */
18908 void
18909 rs6000_aix_asm_output_dwarf_table_ref (char * frame_table_label)
18911 fprintf (asm_out_file, "\t.ref %s\n",
18912 (* targetm.strip_name_encoding) (frame_table_label));
18915 /* This ties together stack memory (MEM with an alias set of frame_alias_set)
18916 and the change to the stack pointer. */
18918 static void
18919 rs6000_emit_stack_tie (rtx fp, bool hard_frame_needed)
18921 rtvec p;
18922 int i;
18923 rtx regs[3];
18925 i = 0;
18926 regs[i++] = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
18927 if (hard_frame_needed)
18928 regs[i++] = gen_rtx_REG (Pmode, HARD_FRAME_POINTER_REGNUM);
18929 if (!(REGNO (fp) == STACK_POINTER_REGNUM
18930 || (hard_frame_needed
18931 && REGNO (fp) == HARD_FRAME_POINTER_REGNUM)))
18932 regs[i++] = fp;
18934 p = rtvec_alloc (i);
18935 while (--i >= 0)
18937 rtx mem = gen_frame_mem (BLKmode, regs[i]);
18938 RTVEC_ELT (p, i) = gen_rtx_SET (VOIDmode, mem, const0_rtx);
18941 emit_insn (gen_stack_tie (gen_rtx_PARALLEL (VOIDmode, p)));
18944 /* Emit the correct code for allocating stack space, as insns.
18945 If COPY_REG, make sure a copy of the old frame is left there.
18946 The generated code may use hard register 0 as a temporary. */
18948 static void
18949 rs6000_emit_allocate_stack (HOST_WIDE_INT size, rtx copy_reg, int copy_off)
18951 rtx insn;
18952 rtx stack_reg = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
18953 rtx tmp_reg = gen_rtx_REG (Pmode, 0);
18954 rtx todec = gen_int_mode (-size, Pmode);
18955 rtx par, set, mem;
18957 if (INTVAL (todec) != -size)
18959 warning (0, "stack frame too large");
18960 emit_insn (gen_trap ());
18961 return;
18964 if (crtl->limit_stack)
18966 if (REG_P (stack_limit_rtx)
18967 && REGNO (stack_limit_rtx) > 1
18968 && REGNO (stack_limit_rtx) <= 31)
18970 emit_insn (gen_add3_insn (tmp_reg, stack_limit_rtx, GEN_INT (size)));
18971 emit_insn (gen_cond_trap (LTU, stack_reg, tmp_reg,
18972 const0_rtx));
18974 else if (GET_CODE (stack_limit_rtx) == SYMBOL_REF
18975 && TARGET_32BIT
18976 && DEFAULT_ABI == ABI_V4)
18978 rtx toload = gen_rtx_CONST (VOIDmode,
18979 gen_rtx_PLUS (Pmode,
18980 stack_limit_rtx,
18981 GEN_INT (size)));
18983 emit_insn (gen_elf_high (tmp_reg, toload));
18984 emit_insn (gen_elf_low (tmp_reg, tmp_reg, toload));
18985 emit_insn (gen_cond_trap (LTU, stack_reg, tmp_reg,
18986 const0_rtx));
18988 else
18989 warning (0, "stack limit expression is not supported");
18992 if (copy_reg)
18994 if (copy_off != 0)
18995 emit_insn (gen_add3_insn (copy_reg, stack_reg, GEN_INT (copy_off)));
18996 else
18997 emit_move_insn (copy_reg, stack_reg);
19000 if (size > 32767)
19002 /* Need a note here so that try_split doesn't get confused. */
19003 if (get_last_insn () == NULL_RTX)
19004 emit_note (NOTE_INSN_DELETED);
19005 insn = emit_move_insn (tmp_reg, todec);
19006 try_split (PATTERN (insn), insn, 0);
19007 todec = tmp_reg;
19010 insn = emit_insn (TARGET_32BIT
19011 ? gen_movsi_update_stack (stack_reg, stack_reg,
19012 todec, stack_reg)
19013 : gen_movdi_di_update_stack (stack_reg, stack_reg,
19014 todec, stack_reg));
19015 /* Since we didn't use gen_frame_mem to generate the MEM, grab
19016 it now and set the alias set/attributes. The above gen_*_update
19017 calls will generate a PARALLEL with the MEM set being the first
19018 operation. */
19019 par = PATTERN (insn);
19020 gcc_assert (GET_CODE (par) == PARALLEL);
19021 set = XVECEXP (par, 0, 0);
19022 gcc_assert (GET_CODE (set) == SET);
19023 mem = SET_DEST (set);
19024 gcc_assert (MEM_P (mem));
19025 MEM_NOTRAP_P (mem) = 1;
19026 set_mem_alias_set (mem, get_frame_alias_set ());
19028 RTX_FRAME_RELATED_P (insn) = 1;
19029 add_reg_note (insn, REG_FRAME_RELATED_EXPR,
19030 gen_rtx_SET (VOIDmode, stack_reg,
19031 gen_rtx_PLUS (Pmode, stack_reg,
19032 GEN_INT (-size))));
19035 #define PROBE_INTERVAL (1 << STACK_CHECK_PROBE_INTERVAL_EXP)
19037 #if PROBE_INTERVAL > 32768
19038 #error Cannot use indexed addressing mode for stack probing
19039 #endif
19041 /* Emit code to probe a range of stack addresses from FIRST to FIRST+SIZE,
19042 inclusive. These are offsets from the current stack pointer. */
19044 static void
19045 rs6000_emit_probe_stack_range (HOST_WIDE_INT first, HOST_WIDE_INT size)
19047 /* See if we have a constant small number of probes to generate. If so,
19048 that's the easy case. */
19049 if (first + size <= 32768)
19051 HOST_WIDE_INT i;
19053 /* Probe at FIRST + N * PROBE_INTERVAL for values of N from 1 until
19054 it exceeds SIZE. If only one probe is needed, this will not
19055 generate any code. Then probe at FIRST + SIZE. */
19056 for (i = PROBE_INTERVAL; i < size; i += PROBE_INTERVAL)
19057 emit_stack_probe (plus_constant (Pmode, stack_pointer_rtx,
19058 -(first + i)));
19060 emit_stack_probe (plus_constant (Pmode, stack_pointer_rtx,
19061 -(first + size)));
19064 /* Otherwise, do the same as above, but in a loop. Note that we must be
19065 extra careful with variables wrapping around because we might be at
19066 the very top (or the very bottom) of the address space and we have
19067 to be able to handle this case properly; in particular, we use an
19068 equality test for the loop condition. */
19069 else
19071 HOST_WIDE_INT rounded_size;
19072 rtx r12 = gen_rtx_REG (Pmode, 12);
19073 rtx r0 = gen_rtx_REG (Pmode, 0);
19075 /* Sanity check for the addressing mode we're going to use. */
19076 gcc_assert (first <= 32768);
19078 /* Step 1: round SIZE to the previous multiple of the interval. */
19080 rounded_size = size & -PROBE_INTERVAL;
19083 /* Step 2: compute initial and final value of the loop counter. */
19085 /* TEST_ADDR = SP + FIRST. */
19086 emit_insn (gen_rtx_SET (VOIDmode, r12,
19087 plus_constant (Pmode, stack_pointer_rtx,
19088 -first)));
19090 /* LAST_ADDR = SP + FIRST + ROUNDED_SIZE. */
19091 if (rounded_size > 32768)
19093 emit_move_insn (r0, GEN_INT (-rounded_size));
19094 emit_insn (gen_rtx_SET (VOIDmode, r0,
19095 gen_rtx_PLUS (Pmode, r12, r0)));
19097 else
19098 emit_insn (gen_rtx_SET (VOIDmode, r0,
19099 plus_constant (Pmode, r12, -rounded_size)));
19102 /* Step 3: the loop
19104 while (TEST_ADDR != LAST_ADDR)
19106 TEST_ADDR = TEST_ADDR + PROBE_INTERVAL
19107 probe at TEST_ADDR
19110 probes at FIRST + N * PROBE_INTERVAL for values of N from 1
19111 until it is equal to ROUNDED_SIZE. */
19113 if (TARGET_64BIT)
19114 emit_insn (gen_probe_stack_rangedi (r12, r12, r0));
19115 else
19116 emit_insn (gen_probe_stack_rangesi (r12, r12, r0));
19119 /* Step 4: probe at FIRST + SIZE if we cannot assert at compile-time
19120 that SIZE is equal to ROUNDED_SIZE. */
19122 if (size != rounded_size)
19123 emit_stack_probe (plus_constant (Pmode, r12, rounded_size - size));
19127 /* Probe a range of stack addresses from REG1 to REG2 inclusive. These are
19128 absolute addresses. */
19130 const char *
19131 output_probe_stack_range (rtx reg1, rtx reg2)
19133 static int labelno = 0;
19134 char loop_lab[32], end_lab[32];
19135 rtx xops[2];
19137 ASM_GENERATE_INTERNAL_LABEL (loop_lab, "LPSRL", labelno);
19138 ASM_GENERATE_INTERNAL_LABEL (end_lab, "LPSRE", labelno++);
19140 ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, loop_lab);
19142 /* Jump to END_LAB if TEST_ADDR == LAST_ADDR. */
19143 xops[0] = reg1;
19144 xops[1] = reg2;
19145 if (TARGET_64BIT)
19146 output_asm_insn ("cmpd 0,%0,%1", xops);
19147 else
19148 output_asm_insn ("cmpw 0,%0,%1", xops);
19150 fputs ("\tbeq 0,", asm_out_file);
19151 assemble_name_raw (asm_out_file, end_lab);
19152 fputc ('\n', asm_out_file);
19154 /* TEST_ADDR = TEST_ADDR + PROBE_INTERVAL. */
19155 xops[1] = GEN_INT (-PROBE_INTERVAL);
19156 output_asm_insn ("addi %0,%0,%1", xops);
19158 /* Probe at TEST_ADDR and branch. */
19159 xops[1] = gen_rtx_REG (Pmode, 0);
19160 output_asm_insn ("stw %1,0(%0)", xops);
19161 fprintf (asm_out_file, "\tb ");
19162 assemble_name_raw (asm_out_file, loop_lab);
19163 fputc ('\n', asm_out_file);
19165 ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, end_lab);
19167 return "";
19170 /* Add to 'insn' a note which is PATTERN (INSN) but with REG replaced
19171 with (plus:P (reg 1) VAL), and with REG2 replaced with RREG if REG2
19172 is not NULL. It would be nice if dwarf2out_frame_debug_expr could
19173 deduce these equivalences by itself so it wasn't necessary to hold
19174 its hand so much. Don't be tempted to always supply d2_f_d_e with
19175 the actual cfa register, ie. r31 when we are using a hard frame
19176 pointer. That fails when saving regs off r1, and sched moves the
19177 r31 setup past the reg saves. */
19179 static rtx
19180 rs6000_frame_related (rtx insn, rtx reg, HOST_WIDE_INT val,
19181 rtx reg2, rtx rreg)
19183 rtx real, temp;
19185 if (REGNO (reg) == STACK_POINTER_REGNUM && reg2 == NULL_RTX)
19187 /* No need for any replacement. Just set RTX_FRAME_RELATED_P. */
19188 int i;
19190 gcc_checking_assert (val == 0);
19191 real = PATTERN (insn);
19192 if (GET_CODE (real) == PARALLEL)
19193 for (i = 0; i < XVECLEN (real, 0); i++)
19194 if (GET_CODE (XVECEXP (real, 0, i)) == SET)
19196 rtx set = XVECEXP (real, 0, i);
19198 RTX_FRAME_RELATED_P (set) = 1;
19200 RTX_FRAME_RELATED_P (insn) = 1;
19201 return insn;
19204 /* copy_rtx will not make unique copies of registers, so we need to
19205 ensure we don't have unwanted sharing here. */
19206 if (reg == reg2)
19207 reg = gen_raw_REG (GET_MODE (reg), REGNO (reg));
19209 if (reg == rreg)
19210 reg = gen_raw_REG (GET_MODE (reg), REGNO (reg));
19212 real = copy_rtx (PATTERN (insn));
19214 if (reg2 != NULL_RTX)
19215 real = replace_rtx (real, reg2, rreg);
19217 if (REGNO (reg) == STACK_POINTER_REGNUM)
19218 gcc_checking_assert (val == 0);
19219 else
19220 real = replace_rtx (real, reg,
19221 gen_rtx_PLUS (Pmode, gen_rtx_REG (Pmode,
19222 STACK_POINTER_REGNUM),
19223 GEN_INT (val)));
19225 /* We expect that 'real' is either a SET or a PARALLEL containing
19226 SETs (and possibly other stuff). In a PARALLEL, all the SETs
19227 are important so they all have to be marked RTX_FRAME_RELATED_P. */
19229 if (GET_CODE (real) == SET)
19231 rtx set = real;
19233 temp = simplify_rtx (SET_SRC (set));
19234 if (temp)
19235 SET_SRC (set) = temp;
19236 temp = simplify_rtx (SET_DEST (set));
19237 if (temp)
19238 SET_DEST (set) = temp;
19239 if (GET_CODE (SET_DEST (set)) == MEM)
19241 temp = simplify_rtx (XEXP (SET_DEST (set), 0));
19242 if (temp)
19243 XEXP (SET_DEST (set), 0) = temp;
19246 else
19248 int i;
19250 gcc_assert (GET_CODE (real) == PARALLEL);
19251 for (i = 0; i < XVECLEN (real, 0); i++)
19252 if (GET_CODE (XVECEXP (real, 0, i)) == SET)
19254 rtx set = XVECEXP (real, 0, i);
19256 temp = simplify_rtx (SET_SRC (set));
19257 if (temp)
19258 SET_SRC (set) = temp;
19259 temp = simplify_rtx (SET_DEST (set));
19260 if (temp)
19261 SET_DEST (set) = temp;
19262 if (GET_CODE (SET_DEST (set)) == MEM)
19264 temp = simplify_rtx (XEXP (SET_DEST (set), 0));
19265 if (temp)
19266 XEXP (SET_DEST (set), 0) = temp;
19268 RTX_FRAME_RELATED_P (set) = 1;
19272 RTX_FRAME_RELATED_P (insn) = 1;
19273 add_reg_note (insn, REG_FRAME_RELATED_EXPR, real);
19275 return insn;
19278 /* Returns an insn that has a vrsave set operation with the
19279 appropriate CLOBBERs. */
19281 static rtx
19282 generate_set_vrsave (rtx reg, rs6000_stack_t *info, int epiloguep)
19284 int nclobs, i;
19285 rtx insn, clobs[TOTAL_ALTIVEC_REGS + 1];
19286 rtx vrsave = gen_rtx_REG (SImode, VRSAVE_REGNO);
19288 clobs[0]
19289 = gen_rtx_SET (VOIDmode,
19290 vrsave,
19291 gen_rtx_UNSPEC_VOLATILE (SImode,
19292 gen_rtvec (2, reg, vrsave),
19293 UNSPECV_SET_VRSAVE));
19295 nclobs = 1;
19297 /* We need to clobber the registers in the mask so the scheduler
19298 does not move sets to VRSAVE before sets of AltiVec registers.
19300 However, if the function receives nonlocal gotos, reload will set
19301 all call saved registers live. We will end up with:
19303 (set (reg 999) (mem))
19304 (parallel [ (set (reg vrsave) (unspec blah))
19305 (clobber (reg 999))])
19307 The clobber will cause the store into reg 999 to be dead, and
19308 flow will attempt to delete an epilogue insn. In this case, we
19309 need an unspec use/set of the register. */
19311 for (i = FIRST_ALTIVEC_REGNO; i <= LAST_ALTIVEC_REGNO; ++i)
19312 if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
19314 if (!epiloguep || call_used_regs [i])
19315 clobs[nclobs++] = gen_rtx_CLOBBER (VOIDmode,
19316 gen_rtx_REG (V4SImode, i));
19317 else
19319 rtx reg = gen_rtx_REG (V4SImode, i);
19321 clobs[nclobs++]
19322 = gen_rtx_SET (VOIDmode,
19323 reg,
19324 gen_rtx_UNSPEC (V4SImode,
19325 gen_rtvec (1, reg), 27));
19329 insn = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (nclobs));
19331 for (i = 0; i < nclobs; ++i)
19332 XVECEXP (insn, 0, i) = clobs[i];
19334 return insn;
19337 static rtx
19338 gen_frame_set (rtx reg, rtx frame_reg, int offset, bool store)
19340 rtx addr, mem;
19342 addr = gen_rtx_PLUS (Pmode, frame_reg, GEN_INT (offset));
19343 mem = gen_frame_mem (GET_MODE (reg), addr);
19344 return gen_rtx_SET (VOIDmode, store ? mem : reg, store ? reg : mem);
19347 static rtx
19348 gen_frame_load (rtx reg, rtx frame_reg, int offset)
19350 return gen_frame_set (reg, frame_reg, offset, false);
19353 static rtx
19354 gen_frame_store (rtx reg, rtx frame_reg, int offset)
19356 return gen_frame_set (reg, frame_reg, offset, true);
19359 /* Save a register into the frame, and emit RTX_FRAME_RELATED_P notes.
19360 Save REGNO into [FRAME_REG + OFFSET] in mode MODE. */
19362 static rtx
19363 emit_frame_save (rtx frame_reg, enum machine_mode mode,
19364 unsigned int regno, int offset, HOST_WIDE_INT frame_reg_to_sp)
19366 rtx reg, insn;
19368 /* Some cases that need register indexed addressing. */
19369 gcc_checking_assert (!((TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (mode))
19370 || (TARGET_VSX && ALTIVEC_OR_VSX_VECTOR_MODE (mode))
19371 || (TARGET_E500_DOUBLE && mode == DFmode)
19372 || (TARGET_SPE_ABI
19373 && SPE_VECTOR_MODE (mode)
19374 && !SPE_CONST_OFFSET_OK (offset))));
19376 reg = gen_rtx_REG (mode, regno);
19377 insn = emit_insn (gen_frame_store (reg, frame_reg, offset));
19378 return rs6000_frame_related (insn, frame_reg, frame_reg_to_sp,
19379 NULL_RTX, NULL_RTX);
19382 /* Emit an offset memory reference suitable for a frame store, while
19383 converting to a valid addressing mode. */
19385 static rtx
19386 gen_frame_mem_offset (enum machine_mode mode, rtx reg, int offset)
19388 rtx int_rtx, offset_rtx;
19390 int_rtx = GEN_INT (offset);
19392 if ((TARGET_SPE_ABI && SPE_VECTOR_MODE (mode) && !SPE_CONST_OFFSET_OK (offset))
19393 || (TARGET_E500_DOUBLE && mode == DFmode))
19395 offset_rtx = gen_rtx_REG (Pmode, FIXED_SCRATCH);
19396 emit_move_insn (offset_rtx, int_rtx);
19398 else
19399 offset_rtx = int_rtx;
19401 return gen_frame_mem (mode, gen_rtx_PLUS (Pmode, reg, offset_rtx));
19404 #ifndef TARGET_FIX_AND_CONTINUE
19405 #define TARGET_FIX_AND_CONTINUE 0
19406 #endif
19408 /* It's really GPR 13 or 14, FPR 14 and VR 20. We need the smallest. */
19409 #define FIRST_SAVRES_REGISTER FIRST_SAVED_GP_REGNO
19410 #define LAST_SAVRES_REGISTER 31
19411 #define N_SAVRES_REGISTERS (LAST_SAVRES_REGISTER - FIRST_SAVRES_REGISTER + 1)
19413 enum {
19414 SAVRES_LR = 0x1,
19415 SAVRES_SAVE = 0x2,
19416 SAVRES_REG = 0x0c,
19417 SAVRES_GPR = 0,
19418 SAVRES_FPR = 4,
19419 SAVRES_VR = 8
19422 static GTY(()) rtx savres_routine_syms[N_SAVRES_REGISTERS][12];
19424 /* Temporary holding space for an out-of-line register save/restore
19425 routine name. */
19426 static char savres_routine_name[30];
19428 /* Return the name for an out-of-line register save/restore routine.
19429 We are saving/restoring GPRs if GPR is true. */
19431 static char *
19432 rs6000_savres_routine_name (rs6000_stack_t *info, int regno, int sel)
19434 const char *prefix = "";
19435 const char *suffix = "";
19437 /* Different targets are supposed to define
19438 {SAVE,RESTORE}_FP_{PREFIX,SUFFIX} with the idea that the needed
19439 routine name could be defined with:
19441 sprintf (name, "%s%d%s", SAVE_FP_PREFIX, regno, SAVE_FP_SUFFIX)
19443 This is a nice idea in practice, but in reality, things are
19444 complicated in several ways:
19446 - ELF targets have save/restore routines for GPRs.
19448 - SPE targets use different prefixes for 32/64-bit registers, and
19449 neither of them fit neatly in the FOO_{PREFIX,SUFFIX} regimen.
19451 - PPC64 ELF targets have routines for save/restore of GPRs that
19452 differ in what they do with the link register, so having a set
19453 prefix doesn't work. (We only use one of the save routines at
19454 the moment, though.)
19456 - PPC32 elf targets have "exit" versions of the restore routines
19457 that restore the link register and can save some extra space.
19458 These require an extra suffix. (There are also "tail" versions
19459 of the restore routines and "GOT" versions of the save routines,
19460 but we don't generate those at present. Same problems apply,
19461 though.)
19463 We deal with all this by synthesizing our own prefix/suffix and
19464 using that for the simple sprintf call shown above. */
19465 if (TARGET_SPE)
19467 /* No floating point saves on the SPE. */
19468 gcc_assert ((sel & SAVRES_REG) == SAVRES_GPR);
19470 if ((sel & SAVRES_SAVE))
19471 prefix = info->spe_64bit_regs_used ? "_save64gpr_" : "_save32gpr_";
19472 else
19473 prefix = info->spe_64bit_regs_used ? "_rest64gpr_" : "_rest32gpr_";
19475 if ((sel & SAVRES_LR))
19476 suffix = "_x";
19478 else if (DEFAULT_ABI == ABI_V4)
19480 if (TARGET_64BIT)
19481 goto aix_names;
19483 if ((sel & SAVRES_REG) == SAVRES_GPR)
19484 prefix = (sel & SAVRES_SAVE) ? "_savegpr_" : "_restgpr_";
19485 else if ((sel & SAVRES_REG) == SAVRES_FPR)
19486 prefix = (sel & SAVRES_SAVE) ? "_savefpr_" : "_restfpr_";
19487 else if ((sel & SAVRES_REG) == SAVRES_VR)
19488 prefix = (sel & SAVRES_SAVE) ? "_savevr_" : "_restvr_";
19489 else
19490 abort ();
19492 if ((sel & SAVRES_LR))
19493 suffix = "_x";
19495 else if (DEFAULT_ABI == ABI_AIX)
19497 #if !defined (POWERPC_LINUX) && !defined (POWERPC_FREEBSD)
19498 /* No out-of-line save/restore routines for GPRs on AIX. */
19499 gcc_assert (!TARGET_AIX || (sel & SAVRES_REG) != SAVRES_GPR);
19500 #endif
19502 aix_names:
19503 if ((sel & SAVRES_REG) == SAVRES_GPR)
19504 prefix = ((sel & SAVRES_SAVE)
19505 ? ((sel & SAVRES_LR) ? "_savegpr0_" : "_savegpr1_")
19506 : ((sel & SAVRES_LR) ? "_restgpr0_" : "_restgpr1_"));
19507 else if ((sel & SAVRES_REG) == SAVRES_FPR)
19509 #if defined (POWERPC_LINUX) || defined (POWERPC_FREEBSD)
19510 if ((sel & SAVRES_LR))
19511 prefix = ((sel & SAVRES_SAVE) ? "_savefpr_" : "_restfpr_");
19512 else
19513 #endif
19515 prefix = (sel & SAVRES_SAVE) ? SAVE_FP_PREFIX : RESTORE_FP_PREFIX;
19516 suffix = (sel & SAVRES_SAVE) ? SAVE_FP_SUFFIX : RESTORE_FP_SUFFIX;
19519 else if ((sel & SAVRES_REG) == SAVRES_VR)
19520 prefix = (sel & SAVRES_SAVE) ? "_savevr_" : "_restvr_";
19521 else
19522 abort ();
19525 if (DEFAULT_ABI == ABI_DARWIN)
19527 /* The Darwin approach is (slightly) different, in order to be
19528 compatible with code generated by the system toolchain. There is a
19529 single symbol for the start of save sequence, and the code here
19530 embeds an offset into that code on the basis of the first register
19531 to be saved. */
19532 prefix = (sel & SAVRES_SAVE) ? "save" : "rest" ;
19533 if ((sel & SAVRES_REG) == SAVRES_GPR)
19534 sprintf (savres_routine_name, "*%sGPR%s%s%.0d ; %s r%d-r31", prefix,
19535 ((sel & SAVRES_LR) ? "x" : ""), (regno == 13 ? "" : "+"),
19536 (regno - 13) * 4, prefix, regno);
19537 else if ((sel & SAVRES_REG) == SAVRES_FPR)
19538 sprintf (savres_routine_name, "*%sFP%s%.0d ; %s f%d-f31", prefix,
19539 (regno == 14 ? "" : "+"), (regno - 14) * 4, prefix, regno);
19540 else if ((sel & SAVRES_REG) == SAVRES_VR)
19541 sprintf (savres_routine_name, "*%sVEC%s%.0d ; %s v%d-v31", prefix,
19542 (regno == 20 ? "" : "+"), (regno - 20) * 8, prefix, regno);
19543 else
19544 abort ();
19546 else
19547 sprintf (savres_routine_name, "%s%d%s", prefix, regno, suffix);
19549 return savres_routine_name;
19552 /* Return an RTL SYMBOL_REF for an out-of-line register save/restore routine.
19553 We are saving/restoring GPRs if GPR is true. */
19555 static rtx
19556 rs6000_savres_routine_sym (rs6000_stack_t *info, int sel)
19558 int regno = ((sel & SAVRES_REG) == SAVRES_GPR
19559 ? info->first_gp_reg_save
19560 : (sel & SAVRES_REG) == SAVRES_FPR
19561 ? info->first_fp_reg_save - 32
19562 : (sel & SAVRES_REG) == SAVRES_VR
19563 ? info->first_altivec_reg_save - FIRST_ALTIVEC_REGNO
19564 : -1);
19565 rtx sym;
19566 int select = sel;
19568 /* On the SPE, we never have any FPRs, but we do have 32/64-bit
19569 versions of the gpr routines. */
19570 if (TARGET_SPE_ABI && (sel & SAVRES_REG) == SAVRES_GPR
19571 && info->spe_64bit_regs_used)
19572 select ^= SAVRES_FPR ^ SAVRES_GPR;
19574 /* Don't generate bogus routine names. */
19575 gcc_assert (FIRST_SAVRES_REGISTER <= regno
19576 && regno <= LAST_SAVRES_REGISTER
19577 && select >= 0 && select <= 12);
19579 sym = savres_routine_syms[regno-FIRST_SAVRES_REGISTER][select];
19581 if (sym == NULL)
19583 char *name;
19585 name = rs6000_savres_routine_name (info, regno, sel);
19587 sym = savres_routine_syms[regno-FIRST_SAVRES_REGISTER][select]
19588 = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (name));
19589 SYMBOL_REF_FLAGS (sym) |= SYMBOL_FLAG_FUNCTION;
19592 return sym;
19595 /* Emit a sequence of insns, including a stack tie if needed, for
19596 resetting the stack pointer. If UPDT_REGNO is not 1, then don't
19597 reset the stack pointer, but move the base of the frame into
19598 reg UPDT_REGNO for use by out-of-line register restore routines. */
19600 static rtx
19601 rs6000_emit_stack_reset (rs6000_stack_t *info,
19602 rtx frame_reg_rtx, HOST_WIDE_INT frame_off,
19603 unsigned updt_regno)
19605 rtx updt_reg_rtx;
19607 /* This blockage is needed so that sched doesn't decide to move
19608 the sp change before the register restores. */
19609 if (DEFAULT_ABI == ABI_V4
19610 || (TARGET_SPE_ABI
19611 && info->spe_64bit_regs_used != 0
19612 && info->first_gp_reg_save != 32))
19613 rs6000_emit_stack_tie (frame_reg_rtx, frame_pointer_needed);
19615 /* If we are restoring registers out-of-line, we will be using the
19616 "exit" variants of the restore routines, which will reset the
19617 stack for us. But we do need to point updt_reg into the
19618 right place for those routines. */
19619 updt_reg_rtx = gen_rtx_REG (Pmode, updt_regno);
19621 if (frame_off != 0)
19622 return emit_insn (gen_add3_insn (updt_reg_rtx,
19623 frame_reg_rtx, GEN_INT (frame_off)));
19624 else if (REGNO (frame_reg_rtx) != updt_regno)
19625 return emit_move_insn (updt_reg_rtx, frame_reg_rtx);
19627 return NULL_RTX;
19630 /* Return the register number used as a pointer by out-of-line
19631 save/restore functions. */
19633 static inline unsigned
19634 ptr_regno_for_savres (int sel)
19636 if (DEFAULT_ABI == ABI_AIX)
19637 return (sel & SAVRES_REG) == SAVRES_FPR || (sel & SAVRES_LR) ? 1 : 12;
19638 return DEFAULT_ABI == ABI_DARWIN && (sel & SAVRES_REG) == SAVRES_FPR ? 1 : 11;
19641 /* Construct a parallel rtx describing the effect of a call to an
19642 out-of-line register save/restore routine, and emit the insn
19643 or jump_insn as appropriate. */
19645 static rtx
19646 rs6000_emit_savres_rtx (rs6000_stack_t *info,
19647 rtx frame_reg_rtx, int save_area_offset, int lr_offset,
19648 enum machine_mode reg_mode, int sel)
19650 int i;
19651 int offset, start_reg, end_reg, n_regs, use_reg;
19652 int reg_size = GET_MODE_SIZE (reg_mode);
19653 rtx sym;
19654 rtvec p;
19655 rtx par, insn;
19657 offset = 0;
19658 start_reg = ((sel & SAVRES_REG) == SAVRES_GPR
19659 ? info->first_gp_reg_save
19660 : (sel & SAVRES_REG) == SAVRES_FPR
19661 ? info->first_fp_reg_save
19662 : (sel & SAVRES_REG) == SAVRES_VR
19663 ? info->first_altivec_reg_save
19664 : -1);
19665 end_reg = ((sel & SAVRES_REG) == SAVRES_GPR
19666 ? 32
19667 : (sel & SAVRES_REG) == SAVRES_FPR
19668 ? 64
19669 : (sel & SAVRES_REG) == SAVRES_VR
19670 ? LAST_ALTIVEC_REGNO + 1
19671 : -1);
19672 n_regs = end_reg - start_reg;
19673 p = rtvec_alloc (3 + ((sel & SAVRES_LR) ? 1 : 0)
19674 + ((sel & SAVRES_REG) == SAVRES_VR ? 1 : 0)
19675 + n_regs);
19677 if (!(sel & SAVRES_SAVE) && (sel & SAVRES_LR))
19678 RTVEC_ELT (p, offset++) = ret_rtx;
19680 RTVEC_ELT (p, offset++)
19681 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, LR_REGNO));
19683 sym = rs6000_savres_routine_sym (info, sel);
19684 RTVEC_ELT (p, offset++) = gen_rtx_USE (VOIDmode, sym);
19686 use_reg = ptr_regno_for_savres (sel);
19687 if ((sel & SAVRES_REG) == SAVRES_VR)
19689 /* Vector regs are saved/restored using [reg+reg] addressing. */
19690 RTVEC_ELT (p, offset++)
19691 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, use_reg));
19692 RTVEC_ELT (p, offset++)
19693 = gen_rtx_USE (VOIDmode, gen_rtx_REG (Pmode, 0));
19695 else
19696 RTVEC_ELT (p, offset++)
19697 = gen_rtx_USE (VOIDmode, gen_rtx_REG (Pmode, use_reg));
19699 for (i = 0; i < end_reg - start_reg; i++)
19700 RTVEC_ELT (p, i + offset)
19701 = gen_frame_set (gen_rtx_REG (reg_mode, start_reg + i),
19702 frame_reg_rtx, save_area_offset + reg_size * i,
19703 (sel & SAVRES_SAVE) != 0);
19705 if ((sel & SAVRES_SAVE) && (sel & SAVRES_LR))
19706 RTVEC_ELT (p, i + offset)
19707 = gen_frame_store (gen_rtx_REG (Pmode, 0), frame_reg_rtx, lr_offset);
19709 par = gen_rtx_PARALLEL (VOIDmode, p);
19711 if (!(sel & SAVRES_SAVE) && (sel & SAVRES_LR))
19713 insn = emit_jump_insn (par);
19714 JUMP_LABEL (insn) = ret_rtx;
19716 else
19717 insn = emit_insn (par);
19718 return insn;
19721 /* Determine whether the gp REG is really used. */
19723 static bool
19724 rs6000_reg_live_or_pic_offset_p (int reg)
19726 /* If the function calls eh_return, claim used all the registers that would
19727 be checked for liveness otherwise. This is required for the PIC offset
19728 register with -mminimal-toc on AIX, as it is advertised as "fixed" for
19729 register allocation purposes in this case. */
19731 return (((crtl->calls_eh_return || df_regs_ever_live_p (reg))
19732 && (!call_used_regs[reg]
19733 || (reg == RS6000_PIC_OFFSET_TABLE_REGNUM
19734 && !TARGET_SINGLE_PIC_BASE
19735 && TARGET_TOC && TARGET_MINIMAL_TOC)))
19736 || (reg == RS6000_PIC_OFFSET_TABLE_REGNUM
19737 && !TARGET_SINGLE_PIC_BASE
19738 && ((DEFAULT_ABI == ABI_V4 && flag_pic != 0)
19739 || (DEFAULT_ABI == ABI_DARWIN && flag_pic))));
19742 /* Emit function prologue as insns. */
19744 void
19745 rs6000_emit_prologue (void)
19747 rs6000_stack_t *info = rs6000_stack_info ();
19748 enum machine_mode reg_mode = Pmode;
19749 int reg_size = TARGET_32BIT ? 4 : 8;
19750 rtx sp_reg_rtx = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
19751 rtx frame_reg_rtx = sp_reg_rtx;
19752 unsigned int cr_save_regno;
19753 rtx cr_save_rtx = NULL_RTX;
19754 rtx insn;
19755 int strategy;
19756 int using_static_chain_p = (cfun->static_chain_decl != NULL_TREE
19757 && df_regs_ever_live_p (STATIC_CHAIN_REGNUM)
19758 && call_used_regs[STATIC_CHAIN_REGNUM]);
19759 /* Offset to top of frame for frame_reg and sp respectively. */
19760 HOST_WIDE_INT frame_off = 0;
19761 HOST_WIDE_INT sp_off = 0;
19763 #ifdef ENABLE_CHECKING
19764 /* Track and check usage of r0, r11, r12. */
19765 int reg_inuse = using_static_chain_p ? 1 << 11 : 0;
19766 #define START_USE(R) do \
19768 gcc_assert ((reg_inuse & (1 << (R))) == 0); \
19769 reg_inuse |= 1 << (R); \
19770 } while (0)
19771 #define END_USE(R) do \
19773 gcc_assert ((reg_inuse & (1 << (R))) != 0); \
19774 reg_inuse &= ~(1 << (R)); \
19775 } while (0)
19776 #define NOT_INUSE(R) do \
19778 gcc_assert ((reg_inuse & (1 << (R))) == 0); \
19779 } while (0)
19780 #else
19781 #define START_USE(R) do {} while (0)
19782 #define END_USE(R) do {} while (0)
19783 #define NOT_INUSE(R) do {} while (0)
19784 #endif
19786 if (flag_stack_usage_info)
19787 current_function_static_stack_size = info->total_size;
19789 if (flag_stack_check == STATIC_BUILTIN_STACK_CHECK && info->total_size)
19790 rs6000_emit_probe_stack_range (STACK_CHECK_PROTECT, info->total_size);
19792 if (TARGET_FIX_AND_CONTINUE)
19794 /* gdb on darwin arranges to forward a function from the old
19795 address by modifying the first 5 instructions of the function
19796 to branch to the overriding function. This is necessary to
19797 permit function pointers that point to the old function to
19798 actually forward to the new function. */
19799 emit_insn (gen_nop ());
19800 emit_insn (gen_nop ());
19801 emit_insn (gen_nop ());
19802 emit_insn (gen_nop ());
19803 emit_insn (gen_nop ());
19806 if (TARGET_SPE_ABI && info->spe_64bit_regs_used != 0)
19808 reg_mode = V2SImode;
19809 reg_size = 8;
19812 /* Handle world saves specially here. */
19813 if (WORLD_SAVE_P (info))
19815 int i, j, sz;
19816 rtx treg;
19817 rtvec p;
19818 rtx reg0;
19820 /* save_world expects lr in r0. */
19821 reg0 = gen_rtx_REG (Pmode, 0);
19822 if (info->lr_save_p)
19824 insn = emit_move_insn (reg0,
19825 gen_rtx_REG (Pmode, LR_REGNO));
19826 RTX_FRAME_RELATED_P (insn) = 1;
19829 /* The SAVE_WORLD and RESTORE_WORLD routines make a number of
19830 assumptions about the offsets of various bits of the stack
19831 frame. */
19832 gcc_assert (info->gp_save_offset == -220
19833 && info->fp_save_offset == -144
19834 && info->lr_save_offset == 8
19835 && info->cr_save_offset == 4
19836 && info->push_p
19837 && info->lr_save_p
19838 && (!crtl->calls_eh_return
19839 || info->ehrd_offset == -432)
19840 && info->vrsave_save_offset == -224
19841 && info->altivec_save_offset == -416);
19843 treg = gen_rtx_REG (SImode, 11);
19844 emit_move_insn (treg, GEN_INT (-info->total_size));
19846 /* SAVE_WORLD takes the caller's LR in R0 and the frame size
19847 in R11. It also clobbers R12, so beware! */
19849 /* Preserve CR2 for save_world prologues */
19850 sz = 5;
19851 sz += 32 - info->first_gp_reg_save;
19852 sz += 64 - info->first_fp_reg_save;
19853 sz += LAST_ALTIVEC_REGNO - info->first_altivec_reg_save + 1;
19854 p = rtvec_alloc (sz);
19855 j = 0;
19856 RTVEC_ELT (p, j++) = gen_rtx_CLOBBER (VOIDmode,
19857 gen_rtx_REG (SImode,
19858 LR_REGNO));
19859 RTVEC_ELT (p, j++) = gen_rtx_USE (VOIDmode,
19860 gen_rtx_SYMBOL_REF (Pmode,
19861 "*save_world"));
19862 /* We do floats first so that the instruction pattern matches
19863 properly. */
19864 for (i = 0; i < 64 - info->first_fp_reg_save; i++)
19865 RTVEC_ELT (p, j++)
19866 = gen_frame_store (gen_rtx_REG (TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT
19867 ? DFmode : SFmode,
19868 info->first_fp_reg_save + i),
19869 frame_reg_rtx,
19870 info->fp_save_offset + frame_off + 8 * i);
19871 for (i = 0; info->first_altivec_reg_save + i <= LAST_ALTIVEC_REGNO; i++)
19872 RTVEC_ELT (p, j++)
19873 = gen_frame_store (gen_rtx_REG (V4SImode,
19874 info->first_altivec_reg_save + i),
19875 frame_reg_rtx,
19876 info->altivec_save_offset + frame_off + 16 * i);
19877 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
19878 RTVEC_ELT (p, j++)
19879 = gen_frame_store (gen_rtx_REG (reg_mode, info->first_gp_reg_save + i),
19880 frame_reg_rtx,
19881 info->gp_save_offset + frame_off + reg_size * i);
19883 /* CR register traditionally saved as CR2. */
19884 RTVEC_ELT (p, j++)
19885 = gen_frame_store (gen_rtx_REG (SImode, CR2_REGNO),
19886 frame_reg_rtx, info->cr_save_offset + frame_off);
19887 /* Explain about use of R0. */
19888 if (info->lr_save_p)
19889 RTVEC_ELT (p, j++)
19890 = gen_frame_store (reg0,
19891 frame_reg_rtx, info->lr_save_offset + frame_off);
19892 /* Explain what happens to the stack pointer. */
19894 rtx newval = gen_rtx_PLUS (Pmode, sp_reg_rtx, treg);
19895 RTVEC_ELT (p, j++) = gen_rtx_SET (VOIDmode, sp_reg_rtx, newval);
19898 insn = emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
19899 rs6000_frame_related (insn, frame_reg_rtx, sp_off - frame_off,
19900 treg, GEN_INT (-info->total_size));
19901 sp_off = frame_off = info->total_size;
19904 strategy = info->savres_strategy;
19906 /* For V.4, update stack before we do any saving and set back pointer. */
19907 if (! WORLD_SAVE_P (info)
19908 && info->push_p
19909 && (DEFAULT_ABI == ABI_V4
19910 || crtl->calls_eh_return))
19912 bool need_r11 = (TARGET_SPE
19913 ? (!(strategy & SAVE_INLINE_GPRS)
19914 && info->spe_64bit_regs_used == 0)
19915 : (!(strategy & SAVE_INLINE_FPRS)
19916 || !(strategy & SAVE_INLINE_GPRS)
19917 || !(strategy & SAVE_INLINE_VRS)));
19918 int ptr_regno = -1;
19919 rtx ptr_reg = NULL_RTX;
19920 int ptr_off = 0;
19922 if (info->total_size < 32767)
19923 frame_off = info->total_size;
19924 else if (need_r11)
19925 ptr_regno = 11;
19926 else if (info->cr_save_p
19927 || info->lr_save_p
19928 || info->first_fp_reg_save < 64
19929 || info->first_gp_reg_save < 32
19930 || info->altivec_size != 0
19931 || info->vrsave_mask != 0
19932 || crtl->calls_eh_return)
19933 ptr_regno = 12;
19934 else
19936 /* The prologue won't be saving any regs so there is no need
19937 to set up a frame register to access any frame save area.
19938 We also won't be using frame_off anywhere below, but set
19939 the correct value anyway to protect against future
19940 changes to this function. */
19941 frame_off = info->total_size;
19943 if (ptr_regno != -1)
19945 /* Set up the frame offset to that needed by the first
19946 out-of-line save function. */
19947 START_USE (ptr_regno);
19948 ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
19949 frame_reg_rtx = ptr_reg;
19950 if (!(strategy & SAVE_INLINE_FPRS) && info->fp_size != 0)
19951 gcc_checking_assert (info->fp_save_offset + info->fp_size == 0);
19952 else if (!(strategy & SAVE_INLINE_GPRS) && info->first_gp_reg_save < 32)
19953 ptr_off = info->gp_save_offset + info->gp_size;
19954 else if (!(strategy & SAVE_INLINE_VRS) && info->altivec_size != 0)
19955 ptr_off = info->altivec_save_offset + info->altivec_size;
19956 frame_off = -ptr_off;
19958 rs6000_emit_allocate_stack (info->total_size, ptr_reg, ptr_off);
19959 sp_off = info->total_size;
19960 if (frame_reg_rtx != sp_reg_rtx)
19961 rs6000_emit_stack_tie (frame_reg_rtx, false);
19964 /* If we use the link register, get it into r0. */
19965 if (!WORLD_SAVE_P (info) && info->lr_save_p)
19967 rtx addr, reg, mem;
19969 reg = gen_rtx_REG (Pmode, 0);
19970 START_USE (0);
19971 insn = emit_move_insn (reg, gen_rtx_REG (Pmode, LR_REGNO));
19972 RTX_FRAME_RELATED_P (insn) = 1;
19974 if (!(strategy & (SAVE_NOINLINE_GPRS_SAVES_LR
19975 | SAVE_NOINLINE_FPRS_SAVES_LR)))
19977 addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
19978 GEN_INT (info->lr_save_offset + frame_off));
19979 mem = gen_rtx_MEM (Pmode, addr);
19980 /* This should not be of rs6000_sr_alias_set, because of
19981 __builtin_return_address. */
19983 insn = emit_move_insn (mem, reg);
19984 rs6000_frame_related (insn, frame_reg_rtx, sp_off - frame_off,
19985 NULL_RTX, NULL_RTX);
19986 END_USE (0);
19990 /* If we need to save CR, put it into r12 or r11. Choose r12 except when
19991 r12 will be needed by out-of-line gpr restore. */
19992 cr_save_regno = (DEFAULT_ABI == ABI_AIX
19993 && !(strategy & (SAVE_INLINE_GPRS
19994 | SAVE_NOINLINE_GPRS_SAVES_LR))
19995 ? 11 : 12);
19996 if (!WORLD_SAVE_P (info)
19997 && info->cr_save_p
19998 && REGNO (frame_reg_rtx) != cr_save_regno
19999 && !(using_static_chain_p && cr_save_regno == 11))
20001 rtx set;
20003 cr_save_rtx = gen_rtx_REG (SImode, cr_save_regno);
20004 START_USE (cr_save_regno);
20005 insn = emit_insn (gen_movesi_from_cr (cr_save_rtx));
20006 RTX_FRAME_RELATED_P (insn) = 1;
20007 /* Now, there's no way that dwarf2out_frame_debug_expr is going
20008 to understand '(unspec:SI [(reg:CC 68) ...] UNSPEC_MOVESI_FROM_CR)'.
20009 But that's OK. All we have to do is specify that _one_ condition
20010 code register is saved in this stack slot. The thrower's epilogue
20011 will then restore all the call-saved registers.
20012 We use CR2_REGNO (70) to be compatible with gcc-2.95 on Linux. */
20013 set = gen_rtx_SET (VOIDmode, cr_save_rtx,
20014 gen_rtx_REG (SImode, CR2_REGNO));
20015 add_reg_note (insn, REG_FRAME_RELATED_EXPR, set);
20018 /* Do any required saving of fpr's. If only one or two to save, do
20019 it ourselves. Otherwise, call function. */
20020 if (!WORLD_SAVE_P (info) && (strategy & SAVE_INLINE_FPRS))
20022 int i;
20023 for (i = 0; i < 64 - info->first_fp_reg_save; i++)
20024 if (save_reg_p (info->first_fp_reg_save + i))
20025 emit_frame_save (frame_reg_rtx,
20026 (TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT
20027 ? DFmode : SFmode),
20028 info->first_fp_reg_save + i,
20029 info->fp_save_offset + frame_off + 8 * i,
20030 sp_off - frame_off);
20032 else if (!WORLD_SAVE_P (info) && info->first_fp_reg_save != 64)
20034 bool lr = (strategy & SAVE_NOINLINE_FPRS_SAVES_LR) != 0;
20035 int sel = SAVRES_SAVE | SAVRES_FPR | (lr ? SAVRES_LR : 0);
20036 unsigned ptr_regno = ptr_regno_for_savres (sel);
20037 rtx ptr_reg = frame_reg_rtx;
20039 if (REGNO (frame_reg_rtx) == ptr_regno)
20040 gcc_checking_assert (frame_off == 0);
20041 else
20043 ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
20044 NOT_INUSE (ptr_regno);
20045 emit_insn (gen_add3_insn (ptr_reg,
20046 frame_reg_rtx, GEN_INT (frame_off)));
20048 insn = rs6000_emit_savres_rtx (info, ptr_reg,
20049 info->fp_save_offset,
20050 info->lr_save_offset,
20051 DFmode, sel);
20052 rs6000_frame_related (insn, ptr_reg, sp_off,
20053 NULL_RTX, NULL_RTX);
20054 if (lr)
20055 END_USE (0);
20058 /* Save GPRs. This is done as a PARALLEL if we are using
20059 the store-multiple instructions. */
20060 if (!WORLD_SAVE_P (info)
20061 && TARGET_SPE_ABI
20062 && info->spe_64bit_regs_used != 0
20063 && info->first_gp_reg_save != 32)
20065 int i;
20066 rtx spe_save_area_ptr;
20067 HOST_WIDE_INT save_off;
20068 int ool_adjust = 0;
20070 /* Determine whether we can address all of the registers that need
20071 to be saved with an offset from frame_reg_rtx that fits in
20072 the small const field for SPE memory instructions. */
20073 int spe_regs_addressable
20074 = (SPE_CONST_OFFSET_OK (info->spe_gp_save_offset + frame_off
20075 + reg_size * (32 - info->first_gp_reg_save - 1))
20076 && (strategy & SAVE_INLINE_GPRS));
20078 if (spe_regs_addressable)
20080 spe_save_area_ptr = frame_reg_rtx;
20081 save_off = frame_off;
20083 else
20085 /* Make r11 point to the start of the SPE save area. We need
20086 to be careful here if r11 is holding the static chain. If
20087 it is, then temporarily save it in r0. */
20088 HOST_WIDE_INT offset;
20090 if (!(strategy & SAVE_INLINE_GPRS))
20091 ool_adjust = 8 * (info->first_gp_reg_save
20092 - (FIRST_SAVRES_REGISTER + 1));
20093 offset = info->spe_gp_save_offset + frame_off - ool_adjust;
20094 spe_save_area_ptr = gen_rtx_REG (Pmode, 11);
20095 save_off = frame_off - offset;
20097 if (using_static_chain_p)
20099 rtx r0 = gen_rtx_REG (Pmode, 0);
20101 START_USE (0);
20102 gcc_assert (info->first_gp_reg_save > 11);
20104 emit_move_insn (r0, spe_save_area_ptr);
20106 else if (REGNO (frame_reg_rtx) != 11)
20107 START_USE (11);
20109 emit_insn (gen_addsi3 (spe_save_area_ptr,
20110 frame_reg_rtx, GEN_INT (offset)));
20111 if (!using_static_chain_p && REGNO (frame_reg_rtx) == 11)
20112 frame_off = -info->spe_gp_save_offset + ool_adjust;
20115 if ((strategy & SAVE_INLINE_GPRS))
20117 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
20118 if (rs6000_reg_live_or_pic_offset_p (info->first_gp_reg_save + i))
20119 emit_frame_save (spe_save_area_ptr, reg_mode,
20120 info->first_gp_reg_save + i,
20121 (info->spe_gp_save_offset + save_off
20122 + reg_size * i),
20123 sp_off - save_off);
20125 else
20127 insn = rs6000_emit_savres_rtx (info, spe_save_area_ptr,
20128 info->spe_gp_save_offset + save_off,
20129 0, reg_mode,
20130 SAVRES_SAVE | SAVRES_GPR);
20132 rs6000_frame_related (insn, spe_save_area_ptr, sp_off - save_off,
20133 NULL_RTX, NULL_RTX);
20136 /* Move the static chain pointer back. */
20137 if (!spe_regs_addressable)
20139 if (using_static_chain_p)
20141 emit_move_insn (spe_save_area_ptr, gen_rtx_REG (Pmode, 0));
20142 END_USE (0);
20144 else if (REGNO (frame_reg_rtx) != 11)
20145 END_USE (11);
20148 else if (!WORLD_SAVE_P (info) && !(strategy & SAVE_INLINE_GPRS))
20150 bool lr = (strategy & SAVE_NOINLINE_GPRS_SAVES_LR) != 0;
20151 int sel = SAVRES_SAVE | SAVRES_GPR | (lr ? SAVRES_LR : 0);
20152 unsigned ptr_regno = ptr_regno_for_savres (sel);
20153 rtx ptr_reg = frame_reg_rtx;
20154 bool ptr_set_up = REGNO (ptr_reg) == ptr_regno;
20155 int end_save = info->gp_save_offset + info->gp_size;
20156 int ptr_off;
20158 if (!ptr_set_up)
20159 ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
20161 /* Need to adjust r11 (r12) if we saved any FPRs. */
20162 if (end_save + frame_off != 0)
20164 rtx offset = GEN_INT (end_save + frame_off);
20166 if (ptr_set_up)
20167 frame_off = -end_save;
20168 else
20169 NOT_INUSE (ptr_regno);
20170 emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx, offset));
20172 else if (!ptr_set_up)
20174 NOT_INUSE (ptr_regno);
20175 emit_move_insn (ptr_reg, frame_reg_rtx);
20177 ptr_off = -end_save;
20178 insn = rs6000_emit_savres_rtx (info, ptr_reg,
20179 info->gp_save_offset + ptr_off,
20180 info->lr_save_offset + ptr_off,
20181 reg_mode, sel);
20182 rs6000_frame_related (insn, ptr_reg, sp_off - ptr_off,
20183 NULL_RTX, NULL_RTX);
20184 if (lr)
20185 END_USE (0);
20187 else if (!WORLD_SAVE_P (info) && (strategy & SAVRES_MULTIPLE))
20189 rtvec p;
20190 int i;
20191 p = rtvec_alloc (32 - info->first_gp_reg_save);
20192 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
20193 RTVEC_ELT (p, i)
20194 = gen_frame_store (gen_rtx_REG (reg_mode, info->first_gp_reg_save + i),
20195 frame_reg_rtx,
20196 info->gp_save_offset + frame_off + reg_size * i);
20197 insn = emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
20198 rs6000_frame_related (insn, frame_reg_rtx, sp_off - frame_off,
20199 NULL_RTX, NULL_RTX);
20201 else if (!WORLD_SAVE_P (info))
20203 int i;
20204 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
20205 if (rs6000_reg_live_or_pic_offset_p (info->first_gp_reg_save + i))
20206 emit_frame_save (frame_reg_rtx, reg_mode,
20207 info->first_gp_reg_save + i,
20208 info->gp_save_offset + frame_off + reg_size * i,
20209 sp_off - frame_off);
20212 if (crtl->calls_eh_return)
20214 unsigned int i;
20215 rtvec p;
20217 for (i = 0; ; ++i)
20219 unsigned int regno = EH_RETURN_DATA_REGNO (i);
20220 if (regno == INVALID_REGNUM)
20221 break;
20224 p = rtvec_alloc (i);
20226 for (i = 0; ; ++i)
20228 unsigned int regno = EH_RETURN_DATA_REGNO (i);
20229 if (regno == INVALID_REGNUM)
20230 break;
20232 insn
20233 = gen_frame_store (gen_rtx_REG (reg_mode, regno),
20234 sp_reg_rtx,
20235 info->ehrd_offset + sp_off + reg_size * (int) i);
20236 RTVEC_ELT (p, i) = insn;
20237 RTX_FRAME_RELATED_P (insn) = 1;
20240 insn = emit_insn (gen_blockage ());
20241 RTX_FRAME_RELATED_P (insn) = 1;
20242 add_reg_note (insn, REG_FRAME_RELATED_EXPR, gen_rtx_PARALLEL (VOIDmode, p));
20245 /* In AIX ABI we need to make sure r2 is really saved. */
20246 if (TARGET_AIX && crtl->calls_eh_return)
20248 rtx tmp_reg, tmp_reg_si, hi, lo, compare_result, toc_save_done, jump;
20249 rtx save_insn, join_insn, note;
20250 long toc_restore_insn;
20252 tmp_reg = gen_rtx_REG (Pmode, 11);
20253 tmp_reg_si = gen_rtx_REG (SImode, 11);
20254 if (using_static_chain_p)
20256 START_USE (0);
20257 emit_move_insn (gen_rtx_REG (Pmode, 0), tmp_reg);
20259 else
20260 START_USE (11);
20261 emit_move_insn (tmp_reg, gen_rtx_REG (Pmode, LR_REGNO));
20262 /* Peek at instruction to which this function returns. If it's
20263 restoring r2, then we know we've already saved r2. We can't
20264 unconditionally save r2 because the value we have will already
20265 be updated if we arrived at this function via a plt call or
20266 toc adjusting stub. */
20267 emit_move_insn (tmp_reg_si, gen_rtx_MEM (SImode, tmp_reg));
20268 toc_restore_insn = TARGET_32BIT ? 0x80410014 : 0xE8410028;
20269 hi = gen_int_mode (toc_restore_insn & ~0xffff, SImode);
20270 emit_insn (gen_xorsi3 (tmp_reg_si, tmp_reg_si, hi));
20271 compare_result = gen_rtx_REG (CCUNSmode, CR0_REGNO);
20272 validate_condition_mode (EQ, CCUNSmode);
20273 lo = gen_int_mode (toc_restore_insn & 0xffff, SImode);
20274 emit_insn (gen_rtx_SET (VOIDmode, compare_result,
20275 gen_rtx_COMPARE (CCUNSmode, tmp_reg_si, lo)));
20276 toc_save_done = gen_label_rtx ();
20277 jump = gen_rtx_IF_THEN_ELSE (VOIDmode,
20278 gen_rtx_EQ (VOIDmode, compare_result,
20279 const0_rtx),
20280 gen_rtx_LABEL_REF (VOIDmode, toc_save_done),
20281 pc_rtx);
20282 jump = emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, jump));
20283 JUMP_LABEL (jump) = toc_save_done;
20284 LABEL_NUSES (toc_save_done) += 1;
20286 save_insn = emit_frame_save (frame_reg_rtx, reg_mode,
20287 TOC_REGNUM, frame_off + 5 * reg_size,
20288 sp_off - frame_off);
20290 emit_label (toc_save_done);
20292 /* ??? If we leave SAVE_INSN as marked as saving R2, then we'll
20293 have a CFG that has different saves along different paths.
20294 Move the note to a dummy blockage insn, which describes that
20295 R2 is unconditionally saved after the label. */
20296 /* ??? An alternate representation might be a special insn pattern
20297 containing both the branch and the store. That might let the
20298 code that minimizes the number of DW_CFA_advance opcodes better
20299 freedom in placing the annotations. */
20300 note = find_reg_note (save_insn, REG_FRAME_RELATED_EXPR, NULL);
20301 if (note)
20302 remove_note (save_insn, note);
20303 else
20304 note = alloc_reg_note (REG_FRAME_RELATED_EXPR,
20305 copy_rtx (PATTERN (save_insn)), NULL_RTX);
20306 RTX_FRAME_RELATED_P (save_insn) = 0;
20308 join_insn = emit_insn (gen_blockage ());
20309 REG_NOTES (join_insn) = note;
20310 RTX_FRAME_RELATED_P (join_insn) = 1;
20312 if (using_static_chain_p)
20314 emit_move_insn (tmp_reg, gen_rtx_REG (Pmode, 0));
20315 END_USE (0);
20317 else
20318 END_USE (11);
20321 /* Save CR if we use any that must be preserved. */
20322 if (!WORLD_SAVE_P (info) && info->cr_save_p)
20324 rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
20325 GEN_INT (info->cr_save_offset + frame_off));
20326 rtx mem = gen_frame_mem (SImode, addr);
20327 /* See the large comment above about why CR2_REGNO is used. */
20328 rtx magic_eh_cr_reg = gen_rtx_REG (SImode, CR2_REGNO);
20330 /* If we didn't copy cr before, do so now using r0. */
20331 if (cr_save_rtx == NULL_RTX)
20333 rtx set;
20335 START_USE (0);
20336 cr_save_rtx = gen_rtx_REG (SImode, 0);
20337 insn = emit_insn (gen_movesi_from_cr (cr_save_rtx));
20338 RTX_FRAME_RELATED_P (insn) = 1;
20339 set = gen_rtx_SET (VOIDmode, cr_save_rtx, magic_eh_cr_reg);
20340 add_reg_note (insn, REG_FRAME_RELATED_EXPR, set);
20342 insn = emit_move_insn (mem, cr_save_rtx);
20343 END_USE (REGNO (cr_save_rtx));
20345 rs6000_frame_related (insn, frame_reg_rtx, sp_off - frame_off,
20346 NULL_RTX, NULL_RTX);
20349 /* Update stack and set back pointer unless this is V.4,
20350 for which it was done previously. */
20351 if (!WORLD_SAVE_P (info) && info->push_p
20352 && !(DEFAULT_ABI == ABI_V4 || crtl->calls_eh_return))
20354 rtx ptr_reg = NULL;
20355 int ptr_off = 0;
20357 /* If saving altivec regs we need to be able to address all save
20358 locations using a 16-bit offset. */
20359 if ((strategy & SAVE_INLINE_VRS) == 0
20360 || (info->altivec_size != 0
20361 && (info->altivec_save_offset + info->altivec_size - 16
20362 + info->total_size - frame_off) > 32767)
20363 || (info->vrsave_size != 0
20364 && (info->vrsave_save_offset
20365 + info->total_size - frame_off) > 32767))
20367 int sel = SAVRES_SAVE | SAVRES_VR;
20368 unsigned ptr_regno = ptr_regno_for_savres (sel);
20370 if (using_static_chain_p
20371 && ptr_regno == STATIC_CHAIN_REGNUM)
20372 ptr_regno = 12;
20373 if (REGNO (frame_reg_rtx) != ptr_regno)
20374 START_USE (ptr_regno);
20375 ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
20376 frame_reg_rtx = ptr_reg;
20377 ptr_off = info->altivec_save_offset + info->altivec_size;
20378 frame_off = -ptr_off;
20380 else if (REGNO (frame_reg_rtx) == 1)
20381 frame_off = info->total_size;
20382 rs6000_emit_allocate_stack (info->total_size, ptr_reg, ptr_off);
20383 sp_off = info->total_size;
20384 if (frame_reg_rtx != sp_reg_rtx)
20385 rs6000_emit_stack_tie (frame_reg_rtx, false);
20388 /* Set frame pointer, if needed. */
20389 if (frame_pointer_needed)
20391 insn = emit_move_insn (gen_rtx_REG (Pmode, HARD_FRAME_POINTER_REGNUM),
20392 sp_reg_rtx);
20393 RTX_FRAME_RELATED_P (insn) = 1;
20396 /* Save AltiVec registers if needed. Save here because the red zone does
20397 not always include AltiVec registers. */
20398 if (!WORLD_SAVE_P (info) && TARGET_ALTIVEC_ABI
20399 && info->altivec_size != 0 && (strategy & SAVE_INLINE_VRS) == 0)
20401 int end_save = info->altivec_save_offset + info->altivec_size;
20402 int ptr_off;
20403 /* Oddly, the vector save/restore functions point r0 at the end
20404 of the save area, then use r11 or r12 to load offsets for
20405 [reg+reg] addressing. */
20406 rtx ptr_reg = gen_rtx_REG (Pmode, 0);
20407 int scratch_regno = ptr_regno_for_savres (SAVRES_SAVE | SAVRES_VR);
20408 rtx scratch_reg = gen_rtx_REG (Pmode, scratch_regno);
20410 gcc_checking_assert (scratch_regno == 11 || scratch_regno == 12);
20411 NOT_INUSE (0);
20412 if (end_save + frame_off != 0)
20414 rtx offset = GEN_INT (end_save + frame_off);
20416 emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx, offset));
20418 else
20419 emit_move_insn (ptr_reg, frame_reg_rtx);
20421 ptr_off = -end_save;
20422 insn = rs6000_emit_savres_rtx (info, scratch_reg,
20423 info->altivec_save_offset + ptr_off,
20424 0, V4SImode, SAVRES_SAVE | SAVRES_VR);
20425 rs6000_frame_related (insn, scratch_reg, sp_off - ptr_off,
20426 NULL_RTX, NULL_RTX);
20427 if (REGNO (frame_reg_rtx) == REGNO (scratch_reg))
20429 /* The oddity mentioned above clobbered our frame reg. */
20430 emit_move_insn (frame_reg_rtx, ptr_reg);
20431 frame_off = ptr_off;
20434 else if (!WORLD_SAVE_P (info) && TARGET_ALTIVEC_ABI
20435 && info->altivec_size != 0)
20437 int i;
20439 for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
20440 if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
20442 rtx areg, savereg, mem;
20443 int offset;
20445 offset = (info->altivec_save_offset + frame_off
20446 + 16 * (i - info->first_altivec_reg_save));
20448 savereg = gen_rtx_REG (V4SImode, i);
20450 NOT_INUSE (0);
20451 areg = gen_rtx_REG (Pmode, 0);
20452 emit_move_insn (areg, GEN_INT (offset));
20454 /* AltiVec addressing mode is [reg+reg]. */
20455 mem = gen_frame_mem (V4SImode,
20456 gen_rtx_PLUS (Pmode, frame_reg_rtx, areg));
20458 insn = emit_move_insn (mem, savereg);
20460 rs6000_frame_related (insn, frame_reg_rtx, sp_off - frame_off,
20461 areg, GEN_INT (offset));
20465 /* VRSAVE is a bit vector representing which AltiVec registers
20466 are used. The OS uses this to determine which vector
20467 registers to save on a context switch. We need to save
20468 VRSAVE on the stack frame, add whatever AltiVec registers we
20469 used in this function, and do the corresponding magic in the
20470 epilogue. */
20472 if (!WORLD_SAVE_P (info)
20473 && TARGET_ALTIVEC
20474 && TARGET_ALTIVEC_VRSAVE
20475 && info->vrsave_mask != 0)
20477 rtx reg, vrsave;
20478 int offset;
20479 int save_regno;
20481 /* Get VRSAVE onto a GPR. Note that ABI_V4 and ABI_DARWIN might
20482 be using r12 as frame_reg_rtx and r11 as the static chain
20483 pointer for nested functions. */
20484 save_regno = 12;
20485 if (DEFAULT_ABI == ABI_AIX && !using_static_chain_p)
20486 save_regno = 11;
20487 else if (REGNO (frame_reg_rtx) == 12)
20489 save_regno = 11;
20490 if (using_static_chain_p)
20491 save_regno = 0;
20494 NOT_INUSE (save_regno);
20495 reg = gen_rtx_REG (SImode, save_regno);
20496 vrsave = gen_rtx_REG (SImode, VRSAVE_REGNO);
20497 if (TARGET_MACHO)
20498 emit_insn (gen_get_vrsave_internal (reg));
20499 else
20500 emit_insn (gen_rtx_SET (VOIDmode, reg, vrsave));
20502 /* Save VRSAVE. */
20503 offset = info->vrsave_save_offset + frame_off;
20504 insn = emit_insn (gen_frame_store (reg, frame_reg_rtx, offset));
20506 /* Include the registers in the mask. */
20507 emit_insn (gen_iorsi3 (reg, reg, GEN_INT ((int) info->vrsave_mask)));
20509 insn = emit_insn (generate_set_vrsave (reg, info, 0));
20512 /* If we are using RS6000_PIC_OFFSET_TABLE_REGNUM, we need to set it up. */
20513 if (!TARGET_SINGLE_PIC_BASE
20514 && ((TARGET_TOC && TARGET_MINIMAL_TOC && get_pool_size () != 0)
20515 || (DEFAULT_ABI == ABI_V4
20516 && (flag_pic == 1 || (flag_pic && TARGET_SECURE_PLT))
20517 && df_regs_ever_live_p (RS6000_PIC_OFFSET_TABLE_REGNUM))))
20519 /* If emit_load_toc_table will use the link register, we need to save
20520 it. We use R12 for this purpose because emit_load_toc_table
20521 can use register 0. This allows us to use a plain 'blr' to return
20522 from the procedure more often. */
20523 int save_LR_around_toc_setup = (TARGET_ELF
20524 && DEFAULT_ABI != ABI_AIX
20525 && flag_pic
20526 && ! info->lr_save_p
20527 && EDGE_COUNT (EXIT_BLOCK_PTR->preds) > 0);
20528 if (save_LR_around_toc_setup)
20530 rtx lr = gen_rtx_REG (Pmode, LR_REGNO);
20531 rtx tmp = gen_rtx_REG (Pmode, 12);
20533 insn = emit_move_insn (tmp, lr);
20534 RTX_FRAME_RELATED_P (insn) = 1;
20536 rs6000_emit_load_toc_table (TRUE);
20538 insn = emit_move_insn (lr, tmp);
20539 add_reg_note (insn, REG_CFA_RESTORE, lr);
20540 RTX_FRAME_RELATED_P (insn) = 1;
20542 else
20543 rs6000_emit_load_toc_table (TRUE);
20546 #if TARGET_MACHO
20547 if (!TARGET_SINGLE_PIC_BASE
20548 && DEFAULT_ABI == ABI_DARWIN
20549 && flag_pic && crtl->uses_pic_offset_table)
20551 rtx lr = gen_rtx_REG (Pmode, LR_REGNO);
20552 rtx src = gen_rtx_SYMBOL_REF (Pmode, MACHOPIC_FUNCTION_BASE_NAME);
20554 /* Save and restore LR locally around this call (in R0). */
20555 if (!info->lr_save_p)
20556 emit_move_insn (gen_rtx_REG (Pmode, 0), lr);
20558 emit_insn (gen_load_macho_picbase (src));
20560 emit_move_insn (gen_rtx_REG (Pmode,
20561 RS6000_PIC_OFFSET_TABLE_REGNUM),
20562 lr);
20564 if (!info->lr_save_p)
20565 emit_move_insn (lr, gen_rtx_REG (Pmode, 0));
20567 #endif
20569 /* If we need to, save the TOC register after doing the stack setup.
20570 Do not emit eh frame info for this save. The unwinder wants info,
20571 conceptually attached to instructions in this function, about
20572 register values in the caller of this function. This R2 may have
20573 already been changed from the value in the caller.
20574 We don't attempt to write accurate DWARF EH frame info for R2
20575 because code emitted by gcc for a (non-pointer) function call
20576 doesn't save and restore R2. Instead, R2 is managed out-of-line
20577 by a linker generated plt call stub when the function resides in
20578 a shared library. This behaviour is costly to describe in DWARF,
20579 both in terms of the size of DWARF info and the time taken in the
20580 unwinder to interpret it. R2 changes, apart from the
20581 calls_eh_return case earlier in this function, are handled by
20582 linux-unwind.h frob_update_context. */
20583 if (rs6000_save_toc_in_prologue_p ())
20585 rtx reg = gen_rtx_REG (reg_mode, TOC_REGNUM);
20586 emit_insn (gen_frame_store (reg, sp_reg_rtx, 5 * reg_size));
20590 /* Write function prologue. */
20592 static void
20593 rs6000_output_function_prologue (FILE *file,
20594 HOST_WIDE_INT size ATTRIBUTE_UNUSED)
20596 rs6000_stack_t *info = rs6000_stack_info ();
20598 if (TARGET_DEBUG_STACK)
20599 debug_stack_info (info);
20601 /* Write .extern for any function we will call to save and restore
20602 fp values. */
20603 if (info->first_fp_reg_save < 64
20604 && !TARGET_MACHO
20605 && !TARGET_ELF)
20607 char *name;
20608 int regno = info->first_fp_reg_save - 32;
20610 if ((info->savres_strategy & SAVE_INLINE_FPRS) == 0)
20612 bool lr = (info->savres_strategy & SAVE_NOINLINE_FPRS_SAVES_LR) != 0;
20613 int sel = SAVRES_SAVE | SAVRES_FPR | (lr ? SAVRES_LR : 0);
20614 name = rs6000_savres_routine_name (info, regno, sel);
20615 fprintf (file, "\t.extern %s\n", name);
20617 if ((info->savres_strategy & REST_INLINE_FPRS) == 0)
20619 bool lr = (info->savres_strategy
20620 & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR) == 0;
20621 int sel = SAVRES_FPR | (lr ? SAVRES_LR : 0);
20622 name = rs6000_savres_routine_name (info, regno, sel);
20623 fprintf (file, "\t.extern %s\n", name);
20627 rs6000_pic_labelno++;
20630 /* Non-zero if vmx regs are restored before the frame pop, zero if
20631 we restore after the pop when possible. */
20632 #define ALWAYS_RESTORE_ALTIVEC_BEFORE_POP 0
20634 /* Restoring cr is a two step process: loading a reg from the frame
20635 save, then moving the reg to cr. For ABI_V4 we must let the
20636 unwinder know that the stack location is no longer valid at or
20637 before the stack deallocation, but we can't emit a cfa_restore for
20638 cr at the stack deallocation like we do for other registers.
20639 The trouble is that it is possible for the move to cr to be
20640 scheduled after the stack deallocation. So say exactly where cr
20641 is located on each of the two insns. */
20643 static rtx
20644 load_cr_save (int regno, rtx frame_reg_rtx, int offset, bool exit_func)
20646 rtx mem = gen_frame_mem_offset (SImode, frame_reg_rtx, offset);
20647 rtx reg = gen_rtx_REG (SImode, regno);
20648 rtx insn = emit_move_insn (reg, mem);
20650 if (!exit_func && DEFAULT_ABI == ABI_V4)
20652 rtx cr = gen_rtx_REG (SImode, CR2_REGNO);
20653 rtx set = gen_rtx_SET (VOIDmode, reg, cr);
20655 add_reg_note (insn, REG_CFA_REGISTER, set);
20656 RTX_FRAME_RELATED_P (insn) = 1;
20658 return reg;
20661 /* Reload CR from REG. */
20663 static void
20664 restore_saved_cr (rtx reg, int using_mfcr_multiple, bool exit_func)
20666 int count = 0;
20667 int i;
20669 if (using_mfcr_multiple)
20671 for (i = 0; i < 8; i++)
20672 if (save_reg_p (CR0_REGNO + i))
20673 count++;
20674 gcc_assert (count);
20677 if (using_mfcr_multiple && count > 1)
20679 rtvec p;
20680 int ndx;
20682 p = rtvec_alloc (count);
20684 ndx = 0;
20685 for (i = 0; i < 8; i++)
20686 if (save_reg_p (CR0_REGNO + i))
20688 rtvec r = rtvec_alloc (2);
20689 RTVEC_ELT (r, 0) = reg;
20690 RTVEC_ELT (r, 1) = GEN_INT (1 << (7-i));
20691 RTVEC_ELT (p, ndx) =
20692 gen_rtx_SET (VOIDmode, gen_rtx_REG (CCmode, CR0_REGNO + i),
20693 gen_rtx_UNSPEC (CCmode, r, UNSPEC_MOVESI_TO_CR));
20694 ndx++;
20696 emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
20697 gcc_assert (ndx == count);
20699 else
20700 for (i = 0; i < 8; i++)
20701 if (save_reg_p (CR0_REGNO + i))
20702 emit_insn (gen_movsi_to_cr_one (gen_rtx_REG (CCmode, CR0_REGNO + i),
20703 reg));
20705 if (!exit_func && (DEFAULT_ABI == ABI_V4 || flag_shrink_wrap))
20707 rtx insn = get_last_insn ();
20708 rtx cr = gen_rtx_REG (SImode, CR2_REGNO);
20710 add_reg_note (insn, REG_CFA_RESTORE, cr);
20711 RTX_FRAME_RELATED_P (insn) = 1;
20715 /* Like cr, the move to lr instruction can be scheduled after the
20716 stack deallocation, but unlike cr, its stack frame save is still
20717 valid. So we only need to emit the cfa_restore on the correct
20718 instruction. */
20720 static void
20721 load_lr_save (int regno, rtx frame_reg_rtx, int offset)
20723 rtx mem = gen_frame_mem_offset (Pmode, frame_reg_rtx, offset);
20724 rtx reg = gen_rtx_REG (Pmode, regno);
20726 emit_move_insn (reg, mem);
20729 static void
20730 restore_saved_lr (int regno, bool exit_func)
20732 rtx reg = gen_rtx_REG (Pmode, regno);
20733 rtx lr = gen_rtx_REG (Pmode, LR_REGNO);
20734 rtx insn = emit_move_insn (lr, reg);
20736 if (!exit_func && flag_shrink_wrap)
20738 add_reg_note (insn, REG_CFA_RESTORE, lr);
20739 RTX_FRAME_RELATED_P (insn) = 1;
20743 static rtx
20744 add_crlr_cfa_restore (const rs6000_stack_t *info, rtx cfa_restores)
20746 if (info->cr_save_p)
20747 cfa_restores = alloc_reg_note (REG_CFA_RESTORE,
20748 gen_rtx_REG (SImode, CR2_REGNO),
20749 cfa_restores);
20750 if (info->lr_save_p)
20751 cfa_restores = alloc_reg_note (REG_CFA_RESTORE,
20752 gen_rtx_REG (Pmode, LR_REGNO),
20753 cfa_restores);
20754 return cfa_restores;
20757 /* Return true if OFFSET from stack pointer can be clobbered by signals.
20758 V.4 doesn't have any stack cushion, AIX ABIs have 220 or 288 bytes
20759 below stack pointer not cloberred by signals. */
20761 static inline bool
20762 offset_below_red_zone_p (HOST_WIDE_INT offset)
20764 return offset < (DEFAULT_ABI == ABI_V4
20766 : TARGET_32BIT ? -220 : -288);
20769 /* Append CFA_RESTORES to any existing REG_NOTES on the last insn. */
20771 static void
20772 emit_cfa_restores (rtx cfa_restores)
20774 rtx insn = get_last_insn ();
20775 rtx *loc = &REG_NOTES (insn);
20777 while (*loc)
20778 loc = &XEXP (*loc, 1);
20779 *loc = cfa_restores;
20780 RTX_FRAME_RELATED_P (insn) = 1;
20783 /* Emit function epilogue as insns. */
20785 void
20786 rs6000_emit_epilogue (int sibcall)
20788 rs6000_stack_t *info;
20789 int restoring_GPRs_inline;
20790 int restoring_FPRs_inline;
20791 int using_load_multiple;
20792 int using_mtcr_multiple;
20793 int use_backchain_to_restore_sp;
20794 int restore_lr;
20795 int strategy;
20796 HOST_WIDE_INT frame_off = 0;
20797 rtx sp_reg_rtx = gen_rtx_REG (Pmode, 1);
20798 rtx frame_reg_rtx = sp_reg_rtx;
20799 rtx cfa_restores = NULL_RTX;
20800 rtx insn;
20801 rtx cr_save_reg = NULL_RTX;
20802 enum machine_mode reg_mode = Pmode;
20803 int reg_size = TARGET_32BIT ? 4 : 8;
20804 int i;
20805 bool exit_func;
20806 unsigned ptr_regno;
20808 info = rs6000_stack_info ();
20810 if (TARGET_SPE_ABI && info->spe_64bit_regs_used != 0)
20812 reg_mode = V2SImode;
20813 reg_size = 8;
20816 strategy = info->savres_strategy;
20817 using_load_multiple = strategy & SAVRES_MULTIPLE;
20818 restoring_FPRs_inline = sibcall || (strategy & REST_INLINE_FPRS);
20819 restoring_GPRs_inline = sibcall || (strategy & REST_INLINE_GPRS);
20820 using_mtcr_multiple = (rs6000_cpu == PROCESSOR_PPC601
20821 || rs6000_cpu == PROCESSOR_PPC603
20822 || rs6000_cpu == PROCESSOR_PPC750
20823 || optimize_size);
20824 /* Restore via the backchain when we have a large frame, since this
20825 is more efficient than an addis, addi pair. The second condition
20826 here will not trigger at the moment; We don't actually need a
20827 frame pointer for alloca, but the generic parts of the compiler
20828 give us one anyway. */
20829 use_backchain_to_restore_sp = (info->total_size > 32767 - info->lr_save_offset
20830 || (cfun->calls_alloca
20831 && !frame_pointer_needed));
20832 restore_lr = (info->lr_save_p
20833 && (restoring_FPRs_inline
20834 || (strategy & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR))
20835 && (restoring_GPRs_inline
20836 || info->first_fp_reg_save < 64));
20838 if (WORLD_SAVE_P (info))
20840 int i, j;
20841 char rname[30];
20842 const char *alloc_rname;
20843 rtvec p;
20845 /* eh_rest_world_r10 will return to the location saved in the LR
20846 stack slot (which is not likely to be our caller.)
20847 Input: R10 -- stack adjustment. Clobbers R0, R11, R12, R7, R8.
20848 rest_world is similar, except any R10 parameter is ignored.
20849 The exception-handling stuff that was here in 2.95 is no
20850 longer necessary. */
20852 p = rtvec_alloc (9
20854 + 32 - info->first_gp_reg_save
20855 + LAST_ALTIVEC_REGNO + 1 - info->first_altivec_reg_save
20856 + 63 + 1 - info->first_fp_reg_save);
20858 strcpy (rname, ((crtl->calls_eh_return) ?
20859 "*eh_rest_world_r10" : "*rest_world"));
20860 alloc_rname = ggc_strdup (rname);
20862 j = 0;
20863 RTVEC_ELT (p, j++) = ret_rtx;
20864 RTVEC_ELT (p, j++) = gen_rtx_USE (VOIDmode,
20865 gen_rtx_REG (Pmode,
20866 LR_REGNO));
20867 RTVEC_ELT (p, j++)
20868 = gen_rtx_USE (VOIDmode, gen_rtx_SYMBOL_REF (Pmode, alloc_rname));
20869 /* The instruction pattern requires a clobber here;
20870 it is shared with the restVEC helper. */
20871 RTVEC_ELT (p, j++)
20872 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, 11));
20875 /* CR register traditionally saved as CR2. */
20876 rtx reg = gen_rtx_REG (SImode, CR2_REGNO);
20877 RTVEC_ELT (p, j++)
20878 = gen_frame_load (reg, frame_reg_rtx, info->cr_save_offset);
20879 if (flag_shrink_wrap)
20881 cfa_restores = alloc_reg_note (REG_CFA_RESTORE,
20882 gen_rtx_REG (Pmode, LR_REGNO),
20883 cfa_restores);
20884 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
20888 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
20890 rtx reg = gen_rtx_REG (reg_mode, info->first_gp_reg_save + i);
20891 RTVEC_ELT (p, j++)
20892 = gen_frame_load (reg,
20893 frame_reg_rtx, info->gp_save_offset + reg_size * i);
20894 if (flag_shrink_wrap)
20895 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
20897 for (i = 0; info->first_altivec_reg_save + i <= LAST_ALTIVEC_REGNO; i++)
20899 rtx reg = gen_rtx_REG (V4SImode, info->first_altivec_reg_save + i);
20900 RTVEC_ELT (p, j++)
20901 = gen_frame_load (reg,
20902 frame_reg_rtx, info->altivec_save_offset + 16 * i);
20903 if (flag_shrink_wrap)
20904 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
20906 for (i = 0; info->first_fp_reg_save + i <= 63; i++)
20908 rtx reg = gen_rtx_REG ((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT
20909 ? DFmode : SFmode),
20910 info->first_fp_reg_save + i);
20911 RTVEC_ELT (p, j++)
20912 = gen_frame_load (reg, frame_reg_rtx, info->fp_save_offset + 8 * i);
20913 if (flag_shrink_wrap)
20914 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
20916 RTVEC_ELT (p, j++)
20917 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, 0));
20918 RTVEC_ELT (p, j++)
20919 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (SImode, 12));
20920 RTVEC_ELT (p, j++)
20921 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (SImode, 7));
20922 RTVEC_ELT (p, j++)
20923 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (SImode, 8));
20924 RTVEC_ELT (p, j++)
20925 = gen_rtx_USE (VOIDmode, gen_rtx_REG (SImode, 10));
20926 insn = emit_jump_insn (gen_rtx_PARALLEL (VOIDmode, p));
20928 if (flag_shrink_wrap)
20930 REG_NOTES (insn) = cfa_restores;
20931 add_reg_note (insn, REG_CFA_DEF_CFA, sp_reg_rtx);
20932 RTX_FRAME_RELATED_P (insn) = 1;
20934 return;
20937 /* frame_reg_rtx + frame_off points to the top of this stack frame. */
20938 if (info->push_p)
20939 frame_off = info->total_size;
20941 /* Restore AltiVec registers if we must do so before adjusting the
20942 stack. */
20943 if (TARGET_ALTIVEC_ABI
20944 && info->altivec_size != 0
20945 && (ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
20946 || (DEFAULT_ABI != ABI_V4
20947 && offset_below_red_zone_p (info->altivec_save_offset))))
20949 int i;
20950 int scratch_regno = ptr_regno_for_savres (SAVRES_VR);
20952 gcc_checking_assert (scratch_regno == 11 || scratch_regno == 12);
20953 if (use_backchain_to_restore_sp)
20955 int frame_regno = 11;
20957 if ((strategy & REST_INLINE_VRS) == 0)
20959 /* Of r11 and r12, select the one not clobbered by an
20960 out-of-line restore function for the frame register. */
20961 frame_regno = 11 + 12 - scratch_regno;
20963 frame_reg_rtx = gen_rtx_REG (Pmode, frame_regno);
20964 emit_move_insn (frame_reg_rtx,
20965 gen_rtx_MEM (Pmode, sp_reg_rtx));
20966 frame_off = 0;
20968 else if (frame_pointer_needed)
20969 frame_reg_rtx = hard_frame_pointer_rtx;
20971 if ((strategy & REST_INLINE_VRS) == 0)
20973 int end_save = info->altivec_save_offset + info->altivec_size;
20974 int ptr_off;
20975 rtx ptr_reg = gen_rtx_REG (Pmode, 0);
20976 rtx scratch_reg = gen_rtx_REG (Pmode, scratch_regno);
20978 if (end_save + frame_off != 0)
20980 rtx offset = GEN_INT (end_save + frame_off);
20982 emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx, offset));
20984 else
20985 emit_move_insn (ptr_reg, frame_reg_rtx);
20987 ptr_off = -end_save;
20988 insn = rs6000_emit_savres_rtx (info, scratch_reg,
20989 info->altivec_save_offset + ptr_off,
20990 0, V4SImode, SAVRES_VR);
20992 else
20994 for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
20995 if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
20997 rtx addr, areg, mem, reg;
20999 areg = gen_rtx_REG (Pmode, 0);
21000 emit_move_insn
21001 (areg, GEN_INT (info->altivec_save_offset
21002 + frame_off
21003 + 16 * (i - info->first_altivec_reg_save)));
21005 /* AltiVec addressing mode is [reg+reg]. */
21006 addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, areg);
21007 mem = gen_frame_mem (V4SImode, addr);
21009 reg = gen_rtx_REG (V4SImode, i);
21010 emit_move_insn (reg, mem);
21014 for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
21015 if (((strategy & REST_INLINE_VRS) == 0
21016 || (info->vrsave_mask & ALTIVEC_REG_BIT (i)) != 0)
21017 && (flag_shrink_wrap
21018 || (offset_below_red_zone_p
21019 (info->altivec_save_offset
21020 + 16 * (i - info->first_altivec_reg_save)))))
21022 rtx reg = gen_rtx_REG (V4SImode, i);
21023 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
21027 /* Restore VRSAVE if we must do so before adjusting the stack. */
21028 if (TARGET_ALTIVEC
21029 && TARGET_ALTIVEC_VRSAVE
21030 && info->vrsave_mask != 0
21031 && (ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
21032 || (DEFAULT_ABI != ABI_V4
21033 && offset_below_red_zone_p (info->vrsave_save_offset))))
21035 rtx reg;
21037 if (frame_reg_rtx == sp_reg_rtx)
21039 if (use_backchain_to_restore_sp)
21041 frame_reg_rtx = gen_rtx_REG (Pmode, 11);
21042 emit_move_insn (frame_reg_rtx,
21043 gen_rtx_MEM (Pmode, sp_reg_rtx));
21044 frame_off = 0;
21046 else if (frame_pointer_needed)
21047 frame_reg_rtx = hard_frame_pointer_rtx;
21050 reg = gen_rtx_REG (SImode, 12);
21051 emit_insn (gen_frame_load (reg, frame_reg_rtx,
21052 info->vrsave_save_offset + frame_off));
21054 emit_insn (generate_set_vrsave (reg, info, 1));
21057 insn = NULL_RTX;
21058 /* If we have a large stack frame, restore the old stack pointer
21059 using the backchain. */
21060 if (use_backchain_to_restore_sp)
21062 if (frame_reg_rtx == sp_reg_rtx)
21064 /* Under V.4, don't reset the stack pointer until after we're done
21065 loading the saved registers. */
21066 if (DEFAULT_ABI == ABI_V4)
21067 frame_reg_rtx = gen_rtx_REG (Pmode, 11);
21069 insn = emit_move_insn (frame_reg_rtx,
21070 gen_rtx_MEM (Pmode, sp_reg_rtx));
21071 frame_off = 0;
21073 else if (ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
21074 && DEFAULT_ABI == ABI_V4)
21075 /* frame_reg_rtx has been set up by the altivec restore. */
21077 else
21079 insn = emit_move_insn (sp_reg_rtx, frame_reg_rtx);
21080 frame_reg_rtx = sp_reg_rtx;
21083 /* If we have a frame pointer, we can restore the old stack pointer
21084 from it. */
21085 else if (frame_pointer_needed)
21087 frame_reg_rtx = sp_reg_rtx;
21088 if (DEFAULT_ABI == ABI_V4)
21089 frame_reg_rtx = gen_rtx_REG (Pmode, 11);
21090 /* Prevent reordering memory accesses against stack pointer restore. */
21091 else if (cfun->calls_alloca
21092 || offset_below_red_zone_p (-info->total_size))
21093 rs6000_emit_stack_tie (frame_reg_rtx, true);
21095 insn = emit_insn (gen_add3_insn (frame_reg_rtx, hard_frame_pointer_rtx,
21096 GEN_INT (info->total_size)));
21097 frame_off = 0;
21099 else if (info->push_p
21100 && DEFAULT_ABI != ABI_V4
21101 && !crtl->calls_eh_return)
21103 /* Prevent reordering memory accesses against stack pointer restore. */
21104 if (cfun->calls_alloca
21105 || offset_below_red_zone_p (-info->total_size))
21106 rs6000_emit_stack_tie (frame_reg_rtx, false);
21107 insn = emit_insn (gen_add3_insn (sp_reg_rtx, sp_reg_rtx,
21108 GEN_INT (info->total_size)));
21109 frame_off = 0;
21111 if (insn && frame_reg_rtx == sp_reg_rtx)
21113 if (cfa_restores)
21115 REG_NOTES (insn) = cfa_restores;
21116 cfa_restores = NULL_RTX;
21118 add_reg_note (insn, REG_CFA_DEF_CFA, sp_reg_rtx);
21119 RTX_FRAME_RELATED_P (insn) = 1;
21122 /* Restore AltiVec registers if we have not done so already. */
21123 if (!ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
21124 && TARGET_ALTIVEC_ABI
21125 && info->altivec_size != 0
21126 && (DEFAULT_ABI == ABI_V4
21127 || !offset_below_red_zone_p (info->altivec_save_offset)))
21129 int i;
21131 if ((strategy & REST_INLINE_VRS) == 0)
21133 int end_save = info->altivec_save_offset + info->altivec_size;
21134 int ptr_off;
21135 rtx ptr_reg = gen_rtx_REG (Pmode, 0);
21136 int scratch_regno = ptr_regno_for_savres (SAVRES_VR);
21137 rtx scratch_reg = gen_rtx_REG (Pmode, scratch_regno);
21139 if (end_save + frame_off != 0)
21141 rtx offset = GEN_INT (end_save + frame_off);
21143 emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx, offset));
21145 else
21146 emit_move_insn (ptr_reg, frame_reg_rtx);
21148 ptr_off = -end_save;
21149 insn = rs6000_emit_savres_rtx (info, scratch_reg,
21150 info->altivec_save_offset + ptr_off,
21151 0, V4SImode, SAVRES_VR);
21152 if (REGNO (frame_reg_rtx) == REGNO (scratch_reg))
21154 /* Frame reg was clobbered by out-of-line save. Restore it
21155 from ptr_reg, and if we are calling out-of-line gpr or
21156 fpr restore set up the correct pointer and offset. */
21157 unsigned newptr_regno = 1;
21158 if (!restoring_GPRs_inline)
21160 bool lr = info->gp_save_offset + info->gp_size == 0;
21161 int sel = SAVRES_GPR | (lr ? SAVRES_LR : 0);
21162 newptr_regno = ptr_regno_for_savres (sel);
21163 end_save = info->gp_save_offset + info->gp_size;
21165 else if (!restoring_FPRs_inline)
21167 bool lr = !(strategy & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR);
21168 int sel = SAVRES_FPR | (lr ? SAVRES_LR : 0);
21169 newptr_regno = ptr_regno_for_savres (sel);
21170 end_save = info->gp_save_offset + info->gp_size;
21173 if (newptr_regno != 1 && REGNO (frame_reg_rtx) != newptr_regno)
21174 frame_reg_rtx = gen_rtx_REG (Pmode, newptr_regno);
21176 if (end_save + ptr_off != 0)
21178 rtx offset = GEN_INT (end_save + ptr_off);
21180 frame_off = -end_save;
21181 emit_insn (gen_add3_insn (frame_reg_rtx, ptr_reg, offset));
21183 else
21185 frame_off = ptr_off;
21186 emit_move_insn (frame_reg_rtx, ptr_reg);
21190 else
21192 for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
21193 if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
21195 rtx addr, areg, mem, reg;
21197 areg = gen_rtx_REG (Pmode, 0);
21198 emit_move_insn
21199 (areg, GEN_INT (info->altivec_save_offset
21200 + frame_off
21201 + 16 * (i - info->first_altivec_reg_save)));
21203 /* AltiVec addressing mode is [reg+reg]. */
21204 addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, areg);
21205 mem = gen_frame_mem (V4SImode, addr);
21207 reg = gen_rtx_REG (V4SImode, i);
21208 emit_move_insn (reg, mem);
21212 for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
21213 if (((strategy & REST_INLINE_VRS) == 0
21214 || (info->vrsave_mask & ALTIVEC_REG_BIT (i)) != 0)
21215 && (DEFAULT_ABI == ABI_V4 || flag_shrink_wrap))
21217 rtx reg = gen_rtx_REG (V4SImode, i);
21218 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
21222 /* Restore VRSAVE if we have not done so already. */
21223 if (!ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
21224 && TARGET_ALTIVEC
21225 && TARGET_ALTIVEC_VRSAVE
21226 && info->vrsave_mask != 0
21227 && (DEFAULT_ABI == ABI_V4
21228 || !offset_below_red_zone_p (info->vrsave_save_offset)))
21230 rtx reg;
21232 reg = gen_rtx_REG (SImode, 12);
21233 emit_insn (gen_frame_load (reg, frame_reg_rtx,
21234 info->vrsave_save_offset + frame_off));
21236 emit_insn (generate_set_vrsave (reg, info, 1));
21239 /* If we exit by an out-of-line restore function on ABI_V4 then that
21240 function will deallocate the stack, so we don't need to worry
21241 about the unwinder restoring cr from an invalid stack frame
21242 location. */
21243 exit_func = (!restoring_FPRs_inline
21244 || (!restoring_GPRs_inline
21245 && info->first_fp_reg_save == 64));
21247 /* Get the old lr if we saved it. If we are restoring registers
21248 out-of-line, then the out-of-line routines can do this for us. */
21249 if (restore_lr && restoring_GPRs_inline)
21250 load_lr_save (0, frame_reg_rtx, info->lr_save_offset + frame_off);
21252 /* Get the old cr if we saved it. */
21253 if (info->cr_save_p)
21255 unsigned cr_save_regno = 12;
21257 if (!restoring_GPRs_inline)
21259 /* Ensure we don't use the register used by the out-of-line
21260 gpr register restore below. */
21261 bool lr = info->gp_save_offset + info->gp_size == 0;
21262 int sel = SAVRES_GPR | (lr ? SAVRES_LR : 0);
21263 int gpr_ptr_regno = ptr_regno_for_savres (sel);
21265 if (gpr_ptr_regno == 12)
21266 cr_save_regno = 11;
21267 gcc_checking_assert (REGNO (frame_reg_rtx) != cr_save_regno);
21269 else if (REGNO (frame_reg_rtx) == 12)
21270 cr_save_regno = 11;
21272 cr_save_reg = load_cr_save (cr_save_regno, frame_reg_rtx,
21273 info->cr_save_offset + frame_off,
21274 exit_func);
21277 /* Set LR here to try to overlap restores below. */
21278 if (restore_lr && restoring_GPRs_inline)
21279 restore_saved_lr (0, exit_func);
21281 /* Load exception handler data registers, if needed. */
21282 if (crtl->calls_eh_return)
21284 unsigned int i, regno;
21286 if (TARGET_AIX)
21288 rtx reg = gen_rtx_REG (reg_mode, 2);
21289 emit_insn (gen_frame_load (reg, frame_reg_rtx,
21290 frame_off + 5 * reg_size));
21293 for (i = 0; ; ++i)
21295 rtx mem;
21297 regno = EH_RETURN_DATA_REGNO (i);
21298 if (regno == INVALID_REGNUM)
21299 break;
21301 /* Note: possible use of r0 here to address SPE regs. */
21302 mem = gen_frame_mem_offset (reg_mode, frame_reg_rtx,
21303 info->ehrd_offset + frame_off
21304 + reg_size * (int) i);
21306 emit_move_insn (gen_rtx_REG (reg_mode, regno), mem);
21310 /* Restore GPRs. This is done as a PARALLEL if we are using
21311 the load-multiple instructions. */
21312 if (TARGET_SPE_ABI
21313 && info->spe_64bit_regs_used
21314 && info->first_gp_reg_save != 32)
21316 /* Determine whether we can address all of the registers that need
21317 to be saved with an offset from frame_reg_rtx that fits in
21318 the small const field for SPE memory instructions. */
21319 int spe_regs_addressable
21320 = (SPE_CONST_OFFSET_OK (info->spe_gp_save_offset + frame_off
21321 + reg_size * (32 - info->first_gp_reg_save - 1))
21322 && restoring_GPRs_inline);
21324 if (!spe_regs_addressable)
21326 int ool_adjust = 0;
21327 rtx old_frame_reg_rtx = frame_reg_rtx;
21328 /* Make r11 point to the start of the SPE save area. We worried about
21329 not clobbering it when we were saving registers in the prologue.
21330 There's no need to worry here because the static chain is passed
21331 anew to every function. */
21333 if (!restoring_GPRs_inline)
21334 ool_adjust = 8 * (info->first_gp_reg_save
21335 - (FIRST_SAVRES_REGISTER + 1));
21336 frame_reg_rtx = gen_rtx_REG (Pmode, 11);
21337 emit_insn (gen_addsi3 (frame_reg_rtx, old_frame_reg_rtx,
21338 GEN_INT (info->spe_gp_save_offset
21339 + frame_off
21340 - ool_adjust)));
21341 /* Keep the invariant that frame_reg_rtx + frame_off points
21342 at the top of the stack frame. */
21343 frame_off = -info->spe_gp_save_offset + ool_adjust;
21346 if (restoring_GPRs_inline)
21348 HOST_WIDE_INT spe_offset = info->spe_gp_save_offset + frame_off;
21350 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
21351 if (rs6000_reg_live_or_pic_offset_p (info->first_gp_reg_save + i))
21353 rtx offset, addr, mem, reg;
21355 /* We're doing all this to ensure that the immediate offset
21356 fits into the immediate field of 'evldd'. */
21357 gcc_assert (SPE_CONST_OFFSET_OK (spe_offset + reg_size * i));
21359 offset = GEN_INT (spe_offset + reg_size * i);
21360 addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, offset);
21361 mem = gen_rtx_MEM (V2SImode, addr);
21362 reg = gen_rtx_REG (reg_mode, info->first_gp_reg_save + i);
21364 emit_move_insn (reg, mem);
21367 else
21368 rs6000_emit_savres_rtx (info, frame_reg_rtx,
21369 info->spe_gp_save_offset + frame_off,
21370 info->lr_save_offset + frame_off,
21371 reg_mode,
21372 SAVRES_GPR | SAVRES_LR);
21374 else if (!restoring_GPRs_inline)
21376 /* We are jumping to an out-of-line function. */
21377 rtx ptr_reg;
21378 int end_save = info->gp_save_offset + info->gp_size;
21379 bool can_use_exit = end_save == 0;
21380 int sel = SAVRES_GPR | (can_use_exit ? SAVRES_LR : 0);
21381 int ptr_off;
21383 /* Emit stack reset code if we need it. */
21384 ptr_regno = ptr_regno_for_savres (sel);
21385 ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
21386 if (can_use_exit)
21387 rs6000_emit_stack_reset (info, frame_reg_rtx, frame_off, ptr_regno);
21388 else if (end_save + frame_off != 0)
21389 emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx,
21390 GEN_INT (end_save + frame_off)));
21391 else if (REGNO (frame_reg_rtx) != ptr_regno)
21392 emit_move_insn (ptr_reg, frame_reg_rtx);
21393 if (REGNO (frame_reg_rtx) == ptr_regno)
21394 frame_off = -end_save;
21396 if (can_use_exit && info->cr_save_p)
21397 restore_saved_cr (cr_save_reg, using_mtcr_multiple, true);
21399 ptr_off = -end_save;
21400 rs6000_emit_savres_rtx (info, ptr_reg,
21401 info->gp_save_offset + ptr_off,
21402 info->lr_save_offset + ptr_off,
21403 reg_mode, sel);
21405 else if (using_load_multiple)
21407 rtvec p;
21408 p = rtvec_alloc (32 - info->first_gp_reg_save);
21409 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
21410 RTVEC_ELT (p, i)
21411 = gen_frame_load (gen_rtx_REG (reg_mode, info->first_gp_reg_save + i),
21412 frame_reg_rtx,
21413 info->gp_save_offset + frame_off + reg_size * i);
21414 emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
21416 else
21418 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
21419 if (rs6000_reg_live_or_pic_offset_p (info->first_gp_reg_save + i))
21420 emit_insn (gen_frame_load
21421 (gen_rtx_REG (reg_mode, info->first_gp_reg_save + i),
21422 frame_reg_rtx,
21423 info->gp_save_offset + frame_off + reg_size * i));
21426 if (DEFAULT_ABI == ABI_V4 || flag_shrink_wrap)
21428 /* If the frame pointer was used then we can't delay emitting
21429 a REG_CFA_DEF_CFA note. This must happen on the insn that
21430 restores the frame pointer, r31. We may have already emitted
21431 a REG_CFA_DEF_CFA note, but that's OK; A duplicate is
21432 discarded by dwarf2cfi.c/dwarf2out.c, and in any case would
21433 be harmless if emitted. */
21434 if (frame_pointer_needed)
21436 insn = get_last_insn ();
21437 add_reg_note (insn, REG_CFA_DEF_CFA,
21438 plus_constant (Pmode, frame_reg_rtx, frame_off));
21439 RTX_FRAME_RELATED_P (insn) = 1;
21442 /* Set up cfa_restores. We always need these when
21443 shrink-wrapping. If not shrink-wrapping then we only need
21444 the cfa_restore when the stack location is no longer valid.
21445 The cfa_restores must be emitted on or before the insn that
21446 invalidates the stack, and of course must not be emitted
21447 before the insn that actually does the restore. The latter
21448 is why it is a bad idea to emit the cfa_restores as a group
21449 on the last instruction here that actually does a restore:
21450 That insn may be reordered with respect to others doing
21451 restores. */
21452 if (flag_shrink_wrap
21453 && !restoring_GPRs_inline
21454 && info->first_fp_reg_save == 64)
21455 cfa_restores = add_crlr_cfa_restore (info, cfa_restores);
21457 for (i = info->first_gp_reg_save; i < 32; i++)
21458 if (!restoring_GPRs_inline
21459 || using_load_multiple
21460 || rs6000_reg_live_or_pic_offset_p (i))
21462 rtx reg = gen_rtx_REG (reg_mode, i);
21464 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
21468 if (!restoring_GPRs_inline
21469 && info->first_fp_reg_save == 64)
21471 /* We are jumping to an out-of-line function. */
21472 if (cfa_restores)
21473 emit_cfa_restores (cfa_restores);
21474 return;
21477 if (restore_lr && !restoring_GPRs_inline)
21479 load_lr_save (0, frame_reg_rtx, info->lr_save_offset + frame_off);
21480 restore_saved_lr (0, exit_func);
21483 /* Restore fpr's if we need to do it without calling a function. */
21484 if (restoring_FPRs_inline)
21485 for (i = 0; i < 64 - info->first_fp_reg_save; i++)
21486 if (save_reg_p (info->first_fp_reg_save + i))
21488 rtx reg = gen_rtx_REG ((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT
21489 ? DFmode : SFmode),
21490 info->first_fp_reg_save + i);
21491 emit_insn (gen_frame_load (reg, frame_reg_rtx,
21492 info->fp_save_offset + frame_off + 8 * i));
21493 if (DEFAULT_ABI == ABI_V4 || flag_shrink_wrap)
21494 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
21497 /* If we saved cr, restore it here. Just those that were used. */
21498 if (info->cr_save_p)
21499 restore_saved_cr (cr_save_reg, using_mtcr_multiple, exit_func);
21501 /* If this is V.4, unwind the stack pointer after all of the loads
21502 have been done, or set up r11 if we are restoring fp out of line. */
21503 ptr_regno = 1;
21504 if (!restoring_FPRs_inline)
21506 bool lr = (strategy & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR) == 0;
21507 int sel = SAVRES_FPR | (lr ? SAVRES_LR : 0);
21508 ptr_regno = ptr_regno_for_savres (sel);
21511 insn = rs6000_emit_stack_reset (info, frame_reg_rtx, frame_off, ptr_regno);
21512 if (REGNO (frame_reg_rtx) == ptr_regno)
21513 frame_off = 0;
21515 if (insn && restoring_FPRs_inline)
21517 if (cfa_restores)
21519 REG_NOTES (insn) = cfa_restores;
21520 cfa_restores = NULL_RTX;
21522 add_reg_note (insn, REG_CFA_DEF_CFA, sp_reg_rtx);
21523 RTX_FRAME_RELATED_P (insn) = 1;
21526 if (crtl->calls_eh_return)
21528 rtx sa = EH_RETURN_STACKADJ_RTX;
21529 emit_insn (gen_add3_insn (sp_reg_rtx, sp_reg_rtx, sa));
21532 if (!sibcall)
21534 rtvec p;
21535 bool lr = (strategy & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR) == 0;
21536 if (! restoring_FPRs_inline)
21538 p = rtvec_alloc (4 + 64 - info->first_fp_reg_save);
21539 RTVEC_ELT (p, 0) = ret_rtx;
21541 else
21543 if (cfa_restores)
21545 /* We can't hang the cfa_restores off a simple return,
21546 since the shrink-wrap code sometimes uses an existing
21547 return. This means there might be a path from
21548 pre-prologue code to this return, and dwarf2cfi code
21549 wants the eh_frame unwinder state to be the same on
21550 all paths to any point. So we need to emit the
21551 cfa_restores before the return. For -m64 we really
21552 don't need epilogue cfa_restores at all, except for
21553 this irritating dwarf2cfi with shrink-wrap
21554 requirement; The stack red-zone means eh_frame info
21555 from the prologue telling the unwinder to restore
21556 from the stack is perfectly good right to the end of
21557 the function. */
21558 emit_insn (gen_blockage ());
21559 emit_cfa_restores (cfa_restores);
21560 cfa_restores = NULL_RTX;
21562 p = rtvec_alloc (2);
21563 RTVEC_ELT (p, 0) = simple_return_rtx;
21566 RTVEC_ELT (p, 1) = ((restoring_FPRs_inline || !lr)
21567 ? gen_rtx_USE (VOIDmode,
21568 gen_rtx_REG (Pmode, LR_REGNO))
21569 : gen_rtx_CLOBBER (VOIDmode,
21570 gen_rtx_REG (Pmode, LR_REGNO)));
21572 /* If we have to restore more than two FP registers, branch to the
21573 restore function. It will return to our caller. */
21574 if (! restoring_FPRs_inline)
21576 int i;
21577 rtx sym;
21579 if (flag_shrink_wrap)
21580 cfa_restores = add_crlr_cfa_restore (info, cfa_restores);
21582 sym = rs6000_savres_routine_sym (info,
21583 SAVRES_FPR | (lr ? SAVRES_LR : 0));
21584 RTVEC_ELT (p, 2) = gen_rtx_USE (VOIDmode, sym);
21585 RTVEC_ELT (p, 3) = gen_rtx_USE (VOIDmode,
21586 gen_rtx_REG (Pmode,
21587 DEFAULT_ABI == ABI_AIX
21588 ? 1 : 11));
21589 for (i = 0; i < 64 - info->first_fp_reg_save; i++)
21591 rtx reg = gen_rtx_REG (DFmode, info->first_fp_reg_save + i);
21593 RTVEC_ELT (p, i + 4)
21594 = gen_frame_load (reg, sp_reg_rtx, info->fp_save_offset + 8 * i);
21595 if (flag_shrink_wrap)
21596 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg,
21597 cfa_restores);
21601 emit_jump_insn (gen_rtx_PARALLEL (VOIDmode, p));
21604 if (cfa_restores)
21606 if (sibcall)
21607 /* Ensure the cfa_restores are hung off an insn that won't
21608 be reordered above other restores. */
21609 emit_insn (gen_blockage ());
21611 emit_cfa_restores (cfa_restores);
21615 /* Write function epilogue. */
21617 static void
21618 rs6000_output_function_epilogue (FILE *file,
21619 HOST_WIDE_INT size ATTRIBUTE_UNUSED)
21621 #if TARGET_MACHO
21622 macho_branch_islands ();
21623 /* Mach-O doesn't support labels at the end of objects, so if
21624 it looks like we might want one, insert a NOP. */
21626 rtx insn = get_last_insn ();
21627 rtx deleted_debug_label = NULL_RTX;
21628 while (insn
21629 && NOTE_P (insn)
21630 && NOTE_KIND (insn) != NOTE_INSN_DELETED_LABEL)
21632 /* Don't insert a nop for NOTE_INSN_DELETED_DEBUG_LABEL
21633 notes only, instead set their CODE_LABEL_NUMBER to -1,
21634 otherwise there would be code generation differences
21635 in between -g and -g0. */
21636 if (NOTE_P (insn) && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL)
21637 deleted_debug_label = insn;
21638 insn = PREV_INSN (insn);
21640 if (insn
21641 && (LABEL_P (insn)
21642 || (NOTE_P (insn)
21643 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL)))
21644 fputs ("\tnop\n", file);
21645 else if (deleted_debug_label)
21646 for (insn = deleted_debug_label; insn; insn = NEXT_INSN (insn))
21647 if (NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL)
21648 CODE_LABEL_NUMBER (insn) = -1;
21650 #endif
21652 /* Output a traceback table here. See /usr/include/sys/debug.h for info
21653 on its format.
21655 We don't output a traceback table if -finhibit-size-directive was
21656 used. The documentation for -finhibit-size-directive reads
21657 ``don't output a @code{.size} assembler directive, or anything
21658 else that would cause trouble if the function is split in the
21659 middle, and the two halves are placed at locations far apart in
21660 memory.'' The traceback table has this property, since it
21661 includes the offset from the start of the function to the
21662 traceback table itself.
21664 System V.4 Powerpc's (and the embedded ABI derived from it) use a
21665 different traceback table. */
21666 if (DEFAULT_ABI == ABI_AIX && ! flag_inhibit_size_directive
21667 && rs6000_traceback != traceback_none && !cfun->is_thunk)
21669 const char *fname = NULL;
21670 const char *language_string = lang_hooks.name;
21671 int fixed_parms = 0, float_parms = 0, parm_info = 0;
21672 int i;
21673 int optional_tbtab;
21674 rs6000_stack_t *info = rs6000_stack_info ();
21676 if (rs6000_traceback == traceback_full)
21677 optional_tbtab = 1;
21678 else if (rs6000_traceback == traceback_part)
21679 optional_tbtab = 0;
21680 else
21681 optional_tbtab = !optimize_size && !TARGET_ELF;
21683 if (optional_tbtab)
21685 fname = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
21686 while (*fname == '.') /* V.4 encodes . in the name */
21687 fname++;
21689 /* Need label immediately before tbtab, so we can compute
21690 its offset from the function start. */
21691 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LT");
21692 ASM_OUTPUT_LABEL (file, fname);
21695 /* The .tbtab pseudo-op can only be used for the first eight
21696 expressions, since it can't handle the possibly variable
21697 length fields that follow. However, if you omit the optional
21698 fields, the assembler outputs zeros for all optional fields
21699 anyways, giving each variable length field is minimum length
21700 (as defined in sys/debug.h). Thus we can not use the .tbtab
21701 pseudo-op at all. */
21703 /* An all-zero word flags the start of the tbtab, for debuggers
21704 that have to find it by searching forward from the entry
21705 point or from the current pc. */
21706 fputs ("\t.long 0\n", file);
21708 /* Tbtab format type. Use format type 0. */
21709 fputs ("\t.byte 0,", file);
21711 /* Language type. Unfortunately, there does not seem to be any
21712 official way to discover the language being compiled, so we
21713 use language_string.
21714 C is 0. Fortran is 1. Pascal is 2. Ada is 3. C++ is 9.
21715 Java is 13. Objective-C is 14. Objective-C++ isn't assigned
21716 a number, so for now use 9. LTO and Go aren't assigned numbers
21717 either, so for now use 0. */
21718 if (! strcmp (language_string, "GNU C")
21719 || ! strcmp (language_string, "GNU GIMPLE")
21720 || ! strcmp (language_string, "GNU Go"))
21721 i = 0;
21722 else if (! strcmp (language_string, "GNU F77")
21723 || ! strcmp (language_string, "GNU Fortran"))
21724 i = 1;
21725 else if (! strcmp (language_string, "GNU Pascal"))
21726 i = 2;
21727 else if (! strcmp (language_string, "GNU Ada"))
21728 i = 3;
21729 else if (! strcmp (language_string, "GNU C++")
21730 || ! strcmp (language_string, "GNU Objective-C++"))
21731 i = 9;
21732 else if (! strcmp (language_string, "GNU Java"))
21733 i = 13;
21734 else if (! strcmp (language_string, "GNU Objective-C"))
21735 i = 14;
21736 else
21737 gcc_unreachable ();
21738 fprintf (file, "%d,", i);
21740 /* 8 single bit fields: global linkage (not set for C extern linkage,
21741 apparently a PL/I convention?), out-of-line epilogue/prologue, offset
21742 from start of procedure stored in tbtab, internal function, function
21743 has controlled storage, function has no toc, function uses fp,
21744 function logs/aborts fp operations. */
21745 /* Assume that fp operations are used if any fp reg must be saved. */
21746 fprintf (file, "%d,",
21747 (optional_tbtab << 5) | ((info->first_fp_reg_save != 64) << 1));
21749 /* 6 bitfields: function is interrupt handler, name present in
21750 proc table, function calls alloca, on condition directives
21751 (controls stack walks, 3 bits), saves condition reg, saves
21752 link reg. */
21753 /* The `function calls alloca' bit seems to be set whenever reg 31 is
21754 set up as a frame pointer, even when there is no alloca call. */
21755 fprintf (file, "%d,",
21756 ((optional_tbtab << 6)
21757 | ((optional_tbtab & frame_pointer_needed) << 5)
21758 | (info->cr_save_p << 1)
21759 | (info->lr_save_p)));
21761 /* 3 bitfields: saves backchain, fixup code, number of fpr saved
21762 (6 bits). */
21763 fprintf (file, "%d,",
21764 (info->push_p << 7) | (64 - info->first_fp_reg_save));
21766 /* 2 bitfields: spare bits (2 bits), number of gpr saved (6 bits). */
21767 fprintf (file, "%d,", (32 - first_reg_to_save ()));
21769 if (optional_tbtab)
21771 /* Compute the parameter info from the function decl argument
21772 list. */
21773 tree decl;
21774 int next_parm_info_bit = 31;
21776 for (decl = DECL_ARGUMENTS (current_function_decl);
21777 decl; decl = DECL_CHAIN (decl))
21779 rtx parameter = DECL_INCOMING_RTL (decl);
21780 enum machine_mode mode = GET_MODE (parameter);
21782 if (GET_CODE (parameter) == REG)
21784 if (SCALAR_FLOAT_MODE_P (mode))
21786 int bits;
21788 float_parms++;
21790 switch (mode)
21792 case SFmode:
21793 case SDmode:
21794 bits = 0x2;
21795 break;
21797 case DFmode:
21798 case DDmode:
21799 case TFmode:
21800 case TDmode:
21801 bits = 0x3;
21802 break;
21804 default:
21805 gcc_unreachable ();
21808 /* If only one bit will fit, don't or in this entry. */
21809 if (next_parm_info_bit > 0)
21810 parm_info |= (bits << (next_parm_info_bit - 1));
21811 next_parm_info_bit -= 2;
21813 else
21815 fixed_parms += ((GET_MODE_SIZE (mode)
21816 + (UNITS_PER_WORD - 1))
21817 / UNITS_PER_WORD);
21818 next_parm_info_bit -= 1;
21824 /* Number of fixed point parameters. */
21825 /* This is actually the number of words of fixed point parameters; thus
21826 an 8 byte struct counts as 2; and thus the maximum value is 8. */
21827 fprintf (file, "%d,", fixed_parms);
21829 /* 2 bitfields: number of floating point parameters (7 bits), parameters
21830 all on stack. */
21831 /* This is actually the number of fp registers that hold parameters;
21832 and thus the maximum value is 13. */
21833 /* Set parameters on stack bit if parameters are not in their original
21834 registers, regardless of whether they are on the stack? Xlc
21835 seems to set the bit when not optimizing. */
21836 fprintf (file, "%d\n", ((float_parms << 1) | (! optimize)));
21838 if (! optional_tbtab)
21839 return;
21841 /* Optional fields follow. Some are variable length. */
21843 /* Parameter types, left adjusted bit fields: 0 fixed, 10 single float,
21844 11 double float. */
21845 /* There is an entry for each parameter in a register, in the order that
21846 they occur in the parameter list. Any intervening arguments on the
21847 stack are ignored. If the list overflows a long (max possible length
21848 34 bits) then completely leave off all elements that don't fit. */
21849 /* Only emit this long if there was at least one parameter. */
21850 if (fixed_parms || float_parms)
21851 fprintf (file, "\t.long %d\n", parm_info);
21853 /* Offset from start of code to tb table. */
21854 fputs ("\t.long ", file);
21855 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LT");
21856 RS6000_OUTPUT_BASENAME (file, fname);
21857 putc ('-', file);
21858 rs6000_output_function_entry (file, fname);
21859 putc ('\n', file);
21861 /* Interrupt handler mask. */
21862 /* Omit this long, since we never set the interrupt handler bit
21863 above. */
21865 /* Number of CTL (controlled storage) anchors. */
21866 /* Omit this long, since the has_ctl bit is never set above. */
21868 /* Displacement into stack of each CTL anchor. */
21869 /* Omit this list of longs, because there are no CTL anchors. */
21871 /* Length of function name. */
21872 if (*fname == '*')
21873 ++fname;
21874 fprintf (file, "\t.short %d\n", (int) strlen (fname));
21876 /* Function name. */
21877 assemble_string (fname, strlen (fname));
21879 /* Register for alloca automatic storage; this is always reg 31.
21880 Only emit this if the alloca bit was set above. */
21881 if (frame_pointer_needed)
21882 fputs ("\t.byte 31\n", file);
21884 fputs ("\t.align 2\n", file);
21888 /* A C compound statement that outputs the assembler code for a thunk
21889 function, used to implement C++ virtual function calls with
21890 multiple inheritance. The thunk acts as a wrapper around a virtual
21891 function, adjusting the implicit object parameter before handing
21892 control off to the real function.
21894 First, emit code to add the integer DELTA to the location that
21895 contains the incoming first argument. Assume that this argument
21896 contains a pointer, and is the one used to pass the `this' pointer
21897 in C++. This is the incoming argument *before* the function
21898 prologue, e.g. `%o0' on a sparc. The addition must preserve the
21899 values of all other incoming arguments.
21901 After the addition, emit code to jump to FUNCTION, which is a
21902 `FUNCTION_DECL'. This is a direct pure jump, not a call, and does
21903 not touch the return address. Hence returning from FUNCTION will
21904 return to whoever called the current `thunk'.
21906 The effect must be as if FUNCTION had been called directly with the
21907 adjusted first argument. This macro is responsible for emitting
21908 all of the code for a thunk function; output_function_prologue()
21909 and output_function_epilogue() are not invoked.
21911 The THUNK_FNDECL is redundant. (DELTA and FUNCTION have already
21912 been extracted from it.) It might possibly be useful on some
21913 targets, but probably not.
21915 If you do not define this macro, the target-independent code in the
21916 C++ frontend will generate a less efficient heavyweight thunk that
21917 calls FUNCTION instead of jumping to it. The generic approach does
21918 not support varargs. */
21920 static void
21921 rs6000_output_mi_thunk (FILE *file, tree thunk_fndecl ATTRIBUTE_UNUSED,
21922 HOST_WIDE_INT delta, HOST_WIDE_INT vcall_offset,
21923 tree function)
21925 rtx this_rtx, insn, funexp;
21927 reload_completed = 1;
21928 epilogue_completed = 1;
21930 /* Mark the end of the (empty) prologue. */
21931 emit_note (NOTE_INSN_PROLOGUE_END);
21933 /* Find the "this" pointer. If the function returns a structure,
21934 the structure return pointer is in r3. */
21935 if (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function))
21936 this_rtx = gen_rtx_REG (Pmode, 4);
21937 else
21938 this_rtx = gen_rtx_REG (Pmode, 3);
21940 /* Apply the constant offset, if required. */
21941 if (delta)
21942 emit_insn (gen_add3_insn (this_rtx, this_rtx, GEN_INT (delta)));
21944 /* Apply the offset from the vtable, if required. */
21945 if (vcall_offset)
21947 rtx vcall_offset_rtx = GEN_INT (vcall_offset);
21948 rtx tmp = gen_rtx_REG (Pmode, 12);
21950 emit_move_insn (tmp, gen_rtx_MEM (Pmode, this_rtx));
21951 if (((unsigned HOST_WIDE_INT) vcall_offset) + 0x8000 >= 0x10000)
21953 emit_insn (gen_add3_insn (tmp, tmp, vcall_offset_rtx));
21954 emit_move_insn (tmp, gen_rtx_MEM (Pmode, tmp));
21956 else
21958 rtx loc = gen_rtx_PLUS (Pmode, tmp, vcall_offset_rtx);
21960 emit_move_insn (tmp, gen_rtx_MEM (Pmode, loc));
21962 emit_insn (gen_add3_insn (this_rtx, this_rtx, tmp));
21965 /* Generate a tail call to the target function. */
21966 if (!TREE_USED (function))
21968 assemble_external (function);
21969 TREE_USED (function) = 1;
21971 funexp = XEXP (DECL_RTL (function), 0);
21972 funexp = gen_rtx_MEM (FUNCTION_MODE, funexp);
21974 #if TARGET_MACHO
21975 if (MACHOPIC_INDIRECT)
21976 funexp = machopic_indirect_call_target (funexp);
21977 #endif
21979 /* gen_sibcall expects reload to convert scratch pseudo to LR so we must
21980 generate sibcall RTL explicitly. */
21981 insn = emit_call_insn (
21982 gen_rtx_PARALLEL (VOIDmode,
21983 gen_rtvec (4,
21984 gen_rtx_CALL (VOIDmode,
21985 funexp, const0_rtx),
21986 gen_rtx_USE (VOIDmode, const0_rtx),
21987 gen_rtx_USE (VOIDmode,
21988 gen_rtx_REG (SImode,
21989 LR_REGNO)),
21990 simple_return_rtx)));
21991 SIBLING_CALL_P (insn) = 1;
21992 emit_barrier ();
21994 /* Run just enough of rest_of_compilation to get the insns emitted.
21995 There's not really enough bulk here to make other passes such as
21996 instruction scheduling worth while. Note that use_thunk calls
21997 assemble_start_function and assemble_end_function. */
21998 insn = get_insns ();
21999 shorten_branches (insn);
22000 final_start_function (insn, file, 1);
22001 final (insn, file, 1);
22002 final_end_function ();
22004 reload_completed = 0;
22005 epilogue_completed = 0;
22008 /* A quick summary of the various types of 'constant-pool tables'
22009 under PowerPC:
22011 Target Flags Name One table per
22012 AIX (none) AIX TOC object file
22013 AIX -mfull-toc AIX TOC object file
22014 AIX -mminimal-toc AIX minimal TOC translation unit
22015 SVR4/EABI (none) SVR4 SDATA object file
22016 SVR4/EABI -fpic SVR4 pic object file
22017 SVR4/EABI -fPIC SVR4 PIC translation unit
22018 SVR4/EABI -mrelocatable EABI TOC function
22019 SVR4/EABI -maix AIX TOC object file
22020 SVR4/EABI -maix -mminimal-toc
22021 AIX minimal TOC translation unit
22023 Name Reg. Set by entries contains:
22024 made by addrs? fp? sum?
22026 AIX TOC 2 crt0 as Y option option
22027 AIX minimal TOC 30 prolog gcc Y Y option
22028 SVR4 SDATA 13 crt0 gcc N Y N
22029 SVR4 pic 30 prolog ld Y not yet N
22030 SVR4 PIC 30 prolog gcc Y option option
22031 EABI TOC 30 prolog gcc Y option option
22035 /* Hash functions for the hash table. */
22037 static unsigned
22038 rs6000_hash_constant (rtx k)
22040 enum rtx_code code = GET_CODE (k);
22041 enum machine_mode mode = GET_MODE (k);
22042 unsigned result = (code << 3) ^ mode;
22043 const char *format;
22044 int flen, fidx;
22046 format = GET_RTX_FORMAT (code);
22047 flen = strlen (format);
22048 fidx = 0;
22050 switch (code)
22052 case LABEL_REF:
22053 return result * 1231 + (unsigned) INSN_UID (XEXP (k, 0));
22055 case CONST_DOUBLE:
22056 if (mode != VOIDmode)
22057 return real_hash (CONST_DOUBLE_REAL_VALUE (k)) * result;
22058 flen = 2;
22059 break;
22061 case CODE_LABEL:
22062 fidx = 3;
22063 break;
22065 default:
22066 break;
22069 for (; fidx < flen; fidx++)
22070 switch (format[fidx])
22072 case 's':
22074 unsigned i, len;
22075 const char *str = XSTR (k, fidx);
22076 len = strlen (str);
22077 result = result * 613 + len;
22078 for (i = 0; i < len; i++)
22079 result = result * 613 + (unsigned) str[i];
22080 break;
22082 case 'u':
22083 case 'e':
22084 result = result * 1231 + rs6000_hash_constant (XEXP (k, fidx));
22085 break;
22086 case 'i':
22087 case 'n':
22088 result = result * 613 + (unsigned) XINT (k, fidx);
22089 break;
22090 case 'w':
22091 if (sizeof (unsigned) >= sizeof (HOST_WIDE_INT))
22092 result = result * 613 + (unsigned) XWINT (k, fidx);
22093 else
22095 size_t i;
22096 for (i = 0; i < sizeof (HOST_WIDE_INT) / sizeof (unsigned); i++)
22097 result = result * 613 + (unsigned) (XWINT (k, fidx)
22098 >> CHAR_BIT * i);
22100 break;
22101 case '0':
22102 break;
22103 default:
22104 gcc_unreachable ();
22107 return result;
22110 static unsigned
22111 toc_hash_function (const void *hash_entry)
22113 const struct toc_hash_struct *thc =
22114 (const struct toc_hash_struct *) hash_entry;
22115 return rs6000_hash_constant (thc->key) ^ thc->key_mode;
22118 /* Compare H1 and H2 for equivalence. */
22120 static int
22121 toc_hash_eq (const void *h1, const void *h2)
22123 rtx r1 = ((const struct toc_hash_struct *) h1)->key;
22124 rtx r2 = ((const struct toc_hash_struct *) h2)->key;
22126 if (((const struct toc_hash_struct *) h1)->key_mode
22127 != ((const struct toc_hash_struct *) h2)->key_mode)
22128 return 0;
22130 return rtx_equal_p (r1, r2);
22133 /* These are the names given by the C++ front-end to vtables, and
22134 vtable-like objects. Ideally, this logic should not be here;
22135 instead, there should be some programmatic way of inquiring as
22136 to whether or not an object is a vtable. */
22138 #define VTABLE_NAME_P(NAME) \
22139 (strncmp ("_vt.", name, strlen ("_vt.")) == 0 \
22140 || strncmp ("_ZTV", name, strlen ("_ZTV")) == 0 \
22141 || strncmp ("_ZTT", name, strlen ("_ZTT")) == 0 \
22142 || strncmp ("_ZTI", name, strlen ("_ZTI")) == 0 \
22143 || strncmp ("_ZTC", name, strlen ("_ZTC")) == 0)
22145 #ifdef NO_DOLLAR_IN_LABEL
22146 /* Return a GGC-allocated character string translating dollar signs in
22147 input NAME to underscores. Used by XCOFF ASM_OUTPUT_LABELREF. */
22149 const char *
22150 rs6000_xcoff_strip_dollar (const char *name)
22152 char *strip, *p;
22153 const char *q;
22154 size_t len;
22156 q = (const char *) strchr (name, '$');
22158 if (q == 0 || q == name)
22159 return name;
22161 len = strlen (name);
22162 strip = XALLOCAVEC (char, len + 1);
22163 strcpy (strip, name);
22164 p = strip + (q - name);
22165 while (p)
22167 *p = '_';
22168 p = strchr (p + 1, '$');
22171 return ggc_alloc_string (strip, len);
22173 #endif
22175 void
22176 rs6000_output_symbol_ref (FILE *file, rtx x)
22178 /* Currently C++ toc references to vtables can be emitted before it
22179 is decided whether the vtable is public or private. If this is
22180 the case, then the linker will eventually complain that there is
22181 a reference to an unknown section. Thus, for vtables only,
22182 we emit the TOC reference to reference the symbol and not the
22183 section. */
22184 const char *name = XSTR (x, 0);
22186 if (VTABLE_NAME_P (name))
22188 RS6000_OUTPUT_BASENAME (file, name);
22190 else
22191 assemble_name (file, name);
22194 /* Output a TOC entry. We derive the entry name from what is being
22195 written. */
22197 void
22198 output_toc (FILE *file, rtx x, int labelno, enum machine_mode mode)
22200 char buf[256];
22201 const char *name = buf;
22202 rtx base = x;
22203 HOST_WIDE_INT offset = 0;
22205 gcc_assert (!TARGET_NO_TOC);
22207 /* When the linker won't eliminate them, don't output duplicate
22208 TOC entries (this happens on AIX if there is any kind of TOC,
22209 and on SVR4 under -fPIC or -mrelocatable). Don't do this for
22210 CODE_LABELs. */
22211 if (TARGET_TOC && GET_CODE (x) != LABEL_REF)
22213 struct toc_hash_struct *h;
22214 void * * found;
22216 /* Create toc_hash_table. This can't be done at TARGET_OPTION_OVERRIDE
22217 time because GGC is not initialized at that point. */
22218 if (toc_hash_table == NULL)
22219 toc_hash_table = htab_create_ggc (1021, toc_hash_function,
22220 toc_hash_eq, NULL);
22222 h = ggc_alloc_toc_hash_struct ();
22223 h->key = x;
22224 h->key_mode = mode;
22225 h->labelno = labelno;
22227 found = htab_find_slot (toc_hash_table, h, INSERT);
22228 if (*found == NULL)
22229 *found = h;
22230 else /* This is indeed a duplicate.
22231 Set this label equal to that label. */
22233 fputs ("\t.set ", file);
22234 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LC");
22235 fprintf (file, "%d,", labelno);
22236 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LC");
22237 fprintf (file, "%d\n", ((*(const struct toc_hash_struct **)
22238 found)->labelno));
22240 #ifdef HAVE_AS_TLS
22241 if (TARGET_XCOFF && GET_CODE (x) == SYMBOL_REF
22242 && (SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_GLOBAL_DYNAMIC
22243 || SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_DYNAMIC))
22245 fputs ("\t.set ", file);
22246 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LCM");
22247 fprintf (file, "%d,", labelno);
22248 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LCM");
22249 fprintf (file, "%d\n", ((*(const struct toc_hash_struct **)
22250 found)->labelno));
22252 #endif
22253 return;
22257 /* If we're going to put a double constant in the TOC, make sure it's
22258 aligned properly when strict alignment is on. */
22259 if (GET_CODE (x) == CONST_DOUBLE
22260 && STRICT_ALIGNMENT
22261 && GET_MODE_BITSIZE (mode) >= 64
22262 && ! (TARGET_NO_FP_IN_TOC && ! TARGET_MINIMAL_TOC)) {
22263 ASM_OUTPUT_ALIGN (file, 3);
22266 (*targetm.asm_out.internal_label) (file, "LC", labelno);
22268 /* Handle FP constants specially. Note that if we have a minimal
22269 TOC, things we put here aren't actually in the TOC, so we can allow
22270 FP constants. */
22271 if (GET_CODE (x) == CONST_DOUBLE &&
22272 (GET_MODE (x) == TFmode || GET_MODE (x) == TDmode))
22274 REAL_VALUE_TYPE rv;
22275 long k[4];
22277 REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
22278 if (DECIMAL_FLOAT_MODE_P (GET_MODE (x)))
22279 REAL_VALUE_TO_TARGET_DECIMAL128 (rv, k);
22280 else
22281 REAL_VALUE_TO_TARGET_LONG_DOUBLE (rv, k);
22283 if (TARGET_64BIT)
22285 if (TARGET_ELF || TARGET_MINIMAL_TOC)
22286 fputs (DOUBLE_INT_ASM_OP, file);
22287 else
22288 fprintf (file, "\t.tc FT_%lx_%lx_%lx_%lx[TC],",
22289 k[0] & 0xffffffff, k[1] & 0xffffffff,
22290 k[2] & 0xffffffff, k[3] & 0xffffffff);
22291 fprintf (file, "0x%lx%08lx,0x%lx%08lx\n",
22292 k[WORDS_BIG_ENDIAN ? 0 : 1] & 0xffffffff,
22293 k[WORDS_BIG_ENDIAN ? 1 : 0] & 0xffffffff,
22294 k[WORDS_BIG_ENDIAN ? 2 : 3] & 0xffffffff,
22295 k[WORDS_BIG_ENDIAN ? 3 : 2] & 0xffffffff);
22296 return;
22298 else
22300 if (TARGET_ELF || TARGET_MINIMAL_TOC)
22301 fputs ("\t.long ", file);
22302 else
22303 fprintf (file, "\t.tc FT_%lx_%lx_%lx_%lx[TC],",
22304 k[0] & 0xffffffff, k[1] & 0xffffffff,
22305 k[2] & 0xffffffff, k[3] & 0xffffffff);
22306 fprintf (file, "0x%lx,0x%lx,0x%lx,0x%lx\n",
22307 k[0] & 0xffffffff, k[1] & 0xffffffff,
22308 k[2] & 0xffffffff, k[3] & 0xffffffff);
22309 return;
22312 else if (GET_CODE (x) == CONST_DOUBLE &&
22313 (GET_MODE (x) == DFmode || GET_MODE (x) == DDmode))
22315 REAL_VALUE_TYPE rv;
22316 long k[2];
22318 REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
22320 if (DECIMAL_FLOAT_MODE_P (GET_MODE (x)))
22321 REAL_VALUE_TO_TARGET_DECIMAL64 (rv, k);
22322 else
22323 REAL_VALUE_TO_TARGET_DOUBLE (rv, k);
22325 if (TARGET_64BIT)
22327 if (TARGET_ELF || TARGET_MINIMAL_TOC)
22328 fputs (DOUBLE_INT_ASM_OP, file);
22329 else
22330 fprintf (file, "\t.tc FD_%lx_%lx[TC],",
22331 k[0] & 0xffffffff, k[1] & 0xffffffff);
22332 fprintf (file, "0x%lx%08lx\n",
22333 k[WORDS_BIG_ENDIAN ? 0 : 1] & 0xffffffff,
22334 k[WORDS_BIG_ENDIAN ? 1 : 0] & 0xffffffff);
22335 return;
22337 else
22339 if (TARGET_ELF || TARGET_MINIMAL_TOC)
22340 fputs ("\t.long ", file);
22341 else
22342 fprintf (file, "\t.tc FD_%lx_%lx[TC],",
22343 k[0] & 0xffffffff, k[1] & 0xffffffff);
22344 fprintf (file, "0x%lx,0x%lx\n",
22345 k[0] & 0xffffffff, k[1] & 0xffffffff);
22346 return;
22349 else if (GET_CODE (x) == CONST_DOUBLE &&
22350 (GET_MODE (x) == SFmode || GET_MODE (x) == SDmode))
22352 REAL_VALUE_TYPE rv;
22353 long l;
22355 REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
22356 if (DECIMAL_FLOAT_MODE_P (GET_MODE (x)))
22357 REAL_VALUE_TO_TARGET_DECIMAL32 (rv, l);
22358 else
22359 REAL_VALUE_TO_TARGET_SINGLE (rv, l);
22361 if (TARGET_64BIT)
22363 if (TARGET_ELF || TARGET_MINIMAL_TOC)
22364 fputs (DOUBLE_INT_ASM_OP, file);
22365 else
22366 fprintf (file, "\t.tc FS_%lx[TC],", l & 0xffffffff);
22367 fprintf (file, "0x%lx00000000\n", l & 0xffffffff);
22368 return;
22370 else
22372 if (TARGET_ELF || TARGET_MINIMAL_TOC)
22373 fputs ("\t.long ", file);
22374 else
22375 fprintf (file, "\t.tc FS_%lx[TC],", l & 0xffffffff);
22376 fprintf (file, "0x%lx\n", l & 0xffffffff);
22377 return;
22380 else if (GET_MODE (x) == VOIDmode && GET_CODE (x) == CONST_INT)
22382 unsigned HOST_WIDE_INT low;
22383 HOST_WIDE_INT high;
22385 low = INTVAL (x) & 0xffffffff;
22386 high = (HOST_WIDE_INT) INTVAL (x) >> 32;
22388 /* TOC entries are always Pmode-sized, so when big-endian
22389 smaller integer constants in the TOC need to be padded.
22390 (This is still a win over putting the constants in
22391 a separate constant pool, because then we'd have
22392 to have both a TOC entry _and_ the actual constant.)
22394 For a 32-bit target, CONST_INT values are loaded and shifted
22395 entirely within `low' and can be stored in one TOC entry. */
22397 /* It would be easy to make this work, but it doesn't now. */
22398 gcc_assert (!TARGET_64BIT || POINTER_SIZE >= GET_MODE_BITSIZE (mode));
22400 if (WORDS_BIG_ENDIAN && POINTER_SIZE > GET_MODE_BITSIZE (mode))
22402 low |= high << 32;
22403 low <<= POINTER_SIZE - GET_MODE_BITSIZE (mode);
22404 high = (HOST_WIDE_INT) low >> 32;
22405 low &= 0xffffffff;
22408 if (TARGET_64BIT)
22410 if (TARGET_ELF || TARGET_MINIMAL_TOC)
22411 fputs (DOUBLE_INT_ASM_OP, file);
22412 else
22413 fprintf (file, "\t.tc ID_%lx_%lx[TC],",
22414 (long) high & 0xffffffff, (long) low & 0xffffffff);
22415 fprintf (file, "0x%lx%08lx\n",
22416 (long) high & 0xffffffff, (long) low & 0xffffffff);
22417 return;
22419 else
22421 if (POINTER_SIZE < GET_MODE_BITSIZE (mode))
22423 if (TARGET_ELF || TARGET_MINIMAL_TOC)
22424 fputs ("\t.long ", file);
22425 else
22426 fprintf (file, "\t.tc ID_%lx_%lx[TC],",
22427 (long) high & 0xffffffff, (long) low & 0xffffffff);
22428 fprintf (file, "0x%lx,0x%lx\n",
22429 (long) high & 0xffffffff, (long) low & 0xffffffff);
22431 else
22433 if (TARGET_ELF || TARGET_MINIMAL_TOC)
22434 fputs ("\t.long ", file);
22435 else
22436 fprintf (file, "\t.tc IS_%lx[TC],", (long) low & 0xffffffff);
22437 fprintf (file, "0x%lx\n", (long) low & 0xffffffff);
22439 return;
22443 if (GET_CODE (x) == CONST)
22445 gcc_assert (GET_CODE (XEXP (x, 0)) == PLUS
22446 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT);
22448 base = XEXP (XEXP (x, 0), 0);
22449 offset = INTVAL (XEXP (XEXP (x, 0), 1));
22452 switch (GET_CODE (base))
22454 case SYMBOL_REF:
22455 name = XSTR (base, 0);
22456 break;
22458 case LABEL_REF:
22459 ASM_GENERATE_INTERNAL_LABEL (buf, "L",
22460 CODE_LABEL_NUMBER (XEXP (base, 0)));
22461 break;
22463 case CODE_LABEL:
22464 ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (base));
22465 break;
22467 default:
22468 gcc_unreachable ();
22471 if (TARGET_ELF || TARGET_MINIMAL_TOC)
22472 fputs (TARGET_32BIT ? "\t.long " : DOUBLE_INT_ASM_OP, file);
22473 else
22475 fputs ("\t.tc ", file);
22476 RS6000_OUTPUT_BASENAME (file, name);
22478 if (offset < 0)
22479 fprintf (file, ".N" HOST_WIDE_INT_PRINT_UNSIGNED, - offset);
22480 else if (offset)
22481 fprintf (file, ".P" HOST_WIDE_INT_PRINT_UNSIGNED, offset);
22483 /* Mark large TOC symbols on AIX with [TE] so they are mapped
22484 after other TOC symbols, reducing overflow of small TOC access
22485 to [TC] symbols. */
22486 fputs (TARGET_XCOFF && TARGET_CMODEL != CMODEL_SMALL
22487 ? "[TE]," : "[TC],", file);
22490 /* Currently C++ toc references to vtables can be emitted before it
22491 is decided whether the vtable is public or private. If this is
22492 the case, then the linker will eventually complain that there is
22493 a TOC reference to an unknown section. Thus, for vtables only,
22494 we emit the TOC reference to reference the symbol and not the
22495 section. */
22496 if (VTABLE_NAME_P (name))
22498 RS6000_OUTPUT_BASENAME (file, name);
22499 if (offset < 0)
22500 fprintf (file, HOST_WIDE_INT_PRINT_DEC, offset);
22501 else if (offset > 0)
22502 fprintf (file, "+" HOST_WIDE_INT_PRINT_DEC, offset);
22504 else
22505 output_addr_const (file, x);
22507 #if HAVE_AS_TLS
22508 if (TARGET_XCOFF && GET_CODE (base) == SYMBOL_REF
22509 && SYMBOL_REF_TLS_MODEL (base) != 0)
22511 if (SYMBOL_REF_TLS_MODEL (base) == TLS_MODEL_LOCAL_EXEC)
22512 fputs ("@le", file);
22513 else if (SYMBOL_REF_TLS_MODEL (base) == TLS_MODEL_INITIAL_EXEC)
22514 fputs ("@ie", file);
22515 /* Use global-dynamic for local-dynamic. */
22516 else if (SYMBOL_REF_TLS_MODEL (base) == TLS_MODEL_GLOBAL_DYNAMIC
22517 || SYMBOL_REF_TLS_MODEL (base) == TLS_MODEL_LOCAL_DYNAMIC)
22519 putc ('\n', file);
22520 (*targetm.asm_out.internal_label) (file, "LCM", labelno);
22521 fputs ("\t.tc .", file);
22522 RS6000_OUTPUT_BASENAME (file, name);
22523 fputs ("[TC],", file);
22524 output_addr_const (file, x);
22525 fputs ("@m", file);
22528 #endif
22530 putc ('\n', file);
22533 /* Output an assembler pseudo-op to write an ASCII string of N characters
22534 starting at P to FILE.
22536 On the RS/6000, we have to do this using the .byte operation and
22537 write out special characters outside the quoted string.
22538 Also, the assembler is broken; very long strings are truncated,
22539 so we must artificially break them up early. */
22541 void
22542 output_ascii (FILE *file, const char *p, int n)
22544 char c;
22545 int i, count_string;
22546 const char *for_string = "\t.byte \"";
22547 const char *for_decimal = "\t.byte ";
22548 const char *to_close = NULL;
22550 count_string = 0;
22551 for (i = 0; i < n; i++)
22553 c = *p++;
22554 if (c >= ' ' && c < 0177)
22556 if (for_string)
22557 fputs (for_string, file);
22558 putc (c, file);
22560 /* Write two quotes to get one. */
22561 if (c == '"')
22563 putc (c, file);
22564 ++count_string;
22567 for_string = NULL;
22568 for_decimal = "\"\n\t.byte ";
22569 to_close = "\"\n";
22570 ++count_string;
22572 if (count_string >= 512)
22574 fputs (to_close, file);
22576 for_string = "\t.byte \"";
22577 for_decimal = "\t.byte ";
22578 to_close = NULL;
22579 count_string = 0;
22582 else
22584 if (for_decimal)
22585 fputs (for_decimal, file);
22586 fprintf (file, "%d", c);
22588 for_string = "\n\t.byte \"";
22589 for_decimal = ", ";
22590 to_close = "\n";
22591 count_string = 0;
22595 /* Now close the string if we have written one. Then end the line. */
22596 if (to_close)
22597 fputs (to_close, file);
22600 /* Generate a unique section name for FILENAME for a section type
22601 represented by SECTION_DESC. Output goes into BUF.
22603 SECTION_DESC can be any string, as long as it is different for each
22604 possible section type.
22606 We name the section in the same manner as xlc. The name begins with an
22607 underscore followed by the filename (after stripping any leading directory
22608 names) with the last period replaced by the string SECTION_DESC. If
22609 FILENAME does not contain a period, SECTION_DESC is appended to the end of
22610 the name. */
22612 void
22613 rs6000_gen_section_name (char **buf, const char *filename,
22614 const char *section_desc)
22616 const char *q, *after_last_slash, *last_period = 0;
22617 char *p;
22618 int len;
22620 after_last_slash = filename;
22621 for (q = filename; *q; q++)
22623 if (*q == '/')
22624 after_last_slash = q + 1;
22625 else if (*q == '.')
22626 last_period = q;
22629 len = strlen (after_last_slash) + strlen (section_desc) + 2;
22630 *buf = (char *) xmalloc (len);
22632 p = *buf;
22633 *p++ = '_';
22635 for (q = after_last_slash; *q; q++)
22637 if (q == last_period)
22639 strcpy (p, section_desc);
22640 p += strlen (section_desc);
22641 break;
22644 else if (ISALNUM (*q))
22645 *p++ = *q;
22648 if (last_period == 0)
22649 strcpy (p, section_desc);
22650 else
22651 *p = '\0';
22654 /* Emit profile function. */
22656 void
22657 output_profile_hook (int labelno ATTRIBUTE_UNUSED)
22659 /* Non-standard profiling for kernels, which just saves LR then calls
22660 _mcount without worrying about arg saves. The idea is to change
22661 the function prologue as little as possible as it isn't easy to
22662 account for arg save/restore code added just for _mcount. */
22663 if (TARGET_PROFILE_KERNEL)
22664 return;
22666 if (DEFAULT_ABI == ABI_AIX)
22668 #ifndef NO_PROFILE_COUNTERS
22669 # define NO_PROFILE_COUNTERS 0
22670 #endif
22671 if (NO_PROFILE_COUNTERS)
22672 emit_library_call (init_one_libfunc (RS6000_MCOUNT),
22673 LCT_NORMAL, VOIDmode, 0);
22674 else
22676 char buf[30];
22677 const char *label_name;
22678 rtx fun;
22680 ASM_GENERATE_INTERNAL_LABEL (buf, "LP", labelno);
22681 label_name = ggc_strdup ((*targetm.strip_name_encoding) (buf));
22682 fun = gen_rtx_SYMBOL_REF (Pmode, label_name);
22684 emit_library_call (init_one_libfunc (RS6000_MCOUNT),
22685 LCT_NORMAL, VOIDmode, 1, fun, Pmode);
22688 else if (DEFAULT_ABI == ABI_DARWIN)
22690 const char *mcount_name = RS6000_MCOUNT;
22691 int caller_addr_regno = LR_REGNO;
22693 /* Be conservative and always set this, at least for now. */
22694 crtl->uses_pic_offset_table = 1;
22696 #if TARGET_MACHO
22697 /* For PIC code, set up a stub and collect the caller's address
22698 from r0, which is where the prologue puts it. */
22699 if (MACHOPIC_INDIRECT
22700 && crtl->uses_pic_offset_table)
22701 caller_addr_regno = 0;
22702 #endif
22703 emit_library_call (gen_rtx_SYMBOL_REF (Pmode, mcount_name),
22704 LCT_NORMAL, VOIDmode, 1,
22705 gen_rtx_REG (Pmode, caller_addr_regno), Pmode);
22709 /* Write function profiler code. */
22711 void
22712 output_function_profiler (FILE *file, int labelno)
22714 char buf[100];
22716 switch (DEFAULT_ABI)
22718 default:
22719 gcc_unreachable ();
22721 case ABI_V4:
22722 if (!TARGET_32BIT)
22724 warning (0, "no profiling of 64-bit code for this ABI");
22725 return;
22727 ASM_GENERATE_INTERNAL_LABEL (buf, "LP", labelno);
22728 fprintf (file, "\tmflr %s\n", reg_names[0]);
22729 if (NO_PROFILE_COUNTERS)
22731 asm_fprintf (file, "\tstw %s,4(%s)\n",
22732 reg_names[0], reg_names[1]);
22734 else if (TARGET_SECURE_PLT && flag_pic)
22736 if (TARGET_LINK_STACK)
22738 char name[32];
22739 get_ppc476_thunk_name (name);
22740 asm_fprintf (file, "\tbl %s\n", name);
22742 else
22743 asm_fprintf (file, "\tbcl 20,31,1f\n1:\n");
22744 asm_fprintf (file, "\tstw %s,4(%s)\n",
22745 reg_names[0], reg_names[1]);
22746 asm_fprintf (file, "\tmflr %s\n", reg_names[12]);
22747 asm_fprintf (file, "\taddis %s,%s,",
22748 reg_names[12], reg_names[12]);
22749 assemble_name (file, buf);
22750 asm_fprintf (file, "-1b@ha\n\tla %s,", reg_names[0]);
22751 assemble_name (file, buf);
22752 asm_fprintf (file, "-1b@l(%s)\n", reg_names[12]);
22754 else if (flag_pic == 1)
22756 fputs ("\tbl _GLOBAL_OFFSET_TABLE_@local-4\n", file);
22757 asm_fprintf (file, "\tstw %s,4(%s)\n",
22758 reg_names[0], reg_names[1]);
22759 asm_fprintf (file, "\tmflr %s\n", reg_names[12]);
22760 asm_fprintf (file, "\tlwz %s,", reg_names[0]);
22761 assemble_name (file, buf);
22762 asm_fprintf (file, "@got(%s)\n", reg_names[12]);
22764 else if (flag_pic > 1)
22766 asm_fprintf (file, "\tstw %s,4(%s)\n",
22767 reg_names[0], reg_names[1]);
22768 /* Now, we need to get the address of the label. */
22769 if (TARGET_LINK_STACK)
22771 char name[32];
22772 get_ppc476_thunk_name (name);
22773 asm_fprintf (file, "\tbl %s\n\tb 1f\n\t.long ", name);
22774 assemble_name (file, buf);
22775 fputs ("-.\n1:", file);
22776 asm_fprintf (file, "\tmflr %s\n", reg_names[11]);
22777 asm_fprintf (file, "\taddi %s,%s,4\n",
22778 reg_names[11], reg_names[11]);
22780 else
22782 fputs ("\tbcl 20,31,1f\n\t.long ", file);
22783 assemble_name (file, buf);
22784 fputs ("-.\n1:", file);
22785 asm_fprintf (file, "\tmflr %s\n", reg_names[11]);
22787 asm_fprintf (file, "\tlwz %s,0(%s)\n",
22788 reg_names[0], reg_names[11]);
22789 asm_fprintf (file, "\tadd %s,%s,%s\n",
22790 reg_names[0], reg_names[0], reg_names[11]);
22792 else
22794 asm_fprintf (file, "\tlis %s,", reg_names[12]);
22795 assemble_name (file, buf);
22796 fputs ("@ha\n", file);
22797 asm_fprintf (file, "\tstw %s,4(%s)\n",
22798 reg_names[0], reg_names[1]);
22799 asm_fprintf (file, "\tla %s,", reg_names[0]);
22800 assemble_name (file, buf);
22801 asm_fprintf (file, "@l(%s)\n", reg_names[12]);
22804 /* ABI_V4 saves the static chain reg with ASM_OUTPUT_REG_PUSH. */
22805 fprintf (file, "\tbl %s%s\n",
22806 RS6000_MCOUNT, flag_pic ? "@plt" : "");
22807 break;
22809 case ABI_AIX:
22810 case ABI_DARWIN:
22811 if (!TARGET_PROFILE_KERNEL)
22813 /* Don't do anything, done in output_profile_hook (). */
22815 else
22817 gcc_assert (!TARGET_32BIT);
22819 asm_fprintf (file, "\tmflr %s\n", reg_names[0]);
22820 asm_fprintf (file, "\tstd %s,16(%s)\n", reg_names[0], reg_names[1]);
22822 if (cfun->static_chain_decl != NULL)
22824 asm_fprintf (file, "\tstd %s,24(%s)\n",
22825 reg_names[STATIC_CHAIN_REGNUM], reg_names[1]);
22826 fprintf (file, "\tbl %s\n", RS6000_MCOUNT);
22827 asm_fprintf (file, "\tld %s,24(%s)\n",
22828 reg_names[STATIC_CHAIN_REGNUM], reg_names[1]);
22830 else
22831 fprintf (file, "\tbl %s\n", RS6000_MCOUNT);
22833 break;
22839 /* The following variable value is the last issued insn. */
22841 static rtx last_scheduled_insn;
22843 /* The following variable helps to balance issuing of load and
22844 store instructions */
22846 static int load_store_pendulum;
22848 /* Power4 load update and store update instructions are cracked into a
22849 load or store and an integer insn which are executed in the same cycle.
22850 Branches have their own dispatch slot which does not count against the
22851 GCC issue rate, but it changes the program flow so there are no other
22852 instructions to issue in this cycle. */
22854 static int
22855 rs6000_variable_issue_1 (rtx insn, int more)
22857 last_scheduled_insn = insn;
22858 if (GET_CODE (PATTERN (insn)) == USE
22859 || GET_CODE (PATTERN (insn)) == CLOBBER)
22861 cached_can_issue_more = more;
22862 return cached_can_issue_more;
22865 if (insn_terminates_group_p (insn, current_group))
22867 cached_can_issue_more = 0;
22868 return cached_can_issue_more;
22871 /* If no reservation, but reach here */
22872 if (recog_memoized (insn) < 0)
22873 return more;
22875 if (rs6000_sched_groups)
22877 if (is_microcoded_insn (insn))
22878 cached_can_issue_more = 0;
22879 else if (is_cracked_insn (insn))
22880 cached_can_issue_more = more > 2 ? more - 2 : 0;
22881 else
22882 cached_can_issue_more = more - 1;
22884 return cached_can_issue_more;
22887 if (rs6000_cpu_attr == CPU_CELL && is_nonpipeline_insn (insn))
22888 return 0;
22890 cached_can_issue_more = more - 1;
22891 return cached_can_issue_more;
22894 static int
22895 rs6000_variable_issue (FILE *stream, int verbose, rtx insn, int more)
22897 int r = rs6000_variable_issue_1 (insn, more);
22898 if (verbose)
22899 fprintf (stream, "// rs6000_variable_issue (more = %d) = %d\n", more, r);
22900 return r;
22903 /* Adjust the cost of a scheduling dependency. Return the new cost of
22904 a dependency LINK or INSN on DEP_INSN. COST is the current cost. */
22906 static int
22907 rs6000_adjust_cost (rtx insn, rtx link, rtx dep_insn, int cost)
22909 enum attr_type attr_type;
22911 if (! recog_memoized (insn))
22912 return 0;
22914 switch (REG_NOTE_KIND (link))
22916 case REG_DEP_TRUE:
22918 /* Data dependency; DEP_INSN writes a register that INSN reads
22919 some cycles later. */
22921 /* Separate a load from a narrower, dependent store. */
22922 if (rs6000_sched_groups
22923 && GET_CODE (PATTERN (insn)) == SET
22924 && GET_CODE (PATTERN (dep_insn)) == SET
22925 && GET_CODE (XEXP (PATTERN (insn), 1)) == MEM
22926 && GET_CODE (XEXP (PATTERN (dep_insn), 0)) == MEM
22927 && (GET_MODE_SIZE (GET_MODE (XEXP (PATTERN (insn), 1)))
22928 > GET_MODE_SIZE (GET_MODE (XEXP (PATTERN (dep_insn), 0)))))
22929 return cost + 14;
22931 attr_type = get_attr_type (insn);
22933 switch (attr_type)
22935 case TYPE_JMPREG:
22936 /* Tell the first scheduling pass about the latency between
22937 a mtctr and bctr (and mtlr and br/blr). The first
22938 scheduling pass will not know about this latency since
22939 the mtctr instruction, which has the latency associated
22940 to it, will be generated by reload. */
22941 return 4;
22942 case TYPE_BRANCH:
22943 /* Leave some extra cycles between a compare and its
22944 dependent branch, to inhibit expensive mispredicts. */
22945 if ((rs6000_cpu_attr == CPU_PPC603
22946 || rs6000_cpu_attr == CPU_PPC604
22947 || rs6000_cpu_attr == CPU_PPC604E
22948 || rs6000_cpu_attr == CPU_PPC620
22949 || rs6000_cpu_attr == CPU_PPC630
22950 || rs6000_cpu_attr == CPU_PPC750
22951 || rs6000_cpu_attr == CPU_PPC7400
22952 || rs6000_cpu_attr == CPU_PPC7450
22953 || rs6000_cpu_attr == CPU_PPCE5500
22954 || rs6000_cpu_attr == CPU_PPCE6500
22955 || rs6000_cpu_attr == CPU_POWER4
22956 || rs6000_cpu_attr == CPU_POWER5
22957 || rs6000_cpu_attr == CPU_POWER7
22958 || rs6000_cpu_attr == CPU_CELL)
22959 && recog_memoized (dep_insn)
22960 && (INSN_CODE (dep_insn) >= 0))
22962 switch (get_attr_type (dep_insn))
22964 case TYPE_CMP:
22965 case TYPE_COMPARE:
22966 case TYPE_DELAYED_COMPARE:
22967 case TYPE_IMUL_COMPARE:
22968 case TYPE_LMUL_COMPARE:
22969 case TYPE_FPCOMPARE:
22970 case TYPE_CR_LOGICAL:
22971 case TYPE_DELAYED_CR:
22972 return cost + 2;
22973 default:
22974 break;
22976 break;
22978 case TYPE_STORE:
22979 case TYPE_STORE_U:
22980 case TYPE_STORE_UX:
22981 case TYPE_FPSTORE:
22982 case TYPE_FPSTORE_U:
22983 case TYPE_FPSTORE_UX:
22984 if ((rs6000_cpu == PROCESSOR_POWER6)
22985 && recog_memoized (dep_insn)
22986 && (INSN_CODE (dep_insn) >= 0))
22989 if (GET_CODE (PATTERN (insn)) != SET)
22990 /* If this happens, we have to extend this to schedule
22991 optimally. Return default for now. */
22992 return cost;
22994 /* Adjust the cost for the case where the value written
22995 by a fixed point operation is used as the address
22996 gen value on a store. */
22997 switch (get_attr_type (dep_insn))
22999 case TYPE_LOAD:
23000 case TYPE_LOAD_U:
23001 case TYPE_LOAD_UX:
23002 case TYPE_CNTLZ:
23004 if (! store_data_bypass_p (dep_insn, insn))
23005 return 4;
23006 break;
23008 case TYPE_LOAD_EXT:
23009 case TYPE_LOAD_EXT_U:
23010 case TYPE_LOAD_EXT_UX:
23011 case TYPE_VAR_SHIFT_ROTATE:
23012 case TYPE_VAR_DELAYED_COMPARE:
23014 if (! store_data_bypass_p (dep_insn, insn))
23015 return 6;
23016 break;
23018 case TYPE_INTEGER:
23019 case TYPE_COMPARE:
23020 case TYPE_FAST_COMPARE:
23021 case TYPE_EXTS:
23022 case TYPE_SHIFT:
23023 case TYPE_INSERT_WORD:
23024 case TYPE_INSERT_DWORD:
23025 case TYPE_FPLOAD_U:
23026 case TYPE_FPLOAD_UX:
23027 case TYPE_STORE_U:
23028 case TYPE_STORE_UX:
23029 case TYPE_FPSTORE_U:
23030 case TYPE_FPSTORE_UX:
23032 if (! store_data_bypass_p (dep_insn, insn))
23033 return 3;
23034 break;
23036 case TYPE_IMUL:
23037 case TYPE_IMUL2:
23038 case TYPE_IMUL3:
23039 case TYPE_LMUL:
23040 case TYPE_IMUL_COMPARE:
23041 case TYPE_LMUL_COMPARE:
23043 if (! store_data_bypass_p (dep_insn, insn))
23044 return 17;
23045 break;
23047 case TYPE_IDIV:
23049 if (! store_data_bypass_p (dep_insn, insn))
23050 return 45;
23051 break;
23053 case TYPE_LDIV:
23055 if (! store_data_bypass_p (dep_insn, insn))
23056 return 57;
23057 break;
23059 default:
23060 break;
23063 break;
23065 case TYPE_LOAD:
23066 case TYPE_LOAD_U:
23067 case TYPE_LOAD_UX:
23068 case TYPE_LOAD_EXT:
23069 case TYPE_LOAD_EXT_U:
23070 case TYPE_LOAD_EXT_UX:
23071 if ((rs6000_cpu == PROCESSOR_POWER6)
23072 && recog_memoized (dep_insn)
23073 && (INSN_CODE (dep_insn) >= 0))
23076 /* Adjust the cost for the case where the value written
23077 by a fixed point instruction is used within the address
23078 gen portion of a subsequent load(u)(x) */
23079 switch (get_attr_type (dep_insn))
23081 case TYPE_LOAD:
23082 case TYPE_LOAD_U:
23083 case TYPE_LOAD_UX:
23084 case TYPE_CNTLZ:
23086 if (set_to_load_agen (dep_insn, insn))
23087 return 4;
23088 break;
23090 case TYPE_LOAD_EXT:
23091 case TYPE_LOAD_EXT_U:
23092 case TYPE_LOAD_EXT_UX:
23093 case TYPE_VAR_SHIFT_ROTATE:
23094 case TYPE_VAR_DELAYED_COMPARE:
23096 if (set_to_load_agen (dep_insn, insn))
23097 return 6;
23098 break;
23100 case TYPE_INTEGER:
23101 case TYPE_COMPARE:
23102 case TYPE_FAST_COMPARE:
23103 case TYPE_EXTS:
23104 case TYPE_SHIFT:
23105 case TYPE_INSERT_WORD:
23106 case TYPE_INSERT_DWORD:
23107 case TYPE_FPLOAD_U:
23108 case TYPE_FPLOAD_UX:
23109 case TYPE_STORE_U:
23110 case TYPE_STORE_UX:
23111 case TYPE_FPSTORE_U:
23112 case TYPE_FPSTORE_UX:
23114 if (set_to_load_agen (dep_insn, insn))
23115 return 3;
23116 break;
23118 case TYPE_IMUL:
23119 case TYPE_IMUL2:
23120 case TYPE_IMUL3:
23121 case TYPE_LMUL:
23122 case TYPE_IMUL_COMPARE:
23123 case TYPE_LMUL_COMPARE:
23125 if (set_to_load_agen (dep_insn, insn))
23126 return 17;
23127 break;
23129 case TYPE_IDIV:
23131 if (set_to_load_agen (dep_insn, insn))
23132 return 45;
23133 break;
23135 case TYPE_LDIV:
23137 if (set_to_load_agen (dep_insn, insn))
23138 return 57;
23139 break;
23141 default:
23142 break;
23145 break;
23147 case TYPE_FPLOAD:
23148 if ((rs6000_cpu == PROCESSOR_POWER6)
23149 && recog_memoized (dep_insn)
23150 && (INSN_CODE (dep_insn) >= 0)
23151 && (get_attr_type (dep_insn) == TYPE_MFFGPR))
23152 return 2;
23154 default:
23155 break;
23158 /* Fall out to return default cost. */
23160 break;
23162 case REG_DEP_OUTPUT:
23163 /* Output dependency; DEP_INSN writes a register that INSN writes some
23164 cycles later. */
23165 if ((rs6000_cpu == PROCESSOR_POWER6)
23166 && recog_memoized (dep_insn)
23167 && (INSN_CODE (dep_insn) >= 0))
23169 attr_type = get_attr_type (insn);
23171 switch (attr_type)
23173 case TYPE_FP:
23174 if (get_attr_type (dep_insn) == TYPE_FP)
23175 return 1;
23176 break;
23177 case TYPE_FPLOAD:
23178 if (get_attr_type (dep_insn) == TYPE_MFFGPR)
23179 return 2;
23180 break;
23181 default:
23182 break;
23185 case REG_DEP_ANTI:
23186 /* Anti dependency; DEP_INSN reads a register that INSN writes some
23187 cycles later. */
23188 return 0;
23190 default:
23191 gcc_unreachable ();
23194 return cost;
23197 /* Debug version of rs6000_adjust_cost. */
23199 static int
23200 rs6000_debug_adjust_cost (rtx insn, rtx link, rtx dep_insn, int cost)
23202 int ret = rs6000_adjust_cost (insn, link, dep_insn, cost);
23204 if (ret != cost)
23206 const char *dep;
23208 switch (REG_NOTE_KIND (link))
23210 default: dep = "unknown depencency"; break;
23211 case REG_DEP_TRUE: dep = "data dependency"; break;
23212 case REG_DEP_OUTPUT: dep = "output dependency"; break;
23213 case REG_DEP_ANTI: dep = "anti depencency"; break;
23216 fprintf (stderr,
23217 "\nrs6000_adjust_cost, final cost = %d, orig cost = %d, "
23218 "%s, insn:\n", ret, cost, dep);
23220 debug_rtx (insn);
23223 return ret;
23226 /* The function returns a true if INSN is microcoded.
23227 Return false otherwise. */
23229 static bool
23230 is_microcoded_insn (rtx insn)
23232 if (!insn || !NONDEBUG_INSN_P (insn)
23233 || GET_CODE (PATTERN (insn)) == USE
23234 || GET_CODE (PATTERN (insn)) == CLOBBER)
23235 return false;
23237 if (rs6000_cpu_attr == CPU_CELL)
23238 return get_attr_cell_micro (insn) == CELL_MICRO_ALWAYS;
23240 if (rs6000_sched_groups)
23242 enum attr_type type = get_attr_type (insn);
23243 if (type == TYPE_LOAD_EXT_U
23244 || type == TYPE_LOAD_EXT_UX
23245 || type == TYPE_LOAD_UX
23246 || type == TYPE_STORE_UX
23247 || type == TYPE_MFCR)
23248 return true;
23251 return false;
23254 /* The function returns true if INSN is cracked into 2 instructions
23255 by the processor (and therefore occupies 2 issue slots). */
23257 static bool
23258 is_cracked_insn (rtx insn)
23260 if (!insn || !NONDEBUG_INSN_P (insn)
23261 || GET_CODE (PATTERN (insn)) == USE
23262 || GET_CODE (PATTERN (insn)) == CLOBBER)
23263 return false;
23265 if (rs6000_sched_groups)
23267 enum attr_type type = get_attr_type (insn);
23268 if (type == TYPE_LOAD_U || type == TYPE_STORE_U
23269 || type == TYPE_FPLOAD_U || type == TYPE_FPSTORE_U
23270 || type == TYPE_FPLOAD_UX || type == TYPE_FPSTORE_UX
23271 || type == TYPE_LOAD_EXT || type == TYPE_DELAYED_CR
23272 || type == TYPE_COMPARE || type == TYPE_DELAYED_COMPARE
23273 || type == TYPE_IMUL_COMPARE || type == TYPE_LMUL_COMPARE
23274 || type == TYPE_IDIV || type == TYPE_LDIV
23275 || type == TYPE_INSERT_WORD)
23276 return true;
23279 return false;
23282 /* The function returns true if INSN can be issued only from
23283 the branch slot. */
23285 static bool
23286 is_branch_slot_insn (rtx insn)
23288 if (!insn || !NONDEBUG_INSN_P (insn)
23289 || GET_CODE (PATTERN (insn)) == USE
23290 || GET_CODE (PATTERN (insn)) == CLOBBER)
23291 return false;
23293 if (rs6000_sched_groups)
23295 enum attr_type type = get_attr_type (insn);
23296 if (type == TYPE_BRANCH || type == TYPE_JMPREG)
23297 return true;
23298 return false;
23301 return false;
23304 /* The function returns true if out_inst sets a value that is
23305 used in the address generation computation of in_insn */
23306 static bool
23307 set_to_load_agen (rtx out_insn, rtx in_insn)
23309 rtx out_set, in_set;
23311 /* For performance reasons, only handle the simple case where
23312 both loads are a single_set. */
23313 out_set = single_set (out_insn);
23314 if (out_set)
23316 in_set = single_set (in_insn);
23317 if (in_set)
23318 return reg_mentioned_p (SET_DEST (out_set), SET_SRC (in_set));
23321 return false;
23324 /* Try to determine base/offset/size parts of the given MEM.
23325 Return true if successful, false if all the values couldn't
23326 be determined.
23328 This function only looks for REG or REG+CONST address forms.
23329 REG+REG address form will return false. */
23331 static bool
23332 get_memref_parts (rtx mem, rtx *base, HOST_WIDE_INT *offset,
23333 HOST_WIDE_INT *size)
23335 rtx addr_rtx;
23336 if MEM_SIZE_KNOWN_P (mem)
23337 *size = MEM_SIZE (mem);
23338 else
23339 return false;
23341 if (GET_CODE (XEXP (mem, 0)) == PRE_MODIFY)
23342 addr_rtx = XEXP (XEXP (mem, 0), 1);
23343 else
23344 addr_rtx = (XEXP (mem, 0));
23346 if (GET_CODE (addr_rtx) == REG)
23348 *base = addr_rtx;
23349 *offset = 0;
23351 else if (GET_CODE (addr_rtx) == PLUS
23352 && CONST_INT_P (XEXP (addr_rtx, 1)))
23354 *base = XEXP (addr_rtx, 0);
23355 *offset = INTVAL (XEXP (addr_rtx, 1));
23357 else
23358 return false;
23360 return true;
23363 /* The function returns true if the target storage location of
23364 mem1 is adjacent to the target storage location of mem2 */
23365 /* Return 1 if memory locations are adjacent. */
23367 static bool
23368 adjacent_mem_locations (rtx mem1, rtx mem2)
23370 rtx reg1, reg2;
23371 HOST_WIDE_INT off1, size1, off2, size2;
23373 if (get_memref_parts (mem1, &reg1, &off1, &size1)
23374 && get_memref_parts (mem2, &reg2, &off2, &size2))
23375 return ((REGNO (reg1) == REGNO (reg2))
23376 && ((off1 + size1 == off2)
23377 || (off2 + size2 == off1)));
23379 return false;
23382 /* This function returns true if it can be determined that the two MEM
23383 locations overlap by at least 1 byte based on base reg/offset/size. */
23385 static bool
23386 mem_locations_overlap (rtx mem1, rtx mem2)
23388 rtx reg1, reg2;
23389 HOST_WIDE_INT off1, size1, off2, size2;
23391 if (get_memref_parts (mem1, &reg1, &off1, &size1)
23392 && get_memref_parts (mem2, &reg2, &off2, &size2))
23393 return ((REGNO (reg1) == REGNO (reg2))
23394 && (((off1 <= off2) && (off1 + size1 > off2))
23395 || ((off2 <= off1) && (off2 + size2 > off1))));
23397 return false;
23400 /* A C statement (sans semicolon) to update the integer scheduling
23401 priority INSN_PRIORITY (INSN). Increase the priority to execute the
23402 INSN earlier, reduce the priority to execute INSN later. Do not
23403 define this macro if you do not need to adjust the scheduling
23404 priorities of insns. */
23406 static int
23407 rs6000_adjust_priority (rtx insn ATTRIBUTE_UNUSED, int priority)
23409 rtx load_mem, str_mem;
23410 /* On machines (like the 750) which have asymmetric integer units,
23411 where one integer unit can do multiply and divides and the other
23412 can't, reduce the priority of multiply/divide so it is scheduled
23413 before other integer operations. */
23415 #if 0
23416 if (! INSN_P (insn))
23417 return priority;
23419 if (GET_CODE (PATTERN (insn)) == USE)
23420 return priority;
23422 switch (rs6000_cpu_attr) {
23423 case CPU_PPC750:
23424 switch (get_attr_type (insn))
23426 default:
23427 break;
23429 case TYPE_IMUL:
23430 case TYPE_IDIV:
23431 fprintf (stderr, "priority was %#x (%d) before adjustment\n",
23432 priority, priority);
23433 if (priority >= 0 && priority < 0x01000000)
23434 priority >>= 3;
23435 break;
23438 #endif
23440 if (insn_must_be_first_in_group (insn)
23441 && reload_completed
23442 && current_sched_info->sched_max_insns_priority
23443 && rs6000_sched_restricted_insns_priority)
23446 /* Prioritize insns that can be dispatched only in the first
23447 dispatch slot. */
23448 if (rs6000_sched_restricted_insns_priority == 1)
23449 /* Attach highest priority to insn. This means that in
23450 haifa-sched.c:ready_sort(), dispatch-slot restriction considerations
23451 precede 'priority' (critical path) considerations. */
23452 return current_sched_info->sched_max_insns_priority;
23453 else if (rs6000_sched_restricted_insns_priority == 2)
23454 /* Increase priority of insn by a minimal amount. This means that in
23455 haifa-sched.c:ready_sort(), only 'priority' (critical path)
23456 considerations precede dispatch-slot restriction considerations. */
23457 return (priority + 1);
23460 if (rs6000_cpu == PROCESSOR_POWER6
23461 && ((load_store_pendulum == -2 && is_load_insn (insn, &load_mem))
23462 || (load_store_pendulum == 2 && is_store_insn (insn, &str_mem))))
23463 /* Attach highest priority to insn if the scheduler has just issued two
23464 stores and this instruction is a load, or two loads and this instruction
23465 is a store. Power6 wants loads and stores scheduled alternately
23466 when possible */
23467 return current_sched_info->sched_max_insns_priority;
23469 return priority;
23472 /* Return true if the instruction is nonpipelined on the Cell. */
23473 static bool
23474 is_nonpipeline_insn (rtx insn)
23476 enum attr_type type;
23477 if (!insn || !NONDEBUG_INSN_P (insn)
23478 || GET_CODE (PATTERN (insn)) == USE
23479 || GET_CODE (PATTERN (insn)) == CLOBBER)
23480 return false;
23482 type = get_attr_type (insn);
23483 if (type == TYPE_IMUL
23484 || type == TYPE_IMUL2
23485 || type == TYPE_IMUL3
23486 || type == TYPE_LMUL
23487 || type == TYPE_IDIV
23488 || type == TYPE_LDIV
23489 || type == TYPE_SDIV
23490 || type == TYPE_DDIV
23491 || type == TYPE_SSQRT
23492 || type == TYPE_DSQRT
23493 || type == TYPE_MFCR
23494 || type == TYPE_MFCRF
23495 || type == TYPE_MFJMPR)
23497 return true;
23499 return false;
23503 /* Return how many instructions the machine can issue per cycle. */
23505 static int
23506 rs6000_issue_rate (void)
23508 /* Unless scheduling for register pressure, use issue rate of 1 for
23509 first scheduling pass to decrease degradation. */
23510 if (!reload_completed && !flag_sched_pressure)
23511 return 1;
23513 switch (rs6000_cpu_attr) {
23514 case CPU_RS64A:
23515 case CPU_PPC601: /* ? */
23516 case CPU_PPC7450:
23517 return 3;
23518 case CPU_PPC440:
23519 case CPU_PPC603:
23520 case CPU_PPC750:
23521 case CPU_PPC7400:
23522 case CPU_PPC8540:
23523 case CPU_PPC8548:
23524 case CPU_CELL:
23525 case CPU_PPCE300C2:
23526 case CPU_PPCE300C3:
23527 case CPU_PPCE500MC:
23528 case CPU_PPCE500MC64:
23529 case CPU_PPCE5500:
23530 case CPU_PPCE6500:
23531 case CPU_TITAN:
23532 return 2;
23533 case CPU_PPC476:
23534 case CPU_PPC604:
23535 case CPU_PPC604E:
23536 case CPU_PPC620:
23537 case CPU_PPC630:
23538 return 4;
23539 case CPU_POWER4:
23540 case CPU_POWER5:
23541 case CPU_POWER6:
23542 case CPU_POWER7:
23543 return 5;
23544 default:
23545 return 1;
23549 /* Return how many instructions to look ahead for better insn
23550 scheduling. */
23552 static int
23553 rs6000_use_sched_lookahead (void)
23555 switch (rs6000_cpu_attr)
23557 case CPU_PPC8540:
23558 case CPU_PPC8548:
23559 return 4;
23561 case CPU_CELL:
23562 return (reload_completed ? 8 : 0);
23564 default:
23565 return 0;
23569 /* We are choosing insn from the ready queue. Return nonzero if INSN can be chosen. */
23570 static int
23571 rs6000_use_sched_lookahead_guard (rtx insn)
23573 if (rs6000_cpu_attr != CPU_CELL)
23574 return 1;
23576 if (insn == NULL_RTX || !INSN_P (insn))
23577 abort ();
23579 if (!reload_completed
23580 || is_nonpipeline_insn (insn)
23581 || is_microcoded_insn (insn))
23582 return 0;
23584 return 1;
23587 /* Determine if PAT refers to memory. If so, set MEM_REF to the MEM rtx
23588 and return true. */
23590 static bool
23591 find_mem_ref (rtx pat, rtx *mem_ref)
23593 const char * fmt;
23594 int i, j;
23596 /* stack_tie does not produce any real memory traffic. */
23597 if (tie_operand (pat, VOIDmode))
23598 return false;
23600 if (GET_CODE (pat) == MEM)
23602 *mem_ref = pat;
23603 return true;
23606 /* Recursively process the pattern. */
23607 fmt = GET_RTX_FORMAT (GET_CODE (pat));
23609 for (i = GET_RTX_LENGTH (GET_CODE (pat)) - 1; i >= 0; i--)
23611 if (fmt[i] == 'e')
23613 if (find_mem_ref (XEXP (pat, i), mem_ref))
23614 return true;
23616 else if (fmt[i] == 'E')
23617 for (j = XVECLEN (pat, i) - 1; j >= 0; j--)
23619 if (find_mem_ref (XVECEXP (pat, i, j), mem_ref))
23620 return true;
23624 return false;
23627 /* Determine if PAT is a PATTERN of a load insn. */
23629 static bool
23630 is_load_insn1 (rtx pat, rtx *load_mem)
23632 if (!pat || pat == NULL_RTX)
23633 return false;
23635 if (GET_CODE (pat) == SET)
23636 return find_mem_ref (SET_SRC (pat), load_mem);
23638 if (GET_CODE (pat) == PARALLEL)
23640 int i;
23642 for (i = 0; i < XVECLEN (pat, 0); i++)
23643 if (is_load_insn1 (XVECEXP (pat, 0, i), load_mem))
23644 return true;
23647 return false;
23650 /* Determine if INSN loads from memory. */
23652 static bool
23653 is_load_insn (rtx insn, rtx *load_mem)
23655 if (!insn || !INSN_P (insn))
23656 return false;
23658 if (CALL_P (insn))
23659 return false;
23661 return is_load_insn1 (PATTERN (insn), load_mem);
23664 /* Determine if PAT is a PATTERN of a store insn. */
23666 static bool
23667 is_store_insn1 (rtx pat, rtx *str_mem)
23669 if (!pat || pat == NULL_RTX)
23670 return false;
23672 if (GET_CODE (pat) == SET)
23673 return find_mem_ref (SET_DEST (pat), str_mem);
23675 if (GET_CODE (pat) == PARALLEL)
23677 int i;
23679 for (i = 0; i < XVECLEN (pat, 0); i++)
23680 if (is_store_insn1 (XVECEXP (pat, 0, i), str_mem))
23681 return true;
23684 return false;
23687 /* Determine if INSN stores to memory. */
23689 static bool
23690 is_store_insn (rtx insn, rtx *str_mem)
23692 if (!insn || !INSN_P (insn))
23693 return false;
23695 return is_store_insn1 (PATTERN (insn), str_mem);
23698 /* Returns whether the dependence between INSN and NEXT is considered
23699 costly by the given target. */
23701 static bool
23702 rs6000_is_costly_dependence (dep_t dep, int cost, int distance)
23704 rtx insn;
23705 rtx next;
23706 rtx load_mem, str_mem;
23708 /* If the flag is not enabled - no dependence is considered costly;
23709 allow all dependent insns in the same group.
23710 This is the most aggressive option. */
23711 if (rs6000_sched_costly_dep == no_dep_costly)
23712 return false;
23714 /* If the flag is set to 1 - a dependence is always considered costly;
23715 do not allow dependent instructions in the same group.
23716 This is the most conservative option. */
23717 if (rs6000_sched_costly_dep == all_deps_costly)
23718 return true;
23720 insn = DEP_PRO (dep);
23721 next = DEP_CON (dep);
23723 if (rs6000_sched_costly_dep == store_to_load_dep_costly
23724 && is_load_insn (next, &load_mem)
23725 && is_store_insn (insn, &str_mem))
23726 /* Prevent load after store in the same group. */
23727 return true;
23729 if (rs6000_sched_costly_dep == true_store_to_load_dep_costly
23730 && is_load_insn (next, &load_mem)
23731 && is_store_insn (insn, &str_mem)
23732 && DEP_TYPE (dep) == REG_DEP_TRUE
23733 && mem_locations_overlap(str_mem, load_mem))
23734 /* Prevent load after store in the same group if it is a true
23735 dependence. */
23736 return true;
23738 /* The flag is set to X; dependences with latency >= X are considered costly,
23739 and will not be scheduled in the same group. */
23740 if (rs6000_sched_costly_dep <= max_dep_latency
23741 && ((cost - distance) >= (int)rs6000_sched_costly_dep))
23742 return true;
23744 return false;
23747 /* Return the next insn after INSN that is found before TAIL is reached,
23748 skipping any "non-active" insns - insns that will not actually occupy
23749 an issue slot. Return NULL_RTX if such an insn is not found. */
23751 static rtx
23752 get_next_active_insn (rtx insn, rtx tail)
23754 if (insn == NULL_RTX || insn == tail)
23755 return NULL_RTX;
23757 while (1)
23759 insn = NEXT_INSN (insn);
23760 if (insn == NULL_RTX || insn == tail)
23761 return NULL_RTX;
23763 if (CALL_P (insn)
23764 || JUMP_P (insn) || JUMP_TABLE_DATA_P (insn)
23765 || (NONJUMP_INSN_P (insn)
23766 && GET_CODE (PATTERN (insn)) != USE
23767 && GET_CODE (PATTERN (insn)) != CLOBBER
23768 && INSN_CODE (insn) != CODE_FOR_stack_tie))
23769 break;
23771 return insn;
23774 /* We are about to begin issuing insns for this clock cycle. */
23776 static int
23777 rs6000_sched_reorder (FILE *dump ATTRIBUTE_UNUSED, int sched_verbose,
23778 rtx *ready ATTRIBUTE_UNUSED,
23779 int *pn_ready ATTRIBUTE_UNUSED,
23780 int clock_var ATTRIBUTE_UNUSED)
23782 int n_ready = *pn_ready;
23784 if (sched_verbose)
23785 fprintf (dump, "// rs6000_sched_reorder :\n");
23787 /* Reorder the ready list, if the second to last ready insn
23788 is a nonepipeline insn. */
23789 if (rs6000_cpu_attr == CPU_CELL && n_ready > 1)
23791 if (is_nonpipeline_insn (ready[n_ready - 1])
23792 && (recog_memoized (ready[n_ready - 2]) > 0))
23793 /* Simply swap first two insns. */
23795 rtx tmp = ready[n_ready - 1];
23796 ready[n_ready - 1] = ready[n_ready - 2];
23797 ready[n_ready - 2] = tmp;
23801 if (rs6000_cpu == PROCESSOR_POWER6)
23802 load_store_pendulum = 0;
23804 return rs6000_issue_rate ();
23807 /* Like rs6000_sched_reorder, but called after issuing each insn. */
23809 static int
23810 rs6000_sched_reorder2 (FILE *dump, int sched_verbose, rtx *ready,
23811 int *pn_ready, int clock_var ATTRIBUTE_UNUSED)
23813 if (sched_verbose)
23814 fprintf (dump, "// rs6000_sched_reorder2 :\n");
23816 /* For Power6, we need to handle some special cases to try and keep the
23817 store queue from overflowing and triggering expensive flushes.
23819 This code monitors how load and store instructions are being issued
23820 and skews the ready list one way or the other to increase the likelihood
23821 that a desired instruction is issued at the proper time.
23823 A couple of things are done. First, we maintain a "load_store_pendulum"
23824 to track the current state of load/store issue.
23826 - If the pendulum is at zero, then no loads or stores have been
23827 issued in the current cycle so we do nothing.
23829 - If the pendulum is 1, then a single load has been issued in this
23830 cycle and we attempt to locate another load in the ready list to
23831 issue with it.
23833 - If the pendulum is -2, then two stores have already been
23834 issued in this cycle, so we increase the priority of the first load
23835 in the ready list to increase it's likelihood of being chosen first
23836 in the next cycle.
23838 - If the pendulum is -1, then a single store has been issued in this
23839 cycle and we attempt to locate another store in the ready list to
23840 issue with it, preferring a store to an adjacent memory location to
23841 facilitate store pairing in the store queue.
23843 - If the pendulum is 2, then two loads have already been
23844 issued in this cycle, so we increase the priority of the first store
23845 in the ready list to increase it's likelihood of being chosen first
23846 in the next cycle.
23848 - If the pendulum < -2 or > 2, then do nothing.
23850 Note: This code covers the most common scenarios. There exist non
23851 load/store instructions which make use of the LSU and which
23852 would need to be accounted for to strictly model the behavior
23853 of the machine. Those instructions are currently unaccounted
23854 for to help minimize compile time overhead of this code.
23856 if (rs6000_cpu == PROCESSOR_POWER6 && last_scheduled_insn)
23858 int pos;
23859 int i;
23860 rtx tmp, load_mem, str_mem;
23862 if (is_store_insn (last_scheduled_insn, &str_mem))
23863 /* Issuing a store, swing the load_store_pendulum to the left */
23864 load_store_pendulum--;
23865 else if (is_load_insn (last_scheduled_insn, &load_mem))
23866 /* Issuing a load, swing the load_store_pendulum to the right */
23867 load_store_pendulum++;
23868 else
23869 return cached_can_issue_more;
23871 /* If the pendulum is balanced, or there is only one instruction on
23872 the ready list, then all is well, so return. */
23873 if ((load_store_pendulum == 0) || (*pn_ready <= 1))
23874 return cached_can_issue_more;
23876 if (load_store_pendulum == 1)
23878 /* A load has been issued in this cycle. Scan the ready list
23879 for another load to issue with it */
23880 pos = *pn_ready-1;
23882 while (pos >= 0)
23884 if (is_load_insn (ready[pos], &load_mem))
23886 /* Found a load. Move it to the head of the ready list,
23887 and adjust it's priority so that it is more likely to
23888 stay there */
23889 tmp = ready[pos];
23890 for (i=pos; i<*pn_ready-1; i++)
23891 ready[i] = ready[i + 1];
23892 ready[*pn_ready-1] = tmp;
23894 if (!sel_sched_p () && INSN_PRIORITY_KNOWN (tmp))
23895 INSN_PRIORITY (tmp)++;
23896 break;
23898 pos--;
23901 else if (load_store_pendulum == -2)
23903 /* Two stores have been issued in this cycle. Increase the
23904 priority of the first load in the ready list to favor it for
23905 issuing in the next cycle. */
23906 pos = *pn_ready-1;
23908 while (pos >= 0)
23910 if (is_load_insn (ready[pos], &load_mem)
23911 && !sel_sched_p ()
23912 && INSN_PRIORITY_KNOWN (ready[pos]))
23914 INSN_PRIORITY (ready[pos])++;
23916 /* Adjust the pendulum to account for the fact that a load
23917 was found and increased in priority. This is to prevent
23918 increasing the priority of multiple loads */
23919 load_store_pendulum--;
23921 break;
23923 pos--;
23926 else if (load_store_pendulum == -1)
23928 /* A store has been issued in this cycle. Scan the ready list for
23929 another store to issue with it, preferring a store to an adjacent
23930 memory location */
23931 int first_store_pos = -1;
23933 pos = *pn_ready-1;
23935 while (pos >= 0)
23937 if (is_store_insn (ready[pos], &str_mem))
23939 rtx str_mem2;
23940 /* Maintain the index of the first store found on the
23941 list */
23942 if (first_store_pos == -1)
23943 first_store_pos = pos;
23945 if (is_store_insn (last_scheduled_insn, &str_mem2)
23946 && adjacent_mem_locations (str_mem, str_mem2))
23948 /* Found an adjacent store. Move it to the head of the
23949 ready list, and adjust it's priority so that it is
23950 more likely to stay there */
23951 tmp = ready[pos];
23952 for (i=pos; i<*pn_ready-1; i++)
23953 ready[i] = ready[i + 1];
23954 ready[*pn_ready-1] = tmp;
23956 if (!sel_sched_p () && INSN_PRIORITY_KNOWN (tmp))
23957 INSN_PRIORITY (tmp)++;
23959 first_store_pos = -1;
23961 break;
23964 pos--;
23967 if (first_store_pos >= 0)
23969 /* An adjacent store wasn't found, but a non-adjacent store was,
23970 so move the non-adjacent store to the front of the ready
23971 list, and adjust its priority so that it is more likely to
23972 stay there. */
23973 tmp = ready[first_store_pos];
23974 for (i=first_store_pos; i<*pn_ready-1; i++)
23975 ready[i] = ready[i + 1];
23976 ready[*pn_ready-1] = tmp;
23977 if (!sel_sched_p () && INSN_PRIORITY_KNOWN (tmp))
23978 INSN_PRIORITY (tmp)++;
23981 else if (load_store_pendulum == 2)
23983 /* Two loads have been issued in this cycle. Increase the priority
23984 of the first store in the ready list to favor it for issuing in
23985 the next cycle. */
23986 pos = *pn_ready-1;
23988 while (pos >= 0)
23990 if (is_store_insn (ready[pos], &str_mem)
23991 && !sel_sched_p ()
23992 && INSN_PRIORITY_KNOWN (ready[pos]))
23994 INSN_PRIORITY (ready[pos])++;
23996 /* Adjust the pendulum to account for the fact that a store
23997 was found and increased in priority. This is to prevent
23998 increasing the priority of multiple stores */
23999 load_store_pendulum++;
24001 break;
24003 pos--;
24008 return cached_can_issue_more;
24011 /* Return whether the presence of INSN causes a dispatch group termination
24012 of group WHICH_GROUP.
24014 If WHICH_GROUP == current_group, this function will return true if INSN
24015 causes the termination of the current group (i.e, the dispatch group to
24016 which INSN belongs). This means that INSN will be the last insn in the
24017 group it belongs to.
24019 If WHICH_GROUP == previous_group, this function will return true if INSN
24020 causes the termination of the previous group (i.e, the dispatch group that
24021 precedes the group to which INSN belongs). This means that INSN will be
24022 the first insn in the group it belongs to). */
24024 static bool
24025 insn_terminates_group_p (rtx insn, enum group_termination which_group)
24027 bool first, last;
24029 if (! insn)
24030 return false;
24032 first = insn_must_be_first_in_group (insn);
24033 last = insn_must_be_last_in_group (insn);
24035 if (first && last)
24036 return true;
24038 if (which_group == current_group)
24039 return last;
24040 else if (which_group == previous_group)
24041 return first;
24043 return false;
24047 static bool
24048 insn_must_be_first_in_group (rtx insn)
24050 enum attr_type type;
24052 if (!insn
24053 || NOTE_P (insn)
24054 || DEBUG_INSN_P (insn)
24055 || GET_CODE (PATTERN (insn)) == USE
24056 || GET_CODE (PATTERN (insn)) == CLOBBER)
24057 return false;
24059 switch (rs6000_cpu)
24061 case PROCESSOR_POWER5:
24062 if (is_cracked_insn (insn))
24063 return true;
24064 case PROCESSOR_POWER4:
24065 if (is_microcoded_insn (insn))
24066 return true;
24068 if (!rs6000_sched_groups)
24069 return false;
24071 type = get_attr_type (insn);
24073 switch (type)
24075 case TYPE_MFCR:
24076 case TYPE_MFCRF:
24077 case TYPE_MTCR:
24078 case TYPE_DELAYED_CR:
24079 case TYPE_CR_LOGICAL:
24080 case TYPE_MTJMPR:
24081 case TYPE_MFJMPR:
24082 case TYPE_IDIV:
24083 case TYPE_LDIV:
24084 case TYPE_LOAD_L:
24085 case TYPE_STORE_C:
24086 case TYPE_ISYNC:
24087 case TYPE_SYNC:
24088 return true;
24089 default:
24090 break;
24092 break;
24093 case PROCESSOR_POWER6:
24094 type = get_attr_type (insn);
24096 switch (type)
24098 case TYPE_INSERT_DWORD:
24099 case TYPE_EXTS:
24100 case TYPE_CNTLZ:
24101 case TYPE_SHIFT:
24102 case TYPE_VAR_SHIFT_ROTATE:
24103 case TYPE_TRAP:
24104 case TYPE_IMUL:
24105 case TYPE_IMUL2:
24106 case TYPE_IMUL3:
24107 case TYPE_LMUL:
24108 case TYPE_IDIV:
24109 case TYPE_INSERT_WORD:
24110 case TYPE_DELAYED_COMPARE:
24111 case TYPE_IMUL_COMPARE:
24112 case TYPE_LMUL_COMPARE:
24113 case TYPE_FPCOMPARE:
24114 case TYPE_MFCR:
24115 case TYPE_MTCR:
24116 case TYPE_MFJMPR:
24117 case TYPE_MTJMPR:
24118 case TYPE_ISYNC:
24119 case TYPE_SYNC:
24120 case TYPE_LOAD_L:
24121 case TYPE_STORE_C:
24122 case TYPE_LOAD_U:
24123 case TYPE_LOAD_UX:
24124 case TYPE_LOAD_EXT_UX:
24125 case TYPE_STORE_U:
24126 case TYPE_STORE_UX:
24127 case TYPE_FPLOAD_U:
24128 case TYPE_FPLOAD_UX:
24129 case TYPE_FPSTORE_U:
24130 case TYPE_FPSTORE_UX:
24131 return true;
24132 default:
24133 break;
24135 break;
24136 case PROCESSOR_POWER7:
24137 type = get_attr_type (insn);
24139 switch (type)
24141 case TYPE_CR_LOGICAL:
24142 case TYPE_MFCR:
24143 case TYPE_MFCRF:
24144 case TYPE_MTCR:
24145 case TYPE_IDIV:
24146 case TYPE_LDIV:
24147 case TYPE_COMPARE:
24148 case TYPE_DELAYED_COMPARE:
24149 case TYPE_VAR_DELAYED_COMPARE:
24150 case TYPE_ISYNC:
24151 case TYPE_LOAD_L:
24152 case TYPE_STORE_C:
24153 case TYPE_LOAD_U:
24154 case TYPE_LOAD_UX:
24155 case TYPE_LOAD_EXT:
24156 case TYPE_LOAD_EXT_U:
24157 case TYPE_LOAD_EXT_UX:
24158 case TYPE_STORE_U:
24159 case TYPE_STORE_UX:
24160 case TYPE_FPLOAD_U:
24161 case TYPE_FPLOAD_UX:
24162 case TYPE_FPSTORE_U:
24163 case TYPE_FPSTORE_UX:
24164 case TYPE_MFJMPR:
24165 case TYPE_MTJMPR:
24166 return true;
24167 default:
24168 break;
24170 break;
24171 default:
24172 break;
24175 return false;
24178 static bool
24179 insn_must_be_last_in_group (rtx insn)
24181 enum attr_type type;
24183 if (!insn
24184 || NOTE_P (insn)
24185 || DEBUG_INSN_P (insn)
24186 || GET_CODE (PATTERN (insn)) == USE
24187 || GET_CODE (PATTERN (insn)) == CLOBBER)
24188 return false;
24190 switch (rs6000_cpu) {
24191 case PROCESSOR_POWER4:
24192 case PROCESSOR_POWER5:
24193 if (is_microcoded_insn (insn))
24194 return true;
24196 if (is_branch_slot_insn (insn))
24197 return true;
24199 break;
24200 case PROCESSOR_POWER6:
24201 type = get_attr_type (insn);
24203 switch (type)
24205 case TYPE_EXTS:
24206 case TYPE_CNTLZ:
24207 case TYPE_SHIFT:
24208 case TYPE_VAR_SHIFT_ROTATE:
24209 case TYPE_TRAP:
24210 case TYPE_IMUL:
24211 case TYPE_IMUL2:
24212 case TYPE_IMUL3:
24213 case TYPE_LMUL:
24214 case TYPE_IDIV:
24215 case TYPE_DELAYED_COMPARE:
24216 case TYPE_IMUL_COMPARE:
24217 case TYPE_LMUL_COMPARE:
24218 case TYPE_FPCOMPARE:
24219 case TYPE_MFCR:
24220 case TYPE_MTCR:
24221 case TYPE_MFJMPR:
24222 case TYPE_MTJMPR:
24223 case TYPE_ISYNC:
24224 case TYPE_SYNC:
24225 case TYPE_LOAD_L:
24226 case TYPE_STORE_C:
24227 return true;
24228 default:
24229 break;
24231 break;
24232 case PROCESSOR_POWER7:
24233 type = get_attr_type (insn);
24235 switch (type)
24237 case TYPE_ISYNC:
24238 case TYPE_SYNC:
24239 case TYPE_LOAD_L:
24240 case TYPE_STORE_C:
24241 case TYPE_LOAD_EXT_U:
24242 case TYPE_LOAD_EXT_UX:
24243 case TYPE_STORE_UX:
24244 return true;
24245 default:
24246 break;
24248 break;
24249 default:
24250 break;
24253 return false;
24256 /* Return true if it is recommended to keep NEXT_INSN "far" (in a separate
24257 dispatch group) from the insns in GROUP_INSNS. Return false otherwise. */
24259 static bool
24260 is_costly_group (rtx *group_insns, rtx next_insn)
24262 int i;
24263 int issue_rate = rs6000_issue_rate ();
24265 for (i = 0; i < issue_rate; i++)
24267 sd_iterator_def sd_it;
24268 dep_t dep;
24269 rtx insn = group_insns[i];
24271 if (!insn)
24272 continue;
24274 FOR_EACH_DEP (insn, SD_LIST_RES_FORW, sd_it, dep)
24276 rtx next = DEP_CON (dep);
24278 if (next == next_insn
24279 && rs6000_is_costly_dependence (dep, dep_cost (dep), 0))
24280 return true;
24284 return false;
24287 /* Utility of the function redefine_groups.
24288 Check if it is too costly to schedule NEXT_INSN together with GROUP_INSNS
24289 in the same dispatch group. If so, insert nops before NEXT_INSN, in order
24290 to keep it "far" (in a separate group) from GROUP_INSNS, following
24291 one of the following schemes, depending on the value of the flag
24292 -minsert_sched_nops = X:
24293 (1) X == sched_finish_regroup_exact: insert exactly as many nops as needed
24294 in order to force NEXT_INSN into a separate group.
24295 (2) X < sched_finish_regroup_exact: insert exactly X nops.
24296 GROUP_END, CAN_ISSUE_MORE and GROUP_COUNT record the state after nop
24297 insertion (has a group just ended, how many vacant issue slots remain in the
24298 last group, and how many dispatch groups were encountered so far). */
24300 static int
24301 force_new_group (int sched_verbose, FILE *dump, rtx *group_insns,
24302 rtx next_insn, bool *group_end, int can_issue_more,
24303 int *group_count)
24305 rtx nop;
24306 bool force;
24307 int issue_rate = rs6000_issue_rate ();
24308 bool end = *group_end;
24309 int i;
24311 if (next_insn == NULL_RTX || DEBUG_INSN_P (next_insn))
24312 return can_issue_more;
24314 if (rs6000_sched_insert_nops > sched_finish_regroup_exact)
24315 return can_issue_more;
24317 force = is_costly_group (group_insns, next_insn);
24318 if (!force)
24319 return can_issue_more;
24321 if (sched_verbose > 6)
24322 fprintf (dump,"force: group count = %d, can_issue_more = %d\n",
24323 *group_count ,can_issue_more);
24325 if (rs6000_sched_insert_nops == sched_finish_regroup_exact)
24327 if (*group_end)
24328 can_issue_more = 0;
24330 /* Since only a branch can be issued in the last issue_slot, it is
24331 sufficient to insert 'can_issue_more - 1' nops if next_insn is not
24332 a branch. If next_insn is a branch, we insert 'can_issue_more' nops;
24333 in this case the last nop will start a new group and the branch
24334 will be forced to the new group. */
24335 if (can_issue_more && !is_branch_slot_insn (next_insn))
24336 can_issue_more--;
24338 /* Power6 and Power7 have special group ending nop. */
24339 if (rs6000_cpu_attr == CPU_POWER6 || rs6000_cpu_attr == CPU_POWER7)
24341 nop = gen_group_ending_nop ();
24342 emit_insn_before (nop, next_insn);
24343 can_issue_more = 0;
24345 else
24346 while (can_issue_more > 0)
24348 nop = gen_nop ();
24349 emit_insn_before (nop, next_insn);
24350 can_issue_more--;
24353 *group_end = true;
24354 return 0;
24357 if (rs6000_sched_insert_nops < sched_finish_regroup_exact)
24359 int n_nops = rs6000_sched_insert_nops;
24361 /* Nops can't be issued from the branch slot, so the effective
24362 issue_rate for nops is 'issue_rate - 1'. */
24363 if (can_issue_more == 0)
24364 can_issue_more = issue_rate;
24365 can_issue_more--;
24366 if (can_issue_more == 0)
24368 can_issue_more = issue_rate - 1;
24369 (*group_count)++;
24370 end = true;
24371 for (i = 0; i < issue_rate; i++)
24373 group_insns[i] = 0;
24377 while (n_nops > 0)
24379 nop = gen_nop ();
24380 emit_insn_before (nop, next_insn);
24381 if (can_issue_more == issue_rate - 1) /* new group begins */
24382 end = false;
24383 can_issue_more--;
24384 if (can_issue_more == 0)
24386 can_issue_more = issue_rate - 1;
24387 (*group_count)++;
24388 end = true;
24389 for (i = 0; i < issue_rate; i++)
24391 group_insns[i] = 0;
24394 n_nops--;
24397 /* Scale back relative to 'issue_rate' (instead of 'issue_rate - 1'). */
24398 can_issue_more++;
24400 /* Is next_insn going to start a new group? */
24401 *group_end
24402 = (end
24403 || (can_issue_more == 1 && !is_branch_slot_insn (next_insn))
24404 || (can_issue_more <= 2 && is_cracked_insn (next_insn))
24405 || (can_issue_more < issue_rate &&
24406 insn_terminates_group_p (next_insn, previous_group)));
24407 if (*group_end && end)
24408 (*group_count)--;
24410 if (sched_verbose > 6)
24411 fprintf (dump, "done force: group count = %d, can_issue_more = %d\n",
24412 *group_count, can_issue_more);
24413 return can_issue_more;
24416 return can_issue_more;
24419 /* This function tries to synch the dispatch groups that the compiler "sees"
24420 with the dispatch groups that the processor dispatcher is expected to
24421 form in practice. It tries to achieve this synchronization by forcing the
24422 estimated processor grouping on the compiler (as opposed to the function
24423 'pad_goups' which tries to force the scheduler's grouping on the processor).
24425 The function scans the insn sequence between PREV_HEAD_INSN and TAIL and
24426 examines the (estimated) dispatch groups that will be formed by the processor
24427 dispatcher. It marks these group boundaries to reflect the estimated
24428 processor grouping, overriding the grouping that the scheduler had marked.
24429 Depending on the value of the flag '-minsert-sched-nops' this function can
24430 force certain insns into separate groups or force a certain distance between
24431 them by inserting nops, for example, if there exists a "costly dependence"
24432 between the insns.
24434 The function estimates the group boundaries that the processor will form as
24435 follows: It keeps track of how many vacant issue slots are available after
24436 each insn. A subsequent insn will start a new group if one of the following
24437 4 cases applies:
24438 - no more vacant issue slots remain in the current dispatch group.
24439 - only the last issue slot, which is the branch slot, is vacant, but the next
24440 insn is not a branch.
24441 - only the last 2 or less issue slots, including the branch slot, are vacant,
24442 which means that a cracked insn (which occupies two issue slots) can't be
24443 issued in this group.
24444 - less than 'issue_rate' slots are vacant, and the next insn always needs to
24445 start a new group. */
24447 static int
24448 redefine_groups (FILE *dump, int sched_verbose, rtx prev_head_insn, rtx tail)
24450 rtx insn, next_insn;
24451 int issue_rate;
24452 int can_issue_more;
24453 int slot, i;
24454 bool group_end;
24455 int group_count = 0;
24456 rtx *group_insns;
24458 /* Initialize. */
24459 issue_rate = rs6000_issue_rate ();
24460 group_insns = XALLOCAVEC (rtx, issue_rate);
24461 for (i = 0; i < issue_rate; i++)
24463 group_insns[i] = 0;
24465 can_issue_more = issue_rate;
24466 slot = 0;
24467 insn = get_next_active_insn (prev_head_insn, tail);
24468 group_end = false;
24470 while (insn != NULL_RTX)
24472 slot = (issue_rate - can_issue_more);
24473 group_insns[slot] = insn;
24474 can_issue_more =
24475 rs6000_variable_issue (dump, sched_verbose, insn, can_issue_more);
24476 if (insn_terminates_group_p (insn, current_group))
24477 can_issue_more = 0;
24479 next_insn = get_next_active_insn (insn, tail);
24480 if (next_insn == NULL_RTX)
24481 return group_count + 1;
24483 /* Is next_insn going to start a new group? */
24484 group_end
24485 = (can_issue_more == 0
24486 || (can_issue_more == 1 && !is_branch_slot_insn (next_insn))
24487 || (can_issue_more <= 2 && is_cracked_insn (next_insn))
24488 || (can_issue_more < issue_rate &&
24489 insn_terminates_group_p (next_insn, previous_group)));
24491 can_issue_more = force_new_group (sched_verbose, dump, group_insns,
24492 next_insn, &group_end, can_issue_more,
24493 &group_count);
24495 if (group_end)
24497 group_count++;
24498 can_issue_more = 0;
24499 for (i = 0; i < issue_rate; i++)
24501 group_insns[i] = 0;
24505 if (GET_MODE (next_insn) == TImode && can_issue_more)
24506 PUT_MODE (next_insn, VOIDmode);
24507 else if (!can_issue_more && GET_MODE (next_insn) != TImode)
24508 PUT_MODE (next_insn, TImode);
24510 insn = next_insn;
24511 if (can_issue_more == 0)
24512 can_issue_more = issue_rate;
24513 } /* while */
24515 return group_count;
24518 /* Scan the insn sequence between PREV_HEAD_INSN and TAIL and examine the
24519 dispatch group boundaries that the scheduler had marked. Pad with nops
24520 any dispatch groups which have vacant issue slots, in order to force the
24521 scheduler's grouping on the processor dispatcher. The function
24522 returns the number of dispatch groups found. */
24524 static int
24525 pad_groups (FILE *dump, int sched_verbose, rtx prev_head_insn, rtx tail)
24527 rtx insn, next_insn;
24528 rtx nop;
24529 int issue_rate;
24530 int can_issue_more;
24531 int group_end;
24532 int group_count = 0;
24534 /* Initialize issue_rate. */
24535 issue_rate = rs6000_issue_rate ();
24536 can_issue_more = issue_rate;
24538 insn = get_next_active_insn (prev_head_insn, tail);
24539 next_insn = get_next_active_insn (insn, tail);
24541 while (insn != NULL_RTX)
24543 can_issue_more =
24544 rs6000_variable_issue (dump, sched_verbose, insn, can_issue_more);
24546 group_end = (next_insn == NULL_RTX || GET_MODE (next_insn) == TImode);
24548 if (next_insn == NULL_RTX)
24549 break;
24551 if (group_end)
24553 /* If the scheduler had marked group termination at this location
24554 (between insn and next_insn), and neither insn nor next_insn will
24555 force group termination, pad the group with nops to force group
24556 termination. */
24557 if (can_issue_more
24558 && (rs6000_sched_insert_nops == sched_finish_pad_groups)
24559 && !insn_terminates_group_p (insn, current_group)
24560 && !insn_terminates_group_p (next_insn, previous_group))
24562 if (!is_branch_slot_insn (next_insn))
24563 can_issue_more--;
24565 while (can_issue_more)
24567 nop = gen_nop ();
24568 emit_insn_before (nop, next_insn);
24569 can_issue_more--;
24573 can_issue_more = issue_rate;
24574 group_count++;
24577 insn = next_insn;
24578 next_insn = get_next_active_insn (insn, tail);
24581 return group_count;
24584 /* We're beginning a new block. Initialize data structures as necessary. */
24586 static void
24587 rs6000_sched_init (FILE *dump ATTRIBUTE_UNUSED,
24588 int sched_verbose ATTRIBUTE_UNUSED,
24589 int max_ready ATTRIBUTE_UNUSED)
24591 last_scheduled_insn = NULL_RTX;
24592 load_store_pendulum = 0;
24595 /* The following function is called at the end of scheduling BB.
24596 After reload, it inserts nops at insn group bundling. */
24598 static void
24599 rs6000_sched_finish (FILE *dump, int sched_verbose)
24601 int n_groups;
24603 if (sched_verbose)
24604 fprintf (dump, "=== Finishing schedule.\n");
24606 if (reload_completed && rs6000_sched_groups)
24608 /* Do not run sched_finish hook when selective scheduling enabled. */
24609 if (sel_sched_p ())
24610 return;
24612 if (rs6000_sched_insert_nops == sched_finish_none)
24613 return;
24615 if (rs6000_sched_insert_nops == sched_finish_pad_groups)
24616 n_groups = pad_groups (dump, sched_verbose,
24617 current_sched_info->prev_head,
24618 current_sched_info->next_tail);
24619 else
24620 n_groups = redefine_groups (dump, sched_verbose,
24621 current_sched_info->prev_head,
24622 current_sched_info->next_tail);
24624 if (sched_verbose >= 6)
24626 fprintf (dump, "ngroups = %d\n", n_groups);
24627 print_rtl (dump, current_sched_info->prev_head);
24628 fprintf (dump, "Done finish_sched\n");
24633 struct _rs6000_sched_context
24635 short cached_can_issue_more;
24636 rtx last_scheduled_insn;
24637 int load_store_pendulum;
24640 typedef struct _rs6000_sched_context rs6000_sched_context_def;
24641 typedef rs6000_sched_context_def *rs6000_sched_context_t;
24643 /* Allocate store for new scheduling context. */
24644 static void *
24645 rs6000_alloc_sched_context (void)
24647 return xmalloc (sizeof (rs6000_sched_context_def));
24650 /* If CLEAN_P is true then initializes _SC with clean data,
24651 and from the global context otherwise. */
24652 static void
24653 rs6000_init_sched_context (void *_sc, bool clean_p)
24655 rs6000_sched_context_t sc = (rs6000_sched_context_t) _sc;
24657 if (clean_p)
24659 sc->cached_can_issue_more = 0;
24660 sc->last_scheduled_insn = NULL_RTX;
24661 sc->load_store_pendulum = 0;
24663 else
24665 sc->cached_can_issue_more = cached_can_issue_more;
24666 sc->last_scheduled_insn = last_scheduled_insn;
24667 sc->load_store_pendulum = load_store_pendulum;
24671 /* Sets the global scheduling context to the one pointed to by _SC. */
24672 static void
24673 rs6000_set_sched_context (void *_sc)
24675 rs6000_sched_context_t sc = (rs6000_sched_context_t) _sc;
24677 gcc_assert (sc != NULL);
24679 cached_can_issue_more = sc->cached_can_issue_more;
24680 last_scheduled_insn = sc->last_scheduled_insn;
24681 load_store_pendulum = sc->load_store_pendulum;
24684 /* Free _SC. */
24685 static void
24686 rs6000_free_sched_context (void *_sc)
24688 gcc_assert (_sc != NULL);
24690 free (_sc);
24694 /* Length in units of the trampoline for entering a nested function. */
24697 rs6000_trampoline_size (void)
24699 int ret = 0;
24701 switch (DEFAULT_ABI)
24703 default:
24704 gcc_unreachable ();
24706 case ABI_AIX:
24707 ret = (TARGET_32BIT) ? 12 : 24;
24708 break;
24710 case ABI_DARWIN:
24711 case ABI_V4:
24712 ret = (TARGET_32BIT) ? 40 : 48;
24713 break;
24716 return ret;
24719 /* Emit RTL insns to initialize the variable parts of a trampoline.
24720 FNADDR is an RTX for the address of the function's pure code.
24721 CXT is an RTX for the static chain value for the function. */
24723 static void
24724 rs6000_trampoline_init (rtx m_tramp, tree fndecl, rtx cxt)
24726 int regsize = (TARGET_32BIT) ? 4 : 8;
24727 rtx fnaddr = XEXP (DECL_RTL (fndecl), 0);
24728 rtx ctx_reg = force_reg (Pmode, cxt);
24729 rtx addr = force_reg (Pmode, XEXP (m_tramp, 0));
24731 switch (DEFAULT_ABI)
24733 default:
24734 gcc_unreachable ();
24736 /* Under AIX, just build the 3 word function descriptor */
24737 case ABI_AIX:
24739 rtx fnmem, fn_reg, toc_reg;
24741 if (!TARGET_POINTERS_TO_NESTED_FUNCTIONS)
24742 error ("You cannot take the address of a nested function if you use "
24743 "the -mno-pointers-to-nested-functions option.");
24745 fnmem = gen_const_mem (Pmode, force_reg (Pmode, fnaddr));
24746 fn_reg = gen_reg_rtx (Pmode);
24747 toc_reg = gen_reg_rtx (Pmode);
24749 /* Macro to shorten the code expansions below. */
24750 # define MEM_PLUS(MEM, OFFSET) adjust_address (MEM, Pmode, OFFSET)
24752 m_tramp = replace_equiv_address (m_tramp, addr);
24754 emit_move_insn (fn_reg, MEM_PLUS (fnmem, 0));
24755 emit_move_insn (toc_reg, MEM_PLUS (fnmem, regsize));
24756 emit_move_insn (MEM_PLUS (m_tramp, 0), fn_reg);
24757 emit_move_insn (MEM_PLUS (m_tramp, regsize), toc_reg);
24758 emit_move_insn (MEM_PLUS (m_tramp, 2*regsize), ctx_reg);
24760 # undef MEM_PLUS
24762 break;
24764 /* Under V.4/eabi/darwin, __trampoline_setup does the real work. */
24765 case ABI_DARWIN:
24766 case ABI_V4:
24767 emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "__trampoline_setup"),
24768 LCT_NORMAL, VOIDmode, 4,
24769 addr, Pmode,
24770 GEN_INT (rs6000_trampoline_size ()), SImode,
24771 fnaddr, Pmode,
24772 ctx_reg, Pmode);
24773 break;
24778 /* Returns TRUE iff the target attribute indicated by ATTR_ID takes a plain
24779 identifier as an argument, so the front end shouldn't look it up. */
24781 static bool
24782 rs6000_attribute_takes_identifier_p (const_tree attr_id)
24784 return is_attribute_p ("altivec", attr_id);
24787 /* Handle the "altivec" attribute. The attribute may have
24788 arguments as follows:
24790 __attribute__((altivec(vector__)))
24791 __attribute__((altivec(pixel__))) (always followed by 'unsigned short')
24792 __attribute__((altivec(bool__))) (always followed by 'unsigned')
24794 and may appear more than once (e.g., 'vector bool char') in a
24795 given declaration. */
24797 static tree
24798 rs6000_handle_altivec_attribute (tree *node,
24799 tree name ATTRIBUTE_UNUSED,
24800 tree args,
24801 int flags ATTRIBUTE_UNUSED,
24802 bool *no_add_attrs)
24804 tree type = *node, result = NULL_TREE;
24805 enum machine_mode mode;
24806 int unsigned_p;
24807 char altivec_type
24808 = ((args && TREE_CODE (args) == TREE_LIST && TREE_VALUE (args)
24809 && TREE_CODE (TREE_VALUE (args)) == IDENTIFIER_NODE)
24810 ? *IDENTIFIER_POINTER (TREE_VALUE (args))
24811 : '?');
24813 while (POINTER_TYPE_P (type)
24814 || TREE_CODE (type) == FUNCTION_TYPE
24815 || TREE_CODE (type) == METHOD_TYPE
24816 || TREE_CODE (type) == ARRAY_TYPE)
24817 type = TREE_TYPE (type);
24819 mode = TYPE_MODE (type);
24821 /* Check for invalid AltiVec type qualifiers. */
24822 if (type == long_double_type_node)
24823 error ("use of %<long double%> in AltiVec types is invalid");
24824 else if (type == boolean_type_node)
24825 error ("use of boolean types in AltiVec types is invalid");
24826 else if (TREE_CODE (type) == COMPLEX_TYPE)
24827 error ("use of %<complex%> in AltiVec types is invalid");
24828 else if (DECIMAL_FLOAT_MODE_P (mode))
24829 error ("use of decimal floating point types in AltiVec types is invalid");
24830 else if (!TARGET_VSX)
24832 if (type == long_unsigned_type_node || type == long_integer_type_node)
24834 if (TARGET_64BIT)
24835 error ("use of %<long%> in AltiVec types is invalid for "
24836 "64-bit code without -mvsx");
24837 else if (rs6000_warn_altivec_long)
24838 warning (0, "use of %<long%> in AltiVec types is deprecated; "
24839 "use %<int%>");
24841 else if (type == long_long_unsigned_type_node
24842 || type == long_long_integer_type_node)
24843 error ("use of %<long long%> in AltiVec types is invalid without "
24844 "-mvsx");
24845 else if (type == double_type_node)
24846 error ("use of %<double%> in AltiVec types is invalid without -mvsx");
24849 switch (altivec_type)
24851 case 'v':
24852 unsigned_p = TYPE_UNSIGNED (type);
24853 switch (mode)
24855 case DImode:
24856 result = (unsigned_p ? unsigned_V2DI_type_node : V2DI_type_node);
24857 break;
24858 case SImode:
24859 result = (unsigned_p ? unsigned_V4SI_type_node : V4SI_type_node);
24860 break;
24861 case HImode:
24862 result = (unsigned_p ? unsigned_V8HI_type_node : V8HI_type_node);
24863 break;
24864 case QImode:
24865 result = (unsigned_p ? unsigned_V16QI_type_node : V16QI_type_node);
24866 break;
24867 case SFmode: result = V4SF_type_node; break;
24868 case DFmode: result = V2DF_type_node; break;
24869 /* If the user says 'vector int bool', we may be handed the 'bool'
24870 attribute _before_ the 'vector' attribute, and so select the
24871 proper type in the 'b' case below. */
24872 case V4SImode: case V8HImode: case V16QImode: case V4SFmode:
24873 case V2DImode: case V2DFmode:
24874 result = type;
24875 default: break;
24877 break;
24878 case 'b':
24879 switch (mode)
24881 case DImode: case V2DImode: result = bool_V2DI_type_node; break;
24882 case SImode: case V4SImode: result = bool_V4SI_type_node; break;
24883 case HImode: case V8HImode: result = bool_V8HI_type_node; break;
24884 case QImode: case V16QImode: result = bool_V16QI_type_node;
24885 default: break;
24887 break;
24888 case 'p':
24889 switch (mode)
24891 case V8HImode: result = pixel_V8HI_type_node;
24892 default: break;
24894 default: break;
24897 /* Propagate qualifiers attached to the element type
24898 onto the vector type. */
24899 if (result && result != type && TYPE_QUALS (type))
24900 result = build_qualified_type (result, TYPE_QUALS (type));
24902 *no_add_attrs = true; /* No need to hang on to the attribute. */
24904 if (result)
24905 *node = lang_hooks.types.reconstruct_complex_type (*node, result);
24907 return NULL_TREE;
24910 /* AltiVec defines four built-in scalar types that serve as vector
24911 elements; we must teach the compiler how to mangle them. */
24913 static const char *
24914 rs6000_mangle_type (const_tree type)
24916 type = TYPE_MAIN_VARIANT (type);
24918 if (TREE_CODE (type) != VOID_TYPE && TREE_CODE (type) != BOOLEAN_TYPE
24919 && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
24920 return NULL;
24922 if (type == bool_char_type_node) return "U6__boolc";
24923 if (type == bool_short_type_node) return "U6__bools";
24924 if (type == pixel_type_node) return "u7__pixel";
24925 if (type == bool_int_type_node) return "U6__booli";
24926 if (type == bool_long_type_node) return "U6__booll";
24928 /* Mangle IBM extended float long double as `g' (__float128) on
24929 powerpc*-linux where long-double-64 previously was the default. */
24930 if (TYPE_MAIN_VARIANT (type) == long_double_type_node
24931 && TARGET_ELF
24932 && TARGET_LONG_DOUBLE_128
24933 && !TARGET_IEEEQUAD)
24934 return "g";
24936 /* For all other types, use normal C++ mangling. */
24937 return NULL;
24940 /* Handle a "longcall" or "shortcall" attribute; arguments as in
24941 struct attribute_spec.handler. */
24943 static tree
24944 rs6000_handle_longcall_attribute (tree *node, tree name,
24945 tree args ATTRIBUTE_UNUSED,
24946 int flags ATTRIBUTE_UNUSED,
24947 bool *no_add_attrs)
24949 if (TREE_CODE (*node) != FUNCTION_TYPE
24950 && TREE_CODE (*node) != FIELD_DECL
24951 && TREE_CODE (*node) != TYPE_DECL)
24953 warning (OPT_Wattributes, "%qE attribute only applies to functions",
24954 name);
24955 *no_add_attrs = true;
24958 return NULL_TREE;
24961 /* Set longcall attributes on all functions declared when
24962 rs6000_default_long_calls is true. */
24963 static void
24964 rs6000_set_default_type_attributes (tree type)
24966 if (rs6000_default_long_calls
24967 && (TREE_CODE (type) == FUNCTION_TYPE
24968 || TREE_CODE (type) == METHOD_TYPE))
24969 TYPE_ATTRIBUTES (type) = tree_cons (get_identifier ("longcall"),
24970 NULL_TREE,
24971 TYPE_ATTRIBUTES (type));
24973 #if TARGET_MACHO
24974 darwin_set_default_type_attributes (type);
24975 #endif
24978 /* Return a reference suitable for calling a function with the
24979 longcall attribute. */
24982 rs6000_longcall_ref (rtx call_ref)
24984 const char *call_name;
24985 tree node;
24987 if (GET_CODE (call_ref) != SYMBOL_REF)
24988 return call_ref;
24990 /* System V adds '.' to the internal name, so skip them. */
24991 call_name = XSTR (call_ref, 0);
24992 if (*call_name == '.')
24994 while (*call_name == '.')
24995 call_name++;
24997 node = get_identifier (call_name);
24998 call_ref = gen_rtx_SYMBOL_REF (VOIDmode, IDENTIFIER_POINTER (node));
25001 return force_reg (Pmode, call_ref);
25004 #ifndef TARGET_USE_MS_BITFIELD_LAYOUT
25005 #define TARGET_USE_MS_BITFIELD_LAYOUT 0
25006 #endif
25008 /* Handle a "ms_struct" or "gcc_struct" attribute; arguments as in
25009 struct attribute_spec.handler. */
25010 static tree
25011 rs6000_handle_struct_attribute (tree *node, tree name,
25012 tree args ATTRIBUTE_UNUSED,
25013 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
25015 tree *type = NULL;
25016 if (DECL_P (*node))
25018 if (TREE_CODE (*node) == TYPE_DECL)
25019 type = &TREE_TYPE (*node);
25021 else
25022 type = node;
25024 if (!(type && (TREE_CODE (*type) == RECORD_TYPE
25025 || TREE_CODE (*type) == UNION_TYPE)))
25027 warning (OPT_Wattributes, "%qE attribute ignored", name);
25028 *no_add_attrs = true;
25031 else if ((is_attribute_p ("ms_struct", name)
25032 && lookup_attribute ("gcc_struct", TYPE_ATTRIBUTES (*type)))
25033 || ((is_attribute_p ("gcc_struct", name)
25034 && lookup_attribute ("ms_struct", TYPE_ATTRIBUTES (*type)))))
25036 warning (OPT_Wattributes, "%qE incompatible attribute ignored",
25037 name);
25038 *no_add_attrs = true;
25041 return NULL_TREE;
25044 static bool
25045 rs6000_ms_bitfield_layout_p (const_tree record_type)
25047 return (TARGET_USE_MS_BITFIELD_LAYOUT &&
25048 !lookup_attribute ("gcc_struct", TYPE_ATTRIBUTES (record_type)))
25049 || lookup_attribute ("ms_struct", TYPE_ATTRIBUTES (record_type));
25052 #ifdef USING_ELFOS_H
25054 /* A get_unnamed_section callback, used for switching to toc_section. */
25056 static void
25057 rs6000_elf_output_toc_section_asm_op (const void *data ATTRIBUTE_UNUSED)
25059 if (DEFAULT_ABI == ABI_AIX
25060 && TARGET_MINIMAL_TOC
25061 && !TARGET_RELOCATABLE)
25063 if (!toc_initialized)
25065 toc_initialized = 1;
25066 fprintf (asm_out_file, "%s\n", TOC_SECTION_ASM_OP);
25067 (*targetm.asm_out.internal_label) (asm_out_file, "LCTOC", 0);
25068 fprintf (asm_out_file, "\t.tc ");
25069 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (asm_out_file, "LCTOC1[TC],");
25070 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (asm_out_file, "LCTOC1");
25071 fprintf (asm_out_file, "\n");
25073 fprintf (asm_out_file, "%s\n", MINIMAL_TOC_SECTION_ASM_OP);
25074 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (asm_out_file, "LCTOC1");
25075 fprintf (asm_out_file, " = .+32768\n");
25077 else
25078 fprintf (asm_out_file, "%s\n", MINIMAL_TOC_SECTION_ASM_OP);
25080 else if (DEFAULT_ABI == ABI_AIX && !TARGET_RELOCATABLE)
25081 fprintf (asm_out_file, "%s\n", TOC_SECTION_ASM_OP);
25082 else
25084 fprintf (asm_out_file, "%s\n", MINIMAL_TOC_SECTION_ASM_OP);
25085 if (!toc_initialized)
25087 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (asm_out_file, "LCTOC1");
25088 fprintf (asm_out_file, " = .+32768\n");
25089 toc_initialized = 1;
25094 /* Implement TARGET_ASM_INIT_SECTIONS. */
25096 static void
25097 rs6000_elf_asm_init_sections (void)
25099 toc_section
25100 = get_unnamed_section (0, rs6000_elf_output_toc_section_asm_op, NULL);
25102 sdata2_section
25103 = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
25104 SDATA2_SECTION_ASM_OP);
25107 /* Implement TARGET_SELECT_RTX_SECTION. */
25109 static section *
25110 rs6000_elf_select_rtx_section (enum machine_mode mode, rtx x,
25111 unsigned HOST_WIDE_INT align)
25113 if (ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (x, mode))
25114 return toc_section;
25115 else
25116 return default_elf_select_rtx_section (mode, x, align);
25119 /* For a SYMBOL_REF, set generic flags and then perform some
25120 target-specific processing.
25122 When the AIX ABI is requested on a non-AIX system, replace the
25123 function name with the real name (with a leading .) rather than the
25124 function descriptor name. This saves a lot of overriding code to
25125 read the prefixes. */
25127 static void rs6000_elf_encode_section_info (tree, rtx, int) ATTRIBUTE_UNUSED;
25128 static void
25129 rs6000_elf_encode_section_info (tree decl, rtx rtl, int first)
25131 default_encode_section_info (decl, rtl, first);
25133 if (first
25134 && TREE_CODE (decl) == FUNCTION_DECL
25135 && !TARGET_AIX
25136 && DEFAULT_ABI == ABI_AIX)
25138 rtx sym_ref = XEXP (rtl, 0);
25139 size_t len = strlen (XSTR (sym_ref, 0));
25140 char *str = XALLOCAVEC (char, len + 2);
25141 str[0] = '.';
25142 memcpy (str + 1, XSTR (sym_ref, 0), len + 1);
25143 XSTR (sym_ref, 0) = ggc_alloc_string (str, len + 1);
25147 static inline bool
25148 compare_section_name (const char *section, const char *templ)
25150 int len;
25152 len = strlen (templ);
25153 return (strncmp (section, templ, len) == 0
25154 && (section[len] == 0 || section[len] == '.'));
25157 bool
25158 rs6000_elf_in_small_data_p (const_tree decl)
25160 if (rs6000_sdata == SDATA_NONE)
25161 return false;
25163 /* We want to merge strings, so we never consider them small data. */
25164 if (TREE_CODE (decl) == STRING_CST)
25165 return false;
25167 /* Functions are never in the small data area. */
25168 if (TREE_CODE (decl) == FUNCTION_DECL)
25169 return false;
25171 if (TREE_CODE (decl) == VAR_DECL && DECL_SECTION_NAME (decl))
25173 const char *section = TREE_STRING_POINTER (DECL_SECTION_NAME (decl));
25174 if (compare_section_name (section, ".sdata")
25175 || compare_section_name (section, ".sdata2")
25176 || compare_section_name (section, ".gnu.linkonce.s")
25177 || compare_section_name (section, ".sbss")
25178 || compare_section_name (section, ".sbss2")
25179 || compare_section_name (section, ".gnu.linkonce.sb")
25180 || strcmp (section, ".PPC.EMB.sdata0") == 0
25181 || strcmp (section, ".PPC.EMB.sbss0") == 0)
25182 return true;
25184 else
25186 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (decl));
25188 if (size > 0
25189 && size <= g_switch_value
25190 /* If it's not public, and we're not going to reference it there,
25191 there's no need to put it in the small data section. */
25192 && (rs6000_sdata != SDATA_DATA || TREE_PUBLIC (decl)))
25193 return true;
25196 return false;
25199 #endif /* USING_ELFOS_H */
25201 /* Implement TARGET_USE_BLOCKS_FOR_CONSTANT_P. */
25203 static bool
25204 rs6000_use_blocks_for_constant_p (enum machine_mode mode, const_rtx x)
25206 return !ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (x, mode);
25209 /* Do not place thread-local symbols refs in the object blocks. */
25211 static bool
25212 rs6000_use_blocks_for_decl_p (const_tree decl)
25214 return !DECL_THREAD_LOCAL_P (decl);
25217 /* Return a REG that occurs in ADDR with coefficient 1.
25218 ADDR can be effectively incremented by incrementing REG.
25220 r0 is special and we must not select it as an address
25221 register by this routine since our caller will try to
25222 increment the returned register via an "la" instruction. */
25225 find_addr_reg (rtx addr)
25227 while (GET_CODE (addr) == PLUS)
25229 if (GET_CODE (XEXP (addr, 0)) == REG
25230 && REGNO (XEXP (addr, 0)) != 0)
25231 addr = XEXP (addr, 0);
25232 else if (GET_CODE (XEXP (addr, 1)) == REG
25233 && REGNO (XEXP (addr, 1)) != 0)
25234 addr = XEXP (addr, 1);
25235 else if (CONSTANT_P (XEXP (addr, 0)))
25236 addr = XEXP (addr, 1);
25237 else if (CONSTANT_P (XEXP (addr, 1)))
25238 addr = XEXP (addr, 0);
25239 else
25240 gcc_unreachable ();
25242 gcc_assert (GET_CODE (addr) == REG && REGNO (addr) != 0);
25243 return addr;
25246 void
25247 rs6000_fatal_bad_address (rtx op)
25249 fatal_insn ("bad address", op);
25252 #if TARGET_MACHO
25254 typedef struct branch_island_d {
25255 tree function_name;
25256 tree label_name;
25257 int line_number;
25258 } branch_island;
25261 static vec<branch_island, va_gc> *branch_islands;
25263 /* Remember to generate a branch island for far calls to the given
25264 function. */
25266 static void
25267 add_compiler_branch_island (tree label_name, tree function_name,
25268 int line_number)
25270 branch_island bi = {function_name, label_name, line_number};
25271 vec_safe_push (branch_islands, bi);
25274 /* Generate far-jump branch islands for everything recorded in
25275 branch_islands. Invoked immediately after the last instruction of
25276 the epilogue has been emitted; the branch islands must be appended
25277 to, and contiguous with, the function body. Mach-O stubs are
25278 generated in machopic_output_stub(). */
25280 static void
25281 macho_branch_islands (void)
25283 char tmp_buf[512];
25285 while (!vec_safe_is_empty (branch_islands))
25287 branch_island *bi = &branch_islands->last ();
25288 const char *label = IDENTIFIER_POINTER (bi->label_name);
25289 const char *name = IDENTIFIER_POINTER (bi->function_name);
25290 char name_buf[512];
25291 /* Cheap copy of the details from the Darwin ASM_OUTPUT_LABELREF(). */
25292 if (name[0] == '*' || name[0] == '&')
25293 strcpy (name_buf, name+1);
25294 else
25296 name_buf[0] = '_';
25297 strcpy (name_buf+1, name);
25299 strcpy (tmp_buf, "\n");
25300 strcat (tmp_buf, label);
25301 #if defined (DBX_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)
25302 if (write_symbols == DBX_DEBUG || write_symbols == XCOFF_DEBUG)
25303 dbxout_stabd (N_SLINE, bi->line_number);
25304 #endif /* DBX_DEBUGGING_INFO || XCOFF_DEBUGGING_INFO */
25305 if (flag_pic)
25307 if (TARGET_LINK_STACK)
25309 char name[32];
25310 get_ppc476_thunk_name (name);
25311 strcat (tmp_buf, ":\n\tmflr r0\n\tbl ");
25312 strcat (tmp_buf, name);
25313 strcat (tmp_buf, "\n");
25314 strcat (tmp_buf, label);
25315 strcat (tmp_buf, "_pic:\n\tmflr r11\n");
25317 else
25319 strcat (tmp_buf, ":\n\tmflr r0\n\tbcl 20,31,");
25320 strcat (tmp_buf, label);
25321 strcat (tmp_buf, "_pic\n");
25322 strcat (tmp_buf, label);
25323 strcat (tmp_buf, "_pic:\n\tmflr r11\n");
25326 strcat (tmp_buf, "\taddis r11,r11,ha16(");
25327 strcat (tmp_buf, name_buf);
25328 strcat (tmp_buf, " - ");
25329 strcat (tmp_buf, label);
25330 strcat (tmp_buf, "_pic)\n");
25332 strcat (tmp_buf, "\tmtlr r0\n");
25334 strcat (tmp_buf, "\taddi r12,r11,lo16(");
25335 strcat (tmp_buf, name_buf);
25336 strcat (tmp_buf, " - ");
25337 strcat (tmp_buf, label);
25338 strcat (tmp_buf, "_pic)\n");
25340 strcat (tmp_buf, "\tmtctr r12\n\tbctr\n");
25342 else
25344 strcat (tmp_buf, ":\nlis r12,hi16(");
25345 strcat (tmp_buf, name_buf);
25346 strcat (tmp_buf, ")\n\tori r12,r12,lo16(");
25347 strcat (tmp_buf, name_buf);
25348 strcat (tmp_buf, ")\n\tmtctr r12\n\tbctr");
25350 output_asm_insn (tmp_buf, 0);
25351 #if defined (DBX_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)
25352 if (write_symbols == DBX_DEBUG || write_symbols == XCOFF_DEBUG)
25353 dbxout_stabd (N_SLINE, bi->line_number);
25354 #endif /* DBX_DEBUGGING_INFO || XCOFF_DEBUGGING_INFO */
25355 branch_islands->pop ();
25359 /* NO_PREVIOUS_DEF checks in the link list whether the function name is
25360 already there or not. */
25362 static int
25363 no_previous_def (tree function_name)
25365 branch_island *bi;
25366 unsigned ix;
25368 FOR_EACH_VEC_SAFE_ELT (branch_islands, ix, bi)
25369 if (function_name == bi->function_name)
25370 return 0;
25371 return 1;
25374 /* GET_PREV_LABEL gets the label name from the previous definition of
25375 the function. */
25377 static tree
25378 get_prev_label (tree function_name)
25380 branch_island *bi;
25381 unsigned ix;
25383 FOR_EACH_VEC_SAFE_ELT (branch_islands, ix, bi)
25384 if (function_name == bi->function_name)
25385 return bi->label_name;
25386 return NULL_TREE;
25389 /* INSN is either a function call or a millicode call. It may have an
25390 unconditional jump in its delay slot.
25392 CALL_DEST is the routine we are calling. */
25394 char *
25395 output_call (rtx insn, rtx *operands, int dest_operand_number,
25396 int cookie_operand_number)
25398 static char buf[256];
25399 if (darwin_emit_branch_islands
25400 && GET_CODE (operands[dest_operand_number]) == SYMBOL_REF
25401 && (INTVAL (operands[cookie_operand_number]) & CALL_LONG))
25403 tree labelname;
25404 tree funname = get_identifier (XSTR (operands[dest_operand_number], 0));
25406 if (no_previous_def (funname))
25408 rtx label_rtx = gen_label_rtx ();
25409 char *label_buf, temp_buf[256];
25410 ASM_GENERATE_INTERNAL_LABEL (temp_buf, "L",
25411 CODE_LABEL_NUMBER (label_rtx));
25412 label_buf = temp_buf[0] == '*' ? temp_buf + 1 : temp_buf;
25413 labelname = get_identifier (label_buf);
25414 add_compiler_branch_island (labelname, funname, insn_line (insn));
25416 else
25417 labelname = get_prev_label (funname);
25419 /* "jbsr foo, L42" is Mach-O for "Link as 'bl foo' if a 'bl'
25420 instruction will reach 'foo', otherwise link as 'bl L42'".
25421 "L42" should be a 'branch island', that will do a far jump to
25422 'foo'. Branch islands are generated in
25423 macho_branch_islands(). */
25424 sprintf (buf, "jbsr %%z%d,%.246s",
25425 dest_operand_number, IDENTIFIER_POINTER (labelname));
25427 else
25428 sprintf (buf, "bl %%z%d", dest_operand_number);
25429 return buf;
25432 /* Generate PIC and indirect symbol stubs. */
25434 void
25435 machopic_output_stub (FILE *file, const char *symb, const char *stub)
25437 unsigned int length;
25438 char *symbol_name, *lazy_ptr_name;
25439 char *local_label_0;
25440 static int label = 0;
25442 /* Lose our funky encoding stuff so it doesn't contaminate the stub. */
25443 symb = (*targetm.strip_name_encoding) (symb);
25446 length = strlen (symb);
25447 symbol_name = XALLOCAVEC (char, length + 32);
25448 GEN_SYMBOL_NAME_FOR_SYMBOL (symbol_name, symb, length);
25450 lazy_ptr_name = XALLOCAVEC (char, length + 32);
25451 GEN_LAZY_PTR_NAME_FOR_SYMBOL (lazy_ptr_name, symb, length);
25453 if (flag_pic == 2)
25454 switch_to_section (darwin_sections[machopic_picsymbol_stub1_section]);
25455 else
25456 switch_to_section (darwin_sections[machopic_symbol_stub1_section]);
25458 if (flag_pic == 2)
25460 fprintf (file, "\t.align 5\n");
25462 fprintf (file, "%s:\n", stub);
25463 fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
25465 label++;
25466 local_label_0 = XALLOCAVEC (char, sizeof ("\"L00000000000$spb\""));
25467 sprintf (local_label_0, "\"L%011d$spb\"", label);
25469 fprintf (file, "\tmflr r0\n");
25470 if (TARGET_LINK_STACK)
25472 char name[32];
25473 get_ppc476_thunk_name (name);
25474 fprintf (file, "\tbl %s\n", name);
25475 fprintf (file, "%s:\n\tmflr r11\n", local_label_0);
25477 else
25479 fprintf (file, "\tbcl 20,31,%s\n", local_label_0);
25480 fprintf (file, "%s:\n\tmflr r11\n", local_label_0);
25482 fprintf (file, "\taddis r11,r11,ha16(%s-%s)\n",
25483 lazy_ptr_name, local_label_0);
25484 fprintf (file, "\tmtlr r0\n");
25485 fprintf (file, "\t%s r12,lo16(%s-%s)(r11)\n",
25486 (TARGET_64BIT ? "ldu" : "lwzu"),
25487 lazy_ptr_name, local_label_0);
25488 fprintf (file, "\tmtctr r12\n");
25489 fprintf (file, "\tbctr\n");
25491 else
25493 fprintf (file, "\t.align 4\n");
25495 fprintf (file, "%s:\n", stub);
25496 fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
25498 fprintf (file, "\tlis r11,ha16(%s)\n", lazy_ptr_name);
25499 fprintf (file, "\t%s r12,lo16(%s)(r11)\n",
25500 (TARGET_64BIT ? "ldu" : "lwzu"),
25501 lazy_ptr_name);
25502 fprintf (file, "\tmtctr r12\n");
25503 fprintf (file, "\tbctr\n");
25506 switch_to_section (darwin_sections[machopic_lazy_symbol_ptr_section]);
25507 fprintf (file, "%s:\n", lazy_ptr_name);
25508 fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
25509 fprintf (file, "%sdyld_stub_binding_helper\n",
25510 (TARGET_64BIT ? DOUBLE_INT_ASM_OP : "\t.long\t"));
25513 /* Legitimize PIC addresses. If the address is already
25514 position-independent, we return ORIG. Newly generated
25515 position-independent addresses go into a reg. This is REG if non
25516 zero, otherwise we allocate register(s) as necessary. */
25518 #define SMALL_INT(X) ((UINTVAL (X) + 0x8000) < 0x10000)
25521 rs6000_machopic_legitimize_pic_address (rtx orig, enum machine_mode mode,
25522 rtx reg)
25524 rtx base, offset;
25526 if (reg == NULL && ! reload_in_progress && ! reload_completed)
25527 reg = gen_reg_rtx (Pmode);
25529 if (GET_CODE (orig) == CONST)
25531 rtx reg_temp;
25533 if (GET_CODE (XEXP (orig, 0)) == PLUS
25534 && XEXP (XEXP (orig, 0), 0) == pic_offset_table_rtx)
25535 return orig;
25537 gcc_assert (GET_CODE (XEXP (orig, 0)) == PLUS);
25539 /* Use a different reg for the intermediate value, as
25540 it will be marked UNCHANGING. */
25541 reg_temp = !can_create_pseudo_p () ? reg : gen_reg_rtx (Pmode);
25542 base = rs6000_machopic_legitimize_pic_address (XEXP (XEXP (orig, 0), 0),
25543 Pmode, reg_temp);
25544 offset =
25545 rs6000_machopic_legitimize_pic_address (XEXP (XEXP (orig, 0), 1),
25546 Pmode, reg);
25548 if (GET_CODE (offset) == CONST_INT)
25550 if (SMALL_INT (offset))
25551 return plus_constant (Pmode, base, INTVAL (offset));
25552 else if (! reload_in_progress && ! reload_completed)
25553 offset = force_reg (Pmode, offset);
25554 else
25556 rtx mem = force_const_mem (Pmode, orig);
25557 return machopic_legitimize_pic_address (mem, Pmode, reg);
25560 return gen_rtx_PLUS (Pmode, base, offset);
25563 /* Fall back on generic machopic code. */
25564 return machopic_legitimize_pic_address (orig, mode, reg);
25567 /* Output a .machine directive for the Darwin assembler, and call
25568 the generic start_file routine. */
25570 static void
25571 rs6000_darwin_file_start (void)
25573 static const struct
25575 const char *arg;
25576 const char *name;
25577 HOST_WIDE_INT if_set;
25578 } mapping[] = {
25579 { "ppc64", "ppc64", MASK_64BIT },
25580 { "970", "ppc970", MASK_PPC_GPOPT | MASK_MFCRF | MASK_POWERPC64 },
25581 { "power4", "ppc970", 0 },
25582 { "G5", "ppc970", 0 },
25583 { "7450", "ppc7450", 0 },
25584 { "7400", "ppc7400", MASK_ALTIVEC },
25585 { "G4", "ppc7400", 0 },
25586 { "750", "ppc750", 0 },
25587 { "740", "ppc750", 0 },
25588 { "G3", "ppc750", 0 },
25589 { "604e", "ppc604e", 0 },
25590 { "604", "ppc604", 0 },
25591 { "603e", "ppc603", 0 },
25592 { "603", "ppc603", 0 },
25593 { "601", "ppc601", 0 },
25594 { NULL, "ppc", 0 } };
25595 const char *cpu_id = "";
25596 size_t i;
25598 rs6000_file_start ();
25599 darwin_file_start ();
25601 /* Determine the argument to -mcpu=. Default to G3 if not specified. */
25603 if (rs6000_default_cpu != 0 && rs6000_default_cpu[0] != '\0')
25604 cpu_id = rs6000_default_cpu;
25606 if (global_options_set.x_rs6000_cpu_index)
25607 cpu_id = processor_target_table[rs6000_cpu_index].name;
25609 /* Look through the mapping array. Pick the first name that either
25610 matches the argument, has a bit set in IF_SET that is also set
25611 in the target flags, or has a NULL name. */
25613 i = 0;
25614 while (mapping[i].arg != NULL
25615 && strcmp (mapping[i].arg, cpu_id) != 0
25616 && (mapping[i].if_set & rs6000_isa_flags) == 0)
25617 i++;
25619 fprintf (asm_out_file, "\t.machine %s\n", mapping[i].name);
25622 #endif /* TARGET_MACHO */
25624 #if TARGET_ELF
25625 static int
25626 rs6000_elf_reloc_rw_mask (void)
25628 if (flag_pic)
25629 return 3;
25630 else if (DEFAULT_ABI == ABI_AIX)
25631 return 2;
25632 else
25633 return 0;
25636 /* Record an element in the table of global constructors. SYMBOL is
25637 a SYMBOL_REF of the function to be called; PRIORITY is a number
25638 between 0 and MAX_INIT_PRIORITY.
25640 This differs from default_named_section_asm_out_constructor in
25641 that we have special handling for -mrelocatable. */
25643 static void rs6000_elf_asm_out_constructor (rtx, int) ATTRIBUTE_UNUSED;
25644 static void
25645 rs6000_elf_asm_out_constructor (rtx symbol, int priority)
25647 const char *section = ".ctors";
25648 char buf[16];
25650 if (priority != DEFAULT_INIT_PRIORITY)
25652 sprintf (buf, ".ctors.%.5u",
25653 /* Invert the numbering so the linker puts us in the proper
25654 order; constructors are run from right to left, and the
25655 linker sorts in increasing order. */
25656 MAX_INIT_PRIORITY - priority);
25657 section = buf;
25660 switch_to_section (get_section (section, SECTION_WRITE, NULL));
25661 assemble_align (POINTER_SIZE);
25663 if (TARGET_RELOCATABLE)
25665 fputs ("\t.long (", asm_out_file);
25666 output_addr_const (asm_out_file, symbol);
25667 fputs (")@fixup\n", asm_out_file);
25669 else
25670 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
25673 static void rs6000_elf_asm_out_destructor (rtx, int) ATTRIBUTE_UNUSED;
25674 static void
25675 rs6000_elf_asm_out_destructor (rtx symbol, int priority)
25677 const char *section = ".dtors";
25678 char buf[16];
25680 if (priority != DEFAULT_INIT_PRIORITY)
25682 sprintf (buf, ".dtors.%.5u",
25683 /* Invert the numbering so the linker puts us in the proper
25684 order; constructors are run from right to left, and the
25685 linker sorts in increasing order. */
25686 MAX_INIT_PRIORITY - priority);
25687 section = buf;
25690 switch_to_section (get_section (section, SECTION_WRITE, NULL));
25691 assemble_align (POINTER_SIZE);
25693 if (TARGET_RELOCATABLE)
25695 fputs ("\t.long (", asm_out_file);
25696 output_addr_const (asm_out_file, symbol);
25697 fputs (")@fixup\n", asm_out_file);
25699 else
25700 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
25703 void
25704 rs6000_elf_declare_function_name (FILE *file, const char *name, tree decl)
25706 if (TARGET_64BIT)
25708 fputs ("\t.section\t\".opd\",\"aw\"\n\t.align 3\n", file);
25709 ASM_OUTPUT_LABEL (file, name);
25710 fputs (DOUBLE_INT_ASM_OP, file);
25711 rs6000_output_function_entry (file, name);
25712 fputs (",.TOC.@tocbase,0\n\t.previous\n", file);
25713 if (DOT_SYMBOLS)
25715 fputs ("\t.size\t", file);
25716 assemble_name (file, name);
25717 fputs (",24\n\t.type\t.", file);
25718 assemble_name (file, name);
25719 fputs (",@function\n", file);
25720 if (TREE_PUBLIC (decl) && ! DECL_WEAK (decl))
25722 fputs ("\t.globl\t.", file);
25723 assemble_name (file, name);
25724 putc ('\n', file);
25727 else
25728 ASM_OUTPUT_TYPE_DIRECTIVE (file, name, "function");
25729 ASM_DECLARE_RESULT (file, DECL_RESULT (decl));
25730 rs6000_output_function_entry (file, name);
25731 fputs (":\n", file);
25732 return;
25735 if (TARGET_RELOCATABLE
25736 && !TARGET_SECURE_PLT
25737 && (get_pool_size () != 0 || crtl->profile)
25738 && uses_TOC ())
25740 char buf[256];
25742 (*targetm.asm_out.internal_label) (file, "LCL", rs6000_pic_labelno);
25744 ASM_GENERATE_INTERNAL_LABEL (buf, "LCTOC", 1);
25745 fprintf (file, "\t.long ");
25746 assemble_name (file, buf);
25747 putc ('-', file);
25748 ASM_GENERATE_INTERNAL_LABEL (buf, "LCF", rs6000_pic_labelno);
25749 assemble_name (file, buf);
25750 putc ('\n', file);
25753 ASM_OUTPUT_TYPE_DIRECTIVE (file, name, "function");
25754 ASM_DECLARE_RESULT (file, DECL_RESULT (decl));
25756 if (DEFAULT_ABI == ABI_AIX)
25758 const char *desc_name, *orig_name;
25760 orig_name = (*targetm.strip_name_encoding) (name);
25761 desc_name = orig_name;
25762 while (*desc_name == '.')
25763 desc_name++;
25765 if (TREE_PUBLIC (decl))
25766 fprintf (file, "\t.globl %s\n", desc_name);
25768 fprintf (file, "%s\n", MINIMAL_TOC_SECTION_ASM_OP);
25769 fprintf (file, "%s:\n", desc_name);
25770 fprintf (file, "\t.long %s\n", orig_name);
25771 fputs ("\t.long _GLOBAL_OFFSET_TABLE_\n", file);
25772 if (DEFAULT_ABI == ABI_AIX)
25773 fputs ("\t.long 0\n", file);
25774 fprintf (file, "\t.previous\n");
25776 ASM_OUTPUT_LABEL (file, name);
25779 static void rs6000_elf_file_end (void) ATTRIBUTE_UNUSED;
25780 static void
25781 rs6000_elf_file_end (void)
25783 #ifdef HAVE_AS_GNU_ATTRIBUTE
25784 if (TARGET_32BIT && DEFAULT_ABI == ABI_V4)
25786 if (rs6000_passes_float)
25787 fprintf (asm_out_file, "\t.gnu_attribute 4, %d\n",
25788 ((TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT) ? 1
25789 : (TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_SINGLE_FLOAT) ? 3
25790 : 2));
25791 if (rs6000_passes_vector)
25792 fprintf (asm_out_file, "\t.gnu_attribute 8, %d\n",
25793 (TARGET_ALTIVEC_ABI ? 2
25794 : TARGET_SPE_ABI ? 3
25795 : 1));
25796 if (rs6000_returns_struct)
25797 fprintf (asm_out_file, "\t.gnu_attribute 12, %d\n",
25798 aix_struct_return ? 2 : 1);
25800 #endif
25801 #if defined (POWERPC_LINUX) || defined (POWERPC_FREEBSD)
25802 if (TARGET_32BIT)
25803 file_end_indicate_exec_stack ();
25804 #endif
25806 #endif
25808 #if TARGET_XCOFF
25809 static void
25810 rs6000_xcoff_asm_output_anchor (rtx symbol)
25812 char buffer[100];
25814 sprintf (buffer, "$ + " HOST_WIDE_INT_PRINT_DEC,
25815 SYMBOL_REF_BLOCK_OFFSET (symbol));
25816 ASM_OUTPUT_DEF (asm_out_file, XSTR (symbol, 0), buffer);
25819 static void
25820 rs6000_xcoff_asm_globalize_label (FILE *stream, const char *name)
25822 fputs (GLOBAL_ASM_OP, stream);
25823 RS6000_OUTPUT_BASENAME (stream, name);
25824 putc ('\n', stream);
25827 /* A get_unnamed_decl callback, used for read-only sections. PTR
25828 points to the section string variable. */
25830 static void
25831 rs6000_xcoff_output_readonly_section_asm_op (const void *directive)
25833 fprintf (asm_out_file, "\t.csect %s[RO],%s\n",
25834 *(const char *const *) directive,
25835 XCOFF_CSECT_DEFAULT_ALIGNMENT_STR);
25838 /* Likewise for read-write sections. */
25840 static void
25841 rs6000_xcoff_output_readwrite_section_asm_op (const void *directive)
25843 fprintf (asm_out_file, "\t.csect %s[RW],%s\n",
25844 *(const char *const *) directive,
25845 XCOFF_CSECT_DEFAULT_ALIGNMENT_STR);
25848 static void
25849 rs6000_xcoff_output_tls_section_asm_op (const void *directive)
25851 fprintf (asm_out_file, "\t.csect %s[TL],%s\n",
25852 *(const char *const *) directive,
25853 XCOFF_CSECT_DEFAULT_ALIGNMENT_STR);
25856 /* A get_unnamed_section callback, used for switching to toc_section. */
25858 static void
25859 rs6000_xcoff_output_toc_section_asm_op (const void *data ATTRIBUTE_UNUSED)
25861 if (TARGET_MINIMAL_TOC)
25863 /* toc_section is always selected at least once from
25864 rs6000_xcoff_file_start, so this is guaranteed to
25865 always be defined once and only once in each file. */
25866 if (!toc_initialized)
25868 fputs ("\t.toc\nLCTOC..1:\n", asm_out_file);
25869 fputs ("\t.tc toc_table[TC],toc_table[RW]\n", asm_out_file);
25870 toc_initialized = 1;
25872 fprintf (asm_out_file, "\t.csect toc_table[RW]%s\n",
25873 (TARGET_32BIT ? "" : ",3"));
25875 else
25876 fputs ("\t.toc\n", asm_out_file);
25879 /* Implement TARGET_ASM_INIT_SECTIONS. */
25881 static void
25882 rs6000_xcoff_asm_init_sections (void)
25884 read_only_data_section
25885 = get_unnamed_section (0, rs6000_xcoff_output_readonly_section_asm_op,
25886 &xcoff_read_only_section_name);
25888 private_data_section
25889 = get_unnamed_section (SECTION_WRITE,
25890 rs6000_xcoff_output_readwrite_section_asm_op,
25891 &xcoff_private_data_section_name);
25893 tls_data_section
25894 = get_unnamed_section (SECTION_TLS,
25895 rs6000_xcoff_output_tls_section_asm_op,
25896 &xcoff_tls_data_section_name);
25898 tls_private_data_section
25899 = get_unnamed_section (SECTION_TLS,
25900 rs6000_xcoff_output_tls_section_asm_op,
25901 &xcoff_private_data_section_name);
25903 read_only_private_data_section
25904 = get_unnamed_section (0, rs6000_xcoff_output_readonly_section_asm_op,
25905 &xcoff_private_data_section_name);
25907 toc_section
25908 = get_unnamed_section (0, rs6000_xcoff_output_toc_section_asm_op, NULL);
25910 readonly_data_section = read_only_data_section;
25911 exception_section = data_section;
25914 static int
25915 rs6000_xcoff_reloc_rw_mask (void)
25917 return 3;
25920 static void
25921 rs6000_xcoff_asm_named_section (const char *name, unsigned int flags,
25922 tree decl ATTRIBUTE_UNUSED)
25924 int smclass;
25925 static const char * const suffix[4] = { "PR", "RO", "RW", "TL" };
25927 if (flags & SECTION_CODE)
25928 smclass = 0;
25929 else if (flags & SECTION_TLS)
25930 smclass = 3;
25931 else if (flags & SECTION_WRITE)
25932 smclass = 2;
25933 else
25934 smclass = 1;
25936 fprintf (asm_out_file, "\t.csect %s%s[%s],%u\n",
25937 (flags & SECTION_CODE) ? "." : "",
25938 name, suffix[smclass], flags & SECTION_ENTSIZE);
25941 static section *
25942 rs6000_xcoff_select_section (tree decl, int reloc,
25943 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
25945 if (decl_readonly_section (decl, reloc))
25947 if (TREE_PUBLIC (decl))
25948 return read_only_data_section;
25949 else
25950 return read_only_private_data_section;
25952 else
25954 #if HAVE_AS_TLS
25955 if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
25957 if (TREE_PUBLIC (decl))
25958 return tls_data_section;
25959 else if (bss_initializer_p (decl))
25961 /* Convert to COMMON to emit in BSS. */
25962 DECL_COMMON (decl) = 1;
25963 return tls_comm_section;
25965 else
25966 return tls_private_data_section;
25968 else
25969 #endif
25970 if (TREE_PUBLIC (decl))
25971 return data_section;
25972 else
25973 return private_data_section;
25977 static void
25978 rs6000_xcoff_unique_section (tree decl, int reloc ATTRIBUTE_UNUSED)
25980 const char *name;
25982 /* Use select_section for private and uninitialized data. */
25983 if (!TREE_PUBLIC (decl)
25984 || DECL_COMMON (decl)
25985 || DECL_INITIAL (decl) == NULL_TREE
25986 || DECL_INITIAL (decl) == error_mark_node
25987 || (flag_zero_initialized_in_bss
25988 && initializer_zerop (DECL_INITIAL (decl))))
25989 return;
25991 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
25992 name = (*targetm.strip_name_encoding) (name);
25993 DECL_SECTION_NAME (decl) = build_string (strlen (name), name);
25996 /* Select section for constant in constant pool.
25998 On RS/6000, all constants are in the private read-only data area.
25999 However, if this is being placed in the TOC it must be output as a
26000 toc entry. */
26002 static section *
26003 rs6000_xcoff_select_rtx_section (enum machine_mode mode, rtx x,
26004 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
26006 if (ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (x, mode))
26007 return toc_section;
26008 else
26009 return read_only_private_data_section;
26012 /* Remove any trailing [DS] or the like from the symbol name. */
26014 static const char *
26015 rs6000_xcoff_strip_name_encoding (const char *name)
26017 size_t len;
26018 if (*name == '*')
26019 name++;
26020 len = strlen (name);
26021 if (name[len - 1] == ']')
26022 return ggc_alloc_string (name, len - 4);
26023 else
26024 return name;
26027 /* Section attributes. AIX is always PIC. */
26029 static unsigned int
26030 rs6000_xcoff_section_type_flags (tree decl, const char *name, int reloc)
26032 unsigned int align;
26033 unsigned int flags = default_section_type_flags (decl, name, reloc);
26035 /* Align to at least UNIT size. */
26036 if (flags & SECTION_CODE || !decl)
26037 align = MIN_UNITS_PER_WORD;
26038 else
26039 /* Increase alignment of large objects if not already stricter. */
26040 align = MAX ((DECL_ALIGN (decl) / BITS_PER_UNIT),
26041 int_size_in_bytes (TREE_TYPE (decl)) > MIN_UNITS_PER_WORD
26042 ? UNITS_PER_FP_WORD : MIN_UNITS_PER_WORD);
26044 return flags | (exact_log2 (align) & SECTION_ENTSIZE);
26047 /* Output at beginning of assembler file.
26049 Initialize the section names for the RS/6000 at this point.
26051 Specify filename, including full path, to assembler.
26053 We want to go into the TOC section so at least one .toc will be emitted.
26054 Also, in order to output proper .bs/.es pairs, we need at least one static
26055 [RW] section emitted.
26057 Finally, declare mcount when profiling to make the assembler happy. */
26059 static void
26060 rs6000_xcoff_file_start (void)
26062 rs6000_gen_section_name (&xcoff_bss_section_name,
26063 main_input_filename, ".bss_");
26064 rs6000_gen_section_name (&xcoff_private_data_section_name,
26065 main_input_filename, ".rw_");
26066 rs6000_gen_section_name (&xcoff_read_only_section_name,
26067 main_input_filename, ".ro_");
26068 rs6000_gen_section_name (&xcoff_tls_data_section_name,
26069 main_input_filename, ".tls_");
26070 rs6000_gen_section_name (&xcoff_tbss_section_name,
26071 main_input_filename, ".tbss_[UL]");
26073 fputs ("\t.file\t", asm_out_file);
26074 output_quoted_string (asm_out_file, main_input_filename);
26075 fputc ('\n', asm_out_file);
26076 if (write_symbols != NO_DEBUG)
26077 switch_to_section (private_data_section);
26078 switch_to_section (text_section);
26079 if (profile_flag)
26080 fprintf (asm_out_file, "\t.extern %s\n", RS6000_MCOUNT);
26081 rs6000_file_start ();
26084 /* Output at end of assembler file.
26085 On the RS/6000, referencing data should automatically pull in text. */
26087 static void
26088 rs6000_xcoff_file_end (void)
26090 switch_to_section (text_section);
26091 fputs ("_section_.text:\n", asm_out_file);
26092 switch_to_section (data_section);
26093 fputs (TARGET_32BIT
26094 ? "\t.long _section_.text\n" : "\t.llong _section_.text\n",
26095 asm_out_file);
26098 #ifdef HAVE_AS_TLS
26099 static void
26100 rs6000_xcoff_encode_section_info (tree decl, rtx rtl, int first)
26102 rtx symbol;
26103 int flags;
26105 default_encode_section_info (decl, rtl, first);
26107 /* Careful not to prod global register variables. */
26108 if (!MEM_P (rtl))
26109 return;
26110 symbol = XEXP (rtl, 0);
26111 if (GET_CODE (symbol) != SYMBOL_REF)
26112 return;
26114 flags = SYMBOL_REF_FLAGS (symbol);
26116 if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
26117 flags &= ~SYMBOL_FLAG_HAS_BLOCK_INFO;
26119 SYMBOL_REF_FLAGS (symbol) = flags;
26121 #endif /* HAVE_AS_TLS */
26122 #endif /* TARGET_XCOFF */
26124 /* Compute a (partial) cost for rtx X. Return true if the complete
26125 cost has been computed, and false if subexpressions should be
26126 scanned. In either case, *TOTAL contains the cost result. */
26128 static bool
26129 rs6000_rtx_costs (rtx x, int code, int outer_code, int opno ATTRIBUTE_UNUSED,
26130 int *total, bool speed)
26132 enum machine_mode mode = GET_MODE (x);
26134 switch (code)
26136 /* On the RS/6000, if it is valid in the insn, it is free. */
26137 case CONST_INT:
26138 if (((outer_code == SET
26139 || outer_code == PLUS
26140 || outer_code == MINUS)
26141 && (satisfies_constraint_I (x)
26142 || satisfies_constraint_L (x)))
26143 || (outer_code == AND
26144 && (satisfies_constraint_K (x)
26145 || (mode == SImode
26146 ? satisfies_constraint_L (x)
26147 : satisfies_constraint_J (x))
26148 || mask_operand (x, mode)
26149 || (mode == DImode
26150 && mask64_operand (x, DImode))))
26151 || ((outer_code == IOR || outer_code == XOR)
26152 && (satisfies_constraint_K (x)
26153 || (mode == SImode
26154 ? satisfies_constraint_L (x)
26155 : satisfies_constraint_J (x))))
26156 || outer_code == ASHIFT
26157 || outer_code == ASHIFTRT
26158 || outer_code == LSHIFTRT
26159 || outer_code == ROTATE
26160 || outer_code == ROTATERT
26161 || outer_code == ZERO_EXTRACT
26162 || (outer_code == MULT
26163 && satisfies_constraint_I (x))
26164 || ((outer_code == DIV || outer_code == UDIV
26165 || outer_code == MOD || outer_code == UMOD)
26166 && exact_log2 (INTVAL (x)) >= 0)
26167 || (outer_code == COMPARE
26168 && (satisfies_constraint_I (x)
26169 || satisfies_constraint_K (x)))
26170 || ((outer_code == EQ || outer_code == NE)
26171 && (satisfies_constraint_I (x)
26172 || satisfies_constraint_K (x)
26173 || (mode == SImode
26174 ? satisfies_constraint_L (x)
26175 : satisfies_constraint_J (x))))
26176 || (outer_code == GTU
26177 && satisfies_constraint_I (x))
26178 || (outer_code == LTU
26179 && satisfies_constraint_P (x)))
26181 *total = 0;
26182 return true;
26184 else if ((outer_code == PLUS
26185 && reg_or_add_cint_operand (x, VOIDmode))
26186 || (outer_code == MINUS
26187 && reg_or_sub_cint_operand (x, VOIDmode))
26188 || ((outer_code == SET
26189 || outer_code == IOR
26190 || outer_code == XOR)
26191 && (INTVAL (x)
26192 & ~ (unsigned HOST_WIDE_INT) 0xffffffff) == 0))
26194 *total = COSTS_N_INSNS (1);
26195 return true;
26197 /* FALLTHRU */
26199 case CONST_DOUBLE:
26200 case CONST:
26201 case HIGH:
26202 case SYMBOL_REF:
26203 case MEM:
26204 /* When optimizing for size, MEM should be slightly more expensive
26205 than generating address, e.g., (plus (reg) (const)).
26206 L1 cache latency is about two instructions. */
26207 *total = !speed ? COSTS_N_INSNS (1) + 1 : COSTS_N_INSNS (2);
26208 return true;
26210 case LABEL_REF:
26211 *total = 0;
26212 return true;
26214 case PLUS:
26215 case MINUS:
26216 if (FLOAT_MODE_P (mode))
26217 *total = rs6000_cost->fp;
26218 else
26219 *total = COSTS_N_INSNS (1);
26220 return false;
26222 case MULT:
26223 if (GET_CODE (XEXP (x, 1)) == CONST_INT
26224 && satisfies_constraint_I (XEXP (x, 1)))
26226 if (INTVAL (XEXP (x, 1)) >= -256
26227 && INTVAL (XEXP (x, 1)) <= 255)
26228 *total = rs6000_cost->mulsi_const9;
26229 else
26230 *total = rs6000_cost->mulsi_const;
26232 else if (mode == SFmode)
26233 *total = rs6000_cost->fp;
26234 else if (FLOAT_MODE_P (mode))
26235 *total = rs6000_cost->dmul;
26236 else if (mode == DImode)
26237 *total = rs6000_cost->muldi;
26238 else
26239 *total = rs6000_cost->mulsi;
26240 return false;
26242 case FMA:
26243 if (mode == SFmode)
26244 *total = rs6000_cost->fp;
26245 else
26246 *total = rs6000_cost->dmul;
26247 break;
26249 case DIV:
26250 case MOD:
26251 if (FLOAT_MODE_P (mode))
26253 *total = mode == DFmode ? rs6000_cost->ddiv
26254 : rs6000_cost->sdiv;
26255 return false;
26257 /* FALLTHRU */
26259 case UDIV:
26260 case UMOD:
26261 if (GET_CODE (XEXP (x, 1)) == CONST_INT
26262 && exact_log2 (INTVAL (XEXP (x, 1))) >= 0)
26264 if (code == DIV || code == MOD)
26265 /* Shift, addze */
26266 *total = COSTS_N_INSNS (2);
26267 else
26268 /* Shift */
26269 *total = COSTS_N_INSNS (1);
26271 else
26273 if (GET_MODE (XEXP (x, 1)) == DImode)
26274 *total = rs6000_cost->divdi;
26275 else
26276 *total = rs6000_cost->divsi;
26278 /* Add in shift and subtract for MOD. */
26279 if (code == MOD || code == UMOD)
26280 *total += COSTS_N_INSNS (2);
26281 return false;
26283 case CTZ:
26284 case FFS:
26285 *total = COSTS_N_INSNS (4);
26286 return false;
26288 case POPCOUNT:
26289 *total = COSTS_N_INSNS (TARGET_POPCNTD ? 1 : 6);
26290 return false;
26292 case PARITY:
26293 *total = COSTS_N_INSNS (TARGET_CMPB ? 2 : 6);
26294 return false;
26296 case NOT:
26297 if (outer_code == AND || outer_code == IOR || outer_code == XOR)
26299 *total = 0;
26300 return false;
26302 /* FALLTHRU */
26304 case AND:
26305 case CLZ:
26306 case IOR:
26307 case XOR:
26308 case ZERO_EXTRACT:
26309 *total = COSTS_N_INSNS (1);
26310 return false;
26312 case ASHIFT:
26313 case ASHIFTRT:
26314 case LSHIFTRT:
26315 case ROTATE:
26316 case ROTATERT:
26317 /* Handle mul_highpart. */
26318 if (outer_code == TRUNCATE
26319 && GET_CODE (XEXP (x, 0)) == MULT)
26321 if (mode == DImode)
26322 *total = rs6000_cost->muldi;
26323 else
26324 *total = rs6000_cost->mulsi;
26325 return true;
26327 else if (outer_code == AND)
26328 *total = 0;
26329 else
26330 *total = COSTS_N_INSNS (1);
26331 return false;
26333 case SIGN_EXTEND:
26334 case ZERO_EXTEND:
26335 if (GET_CODE (XEXP (x, 0)) == MEM)
26336 *total = 0;
26337 else
26338 *total = COSTS_N_INSNS (1);
26339 return false;
26341 case COMPARE:
26342 case NEG:
26343 case ABS:
26344 if (!FLOAT_MODE_P (mode))
26346 *total = COSTS_N_INSNS (1);
26347 return false;
26349 /* FALLTHRU */
26351 case FLOAT:
26352 case UNSIGNED_FLOAT:
26353 case FIX:
26354 case UNSIGNED_FIX:
26355 case FLOAT_TRUNCATE:
26356 *total = rs6000_cost->fp;
26357 return false;
26359 case FLOAT_EXTEND:
26360 if (mode == DFmode)
26361 *total = 0;
26362 else
26363 *total = rs6000_cost->fp;
26364 return false;
26366 case UNSPEC:
26367 switch (XINT (x, 1))
26369 case UNSPEC_FRSP:
26370 *total = rs6000_cost->fp;
26371 return true;
26373 default:
26374 break;
26376 break;
26378 case CALL:
26379 case IF_THEN_ELSE:
26380 if (!speed)
26382 *total = COSTS_N_INSNS (1);
26383 return true;
26385 else if (FLOAT_MODE_P (mode)
26386 && TARGET_PPC_GFXOPT && TARGET_HARD_FLOAT && TARGET_FPRS)
26388 *total = rs6000_cost->fp;
26389 return false;
26391 break;
26393 case EQ:
26394 case GTU:
26395 case LTU:
26396 /* Carry bit requires mode == Pmode.
26397 NEG or PLUS already counted so only add one. */
26398 if (mode == Pmode
26399 && (outer_code == NEG || outer_code == PLUS))
26401 *total = COSTS_N_INSNS (1);
26402 return true;
26404 if (outer_code == SET)
26406 if (XEXP (x, 1) == const0_rtx)
26408 if (TARGET_ISEL && !TARGET_MFCRF)
26409 *total = COSTS_N_INSNS (8);
26410 else
26411 *total = COSTS_N_INSNS (2);
26412 return true;
26414 else if (mode == Pmode)
26416 *total = COSTS_N_INSNS (3);
26417 return false;
26420 /* FALLTHRU */
26422 case GT:
26423 case LT:
26424 case UNORDERED:
26425 if (outer_code == SET && (XEXP (x, 1) == const0_rtx))
26427 if (TARGET_ISEL && !TARGET_MFCRF)
26428 *total = COSTS_N_INSNS (8);
26429 else
26430 *total = COSTS_N_INSNS (2);
26431 return true;
26433 /* CC COMPARE. */
26434 if (outer_code == COMPARE)
26436 *total = 0;
26437 return true;
26439 break;
26441 default:
26442 break;
26445 return false;
26448 /* Debug form of r6000_rtx_costs that is selected if -mdebug=cost. */
26450 static bool
26451 rs6000_debug_rtx_costs (rtx x, int code, int outer_code, int opno, int *total,
26452 bool speed)
26454 bool ret = rs6000_rtx_costs (x, code, outer_code, opno, total, speed);
26456 fprintf (stderr,
26457 "\nrs6000_rtx_costs, return = %s, code = %s, outer_code = %s, "
26458 "opno = %d, total = %d, speed = %s, x:\n",
26459 ret ? "complete" : "scan inner",
26460 GET_RTX_NAME (code),
26461 GET_RTX_NAME (outer_code),
26462 opno,
26463 *total,
26464 speed ? "true" : "false");
26466 debug_rtx (x);
26468 return ret;
26471 /* Debug form of ADDRESS_COST that is selected if -mdebug=cost. */
26473 static int
26474 rs6000_debug_address_cost (rtx x, enum machine_mode mode,
26475 addr_space_t as, bool speed)
26477 int ret = TARGET_ADDRESS_COST (x, mode, as, speed);
26479 fprintf (stderr, "\nrs6000_address_cost, return = %d, speed = %s, x:\n",
26480 ret, speed ? "true" : "false");
26481 debug_rtx (x);
26483 return ret;
26487 /* A C expression returning the cost of moving data from a register of class
26488 CLASS1 to one of CLASS2. */
26490 static int
26491 rs6000_register_move_cost (enum machine_mode mode,
26492 reg_class_t from, reg_class_t to)
26494 int ret;
26496 if (TARGET_DEBUG_COST)
26497 dbg_cost_ctrl++;
26499 /* Moves from/to GENERAL_REGS. */
26500 if (reg_classes_intersect_p (to, GENERAL_REGS)
26501 || reg_classes_intersect_p (from, GENERAL_REGS))
26503 reg_class_t rclass = from;
26505 if (! reg_classes_intersect_p (to, GENERAL_REGS))
26506 rclass = to;
26508 if (rclass == FLOAT_REGS || rclass == ALTIVEC_REGS || rclass == VSX_REGS)
26509 ret = (rs6000_memory_move_cost (mode, rclass, false)
26510 + rs6000_memory_move_cost (mode, GENERAL_REGS, false));
26512 /* It's more expensive to move CR_REGS than CR0_REGS because of the
26513 shift. */
26514 else if (rclass == CR_REGS)
26515 ret = 4;
26517 /* For those processors that have slow LR/CTR moves, make them more
26518 expensive than memory in order to bias spills to memory .*/
26519 else if ((rs6000_cpu == PROCESSOR_POWER6
26520 || rs6000_cpu == PROCESSOR_POWER7)
26521 && reg_classes_intersect_p (rclass, LINK_OR_CTR_REGS))
26522 ret = 6 * hard_regno_nregs[0][mode];
26524 else
26525 /* A move will cost one instruction per GPR moved. */
26526 ret = 2 * hard_regno_nregs[0][mode];
26529 /* If we have VSX, we can easily move between FPR or Altivec registers. */
26530 else if (VECTOR_MEM_VSX_P (mode)
26531 && reg_classes_intersect_p (to, VSX_REGS)
26532 && reg_classes_intersect_p (from, VSX_REGS))
26533 ret = 2 * hard_regno_nregs[32][mode];
26535 /* Moving between two similar registers is just one instruction. */
26536 else if (reg_classes_intersect_p (to, from))
26537 ret = (mode == TFmode || mode == TDmode) ? 4 : 2;
26539 /* Everything else has to go through GENERAL_REGS. */
26540 else
26541 ret = (rs6000_register_move_cost (mode, GENERAL_REGS, to)
26542 + rs6000_register_move_cost (mode, from, GENERAL_REGS));
26544 if (TARGET_DEBUG_COST)
26546 if (dbg_cost_ctrl == 1)
26547 fprintf (stderr,
26548 "rs6000_register_move_cost:, ret=%d, mode=%s, from=%s, to=%s\n",
26549 ret, GET_MODE_NAME (mode), reg_class_names[from],
26550 reg_class_names[to]);
26551 dbg_cost_ctrl--;
26554 return ret;
26557 /* A C expressions returning the cost of moving data of MODE from a register to
26558 or from memory. */
26560 static int
26561 rs6000_memory_move_cost (enum machine_mode mode, reg_class_t rclass,
26562 bool in ATTRIBUTE_UNUSED)
26564 int ret;
26566 if (TARGET_DEBUG_COST)
26567 dbg_cost_ctrl++;
26569 if (reg_classes_intersect_p (rclass, GENERAL_REGS))
26570 ret = 4 * hard_regno_nregs[0][mode];
26571 else if ((reg_classes_intersect_p (rclass, FLOAT_REGS)
26572 || reg_classes_intersect_p (rclass, VSX_REGS)))
26573 ret = 4 * hard_regno_nregs[32][mode];
26574 else if (reg_classes_intersect_p (rclass, ALTIVEC_REGS))
26575 ret = 4 * hard_regno_nregs[FIRST_ALTIVEC_REGNO][mode];
26576 else
26577 ret = 4 + rs6000_register_move_cost (mode, rclass, GENERAL_REGS);
26579 if (TARGET_DEBUG_COST)
26581 if (dbg_cost_ctrl == 1)
26582 fprintf (stderr,
26583 "rs6000_memory_move_cost: ret=%d, mode=%s, rclass=%s, in=%d\n",
26584 ret, GET_MODE_NAME (mode), reg_class_names[rclass], in);
26585 dbg_cost_ctrl--;
26588 return ret;
26591 /* Returns a code for a target-specific builtin that implements
26592 reciprocal of the function, or NULL_TREE if not available. */
26594 static tree
26595 rs6000_builtin_reciprocal (unsigned int fn, bool md_fn,
26596 bool sqrt ATTRIBUTE_UNUSED)
26598 if (optimize_insn_for_size_p ())
26599 return NULL_TREE;
26601 if (md_fn)
26602 switch (fn)
26604 case VSX_BUILTIN_XVSQRTDP:
26605 if (!RS6000_RECIP_AUTO_RSQRTE_P (V2DFmode))
26606 return NULL_TREE;
26608 return rs6000_builtin_decls[VSX_BUILTIN_RSQRT_2DF];
26610 case VSX_BUILTIN_XVSQRTSP:
26611 if (!RS6000_RECIP_AUTO_RSQRTE_P (V4SFmode))
26612 return NULL_TREE;
26614 return rs6000_builtin_decls[VSX_BUILTIN_RSQRT_4SF];
26616 default:
26617 return NULL_TREE;
26620 else
26621 switch (fn)
26623 case BUILT_IN_SQRT:
26624 if (!RS6000_RECIP_AUTO_RSQRTE_P (DFmode))
26625 return NULL_TREE;
26627 return rs6000_builtin_decls[RS6000_BUILTIN_RSQRT];
26629 case BUILT_IN_SQRTF:
26630 if (!RS6000_RECIP_AUTO_RSQRTE_P (SFmode))
26631 return NULL_TREE;
26633 return rs6000_builtin_decls[RS6000_BUILTIN_RSQRTF];
26635 default:
26636 return NULL_TREE;
26640 /* Load up a constant. If the mode is a vector mode, splat the value across
26641 all of the vector elements. */
26643 static rtx
26644 rs6000_load_constant_and_splat (enum machine_mode mode, REAL_VALUE_TYPE dconst)
26646 rtx reg;
26648 if (mode == SFmode || mode == DFmode)
26650 rtx d = CONST_DOUBLE_FROM_REAL_VALUE (dconst, mode);
26651 reg = force_reg (mode, d);
26653 else if (mode == V4SFmode)
26655 rtx d = CONST_DOUBLE_FROM_REAL_VALUE (dconst, SFmode);
26656 rtvec v = gen_rtvec (4, d, d, d, d);
26657 reg = gen_reg_rtx (mode);
26658 rs6000_expand_vector_init (reg, gen_rtx_PARALLEL (mode, v));
26660 else if (mode == V2DFmode)
26662 rtx d = CONST_DOUBLE_FROM_REAL_VALUE (dconst, DFmode);
26663 rtvec v = gen_rtvec (2, d, d);
26664 reg = gen_reg_rtx (mode);
26665 rs6000_expand_vector_init (reg, gen_rtx_PARALLEL (mode, v));
26667 else
26668 gcc_unreachable ();
26670 return reg;
26673 /* Generate an FMA instruction. */
26675 static void
26676 rs6000_emit_madd (rtx target, rtx m1, rtx m2, rtx a)
26678 enum machine_mode mode = GET_MODE (target);
26679 rtx dst;
26681 dst = expand_ternary_op (mode, fma_optab, m1, m2, a, target, 0);
26682 gcc_assert (dst != NULL);
26684 if (dst != target)
26685 emit_move_insn (target, dst);
26688 /* Generate a FMSUB instruction: dst = fma(m1, m2, -a). */
26690 static void
26691 rs6000_emit_msub (rtx target, rtx m1, rtx m2, rtx a)
26693 enum machine_mode mode = GET_MODE (target);
26694 rtx dst;
26696 /* Altivec does not support fms directly;
26697 generate in terms of fma in that case. */
26698 if (optab_handler (fms_optab, mode) != CODE_FOR_nothing)
26699 dst = expand_ternary_op (mode, fms_optab, m1, m2, a, target, 0);
26700 else
26702 a = expand_unop (mode, neg_optab, a, NULL_RTX, 0);
26703 dst = expand_ternary_op (mode, fma_optab, m1, m2, a, target, 0);
26705 gcc_assert (dst != NULL);
26707 if (dst != target)
26708 emit_move_insn (target, dst);
26711 /* Generate a FNMSUB instruction: dst = -fma(m1, m2, -a). */
26713 static void
26714 rs6000_emit_nmsub (rtx dst, rtx m1, rtx m2, rtx a)
26716 enum machine_mode mode = GET_MODE (dst);
26717 rtx r;
26719 /* This is a tad more complicated, since the fnma_optab is for
26720 a different expression: fma(-m1, m2, a), which is the same
26721 thing except in the case of signed zeros.
26723 Fortunately we know that if FMA is supported that FNMSUB is
26724 also supported in the ISA. Just expand it directly. */
26726 gcc_assert (optab_handler (fma_optab, mode) != CODE_FOR_nothing);
26728 r = gen_rtx_NEG (mode, a);
26729 r = gen_rtx_FMA (mode, m1, m2, r);
26730 r = gen_rtx_NEG (mode, r);
26731 emit_insn (gen_rtx_SET (VOIDmode, dst, r));
26734 /* Newton-Raphson approximation of floating point divide DST = N/D. If NOTE_P,
26735 add a reg_note saying that this was a division. Support both scalar and
26736 vector divide. Assumes no trapping math and finite arguments. */
26738 void
26739 rs6000_emit_swdiv (rtx dst, rtx n, rtx d, bool note_p)
26741 enum machine_mode mode = GET_MODE (dst);
26742 rtx one, x0, e0, x1, xprev, eprev, xnext, enext, u, v;
26743 int i;
26745 /* Low precision estimates guarantee 5 bits of accuracy. High
26746 precision estimates guarantee 14 bits of accuracy. SFmode
26747 requires 23 bits of accuracy. DFmode requires 52 bits of
26748 accuracy. Each pass at least doubles the accuracy, leading
26749 to the following. */
26750 int passes = (TARGET_RECIP_PRECISION) ? 1 : 3;
26751 if (mode == DFmode || mode == V2DFmode)
26752 passes++;
26754 enum insn_code code = optab_handler (smul_optab, mode);
26755 gen_2arg_fn_t gen_mul = (gen_2arg_fn_t) GEN_FCN (code);
26757 gcc_assert (code != CODE_FOR_nothing);
26759 one = rs6000_load_constant_and_splat (mode, dconst1);
26761 /* x0 = 1./d estimate */
26762 x0 = gen_reg_rtx (mode);
26763 emit_insn (gen_rtx_SET (VOIDmode, x0,
26764 gen_rtx_UNSPEC (mode, gen_rtvec (1, d),
26765 UNSPEC_FRES)));
26767 /* Each iteration but the last calculates x_(i+1) = x_i * (2 - d * x_i). */
26768 if (passes > 1) {
26770 /* e0 = 1. - d * x0 */
26771 e0 = gen_reg_rtx (mode);
26772 rs6000_emit_nmsub (e0, d, x0, one);
26774 /* x1 = x0 + e0 * x0 */
26775 x1 = gen_reg_rtx (mode);
26776 rs6000_emit_madd (x1, e0, x0, x0);
26778 for (i = 0, xprev = x1, eprev = e0; i < passes - 2;
26779 ++i, xprev = xnext, eprev = enext) {
26781 /* enext = eprev * eprev */
26782 enext = gen_reg_rtx (mode);
26783 emit_insn (gen_mul (enext, eprev, eprev));
26785 /* xnext = xprev + enext * xprev */
26786 xnext = gen_reg_rtx (mode);
26787 rs6000_emit_madd (xnext, enext, xprev, xprev);
26790 } else
26791 xprev = x0;
26793 /* The last iteration calculates x_(i+1) = n * x_i * (2 - d * x_i). */
26795 /* u = n * xprev */
26796 u = gen_reg_rtx (mode);
26797 emit_insn (gen_mul (u, n, xprev));
26799 /* v = n - (d * u) */
26800 v = gen_reg_rtx (mode);
26801 rs6000_emit_nmsub (v, d, u, n);
26803 /* dst = (v * xprev) + u */
26804 rs6000_emit_madd (dst, v, xprev, u);
26806 if (note_p)
26807 add_reg_note (get_last_insn (), REG_EQUAL, gen_rtx_DIV (mode, n, d));
26810 /* Newton-Raphson approximation of single/double-precision floating point
26811 rsqrt. Assumes no trapping math and finite arguments. */
26813 void
26814 rs6000_emit_swrsqrt (rtx dst, rtx src)
26816 enum machine_mode mode = GET_MODE (src);
26817 rtx x0 = gen_reg_rtx (mode);
26818 rtx y = gen_reg_rtx (mode);
26820 /* Low precision estimates guarantee 5 bits of accuracy. High
26821 precision estimates guarantee 14 bits of accuracy. SFmode
26822 requires 23 bits of accuracy. DFmode requires 52 bits of
26823 accuracy. Each pass at least doubles the accuracy, leading
26824 to the following. */
26825 int passes = (TARGET_RECIP_PRECISION) ? 1 : 3;
26826 if (mode == DFmode || mode == V2DFmode)
26827 passes++;
26829 REAL_VALUE_TYPE dconst3_2;
26830 int i;
26831 rtx halfthree;
26832 enum insn_code code = optab_handler (smul_optab, mode);
26833 gen_2arg_fn_t gen_mul = (gen_2arg_fn_t) GEN_FCN (code);
26835 gcc_assert (code != CODE_FOR_nothing);
26837 /* Load up the constant 1.5 either as a scalar, or as a vector. */
26838 real_from_integer (&dconst3_2, VOIDmode, 3, 0, 0);
26839 SET_REAL_EXP (&dconst3_2, REAL_EXP (&dconst3_2) - 1);
26841 halfthree = rs6000_load_constant_and_splat (mode, dconst3_2);
26843 /* x0 = rsqrt estimate */
26844 emit_insn (gen_rtx_SET (VOIDmode, x0,
26845 gen_rtx_UNSPEC (mode, gen_rtvec (1, src),
26846 UNSPEC_RSQRT)));
26848 /* y = 0.5 * src = 1.5 * src - src -> fewer constants */
26849 rs6000_emit_msub (y, src, halfthree, src);
26851 for (i = 0; i < passes; i++)
26853 rtx x1 = gen_reg_rtx (mode);
26854 rtx u = gen_reg_rtx (mode);
26855 rtx v = gen_reg_rtx (mode);
26857 /* x1 = x0 * (1.5 - y * (x0 * x0)) */
26858 emit_insn (gen_mul (u, x0, x0));
26859 rs6000_emit_nmsub (v, y, u, halfthree);
26860 emit_insn (gen_mul (x1, x0, v));
26861 x0 = x1;
26864 emit_move_insn (dst, x0);
26865 return;
26868 /* Emit popcount intrinsic on TARGET_POPCNTB (Power5) and TARGET_POPCNTD
26869 (Power7) targets. DST is the target, and SRC is the argument operand. */
26871 void
26872 rs6000_emit_popcount (rtx dst, rtx src)
26874 enum machine_mode mode = GET_MODE (dst);
26875 rtx tmp1, tmp2;
26877 /* Use the PPC ISA 2.06 popcnt{w,d} instruction if we can. */
26878 if (TARGET_POPCNTD)
26880 if (mode == SImode)
26881 emit_insn (gen_popcntdsi2 (dst, src));
26882 else
26883 emit_insn (gen_popcntddi2 (dst, src));
26884 return;
26887 tmp1 = gen_reg_rtx (mode);
26889 if (mode == SImode)
26891 emit_insn (gen_popcntbsi2 (tmp1, src));
26892 tmp2 = expand_mult (SImode, tmp1, GEN_INT (0x01010101),
26893 NULL_RTX, 0);
26894 tmp2 = force_reg (SImode, tmp2);
26895 emit_insn (gen_lshrsi3 (dst, tmp2, GEN_INT (24)));
26897 else
26899 emit_insn (gen_popcntbdi2 (tmp1, src));
26900 tmp2 = expand_mult (DImode, tmp1,
26901 GEN_INT ((HOST_WIDE_INT)
26902 0x01010101 << 32 | 0x01010101),
26903 NULL_RTX, 0);
26904 tmp2 = force_reg (DImode, tmp2);
26905 emit_insn (gen_lshrdi3 (dst, tmp2, GEN_INT (56)));
26910 /* Emit parity intrinsic on TARGET_POPCNTB targets. DST is the
26911 target, and SRC is the argument operand. */
26913 void
26914 rs6000_emit_parity (rtx dst, rtx src)
26916 enum machine_mode mode = GET_MODE (dst);
26917 rtx tmp;
26919 tmp = gen_reg_rtx (mode);
26921 /* Use the PPC ISA 2.05 prtyw/prtyd instruction if we can. */
26922 if (TARGET_CMPB)
26924 if (mode == SImode)
26926 emit_insn (gen_popcntbsi2 (tmp, src));
26927 emit_insn (gen_paritysi2_cmpb (dst, tmp));
26929 else
26931 emit_insn (gen_popcntbdi2 (tmp, src));
26932 emit_insn (gen_paritydi2_cmpb (dst, tmp));
26934 return;
26937 if (mode == SImode)
26939 /* Is mult+shift >= shift+xor+shift+xor? */
26940 if (rs6000_cost->mulsi_const >= COSTS_N_INSNS (3))
26942 rtx tmp1, tmp2, tmp3, tmp4;
26944 tmp1 = gen_reg_rtx (SImode);
26945 emit_insn (gen_popcntbsi2 (tmp1, src));
26947 tmp2 = gen_reg_rtx (SImode);
26948 emit_insn (gen_lshrsi3 (tmp2, tmp1, GEN_INT (16)));
26949 tmp3 = gen_reg_rtx (SImode);
26950 emit_insn (gen_xorsi3 (tmp3, tmp1, tmp2));
26952 tmp4 = gen_reg_rtx (SImode);
26953 emit_insn (gen_lshrsi3 (tmp4, tmp3, GEN_INT (8)));
26954 emit_insn (gen_xorsi3 (tmp, tmp3, tmp4));
26956 else
26957 rs6000_emit_popcount (tmp, src);
26958 emit_insn (gen_andsi3 (dst, tmp, const1_rtx));
26960 else
26962 /* Is mult+shift >= shift+xor+shift+xor+shift+xor? */
26963 if (rs6000_cost->muldi >= COSTS_N_INSNS (5))
26965 rtx tmp1, tmp2, tmp3, tmp4, tmp5, tmp6;
26967 tmp1 = gen_reg_rtx (DImode);
26968 emit_insn (gen_popcntbdi2 (tmp1, src));
26970 tmp2 = gen_reg_rtx (DImode);
26971 emit_insn (gen_lshrdi3 (tmp2, tmp1, GEN_INT (32)));
26972 tmp3 = gen_reg_rtx (DImode);
26973 emit_insn (gen_xordi3 (tmp3, tmp1, tmp2));
26975 tmp4 = gen_reg_rtx (DImode);
26976 emit_insn (gen_lshrdi3 (tmp4, tmp3, GEN_INT (16)));
26977 tmp5 = gen_reg_rtx (DImode);
26978 emit_insn (gen_xordi3 (tmp5, tmp3, tmp4));
26980 tmp6 = gen_reg_rtx (DImode);
26981 emit_insn (gen_lshrdi3 (tmp6, tmp5, GEN_INT (8)));
26982 emit_insn (gen_xordi3 (tmp, tmp5, tmp6));
26984 else
26985 rs6000_emit_popcount (tmp, src);
26986 emit_insn (gen_anddi3 (dst, tmp, const1_rtx));
26990 /* Expand an Altivec constant permutation. Return true if we match
26991 an efficient implementation; false to fall back to VPERM. */
26993 bool
26994 altivec_expand_vec_perm_const (rtx operands[4])
26996 struct altivec_perm_insn {
26997 enum insn_code impl;
26998 unsigned char perm[16];
27000 static const struct altivec_perm_insn patterns[] = {
27001 { CODE_FOR_altivec_vpkuhum,
27002 { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31 } },
27003 { CODE_FOR_altivec_vpkuwum,
27004 { 2, 3, 6, 7, 10, 11, 14, 15, 18, 19, 22, 23, 26, 27, 30, 31 } },
27005 { CODE_FOR_altivec_vmrghb,
27006 { 0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23 } },
27007 { CODE_FOR_altivec_vmrghh,
27008 { 0, 1, 16, 17, 2, 3, 18, 19, 4, 5, 20, 21, 6, 7, 22, 23 } },
27009 { CODE_FOR_altivec_vmrghw,
27010 { 0, 1, 2, 3, 16, 17, 18, 19, 4, 5, 6, 7, 20, 21, 22, 23 } },
27011 { CODE_FOR_altivec_vmrglb,
27012 { 8, 24, 9, 25, 10, 26, 11, 27, 12, 28, 13, 29, 14, 30, 15, 31 } },
27013 { CODE_FOR_altivec_vmrglh,
27014 { 8, 9, 24, 25, 10, 11, 26, 27, 12, 13, 28, 29, 14, 15, 30, 31 } },
27015 { CODE_FOR_altivec_vmrglw,
27016 { 8, 9, 10, 11, 24, 25, 26, 27, 12, 13, 14, 15, 28, 29, 30, 31 } }
27019 unsigned int i, j, elt, which;
27020 unsigned char perm[16];
27021 rtx target, op0, op1, sel, x;
27022 bool one_vec;
27024 target = operands[0];
27025 op0 = operands[1];
27026 op1 = operands[2];
27027 sel = operands[3];
27029 /* Unpack the constant selector. */
27030 for (i = which = 0; i < 16; ++i)
27032 rtx e = XVECEXP (sel, 0, i);
27033 elt = INTVAL (e) & 31;
27034 which |= (elt < 16 ? 1 : 2);
27035 perm[i] = elt;
27038 /* Simplify the constant selector based on operands. */
27039 switch (which)
27041 default:
27042 gcc_unreachable ();
27044 case 3:
27045 one_vec = false;
27046 if (!rtx_equal_p (op0, op1))
27047 break;
27048 /* FALLTHRU */
27050 case 2:
27051 for (i = 0; i < 16; ++i)
27052 perm[i] &= 15;
27053 op0 = op1;
27054 one_vec = true;
27055 break;
27057 case 1:
27058 op1 = op0;
27059 one_vec = true;
27060 break;
27063 /* Look for splat patterns. */
27064 if (one_vec)
27066 elt = perm[0];
27068 for (i = 0; i < 16; ++i)
27069 if (perm[i] != elt)
27070 break;
27071 if (i == 16)
27073 emit_insn (gen_altivec_vspltb (target, op0, GEN_INT (elt)));
27074 return true;
27077 if (elt % 2 == 0)
27079 for (i = 0; i < 16; i += 2)
27080 if (perm[i] != elt || perm[i + 1] != elt + 1)
27081 break;
27082 if (i == 16)
27084 x = gen_reg_rtx (V8HImode);
27085 emit_insn (gen_altivec_vsplth (x, gen_lowpart (V8HImode, op0),
27086 GEN_INT (elt / 2)));
27087 emit_move_insn (target, gen_lowpart (V16QImode, x));
27088 return true;
27092 if (elt % 4 == 0)
27094 for (i = 0; i < 16; i += 4)
27095 if (perm[i] != elt
27096 || perm[i + 1] != elt + 1
27097 || perm[i + 2] != elt + 2
27098 || perm[i + 3] != elt + 3)
27099 break;
27100 if (i == 16)
27102 x = gen_reg_rtx (V4SImode);
27103 emit_insn (gen_altivec_vspltw (x, gen_lowpart (V4SImode, op0),
27104 GEN_INT (elt / 4)));
27105 emit_move_insn (target, gen_lowpart (V16QImode, x));
27106 return true;
27111 /* Look for merge and pack patterns. */
27112 for (j = 0; j < ARRAY_SIZE (patterns); ++j)
27114 bool swapped;
27116 elt = patterns[j].perm[0];
27117 if (perm[0] == elt)
27118 swapped = false;
27119 else if (perm[0] == elt + 16)
27120 swapped = true;
27121 else
27122 continue;
27123 for (i = 1; i < 16; ++i)
27125 elt = patterns[j].perm[i];
27126 if (swapped)
27127 elt = (elt >= 16 ? elt - 16 : elt + 16);
27128 else if (one_vec && elt >= 16)
27129 elt -= 16;
27130 if (perm[i] != elt)
27131 break;
27133 if (i == 16)
27135 enum insn_code icode = patterns[j].impl;
27136 enum machine_mode omode = insn_data[icode].operand[0].mode;
27137 enum machine_mode imode = insn_data[icode].operand[1].mode;
27139 if (swapped)
27140 x = op0, op0 = op1, op1 = x;
27141 if (imode != V16QImode)
27143 op0 = gen_lowpart (imode, op0);
27144 op1 = gen_lowpart (imode, op1);
27146 if (omode == V16QImode)
27147 x = target;
27148 else
27149 x = gen_reg_rtx (omode);
27150 emit_insn (GEN_FCN (icode) (x, op0, op1));
27151 if (omode != V16QImode)
27152 emit_move_insn (target, gen_lowpart (V16QImode, x));
27153 return true;
27157 return false;
27160 /* Expand a Paired Single, VSX Permute Doubleword, or SPE constant permutation.
27161 Return true if we match an efficient implementation. */
27163 static bool
27164 rs6000_expand_vec_perm_const_1 (rtx target, rtx op0, rtx op1,
27165 unsigned char perm0, unsigned char perm1)
27167 rtx x;
27169 /* If both selectors come from the same operand, fold to single op. */
27170 if ((perm0 & 2) == (perm1 & 2))
27172 if (perm0 & 2)
27173 op0 = op1;
27174 else
27175 op1 = op0;
27177 /* If both operands are equal, fold to simpler permutation. */
27178 if (rtx_equal_p (op0, op1))
27180 perm0 = perm0 & 1;
27181 perm1 = (perm1 & 1) + 2;
27183 /* If the first selector comes from the second operand, swap. */
27184 else if (perm0 & 2)
27186 if (perm1 & 2)
27187 return false;
27188 perm0 -= 2;
27189 perm1 += 2;
27190 x = op0, op0 = op1, op1 = x;
27192 /* If the second selector does not come from the second operand, fail. */
27193 else if ((perm1 & 2) == 0)
27194 return false;
27196 /* Success! */
27197 if (target != NULL)
27199 enum machine_mode vmode, dmode;
27200 rtvec v;
27202 vmode = GET_MODE (target);
27203 gcc_assert (GET_MODE_NUNITS (vmode) == 2);
27204 dmode = mode_for_vector (GET_MODE_INNER (vmode), 4);
27206 x = gen_rtx_VEC_CONCAT (dmode, op0, op1);
27207 v = gen_rtvec (2, GEN_INT (perm0), GEN_INT (perm1));
27208 x = gen_rtx_VEC_SELECT (vmode, x, gen_rtx_PARALLEL (VOIDmode, v));
27209 emit_insn (gen_rtx_SET (VOIDmode, target, x));
27211 return true;
27214 bool
27215 rs6000_expand_vec_perm_const (rtx operands[4])
27217 rtx target, op0, op1, sel;
27218 unsigned char perm0, perm1;
27220 target = operands[0];
27221 op0 = operands[1];
27222 op1 = operands[2];
27223 sel = operands[3];
27225 /* Unpack the constant selector. */
27226 perm0 = INTVAL (XVECEXP (sel, 0, 0)) & 3;
27227 perm1 = INTVAL (XVECEXP (sel, 0, 1)) & 3;
27229 return rs6000_expand_vec_perm_const_1 (target, op0, op1, perm0, perm1);
27232 /* Test whether a constant permutation is supported. */
27234 static bool
27235 rs6000_vectorize_vec_perm_const_ok (enum machine_mode vmode,
27236 const unsigned char *sel)
27238 /* AltiVec (and thus VSX) can handle arbitrary permutations. */
27239 if (TARGET_ALTIVEC)
27240 return true;
27242 /* Check for ps_merge* or evmerge* insns. */
27243 if ((TARGET_PAIRED_FLOAT && vmode == V2SFmode)
27244 || (TARGET_SPE && vmode == V2SImode))
27246 rtx op0 = gen_raw_REG (vmode, LAST_VIRTUAL_REGISTER + 1);
27247 rtx op1 = gen_raw_REG (vmode, LAST_VIRTUAL_REGISTER + 2);
27248 return rs6000_expand_vec_perm_const_1 (NULL, op0, op1, sel[0], sel[1]);
27251 return false;
27254 /* A subroutine for rs6000_expand_extract_even & rs6000_expand_interleave. */
27256 static void
27257 rs6000_do_expand_vec_perm (rtx target, rtx op0, rtx op1,
27258 enum machine_mode vmode, unsigned nelt, rtx perm[])
27260 enum machine_mode imode;
27261 rtx x;
27263 imode = vmode;
27264 if (GET_MODE_CLASS (vmode) != MODE_VECTOR_INT)
27266 imode = GET_MODE_INNER (vmode);
27267 imode = mode_for_size (GET_MODE_BITSIZE (imode), MODE_INT, 0);
27268 imode = mode_for_vector (imode, nelt);
27271 x = gen_rtx_CONST_VECTOR (imode, gen_rtvec_v (nelt, perm));
27272 x = expand_vec_perm (vmode, op0, op1, x, target);
27273 if (x != target)
27274 emit_move_insn (target, x);
27277 /* Expand an extract even operation. */
27279 void
27280 rs6000_expand_extract_even (rtx target, rtx op0, rtx op1)
27282 enum machine_mode vmode = GET_MODE (target);
27283 unsigned i, nelt = GET_MODE_NUNITS (vmode);
27284 rtx perm[16];
27286 for (i = 0; i < nelt; i++)
27287 perm[i] = GEN_INT (i * 2);
27289 rs6000_do_expand_vec_perm (target, op0, op1, vmode, nelt, perm);
27292 /* Expand a vector interleave operation. */
27294 void
27295 rs6000_expand_interleave (rtx target, rtx op0, rtx op1, bool highp)
27297 enum machine_mode vmode = GET_MODE (target);
27298 unsigned i, high, nelt = GET_MODE_NUNITS (vmode);
27299 rtx perm[16];
27301 high = (highp == BYTES_BIG_ENDIAN ? 0 : nelt / 2);
27302 for (i = 0; i < nelt / 2; i++)
27304 perm[i * 2] = GEN_INT (i + high);
27305 perm[i * 2 + 1] = GEN_INT (i + nelt + high);
27308 rs6000_do_expand_vec_perm (target, op0, op1, vmode, nelt, perm);
27311 /* Return an RTX representing where to find the function value of a
27312 function returning MODE. */
27313 static rtx
27314 rs6000_complex_function_value (enum machine_mode mode)
27316 unsigned int regno;
27317 rtx r1, r2;
27318 enum machine_mode inner = GET_MODE_INNER (mode);
27319 unsigned int inner_bytes = GET_MODE_SIZE (inner);
27321 if (FLOAT_MODE_P (mode) && TARGET_HARD_FLOAT && TARGET_FPRS)
27322 regno = FP_ARG_RETURN;
27323 else
27325 regno = GP_ARG_RETURN;
27327 /* 32-bit is OK since it'll go in r3/r4. */
27328 if (TARGET_32BIT && inner_bytes >= 4)
27329 return gen_rtx_REG (mode, regno);
27332 if (inner_bytes >= 8)
27333 return gen_rtx_REG (mode, regno);
27335 r1 = gen_rtx_EXPR_LIST (inner, gen_rtx_REG (inner, regno),
27336 const0_rtx);
27337 r2 = gen_rtx_EXPR_LIST (inner, gen_rtx_REG (inner, regno + 1),
27338 GEN_INT (inner_bytes));
27339 return gen_rtx_PARALLEL (mode, gen_rtvec (2, r1, r2));
27342 /* Target hook for TARGET_FUNCTION_VALUE.
27344 On the SPE, both FPs and vectors are returned in r3.
27346 On RS/6000 an integer value is in r3 and a floating-point value is in
27347 fp1, unless -msoft-float. */
27349 static rtx
27350 rs6000_function_value (const_tree valtype,
27351 const_tree fn_decl_or_type ATTRIBUTE_UNUSED,
27352 bool outgoing ATTRIBUTE_UNUSED)
27354 enum machine_mode mode;
27355 unsigned int regno;
27357 /* Special handling for structs in darwin64. */
27358 if (TARGET_MACHO
27359 && rs6000_darwin64_struct_check_p (TYPE_MODE (valtype), valtype))
27361 CUMULATIVE_ARGS valcum;
27362 rtx valret;
27364 valcum.words = 0;
27365 valcum.fregno = FP_ARG_MIN_REG;
27366 valcum.vregno = ALTIVEC_ARG_MIN_REG;
27367 /* Do a trial code generation as if this were going to be passed as
27368 an argument; if any part goes in memory, we return NULL. */
27369 valret = rs6000_darwin64_record_arg (&valcum, valtype, true, /* retval= */ true);
27370 if (valret)
27371 return valret;
27372 /* Otherwise fall through to standard ABI rules. */
27375 if (TARGET_32BIT && TARGET_POWERPC64 && TYPE_MODE (valtype) == DImode)
27377 /* Long long return value need be split in -mpowerpc64, 32bit ABI. */
27378 return gen_rtx_PARALLEL (DImode,
27379 gen_rtvec (2,
27380 gen_rtx_EXPR_LIST (VOIDmode,
27381 gen_rtx_REG (SImode, GP_ARG_RETURN),
27382 const0_rtx),
27383 gen_rtx_EXPR_LIST (VOIDmode,
27384 gen_rtx_REG (SImode,
27385 GP_ARG_RETURN + 1),
27386 GEN_INT (4))));
27388 if (TARGET_32BIT && TARGET_POWERPC64 && TYPE_MODE (valtype) == DCmode)
27390 return gen_rtx_PARALLEL (DCmode,
27391 gen_rtvec (4,
27392 gen_rtx_EXPR_LIST (VOIDmode,
27393 gen_rtx_REG (SImode, GP_ARG_RETURN),
27394 const0_rtx),
27395 gen_rtx_EXPR_LIST (VOIDmode,
27396 gen_rtx_REG (SImode,
27397 GP_ARG_RETURN + 1),
27398 GEN_INT (4)),
27399 gen_rtx_EXPR_LIST (VOIDmode,
27400 gen_rtx_REG (SImode,
27401 GP_ARG_RETURN + 2),
27402 GEN_INT (8)),
27403 gen_rtx_EXPR_LIST (VOIDmode,
27404 gen_rtx_REG (SImode,
27405 GP_ARG_RETURN + 3),
27406 GEN_INT (12))));
27409 mode = TYPE_MODE (valtype);
27410 if ((INTEGRAL_TYPE_P (valtype) && GET_MODE_BITSIZE (mode) < BITS_PER_WORD)
27411 || POINTER_TYPE_P (valtype))
27412 mode = TARGET_32BIT ? SImode : DImode;
27414 if (DECIMAL_FLOAT_MODE_P (mode) && TARGET_HARD_FLOAT && TARGET_FPRS)
27415 /* _Decimal128 must use an even/odd register pair. */
27416 regno = (mode == TDmode) ? FP_ARG_RETURN + 1 : FP_ARG_RETURN;
27417 else if (SCALAR_FLOAT_TYPE_P (valtype) && TARGET_HARD_FLOAT && TARGET_FPRS
27418 && ((TARGET_SINGLE_FLOAT && (mode == SFmode)) || TARGET_DOUBLE_FLOAT))
27419 regno = FP_ARG_RETURN;
27420 else if (TREE_CODE (valtype) == COMPLEX_TYPE
27421 && targetm.calls.split_complex_arg)
27422 return rs6000_complex_function_value (mode);
27423 /* VSX is a superset of Altivec and adds V2DImode/V2DFmode. Since the same
27424 return register is used in both cases, and we won't see V2DImode/V2DFmode
27425 for pure altivec, combine the two cases. */
27426 else if (TREE_CODE (valtype) == VECTOR_TYPE
27427 && TARGET_ALTIVEC && TARGET_ALTIVEC_ABI
27428 && ALTIVEC_OR_VSX_VECTOR_MODE (mode))
27429 regno = ALTIVEC_ARG_RETURN;
27430 else if (TARGET_E500_DOUBLE && TARGET_HARD_FLOAT
27431 && (mode == DFmode || mode == DCmode
27432 || mode == TFmode || mode == TCmode))
27433 return spe_build_register_parallel (mode, GP_ARG_RETURN);
27434 else
27435 regno = GP_ARG_RETURN;
27437 return gen_rtx_REG (mode, regno);
27440 /* Define how to find the value returned by a library function
27441 assuming the value has mode MODE. */
27443 rs6000_libcall_value (enum machine_mode mode)
27445 unsigned int regno;
27447 if (TARGET_32BIT && TARGET_POWERPC64 && mode == DImode)
27449 /* Long long return value need be split in -mpowerpc64, 32bit ABI. */
27450 return gen_rtx_PARALLEL (DImode,
27451 gen_rtvec (2,
27452 gen_rtx_EXPR_LIST (VOIDmode,
27453 gen_rtx_REG (SImode, GP_ARG_RETURN),
27454 const0_rtx),
27455 gen_rtx_EXPR_LIST (VOIDmode,
27456 gen_rtx_REG (SImode,
27457 GP_ARG_RETURN + 1),
27458 GEN_INT (4))));
27461 if (DECIMAL_FLOAT_MODE_P (mode) && TARGET_HARD_FLOAT && TARGET_FPRS)
27462 /* _Decimal128 must use an even/odd register pair. */
27463 regno = (mode == TDmode) ? FP_ARG_RETURN + 1 : FP_ARG_RETURN;
27464 else if (SCALAR_FLOAT_MODE_P (mode)
27465 && TARGET_HARD_FLOAT && TARGET_FPRS
27466 && ((TARGET_SINGLE_FLOAT && mode == SFmode) || TARGET_DOUBLE_FLOAT))
27467 regno = FP_ARG_RETURN;
27468 /* VSX is a superset of Altivec and adds V2DImode/V2DFmode. Since the same
27469 return register is used in both cases, and we won't see V2DImode/V2DFmode
27470 for pure altivec, combine the two cases. */
27471 else if (ALTIVEC_OR_VSX_VECTOR_MODE (mode)
27472 && TARGET_ALTIVEC && TARGET_ALTIVEC_ABI)
27473 regno = ALTIVEC_ARG_RETURN;
27474 else if (COMPLEX_MODE_P (mode) && targetm.calls.split_complex_arg)
27475 return rs6000_complex_function_value (mode);
27476 else if (TARGET_E500_DOUBLE && TARGET_HARD_FLOAT
27477 && (mode == DFmode || mode == DCmode
27478 || mode == TFmode || mode == TCmode))
27479 return spe_build_register_parallel (mode, GP_ARG_RETURN);
27480 else
27481 regno = GP_ARG_RETURN;
27483 return gen_rtx_REG (mode, regno);
27487 /* Given FROM and TO register numbers, say whether this elimination is allowed.
27488 Frame pointer elimination is automatically handled.
27490 For the RS/6000, if frame pointer elimination is being done, we would like
27491 to convert ap into fp, not sp.
27493 We need r30 if -mminimal-toc was specified, and there are constant pool
27494 references. */
27496 static bool
27497 rs6000_can_eliminate (const int from, const int to)
27499 return (from == ARG_POINTER_REGNUM && to == STACK_POINTER_REGNUM
27500 ? ! frame_pointer_needed
27501 : from == RS6000_PIC_OFFSET_TABLE_REGNUM
27502 ? ! TARGET_MINIMAL_TOC || TARGET_NO_TOC || get_pool_size () == 0
27503 : true);
27506 /* Define the offset between two registers, FROM to be eliminated and its
27507 replacement TO, at the start of a routine. */
27508 HOST_WIDE_INT
27509 rs6000_initial_elimination_offset (int from, int to)
27511 rs6000_stack_t *info = rs6000_stack_info ();
27512 HOST_WIDE_INT offset;
27514 if (from == HARD_FRAME_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
27515 offset = info->push_p ? 0 : -info->total_size;
27516 else if (from == FRAME_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
27518 offset = info->push_p ? 0 : -info->total_size;
27519 if (FRAME_GROWS_DOWNWARD)
27520 offset += info->fixed_size + info->vars_size + info->parm_size;
27522 else if (from == FRAME_POINTER_REGNUM && to == HARD_FRAME_POINTER_REGNUM)
27523 offset = FRAME_GROWS_DOWNWARD
27524 ? info->fixed_size + info->vars_size + info->parm_size
27525 : 0;
27526 else if (from == ARG_POINTER_REGNUM && to == HARD_FRAME_POINTER_REGNUM)
27527 offset = info->total_size;
27528 else if (from == ARG_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
27529 offset = info->push_p ? info->total_size : 0;
27530 else if (from == RS6000_PIC_OFFSET_TABLE_REGNUM)
27531 offset = 0;
27532 else
27533 gcc_unreachable ();
27535 return offset;
27538 static rtx
27539 rs6000_dwarf_register_span (rtx reg)
27541 rtx parts[8];
27542 int i, words;
27543 unsigned regno = REGNO (reg);
27544 enum machine_mode mode = GET_MODE (reg);
27546 if (TARGET_SPE
27547 && regno < 32
27548 && (SPE_VECTOR_MODE (GET_MODE (reg))
27549 || (TARGET_E500_DOUBLE && FLOAT_MODE_P (mode)
27550 && mode != SFmode && mode != SDmode && mode != SCmode)))
27552 else
27553 return NULL_RTX;
27555 regno = REGNO (reg);
27557 /* The duality of the SPE register size wreaks all kinds of havoc.
27558 This is a way of distinguishing r0 in 32-bits from r0 in
27559 64-bits. */
27560 words = (GET_MODE_SIZE (mode) + UNITS_PER_FP_WORD - 1) / UNITS_PER_FP_WORD;
27561 gcc_assert (words <= 4);
27562 for (i = 0; i < words; i++, regno++)
27564 if (BYTES_BIG_ENDIAN)
27566 parts[2 * i] = gen_rtx_REG (SImode, regno + 1200);
27567 parts[2 * i + 1] = gen_rtx_REG (SImode, regno);
27569 else
27571 parts[2 * i] = gen_rtx_REG (SImode, regno);
27572 parts[2 * i + 1] = gen_rtx_REG (SImode, regno + 1200);
27576 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (words * 2, parts));
27579 /* Fill in sizes for SPE register high parts in table used by unwinder. */
27581 static void
27582 rs6000_init_dwarf_reg_sizes_extra (tree address)
27584 if (TARGET_SPE)
27586 int i;
27587 enum machine_mode mode = TYPE_MODE (char_type_node);
27588 rtx addr = expand_expr (address, NULL_RTX, VOIDmode, EXPAND_NORMAL);
27589 rtx mem = gen_rtx_MEM (BLKmode, addr);
27590 rtx value = gen_int_mode (4, mode);
27592 for (i = 1201; i < 1232; i++)
27594 int column = DWARF_REG_TO_UNWIND_COLUMN (i);
27595 HOST_WIDE_INT offset
27596 = DWARF_FRAME_REGNUM (column) * GET_MODE_SIZE (mode);
27598 emit_move_insn (adjust_address (mem, mode, offset), value);
27603 /* Map internal gcc register numbers to DWARF2 register numbers. */
27605 unsigned int
27606 rs6000_dbx_register_number (unsigned int regno)
27608 if (regno <= 63 || write_symbols != DWARF2_DEBUG)
27609 return regno;
27610 if (regno == LR_REGNO)
27611 return 108;
27612 if (regno == CTR_REGNO)
27613 return 109;
27614 if (CR_REGNO_P (regno))
27615 return regno - CR0_REGNO + 86;
27616 if (regno == CA_REGNO)
27617 return 101; /* XER */
27618 if (ALTIVEC_REGNO_P (regno))
27619 return regno - FIRST_ALTIVEC_REGNO + 1124;
27620 if (regno == VRSAVE_REGNO)
27621 return 356;
27622 if (regno == VSCR_REGNO)
27623 return 67;
27624 if (regno == SPE_ACC_REGNO)
27625 return 99;
27626 if (regno == SPEFSCR_REGNO)
27627 return 612;
27628 /* SPE high reg number. We get these values of regno from
27629 rs6000_dwarf_register_span. */
27630 gcc_assert (regno >= 1200 && regno < 1232);
27631 return regno;
27634 /* target hook eh_return_filter_mode */
27635 static enum machine_mode
27636 rs6000_eh_return_filter_mode (void)
27638 return TARGET_32BIT ? SImode : word_mode;
27641 /* Target hook for scalar_mode_supported_p. */
27642 static bool
27643 rs6000_scalar_mode_supported_p (enum machine_mode mode)
27645 if (DECIMAL_FLOAT_MODE_P (mode))
27646 return default_decimal_float_supported_p ();
27647 else
27648 return default_scalar_mode_supported_p (mode);
27651 /* Target hook for vector_mode_supported_p. */
27652 static bool
27653 rs6000_vector_mode_supported_p (enum machine_mode mode)
27656 if (TARGET_PAIRED_FLOAT && PAIRED_VECTOR_MODE (mode))
27657 return true;
27659 if (TARGET_SPE && SPE_VECTOR_MODE (mode))
27660 return true;
27662 else if (VECTOR_MEM_ALTIVEC_OR_VSX_P (mode))
27663 return true;
27665 else
27666 return false;
27669 /* Target hook for invalid_arg_for_unprototyped_fn. */
27670 static const char *
27671 invalid_arg_for_unprototyped_fn (const_tree typelist, const_tree funcdecl, const_tree val)
27673 return (!rs6000_darwin64_abi
27674 && typelist == 0
27675 && TREE_CODE (TREE_TYPE (val)) == VECTOR_TYPE
27676 && (funcdecl == NULL_TREE
27677 || (TREE_CODE (funcdecl) == FUNCTION_DECL
27678 && DECL_BUILT_IN_CLASS (funcdecl) != BUILT_IN_MD)))
27679 ? N_("AltiVec argument passed to unprototyped function")
27680 : NULL;
27683 /* For TARGET_SECURE_PLT 32-bit PIC code we can save PIC register
27684 setup by using __stack_chk_fail_local hidden function instead of
27685 calling __stack_chk_fail directly. Otherwise it is better to call
27686 __stack_chk_fail directly. */
27688 static tree ATTRIBUTE_UNUSED
27689 rs6000_stack_protect_fail (void)
27691 return (DEFAULT_ABI == ABI_V4 && TARGET_SECURE_PLT && flag_pic)
27692 ? default_hidden_stack_protect_fail ()
27693 : default_external_stack_protect_fail ();
27696 void
27697 rs6000_final_prescan_insn (rtx insn, rtx *operand ATTRIBUTE_UNUSED,
27698 int num_operands ATTRIBUTE_UNUSED)
27700 if (rs6000_warn_cell_microcode)
27702 const char *temp;
27703 int insn_code_number = recog_memoized (insn);
27704 location_t location = INSN_LOCATION (insn);
27706 /* Punt on insns we cannot recognize. */
27707 if (insn_code_number < 0)
27708 return;
27710 temp = get_insn_template (insn_code_number, insn);
27712 if (get_attr_cell_micro (insn) == CELL_MICRO_ALWAYS)
27713 warning_at (location, OPT_mwarn_cell_microcode,
27714 "emitting microcode insn %s\t[%s] #%d",
27715 temp, insn_data[INSN_CODE (insn)].name, INSN_UID (insn));
27716 else if (get_attr_cell_micro (insn) == CELL_MICRO_CONDITIONAL)
27717 warning_at (location, OPT_mwarn_cell_microcode,
27718 "emitting conditional microcode insn %s\t[%s] #%d",
27719 temp, insn_data[INSN_CODE (insn)].name, INSN_UID (insn));
27723 /* Implement the TARGET_ASAN_SHADOW_OFFSET hook. */
27725 #if TARGET_ELF
27726 static unsigned HOST_WIDE_INT
27727 rs6000_asan_shadow_offset (void)
27729 return (unsigned HOST_WIDE_INT) 1 << (TARGET_64BIT ? 41 : 29);
27731 #endif
27733 /* Mask options that we want to support inside of attribute((target)) and
27734 #pragma GCC target operations. Note, we do not include things like
27735 64/32-bit, endianess, hard/soft floating point, etc. that would have
27736 different calling sequences. */
27738 struct rs6000_opt_mask {
27739 const char *name; /* option name */
27740 HOST_WIDE_INT mask; /* mask to set */
27741 bool invert; /* invert sense of mask */
27742 bool valid_target; /* option is a target option */
27745 static struct rs6000_opt_mask const rs6000_opt_masks[] =
27747 { "altivec", OPTION_MASK_ALTIVEC, false, true },
27748 { "cmpb", OPTION_MASK_CMPB, false, true },
27749 { "dlmzb", OPTION_MASK_DLMZB, false, true },
27750 { "fprnd", OPTION_MASK_FPRND, false, true },
27751 { "hard-dfp", OPTION_MASK_DFP, false, true },
27752 { "isel", OPTION_MASK_ISEL, false, true },
27753 { "mfcrf", OPTION_MASK_MFCRF, false, true },
27754 { "mfpgpr", OPTION_MASK_MFPGPR, false, true },
27755 { "mulhw", OPTION_MASK_MULHW, false, true },
27756 { "multiple", OPTION_MASK_MULTIPLE, false, true },
27757 { "update", OPTION_MASK_NO_UPDATE, true , true },
27758 { "popcntb", OPTION_MASK_POPCNTB, false, true },
27759 { "popcntd", OPTION_MASK_POPCNTD, false, true },
27760 { "powerpc-gfxopt", OPTION_MASK_PPC_GFXOPT, false, true },
27761 { "powerpc-gpopt", OPTION_MASK_PPC_GPOPT, false, true },
27762 { "recip-precision", OPTION_MASK_RECIP_PRECISION, false, true },
27763 { "string", OPTION_MASK_STRING, false, true },
27764 { "vsx", OPTION_MASK_VSX, false, true },
27765 { "vsx-timode", OPTION_MASK_VSX_TIMODE, false, true },
27766 #ifdef OPTION_MASK_64BIT
27767 #if TARGET_AIX_OS
27768 { "aix64", OPTION_MASK_64BIT, false, false },
27769 { "aix32", OPTION_MASK_64BIT, true, false },
27770 #else
27771 { "64", OPTION_MASK_64BIT, false, false },
27772 { "32", OPTION_MASK_64BIT, true, false },
27773 #endif
27774 #endif
27775 #ifdef OPTION_MASK_EABI
27776 { "eabi", OPTION_MASK_EABI, false, false },
27777 #endif
27778 #ifdef OPTION_MASK_LITTLE_ENDIAN
27779 { "little", OPTION_MASK_LITTLE_ENDIAN, false, false },
27780 { "big", OPTION_MASK_LITTLE_ENDIAN, true, false },
27781 #endif
27782 #ifdef OPTION_MASK_RELOCATABLE
27783 { "relocatable", OPTION_MASK_RELOCATABLE, false, false },
27784 #endif
27785 #ifdef OPTION_MASK_STRICT_ALIGN
27786 { "strict-align", OPTION_MASK_STRICT_ALIGN, false, false },
27787 #endif
27788 { "soft-float", OPTION_MASK_SOFT_FLOAT, false, false },
27789 { "string", OPTION_MASK_STRING, false, false },
27792 /* Builtin mask mapping for printing the flags. */
27793 static struct rs6000_opt_mask const rs6000_builtin_mask_names[] =
27795 { "altivec", RS6000_BTM_ALTIVEC, false, false },
27796 { "vsx", RS6000_BTM_VSX, false, false },
27797 { "spe", RS6000_BTM_SPE, false, false },
27798 { "paired", RS6000_BTM_PAIRED, false, false },
27799 { "fre", RS6000_BTM_FRE, false, false },
27800 { "fres", RS6000_BTM_FRES, false, false },
27801 { "frsqrte", RS6000_BTM_FRSQRTE, false, false },
27802 { "frsqrtes", RS6000_BTM_FRSQRTES, false, false },
27803 { "popcntd", RS6000_BTM_POPCNTD, false, false },
27804 { "cell", RS6000_BTM_CELL, false, false },
27807 /* Option variables that we want to support inside attribute((target)) and
27808 #pragma GCC target operations. */
27810 struct rs6000_opt_var {
27811 const char *name; /* option name */
27812 size_t global_offset; /* offset of the option in global_options. */
27813 size_t target_offset; /* offset of the option in target optiosn. */
27816 static struct rs6000_opt_var const rs6000_opt_vars[] =
27818 { "friz",
27819 offsetof (struct gcc_options, x_TARGET_FRIZ),
27820 offsetof (struct cl_target_option, x_TARGET_FRIZ), },
27821 { "avoid-indexed-addresses",
27822 offsetof (struct gcc_options, x_TARGET_AVOID_XFORM),
27823 offsetof (struct cl_target_option, x_TARGET_AVOID_XFORM) },
27824 { "paired",
27825 offsetof (struct gcc_options, x_rs6000_paired_float),
27826 offsetof (struct cl_target_option, x_rs6000_paired_float), },
27827 { "longcall",
27828 offsetof (struct gcc_options, x_rs6000_default_long_calls),
27829 offsetof (struct cl_target_option, x_rs6000_default_long_calls), },
27832 /* Inner function to handle attribute((target("..."))) and #pragma GCC target
27833 parsing. Return true if there were no errors. */
27835 static bool
27836 rs6000_inner_target_options (tree args, bool attr_p)
27838 bool ret = true;
27840 if (args == NULL_TREE)
27843 else if (TREE_CODE (args) == STRING_CST)
27845 char *p = ASTRDUP (TREE_STRING_POINTER (args));
27846 char *q;
27848 while ((q = strtok (p, ",")) != NULL)
27850 bool error_p = false;
27851 bool not_valid_p = false;
27852 const char *cpu_opt = NULL;
27854 p = NULL;
27855 if (strncmp (q, "cpu=", 4) == 0)
27857 int cpu_index = rs6000_cpu_name_lookup (q+4);
27858 if (cpu_index >= 0)
27859 rs6000_cpu_index = cpu_index;
27860 else
27862 error_p = true;
27863 cpu_opt = q+4;
27866 else if (strncmp (q, "tune=", 5) == 0)
27868 int tune_index = rs6000_cpu_name_lookup (q+5);
27869 if (tune_index >= 0)
27870 rs6000_tune_index = tune_index;
27871 else
27873 error_p = true;
27874 cpu_opt = q+5;
27877 else
27879 size_t i;
27880 bool invert = false;
27881 char *r = q;
27883 error_p = true;
27884 if (strncmp (r, "no-", 3) == 0)
27886 invert = true;
27887 r += 3;
27890 for (i = 0; i < ARRAY_SIZE (rs6000_opt_masks); i++)
27891 if (strcmp (r, rs6000_opt_masks[i].name) == 0)
27893 HOST_WIDE_INT mask = rs6000_opt_masks[i].mask;
27895 if (!rs6000_opt_masks[i].valid_target)
27896 not_valid_p = true;
27897 else
27899 error_p = false;
27900 rs6000_isa_flags_explicit |= mask;
27902 /* VSX needs altivec, so -mvsx automagically sets
27903 altivec. */
27904 if (mask == OPTION_MASK_VSX && !invert)
27905 mask |= OPTION_MASK_ALTIVEC;
27907 if (rs6000_opt_masks[i].invert)
27908 invert = !invert;
27910 if (invert)
27911 rs6000_isa_flags &= ~mask;
27912 else
27913 rs6000_isa_flags |= mask;
27915 break;
27918 if (error_p && !not_valid_p)
27920 for (i = 0; i < ARRAY_SIZE (rs6000_opt_vars); i++)
27921 if (strcmp (r, rs6000_opt_vars[i].name) == 0)
27923 size_t j = rs6000_opt_vars[i].global_offset;
27924 *((int *) ((char *)&global_options + j)) = !invert;
27925 error_p = false;
27926 break;
27931 if (error_p)
27933 const char *eprefix, *esuffix;
27935 ret = false;
27936 if (attr_p)
27938 eprefix = "__attribute__((__target__(";
27939 esuffix = ")))";
27941 else
27943 eprefix = "#pragma GCC target ";
27944 esuffix = "";
27947 if (cpu_opt)
27948 error ("invalid cpu \"%s\" for %s\"%s\"%s", cpu_opt, eprefix,
27949 q, esuffix);
27950 else if (not_valid_p)
27951 error ("%s\"%s\"%s is not allowed", eprefix, q, esuffix);
27952 else
27953 error ("%s\"%s\"%s is invalid", eprefix, q, esuffix);
27958 else if (TREE_CODE (args) == TREE_LIST)
27962 tree value = TREE_VALUE (args);
27963 if (value)
27965 bool ret2 = rs6000_inner_target_options (value, attr_p);
27966 if (!ret2)
27967 ret = false;
27969 args = TREE_CHAIN (args);
27971 while (args != NULL_TREE);
27974 else
27975 gcc_unreachable ();
27977 return ret;
27980 /* Print out the target options as a list for -mdebug=target. */
27982 static void
27983 rs6000_debug_target_options (tree args, const char *prefix)
27985 if (args == NULL_TREE)
27986 fprintf (stderr, "%s<NULL>", prefix);
27988 else if (TREE_CODE (args) == STRING_CST)
27990 char *p = ASTRDUP (TREE_STRING_POINTER (args));
27991 char *q;
27993 while ((q = strtok (p, ",")) != NULL)
27995 p = NULL;
27996 fprintf (stderr, "%s\"%s\"", prefix, q);
27997 prefix = ", ";
28001 else if (TREE_CODE (args) == TREE_LIST)
28005 tree value = TREE_VALUE (args);
28006 if (value)
28008 rs6000_debug_target_options (value, prefix);
28009 prefix = ", ";
28011 args = TREE_CHAIN (args);
28013 while (args != NULL_TREE);
28016 else
28017 gcc_unreachable ();
28019 return;
28023 /* Hook to validate attribute((target("..."))). */
28025 static bool
28026 rs6000_valid_attribute_p (tree fndecl,
28027 tree ARG_UNUSED (name),
28028 tree args,
28029 int flags)
28031 struct cl_target_option cur_target;
28032 bool ret;
28033 tree old_optimize = build_optimization_node ();
28034 tree new_target, new_optimize;
28035 tree func_optimize = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl);
28037 gcc_assert ((fndecl != NULL_TREE) && (args != NULL_TREE));
28039 if (TARGET_DEBUG_TARGET)
28041 tree tname = DECL_NAME (fndecl);
28042 fprintf (stderr, "\n==================== rs6000_valid_attribute_p:\n");
28043 if (tname)
28044 fprintf (stderr, "function: %.*s\n",
28045 (int) IDENTIFIER_LENGTH (tname),
28046 IDENTIFIER_POINTER (tname));
28047 else
28048 fprintf (stderr, "function: unknown\n");
28050 fprintf (stderr, "args:");
28051 rs6000_debug_target_options (args, " ");
28052 fprintf (stderr, "\n");
28054 if (flags)
28055 fprintf (stderr, "flags: 0x%x\n", flags);
28057 fprintf (stderr, "--------------------\n");
28060 old_optimize = build_optimization_node ();
28061 func_optimize = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl);
28063 /* If the function changed the optimization levels as well as setting target
28064 options, start with the optimizations specified. */
28065 if (func_optimize && func_optimize != old_optimize)
28066 cl_optimization_restore (&global_options,
28067 TREE_OPTIMIZATION (func_optimize));
28069 /* The target attributes may also change some optimization flags, so update
28070 the optimization options if necessary. */
28071 cl_target_option_save (&cur_target, &global_options);
28072 rs6000_cpu_index = rs6000_tune_index = -1;
28073 ret = rs6000_inner_target_options (args, true);
28075 /* Set up any additional state. */
28076 if (ret)
28078 ret = rs6000_option_override_internal (false);
28079 new_target = build_target_option_node ();
28081 else
28082 new_target = NULL;
28084 new_optimize = build_optimization_node ();
28086 if (!new_target)
28087 ret = false;
28089 else if (fndecl)
28091 DECL_FUNCTION_SPECIFIC_TARGET (fndecl) = new_target;
28093 if (old_optimize != new_optimize)
28094 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl) = new_optimize;
28097 cl_target_option_restore (&global_options, &cur_target);
28099 if (old_optimize != new_optimize)
28100 cl_optimization_restore (&global_options,
28101 TREE_OPTIMIZATION (old_optimize));
28103 return ret;
28107 /* Hook to validate the current #pragma GCC target and set the state, and
28108 update the macros based on what was changed. If ARGS is NULL, then
28109 POP_TARGET is used to reset the options. */
28111 bool
28112 rs6000_pragma_target_parse (tree args, tree pop_target)
28114 tree prev_tree = build_target_option_node ();
28115 tree cur_tree;
28116 struct cl_target_option *prev_opt, *cur_opt;
28117 HOST_WIDE_INT prev_flags, cur_flags, diff_flags;
28118 HOST_WIDE_INT prev_bumask, cur_bumask, diff_bumask;
28120 if (TARGET_DEBUG_TARGET)
28122 fprintf (stderr, "\n==================== rs6000_pragma_target_parse\n");
28123 fprintf (stderr, "args:");
28124 rs6000_debug_target_options (args, " ");
28125 fprintf (stderr, "\n");
28127 if (pop_target)
28129 fprintf (stderr, "pop_target:\n");
28130 debug_tree (pop_target);
28132 else
28133 fprintf (stderr, "pop_target: <NULL>\n");
28135 fprintf (stderr, "--------------------\n");
28138 if (! args)
28140 cur_tree = ((pop_target)
28141 ? pop_target
28142 : target_option_default_node);
28143 cl_target_option_restore (&global_options,
28144 TREE_TARGET_OPTION (cur_tree));
28146 else
28148 rs6000_cpu_index = rs6000_tune_index = -1;
28149 if (!rs6000_inner_target_options (args, false)
28150 || !rs6000_option_override_internal (false)
28151 || (cur_tree = build_target_option_node ()) == NULL_TREE)
28153 if (TARGET_DEBUG_BUILTIN || TARGET_DEBUG_TARGET)
28154 fprintf (stderr, "invalid pragma\n");
28156 return false;
28160 target_option_current_node = cur_tree;
28162 /* If we have the preprocessor linked in (i.e. C or C++ languages), possibly
28163 change the macros that are defined. */
28164 if (rs6000_target_modify_macros_ptr)
28166 prev_opt = TREE_TARGET_OPTION (prev_tree);
28167 prev_bumask = prev_opt->x_rs6000_builtin_mask;
28168 prev_flags = prev_opt->x_rs6000_isa_flags;
28170 cur_opt = TREE_TARGET_OPTION (cur_tree);
28171 cur_flags = cur_opt->x_rs6000_isa_flags;
28172 cur_bumask = cur_opt->x_rs6000_builtin_mask;
28174 diff_bumask = (prev_bumask ^ cur_bumask);
28175 diff_flags = (prev_flags ^ cur_flags);
28177 if ((diff_flags != 0) || (diff_bumask != 0))
28179 /* Delete old macros. */
28180 rs6000_target_modify_macros_ptr (false,
28181 prev_flags & diff_flags,
28182 prev_bumask & diff_bumask);
28184 /* Define new macros. */
28185 rs6000_target_modify_macros_ptr (true,
28186 cur_flags & diff_flags,
28187 cur_bumask & diff_bumask);
28191 return true;
28195 /* Remember the last target of rs6000_set_current_function. */
28196 static GTY(()) tree rs6000_previous_fndecl;
28198 /* Establish appropriate back-end context for processing the function
28199 FNDECL. The argument might be NULL to indicate processing at top
28200 level, outside of any function scope. */
28201 static void
28202 rs6000_set_current_function (tree fndecl)
28204 tree old_tree = (rs6000_previous_fndecl
28205 ? DECL_FUNCTION_SPECIFIC_TARGET (rs6000_previous_fndecl)
28206 : NULL_TREE);
28208 tree new_tree = (fndecl
28209 ? DECL_FUNCTION_SPECIFIC_TARGET (fndecl)
28210 : NULL_TREE);
28212 if (TARGET_DEBUG_TARGET)
28214 bool print_final = false;
28215 fprintf (stderr, "\n==================== rs6000_set_current_function");
28217 if (fndecl)
28218 fprintf (stderr, ", fndecl %s (%p)",
28219 (DECL_NAME (fndecl)
28220 ? IDENTIFIER_POINTER (DECL_NAME (fndecl))
28221 : "<unknown>"), (void *)fndecl);
28223 if (rs6000_previous_fndecl)
28224 fprintf (stderr, ", prev_fndecl (%p)", (void *)rs6000_previous_fndecl);
28226 fprintf (stderr, "\n");
28227 if (new_tree)
28229 fprintf (stderr, "\nnew fndecl target specific options:\n");
28230 debug_tree (new_tree);
28231 print_final = true;
28234 if (old_tree)
28236 fprintf (stderr, "\nold fndecl target specific options:\n");
28237 debug_tree (old_tree);
28238 print_final = true;
28241 if (print_final)
28242 fprintf (stderr, "--------------------\n");
28245 /* Only change the context if the function changes. This hook is called
28246 several times in the course of compiling a function, and we don't want to
28247 slow things down too much or call target_reinit when it isn't safe. */
28248 if (fndecl && fndecl != rs6000_previous_fndecl)
28250 rs6000_previous_fndecl = fndecl;
28251 if (old_tree == new_tree)
28254 else if (new_tree)
28256 cl_target_option_restore (&global_options,
28257 TREE_TARGET_OPTION (new_tree));
28258 target_reinit ();
28261 else if (old_tree)
28263 struct cl_target_option *def
28264 = TREE_TARGET_OPTION (target_option_current_node);
28266 cl_target_option_restore (&global_options, def);
28267 target_reinit ();
28273 /* Save the current options */
28275 static void
28276 rs6000_function_specific_save (struct cl_target_option *ptr)
28278 ptr->x_rs6000_isa_flags = rs6000_isa_flags;
28279 ptr->x_rs6000_isa_flags_explicit = rs6000_isa_flags_explicit;
28282 /* Restore the current options */
28284 static void
28285 rs6000_function_specific_restore (struct cl_target_option *ptr)
28287 rs6000_isa_flags = ptr->x_rs6000_isa_flags;
28288 rs6000_isa_flags_explicit = ptr->x_rs6000_isa_flags_explicit;
28289 (void) rs6000_option_override_internal (false);
28292 /* Print the current options */
28294 static void
28295 rs6000_function_specific_print (FILE *file, int indent,
28296 struct cl_target_option *ptr)
28298 rs6000_print_isa_options (file, indent, "Isa options set",
28299 ptr->x_rs6000_isa_flags);
28301 rs6000_print_isa_options (file, indent, "Isa options explicit",
28302 ptr->x_rs6000_isa_flags_explicit);
28305 /* Helper function to print the current isa or misc options on a line. */
28307 static void
28308 rs6000_print_options_internal (FILE *file,
28309 int indent,
28310 const char *string,
28311 HOST_WIDE_INT flags,
28312 const char *prefix,
28313 const struct rs6000_opt_mask *opts,
28314 size_t num_elements)
28316 size_t i;
28317 size_t start_column = 0;
28318 size_t cur_column;
28319 size_t max_column = 76;
28320 const char *comma = "";
28321 const char *nl = "\n";
28323 if (indent)
28324 start_column += fprintf (file, "%*s", indent, "");
28326 if (!flags)
28328 fprintf (stderr, DEBUG_FMT_S, string, "<none>");
28329 return;
28332 start_column += fprintf (stderr, DEBUG_FMT_WX, string, flags);
28334 /* Print the various mask options. */
28335 cur_column = start_column;
28336 for (i = 0; i < num_elements; i++)
28338 if ((flags & opts[i].mask) != 0)
28340 const char *no_str = rs6000_opt_masks[i].invert ? "no-" : "";
28341 size_t len = (strlen (comma)
28342 + strlen (prefix)
28343 + strlen (no_str)
28344 + strlen (rs6000_opt_masks[i].name));
28346 cur_column += len;
28347 if (cur_column > max_column)
28349 fprintf (stderr, ", \\\n%*s", (int)start_column, "");
28350 cur_column = start_column + len;
28351 comma = "";
28352 nl = "\n\n";
28355 fprintf (file, "%s%s%s%s", comma, prefix, no_str,
28356 rs6000_opt_masks[i].name);
28357 flags &= ~ opts[i].mask;
28358 comma = ", ";
28362 fputs (nl, file);
28365 /* Helper function to print the current isa options on a line. */
28367 static void
28368 rs6000_print_isa_options (FILE *file, int indent, const char *string,
28369 HOST_WIDE_INT flags)
28371 rs6000_print_options_internal (file, indent, string, flags, "-m",
28372 &rs6000_opt_masks[0],
28373 ARRAY_SIZE (rs6000_opt_masks));
28376 static void
28377 rs6000_print_builtin_options (FILE *file, int indent, const char *string,
28378 HOST_WIDE_INT flags)
28380 rs6000_print_options_internal (file, indent, string, flags, "",
28381 &rs6000_builtin_mask_names[0],
28382 ARRAY_SIZE (rs6000_builtin_mask_names));
28386 /* Hook to determine if one function can safely inline another. */
28388 static bool
28389 rs6000_can_inline_p (tree caller, tree callee)
28391 bool ret = false;
28392 tree caller_tree = DECL_FUNCTION_SPECIFIC_TARGET (caller);
28393 tree callee_tree = DECL_FUNCTION_SPECIFIC_TARGET (callee);
28395 /* If callee has no option attributes, then it is ok to inline. */
28396 if (!callee_tree)
28397 ret = true;
28399 /* If caller has no option attributes, but callee does then it is not ok to
28400 inline. */
28401 else if (!caller_tree)
28402 ret = false;
28404 else
28406 struct cl_target_option *caller_opts = TREE_TARGET_OPTION (caller_tree);
28407 struct cl_target_option *callee_opts = TREE_TARGET_OPTION (callee_tree);
28409 /* Callee's options should a subset of the caller's, i.e. a vsx function
28410 can inline an altivec function but a non-vsx function can't inline a
28411 vsx function. */
28412 if ((caller_opts->x_rs6000_isa_flags & callee_opts->x_rs6000_isa_flags)
28413 == callee_opts->x_rs6000_isa_flags)
28414 ret = true;
28417 if (TARGET_DEBUG_TARGET)
28418 fprintf (stderr, "rs6000_can_inline_p:, caller %s, callee %s, %s inline\n",
28419 (DECL_NAME (caller)
28420 ? IDENTIFIER_POINTER (DECL_NAME (caller))
28421 : "<unknown>"),
28422 (DECL_NAME (callee)
28423 ? IDENTIFIER_POINTER (DECL_NAME (callee))
28424 : "<unknown>"),
28425 (ret ? "can" : "cannot"));
28427 return ret;
28430 /* Allocate a stack temp and fixup the address so it meets the particular
28431 memory requirements (either offetable or REG+REG addressing). */
28434 rs6000_allocate_stack_temp (enum machine_mode mode,
28435 bool offsettable_p,
28436 bool reg_reg_p)
28438 rtx stack = assign_stack_temp (mode, GET_MODE_SIZE (mode));
28439 rtx addr = XEXP (stack, 0);
28440 int strict_p = (reload_in_progress || reload_completed);
28442 if (!legitimate_indirect_address_p (addr, strict_p))
28444 if (offsettable_p
28445 && !rs6000_legitimate_offset_address_p (mode, addr, strict_p, true))
28446 stack = replace_equiv_address (stack, copy_addr_to_reg (addr));
28448 else if (reg_reg_p && !legitimate_indexed_address_p (addr, strict_p))
28449 stack = replace_equiv_address (stack, copy_addr_to_reg (addr));
28452 return stack;
28455 /* Given a memory reference, if it is not a reg or reg+reg addressing, convert
28456 to such a form to deal with memory reference instructions like STFIWX that
28457 only take reg+reg addressing. */
28460 rs6000_address_for_fpconvert (rtx x)
28462 int strict_p = (reload_in_progress || reload_completed);
28463 rtx addr;
28465 gcc_assert (MEM_P (x));
28466 addr = XEXP (x, 0);
28467 if (! legitimate_indirect_address_p (addr, strict_p)
28468 && ! legitimate_indexed_address_p (addr, strict_p))
28470 if (GET_CODE (addr) == PRE_INC || GET_CODE (addr) == PRE_DEC)
28472 rtx reg = XEXP (addr, 0);
28473 HOST_WIDE_INT size = GET_MODE_SIZE (GET_MODE (x));
28474 rtx size_rtx = GEN_INT ((GET_CODE (addr) == PRE_DEC) ? -size : size);
28475 gcc_assert (REG_P (reg));
28476 emit_insn (gen_add3_insn (reg, reg, size_rtx));
28477 addr = reg;
28479 else if (GET_CODE (addr) == PRE_MODIFY)
28481 rtx reg = XEXP (addr, 0);
28482 rtx expr = XEXP (addr, 1);
28483 gcc_assert (REG_P (reg));
28484 gcc_assert (GET_CODE (expr) == PLUS);
28485 emit_insn (gen_add3_insn (reg, XEXP (expr, 0), XEXP (expr, 1)));
28486 addr = reg;
28489 x = replace_equiv_address (x, copy_addr_to_reg (addr));
28492 return x;
28495 /* Given a memory reference, if it is not in the form for altivec memory
28496 reference instructions (i.e. reg or reg+reg addressing with AND of -16),
28497 convert to the altivec format. */
28500 rs6000_address_for_altivec (rtx x)
28502 gcc_assert (MEM_P (x));
28503 if (!altivec_indexed_or_indirect_operand (x, GET_MODE (x)))
28505 rtx addr = XEXP (x, 0);
28506 int strict_p = (reload_in_progress || reload_completed);
28508 if (!legitimate_indexed_address_p (addr, strict_p)
28509 && !legitimate_indirect_address_p (addr, strict_p))
28510 addr = copy_to_mode_reg (Pmode, addr);
28512 addr = gen_rtx_AND (Pmode, addr, GEN_INT (-16));
28513 x = change_address (x, GET_MODE (x), addr);
28516 return x;
28519 /* Implement TARGET_LEGITIMATE_CONSTANT_P.
28521 On the RS/6000, all integer constants are acceptable, most won't be valid
28522 for particular insns, though. Only easy FP constants are acceptable. */
28524 static bool
28525 rs6000_legitimate_constant_p (enum machine_mode mode, rtx x)
28527 if (TARGET_ELF && rs6000_tls_referenced_p (x))
28528 return false;
28530 return ((GET_CODE (x) != CONST_DOUBLE && GET_CODE (x) != CONST_VECTOR)
28531 || GET_MODE (x) == VOIDmode
28532 || (TARGET_POWERPC64 && mode == DImode)
28533 || easy_fp_constant (x, mode)
28534 || easy_vector_constant (x, mode));
28538 /* A function pointer under AIX is a pointer to a data area whose first word
28539 contains the actual address of the function, whose second word contains a
28540 pointer to its TOC, and whose third word contains a value to place in the
28541 static chain register (r11). Note that if we load the static chain, our
28542 "trampoline" need not have any executable code. */
28544 void
28545 rs6000_call_indirect_aix (rtx value, rtx func_desc, rtx flag)
28547 rtx func_addr;
28548 rtx toc_reg;
28549 rtx sc_reg;
28550 rtx stack_ptr;
28551 rtx stack_toc_offset;
28552 rtx stack_toc_mem;
28553 rtx func_toc_offset;
28554 rtx func_toc_mem;
28555 rtx func_sc_offset;
28556 rtx func_sc_mem;
28557 rtx insn;
28558 rtx (*call_func) (rtx, rtx, rtx, rtx);
28559 rtx (*call_value_func) (rtx, rtx, rtx, rtx, rtx);
28561 stack_ptr = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
28562 toc_reg = gen_rtx_REG (Pmode, TOC_REGNUM);
28564 /* Load up address of the actual function. */
28565 func_desc = force_reg (Pmode, func_desc);
28566 func_addr = gen_reg_rtx (Pmode);
28567 emit_move_insn (func_addr, gen_rtx_MEM (Pmode, func_desc));
28569 if (TARGET_32BIT)
28572 stack_toc_offset = GEN_INT (TOC_SAVE_OFFSET_32BIT);
28573 func_toc_offset = GEN_INT (AIX_FUNC_DESC_TOC_32BIT);
28574 func_sc_offset = GEN_INT (AIX_FUNC_DESC_SC_32BIT);
28575 if (TARGET_POINTERS_TO_NESTED_FUNCTIONS)
28577 call_func = gen_call_indirect_aix32bit;
28578 call_value_func = gen_call_value_indirect_aix32bit;
28580 else
28582 call_func = gen_call_indirect_aix32bit_nor11;
28583 call_value_func = gen_call_value_indirect_aix32bit_nor11;
28586 else
28588 stack_toc_offset = GEN_INT (TOC_SAVE_OFFSET_64BIT);
28589 func_toc_offset = GEN_INT (AIX_FUNC_DESC_TOC_64BIT);
28590 func_sc_offset = GEN_INT (AIX_FUNC_DESC_SC_64BIT);
28591 if (TARGET_POINTERS_TO_NESTED_FUNCTIONS)
28593 call_func = gen_call_indirect_aix64bit;
28594 call_value_func = gen_call_value_indirect_aix64bit;
28596 else
28598 call_func = gen_call_indirect_aix64bit_nor11;
28599 call_value_func = gen_call_value_indirect_aix64bit_nor11;
28603 /* Reserved spot to store the TOC. */
28604 stack_toc_mem = gen_frame_mem (Pmode,
28605 gen_rtx_PLUS (Pmode,
28606 stack_ptr,
28607 stack_toc_offset));
28609 gcc_assert (cfun);
28610 gcc_assert (cfun->machine);
28612 /* Can we optimize saving the TOC in the prologue or do we need to do it at
28613 every call? */
28614 if (TARGET_SAVE_TOC_INDIRECT && !cfun->calls_alloca)
28615 cfun->machine->save_toc_in_prologue = true;
28617 else
28619 MEM_VOLATILE_P (stack_toc_mem) = 1;
28620 emit_move_insn (stack_toc_mem, toc_reg);
28623 /* Calculate the address to load the TOC of the called function. We don't
28624 actually load this until the split after reload. */
28625 func_toc_mem = gen_rtx_MEM (Pmode,
28626 gen_rtx_PLUS (Pmode,
28627 func_desc,
28628 func_toc_offset));
28630 /* If we have a static chain, load it up. */
28631 if (TARGET_POINTERS_TO_NESTED_FUNCTIONS)
28633 func_sc_mem = gen_rtx_MEM (Pmode,
28634 gen_rtx_PLUS (Pmode,
28635 func_desc,
28636 func_sc_offset));
28638 sc_reg = gen_rtx_REG (Pmode, STATIC_CHAIN_REGNUM);
28639 emit_move_insn (sc_reg, func_sc_mem);
28642 /* Create the call. */
28643 if (value)
28644 insn = call_value_func (value, func_addr, flag, func_toc_mem,
28645 stack_toc_mem);
28646 else
28647 insn = call_func (func_addr, flag, func_toc_mem, stack_toc_mem);
28649 emit_call_insn (insn);
28652 /* Return whether we need to always update the saved TOC pointer when we update
28653 the stack pointer. */
28655 static bool
28656 rs6000_save_toc_in_prologue_p (void)
28658 return (cfun && cfun->machine && cfun->machine->save_toc_in_prologue);
28661 #ifdef HAVE_GAS_HIDDEN
28662 # define USE_HIDDEN_LINKONCE 1
28663 #else
28664 # define USE_HIDDEN_LINKONCE 0
28665 #endif
28667 /* Fills in the label name that should be used for a 476 link stack thunk. */
28669 void
28670 get_ppc476_thunk_name (char name[32])
28672 gcc_assert (TARGET_LINK_STACK);
28674 if (USE_HIDDEN_LINKONCE)
28675 sprintf (name, "__ppc476.get_thunk");
28676 else
28677 ASM_GENERATE_INTERNAL_LABEL (name, "LPPC476_", 0);
28680 /* This function emits the simple thunk routine that is used to preserve
28681 the link stack on the 476 cpu. */
28683 static void rs6000_code_end (void) ATTRIBUTE_UNUSED;
28684 static void
28685 rs6000_code_end (void)
28687 char name[32];
28688 tree decl;
28690 if (!TARGET_LINK_STACK)
28691 return;
28693 get_ppc476_thunk_name (name);
28695 decl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL, get_identifier (name),
28696 build_function_type_list (void_type_node, NULL_TREE));
28697 DECL_RESULT (decl) = build_decl (BUILTINS_LOCATION, RESULT_DECL,
28698 NULL_TREE, void_type_node);
28699 TREE_PUBLIC (decl) = 1;
28700 TREE_STATIC (decl) = 1;
28702 #if RS6000_WEAK
28703 if (USE_HIDDEN_LINKONCE)
28705 DECL_COMDAT_GROUP (decl) = DECL_ASSEMBLER_NAME (decl);
28706 targetm.asm_out.unique_section (decl, 0);
28707 switch_to_section (get_named_section (decl, NULL, 0));
28708 DECL_WEAK (decl) = 1;
28709 ASM_WEAKEN_DECL (asm_out_file, decl, name, 0);
28710 targetm.asm_out.globalize_label (asm_out_file, name);
28711 targetm.asm_out.assemble_visibility (decl, VISIBILITY_HIDDEN);
28712 ASM_DECLARE_FUNCTION_NAME (asm_out_file, name, decl);
28714 else
28715 #endif
28717 switch_to_section (text_section);
28718 ASM_OUTPUT_LABEL (asm_out_file, name);
28721 DECL_INITIAL (decl) = make_node (BLOCK);
28722 current_function_decl = decl;
28723 init_function_start (decl);
28724 first_function_block_is_cold = false;
28725 /* Make sure unwind info is emitted for the thunk if needed. */
28726 final_start_function (emit_barrier (), asm_out_file, 1);
28728 fputs ("\tblr\n", asm_out_file);
28730 final_end_function ();
28731 init_insn_lengths ();
28732 free_after_compilation (cfun);
28733 set_cfun (NULL);
28734 current_function_decl = NULL;
28737 /* Add r30 to hard reg set if the prologue sets it up and it is not
28738 pic_offset_table_rtx. */
28740 static void
28741 rs6000_set_up_by_prologue (struct hard_reg_set_container *set)
28743 if (!TARGET_SINGLE_PIC_BASE
28744 && TARGET_TOC
28745 && TARGET_MINIMAL_TOC
28746 && get_pool_size () != 0)
28747 add_to_hard_reg_set (&set->set, Pmode, RS6000_PIC_OFFSET_TABLE_REGNUM);
28750 struct gcc_target targetm = TARGET_INITIALIZER;
28752 #include "gt-rs6000.h"