2012-11-19 Mans Rullgard <mans@mansr.com>
[official-gcc.git] / gcc / config / rs6000 / rs6000.c
blobf55597f33d1b23c5953703b902a5843000cacbb6
1 /* Subroutines used for code generation on IBM RS/6000.
2 Copyright (C) 1991-2012 Free Software Foundation, Inc.
3 Contributed by Richard Kenner (kenner@vlsi1.ultra.nyu.edu)
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published
9 by the Free Software Foundation; either version 3, or (at your
10 option) any later version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
15 License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "rtl.h"
26 #include "regs.h"
27 #include "hard-reg-set.h"
28 #include "insn-config.h"
29 #include "conditions.h"
30 #include "insn-attr.h"
31 #include "flags.h"
32 #include "recog.h"
33 #include "obstack.h"
34 #include "tree.h"
35 #include "expr.h"
36 #include "optabs.h"
37 #include "except.h"
38 #include "function.h"
39 #include "output.h"
40 #include "dbxout.h"
41 #include "basic-block.h"
42 #include "diagnostic-core.h"
43 #include "toplev.h"
44 #include "ggc.h"
45 #include "hashtab.h"
46 #include "tm_p.h"
47 #include "target.h"
48 #include "target-def.h"
49 #include "common/common-target.h"
50 #include "langhooks.h"
51 #include "reload.h"
52 #include "cfgloop.h"
53 #include "sched-int.h"
54 #include "gimple.h"
55 #include "tree-flow.h"
56 #include "intl.h"
57 #include "params.h"
58 #include "tm-constrs.h"
59 #include "opts.h"
60 #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 *read_only_private_data_section;
212 static GTY(()) section *sdata2_section;
213 static GTY(()) section *toc_section;
215 struct builtin_description
217 const HOST_WIDE_INT mask;
218 const enum insn_code icode;
219 const char *const name;
220 const enum rs6000_builtins code;
223 /* Describe the vector unit used for modes. */
224 enum rs6000_vector rs6000_vector_unit[NUM_MACHINE_MODES];
225 enum rs6000_vector rs6000_vector_mem[NUM_MACHINE_MODES];
227 /* Register classes for various constraints that are based on the target
228 switches. */
229 enum reg_class rs6000_constraints[RS6000_CONSTRAINT_MAX];
231 /* Describe the alignment of a vector. */
232 int rs6000_vector_align[NUM_MACHINE_MODES];
234 /* Map selected modes to types for builtins. */
235 static GTY(()) tree builtin_mode_to_type[MAX_MACHINE_MODE][2];
237 /* What modes to automatically generate reciprocal divide estimate (fre) and
238 reciprocal sqrt (frsqrte) for. */
239 unsigned char rs6000_recip_bits[MAX_MACHINE_MODE];
241 /* Masks to determine which reciprocal esitmate instructions to generate
242 automatically. */
243 enum rs6000_recip_mask {
244 RECIP_SF_DIV = 0x001, /* Use divide estimate */
245 RECIP_DF_DIV = 0x002,
246 RECIP_V4SF_DIV = 0x004,
247 RECIP_V2DF_DIV = 0x008,
249 RECIP_SF_RSQRT = 0x010, /* Use reciprocal sqrt estimate. */
250 RECIP_DF_RSQRT = 0x020,
251 RECIP_V4SF_RSQRT = 0x040,
252 RECIP_V2DF_RSQRT = 0x080,
254 /* Various combination of flags for -mrecip=xxx. */
255 RECIP_NONE = 0,
256 RECIP_ALL = (RECIP_SF_DIV | RECIP_DF_DIV | RECIP_V4SF_DIV
257 | RECIP_V2DF_DIV | RECIP_SF_RSQRT | RECIP_DF_RSQRT
258 | RECIP_V4SF_RSQRT | RECIP_V2DF_RSQRT),
260 RECIP_HIGH_PRECISION = RECIP_ALL,
262 /* On low precision machines like the power5, don't enable double precision
263 reciprocal square root estimate, since it isn't accurate enough. */
264 RECIP_LOW_PRECISION = (RECIP_ALL & ~(RECIP_DF_RSQRT | RECIP_V2DF_RSQRT))
267 /* -mrecip options. */
268 static struct
270 const char *string; /* option name */
271 unsigned int mask; /* mask bits to set */
272 } recip_options[] = {
273 { "all", RECIP_ALL },
274 { "none", RECIP_NONE },
275 { "div", (RECIP_SF_DIV | RECIP_DF_DIV | RECIP_V4SF_DIV
276 | RECIP_V2DF_DIV) },
277 { "divf", (RECIP_SF_DIV | RECIP_V4SF_DIV) },
278 { "divd", (RECIP_DF_DIV | RECIP_V2DF_DIV) },
279 { "rsqrt", (RECIP_SF_RSQRT | RECIP_DF_RSQRT | RECIP_V4SF_RSQRT
280 | RECIP_V2DF_RSQRT) },
281 { "rsqrtf", (RECIP_SF_RSQRT | RECIP_V4SF_RSQRT) },
282 { "rsqrtd", (RECIP_DF_RSQRT | RECIP_V2DF_RSQRT) },
285 /* 2 argument gen function typedef. */
286 typedef rtx (*gen_2arg_fn_t) (rtx, rtx, rtx);
288 /* Pointer to function (in rs6000-c.c) that can define or undefine target
289 macros that have changed. Languages that don't support the preprocessor
290 don't link in rs6000-c.c, so we can't call it directly. */
291 void (*rs6000_target_modify_macros_ptr) (bool, HOST_WIDE_INT, HOST_WIDE_INT);
294 /* Target cpu costs. */
296 struct processor_costs {
297 const int mulsi; /* cost of SImode multiplication. */
298 const int mulsi_const; /* cost of SImode multiplication by constant. */
299 const int mulsi_const9; /* cost of SImode mult by short constant. */
300 const int muldi; /* cost of DImode multiplication. */
301 const int divsi; /* cost of SImode division. */
302 const int divdi; /* cost of DImode division. */
303 const int fp; /* cost of simple SFmode and DFmode insns. */
304 const int dmul; /* cost of DFmode multiplication (and fmadd). */
305 const int sdiv; /* cost of SFmode division (fdivs). */
306 const int ddiv; /* cost of DFmode division (fdiv). */
307 const int cache_line_size; /* cache line size in bytes. */
308 const int l1_cache_size; /* size of l1 cache, in kilobytes. */
309 const int l2_cache_size; /* size of l2 cache, in kilobytes. */
310 const int simultaneous_prefetches; /* number of parallel prefetch
311 operations. */
314 const struct processor_costs *rs6000_cost;
316 /* Processor costs (relative to an add) */
318 /* Instruction size costs on 32bit processors. */
319 static const
320 struct processor_costs size32_cost = {
321 COSTS_N_INSNS (1), /* mulsi */
322 COSTS_N_INSNS (1), /* mulsi_const */
323 COSTS_N_INSNS (1), /* mulsi_const9 */
324 COSTS_N_INSNS (1), /* muldi */
325 COSTS_N_INSNS (1), /* divsi */
326 COSTS_N_INSNS (1), /* divdi */
327 COSTS_N_INSNS (1), /* fp */
328 COSTS_N_INSNS (1), /* dmul */
329 COSTS_N_INSNS (1), /* sdiv */
330 COSTS_N_INSNS (1), /* ddiv */
337 /* Instruction size costs on 64bit processors. */
338 static const
339 struct processor_costs size64_cost = {
340 COSTS_N_INSNS (1), /* mulsi */
341 COSTS_N_INSNS (1), /* mulsi_const */
342 COSTS_N_INSNS (1), /* mulsi_const9 */
343 COSTS_N_INSNS (1), /* muldi */
344 COSTS_N_INSNS (1), /* divsi */
345 COSTS_N_INSNS (1), /* divdi */
346 COSTS_N_INSNS (1), /* fp */
347 COSTS_N_INSNS (1), /* dmul */
348 COSTS_N_INSNS (1), /* sdiv */
349 COSTS_N_INSNS (1), /* ddiv */
350 128,
356 /* Instruction costs on RS64A processors. */
357 static const
358 struct processor_costs rs64a_cost = {
359 COSTS_N_INSNS (20), /* mulsi */
360 COSTS_N_INSNS (12), /* mulsi_const */
361 COSTS_N_INSNS (8), /* mulsi_const9 */
362 COSTS_N_INSNS (34), /* muldi */
363 COSTS_N_INSNS (65), /* divsi */
364 COSTS_N_INSNS (67), /* divdi */
365 COSTS_N_INSNS (4), /* fp */
366 COSTS_N_INSNS (4), /* dmul */
367 COSTS_N_INSNS (31), /* sdiv */
368 COSTS_N_INSNS (31), /* ddiv */
369 128, /* cache line size */
370 128, /* l1 cache */
371 2048, /* l2 cache */
372 1, /* streams */
375 /* Instruction costs on MPCCORE processors. */
376 static const
377 struct processor_costs mpccore_cost = {
378 COSTS_N_INSNS (2), /* mulsi */
379 COSTS_N_INSNS (2), /* mulsi_const */
380 COSTS_N_INSNS (2), /* mulsi_const9 */
381 COSTS_N_INSNS (2), /* muldi */
382 COSTS_N_INSNS (6), /* divsi */
383 COSTS_N_INSNS (6), /* divdi */
384 COSTS_N_INSNS (4), /* fp */
385 COSTS_N_INSNS (5), /* dmul */
386 COSTS_N_INSNS (10), /* sdiv */
387 COSTS_N_INSNS (17), /* ddiv */
388 32, /* cache line size */
389 4, /* l1 cache */
390 16, /* l2 cache */
391 1, /* streams */
394 /* Instruction costs on PPC403 processors. */
395 static const
396 struct processor_costs ppc403_cost = {
397 COSTS_N_INSNS (4), /* mulsi */
398 COSTS_N_INSNS (4), /* mulsi_const */
399 COSTS_N_INSNS (4), /* mulsi_const9 */
400 COSTS_N_INSNS (4), /* muldi */
401 COSTS_N_INSNS (33), /* divsi */
402 COSTS_N_INSNS (33), /* divdi */
403 COSTS_N_INSNS (11), /* fp */
404 COSTS_N_INSNS (11), /* dmul */
405 COSTS_N_INSNS (11), /* sdiv */
406 COSTS_N_INSNS (11), /* ddiv */
407 32, /* cache line size */
408 4, /* l1 cache */
409 16, /* l2 cache */
410 1, /* streams */
413 /* Instruction costs on PPC405 processors. */
414 static const
415 struct processor_costs ppc405_cost = {
416 COSTS_N_INSNS (5), /* mulsi */
417 COSTS_N_INSNS (4), /* mulsi_const */
418 COSTS_N_INSNS (3), /* mulsi_const9 */
419 COSTS_N_INSNS (5), /* muldi */
420 COSTS_N_INSNS (35), /* divsi */
421 COSTS_N_INSNS (35), /* divdi */
422 COSTS_N_INSNS (11), /* fp */
423 COSTS_N_INSNS (11), /* dmul */
424 COSTS_N_INSNS (11), /* sdiv */
425 COSTS_N_INSNS (11), /* ddiv */
426 32, /* cache line size */
427 16, /* l1 cache */
428 128, /* l2 cache */
429 1, /* streams */
432 /* Instruction costs on PPC440 processors. */
433 static const
434 struct processor_costs ppc440_cost = {
435 COSTS_N_INSNS (3), /* mulsi */
436 COSTS_N_INSNS (2), /* mulsi_const */
437 COSTS_N_INSNS (2), /* mulsi_const9 */
438 COSTS_N_INSNS (3), /* muldi */
439 COSTS_N_INSNS (34), /* divsi */
440 COSTS_N_INSNS (34), /* divdi */
441 COSTS_N_INSNS (5), /* fp */
442 COSTS_N_INSNS (5), /* dmul */
443 COSTS_N_INSNS (19), /* sdiv */
444 COSTS_N_INSNS (33), /* ddiv */
445 32, /* cache line size */
446 32, /* l1 cache */
447 256, /* l2 cache */
448 1, /* streams */
451 /* Instruction costs on PPC476 processors. */
452 static const
453 struct processor_costs ppc476_cost = {
454 COSTS_N_INSNS (4), /* mulsi */
455 COSTS_N_INSNS (4), /* mulsi_const */
456 COSTS_N_INSNS (4), /* mulsi_const9 */
457 COSTS_N_INSNS (4), /* muldi */
458 COSTS_N_INSNS (11), /* divsi */
459 COSTS_N_INSNS (11), /* divdi */
460 COSTS_N_INSNS (6), /* fp */
461 COSTS_N_INSNS (6), /* dmul */
462 COSTS_N_INSNS (19), /* sdiv */
463 COSTS_N_INSNS (33), /* ddiv */
464 32, /* l1 cache line size */
465 32, /* l1 cache */
466 512, /* l2 cache */
467 1, /* streams */
470 /* Instruction costs on PPC601 processors. */
471 static const
472 struct processor_costs ppc601_cost = {
473 COSTS_N_INSNS (5), /* mulsi */
474 COSTS_N_INSNS (5), /* mulsi_const */
475 COSTS_N_INSNS (5), /* mulsi_const9 */
476 COSTS_N_INSNS (5), /* muldi */
477 COSTS_N_INSNS (36), /* divsi */
478 COSTS_N_INSNS (36), /* divdi */
479 COSTS_N_INSNS (4), /* fp */
480 COSTS_N_INSNS (5), /* dmul */
481 COSTS_N_INSNS (17), /* sdiv */
482 COSTS_N_INSNS (31), /* ddiv */
483 32, /* cache line size */
484 32, /* l1 cache */
485 256, /* l2 cache */
486 1, /* streams */
489 /* Instruction costs on PPC603 processors. */
490 static const
491 struct processor_costs ppc603_cost = {
492 COSTS_N_INSNS (5), /* mulsi */
493 COSTS_N_INSNS (3), /* mulsi_const */
494 COSTS_N_INSNS (2), /* mulsi_const9 */
495 COSTS_N_INSNS (5), /* muldi */
496 COSTS_N_INSNS (37), /* divsi */
497 COSTS_N_INSNS (37), /* divdi */
498 COSTS_N_INSNS (3), /* fp */
499 COSTS_N_INSNS (4), /* dmul */
500 COSTS_N_INSNS (18), /* sdiv */
501 COSTS_N_INSNS (33), /* ddiv */
502 32, /* cache line size */
503 8, /* l1 cache */
504 64, /* l2 cache */
505 1, /* streams */
508 /* Instruction costs on PPC604 processors. */
509 static const
510 struct processor_costs ppc604_cost = {
511 COSTS_N_INSNS (4), /* mulsi */
512 COSTS_N_INSNS (4), /* mulsi_const */
513 COSTS_N_INSNS (4), /* mulsi_const9 */
514 COSTS_N_INSNS (4), /* muldi */
515 COSTS_N_INSNS (20), /* divsi */
516 COSTS_N_INSNS (20), /* divdi */
517 COSTS_N_INSNS (3), /* fp */
518 COSTS_N_INSNS (3), /* dmul */
519 COSTS_N_INSNS (18), /* sdiv */
520 COSTS_N_INSNS (32), /* ddiv */
521 32, /* cache line size */
522 16, /* l1 cache */
523 512, /* l2 cache */
524 1, /* streams */
527 /* Instruction costs on PPC604e processors. */
528 static const
529 struct processor_costs ppc604e_cost = {
530 COSTS_N_INSNS (2), /* mulsi */
531 COSTS_N_INSNS (2), /* mulsi_const */
532 COSTS_N_INSNS (2), /* mulsi_const9 */
533 COSTS_N_INSNS (2), /* muldi */
534 COSTS_N_INSNS (20), /* divsi */
535 COSTS_N_INSNS (20), /* divdi */
536 COSTS_N_INSNS (3), /* fp */
537 COSTS_N_INSNS (3), /* dmul */
538 COSTS_N_INSNS (18), /* sdiv */
539 COSTS_N_INSNS (32), /* ddiv */
540 32, /* cache line size */
541 32, /* l1 cache */
542 1024, /* l2 cache */
543 1, /* streams */
546 /* Instruction costs on PPC620 processors. */
547 static const
548 struct processor_costs ppc620_cost = {
549 COSTS_N_INSNS (5), /* mulsi */
550 COSTS_N_INSNS (4), /* mulsi_const */
551 COSTS_N_INSNS (3), /* mulsi_const9 */
552 COSTS_N_INSNS (7), /* muldi */
553 COSTS_N_INSNS (21), /* divsi */
554 COSTS_N_INSNS (37), /* divdi */
555 COSTS_N_INSNS (3), /* fp */
556 COSTS_N_INSNS (3), /* dmul */
557 COSTS_N_INSNS (18), /* sdiv */
558 COSTS_N_INSNS (32), /* ddiv */
559 128, /* cache line size */
560 32, /* l1 cache */
561 1024, /* l2 cache */
562 1, /* streams */
565 /* Instruction costs on PPC630 processors. */
566 static const
567 struct processor_costs ppc630_cost = {
568 COSTS_N_INSNS (5), /* mulsi */
569 COSTS_N_INSNS (4), /* mulsi_const */
570 COSTS_N_INSNS (3), /* mulsi_const9 */
571 COSTS_N_INSNS (7), /* muldi */
572 COSTS_N_INSNS (21), /* divsi */
573 COSTS_N_INSNS (37), /* divdi */
574 COSTS_N_INSNS (3), /* fp */
575 COSTS_N_INSNS (3), /* dmul */
576 COSTS_N_INSNS (17), /* sdiv */
577 COSTS_N_INSNS (21), /* ddiv */
578 128, /* cache line size */
579 64, /* l1 cache */
580 1024, /* l2 cache */
581 1, /* streams */
584 /* Instruction costs on Cell processor. */
585 /* COSTS_N_INSNS (1) ~ one add. */
586 static const
587 struct processor_costs ppccell_cost = {
588 COSTS_N_INSNS (9/2)+2, /* mulsi */
589 COSTS_N_INSNS (6/2), /* mulsi_const */
590 COSTS_N_INSNS (6/2), /* mulsi_const9 */
591 COSTS_N_INSNS (15/2)+2, /* muldi */
592 COSTS_N_INSNS (38/2), /* divsi */
593 COSTS_N_INSNS (70/2), /* divdi */
594 COSTS_N_INSNS (10/2), /* fp */
595 COSTS_N_INSNS (10/2), /* dmul */
596 COSTS_N_INSNS (74/2), /* sdiv */
597 COSTS_N_INSNS (74/2), /* ddiv */
598 128, /* cache line size */
599 32, /* l1 cache */
600 512, /* l2 cache */
601 6, /* streams */
604 /* Instruction costs on PPC750 and PPC7400 processors. */
605 static const
606 struct processor_costs ppc750_cost = {
607 COSTS_N_INSNS (5), /* mulsi */
608 COSTS_N_INSNS (3), /* mulsi_const */
609 COSTS_N_INSNS (2), /* mulsi_const9 */
610 COSTS_N_INSNS (5), /* muldi */
611 COSTS_N_INSNS (17), /* divsi */
612 COSTS_N_INSNS (17), /* divdi */
613 COSTS_N_INSNS (3), /* fp */
614 COSTS_N_INSNS (3), /* dmul */
615 COSTS_N_INSNS (17), /* sdiv */
616 COSTS_N_INSNS (31), /* ddiv */
617 32, /* cache line size */
618 32, /* l1 cache */
619 512, /* l2 cache */
620 1, /* streams */
623 /* Instruction costs on PPC7450 processors. */
624 static const
625 struct processor_costs ppc7450_cost = {
626 COSTS_N_INSNS (4), /* mulsi */
627 COSTS_N_INSNS (3), /* mulsi_const */
628 COSTS_N_INSNS (3), /* mulsi_const9 */
629 COSTS_N_INSNS (4), /* muldi */
630 COSTS_N_INSNS (23), /* divsi */
631 COSTS_N_INSNS (23), /* divdi */
632 COSTS_N_INSNS (5), /* fp */
633 COSTS_N_INSNS (5), /* dmul */
634 COSTS_N_INSNS (21), /* sdiv */
635 COSTS_N_INSNS (35), /* ddiv */
636 32, /* cache line size */
637 32, /* l1 cache */
638 1024, /* l2 cache */
639 1, /* streams */
642 /* Instruction costs on PPC8540 processors. */
643 static const
644 struct processor_costs ppc8540_cost = {
645 COSTS_N_INSNS (4), /* mulsi */
646 COSTS_N_INSNS (4), /* mulsi_const */
647 COSTS_N_INSNS (4), /* mulsi_const9 */
648 COSTS_N_INSNS (4), /* muldi */
649 COSTS_N_INSNS (19), /* divsi */
650 COSTS_N_INSNS (19), /* divdi */
651 COSTS_N_INSNS (4), /* fp */
652 COSTS_N_INSNS (4), /* dmul */
653 COSTS_N_INSNS (29), /* sdiv */
654 COSTS_N_INSNS (29), /* ddiv */
655 32, /* cache line size */
656 32, /* l1 cache */
657 256, /* l2 cache */
658 1, /* prefetch streams /*/
661 /* Instruction costs on E300C2 and E300C3 cores. */
662 static const
663 struct processor_costs ppce300c2c3_cost = {
664 COSTS_N_INSNS (4), /* mulsi */
665 COSTS_N_INSNS (4), /* mulsi_const */
666 COSTS_N_INSNS (4), /* mulsi_const9 */
667 COSTS_N_INSNS (4), /* muldi */
668 COSTS_N_INSNS (19), /* divsi */
669 COSTS_N_INSNS (19), /* divdi */
670 COSTS_N_INSNS (3), /* fp */
671 COSTS_N_INSNS (4), /* dmul */
672 COSTS_N_INSNS (18), /* sdiv */
673 COSTS_N_INSNS (33), /* ddiv */
675 16, /* l1 cache */
676 16, /* l2 cache */
677 1, /* prefetch streams /*/
680 /* Instruction costs on PPCE500MC processors. */
681 static const
682 struct processor_costs ppce500mc_cost = {
683 COSTS_N_INSNS (4), /* mulsi */
684 COSTS_N_INSNS (4), /* mulsi_const */
685 COSTS_N_INSNS (4), /* mulsi_const9 */
686 COSTS_N_INSNS (4), /* muldi */
687 COSTS_N_INSNS (14), /* divsi */
688 COSTS_N_INSNS (14), /* divdi */
689 COSTS_N_INSNS (8), /* fp */
690 COSTS_N_INSNS (10), /* dmul */
691 COSTS_N_INSNS (36), /* sdiv */
692 COSTS_N_INSNS (66), /* ddiv */
693 64, /* cache line size */
694 32, /* l1 cache */
695 128, /* l2 cache */
696 1, /* prefetch streams /*/
699 /* Instruction costs on PPCE500MC64 processors. */
700 static const
701 struct processor_costs ppce500mc64_cost = {
702 COSTS_N_INSNS (4), /* mulsi */
703 COSTS_N_INSNS (4), /* mulsi_const */
704 COSTS_N_INSNS (4), /* mulsi_const9 */
705 COSTS_N_INSNS (4), /* muldi */
706 COSTS_N_INSNS (14), /* divsi */
707 COSTS_N_INSNS (14), /* divdi */
708 COSTS_N_INSNS (4), /* fp */
709 COSTS_N_INSNS (10), /* dmul */
710 COSTS_N_INSNS (36), /* sdiv */
711 COSTS_N_INSNS (66), /* ddiv */
712 64, /* cache line size */
713 32, /* l1 cache */
714 128, /* l2 cache */
715 1, /* prefetch streams /*/
718 /* Instruction costs on PPCE5500 processors. */
719 static const
720 struct processor_costs ppce5500_cost = {
721 COSTS_N_INSNS (5), /* mulsi */
722 COSTS_N_INSNS (5), /* mulsi_const */
723 COSTS_N_INSNS (4), /* mulsi_const9 */
724 COSTS_N_INSNS (5), /* muldi */
725 COSTS_N_INSNS (14), /* divsi */
726 COSTS_N_INSNS (14), /* divdi */
727 COSTS_N_INSNS (7), /* fp */
728 COSTS_N_INSNS (10), /* dmul */
729 COSTS_N_INSNS (36), /* sdiv */
730 COSTS_N_INSNS (66), /* ddiv */
731 64, /* cache line size */
732 32, /* l1 cache */
733 128, /* l2 cache */
734 1, /* prefetch streams /*/
737 /* Instruction costs on PPCE6500 processors. */
738 static const
739 struct processor_costs ppce6500_cost = {
740 COSTS_N_INSNS (5), /* mulsi */
741 COSTS_N_INSNS (5), /* mulsi_const */
742 COSTS_N_INSNS (4), /* mulsi_const9 */
743 COSTS_N_INSNS (5), /* muldi */
744 COSTS_N_INSNS (14), /* divsi */
745 COSTS_N_INSNS (14), /* divdi */
746 COSTS_N_INSNS (7), /* fp */
747 COSTS_N_INSNS (10), /* dmul */
748 COSTS_N_INSNS (36), /* sdiv */
749 COSTS_N_INSNS (66), /* ddiv */
750 64, /* cache line size */
751 32, /* l1 cache */
752 128, /* l2 cache */
753 1, /* prefetch streams /*/
756 /* Instruction costs on AppliedMicro Titan processors. */
757 static const
758 struct processor_costs titan_cost = {
759 COSTS_N_INSNS (5), /* mulsi */
760 COSTS_N_INSNS (5), /* mulsi_const */
761 COSTS_N_INSNS (5), /* mulsi_const9 */
762 COSTS_N_INSNS (5), /* muldi */
763 COSTS_N_INSNS (18), /* divsi */
764 COSTS_N_INSNS (18), /* divdi */
765 COSTS_N_INSNS (10), /* fp */
766 COSTS_N_INSNS (10), /* dmul */
767 COSTS_N_INSNS (46), /* sdiv */
768 COSTS_N_INSNS (72), /* ddiv */
769 32, /* cache line size */
770 32, /* l1 cache */
771 512, /* l2 cache */
772 1, /* prefetch streams /*/
775 /* Instruction costs on POWER4 and POWER5 processors. */
776 static const
777 struct processor_costs power4_cost = {
778 COSTS_N_INSNS (3), /* mulsi */
779 COSTS_N_INSNS (2), /* mulsi_const */
780 COSTS_N_INSNS (2), /* mulsi_const9 */
781 COSTS_N_INSNS (4), /* muldi */
782 COSTS_N_INSNS (18), /* divsi */
783 COSTS_N_INSNS (34), /* divdi */
784 COSTS_N_INSNS (3), /* fp */
785 COSTS_N_INSNS (3), /* dmul */
786 COSTS_N_INSNS (17), /* sdiv */
787 COSTS_N_INSNS (17), /* ddiv */
788 128, /* cache line size */
789 32, /* l1 cache */
790 1024, /* l2 cache */
791 8, /* prefetch streams /*/
794 /* Instruction costs on POWER6 processors. */
795 static const
796 struct processor_costs power6_cost = {
797 COSTS_N_INSNS (8), /* mulsi */
798 COSTS_N_INSNS (8), /* mulsi_const */
799 COSTS_N_INSNS (8), /* mulsi_const9 */
800 COSTS_N_INSNS (8), /* muldi */
801 COSTS_N_INSNS (22), /* divsi */
802 COSTS_N_INSNS (28), /* divdi */
803 COSTS_N_INSNS (3), /* fp */
804 COSTS_N_INSNS (3), /* dmul */
805 COSTS_N_INSNS (13), /* sdiv */
806 COSTS_N_INSNS (16), /* ddiv */
807 128, /* cache line size */
808 64, /* l1 cache */
809 2048, /* l2 cache */
810 16, /* prefetch streams */
813 /* Instruction costs on POWER7 processors. */
814 static const
815 struct processor_costs power7_cost = {
816 COSTS_N_INSNS (2), /* mulsi */
817 COSTS_N_INSNS (2), /* mulsi_const */
818 COSTS_N_INSNS (2), /* mulsi_const9 */
819 COSTS_N_INSNS (2), /* muldi */
820 COSTS_N_INSNS (18), /* divsi */
821 COSTS_N_INSNS (34), /* divdi */
822 COSTS_N_INSNS (3), /* fp */
823 COSTS_N_INSNS (3), /* dmul */
824 COSTS_N_INSNS (13), /* sdiv */
825 COSTS_N_INSNS (16), /* ddiv */
826 128, /* cache line size */
827 32, /* l1 cache */
828 256, /* l2 cache */
829 12, /* prefetch streams */
832 /* Instruction costs on POWER A2 processors. */
833 static const
834 struct processor_costs ppca2_cost = {
835 COSTS_N_INSNS (16), /* mulsi */
836 COSTS_N_INSNS (16), /* mulsi_const */
837 COSTS_N_INSNS (16), /* mulsi_const9 */
838 COSTS_N_INSNS (16), /* muldi */
839 COSTS_N_INSNS (22), /* divsi */
840 COSTS_N_INSNS (28), /* divdi */
841 COSTS_N_INSNS (3), /* fp */
842 COSTS_N_INSNS (3), /* dmul */
843 COSTS_N_INSNS (59), /* sdiv */
844 COSTS_N_INSNS (72), /* ddiv */
846 16, /* l1 cache */
847 2048, /* l2 cache */
848 16, /* prefetch streams */
852 /* Table that classifies rs6000 builtin functions (pure, const, etc.). */
853 #undef RS6000_BUILTIN_1
854 #undef RS6000_BUILTIN_2
855 #undef RS6000_BUILTIN_3
856 #undef RS6000_BUILTIN_A
857 #undef RS6000_BUILTIN_D
858 #undef RS6000_BUILTIN_E
859 #undef RS6000_BUILTIN_P
860 #undef RS6000_BUILTIN_Q
861 #undef RS6000_BUILTIN_S
862 #undef RS6000_BUILTIN_X
864 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE) \
865 { NAME, ICODE, MASK, ATTR },
867 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE) \
868 { NAME, ICODE, MASK, ATTR },
870 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE) \
871 { NAME, ICODE, MASK, ATTR },
873 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE) \
874 { NAME, ICODE, MASK, ATTR },
876 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE) \
877 { NAME, ICODE, MASK, ATTR },
879 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE) \
880 { NAME, ICODE, MASK, ATTR },
882 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE) \
883 { NAME, ICODE, MASK, ATTR },
885 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE) \
886 { NAME, ICODE, MASK, ATTR },
888 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE) \
889 { NAME, ICODE, MASK, ATTR },
891 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE) \
892 { NAME, ICODE, MASK, ATTR },
894 struct rs6000_builtin_info_type {
895 const char *name;
896 const enum insn_code icode;
897 const HOST_WIDE_INT mask;
898 const unsigned attr;
901 static const struct rs6000_builtin_info_type rs6000_builtin_info[] =
903 #include "rs6000-builtin.def"
906 #undef RS6000_BUILTIN_1
907 #undef RS6000_BUILTIN_2
908 #undef RS6000_BUILTIN_3
909 #undef RS6000_BUILTIN_A
910 #undef RS6000_BUILTIN_D
911 #undef RS6000_BUILTIN_E
912 #undef RS6000_BUILTIN_P
913 #undef RS6000_BUILTIN_Q
914 #undef RS6000_BUILTIN_S
915 #undef RS6000_BUILTIN_X
917 /* Support for -mveclibabi=<xxx> to control which vector library to use. */
918 static tree (*rs6000_veclib_handler) (tree, tree, tree);
921 static bool rs6000_debug_legitimate_address_p (enum machine_mode, rtx, bool);
922 static bool spe_func_has_64bit_regs_p (void);
923 static struct machine_function * rs6000_init_machine_status (void);
924 static int rs6000_ra_ever_killed (void);
925 static tree rs6000_handle_longcall_attribute (tree *, tree, tree, int, bool *);
926 static tree rs6000_handle_altivec_attribute (tree *, tree, tree, int, bool *);
927 static tree rs6000_handle_struct_attribute (tree *, tree, tree, int, bool *);
928 static tree rs6000_builtin_vectorized_libmass (tree, tree, tree);
929 static rtx rs6000_emit_set_long_const (rtx, HOST_WIDE_INT, HOST_WIDE_INT);
930 static int rs6000_memory_move_cost (enum machine_mode, reg_class_t, bool);
931 static bool rs6000_debug_rtx_costs (rtx, int, int, int, int *, bool);
932 static int rs6000_debug_address_cost (rtx, enum machine_mode, addr_space_t,
933 bool);
934 static int rs6000_debug_adjust_cost (rtx, rtx, rtx, int);
935 static bool is_microcoded_insn (rtx);
936 static bool is_nonpipeline_insn (rtx);
937 static bool is_cracked_insn (rtx);
938 static bool is_load_insn (rtx, rtx *);
939 static bool is_store_insn (rtx, rtx *);
940 static bool set_to_load_agen (rtx,rtx);
941 static bool insn_terminates_group_p (rtx , enum group_termination);
942 static bool insn_must_be_first_in_group (rtx);
943 static bool insn_must_be_last_in_group (rtx);
944 static void altivec_init_builtins (void);
945 static tree builtin_function_type (enum machine_mode, enum machine_mode,
946 enum machine_mode, enum machine_mode,
947 enum rs6000_builtins, const char *name);
948 static void rs6000_common_init_builtins (void);
949 static void paired_init_builtins (void);
950 static rtx paired_expand_predicate_builtin (enum insn_code, tree, rtx);
951 static void spe_init_builtins (void);
952 static rtx spe_expand_predicate_builtin (enum insn_code, tree, rtx);
953 static rtx spe_expand_evsel_builtin (enum insn_code, tree, rtx);
954 static int rs6000_emit_int_cmove (rtx, rtx, rtx, rtx);
955 static rs6000_stack_t *rs6000_stack_info (void);
956 static void is_altivec_return_reg (rtx, void *);
957 int easy_vector_constant (rtx, enum machine_mode);
958 static rtx rs6000_debug_legitimize_address (rtx, rtx, enum machine_mode);
959 static rtx rs6000_legitimize_tls_address (rtx, enum tls_model);
960 static int rs6000_tls_symbol_ref_1 (rtx *, void *);
961 static int rs6000_get_some_local_dynamic_name_1 (rtx *, void *);
962 static rtx rs6000_darwin64_record_arg (CUMULATIVE_ARGS *, const_tree,
963 bool, bool);
964 #if TARGET_MACHO
965 static void macho_branch_islands (void);
966 #endif
967 static rtx rs6000_legitimize_reload_address (rtx, enum machine_mode, int, int,
968 int, int *);
969 static rtx rs6000_debug_legitimize_reload_address (rtx, enum machine_mode, int,
970 int, int, int *);
971 static bool rs6000_mode_dependent_address (const_rtx);
972 static bool rs6000_debug_mode_dependent_address (const_rtx);
973 static enum reg_class rs6000_secondary_reload_class (enum reg_class,
974 enum machine_mode, rtx);
975 static enum reg_class rs6000_debug_secondary_reload_class (enum reg_class,
976 enum machine_mode,
977 rtx);
978 static enum reg_class rs6000_preferred_reload_class (rtx, enum reg_class);
979 static enum reg_class rs6000_debug_preferred_reload_class (rtx,
980 enum reg_class);
981 static bool rs6000_secondary_memory_needed (enum reg_class, enum reg_class,
982 enum machine_mode);
983 static bool rs6000_debug_secondary_memory_needed (enum reg_class,
984 enum reg_class,
985 enum machine_mode);
986 static bool rs6000_cannot_change_mode_class (enum machine_mode,
987 enum machine_mode,
988 enum reg_class);
989 static bool rs6000_debug_cannot_change_mode_class (enum machine_mode,
990 enum machine_mode,
991 enum reg_class);
992 static bool rs6000_save_toc_in_prologue_p (void);
994 rtx (*rs6000_legitimize_reload_address_ptr) (rtx, enum machine_mode, int, int,
995 int, int *)
996 = rs6000_legitimize_reload_address;
998 static bool (*rs6000_mode_dependent_address_ptr) (const_rtx)
999 = rs6000_mode_dependent_address;
1001 enum reg_class (*rs6000_secondary_reload_class_ptr) (enum reg_class,
1002 enum machine_mode, rtx)
1003 = rs6000_secondary_reload_class;
1005 enum reg_class (*rs6000_preferred_reload_class_ptr) (rtx, enum reg_class)
1006 = rs6000_preferred_reload_class;
1008 bool (*rs6000_secondary_memory_needed_ptr) (enum reg_class, enum reg_class,
1009 enum machine_mode)
1010 = rs6000_secondary_memory_needed;
1012 bool (*rs6000_cannot_change_mode_class_ptr) (enum machine_mode,
1013 enum machine_mode,
1014 enum reg_class)
1015 = rs6000_cannot_change_mode_class;
1017 const int INSN_NOT_AVAILABLE = -1;
1019 static void rs6000_print_isa_options (FILE *, int, const char *,
1020 HOST_WIDE_INT);
1021 static void rs6000_print_builtin_options (FILE *, int, const char *,
1022 HOST_WIDE_INT);
1024 /* Hash table stuff for keeping track of TOC entries. */
1026 struct GTY(()) toc_hash_struct
1028 /* `key' will satisfy CONSTANT_P; in fact, it will satisfy
1029 ASM_OUTPUT_SPECIAL_POOL_ENTRY_P. */
1030 rtx key;
1031 enum machine_mode key_mode;
1032 int labelno;
1035 static GTY ((param_is (struct toc_hash_struct))) htab_t toc_hash_table;
1037 /* Hash table to keep track of the argument types for builtin functions. */
1039 struct GTY(()) builtin_hash_struct
1041 tree type;
1042 enum machine_mode mode[4]; /* return value + 3 arguments. */
1043 unsigned char uns_p[4]; /* and whether the types are unsigned. */
1046 static GTY ((param_is (struct builtin_hash_struct))) htab_t builtin_hash_table;
1049 /* Default register names. */
1050 char rs6000_reg_names[][8] =
1052 "0", "1", "2", "3", "4", "5", "6", "7",
1053 "8", "9", "10", "11", "12", "13", "14", "15",
1054 "16", "17", "18", "19", "20", "21", "22", "23",
1055 "24", "25", "26", "27", "28", "29", "30", "31",
1056 "0", "1", "2", "3", "4", "5", "6", "7",
1057 "8", "9", "10", "11", "12", "13", "14", "15",
1058 "16", "17", "18", "19", "20", "21", "22", "23",
1059 "24", "25", "26", "27", "28", "29", "30", "31",
1060 "mq", "lr", "ctr","ap",
1061 "0", "1", "2", "3", "4", "5", "6", "7",
1062 "ca",
1063 /* AltiVec registers. */
1064 "0", "1", "2", "3", "4", "5", "6", "7",
1065 "8", "9", "10", "11", "12", "13", "14", "15",
1066 "16", "17", "18", "19", "20", "21", "22", "23",
1067 "24", "25", "26", "27", "28", "29", "30", "31",
1068 "vrsave", "vscr",
1069 /* SPE registers. */
1070 "spe_acc", "spefscr",
1071 /* Soft frame pointer. */
1072 "sfp"
1075 #ifdef TARGET_REGNAMES
1076 static const char alt_reg_names[][8] =
1078 "%r0", "%r1", "%r2", "%r3", "%r4", "%r5", "%r6", "%r7",
1079 "%r8", "%r9", "%r10", "%r11", "%r12", "%r13", "%r14", "%r15",
1080 "%r16", "%r17", "%r18", "%r19", "%r20", "%r21", "%r22", "%r23",
1081 "%r24", "%r25", "%r26", "%r27", "%r28", "%r29", "%r30", "%r31",
1082 "%f0", "%f1", "%f2", "%f3", "%f4", "%f5", "%f6", "%f7",
1083 "%f8", "%f9", "%f10", "%f11", "%f12", "%f13", "%f14", "%f15",
1084 "%f16", "%f17", "%f18", "%f19", "%f20", "%f21", "%f22", "%f23",
1085 "%f24", "%f25", "%f26", "%f27", "%f28", "%f29", "%f30", "%f31",
1086 "mq", "lr", "ctr", "ap",
1087 "%cr0", "%cr1", "%cr2", "%cr3", "%cr4", "%cr5", "%cr6", "%cr7",
1088 "ca",
1089 /* AltiVec registers. */
1090 "%v0", "%v1", "%v2", "%v3", "%v4", "%v5", "%v6", "%v7",
1091 "%v8", "%v9", "%v10", "%v11", "%v12", "%v13", "%v14", "%v15",
1092 "%v16", "%v17", "%v18", "%v19", "%v20", "%v21", "%v22", "%v23",
1093 "%v24", "%v25", "%v26", "%v27", "%v28", "%v29", "%v30", "%v31",
1094 "vrsave", "vscr",
1095 /* SPE registers. */
1096 "spe_acc", "spefscr",
1097 /* Soft frame pointer. */
1098 "sfp"
1100 #endif
1102 /* Table of valid machine attributes. */
1104 static const struct attribute_spec rs6000_attribute_table[] =
1106 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
1107 affects_type_identity } */
1108 { "altivec", 1, 1, false, true, false, rs6000_handle_altivec_attribute,
1109 false },
1110 { "longcall", 0, 0, false, true, true, rs6000_handle_longcall_attribute,
1111 false },
1112 { "shortcall", 0, 0, false, true, true, rs6000_handle_longcall_attribute,
1113 false },
1114 { "ms_struct", 0, 0, false, false, false, rs6000_handle_struct_attribute,
1115 false },
1116 { "gcc_struct", 0, 0, false, false, false, rs6000_handle_struct_attribute,
1117 false },
1118 #ifdef SUBTARGET_ATTRIBUTE_TABLE
1119 SUBTARGET_ATTRIBUTE_TABLE,
1120 #endif
1121 { NULL, 0, 0, false, false, false, NULL, false }
1124 #ifndef TARGET_PROFILE_KERNEL
1125 #define TARGET_PROFILE_KERNEL 0
1126 #endif
1128 /* The VRSAVE bitmask puts bit %v0 as the most significant bit. */
1129 #define ALTIVEC_REG_BIT(REGNO) (0x80000000 >> ((REGNO) - FIRST_ALTIVEC_REGNO))
1131 /* Initialize the GCC target structure. */
1132 #undef TARGET_ATTRIBUTE_TABLE
1133 #define TARGET_ATTRIBUTE_TABLE rs6000_attribute_table
1134 #undef TARGET_SET_DEFAULT_TYPE_ATTRIBUTES
1135 #define TARGET_SET_DEFAULT_TYPE_ATTRIBUTES rs6000_set_default_type_attributes
1136 #undef TARGET_ATTRIBUTE_TAKES_IDENTIFIER_P
1137 #define TARGET_ATTRIBUTE_TAKES_IDENTIFIER_P rs6000_attribute_takes_identifier_p
1139 #undef TARGET_ASM_ALIGNED_DI_OP
1140 #define TARGET_ASM_ALIGNED_DI_OP DOUBLE_INT_ASM_OP
1142 /* Default unaligned ops are only provided for ELF. Find the ops needed
1143 for non-ELF systems. */
1144 #ifndef OBJECT_FORMAT_ELF
1145 #if TARGET_XCOFF
1146 /* For XCOFF. rs6000_assemble_integer will handle unaligned DIs on
1147 64-bit targets. */
1148 #undef TARGET_ASM_UNALIGNED_HI_OP
1149 #define TARGET_ASM_UNALIGNED_HI_OP "\t.vbyte\t2,"
1150 #undef TARGET_ASM_UNALIGNED_SI_OP
1151 #define TARGET_ASM_UNALIGNED_SI_OP "\t.vbyte\t4,"
1152 #undef TARGET_ASM_UNALIGNED_DI_OP
1153 #define TARGET_ASM_UNALIGNED_DI_OP "\t.vbyte\t8,"
1154 #else
1155 /* For Darwin. */
1156 #undef TARGET_ASM_UNALIGNED_HI_OP
1157 #define TARGET_ASM_UNALIGNED_HI_OP "\t.short\t"
1158 #undef TARGET_ASM_UNALIGNED_SI_OP
1159 #define TARGET_ASM_UNALIGNED_SI_OP "\t.long\t"
1160 #undef TARGET_ASM_UNALIGNED_DI_OP
1161 #define TARGET_ASM_UNALIGNED_DI_OP "\t.quad\t"
1162 #undef TARGET_ASM_ALIGNED_DI_OP
1163 #define TARGET_ASM_ALIGNED_DI_OP "\t.quad\t"
1164 #endif
1165 #endif
1167 /* This hook deals with fixups for relocatable code and DI-mode objects
1168 in 64-bit code. */
1169 #undef TARGET_ASM_INTEGER
1170 #define TARGET_ASM_INTEGER rs6000_assemble_integer
1172 #if defined (HAVE_GAS_HIDDEN) && !TARGET_MACHO
1173 #undef TARGET_ASM_ASSEMBLE_VISIBILITY
1174 #define TARGET_ASM_ASSEMBLE_VISIBILITY rs6000_assemble_visibility
1175 #endif
1177 #undef TARGET_SET_UP_BY_PROLOGUE
1178 #define TARGET_SET_UP_BY_PROLOGUE rs6000_set_up_by_prologue
1180 #undef TARGET_HAVE_TLS
1181 #define TARGET_HAVE_TLS HAVE_AS_TLS
1183 #undef TARGET_CANNOT_FORCE_CONST_MEM
1184 #define TARGET_CANNOT_FORCE_CONST_MEM rs6000_cannot_force_const_mem
1186 #undef TARGET_DELEGITIMIZE_ADDRESS
1187 #define TARGET_DELEGITIMIZE_ADDRESS rs6000_delegitimize_address
1189 #undef TARGET_CONST_NOT_OK_FOR_DEBUG_P
1190 #define TARGET_CONST_NOT_OK_FOR_DEBUG_P rs6000_const_not_ok_for_debug_p
1192 #undef TARGET_ASM_FUNCTION_PROLOGUE
1193 #define TARGET_ASM_FUNCTION_PROLOGUE rs6000_output_function_prologue
1194 #undef TARGET_ASM_FUNCTION_EPILOGUE
1195 #define TARGET_ASM_FUNCTION_EPILOGUE rs6000_output_function_epilogue
1197 #undef TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA
1198 #define TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA rs6000_output_addr_const_extra
1200 #undef TARGET_LEGITIMIZE_ADDRESS
1201 #define TARGET_LEGITIMIZE_ADDRESS rs6000_legitimize_address
1203 #undef TARGET_SCHED_VARIABLE_ISSUE
1204 #define TARGET_SCHED_VARIABLE_ISSUE rs6000_variable_issue
1206 #undef TARGET_SCHED_ISSUE_RATE
1207 #define TARGET_SCHED_ISSUE_RATE rs6000_issue_rate
1208 #undef TARGET_SCHED_ADJUST_COST
1209 #define TARGET_SCHED_ADJUST_COST rs6000_adjust_cost
1210 #undef TARGET_SCHED_ADJUST_PRIORITY
1211 #define TARGET_SCHED_ADJUST_PRIORITY rs6000_adjust_priority
1212 #undef TARGET_SCHED_IS_COSTLY_DEPENDENCE
1213 #define TARGET_SCHED_IS_COSTLY_DEPENDENCE rs6000_is_costly_dependence
1214 #undef TARGET_SCHED_INIT
1215 #define TARGET_SCHED_INIT rs6000_sched_init
1216 #undef TARGET_SCHED_FINISH
1217 #define TARGET_SCHED_FINISH rs6000_sched_finish
1218 #undef TARGET_SCHED_REORDER
1219 #define TARGET_SCHED_REORDER rs6000_sched_reorder
1220 #undef TARGET_SCHED_REORDER2
1221 #define TARGET_SCHED_REORDER2 rs6000_sched_reorder2
1223 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD
1224 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD rs6000_use_sched_lookahead
1226 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD_GUARD
1227 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD_GUARD rs6000_use_sched_lookahead_guard
1229 #undef TARGET_SCHED_ALLOC_SCHED_CONTEXT
1230 #define TARGET_SCHED_ALLOC_SCHED_CONTEXT rs6000_alloc_sched_context
1231 #undef TARGET_SCHED_INIT_SCHED_CONTEXT
1232 #define TARGET_SCHED_INIT_SCHED_CONTEXT rs6000_init_sched_context
1233 #undef TARGET_SCHED_SET_SCHED_CONTEXT
1234 #define TARGET_SCHED_SET_SCHED_CONTEXT rs6000_set_sched_context
1235 #undef TARGET_SCHED_FREE_SCHED_CONTEXT
1236 #define TARGET_SCHED_FREE_SCHED_CONTEXT rs6000_free_sched_context
1238 #undef TARGET_VECTORIZE_BUILTIN_MASK_FOR_LOAD
1239 #define TARGET_VECTORIZE_BUILTIN_MASK_FOR_LOAD rs6000_builtin_mask_for_load
1240 #undef TARGET_VECTORIZE_SUPPORT_VECTOR_MISALIGNMENT
1241 #define TARGET_VECTORIZE_SUPPORT_VECTOR_MISALIGNMENT \
1242 rs6000_builtin_support_vector_misalignment
1243 #undef TARGET_VECTORIZE_VECTOR_ALIGNMENT_REACHABLE
1244 #define TARGET_VECTORIZE_VECTOR_ALIGNMENT_REACHABLE rs6000_vector_alignment_reachable
1245 #undef TARGET_VECTORIZE_BUILTIN_VECTORIZATION_COST
1246 #define TARGET_VECTORIZE_BUILTIN_VECTORIZATION_COST \
1247 rs6000_builtin_vectorization_cost
1248 #undef TARGET_VECTORIZE_PREFERRED_SIMD_MODE
1249 #define TARGET_VECTORIZE_PREFERRED_SIMD_MODE \
1250 rs6000_preferred_simd_mode
1251 #undef TARGET_VECTORIZE_INIT_COST
1252 #define TARGET_VECTORIZE_INIT_COST rs6000_init_cost
1253 #undef TARGET_VECTORIZE_ADD_STMT_COST
1254 #define TARGET_VECTORIZE_ADD_STMT_COST rs6000_add_stmt_cost
1255 #undef TARGET_VECTORIZE_FINISH_COST
1256 #define TARGET_VECTORIZE_FINISH_COST rs6000_finish_cost
1257 #undef TARGET_VECTORIZE_DESTROY_COST_DATA
1258 #define TARGET_VECTORIZE_DESTROY_COST_DATA rs6000_destroy_cost_data
1260 #undef TARGET_INIT_BUILTINS
1261 #define TARGET_INIT_BUILTINS rs6000_init_builtins
1262 #undef TARGET_BUILTIN_DECL
1263 #define TARGET_BUILTIN_DECL rs6000_builtin_decl
1265 #undef TARGET_EXPAND_BUILTIN
1266 #define TARGET_EXPAND_BUILTIN rs6000_expand_builtin
1268 #undef TARGET_MANGLE_TYPE
1269 #define TARGET_MANGLE_TYPE rs6000_mangle_type
1271 #undef TARGET_INIT_LIBFUNCS
1272 #define TARGET_INIT_LIBFUNCS rs6000_init_libfuncs
1274 #if TARGET_MACHO
1275 #undef TARGET_BINDS_LOCAL_P
1276 #define TARGET_BINDS_LOCAL_P darwin_binds_local_p
1277 #endif
1279 #undef TARGET_MS_BITFIELD_LAYOUT_P
1280 #define TARGET_MS_BITFIELD_LAYOUT_P rs6000_ms_bitfield_layout_p
1282 #undef TARGET_ASM_OUTPUT_MI_THUNK
1283 #define TARGET_ASM_OUTPUT_MI_THUNK rs6000_output_mi_thunk
1285 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
1286 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK hook_bool_const_tree_hwi_hwi_const_tree_true
1288 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
1289 #define TARGET_FUNCTION_OK_FOR_SIBCALL rs6000_function_ok_for_sibcall
1291 #undef TARGET_INVALID_WITHIN_DOLOOP
1292 #define TARGET_INVALID_WITHIN_DOLOOP rs6000_invalid_within_doloop
1294 #undef TARGET_REGISTER_MOVE_COST
1295 #define TARGET_REGISTER_MOVE_COST rs6000_register_move_cost
1296 #undef TARGET_MEMORY_MOVE_COST
1297 #define TARGET_MEMORY_MOVE_COST rs6000_memory_move_cost
1298 #undef TARGET_RTX_COSTS
1299 #define TARGET_RTX_COSTS rs6000_rtx_costs
1300 #undef TARGET_ADDRESS_COST
1301 #define TARGET_ADDRESS_COST hook_int_rtx_mode_as_bool_0
1303 #undef TARGET_DWARF_REGISTER_SPAN
1304 #define TARGET_DWARF_REGISTER_SPAN rs6000_dwarf_register_span
1306 #undef TARGET_INIT_DWARF_REG_SIZES_EXTRA
1307 #define TARGET_INIT_DWARF_REG_SIZES_EXTRA rs6000_init_dwarf_reg_sizes_extra
1309 #undef TARGET_MEMBER_TYPE_FORCES_BLK
1310 #define TARGET_MEMBER_TYPE_FORCES_BLK rs6000_member_type_forces_blk
1312 /* On rs6000, function arguments are promoted, as are function return
1313 values. */
1314 #undef TARGET_PROMOTE_FUNCTION_MODE
1315 #define TARGET_PROMOTE_FUNCTION_MODE default_promote_function_mode_always_promote
1317 #undef TARGET_RETURN_IN_MEMORY
1318 #define TARGET_RETURN_IN_MEMORY rs6000_return_in_memory
1320 #undef TARGET_SETUP_INCOMING_VARARGS
1321 #define TARGET_SETUP_INCOMING_VARARGS setup_incoming_varargs
1323 /* Always strict argument naming on rs6000. */
1324 #undef TARGET_STRICT_ARGUMENT_NAMING
1325 #define TARGET_STRICT_ARGUMENT_NAMING hook_bool_CUMULATIVE_ARGS_true
1326 #undef TARGET_PRETEND_OUTGOING_VARARGS_NAMED
1327 #define TARGET_PRETEND_OUTGOING_VARARGS_NAMED hook_bool_CUMULATIVE_ARGS_true
1328 #undef TARGET_SPLIT_COMPLEX_ARG
1329 #define TARGET_SPLIT_COMPLEX_ARG hook_bool_const_tree_true
1330 #undef TARGET_MUST_PASS_IN_STACK
1331 #define TARGET_MUST_PASS_IN_STACK rs6000_must_pass_in_stack
1332 #undef TARGET_PASS_BY_REFERENCE
1333 #define TARGET_PASS_BY_REFERENCE rs6000_pass_by_reference
1334 #undef TARGET_ARG_PARTIAL_BYTES
1335 #define TARGET_ARG_PARTIAL_BYTES rs6000_arg_partial_bytes
1336 #undef TARGET_FUNCTION_ARG_ADVANCE
1337 #define TARGET_FUNCTION_ARG_ADVANCE rs6000_function_arg_advance
1338 #undef TARGET_FUNCTION_ARG
1339 #define TARGET_FUNCTION_ARG rs6000_function_arg
1340 #undef TARGET_FUNCTION_ARG_BOUNDARY
1341 #define TARGET_FUNCTION_ARG_BOUNDARY rs6000_function_arg_boundary
1343 #undef TARGET_BUILD_BUILTIN_VA_LIST
1344 #define TARGET_BUILD_BUILTIN_VA_LIST rs6000_build_builtin_va_list
1346 #undef TARGET_EXPAND_BUILTIN_VA_START
1347 #define TARGET_EXPAND_BUILTIN_VA_START rs6000_va_start
1349 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
1350 #define TARGET_GIMPLIFY_VA_ARG_EXPR rs6000_gimplify_va_arg
1352 #undef TARGET_EH_RETURN_FILTER_MODE
1353 #define TARGET_EH_RETURN_FILTER_MODE rs6000_eh_return_filter_mode
1355 #undef TARGET_SCALAR_MODE_SUPPORTED_P
1356 #define TARGET_SCALAR_MODE_SUPPORTED_P rs6000_scalar_mode_supported_p
1358 #undef TARGET_VECTOR_MODE_SUPPORTED_P
1359 #define TARGET_VECTOR_MODE_SUPPORTED_P rs6000_vector_mode_supported_p
1361 #undef TARGET_INVALID_ARG_FOR_UNPROTOTYPED_FN
1362 #define TARGET_INVALID_ARG_FOR_UNPROTOTYPED_FN invalid_arg_for_unprototyped_fn
1364 #undef TARGET_ASM_LOOP_ALIGN_MAX_SKIP
1365 #define TARGET_ASM_LOOP_ALIGN_MAX_SKIP rs6000_loop_align_max_skip
1367 #undef TARGET_OPTION_OVERRIDE
1368 #define TARGET_OPTION_OVERRIDE rs6000_option_override
1370 #undef TARGET_VECTORIZE_BUILTIN_VECTORIZED_FUNCTION
1371 #define TARGET_VECTORIZE_BUILTIN_VECTORIZED_FUNCTION \
1372 rs6000_builtin_vectorized_function
1374 #if !TARGET_MACHO
1375 #undef TARGET_STACK_PROTECT_FAIL
1376 #define TARGET_STACK_PROTECT_FAIL rs6000_stack_protect_fail
1377 #endif
1379 /* MPC604EUM 3.5.2 Weak Consistency between Multiple Processors
1380 The PowerPC architecture requires only weak consistency among
1381 processors--that is, memory accesses between processors need not be
1382 sequentially consistent and memory accesses among processors can occur
1383 in any order. The ability to order memory accesses weakly provides
1384 opportunities for more efficient use of the system bus. Unless a
1385 dependency exists, the 604e allows read operations to precede store
1386 operations. */
1387 #undef TARGET_RELAXED_ORDERING
1388 #define TARGET_RELAXED_ORDERING true
1390 #ifdef HAVE_AS_TLS
1391 #undef TARGET_ASM_OUTPUT_DWARF_DTPREL
1392 #define TARGET_ASM_OUTPUT_DWARF_DTPREL rs6000_output_dwarf_dtprel
1393 #endif
1395 /* Use a 32-bit anchor range. This leads to sequences like:
1397 addis tmp,anchor,high
1398 add dest,tmp,low
1400 where tmp itself acts as an anchor, and can be shared between
1401 accesses to the same 64k page. */
1402 #undef TARGET_MIN_ANCHOR_OFFSET
1403 #define TARGET_MIN_ANCHOR_OFFSET -0x7fffffff - 1
1404 #undef TARGET_MAX_ANCHOR_OFFSET
1405 #define TARGET_MAX_ANCHOR_OFFSET 0x7fffffff
1406 #undef TARGET_USE_BLOCKS_FOR_CONSTANT_P
1407 #define TARGET_USE_BLOCKS_FOR_CONSTANT_P rs6000_use_blocks_for_constant_p
1409 #undef TARGET_BUILTIN_RECIPROCAL
1410 #define TARGET_BUILTIN_RECIPROCAL rs6000_builtin_reciprocal
1412 #undef TARGET_EXPAND_TO_RTL_HOOK
1413 #define TARGET_EXPAND_TO_RTL_HOOK rs6000_alloc_sdmode_stack_slot
1415 #undef TARGET_INSTANTIATE_DECLS
1416 #define TARGET_INSTANTIATE_DECLS rs6000_instantiate_decls
1418 #undef TARGET_SECONDARY_RELOAD
1419 #define TARGET_SECONDARY_RELOAD rs6000_secondary_reload
1421 #undef TARGET_LEGITIMATE_ADDRESS_P
1422 #define TARGET_LEGITIMATE_ADDRESS_P rs6000_legitimate_address_p
1424 #undef TARGET_MODE_DEPENDENT_ADDRESS_P
1425 #define TARGET_MODE_DEPENDENT_ADDRESS_P rs6000_mode_dependent_address_p
1427 #undef TARGET_CAN_ELIMINATE
1428 #define TARGET_CAN_ELIMINATE rs6000_can_eliminate
1430 #undef TARGET_CONDITIONAL_REGISTER_USAGE
1431 #define TARGET_CONDITIONAL_REGISTER_USAGE rs6000_conditional_register_usage
1433 #undef TARGET_TRAMPOLINE_INIT
1434 #define TARGET_TRAMPOLINE_INIT rs6000_trampoline_init
1436 #undef TARGET_FUNCTION_VALUE
1437 #define TARGET_FUNCTION_VALUE rs6000_function_value
1439 #undef TARGET_OPTION_VALID_ATTRIBUTE_P
1440 #define TARGET_OPTION_VALID_ATTRIBUTE_P rs6000_valid_attribute_p
1442 #undef TARGET_OPTION_SAVE
1443 #define TARGET_OPTION_SAVE rs6000_function_specific_save
1445 #undef TARGET_OPTION_RESTORE
1446 #define TARGET_OPTION_RESTORE rs6000_function_specific_restore
1448 #undef TARGET_OPTION_PRINT
1449 #define TARGET_OPTION_PRINT rs6000_function_specific_print
1451 #undef TARGET_CAN_INLINE_P
1452 #define TARGET_CAN_INLINE_P rs6000_can_inline_p
1454 #undef TARGET_SET_CURRENT_FUNCTION
1455 #define TARGET_SET_CURRENT_FUNCTION rs6000_set_current_function
1457 #undef TARGET_LEGITIMATE_CONSTANT_P
1458 #define TARGET_LEGITIMATE_CONSTANT_P rs6000_legitimate_constant_p
1460 #undef TARGET_VECTORIZE_VEC_PERM_CONST_OK
1461 #define TARGET_VECTORIZE_VEC_PERM_CONST_OK rs6000_vectorize_vec_perm_const_ok
1464 /* Processor table. */
1465 struct rs6000_ptt
1467 const char *const name; /* Canonical processor name. */
1468 const enum processor_type processor; /* Processor type enum value. */
1469 const HOST_WIDE_INT target_enable; /* Target flags to enable. */
1472 static struct rs6000_ptt const processor_target_table[] =
1474 #define RS6000_CPU(NAME, CPU, FLAGS) { NAME, CPU, FLAGS },
1475 #include "rs6000-cpus.def"
1476 #undef RS6000_CPU
1479 /* Look up a processor name for -mcpu=xxx and -mtune=xxx. Return -1 if the
1480 name is invalid. */
1482 static int
1483 rs6000_cpu_name_lookup (const char *name)
1485 size_t i;
1487 if (name != NULL)
1489 for (i = 0; i < ARRAY_SIZE (processor_target_table); i++)
1490 if (! strcmp (name, processor_target_table[i].name))
1491 return (int)i;
1494 return -1;
1498 /* Return number of consecutive hard regs needed starting at reg REGNO
1499 to hold something of mode MODE.
1500 This is ordinarily the length in words of a value of mode MODE
1501 but can be less for certain modes in special long registers.
1503 For the SPE, GPRs are 64 bits but only 32 bits are visible in
1504 scalar instructions. The upper 32 bits are only available to the
1505 SIMD instructions.
1507 POWER and PowerPC GPRs hold 32 bits worth;
1508 PowerPC64 GPRs and FPRs point register holds 64 bits worth. */
1510 static int
1511 rs6000_hard_regno_nregs_internal (int regno, enum machine_mode mode)
1513 unsigned HOST_WIDE_INT reg_size;
1515 if (FP_REGNO_P (regno))
1516 reg_size = (VECTOR_MEM_VSX_P (mode)
1517 ? UNITS_PER_VSX_WORD
1518 : UNITS_PER_FP_WORD);
1520 else if (SPE_SIMD_REGNO_P (regno) && TARGET_SPE && SPE_VECTOR_MODE (mode))
1521 reg_size = UNITS_PER_SPE_WORD;
1523 else if (ALTIVEC_REGNO_P (regno))
1524 reg_size = UNITS_PER_ALTIVEC_WORD;
1526 /* The value returned for SCmode in the E500 double case is 2 for
1527 ABI compatibility; storing an SCmode value in a single register
1528 would require function_arg and rs6000_spe_function_arg to handle
1529 SCmode so as to pass the value correctly in a pair of
1530 registers. */
1531 else if (TARGET_E500_DOUBLE && FLOAT_MODE_P (mode) && mode != SCmode
1532 && !DECIMAL_FLOAT_MODE_P (mode))
1533 reg_size = UNITS_PER_FP_WORD;
1535 else
1536 reg_size = UNITS_PER_WORD;
1538 return (GET_MODE_SIZE (mode) + reg_size - 1) / reg_size;
1541 /* Value is 1 if hard register REGNO can hold a value of machine-mode
1542 MODE. */
1543 static int
1544 rs6000_hard_regno_mode_ok (int regno, enum machine_mode mode)
1546 int last_regno = regno + rs6000_hard_regno_nregs[mode][regno] - 1;
1548 /* VSX registers that overlap the FPR registers are larger than for non-VSX
1549 implementations. Don't allow an item to be split between a FP register
1550 and an Altivec register. */
1551 if (VECTOR_MEM_VSX_P (mode))
1553 if (FP_REGNO_P (regno))
1554 return FP_REGNO_P (last_regno);
1556 if (ALTIVEC_REGNO_P (regno))
1557 return ALTIVEC_REGNO_P (last_regno);
1560 /* The GPRs can hold any mode, but values bigger than one register
1561 cannot go past R31. */
1562 if (INT_REGNO_P (regno))
1563 return INT_REGNO_P (last_regno);
1565 /* The float registers (except for VSX vector modes) can only hold floating
1566 modes and DImode. This excludes the 32-bit decimal float mode for
1567 now. */
1568 if (FP_REGNO_P (regno))
1570 if (SCALAR_FLOAT_MODE_P (mode)
1571 && (mode != TDmode || (regno % 2) == 0)
1572 && FP_REGNO_P (last_regno))
1573 return 1;
1575 if (GET_MODE_CLASS (mode) == MODE_INT
1576 && GET_MODE_SIZE (mode) == UNITS_PER_FP_WORD)
1577 return 1;
1579 if (PAIRED_SIMD_REGNO_P (regno) && TARGET_PAIRED_FLOAT
1580 && PAIRED_VECTOR_MODE (mode))
1581 return 1;
1583 return 0;
1586 /* The CR register can only hold CC modes. */
1587 if (CR_REGNO_P (regno))
1588 return GET_MODE_CLASS (mode) == MODE_CC;
1590 if (CA_REGNO_P (regno))
1591 return mode == BImode;
1593 /* AltiVec only in AldyVec registers. */
1594 if (ALTIVEC_REGNO_P (regno))
1595 return VECTOR_MEM_ALTIVEC_OR_VSX_P (mode);
1597 /* ...but GPRs can hold SIMD data on the SPE in one register. */
1598 if (SPE_SIMD_REGNO_P (regno) && TARGET_SPE && SPE_VECTOR_MODE (mode))
1599 return 1;
1601 /* We cannot put TImode anywhere except general register and it must be able
1602 to fit within the register set. In the future, allow TImode in the
1603 Altivec or VSX registers. */
1605 return GET_MODE_SIZE (mode) <= UNITS_PER_WORD;
1608 /* Print interesting facts about registers. */
1609 static void
1610 rs6000_debug_reg_print (int first_regno, int last_regno, const char *reg_name)
1612 int r, m;
1614 for (r = first_regno; r <= last_regno; ++r)
1616 const char *comma = "";
1617 int len;
1619 if (first_regno == last_regno)
1620 fprintf (stderr, "%s:\t", reg_name);
1621 else
1622 fprintf (stderr, "%s%d:\t", reg_name, r - first_regno);
1624 len = 8;
1625 for (m = 0; m < NUM_MACHINE_MODES; ++m)
1626 if (rs6000_hard_regno_mode_ok_p[m][r] && rs6000_hard_regno_nregs[m][r])
1628 if (len > 70)
1630 fprintf (stderr, ",\n\t");
1631 len = 8;
1632 comma = "";
1635 if (rs6000_hard_regno_nregs[m][r] > 1)
1636 len += fprintf (stderr, "%s%s/%d", comma, GET_MODE_NAME (m),
1637 rs6000_hard_regno_nregs[m][r]);
1638 else
1639 len += fprintf (stderr, "%s%s", comma, GET_MODE_NAME (m));
1641 comma = ", ";
1644 if (call_used_regs[r])
1646 if (len > 70)
1648 fprintf (stderr, ",\n\t");
1649 len = 8;
1650 comma = "";
1653 len += fprintf (stderr, "%s%s", comma, "call-used");
1654 comma = ", ";
1657 if (fixed_regs[r])
1659 if (len > 70)
1661 fprintf (stderr, ",\n\t");
1662 len = 8;
1663 comma = "";
1666 len += fprintf (stderr, "%s%s", comma, "fixed");
1667 comma = ", ";
1670 if (len > 70)
1672 fprintf (stderr, ",\n\t");
1673 comma = "";
1676 fprintf (stderr, "%sregno = %d\n", comma, r);
1680 #define DEBUG_FMT_ID "%-32s= "
1681 #define DEBUG_FMT_D DEBUG_FMT_ID "%d\n"
1682 #define DEBUG_FMT_WX DEBUG_FMT_ID "%#.12" HOST_WIDE_INT_PRINT "x: "
1683 #define DEBUG_FMT_S DEBUG_FMT_ID "%s\n"
1685 /* Print various interesting information with -mdebug=reg. */
1686 static void
1687 rs6000_debug_reg_global (void)
1689 static const char *const tf[2] = { "false", "true" };
1690 const char *nl = (const char *)0;
1691 int m;
1692 char costly_num[20];
1693 char nop_num[20];
1694 char flags_buffer[40];
1695 const char *costly_str;
1696 const char *nop_str;
1697 const char *trace_str;
1698 const char *abi_str;
1699 const char *cmodel_str;
1700 struct cl_target_option cl_opts;
1702 /* Map enum rs6000_vector to string. */
1703 static const char *rs6000_debug_vector_unit[] = {
1704 "none",
1705 "altivec",
1706 "vsx",
1707 "paired",
1708 "spe",
1709 "other"
1712 fprintf (stderr, "Register information: (last virtual reg = %d)\n",
1713 LAST_VIRTUAL_REGISTER);
1714 rs6000_debug_reg_print (0, 31, "gr");
1715 rs6000_debug_reg_print (32, 63, "fp");
1716 rs6000_debug_reg_print (FIRST_ALTIVEC_REGNO,
1717 LAST_ALTIVEC_REGNO,
1718 "vs");
1719 rs6000_debug_reg_print (LR_REGNO, LR_REGNO, "lr");
1720 rs6000_debug_reg_print (CTR_REGNO, CTR_REGNO, "ctr");
1721 rs6000_debug_reg_print (CR0_REGNO, CR7_REGNO, "cr");
1722 rs6000_debug_reg_print (CA_REGNO, CA_REGNO, "ca");
1723 rs6000_debug_reg_print (VRSAVE_REGNO, VRSAVE_REGNO, "vrsave");
1724 rs6000_debug_reg_print (VSCR_REGNO, VSCR_REGNO, "vscr");
1725 rs6000_debug_reg_print (SPE_ACC_REGNO, SPE_ACC_REGNO, "spe_a");
1726 rs6000_debug_reg_print (SPEFSCR_REGNO, SPEFSCR_REGNO, "spe_f");
1728 fprintf (stderr,
1729 "\n"
1730 "d reg_class = %s\n"
1731 "f reg_class = %s\n"
1732 "v reg_class = %s\n"
1733 "wa reg_class = %s\n"
1734 "wd reg_class = %s\n"
1735 "wf reg_class = %s\n"
1736 "ws reg_class = %s\n\n",
1737 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_d]],
1738 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_f]],
1739 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_v]],
1740 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wa]],
1741 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wd]],
1742 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wf]],
1743 reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_ws]]);
1745 for (m = 0; m < NUM_MACHINE_MODES; ++m)
1746 if (rs6000_vector_unit[m] || rs6000_vector_mem[m])
1748 nl = "\n";
1749 fprintf (stderr, "Vector mode: %-5s arithmetic: %-8s move: %-8s\n",
1750 GET_MODE_NAME (m),
1751 rs6000_debug_vector_unit[ rs6000_vector_unit[m] ],
1752 rs6000_debug_vector_unit[ rs6000_vector_mem[m] ]);
1755 if (nl)
1756 fputs (nl, stderr);
1758 if (rs6000_recip_control)
1760 fprintf (stderr, "\nReciprocal mask = 0x%x\n", rs6000_recip_control);
1762 for (m = 0; m < NUM_MACHINE_MODES; ++m)
1763 if (rs6000_recip_bits[m])
1765 fprintf (stderr,
1766 "Reciprocal estimate mode: %-5s divide: %s rsqrt: %s\n",
1767 GET_MODE_NAME (m),
1768 (RS6000_RECIP_AUTO_RE_P (m)
1769 ? "auto"
1770 : (RS6000_RECIP_HAVE_RE_P (m) ? "have" : "none")),
1771 (RS6000_RECIP_AUTO_RSQRTE_P (m)
1772 ? "auto"
1773 : (RS6000_RECIP_HAVE_RSQRTE_P (m) ? "have" : "none")));
1776 fputs ("\n", stderr);
1779 if (rs6000_cpu_index >= 0)
1781 const char *name = processor_target_table[rs6000_cpu_index].name;
1782 HOST_WIDE_INT flags
1783 = processor_target_table[rs6000_cpu_index].target_enable;
1785 sprintf (flags_buffer, "-mcpu=%s flags", name);
1786 rs6000_print_isa_options (stderr, 0, flags_buffer, flags);
1788 else
1789 fprintf (stderr, DEBUG_FMT_S, "cpu", "<none>");
1791 if (rs6000_tune_index >= 0)
1793 const char *name = processor_target_table[rs6000_tune_index].name;
1794 HOST_WIDE_INT flags
1795 = processor_target_table[rs6000_tune_index].target_enable;
1797 sprintf (flags_buffer, "-mtune=%s flags", name);
1798 rs6000_print_isa_options (stderr, 0, flags_buffer, flags);
1800 else
1801 fprintf (stderr, DEBUG_FMT_S, "tune", "<none>");
1803 cl_target_option_save (&cl_opts, &global_options);
1804 rs6000_print_isa_options (stderr, 0, "rs6000_isa_flags",
1805 rs6000_isa_flags);
1807 rs6000_print_isa_options (stderr, 0, "rs6000_isa_flags_explicit",
1808 rs6000_isa_flags_explicit);
1810 rs6000_print_builtin_options (stderr, 0, "rs6000_builtin_mask",
1811 rs6000_builtin_mask);
1813 rs6000_print_isa_options (stderr, 0, "TARGET_DEFAULT", TARGET_DEFAULT);
1815 fprintf (stderr, DEBUG_FMT_S, "--with-cpu default",
1816 OPTION_TARGET_CPU_DEFAULT ? OPTION_TARGET_CPU_DEFAULT : "<none>");
1818 switch (rs6000_sched_costly_dep)
1820 case max_dep_latency:
1821 costly_str = "max_dep_latency";
1822 break;
1824 case no_dep_costly:
1825 costly_str = "no_dep_costly";
1826 break;
1828 case all_deps_costly:
1829 costly_str = "all_deps_costly";
1830 break;
1832 case true_store_to_load_dep_costly:
1833 costly_str = "true_store_to_load_dep_costly";
1834 break;
1836 case store_to_load_dep_costly:
1837 costly_str = "store_to_load_dep_costly";
1838 break;
1840 default:
1841 costly_str = costly_num;
1842 sprintf (costly_num, "%d", (int)rs6000_sched_costly_dep);
1843 break;
1846 fprintf (stderr, DEBUG_FMT_S, "sched_costly_dep", costly_str);
1848 switch (rs6000_sched_insert_nops)
1850 case sched_finish_regroup_exact:
1851 nop_str = "sched_finish_regroup_exact";
1852 break;
1854 case sched_finish_pad_groups:
1855 nop_str = "sched_finish_pad_groups";
1856 break;
1858 case sched_finish_none:
1859 nop_str = "sched_finish_none";
1860 break;
1862 default:
1863 nop_str = nop_num;
1864 sprintf (nop_num, "%d", (int)rs6000_sched_insert_nops);
1865 break;
1868 fprintf (stderr, DEBUG_FMT_S, "sched_insert_nops", nop_str);
1870 switch (rs6000_sdata)
1872 default:
1873 case SDATA_NONE:
1874 break;
1876 case SDATA_DATA:
1877 fprintf (stderr, DEBUG_FMT_S, "sdata", "data");
1878 break;
1880 case SDATA_SYSV:
1881 fprintf (stderr, DEBUG_FMT_S, "sdata", "sysv");
1882 break;
1884 case SDATA_EABI:
1885 fprintf (stderr, DEBUG_FMT_S, "sdata", "eabi");
1886 break;
1890 switch (rs6000_traceback)
1892 case traceback_default: trace_str = "default"; break;
1893 case traceback_none: trace_str = "none"; break;
1894 case traceback_part: trace_str = "part"; break;
1895 case traceback_full: trace_str = "full"; break;
1896 default: trace_str = "unknown"; break;
1899 fprintf (stderr, DEBUG_FMT_S, "traceback", trace_str);
1901 switch (rs6000_current_cmodel)
1903 case CMODEL_SMALL: cmodel_str = "small"; break;
1904 case CMODEL_MEDIUM: cmodel_str = "medium"; break;
1905 case CMODEL_LARGE: cmodel_str = "large"; break;
1906 default: cmodel_str = "unknown"; break;
1909 fprintf (stderr, DEBUG_FMT_S, "cmodel", cmodel_str);
1911 switch (rs6000_current_abi)
1913 case ABI_NONE: abi_str = "none"; break;
1914 case ABI_AIX: abi_str = "aix"; break;
1915 case ABI_V4: abi_str = "V4"; break;
1916 case ABI_DARWIN: abi_str = "darwin"; break;
1917 default: abi_str = "unknown"; break;
1920 fprintf (stderr, DEBUG_FMT_S, "abi", abi_str);
1922 if (rs6000_altivec_abi)
1923 fprintf (stderr, DEBUG_FMT_S, "altivec_abi", "true");
1925 if (rs6000_spe_abi)
1926 fprintf (stderr, DEBUG_FMT_S, "spe_abi", "true");
1928 if (rs6000_darwin64_abi)
1929 fprintf (stderr, DEBUG_FMT_S, "darwin64_abi", "true");
1931 if (rs6000_float_gprs)
1932 fprintf (stderr, DEBUG_FMT_S, "float_gprs", "true");
1934 if (TARGET_LINK_STACK)
1935 fprintf (stderr, DEBUG_FMT_S, "link_stack", "true");
1937 fprintf (stderr, DEBUG_FMT_S, "plt-format",
1938 TARGET_SECURE_PLT ? "secure" : "bss");
1939 fprintf (stderr, DEBUG_FMT_S, "struct-return",
1940 aix_struct_return ? "aix" : "sysv");
1941 fprintf (stderr, DEBUG_FMT_S, "always_hint", tf[!!rs6000_always_hint]);
1942 fprintf (stderr, DEBUG_FMT_S, "sched_groups", tf[!!rs6000_sched_groups]);
1943 fprintf (stderr, DEBUG_FMT_S, "align_branch",
1944 tf[!!rs6000_align_branch_targets]);
1945 fprintf (stderr, DEBUG_FMT_D, "tls_size", rs6000_tls_size);
1946 fprintf (stderr, DEBUG_FMT_D, "long_double_size",
1947 rs6000_long_double_type_size);
1948 fprintf (stderr, DEBUG_FMT_D, "sched_restricted_insns_priority",
1949 (int)rs6000_sched_restricted_insns_priority);
1950 fprintf (stderr, DEBUG_FMT_D, "Number of standard builtins",
1951 (int)END_BUILTINS);
1952 fprintf (stderr, DEBUG_FMT_D, "Number of rs6000 builtins",
1953 (int)RS6000_BUILTIN_COUNT);
1956 /* Initialize the various global tables that are based on register size. */
1957 static void
1958 rs6000_init_hard_regno_mode_ok (bool global_init_p)
1960 int r, m, c;
1961 int align64;
1962 int align32;
1964 /* Precalculate REGNO_REG_CLASS. */
1965 rs6000_regno_regclass[0] = GENERAL_REGS;
1966 for (r = 1; r < 32; ++r)
1967 rs6000_regno_regclass[r] = BASE_REGS;
1969 for (r = 32; r < 64; ++r)
1970 rs6000_regno_regclass[r] = FLOAT_REGS;
1972 for (r = 64; r < FIRST_PSEUDO_REGISTER; ++r)
1973 rs6000_regno_regclass[r] = NO_REGS;
1975 for (r = FIRST_ALTIVEC_REGNO; r <= LAST_ALTIVEC_REGNO; ++r)
1976 rs6000_regno_regclass[r] = ALTIVEC_REGS;
1978 rs6000_regno_regclass[CR0_REGNO] = CR0_REGS;
1979 for (r = CR1_REGNO; r <= CR7_REGNO; ++r)
1980 rs6000_regno_regclass[r] = CR_REGS;
1982 rs6000_regno_regclass[LR_REGNO] = LINK_REGS;
1983 rs6000_regno_regclass[CTR_REGNO] = CTR_REGS;
1984 rs6000_regno_regclass[CA_REGNO] = CA_REGS;
1985 rs6000_regno_regclass[VRSAVE_REGNO] = VRSAVE_REGS;
1986 rs6000_regno_regclass[VSCR_REGNO] = VRSAVE_REGS;
1987 rs6000_regno_regclass[SPE_ACC_REGNO] = SPE_ACC_REGS;
1988 rs6000_regno_regclass[SPEFSCR_REGNO] = SPEFSCR_REGS;
1989 rs6000_regno_regclass[ARG_POINTER_REGNUM] = BASE_REGS;
1990 rs6000_regno_regclass[FRAME_POINTER_REGNUM] = BASE_REGS;
1992 /* Precalculate vector information, this must be set up before the
1993 rs6000_hard_regno_nregs_internal below. */
1994 for (m = 0; m < NUM_MACHINE_MODES; ++m)
1996 rs6000_vector_unit[m] = rs6000_vector_mem[m] = VECTOR_NONE;
1997 rs6000_vector_reload[m][0] = CODE_FOR_nothing;
1998 rs6000_vector_reload[m][1] = CODE_FOR_nothing;
2001 for (c = 0; c < (int)(int)RS6000_CONSTRAINT_MAX; c++)
2002 rs6000_constraints[c] = NO_REGS;
2004 /* The VSX hardware allows native alignment for vectors, but control whether the compiler
2005 believes it can use native alignment or still uses 128-bit alignment. */
2006 if (TARGET_VSX && !TARGET_VSX_ALIGN_128)
2008 align64 = 64;
2009 align32 = 32;
2011 else
2013 align64 = 128;
2014 align32 = 128;
2017 /* V2DF mode, VSX only. */
2018 if (TARGET_VSX)
2020 rs6000_vector_unit[V2DFmode] = VECTOR_VSX;
2021 rs6000_vector_mem[V2DFmode] = VECTOR_VSX;
2022 rs6000_vector_align[V2DFmode] = align64;
2025 /* V4SF mode, either VSX or Altivec. */
2026 if (TARGET_VSX)
2028 rs6000_vector_unit[V4SFmode] = VECTOR_VSX;
2029 rs6000_vector_mem[V4SFmode] = VECTOR_VSX;
2030 rs6000_vector_align[V4SFmode] = align32;
2032 else if (TARGET_ALTIVEC)
2034 rs6000_vector_unit[V4SFmode] = VECTOR_ALTIVEC;
2035 rs6000_vector_mem[V4SFmode] = VECTOR_ALTIVEC;
2036 rs6000_vector_align[V4SFmode] = align32;
2039 /* V16QImode, V8HImode, V4SImode are Altivec only, but possibly do VSX loads
2040 and stores. */
2041 if (TARGET_ALTIVEC)
2043 rs6000_vector_unit[V4SImode] = VECTOR_ALTIVEC;
2044 rs6000_vector_unit[V8HImode] = VECTOR_ALTIVEC;
2045 rs6000_vector_unit[V16QImode] = VECTOR_ALTIVEC;
2046 rs6000_vector_align[V4SImode] = align32;
2047 rs6000_vector_align[V8HImode] = align32;
2048 rs6000_vector_align[V16QImode] = align32;
2050 if (TARGET_VSX)
2052 rs6000_vector_mem[V4SImode] = VECTOR_VSX;
2053 rs6000_vector_mem[V8HImode] = VECTOR_VSX;
2054 rs6000_vector_mem[V16QImode] = VECTOR_VSX;
2056 else
2058 rs6000_vector_mem[V4SImode] = VECTOR_ALTIVEC;
2059 rs6000_vector_mem[V8HImode] = VECTOR_ALTIVEC;
2060 rs6000_vector_mem[V16QImode] = VECTOR_ALTIVEC;
2064 /* V2DImode, only allow under VSX, which can do V2DI insert/splat/extract.
2065 Altivec doesn't have 64-bit support. */
2066 if (TARGET_VSX)
2068 rs6000_vector_mem[V2DImode] = VECTOR_VSX;
2069 rs6000_vector_unit[V2DImode] = VECTOR_NONE;
2070 rs6000_vector_align[V2DImode] = align64;
2073 /* DFmode, see if we want to use the VSX unit. */
2074 if (TARGET_VSX && TARGET_VSX_SCALAR_DOUBLE)
2076 rs6000_vector_unit[DFmode] = VECTOR_VSX;
2077 rs6000_vector_mem[DFmode]
2078 = (TARGET_VSX_SCALAR_MEMORY ? VECTOR_VSX : VECTOR_NONE);
2079 rs6000_vector_align[DFmode] = align64;
2082 /* TODO add SPE and paired floating point vector support. */
2084 /* Register class constraints for the constraints that depend on compile
2085 switches. */
2086 if (TARGET_HARD_FLOAT && TARGET_FPRS)
2087 rs6000_constraints[RS6000_CONSTRAINT_f] = FLOAT_REGS;
2089 if (TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT)
2090 rs6000_constraints[RS6000_CONSTRAINT_d] = FLOAT_REGS;
2092 if (TARGET_VSX)
2094 /* At present, we just use VSX_REGS, but we have different constraints
2095 based on the use, in case we want to fine tune the default register
2096 class used. wa = any VSX register, wf = register class to use for
2097 V4SF, wd = register class to use for V2DF, and ws = register classs to
2098 use for DF scalars. */
2099 rs6000_constraints[RS6000_CONSTRAINT_wa] = VSX_REGS;
2100 rs6000_constraints[RS6000_CONSTRAINT_wf] = VSX_REGS;
2101 rs6000_constraints[RS6000_CONSTRAINT_wd] = VSX_REGS;
2102 rs6000_constraints[RS6000_CONSTRAINT_ws] = (TARGET_VSX_SCALAR_MEMORY
2103 ? VSX_REGS
2104 : FLOAT_REGS);
2107 if (TARGET_ALTIVEC)
2108 rs6000_constraints[RS6000_CONSTRAINT_v] = ALTIVEC_REGS;
2110 /* Set up the reload helper functions. */
2111 if (TARGET_VSX || TARGET_ALTIVEC)
2113 if (TARGET_64BIT)
2115 rs6000_vector_reload[V16QImode][0] = CODE_FOR_reload_v16qi_di_store;
2116 rs6000_vector_reload[V16QImode][1] = CODE_FOR_reload_v16qi_di_load;
2117 rs6000_vector_reload[V8HImode][0] = CODE_FOR_reload_v8hi_di_store;
2118 rs6000_vector_reload[V8HImode][1] = CODE_FOR_reload_v8hi_di_load;
2119 rs6000_vector_reload[V4SImode][0] = CODE_FOR_reload_v4si_di_store;
2120 rs6000_vector_reload[V4SImode][1] = CODE_FOR_reload_v4si_di_load;
2121 rs6000_vector_reload[V2DImode][0] = CODE_FOR_reload_v2di_di_store;
2122 rs6000_vector_reload[V2DImode][1] = CODE_FOR_reload_v2di_di_load;
2123 rs6000_vector_reload[V4SFmode][0] = CODE_FOR_reload_v4sf_di_store;
2124 rs6000_vector_reload[V4SFmode][1] = CODE_FOR_reload_v4sf_di_load;
2125 rs6000_vector_reload[V2DFmode][0] = CODE_FOR_reload_v2df_di_store;
2126 rs6000_vector_reload[V2DFmode][1] = CODE_FOR_reload_v2df_di_load;
2127 if (TARGET_VSX && TARGET_VSX_SCALAR_MEMORY)
2129 rs6000_vector_reload[DFmode][0] = CODE_FOR_reload_df_di_store;
2130 rs6000_vector_reload[DFmode][1] = CODE_FOR_reload_df_di_load;
2133 else
2135 rs6000_vector_reload[V16QImode][0] = CODE_FOR_reload_v16qi_si_store;
2136 rs6000_vector_reload[V16QImode][1] = CODE_FOR_reload_v16qi_si_load;
2137 rs6000_vector_reload[V8HImode][0] = CODE_FOR_reload_v8hi_si_store;
2138 rs6000_vector_reload[V8HImode][1] = CODE_FOR_reload_v8hi_si_load;
2139 rs6000_vector_reload[V4SImode][0] = CODE_FOR_reload_v4si_si_store;
2140 rs6000_vector_reload[V4SImode][1] = CODE_FOR_reload_v4si_si_load;
2141 rs6000_vector_reload[V2DImode][0] = CODE_FOR_reload_v2di_si_store;
2142 rs6000_vector_reload[V2DImode][1] = CODE_FOR_reload_v2di_si_load;
2143 rs6000_vector_reload[V4SFmode][0] = CODE_FOR_reload_v4sf_si_store;
2144 rs6000_vector_reload[V4SFmode][1] = CODE_FOR_reload_v4sf_si_load;
2145 rs6000_vector_reload[V2DFmode][0] = CODE_FOR_reload_v2df_si_store;
2146 rs6000_vector_reload[V2DFmode][1] = CODE_FOR_reload_v2df_si_load;
2147 if (TARGET_VSX && TARGET_VSX_SCALAR_MEMORY)
2149 rs6000_vector_reload[DFmode][0] = CODE_FOR_reload_df_si_store;
2150 rs6000_vector_reload[DFmode][1] = CODE_FOR_reload_df_si_load;
2155 /* Precalculate HARD_REGNO_NREGS. */
2156 for (r = 0; r < FIRST_PSEUDO_REGISTER; ++r)
2157 for (m = 0; m < NUM_MACHINE_MODES; ++m)
2158 rs6000_hard_regno_nregs[m][r]
2159 = rs6000_hard_regno_nregs_internal (r, (enum machine_mode)m);
2161 /* Precalculate HARD_REGNO_MODE_OK. */
2162 for (r = 0; r < FIRST_PSEUDO_REGISTER; ++r)
2163 for (m = 0; m < NUM_MACHINE_MODES; ++m)
2164 if (rs6000_hard_regno_mode_ok (r, (enum machine_mode)m))
2165 rs6000_hard_regno_mode_ok_p[m][r] = true;
2167 /* Precalculate CLASS_MAX_NREGS sizes. */
2168 for (c = 0; c < LIM_REG_CLASSES; ++c)
2170 int reg_size;
2172 if (TARGET_VSX && VSX_REG_CLASS_P (c))
2173 reg_size = UNITS_PER_VSX_WORD;
2175 else if (c == ALTIVEC_REGS)
2176 reg_size = UNITS_PER_ALTIVEC_WORD;
2178 else if (c == FLOAT_REGS)
2179 reg_size = UNITS_PER_FP_WORD;
2181 else
2182 reg_size = UNITS_PER_WORD;
2184 for (m = 0; m < NUM_MACHINE_MODES; ++m)
2185 rs6000_class_max_nregs[m][c]
2186 = (GET_MODE_SIZE (m) + reg_size - 1) / reg_size;
2189 if (TARGET_E500_DOUBLE)
2190 rs6000_class_max_nregs[DFmode][GENERAL_REGS] = 1;
2192 /* Calculate which modes to automatically generate code to use a the
2193 reciprocal divide and square root instructions. In the future, possibly
2194 automatically generate the instructions even if the user did not specify
2195 -mrecip. The older machines double precision reciprocal sqrt estimate is
2196 not accurate enough. */
2197 memset (rs6000_recip_bits, 0, sizeof (rs6000_recip_bits));
2198 if (TARGET_FRES)
2199 rs6000_recip_bits[SFmode] = RS6000_RECIP_MASK_HAVE_RE;
2200 if (TARGET_FRE)
2201 rs6000_recip_bits[DFmode] = RS6000_RECIP_MASK_HAVE_RE;
2202 if (VECTOR_UNIT_ALTIVEC_OR_VSX_P (V4SFmode))
2203 rs6000_recip_bits[V4SFmode] = RS6000_RECIP_MASK_HAVE_RE;
2204 if (VECTOR_UNIT_VSX_P (V2DFmode))
2205 rs6000_recip_bits[V2DFmode] = RS6000_RECIP_MASK_HAVE_RE;
2207 if (TARGET_FRSQRTES)
2208 rs6000_recip_bits[SFmode] |= RS6000_RECIP_MASK_HAVE_RSQRTE;
2209 if (TARGET_FRSQRTE)
2210 rs6000_recip_bits[DFmode] |= RS6000_RECIP_MASK_HAVE_RSQRTE;
2211 if (VECTOR_UNIT_ALTIVEC_OR_VSX_P (V4SFmode))
2212 rs6000_recip_bits[V4SFmode] |= RS6000_RECIP_MASK_HAVE_RSQRTE;
2213 if (VECTOR_UNIT_VSX_P (V2DFmode))
2214 rs6000_recip_bits[V2DFmode] |= RS6000_RECIP_MASK_HAVE_RSQRTE;
2216 if (rs6000_recip_control)
2218 if (!flag_finite_math_only)
2219 warning (0, "-mrecip requires -ffinite-math or -ffast-math");
2220 if (flag_trapping_math)
2221 warning (0, "-mrecip requires -fno-trapping-math or -ffast-math");
2222 if (!flag_reciprocal_math)
2223 warning (0, "-mrecip requires -freciprocal-math or -ffast-math");
2224 if (flag_finite_math_only && !flag_trapping_math && flag_reciprocal_math)
2226 if (RS6000_RECIP_HAVE_RE_P (SFmode)
2227 && (rs6000_recip_control & RECIP_SF_DIV) != 0)
2228 rs6000_recip_bits[SFmode] |= RS6000_RECIP_MASK_AUTO_RE;
2230 if (RS6000_RECIP_HAVE_RE_P (DFmode)
2231 && (rs6000_recip_control & RECIP_DF_DIV) != 0)
2232 rs6000_recip_bits[DFmode] |= RS6000_RECIP_MASK_AUTO_RE;
2234 if (RS6000_RECIP_HAVE_RE_P (V4SFmode)
2235 && (rs6000_recip_control & RECIP_V4SF_DIV) != 0)
2236 rs6000_recip_bits[V4SFmode] |= RS6000_RECIP_MASK_AUTO_RE;
2238 if (RS6000_RECIP_HAVE_RE_P (V2DFmode)
2239 && (rs6000_recip_control & RECIP_V2DF_DIV) != 0)
2240 rs6000_recip_bits[V2DFmode] |= RS6000_RECIP_MASK_AUTO_RE;
2242 if (RS6000_RECIP_HAVE_RSQRTE_P (SFmode)
2243 && (rs6000_recip_control & RECIP_SF_RSQRT) != 0)
2244 rs6000_recip_bits[SFmode] |= RS6000_RECIP_MASK_AUTO_RSQRTE;
2246 if (RS6000_RECIP_HAVE_RSQRTE_P (DFmode)
2247 && (rs6000_recip_control & RECIP_DF_RSQRT) != 0)
2248 rs6000_recip_bits[DFmode] |= RS6000_RECIP_MASK_AUTO_RSQRTE;
2250 if (RS6000_RECIP_HAVE_RSQRTE_P (V4SFmode)
2251 && (rs6000_recip_control & RECIP_V4SF_RSQRT) != 0)
2252 rs6000_recip_bits[V4SFmode] |= RS6000_RECIP_MASK_AUTO_RSQRTE;
2254 if (RS6000_RECIP_HAVE_RSQRTE_P (V2DFmode)
2255 && (rs6000_recip_control & RECIP_V2DF_RSQRT) != 0)
2256 rs6000_recip_bits[V2DFmode] |= RS6000_RECIP_MASK_AUTO_RSQRTE;
2260 if (global_init_p || TARGET_DEBUG_TARGET)
2262 if (TARGET_DEBUG_REG)
2263 rs6000_debug_reg_global ();
2265 if (TARGET_DEBUG_COST || TARGET_DEBUG_REG)
2266 fprintf (stderr,
2267 "SImode variable mult cost = %d\n"
2268 "SImode constant mult cost = %d\n"
2269 "SImode short constant mult cost = %d\n"
2270 "DImode multipliciation cost = %d\n"
2271 "SImode division cost = %d\n"
2272 "DImode division cost = %d\n"
2273 "Simple fp operation cost = %d\n"
2274 "DFmode multiplication cost = %d\n"
2275 "SFmode division cost = %d\n"
2276 "DFmode division cost = %d\n"
2277 "cache line size = %d\n"
2278 "l1 cache size = %d\n"
2279 "l2 cache size = %d\n"
2280 "simultaneous prefetches = %d\n"
2281 "\n",
2282 rs6000_cost->mulsi,
2283 rs6000_cost->mulsi_const,
2284 rs6000_cost->mulsi_const9,
2285 rs6000_cost->muldi,
2286 rs6000_cost->divsi,
2287 rs6000_cost->divdi,
2288 rs6000_cost->fp,
2289 rs6000_cost->dmul,
2290 rs6000_cost->sdiv,
2291 rs6000_cost->ddiv,
2292 rs6000_cost->cache_line_size,
2293 rs6000_cost->l1_cache_size,
2294 rs6000_cost->l2_cache_size,
2295 rs6000_cost->simultaneous_prefetches);
2299 #if TARGET_MACHO
2300 /* The Darwin version of SUBTARGET_OVERRIDE_OPTIONS. */
2302 static void
2303 darwin_rs6000_override_options (void)
2305 /* The Darwin ABI always includes AltiVec, can't be (validly) turned
2306 off. */
2307 rs6000_altivec_abi = 1;
2308 TARGET_ALTIVEC_VRSAVE = 1;
2309 rs6000_current_abi = ABI_DARWIN;
2311 if (DEFAULT_ABI == ABI_DARWIN
2312 && TARGET_64BIT)
2313 darwin_one_byte_bool = 1;
2315 if (TARGET_64BIT && ! TARGET_POWERPC64)
2317 rs6000_isa_flags |= OPTION_MASK_POWERPC64;
2318 warning (0, "-m64 requires PowerPC64 architecture, enabling");
2320 if (flag_mkernel)
2322 rs6000_default_long_calls = 1;
2323 rs6000_isa_flags |= OPTION_MASK_SOFT_FLOAT;
2326 /* Make -m64 imply -maltivec. Darwin's 64-bit ABI includes
2327 Altivec. */
2328 if (!flag_mkernel && !flag_apple_kext
2329 && TARGET_64BIT
2330 && ! (rs6000_isa_flags_explicit & OPTION_MASK_ALTIVEC))
2331 rs6000_isa_flags |= OPTION_MASK_ALTIVEC;
2333 /* Unless the user (not the configurer) has explicitly overridden
2334 it with -mcpu=G3 or -mno-altivec, then 10.5+ targets default to
2335 G4 unless targeting the kernel. */
2336 if (!flag_mkernel
2337 && !flag_apple_kext
2338 && strverscmp (darwin_macosx_version_min, "10.5") >= 0
2339 && ! (rs6000_isa_flags_explicit & OPTION_MASK_ALTIVEC)
2340 && ! global_options_set.x_rs6000_cpu_index)
2342 rs6000_isa_flags |= OPTION_MASK_ALTIVEC;
2345 #endif
2347 /* If not otherwise specified by a target, make 'long double' equivalent to
2348 'double'. */
2350 #ifndef RS6000_DEFAULT_LONG_DOUBLE_SIZE
2351 #define RS6000_DEFAULT_LONG_DOUBLE_SIZE 64
2352 #endif
2354 /* Return the builtin mask of the various options used that could affect which
2355 builtins were used. In the past we used target_flags, but we've run out of
2356 bits, and some options like SPE and PAIRED are no longer in
2357 target_flags. */
2359 HOST_WIDE_INT
2360 rs6000_builtin_mask_calculate (void)
2362 return (((TARGET_ALTIVEC) ? RS6000_BTM_ALTIVEC : 0)
2363 | ((TARGET_VSX) ? RS6000_BTM_VSX : 0)
2364 | ((TARGET_SPE) ? RS6000_BTM_SPE : 0)
2365 | ((TARGET_PAIRED_FLOAT) ? RS6000_BTM_PAIRED : 0)
2366 | ((TARGET_FRE) ? RS6000_BTM_FRE : 0)
2367 | ((TARGET_FRES) ? RS6000_BTM_FRES : 0)
2368 | ((TARGET_FRSQRTE) ? RS6000_BTM_FRSQRTE : 0)
2369 | ((TARGET_FRSQRTES) ? RS6000_BTM_FRSQRTES : 0)
2370 | ((TARGET_POPCNTD) ? RS6000_BTM_POPCNTD : 0)
2371 | ((rs6000_cpu == PROCESSOR_CELL) ? RS6000_BTM_CELL : 0));
2374 /* Override command line options. Mostly we process the processor type and
2375 sometimes adjust other TARGET_ options. */
2377 static bool
2378 rs6000_option_override_internal (bool global_init_p)
2380 bool ret = true;
2381 bool have_cpu = false;
2383 /* The default cpu requested at configure time, if any. */
2384 const char *implicit_cpu = OPTION_TARGET_CPU_DEFAULT;
2386 HOST_WIDE_INT set_masks;
2387 int cpu_index;
2388 int tune_index;
2389 struct cl_target_option *main_target_opt
2390 = ((global_init_p || target_option_default_node == NULL)
2391 ? NULL : TREE_TARGET_OPTION (target_option_default_node));
2393 /* On 64-bit Darwin, power alignment is ABI-incompatible with some C
2394 library functions, so warn about it. The flag may be useful for
2395 performance studies from time to time though, so don't disable it
2396 entirely. */
2397 if (global_options_set.x_rs6000_alignment_flags
2398 && rs6000_alignment_flags == MASK_ALIGN_POWER
2399 && DEFAULT_ABI == ABI_DARWIN
2400 && TARGET_64BIT)
2401 warning (0, "-malign-power is not supported for 64-bit Darwin;"
2402 " it is incompatible with the installed C and C++ libraries");
2404 /* Numerous experiment shows that IRA based loop pressure
2405 calculation works better for RTL loop invariant motion on targets
2406 with enough (>= 32) registers. It is an expensive optimization.
2407 So it is on only for peak performance. */
2408 if (optimize >= 3 && global_init_p)
2409 flag_ira_loop_pressure = 1;
2411 /* Set the pointer size. */
2412 if (TARGET_64BIT)
2414 rs6000_pmode = (int)DImode;
2415 rs6000_pointer_size = 64;
2417 else
2419 rs6000_pmode = (int)SImode;
2420 rs6000_pointer_size = 32;
2423 /* Some OSs don't support saving the high part of 64-bit registers on context
2424 switch. Other OSs don't support saving Altivec registers. On those OSs,
2425 we don't touch the OPTION_MASK_POWERPC64 or OPTION_MASK_ALTIVEC settings;
2426 if the user wants either, the user must explicitly specify them and we
2427 won't interfere with the user's specification. */
2429 set_masks = POWERPC_MASKS;
2430 #ifdef OS_MISSING_POWERPC64
2431 if (OS_MISSING_POWERPC64)
2432 set_masks &= ~OPTION_MASK_POWERPC64;
2433 #endif
2434 #ifdef OS_MISSING_ALTIVEC
2435 if (OS_MISSING_ALTIVEC)
2436 set_masks &= ~(OPTION_MASK_ALTIVEC | OPTION_MASK_VSX);
2437 #endif
2439 /* Don't override by the processor default if given explicitly. */
2440 set_masks &= ~rs6000_isa_flags_explicit;
2442 /* Process the -mcpu=<xxx> and -mtune=<xxx> argument. If the user changed
2443 the cpu in a target attribute or pragma, but did not specify a tuning
2444 option, use the cpu for the tuning option rather than the option specified
2445 with -mtune on the command line. Process a '--with-cpu' configuration
2446 request as an implicit --cpu. */
2447 if (rs6000_cpu_index >= 0)
2449 cpu_index = rs6000_cpu_index;
2450 have_cpu = true;
2452 else if (main_target_opt != NULL && main_target_opt->x_rs6000_cpu_index >= 0)
2454 rs6000_cpu_index = cpu_index = main_target_opt->x_rs6000_cpu_index;
2455 have_cpu = true;
2457 else if (implicit_cpu)
2459 rs6000_cpu_index = cpu_index = rs6000_cpu_name_lookup (implicit_cpu);
2460 have_cpu = true;
2462 else
2464 const char *default_cpu = (TARGET_POWERPC64 ? "powerpc64" : "powerpc");
2465 rs6000_cpu_index = cpu_index = rs6000_cpu_name_lookup (default_cpu);
2466 have_cpu = false;
2469 gcc_assert (cpu_index >= 0);
2471 /* If we have a cpu, either through an explicit -mcpu=<xxx> or if the
2472 compiler was configured with --with-cpu=<xxx>, replace all of the ISA bits
2473 with those from the cpu, except for options that were explicitly set. If
2474 we don't have a cpu, do not override the target bits set in
2475 TARGET_DEFAULT. */
2476 if (have_cpu)
2478 rs6000_isa_flags &= ~set_masks;
2479 rs6000_isa_flags |= (processor_target_table[cpu_index].target_enable
2480 & set_masks);
2482 else
2483 rs6000_isa_flags |= (processor_target_table[cpu_index].target_enable
2484 & ~rs6000_isa_flags_explicit);
2486 /* If no -mcpu=<xxx>, inherit any default options that were cleared via
2487 POWERPC_MASKS. Originally, TARGET_DEFAULT was used to initialize
2488 target_flags via the TARGET_DEFAULT_TARGET_FLAGS hook. When we switched
2489 to using rs6000_isa_flags, we need to do the initialization here. */
2490 if (!have_cpu)
2491 rs6000_isa_flags |= (TARGET_DEFAULT & ~rs6000_isa_flags_explicit);
2493 if (rs6000_tune_index >= 0)
2494 tune_index = rs6000_tune_index;
2495 else if (have_cpu)
2496 rs6000_tune_index = tune_index = cpu_index;
2497 else
2499 size_t i;
2500 enum processor_type tune_proc
2501 = (TARGET_POWERPC64 ? PROCESSOR_DEFAULT64 : PROCESSOR_DEFAULT);
2503 tune_index = -1;
2504 for (i = 0; i < ARRAY_SIZE (processor_target_table); i++)
2505 if (processor_target_table[i].processor == tune_proc)
2507 rs6000_tune_index = tune_index = i;
2508 break;
2512 gcc_assert (tune_index >= 0);
2513 rs6000_cpu = processor_target_table[tune_index].processor;
2515 /* Pick defaults for SPE related control flags. Do this early to make sure
2516 that the TARGET_ macros are representative ASAP. */
2518 int spe_capable_cpu =
2519 (rs6000_cpu == PROCESSOR_PPC8540
2520 || rs6000_cpu == PROCESSOR_PPC8548);
2522 if (!global_options_set.x_rs6000_spe_abi)
2523 rs6000_spe_abi = spe_capable_cpu;
2525 if (!global_options_set.x_rs6000_spe)
2526 rs6000_spe = spe_capable_cpu;
2528 if (!global_options_set.x_rs6000_float_gprs)
2529 rs6000_float_gprs =
2530 (rs6000_cpu == PROCESSOR_PPC8540 ? 1
2531 : rs6000_cpu == PROCESSOR_PPC8548 ? 2
2532 : 0);
2535 if (global_options_set.x_rs6000_spe_abi
2536 && rs6000_spe_abi
2537 && !TARGET_SPE_ABI)
2538 error ("not configured for SPE ABI");
2540 if (global_options_set.x_rs6000_spe
2541 && rs6000_spe
2542 && !TARGET_SPE)
2543 error ("not configured for SPE instruction set");
2545 if (main_target_opt != NULL
2546 && ((main_target_opt->x_rs6000_spe_abi != rs6000_spe_abi)
2547 || (main_target_opt->x_rs6000_spe != rs6000_spe)
2548 || (main_target_opt->x_rs6000_float_gprs != rs6000_float_gprs)))
2549 error ("target attribute or pragma changes SPE ABI");
2551 if (rs6000_cpu == PROCESSOR_PPCE300C2 || rs6000_cpu == PROCESSOR_PPCE300C3
2552 || rs6000_cpu == PROCESSOR_PPCE500MC || rs6000_cpu == PROCESSOR_PPCE500MC64
2553 || rs6000_cpu == PROCESSOR_PPCE5500)
2555 if (TARGET_ALTIVEC)
2556 error ("AltiVec not supported in this target");
2557 if (TARGET_SPE)
2558 error ("SPE not supported in this target");
2560 if (rs6000_cpu == PROCESSOR_PPCE6500)
2562 if (TARGET_SPE)
2563 error ("SPE not supported in this target");
2566 /* Disable Cell microcode if we are optimizing for the Cell
2567 and not optimizing for size. */
2568 if (rs6000_gen_cell_microcode == -1)
2569 rs6000_gen_cell_microcode = !(rs6000_cpu == PROCESSOR_CELL
2570 && !optimize_size);
2572 /* If we are optimizing big endian systems for space and it's OK to
2573 use instructions that would be microcoded on the Cell, use the
2574 load/store multiple and string instructions. */
2575 if (BYTES_BIG_ENDIAN && optimize_size && rs6000_gen_cell_microcode)
2576 rs6000_isa_flags |= ~rs6000_isa_flags_explicit & (OPTION_MASK_MULTIPLE
2577 | OPTION_MASK_STRING);
2579 /* Don't allow -mmultiple or -mstring on little endian systems
2580 unless the cpu is a 750, because the hardware doesn't support the
2581 instructions used in little endian mode, and causes an alignment
2582 trap. The 750 does not cause an alignment trap (except when the
2583 target is unaligned). */
2585 if (!BYTES_BIG_ENDIAN && rs6000_cpu != PROCESSOR_PPC750)
2587 if (TARGET_MULTIPLE)
2589 rs6000_isa_flags &= ~OPTION_MASK_MULTIPLE;
2590 if ((rs6000_isa_flags_explicit & OPTION_MASK_MULTIPLE) != 0)
2591 warning (0, "-mmultiple is not supported on little endian systems");
2594 if (TARGET_STRING)
2596 rs6000_isa_flags &= ~OPTION_MASK_STRING;
2597 if ((rs6000_isa_flags_explicit & OPTION_MASK_STRING) != 0)
2598 warning (0, "-mstring is not supported on little endian systems");
2602 /* Add some warnings for VSX. */
2603 if (TARGET_VSX)
2605 const char *msg = NULL;
2606 if (!TARGET_HARD_FLOAT || !TARGET_FPRS
2607 || !TARGET_SINGLE_FLOAT || !TARGET_DOUBLE_FLOAT)
2609 if (rs6000_isa_flags_explicit & OPTION_MASK_VSX)
2610 msg = N_("-mvsx requires hardware floating point");
2611 else
2612 rs6000_isa_flags &= ~ OPTION_MASK_VSX;
2614 else if (TARGET_PAIRED_FLOAT)
2615 msg = N_("-mvsx and -mpaired are incompatible");
2616 /* The hardware will allow VSX and little endian, but until we make sure
2617 things like vector select, etc. work don't allow VSX on little endian
2618 systems at this point. */
2619 else if (!BYTES_BIG_ENDIAN)
2620 msg = N_("-mvsx used with little endian code");
2621 else if (TARGET_AVOID_XFORM > 0)
2622 msg = N_("-mvsx needs indexed addressing");
2623 else if (!TARGET_ALTIVEC && (rs6000_isa_flags_explicit
2624 & OPTION_MASK_ALTIVEC))
2626 if (rs6000_isa_flags_explicit & OPTION_MASK_VSX)
2627 msg = N_("-mvsx and -mno-altivec are incompatible");
2628 else
2629 msg = N_("-mno-altivec disables vsx");
2632 if (msg)
2634 warning (0, msg);
2635 rs6000_isa_flags &= ~ OPTION_MASK_VSX;
2636 rs6000_isa_flags_explicit |= OPTION_MASK_VSX;
2640 /* For the newer switches (vsx, dfp, etc.) set some of the older options,
2641 unless the user explicitly used the -mno-<option> to disable the code. */
2642 if (TARGET_VSX)
2643 rs6000_isa_flags |= (ISA_2_6_MASKS_SERVER & ~rs6000_isa_flags_explicit);
2644 else if (TARGET_POPCNTD)
2645 rs6000_isa_flags |= (ISA_2_6_MASKS_EMBEDDED & ~rs6000_isa_flags_explicit);
2646 else if (TARGET_DFP)
2647 rs6000_isa_flags |= (ISA_2_5_MASKS_SERVER & ~rs6000_isa_flags_explicit);
2648 else if (TARGET_CMPB)
2649 rs6000_isa_flags |= (ISA_2_5_MASKS_EMBEDDED & ~rs6000_isa_flags_explicit);
2650 else if (TARGET_FPRND)
2651 rs6000_isa_flags |= (ISA_2_4_MASKS & ~rs6000_isa_flags_explicit);
2652 else if (TARGET_POPCNTB)
2653 rs6000_isa_flags |= (ISA_2_2_MASKS & ~rs6000_isa_flags_explicit);
2654 else if (TARGET_ALTIVEC)
2655 rs6000_isa_flags |= (OPTION_MASK_PPC_GFXOPT & ~rs6000_isa_flags_explicit);
2657 /* E500mc does "better" if we inline more aggressively. Respect the
2658 user's opinion, though. */
2659 if (rs6000_block_move_inline_limit == 0
2660 && (rs6000_cpu == PROCESSOR_PPCE500MC
2661 || rs6000_cpu == PROCESSOR_PPCE500MC64
2662 || rs6000_cpu == PROCESSOR_PPCE5500
2663 || rs6000_cpu == PROCESSOR_PPCE6500))
2664 rs6000_block_move_inline_limit = 128;
2666 /* store_one_arg depends on expand_block_move to handle at least the
2667 size of reg_parm_stack_space. */
2668 if (rs6000_block_move_inline_limit < (TARGET_POWERPC64 ? 64 : 32))
2669 rs6000_block_move_inline_limit = (TARGET_POWERPC64 ? 64 : 32);
2671 if (global_init_p)
2673 /* If the appropriate debug option is enabled, replace the target hooks
2674 with debug versions that call the real version and then prints
2675 debugging information. */
2676 if (TARGET_DEBUG_COST)
2678 targetm.rtx_costs = rs6000_debug_rtx_costs;
2679 targetm.address_cost = rs6000_debug_address_cost;
2680 targetm.sched.adjust_cost = rs6000_debug_adjust_cost;
2683 if (TARGET_DEBUG_ADDR)
2685 targetm.legitimate_address_p = rs6000_debug_legitimate_address_p;
2686 targetm.legitimize_address = rs6000_debug_legitimize_address;
2687 rs6000_secondary_reload_class_ptr
2688 = rs6000_debug_secondary_reload_class;
2689 rs6000_secondary_memory_needed_ptr
2690 = rs6000_debug_secondary_memory_needed;
2691 rs6000_cannot_change_mode_class_ptr
2692 = rs6000_debug_cannot_change_mode_class;
2693 rs6000_preferred_reload_class_ptr
2694 = rs6000_debug_preferred_reload_class;
2695 rs6000_legitimize_reload_address_ptr
2696 = rs6000_debug_legitimize_reload_address;
2697 rs6000_mode_dependent_address_ptr
2698 = rs6000_debug_mode_dependent_address;
2701 if (rs6000_veclibabi_name)
2703 if (strcmp (rs6000_veclibabi_name, "mass") == 0)
2704 rs6000_veclib_handler = rs6000_builtin_vectorized_libmass;
2705 else
2707 error ("unknown vectorization library ABI type (%s) for "
2708 "-mveclibabi= switch", rs6000_veclibabi_name);
2709 ret = false;
2714 if (!global_options_set.x_rs6000_long_double_type_size)
2716 if (main_target_opt != NULL
2717 && (main_target_opt->x_rs6000_long_double_type_size
2718 != RS6000_DEFAULT_LONG_DOUBLE_SIZE))
2719 error ("target attribute or pragma changes long double size");
2720 else
2721 rs6000_long_double_type_size = RS6000_DEFAULT_LONG_DOUBLE_SIZE;
2724 #if !defined (POWERPC_LINUX) && !defined (POWERPC_FREEBSD)
2725 if (!global_options_set.x_rs6000_ieeequad)
2726 rs6000_ieeequad = 1;
2727 #endif
2729 /* Disable VSX and Altivec silently if the user switched cpus to power7 in a
2730 target attribute or pragma which automatically enables both options,
2731 unless the altivec ABI was set. This is set by default for 64-bit, but
2732 not for 32-bit. */
2733 if (main_target_opt != NULL && !main_target_opt->x_rs6000_altivec_abi)
2734 rs6000_isa_flags &= ~((OPTION_MASK_VSX | OPTION_MASK_ALTIVEC)
2735 & ~rs6000_isa_flags_explicit);
2737 /* Enable Altivec ABI for AIX -maltivec. */
2738 if (TARGET_XCOFF && (TARGET_ALTIVEC || TARGET_VSX))
2740 if (main_target_opt != NULL && !main_target_opt->x_rs6000_altivec_abi)
2741 error ("target attribute or pragma changes AltiVec ABI");
2742 else
2743 rs6000_altivec_abi = 1;
2746 /* The AltiVec ABI is the default for PowerPC-64 GNU/Linux. For
2747 PowerPC-32 GNU/Linux, -maltivec implies the AltiVec ABI. It can
2748 be explicitly overridden in either case. */
2749 if (TARGET_ELF)
2751 if (!global_options_set.x_rs6000_altivec_abi
2752 && (TARGET_64BIT || TARGET_ALTIVEC || TARGET_VSX))
2754 if (main_target_opt != NULL &&
2755 !main_target_opt->x_rs6000_altivec_abi)
2756 error ("target attribute or pragma changes AltiVec ABI");
2757 else
2758 rs6000_altivec_abi = 1;
2762 /* Set the Darwin64 ABI as default for 64-bit Darwin.
2763 So far, the only darwin64 targets are also MACH-O. */
2764 if (TARGET_MACHO
2765 && DEFAULT_ABI == ABI_DARWIN
2766 && TARGET_64BIT)
2768 if (main_target_opt != NULL && !main_target_opt->x_rs6000_darwin64_abi)
2769 error ("target attribute or pragma changes darwin64 ABI");
2770 else
2772 rs6000_darwin64_abi = 1;
2773 /* Default to natural alignment, for better performance. */
2774 rs6000_alignment_flags = MASK_ALIGN_NATURAL;
2778 /* Place FP constants in the constant pool instead of TOC
2779 if section anchors enabled. */
2780 if (flag_section_anchors)
2781 TARGET_NO_FP_IN_TOC = 1;
2783 #ifdef SUBTARGET_OVERRIDE_OPTIONS
2784 SUBTARGET_OVERRIDE_OPTIONS;
2785 #endif
2786 #ifdef SUBSUBTARGET_OVERRIDE_OPTIONS
2787 SUBSUBTARGET_OVERRIDE_OPTIONS;
2788 #endif
2789 #ifdef SUB3TARGET_OVERRIDE_OPTIONS
2790 SUB3TARGET_OVERRIDE_OPTIONS;
2791 #endif
2793 /* For the E500 family of cores, reset the single/double FP flags to let us
2794 check that they remain constant across attributes or pragmas. Also,
2795 clear a possible request for string instructions, not supported and which
2796 we might have silently queried above for -Os.
2798 For other families, clear ISEL in case it was set implicitly.
2801 switch (rs6000_cpu)
2803 case PROCESSOR_PPC8540:
2804 case PROCESSOR_PPC8548:
2805 case PROCESSOR_PPCE500MC:
2806 case PROCESSOR_PPCE500MC64:
2807 case PROCESSOR_PPCE5500:
2808 case PROCESSOR_PPCE6500:
2810 rs6000_single_float = TARGET_E500_SINGLE || TARGET_E500_DOUBLE;
2811 rs6000_double_float = TARGET_E500_DOUBLE;
2813 rs6000_isa_flags &= ~OPTION_MASK_STRING;
2815 break;
2817 default:
2819 if (have_cpu && !(rs6000_isa_flags_explicit & OPTION_MASK_ISEL))
2820 rs6000_isa_flags &= ~OPTION_MASK_ISEL;
2822 break;
2825 if (main_target_opt)
2827 if (main_target_opt->x_rs6000_single_float != rs6000_single_float)
2828 error ("target attribute or pragma changes single precision floating "
2829 "point");
2830 if (main_target_opt->x_rs6000_double_float != rs6000_double_float)
2831 error ("target attribute or pragma changes double precision floating "
2832 "point");
2835 /* Detect invalid option combinations with E500. */
2836 CHECK_E500_OPTIONS;
2838 rs6000_always_hint = (rs6000_cpu != PROCESSOR_POWER4
2839 && rs6000_cpu != PROCESSOR_POWER5
2840 && rs6000_cpu != PROCESSOR_POWER6
2841 && rs6000_cpu != PROCESSOR_POWER7
2842 && rs6000_cpu != PROCESSOR_PPCA2
2843 && rs6000_cpu != PROCESSOR_CELL
2844 && rs6000_cpu != PROCESSOR_PPC476);
2845 rs6000_sched_groups = (rs6000_cpu == PROCESSOR_POWER4
2846 || rs6000_cpu == PROCESSOR_POWER5
2847 || rs6000_cpu == PROCESSOR_POWER7);
2848 rs6000_align_branch_targets = (rs6000_cpu == PROCESSOR_POWER4
2849 || rs6000_cpu == PROCESSOR_POWER5
2850 || rs6000_cpu == PROCESSOR_POWER6
2851 || rs6000_cpu == PROCESSOR_POWER7
2852 || rs6000_cpu == PROCESSOR_PPCE500MC
2853 || rs6000_cpu == PROCESSOR_PPCE500MC64
2854 || rs6000_cpu == PROCESSOR_PPCE5500
2855 || rs6000_cpu == PROCESSOR_PPCE6500);
2857 /* Allow debug switches to override the above settings. These are set to -1
2858 in rs6000.opt to indicate the user hasn't directly set the switch. */
2859 if (TARGET_ALWAYS_HINT >= 0)
2860 rs6000_always_hint = TARGET_ALWAYS_HINT;
2862 if (TARGET_SCHED_GROUPS >= 0)
2863 rs6000_sched_groups = TARGET_SCHED_GROUPS;
2865 if (TARGET_ALIGN_BRANCH_TARGETS >= 0)
2866 rs6000_align_branch_targets = TARGET_ALIGN_BRANCH_TARGETS;
2868 rs6000_sched_restricted_insns_priority
2869 = (rs6000_sched_groups ? 1 : 0);
2871 /* Handle -msched-costly-dep option. */
2872 rs6000_sched_costly_dep
2873 = (rs6000_sched_groups ? true_store_to_load_dep_costly : no_dep_costly);
2875 if (rs6000_sched_costly_dep_str)
2877 if (! strcmp (rs6000_sched_costly_dep_str, "no"))
2878 rs6000_sched_costly_dep = no_dep_costly;
2879 else if (! strcmp (rs6000_sched_costly_dep_str, "all"))
2880 rs6000_sched_costly_dep = all_deps_costly;
2881 else if (! strcmp (rs6000_sched_costly_dep_str, "true_store_to_load"))
2882 rs6000_sched_costly_dep = true_store_to_load_dep_costly;
2883 else if (! strcmp (rs6000_sched_costly_dep_str, "store_to_load"))
2884 rs6000_sched_costly_dep = store_to_load_dep_costly;
2885 else
2886 rs6000_sched_costly_dep = ((enum rs6000_dependence_cost)
2887 atoi (rs6000_sched_costly_dep_str));
2890 /* Handle -minsert-sched-nops option. */
2891 rs6000_sched_insert_nops
2892 = (rs6000_sched_groups ? sched_finish_regroup_exact : sched_finish_none);
2894 if (rs6000_sched_insert_nops_str)
2896 if (! strcmp (rs6000_sched_insert_nops_str, "no"))
2897 rs6000_sched_insert_nops = sched_finish_none;
2898 else if (! strcmp (rs6000_sched_insert_nops_str, "pad"))
2899 rs6000_sched_insert_nops = sched_finish_pad_groups;
2900 else if (! strcmp (rs6000_sched_insert_nops_str, "regroup_exact"))
2901 rs6000_sched_insert_nops = sched_finish_regroup_exact;
2902 else
2903 rs6000_sched_insert_nops = ((enum rs6000_nop_insertion)
2904 atoi (rs6000_sched_insert_nops_str));
2907 if (global_init_p)
2909 #ifdef TARGET_REGNAMES
2910 /* If the user desires alternate register names, copy in the
2911 alternate names now. */
2912 if (TARGET_REGNAMES)
2913 memcpy (rs6000_reg_names, alt_reg_names, sizeof (rs6000_reg_names));
2914 #endif
2916 /* Set aix_struct_return last, after the ABI is determined.
2917 If -maix-struct-return or -msvr4-struct-return was explicitly
2918 used, don't override with the ABI default. */
2919 if (!global_options_set.x_aix_struct_return)
2920 aix_struct_return = (DEFAULT_ABI != ABI_V4 || DRAFT_V4_STRUCT_RET);
2922 #if 0
2923 /* IBM XL compiler defaults to unsigned bitfields. */
2924 if (TARGET_XL_COMPAT)
2925 flag_signed_bitfields = 0;
2926 #endif
2928 if (TARGET_LONG_DOUBLE_128 && !TARGET_IEEEQUAD)
2929 REAL_MODE_FORMAT (TFmode) = &ibm_extended_format;
2931 if (TARGET_TOC)
2932 ASM_GENERATE_INTERNAL_LABEL (toc_label_name, "LCTOC", 1);
2934 /* We can only guarantee the availability of DI pseudo-ops when
2935 assembling for 64-bit targets. */
2936 if (!TARGET_64BIT)
2938 targetm.asm_out.aligned_op.di = NULL;
2939 targetm.asm_out.unaligned_op.di = NULL;
2943 /* Set branch target alignment, if not optimizing for size. */
2944 if (!optimize_size)
2946 /* Cell wants to be aligned 8byte for dual issue. Titan wants to be
2947 aligned 8byte to avoid misprediction by the branch predictor. */
2948 if (rs6000_cpu == PROCESSOR_TITAN
2949 || rs6000_cpu == PROCESSOR_CELL)
2951 if (align_functions <= 0)
2952 align_functions = 8;
2953 if (align_jumps <= 0)
2954 align_jumps = 8;
2955 if (align_loops <= 0)
2956 align_loops = 8;
2958 if (rs6000_align_branch_targets)
2960 if (align_functions <= 0)
2961 align_functions = 16;
2962 if (align_jumps <= 0)
2963 align_jumps = 16;
2964 if (align_loops <= 0)
2966 can_override_loop_align = 1;
2967 align_loops = 16;
2970 if (align_jumps_max_skip <= 0)
2971 align_jumps_max_skip = 15;
2972 if (align_loops_max_skip <= 0)
2973 align_loops_max_skip = 15;
2976 /* Arrange to save and restore machine status around nested functions. */
2977 init_machine_status = rs6000_init_machine_status;
2979 /* We should always be splitting complex arguments, but we can't break
2980 Linux and Darwin ABIs at the moment. For now, only AIX is fixed. */
2981 if (DEFAULT_ABI != ABI_AIX)
2982 targetm.calls.split_complex_arg = NULL;
2985 /* Initialize rs6000_cost with the appropriate target costs. */
2986 if (optimize_size)
2987 rs6000_cost = TARGET_POWERPC64 ? &size64_cost : &size32_cost;
2988 else
2989 switch (rs6000_cpu)
2991 case PROCESSOR_RS64A:
2992 rs6000_cost = &rs64a_cost;
2993 break;
2995 case PROCESSOR_MPCCORE:
2996 rs6000_cost = &mpccore_cost;
2997 break;
2999 case PROCESSOR_PPC403:
3000 rs6000_cost = &ppc403_cost;
3001 break;
3003 case PROCESSOR_PPC405:
3004 rs6000_cost = &ppc405_cost;
3005 break;
3007 case PROCESSOR_PPC440:
3008 rs6000_cost = &ppc440_cost;
3009 break;
3011 case PROCESSOR_PPC476:
3012 rs6000_cost = &ppc476_cost;
3013 break;
3015 case PROCESSOR_PPC601:
3016 rs6000_cost = &ppc601_cost;
3017 break;
3019 case PROCESSOR_PPC603:
3020 rs6000_cost = &ppc603_cost;
3021 break;
3023 case PROCESSOR_PPC604:
3024 rs6000_cost = &ppc604_cost;
3025 break;
3027 case PROCESSOR_PPC604e:
3028 rs6000_cost = &ppc604e_cost;
3029 break;
3031 case PROCESSOR_PPC620:
3032 rs6000_cost = &ppc620_cost;
3033 break;
3035 case PROCESSOR_PPC630:
3036 rs6000_cost = &ppc630_cost;
3037 break;
3039 case PROCESSOR_CELL:
3040 rs6000_cost = &ppccell_cost;
3041 break;
3043 case PROCESSOR_PPC750:
3044 case PROCESSOR_PPC7400:
3045 rs6000_cost = &ppc750_cost;
3046 break;
3048 case PROCESSOR_PPC7450:
3049 rs6000_cost = &ppc7450_cost;
3050 break;
3052 case PROCESSOR_PPC8540:
3053 case PROCESSOR_PPC8548:
3054 rs6000_cost = &ppc8540_cost;
3055 break;
3057 case PROCESSOR_PPCE300C2:
3058 case PROCESSOR_PPCE300C3:
3059 rs6000_cost = &ppce300c2c3_cost;
3060 break;
3062 case PROCESSOR_PPCE500MC:
3063 rs6000_cost = &ppce500mc_cost;
3064 break;
3066 case PROCESSOR_PPCE500MC64:
3067 rs6000_cost = &ppce500mc64_cost;
3068 break;
3070 case PROCESSOR_PPCE5500:
3071 rs6000_cost = &ppce5500_cost;
3072 break;
3074 case PROCESSOR_PPCE6500:
3075 rs6000_cost = &ppce6500_cost;
3076 break;
3078 case PROCESSOR_TITAN:
3079 rs6000_cost = &titan_cost;
3080 break;
3082 case PROCESSOR_POWER4:
3083 case PROCESSOR_POWER5:
3084 rs6000_cost = &power4_cost;
3085 break;
3087 case PROCESSOR_POWER6:
3088 rs6000_cost = &power6_cost;
3089 break;
3091 case PROCESSOR_POWER7:
3092 rs6000_cost = &power7_cost;
3093 break;
3095 case PROCESSOR_PPCA2:
3096 rs6000_cost = &ppca2_cost;
3097 break;
3099 default:
3100 gcc_unreachable ();
3103 if (global_init_p)
3105 maybe_set_param_value (PARAM_SIMULTANEOUS_PREFETCHES,
3106 rs6000_cost->simultaneous_prefetches,
3107 global_options.x_param_values,
3108 global_options_set.x_param_values);
3109 maybe_set_param_value (PARAM_L1_CACHE_SIZE, rs6000_cost->l1_cache_size,
3110 global_options.x_param_values,
3111 global_options_set.x_param_values);
3112 maybe_set_param_value (PARAM_L1_CACHE_LINE_SIZE,
3113 rs6000_cost->cache_line_size,
3114 global_options.x_param_values,
3115 global_options_set.x_param_values);
3116 maybe_set_param_value (PARAM_L2_CACHE_SIZE, rs6000_cost->l2_cache_size,
3117 global_options.x_param_values,
3118 global_options_set.x_param_values);
3120 /* If using typedef char *va_list, signal that
3121 __builtin_va_start (&ap, 0) can be optimized to
3122 ap = __builtin_next_arg (0). */
3123 if (DEFAULT_ABI != ABI_V4)
3124 targetm.expand_builtin_va_start = NULL;
3127 /* Set up single/double float flags.
3128 If TARGET_HARD_FLOAT is set, but neither single or double is set,
3129 then set both flags. */
3130 if (TARGET_HARD_FLOAT && TARGET_FPRS
3131 && rs6000_single_float == 0 && rs6000_double_float == 0)
3132 rs6000_single_float = rs6000_double_float = 1;
3134 /* If not explicitly specified via option, decide whether to generate indexed
3135 load/store instructions. */
3136 if (TARGET_AVOID_XFORM == -1)
3137 /* Avoid indexed addressing when targeting Power6 in order to avoid the
3138 DERAT mispredict penalty. However the LVE and STVE altivec instructions
3139 need indexed accesses and the type used is the scalar type of the element
3140 being loaded or stored. */
3141 TARGET_AVOID_XFORM = (rs6000_cpu == PROCESSOR_POWER6 && TARGET_CMPB
3142 && !TARGET_ALTIVEC);
3144 /* Set the -mrecip options. */
3145 if (rs6000_recip_name)
3147 char *p = ASTRDUP (rs6000_recip_name);
3148 char *q;
3149 unsigned int mask, i;
3150 bool invert;
3152 while ((q = strtok (p, ",")) != NULL)
3154 p = NULL;
3155 if (*q == '!')
3157 invert = true;
3158 q++;
3160 else
3161 invert = false;
3163 if (!strcmp (q, "default"))
3164 mask = ((TARGET_RECIP_PRECISION)
3165 ? RECIP_HIGH_PRECISION : RECIP_LOW_PRECISION);
3166 else
3168 for (i = 0; i < ARRAY_SIZE (recip_options); i++)
3169 if (!strcmp (q, recip_options[i].string))
3171 mask = recip_options[i].mask;
3172 break;
3175 if (i == ARRAY_SIZE (recip_options))
3177 error ("unknown option for -mrecip=%s", q);
3178 invert = false;
3179 mask = 0;
3180 ret = false;
3184 if (invert)
3185 rs6000_recip_control &= ~mask;
3186 else
3187 rs6000_recip_control |= mask;
3191 /* Set the builtin mask of the various options used that could affect which
3192 builtins were used. In the past we used target_flags, but we've run out
3193 of bits, and some options like SPE and PAIRED are no longer in
3194 target_flags. */
3195 rs6000_builtin_mask = rs6000_builtin_mask_calculate ();
3196 if (TARGET_DEBUG_BUILTIN || TARGET_DEBUG_TARGET)
3198 fprintf (stderr,
3199 "new builtin mask = " HOST_WIDE_INT_PRINT_HEX ", ",
3200 rs6000_builtin_mask);
3201 rs6000_print_builtin_options (stderr, 0, NULL, rs6000_builtin_mask);
3204 /* Initialize all of the registers. */
3205 rs6000_init_hard_regno_mode_ok (global_init_p);
3207 /* Save the initial options in case the user does function specific options */
3208 if (global_init_p)
3209 target_option_default_node = target_option_current_node
3210 = build_target_option_node ();
3212 /* If not explicitly specified via option, decide whether to generate the
3213 extra blr's required to preserve the link stack on some cpus (eg, 476). */
3214 if (TARGET_LINK_STACK == -1)
3215 SET_TARGET_LINK_STACK (rs6000_cpu == PROCESSOR_PPC476 && flag_pic);
3217 return ret;
3220 /* Implement TARGET_OPTION_OVERRIDE. On the RS/6000 this is used to
3221 define the target cpu type. */
3223 static void
3224 rs6000_option_override (void)
3226 (void) rs6000_option_override_internal (true);
3230 /* Implement targetm.vectorize.builtin_mask_for_load. */
3231 static tree
3232 rs6000_builtin_mask_for_load (void)
3234 if (TARGET_ALTIVEC || TARGET_VSX)
3235 return altivec_builtin_mask_for_load;
3236 else
3237 return 0;
3240 /* Implement LOOP_ALIGN. */
3242 rs6000_loop_align (rtx label)
3244 basic_block bb;
3245 int ninsns;
3247 /* Don't override loop alignment if -falign-loops was specified. */
3248 if (!can_override_loop_align)
3249 return align_loops_log;
3251 bb = BLOCK_FOR_INSN (label);
3252 ninsns = num_loop_insns(bb->loop_father);
3254 /* Align small loops to 32 bytes to fit in an icache sector, otherwise return default. */
3255 if (ninsns > 4 && ninsns <= 8
3256 && (rs6000_cpu == PROCESSOR_POWER4
3257 || rs6000_cpu == PROCESSOR_POWER5
3258 || rs6000_cpu == PROCESSOR_POWER6
3259 || rs6000_cpu == PROCESSOR_POWER7))
3260 return 5;
3261 else
3262 return align_loops_log;
3265 /* Implement TARGET_LOOP_ALIGN_MAX_SKIP. */
3266 static int
3267 rs6000_loop_align_max_skip (rtx label)
3269 return (1 << rs6000_loop_align (label)) - 1;
3272 /* Return true iff, data reference of TYPE can reach vector alignment (16)
3273 after applying N number of iterations. This routine does not determine
3274 how may iterations are required to reach desired alignment. */
3276 static bool
3277 rs6000_vector_alignment_reachable (const_tree type ATTRIBUTE_UNUSED, bool is_packed)
3279 if (is_packed)
3280 return false;
3282 if (TARGET_32BIT)
3284 if (rs6000_alignment_flags == MASK_ALIGN_NATURAL)
3285 return true;
3287 if (rs6000_alignment_flags == MASK_ALIGN_POWER)
3288 return true;
3290 return false;
3292 else
3294 if (TARGET_MACHO)
3295 return false;
3297 /* Assuming that all other types are naturally aligned. CHECKME! */
3298 return true;
3302 /* Return true if the vector misalignment factor is supported by the
3303 target. */
3304 static bool
3305 rs6000_builtin_support_vector_misalignment (enum machine_mode mode,
3306 const_tree type,
3307 int misalignment,
3308 bool is_packed)
3310 if (TARGET_VSX)
3312 /* Return if movmisalign pattern is not supported for this mode. */
3313 if (optab_handler (movmisalign_optab, mode) == CODE_FOR_nothing)
3314 return false;
3316 if (misalignment == -1)
3318 /* Misalignment factor is unknown at compile time but we know
3319 it's word aligned. */
3320 if (rs6000_vector_alignment_reachable (type, is_packed))
3322 int element_size = TREE_INT_CST_LOW (TYPE_SIZE (type));
3324 if (element_size == 64 || element_size == 32)
3325 return true;
3328 return false;
3331 /* VSX supports word-aligned vector. */
3332 if (misalignment % 4 == 0)
3333 return true;
3335 return false;
3338 /* Implement targetm.vectorize.builtin_vectorization_cost. */
3339 static int
3340 rs6000_builtin_vectorization_cost (enum vect_cost_for_stmt type_of_cost,
3341 tree vectype, int misalign)
3343 unsigned elements;
3344 tree elem_type;
3346 switch (type_of_cost)
3348 case scalar_stmt:
3349 case scalar_load:
3350 case scalar_store:
3351 case vector_stmt:
3352 case vector_load:
3353 case vector_store:
3354 case vec_to_scalar:
3355 case scalar_to_vec:
3356 case cond_branch_not_taken:
3357 return 1;
3359 case vec_perm:
3360 if (TARGET_VSX)
3361 return 3;
3362 else
3363 return 1;
3365 case vec_promote_demote:
3366 if (TARGET_VSX)
3367 return 4;
3368 else
3369 return 1;
3371 case cond_branch_taken:
3372 return 3;
3374 case unaligned_load:
3375 if (TARGET_VSX && TARGET_ALLOW_MOVMISALIGN)
3377 elements = TYPE_VECTOR_SUBPARTS (vectype);
3378 if (elements == 2)
3379 /* Double word aligned. */
3380 return 2;
3382 if (elements == 4)
3384 switch (misalign)
3386 case 8:
3387 /* Double word aligned. */
3388 return 2;
3390 case -1:
3391 /* Unknown misalignment. */
3392 case 4:
3393 case 12:
3394 /* Word aligned. */
3395 return 22;
3397 default:
3398 gcc_unreachable ();
3403 if (TARGET_ALTIVEC)
3404 /* Misaligned loads are not supported. */
3405 gcc_unreachable ();
3407 return 2;
3409 case unaligned_store:
3410 if (TARGET_VSX && TARGET_ALLOW_MOVMISALIGN)
3412 elements = TYPE_VECTOR_SUBPARTS (vectype);
3413 if (elements == 2)
3414 /* Double word aligned. */
3415 return 2;
3417 if (elements == 4)
3419 switch (misalign)
3421 case 8:
3422 /* Double word aligned. */
3423 return 2;
3425 case -1:
3426 /* Unknown misalignment. */
3427 case 4:
3428 case 12:
3429 /* Word aligned. */
3430 return 23;
3432 default:
3433 gcc_unreachable ();
3438 if (TARGET_ALTIVEC)
3439 /* Misaligned stores are not supported. */
3440 gcc_unreachable ();
3442 return 2;
3444 case vec_construct:
3445 elements = TYPE_VECTOR_SUBPARTS (vectype);
3446 elem_type = TREE_TYPE (vectype);
3447 /* 32-bit vectors loaded into registers are stored as double
3448 precision, so we need n/2 converts in addition to the usual
3449 n/2 merges to construct a vector of short floats from them. */
3450 if (SCALAR_FLOAT_TYPE_P (elem_type)
3451 && TYPE_PRECISION (elem_type) == 32)
3452 return elements + 1;
3453 else
3454 return elements / 2 + 1;
3456 default:
3457 gcc_unreachable ();
3461 /* Implement targetm.vectorize.preferred_simd_mode. */
3463 static enum machine_mode
3464 rs6000_preferred_simd_mode (enum machine_mode mode)
3466 if (TARGET_VSX)
3467 switch (mode)
3469 case DFmode:
3470 return V2DFmode;
3471 default:;
3473 if (TARGET_ALTIVEC || TARGET_VSX)
3474 switch (mode)
3476 case SFmode:
3477 return V4SFmode;
3478 case DImode:
3479 return V2DImode;
3480 case SImode:
3481 return V4SImode;
3482 case HImode:
3483 return V8HImode;
3484 case QImode:
3485 return V16QImode;
3486 default:;
3488 if (TARGET_SPE)
3489 switch (mode)
3491 case SFmode:
3492 return V2SFmode;
3493 case SImode:
3494 return V2SImode;
3495 default:;
3497 if (TARGET_PAIRED_FLOAT
3498 && mode == SFmode)
3499 return V2SFmode;
3500 return word_mode;
3503 typedef struct _rs6000_cost_data
3505 struct loop *loop_info;
3506 unsigned cost[3];
3507 } rs6000_cost_data;
3509 /* Test for likely overcommitment of vector hardware resources. If a
3510 loop iteration is relatively large, and too large a percentage of
3511 instructions in the loop are vectorized, the cost model may not
3512 adequately reflect delays from unavailable vector resources.
3513 Penalize the loop body cost for this case. */
3515 static void
3516 rs6000_density_test (rs6000_cost_data *data)
3518 const int DENSITY_PCT_THRESHOLD = 85;
3519 const int DENSITY_SIZE_THRESHOLD = 70;
3520 const int DENSITY_PENALTY = 10;
3521 struct loop *loop = data->loop_info;
3522 basic_block *bbs = get_loop_body (loop);
3523 int nbbs = loop->num_nodes;
3524 int vec_cost = data->cost[vect_body], not_vec_cost = 0;
3525 int i, density_pct;
3527 for (i = 0; i < nbbs; i++)
3529 basic_block bb = bbs[i];
3530 gimple_stmt_iterator gsi;
3532 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
3534 gimple stmt = gsi_stmt (gsi);
3535 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
3537 if (!STMT_VINFO_RELEVANT_P (stmt_info)
3538 && !STMT_VINFO_IN_PATTERN_P (stmt_info))
3539 not_vec_cost++;
3543 free (bbs);
3544 density_pct = (vec_cost * 100) / (vec_cost + not_vec_cost);
3546 if (density_pct > DENSITY_PCT_THRESHOLD
3547 && vec_cost + not_vec_cost > DENSITY_SIZE_THRESHOLD)
3549 data->cost[vect_body] = vec_cost * (100 + DENSITY_PENALTY) / 100;
3550 if (dump_enabled_p ())
3551 dump_printf_loc (MSG_NOTE, vect_location,
3552 "density %d%%, cost %d exceeds threshold, penalizing "
3553 "loop body cost by %d%%", density_pct,
3554 vec_cost + not_vec_cost, DENSITY_PENALTY);
3558 /* Implement targetm.vectorize.init_cost. */
3560 static void *
3561 rs6000_init_cost (struct loop *loop_info)
3563 rs6000_cost_data *data = XNEW (struct _rs6000_cost_data);
3564 data->loop_info = loop_info;
3565 data->cost[vect_prologue] = 0;
3566 data->cost[vect_body] = 0;
3567 data->cost[vect_epilogue] = 0;
3568 return data;
3571 /* Implement targetm.vectorize.add_stmt_cost. */
3573 static unsigned
3574 rs6000_add_stmt_cost (void *data, int count, enum vect_cost_for_stmt kind,
3575 struct _stmt_vec_info *stmt_info, int misalign,
3576 enum vect_cost_model_location where)
3578 rs6000_cost_data *cost_data = (rs6000_cost_data*) data;
3579 unsigned retval = 0;
3581 if (flag_vect_cost_model)
3583 tree vectype = stmt_info ? stmt_vectype (stmt_info) : NULL_TREE;
3584 int stmt_cost = rs6000_builtin_vectorization_cost (kind, vectype,
3585 misalign);
3586 /* Statements in an inner loop relative to the loop being
3587 vectorized are weighted more heavily. The value here is
3588 arbitrary and could potentially be improved with analysis. */
3589 if (where == vect_body && stmt_info && stmt_in_inner_loop_p (stmt_info))
3590 count *= 50; /* FIXME. */
3592 retval = (unsigned) (count * stmt_cost);
3593 cost_data->cost[where] += retval;
3596 return retval;
3599 /* Implement targetm.vectorize.finish_cost. */
3601 static void
3602 rs6000_finish_cost (void *data, unsigned *prologue_cost,
3603 unsigned *body_cost, unsigned *epilogue_cost)
3605 rs6000_cost_data *cost_data = (rs6000_cost_data*) data;
3607 if (cost_data->loop_info)
3608 rs6000_density_test (cost_data);
3610 *prologue_cost = cost_data->cost[vect_prologue];
3611 *body_cost = cost_data->cost[vect_body];
3612 *epilogue_cost = cost_data->cost[vect_epilogue];
3615 /* Implement targetm.vectorize.destroy_cost_data. */
3617 static void
3618 rs6000_destroy_cost_data (void *data)
3620 free (data);
3623 /* Handler for the Mathematical Acceleration Subsystem (mass) interface to a
3624 library with vectorized intrinsics. */
3626 static tree
3627 rs6000_builtin_vectorized_libmass (tree fndecl, tree type_out, tree type_in)
3629 char name[32];
3630 const char *suffix = NULL;
3631 tree fntype, new_fndecl, bdecl = NULL_TREE;
3632 int n_args = 1;
3633 const char *bname;
3634 enum machine_mode el_mode, in_mode;
3635 int n, in_n;
3637 /* Libmass is suitable for unsafe math only as it does not correctly support
3638 parts of IEEE with the required precision such as denormals. Only support
3639 it if we have VSX to use the simd d2 or f4 functions.
3640 XXX: Add variable length support. */
3641 if (!flag_unsafe_math_optimizations || !TARGET_VSX)
3642 return NULL_TREE;
3644 el_mode = TYPE_MODE (TREE_TYPE (type_out));
3645 n = TYPE_VECTOR_SUBPARTS (type_out);
3646 in_mode = TYPE_MODE (TREE_TYPE (type_in));
3647 in_n = TYPE_VECTOR_SUBPARTS (type_in);
3648 if (el_mode != in_mode
3649 || n != in_n)
3650 return NULL_TREE;
3652 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
3654 enum built_in_function fn = DECL_FUNCTION_CODE (fndecl);
3655 switch (fn)
3657 case BUILT_IN_ATAN2:
3658 case BUILT_IN_HYPOT:
3659 case BUILT_IN_POW:
3660 n_args = 2;
3661 /* fall through */
3663 case BUILT_IN_ACOS:
3664 case BUILT_IN_ACOSH:
3665 case BUILT_IN_ASIN:
3666 case BUILT_IN_ASINH:
3667 case BUILT_IN_ATAN:
3668 case BUILT_IN_ATANH:
3669 case BUILT_IN_CBRT:
3670 case BUILT_IN_COS:
3671 case BUILT_IN_COSH:
3672 case BUILT_IN_ERF:
3673 case BUILT_IN_ERFC:
3674 case BUILT_IN_EXP2:
3675 case BUILT_IN_EXP:
3676 case BUILT_IN_EXPM1:
3677 case BUILT_IN_LGAMMA:
3678 case BUILT_IN_LOG10:
3679 case BUILT_IN_LOG1P:
3680 case BUILT_IN_LOG2:
3681 case BUILT_IN_LOG:
3682 case BUILT_IN_SIN:
3683 case BUILT_IN_SINH:
3684 case BUILT_IN_SQRT:
3685 case BUILT_IN_TAN:
3686 case BUILT_IN_TANH:
3687 bdecl = builtin_decl_implicit (fn);
3688 suffix = "d2"; /* pow -> powd2 */
3689 if (el_mode != DFmode
3690 || n != 2)
3691 return NULL_TREE;
3692 break;
3694 case BUILT_IN_ATAN2F:
3695 case BUILT_IN_HYPOTF:
3696 case BUILT_IN_POWF:
3697 n_args = 2;
3698 /* fall through */
3700 case BUILT_IN_ACOSF:
3701 case BUILT_IN_ACOSHF:
3702 case BUILT_IN_ASINF:
3703 case BUILT_IN_ASINHF:
3704 case BUILT_IN_ATANF:
3705 case BUILT_IN_ATANHF:
3706 case BUILT_IN_CBRTF:
3707 case BUILT_IN_COSF:
3708 case BUILT_IN_COSHF:
3709 case BUILT_IN_ERFF:
3710 case BUILT_IN_ERFCF:
3711 case BUILT_IN_EXP2F:
3712 case BUILT_IN_EXPF:
3713 case BUILT_IN_EXPM1F:
3714 case BUILT_IN_LGAMMAF:
3715 case BUILT_IN_LOG10F:
3716 case BUILT_IN_LOG1PF:
3717 case BUILT_IN_LOG2F:
3718 case BUILT_IN_LOGF:
3719 case BUILT_IN_SINF:
3720 case BUILT_IN_SINHF:
3721 case BUILT_IN_SQRTF:
3722 case BUILT_IN_TANF:
3723 case BUILT_IN_TANHF:
3724 bdecl = builtin_decl_implicit (fn);
3725 suffix = "4"; /* powf -> powf4 */
3726 if (el_mode != SFmode
3727 || n != 4)
3728 return NULL_TREE;
3729 break;
3731 default:
3732 return NULL_TREE;
3735 else
3736 return NULL_TREE;
3738 gcc_assert (suffix != NULL);
3739 bname = IDENTIFIER_POINTER (DECL_NAME (bdecl));
3740 strcpy (name, bname + sizeof ("__builtin_") - 1);
3741 strcat (name, suffix);
3743 if (n_args == 1)
3744 fntype = build_function_type_list (type_out, type_in, NULL);
3745 else if (n_args == 2)
3746 fntype = build_function_type_list (type_out, type_in, type_in, NULL);
3747 else
3748 gcc_unreachable ();
3750 /* Build a function declaration for the vectorized function. */
3751 new_fndecl = build_decl (BUILTINS_LOCATION,
3752 FUNCTION_DECL, get_identifier (name), fntype);
3753 TREE_PUBLIC (new_fndecl) = 1;
3754 DECL_EXTERNAL (new_fndecl) = 1;
3755 DECL_IS_NOVOPS (new_fndecl) = 1;
3756 TREE_READONLY (new_fndecl) = 1;
3758 return new_fndecl;
3761 /* Returns a function decl for a vectorized version of the builtin function
3762 with builtin function code FN and the result vector type TYPE, or NULL_TREE
3763 if it is not available. */
3765 static tree
3766 rs6000_builtin_vectorized_function (tree fndecl, tree type_out,
3767 tree type_in)
3769 enum machine_mode in_mode, out_mode;
3770 int in_n, out_n;
3772 if (TARGET_DEBUG_BUILTIN)
3773 fprintf (stderr, "rs6000_builtin_vectorized_function (%s, %s, %s)\n",
3774 IDENTIFIER_POINTER (DECL_NAME (fndecl)),
3775 GET_MODE_NAME (TYPE_MODE (type_out)),
3776 GET_MODE_NAME (TYPE_MODE (type_in)));
3778 if (TREE_CODE (type_out) != VECTOR_TYPE
3779 || TREE_CODE (type_in) != VECTOR_TYPE
3780 || !TARGET_VECTORIZE_BUILTINS)
3781 return NULL_TREE;
3783 out_mode = TYPE_MODE (TREE_TYPE (type_out));
3784 out_n = TYPE_VECTOR_SUBPARTS (type_out);
3785 in_mode = TYPE_MODE (TREE_TYPE (type_in));
3786 in_n = TYPE_VECTOR_SUBPARTS (type_in);
3788 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
3790 enum built_in_function fn = DECL_FUNCTION_CODE (fndecl);
3791 switch (fn)
3793 case BUILT_IN_COPYSIGN:
3794 if (VECTOR_UNIT_VSX_P (V2DFmode)
3795 && out_mode == DFmode && out_n == 2
3796 && in_mode == DFmode && in_n == 2)
3797 return rs6000_builtin_decls[VSX_BUILTIN_CPSGNDP];
3798 break;
3799 case BUILT_IN_COPYSIGNF:
3800 if (out_mode != SFmode || out_n != 4
3801 || in_mode != SFmode || in_n != 4)
3802 break;
3803 if (VECTOR_UNIT_VSX_P (V4SFmode))
3804 return rs6000_builtin_decls[VSX_BUILTIN_CPSGNSP];
3805 if (VECTOR_UNIT_ALTIVEC_P (V4SFmode))
3806 return rs6000_builtin_decls[ALTIVEC_BUILTIN_COPYSIGN_V4SF];
3807 break;
3808 case BUILT_IN_SQRT:
3809 if (VECTOR_UNIT_VSX_P (V2DFmode)
3810 && out_mode == DFmode && out_n == 2
3811 && in_mode == DFmode && in_n == 2)
3812 return rs6000_builtin_decls[VSX_BUILTIN_XVSQRTDP];
3813 break;
3814 case BUILT_IN_SQRTF:
3815 if (VECTOR_UNIT_VSX_P (V4SFmode)
3816 && out_mode == SFmode && out_n == 4
3817 && in_mode == SFmode && in_n == 4)
3818 return rs6000_builtin_decls[VSX_BUILTIN_XVSQRTSP];
3819 break;
3820 case BUILT_IN_CEIL:
3821 if (VECTOR_UNIT_VSX_P (V2DFmode)
3822 && out_mode == DFmode && out_n == 2
3823 && in_mode == DFmode && in_n == 2)
3824 return rs6000_builtin_decls[VSX_BUILTIN_XVRDPIP];
3825 break;
3826 case BUILT_IN_CEILF:
3827 if (out_mode != SFmode || out_n != 4
3828 || in_mode != SFmode || in_n != 4)
3829 break;
3830 if (VECTOR_UNIT_VSX_P (V4SFmode))
3831 return rs6000_builtin_decls[VSX_BUILTIN_XVRSPIP];
3832 if (VECTOR_UNIT_ALTIVEC_P (V4SFmode))
3833 return rs6000_builtin_decls[ALTIVEC_BUILTIN_VRFIP];
3834 break;
3835 case BUILT_IN_FLOOR:
3836 if (VECTOR_UNIT_VSX_P (V2DFmode)
3837 && out_mode == DFmode && out_n == 2
3838 && in_mode == DFmode && in_n == 2)
3839 return rs6000_builtin_decls[VSX_BUILTIN_XVRDPIM];
3840 break;
3841 case BUILT_IN_FLOORF:
3842 if (out_mode != SFmode || out_n != 4
3843 || in_mode != SFmode || in_n != 4)
3844 break;
3845 if (VECTOR_UNIT_VSX_P (V4SFmode))
3846 return rs6000_builtin_decls[VSX_BUILTIN_XVRSPIM];
3847 if (VECTOR_UNIT_ALTIVEC_P (V4SFmode))
3848 return rs6000_builtin_decls[ALTIVEC_BUILTIN_VRFIM];
3849 break;
3850 case BUILT_IN_FMA:
3851 if (VECTOR_UNIT_VSX_P (V2DFmode)
3852 && out_mode == DFmode && out_n == 2
3853 && in_mode == DFmode && in_n == 2)
3854 return rs6000_builtin_decls[VSX_BUILTIN_XVMADDDP];
3855 break;
3856 case BUILT_IN_FMAF:
3857 if (VECTOR_UNIT_VSX_P (V4SFmode)
3858 && out_mode == SFmode && out_n == 4
3859 && in_mode == SFmode && in_n == 4)
3860 return rs6000_builtin_decls[VSX_BUILTIN_XVMADDSP];
3861 else if (VECTOR_UNIT_ALTIVEC_P (V4SFmode)
3862 && out_mode == SFmode && out_n == 4
3863 && in_mode == SFmode && in_n == 4)
3864 return rs6000_builtin_decls[ALTIVEC_BUILTIN_VMADDFP];
3865 break;
3866 case BUILT_IN_TRUNC:
3867 if (VECTOR_UNIT_VSX_P (V2DFmode)
3868 && out_mode == DFmode && out_n == 2
3869 && in_mode == DFmode && in_n == 2)
3870 return rs6000_builtin_decls[VSX_BUILTIN_XVRDPIZ];
3871 break;
3872 case BUILT_IN_TRUNCF:
3873 if (out_mode != SFmode || out_n != 4
3874 || in_mode != SFmode || in_n != 4)
3875 break;
3876 if (VECTOR_UNIT_VSX_P (V4SFmode))
3877 return rs6000_builtin_decls[VSX_BUILTIN_XVRSPIZ];
3878 if (VECTOR_UNIT_ALTIVEC_P (V4SFmode))
3879 return rs6000_builtin_decls[ALTIVEC_BUILTIN_VRFIZ];
3880 break;
3881 case BUILT_IN_NEARBYINT:
3882 if (VECTOR_UNIT_VSX_P (V2DFmode)
3883 && flag_unsafe_math_optimizations
3884 && out_mode == DFmode && out_n == 2
3885 && in_mode == DFmode && in_n == 2)
3886 return rs6000_builtin_decls[VSX_BUILTIN_XVRDPI];
3887 break;
3888 case BUILT_IN_NEARBYINTF:
3889 if (VECTOR_UNIT_VSX_P (V4SFmode)
3890 && flag_unsafe_math_optimizations
3891 && out_mode == SFmode && out_n == 4
3892 && in_mode == SFmode && in_n == 4)
3893 return rs6000_builtin_decls[VSX_BUILTIN_XVRSPI];
3894 break;
3895 case BUILT_IN_RINT:
3896 if (VECTOR_UNIT_VSX_P (V2DFmode)
3897 && !flag_trapping_math
3898 && out_mode == DFmode && out_n == 2
3899 && in_mode == DFmode && in_n == 2)
3900 return rs6000_builtin_decls[VSX_BUILTIN_XVRDPIC];
3901 break;
3902 case BUILT_IN_RINTF:
3903 if (VECTOR_UNIT_VSX_P (V4SFmode)
3904 && !flag_trapping_math
3905 && out_mode == SFmode && out_n == 4
3906 && in_mode == SFmode && in_n == 4)
3907 return rs6000_builtin_decls[VSX_BUILTIN_XVRSPIC];
3908 break;
3909 default:
3910 break;
3914 else if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
3916 enum rs6000_builtins fn
3917 = (enum rs6000_builtins)DECL_FUNCTION_CODE (fndecl);
3918 switch (fn)
3920 case RS6000_BUILTIN_RSQRTF:
3921 if (VECTOR_UNIT_ALTIVEC_OR_VSX_P (V4SFmode)
3922 && out_mode == SFmode && out_n == 4
3923 && in_mode == SFmode && in_n == 4)
3924 return rs6000_builtin_decls[ALTIVEC_BUILTIN_VRSQRTFP];
3925 break;
3926 case RS6000_BUILTIN_RSQRT:
3927 if (VECTOR_UNIT_VSX_P (V2DFmode)
3928 && out_mode == DFmode && out_n == 2
3929 && in_mode == DFmode && in_n == 2)
3930 return rs6000_builtin_decls[VSX_BUILTIN_RSQRT_2DF];
3931 break;
3932 case RS6000_BUILTIN_RECIPF:
3933 if (VECTOR_UNIT_ALTIVEC_OR_VSX_P (V4SFmode)
3934 && out_mode == SFmode && out_n == 4
3935 && in_mode == SFmode && in_n == 4)
3936 return rs6000_builtin_decls[ALTIVEC_BUILTIN_VRECIPFP];
3937 break;
3938 case RS6000_BUILTIN_RECIP:
3939 if (VECTOR_UNIT_VSX_P (V2DFmode)
3940 && out_mode == DFmode && out_n == 2
3941 && in_mode == DFmode && in_n == 2)
3942 return rs6000_builtin_decls[VSX_BUILTIN_RECIP_V2DF];
3943 break;
3944 default:
3945 break;
3949 /* Generate calls to libmass if appropriate. */
3950 if (rs6000_veclib_handler)
3951 return rs6000_veclib_handler (fndecl, type_out, type_in);
3953 return NULL_TREE;
3956 /* Default CPU string for rs6000*_file_start functions. */
3957 static const char *rs6000_default_cpu;
3959 /* Do anything needed at the start of the asm file. */
3961 static void
3962 rs6000_file_start (void)
3964 char buffer[80];
3965 const char *start = buffer;
3966 FILE *file = asm_out_file;
3968 rs6000_default_cpu = TARGET_CPU_DEFAULT;
3970 default_file_start ();
3972 if (flag_verbose_asm)
3974 sprintf (buffer, "\n%s rs6000/powerpc options:", ASM_COMMENT_START);
3976 if (rs6000_default_cpu != 0 && rs6000_default_cpu[0] != '\0')
3978 fprintf (file, "%s --with-cpu=%s", start, rs6000_default_cpu);
3979 start = "";
3982 if (global_options_set.x_rs6000_cpu_index)
3984 fprintf (file, "%s -mcpu=%s", start,
3985 processor_target_table[rs6000_cpu_index].name);
3986 start = "";
3989 if (global_options_set.x_rs6000_tune_index)
3991 fprintf (file, "%s -mtune=%s", start,
3992 processor_target_table[rs6000_tune_index].name);
3993 start = "";
3996 if (PPC405_ERRATUM77)
3998 fprintf (file, "%s PPC405CR_ERRATUM77", start);
3999 start = "";
4002 #ifdef USING_ELFOS_H
4003 switch (rs6000_sdata)
4005 case SDATA_NONE: fprintf (file, "%s -msdata=none", start); start = ""; break;
4006 case SDATA_DATA: fprintf (file, "%s -msdata=data", start); start = ""; break;
4007 case SDATA_SYSV: fprintf (file, "%s -msdata=sysv", start); start = ""; break;
4008 case SDATA_EABI: fprintf (file, "%s -msdata=eabi", start); start = ""; break;
4011 if (rs6000_sdata && g_switch_value)
4013 fprintf (file, "%s -G %d", start,
4014 g_switch_value);
4015 start = "";
4017 #endif
4019 if (*start == '\0')
4020 putc ('\n', file);
4023 if (DEFAULT_ABI == ABI_AIX || (TARGET_ELF && flag_pic == 2))
4025 switch_to_section (toc_section);
4026 switch_to_section (text_section);
4031 /* Return nonzero if this function is known to have a null epilogue. */
4034 direct_return (void)
4036 if (reload_completed)
4038 rs6000_stack_t *info = rs6000_stack_info ();
4040 if (info->first_gp_reg_save == 32
4041 && info->first_fp_reg_save == 64
4042 && info->first_altivec_reg_save == LAST_ALTIVEC_REGNO + 1
4043 && ! info->lr_save_p
4044 && ! info->cr_save_p
4045 && info->vrsave_mask == 0
4046 && ! info->push_p)
4047 return 1;
4050 return 0;
4053 /* Return the number of instructions it takes to form a constant in an
4054 integer register. */
4057 num_insns_constant_wide (HOST_WIDE_INT value)
4059 /* signed constant loadable with addi */
4060 if ((unsigned HOST_WIDE_INT) (value + 0x8000) < 0x10000)
4061 return 1;
4063 /* constant loadable with addis */
4064 else if ((value & 0xffff) == 0
4065 && (value >> 31 == -1 || value >> 31 == 0))
4066 return 1;
4068 #if HOST_BITS_PER_WIDE_INT == 64
4069 else if (TARGET_POWERPC64)
4071 HOST_WIDE_INT low = ((value & 0xffffffff) ^ 0x80000000) - 0x80000000;
4072 HOST_WIDE_INT high = value >> 31;
4074 if (high == 0 || high == -1)
4075 return 2;
4077 high >>= 1;
4079 if (low == 0)
4080 return num_insns_constant_wide (high) + 1;
4081 else if (high == 0)
4082 return num_insns_constant_wide (low) + 1;
4083 else
4084 return (num_insns_constant_wide (high)
4085 + num_insns_constant_wide (low) + 1);
4087 #endif
4089 else
4090 return 2;
4094 num_insns_constant (rtx op, enum machine_mode mode)
4096 HOST_WIDE_INT low, high;
4098 switch (GET_CODE (op))
4100 case CONST_INT:
4101 #if HOST_BITS_PER_WIDE_INT == 64
4102 if ((INTVAL (op) >> 31) != 0 && (INTVAL (op) >> 31) != -1
4103 && mask64_operand (op, mode))
4104 return 2;
4105 else
4106 #endif
4107 return num_insns_constant_wide (INTVAL (op));
4109 case CONST_DOUBLE:
4110 if (mode == SFmode || mode == SDmode)
4112 long l;
4113 REAL_VALUE_TYPE rv;
4115 REAL_VALUE_FROM_CONST_DOUBLE (rv, op);
4116 if (DECIMAL_FLOAT_MODE_P (mode))
4117 REAL_VALUE_TO_TARGET_DECIMAL32 (rv, l);
4118 else
4119 REAL_VALUE_TO_TARGET_SINGLE (rv, l);
4120 return num_insns_constant_wide ((HOST_WIDE_INT) l);
4123 if (mode == VOIDmode || mode == DImode)
4125 high = CONST_DOUBLE_HIGH (op);
4126 low = CONST_DOUBLE_LOW (op);
4128 else
4130 long l[2];
4131 REAL_VALUE_TYPE rv;
4133 REAL_VALUE_FROM_CONST_DOUBLE (rv, op);
4134 if (DECIMAL_FLOAT_MODE_P (mode))
4135 REAL_VALUE_TO_TARGET_DECIMAL64 (rv, l);
4136 else
4137 REAL_VALUE_TO_TARGET_DOUBLE (rv, l);
4138 high = l[WORDS_BIG_ENDIAN == 0];
4139 low = l[WORDS_BIG_ENDIAN != 0];
4142 if (TARGET_32BIT)
4143 return (num_insns_constant_wide (low)
4144 + num_insns_constant_wide (high));
4145 else
4147 if ((high == 0 && low >= 0)
4148 || (high == -1 && low < 0))
4149 return num_insns_constant_wide (low);
4151 else if (mask64_operand (op, mode))
4152 return 2;
4154 else if (low == 0)
4155 return num_insns_constant_wide (high) + 1;
4157 else
4158 return (num_insns_constant_wide (high)
4159 + num_insns_constant_wide (low) + 1);
4162 default:
4163 gcc_unreachable ();
4167 /* Interpret element ELT of the CONST_VECTOR OP as an integer value.
4168 If the mode of OP is MODE_VECTOR_INT, this simply returns the
4169 corresponding element of the vector, but for V4SFmode and V2SFmode,
4170 the corresponding "float" is interpreted as an SImode integer. */
4172 HOST_WIDE_INT
4173 const_vector_elt_as_int (rtx op, unsigned int elt)
4175 rtx tmp;
4177 /* We can't handle V2DImode and V2DFmode vector constants here yet. */
4178 gcc_assert (GET_MODE (op) != V2DImode
4179 && GET_MODE (op) != V2DFmode);
4181 tmp = CONST_VECTOR_ELT (op, elt);
4182 if (GET_MODE (op) == V4SFmode
4183 || GET_MODE (op) == V2SFmode)
4184 tmp = gen_lowpart (SImode, tmp);
4185 return INTVAL (tmp);
4188 /* Return true if OP can be synthesized with a particular vspltisb, vspltish
4189 or vspltisw instruction. OP is a CONST_VECTOR. Which instruction is used
4190 depends on STEP and COPIES, one of which will be 1. If COPIES > 1,
4191 all items are set to the same value and contain COPIES replicas of the
4192 vsplt's operand; if STEP > 1, one in STEP elements is set to the vsplt's
4193 operand and the others are set to the value of the operand's msb. */
4195 static bool
4196 vspltis_constant (rtx op, unsigned step, unsigned copies)
4198 enum machine_mode mode = GET_MODE (op);
4199 enum machine_mode inner = GET_MODE_INNER (mode);
4201 unsigned i;
4202 unsigned nunits;
4203 unsigned bitsize;
4204 unsigned mask;
4206 HOST_WIDE_INT val;
4207 HOST_WIDE_INT splat_val;
4208 HOST_WIDE_INT msb_val;
4210 if (mode == V2DImode || mode == V2DFmode)
4211 return false;
4213 nunits = GET_MODE_NUNITS (mode);
4214 bitsize = GET_MODE_BITSIZE (inner);
4215 mask = GET_MODE_MASK (inner);
4217 val = const_vector_elt_as_int (op, nunits - 1);
4218 splat_val = val;
4219 msb_val = val > 0 ? 0 : -1;
4221 /* Construct the value to be splatted, if possible. If not, return 0. */
4222 for (i = 2; i <= copies; i *= 2)
4224 HOST_WIDE_INT small_val;
4225 bitsize /= 2;
4226 small_val = splat_val >> bitsize;
4227 mask >>= bitsize;
4228 if (splat_val != ((small_val << bitsize) | (small_val & mask)))
4229 return false;
4230 splat_val = small_val;
4233 /* Check if SPLAT_VAL can really be the operand of a vspltis[bhw]. */
4234 if (EASY_VECTOR_15 (splat_val))
4237 /* Also check if we can splat, and then add the result to itself. Do so if
4238 the value is positive, of if the splat instruction is using OP's mode;
4239 for splat_val < 0, the splat and the add should use the same mode. */
4240 else if (EASY_VECTOR_15_ADD_SELF (splat_val)
4241 && (splat_val >= 0 || (step == 1 && copies == 1)))
4244 /* Also check if are loading up the most significant bit which can be done by
4245 loading up -1 and shifting the value left by -1. */
4246 else if (EASY_VECTOR_MSB (splat_val, inner))
4249 else
4250 return false;
4252 /* Check if VAL is present in every STEP-th element, and the
4253 other elements are filled with its most significant bit. */
4254 for (i = 0; i < nunits - 1; ++i)
4256 HOST_WIDE_INT desired_val;
4257 if (((i + 1) & (step - 1)) == 0)
4258 desired_val = val;
4259 else
4260 desired_val = msb_val;
4262 if (desired_val != const_vector_elt_as_int (op, i))
4263 return false;
4266 return true;
4270 /* Return true if OP is of the given MODE and can be synthesized
4271 with a vspltisb, vspltish or vspltisw. */
4273 bool
4274 easy_altivec_constant (rtx op, enum machine_mode mode)
4276 unsigned step, copies;
4278 if (mode == VOIDmode)
4279 mode = GET_MODE (op);
4280 else if (mode != GET_MODE (op))
4281 return false;
4283 /* V2DI/V2DF was added with VSX. Only allow 0 and all 1's as easy
4284 constants. */
4285 if (mode == V2DFmode)
4286 return zero_constant (op, mode);
4288 if (mode == V2DImode)
4290 /* In case the compiler is built 32-bit, CONST_DOUBLE constants are not
4291 easy. */
4292 if (GET_CODE (CONST_VECTOR_ELT (op, 0)) != CONST_INT
4293 || GET_CODE (CONST_VECTOR_ELT (op, 1)) != CONST_INT)
4294 return false;
4296 if (zero_constant (op, mode))
4297 return true;
4299 if (INTVAL (CONST_VECTOR_ELT (op, 0)) == -1
4300 && INTVAL (CONST_VECTOR_ELT (op, 1)) == -1)
4301 return true;
4303 return false;
4306 /* Start with a vspltisw. */
4307 step = GET_MODE_NUNITS (mode) / 4;
4308 copies = 1;
4310 if (vspltis_constant (op, step, copies))
4311 return true;
4313 /* Then try with a vspltish. */
4314 if (step == 1)
4315 copies <<= 1;
4316 else
4317 step >>= 1;
4319 if (vspltis_constant (op, step, copies))
4320 return true;
4322 /* And finally a vspltisb. */
4323 if (step == 1)
4324 copies <<= 1;
4325 else
4326 step >>= 1;
4328 if (vspltis_constant (op, step, copies))
4329 return true;
4331 return false;
4334 /* Generate a VEC_DUPLICATE representing a vspltis[bhw] instruction whose
4335 result is OP. Abort if it is not possible. */
4338 gen_easy_altivec_constant (rtx op)
4340 enum machine_mode mode = GET_MODE (op);
4341 int nunits = GET_MODE_NUNITS (mode);
4342 rtx last = CONST_VECTOR_ELT (op, nunits - 1);
4343 unsigned step = nunits / 4;
4344 unsigned copies = 1;
4346 /* Start with a vspltisw. */
4347 if (vspltis_constant (op, step, copies))
4348 return gen_rtx_VEC_DUPLICATE (V4SImode, gen_lowpart (SImode, last));
4350 /* Then try with a vspltish. */
4351 if (step == 1)
4352 copies <<= 1;
4353 else
4354 step >>= 1;
4356 if (vspltis_constant (op, step, copies))
4357 return gen_rtx_VEC_DUPLICATE (V8HImode, gen_lowpart (HImode, last));
4359 /* And finally a vspltisb. */
4360 if (step == 1)
4361 copies <<= 1;
4362 else
4363 step >>= 1;
4365 if (vspltis_constant (op, step, copies))
4366 return gen_rtx_VEC_DUPLICATE (V16QImode, gen_lowpart (QImode, last));
4368 gcc_unreachable ();
4371 const char *
4372 output_vec_const_move (rtx *operands)
4374 int cst, cst2;
4375 enum machine_mode mode;
4376 rtx dest, vec;
4378 dest = operands[0];
4379 vec = operands[1];
4380 mode = GET_MODE (dest);
4382 if (TARGET_VSX)
4384 if (zero_constant (vec, mode))
4385 return "xxlxor %x0,%x0,%x0";
4387 if (mode == V2DImode
4388 && INTVAL (CONST_VECTOR_ELT (vec, 0)) == -1
4389 && INTVAL (CONST_VECTOR_ELT (vec, 1)) == -1)
4390 return "vspltisw %0,-1";
4393 if (TARGET_ALTIVEC)
4395 rtx splat_vec;
4396 if (zero_constant (vec, mode))
4397 return "vxor %0,%0,%0";
4399 splat_vec = gen_easy_altivec_constant (vec);
4400 gcc_assert (GET_CODE (splat_vec) == VEC_DUPLICATE);
4401 operands[1] = XEXP (splat_vec, 0);
4402 if (!EASY_VECTOR_15 (INTVAL (operands[1])))
4403 return "#";
4405 switch (GET_MODE (splat_vec))
4407 case V4SImode:
4408 return "vspltisw %0,%1";
4410 case V8HImode:
4411 return "vspltish %0,%1";
4413 case V16QImode:
4414 return "vspltisb %0,%1";
4416 default:
4417 gcc_unreachable ();
4421 gcc_assert (TARGET_SPE);
4423 /* Vector constant 0 is handled as a splitter of V2SI, and in the
4424 pattern of V1DI, V4HI, and V2SF.
4426 FIXME: We should probably return # and add post reload
4427 splitters for these, but this way is so easy ;-). */
4428 cst = INTVAL (CONST_VECTOR_ELT (vec, 0));
4429 cst2 = INTVAL (CONST_VECTOR_ELT (vec, 1));
4430 operands[1] = CONST_VECTOR_ELT (vec, 0);
4431 operands[2] = CONST_VECTOR_ELT (vec, 1);
4432 if (cst == cst2)
4433 return "li %0,%1\n\tevmergelo %0,%0,%0";
4434 else
4435 return "li %0,%1\n\tevmergelo %0,%0,%0\n\tli %0,%2";
4438 /* Initialize TARGET of vector PAIRED to VALS. */
4440 void
4441 paired_expand_vector_init (rtx target, rtx vals)
4443 enum machine_mode mode = GET_MODE (target);
4444 int n_elts = GET_MODE_NUNITS (mode);
4445 int n_var = 0;
4446 rtx x, new_rtx, tmp, constant_op, op1, op2;
4447 int i;
4449 for (i = 0; i < n_elts; ++i)
4451 x = XVECEXP (vals, 0, i);
4452 if (!(CONST_INT_P (x)
4453 || GET_CODE (x) == CONST_DOUBLE
4454 || GET_CODE (x) == CONST_FIXED))
4455 ++n_var;
4457 if (n_var == 0)
4459 /* Load from constant pool. */
4460 emit_move_insn (target, gen_rtx_CONST_VECTOR (mode, XVEC (vals, 0)));
4461 return;
4464 if (n_var == 2)
4466 /* The vector is initialized only with non-constants. */
4467 new_rtx = gen_rtx_VEC_CONCAT (V2SFmode, XVECEXP (vals, 0, 0),
4468 XVECEXP (vals, 0, 1));
4470 emit_move_insn (target, new_rtx);
4471 return;
4474 /* One field is non-constant and the other one is a constant. Load the
4475 constant from the constant pool and use ps_merge instruction to
4476 construct the whole vector. */
4477 op1 = XVECEXP (vals, 0, 0);
4478 op2 = XVECEXP (vals, 0, 1);
4480 constant_op = (CONSTANT_P (op1)) ? op1 : op2;
4482 tmp = gen_reg_rtx (GET_MODE (constant_op));
4483 emit_move_insn (tmp, constant_op);
4485 if (CONSTANT_P (op1))
4486 new_rtx = gen_rtx_VEC_CONCAT (V2SFmode, tmp, op2);
4487 else
4488 new_rtx = gen_rtx_VEC_CONCAT (V2SFmode, op1, tmp);
4490 emit_move_insn (target, new_rtx);
4493 void
4494 paired_expand_vector_move (rtx operands[])
4496 rtx op0 = operands[0], op1 = operands[1];
4498 emit_move_insn (op0, op1);
4501 /* Emit vector compare for code RCODE. DEST is destination, OP1 and
4502 OP2 are two VEC_COND_EXPR operands, CC_OP0 and CC_OP1 are the two
4503 operands for the relation operation COND. This is a recursive
4504 function. */
4506 static void
4507 paired_emit_vector_compare (enum rtx_code rcode,
4508 rtx dest, rtx op0, rtx op1,
4509 rtx cc_op0, rtx cc_op1)
4511 rtx tmp = gen_reg_rtx (V2SFmode);
4512 rtx tmp1, max, min;
4514 gcc_assert (TARGET_PAIRED_FLOAT);
4515 gcc_assert (GET_MODE (op0) == GET_MODE (op1));
4517 switch (rcode)
4519 case LT:
4520 case LTU:
4521 paired_emit_vector_compare (GE, dest, op1, op0, cc_op0, cc_op1);
4522 return;
4523 case GE:
4524 case GEU:
4525 emit_insn (gen_subv2sf3 (tmp, cc_op0, cc_op1));
4526 emit_insn (gen_selv2sf4 (dest, tmp, op0, op1, CONST0_RTX (SFmode)));
4527 return;
4528 case LE:
4529 case LEU:
4530 paired_emit_vector_compare (GE, dest, op0, op1, cc_op1, cc_op0);
4531 return;
4532 case GT:
4533 paired_emit_vector_compare (LE, dest, op1, op0, cc_op0, cc_op1);
4534 return;
4535 case EQ:
4536 tmp1 = gen_reg_rtx (V2SFmode);
4537 max = gen_reg_rtx (V2SFmode);
4538 min = gen_reg_rtx (V2SFmode);
4539 gen_reg_rtx (V2SFmode);
4541 emit_insn (gen_subv2sf3 (tmp, cc_op0, cc_op1));
4542 emit_insn (gen_selv2sf4
4543 (max, tmp, cc_op0, cc_op1, CONST0_RTX (SFmode)));
4544 emit_insn (gen_subv2sf3 (tmp, cc_op1, cc_op0));
4545 emit_insn (gen_selv2sf4
4546 (min, tmp, cc_op0, cc_op1, CONST0_RTX (SFmode)));
4547 emit_insn (gen_subv2sf3 (tmp1, min, max));
4548 emit_insn (gen_selv2sf4 (dest, tmp1, op0, op1, CONST0_RTX (SFmode)));
4549 return;
4550 case NE:
4551 paired_emit_vector_compare (EQ, dest, op1, op0, cc_op0, cc_op1);
4552 return;
4553 case UNLE:
4554 paired_emit_vector_compare (LE, dest, op1, op0, cc_op0, cc_op1);
4555 return;
4556 case UNLT:
4557 paired_emit_vector_compare (LT, dest, op1, op0, cc_op0, cc_op1);
4558 return;
4559 case UNGE:
4560 paired_emit_vector_compare (GE, dest, op1, op0, cc_op0, cc_op1);
4561 return;
4562 case UNGT:
4563 paired_emit_vector_compare (GT, dest, op1, op0, cc_op0, cc_op1);
4564 return;
4565 default:
4566 gcc_unreachable ();
4569 return;
4572 /* Emit vector conditional expression.
4573 DEST is destination. OP1 and OP2 are two VEC_COND_EXPR operands.
4574 CC_OP0 and CC_OP1 are the two operands for the relation operation COND. */
4577 paired_emit_vector_cond_expr (rtx dest, rtx op1, rtx op2,
4578 rtx cond, rtx cc_op0, rtx cc_op1)
4580 enum rtx_code rcode = GET_CODE (cond);
4582 if (!TARGET_PAIRED_FLOAT)
4583 return 0;
4585 paired_emit_vector_compare (rcode, dest, op1, op2, cc_op0, cc_op1);
4587 return 1;
4590 /* Initialize vector TARGET to VALS. */
4592 void
4593 rs6000_expand_vector_init (rtx target, rtx vals)
4595 enum machine_mode mode = GET_MODE (target);
4596 enum machine_mode inner_mode = GET_MODE_INNER (mode);
4597 int n_elts = GET_MODE_NUNITS (mode);
4598 int n_var = 0, one_var = -1;
4599 bool all_same = true, all_const_zero = true;
4600 rtx x, mem;
4601 int i;
4603 for (i = 0; i < n_elts; ++i)
4605 x = XVECEXP (vals, 0, i);
4606 if (!(CONST_INT_P (x)
4607 || GET_CODE (x) == CONST_DOUBLE
4608 || GET_CODE (x) == CONST_FIXED))
4609 ++n_var, one_var = i;
4610 else if (x != CONST0_RTX (inner_mode))
4611 all_const_zero = false;
4613 if (i > 0 && !rtx_equal_p (x, XVECEXP (vals, 0, 0)))
4614 all_same = false;
4617 if (n_var == 0)
4619 rtx const_vec = gen_rtx_CONST_VECTOR (mode, XVEC (vals, 0));
4620 bool int_vector_p = (GET_MODE_CLASS (mode) == MODE_VECTOR_INT);
4621 if ((int_vector_p || TARGET_VSX) && all_const_zero)
4623 /* Zero register. */
4624 emit_insn (gen_rtx_SET (VOIDmode, target,
4625 gen_rtx_XOR (mode, target, target)));
4626 return;
4628 else if (int_vector_p && easy_vector_constant (const_vec, mode))
4630 /* Splat immediate. */
4631 emit_insn (gen_rtx_SET (VOIDmode, target, const_vec));
4632 return;
4634 else
4636 /* Load from constant pool. */
4637 emit_move_insn (target, const_vec);
4638 return;
4642 /* Double word values on VSX can use xxpermdi or lxvdsx. */
4643 if (VECTOR_MEM_VSX_P (mode) && (mode == V2DFmode || mode == V2DImode))
4645 rtx op0 = XVECEXP (vals, 0, 0);
4646 rtx op1 = XVECEXP (vals, 0, 1);
4647 if (all_same)
4649 if (!MEM_P (op0) && !REG_P (op0))
4650 op0 = force_reg (inner_mode, op0);
4651 if (mode == V2DFmode)
4652 emit_insn (gen_vsx_splat_v2df (target, op0));
4653 else
4654 emit_insn (gen_vsx_splat_v2di (target, op0));
4656 else
4658 op0 = force_reg (inner_mode, op0);
4659 op1 = force_reg (inner_mode, op1);
4660 if (mode == V2DFmode)
4661 emit_insn (gen_vsx_concat_v2df (target, op0, op1));
4662 else
4663 emit_insn (gen_vsx_concat_v2di (target, op0, op1));
4665 return;
4668 /* With single precision floating point on VSX, know that internally single
4669 precision is actually represented as a double, and either make 2 V2DF
4670 vectors, and convert these vectors to single precision, or do one
4671 conversion, and splat the result to the other elements. */
4672 if (mode == V4SFmode && VECTOR_MEM_VSX_P (mode))
4674 if (all_same)
4676 rtx freg = gen_reg_rtx (V4SFmode);
4677 rtx sreg = force_reg (SFmode, XVECEXP (vals, 0, 0));
4679 emit_insn (gen_vsx_xscvdpsp_scalar (freg, sreg));
4680 emit_insn (gen_vsx_xxspltw_v4sf (target, freg, const0_rtx));
4682 else
4684 rtx dbl_even = gen_reg_rtx (V2DFmode);
4685 rtx dbl_odd = gen_reg_rtx (V2DFmode);
4686 rtx flt_even = gen_reg_rtx (V4SFmode);
4687 rtx flt_odd = gen_reg_rtx (V4SFmode);
4688 rtx op0 = force_reg (SFmode, XVECEXP (vals, 0, 0));
4689 rtx op1 = force_reg (SFmode, XVECEXP (vals, 0, 1));
4690 rtx op2 = force_reg (SFmode, XVECEXP (vals, 0, 2));
4691 rtx op3 = force_reg (SFmode, XVECEXP (vals, 0, 3));
4693 emit_insn (gen_vsx_concat_v2sf (dbl_even, op0, op1));
4694 emit_insn (gen_vsx_concat_v2sf (dbl_odd, op2, op3));
4695 emit_insn (gen_vsx_xvcvdpsp (flt_even, dbl_even));
4696 emit_insn (gen_vsx_xvcvdpsp (flt_odd, dbl_odd));
4697 rs6000_expand_extract_even (target, flt_even, flt_odd);
4699 return;
4702 /* Store value to stack temp. Load vector element. Splat. However, splat
4703 of 64-bit items is not supported on Altivec. */
4704 if (all_same && GET_MODE_SIZE (inner_mode) <= 4)
4706 mem = assign_stack_temp (mode, GET_MODE_SIZE (inner_mode));
4707 emit_move_insn (adjust_address_nv (mem, inner_mode, 0),
4708 XVECEXP (vals, 0, 0));
4709 x = gen_rtx_UNSPEC (VOIDmode,
4710 gen_rtvec (1, const0_rtx), UNSPEC_LVE);
4711 emit_insn (gen_rtx_PARALLEL (VOIDmode,
4712 gen_rtvec (2,
4713 gen_rtx_SET (VOIDmode,
4714 target, mem),
4715 x)));
4716 x = gen_rtx_VEC_SELECT (inner_mode, target,
4717 gen_rtx_PARALLEL (VOIDmode,
4718 gen_rtvec (1, const0_rtx)));
4719 emit_insn (gen_rtx_SET (VOIDmode, target,
4720 gen_rtx_VEC_DUPLICATE (mode, x)));
4721 return;
4724 /* One field is non-constant. Load constant then overwrite
4725 varying field. */
4726 if (n_var == 1)
4728 rtx copy = copy_rtx (vals);
4730 /* Load constant part of vector, substitute neighboring value for
4731 varying element. */
4732 XVECEXP (copy, 0, one_var) = XVECEXP (vals, 0, (one_var + 1) % n_elts);
4733 rs6000_expand_vector_init (target, copy);
4735 /* Insert variable. */
4736 rs6000_expand_vector_set (target, XVECEXP (vals, 0, one_var), one_var);
4737 return;
4740 /* Construct the vector in memory one field at a time
4741 and load the whole vector. */
4742 mem = assign_stack_temp (mode, GET_MODE_SIZE (mode));
4743 for (i = 0; i < n_elts; i++)
4744 emit_move_insn (adjust_address_nv (mem, inner_mode,
4745 i * GET_MODE_SIZE (inner_mode)),
4746 XVECEXP (vals, 0, i));
4747 emit_move_insn (target, mem);
4750 /* Set field ELT of TARGET to VAL. */
4752 void
4753 rs6000_expand_vector_set (rtx target, rtx val, int elt)
4755 enum machine_mode mode = GET_MODE (target);
4756 enum machine_mode inner_mode = GET_MODE_INNER (mode);
4757 rtx reg = gen_reg_rtx (mode);
4758 rtx mask, mem, x;
4759 int width = GET_MODE_SIZE (inner_mode);
4760 int i;
4762 if (VECTOR_MEM_VSX_P (mode) && (mode == V2DFmode || mode == V2DImode))
4764 rtx (*set_func) (rtx, rtx, rtx, rtx)
4765 = ((mode == V2DFmode) ? gen_vsx_set_v2df : gen_vsx_set_v2di);
4766 emit_insn (set_func (target, target, val, GEN_INT (elt)));
4767 return;
4770 /* Load single variable value. */
4771 mem = assign_stack_temp (mode, GET_MODE_SIZE (inner_mode));
4772 emit_move_insn (adjust_address_nv (mem, inner_mode, 0), val);
4773 x = gen_rtx_UNSPEC (VOIDmode,
4774 gen_rtvec (1, const0_rtx), UNSPEC_LVE);
4775 emit_insn (gen_rtx_PARALLEL (VOIDmode,
4776 gen_rtvec (2,
4777 gen_rtx_SET (VOIDmode,
4778 reg, mem),
4779 x)));
4781 /* Linear sequence. */
4782 mask = gen_rtx_PARALLEL (V16QImode, rtvec_alloc (16));
4783 for (i = 0; i < 16; ++i)
4784 XVECEXP (mask, 0, i) = GEN_INT (i);
4786 /* Set permute mask to insert element into target. */
4787 for (i = 0; i < width; ++i)
4788 XVECEXP (mask, 0, elt*width + i)
4789 = GEN_INT (i + 0x10);
4790 x = gen_rtx_CONST_VECTOR (V16QImode, XVEC (mask, 0));
4791 x = gen_rtx_UNSPEC (mode,
4792 gen_rtvec (3, target, reg,
4793 force_reg (V16QImode, x)),
4794 UNSPEC_VPERM);
4795 emit_insn (gen_rtx_SET (VOIDmode, target, x));
4798 /* Extract field ELT from VEC into TARGET. */
4800 void
4801 rs6000_expand_vector_extract (rtx target, rtx vec, int elt)
4803 enum machine_mode mode = GET_MODE (vec);
4804 enum machine_mode inner_mode = GET_MODE_INNER (mode);
4805 rtx mem;
4807 if (VECTOR_MEM_VSX_P (mode))
4809 switch (mode)
4811 default:
4812 break;
4813 case V2DFmode:
4814 emit_insn (gen_vsx_extract_v2df (target, vec, GEN_INT (elt)));
4815 return;
4816 case V2DImode:
4817 emit_insn (gen_vsx_extract_v2di (target, vec, GEN_INT (elt)));
4818 return;
4819 case V4SFmode:
4820 emit_insn (gen_vsx_extract_v4sf (target, vec, GEN_INT (elt)));
4821 return;
4825 /* Allocate mode-sized buffer. */
4826 mem = assign_stack_temp (mode, GET_MODE_SIZE (mode));
4828 emit_move_insn (mem, vec);
4830 /* Add offset to field within buffer matching vector element. */
4831 mem = adjust_address_nv (mem, inner_mode, elt * GET_MODE_SIZE (inner_mode));
4833 emit_move_insn (target, adjust_address_nv (mem, inner_mode, 0));
4836 /* Generates shifts and masks for a pair of rldicl or rldicr insns to
4837 implement ANDing by the mask IN. */
4838 void
4839 build_mask64_2_operands (rtx in, rtx *out)
4841 #if HOST_BITS_PER_WIDE_INT >= 64
4842 unsigned HOST_WIDE_INT c, lsb, m1, m2;
4843 int shift;
4845 gcc_assert (GET_CODE (in) == CONST_INT);
4847 c = INTVAL (in);
4848 if (c & 1)
4850 /* Assume c initially something like 0x00fff000000fffff. The idea
4851 is to rotate the word so that the middle ^^^^^^ group of zeros
4852 is at the MS end and can be cleared with an rldicl mask. We then
4853 rotate back and clear off the MS ^^ group of zeros with a
4854 second rldicl. */
4855 c = ~c; /* c == 0xff000ffffff00000 */
4856 lsb = c & -c; /* lsb == 0x0000000000100000 */
4857 m1 = -lsb; /* m1 == 0xfffffffffff00000 */
4858 c = ~c; /* c == 0x00fff000000fffff */
4859 c &= -lsb; /* c == 0x00fff00000000000 */
4860 lsb = c & -c; /* lsb == 0x0000100000000000 */
4861 c = ~c; /* c == 0xff000fffffffffff */
4862 c &= -lsb; /* c == 0xff00000000000000 */
4863 shift = 0;
4864 while ((lsb >>= 1) != 0)
4865 shift++; /* shift == 44 on exit from loop */
4866 m1 <<= 64 - shift; /* m1 == 0xffffff0000000000 */
4867 m1 = ~m1; /* m1 == 0x000000ffffffffff */
4868 m2 = ~c; /* m2 == 0x00ffffffffffffff */
4870 else
4872 /* Assume c initially something like 0xff000f0000000000. The idea
4873 is to rotate the word so that the ^^^ middle group of zeros
4874 is at the LS end and can be cleared with an rldicr mask. We then
4875 rotate back and clear off the LS group of ^^^^^^^^^^ zeros with
4876 a second rldicr. */
4877 lsb = c & -c; /* lsb == 0x0000010000000000 */
4878 m2 = -lsb; /* m2 == 0xffffff0000000000 */
4879 c = ~c; /* c == 0x00fff0ffffffffff */
4880 c &= -lsb; /* c == 0x00fff00000000000 */
4881 lsb = c & -c; /* lsb == 0x0000100000000000 */
4882 c = ~c; /* c == 0xff000fffffffffff */
4883 c &= -lsb; /* c == 0xff00000000000000 */
4884 shift = 0;
4885 while ((lsb >>= 1) != 0)
4886 shift++; /* shift == 44 on exit from loop */
4887 m1 = ~c; /* m1 == 0x00ffffffffffffff */
4888 m1 >>= shift; /* m1 == 0x0000000000000fff */
4889 m1 = ~m1; /* m1 == 0xfffffffffffff000 */
4892 /* Note that when we only have two 0->1 and 1->0 transitions, one of the
4893 masks will be all 1's. We are guaranteed more than one transition. */
4894 out[0] = GEN_INT (64 - shift);
4895 out[1] = GEN_INT (m1);
4896 out[2] = GEN_INT (shift);
4897 out[3] = GEN_INT (m2);
4898 #else
4899 (void)in;
4900 (void)out;
4901 gcc_unreachable ();
4902 #endif
4905 /* Return TRUE if OP is an invalid SUBREG operation on the e500. */
4907 bool
4908 invalid_e500_subreg (rtx op, enum machine_mode mode)
4910 if (TARGET_E500_DOUBLE)
4912 /* Reject (subreg:SI (reg:DF)); likewise with subreg:DI or
4913 subreg:TI and reg:TF. Decimal float modes are like integer
4914 modes (only low part of each register used) for this
4915 purpose. */
4916 if (GET_CODE (op) == SUBREG
4917 && (mode == SImode || mode == DImode || mode == TImode
4918 || mode == DDmode || mode == TDmode)
4919 && REG_P (SUBREG_REG (op))
4920 && (GET_MODE (SUBREG_REG (op)) == DFmode
4921 || GET_MODE (SUBREG_REG (op)) == TFmode))
4922 return true;
4924 /* Reject (subreg:DF (reg:DI)); likewise with subreg:TF and
4925 reg:TI. */
4926 if (GET_CODE (op) == SUBREG
4927 && (mode == DFmode || mode == TFmode)
4928 && REG_P (SUBREG_REG (op))
4929 && (GET_MODE (SUBREG_REG (op)) == DImode
4930 || GET_MODE (SUBREG_REG (op)) == TImode
4931 || GET_MODE (SUBREG_REG (op)) == DDmode
4932 || GET_MODE (SUBREG_REG (op)) == TDmode))
4933 return true;
4936 if (TARGET_SPE
4937 && GET_CODE (op) == SUBREG
4938 && mode == SImode
4939 && REG_P (SUBREG_REG (op))
4940 && SPE_VECTOR_MODE (GET_MODE (SUBREG_REG (op))))
4941 return true;
4943 return false;
4946 /* AIX increases natural record alignment to doubleword if the first
4947 field is an FP double while the FP fields remain word aligned. */
4949 unsigned int
4950 rs6000_special_round_type_align (tree type, unsigned int computed,
4951 unsigned int specified)
4953 unsigned int align = MAX (computed, specified);
4954 tree field = TYPE_FIELDS (type);
4956 /* Skip all non field decls */
4957 while (field != NULL && TREE_CODE (field) != FIELD_DECL)
4958 field = DECL_CHAIN (field);
4960 if (field != NULL && field != type)
4962 type = TREE_TYPE (field);
4963 while (TREE_CODE (type) == ARRAY_TYPE)
4964 type = TREE_TYPE (type);
4966 if (type != error_mark_node && TYPE_MODE (type) == DFmode)
4967 align = MAX (align, 64);
4970 return align;
4973 /* Darwin increases record alignment to the natural alignment of
4974 the first field. */
4976 unsigned int
4977 darwin_rs6000_special_round_type_align (tree type, unsigned int computed,
4978 unsigned int specified)
4980 unsigned int align = MAX (computed, specified);
4982 if (TYPE_PACKED (type))
4983 return align;
4985 /* Find the first field, looking down into aggregates. */
4986 do {
4987 tree field = TYPE_FIELDS (type);
4988 /* Skip all non field decls */
4989 while (field != NULL && TREE_CODE (field) != FIELD_DECL)
4990 field = DECL_CHAIN (field);
4991 if (! field)
4992 break;
4993 /* A packed field does not contribute any extra alignment. */
4994 if (DECL_PACKED (field))
4995 return align;
4996 type = TREE_TYPE (field);
4997 while (TREE_CODE (type) == ARRAY_TYPE)
4998 type = TREE_TYPE (type);
4999 } while (AGGREGATE_TYPE_P (type));
5001 if (! AGGREGATE_TYPE_P (type) && type != error_mark_node)
5002 align = MAX (align, TYPE_ALIGN (type));
5004 return align;
5007 /* Return 1 for an operand in small memory on V.4/eabi. */
5010 small_data_operand (rtx op ATTRIBUTE_UNUSED,
5011 enum machine_mode mode ATTRIBUTE_UNUSED)
5013 #if TARGET_ELF
5014 rtx sym_ref;
5016 if (rs6000_sdata == SDATA_NONE || rs6000_sdata == SDATA_DATA)
5017 return 0;
5019 if (DEFAULT_ABI != ABI_V4)
5020 return 0;
5022 /* Vector and float memory instructions have a limited offset on the
5023 SPE, so using a vector or float variable directly as an operand is
5024 not useful. */
5025 if (TARGET_SPE
5026 && (SPE_VECTOR_MODE (mode) || FLOAT_MODE_P (mode)))
5027 return 0;
5029 if (GET_CODE (op) == SYMBOL_REF)
5030 sym_ref = op;
5032 else if (GET_CODE (op) != CONST
5033 || GET_CODE (XEXP (op, 0)) != PLUS
5034 || GET_CODE (XEXP (XEXP (op, 0), 0)) != SYMBOL_REF
5035 || GET_CODE (XEXP (XEXP (op, 0), 1)) != CONST_INT)
5036 return 0;
5038 else
5040 rtx sum = XEXP (op, 0);
5041 HOST_WIDE_INT summand;
5043 /* We have to be careful here, because it is the referenced address
5044 that must be 32k from _SDA_BASE_, not just the symbol. */
5045 summand = INTVAL (XEXP (sum, 1));
5046 if (summand < 0 || summand > g_switch_value)
5047 return 0;
5049 sym_ref = XEXP (sum, 0);
5052 return SYMBOL_REF_SMALL_P (sym_ref);
5053 #else
5054 return 0;
5055 #endif
5058 /* Return true if either operand is a general purpose register. */
5060 bool
5061 gpr_or_gpr_p (rtx op0, rtx op1)
5063 return ((REG_P (op0) && INT_REGNO_P (REGNO (op0)))
5064 || (REG_P (op1) && INT_REGNO_P (REGNO (op1))));
5067 /* Given an address, return a constant offset term if one exists. */
5069 static rtx
5070 address_offset (rtx op)
5072 if (GET_CODE (op) == PRE_INC
5073 || GET_CODE (op) == PRE_DEC)
5074 op = XEXP (op, 0);
5075 else if (GET_CODE (op) == PRE_MODIFY
5076 || GET_CODE (op) == LO_SUM)
5077 op = XEXP (op, 1);
5079 if (GET_CODE (op) == CONST)
5080 op = XEXP (op, 0);
5082 if (GET_CODE (op) == PLUS)
5083 op = XEXP (op, 1);
5085 if (CONST_INT_P (op))
5086 return op;
5088 return NULL_RTX;
5091 /* Return true if the MEM operand is a memory operand suitable for use
5092 with a (full width, possibly multiple) gpr load/store. On
5093 powerpc64 this means the offset must be divisible by 4.
5094 Implements 'Y' constraint.
5096 Accept direct, indexed, offset, lo_sum and tocref. Since this is
5097 a constraint function we know the operand has satisfied a suitable
5098 memory predicate. Also accept some odd rtl generated by reload
5099 (see rs6000_legitimize_reload_address for various forms). It is
5100 important that reload rtl be accepted by appropriate constraints
5101 but not by the operand predicate.
5103 Offsetting a lo_sum should not be allowed, except where we know by
5104 alignment that a 32k boundary is not crossed, but see the ???
5105 comment in rs6000_legitimize_reload_address. Note that by
5106 "offsetting" here we mean a further offset to access parts of the
5107 MEM. It's fine to have a lo_sum where the inner address is offset
5108 from a sym, since the same sym+offset will appear in the high part
5109 of the address calculation. */
5111 bool
5112 mem_operand_gpr (rtx op, enum machine_mode mode)
5114 unsigned HOST_WIDE_INT offset;
5115 int extra;
5116 rtx addr = XEXP (op, 0);
5118 op = address_offset (addr);
5119 if (op == NULL_RTX)
5120 return true;
5122 offset = INTVAL (op);
5123 if (TARGET_POWERPC64 && (offset & 3) != 0)
5124 return false;
5126 if (GET_CODE (addr) == LO_SUM)
5127 /* We know by alignment that ABI_AIX medium/large model toc refs
5128 will not cross a 32k boundary, since all entries in the
5129 constant pool are naturally aligned and we check alignment for
5130 other medium model toc-relative addresses. For ABI_V4 and
5131 ABI_DARWIN lo_sum addresses, we just check that 64-bit
5132 offsets are 4-byte aligned. */
5133 return true;
5135 extra = GET_MODE_SIZE (mode) - UNITS_PER_WORD;
5136 gcc_assert (extra >= 0);
5137 return offset + 0x8000 < 0x10000u - extra;
5140 /* Subroutines of rs6000_legitimize_address and rs6000_legitimate_address_p. */
5142 static bool
5143 reg_offset_addressing_ok_p (enum machine_mode mode)
5145 switch (mode)
5147 case V16QImode:
5148 case V8HImode:
5149 case V4SFmode:
5150 case V4SImode:
5151 case V2DFmode:
5152 case V2DImode:
5153 /* AltiVec/VSX vector modes. Only reg+reg addressing is valid. */
5154 if (VECTOR_MEM_ALTIVEC_OR_VSX_P (mode))
5155 return false;
5156 break;
5158 case V4HImode:
5159 case V2SImode:
5160 case V1DImode:
5161 case V2SFmode:
5162 /* Paired vector modes. Only reg+reg addressing is valid. */
5163 if (TARGET_PAIRED_FLOAT)
5164 return false;
5165 break;
5167 default:
5168 break;
5171 return true;
5174 static bool
5175 virtual_stack_registers_memory_p (rtx op)
5177 int regnum;
5179 if (GET_CODE (op) == REG)
5180 regnum = REGNO (op);
5182 else if (GET_CODE (op) == PLUS
5183 && GET_CODE (XEXP (op, 0)) == REG
5184 && GET_CODE (XEXP (op, 1)) == CONST_INT)
5185 regnum = REGNO (XEXP (op, 0));
5187 else
5188 return false;
5190 return (regnum >= FIRST_VIRTUAL_REGISTER
5191 && regnum <= LAST_VIRTUAL_POINTER_REGISTER);
5194 /* Return true if memory accesses to OP are known to never straddle
5195 a 32k boundary. */
5197 static bool
5198 offsettable_ok_by_alignment (rtx op, HOST_WIDE_INT offset,
5199 enum machine_mode mode)
5201 tree decl, type;
5202 unsigned HOST_WIDE_INT dsize, dalign;
5204 if (GET_CODE (op) != SYMBOL_REF)
5205 return false;
5207 decl = SYMBOL_REF_DECL (op);
5208 if (!decl)
5210 if (GET_MODE_SIZE (mode) == 0)
5211 return false;
5213 /* -fsection-anchors loses the original SYMBOL_REF_DECL when
5214 replacing memory addresses with an anchor plus offset. We
5215 could find the decl by rummaging around in the block->objects
5216 VEC for the given offset but that seems like too much work. */
5217 dalign = 1;
5218 if (SYMBOL_REF_HAS_BLOCK_INFO_P (op)
5219 && SYMBOL_REF_ANCHOR_P (op)
5220 && SYMBOL_REF_BLOCK (op) != NULL)
5222 struct object_block *block = SYMBOL_REF_BLOCK (op);
5223 HOST_WIDE_INT lsb, mask;
5225 /* Given the alignment of the block.. */
5226 dalign = block->alignment;
5227 mask = dalign / BITS_PER_UNIT - 1;
5229 /* ..and the combined offset of the anchor and any offset
5230 to this block object.. */
5231 offset += SYMBOL_REF_BLOCK_OFFSET (op);
5232 lsb = offset & -offset;
5234 /* ..find how many bits of the alignment we know for the
5235 object. */
5236 mask &= lsb - 1;
5237 dalign = mask + 1;
5239 return dalign >= GET_MODE_SIZE (mode);
5242 if (DECL_P (decl))
5244 if (TREE_CODE (decl) == FUNCTION_DECL)
5245 return true;
5247 if (!DECL_SIZE_UNIT (decl))
5248 return false;
5250 if (!host_integerp (DECL_SIZE_UNIT (decl), 1))
5251 return false;
5253 dsize = tree_low_cst (DECL_SIZE_UNIT (decl), 1);
5254 if (dsize > 32768)
5255 return false;
5257 dalign = DECL_ALIGN_UNIT (decl);
5258 return dalign >= dsize;
5261 type = TREE_TYPE (decl);
5263 if (TREE_CODE (decl) == STRING_CST)
5264 dsize = TREE_STRING_LENGTH (decl);
5265 else if (TYPE_SIZE_UNIT (type)
5266 && host_integerp (TYPE_SIZE_UNIT (type), 1))
5267 dsize = tree_low_cst (TYPE_SIZE_UNIT (type), 1);
5268 else
5269 return false;
5270 if (dsize > 32768)
5271 return false;
5273 dalign = TYPE_ALIGN (type);
5274 if (CONSTANT_CLASS_P (decl))
5275 dalign = CONSTANT_ALIGNMENT (decl, dalign);
5276 else
5277 dalign = DATA_ALIGNMENT (decl, dalign);
5278 dalign /= BITS_PER_UNIT;
5279 return dalign >= dsize;
5282 static bool
5283 constant_pool_expr_p (rtx op)
5285 rtx base, offset;
5287 split_const (op, &base, &offset);
5288 return (GET_CODE (base) == SYMBOL_REF
5289 && CONSTANT_POOL_ADDRESS_P (base)
5290 && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (base), Pmode));
5293 static const_rtx tocrel_base, tocrel_offset;
5295 /* Return true if OP is a toc pointer relative address (the output
5296 of create_TOC_reference). If STRICT, do not match high part or
5297 non-split -mcmodel=large/medium toc pointer relative addresses. */
5299 bool
5300 toc_relative_expr_p (const_rtx op, bool strict)
5302 if (!TARGET_TOC)
5303 return false;
5305 if (TARGET_CMODEL != CMODEL_SMALL)
5307 /* Only match the low part. */
5308 if (GET_CODE (op) == LO_SUM
5309 && REG_P (XEXP (op, 0))
5310 && INT_REG_OK_FOR_BASE_P (XEXP (op, 0), strict))
5311 op = XEXP (op, 1);
5312 else if (strict)
5313 return false;
5316 tocrel_base = op;
5317 tocrel_offset = const0_rtx;
5318 if (GET_CODE (op) == PLUS && CONST_INT_P (XEXP (op, 1)))
5320 tocrel_base = XEXP (op, 0);
5321 tocrel_offset = XEXP (op, 1);
5324 return (GET_CODE (tocrel_base) == UNSPEC
5325 && XINT (tocrel_base, 1) == UNSPEC_TOCREL);
5328 /* Return true if X is a constant pool address, and also for cmodel=medium
5329 if X is a toc-relative address known to be offsettable within MODE. */
5331 bool
5332 legitimate_constant_pool_address_p (const_rtx x, enum machine_mode mode,
5333 bool strict)
5335 return (toc_relative_expr_p (x, strict)
5336 && (TARGET_CMODEL != CMODEL_MEDIUM
5337 || constant_pool_expr_p (XVECEXP (tocrel_base, 0, 0))
5338 || mode == QImode
5339 || offsettable_ok_by_alignment (XVECEXP (tocrel_base, 0, 0),
5340 INTVAL (tocrel_offset), mode)));
5343 static bool
5344 legitimate_small_data_p (enum machine_mode mode, rtx x)
5346 return (DEFAULT_ABI == ABI_V4
5347 && !flag_pic && !TARGET_TOC
5348 && (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == CONST)
5349 && small_data_operand (x, mode));
5352 /* SPE offset addressing is limited to 5-bits worth of double words. */
5353 #define SPE_CONST_OFFSET_OK(x) (((x) & ~0xf8) == 0)
5355 bool
5356 rs6000_legitimate_offset_address_p (enum machine_mode mode, rtx x,
5357 bool strict, bool worst_case)
5359 unsigned HOST_WIDE_INT offset;
5360 unsigned int extra;
5362 if (GET_CODE (x) != PLUS)
5363 return false;
5364 if (!REG_P (XEXP (x, 0)))
5365 return false;
5366 if (!INT_REG_OK_FOR_BASE_P (XEXP (x, 0), strict))
5367 return false;
5368 if (!reg_offset_addressing_ok_p (mode))
5369 return virtual_stack_registers_memory_p (x);
5370 if (legitimate_constant_pool_address_p (x, mode, strict))
5371 return true;
5372 if (GET_CODE (XEXP (x, 1)) != CONST_INT)
5373 return false;
5375 offset = INTVAL (XEXP (x, 1));
5376 extra = 0;
5377 switch (mode)
5379 case V4HImode:
5380 case V2SImode:
5381 case V1DImode:
5382 case V2SFmode:
5383 /* SPE vector modes. */
5384 return SPE_CONST_OFFSET_OK (offset);
5386 case DFmode:
5387 case DDmode:
5388 case DImode:
5389 /* On e500v2, we may have:
5391 (subreg:DF (mem:DI (plus (reg) (const_int))) 0).
5393 Which gets addressed with evldd instructions. */
5394 if (TARGET_E500_DOUBLE)
5395 return SPE_CONST_OFFSET_OK (offset);
5397 /* If we are using VSX scalar loads, restrict ourselves to reg+reg
5398 addressing. */
5399 if (mode == DFmode && VECTOR_MEM_VSX_P (DFmode))
5400 return false;
5402 if (!worst_case)
5403 break;
5404 if (!TARGET_POWERPC64)
5405 extra = 4;
5406 else if (offset & 3)
5407 return false;
5408 break;
5410 case TFmode:
5411 case TDmode:
5412 case TImode:
5413 if (TARGET_E500_DOUBLE)
5414 return (SPE_CONST_OFFSET_OK (offset)
5415 && SPE_CONST_OFFSET_OK (offset + 8));
5417 extra = 8;
5418 if (!worst_case)
5419 break;
5420 if (!TARGET_POWERPC64)
5421 extra = 12;
5422 else if (offset & 3)
5423 return false;
5424 break;
5426 default:
5427 break;
5430 offset += 0x8000;
5431 return offset < 0x10000 - extra;
5434 bool
5435 legitimate_indexed_address_p (rtx x, int strict)
5437 rtx op0, op1;
5439 if (GET_CODE (x) != PLUS)
5440 return false;
5442 op0 = XEXP (x, 0);
5443 op1 = XEXP (x, 1);
5445 /* Recognize the rtl generated by reload which we know will later be
5446 replaced with proper base and index regs. */
5447 if (!strict
5448 && reload_in_progress
5449 && (REG_P (op0) || GET_CODE (op0) == PLUS)
5450 && REG_P (op1))
5451 return true;
5453 return (REG_P (op0) && REG_P (op1)
5454 && ((INT_REG_OK_FOR_BASE_P (op0, strict)
5455 && INT_REG_OK_FOR_INDEX_P (op1, strict))
5456 || (INT_REG_OK_FOR_BASE_P (op1, strict)
5457 && INT_REG_OK_FOR_INDEX_P (op0, strict))));
5460 bool
5461 avoiding_indexed_address_p (enum machine_mode mode)
5463 /* Avoid indexed addressing for modes that have non-indexed
5464 load/store instruction forms. */
5465 return (TARGET_AVOID_XFORM && VECTOR_MEM_NONE_P (mode));
5468 bool
5469 legitimate_indirect_address_p (rtx x, int strict)
5471 return GET_CODE (x) == REG && INT_REG_OK_FOR_BASE_P (x, strict);
5474 bool
5475 macho_lo_sum_memory_operand (rtx x, enum machine_mode mode)
5477 if (!TARGET_MACHO || !flag_pic
5478 || mode != SImode || GET_CODE (x) != MEM)
5479 return false;
5480 x = XEXP (x, 0);
5482 if (GET_CODE (x) != LO_SUM)
5483 return false;
5484 if (GET_CODE (XEXP (x, 0)) != REG)
5485 return false;
5486 if (!INT_REG_OK_FOR_BASE_P (XEXP (x, 0), 0))
5487 return false;
5488 x = XEXP (x, 1);
5490 return CONSTANT_P (x);
5493 static bool
5494 legitimate_lo_sum_address_p (enum machine_mode mode, rtx x, int strict)
5496 if (GET_CODE (x) != LO_SUM)
5497 return false;
5498 if (GET_CODE (XEXP (x, 0)) != REG)
5499 return false;
5500 if (!INT_REG_OK_FOR_BASE_P (XEXP (x, 0), strict))
5501 return false;
5502 /* Restrict addressing for DI because of our SUBREG hackery. */
5503 if (TARGET_E500_DOUBLE && GET_MODE_SIZE (mode) > UNITS_PER_WORD)
5504 return false;
5505 x = XEXP (x, 1);
5507 if (TARGET_ELF || TARGET_MACHO)
5509 if (DEFAULT_ABI != ABI_AIX && DEFAULT_ABI != ABI_DARWIN && flag_pic)
5510 return false;
5511 if (TARGET_TOC)
5512 return false;
5513 if (GET_MODE_NUNITS (mode) != 1)
5514 return false;
5515 if (GET_MODE_SIZE (mode) > UNITS_PER_WORD
5516 && !(/* ??? Assume floating point reg based on mode? */
5517 TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT
5518 && (mode == DFmode || mode == DDmode)))
5519 return false;
5521 return CONSTANT_P (x);
5524 return false;
5528 /* Try machine-dependent ways of modifying an illegitimate address
5529 to be legitimate. If we find one, return the new, valid address.
5530 This is used from only one place: `memory_address' in explow.c.
5532 OLDX is the address as it was before break_out_memory_refs was
5533 called. In some cases it is useful to look at this to decide what
5534 needs to be done.
5536 It is always safe for this function to do nothing. It exists to
5537 recognize opportunities to optimize the output.
5539 On RS/6000, first check for the sum of a register with a constant
5540 integer that is out of range. If so, generate code to add the
5541 constant with the low-order 16 bits masked to the register and force
5542 this result into another register (this can be done with `cau').
5543 Then generate an address of REG+(CONST&0xffff), allowing for the
5544 possibility of bit 16 being a one.
5546 Then check for the sum of a register and something not constant, try to
5547 load the other things into a register and return the sum. */
5549 static rtx
5550 rs6000_legitimize_address (rtx x, rtx oldx ATTRIBUTE_UNUSED,
5551 enum machine_mode mode)
5553 unsigned int extra;
5555 if (!reg_offset_addressing_ok_p (mode))
5557 if (virtual_stack_registers_memory_p (x))
5558 return x;
5560 /* In theory we should not be seeing addresses of the form reg+0,
5561 but just in case it is generated, optimize it away. */
5562 if (GET_CODE (x) == PLUS && XEXP (x, 1) == const0_rtx)
5563 return force_reg (Pmode, XEXP (x, 0));
5565 /* Make sure both operands are registers. */
5566 else if (GET_CODE (x) == PLUS)
5567 return gen_rtx_PLUS (Pmode,
5568 force_reg (Pmode, XEXP (x, 0)),
5569 force_reg (Pmode, XEXP (x, 1)));
5570 else
5571 return force_reg (Pmode, x);
5573 if (GET_CODE (x) == SYMBOL_REF)
5575 enum tls_model model = SYMBOL_REF_TLS_MODEL (x);
5576 if (model != 0)
5577 return rs6000_legitimize_tls_address (x, model);
5580 extra = 0;
5581 switch (mode)
5583 case TFmode:
5584 case TDmode:
5585 case TImode:
5586 /* As in legitimate_offset_address_p we do not assume
5587 worst-case. The mode here is just a hint as to the registers
5588 used. A TImode is usually in gprs, but may actually be in
5589 fprs. Leave worst-case scenario for reload to handle via
5590 insn constraints. */
5591 extra = 8;
5592 break;
5593 default:
5594 break;
5597 if (GET_CODE (x) == PLUS
5598 && GET_CODE (XEXP (x, 0)) == REG
5599 && GET_CODE (XEXP (x, 1)) == CONST_INT
5600 && ((unsigned HOST_WIDE_INT) (INTVAL (XEXP (x, 1)) + 0x8000)
5601 >= 0x10000 - extra)
5602 && !(SPE_VECTOR_MODE (mode)
5603 || (TARGET_E500_DOUBLE && GET_MODE_SIZE (mode) > UNITS_PER_WORD)))
5605 HOST_WIDE_INT high_int, low_int;
5606 rtx sum;
5607 low_int = ((INTVAL (XEXP (x, 1)) & 0xffff) ^ 0x8000) - 0x8000;
5608 if (low_int >= 0x8000 - extra)
5609 low_int = 0;
5610 high_int = INTVAL (XEXP (x, 1)) - low_int;
5611 sum = force_operand (gen_rtx_PLUS (Pmode, XEXP (x, 0),
5612 GEN_INT (high_int)), 0);
5613 return plus_constant (Pmode, sum, low_int);
5615 else if (GET_CODE (x) == PLUS
5616 && GET_CODE (XEXP (x, 0)) == REG
5617 && GET_CODE (XEXP (x, 1)) != CONST_INT
5618 && GET_MODE_NUNITS (mode) == 1
5619 && (GET_MODE_SIZE (mode) <= UNITS_PER_WORD
5620 || (/* ??? Assume floating point reg based on mode? */
5621 (TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT)
5622 && (mode == DFmode || mode == DDmode)))
5623 && !avoiding_indexed_address_p (mode))
5625 return gen_rtx_PLUS (Pmode, XEXP (x, 0),
5626 force_reg (Pmode, force_operand (XEXP (x, 1), 0)));
5628 else if (SPE_VECTOR_MODE (mode)
5629 || (TARGET_E500_DOUBLE && GET_MODE_SIZE (mode) > UNITS_PER_WORD))
5631 if (mode == DImode)
5632 return x;
5633 /* We accept [reg + reg] and [reg + OFFSET]. */
5635 if (GET_CODE (x) == PLUS)
5637 rtx op1 = XEXP (x, 0);
5638 rtx op2 = XEXP (x, 1);
5639 rtx y;
5641 op1 = force_reg (Pmode, op1);
5643 if (GET_CODE (op2) != REG
5644 && (GET_CODE (op2) != CONST_INT
5645 || !SPE_CONST_OFFSET_OK (INTVAL (op2))
5646 || (GET_MODE_SIZE (mode) > 8
5647 && !SPE_CONST_OFFSET_OK (INTVAL (op2) + 8))))
5648 op2 = force_reg (Pmode, op2);
5650 /* We can't always do [reg + reg] for these, because [reg +
5651 reg + offset] is not a legitimate addressing mode. */
5652 y = gen_rtx_PLUS (Pmode, op1, op2);
5654 if ((GET_MODE_SIZE (mode) > 8 || mode == DDmode) && REG_P (op2))
5655 return force_reg (Pmode, y);
5656 else
5657 return y;
5660 return force_reg (Pmode, x);
5662 else if ((TARGET_ELF
5663 #if TARGET_MACHO
5664 || !MACHO_DYNAMIC_NO_PIC_P
5665 #endif
5667 && TARGET_32BIT
5668 && TARGET_NO_TOC
5669 && ! flag_pic
5670 && GET_CODE (x) != CONST_INT
5671 && GET_CODE (x) != CONST_DOUBLE
5672 && CONSTANT_P (x)
5673 && GET_MODE_NUNITS (mode) == 1
5674 && (GET_MODE_SIZE (mode) <= UNITS_PER_WORD
5675 || (/* ??? Assume floating point reg based on mode? */
5676 (TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT)
5677 && (mode == DFmode || mode == DDmode))))
5679 rtx reg = gen_reg_rtx (Pmode);
5680 if (TARGET_ELF)
5681 emit_insn (gen_elf_high (reg, x));
5682 else
5683 emit_insn (gen_macho_high (reg, x));
5684 return gen_rtx_LO_SUM (Pmode, reg, x);
5686 else if (TARGET_TOC
5687 && GET_CODE (x) == SYMBOL_REF
5688 && constant_pool_expr_p (x)
5689 && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (x), Pmode))
5690 return create_TOC_reference (x, NULL_RTX);
5691 else
5692 return x;
5695 /* Debug version of rs6000_legitimize_address. */
5696 static rtx
5697 rs6000_debug_legitimize_address (rtx x, rtx oldx, enum machine_mode mode)
5699 rtx ret;
5700 rtx insns;
5702 start_sequence ();
5703 ret = rs6000_legitimize_address (x, oldx, mode);
5704 insns = get_insns ();
5705 end_sequence ();
5707 if (ret != x)
5709 fprintf (stderr,
5710 "\nrs6000_legitimize_address: mode %s, old code %s, "
5711 "new code %s, modified\n",
5712 GET_MODE_NAME (mode), GET_RTX_NAME (GET_CODE (x)),
5713 GET_RTX_NAME (GET_CODE (ret)));
5715 fprintf (stderr, "Original address:\n");
5716 debug_rtx (x);
5718 fprintf (stderr, "oldx:\n");
5719 debug_rtx (oldx);
5721 fprintf (stderr, "New address:\n");
5722 debug_rtx (ret);
5724 if (insns)
5726 fprintf (stderr, "Insns added:\n");
5727 debug_rtx_list (insns, 20);
5730 else
5732 fprintf (stderr,
5733 "\nrs6000_legitimize_address: mode %s, code %s, no change:\n",
5734 GET_MODE_NAME (mode), GET_RTX_NAME (GET_CODE (x)));
5736 debug_rtx (x);
5739 if (insns)
5740 emit_insn (insns);
5742 return ret;
5745 /* This is called from dwarf2out.c via TARGET_ASM_OUTPUT_DWARF_DTPREL.
5746 We need to emit DTP-relative relocations. */
5748 static void rs6000_output_dwarf_dtprel (FILE *, int, rtx) ATTRIBUTE_UNUSED;
5749 static void
5750 rs6000_output_dwarf_dtprel (FILE *file, int size, rtx x)
5752 switch (size)
5754 case 4:
5755 fputs ("\t.long\t", file);
5756 break;
5757 case 8:
5758 fputs (DOUBLE_INT_ASM_OP, file);
5759 break;
5760 default:
5761 gcc_unreachable ();
5763 output_addr_const (file, x);
5764 fputs ("@dtprel+0x8000", file);
5767 /* In the name of slightly smaller debug output, and to cater to
5768 general assembler lossage, recognize various UNSPEC sequences
5769 and turn them back into a direct symbol reference. */
5771 static rtx
5772 rs6000_delegitimize_address (rtx orig_x)
5774 rtx x, y, offset;
5776 orig_x = delegitimize_mem_from_attrs (orig_x);
5777 x = orig_x;
5778 if (MEM_P (x))
5779 x = XEXP (x, 0);
5781 y = x;
5782 if (TARGET_CMODEL != CMODEL_SMALL
5783 && GET_CODE (y) == LO_SUM)
5784 y = XEXP (y, 1);
5786 offset = NULL_RTX;
5787 if (GET_CODE (y) == PLUS
5788 && GET_MODE (y) == Pmode
5789 && CONST_INT_P (XEXP (y, 1)))
5791 offset = XEXP (y, 1);
5792 y = XEXP (y, 0);
5795 if (GET_CODE (y) == UNSPEC
5796 && XINT (y, 1) == UNSPEC_TOCREL)
5798 #ifdef ENABLE_CHECKING
5799 if (REG_P (XVECEXP (y, 0, 1))
5800 && REGNO (XVECEXP (y, 0, 1)) == TOC_REGISTER)
5802 /* All good. */
5804 else if (GET_CODE (XVECEXP (y, 0, 1)) == DEBUG_EXPR)
5806 /* Weirdness alert. df_note_compute can replace r2 with a
5807 debug_expr when this unspec is in a debug_insn.
5808 Seen in gcc.dg/pr51957-1.c */
5810 else
5812 debug_rtx (orig_x);
5813 abort ();
5815 #endif
5816 y = XVECEXP (y, 0, 0);
5817 if (offset != NULL_RTX)
5818 y = gen_rtx_PLUS (Pmode, y, offset);
5819 if (!MEM_P (orig_x))
5820 return y;
5821 else
5822 return replace_equiv_address_nv (orig_x, y);
5825 if (TARGET_MACHO
5826 && GET_CODE (orig_x) == LO_SUM
5827 && GET_CODE (XEXP (orig_x, 1)) == CONST)
5829 y = XEXP (XEXP (orig_x, 1), 0);
5830 if (GET_CODE (y) == UNSPEC
5831 && XINT (y, 1) == UNSPEC_MACHOPIC_OFFSET)
5832 return XVECEXP (y, 0, 0);
5835 return orig_x;
5838 /* Return true if X shouldn't be emitted into the debug info.
5839 The linker doesn't like .toc section references from
5840 .debug_* sections, so reject .toc section symbols. */
5842 static bool
5843 rs6000_const_not_ok_for_debug_p (rtx x)
5845 if (GET_CODE (x) == SYMBOL_REF
5846 && CONSTANT_POOL_ADDRESS_P (x))
5848 rtx c = get_pool_constant (x);
5849 enum machine_mode cmode = get_pool_mode (x);
5850 if (ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (c, cmode))
5851 return true;
5854 return false;
5857 /* Construct the SYMBOL_REF for the tls_get_addr function. */
5859 static GTY(()) rtx rs6000_tls_symbol;
5860 static rtx
5861 rs6000_tls_get_addr (void)
5863 if (!rs6000_tls_symbol)
5864 rs6000_tls_symbol = init_one_libfunc ("__tls_get_addr");
5866 return rs6000_tls_symbol;
5869 /* Construct the SYMBOL_REF for TLS GOT references. */
5871 static GTY(()) rtx rs6000_got_symbol;
5872 static rtx
5873 rs6000_got_sym (void)
5875 if (!rs6000_got_symbol)
5877 rs6000_got_symbol = gen_rtx_SYMBOL_REF (Pmode, "_GLOBAL_OFFSET_TABLE_");
5878 SYMBOL_REF_FLAGS (rs6000_got_symbol) |= SYMBOL_FLAG_LOCAL;
5879 SYMBOL_REF_FLAGS (rs6000_got_symbol) |= SYMBOL_FLAG_EXTERNAL;
5882 return rs6000_got_symbol;
5885 /* ADDR contains a thread-local SYMBOL_REF. Generate code to compute
5886 this (thread-local) address. */
5888 static rtx
5889 rs6000_legitimize_tls_address (rtx addr, enum tls_model model)
5891 rtx dest, insn;
5893 dest = gen_reg_rtx (Pmode);
5894 if (model == TLS_MODEL_LOCAL_EXEC && rs6000_tls_size == 16)
5896 rtx tlsreg;
5898 if (TARGET_64BIT)
5900 tlsreg = gen_rtx_REG (Pmode, 13);
5901 insn = gen_tls_tprel_64 (dest, tlsreg, addr);
5903 else
5905 tlsreg = gen_rtx_REG (Pmode, 2);
5906 insn = gen_tls_tprel_32 (dest, tlsreg, addr);
5908 emit_insn (insn);
5910 else if (model == TLS_MODEL_LOCAL_EXEC && rs6000_tls_size == 32)
5912 rtx tlsreg, tmp;
5914 tmp = gen_reg_rtx (Pmode);
5915 if (TARGET_64BIT)
5917 tlsreg = gen_rtx_REG (Pmode, 13);
5918 insn = gen_tls_tprel_ha_64 (tmp, tlsreg, addr);
5920 else
5922 tlsreg = gen_rtx_REG (Pmode, 2);
5923 insn = gen_tls_tprel_ha_32 (tmp, tlsreg, addr);
5925 emit_insn (insn);
5926 if (TARGET_64BIT)
5927 insn = gen_tls_tprel_lo_64 (dest, tmp, addr);
5928 else
5929 insn = gen_tls_tprel_lo_32 (dest, tmp, addr);
5930 emit_insn (insn);
5932 else
5934 rtx r3, got, tga, tmp1, tmp2, call_insn;
5936 /* We currently use relocations like @got@tlsgd for tls, which
5937 means the linker will handle allocation of tls entries, placing
5938 them in the .got section. So use a pointer to the .got section,
5939 not one to secondary TOC sections used by 64-bit -mminimal-toc,
5940 or to secondary GOT sections used by 32-bit -fPIC. */
5941 if (TARGET_64BIT)
5942 got = gen_rtx_REG (Pmode, 2);
5943 else
5945 if (flag_pic == 1)
5946 got = gen_rtx_REG (Pmode, RS6000_PIC_OFFSET_TABLE_REGNUM);
5947 else
5949 rtx gsym = rs6000_got_sym ();
5950 got = gen_reg_rtx (Pmode);
5951 if (flag_pic == 0)
5952 rs6000_emit_move (got, gsym, Pmode);
5953 else
5955 rtx mem, lab, last;
5957 tmp1 = gen_reg_rtx (Pmode);
5958 tmp2 = gen_reg_rtx (Pmode);
5959 mem = gen_const_mem (Pmode, tmp1);
5960 lab = gen_label_rtx ();
5961 emit_insn (gen_load_toc_v4_PIC_1b (gsym, lab));
5962 emit_move_insn (tmp1, gen_rtx_REG (Pmode, LR_REGNO));
5963 if (TARGET_LINK_STACK)
5964 emit_insn (gen_addsi3 (tmp1, tmp1, GEN_INT (4)));
5965 emit_move_insn (tmp2, mem);
5966 last = emit_insn (gen_addsi3 (got, tmp1, tmp2));
5967 set_unique_reg_note (last, REG_EQUAL, gsym);
5972 if (model == TLS_MODEL_GLOBAL_DYNAMIC)
5974 tga = rs6000_tls_get_addr ();
5975 emit_library_call_value (tga, dest, LCT_CONST, Pmode,
5976 1, const0_rtx, Pmode);
5978 r3 = gen_rtx_REG (Pmode, 3);
5979 if (DEFAULT_ABI == ABI_AIX && TARGET_64BIT)
5980 insn = gen_tls_gd_aix64 (r3, got, addr, tga, const0_rtx);
5981 else if (DEFAULT_ABI == ABI_AIX && !TARGET_64BIT)
5982 insn = gen_tls_gd_aix32 (r3, got, addr, tga, const0_rtx);
5983 else if (DEFAULT_ABI == ABI_V4)
5984 insn = gen_tls_gd_sysvsi (r3, got, addr, tga, const0_rtx);
5985 else
5986 gcc_unreachable ();
5987 call_insn = last_call_insn ();
5988 PATTERN (call_insn) = insn;
5989 if (DEFAULT_ABI == ABI_V4 && TARGET_SECURE_PLT && flag_pic)
5990 use_reg (&CALL_INSN_FUNCTION_USAGE (call_insn),
5991 pic_offset_table_rtx);
5993 else if (model == TLS_MODEL_LOCAL_DYNAMIC)
5995 tga = rs6000_tls_get_addr ();
5996 tmp1 = gen_reg_rtx (Pmode);
5997 emit_library_call_value (tga, tmp1, LCT_CONST, Pmode,
5998 1, const0_rtx, Pmode);
6000 r3 = gen_rtx_REG (Pmode, 3);
6001 if (DEFAULT_ABI == ABI_AIX && TARGET_64BIT)
6002 insn = gen_tls_ld_aix64 (r3, got, tga, const0_rtx);
6003 else if (DEFAULT_ABI == ABI_AIX && !TARGET_64BIT)
6004 insn = gen_tls_ld_aix32 (r3, got, tga, const0_rtx);
6005 else if (DEFAULT_ABI == ABI_V4)
6006 insn = gen_tls_ld_sysvsi (r3, got, tga, const0_rtx);
6007 else
6008 gcc_unreachable ();
6009 call_insn = last_call_insn ();
6010 PATTERN (call_insn) = insn;
6011 if (DEFAULT_ABI == ABI_V4 && TARGET_SECURE_PLT && flag_pic)
6012 use_reg (&CALL_INSN_FUNCTION_USAGE (call_insn),
6013 pic_offset_table_rtx);
6015 if (rs6000_tls_size == 16)
6017 if (TARGET_64BIT)
6018 insn = gen_tls_dtprel_64 (dest, tmp1, addr);
6019 else
6020 insn = gen_tls_dtprel_32 (dest, tmp1, addr);
6022 else if (rs6000_tls_size == 32)
6024 tmp2 = gen_reg_rtx (Pmode);
6025 if (TARGET_64BIT)
6026 insn = gen_tls_dtprel_ha_64 (tmp2, tmp1, addr);
6027 else
6028 insn = gen_tls_dtprel_ha_32 (tmp2, tmp1, addr);
6029 emit_insn (insn);
6030 if (TARGET_64BIT)
6031 insn = gen_tls_dtprel_lo_64 (dest, tmp2, addr);
6032 else
6033 insn = gen_tls_dtprel_lo_32 (dest, tmp2, addr);
6035 else
6037 tmp2 = gen_reg_rtx (Pmode);
6038 if (TARGET_64BIT)
6039 insn = gen_tls_got_dtprel_64 (tmp2, got, addr);
6040 else
6041 insn = gen_tls_got_dtprel_32 (tmp2, got, addr);
6042 emit_insn (insn);
6043 insn = gen_rtx_SET (Pmode, dest,
6044 gen_rtx_PLUS (Pmode, tmp2, tmp1));
6046 emit_insn (insn);
6048 else
6050 /* IE, or 64-bit offset LE. */
6051 tmp2 = gen_reg_rtx (Pmode);
6052 if (TARGET_64BIT)
6053 insn = gen_tls_got_tprel_64 (tmp2, got, addr);
6054 else
6055 insn = gen_tls_got_tprel_32 (tmp2, got, addr);
6056 emit_insn (insn);
6057 if (TARGET_64BIT)
6058 insn = gen_tls_tls_64 (dest, tmp2, addr);
6059 else
6060 insn = gen_tls_tls_32 (dest, tmp2, addr);
6061 emit_insn (insn);
6065 return dest;
6068 /* Return 1 if X contains a thread-local symbol. */
6070 static bool
6071 rs6000_tls_referenced_p (rtx x)
6073 if (! TARGET_HAVE_TLS)
6074 return false;
6076 return for_each_rtx (&x, &rs6000_tls_symbol_ref_1, 0);
6079 /* Implement TARGET_CANNOT_FORCE_CONST_MEM. */
6081 static bool
6082 rs6000_cannot_force_const_mem (enum machine_mode mode ATTRIBUTE_UNUSED, rtx x)
6084 if (GET_CODE (x) == HIGH
6085 && GET_CODE (XEXP (x, 0)) == UNSPEC)
6086 return true;
6088 return rs6000_tls_referenced_p (x);
6091 /* Return 1 if *X is a thread-local symbol. This is the same as
6092 rs6000_tls_symbol_ref except for the type of the unused argument. */
6094 static int
6095 rs6000_tls_symbol_ref_1 (rtx *x, void *data ATTRIBUTE_UNUSED)
6097 return RS6000_SYMBOL_REF_TLS_P (*x);
6100 /* Return true iff the given SYMBOL_REF refers to a constant pool entry
6101 that we have put in the TOC, or for cmodel=medium, if the SYMBOL_REF
6102 can be addressed relative to the toc pointer. */
6104 static bool
6105 use_toc_relative_ref (rtx sym)
6107 return ((constant_pool_expr_p (sym)
6108 && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (sym),
6109 get_pool_mode (sym)))
6110 || (TARGET_CMODEL == CMODEL_MEDIUM
6111 && !CONSTANT_POOL_ADDRESS_P (sym)
6112 && SYMBOL_REF_LOCAL_P (sym)));
6115 /* Our implementation of LEGITIMIZE_RELOAD_ADDRESS. Returns a value to
6116 replace the input X, or the original X if no replacement is called for.
6117 The output parameter *WIN is 1 if the calling macro should goto WIN,
6118 0 if it should not.
6120 For RS/6000, we wish to handle large displacements off a base
6121 register by splitting the addend across an addiu/addis and the mem insn.
6122 This cuts number of extra insns needed from 3 to 1.
6124 On Darwin, we use this to generate code for floating point constants.
6125 A movsf_low is generated so we wind up with 2 instructions rather than 3.
6126 The Darwin code is inside #if TARGET_MACHO because only then are the
6127 machopic_* functions defined. */
6128 static rtx
6129 rs6000_legitimize_reload_address (rtx x, enum machine_mode mode,
6130 int opnum, int type,
6131 int ind_levels ATTRIBUTE_UNUSED, int *win)
6133 bool reg_offset_p = reg_offset_addressing_ok_p (mode);
6135 /* Nasty hack for vsx_splat_V2DF/V2DI load from mem, which takes a
6136 DFmode/DImode MEM. */
6137 if (reg_offset_p
6138 && opnum == 1
6139 && ((mode == DFmode && recog_data.operand_mode[0] == V2DFmode)
6140 || (mode == DImode && recog_data.operand_mode[0] == V2DImode)))
6141 reg_offset_p = false;
6143 /* We must recognize output that we have already generated ourselves. */
6144 if (GET_CODE (x) == PLUS
6145 && GET_CODE (XEXP (x, 0)) == PLUS
6146 && GET_CODE (XEXP (XEXP (x, 0), 0)) == REG
6147 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6148 && GET_CODE (XEXP (x, 1)) == CONST_INT)
6150 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
6151 BASE_REG_CLASS, GET_MODE (x), VOIDmode, 0, 0,
6152 opnum, (enum reload_type) type);
6153 *win = 1;
6154 return x;
6157 /* Likewise for (lo_sum (high ...) ...) output we have generated. */
6158 if (GET_CODE (x) == LO_SUM
6159 && GET_CODE (XEXP (x, 0)) == HIGH)
6161 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
6162 BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
6163 opnum, (enum reload_type) type);
6164 *win = 1;
6165 return x;
6168 #if TARGET_MACHO
6169 if (DEFAULT_ABI == ABI_DARWIN && flag_pic
6170 && GET_CODE (x) == LO_SUM
6171 && GET_CODE (XEXP (x, 0)) == PLUS
6172 && XEXP (XEXP (x, 0), 0) == pic_offset_table_rtx
6173 && GET_CODE (XEXP (XEXP (x, 0), 1)) == HIGH
6174 && XEXP (XEXP (XEXP (x, 0), 1), 0) == XEXP (x, 1)
6175 && machopic_operand_p (XEXP (x, 1)))
6177 /* Result of previous invocation of this function on Darwin
6178 floating point constant. */
6179 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
6180 BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
6181 opnum, (enum reload_type) type);
6182 *win = 1;
6183 return x;
6185 #endif
6187 if (TARGET_CMODEL != CMODEL_SMALL
6188 && reg_offset_p
6189 && small_toc_ref (x, VOIDmode))
6191 rtx hi = gen_rtx_HIGH (Pmode, copy_rtx (x));
6192 x = gen_rtx_LO_SUM (Pmode, hi, x);
6193 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
6194 BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
6195 opnum, (enum reload_type) type);
6196 *win = 1;
6197 return x;
6200 if (GET_CODE (x) == PLUS
6201 && GET_CODE (XEXP (x, 0)) == REG
6202 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER
6203 && INT_REG_OK_FOR_BASE_P (XEXP (x, 0), 1)
6204 && GET_CODE (XEXP (x, 1)) == CONST_INT
6205 && reg_offset_p
6206 && !SPE_VECTOR_MODE (mode)
6207 && !(TARGET_E500_DOUBLE && (mode == DFmode || mode == TFmode
6208 || mode == DDmode || mode == TDmode
6209 || mode == DImode))
6210 && VECTOR_MEM_NONE_P (mode))
6212 HOST_WIDE_INT val = INTVAL (XEXP (x, 1));
6213 HOST_WIDE_INT low = ((val & 0xffff) ^ 0x8000) - 0x8000;
6214 HOST_WIDE_INT high
6215 = (((val - low) & 0xffffffff) ^ 0x80000000) - 0x80000000;
6217 /* Check for 32-bit overflow. */
6218 if (high + low != val)
6220 *win = 0;
6221 return x;
6224 /* Reload the high part into a base reg; leave the low part
6225 in the mem directly. */
6227 x = gen_rtx_PLUS (GET_MODE (x),
6228 gen_rtx_PLUS (GET_MODE (x), XEXP (x, 0),
6229 GEN_INT (high)),
6230 GEN_INT (low));
6232 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
6233 BASE_REG_CLASS, GET_MODE (x), VOIDmode, 0, 0,
6234 opnum, (enum reload_type) type);
6235 *win = 1;
6236 return x;
6239 if (GET_CODE (x) == SYMBOL_REF
6240 && reg_offset_p
6241 && VECTOR_MEM_NONE_P (mode)
6242 && !SPE_VECTOR_MODE (mode)
6243 #if TARGET_MACHO
6244 && DEFAULT_ABI == ABI_DARWIN
6245 && (flag_pic || MACHO_DYNAMIC_NO_PIC_P)
6246 && machopic_symbol_defined_p (x)
6247 #else
6248 && DEFAULT_ABI == ABI_V4
6249 && !flag_pic
6250 #endif
6251 /* Don't do this for TFmode or TDmode, since the result isn't offsettable.
6252 The same goes for DImode without 64-bit gprs and DFmode and DDmode
6253 without fprs.
6254 ??? Assume floating point reg based on mode? This assumption is
6255 violated by eg. powerpc-linux -m32 compile of gcc.dg/pr28796-2.c
6256 where reload ends up doing a DFmode load of a constant from
6257 mem using two gprs. Unfortunately, at this point reload
6258 hasn't yet selected regs so poking around in reload data
6259 won't help and even if we could figure out the regs reliably,
6260 we'd still want to allow this transformation when the mem is
6261 naturally aligned. Since we say the address is good here, we
6262 can't disable offsets from LO_SUMs in mem_operand_gpr.
6263 FIXME: Allow offset from lo_sum for other modes too, when
6264 mem is sufficiently aligned. */
6265 && mode != TFmode
6266 && mode != TDmode
6267 && (mode != DImode || TARGET_POWERPC64)
6268 && ((mode != DFmode && mode != DDmode) || TARGET_POWERPC64
6269 || (TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT)))
6271 #if TARGET_MACHO
6272 if (flag_pic)
6274 rtx offset = machopic_gen_offset (x);
6275 x = gen_rtx_LO_SUM (GET_MODE (x),
6276 gen_rtx_PLUS (Pmode, pic_offset_table_rtx,
6277 gen_rtx_HIGH (Pmode, offset)), offset);
6279 else
6280 #endif
6281 x = gen_rtx_LO_SUM (GET_MODE (x),
6282 gen_rtx_HIGH (Pmode, x), x);
6284 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
6285 BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
6286 opnum, (enum reload_type) type);
6287 *win = 1;
6288 return x;
6291 /* Reload an offset address wrapped by an AND that represents the
6292 masking of the lower bits. Strip the outer AND and let reload
6293 convert the offset address into an indirect address. For VSX,
6294 force reload to create the address with an AND in a separate
6295 register, because we can't guarantee an altivec register will
6296 be used. */
6297 if (VECTOR_MEM_ALTIVEC_P (mode)
6298 && GET_CODE (x) == AND
6299 && GET_CODE (XEXP (x, 0)) == PLUS
6300 && GET_CODE (XEXP (XEXP (x, 0), 0)) == REG
6301 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6302 && GET_CODE (XEXP (x, 1)) == CONST_INT
6303 && INTVAL (XEXP (x, 1)) == -16)
6305 x = XEXP (x, 0);
6306 *win = 1;
6307 return x;
6310 if (TARGET_TOC
6311 && reg_offset_p
6312 && GET_CODE (x) == SYMBOL_REF
6313 && use_toc_relative_ref (x))
6315 x = create_TOC_reference (x, NULL_RTX);
6316 if (TARGET_CMODEL != CMODEL_SMALL)
6317 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
6318 BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
6319 opnum, (enum reload_type) type);
6320 *win = 1;
6321 return x;
6323 *win = 0;
6324 return x;
6327 /* Debug version of rs6000_legitimize_reload_address. */
6328 static rtx
6329 rs6000_debug_legitimize_reload_address (rtx x, enum machine_mode mode,
6330 int opnum, int type,
6331 int ind_levels, int *win)
6333 rtx ret = rs6000_legitimize_reload_address (x, mode, opnum, type,
6334 ind_levels, win);
6335 fprintf (stderr,
6336 "\nrs6000_legitimize_reload_address: mode = %s, opnum = %d, "
6337 "type = %d, ind_levels = %d, win = %d, original addr:\n",
6338 GET_MODE_NAME (mode), opnum, type, ind_levels, *win);
6339 debug_rtx (x);
6341 if (x == ret)
6342 fprintf (stderr, "Same address returned\n");
6343 else if (!ret)
6344 fprintf (stderr, "NULL returned\n");
6345 else
6347 fprintf (stderr, "New address:\n");
6348 debug_rtx (ret);
6351 return ret;
6354 /* TARGET_LEGITIMATE_ADDRESS_P recognizes an RTL expression
6355 that is a valid memory address for an instruction.
6356 The MODE argument is the machine mode for the MEM expression
6357 that wants to use this address.
6359 On the RS/6000, there are four valid address: a SYMBOL_REF that
6360 refers to a constant pool entry of an address (or the sum of it
6361 plus a constant), a short (16-bit signed) constant plus a register,
6362 the sum of two registers, or a register indirect, possibly with an
6363 auto-increment. For DFmode, DDmode and DImode with a constant plus
6364 register, we must ensure that both words are addressable or PowerPC64
6365 with offset word aligned.
6367 For modes spanning multiple registers (DFmode and DDmode in 32-bit GPRs,
6368 32-bit DImode, TImode, TFmode, TDmode), indexed addressing cannot be used
6369 because adjacent memory cells are accessed by adding word-sized offsets
6370 during assembly output. */
6371 static bool
6372 rs6000_legitimate_address_p (enum machine_mode mode, rtx x, bool reg_ok_strict)
6374 bool reg_offset_p = reg_offset_addressing_ok_p (mode);
6376 /* If this is an unaligned stvx/ldvx type address, discard the outer AND. */
6377 if (VECTOR_MEM_ALTIVEC_P (mode)
6378 && GET_CODE (x) == AND
6379 && GET_CODE (XEXP (x, 1)) == CONST_INT
6380 && INTVAL (XEXP (x, 1)) == -16)
6381 x = XEXP (x, 0);
6383 if (RS6000_SYMBOL_REF_TLS_P (x))
6384 return 0;
6385 if (legitimate_indirect_address_p (x, reg_ok_strict))
6386 return 1;
6387 if ((GET_CODE (x) == PRE_INC || GET_CODE (x) == PRE_DEC)
6388 && !VECTOR_MEM_ALTIVEC_OR_VSX_P (mode)
6389 && !SPE_VECTOR_MODE (mode)
6390 && mode != TFmode
6391 && mode != TDmode
6392 /* Restrict addressing for DI because of our SUBREG hackery. */
6393 && !(TARGET_E500_DOUBLE
6394 && (mode == DFmode || mode == DDmode || mode == DImode))
6395 && TARGET_UPDATE
6396 && legitimate_indirect_address_p (XEXP (x, 0), reg_ok_strict))
6397 return 1;
6398 if (virtual_stack_registers_memory_p (x))
6399 return 1;
6400 if (reg_offset_p && legitimate_small_data_p (mode, x))
6401 return 1;
6402 if (reg_offset_p
6403 && legitimate_constant_pool_address_p (x, mode, reg_ok_strict))
6404 return 1;
6405 /* If not REG_OK_STRICT (before reload) let pass any stack offset. */
6406 if (! reg_ok_strict
6407 && reg_offset_p
6408 && GET_CODE (x) == PLUS
6409 && GET_CODE (XEXP (x, 0)) == REG
6410 && (XEXP (x, 0) == virtual_stack_vars_rtx
6411 || XEXP (x, 0) == arg_pointer_rtx)
6412 && GET_CODE (XEXP (x, 1)) == CONST_INT)
6413 return 1;
6414 if (rs6000_legitimate_offset_address_p (mode, x, reg_ok_strict, false))
6415 return 1;
6416 if (mode != TImode
6417 && mode != TFmode
6418 && mode != TDmode
6419 && ((TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT)
6420 || TARGET_POWERPC64
6421 || (mode != DFmode && mode != DDmode)
6422 || (TARGET_E500_DOUBLE && mode != DDmode))
6423 && (TARGET_POWERPC64 || mode != DImode)
6424 && !avoiding_indexed_address_p (mode)
6425 && legitimate_indexed_address_p (x, reg_ok_strict))
6426 return 1;
6427 if (GET_CODE (x) == PRE_MODIFY
6428 && mode != TImode
6429 && mode != TFmode
6430 && mode != TDmode
6431 && ((TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT)
6432 || TARGET_POWERPC64
6433 || ((mode != DFmode && mode != DDmode) || TARGET_E500_DOUBLE))
6434 && (TARGET_POWERPC64 || mode != DImode)
6435 && !VECTOR_MEM_ALTIVEC_OR_VSX_P (mode)
6436 && !SPE_VECTOR_MODE (mode)
6437 /* Restrict addressing for DI because of our SUBREG hackery. */
6438 && !(TARGET_E500_DOUBLE
6439 && (mode == DFmode || mode == DDmode || mode == DImode))
6440 && TARGET_UPDATE
6441 && legitimate_indirect_address_p (XEXP (x, 0), reg_ok_strict)
6442 && (rs6000_legitimate_offset_address_p (mode, XEXP (x, 1),
6443 reg_ok_strict, false)
6444 || (!avoiding_indexed_address_p (mode)
6445 && legitimate_indexed_address_p (XEXP (x, 1), reg_ok_strict)))
6446 && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
6447 return 1;
6448 if (reg_offset_p && legitimate_lo_sum_address_p (mode, x, reg_ok_strict))
6449 return 1;
6450 return 0;
6453 /* Debug version of rs6000_legitimate_address_p. */
6454 static bool
6455 rs6000_debug_legitimate_address_p (enum machine_mode mode, rtx x,
6456 bool reg_ok_strict)
6458 bool ret = rs6000_legitimate_address_p (mode, x, reg_ok_strict);
6459 fprintf (stderr,
6460 "\nrs6000_legitimate_address_p: return = %s, mode = %s, "
6461 "strict = %d, code = %s\n",
6462 ret ? "true" : "false",
6463 GET_MODE_NAME (mode),
6464 reg_ok_strict,
6465 GET_RTX_NAME (GET_CODE (x)));
6466 debug_rtx (x);
6468 return ret;
6471 /* Implement TARGET_MODE_DEPENDENT_ADDRESS_P. */
6473 static bool
6474 rs6000_mode_dependent_address_p (const_rtx addr,
6475 addr_space_t as ATTRIBUTE_UNUSED)
6477 return rs6000_mode_dependent_address_ptr (addr);
6480 /* Go to LABEL if ADDR (a legitimate address expression)
6481 has an effect that depends on the machine mode it is used for.
6483 On the RS/6000 this is true of all integral offsets (since AltiVec
6484 and VSX modes don't allow them) or is a pre-increment or decrement.
6486 ??? Except that due to conceptual problems in offsettable_address_p
6487 we can't really report the problems of integral offsets. So leave
6488 this assuming that the adjustable offset must be valid for the
6489 sub-words of a TFmode operand, which is what we had before. */
6491 static bool
6492 rs6000_mode_dependent_address (const_rtx addr)
6494 switch (GET_CODE (addr))
6496 case PLUS:
6497 /* Any offset from virtual_stack_vars_rtx and arg_pointer_rtx
6498 is considered a legitimate address before reload, so there
6499 are no offset restrictions in that case. Note that this
6500 condition is safe in strict mode because any address involving
6501 virtual_stack_vars_rtx or arg_pointer_rtx would already have
6502 been rejected as illegitimate. */
6503 if (XEXP (addr, 0) != virtual_stack_vars_rtx
6504 && XEXP (addr, 0) != arg_pointer_rtx
6505 && GET_CODE (XEXP (addr, 1)) == CONST_INT)
6507 unsigned HOST_WIDE_INT val = INTVAL (XEXP (addr, 1));
6508 return val + 0x8000 >= 0x10000 - (TARGET_POWERPC64 ? 8 : 12);
6510 break;
6512 case LO_SUM:
6513 /* Anything in the constant pool is sufficiently aligned that
6514 all bytes have the same high part address. */
6515 return !legitimate_constant_pool_address_p (addr, QImode, false);
6517 /* Auto-increment cases are now treated generically in recog.c. */
6518 case PRE_MODIFY:
6519 return TARGET_UPDATE;
6521 /* AND is only allowed in Altivec loads. */
6522 case AND:
6523 return true;
6525 default:
6526 break;
6529 return false;
6532 /* Debug version of rs6000_mode_dependent_address. */
6533 static bool
6534 rs6000_debug_mode_dependent_address (const_rtx addr)
6536 bool ret = rs6000_mode_dependent_address (addr);
6538 fprintf (stderr, "\nrs6000_mode_dependent_address: ret = %s\n",
6539 ret ? "true" : "false");
6540 debug_rtx (addr);
6542 return ret;
6545 /* Implement FIND_BASE_TERM. */
6548 rs6000_find_base_term (rtx op)
6550 rtx base;
6552 base = op;
6553 if (GET_CODE (base) == CONST)
6554 base = XEXP (base, 0);
6555 if (GET_CODE (base) == PLUS)
6556 base = XEXP (base, 0);
6557 if (GET_CODE (base) == UNSPEC)
6558 switch (XINT (base, 1))
6560 case UNSPEC_TOCREL:
6561 case UNSPEC_MACHOPIC_OFFSET:
6562 /* OP represents SYM [+ OFFSET] - ANCHOR. SYM is the base term
6563 for aliasing purposes. */
6564 return XVECEXP (base, 0, 0);
6567 return op;
6570 /* More elaborate version of recog's offsettable_memref_p predicate
6571 that works around the ??? note of rs6000_mode_dependent_address.
6572 In particular it accepts
6574 (mem:DI (plus:SI (reg/f:SI 31 31) (const_int 32760 [0x7ff8])))
6576 in 32-bit mode, that the recog predicate rejects. */
6578 static bool
6579 rs6000_offsettable_memref_p (rtx op, enum machine_mode reg_mode)
6581 bool worst_case;
6583 if (!MEM_P (op))
6584 return false;
6586 /* First mimic offsettable_memref_p. */
6587 if (offsettable_address_p (true, GET_MODE (op), XEXP (op, 0)))
6588 return true;
6590 /* offsettable_address_p invokes rs6000_mode_dependent_address, but
6591 the latter predicate knows nothing about the mode of the memory
6592 reference and, therefore, assumes that it is the largest supported
6593 mode (TFmode). As a consequence, legitimate offsettable memory
6594 references are rejected. rs6000_legitimate_offset_address_p contains
6595 the correct logic for the PLUS case of rs6000_mode_dependent_address,
6596 at least with a little bit of help here given that we know the
6597 actual registers used. */
6598 worst_case = ((TARGET_POWERPC64 && GET_MODE_CLASS (reg_mode) == MODE_INT)
6599 || GET_MODE_SIZE (reg_mode) == 4);
6600 return rs6000_legitimate_offset_address_p (GET_MODE (op), XEXP (op, 0),
6601 true, worst_case);
6604 /* Change register usage conditional on target flags. */
6605 static void
6606 rs6000_conditional_register_usage (void)
6608 int i;
6610 if (TARGET_DEBUG_TARGET)
6611 fprintf (stderr, "rs6000_conditional_register_usage called\n");
6613 /* Set MQ register fixed (already call_used) so that it will not be
6614 allocated. */
6615 fixed_regs[64] = 1;
6617 /* 64-bit AIX and Linux reserve GPR13 for thread-private data. */
6618 if (TARGET_64BIT)
6619 fixed_regs[13] = call_used_regs[13]
6620 = call_really_used_regs[13] = 1;
6622 /* Conditionally disable FPRs. */
6623 if (TARGET_SOFT_FLOAT || !TARGET_FPRS)
6624 for (i = 32; i < 64; i++)
6625 fixed_regs[i] = call_used_regs[i]
6626 = call_really_used_regs[i] = 1;
6628 /* The TOC register is not killed across calls in a way that is
6629 visible to the compiler. */
6630 if (DEFAULT_ABI == ABI_AIX)
6631 call_really_used_regs[2] = 0;
6633 if (DEFAULT_ABI == ABI_V4
6634 && PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
6635 && flag_pic == 2)
6636 fixed_regs[RS6000_PIC_OFFSET_TABLE_REGNUM] = 1;
6638 if (DEFAULT_ABI == ABI_V4
6639 && PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
6640 && flag_pic == 1)
6641 fixed_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
6642 = call_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
6643 = call_really_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM] = 1;
6645 if (DEFAULT_ABI == ABI_DARWIN
6646 && PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM)
6647 fixed_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
6648 = call_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
6649 = call_really_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM] = 1;
6651 if (TARGET_TOC && TARGET_MINIMAL_TOC)
6652 fixed_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
6653 = call_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM] = 1;
6655 if (TARGET_SPE)
6657 global_regs[SPEFSCR_REGNO] = 1;
6658 /* We used to use r14 as FIXED_SCRATCH to address SPE 64-bit
6659 registers in prologues and epilogues. We no longer use r14
6660 for FIXED_SCRATCH, but we're keeping r14 out of the allocation
6661 pool for link-compatibility with older versions of GCC. Once
6662 "old" code has died out, we can return r14 to the allocation
6663 pool. */
6664 fixed_regs[14]
6665 = call_used_regs[14]
6666 = call_really_used_regs[14] = 1;
6669 if (!TARGET_ALTIVEC && !TARGET_VSX)
6671 for (i = FIRST_ALTIVEC_REGNO; i <= LAST_ALTIVEC_REGNO; ++i)
6672 fixed_regs[i] = call_used_regs[i] = call_really_used_regs[i] = 1;
6673 call_really_used_regs[VRSAVE_REGNO] = 1;
6676 if (TARGET_ALTIVEC || TARGET_VSX)
6677 global_regs[VSCR_REGNO] = 1;
6679 if (TARGET_ALTIVEC_ABI)
6681 for (i = FIRST_ALTIVEC_REGNO; i < FIRST_ALTIVEC_REGNO + 20; ++i)
6682 call_used_regs[i] = call_really_used_regs[i] = 1;
6684 /* AIX reserves VR20:31 in non-extended ABI mode. */
6685 if (TARGET_XCOFF)
6686 for (i = FIRST_ALTIVEC_REGNO + 20; i < FIRST_ALTIVEC_REGNO + 32; ++i)
6687 fixed_regs[i] = call_used_regs[i] = call_really_used_regs[i] = 1;
6691 /* Try to output insns to set TARGET equal to the constant C if it can
6692 be done in less than N insns. Do all computations in MODE.
6693 Returns the place where the output has been placed if it can be
6694 done and the insns have been emitted. If it would take more than N
6695 insns, zero is returned and no insns and emitted. */
6698 rs6000_emit_set_const (rtx dest, enum machine_mode mode,
6699 rtx source, int n ATTRIBUTE_UNUSED)
6701 rtx result, insn, set;
6702 HOST_WIDE_INT c0, c1;
6704 switch (mode)
6706 case QImode:
6707 case HImode:
6708 if (dest == NULL)
6709 dest = gen_reg_rtx (mode);
6710 emit_insn (gen_rtx_SET (VOIDmode, dest, source));
6711 return dest;
6713 case SImode:
6714 result = !can_create_pseudo_p () ? dest : gen_reg_rtx (SImode);
6716 emit_insn (gen_rtx_SET (VOIDmode, copy_rtx (result),
6717 GEN_INT (INTVAL (source)
6718 & (~ (HOST_WIDE_INT) 0xffff))));
6719 emit_insn (gen_rtx_SET (VOIDmode, dest,
6720 gen_rtx_IOR (SImode, copy_rtx (result),
6721 GEN_INT (INTVAL (source) & 0xffff))));
6722 result = dest;
6723 break;
6725 case DImode:
6726 switch (GET_CODE (source))
6728 case CONST_INT:
6729 c0 = INTVAL (source);
6730 c1 = -(c0 < 0);
6731 break;
6733 case CONST_DOUBLE:
6734 #if HOST_BITS_PER_WIDE_INT >= 64
6735 c0 = CONST_DOUBLE_LOW (source);
6736 c1 = -(c0 < 0);
6737 #else
6738 c0 = CONST_DOUBLE_LOW (source);
6739 c1 = CONST_DOUBLE_HIGH (source);
6740 #endif
6741 break;
6743 default:
6744 gcc_unreachable ();
6747 result = rs6000_emit_set_long_const (dest, c0, c1);
6748 break;
6750 default:
6751 gcc_unreachable ();
6754 insn = get_last_insn ();
6755 set = single_set (insn);
6756 if (! CONSTANT_P (SET_SRC (set)))
6757 set_unique_reg_note (insn, REG_EQUAL, source);
6759 return result;
6762 /* Having failed to find a 3 insn sequence in rs6000_emit_set_const,
6763 fall back to a straight forward decomposition. We do this to avoid
6764 exponential run times encountered when looking for longer sequences
6765 with rs6000_emit_set_const. */
6766 static rtx
6767 rs6000_emit_set_long_const (rtx dest, HOST_WIDE_INT c1, HOST_WIDE_INT c2)
6769 if (!TARGET_POWERPC64)
6771 rtx operand1, operand2;
6773 operand1 = operand_subword_force (dest, WORDS_BIG_ENDIAN == 0,
6774 DImode);
6775 operand2 = operand_subword_force (copy_rtx (dest), WORDS_BIG_ENDIAN != 0,
6776 DImode);
6777 emit_move_insn (operand1, GEN_INT (c1));
6778 emit_move_insn (operand2, GEN_INT (c2));
6780 else
6782 HOST_WIDE_INT ud1, ud2, ud3, ud4;
6784 ud1 = c1 & 0xffff;
6785 ud2 = (c1 & 0xffff0000) >> 16;
6786 #if HOST_BITS_PER_WIDE_INT >= 64
6787 c2 = c1 >> 32;
6788 #endif
6789 ud3 = c2 & 0xffff;
6790 ud4 = (c2 & 0xffff0000) >> 16;
6792 if ((ud4 == 0xffff && ud3 == 0xffff && ud2 == 0xffff && (ud1 & 0x8000))
6793 || (ud4 == 0 && ud3 == 0 && ud2 == 0 && ! (ud1 & 0x8000)))
6795 if (ud1 & 0x8000)
6796 emit_move_insn (dest, GEN_INT (((ud1 ^ 0x8000) - 0x8000)));
6797 else
6798 emit_move_insn (dest, GEN_INT (ud1));
6801 else if ((ud4 == 0xffff && ud3 == 0xffff && (ud2 & 0x8000))
6802 || (ud4 == 0 && ud3 == 0 && ! (ud2 & 0x8000)))
6804 if (ud2 & 0x8000)
6805 emit_move_insn (dest, GEN_INT (((ud2 << 16) ^ 0x80000000)
6806 - 0x80000000));
6807 else
6808 emit_move_insn (dest, GEN_INT (ud2 << 16));
6809 if (ud1 != 0)
6810 emit_move_insn (copy_rtx (dest),
6811 gen_rtx_IOR (DImode, copy_rtx (dest),
6812 GEN_INT (ud1)));
6814 else if (ud3 == 0 && ud4 == 0)
6816 gcc_assert (ud2 & 0x8000);
6817 emit_move_insn (dest, GEN_INT (((ud2 << 16) ^ 0x80000000)
6818 - 0x80000000));
6819 if (ud1 != 0)
6820 emit_move_insn (copy_rtx (dest),
6821 gen_rtx_IOR (DImode, copy_rtx (dest),
6822 GEN_INT (ud1)));
6823 emit_move_insn (copy_rtx (dest),
6824 gen_rtx_ZERO_EXTEND (DImode,
6825 gen_lowpart (SImode,
6826 copy_rtx (dest))));
6828 else if ((ud4 == 0xffff && (ud3 & 0x8000))
6829 || (ud4 == 0 && ! (ud3 & 0x8000)))
6831 if (ud3 & 0x8000)
6832 emit_move_insn (dest, GEN_INT (((ud3 << 16) ^ 0x80000000)
6833 - 0x80000000));
6834 else
6835 emit_move_insn (dest, GEN_INT (ud3 << 16));
6837 if (ud2 != 0)
6838 emit_move_insn (copy_rtx (dest),
6839 gen_rtx_IOR (DImode, copy_rtx (dest),
6840 GEN_INT (ud2)));
6841 emit_move_insn (copy_rtx (dest),
6842 gen_rtx_ASHIFT (DImode, copy_rtx (dest),
6843 GEN_INT (16)));
6844 if (ud1 != 0)
6845 emit_move_insn (copy_rtx (dest),
6846 gen_rtx_IOR (DImode, copy_rtx (dest),
6847 GEN_INT (ud1)));
6849 else
6851 if (ud4 & 0x8000)
6852 emit_move_insn (dest, GEN_INT (((ud4 << 16) ^ 0x80000000)
6853 - 0x80000000));
6854 else
6855 emit_move_insn (dest, GEN_INT (ud4 << 16));
6857 if (ud3 != 0)
6858 emit_move_insn (copy_rtx (dest),
6859 gen_rtx_IOR (DImode, copy_rtx (dest),
6860 GEN_INT (ud3)));
6862 emit_move_insn (copy_rtx (dest),
6863 gen_rtx_ASHIFT (DImode, copy_rtx (dest),
6864 GEN_INT (32)));
6865 if (ud2 != 0)
6866 emit_move_insn (copy_rtx (dest),
6867 gen_rtx_IOR (DImode, copy_rtx (dest),
6868 GEN_INT (ud2 << 16)));
6869 if (ud1 != 0)
6870 emit_move_insn (copy_rtx (dest),
6871 gen_rtx_IOR (DImode, copy_rtx (dest), GEN_INT (ud1)));
6874 return dest;
6877 /* Helper for the following. Get rid of [r+r] memory refs
6878 in cases where it won't work (TImode, TFmode, TDmode). */
6880 static void
6881 rs6000_eliminate_indexed_memrefs (rtx operands[2])
6883 if (reload_in_progress)
6884 return;
6886 if (GET_CODE (operands[0]) == MEM
6887 && GET_CODE (XEXP (operands[0], 0)) != REG
6888 && ! legitimate_constant_pool_address_p (XEXP (operands[0], 0),
6889 GET_MODE (operands[0]), false))
6890 operands[0]
6891 = replace_equiv_address (operands[0],
6892 copy_addr_to_reg (XEXP (operands[0], 0)));
6894 if (GET_CODE (operands[1]) == MEM
6895 && GET_CODE (XEXP (operands[1], 0)) != REG
6896 && ! legitimate_constant_pool_address_p (XEXP (operands[1], 0),
6897 GET_MODE (operands[1]), false))
6898 operands[1]
6899 = replace_equiv_address (operands[1],
6900 copy_addr_to_reg (XEXP (operands[1], 0)));
6903 /* Emit a move from SOURCE to DEST in mode MODE. */
6904 void
6905 rs6000_emit_move (rtx dest, rtx source, enum machine_mode mode)
6907 rtx operands[2];
6908 operands[0] = dest;
6909 operands[1] = source;
6911 if (TARGET_DEBUG_ADDR)
6913 fprintf (stderr,
6914 "\nrs6000_emit_move: mode = %s, reload_in_progress = %d, "
6915 "reload_completed = %d, can_create_pseudos = %d.\ndest:\n",
6916 GET_MODE_NAME (mode),
6917 reload_in_progress,
6918 reload_completed,
6919 can_create_pseudo_p ());
6920 debug_rtx (dest);
6921 fprintf (stderr, "source:\n");
6922 debug_rtx (source);
6925 /* Sanity checks. Check that we get CONST_DOUBLE only when we should. */
6926 if (GET_CODE (operands[1]) == CONST_DOUBLE
6927 && ! FLOAT_MODE_P (mode)
6928 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
6930 /* FIXME. This should never happen. */
6931 /* Since it seems that it does, do the safe thing and convert
6932 to a CONST_INT. */
6933 operands[1] = gen_int_mode (CONST_DOUBLE_LOW (operands[1]), mode);
6935 gcc_assert (GET_CODE (operands[1]) != CONST_DOUBLE
6936 || FLOAT_MODE_P (mode)
6937 || ((CONST_DOUBLE_HIGH (operands[1]) != 0
6938 || CONST_DOUBLE_LOW (operands[1]) < 0)
6939 && (CONST_DOUBLE_HIGH (operands[1]) != -1
6940 || CONST_DOUBLE_LOW (operands[1]) >= 0)));
6942 /* Check if GCC is setting up a block move that will end up using FP
6943 registers as temporaries. We must make sure this is acceptable. */
6944 if (GET_CODE (operands[0]) == MEM
6945 && GET_CODE (operands[1]) == MEM
6946 && mode == DImode
6947 && (SLOW_UNALIGNED_ACCESS (DImode, MEM_ALIGN (operands[0]))
6948 || SLOW_UNALIGNED_ACCESS (DImode, MEM_ALIGN (operands[1])))
6949 && ! (SLOW_UNALIGNED_ACCESS (SImode, (MEM_ALIGN (operands[0]) > 32
6950 ? 32 : MEM_ALIGN (operands[0])))
6951 || SLOW_UNALIGNED_ACCESS (SImode, (MEM_ALIGN (operands[1]) > 32
6952 ? 32
6953 : MEM_ALIGN (operands[1]))))
6954 && ! MEM_VOLATILE_P (operands [0])
6955 && ! MEM_VOLATILE_P (operands [1]))
6957 emit_move_insn (adjust_address (operands[0], SImode, 0),
6958 adjust_address (operands[1], SImode, 0));
6959 emit_move_insn (adjust_address (copy_rtx (operands[0]), SImode, 4),
6960 adjust_address (copy_rtx (operands[1]), SImode, 4));
6961 return;
6964 if (can_create_pseudo_p () && GET_CODE (operands[0]) == MEM
6965 && !gpc_reg_operand (operands[1], mode))
6966 operands[1] = force_reg (mode, operands[1]);
6968 /* Recognize the case where operand[1] is a reference to thread-local
6969 data and load its address to a register. */
6970 if (rs6000_tls_referenced_p (operands[1]))
6972 enum tls_model model;
6973 rtx tmp = operands[1];
6974 rtx addend = NULL;
6976 if (GET_CODE (tmp) == CONST && GET_CODE (XEXP (tmp, 0)) == PLUS)
6978 addend = XEXP (XEXP (tmp, 0), 1);
6979 tmp = XEXP (XEXP (tmp, 0), 0);
6982 gcc_assert (GET_CODE (tmp) == SYMBOL_REF);
6983 model = SYMBOL_REF_TLS_MODEL (tmp);
6984 gcc_assert (model != 0);
6986 tmp = rs6000_legitimize_tls_address (tmp, model);
6987 if (addend)
6989 tmp = gen_rtx_PLUS (mode, tmp, addend);
6990 tmp = force_operand (tmp, operands[0]);
6992 operands[1] = tmp;
6995 /* Handle the case where reload calls us with an invalid address. */
6996 if (reload_in_progress && mode == Pmode
6997 && (! general_operand (operands[1], mode)
6998 || ! nonimmediate_operand (operands[0], mode)))
6999 goto emit_set;
7001 /* 128-bit constant floating-point values on Darwin should really be
7002 loaded as two parts. */
7003 if (!TARGET_IEEEQUAD && TARGET_LONG_DOUBLE_128
7004 && mode == TFmode && GET_CODE (operands[1]) == CONST_DOUBLE)
7006 rs6000_emit_move (simplify_gen_subreg (DFmode, operands[0], mode, 0),
7007 simplify_gen_subreg (DFmode, operands[1], mode, 0),
7008 DFmode);
7009 rs6000_emit_move (simplify_gen_subreg (DFmode, operands[0], mode,
7010 GET_MODE_SIZE (DFmode)),
7011 simplify_gen_subreg (DFmode, operands[1], mode,
7012 GET_MODE_SIZE (DFmode)),
7013 DFmode);
7014 return;
7017 if (reload_in_progress && cfun->machine->sdmode_stack_slot != NULL_RTX)
7018 cfun->machine->sdmode_stack_slot =
7019 eliminate_regs (cfun->machine->sdmode_stack_slot, VOIDmode, NULL_RTX);
7021 if (reload_in_progress
7022 && mode == SDmode
7023 && MEM_P (operands[0])
7024 && rtx_equal_p (operands[0], cfun->machine->sdmode_stack_slot)
7025 && REG_P (operands[1]))
7027 if (FP_REGNO_P (REGNO (operands[1])))
7029 rtx mem = adjust_address_nv (operands[0], DDmode, 0);
7030 mem = eliminate_regs (mem, VOIDmode, NULL_RTX);
7031 emit_insn (gen_movsd_store (mem, operands[1]));
7033 else if (INT_REGNO_P (REGNO (operands[1])))
7035 rtx mem = adjust_address_nv (operands[0], mode, 4);
7036 mem = eliminate_regs (mem, VOIDmode, NULL_RTX);
7037 emit_insn (gen_movsd_hardfloat (mem, operands[1]));
7039 else
7040 gcc_unreachable();
7041 return;
7043 if (reload_in_progress
7044 && mode == SDmode
7045 && REG_P (operands[0])
7046 && MEM_P (operands[1])
7047 && rtx_equal_p (operands[1], cfun->machine->sdmode_stack_slot))
7049 if (FP_REGNO_P (REGNO (operands[0])))
7051 rtx mem = adjust_address_nv (operands[1], DDmode, 0);
7052 mem = eliminate_regs (mem, VOIDmode, NULL_RTX);
7053 emit_insn (gen_movsd_load (operands[0], mem));
7055 else if (INT_REGNO_P (REGNO (operands[0])))
7057 rtx mem = adjust_address_nv (operands[1], mode, 4);
7058 mem = eliminate_regs (mem, VOIDmode, NULL_RTX);
7059 emit_insn (gen_movsd_hardfloat (operands[0], mem));
7061 else
7062 gcc_unreachable();
7063 return;
7066 /* FIXME: In the long term, this switch statement should go away
7067 and be replaced by a sequence of tests based on things like
7068 mode == Pmode. */
7069 switch (mode)
7071 case HImode:
7072 case QImode:
7073 if (CONSTANT_P (operands[1])
7074 && GET_CODE (operands[1]) != CONST_INT)
7075 operands[1] = force_const_mem (mode, operands[1]);
7076 break;
7078 case TFmode:
7079 case TDmode:
7080 rs6000_eliminate_indexed_memrefs (operands);
7081 /* fall through */
7083 case DFmode:
7084 case DDmode:
7085 case SFmode:
7086 case SDmode:
7087 if (CONSTANT_P (operands[1])
7088 && ! easy_fp_constant (operands[1], mode))
7089 operands[1] = force_const_mem (mode, operands[1]);
7090 break;
7092 case V16QImode:
7093 case V8HImode:
7094 case V4SFmode:
7095 case V4SImode:
7096 case V4HImode:
7097 case V2SFmode:
7098 case V2SImode:
7099 case V1DImode:
7100 case V2DFmode:
7101 case V2DImode:
7102 if (CONSTANT_P (operands[1])
7103 && !easy_vector_constant (operands[1], mode))
7104 operands[1] = force_const_mem (mode, operands[1]);
7105 break;
7107 case SImode:
7108 case DImode:
7109 /* Use default pattern for address of ELF small data */
7110 if (TARGET_ELF
7111 && mode == Pmode
7112 && DEFAULT_ABI == ABI_V4
7113 && (GET_CODE (operands[1]) == SYMBOL_REF
7114 || GET_CODE (operands[1]) == CONST)
7115 && small_data_operand (operands[1], mode))
7117 emit_insn (gen_rtx_SET (VOIDmode, operands[0], operands[1]));
7118 return;
7121 if (DEFAULT_ABI == ABI_V4
7122 && mode == Pmode && mode == SImode
7123 && flag_pic == 1 && got_operand (operands[1], mode))
7125 emit_insn (gen_movsi_got (operands[0], operands[1]));
7126 return;
7129 if ((TARGET_ELF || DEFAULT_ABI == ABI_DARWIN)
7130 && TARGET_NO_TOC
7131 && ! flag_pic
7132 && mode == Pmode
7133 && CONSTANT_P (operands[1])
7134 && GET_CODE (operands[1]) != HIGH
7135 && GET_CODE (operands[1]) != CONST_INT)
7137 rtx target = (!can_create_pseudo_p ()
7138 ? operands[0]
7139 : gen_reg_rtx (mode));
7141 /* If this is a function address on -mcall-aixdesc,
7142 convert it to the address of the descriptor. */
7143 if (DEFAULT_ABI == ABI_AIX
7144 && GET_CODE (operands[1]) == SYMBOL_REF
7145 && XSTR (operands[1], 0)[0] == '.')
7147 const char *name = XSTR (operands[1], 0);
7148 rtx new_ref;
7149 while (*name == '.')
7150 name++;
7151 new_ref = gen_rtx_SYMBOL_REF (Pmode, name);
7152 CONSTANT_POOL_ADDRESS_P (new_ref)
7153 = CONSTANT_POOL_ADDRESS_P (operands[1]);
7154 SYMBOL_REF_FLAGS (new_ref) = SYMBOL_REF_FLAGS (operands[1]);
7155 SYMBOL_REF_USED (new_ref) = SYMBOL_REF_USED (operands[1]);
7156 SYMBOL_REF_DATA (new_ref) = SYMBOL_REF_DATA (operands[1]);
7157 operands[1] = new_ref;
7160 if (DEFAULT_ABI == ABI_DARWIN)
7162 #if TARGET_MACHO
7163 if (MACHO_DYNAMIC_NO_PIC_P)
7165 /* Take care of any required data indirection. */
7166 operands[1] = rs6000_machopic_legitimize_pic_address (
7167 operands[1], mode, operands[0]);
7168 if (operands[0] != operands[1])
7169 emit_insn (gen_rtx_SET (VOIDmode,
7170 operands[0], operands[1]));
7171 return;
7173 #endif
7174 emit_insn (gen_macho_high (target, operands[1]));
7175 emit_insn (gen_macho_low (operands[0], target, operands[1]));
7176 return;
7179 emit_insn (gen_elf_high (target, operands[1]));
7180 emit_insn (gen_elf_low (operands[0], target, operands[1]));
7181 return;
7184 /* If this is a SYMBOL_REF that refers to a constant pool entry,
7185 and we have put it in the TOC, we just need to make a TOC-relative
7186 reference to it. */
7187 if (TARGET_TOC
7188 && GET_CODE (operands[1]) == SYMBOL_REF
7189 && use_toc_relative_ref (operands[1]))
7190 operands[1] = create_TOC_reference (operands[1], operands[0]);
7191 else if (mode == Pmode
7192 && CONSTANT_P (operands[1])
7193 && GET_CODE (operands[1]) != HIGH
7194 && ((GET_CODE (operands[1]) != CONST_INT
7195 && ! easy_fp_constant (operands[1], mode))
7196 || (GET_CODE (operands[1]) == CONST_INT
7197 && (num_insns_constant (operands[1], mode)
7198 > (TARGET_CMODEL != CMODEL_SMALL ? 3 : 2)))
7199 || (GET_CODE (operands[0]) == REG
7200 && FP_REGNO_P (REGNO (operands[0]))))
7201 && !toc_relative_expr_p (operands[1], false)
7202 && (TARGET_CMODEL == CMODEL_SMALL
7203 || can_create_pseudo_p ()
7204 || (REG_P (operands[0])
7205 && INT_REG_OK_FOR_BASE_P (operands[0], true))))
7208 #if TARGET_MACHO
7209 /* Darwin uses a special PIC legitimizer. */
7210 if (DEFAULT_ABI == ABI_DARWIN && MACHOPIC_INDIRECT)
7212 operands[1] =
7213 rs6000_machopic_legitimize_pic_address (operands[1], mode,
7214 operands[0]);
7215 if (operands[0] != operands[1])
7216 emit_insn (gen_rtx_SET (VOIDmode, operands[0], operands[1]));
7217 return;
7219 #endif
7221 /* If we are to limit the number of things we put in the TOC and
7222 this is a symbol plus a constant we can add in one insn,
7223 just put the symbol in the TOC and add the constant. Don't do
7224 this if reload is in progress. */
7225 if (GET_CODE (operands[1]) == CONST
7226 && TARGET_NO_SUM_IN_TOC && ! reload_in_progress
7227 && GET_CODE (XEXP (operands[1], 0)) == PLUS
7228 && add_operand (XEXP (XEXP (operands[1], 0), 1), mode)
7229 && (GET_CODE (XEXP (XEXP (operands[1], 0), 0)) == LABEL_REF
7230 || GET_CODE (XEXP (XEXP (operands[1], 0), 0)) == SYMBOL_REF)
7231 && ! side_effects_p (operands[0]))
7233 rtx sym =
7234 force_const_mem (mode, XEXP (XEXP (operands[1], 0), 0));
7235 rtx other = XEXP (XEXP (operands[1], 0), 1);
7237 sym = force_reg (mode, sym);
7238 emit_insn (gen_add3_insn (operands[0], sym, other));
7239 return;
7242 operands[1] = force_const_mem (mode, operands[1]);
7244 if (TARGET_TOC
7245 && GET_CODE (XEXP (operands[1], 0)) == SYMBOL_REF
7246 && constant_pool_expr_p (XEXP (operands[1], 0))
7247 && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (
7248 get_pool_constant (XEXP (operands[1], 0)),
7249 get_pool_mode (XEXP (operands[1], 0))))
7251 rtx tocref = create_TOC_reference (XEXP (operands[1], 0),
7252 operands[0]);
7253 operands[1] = gen_const_mem (mode, tocref);
7254 set_mem_alias_set (operands[1], get_TOC_alias_set ());
7257 break;
7259 case TImode:
7260 rs6000_eliminate_indexed_memrefs (operands);
7261 break;
7263 default:
7264 fatal_insn ("bad move", gen_rtx_SET (VOIDmode, dest, source));
7267 /* Above, we may have called force_const_mem which may have returned
7268 an invalid address. If we can, fix this up; otherwise, reload will
7269 have to deal with it. */
7270 if (GET_CODE (operands[1]) == MEM && ! reload_in_progress)
7271 operands[1] = validize_mem (operands[1]);
7273 emit_set:
7274 emit_insn (gen_rtx_SET (VOIDmode, operands[0], operands[1]));
7277 /* Return true if a structure, union or array containing FIELD should be
7278 accessed using `BLKMODE'.
7280 For the SPE, simd types are V2SI, and gcc can be tempted to put the
7281 entire thing in a DI and use subregs to access the internals.
7282 store_bit_field() will force (subreg:DI (reg:V2SI x))'s to the
7283 back-end. Because a single GPR can hold a V2SI, but not a DI, the
7284 best thing to do is set structs to BLKmode and avoid Severe Tire
7285 Damage.
7287 On e500 v2, DF and DI modes suffer from the same anomaly. DF can
7288 fit into 1, whereas DI still needs two. */
7290 static bool
7291 rs6000_member_type_forces_blk (const_tree field, enum machine_mode mode)
7293 return ((TARGET_SPE && TREE_CODE (TREE_TYPE (field)) == VECTOR_TYPE)
7294 || (TARGET_E500_DOUBLE && mode == DFmode));
7297 /* Nonzero if we can use a floating-point register to pass this arg. */
7298 #define USE_FP_FOR_ARG_P(CUM,MODE,TYPE) \
7299 (SCALAR_FLOAT_MODE_P (MODE) \
7300 && (CUM)->fregno <= FP_ARG_MAX_REG \
7301 && TARGET_HARD_FLOAT && TARGET_FPRS)
7303 /* Nonzero if we can use an AltiVec register to pass this arg. */
7304 #define USE_ALTIVEC_FOR_ARG_P(CUM,MODE,TYPE,NAMED) \
7305 (ALTIVEC_OR_VSX_VECTOR_MODE (MODE) \
7306 && (CUM)->vregno <= ALTIVEC_ARG_MAX_REG \
7307 && TARGET_ALTIVEC_ABI \
7308 && (NAMED))
7310 /* Return a nonzero value to say to return the function value in
7311 memory, just as large structures are always returned. TYPE will be
7312 the data type of the value, and FNTYPE will be the type of the
7313 function doing the returning, or @code{NULL} for libcalls.
7315 The AIX ABI for the RS/6000 specifies that all structures are
7316 returned in memory. The Darwin ABI does the same.
7318 For the Darwin 64 Bit ABI, a function result can be returned in
7319 registers or in memory, depending on the size of the return data
7320 type. If it is returned in registers, the value occupies the same
7321 registers as it would if it were the first and only function
7322 argument. Otherwise, the function places its result in memory at
7323 the location pointed to by GPR3.
7325 The SVR4 ABI specifies that structures <= 8 bytes are returned in r3/r4,
7326 but a draft put them in memory, and GCC used to implement the draft
7327 instead of the final standard. Therefore, aix_struct_return
7328 controls this instead of DEFAULT_ABI; V.4 targets needing backward
7329 compatibility can change DRAFT_V4_STRUCT_RET to override the
7330 default, and -m switches get the final word. See
7331 rs6000_option_override_internal for more details.
7333 The PPC32 SVR4 ABI uses IEEE double extended for long double, if 128-bit
7334 long double support is enabled. These values are returned in memory.
7336 int_size_in_bytes returns -1 for variable size objects, which go in
7337 memory always. The cast to unsigned makes -1 > 8. */
7339 static bool
7340 rs6000_return_in_memory (const_tree type, const_tree fntype ATTRIBUTE_UNUSED)
7342 /* For the Darwin64 ABI, test if we can fit the return value in regs. */
7343 if (TARGET_MACHO
7344 && rs6000_darwin64_abi
7345 && TREE_CODE (type) == RECORD_TYPE
7346 && int_size_in_bytes (type) > 0)
7348 CUMULATIVE_ARGS valcum;
7349 rtx valret;
7351 valcum.words = 0;
7352 valcum.fregno = FP_ARG_MIN_REG;
7353 valcum.vregno = ALTIVEC_ARG_MIN_REG;
7354 /* Do a trial code generation as if this were going to be passed
7355 as an argument; if any part goes in memory, we return NULL. */
7356 valret = rs6000_darwin64_record_arg (&valcum, type, true, true);
7357 if (valret)
7358 return false;
7359 /* Otherwise fall through to more conventional ABI rules. */
7362 if (AGGREGATE_TYPE_P (type)
7363 && (aix_struct_return
7364 || (unsigned HOST_WIDE_INT) int_size_in_bytes (type) > 8))
7365 return true;
7367 /* Allow -maltivec -mabi=no-altivec without warning. Altivec vector
7368 modes only exist for GCC vector types if -maltivec. */
7369 if (TARGET_32BIT && !TARGET_ALTIVEC_ABI
7370 && ALTIVEC_VECTOR_MODE (TYPE_MODE (type)))
7371 return false;
7373 /* Return synthetic vectors in memory. */
7374 if (TREE_CODE (type) == VECTOR_TYPE
7375 && int_size_in_bytes (type) > (TARGET_ALTIVEC_ABI ? 16 : 8))
7377 static bool warned_for_return_big_vectors = false;
7378 if (!warned_for_return_big_vectors)
7380 warning (0, "GCC vector returned by reference: "
7381 "non-standard ABI extension with no compatibility guarantee");
7382 warned_for_return_big_vectors = true;
7384 return true;
7387 if (DEFAULT_ABI == ABI_V4 && TARGET_IEEEQUAD && TYPE_MODE (type) == TFmode)
7388 return true;
7390 return false;
7393 #ifdef HAVE_AS_GNU_ATTRIBUTE
7394 /* Return TRUE if a call to function FNDECL may be one that
7395 potentially affects the function calling ABI of the object file. */
7397 static bool
7398 call_ABI_of_interest (tree fndecl)
7400 if (cgraph_state == CGRAPH_STATE_EXPANSION)
7402 struct cgraph_node *c_node;
7404 /* Libcalls are always interesting. */
7405 if (fndecl == NULL_TREE)
7406 return true;
7408 /* Any call to an external function is interesting. */
7409 if (DECL_EXTERNAL (fndecl))
7410 return true;
7412 /* Interesting functions that we are emitting in this object file. */
7413 c_node = cgraph_get_node (fndecl);
7414 c_node = cgraph_function_or_thunk_node (c_node, NULL);
7415 return !cgraph_only_called_directly_p (c_node);
7417 return false;
7419 #endif
7421 /* Initialize a variable CUM of type CUMULATIVE_ARGS
7422 for a call to a function whose data type is FNTYPE.
7423 For a library call, FNTYPE is 0 and RETURN_MODE the return value mode.
7425 For incoming args we set the number of arguments in the prototype large
7426 so we never return a PARALLEL. */
7428 void
7429 init_cumulative_args (CUMULATIVE_ARGS *cum, tree fntype,
7430 rtx libname ATTRIBUTE_UNUSED, int incoming,
7431 int libcall, int n_named_args,
7432 tree fndecl ATTRIBUTE_UNUSED,
7433 enum machine_mode return_mode ATTRIBUTE_UNUSED)
7435 static CUMULATIVE_ARGS zero_cumulative;
7437 *cum = zero_cumulative;
7438 cum->words = 0;
7439 cum->fregno = FP_ARG_MIN_REG;
7440 cum->vregno = ALTIVEC_ARG_MIN_REG;
7441 cum->prototype = (fntype && prototype_p (fntype));
7442 cum->call_cookie = ((DEFAULT_ABI == ABI_V4 && libcall)
7443 ? CALL_LIBCALL : CALL_NORMAL);
7444 cum->sysv_gregno = GP_ARG_MIN_REG;
7445 cum->stdarg = stdarg_p (fntype);
7447 cum->nargs_prototype = 0;
7448 if (incoming || cum->prototype)
7449 cum->nargs_prototype = n_named_args;
7451 /* Check for a longcall attribute. */
7452 if ((!fntype && rs6000_default_long_calls)
7453 || (fntype
7454 && lookup_attribute ("longcall", TYPE_ATTRIBUTES (fntype))
7455 && !lookup_attribute ("shortcall", TYPE_ATTRIBUTES (fntype))))
7456 cum->call_cookie |= CALL_LONG;
7458 if (TARGET_DEBUG_ARG)
7460 fprintf (stderr, "\ninit_cumulative_args:");
7461 if (fntype)
7463 tree ret_type = TREE_TYPE (fntype);
7464 fprintf (stderr, " ret code = %s,",
7465 tree_code_name[ (int)TREE_CODE (ret_type) ]);
7468 if (cum->call_cookie & CALL_LONG)
7469 fprintf (stderr, " longcall,");
7471 fprintf (stderr, " proto = %d, nargs = %d\n",
7472 cum->prototype, cum->nargs_prototype);
7475 #ifdef HAVE_AS_GNU_ATTRIBUTE
7476 if (DEFAULT_ABI == ABI_V4)
7478 cum->escapes = call_ABI_of_interest (fndecl);
7479 if (cum->escapes)
7481 tree return_type;
7483 if (fntype)
7485 return_type = TREE_TYPE (fntype);
7486 return_mode = TYPE_MODE (return_type);
7488 else
7489 return_type = lang_hooks.types.type_for_mode (return_mode, 0);
7491 if (return_type != NULL)
7493 if (TREE_CODE (return_type) == RECORD_TYPE
7494 && TYPE_TRANSPARENT_AGGR (return_type))
7496 return_type = TREE_TYPE (first_field (return_type));
7497 return_mode = TYPE_MODE (return_type);
7499 if (AGGREGATE_TYPE_P (return_type)
7500 && ((unsigned HOST_WIDE_INT) int_size_in_bytes (return_type)
7501 <= 8))
7502 rs6000_returns_struct = true;
7504 if (SCALAR_FLOAT_MODE_P (return_mode))
7505 rs6000_passes_float = true;
7506 else if (ALTIVEC_OR_VSX_VECTOR_MODE (return_mode)
7507 || SPE_VECTOR_MODE (return_mode))
7508 rs6000_passes_vector = true;
7511 #endif
7513 if (fntype
7514 && !TARGET_ALTIVEC
7515 && TARGET_ALTIVEC_ABI
7516 && ALTIVEC_VECTOR_MODE (TYPE_MODE (TREE_TYPE (fntype))))
7518 error ("cannot return value in vector register because"
7519 " altivec instructions are disabled, use -maltivec"
7520 " to enable them");
7524 /* Return true if TYPE must be passed on the stack and not in registers. */
7526 static bool
7527 rs6000_must_pass_in_stack (enum machine_mode mode, const_tree type)
7529 if (DEFAULT_ABI == ABI_AIX || TARGET_64BIT)
7530 return must_pass_in_stack_var_size (mode, type);
7531 else
7532 return must_pass_in_stack_var_size_or_pad (mode, type);
7535 /* If defined, a C expression which determines whether, and in which
7536 direction, to pad out an argument with extra space. The value
7537 should be of type `enum direction': either `upward' to pad above
7538 the argument, `downward' to pad below, or `none' to inhibit
7539 padding.
7541 For the AIX ABI structs are always stored left shifted in their
7542 argument slot. */
7544 enum direction
7545 function_arg_padding (enum machine_mode mode, const_tree type)
7547 #ifndef AGGREGATE_PADDING_FIXED
7548 #define AGGREGATE_PADDING_FIXED 0
7549 #endif
7550 #ifndef AGGREGATES_PAD_UPWARD_ALWAYS
7551 #define AGGREGATES_PAD_UPWARD_ALWAYS 0
7552 #endif
7554 if (!AGGREGATE_PADDING_FIXED)
7556 /* GCC used to pass structures of the same size as integer types as
7557 if they were in fact integers, ignoring FUNCTION_ARG_PADDING.
7558 i.e. Structures of size 1 or 2 (or 4 when TARGET_64BIT) were
7559 passed padded downward, except that -mstrict-align further
7560 muddied the water in that multi-component structures of 2 and 4
7561 bytes in size were passed padded upward.
7563 The following arranges for best compatibility with previous
7564 versions of gcc, but removes the -mstrict-align dependency. */
7565 if (BYTES_BIG_ENDIAN)
7567 HOST_WIDE_INT size = 0;
7569 if (mode == BLKmode)
7571 if (type && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
7572 size = int_size_in_bytes (type);
7574 else
7575 size = GET_MODE_SIZE (mode);
7577 if (size == 1 || size == 2 || size == 4)
7578 return downward;
7580 return upward;
7583 if (AGGREGATES_PAD_UPWARD_ALWAYS)
7585 if (type != 0 && AGGREGATE_TYPE_P (type))
7586 return upward;
7589 /* Fall back to the default. */
7590 return DEFAULT_FUNCTION_ARG_PADDING (mode, type);
7593 /* If defined, a C expression that gives the alignment boundary, in bits,
7594 of an argument with the specified mode and type. If it is not defined,
7595 PARM_BOUNDARY is used for all arguments.
7597 V.4 wants long longs and doubles to be double word aligned. Just
7598 testing the mode size is a boneheaded way to do this as it means
7599 that other types such as complex int are also double word aligned.
7600 However, we're stuck with this because changing the ABI might break
7601 existing library interfaces.
7603 Doubleword align SPE vectors.
7604 Quadword align Altivec/VSX vectors.
7605 Quadword align large synthetic vector types. */
7607 static unsigned int
7608 rs6000_function_arg_boundary (enum machine_mode mode, const_tree type)
7610 if (DEFAULT_ABI == ABI_V4
7611 && (GET_MODE_SIZE (mode) == 8
7612 || (TARGET_HARD_FLOAT
7613 && TARGET_FPRS
7614 && (mode == TFmode || mode == TDmode))))
7615 return 64;
7616 else if (SPE_VECTOR_MODE (mode)
7617 || (type && TREE_CODE (type) == VECTOR_TYPE
7618 && int_size_in_bytes (type) >= 8
7619 && int_size_in_bytes (type) < 16))
7620 return 64;
7621 else if (ALTIVEC_OR_VSX_VECTOR_MODE (mode)
7622 || (type && TREE_CODE (type) == VECTOR_TYPE
7623 && int_size_in_bytes (type) >= 16))
7624 return 128;
7625 else if (TARGET_MACHO
7626 && rs6000_darwin64_abi
7627 && mode == BLKmode
7628 && type && TYPE_ALIGN (type) > 64)
7629 return 128;
7630 else
7631 return PARM_BOUNDARY;
7634 /* For a function parm of MODE and TYPE, return the starting word in
7635 the parameter area. NWORDS of the parameter area are already used. */
7637 static unsigned int
7638 rs6000_parm_start (enum machine_mode mode, const_tree type,
7639 unsigned int nwords)
7641 unsigned int align;
7642 unsigned int parm_offset;
7644 align = rs6000_function_arg_boundary (mode, type) / PARM_BOUNDARY - 1;
7645 parm_offset = DEFAULT_ABI == ABI_V4 ? 2 : 6;
7646 return nwords + (-(parm_offset + nwords) & align);
7649 /* Compute the size (in words) of a function argument. */
7651 static unsigned long
7652 rs6000_arg_size (enum machine_mode mode, const_tree type)
7654 unsigned long size;
7656 if (mode != BLKmode)
7657 size = GET_MODE_SIZE (mode);
7658 else
7659 size = int_size_in_bytes (type);
7661 if (TARGET_32BIT)
7662 return (size + 3) >> 2;
7663 else
7664 return (size + 7) >> 3;
7667 /* Use this to flush pending int fields. */
7669 static void
7670 rs6000_darwin64_record_arg_advance_flush (CUMULATIVE_ARGS *cum,
7671 HOST_WIDE_INT bitpos, int final)
7673 unsigned int startbit, endbit;
7674 int intregs, intoffset;
7675 enum machine_mode mode;
7677 /* Handle the situations where a float is taking up the first half
7678 of the GPR, and the other half is empty (typically due to
7679 alignment restrictions). We can detect this by a 8-byte-aligned
7680 int field, or by seeing that this is the final flush for this
7681 argument. Count the word and continue on. */
7682 if (cum->floats_in_gpr == 1
7683 && (cum->intoffset % 64 == 0
7684 || (cum->intoffset == -1 && final)))
7686 cum->words++;
7687 cum->floats_in_gpr = 0;
7690 if (cum->intoffset == -1)
7691 return;
7693 intoffset = cum->intoffset;
7694 cum->intoffset = -1;
7695 cum->floats_in_gpr = 0;
7697 if (intoffset % BITS_PER_WORD != 0)
7699 mode = mode_for_size (BITS_PER_WORD - intoffset % BITS_PER_WORD,
7700 MODE_INT, 0);
7701 if (mode == BLKmode)
7703 /* We couldn't find an appropriate mode, which happens,
7704 e.g., in packed structs when there are 3 bytes to load.
7705 Back intoffset back to the beginning of the word in this
7706 case. */
7707 intoffset = intoffset & -BITS_PER_WORD;
7711 startbit = intoffset & -BITS_PER_WORD;
7712 endbit = (bitpos + BITS_PER_WORD - 1) & -BITS_PER_WORD;
7713 intregs = (endbit - startbit) / BITS_PER_WORD;
7714 cum->words += intregs;
7715 /* words should be unsigned. */
7716 if ((unsigned)cum->words < (endbit/BITS_PER_WORD))
7718 int pad = (endbit/BITS_PER_WORD) - cum->words;
7719 cum->words += pad;
7723 /* The darwin64 ABI calls for us to recurse down through structs,
7724 looking for elements passed in registers. Unfortunately, we have
7725 to track int register count here also because of misalignments
7726 in powerpc alignment mode. */
7728 static void
7729 rs6000_darwin64_record_arg_advance_recurse (CUMULATIVE_ARGS *cum,
7730 const_tree type,
7731 HOST_WIDE_INT startbitpos)
7733 tree f;
7735 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
7736 if (TREE_CODE (f) == FIELD_DECL)
7738 HOST_WIDE_INT bitpos = startbitpos;
7739 tree ftype = TREE_TYPE (f);
7740 enum machine_mode mode;
7741 if (ftype == error_mark_node)
7742 continue;
7743 mode = TYPE_MODE (ftype);
7745 if (DECL_SIZE (f) != 0
7746 && host_integerp (bit_position (f), 1))
7747 bitpos += int_bit_position (f);
7749 /* ??? FIXME: else assume zero offset. */
7751 if (TREE_CODE (ftype) == RECORD_TYPE)
7752 rs6000_darwin64_record_arg_advance_recurse (cum, ftype, bitpos);
7753 else if (USE_FP_FOR_ARG_P (cum, mode, ftype))
7755 unsigned n_fpregs = (GET_MODE_SIZE (mode) + 7) >> 3;
7756 rs6000_darwin64_record_arg_advance_flush (cum, bitpos, 0);
7757 cum->fregno += n_fpregs;
7758 /* Single-precision floats present a special problem for
7759 us, because they are smaller than an 8-byte GPR, and so
7760 the structure-packing rules combined with the standard
7761 varargs behavior mean that we want to pack float/float
7762 and float/int combinations into a single register's
7763 space. This is complicated by the arg advance flushing,
7764 which works on arbitrarily large groups of int-type
7765 fields. */
7766 if (mode == SFmode)
7768 if (cum->floats_in_gpr == 1)
7770 /* Two floats in a word; count the word and reset
7771 the float count. */
7772 cum->words++;
7773 cum->floats_in_gpr = 0;
7775 else if (bitpos % 64 == 0)
7777 /* A float at the beginning of an 8-byte word;
7778 count it and put off adjusting cum->words until
7779 we see if a arg advance flush is going to do it
7780 for us. */
7781 cum->floats_in_gpr++;
7783 else
7785 /* The float is at the end of a word, preceded
7786 by integer fields, so the arg advance flush
7787 just above has already set cum->words and
7788 everything is taken care of. */
7791 else
7792 cum->words += n_fpregs;
7794 else if (USE_ALTIVEC_FOR_ARG_P (cum, mode, type, 1))
7796 rs6000_darwin64_record_arg_advance_flush (cum, bitpos, 0);
7797 cum->vregno++;
7798 cum->words += 2;
7800 else if (cum->intoffset == -1)
7801 cum->intoffset = bitpos;
7805 /* Check for an item that needs to be considered specially under the darwin 64
7806 bit ABI. These are record types where the mode is BLK or the structure is
7807 8 bytes in size. */
7808 static int
7809 rs6000_darwin64_struct_check_p (enum machine_mode mode, const_tree type)
7811 return rs6000_darwin64_abi
7812 && ((mode == BLKmode
7813 && TREE_CODE (type) == RECORD_TYPE
7814 && int_size_in_bytes (type) > 0)
7815 || (type && TREE_CODE (type) == RECORD_TYPE
7816 && int_size_in_bytes (type) == 8)) ? 1 : 0;
7819 /* Update the data in CUM to advance over an argument
7820 of mode MODE and data type TYPE.
7821 (TYPE is null for libcalls where that information may not be available.)
7823 Note that for args passed by reference, function_arg will be called
7824 with MODE and TYPE set to that of the pointer to the arg, not the arg
7825 itself. */
7827 static void
7828 rs6000_function_arg_advance_1 (CUMULATIVE_ARGS *cum, enum machine_mode mode,
7829 const_tree type, bool named, int depth)
7831 /* Only tick off an argument if we're not recursing. */
7832 if (depth == 0)
7833 cum->nargs_prototype--;
7835 #ifdef HAVE_AS_GNU_ATTRIBUTE
7836 if (DEFAULT_ABI == ABI_V4
7837 && cum->escapes)
7839 if (SCALAR_FLOAT_MODE_P (mode))
7840 rs6000_passes_float = true;
7841 else if (named && ALTIVEC_OR_VSX_VECTOR_MODE (mode))
7842 rs6000_passes_vector = true;
7843 else if (SPE_VECTOR_MODE (mode)
7844 && !cum->stdarg
7845 && cum->sysv_gregno <= GP_ARG_MAX_REG)
7846 rs6000_passes_vector = true;
7848 #endif
7850 if (TARGET_ALTIVEC_ABI
7851 && (ALTIVEC_OR_VSX_VECTOR_MODE (mode)
7852 || (type && TREE_CODE (type) == VECTOR_TYPE
7853 && int_size_in_bytes (type) == 16)))
7855 bool stack = false;
7857 if (USE_ALTIVEC_FOR_ARG_P (cum, mode, type, named))
7859 cum->vregno++;
7860 if (!TARGET_ALTIVEC)
7861 error ("cannot pass argument in vector register because"
7862 " altivec instructions are disabled, use -maltivec"
7863 " to enable them");
7865 /* PowerPC64 Linux and AIX allocate GPRs for a vector argument
7866 even if it is going to be passed in a vector register.
7867 Darwin does the same for variable-argument functions. */
7868 if ((DEFAULT_ABI == ABI_AIX && TARGET_64BIT)
7869 || (cum->stdarg && DEFAULT_ABI != ABI_V4))
7870 stack = true;
7872 else
7873 stack = true;
7875 if (stack)
7877 int align;
7879 /* Vector parameters must be 16-byte aligned. This places
7880 them at 2 mod 4 in terms of words in 32-bit mode, since
7881 the parameter save area starts at offset 24 from the
7882 stack. In 64-bit mode, they just have to start on an
7883 even word, since the parameter save area is 16-byte
7884 aligned. Space for GPRs is reserved even if the argument
7885 will be passed in memory. */
7886 if (TARGET_32BIT)
7887 align = (2 - cum->words) & 3;
7888 else
7889 align = cum->words & 1;
7890 cum->words += align + rs6000_arg_size (mode, type);
7892 if (TARGET_DEBUG_ARG)
7894 fprintf (stderr, "function_adv: words = %2d, align=%d, ",
7895 cum->words, align);
7896 fprintf (stderr, "nargs = %4d, proto = %d, mode = %4s\n",
7897 cum->nargs_prototype, cum->prototype,
7898 GET_MODE_NAME (mode));
7902 else if (TARGET_SPE_ABI && TARGET_SPE && SPE_VECTOR_MODE (mode)
7903 && !cum->stdarg
7904 && cum->sysv_gregno <= GP_ARG_MAX_REG)
7905 cum->sysv_gregno++;
7907 else if (TARGET_MACHO && rs6000_darwin64_struct_check_p (mode, type))
7909 int size = int_size_in_bytes (type);
7910 /* Variable sized types have size == -1 and are
7911 treated as if consisting entirely of ints.
7912 Pad to 16 byte boundary if needed. */
7913 if (TYPE_ALIGN (type) >= 2 * BITS_PER_WORD
7914 && (cum->words % 2) != 0)
7915 cum->words++;
7916 /* For varargs, we can just go up by the size of the struct. */
7917 if (!named)
7918 cum->words += (size + 7) / 8;
7919 else
7921 /* It is tempting to say int register count just goes up by
7922 sizeof(type)/8, but this is wrong in a case such as
7923 { int; double; int; } [powerpc alignment]. We have to
7924 grovel through the fields for these too. */
7925 cum->intoffset = 0;
7926 cum->floats_in_gpr = 0;
7927 rs6000_darwin64_record_arg_advance_recurse (cum, type, 0);
7928 rs6000_darwin64_record_arg_advance_flush (cum,
7929 size * BITS_PER_UNIT, 1);
7931 if (TARGET_DEBUG_ARG)
7933 fprintf (stderr, "function_adv: words = %2d, align=%d, size=%d",
7934 cum->words, TYPE_ALIGN (type), size);
7935 fprintf (stderr,
7936 "nargs = %4d, proto = %d, mode = %4s (darwin64 abi)\n",
7937 cum->nargs_prototype, cum->prototype,
7938 GET_MODE_NAME (mode));
7941 else if (DEFAULT_ABI == ABI_V4)
7943 if (TARGET_HARD_FLOAT && TARGET_FPRS
7944 && ((TARGET_SINGLE_FLOAT && mode == SFmode)
7945 || (TARGET_DOUBLE_FLOAT && mode == DFmode)
7946 || (mode == TFmode && !TARGET_IEEEQUAD)
7947 || mode == SDmode || mode == DDmode || mode == TDmode))
7949 /* _Decimal128 must use an even/odd register pair. This assumes
7950 that the register number is odd when fregno is odd. */
7951 if (mode == TDmode && (cum->fregno % 2) == 1)
7952 cum->fregno++;
7954 if (cum->fregno + (mode == TFmode || mode == TDmode ? 1 : 0)
7955 <= FP_ARG_V4_MAX_REG)
7956 cum->fregno += (GET_MODE_SIZE (mode) + 7) >> 3;
7957 else
7959 cum->fregno = FP_ARG_V4_MAX_REG + 1;
7960 if (mode == DFmode || mode == TFmode
7961 || mode == DDmode || mode == TDmode)
7962 cum->words += cum->words & 1;
7963 cum->words += rs6000_arg_size (mode, type);
7966 else
7968 int n_words = rs6000_arg_size (mode, type);
7969 int gregno = cum->sysv_gregno;
7971 /* Long long and SPE vectors are put in (r3,r4), (r5,r6),
7972 (r7,r8) or (r9,r10). As does any other 2 word item such
7973 as complex int due to a historical mistake. */
7974 if (n_words == 2)
7975 gregno += (1 - gregno) & 1;
7977 /* Multi-reg args are not split between registers and stack. */
7978 if (gregno + n_words - 1 > GP_ARG_MAX_REG)
7980 /* Long long and SPE vectors are aligned on the stack.
7981 So are other 2 word items such as complex int due to
7982 a historical mistake. */
7983 if (n_words == 2)
7984 cum->words += cum->words & 1;
7985 cum->words += n_words;
7988 /* Note: continuing to accumulate gregno past when we've started
7989 spilling to the stack indicates the fact that we've started
7990 spilling to the stack to expand_builtin_saveregs. */
7991 cum->sysv_gregno = gregno + n_words;
7994 if (TARGET_DEBUG_ARG)
7996 fprintf (stderr, "function_adv: words = %2d, fregno = %2d, ",
7997 cum->words, cum->fregno);
7998 fprintf (stderr, "gregno = %2d, nargs = %4d, proto = %d, ",
7999 cum->sysv_gregno, cum->nargs_prototype, cum->prototype);
8000 fprintf (stderr, "mode = %4s, named = %d\n",
8001 GET_MODE_NAME (mode), named);
8004 else
8006 int n_words = rs6000_arg_size (mode, type);
8007 int start_words = cum->words;
8008 int align_words = rs6000_parm_start (mode, type, start_words);
8010 cum->words = align_words + n_words;
8012 if (SCALAR_FLOAT_MODE_P (mode)
8013 && TARGET_HARD_FLOAT && TARGET_FPRS)
8015 /* _Decimal128 must be passed in an even/odd float register pair.
8016 This assumes that the register number is odd when fregno is
8017 odd. */
8018 if (mode == TDmode && (cum->fregno % 2) == 1)
8019 cum->fregno++;
8020 cum->fregno += (GET_MODE_SIZE (mode) + 7) >> 3;
8023 if (TARGET_DEBUG_ARG)
8025 fprintf (stderr, "function_adv: words = %2d, fregno = %2d, ",
8026 cum->words, cum->fregno);
8027 fprintf (stderr, "nargs = %4d, proto = %d, mode = %4s, ",
8028 cum->nargs_prototype, cum->prototype, GET_MODE_NAME (mode));
8029 fprintf (stderr, "named = %d, align = %d, depth = %d\n",
8030 named, align_words - start_words, depth);
8035 static void
8036 rs6000_function_arg_advance (cumulative_args_t cum, enum machine_mode mode,
8037 const_tree type, bool named)
8039 rs6000_function_arg_advance_1 (get_cumulative_args (cum), mode, type, named,
8043 static rtx
8044 spe_build_register_parallel (enum machine_mode mode, int gregno)
8046 rtx r1, r3, r5, r7;
8048 switch (mode)
8050 case DFmode:
8051 r1 = gen_rtx_REG (DImode, gregno);
8052 r1 = gen_rtx_EXPR_LIST (VOIDmode, r1, const0_rtx);
8053 return gen_rtx_PARALLEL (mode, gen_rtvec (1, r1));
8055 case DCmode:
8056 case TFmode:
8057 r1 = gen_rtx_REG (DImode, gregno);
8058 r1 = gen_rtx_EXPR_LIST (VOIDmode, r1, const0_rtx);
8059 r3 = gen_rtx_REG (DImode, gregno + 2);
8060 r3 = gen_rtx_EXPR_LIST (VOIDmode, r3, GEN_INT (8));
8061 return gen_rtx_PARALLEL (mode, gen_rtvec (2, r1, r3));
8063 case TCmode:
8064 r1 = gen_rtx_REG (DImode, gregno);
8065 r1 = gen_rtx_EXPR_LIST (VOIDmode, r1, const0_rtx);
8066 r3 = gen_rtx_REG (DImode, gregno + 2);
8067 r3 = gen_rtx_EXPR_LIST (VOIDmode, r3, GEN_INT (8));
8068 r5 = gen_rtx_REG (DImode, gregno + 4);
8069 r5 = gen_rtx_EXPR_LIST (VOIDmode, r5, GEN_INT (16));
8070 r7 = gen_rtx_REG (DImode, gregno + 6);
8071 r7 = gen_rtx_EXPR_LIST (VOIDmode, r7, GEN_INT (24));
8072 return gen_rtx_PARALLEL (mode, gen_rtvec (4, r1, r3, r5, r7));
8074 default:
8075 gcc_unreachable ();
8079 /* Determine where to put a SIMD argument on the SPE. */
8080 static rtx
8081 rs6000_spe_function_arg (const CUMULATIVE_ARGS *cum, enum machine_mode mode,
8082 const_tree type)
8084 int gregno = cum->sysv_gregno;
8086 /* On E500 v2, double arithmetic is done on the full 64-bit GPR, but
8087 are passed and returned in a pair of GPRs for ABI compatibility. */
8088 if (TARGET_E500_DOUBLE && (mode == DFmode || mode == TFmode
8089 || mode == DCmode || mode == TCmode))
8091 int n_words = rs6000_arg_size (mode, type);
8093 /* Doubles go in an odd/even register pair (r5/r6, etc). */
8094 if (mode == DFmode)
8095 gregno += (1 - gregno) & 1;
8097 /* Multi-reg args are not split between registers and stack. */
8098 if (gregno + n_words - 1 > GP_ARG_MAX_REG)
8099 return NULL_RTX;
8101 return spe_build_register_parallel (mode, gregno);
8103 if (cum->stdarg)
8105 int n_words = rs6000_arg_size (mode, type);
8107 /* SPE vectors are put in odd registers. */
8108 if (n_words == 2 && (gregno & 1) == 0)
8109 gregno += 1;
8111 if (gregno + n_words - 1 <= GP_ARG_MAX_REG)
8113 rtx r1, r2;
8114 enum machine_mode m = SImode;
8116 r1 = gen_rtx_REG (m, gregno);
8117 r1 = gen_rtx_EXPR_LIST (m, r1, const0_rtx);
8118 r2 = gen_rtx_REG (m, gregno + 1);
8119 r2 = gen_rtx_EXPR_LIST (m, r2, GEN_INT (4));
8120 return gen_rtx_PARALLEL (mode, gen_rtvec (2, r1, r2));
8122 else
8123 return NULL_RTX;
8125 else
8127 if (gregno <= GP_ARG_MAX_REG)
8128 return gen_rtx_REG (mode, gregno);
8129 else
8130 return NULL_RTX;
8134 /* A subroutine of rs6000_darwin64_record_arg. Assign the bits of the
8135 structure between cum->intoffset and bitpos to integer registers. */
8137 static void
8138 rs6000_darwin64_record_arg_flush (CUMULATIVE_ARGS *cum,
8139 HOST_WIDE_INT bitpos, rtx rvec[], int *k)
8141 enum machine_mode mode;
8142 unsigned int regno;
8143 unsigned int startbit, endbit;
8144 int this_regno, intregs, intoffset;
8145 rtx reg;
8147 if (cum->intoffset == -1)
8148 return;
8150 intoffset = cum->intoffset;
8151 cum->intoffset = -1;
8153 /* If this is the trailing part of a word, try to only load that
8154 much into the register. Otherwise load the whole register. Note
8155 that in the latter case we may pick up unwanted bits. It's not a
8156 problem at the moment but may wish to revisit. */
8158 if (intoffset % BITS_PER_WORD != 0)
8160 mode = mode_for_size (BITS_PER_WORD - intoffset % BITS_PER_WORD,
8161 MODE_INT, 0);
8162 if (mode == BLKmode)
8164 /* We couldn't find an appropriate mode, which happens,
8165 e.g., in packed structs when there are 3 bytes to load.
8166 Back intoffset back to the beginning of the word in this
8167 case. */
8168 intoffset = intoffset & -BITS_PER_WORD;
8169 mode = word_mode;
8172 else
8173 mode = word_mode;
8175 startbit = intoffset & -BITS_PER_WORD;
8176 endbit = (bitpos + BITS_PER_WORD - 1) & -BITS_PER_WORD;
8177 intregs = (endbit - startbit) / BITS_PER_WORD;
8178 this_regno = cum->words + intoffset / BITS_PER_WORD;
8180 if (intregs > 0 && intregs > GP_ARG_NUM_REG - this_regno)
8181 cum->use_stack = 1;
8183 intregs = MIN (intregs, GP_ARG_NUM_REG - this_regno);
8184 if (intregs <= 0)
8185 return;
8187 intoffset /= BITS_PER_UNIT;
8190 regno = GP_ARG_MIN_REG + this_regno;
8191 reg = gen_rtx_REG (mode, regno);
8192 rvec[(*k)++] =
8193 gen_rtx_EXPR_LIST (VOIDmode, reg, GEN_INT (intoffset));
8195 this_regno += 1;
8196 intoffset = (intoffset | (UNITS_PER_WORD-1)) + 1;
8197 mode = word_mode;
8198 intregs -= 1;
8200 while (intregs > 0);
8203 /* Recursive workhorse for the following. */
8205 static void
8206 rs6000_darwin64_record_arg_recurse (CUMULATIVE_ARGS *cum, const_tree type,
8207 HOST_WIDE_INT startbitpos, rtx rvec[],
8208 int *k)
8210 tree f;
8212 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
8213 if (TREE_CODE (f) == FIELD_DECL)
8215 HOST_WIDE_INT bitpos = startbitpos;
8216 tree ftype = TREE_TYPE (f);
8217 enum machine_mode mode;
8218 if (ftype == error_mark_node)
8219 continue;
8220 mode = TYPE_MODE (ftype);
8222 if (DECL_SIZE (f) != 0
8223 && host_integerp (bit_position (f), 1))
8224 bitpos += int_bit_position (f);
8226 /* ??? FIXME: else assume zero offset. */
8228 if (TREE_CODE (ftype) == RECORD_TYPE)
8229 rs6000_darwin64_record_arg_recurse (cum, ftype, bitpos, rvec, k);
8230 else if (cum->named && USE_FP_FOR_ARG_P (cum, mode, ftype))
8232 unsigned n_fpreg = (GET_MODE_SIZE (mode) + 7) >> 3;
8233 #if 0
8234 switch (mode)
8236 case SCmode: mode = SFmode; break;
8237 case DCmode: mode = DFmode; break;
8238 case TCmode: mode = TFmode; break;
8239 default: break;
8241 #endif
8242 rs6000_darwin64_record_arg_flush (cum, bitpos, rvec, k);
8243 if (cum->fregno + n_fpreg > FP_ARG_MAX_REG + 1)
8245 gcc_assert (cum->fregno == FP_ARG_MAX_REG
8246 && (mode == TFmode || mode == TDmode));
8247 /* Long double or _Decimal128 split over regs and memory. */
8248 mode = DECIMAL_FLOAT_MODE_P (mode) ? DDmode : DFmode;
8249 cum->use_stack=1;
8251 rvec[(*k)++]
8252 = gen_rtx_EXPR_LIST (VOIDmode,
8253 gen_rtx_REG (mode, cum->fregno++),
8254 GEN_INT (bitpos / BITS_PER_UNIT));
8255 if (mode == TFmode || mode == TDmode)
8256 cum->fregno++;
8258 else if (cum->named && USE_ALTIVEC_FOR_ARG_P (cum, mode, ftype, 1))
8260 rs6000_darwin64_record_arg_flush (cum, bitpos, rvec, k);
8261 rvec[(*k)++]
8262 = gen_rtx_EXPR_LIST (VOIDmode,
8263 gen_rtx_REG (mode, cum->vregno++),
8264 GEN_INT (bitpos / BITS_PER_UNIT));
8266 else if (cum->intoffset == -1)
8267 cum->intoffset = bitpos;
8271 /* For the darwin64 ABI, we want to construct a PARALLEL consisting of
8272 the register(s) to be used for each field and subfield of a struct
8273 being passed by value, along with the offset of where the
8274 register's value may be found in the block. FP fields go in FP
8275 register, vector fields go in vector registers, and everything
8276 else goes in int registers, packed as in memory.
8278 This code is also used for function return values. RETVAL indicates
8279 whether this is the case.
8281 Much of this is taken from the SPARC V9 port, which has a similar
8282 calling convention. */
8284 static rtx
8285 rs6000_darwin64_record_arg (CUMULATIVE_ARGS *orig_cum, const_tree type,
8286 bool named, bool retval)
8288 rtx rvec[FIRST_PSEUDO_REGISTER];
8289 int k = 1, kbase = 1;
8290 HOST_WIDE_INT typesize = int_size_in_bytes (type);
8291 /* This is a copy; modifications are not visible to our caller. */
8292 CUMULATIVE_ARGS copy_cum = *orig_cum;
8293 CUMULATIVE_ARGS *cum = &copy_cum;
8295 /* Pad to 16 byte boundary if needed. */
8296 if (!retval && TYPE_ALIGN (type) >= 2 * BITS_PER_WORD
8297 && (cum->words % 2) != 0)
8298 cum->words++;
8300 cum->intoffset = 0;
8301 cum->use_stack = 0;
8302 cum->named = named;
8304 /* Put entries into rvec[] for individual FP and vector fields, and
8305 for the chunks of memory that go in int regs. Note we start at
8306 element 1; 0 is reserved for an indication of using memory, and
8307 may or may not be filled in below. */
8308 rs6000_darwin64_record_arg_recurse (cum, type, /* startbit pos= */ 0, rvec, &k);
8309 rs6000_darwin64_record_arg_flush (cum, typesize * BITS_PER_UNIT, rvec, &k);
8311 /* If any part of the struct went on the stack put all of it there.
8312 This hack is because the generic code for
8313 FUNCTION_ARG_PARTIAL_NREGS cannot handle cases where the register
8314 parts of the struct are not at the beginning. */
8315 if (cum->use_stack)
8317 if (retval)
8318 return NULL_RTX; /* doesn't go in registers at all */
8319 kbase = 0;
8320 rvec[0] = gen_rtx_EXPR_LIST (VOIDmode, NULL_RTX, const0_rtx);
8322 if (k > 1 || cum->use_stack)
8323 return gen_rtx_PARALLEL (BLKmode, gen_rtvec_v (k - kbase, &rvec[kbase]));
8324 else
8325 return NULL_RTX;
8328 /* Determine where to place an argument in 64-bit mode with 32-bit ABI. */
8330 static rtx
8331 rs6000_mixed_function_arg (enum machine_mode mode, const_tree type,
8332 int align_words)
8334 int n_units;
8335 int i, k;
8336 rtx rvec[GP_ARG_NUM_REG + 1];
8338 if (align_words >= GP_ARG_NUM_REG)
8339 return NULL_RTX;
8341 n_units = rs6000_arg_size (mode, type);
8343 /* Optimize the simple case where the arg fits in one gpr, except in
8344 the case of BLKmode due to assign_parms assuming that registers are
8345 BITS_PER_WORD wide. */
8346 if (n_units == 0
8347 || (n_units == 1 && mode != BLKmode))
8348 return gen_rtx_REG (mode, GP_ARG_MIN_REG + align_words);
8350 k = 0;
8351 if (align_words + n_units > GP_ARG_NUM_REG)
8352 /* Not all of the arg fits in gprs. Say that it goes in memory too,
8353 using a magic NULL_RTX component.
8354 This is not strictly correct. Only some of the arg belongs in
8355 memory, not all of it. However, the normal scheme using
8356 function_arg_partial_nregs can result in unusual subregs, eg.
8357 (subreg:SI (reg:DF) 4), which are not handled well. The code to
8358 store the whole arg to memory is often more efficient than code
8359 to store pieces, and we know that space is available in the right
8360 place for the whole arg. */
8361 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, NULL_RTX, const0_rtx);
8363 i = 0;
8366 rtx r = gen_rtx_REG (SImode, GP_ARG_MIN_REG + align_words);
8367 rtx off = GEN_INT (i++ * 4);
8368 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, off);
8370 while (++align_words < GP_ARG_NUM_REG && --n_units != 0);
8372 return gen_rtx_PARALLEL (mode, gen_rtvec_v (k, rvec));
8375 /* Determine where to put an argument to a function.
8376 Value is zero to push the argument on the stack,
8377 or a hard register in which to store the argument.
8379 MODE is the argument's machine mode.
8380 TYPE is the data type of the argument (as a tree).
8381 This is null for libcalls where that information may
8382 not be available.
8383 CUM is a variable of type CUMULATIVE_ARGS which gives info about
8384 the preceding args and about the function being called. It is
8385 not modified in this routine.
8386 NAMED is nonzero if this argument is a named parameter
8387 (otherwise it is an extra parameter matching an ellipsis).
8389 On RS/6000 the first eight words of non-FP are normally in registers
8390 and the rest are pushed. Under AIX, the first 13 FP args are in registers.
8391 Under V.4, the first 8 FP args are in registers.
8393 If this is floating-point and no prototype is specified, we use
8394 both an FP and integer register (or possibly FP reg and stack). Library
8395 functions (when CALL_LIBCALL is set) always have the proper types for args,
8396 so we can pass the FP value just in one register. emit_library_function
8397 doesn't support PARALLEL anyway.
8399 Note that for args passed by reference, function_arg will be called
8400 with MODE and TYPE set to that of the pointer to the arg, not the arg
8401 itself. */
8403 static rtx
8404 rs6000_function_arg (cumulative_args_t cum_v, enum machine_mode mode,
8405 const_tree type, bool named)
8407 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
8408 enum rs6000_abi abi = DEFAULT_ABI;
8410 /* Return a marker to indicate whether CR1 needs to set or clear the
8411 bit that V.4 uses to say fp args were passed in registers.
8412 Assume that we don't need the marker for software floating point,
8413 or compiler generated library calls. */
8414 if (mode == VOIDmode)
8416 if (abi == ABI_V4
8417 && (cum->call_cookie & CALL_LIBCALL) == 0
8418 && (cum->stdarg
8419 || (cum->nargs_prototype < 0
8420 && (cum->prototype || TARGET_NO_PROTOTYPE))))
8422 /* For the SPE, we need to crxor CR6 always. */
8423 if (TARGET_SPE_ABI)
8424 return GEN_INT (cum->call_cookie | CALL_V4_SET_FP_ARGS);
8425 else if (TARGET_HARD_FLOAT && TARGET_FPRS)
8426 return GEN_INT (cum->call_cookie
8427 | ((cum->fregno == FP_ARG_MIN_REG)
8428 ? CALL_V4_SET_FP_ARGS
8429 : CALL_V4_CLEAR_FP_ARGS));
8432 return GEN_INT (cum->call_cookie & ~CALL_LIBCALL);
8435 if (TARGET_MACHO && rs6000_darwin64_struct_check_p (mode, type))
8437 rtx rslt = rs6000_darwin64_record_arg (cum, type, named, /*retval= */false);
8438 if (rslt != NULL_RTX)
8439 return rslt;
8440 /* Else fall through to usual handling. */
8443 if (USE_ALTIVEC_FOR_ARG_P (cum, mode, type, named))
8444 if (TARGET_64BIT && ! cum->prototype)
8446 /* Vector parameters get passed in vector register
8447 and also in GPRs or memory, in absence of prototype. */
8448 int align_words;
8449 rtx slot;
8450 align_words = (cum->words + 1) & ~1;
8452 if (align_words >= GP_ARG_NUM_REG)
8454 slot = NULL_RTX;
8456 else
8458 slot = gen_rtx_REG (mode, GP_ARG_MIN_REG + align_words);
8460 return gen_rtx_PARALLEL (mode,
8461 gen_rtvec (2,
8462 gen_rtx_EXPR_LIST (VOIDmode,
8463 slot, const0_rtx),
8464 gen_rtx_EXPR_LIST (VOIDmode,
8465 gen_rtx_REG (mode, cum->vregno),
8466 const0_rtx)));
8468 else
8469 return gen_rtx_REG (mode, cum->vregno);
8470 else if (TARGET_ALTIVEC_ABI
8471 && (ALTIVEC_OR_VSX_VECTOR_MODE (mode)
8472 || (type && TREE_CODE (type) == VECTOR_TYPE
8473 && int_size_in_bytes (type) == 16)))
8475 if (named || abi == ABI_V4)
8476 return NULL_RTX;
8477 else
8479 /* Vector parameters to varargs functions under AIX or Darwin
8480 get passed in memory and possibly also in GPRs. */
8481 int align, align_words, n_words;
8482 enum machine_mode part_mode;
8484 /* Vector parameters must be 16-byte aligned. This places them at
8485 2 mod 4 in terms of words in 32-bit mode, since the parameter
8486 save area starts at offset 24 from the stack. In 64-bit mode,
8487 they just have to start on an even word, since the parameter
8488 save area is 16-byte aligned. */
8489 if (TARGET_32BIT)
8490 align = (2 - cum->words) & 3;
8491 else
8492 align = cum->words & 1;
8493 align_words = cum->words + align;
8495 /* Out of registers? Memory, then. */
8496 if (align_words >= GP_ARG_NUM_REG)
8497 return NULL_RTX;
8499 if (TARGET_32BIT && TARGET_POWERPC64)
8500 return rs6000_mixed_function_arg (mode, type, align_words);
8502 /* The vector value goes in GPRs. Only the part of the
8503 value in GPRs is reported here. */
8504 part_mode = mode;
8505 n_words = rs6000_arg_size (mode, type);
8506 if (align_words + n_words > GP_ARG_NUM_REG)
8507 /* Fortunately, there are only two possibilities, the value
8508 is either wholly in GPRs or half in GPRs and half not. */
8509 part_mode = DImode;
8511 return gen_rtx_REG (part_mode, GP_ARG_MIN_REG + align_words);
8514 else if (TARGET_SPE_ABI && TARGET_SPE
8515 && (SPE_VECTOR_MODE (mode)
8516 || (TARGET_E500_DOUBLE && (mode == DFmode
8517 || mode == DCmode
8518 || mode == TFmode
8519 || mode == TCmode))))
8520 return rs6000_spe_function_arg (cum, mode, type);
8522 else if (abi == ABI_V4)
8524 if (TARGET_HARD_FLOAT && TARGET_FPRS
8525 && ((TARGET_SINGLE_FLOAT && mode == SFmode)
8526 || (TARGET_DOUBLE_FLOAT && mode == DFmode)
8527 || (mode == TFmode && !TARGET_IEEEQUAD)
8528 || mode == SDmode || mode == DDmode || mode == TDmode))
8530 /* _Decimal128 must use an even/odd register pair. This assumes
8531 that the register number is odd when fregno is odd. */
8532 if (mode == TDmode && (cum->fregno % 2) == 1)
8533 cum->fregno++;
8535 if (cum->fregno + (mode == TFmode || mode == TDmode ? 1 : 0)
8536 <= FP_ARG_V4_MAX_REG)
8537 return gen_rtx_REG (mode, cum->fregno);
8538 else
8539 return NULL_RTX;
8541 else
8543 int n_words = rs6000_arg_size (mode, type);
8544 int gregno = cum->sysv_gregno;
8546 /* Long long and SPE vectors are put in (r3,r4), (r5,r6),
8547 (r7,r8) or (r9,r10). As does any other 2 word item such
8548 as complex int due to a historical mistake. */
8549 if (n_words == 2)
8550 gregno += (1 - gregno) & 1;
8552 /* Multi-reg args are not split between registers and stack. */
8553 if (gregno + n_words - 1 > GP_ARG_MAX_REG)
8554 return NULL_RTX;
8556 if (TARGET_32BIT && TARGET_POWERPC64)
8557 return rs6000_mixed_function_arg (mode, type,
8558 gregno - GP_ARG_MIN_REG);
8559 return gen_rtx_REG (mode, gregno);
8562 else
8564 int align_words = rs6000_parm_start (mode, type, cum->words);
8566 /* _Decimal128 must be passed in an even/odd float register pair.
8567 This assumes that the register number is odd when fregno is odd. */
8568 if (mode == TDmode && (cum->fregno % 2) == 1)
8569 cum->fregno++;
8571 if (USE_FP_FOR_ARG_P (cum, mode, type))
8573 rtx rvec[GP_ARG_NUM_REG + 1];
8574 rtx r;
8575 int k;
8576 bool needs_psave;
8577 enum machine_mode fmode = mode;
8578 unsigned long n_fpreg = (GET_MODE_SIZE (mode) + 7) >> 3;
8580 if (cum->fregno + n_fpreg > FP_ARG_MAX_REG + 1)
8582 /* Currently, we only ever need one reg here because complex
8583 doubles are split. */
8584 gcc_assert (cum->fregno == FP_ARG_MAX_REG
8585 && (fmode == TFmode || fmode == TDmode));
8587 /* Long double or _Decimal128 split over regs and memory. */
8588 fmode = DECIMAL_FLOAT_MODE_P (fmode) ? DDmode : DFmode;
8591 /* Do we also need to pass this arg in the parameter save
8592 area? */
8593 needs_psave = (type
8594 && (cum->nargs_prototype <= 0
8595 || (DEFAULT_ABI == ABI_AIX
8596 && TARGET_XL_COMPAT
8597 && align_words >= GP_ARG_NUM_REG)));
8599 if (!needs_psave && mode == fmode)
8600 return gen_rtx_REG (fmode, cum->fregno);
8602 k = 0;
8603 if (needs_psave)
8605 /* Describe the part that goes in gprs or the stack.
8606 This piece must come first, before the fprs. */
8607 if (align_words < GP_ARG_NUM_REG)
8609 unsigned long n_words = rs6000_arg_size (mode, type);
8611 if (align_words + n_words > GP_ARG_NUM_REG
8612 || (TARGET_32BIT && TARGET_POWERPC64))
8614 /* If this is partially on the stack, then we only
8615 include the portion actually in registers here. */
8616 enum machine_mode rmode = TARGET_32BIT ? SImode : DImode;
8617 rtx off;
8618 int i = 0;
8619 if (align_words + n_words > GP_ARG_NUM_REG)
8620 /* Not all of the arg fits in gprs. Say that it
8621 goes in memory too, using a magic NULL_RTX
8622 component. Also see comment in
8623 rs6000_mixed_function_arg for why the normal
8624 function_arg_partial_nregs scheme doesn't work
8625 in this case. */
8626 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, NULL_RTX,
8627 const0_rtx);
8630 r = gen_rtx_REG (rmode,
8631 GP_ARG_MIN_REG + align_words);
8632 off = GEN_INT (i++ * GET_MODE_SIZE (rmode));
8633 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, off);
8635 while (++align_words < GP_ARG_NUM_REG && --n_words != 0);
8637 else
8639 /* The whole arg fits in gprs. */
8640 r = gen_rtx_REG (mode, GP_ARG_MIN_REG + align_words);
8641 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, const0_rtx);
8644 else
8645 /* It's entirely in memory. */
8646 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, NULL_RTX, const0_rtx);
8649 /* Describe where this piece goes in the fprs. */
8650 r = gen_rtx_REG (fmode, cum->fregno);
8651 rvec[k++] = gen_rtx_EXPR_LIST (VOIDmode, r, const0_rtx);
8653 return gen_rtx_PARALLEL (mode, gen_rtvec_v (k, rvec));
8655 else if (align_words < GP_ARG_NUM_REG)
8657 if (TARGET_32BIT && TARGET_POWERPC64)
8658 return rs6000_mixed_function_arg (mode, type, align_words);
8660 if (mode == BLKmode)
8661 mode = Pmode;
8663 return gen_rtx_REG (mode, GP_ARG_MIN_REG + align_words);
8665 else
8666 return NULL_RTX;
8670 /* For an arg passed partly in registers and partly in memory, this is
8671 the number of bytes passed in registers. For args passed entirely in
8672 registers or entirely in memory, zero. When an arg is described by a
8673 PARALLEL, perhaps using more than one register type, this function
8674 returns the number of bytes used by the first element of the PARALLEL. */
8676 static int
8677 rs6000_arg_partial_bytes (cumulative_args_t cum_v, enum machine_mode mode,
8678 tree type, bool named)
8680 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
8681 int ret = 0;
8682 int align_words;
8684 if (DEFAULT_ABI == ABI_V4)
8685 return 0;
8687 if (USE_ALTIVEC_FOR_ARG_P (cum, mode, type, named)
8688 && cum->nargs_prototype >= 0)
8689 return 0;
8691 /* In this complicated case we just disable the partial_nregs code. */
8692 if (TARGET_MACHO && rs6000_darwin64_struct_check_p (mode, type))
8693 return 0;
8695 align_words = rs6000_parm_start (mode, type, cum->words);
8697 if (USE_FP_FOR_ARG_P (cum, mode, type))
8699 /* If we are passing this arg in the fixed parameter save area
8700 (gprs or memory) as well as fprs, then this function should
8701 return the number of partial bytes passed in the parameter
8702 save area rather than partial bytes passed in fprs. */
8703 if (type
8704 && (cum->nargs_prototype <= 0
8705 || (DEFAULT_ABI == ABI_AIX
8706 && TARGET_XL_COMPAT
8707 && align_words >= GP_ARG_NUM_REG)))
8708 return 0;
8709 else if (cum->fregno + ((GET_MODE_SIZE (mode) + 7) >> 3)
8710 > FP_ARG_MAX_REG + 1)
8711 ret = (FP_ARG_MAX_REG + 1 - cum->fregno) * 8;
8712 else if (cum->nargs_prototype >= 0)
8713 return 0;
8716 if (align_words < GP_ARG_NUM_REG
8717 && GP_ARG_NUM_REG < align_words + rs6000_arg_size (mode, type))
8718 ret = (GP_ARG_NUM_REG - align_words) * (TARGET_32BIT ? 4 : 8);
8720 if (ret != 0 && TARGET_DEBUG_ARG)
8721 fprintf (stderr, "rs6000_arg_partial_bytes: %d\n", ret);
8723 return ret;
8726 /* A C expression that indicates when an argument must be passed by
8727 reference. If nonzero for an argument, a copy of that argument is
8728 made in memory and a pointer to the argument is passed instead of
8729 the argument itself. The pointer is passed in whatever way is
8730 appropriate for passing a pointer to that type.
8732 Under V.4, aggregates and long double are passed by reference.
8734 As an extension to all 32-bit ABIs, AltiVec vectors are passed by
8735 reference unless the AltiVec vector extension ABI is in force.
8737 As an extension to all ABIs, variable sized types are passed by
8738 reference. */
8740 static bool
8741 rs6000_pass_by_reference (cumulative_args_t cum ATTRIBUTE_UNUSED,
8742 enum machine_mode mode, const_tree type,
8743 bool named ATTRIBUTE_UNUSED)
8745 if (DEFAULT_ABI == ABI_V4 && TARGET_IEEEQUAD && mode == TFmode)
8747 if (TARGET_DEBUG_ARG)
8748 fprintf (stderr, "function_arg_pass_by_reference: V4 long double\n");
8749 return 1;
8752 if (!type)
8753 return 0;
8755 if (DEFAULT_ABI == ABI_V4 && AGGREGATE_TYPE_P (type))
8757 if (TARGET_DEBUG_ARG)
8758 fprintf (stderr, "function_arg_pass_by_reference: V4 aggregate\n");
8759 return 1;
8762 if (int_size_in_bytes (type) < 0)
8764 if (TARGET_DEBUG_ARG)
8765 fprintf (stderr, "function_arg_pass_by_reference: variable size\n");
8766 return 1;
8769 /* Allow -maltivec -mabi=no-altivec without warning. Altivec vector
8770 modes only exist for GCC vector types if -maltivec. */
8771 if (TARGET_32BIT && !TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (mode))
8773 if (TARGET_DEBUG_ARG)
8774 fprintf (stderr, "function_arg_pass_by_reference: AltiVec\n");
8775 return 1;
8778 /* Pass synthetic vectors in memory. */
8779 if (TREE_CODE (type) == VECTOR_TYPE
8780 && int_size_in_bytes (type) > (TARGET_ALTIVEC_ABI ? 16 : 8))
8782 static bool warned_for_pass_big_vectors = false;
8783 if (TARGET_DEBUG_ARG)
8784 fprintf (stderr, "function_arg_pass_by_reference: synthetic vector\n");
8785 if (!warned_for_pass_big_vectors)
8787 warning (0, "GCC vector passed by reference: "
8788 "non-standard ABI extension with no compatibility guarantee");
8789 warned_for_pass_big_vectors = true;
8791 return 1;
8794 return 0;
8797 static void
8798 rs6000_move_block_from_reg (int regno, rtx x, int nregs)
8800 int i;
8801 enum machine_mode reg_mode = TARGET_32BIT ? SImode : DImode;
8803 if (nregs == 0)
8804 return;
8806 for (i = 0; i < nregs; i++)
8808 rtx tem = adjust_address_nv (x, reg_mode, i * GET_MODE_SIZE (reg_mode));
8809 if (reload_completed)
8811 if (! strict_memory_address_p (reg_mode, XEXP (tem, 0)))
8812 tem = NULL_RTX;
8813 else
8814 tem = simplify_gen_subreg (reg_mode, x, BLKmode,
8815 i * GET_MODE_SIZE (reg_mode));
8817 else
8818 tem = replace_equiv_address (tem, XEXP (tem, 0));
8820 gcc_assert (tem);
8822 emit_move_insn (tem, gen_rtx_REG (reg_mode, regno + i));
8826 /* Perform any needed actions needed for a function that is receiving a
8827 variable number of arguments.
8829 CUM is as above.
8831 MODE and TYPE are the mode and type of the current parameter.
8833 PRETEND_SIZE is a variable that should be set to the amount of stack
8834 that must be pushed by the prolog to pretend that our caller pushed
8837 Normally, this macro will push all remaining incoming registers on the
8838 stack and set PRETEND_SIZE to the length of the registers pushed. */
8840 static void
8841 setup_incoming_varargs (cumulative_args_t cum, enum machine_mode mode,
8842 tree type, int *pretend_size ATTRIBUTE_UNUSED,
8843 int no_rtl)
8845 CUMULATIVE_ARGS next_cum;
8846 int reg_size = TARGET_32BIT ? 4 : 8;
8847 rtx save_area = NULL_RTX, mem;
8848 int first_reg_offset;
8849 alias_set_type set;
8851 /* Skip the last named argument. */
8852 next_cum = *get_cumulative_args (cum);
8853 rs6000_function_arg_advance_1 (&next_cum, mode, type, true, 0);
8855 if (DEFAULT_ABI == ABI_V4)
8857 first_reg_offset = next_cum.sysv_gregno - GP_ARG_MIN_REG;
8859 if (! no_rtl)
8861 int gpr_reg_num = 0, gpr_size = 0, fpr_size = 0;
8862 HOST_WIDE_INT offset = 0;
8864 /* Try to optimize the size of the varargs save area.
8865 The ABI requires that ap.reg_save_area is doubleword
8866 aligned, but we don't need to allocate space for all
8867 the bytes, only those to which we actually will save
8868 anything. */
8869 if (cfun->va_list_gpr_size && first_reg_offset < GP_ARG_NUM_REG)
8870 gpr_reg_num = GP_ARG_NUM_REG - first_reg_offset;
8871 if (TARGET_HARD_FLOAT && TARGET_FPRS
8872 && next_cum.fregno <= FP_ARG_V4_MAX_REG
8873 && cfun->va_list_fpr_size)
8875 if (gpr_reg_num)
8876 fpr_size = (next_cum.fregno - FP_ARG_MIN_REG)
8877 * UNITS_PER_FP_WORD;
8878 if (cfun->va_list_fpr_size
8879 < FP_ARG_V4_MAX_REG + 1 - next_cum.fregno)
8880 fpr_size += cfun->va_list_fpr_size * UNITS_PER_FP_WORD;
8881 else
8882 fpr_size += (FP_ARG_V4_MAX_REG + 1 - next_cum.fregno)
8883 * UNITS_PER_FP_WORD;
8885 if (gpr_reg_num)
8887 offset = -((first_reg_offset * reg_size) & ~7);
8888 if (!fpr_size && gpr_reg_num > cfun->va_list_gpr_size)
8890 gpr_reg_num = cfun->va_list_gpr_size;
8891 if (reg_size == 4 && (first_reg_offset & 1))
8892 gpr_reg_num++;
8894 gpr_size = (gpr_reg_num * reg_size + 7) & ~7;
8896 else if (fpr_size)
8897 offset = - (int) (next_cum.fregno - FP_ARG_MIN_REG)
8898 * UNITS_PER_FP_WORD
8899 - (int) (GP_ARG_NUM_REG * reg_size);
8901 if (gpr_size + fpr_size)
8903 rtx reg_save_area
8904 = assign_stack_local (BLKmode, gpr_size + fpr_size, 64);
8905 gcc_assert (GET_CODE (reg_save_area) == MEM);
8906 reg_save_area = XEXP (reg_save_area, 0);
8907 if (GET_CODE (reg_save_area) == PLUS)
8909 gcc_assert (XEXP (reg_save_area, 0)
8910 == virtual_stack_vars_rtx);
8911 gcc_assert (GET_CODE (XEXP (reg_save_area, 1)) == CONST_INT);
8912 offset += INTVAL (XEXP (reg_save_area, 1));
8914 else
8915 gcc_assert (reg_save_area == virtual_stack_vars_rtx);
8918 cfun->machine->varargs_save_offset = offset;
8919 save_area = plus_constant (Pmode, virtual_stack_vars_rtx, offset);
8922 else
8924 first_reg_offset = next_cum.words;
8925 save_area = virtual_incoming_args_rtx;
8927 if (targetm.calls.must_pass_in_stack (mode, type))
8928 first_reg_offset += rs6000_arg_size (TYPE_MODE (type), type);
8931 set = get_varargs_alias_set ();
8932 if (! no_rtl && first_reg_offset < GP_ARG_NUM_REG
8933 && cfun->va_list_gpr_size)
8935 int nregs = GP_ARG_NUM_REG - first_reg_offset;
8937 if (va_list_gpr_counter_field)
8939 /* V4 va_list_gpr_size counts number of registers needed. */
8940 if (nregs > cfun->va_list_gpr_size)
8941 nregs = cfun->va_list_gpr_size;
8943 else
8945 /* char * va_list instead counts number of bytes needed. */
8946 if (nregs > cfun->va_list_gpr_size / reg_size)
8947 nregs = cfun->va_list_gpr_size / reg_size;
8950 mem = gen_rtx_MEM (BLKmode,
8951 plus_constant (Pmode, save_area,
8952 first_reg_offset * reg_size));
8953 MEM_NOTRAP_P (mem) = 1;
8954 set_mem_alias_set (mem, set);
8955 set_mem_align (mem, BITS_PER_WORD);
8957 rs6000_move_block_from_reg (GP_ARG_MIN_REG + first_reg_offset, mem,
8958 nregs);
8961 /* Save FP registers if needed. */
8962 if (DEFAULT_ABI == ABI_V4
8963 && TARGET_HARD_FLOAT && TARGET_FPRS
8964 && ! no_rtl
8965 && next_cum.fregno <= FP_ARG_V4_MAX_REG
8966 && cfun->va_list_fpr_size)
8968 int fregno = next_cum.fregno, nregs;
8969 rtx cr1 = gen_rtx_REG (CCmode, CR1_REGNO);
8970 rtx lab = gen_label_rtx ();
8971 int off = (GP_ARG_NUM_REG * reg_size) + ((fregno - FP_ARG_MIN_REG)
8972 * UNITS_PER_FP_WORD);
8974 emit_jump_insn
8975 (gen_rtx_SET (VOIDmode,
8976 pc_rtx,
8977 gen_rtx_IF_THEN_ELSE (VOIDmode,
8978 gen_rtx_NE (VOIDmode, cr1,
8979 const0_rtx),
8980 gen_rtx_LABEL_REF (VOIDmode, lab),
8981 pc_rtx)));
8983 for (nregs = 0;
8984 fregno <= FP_ARG_V4_MAX_REG && nregs < cfun->va_list_fpr_size;
8985 fregno++, off += UNITS_PER_FP_WORD, nregs++)
8987 mem = gen_rtx_MEM ((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT)
8988 ? DFmode : SFmode,
8989 plus_constant (Pmode, save_area, off));
8990 MEM_NOTRAP_P (mem) = 1;
8991 set_mem_alias_set (mem, set);
8992 set_mem_align (mem, GET_MODE_ALIGNMENT (
8993 (TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT)
8994 ? DFmode : SFmode));
8995 emit_move_insn (mem, gen_rtx_REG (
8996 (TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT)
8997 ? DFmode : SFmode, fregno));
9000 emit_label (lab);
9004 /* Create the va_list data type. */
9006 static tree
9007 rs6000_build_builtin_va_list (void)
9009 tree f_gpr, f_fpr, f_res, f_ovf, f_sav, record, type_decl;
9011 /* For AIX, prefer 'char *' because that's what the system
9012 header files like. */
9013 if (DEFAULT_ABI != ABI_V4)
9014 return build_pointer_type (char_type_node);
9016 record = (*lang_hooks.types.make_type) (RECORD_TYPE);
9017 type_decl = build_decl (BUILTINS_LOCATION, TYPE_DECL,
9018 get_identifier ("__va_list_tag"), record);
9020 f_gpr = build_decl (BUILTINS_LOCATION, FIELD_DECL, get_identifier ("gpr"),
9021 unsigned_char_type_node);
9022 f_fpr = build_decl (BUILTINS_LOCATION, FIELD_DECL, get_identifier ("fpr"),
9023 unsigned_char_type_node);
9024 /* Give the two bytes of padding a name, so that -Wpadded won't warn on
9025 every user file. */
9026 f_res = build_decl (BUILTINS_LOCATION, FIELD_DECL,
9027 get_identifier ("reserved"), short_unsigned_type_node);
9028 f_ovf = build_decl (BUILTINS_LOCATION, FIELD_DECL,
9029 get_identifier ("overflow_arg_area"),
9030 ptr_type_node);
9031 f_sav = build_decl (BUILTINS_LOCATION, FIELD_DECL,
9032 get_identifier ("reg_save_area"),
9033 ptr_type_node);
9035 va_list_gpr_counter_field = f_gpr;
9036 va_list_fpr_counter_field = f_fpr;
9038 DECL_FIELD_CONTEXT (f_gpr) = record;
9039 DECL_FIELD_CONTEXT (f_fpr) = record;
9040 DECL_FIELD_CONTEXT (f_res) = record;
9041 DECL_FIELD_CONTEXT (f_ovf) = record;
9042 DECL_FIELD_CONTEXT (f_sav) = record;
9044 TYPE_STUB_DECL (record) = type_decl;
9045 TYPE_NAME (record) = type_decl;
9046 TYPE_FIELDS (record) = f_gpr;
9047 DECL_CHAIN (f_gpr) = f_fpr;
9048 DECL_CHAIN (f_fpr) = f_res;
9049 DECL_CHAIN (f_res) = f_ovf;
9050 DECL_CHAIN (f_ovf) = f_sav;
9052 layout_type (record);
9054 /* The correct type is an array type of one element. */
9055 return build_array_type (record, build_index_type (size_zero_node));
9058 /* Implement va_start. */
9060 static void
9061 rs6000_va_start (tree valist, rtx nextarg)
9063 HOST_WIDE_INT words, n_gpr, n_fpr;
9064 tree f_gpr, f_fpr, f_res, f_ovf, f_sav;
9065 tree gpr, fpr, ovf, sav, t;
9067 /* Only SVR4 needs something special. */
9068 if (DEFAULT_ABI != ABI_V4)
9070 std_expand_builtin_va_start (valist, nextarg);
9071 return;
9074 f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
9075 f_fpr = DECL_CHAIN (f_gpr);
9076 f_res = DECL_CHAIN (f_fpr);
9077 f_ovf = DECL_CHAIN (f_res);
9078 f_sav = DECL_CHAIN (f_ovf);
9080 valist = build_simple_mem_ref (valist);
9081 gpr = build3 (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr, NULL_TREE);
9082 fpr = build3 (COMPONENT_REF, TREE_TYPE (f_fpr), unshare_expr (valist),
9083 f_fpr, NULL_TREE);
9084 ovf = build3 (COMPONENT_REF, TREE_TYPE (f_ovf), unshare_expr (valist),
9085 f_ovf, NULL_TREE);
9086 sav = build3 (COMPONENT_REF, TREE_TYPE (f_sav), unshare_expr (valist),
9087 f_sav, NULL_TREE);
9089 /* Count number of gp and fp argument registers used. */
9090 words = crtl->args.info.words;
9091 n_gpr = MIN (crtl->args.info.sysv_gregno - GP_ARG_MIN_REG,
9092 GP_ARG_NUM_REG);
9093 n_fpr = MIN (crtl->args.info.fregno - FP_ARG_MIN_REG,
9094 FP_ARG_NUM_REG);
9096 if (TARGET_DEBUG_ARG)
9097 fprintf (stderr, "va_start: words = "HOST_WIDE_INT_PRINT_DEC", n_gpr = "
9098 HOST_WIDE_INT_PRINT_DEC", n_fpr = "HOST_WIDE_INT_PRINT_DEC"\n",
9099 words, n_gpr, n_fpr);
9101 if (cfun->va_list_gpr_size)
9103 t = build2 (MODIFY_EXPR, TREE_TYPE (gpr), gpr,
9104 build_int_cst (NULL_TREE, n_gpr));
9105 TREE_SIDE_EFFECTS (t) = 1;
9106 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
9109 if (cfun->va_list_fpr_size)
9111 t = build2 (MODIFY_EXPR, TREE_TYPE (fpr), fpr,
9112 build_int_cst (NULL_TREE, n_fpr));
9113 TREE_SIDE_EFFECTS (t) = 1;
9114 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
9116 #ifdef HAVE_AS_GNU_ATTRIBUTE
9117 if (call_ABI_of_interest (cfun->decl))
9118 rs6000_passes_float = true;
9119 #endif
9122 /* Find the overflow area. */
9123 t = make_tree (TREE_TYPE (ovf), virtual_incoming_args_rtx);
9124 if (words != 0)
9125 t = fold_build_pointer_plus_hwi (t, words * UNITS_PER_WORD);
9126 t = build2 (MODIFY_EXPR, TREE_TYPE (ovf), ovf, t);
9127 TREE_SIDE_EFFECTS (t) = 1;
9128 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
9130 /* If there were no va_arg invocations, don't set up the register
9131 save area. */
9132 if (!cfun->va_list_gpr_size
9133 && !cfun->va_list_fpr_size
9134 && n_gpr < GP_ARG_NUM_REG
9135 && n_fpr < FP_ARG_V4_MAX_REG)
9136 return;
9138 /* Find the register save area. */
9139 t = make_tree (TREE_TYPE (sav), virtual_stack_vars_rtx);
9140 if (cfun->machine->varargs_save_offset)
9141 t = fold_build_pointer_plus_hwi (t, cfun->machine->varargs_save_offset);
9142 t = build2 (MODIFY_EXPR, TREE_TYPE (sav), sav, t);
9143 TREE_SIDE_EFFECTS (t) = 1;
9144 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
9147 /* Implement va_arg. */
9149 static tree
9150 rs6000_gimplify_va_arg (tree valist, tree type, gimple_seq *pre_p,
9151 gimple_seq *post_p)
9153 tree f_gpr, f_fpr, f_res, f_ovf, f_sav;
9154 tree gpr, fpr, ovf, sav, reg, t, u;
9155 int size, rsize, n_reg, sav_ofs, sav_scale;
9156 tree lab_false, lab_over, addr;
9157 int align;
9158 tree ptrtype = build_pointer_type_for_mode (type, ptr_mode, true);
9159 int regalign = 0;
9160 gimple stmt;
9162 if (pass_by_reference (NULL, TYPE_MODE (type), type, false))
9164 t = rs6000_gimplify_va_arg (valist, ptrtype, pre_p, post_p);
9165 return build_va_arg_indirect_ref (t);
9168 /* We need to deal with the fact that the darwin ppc64 ABI is defined by an
9169 earlier version of gcc, with the property that it always applied alignment
9170 adjustments to the va-args (even for zero-sized types). The cheapest way
9171 to deal with this is to replicate the effect of the part of
9172 std_gimplify_va_arg_expr that carries out the align adjust, for the case
9173 of relevance.
9174 We don't need to check for pass-by-reference because of the test above.
9175 We can return a simplifed answer, since we know there's no offset to add. */
9177 if (TARGET_MACHO
9178 && rs6000_darwin64_abi
9179 && integer_zerop (TYPE_SIZE (type)))
9181 unsigned HOST_WIDE_INT align, boundary;
9182 tree valist_tmp = get_initialized_tmp_var (valist, pre_p, NULL);
9183 align = PARM_BOUNDARY / BITS_PER_UNIT;
9184 boundary = rs6000_function_arg_boundary (TYPE_MODE (type), type);
9185 if (boundary > MAX_SUPPORTED_STACK_ALIGNMENT)
9186 boundary = MAX_SUPPORTED_STACK_ALIGNMENT;
9187 boundary /= BITS_PER_UNIT;
9188 if (boundary > align)
9190 tree t ;
9191 /* This updates arg ptr by the amount that would be necessary
9192 to align the zero-sized (but not zero-alignment) item. */
9193 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
9194 fold_build_pointer_plus_hwi (valist_tmp, boundary - 1));
9195 gimplify_and_add (t, pre_p);
9197 t = fold_convert (sizetype, valist_tmp);
9198 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
9199 fold_convert (TREE_TYPE (valist),
9200 fold_build2 (BIT_AND_EXPR, sizetype, t,
9201 size_int (-boundary))));
9202 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist, t);
9203 gimplify_and_add (t, pre_p);
9205 /* Since it is zero-sized there's no increment for the item itself. */
9206 valist_tmp = fold_convert (build_pointer_type (type), valist_tmp);
9207 return build_va_arg_indirect_ref (valist_tmp);
9210 if (DEFAULT_ABI != ABI_V4)
9212 if (targetm.calls.split_complex_arg && TREE_CODE (type) == COMPLEX_TYPE)
9214 tree elem_type = TREE_TYPE (type);
9215 enum machine_mode elem_mode = TYPE_MODE (elem_type);
9216 int elem_size = GET_MODE_SIZE (elem_mode);
9218 if (elem_size < UNITS_PER_WORD)
9220 tree real_part, imag_part;
9221 gimple_seq post = NULL;
9223 real_part = rs6000_gimplify_va_arg (valist, elem_type, pre_p,
9224 &post);
9225 /* Copy the value into a temporary, lest the formal temporary
9226 be reused out from under us. */
9227 real_part = get_initialized_tmp_var (real_part, pre_p, &post);
9228 gimple_seq_add_seq (pre_p, post);
9230 imag_part = rs6000_gimplify_va_arg (valist, elem_type, pre_p,
9231 post_p);
9233 return build2 (COMPLEX_EXPR, type, real_part, imag_part);
9237 return std_gimplify_va_arg_expr (valist, type, pre_p, post_p);
9240 f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
9241 f_fpr = DECL_CHAIN (f_gpr);
9242 f_res = DECL_CHAIN (f_fpr);
9243 f_ovf = DECL_CHAIN (f_res);
9244 f_sav = DECL_CHAIN (f_ovf);
9246 valist = build_va_arg_indirect_ref (valist);
9247 gpr = build3 (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr, NULL_TREE);
9248 fpr = build3 (COMPONENT_REF, TREE_TYPE (f_fpr), unshare_expr (valist),
9249 f_fpr, NULL_TREE);
9250 ovf = build3 (COMPONENT_REF, TREE_TYPE (f_ovf), unshare_expr (valist),
9251 f_ovf, NULL_TREE);
9252 sav = build3 (COMPONENT_REF, TREE_TYPE (f_sav), unshare_expr (valist),
9253 f_sav, NULL_TREE);
9255 size = int_size_in_bytes (type);
9256 rsize = (size + 3) / 4;
9257 align = 1;
9259 if (TARGET_HARD_FLOAT && TARGET_FPRS
9260 && ((TARGET_SINGLE_FLOAT && TYPE_MODE (type) == SFmode)
9261 || (TARGET_DOUBLE_FLOAT
9262 && (TYPE_MODE (type) == DFmode
9263 || TYPE_MODE (type) == TFmode
9264 || TYPE_MODE (type) == SDmode
9265 || TYPE_MODE (type) == DDmode
9266 || TYPE_MODE (type) == TDmode))))
9268 /* FP args go in FP registers, if present. */
9269 reg = fpr;
9270 n_reg = (size + 7) / 8;
9271 sav_ofs = ((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT) ? 8 : 4) * 4;
9272 sav_scale = ((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT) ? 8 : 4);
9273 if (TYPE_MODE (type) != SFmode && TYPE_MODE (type) != SDmode)
9274 align = 8;
9276 else
9278 /* Otherwise into GP registers. */
9279 reg = gpr;
9280 n_reg = rsize;
9281 sav_ofs = 0;
9282 sav_scale = 4;
9283 if (n_reg == 2)
9284 align = 8;
9287 /* Pull the value out of the saved registers.... */
9289 lab_over = NULL;
9290 addr = create_tmp_var (ptr_type_node, "addr");
9292 /* AltiVec vectors never go in registers when -mabi=altivec. */
9293 if (TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (TYPE_MODE (type)))
9294 align = 16;
9295 else
9297 lab_false = create_artificial_label (input_location);
9298 lab_over = create_artificial_label (input_location);
9300 /* Long long and SPE vectors are aligned in the registers.
9301 As are any other 2 gpr item such as complex int due to a
9302 historical mistake. */
9303 u = reg;
9304 if (n_reg == 2 && reg == gpr)
9306 regalign = 1;
9307 u = build2 (BIT_AND_EXPR, TREE_TYPE (reg), unshare_expr (reg),
9308 build_int_cst (TREE_TYPE (reg), n_reg - 1));
9309 u = build2 (POSTINCREMENT_EXPR, TREE_TYPE (reg),
9310 unshare_expr (reg), u);
9312 /* _Decimal128 is passed in even/odd fpr pairs; the stored
9313 reg number is 0 for f1, so we want to make it odd. */
9314 else if (reg == fpr && TYPE_MODE (type) == TDmode)
9316 t = build2 (BIT_IOR_EXPR, TREE_TYPE (reg), unshare_expr (reg),
9317 build_int_cst (TREE_TYPE (reg), 1));
9318 u = build2 (MODIFY_EXPR, void_type_node, unshare_expr (reg), t);
9321 t = fold_convert (TREE_TYPE (reg), size_int (8 - n_reg + 1));
9322 t = build2 (GE_EXPR, boolean_type_node, u, t);
9323 u = build1 (GOTO_EXPR, void_type_node, lab_false);
9324 t = build3 (COND_EXPR, void_type_node, t, u, NULL_TREE);
9325 gimplify_and_add (t, pre_p);
9327 t = sav;
9328 if (sav_ofs)
9329 t = fold_build_pointer_plus_hwi (sav, sav_ofs);
9331 u = build2 (POSTINCREMENT_EXPR, TREE_TYPE (reg), unshare_expr (reg),
9332 build_int_cst (TREE_TYPE (reg), n_reg));
9333 u = fold_convert (sizetype, u);
9334 u = build2 (MULT_EXPR, sizetype, u, size_int (sav_scale));
9335 t = fold_build_pointer_plus (t, u);
9337 /* _Decimal32 varargs are located in the second word of the 64-bit
9338 FP register for 32-bit binaries. */
9339 if (!TARGET_POWERPC64
9340 && TARGET_HARD_FLOAT && TARGET_FPRS
9341 && TYPE_MODE (type) == SDmode)
9342 t = fold_build_pointer_plus_hwi (t, size);
9344 gimplify_assign (addr, t, pre_p);
9346 gimple_seq_add_stmt (pre_p, gimple_build_goto (lab_over));
9348 stmt = gimple_build_label (lab_false);
9349 gimple_seq_add_stmt (pre_p, stmt);
9351 if ((n_reg == 2 && !regalign) || n_reg > 2)
9353 /* Ensure that we don't find any more args in regs.
9354 Alignment has taken care of for special cases. */
9355 gimplify_assign (reg, build_int_cst (TREE_TYPE (reg), 8), pre_p);
9359 /* ... otherwise out of the overflow area. */
9361 /* Care for on-stack alignment if needed. */
9362 t = ovf;
9363 if (align != 1)
9365 t = fold_build_pointer_plus_hwi (t, align - 1);
9366 t = build2 (BIT_AND_EXPR, TREE_TYPE (t), t,
9367 build_int_cst (TREE_TYPE (t), -align));
9369 gimplify_expr (&t, pre_p, NULL, is_gimple_val, fb_rvalue);
9371 gimplify_assign (unshare_expr (addr), t, pre_p);
9373 t = fold_build_pointer_plus_hwi (t, size);
9374 gimplify_assign (unshare_expr (ovf), t, pre_p);
9376 if (lab_over)
9378 stmt = gimple_build_label (lab_over);
9379 gimple_seq_add_stmt (pre_p, stmt);
9382 if (STRICT_ALIGNMENT
9383 && (TYPE_ALIGN (type)
9384 > (unsigned) BITS_PER_UNIT * (align < 4 ? 4 : align)))
9386 /* The value (of type complex double, for example) may not be
9387 aligned in memory in the saved registers, so copy via a
9388 temporary. (This is the same code as used for SPARC.) */
9389 tree tmp = create_tmp_var (type, "va_arg_tmp");
9390 tree dest_addr = build_fold_addr_expr (tmp);
9392 tree copy = build_call_expr (builtin_decl_implicit (BUILT_IN_MEMCPY),
9393 3, dest_addr, addr, size_int (rsize * 4));
9395 gimplify_and_add (copy, pre_p);
9396 addr = dest_addr;
9399 addr = fold_convert (ptrtype, addr);
9400 return build_va_arg_indirect_ref (addr);
9403 /* Builtins. */
9405 static void
9406 def_builtin (const char *name, tree type, enum rs6000_builtins code)
9408 tree t;
9409 unsigned classify = rs6000_builtin_info[(int)code].attr;
9410 const char *attr_string = "";
9412 gcc_assert (name != NULL);
9413 gcc_assert (IN_RANGE ((int)code, 0, (int)RS6000_BUILTIN_COUNT));
9415 if (rs6000_builtin_decls[(int)code])
9416 fatal_error ("internal error: builtin function %s already processed", name);
9418 rs6000_builtin_decls[(int)code] = t =
9419 add_builtin_function (name, type, (int)code, BUILT_IN_MD, NULL, NULL_TREE);
9421 /* Set any special attributes. */
9422 if ((classify & RS6000_BTC_CONST) != 0)
9424 /* const function, function only depends on the inputs. */
9425 TREE_READONLY (t) = 1;
9426 TREE_NOTHROW (t) = 1;
9427 attr_string = ", pure";
9429 else if ((classify & RS6000_BTC_PURE) != 0)
9431 /* pure function, function can read global memory, but does not set any
9432 external state. */
9433 DECL_PURE_P (t) = 1;
9434 TREE_NOTHROW (t) = 1;
9435 attr_string = ", const";
9437 else if ((classify & RS6000_BTC_FP) != 0)
9439 /* Function is a math function. If rounding mode is on, then treat the
9440 function as not reading global memory, but it can have arbitrary side
9441 effects. If it is off, then assume the function is a const function.
9442 This mimics the ATTR_MATHFN_FPROUNDING attribute in
9443 builtin-attribute.def that is used for the math functions. */
9444 TREE_NOTHROW (t) = 1;
9445 if (flag_rounding_math)
9447 DECL_PURE_P (t) = 1;
9448 DECL_IS_NOVOPS (t) = 1;
9449 attr_string = ", fp, pure";
9451 else
9453 TREE_READONLY (t) = 1;
9454 attr_string = ", fp, const";
9457 else if ((classify & RS6000_BTC_ATTR_MASK) != 0)
9458 gcc_unreachable ();
9460 if (TARGET_DEBUG_BUILTIN)
9461 fprintf (stderr, "rs6000_builtin, code = %4d, %s%s\n",
9462 (int)code, name, attr_string);
9465 /* Simple ternary operations: VECd = foo (VECa, VECb, VECc). */
9467 #undef RS6000_BUILTIN_1
9468 #undef RS6000_BUILTIN_2
9469 #undef RS6000_BUILTIN_3
9470 #undef RS6000_BUILTIN_A
9471 #undef RS6000_BUILTIN_D
9472 #undef RS6000_BUILTIN_E
9473 #undef RS6000_BUILTIN_P
9474 #undef RS6000_BUILTIN_Q
9475 #undef RS6000_BUILTIN_S
9476 #undef RS6000_BUILTIN_X
9478 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
9479 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
9480 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE) \
9481 { MASK, ICODE, NAME, ENUM },
9483 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
9484 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
9485 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
9486 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
9487 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
9488 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
9489 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
9491 static const struct builtin_description bdesc_3arg[] =
9493 #include "rs6000-builtin.def"
9496 /* DST operations: void foo (void *, const int, const char). */
9498 #undef RS6000_BUILTIN_1
9499 #undef RS6000_BUILTIN_2
9500 #undef RS6000_BUILTIN_3
9501 #undef RS6000_BUILTIN_A
9502 #undef RS6000_BUILTIN_D
9503 #undef RS6000_BUILTIN_E
9504 #undef RS6000_BUILTIN_P
9505 #undef RS6000_BUILTIN_Q
9506 #undef RS6000_BUILTIN_S
9507 #undef RS6000_BUILTIN_X
9509 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
9510 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
9511 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
9512 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
9513 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE) \
9514 { MASK, ICODE, NAME, ENUM },
9516 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
9517 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
9518 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
9519 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
9520 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
9522 static const struct builtin_description bdesc_dst[] =
9524 #include "rs6000-builtin.def"
9527 /* Simple binary operations: VECc = foo (VECa, VECb). */
9529 #undef RS6000_BUILTIN_1
9530 #undef RS6000_BUILTIN_2
9531 #undef RS6000_BUILTIN_3
9532 #undef RS6000_BUILTIN_A
9533 #undef RS6000_BUILTIN_D
9534 #undef RS6000_BUILTIN_E
9535 #undef RS6000_BUILTIN_P
9536 #undef RS6000_BUILTIN_Q
9537 #undef RS6000_BUILTIN_S
9538 #undef RS6000_BUILTIN_X
9540 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
9541 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE) \
9542 { MASK, ICODE, NAME, ENUM },
9544 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
9545 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
9546 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
9547 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
9548 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
9549 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
9550 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
9551 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
9553 static const struct builtin_description bdesc_2arg[] =
9555 #include "rs6000-builtin.def"
9558 #undef RS6000_BUILTIN_1
9559 #undef RS6000_BUILTIN_2
9560 #undef RS6000_BUILTIN_3
9561 #undef RS6000_BUILTIN_A
9562 #undef RS6000_BUILTIN_D
9563 #undef RS6000_BUILTIN_E
9564 #undef RS6000_BUILTIN_P
9565 #undef RS6000_BUILTIN_Q
9566 #undef RS6000_BUILTIN_S
9567 #undef RS6000_BUILTIN_X
9569 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
9570 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
9571 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
9572 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
9573 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
9574 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
9575 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE) \
9576 { MASK, ICODE, NAME, ENUM },
9578 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
9579 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
9580 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
9582 /* AltiVec predicates. */
9584 static const struct builtin_description bdesc_altivec_preds[] =
9586 #include "rs6000-builtin.def"
9589 /* SPE predicates. */
9590 #undef RS6000_BUILTIN_1
9591 #undef RS6000_BUILTIN_2
9592 #undef RS6000_BUILTIN_3
9593 #undef RS6000_BUILTIN_A
9594 #undef RS6000_BUILTIN_D
9595 #undef RS6000_BUILTIN_E
9596 #undef RS6000_BUILTIN_P
9597 #undef RS6000_BUILTIN_Q
9598 #undef RS6000_BUILTIN_S
9599 #undef RS6000_BUILTIN_X
9601 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
9602 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
9603 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
9604 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
9605 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
9606 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
9607 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
9608 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
9609 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE) \
9610 { MASK, ICODE, NAME, ENUM },
9612 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
9614 static const struct builtin_description bdesc_spe_predicates[] =
9616 #include "rs6000-builtin.def"
9619 /* SPE evsel predicates. */
9620 #undef RS6000_BUILTIN_1
9621 #undef RS6000_BUILTIN_2
9622 #undef RS6000_BUILTIN_3
9623 #undef RS6000_BUILTIN_A
9624 #undef RS6000_BUILTIN_D
9625 #undef RS6000_BUILTIN_E
9626 #undef RS6000_BUILTIN_P
9627 #undef RS6000_BUILTIN_Q
9628 #undef RS6000_BUILTIN_S
9629 #undef RS6000_BUILTIN_X
9631 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
9632 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
9633 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
9634 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
9635 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
9636 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE) \
9637 { MASK, ICODE, NAME, ENUM },
9639 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
9640 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
9641 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
9642 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
9644 static const struct builtin_description bdesc_spe_evsel[] =
9646 #include "rs6000-builtin.def"
9649 /* PAIRED predicates. */
9650 #undef RS6000_BUILTIN_1
9651 #undef RS6000_BUILTIN_2
9652 #undef RS6000_BUILTIN_3
9653 #undef RS6000_BUILTIN_A
9654 #undef RS6000_BUILTIN_D
9655 #undef RS6000_BUILTIN_E
9656 #undef RS6000_BUILTIN_P
9657 #undef RS6000_BUILTIN_Q
9658 #undef RS6000_BUILTIN_S
9659 #undef RS6000_BUILTIN_X
9661 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
9662 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
9663 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
9664 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
9665 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
9666 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
9667 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
9668 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE) \
9669 { MASK, ICODE, NAME, ENUM },
9671 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
9672 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
9674 static const struct builtin_description bdesc_paired_preds[] =
9676 #include "rs6000-builtin.def"
9679 /* ABS* operations. */
9681 #undef RS6000_BUILTIN_1
9682 #undef RS6000_BUILTIN_2
9683 #undef RS6000_BUILTIN_3
9684 #undef RS6000_BUILTIN_A
9685 #undef RS6000_BUILTIN_D
9686 #undef RS6000_BUILTIN_E
9687 #undef RS6000_BUILTIN_P
9688 #undef RS6000_BUILTIN_Q
9689 #undef RS6000_BUILTIN_S
9690 #undef RS6000_BUILTIN_X
9692 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE)
9693 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
9694 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
9695 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE) \
9696 { MASK, ICODE, NAME, ENUM },
9698 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
9699 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
9700 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
9701 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
9702 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
9703 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
9705 static const struct builtin_description bdesc_abs[] =
9707 #include "rs6000-builtin.def"
9710 /* Simple unary operations: VECb = foo (unsigned literal) or VECb =
9711 foo (VECa). */
9713 #undef RS6000_BUILTIN_1
9714 #undef RS6000_BUILTIN_2
9715 #undef RS6000_BUILTIN_3
9716 #undef RS6000_BUILTIN_A
9717 #undef RS6000_BUILTIN_E
9718 #undef RS6000_BUILTIN_D
9719 #undef RS6000_BUILTIN_P
9720 #undef RS6000_BUILTIN_Q
9721 #undef RS6000_BUILTIN_S
9722 #undef RS6000_BUILTIN_X
9724 #define RS6000_BUILTIN_1(ENUM, NAME, MASK, ATTR, ICODE) \
9725 { MASK, ICODE, NAME, ENUM },
9727 #define RS6000_BUILTIN_2(ENUM, NAME, MASK, ATTR, ICODE)
9728 #define RS6000_BUILTIN_3(ENUM, NAME, MASK, ATTR, ICODE)
9729 #define RS6000_BUILTIN_A(ENUM, NAME, MASK, ATTR, ICODE)
9730 #define RS6000_BUILTIN_D(ENUM, NAME, MASK, ATTR, ICODE)
9731 #define RS6000_BUILTIN_E(ENUM, NAME, MASK, ATTR, ICODE)
9732 #define RS6000_BUILTIN_P(ENUM, NAME, MASK, ATTR, ICODE)
9733 #define RS6000_BUILTIN_Q(ENUM, NAME, MASK, ATTR, ICODE)
9734 #define RS6000_BUILTIN_S(ENUM, NAME, MASK, ATTR, ICODE)
9735 #define RS6000_BUILTIN_X(ENUM, NAME, MASK, ATTR, ICODE)
9737 static const struct builtin_description bdesc_1arg[] =
9739 #include "rs6000-builtin.def"
9742 #undef RS6000_BUILTIN_1
9743 #undef RS6000_BUILTIN_2
9744 #undef RS6000_BUILTIN_3
9745 #undef RS6000_BUILTIN_A
9746 #undef RS6000_BUILTIN_D
9747 #undef RS6000_BUILTIN_E
9748 #undef RS6000_BUILTIN_P
9749 #undef RS6000_BUILTIN_Q
9750 #undef RS6000_BUILTIN_S
9751 #undef RS6000_BUILTIN_X
9753 /* Return true if a builtin function is overloaded. */
9754 bool
9755 rs6000_overloaded_builtin_p (enum rs6000_builtins fncode)
9757 return (rs6000_builtin_info[(int)fncode].attr & RS6000_BTC_OVERLOADED) != 0;
9760 /* Expand an expression EXP that calls a builtin without arguments. */
9761 static rtx
9762 rs6000_expand_zeroop_builtin (enum insn_code icode, rtx target)
9764 rtx pat;
9765 enum machine_mode tmode = insn_data[icode].operand[0].mode;
9767 if (icode == CODE_FOR_nothing)
9768 /* Builtin not supported on this processor. */
9769 return 0;
9771 if (target == 0
9772 || GET_MODE (target) != tmode
9773 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
9774 target = gen_reg_rtx (tmode);
9776 pat = GEN_FCN (icode) (target);
9777 if (! pat)
9778 return 0;
9779 emit_insn (pat);
9781 return target;
9785 static rtx
9786 rs6000_expand_unop_builtin (enum insn_code icode, tree exp, rtx target)
9788 rtx pat;
9789 tree arg0 = CALL_EXPR_ARG (exp, 0);
9790 rtx op0 = expand_normal (arg0);
9791 enum machine_mode tmode = insn_data[icode].operand[0].mode;
9792 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
9794 if (icode == CODE_FOR_nothing)
9795 /* Builtin not supported on this processor. */
9796 return 0;
9798 /* If we got invalid arguments bail out before generating bad rtl. */
9799 if (arg0 == error_mark_node)
9800 return const0_rtx;
9802 if (icode == CODE_FOR_altivec_vspltisb
9803 || icode == CODE_FOR_altivec_vspltish
9804 || icode == CODE_FOR_altivec_vspltisw
9805 || icode == CODE_FOR_spe_evsplatfi
9806 || icode == CODE_FOR_spe_evsplati)
9808 /* Only allow 5-bit *signed* literals. */
9809 if (GET_CODE (op0) != CONST_INT
9810 || INTVAL (op0) > 15
9811 || INTVAL (op0) < -16)
9813 error ("argument 1 must be a 5-bit signed literal");
9814 return const0_rtx;
9818 if (target == 0
9819 || GET_MODE (target) != tmode
9820 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
9821 target = gen_reg_rtx (tmode);
9823 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
9824 op0 = copy_to_mode_reg (mode0, op0);
9826 pat = GEN_FCN (icode) (target, op0);
9827 if (! pat)
9828 return 0;
9829 emit_insn (pat);
9831 return target;
9834 static rtx
9835 altivec_expand_abs_builtin (enum insn_code icode, tree exp, rtx target)
9837 rtx pat, scratch1, scratch2;
9838 tree arg0 = CALL_EXPR_ARG (exp, 0);
9839 rtx op0 = expand_normal (arg0);
9840 enum machine_mode tmode = insn_data[icode].operand[0].mode;
9841 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
9843 /* If we have invalid arguments, bail out before generating bad rtl. */
9844 if (arg0 == error_mark_node)
9845 return const0_rtx;
9847 if (target == 0
9848 || GET_MODE (target) != tmode
9849 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
9850 target = gen_reg_rtx (tmode);
9852 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
9853 op0 = copy_to_mode_reg (mode0, op0);
9855 scratch1 = gen_reg_rtx (mode0);
9856 scratch2 = gen_reg_rtx (mode0);
9858 pat = GEN_FCN (icode) (target, op0, scratch1, scratch2);
9859 if (! pat)
9860 return 0;
9861 emit_insn (pat);
9863 return target;
9866 static rtx
9867 rs6000_expand_binop_builtin (enum insn_code icode, tree exp, rtx target)
9869 rtx pat;
9870 tree arg0 = CALL_EXPR_ARG (exp, 0);
9871 tree arg1 = CALL_EXPR_ARG (exp, 1);
9872 rtx op0 = expand_normal (arg0);
9873 rtx op1 = expand_normal (arg1);
9874 enum machine_mode tmode = insn_data[icode].operand[0].mode;
9875 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
9876 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
9878 if (icode == CODE_FOR_nothing)
9879 /* Builtin not supported on this processor. */
9880 return 0;
9882 /* If we got invalid arguments bail out before generating bad rtl. */
9883 if (arg0 == error_mark_node || arg1 == error_mark_node)
9884 return const0_rtx;
9886 if (icode == CODE_FOR_altivec_vcfux
9887 || icode == CODE_FOR_altivec_vcfsx
9888 || icode == CODE_FOR_altivec_vctsxs
9889 || icode == CODE_FOR_altivec_vctuxs
9890 || icode == CODE_FOR_altivec_vspltb
9891 || icode == CODE_FOR_altivec_vsplth
9892 || icode == CODE_FOR_altivec_vspltw
9893 || icode == CODE_FOR_spe_evaddiw
9894 || icode == CODE_FOR_spe_evldd
9895 || icode == CODE_FOR_spe_evldh
9896 || icode == CODE_FOR_spe_evldw
9897 || icode == CODE_FOR_spe_evlhhesplat
9898 || icode == CODE_FOR_spe_evlhhossplat
9899 || icode == CODE_FOR_spe_evlhhousplat
9900 || icode == CODE_FOR_spe_evlwhe
9901 || icode == CODE_FOR_spe_evlwhos
9902 || icode == CODE_FOR_spe_evlwhou
9903 || icode == CODE_FOR_spe_evlwhsplat
9904 || icode == CODE_FOR_spe_evlwwsplat
9905 || icode == CODE_FOR_spe_evrlwi
9906 || icode == CODE_FOR_spe_evslwi
9907 || icode == CODE_FOR_spe_evsrwis
9908 || icode == CODE_FOR_spe_evsubifw
9909 || icode == CODE_FOR_spe_evsrwiu)
9911 /* Only allow 5-bit unsigned literals. */
9912 STRIP_NOPS (arg1);
9913 if (TREE_CODE (arg1) != INTEGER_CST
9914 || TREE_INT_CST_LOW (arg1) & ~0x1f)
9916 error ("argument 2 must be a 5-bit unsigned literal");
9917 return const0_rtx;
9921 if (target == 0
9922 || GET_MODE (target) != tmode
9923 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
9924 target = gen_reg_rtx (tmode);
9926 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
9927 op0 = copy_to_mode_reg (mode0, op0);
9928 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
9929 op1 = copy_to_mode_reg (mode1, op1);
9931 pat = GEN_FCN (icode) (target, op0, op1);
9932 if (! pat)
9933 return 0;
9934 emit_insn (pat);
9936 return target;
9939 static rtx
9940 altivec_expand_predicate_builtin (enum insn_code icode, tree exp, rtx target)
9942 rtx pat, scratch;
9943 tree cr6_form = CALL_EXPR_ARG (exp, 0);
9944 tree arg0 = CALL_EXPR_ARG (exp, 1);
9945 tree arg1 = CALL_EXPR_ARG (exp, 2);
9946 rtx op0 = expand_normal (arg0);
9947 rtx op1 = expand_normal (arg1);
9948 enum machine_mode tmode = SImode;
9949 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
9950 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
9951 int cr6_form_int;
9953 if (TREE_CODE (cr6_form) != INTEGER_CST)
9955 error ("argument 1 of __builtin_altivec_predicate must be a constant");
9956 return const0_rtx;
9958 else
9959 cr6_form_int = TREE_INT_CST_LOW (cr6_form);
9961 gcc_assert (mode0 == mode1);
9963 /* If we have invalid arguments, bail out before generating bad rtl. */
9964 if (arg0 == error_mark_node || arg1 == error_mark_node)
9965 return const0_rtx;
9967 if (target == 0
9968 || GET_MODE (target) != tmode
9969 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
9970 target = gen_reg_rtx (tmode);
9972 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
9973 op0 = copy_to_mode_reg (mode0, op0);
9974 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
9975 op1 = copy_to_mode_reg (mode1, op1);
9977 scratch = gen_reg_rtx (mode0);
9979 pat = GEN_FCN (icode) (scratch, op0, op1);
9980 if (! pat)
9981 return 0;
9982 emit_insn (pat);
9984 /* The vec_any* and vec_all* predicates use the same opcodes for two
9985 different operations, but the bits in CR6 will be different
9986 depending on what information we want. So we have to play tricks
9987 with CR6 to get the right bits out.
9989 If you think this is disgusting, look at the specs for the
9990 AltiVec predicates. */
9992 switch (cr6_form_int)
9994 case 0:
9995 emit_insn (gen_cr6_test_for_zero (target));
9996 break;
9997 case 1:
9998 emit_insn (gen_cr6_test_for_zero_reverse (target));
9999 break;
10000 case 2:
10001 emit_insn (gen_cr6_test_for_lt (target));
10002 break;
10003 case 3:
10004 emit_insn (gen_cr6_test_for_lt_reverse (target));
10005 break;
10006 default:
10007 error ("argument 1 of __builtin_altivec_predicate is out of range");
10008 break;
10011 return target;
10014 static rtx
10015 paired_expand_lv_builtin (enum insn_code icode, tree exp, rtx target)
10017 rtx pat, addr;
10018 tree arg0 = CALL_EXPR_ARG (exp, 0);
10019 tree arg1 = CALL_EXPR_ARG (exp, 1);
10020 enum machine_mode tmode = insn_data[icode].operand[0].mode;
10021 enum machine_mode mode0 = Pmode;
10022 enum machine_mode mode1 = Pmode;
10023 rtx op0 = expand_normal (arg0);
10024 rtx op1 = expand_normal (arg1);
10026 if (icode == CODE_FOR_nothing)
10027 /* Builtin not supported on this processor. */
10028 return 0;
10030 /* If we got invalid arguments bail out before generating bad rtl. */
10031 if (arg0 == error_mark_node || arg1 == error_mark_node)
10032 return const0_rtx;
10034 if (target == 0
10035 || GET_MODE (target) != tmode
10036 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
10037 target = gen_reg_rtx (tmode);
10039 op1 = copy_to_mode_reg (mode1, op1);
10041 if (op0 == const0_rtx)
10043 addr = gen_rtx_MEM (tmode, op1);
10045 else
10047 op0 = copy_to_mode_reg (mode0, op0);
10048 addr = gen_rtx_MEM (tmode, gen_rtx_PLUS (Pmode, op0, op1));
10051 pat = GEN_FCN (icode) (target, addr);
10053 if (! pat)
10054 return 0;
10055 emit_insn (pat);
10057 return target;
10060 static rtx
10061 altivec_expand_lv_builtin (enum insn_code icode, tree exp, rtx target, bool blk)
10063 rtx pat, addr;
10064 tree arg0 = CALL_EXPR_ARG (exp, 0);
10065 tree arg1 = CALL_EXPR_ARG (exp, 1);
10066 enum machine_mode tmode = insn_data[icode].operand[0].mode;
10067 enum machine_mode mode0 = Pmode;
10068 enum machine_mode mode1 = Pmode;
10069 rtx op0 = expand_normal (arg0);
10070 rtx op1 = expand_normal (arg1);
10072 if (icode == CODE_FOR_nothing)
10073 /* Builtin not supported on this processor. */
10074 return 0;
10076 /* If we got invalid arguments bail out before generating bad rtl. */
10077 if (arg0 == error_mark_node || arg1 == error_mark_node)
10078 return const0_rtx;
10080 if (target == 0
10081 || GET_MODE (target) != tmode
10082 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
10083 target = gen_reg_rtx (tmode);
10085 op1 = copy_to_mode_reg (mode1, op1);
10087 if (op0 == const0_rtx)
10089 addr = gen_rtx_MEM (blk ? BLKmode : tmode, op1);
10091 else
10093 op0 = copy_to_mode_reg (mode0, op0);
10094 addr = gen_rtx_MEM (blk ? BLKmode : tmode, gen_rtx_PLUS (Pmode, op0, op1));
10097 pat = GEN_FCN (icode) (target, addr);
10099 if (! pat)
10100 return 0;
10101 emit_insn (pat);
10103 return target;
10106 static rtx
10107 spe_expand_stv_builtin (enum insn_code icode, tree exp)
10109 tree arg0 = CALL_EXPR_ARG (exp, 0);
10110 tree arg1 = CALL_EXPR_ARG (exp, 1);
10111 tree arg2 = CALL_EXPR_ARG (exp, 2);
10112 rtx op0 = expand_normal (arg0);
10113 rtx op1 = expand_normal (arg1);
10114 rtx op2 = expand_normal (arg2);
10115 rtx pat;
10116 enum machine_mode mode0 = insn_data[icode].operand[0].mode;
10117 enum machine_mode mode1 = insn_data[icode].operand[1].mode;
10118 enum machine_mode mode2 = insn_data[icode].operand[2].mode;
10120 /* Invalid arguments. Bail before doing anything stoopid! */
10121 if (arg0 == error_mark_node
10122 || arg1 == error_mark_node
10123 || arg2 == error_mark_node)
10124 return const0_rtx;
10126 if (! (*insn_data[icode].operand[2].predicate) (op0, mode2))
10127 op0 = copy_to_mode_reg (mode2, op0);
10128 if (! (*insn_data[icode].operand[0].predicate) (op1, mode0))
10129 op1 = copy_to_mode_reg (mode0, op1);
10130 if (! (*insn_data[icode].operand[1].predicate) (op2, mode1))
10131 op2 = copy_to_mode_reg (mode1, op2);
10133 pat = GEN_FCN (icode) (op1, op2, op0);
10134 if (pat)
10135 emit_insn (pat);
10136 return NULL_RTX;
10139 static rtx
10140 paired_expand_stv_builtin (enum insn_code icode, tree exp)
10142 tree arg0 = CALL_EXPR_ARG (exp, 0);
10143 tree arg1 = CALL_EXPR_ARG (exp, 1);
10144 tree arg2 = CALL_EXPR_ARG (exp, 2);
10145 rtx op0 = expand_normal (arg0);
10146 rtx op1 = expand_normal (arg1);
10147 rtx op2 = expand_normal (arg2);
10148 rtx pat, addr;
10149 enum machine_mode tmode = insn_data[icode].operand[0].mode;
10150 enum machine_mode mode1 = Pmode;
10151 enum machine_mode mode2 = Pmode;
10153 /* Invalid arguments. Bail before doing anything stoopid! */
10154 if (arg0 == error_mark_node
10155 || arg1 == error_mark_node
10156 || arg2 == error_mark_node)
10157 return const0_rtx;
10159 if (! (*insn_data[icode].operand[1].predicate) (op0, tmode))
10160 op0 = copy_to_mode_reg (tmode, op0);
10162 op2 = copy_to_mode_reg (mode2, op2);
10164 if (op1 == const0_rtx)
10166 addr = gen_rtx_MEM (tmode, op2);
10168 else
10170 op1 = copy_to_mode_reg (mode1, op1);
10171 addr = gen_rtx_MEM (tmode, gen_rtx_PLUS (Pmode, op1, op2));
10174 pat = GEN_FCN (icode) (addr, op0);
10175 if (pat)
10176 emit_insn (pat);
10177 return NULL_RTX;
10180 static rtx
10181 altivec_expand_stv_builtin (enum insn_code icode, tree exp)
10183 tree arg0 = CALL_EXPR_ARG (exp, 0);
10184 tree arg1 = CALL_EXPR_ARG (exp, 1);
10185 tree arg2 = CALL_EXPR_ARG (exp, 2);
10186 rtx op0 = expand_normal (arg0);
10187 rtx op1 = expand_normal (arg1);
10188 rtx op2 = expand_normal (arg2);
10189 rtx pat, addr;
10190 enum machine_mode tmode = insn_data[icode].operand[0].mode;
10191 enum machine_mode smode = insn_data[icode].operand[1].mode;
10192 enum machine_mode mode1 = Pmode;
10193 enum machine_mode mode2 = Pmode;
10195 /* Invalid arguments. Bail before doing anything stoopid! */
10196 if (arg0 == error_mark_node
10197 || arg1 == error_mark_node
10198 || arg2 == error_mark_node)
10199 return const0_rtx;
10201 if (! (*insn_data[icode].operand[1].predicate) (op0, smode))
10202 op0 = copy_to_mode_reg (smode, op0);
10204 op2 = copy_to_mode_reg (mode2, op2);
10206 if (op1 == const0_rtx)
10208 addr = gen_rtx_MEM (tmode, op2);
10210 else
10212 op1 = copy_to_mode_reg (mode1, op1);
10213 addr = gen_rtx_MEM (tmode, gen_rtx_PLUS (Pmode, op1, op2));
10216 pat = GEN_FCN (icode) (addr, op0);
10217 if (pat)
10218 emit_insn (pat);
10219 return NULL_RTX;
10222 static rtx
10223 rs6000_expand_ternop_builtin (enum insn_code icode, tree exp, rtx target)
10225 rtx pat;
10226 tree arg0 = CALL_EXPR_ARG (exp, 0);
10227 tree arg1 = CALL_EXPR_ARG (exp, 1);
10228 tree arg2 = CALL_EXPR_ARG (exp, 2);
10229 rtx op0 = expand_normal (arg0);
10230 rtx op1 = expand_normal (arg1);
10231 rtx op2 = expand_normal (arg2);
10232 enum machine_mode tmode = insn_data[icode].operand[0].mode;
10233 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
10234 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
10235 enum machine_mode mode2 = insn_data[icode].operand[3].mode;
10237 if (icode == CODE_FOR_nothing)
10238 /* Builtin not supported on this processor. */
10239 return 0;
10241 /* If we got invalid arguments bail out before generating bad rtl. */
10242 if (arg0 == error_mark_node
10243 || arg1 == error_mark_node
10244 || arg2 == error_mark_node)
10245 return const0_rtx;
10247 /* Check and prepare argument depending on the instruction code.
10249 Note that a switch statement instead of the sequence of tests
10250 would be incorrect as many of the CODE_FOR values could be
10251 CODE_FOR_nothing and that would yield multiple alternatives
10252 with identical values. We'd never reach here at runtime in
10253 this case. */
10254 if (icode == CODE_FOR_altivec_vsldoi_v4sf
10255 || icode == CODE_FOR_altivec_vsldoi_v4si
10256 || icode == CODE_FOR_altivec_vsldoi_v8hi
10257 || icode == CODE_FOR_altivec_vsldoi_v16qi)
10259 /* Only allow 4-bit unsigned literals. */
10260 STRIP_NOPS (arg2);
10261 if (TREE_CODE (arg2) != INTEGER_CST
10262 || TREE_INT_CST_LOW (arg2) & ~0xf)
10264 error ("argument 3 must be a 4-bit unsigned literal");
10265 return const0_rtx;
10268 else if (icode == CODE_FOR_vsx_xxpermdi_v2df
10269 || icode == CODE_FOR_vsx_xxpermdi_v2di
10270 || icode == CODE_FOR_vsx_xxsldwi_v16qi
10271 || icode == CODE_FOR_vsx_xxsldwi_v8hi
10272 || icode == CODE_FOR_vsx_xxsldwi_v4si
10273 || icode == CODE_FOR_vsx_xxsldwi_v4sf
10274 || icode == CODE_FOR_vsx_xxsldwi_v2di
10275 || icode == CODE_FOR_vsx_xxsldwi_v2df)
10277 /* Only allow 2-bit unsigned literals. */
10278 STRIP_NOPS (arg2);
10279 if (TREE_CODE (arg2) != INTEGER_CST
10280 || TREE_INT_CST_LOW (arg2) & ~0x3)
10282 error ("argument 3 must be a 2-bit unsigned literal");
10283 return const0_rtx;
10286 else if (icode == CODE_FOR_vsx_set_v2df
10287 || icode == CODE_FOR_vsx_set_v2di)
10289 /* Only allow 1-bit unsigned literals. */
10290 STRIP_NOPS (arg2);
10291 if (TREE_CODE (arg2) != INTEGER_CST
10292 || TREE_INT_CST_LOW (arg2) & ~0x1)
10294 error ("argument 3 must be a 1-bit unsigned literal");
10295 return const0_rtx;
10299 if (target == 0
10300 || GET_MODE (target) != tmode
10301 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
10302 target = gen_reg_rtx (tmode);
10304 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
10305 op0 = copy_to_mode_reg (mode0, op0);
10306 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
10307 op1 = copy_to_mode_reg (mode1, op1);
10308 if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
10309 op2 = copy_to_mode_reg (mode2, op2);
10311 if (TARGET_PAIRED_FLOAT && icode == CODE_FOR_selv2sf4)
10312 pat = GEN_FCN (icode) (target, op0, op1, op2, CONST0_RTX (SFmode));
10313 else
10314 pat = GEN_FCN (icode) (target, op0, op1, op2);
10315 if (! pat)
10316 return 0;
10317 emit_insn (pat);
10319 return target;
10322 /* Expand the lvx builtins. */
10323 static rtx
10324 altivec_expand_ld_builtin (tree exp, rtx target, bool *expandedp)
10326 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
10327 unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
10328 tree arg0;
10329 enum machine_mode tmode, mode0;
10330 rtx pat, op0;
10331 enum insn_code icode;
10333 switch (fcode)
10335 case ALTIVEC_BUILTIN_LD_INTERNAL_16qi:
10336 icode = CODE_FOR_vector_altivec_load_v16qi;
10337 break;
10338 case ALTIVEC_BUILTIN_LD_INTERNAL_8hi:
10339 icode = CODE_FOR_vector_altivec_load_v8hi;
10340 break;
10341 case ALTIVEC_BUILTIN_LD_INTERNAL_4si:
10342 icode = CODE_FOR_vector_altivec_load_v4si;
10343 break;
10344 case ALTIVEC_BUILTIN_LD_INTERNAL_4sf:
10345 icode = CODE_FOR_vector_altivec_load_v4sf;
10346 break;
10347 case ALTIVEC_BUILTIN_LD_INTERNAL_2df:
10348 icode = CODE_FOR_vector_altivec_load_v2df;
10349 break;
10350 case ALTIVEC_BUILTIN_LD_INTERNAL_2di:
10351 icode = CODE_FOR_vector_altivec_load_v2di;
10352 break;
10353 default:
10354 *expandedp = false;
10355 return NULL_RTX;
10358 *expandedp = true;
10360 arg0 = CALL_EXPR_ARG (exp, 0);
10361 op0 = expand_normal (arg0);
10362 tmode = insn_data[icode].operand[0].mode;
10363 mode0 = insn_data[icode].operand[1].mode;
10365 if (target == 0
10366 || GET_MODE (target) != tmode
10367 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
10368 target = gen_reg_rtx (tmode);
10370 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
10371 op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
10373 pat = GEN_FCN (icode) (target, op0);
10374 if (! pat)
10375 return 0;
10376 emit_insn (pat);
10377 return target;
10380 /* Expand the stvx builtins. */
10381 static rtx
10382 altivec_expand_st_builtin (tree exp, rtx target ATTRIBUTE_UNUSED,
10383 bool *expandedp)
10385 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
10386 unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
10387 tree arg0, arg1;
10388 enum machine_mode mode0, mode1;
10389 rtx pat, op0, op1;
10390 enum insn_code icode;
10392 switch (fcode)
10394 case ALTIVEC_BUILTIN_ST_INTERNAL_16qi:
10395 icode = CODE_FOR_vector_altivec_store_v16qi;
10396 break;
10397 case ALTIVEC_BUILTIN_ST_INTERNAL_8hi:
10398 icode = CODE_FOR_vector_altivec_store_v8hi;
10399 break;
10400 case ALTIVEC_BUILTIN_ST_INTERNAL_4si:
10401 icode = CODE_FOR_vector_altivec_store_v4si;
10402 break;
10403 case ALTIVEC_BUILTIN_ST_INTERNAL_4sf:
10404 icode = CODE_FOR_vector_altivec_store_v4sf;
10405 break;
10406 case ALTIVEC_BUILTIN_ST_INTERNAL_2df:
10407 icode = CODE_FOR_vector_altivec_store_v2df;
10408 break;
10409 case ALTIVEC_BUILTIN_ST_INTERNAL_2di:
10410 icode = CODE_FOR_vector_altivec_store_v2di;
10411 break;
10412 default:
10413 *expandedp = false;
10414 return NULL_RTX;
10417 arg0 = CALL_EXPR_ARG (exp, 0);
10418 arg1 = CALL_EXPR_ARG (exp, 1);
10419 op0 = expand_normal (arg0);
10420 op1 = expand_normal (arg1);
10421 mode0 = insn_data[icode].operand[0].mode;
10422 mode1 = insn_data[icode].operand[1].mode;
10424 if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
10425 op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
10426 if (! (*insn_data[icode].operand[1].predicate) (op1, mode1))
10427 op1 = copy_to_mode_reg (mode1, op1);
10429 pat = GEN_FCN (icode) (op0, op1);
10430 if (pat)
10431 emit_insn (pat);
10433 *expandedp = true;
10434 return NULL_RTX;
10437 /* Expand the dst builtins. */
10438 static rtx
10439 altivec_expand_dst_builtin (tree exp, rtx target ATTRIBUTE_UNUSED,
10440 bool *expandedp)
10442 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
10443 enum rs6000_builtins fcode = (enum rs6000_builtins) DECL_FUNCTION_CODE (fndecl);
10444 tree arg0, arg1, arg2;
10445 enum machine_mode mode0, mode1;
10446 rtx pat, op0, op1, op2;
10447 const struct builtin_description *d;
10448 size_t i;
10450 *expandedp = false;
10452 /* Handle DST variants. */
10453 d = bdesc_dst;
10454 for (i = 0; i < ARRAY_SIZE (bdesc_dst); i++, d++)
10455 if (d->code == fcode)
10457 arg0 = CALL_EXPR_ARG (exp, 0);
10458 arg1 = CALL_EXPR_ARG (exp, 1);
10459 arg2 = CALL_EXPR_ARG (exp, 2);
10460 op0 = expand_normal (arg0);
10461 op1 = expand_normal (arg1);
10462 op2 = expand_normal (arg2);
10463 mode0 = insn_data[d->icode].operand[0].mode;
10464 mode1 = insn_data[d->icode].operand[1].mode;
10466 /* Invalid arguments, bail out before generating bad rtl. */
10467 if (arg0 == error_mark_node
10468 || arg1 == error_mark_node
10469 || arg2 == error_mark_node)
10470 return const0_rtx;
10472 *expandedp = true;
10473 STRIP_NOPS (arg2);
10474 if (TREE_CODE (arg2) != INTEGER_CST
10475 || TREE_INT_CST_LOW (arg2) & ~0x3)
10477 error ("argument to %qs must be a 2-bit unsigned literal", d->name);
10478 return const0_rtx;
10481 if (! (*insn_data[d->icode].operand[0].predicate) (op0, mode0))
10482 op0 = copy_to_mode_reg (Pmode, op0);
10483 if (! (*insn_data[d->icode].operand[1].predicate) (op1, mode1))
10484 op1 = copy_to_mode_reg (mode1, op1);
10486 pat = GEN_FCN (d->icode) (op0, op1, op2);
10487 if (pat != 0)
10488 emit_insn (pat);
10490 return NULL_RTX;
10493 return NULL_RTX;
10496 /* Expand vec_init builtin. */
10497 static rtx
10498 altivec_expand_vec_init_builtin (tree type, tree exp, rtx target)
10500 enum machine_mode tmode = TYPE_MODE (type);
10501 enum machine_mode inner_mode = GET_MODE_INNER (tmode);
10502 int i, n_elt = GET_MODE_NUNITS (tmode);
10503 rtvec v = rtvec_alloc (n_elt);
10505 gcc_assert (VECTOR_MODE_P (tmode));
10506 gcc_assert (n_elt == call_expr_nargs (exp));
10508 for (i = 0; i < n_elt; ++i)
10510 rtx x = expand_normal (CALL_EXPR_ARG (exp, i));
10511 RTVEC_ELT (v, i) = gen_lowpart (inner_mode, x);
10514 if (!target || !register_operand (target, tmode))
10515 target = gen_reg_rtx (tmode);
10517 rs6000_expand_vector_init (target, gen_rtx_PARALLEL (tmode, v));
10518 return target;
10521 /* Return the integer constant in ARG. Constrain it to be in the range
10522 of the subparts of VEC_TYPE; issue an error if not. */
10524 static int
10525 get_element_number (tree vec_type, tree arg)
10527 unsigned HOST_WIDE_INT elt, max = TYPE_VECTOR_SUBPARTS (vec_type) - 1;
10529 if (!host_integerp (arg, 1)
10530 || (elt = tree_low_cst (arg, 1), elt > max))
10532 error ("selector must be an integer constant in the range 0..%wi", max);
10533 return 0;
10536 return elt;
10539 /* Expand vec_set builtin. */
10540 static rtx
10541 altivec_expand_vec_set_builtin (tree exp)
10543 enum machine_mode tmode, mode1;
10544 tree arg0, arg1, arg2;
10545 int elt;
10546 rtx op0, op1;
10548 arg0 = CALL_EXPR_ARG (exp, 0);
10549 arg1 = CALL_EXPR_ARG (exp, 1);
10550 arg2 = CALL_EXPR_ARG (exp, 2);
10552 tmode = TYPE_MODE (TREE_TYPE (arg0));
10553 mode1 = TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0)));
10554 gcc_assert (VECTOR_MODE_P (tmode));
10556 op0 = expand_expr (arg0, NULL_RTX, tmode, EXPAND_NORMAL);
10557 op1 = expand_expr (arg1, NULL_RTX, mode1, EXPAND_NORMAL);
10558 elt = get_element_number (TREE_TYPE (arg0), arg2);
10560 if (GET_MODE (op1) != mode1 && GET_MODE (op1) != VOIDmode)
10561 op1 = convert_modes (mode1, GET_MODE (op1), op1, true);
10563 op0 = force_reg (tmode, op0);
10564 op1 = force_reg (mode1, op1);
10566 rs6000_expand_vector_set (op0, op1, elt);
10568 return op0;
10571 /* Expand vec_ext builtin. */
10572 static rtx
10573 altivec_expand_vec_ext_builtin (tree exp, rtx target)
10575 enum machine_mode tmode, mode0;
10576 tree arg0, arg1;
10577 int elt;
10578 rtx op0;
10580 arg0 = CALL_EXPR_ARG (exp, 0);
10581 arg1 = CALL_EXPR_ARG (exp, 1);
10583 op0 = expand_normal (arg0);
10584 elt = get_element_number (TREE_TYPE (arg0), arg1);
10586 tmode = TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0)));
10587 mode0 = TYPE_MODE (TREE_TYPE (arg0));
10588 gcc_assert (VECTOR_MODE_P (mode0));
10590 op0 = force_reg (mode0, op0);
10592 if (optimize || !target || !register_operand (target, tmode))
10593 target = gen_reg_rtx (tmode);
10595 rs6000_expand_vector_extract (target, op0, elt);
10597 return target;
10600 /* Expand the builtin in EXP and store the result in TARGET. Store
10601 true in *EXPANDEDP if we found a builtin to expand. */
10602 static rtx
10603 altivec_expand_builtin (tree exp, rtx target, bool *expandedp)
10605 const struct builtin_description *d;
10606 size_t i;
10607 enum insn_code icode;
10608 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
10609 tree arg0;
10610 rtx op0, pat;
10611 enum machine_mode tmode, mode0;
10612 enum rs6000_builtins fcode
10613 = (enum rs6000_builtins) DECL_FUNCTION_CODE (fndecl);
10615 if (rs6000_overloaded_builtin_p (fcode))
10617 *expandedp = true;
10618 error ("unresolved overload for Altivec builtin %qF", fndecl);
10620 /* Given it is invalid, just generate a normal call. */
10621 return expand_call (exp, target, false);
10624 target = altivec_expand_ld_builtin (exp, target, expandedp);
10625 if (*expandedp)
10626 return target;
10628 target = altivec_expand_st_builtin (exp, target, expandedp);
10629 if (*expandedp)
10630 return target;
10632 target = altivec_expand_dst_builtin (exp, target, expandedp);
10633 if (*expandedp)
10634 return target;
10636 *expandedp = true;
10638 switch (fcode)
10640 case ALTIVEC_BUILTIN_STVX:
10641 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvx_v4si, exp);
10642 case ALTIVEC_BUILTIN_STVEBX:
10643 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvebx, exp);
10644 case ALTIVEC_BUILTIN_STVEHX:
10645 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvehx, exp);
10646 case ALTIVEC_BUILTIN_STVEWX:
10647 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvewx, exp);
10648 case ALTIVEC_BUILTIN_STVXL:
10649 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvxl, exp);
10651 case ALTIVEC_BUILTIN_STVLX:
10652 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvlx, exp);
10653 case ALTIVEC_BUILTIN_STVLXL:
10654 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvlxl, exp);
10655 case ALTIVEC_BUILTIN_STVRX:
10656 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvrx, exp);
10657 case ALTIVEC_BUILTIN_STVRXL:
10658 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvrxl, exp);
10660 case VSX_BUILTIN_STXVD2X_V2DF:
10661 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v2df, exp);
10662 case VSX_BUILTIN_STXVD2X_V2DI:
10663 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v2di, exp);
10664 case VSX_BUILTIN_STXVW4X_V4SF:
10665 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v4sf, exp);
10666 case VSX_BUILTIN_STXVW4X_V4SI:
10667 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v4si, exp);
10668 case VSX_BUILTIN_STXVW4X_V8HI:
10669 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v8hi, exp);
10670 case VSX_BUILTIN_STXVW4X_V16QI:
10671 return altivec_expand_stv_builtin (CODE_FOR_vsx_store_v16qi, exp);
10673 case ALTIVEC_BUILTIN_MFVSCR:
10674 icode = CODE_FOR_altivec_mfvscr;
10675 tmode = insn_data[icode].operand[0].mode;
10677 if (target == 0
10678 || GET_MODE (target) != tmode
10679 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
10680 target = gen_reg_rtx (tmode);
10682 pat = GEN_FCN (icode) (target);
10683 if (! pat)
10684 return 0;
10685 emit_insn (pat);
10686 return target;
10688 case ALTIVEC_BUILTIN_MTVSCR:
10689 icode = CODE_FOR_altivec_mtvscr;
10690 arg0 = CALL_EXPR_ARG (exp, 0);
10691 op0 = expand_normal (arg0);
10692 mode0 = insn_data[icode].operand[0].mode;
10694 /* If we got invalid arguments bail out before generating bad rtl. */
10695 if (arg0 == error_mark_node)
10696 return const0_rtx;
10698 if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
10699 op0 = copy_to_mode_reg (mode0, op0);
10701 pat = GEN_FCN (icode) (op0);
10702 if (pat)
10703 emit_insn (pat);
10704 return NULL_RTX;
10706 case ALTIVEC_BUILTIN_DSSALL:
10707 emit_insn (gen_altivec_dssall ());
10708 return NULL_RTX;
10710 case ALTIVEC_BUILTIN_DSS:
10711 icode = CODE_FOR_altivec_dss;
10712 arg0 = CALL_EXPR_ARG (exp, 0);
10713 STRIP_NOPS (arg0);
10714 op0 = expand_normal (arg0);
10715 mode0 = insn_data[icode].operand[0].mode;
10717 /* If we got invalid arguments bail out before generating bad rtl. */
10718 if (arg0 == error_mark_node)
10719 return const0_rtx;
10721 if (TREE_CODE (arg0) != INTEGER_CST
10722 || TREE_INT_CST_LOW (arg0) & ~0x3)
10724 error ("argument to dss must be a 2-bit unsigned literal");
10725 return const0_rtx;
10728 if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
10729 op0 = copy_to_mode_reg (mode0, op0);
10731 emit_insn (gen_altivec_dss (op0));
10732 return NULL_RTX;
10734 case ALTIVEC_BUILTIN_VEC_INIT_V4SI:
10735 case ALTIVEC_BUILTIN_VEC_INIT_V8HI:
10736 case ALTIVEC_BUILTIN_VEC_INIT_V16QI:
10737 case ALTIVEC_BUILTIN_VEC_INIT_V4SF:
10738 case VSX_BUILTIN_VEC_INIT_V2DF:
10739 case VSX_BUILTIN_VEC_INIT_V2DI:
10740 return altivec_expand_vec_init_builtin (TREE_TYPE (exp), exp, target);
10742 case ALTIVEC_BUILTIN_VEC_SET_V4SI:
10743 case ALTIVEC_BUILTIN_VEC_SET_V8HI:
10744 case ALTIVEC_BUILTIN_VEC_SET_V16QI:
10745 case ALTIVEC_BUILTIN_VEC_SET_V4SF:
10746 case VSX_BUILTIN_VEC_SET_V2DF:
10747 case VSX_BUILTIN_VEC_SET_V2DI:
10748 return altivec_expand_vec_set_builtin (exp);
10750 case ALTIVEC_BUILTIN_VEC_EXT_V4SI:
10751 case ALTIVEC_BUILTIN_VEC_EXT_V8HI:
10752 case ALTIVEC_BUILTIN_VEC_EXT_V16QI:
10753 case ALTIVEC_BUILTIN_VEC_EXT_V4SF:
10754 case VSX_BUILTIN_VEC_EXT_V2DF:
10755 case VSX_BUILTIN_VEC_EXT_V2DI:
10756 return altivec_expand_vec_ext_builtin (exp, target);
10758 default:
10759 break;
10760 /* Fall through. */
10763 /* Expand abs* operations. */
10764 d = bdesc_abs;
10765 for (i = 0; i < ARRAY_SIZE (bdesc_abs); i++, d++)
10766 if (d->code == fcode)
10767 return altivec_expand_abs_builtin (d->icode, exp, target);
10769 /* Expand the AltiVec predicates. */
10770 d = bdesc_altivec_preds;
10771 for (i = 0; i < ARRAY_SIZE (bdesc_altivec_preds); i++, d++)
10772 if (d->code == fcode)
10773 return altivec_expand_predicate_builtin (d->icode, exp, target);
10775 /* LV* are funky. We initialized them differently. */
10776 switch (fcode)
10778 case ALTIVEC_BUILTIN_LVSL:
10779 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvsl,
10780 exp, target, false);
10781 case ALTIVEC_BUILTIN_LVSR:
10782 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvsr,
10783 exp, target, false);
10784 case ALTIVEC_BUILTIN_LVEBX:
10785 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvebx,
10786 exp, target, false);
10787 case ALTIVEC_BUILTIN_LVEHX:
10788 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvehx,
10789 exp, target, false);
10790 case ALTIVEC_BUILTIN_LVEWX:
10791 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvewx,
10792 exp, target, false);
10793 case ALTIVEC_BUILTIN_LVXL:
10794 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvxl,
10795 exp, target, false);
10796 case ALTIVEC_BUILTIN_LVX:
10797 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvx_v4si,
10798 exp, target, false);
10799 case ALTIVEC_BUILTIN_LVLX:
10800 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvlx,
10801 exp, target, true);
10802 case ALTIVEC_BUILTIN_LVLXL:
10803 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvlxl,
10804 exp, target, true);
10805 case ALTIVEC_BUILTIN_LVRX:
10806 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvrx,
10807 exp, target, true);
10808 case ALTIVEC_BUILTIN_LVRXL:
10809 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvrxl,
10810 exp, target, true);
10811 case VSX_BUILTIN_LXVD2X_V2DF:
10812 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v2df,
10813 exp, target, false);
10814 case VSX_BUILTIN_LXVD2X_V2DI:
10815 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v2di,
10816 exp, target, false);
10817 case VSX_BUILTIN_LXVW4X_V4SF:
10818 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v4sf,
10819 exp, target, false);
10820 case VSX_BUILTIN_LXVW4X_V4SI:
10821 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v4si,
10822 exp, target, false);
10823 case VSX_BUILTIN_LXVW4X_V8HI:
10824 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v8hi,
10825 exp, target, false);
10826 case VSX_BUILTIN_LXVW4X_V16QI:
10827 return altivec_expand_lv_builtin (CODE_FOR_vsx_load_v16qi,
10828 exp, target, false);
10829 break;
10830 default:
10831 break;
10832 /* Fall through. */
10835 *expandedp = false;
10836 return NULL_RTX;
10839 /* Expand the builtin in EXP and store the result in TARGET. Store
10840 true in *EXPANDEDP if we found a builtin to expand. */
10841 static rtx
10842 paired_expand_builtin (tree exp, rtx target, bool * expandedp)
10844 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
10845 enum rs6000_builtins fcode = (enum rs6000_builtins) DECL_FUNCTION_CODE (fndecl);
10846 const struct builtin_description *d;
10847 size_t i;
10849 *expandedp = true;
10851 switch (fcode)
10853 case PAIRED_BUILTIN_STX:
10854 return paired_expand_stv_builtin (CODE_FOR_paired_stx, exp);
10855 case PAIRED_BUILTIN_LX:
10856 return paired_expand_lv_builtin (CODE_FOR_paired_lx, exp, target);
10857 default:
10858 break;
10859 /* Fall through. */
10862 /* Expand the paired predicates. */
10863 d = bdesc_paired_preds;
10864 for (i = 0; i < ARRAY_SIZE (bdesc_paired_preds); i++, d++)
10865 if (d->code == fcode)
10866 return paired_expand_predicate_builtin (d->icode, exp, target);
10868 *expandedp = false;
10869 return NULL_RTX;
10872 /* Binops that need to be initialized manually, but can be expanded
10873 automagically by rs6000_expand_binop_builtin. */
10874 static const struct builtin_description bdesc_2arg_spe[] =
10876 { RS6000_BTM_SPE, CODE_FOR_spe_evlddx, "__builtin_spe_evlddx", SPE_BUILTIN_EVLDDX },
10877 { RS6000_BTM_SPE, CODE_FOR_spe_evldwx, "__builtin_spe_evldwx", SPE_BUILTIN_EVLDWX },
10878 { RS6000_BTM_SPE, CODE_FOR_spe_evldhx, "__builtin_spe_evldhx", SPE_BUILTIN_EVLDHX },
10879 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhex, "__builtin_spe_evlwhex", SPE_BUILTIN_EVLWHEX },
10880 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhoux, "__builtin_spe_evlwhoux", SPE_BUILTIN_EVLWHOUX },
10881 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhosx, "__builtin_spe_evlwhosx", SPE_BUILTIN_EVLWHOSX },
10882 { RS6000_BTM_SPE, CODE_FOR_spe_evlwwsplatx, "__builtin_spe_evlwwsplatx", SPE_BUILTIN_EVLWWSPLATX },
10883 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhsplatx, "__builtin_spe_evlwhsplatx", SPE_BUILTIN_EVLWHSPLATX },
10884 { RS6000_BTM_SPE, CODE_FOR_spe_evlhhesplatx, "__builtin_spe_evlhhesplatx", SPE_BUILTIN_EVLHHESPLATX },
10885 { RS6000_BTM_SPE, CODE_FOR_spe_evlhhousplatx, "__builtin_spe_evlhhousplatx", SPE_BUILTIN_EVLHHOUSPLATX },
10886 { RS6000_BTM_SPE, CODE_FOR_spe_evlhhossplatx, "__builtin_spe_evlhhossplatx", SPE_BUILTIN_EVLHHOSSPLATX },
10887 { RS6000_BTM_SPE, CODE_FOR_spe_evldd, "__builtin_spe_evldd", SPE_BUILTIN_EVLDD },
10888 { RS6000_BTM_SPE, CODE_FOR_spe_evldw, "__builtin_spe_evldw", SPE_BUILTIN_EVLDW },
10889 { RS6000_BTM_SPE, CODE_FOR_spe_evldh, "__builtin_spe_evldh", SPE_BUILTIN_EVLDH },
10890 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhe, "__builtin_spe_evlwhe", SPE_BUILTIN_EVLWHE },
10891 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhou, "__builtin_spe_evlwhou", SPE_BUILTIN_EVLWHOU },
10892 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhos, "__builtin_spe_evlwhos", SPE_BUILTIN_EVLWHOS },
10893 { RS6000_BTM_SPE, CODE_FOR_spe_evlwwsplat, "__builtin_spe_evlwwsplat", SPE_BUILTIN_EVLWWSPLAT },
10894 { RS6000_BTM_SPE, CODE_FOR_spe_evlwhsplat, "__builtin_spe_evlwhsplat", SPE_BUILTIN_EVLWHSPLAT },
10895 { RS6000_BTM_SPE, CODE_FOR_spe_evlhhesplat, "__builtin_spe_evlhhesplat", SPE_BUILTIN_EVLHHESPLAT },
10896 { RS6000_BTM_SPE, CODE_FOR_spe_evlhhousplat, "__builtin_spe_evlhhousplat", SPE_BUILTIN_EVLHHOUSPLAT },
10897 { RS6000_BTM_SPE, CODE_FOR_spe_evlhhossplat, "__builtin_spe_evlhhossplat", SPE_BUILTIN_EVLHHOSSPLAT }
10900 /* Expand the builtin in EXP and store the result in TARGET. Store
10901 true in *EXPANDEDP if we found a builtin to expand.
10903 This expands the SPE builtins that are not simple unary and binary
10904 operations. */
10905 static rtx
10906 spe_expand_builtin (tree exp, rtx target, bool *expandedp)
10908 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
10909 tree arg1, arg0;
10910 enum rs6000_builtins fcode = (enum rs6000_builtins) DECL_FUNCTION_CODE (fndecl);
10911 enum insn_code icode;
10912 enum machine_mode tmode, mode0;
10913 rtx pat, op0;
10914 const struct builtin_description *d;
10915 size_t i;
10917 *expandedp = true;
10919 /* Syntax check for a 5-bit unsigned immediate. */
10920 switch (fcode)
10922 case SPE_BUILTIN_EVSTDD:
10923 case SPE_BUILTIN_EVSTDH:
10924 case SPE_BUILTIN_EVSTDW:
10925 case SPE_BUILTIN_EVSTWHE:
10926 case SPE_BUILTIN_EVSTWHO:
10927 case SPE_BUILTIN_EVSTWWE:
10928 case SPE_BUILTIN_EVSTWWO:
10929 arg1 = CALL_EXPR_ARG (exp, 2);
10930 if (TREE_CODE (arg1) != INTEGER_CST
10931 || TREE_INT_CST_LOW (arg1) & ~0x1f)
10933 error ("argument 2 must be a 5-bit unsigned literal");
10934 return const0_rtx;
10936 break;
10937 default:
10938 break;
10941 /* The evsplat*i instructions are not quite generic. */
10942 switch (fcode)
10944 case SPE_BUILTIN_EVSPLATFI:
10945 return rs6000_expand_unop_builtin (CODE_FOR_spe_evsplatfi,
10946 exp, target);
10947 case SPE_BUILTIN_EVSPLATI:
10948 return rs6000_expand_unop_builtin (CODE_FOR_spe_evsplati,
10949 exp, target);
10950 default:
10951 break;
10954 d = bdesc_2arg_spe;
10955 for (i = 0; i < ARRAY_SIZE (bdesc_2arg_spe); ++i, ++d)
10956 if (d->code == fcode)
10957 return rs6000_expand_binop_builtin (d->icode, exp, target);
10959 d = bdesc_spe_predicates;
10960 for (i = 0; i < ARRAY_SIZE (bdesc_spe_predicates); ++i, ++d)
10961 if (d->code == fcode)
10962 return spe_expand_predicate_builtin (d->icode, exp, target);
10964 d = bdesc_spe_evsel;
10965 for (i = 0; i < ARRAY_SIZE (bdesc_spe_evsel); ++i, ++d)
10966 if (d->code == fcode)
10967 return spe_expand_evsel_builtin (d->icode, exp, target);
10969 switch (fcode)
10971 case SPE_BUILTIN_EVSTDDX:
10972 return spe_expand_stv_builtin (CODE_FOR_spe_evstddx, exp);
10973 case SPE_BUILTIN_EVSTDHX:
10974 return spe_expand_stv_builtin (CODE_FOR_spe_evstdhx, exp);
10975 case SPE_BUILTIN_EVSTDWX:
10976 return spe_expand_stv_builtin (CODE_FOR_spe_evstdwx, exp);
10977 case SPE_BUILTIN_EVSTWHEX:
10978 return spe_expand_stv_builtin (CODE_FOR_spe_evstwhex, exp);
10979 case SPE_BUILTIN_EVSTWHOX:
10980 return spe_expand_stv_builtin (CODE_FOR_spe_evstwhox, exp);
10981 case SPE_BUILTIN_EVSTWWEX:
10982 return spe_expand_stv_builtin (CODE_FOR_spe_evstwwex, exp);
10983 case SPE_BUILTIN_EVSTWWOX:
10984 return spe_expand_stv_builtin (CODE_FOR_spe_evstwwox, exp);
10985 case SPE_BUILTIN_EVSTDD:
10986 return spe_expand_stv_builtin (CODE_FOR_spe_evstdd, exp);
10987 case SPE_BUILTIN_EVSTDH:
10988 return spe_expand_stv_builtin (CODE_FOR_spe_evstdh, exp);
10989 case SPE_BUILTIN_EVSTDW:
10990 return spe_expand_stv_builtin (CODE_FOR_spe_evstdw, exp);
10991 case SPE_BUILTIN_EVSTWHE:
10992 return spe_expand_stv_builtin (CODE_FOR_spe_evstwhe, exp);
10993 case SPE_BUILTIN_EVSTWHO:
10994 return spe_expand_stv_builtin (CODE_FOR_spe_evstwho, exp);
10995 case SPE_BUILTIN_EVSTWWE:
10996 return spe_expand_stv_builtin (CODE_FOR_spe_evstwwe, exp);
10997 case SPE_BUILTIN_EVSTWWO:
10998 return spe_expand_stv_builtin (CODE_FOR_spe_evstwwo, exp);
10999 case SPE_BUILTIN_MFSPEFSCR:
11000 icode = CODE_FOR_spe_mfspefscr;
11001 tmode = insn_data[icode].operand[0].mode;
11003 if (target == 0
11004 || GET_MODE (target) != tmode
11005 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
11006 target = gen_reg_rtx (tmode);
11008 pat = GEN_FCN (icode) (target);
11009 if (! pat)
11010 return 0;
11011 emit_insn (pat);
11012 return target;
11013 case SPE_BUILTIN_MTSPEFSCR:
11014 icode = CODE_FOR_spe_mtspefscr;
11015 arg0 = CALL_EXPR_ARG (exp, 0);
11016 op0 = expand_normal (arg0);
11017 mode0 = insn_data[icode].operand[0].mode;
11019 if (arg0 == error_mark_node)
11020 return const0_rtx;
11022 if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
11023 op0 = copy_to_mode_reg (mode0, op0);
11025 pat = GEN_FCN (icode) (op0);
11026 if (pat)
11027 emit_insn (pat);
11028 return NULL_RTX;
11029 default:
11030 break;
11033 *expandedp = false;
11034 return NULL_RTX;
11037 static rtx
11038 paired_expand_predicate_builtin (enum insn_code icode, tree exp, rtx target)
11040 rtx pat, scratch, tmp;
11041 tree form = CALL_EXPR_ARG (exp, 0);
11042 tree arg0 = CALL_EXPR_ARG (exp, 1);
11043 tree arg1 = CALL_EXPR_ARG (exp, 2);
11044 rtx op0 = expand_normal (arg0);
11045 rtx op1 = expand_normal (arg1);
11046 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
11047 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
11048 int form_int;
11049 enum rtx_code code;
11051 if (TREE_CODE (form) != INTEGER_CST)
11053 error ("argument 1 of __builtin_paired_predicate must be a constant");
11054 return const0_rtx;
11056 else
11057 form_int = TREE_INT_CST_LOW (form);
11059 gcc_assert (mode0 == mode1);
11061 if (arg0 == error_mark_node || arg1 == error_mark_node)
11062 return const0_rtx;
11064 if (target == 0
11065 || GET_MODE (target) != SImode
11066 || !(*insn_data[icode].operand[0].predicate) (target, SImode))
11067 target = gen_reg_rtx (SImode);
11068 if (!(*insn_data[icode].operand[1].predicate) (op0, mode0))
11069 op0 = copy_to_mode_reg (mode0, op0);
11070 if (!(*insn_data[icode].operand[2].predicate) (op1, mode1))
11071 op1 = copy_to_mode_reg (mode1, op1);
11073 scratch = gen_reg_rtx (CCFPmode);
11075 pat = GEN_FCN (icode) (scratch, op0, op1);
11076 if (!pat)
11077 return const0_rtx;
11079 emit_insn (pat);
11081 switch (form_int)
11083 /* LT bit. */
11084 case 0:
11085 code = LT;
11086 break;
11087 /* GT bit. */
11088 case 1:
11089 code = GT;
11090 break;
11091 /* EQ bit. */
11092 case 2:
11093 code = EQ;
11094 break;
11095 /* UN bit. */
11096 case 3:
11097 emit_insn (gen_move_from_CR_ov_bit (target, scratch));
11098 return target;
11099 default:
11100 error ("argument 1 of __builtin_paired_predicate is out of range");
11101 return const0_rtx;
11104 tmp = gen_rtx_fmt_ee (code, SImode, scratch, const0_rtx);
11105 emit_move_insn (target, tmp);
11106 return target;
11109 static rtx
11110 spe_expand_predicate_builtin (enum insn_code icode, tree exp, rtx target)
11112 rtx pat, scratch, tmp;
11113 tree form = CALL_EXPR_ARG (exp, 0);
11114 tree arg0 = CALL_EXPR_ARG (exp, 1);
11115 tree arg1 = CALL_EXPR_ARG (exp, 2);
11116 rtx op0 = expand_normal (arg0);
11117 rtx op1 = expand_normal (arg1);
11118 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
11119 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
11120 int form_int;
11121 enum rtx_code code;
11123 if (TREE_CODE (form) != INTEGER_CST)
11125 error ("argument 1 of __builtin_spe_predicate must be a constant");
11126 return const0_rtx;
11128 else
11129 form_int = TREE_INT_CST_LOW (form);
11131 gcc_assert (mode0 == mode1);
11133 if (arg0 == error_mark_node || arg1 == error_mark_node)
11134 return const0_rtx;
11136 if (target == 0
11137 || GET_MODE (target) != SImode
11138 || ! (*insn_data[icode].operand[0].predicate) (target, SImode))
11139 target = gen_reg_rtx (SImode);
11141 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
11142 op0 = copy_to_mode_reg (mode0, op0);
11143 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
11144 op1 = copy_to_mode_reg (mode1, op1);
11146 scratch = gen_reg_rtx (CCmode);
11148 pat = GEN_FCN (icode) (scratch, op0, op1);
11149 if (! pat)
11150 return const0_rtx;
11151 emit_insn (pat);
11153 /* There are 4 variants for each predicate: _any_, _all_, _upper_,
11154 _lower_. We use one compare, but look in different bits of the
11155 CR for each variant.
11157 There are 2 elements in each SPE simd type (upper/lower). The CR
11158 bits are set as follows:
11160 BIT0 | BIT 1 | BIT 2 | BIT 3
11161 U | L | (U | L) | (U & L)
11163 So, for an "all" relationship, BIT 3 would be set.
11164 For an "any" relationship, BIT 2 would be set. Etc.
11166 Following traditional nomenclature, these bits map to:
11168 BIT0 | BIT 1 | BIT 2 | BIT 3
11169 LT | GT | EQ | OV
11171 Later, we will generate rtl to look in the LT/EQ/EQ/OV bits.
11174 switch (form_int)
11176 /* All variant. OV bit. */
11177 case 0:
11178 /* We need to get to the OV bit, which is the ORDERED bit. We
11179 could generate (ordered:SI (reg:CC xx) (const_int 0)), but
11180 that's ugly and will make validate_condition_mode die.
11181 So let's just use another pattern. */
11182 emit_insn (gen_move_from_CR_ov_bit (target, scratch));
11183 return target;
11184 /* Any variant. EQ bit. */
11185 case 1:
11186 code = EQ;
11187 break;
11188 /* Upper variant. LT bit. */
11189 case 2:
11190 code = LT;
11191 break;
11192 /* Lower variant. GT bit. */
11193 case 3:
11194 code = GT;
11195 break;
11196 default:
11197 error ("argument 1 of __builtin_spe_predicate is out of range");
11198 return const0_rtx;
11201 tmp = gen_rtx_fmt_ee (code, SImode, scratch, const0_rtx);
11202 emit_move_insn (target, tmp);
11204 return target;
11207 /* The evsel builtins look like this:
11209 e = __builtin_spe_evsel_OP (a, b, c, d);
11211 and work like this:
11213 e[upper] = a[upper] *OP* b[upper] ? c[upper] : d[upper];
11214 e[lower] = a[lower] *OP* b[lower] ? c[lower] : d[lower];
11217 static rtx
11218 spe_expand_evsel_builtin (enum insn_code icode, tree exp, rtx target)
11220 rtx pat, scratch;
11221 tree arg0 = CALL_EXPR_ARG (exp, 0);
11222 tree arg1 = CALL_EXPR_ARG (exp, 1);
11223 tree arg2 = CALL_EXPR_ARG (exp, 2);
11224 tree arg3 = CALL_EXPR_ARG (exp, 3);
11225 rtx op0 = expand_normal (arg0);
11226 rtx op1 = expand_normal (arg1);
11227 rtx op2 = expand_normal (arg2);
11228 rtx op3 = expand_normal (arg3);
11229 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
11230 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
11232 gcc_assert (mode0 == mode1);
11234 if (arg0 == error_mark_node || arg1 == error_mark_node
11235 || arg2 == error_mark_node || arg3 == error_mark_node)
11236 return const0_rtx;
11238 if (target == 0
11239 || GET_MODE (target) != mode0
11240 || ! (*insn_data[icode].operand[0].predicate) (target, mode0))
11241 target = gen_reg_rtx (mode0);
11243 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
11244 op0 = copy_to_mode_reg (mode0, op0);
11245 if (! (*insn_data[icode].operand[1].predicate) (op1, mode1))
11246 op1 = copy_to_mode_reg (mode0, op1);
11247 if (! (*insn_data[icode].operand[1].predicate) (op2, mode1))
11248 op2 = copy_to_mode_reg (mode0, op2);
11249 if (! (*insn_data[icode].operand[1].predicate) (op3, mode1))
11250 op3 = copy_to_mode_reg (mode0, op3);
11252 /* Generate the compare. */
11253 scratch = gen_reg_rtx (CCmode);
11254 pat = GEN_FCN (icode) (scratch, op0, op1);
11255 if (! pat)
11256 return const0_rtx;
11257 emit_insn (pat);
11259 if (mode0 == V2SImode)
11260 emit_insn (gen_spe_evsel (target, op2, op3, scratch));
11261 else
11262 emit_insn (gen_spe_evsel_fs (target, op2, op3, scratch));
11264 return target;
11267 /* Raise an error message for a builtin function that is called without the
11268 appropriate target options being set. */
11270 static void
11271 rs6000_invalid_builtin (enum rs6000_builtins fncode)
11273 size_t uns_fncode = (size_t)fncode;
11274 const char *name = rs6000_builtin_info[uns_fncode].name;
11275 HOST_WIDE_INT fnmask = rs6000_builtin_info[uns_fncode].mask;
11277 gcc_assert (name != NULL);
11278 if ((fnmask & RS6000_BTM_CELL) != 0)
11279 error ("Builtin function %s is only valid for the cell processor", name);
11280 else if ((fnmask & RS6000_BTM_VSX) != 0)
11281 error ("Builtin function %s requires the -mvsx option", name);
11282 else if ((fnmask & RS6000_BTM_ALTIVEC) != 0)
11283 error ("Builtin function %s requires the -maltivec option", name);
11284 else if ((fnmask & RS6000_BTM_PAIRED) != 0)
11285 error ("Builtin function %s requires the -mpaired option", name);
11286 else if ((fnmask & RS6000_BTM_SPE) != 0)
11287 error ("Builtin function %s requires the -mspe option", name);
11288 else
11289 error ("Builtin function %s is not supported with the current options",
11290 name);
11293 /* Expand an expression EXP that calls a built-in function,
11294 with result going to TARGET if that's convenient
11295 (and in mode MODE if that's convenient).
11296 SUBTARGET may be used as the target for computing one of EXP's operands.
11297 IGNORE is nonzero if the value is to be ignored. */
11299 static rtx
11300 rs6000_expand_builtin (tree exp, rtx target, rtx subtarget ATTRIBUTE_UNUSED,
11301 enum machine_mode mode ATTRIBUTE_UNUSED,
11302 int ignore ATTRIBUTE_UNUSED)
11304 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
11305 enum rs6000_builtins fcode
11306 = (enum rs6000_builtins)DECL_FUNCTION_CODE (fndecl);
11307 size_t uns_fcode = (size_t)fcode;
11308 const struct builtin_description *d;
11309 size_t i;
11310 rtx ret;
11311 bool success;
11312 HOST_WIDE_INT mask = rs6000_builtin_info[uns_fcode].mask;
11313 bool func_valid_p = ((rs6000_builtin_mask & mask) == mask);
11315 if (TARGET_DEBUG_BUILTIN)
11317 enum insn_code icode = rs6000_builtin_info[uns_fcode].icode;
11318 const char *name1 = rs6000_builtin_info[uns_fcode].name;
11319 const char *name2 = ((icode != CODE_FOR_nothing)
11320 ? get_insn_name ((int)icode)
11321 : "nothing");
11322 const char *name3;
11324 switch (rs6000_builtin_info[uns_fcode].attr & RS6000_BTC_TYPE_MASK)
11326 default: name3 = "unknown"; break;
11327 case RS6000_BTC_SPECIAL: name3 = "special"; break;
11328 case RS6000_BTC_UNARY: name3 = "unary"; break;
11329 case RS6000_BTC_BINARY: name3 = "binary"; break;
11330 case RS6000_BTC_TERNARY: name3 = "ternary"; break;
11331 case RS6000_BTC_PREDICATE: name3 = "predicate"; break;
11332 case RS6000_BTC_ABS: name3 = "abs"; break;
11333 case RS6000_BTC_EVSEL: name3 = "evsel"; break;
11334 case RS6000_BTC_DST: name3 = "dst"; break;
11338 fprintf (stderr,
11339 "rs6000_expand_builtin, %s (%d), insn = %s (%d), type=%s%s\n",
11340 (name1) ? name1 : "---", fcode,
11341 (name2) ? name2 : "---", (int)icode,
11342 name3,
11343 func_valid_p ? "" : ", not valid");
11346 if (!func_valid_p)
11348 rs6000_invalid_builtin (fcode);
11350 /* Given it is invalid, just generate a normal call. */
11351 return expand_call (exp, target, ignore);
11354 switch (fcode)
11356 case RS6000_BUILTIN_RECIP:
11357 return rs6000_expand_binop_builtin (CODE_FOR_recipdf3, exp, target);
11359 case RS6000_BUILTIN_RECIPF:
11360 return rs6000_expand_binop_builtin (CODE_FOR_recipsf3, exp, target);
11362 case RS6000_BUILTIN_RSQRTF:
11363 return rs6000_expand_unop_builtin (CODE_FOR_rsqrtsf2, exp, target);
11365 case RS6000_BUILTIN_RSQRT:
11366 return rs6000_expand_unop_builtin (CODE_FOR_rsqrtdf2, exp, target);
11368 case POWER7_BUILTIN_BPERMD:
11369 return rs6000_expand_binop_builtin (((TARGET_64BIT)
11370 ? CODE_FOR_bpermd_di
11371 : CODE_FOR_bpermd_si), exp, target);
11373 case RS6000_BUILTIN_GET_TB:
11374 return rs6000_expand_zeroop_builtin (CODE_FOR_rs6000_get_timebase,
11375 target);
11377 case RS6000_BUILTIN_MFTB:
11378 return rs6000_expand_zeroop_builtin (((TARGET_64BIT)
11379 ? CODE_FOR_rs6000_mftb_di
11380 : CODE_FOR_rs6000_mftb_si),
11381 target);
11383 case ALTIVEC_BUILTIN_MASK_FOR_LOAD:
11384 case ALTIVEC_BUILTIN_MASK_FOR_STORE:
11386 int icode = (int) CODE_FOR_altivec_lvsr;
11387 enum machine_mode tmode = insn_data[icode].operand[0].mode;
11388 enum machine_mode mode = insn_data[icode].operand[1].mode;
11389 tree arg;
11390 rtx op, addr, pat;
11392 gcc_assert (TARGET_ALTIVEC);
11394 arg = CALL_EXPR_ARG (exp, 0);
11395 gcc_assert (POINTER_TYPE_P (TREE_TYPE (arg)));
11396 op = expand_expr (arg, NULL_RTX, Pmode, EXPAND_NORMAL);
11397 addr = memory_address (mode, op);
11398 if (fcode == ALTIVEC_BUILTIN_MASK_FOR_STORE)
11399 op = addr;
11400 else
11402 /* For the load case need to negate the address. */
11403 op = gen_reg_rtx (GET_MODE (addr));
11404 emit_insn (gen_rtx_SET (VOIDmode, op,
11405 gen_rtx_NEG (GET_MODE (addr), addr)));
11407 op = gen_rtx_MEM (mode, op);
11409 if (target == 0
11410 || GET_MODE (target) != tmode
11411 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
11412 target = gen_reg_rtx (tmode);
11414 /*pat = gen_altivec_lvsr (target, op);*/
11415 pat = GEN_FCN (icode) (target, op);
11416 if (!pat)
11417 return 0;
11418 emit_insn (pat);
11420 return target;
11423 case ALTIVEC_BUILTIN_VCFUX:
11424 case ALTIVEC_BUILTIN_VCFSX:
11425 case ALTIVEC_BUILTIN_VCTUXS:
11426 case ALTIVEC_BUILTIN_VCTSXS:
11427 /* FIXME: There's got to be a nicer way to handle this case than
11428 constructing a new CALL_EXPR. */
11429 if (call_expr_nargs (exp) == 1)
11431 exp = build_call_nary (TREE_TYPE (exp), CALL_EXPR_FN (exp),
11432 2, CALL_EXPR_ARG (exp, 0), integer_zero_node);
11434 break;
11436 default:
11437 break;
11440 if (TARGET_ALTIVEC)
11442 ret = altivec_expand_builtin (exp, target, &success);
11444 if (success)
11445 return ret;
11447 if (TARGET_SPE)
11449 ret = spe_expand_builtin (exp, target, &success);
11451 if (success)
11452 return ret;
11454 if (TARGET_PAIRED_FLOAT)
11456 ret = paired_expand_builtin (exp, target, &success);
11458 if (success)
11459 return ret;
11462 gcc_assert (TARGET_ALTIVEC || TARGET_VSX || TARGET_SPE || TARGET_PAIRED_FLOAT);
11464 /* Handle simple unary operations. */
11465 d = bdesc_1arg;
11466 for (i = 0; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
11467 if (d->code == fcode)
11468 return rs6000_expand_unop_builtin (d->icode, exp, target);
11470 /* Handle simple binary operations. */
11471 d = bdesc_2arg;
11472 for (i = 0; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
11473 if (d->code == fcode)
11474 return rs6000_expand_binop_builtin (d->icode, exp, target);
11476 /* Handle simple ternary operations. */
11477 d = bdesc_3arg;
11478 for (i = 0; i < ARRAY_SIZE (bdesc_3arg); i++, d++)
11479 if (d->code == fcode)
11480 return rs6000_expand_ternop_builtin (d->icode, exp, target);
11482 gcc_unreachable ();
11485 static void
11486 rs6000_init_builtins (void)
11488 tree tdecl;
11489 tree ftype;
11490 enum machine_mode mode;
11492 if (TARGET_DEBUG_BUILTIN)
11493 fprintf (stderr, "rs6000_init_builtins%s%s%s%s\n",
11494 (TARGET_PAIRED_FLOAT) ? ", paired" : "",
11495 (TARGET_SPE) ? ", spe" : "",
11496 (TARGET_ALTIVEC) ? ", altivec" : "",
11497 (TARGET_VSX) ? ", vsx" : "");
11499 V2SI_type_node = build_vector_type (intSI_type_node, 2);
11500 V2SF_type_node = build_vector_type (float_type_node, 2);
11501 V2DI_type_node = build_vector_type (intDI_type_node, 2);
11502 V2DF_type_node = build_vector_type (double_type_node, 2);
11503 V4HI_type_node = build_vector_type (intHI_type_node, 4);
11504 V4SI_type_node = build_vector_type (intSI_type_node, 4);
11505 V4SF_type_node = build_vector_type (float_type_node, 4);
11506 V8HI_type_node = build_vector_type (intHI_type_node, 8);
11507 V16QI_type_node = build_vector_type (intQI_type_node, 16);
11509 unsigned_V16QI_type_node = build_vector_type (unsigned_intQI_type_node, 16);
11510 unsigned_V8HI_type_node = build_vector_type (unsigned_intHI_type_node, 8);
11511 unsigned_V4SI_type_node = build_vector_type (unsigned_intSI_type_node, 4);
11512 unsigned_V2DI_type_node = build_vector_type (unsigned_intDI_type_node, 2);
11514 opaque_V2SF_type_node = build_opaque_vector_type (float_type_node, 2);
11515 opaque_V2SI_type_node = build_opaque_vector_type (intSI_type_node, 2);
11516 opaque_p_V2SI_type_node = build_pointer_type (opaque_V2SI_type_node);
11517 opaque_V4SI_type_node = build_opaque_vector_type (intSI_type_node, 4);
11519 /* The 'vector bool ...' types must be kept distinct from 'vector unsigned ...'
11520 types, especially in C++ land. Similarly, 'vector pixel' is distinct from
11521 'vector unsigned short'. */
11523 bool_char_type_node = build_distinct_type_copy (unsigned_intQI_type_node);
11524 bool_short_type_node = build_distinct_type_copy (unsigned_intHI_type_node);
11525 bool_int_type_node = build_distinct_type_copy (unsigned_intSI_type_node);
11526 bool_long_type_node = build_distinct_type_copy (unsigned_intDI_type_node);
11527 pixel_type_node = build_distinct_type_copy (unsigned_intHI_type_node);
11529 long_integer_type_internal_node = long_integer_type_node;
11530 long_unsigned_type_internal_node = long_unsigned_type_node;
11531 long_long_integer_type_internal_node = long_long_integer_type_node;
11532 long_long_unsigned_type_internal_node = long_long_unsigned_type_node;
11533 intQI_type_internal_node = intQI_type_node;
11534 uintQI_type_internal_node = unsigned_intQI_type_node;
11535 intHI_type_internal_node = intHI_type_node;
11536 uintHI_type_internal_node = unsigned_intHI_type_node;
11537 intSI_type_internal_node = intSI_type_node;
11538 uintSI_type_internal_node = unsigned_intSI_type_node;
11539 intDI_type_internal_node = intDI_type_node;
11540 uintDI_type_internal_node = unsigned_intDI_type_node;
11541 float_type_internal_node = float_type_node;
11542 double_type_internal_node = double_type_node;
11543 void_type_internal_node = void_type_node;
11545 /* Initialize the modes for builtin_function_type, mapping a machine mode to
11546 tree type node. */
11547 builtin_mode_to_type[QImode][0] = integer_type_node;
11548 builtin_mode_to_type[HImode][0] = integer_type_node;
11549 builtin_mode_to_type[SImode][0] = intSI_type_node;
11550 builtin_mode_to_type[SImode][1] = unsigned_intSI_type_node;
11551 builtin_mode_to_type[DImode][0] = intDI_type_node;
11552 builtin_mode_to_type[DImode][1] = unsigned_intDI_type_node;
11553 builtin_mode_to_type[SFmode][0] = float_type_node;
11554 builtin_mode_to_type[DFmode][0] = double_type_node;
11555 builtin_mode_to_type[V2SImode][0] = V2SI_type_node;
11556 builtin_mode_to_type[V2SFmode][0] = V2SF_type_node;
11557 builtin_mode_to_type[V2DImode][0] = V2DI_type_node;
11558 builtin_mode_to_type[V2DImode][1] = unsigned_V2DI_type_node;
11559 builtin_mode_to_type[V2DFmode][0] = V2DF_type_node;
11560 builtin_mode_to_type[V4HImode][0] = V4HI_type_node;
11561 builtin_mode_to_type[V4SImode][0] = V4SI_type_node;
11562 builtin_mode_to_type[V4SImode][1] = unsigned_V4SI_type_node;
11563 builtin_mode_to_type[V4SFmode][0] = V4SF_type_node;
11564 builtin_mode_to_type[V8HImode][0] = V8HI_type_node;
11565 builtin_mode_to_type[V8HImode][1] = unsigned_V8HI_type_node;
11566 builtin_mode_to_type[V16QImode][0] = V16QI_type_node;
11567 builtin_mode_to_type[V16QImode][1] = unsigned_V16QI_type_node;
11569 tdecl = add_builtin_type ("__bool char", bool_char_type_node);
11570 TYPE_NAME (bool_char_type_node) = tdecl;
11572 tdecl = add_builtin_type ("__bool short", bool_short_type_node);
11573 TYPE_NAME (bool_short_type_node) = tdecl;
11575 tdecl = add_builtin_type ("__bool int", bool_int_type_node);
11576 TYPE_NAME (bool_int_type_node) = tdecl;
11578 tdecl = add_builtin_type ("__pixel", pixel_type_node);
11579 TYPE_NAME (pixel_type_node) = tdecl;
11581 bool_V16QI_type_node = build_vector_type (bool_char_type_node, 16);
11582 bool_V8HI_type_node = build_vector_type (bool_short_type_node, 8);
11583 bool_V4SI_type_node = build_vector_type (bool_int_type_node, 4);
11584 bool_V2DI_type_node = build_vector_type (bool_long_type_node, 2);
11585 pixel_V8HI_type_node = build_vector_type (pixel_type_node, 8);
11587 tdecl = add_builtin_type ("__vector unsigned char", unsigned_V16QI_type_node);
11588 TYPE_NAME (unsigned_V16QI_type_node) = tdecl;
11590 tdecl = add_builtin_type ("__vector signed char", V16QI_type_node);
11591 TYPE_NAME (V16QI_type_node) = tdecl;
11593 tdecl = add_builtin_type ("__vector __bool char", bool_V16QI_type_node);
11594 TYPE_NAME ( bool_V16QI_type_node) = tdecl;
11596 tdecl = add_builtin_type ("__vector unsigned short", unsigned_V8HI_type_node);
11597 TYPE_NAME (unsigned_V8HI_type_node) = tdecl;
11599 tdecl = add_builtin_type ("__vector signed short", V8HI_type_node);
11600 TYPE_NAME (V8HI_type_node) = tdecl;
11602 tdecl = add_builtin_type ("__vector __bool short", bool_V8HI_type_node);
11603 TYPE_NAME (bool_V8HI_type_node) = tdecl;
11605 tdecl = add_builtin_type ("__vector unsigned int", unsigned_V4SI_type_node);
11606 TYPE_NAME (unsigned_V4SI_type_node) = tdecl;
11608 tdecl = add_builtin_type ("__vector signed int", V4SI_type_node);
11609 TYPE_NAME (V4SI_type_node) = tdecl;
11611 tdecl = add_builtin_type ("__vector __bool int", bool_V4SI_type_node);
11612 TYPE_NAME (bool_V4SI_type_node) = tdecl;
11614 tdecl = add_builtin_type ("__vector float", V4SF_type_node);
11615 TYPE_NAME (V4SF_type_node) = tdecl;
11617 tdecl = add_builtin_type ("__vector __pixel", pixel_V8HI_type_node);
11618 TYPE_NAME (pixel_V8HI_type_node) = tdecl;
11620 tdecl = add_builtin_type ("__vector double", V2DF_type_node);
11621 TYPE_NAME (V2DF_type_node) = tdecl;
11623 tdecl = add_builtin_type ("__vector long", V2DI_type_node);
11624 TYPE_NAME (V2DI_type_node) = tdecl;
11626 tdecl = add_builtin_type ("__vector unsigned long", unsigned_V2DI_type_node);
11627 TYPE_NAME (unsigned_V2DI_type_node) = tdecl;
11629 tdecl = add_builtin_type ("__vector __bool long", bool_V2DI_type_node);
11630 TYPE_NAME (bool_V2DI_type_node) = tdecl;
11632 /* Paired and SPE builtins are only available if you build a compiler with
11633 the appropriate options, so only create those builtins with the
11634 appropriate compiler option. Create Altivec and VSX builtins on machines
11635 with at least the general purpose extensions (970 and newer) to allow the
11636 use of the target attribute. */
11637 if (TARGET_PAIRED_FLOAT)
11638 paired_init_builtins ();
11639 if (TARGET_SPE)
11640 spe_init_builtins ();
11641 if (TARGET_EXTRA_BUILTINS)
11642 altivec_init_builtins ();
11643 if (TARGET_EXTRA_BUILTINS || TARGET_SPE || TARGET_PAIRED_FLOAT)
11644 rs6000_common_init_builtins ();
11646 ftype = builtin_function_type (DFmode, DFmode, DFmode, VOIDmode,
11647 RS6000_BUILTIN_RECIP, "__builtin_recipdiv");
11648 def_builtin ("__builtin_recipdiv", ftype, RS6000_BUILTIN_RECIP);
11650 ftype = builtin_function_type (SFmode, SFmode, SFmode, VOIDmode,
11651 RS6000_BUILTIN_RECIPF, "__builtin_recipdivf");
11652 def_builtin ("__builtin_recipdivf", ftype, RS6000_BUILTIN_RECIPF);
11654 ftype = builtin_function_type (DFmode, DFmode, VOIDmode, VOIDmode,
11655 RS6000_BUILTIN_RSQRT, "__builtin_rsqrt");
11656 def_builtin ("__builtin_rsqrt", ftype, RS6000_BUILTIN_RSQRT);
11658 ftype = builtin_function_type (SFmode, SFmode, VOIDmode, VOIDmode,
11659 RS6000_BUILTIN_RSQRTF, "__builtin_rsqrtf");
11660 def_builtin ("__builtin_rsqrtf", ftype, RS6000_BUILTIN_RSQRTF);
11662 mode = (TARGET_64BIT) ? DImode : SImode;
11663 ftype = builtin_function_type (mode, mode, mode, VOIDmode,
11664 POWER7_BUILTIN_BPERMD, "__builtin_bpermd");
11665 def_builtin ("__builtin_bpermd", ftype, POWER7_BUILTIN_BPERMD);
11667 ftype = build_function_type_list (unsigned_intDI_type_node,
11668 NULL_TREE);
11669 def_builtin ("__builtin_ppc_get_timebase", ftype, RS6000_BUILTIN_GET_TB);
11671 if (TARGET_64BIT)
11672 ftype = build_function_type_list (unsigned_intDI_type_node,
11673 NULL_TREE);
11674 else
11675 ftype = build_function_type_list (unsigned_intSI_type_node,
11676 NULL_TREE);
11677 def_builtin ("__builtin_ppc_mftb", ftype, RS6000_BUILTIN_MFTB);
11679 #if TARGET_XCOFF
11680 /* AIX libm provides clog as __clog. */
11681 if ((tdecl = builtin_decl_explicit (BUILT_IN_CLOG)) != NULL_TREE)
11682 set_user_assembler_name (tdecl, "__clog");
11683 #endif
11685 #ifdef SUBTARGET_INIT_BUILTINS
11686 SUBTARGET_INIT_BUILTINS;
11687 #endif
11690 /* Returns the rs6000 builtin decl for CODE. */
11692 static tree
11693 rs6000_builtin_decl (unsigned code, bool initialize_p ATTRIBUTE_UNUSED)
11695 HOST_WIDE_INT fnmask;
11697 if (code >= RS6000_BUILTIN_COUNT)
11698 return error_mark_node;
11700 fnmask = rs6000_builtin_info[code].mask;
11701 if ((fnmask & rs6000_builtin_mask) != fnmask)
11703 rs6000_invalid_builtin ((enum rs6000_builtins)code);
11704 return error_mark_node;
11707 return rs6000_builtin_decls[code];
11710 static void
11711 spe_init_builtins (void)
11713 tree puint_type_node = build_pointer_type (unsigned_type_node);
11714 tree pushort_type_node = build_pointer_type (short_unsigned_type_node);
11715 const struct builtin_description *d;
11716 size_t i;
11718 tree v2si_ftype_4_v2si
11719 = build_function_type_list (opaque_V2SI_type_node,
11720 opaque_V2SI_type_node,
11721 opaque_V2SI_type_node,
11722 opaque_V2SI_type_node,
11723 opaque_V2SI_type_node,
11724 NULL_TREE);
11726 tree v2sf_ftype_4_v2sf
11727 = build_function_type_list (opaque_V2SF_type_node,
11728 opaque_V2SF_type_node,
11729 opaque_V2SF_type_node,
11730 opaque_V2SF_type_node,
11731 opaque_V2SF_type_node,
11732 NULL_TREE);
11734 tree int_ftype_int_v2si_v2si
11735 = build_function_type_list (integer_type_node,
11736 integer_type_node,
11737 opaque_V2SI_type_node,
11738 opaque_V2SI_type_node,
11739 NULL_TREE);
11741 tree int_ftype_int_v2sf_v2sf
11742 = build_function_type_list (integer_type_node,
11743 integer_type_node,
11744 opaque_V2SF_type_node,
11745 opaque_V2SF_type_node,
11746 NULL_TREE);
11748 tree void_ftype_v2si_puint_int
11749 = build_function_type_list (void_type_node,
11750 opaque_V2SI_type_node,
11751 puint_type_node,
11752 integer_type_node,
11753 NULL_TREE);
11755 tree void_ftype_v2si_puint_char
11756 = build_function_type_list (void_type_node,
11757 opaque_V2SI_type_node,
11758 puint_type_node,
11759 char_type_node,
11760 NULL_TREE);
11762 tree void_ftype_v2si_pv2si_int
11763 = build_function_type_list (void_type_node,
11764 opaque_V2SI_type_node,
11765 opaque_p_V2SI_type_node,
11766 integer_type_node,
11767 NULL_TREE);
11769 tree void_ftype_v2si_pv2si_char
11770 = build_function_type_list (void_type_node,
11771 opaque_V2SI_type_node,
11772 opaque_p_V2SI_type_node,
11773 char_type_node,
11774 NULL_TREE);
11776 tree void_ftype_int
11777 = build_function_type_list (void_type_node, integer_type_node, NULL_TREE);
11779 tree int_ftype_void
11780 = build_function_type_list (integer_type_node, NULL_TREE);
11782 tree v2si_ftype_pv2si_int
11783 = build_function_type_list (opaque_V2SI_type_node,
11784 opaque_p_V2SI_type_node,
11785 integer_type_node,
11786 NULL_TREE);
11788 tree v2si_ftype_puint_int
11789 = build_function_type_list (opaque_V2SI_type_node,
11790 puint_type_node,
11791 integer_type_node,
11792 NULL_TREE);
11794 tree v2si_ftype_pushort_int
11795 = build_function_type_list (opaque_V2SI_type_node,
11796 pushort_type_node,
11797 integer_type_node,
11798 NULL_TREE);
11800 tree v2si_ftype_signed_char
11801 = build_function_type_list (opaque_V2SI_type_node,
11802 signed_char_type_node,
11803 NULL_TREE);
11805 add_builtin_type ("__ev64_opaque__", opaque_V2SI_type_node);
11807 /* Initialize irregular SPE builtins. */
11809 def_builtin ("__builtin_spe_mtspefscr", void_ftype_int, SPE_BUILTIN_MTSPEFSCR);
11810 def_builtin ("__builtin_spe_mfspefscr", int_ftype_void, SPE_BUILTIN_MFSPEFSCR);
11811 def_builtin ("__builtin_spe_evstddx", void_ftype_v2si_pv2si_int, SPE_BUILTIN_EVSTDDX);
11812 def_builtin ("__builtin_spe_evstdhx", void_ftype_v2si_pv2si_int, SPE_BUILTIN_EVSTDHX);
11813 def_builtin ("__builtin_spe_evstdwx", void_ftype_v2si_pv2si_int, SPE_BUILTIN_EVSTDWX);
11814 def_builtin ("__builtin_spe_evstwhex", void_ftype_v2si_puint_int, SPE_BUILTIN_EVSTWHEX);
11815 def_builtin ("__builtin_spe_evstwhox", void_ftype_v2si_puint_int, SPE_BUILTIN_EVSTWHOX);
11816 def_builtin ("__builtin_spe_evstwwex", void_ftype_v2si_puint_int, SPE_BUILTIN_EVSTWWEX);
11817 def_builtin ("__builtin_spe_evstwwox", void_ftype_v2si_puint_int, SPE_BUILTIN_EVSTWWOX);
11818 def_builtin ("__builtin_spe_evstdd", void_ftype_v2si_pv2si_char, SPE_BUILTIN_EVSTDD);
11819 def_builtin ("__builtin_spe_evstdh", void_ftype_v2si_pv2si_char, SPE_BUILTIN_EVSTDH);
11820 def_builtin ("__builtin_spe_evstdw", void_ftype_v2si_pv2si_char, SPE_BUILTIN_EVSTDW);
11821 def_builtin ("__builtin_spe_evstwhe", void_ftype_v2si_puint_char, SPE_BUILTIN_EVSTWHE);
11822 def_builtin ("__builtin_spe_evstwho", void_ftype_v2si_puint_char, SPE_BUILTIN_EVSTWHO);
11823 def_builtin ("__builtin_spe_evstwwe", void_ftype_v2si_puint_char, SPE_BUILTIN_EVSTWWE);
11824 def_builtin ("__builtin_spe_evstwwo", void_ftype_v2si_puint_char, SPE_BUILTIN_EVSTWWO);
11825 def_builtin ("__builtin_spe_evsplatfi", v2si_ftype_signed_char, SPE_BUILTIN_EVSPLATFI);
11826 def_builtin ("__builtin_spe_evsplati", v2si_ftype_signed_char, SPE_BUILTIN_EVSPLATI);
11828 /* Loads. */
11829 def_builtin ("__builtin_spe_evlddx", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDDX);
11830 def_builtin ("__builtin_spe_evldwx", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDWX);
11831 def_builtin ("__builtin_spe_evldhx", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDHX);
11832 def_builtin ("__builtin_spe_evlwhex", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHEX);
11833 def_builtin ("__builtin_spe_evlwhoux", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHOUX);
11834 def_builtin ("__builtin_spe_evlwhosx", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHOSX);
11835 def_builtin ("__builtin_spe_evlwwsplatx", v2si_ftype_puint_int, SPE_BUILTIN_EVLWWSPLATX);
11836 def_builtin ("__builtin_spe_evlwhsplatx", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHSPLATX);
11837 def_builtin ("__builtin_spe_evlhhesplatx", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHESPLATX);
11838 def_builtin ("__builtin_spe_evlhhousplatx", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHOUSPLATX);
11839 def_builtin ("__builtin_spe_evlhhossplatx", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHOSSPLATX);
11840 def_builtin ("__builtin_spe_evldd", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDD);
11841 def_builtin ("__builtin_spe_evldw", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDW);
11842 def_builtin ("__builtin_spe_evldh", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDH);
11843 def_builtin ("__builtin_spe_evlhhesplat", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHESPLAT);
11844 def_builtin ("__builtin_spe_evlhhossplat", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHOSSPLAT);
11845 def_builtin ("__builtin_spe_evlhhousplat", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHOUSPLAT);
11846 def_builtin ("__builtin_spe_evlwhe", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHE);
11847 def_builtin ("__builtin_spe_evlwhos", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHOS);
11848 def_builtin ("__builtin_spe_evlwhou", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHOU);
11849 def_builtin ("__builtin_spe_evlwhsplat", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHSPLAT);
11850 def_builtin ("__builtin_spe_evlwwsplat", v2si_ftype_puint_int, SPE_BUILTIN_EVLWWSPLAT);
11852 /* Predicates. */
11853 d = bdesc_spe_predicates;
11854 for (i = 0; i < ARRAY_SIZE (bdesc_spe_predicates); ++i, d++)
11856 tree type;
11858 switch (insn_data[d->icode].operand[1].mode)
11860 case V2SImode:
11861 type = int_ftype_int_v2si_v2si;
11862 break;
11863 case V2SFmode:
11864 type = int_ftype_int_v2sf_v2sf;
11865 break;
11866 default:
11867 gcc_unreachable ();
11870 def_builtin (d->name, type, d->code);
11873 /* Evsel predicates. */
11874 d = bdesc_spe_evsel;
11875 for (i = 0; i < ARRAY_SIZE (bdesc_spe_evsel); ++i, d++)
11877 tree type;
11879 switch (insn_data[d->icode].operand[1].mode)
11881 case V2SImode:
11882 type = v2si_ftype_4_v2si;
11883 break;
11884 case V2SFmode:
11885 type = v2sf_ftype_4_v2sf;
11886 break;
11887 default:
11888 gcc_unreachable ();
11891 def_builtin (d->name, type, d->code);
11895 static void
11896 paired_init_builtins (void)
11898 const struct builtin_description *d;
11899 size_t i;
11901 tree int_ftype_int_v2sf_v2sf
11902 = build_function_type_list (integer_type_node,
11903 integer_type_node,
11904 V2SF_type_node,
11905 V2SF_type_node,
11906 NULL_TREE);
11907 tree pcfloat_type_node =
11908 build_pointer_type (build_qualified_type
11909 (float_type_node, TYPE_QUAL_CONST));
11911 tree v2sf_ftype_long_pcfloat = build_function_type_list (V2SF_type_node,
11912 long_integer_type_node,
11913 pcfloat_type_node,
11914 NULL_TREE);
11915 tree void_ftype_v2sf_long_pcfloat =
11916 build_function_type_list (void_type_node,
11917 V2SF_type_node,
11918 long_integer_type_node,
11919 pcfloat_type_node,
11920 NULL_TREE);
11923 def_builtin ("__builtin_paired_lx", v2sf_ftype_long_pcfloat,
11924 PAIRED_BUILTIN_LX);
11927 def_builtin ("__builtin_paired_stx", void_ftype_v2sf_long_pcfloat,
11928 PAIRED_BUILTIN_STX);
11930 /* Predicates. */
11931 d = bdesc_paired_preds;
11932 for (i = 0; i < ARRAY_SIZE (bdesc_paired_preds); ++i, d++)
11934 tree type;
11936 if (TARGET_DEBUG_BUILTIN)
11937 fprintf (stderr, "paired pred #%d, insn = %s [%d], mode = %s\n",
11938 (int)i, get_insn_name (d->icode), (int)d->icode,
11939 GET_MODE_NAME (insn_data[d->icode].operand[1].mode));
11941 switch (insn_data[d->icode].operand[1].mode)
11943 case V2SFmode:
11944 type = int_ftype_int_v2sf_v2sf;
11945 break;
11946 default:
11947 gcc_unreachable ();
11950 def_builtin (d->name, type, d->code);
11954 static void
11955 altivec_init_builtins (void)
11957 const struct builtin_description *d;
11958 size_t i;
11959 tree ftype;
11960 tree decl;
11962 tree pvoid_type_node = build_pointer_type (void_type_node);
11964 tree pcvoid_type_node
11965 = build_pointer_type (build_qualified_type (void_type_node,
11966 TYPE_QUAL_CONST));
11968 tree int_ftype_opaque
11969 = build_function_type_list (integer_type_node,
11970 opaque_V4SI_type_node, NULL_TREE);
11971 tree opaque_ftype_opaque
11972 = build_function_type_list (integer_type_node, NULL_TREE);
11973 tree opaque_ftype_opaque_int
11974 = build_function_type_list (opaque_V4SI_type_node,
11975 opaque_V4SI_type_node, integer_type_node, NULL_TREE);
11976 tree opaque_ftype_opaque_opaque_int
11977 = build_function_type_list (opaque_V4SI_type_node,
11978 opaque_V4SI_type_node, opaque_V4SI_type_node,
11979 integer_type_node, NULL_TREE);
11980 tree int_ftype_int_opaque_opaque
11981 = build_function_type_list (integer_type_node,
11982 integer_type_node, opaque_V4SI_type_node,
11983 opaque_V4SI_type_node, NULL_TREE);
11984 tree int_ftype_int_v4si_v4si
11985 = build_function_type_list (integer_type_node,
11986 integer_type_node, V4SI_type_node,
11987 V4SI_type_node, NULL_TREE);
11988 tree void_ftype_v4si
11989 = build_function_type_list (void_type_node, V4SI_type_node, NULL_TREE);
11990 tree v8hi_ftype_void
11991 = build_function_type_list (V8HI_type_node, NULL_TREE);
11992 tree void_ftype_void
11993 = build_function_type_list (void_type_node, NULL_TREE);
11994 tree void_ftype_int
11995 = build_function_type_list (void_type_node, integer_type_node, NULL_TREE);
11997 tree opaque_ftype_long_pcvoid
11998 = build_function_type_list (opaque_V4SI_type_node,
11999 long_integer_type_node, pcvoid_type_node,
12000 NULL_TREE);
12001 tree v16qi_ftype_long_pcvoid
12002 = build_function_type_list (V16QI_type_node,
12003 long_integer_type_node, pcvoid_type_node,
12004 NULL_TREE);
12005 tree v8hi_ftype_long_pcvoid
12006 = build_function_type_list (V8HI_type_node,
12007 long_integer_type_node, pcvoid_type_node,
12008 NULL_TREE);
12009 tree v4si_ftype_long_pcvoid
12010 = build_function_type_list (V4SI_type_node,
12011 long_integer_type_node, pcvoid_type_node,
12012 NULL_TREE);
12013 tree v4sf_ftype_long_pcvoid
12014 = build_function_type_list (V4SF_type_node,
12015 long_integer_type_node, pcvoid_type_node,
12016 NULL_TREE);
12017 tree v2df_ftype_long_pcvoid
12018 = build_function_type_list (V2DF_type_node,
12019 long_integer_type_node, pcvoid_type_node,
12020 NULL_TREE);
12021 tree v2di_ftype_long_pcvoid
12022 = build_function_type_list (V2DI_type_node,
12023 long_integer_type_node, pcvoid_type_node,
12024 NULL_TREE);
12026 tree void_ftype_opaque_long_pvoid
12027 = build_function_type_list (void_type_node,
12028 opaque_V4SI_type_node, long_integer_type_node,
12029 pvoid_type_node, NULL_TREE);
12030 tree void_ftype_v4si_long_pvoid
12031 = build_function_type_list (void_type_node,
12032 V4SI_type_node, long_integer_type_node,
12033 pvoid_type_node, NULL_TREE);
12034 tree void_ftype_v16qi_long_pvoid
12035 = build_function_type_list (void_type_node,
12036 V16QI_type_node, long_integer_type_node,
12037 pvoid_type_node, NULL_TREE);
12038 tree void_ftype_v8hi_long_pvoid
12039 = build_function_type_list (void_type_node,
12040 V8HI_type_node, long_integer_type_node,
12041 pvoid_type_node, NULL_TREE);
12042 tree void_ftype_v4sf_long_pvoid
12043 = build_function_type_list (void_type_node,
12044 V4SF_type_node, long_integer_type_node,
12045 pvoid_type_node, NULL_TREE);
12046 tree void_ftype_v2df_long_pvoid
12047 = build_function_type_list (void_type_node,
12048 V2DF_type_node, long_integer_type_node,
12049 pvoid_type_node, NULL_TREE);
12050 tree void_ftype_v2di_long_pvoid
12051 = build_function_type_list (void_type_node,
12052 V2DI_type_node, long_integer_type_node,
12053 pvoid_type_node, NULL_TREE);
12054 tree int_ftype_int_v8hi_v8hi
12055 = build_function_type_list (integer_type_node,
12056 integer_type_node, V8HI_type_node,
12057 V8HI_type_node, NULL_TREE);
12058 tree int_ftype_int_v16qi_v16qi
12059 = build_function_type_list (integer_type_node,
12060 integer_type_node, V16QI_type_node,
12061 V16QI_type_node, NULL_TREE);
12062 tree int_ftype_int_v4sf_v4sf
12063 = build_function_type_list (integer_type_node,
12064 integer_type_node, V4SF_type_node,
12065 V4SF_type_node, NULL_TREE);
12066 tree int_ftype_int_v2df_v2df
12067 = build_function_type_list (integer_type_node,
12068 integer_type_node, V2DF_type_node,
12069 V2DF_type_node, NULL_TREE);
12070 tree v4si_ftype_v4si
12071 = build_function_type_list (V4SI_type_node, V4SI_type_node, NULL_TREE);
12072 tree v8hi_ftype_v8hi
12073 = build_function_type_list (V8HI_type_node, V8HI_type_node, NULL_TREE);
12074 tree v16qi_ftype_v16qi
12075 = build_function_type_list (V16QI_type_node, V16QI_type_node, NULL_TREE);
12076 tree v4sf_ftype_v4sf
12077 = build_function_type_list (V4SF_type_node, V4SF_type_node, NULL_TREE);
12078 tree v2df_ftype_v2df
12079 = build_function_type_list (V2DF_type_node, V2DF_type_node, NULL_TREE);
12080 tree void_ftype_pcvoid_int_int
12081 = build_function_type_list (void_type_node,
12082 pcvoid_type_node, integer_type_node,
12083 integer_type_node, NULL_TREE);
12085 def_builtin ("__builtin_altivec_mtvscr", void_ftype_v4si, ALTIVEC_BUILTIN_MTVSCR);
12086 def_builtin ("__builtin_altivec_mfvscr", v8hi_ftype_void, ALTIVEC_BUILTIN_MFVSCR);
12087 def_builtin ("__builtin_altivec_dssall", void_ftype_void, ALTIVEC_BUILTIN_DSSALL);
12088 def_builtin ("__builtin_altivec_dss", void_ftype_int, ALTIVEC_BUILTIN_DSS);
12089 def_builtin ("__builtin_altivec_lvsl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVSL);
12090 def_builtin ("__builtin_altivec_lvsr", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVSR);
12091 def_builtin ("__builtin_altivec_lvebx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVEBX);
12092 def_builtin ("__builtin_altivec_lvehx", v8hi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVEHX);
12093 def_builtin ("__builtin_altivec_lvewx", v4si_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVEWX);
12094 def_builtin ("__builtin_altivec_lvxl", v4si_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVXL);
12095 def_builtin ("__builtin_altivec_lvx", v4si_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVX);
12096 def_builtin ("__builtin_altivec_stvx", void_ftype_v4si_long_pvoid, ALTIVEC_BUILTIN_STVX);
12097 def_builtin ("__builtin_altivec_stvewx", void_ftype_v4si_long_pvoid, ALTIVEC_BUILTIN_STVEWX);
12098 def_builtin ("__builtin_altivec_stvxl", void_ftype_v4si_long_pvoid, ALTIVEC_BUILTIN_STVXL);
12099 def_builtin ("__builtin_altivec_stvebx", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_STVEBX);
12100 def_builtin ("__builtin_altivec_stvehx", void_ftype_v8hi_long_pvoid, ALTIVEC_BUILTIN_STVEHX);
12101 def_builtin ("__builtin_vec_ld", opaque_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LD);
12102 def_builtin ("__builtin_vec_lde", opaque_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LDE);
12103 def_builtin ("__builtin_vec_ldl", opaque_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LDL);
12104 def_builtin ("__builtin_vec_lvsl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVSL);
12105 def_builtin ("__builtin_vec_lvsr", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVSR);
12106 def_builtin ("__builtin_vec_lvebx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVEBX);
12107 def_builtin ("__builtin_vec_lvehx", v8hi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVEHX);
12108 def_builtin ("__builtin_vec_lvewx", v4si_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVEWX);
12109 def_builtin ("__builtin_vec_st", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_ST);
12110 def_builtin ("__builtin_vec_ste", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_STE);
12111 def_builtin ("__builtin_vec_stl", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_STL);
12112 def_builtin ("__builtin_vec_stvewx", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_STVEWX);
12113 def_builtin ("__builtin_vec_stvebx", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_STVEBX);
12114 def_builtin ("__builtin_vec_stvehx", void_ftype_opaque_long_pvoid, ALTIVEC_BUILTIN_VEC_STVEHX);
12116 def_builtin ("__builtin_vsx_lxvd2x_v2df", v2df_ftype_long_pcvoid,
12117 VSX_BUILTIN_LXVD2X_V2DF);
12118 def_builtin ("__builtin_vsx_lxvd2x_v2di", v2di_ftype_long_pcvoid,
12119 VSX_BUILTIN_LXVD2X_V2DI);
12120 def_builtin ("__builtin_vsx_lxvw4x_v4sf", v4sf_ftype_long_pcvoid,
12121 VSX_BUILTIN_LXVW4X_V4SF);
12122 def_builtin ("__builtin_vsx_lxvw4x_v4si", v4si_ftype_long_pcvoid,
12123 VSX_BUILTIN_LXVW4X_V4SI);
12124 def_builtin ("__builtin_vsx_lxvw4x_v8hi", v8hi_ftype_long_pcvoid,
12125 VSX_BUILTIN_LXVW4X_V8HI);
12126 def_builtin ("__builtin_vsx_lxvw4x_v16qi", v16qi_ftype_long_pcvoid,
12127 VSX_BUILTIN_LXVW4X_V16QI);
12128 def_builtin ("__builtin_vsx_stxvd2x_v2df", void_ftype_v2df_long_pvoid,
12129 VSX_BUILTIN_STXVD2X_V2DF);
12130 def_builtin ("__builtin_vsx_stxvd2x_v2di", void_ftype_v2di_long_pvoid,
12131 VSX_BUILTIN_STXVD2X_V2DI);
12132 def_builtin ("__builtin_vsx_stxvw4x_v4sf", void_ftype_v4sf_long_pvoid,
12133 VSX_BUILTIN_STXVW4X_V4SF);
12134 def_builtin ("__builtin_vsx_stxvw4x_v4si", void_ftype_v4si_long_pvoid,
12135 VSX_BUILTIN_STXVW4X_V4SI);
12136 def_builtin ("__builtin_vsx_stxvw4x_v8hi", void_ftype_v8hi_long_pvoid,
12137 VSX_BUILTIN_STXVW4X_V8HI);
12138 def_builtin ("__builtin_vsx_stxvw4x_v16qi", void_ftype_v16qi_long_pvoid,
12139 VSX_BUILTIN_STXVW4X_V16QI);
12140 def_builtin ("__builtin_vec_vsx_ld", opaque_ftype_long_pcvoid,
12141 VSX_BUILTIN_VEC_LD);
12142 def_builtin ("__builtin_vec_vsx_st", void_ftype_opaque_long_pvoid,
12143 VSX_BUILTIN_VEC_ST);
12145 def_builtin ("__builtin_vec_step", int_ftype_opaque, ALTIVEC_BUILTIN_VEC_STEP);
12146 def_builtin ("__builtin_vec_splats", opaque_ftype_opaque, ALTIVEC_BUILTIN_VEC_SPLATS);
12147 def_builtin ("__builtin_vec_promote", opaque_ftype_opaque, ALTIVEC_BUILTIN_VEC_PROMOTE);
12149 def_builtin ("__builtin_vec_sld", opaque_ftype_opaque_opaque_int, ALTIVEC_BUILTIN_VEC_SLD);
12150 def_builtin ("__builtin_vec_splat", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_SPLAT);
12151 def_builtin ("__builtin_vec_extract", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_EXTRACT);
12152 def_builtin ("__builtin_vec_insert", opaque_ftype_opaque_opaque_int, ALTIVEC_BUILTIN_VEC_INSERT);
12153 def_builtin ("__builtin_vec_vspltw", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_VSPLTW);
12154 def_builtin ("__builtin_vec_vsplth", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_VSPLTH);
12155 def_builtin ("__builtin_vec_vspltb", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_VSPLTB);
12156 def_builtin ("__builtin_vec_ctf", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_CTF);
12157 def_builtin ("__builtin_vec_vcfsx", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_VCFSX);
12158 def_builtin ("__builtin_vec_vcfux", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_VCFUX);
12159 def_builtin ("__builtin_vec_cts", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_CTS);
12160 def_builtin ("__builtin_vec_ctu", opaque_ftype_opaque_int, ALTIVEC_BUILTIN_VEC_CTU);
12162 /* Cell builtins. */
12163 def_builtin ("__builtin_altivec_lvlx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVLX);
12164 def_builtin ("__builtin_altivec_lvlxl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVLXL);
12165 def_builtin ("__builtin_altivec_lvrx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVRX);
12166 def_builtin ("__builtin_altivec_lvrxl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVRXL);
12168 def_builtin ("__builtin_vec_lvlx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVLX);
12169 def_builtin ("__builtin_vec_lvlxl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVLXL);
12170 def_builtin ("__builtin_vec_lvrx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVRX);
12171 def_builtin ("__builtin_vec_lvrxl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_VEC_LVRXL);
12173 def_builtin ("__builtin_altivec_stvlx", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_STVLX);
12174 def_builtin ("__builtin_altivec_stvlxl", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_STVLXL);
12175 def_builtin ("__builtin_altivec_stvrx", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_STVRX);
12176 def_builtin ("__builtin_altivec_stvrxl", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_STVRXL);
12178 def_builtin ("__builtin_vec_stvlx", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_VEC_STVLX);
12179 def_builtin ("__builtin_vec_stvlxl", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_VEC_STVLXL);
12180 def_builtin ("__builtin_vec_stvrx", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_VEC_STVRX);
12181 def_builtin ("__builtin_vec_stvrxl", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_VEC_STVRXL);
12183 /* Add the DST variants. */
12184 d = bdesc_dst;
12185 for (i = 0; i < ARRAY_SIZE (bdesc_dst); i++, d++)
12186 def_builtin (d->name, void_ftype_pcvoid_int_int, d->code);
12188 /* Initialize the predicates. */
12189 d = bdesc_altivec_preds;
12190 for (i = 0; i < ARRAY_SIZE (bdesc_altivec_preds); i++, d++)
12192 enum machine_mode mode1;
12193 tree type;
12195 if (rs6000_overloaded_builtin_p (d->code))
12196 mode1 = VOIDmode;
12197 else
12198 mode1 = insn_data[d->icode].operand[1].mode;
12200 switch (mode1)
12202 case VOIDmode:
12203 type = int_ftype_int_opaque_opaque;
12204 break;
12205 case V4SImode:
12206 type = int_ftype_int_v4si_v4si;
12207 break;
12208 case V8HImode:
12209 type = int_ftype_int_v8hi_v8hi;
12210 break;
12211 case V16QImode:
12212 type = int_ftype_int_v16qi_v16qi;
12213 break;
12214 case V4SFmode:
12215 type = int_ftype_int_v4sf_v4sf;
12216 break;
12217 case V2DFmode:
12218 type = int_ftype_int_v2df_v2df;
12219 break;
12220 default:
12221 gcc_unreachable ();
12224 def_builtin (d->name, type, d->code);
12227 /* Initialize the abs* operators. */
12228 d = bdesc_abs;
12229 for (i = 0; i < ARRAY_SIZE (bdesc_abs); i++, d++)
12231 enum machine_mode mode0;
12232 tree type;
12234 mode0 = insn_data[d->icode].operand[0].mode;
12236 switch (mode0)
12238 case V4SImode:
12239 type = v4si_ftype_v4si;
12240 break;
12241 case V8HImode:
12242 type = v8hi_ftype_v8hi;
12243 break;
12244 case V16QImode:
12245 type = v16qi_ftype_v16qi;
12246 break;
12247 case V4SFmode:
12248 type = v4sf_ftype_v4sf;
12249 break;
12250 case V2DFmode:
12251 type = v2df_ftype_v2df;
12252 break;
12253 default:
12254 gcc_unreachable ();
12257 def_builtin (d->name, type, d->code);
12260 /* Initialize target builtin that implements
12261 targetm.vectorize.builtin_mask_for_load. */
12263 decl = add_builtin_function ("__builtin_altivec_mask_for_load",
12264 v16qi_ftype_long_pcvoid,
12265 ALTIVEC_BUILTIN_MASK_FOR_LOAD,
12266 BUILT_IN_MD, NULL, NULL_TREE);
12267 TREE_READONLY (decl) = 1;
12268 /* Record the decl. Will be used by rs6000_builtin_mask_for_load. */
12269 altivec_builtin_mask_for_load = decl;
12271 /* Access to the vec_init patterns. */
12272 ftype = build_function_type_list (V4SI_type_node, integer_type_node,
12273 integer_type_node, integer_type_node,
12274 integer_type_node, NULL_TREE);
12275 def_builtin ("__builtin_vec_init_v4si", ftype, ALTIVEC_BUILTIN_VEC_INIT_V4SI);
12277 ftype = build_function_type_list (V8HI_type_node, short_integer_type_node,
12278 short_integer_type_node,
12279 short_integer_type_node,
12280 short_integer_type_node,
12281 short_integer_type_node,
12282 short_integer_type_node,
12283 short_integer_type_node,
12284 short_integer_type_node, NULL_TREE);
12285 def_builtin ("__builtin_vec_init_v8hi", ftype, ALTIVEC_BUILTIN_VEC_INIT_V8HI);
12287 ftype = build_function_type_list (V16QI_type_node, char_type_node,
12288 char_type_node, char_type_node,
12289 char_type_node, char_type_node,
12290 char_type_node, char_type_node,
12291 char_type_node, char_type_node,
12292 char_type_node, char_type_node,
12293 char_type_node, char_type_node,
12294 char_type_node, char_type_node,
12295 char_type_node, NULL_TREE);
12296 def_builtin ("__builtin_vec_init_v16qi", ftype,
12297 ALTIVEC_BUILTIN_VEC_INIT_V16QI);
12299 ftype = build_function_type_list (V4SF_type_node, float_type_node,
12300 float_type_node, float_type_node,
12301 float_type_node, NULL_TREE);
12302 def_builtin ("__builtin_vec_init_v4sf", ftype, ALTIVEC_BUILTIN_VEC_INIT_V4SF);
12304 /* VSX builtins. */
12305 ftype = build_function_type_list (V2DF_type_node, double_type_node,
12306 double_type_node, NULL_TREE);
12307 def_builtin ("__builtin_vec_init_v2df", ftype, VSX_BUILTIN_VEC_INIT_V2DF);
12309 ftype = build_function_type_list (V2DI_type_node, intDI_type_node,
12310 intDI_type_node, NULL_TREE);
12311 def_builtin ("__builtin_vec_init_v2di", ftype, VSX_BUILTIN_VEC_INIT_V2DI);
12313 /* Access to the vec_set patterns. */
12314 ftype = build_function_type_list (V4SI_type_node, V4SI_type_node,
12315 intSI_type_node,
12316 integer_type_node, NULL_TREE);
12317 def_builtin ("__builtin_vec_set_v4si", ftype, ALTIVEC_BUILTIN_VEC_SET_V4SI);
12319 ftype = build_function_type_list (V8HI_type_node, V8HI_type_node,
12320 intHI_type_node,
12321 integer_type_node, NULL_TREE);
12322 def_builtin ("__builtin_vec_set_v8hi", ftype, ALTIVEC_BUILTIN_VEC_SET_V8HI);
12324 ftype = build_function_type_list (V16QI_type_node, V16QI_type_node,
12325 intQI_type_node,
12326 integer_type_node, NULL_TREE);
12327 def_builtin ("__builtin_vec_set_v16qi", ftype, ALTIVEC_BUILTIN_VEC_SET_V16QI);
12329 ftype = build_function_type_list (V4SF_type_node, V4SF_type_node,
12330 float_type_node,
12331 integer_type_node, NULL_TREE);
12332 def_builtin ("__builtin_vec_set_v4sf", ftype, ALTIVEC_BUILTIN_VEC_SET_V4SF);
12334 ftype = build_function_type_list (V2DF_type_node, V2DF_type_node,
12335 double_type_node,
12336 integer_type_node, NULL_TREE);
12337 def_builtin ("__builtin_vec_set_v2df", ftype, VSX_BUILTIN_VEC_SET_V2DF);
12339 ftype = build_function_type_list (V2DI_type_node, V2DI_type_node,
12340 intDI_type_node,
12341 integer_type_node, NULL_TREE);
12342 def_builtin ("__builtin_vec_set_v2di", ftype, VSX_BUILTIN_VEC_SET_V2DI);
12344 /* Access to the vec_extract patterns. */
12345 ftype = build_function_type_list (intSI_type_node, V4SI_type_node,
12346 integer_type_node, NULL_TREE);
12347 def_builtin ("__builtin_vec_ext_v4si", ftype, ALTIVEC_BUILTIN_VEC_EXT_V4SI);
12349 ftype = build_function_type_list (intHI_type_node, V8HI_type_node,
12350 integer_type_node, NULL_TREE);
12351 def_builtin ("__builtin_vec_ext_v8hi", ftype, ALTIVEC_BUILTIN_VEC_EXT_V8HI);
12353 ftype = build_function_type_list (intQI_type_node, V16QI_type_node,
12354 integer_type_node, NULL_TREE);
12355 def_builtin ("__builtin_vec_ext_v16qi", ftype, ALTIVEC_BUILTIN_VEC_EXT_V16QI);
12357 ftype = build_function_type_list (float_type_node, V4SF_type_node,
12358 integer_type_node, NULL_TREE);
12359 def_builtin ("__builtin_vec_ext_v4sf", ftype, ALTIVEC_BUILTIN_VEC_EXT_V4SF);
12361 ftype = build_function_type_list (double_type_node, V2DF_type_node,
12362 integer_type_node, NULL_TREE);
12363 def_builtin ("__builtin_vec_ext_v2df", ftype, VSX_BUILTIN_VEC_EXT_V2DF);
12365 ftype = build_function_type_list (intDI_type_node, V2DI_type_node,
12366 integer_type_node, NULL_TREE);
12367 def_builtin ("__builtin_vec_ext_v2di", ftype, VSX_BUILTIN_VEC_EXT_V2DI);
12370 /* Hash function for builtin functions with up to 3 arguments and a return
12371 type. */
12372 static unsigned
12373 builtin_hash_function (const void *hash_entry)
12375 unsigned ret = 0;
12376 int i;
12377 const struct builtin_hash_struct *bh =
12378 (const struct builtin_hash_struct *) hash_entry;
12380 for (i = 0; i < 4; i++)
12382 ret = (ret * (unsigned)MAX_MACHINE_MODE) + ((unsigned)bh->mode[i]);
12383 ret = (ret * 2) + bh->uns_p[i];
12386 return ret;
12389 /* Compare builtin hash entries H1 and H2 for equivalence. */
12390 static int
12391 builtin_hash_eq (const void *h1, const void *h2)
12393 const struct builtin_hash_struct *p1 = (const struct builtin_hash_struct *) h1;
12394 const struct builtin_hash_struct *p2 = (const struct builtin_hash_struct *) h2;
12396 return ((p1->mode[0] == p2->mode[0])
12397 && (p1->mode[1] == p2->mode[1])
12398 && (p1->mode[2] == p2->mode[2])
12399 && (p1->mode[3] == p2->mode[3])
12400 && (p1->uns_p[0] == p2->uns_p[0])
12401 && (p1->uns_p[1] == p2->uns_p[1])
12402 && (p1->uns_p[2] == p2->uns_p[2])
12403 && (p1->uns_p[3] == p2->uns_p[3]));
12406 /* Map types for builtin functions with an explicit return type and up to 3
12407 arguments. Functions with fewer than 3 arguments use VOIDmode as the type
12408 of the argument. */
12409 static tree
12410 builtin_function_type (enum machine_mode mode_ret, enum machine_mode mode_arg0,
12411 enum machine_mode mode_arg1, enum machine_mode mode_arg2,
12412 enum rs6000_builtins builtin, const char *name)
12414 struct builtin_hash_struct h;
12415 struct builtin_hash_struct *h2;
12416 void **found;
12417 int num_args = 3;
12418 int i;
12419 tree ret_type = NULL_TREE;
12420 tree arg_type[3] = { NULL_TREE, NULL_TREE, NULL_TREE };
12422 /* Create builtin_hash_table. */
12423 if (builtin_hash_table == NULL)
12424 builtin_hash_table = htab_create_ggc (1500, builtin_hash_function,
12425 builtin_hash_eq, NULL);
12427 h.type = NULL_TREE;
12428 h.mode[0] = mode_ret;
12429 h.mode[1] = mode_arg0;
12430 h.mode[2] = mode_arg1;
12431 h.mode[3] = mode_arg2;
12432 h.uns_p[0] = 0;
12433 h.uns_p[1] = 0;
12434 h.uns_p[2] = 0;
12435 h.uns_p[3] = 0;
12437 /* If the builtin is a type that produces unsigned results or takes unsigned
12438 arguments, and it is returned as a decl for the vectorizer (such as
12439 widening multiplies, permute), make sure the arguments and return value
12440 are type correct. */
12441 switch (builtin)
12443 /* unsigned 2 argument functions. */
12444 case ALTIVEC_BUILTIN_VMULEUB_UNS:
12445 case ALTIVEC_BUILTIN_VMULEUH_UNS:
12446 case ALTIVEC_BUILTIN_VMULOUB_UNS:
12447 case ALTIVEC_BUILTIN_VMULOUH_UNS:
12448 h.uns_p[0] = 1;
12449 h.uns_p[1] = 1;
12450 h.uns_p[2] = 1;
12451 break;
12453 /* unsigned 3 argument functions. */
12454 case ALTIVEC_BUILTIN_VPERM_16QI_UNS:
12455 case ALTIVEC_BUILTIN_VPERM_8HI_UNS:
12456 case ALTIVEC_BUILTIN_VPERM_4SI_UNS:
12457 case ALTIVEC_BUILTIN_VPERM_2DI_UNS:
12458 case ALTIVEC_BUILTIN_VSEL_16QI_UNS:
12459 case ALTIVEC_BUILTIN_VSEL_8HI_UNS:
12460 case ALTIVEC_BUILTIN_VSEL_4SI_UNS:
12461 case ALTIVEC_BUILTIN_VSEL_2DI_UNS:
12462 case VSX_BUILTIN_VPERM_16QI_UNS:
12463 case VSX_BUILTIN_VPERM_8HI_UNS:
12464 case VSX_BUILTIN_VPERM_4SI_UNS:
12465 case VSX_BUILTIN_VPERM_2DI_UNS:
12466 case VSX_BUILTIN_XXSEL_16QI_UNS:
12467 case VSX_BUILTIN_XXSEL_8HI_UNS:
12468 case VSX_BUILTIN_XXSEL_4SI_UNS:
12469 case VSX_BUILTIN_XXSEL_2DI_UNS:
12470 h.uns_p[0] = 1;
12471 h.uns_p[1] = 1;
12472 h.uns_p[2] = 1;
12473 h.uns_p[3] = 1;
12474 break;
12476 /* signed permute functions with unsigned char mask. */
12477 case ALTIVEC_BUILTIN_VPERM_16QI:
12478 case ALTIVEC_BUILTIN_VPERM_8HI:
12479 case ALTIVEC_BUILTIN_VPERM_4SI:
12480 case ALTIVEC_BUILTIN_VPERM_4SF:
12481 case ALTIVEC_BUILTIN_VPERM_2DI:
12482 case ALTIVEC_BUILTIN_VPERM_2DF:
12483 case VSX_BUILTIN_VPERM_16QI:
12484 case VSX_BUILTIN_VPERM_8HI:
12485 case VSX_BUILTIN_VPERM_4SI:
12486 case VSX_BUILTIN_VPERM_4SF:
12487 case VSX_BUILTIN_VPERM_2DI:
12488 case VSX_BUILTIN_VPERM_2DF:
12489 h.uns_p[3] = 1;
12490 break;
12492 /* unsigned args, signed return. */
12493 case VSX_BUILTIN_XVCVUXDDP_UNS:
12494 case ALTIVEC_BUILTIN_UNSFLOAT_V4SI_V4SF:
12495 h.uns_p[1] = 1;
12496 break;
12498 /* signed args, unsigned return. */
12499 case VSX_BUILTIN_XVCVDPUXDS_UNS:
12500 case ALTIVEC_BUILTIN_FIXUNS_V4SF_V4SI:
12501 h.uns_p[0] = 1;
12502 break;
12504 default:
12505 break;
12508 /* Figure out how many args are present. */
12509 while (num_args > 0 && h.mode[num_args] == VOIDmode)
12510 num_args--;
12512 if (num_args == 0)
12513 fatal_error ("internal error: builtin function %s had no type", name);
12515 ret_type = builtin_mode_to_type[h.mode[0]][h.uns_p[0]];
12516 if (!ret_type && h.uns_p[0])
12517 ret_type = builtin_mode_to_type[h.mode[0]][0];
12519 if (!ret_type)
12520 fatal_error ("internal error: builtin function %s had an unexpected "
12521 "return type %s", name, GET_MODE_NAME (h.mode[0]));
12523 for (i = 0; i < (int) ARRAY_SIZE (arg_type); i++)
12524 arg_type[i] = NULL_TREE;
12526 for (i = 0; i < num_args; i++)
12528 int m = (int) h.mode[i+1];
12529 int uns_p = h.uns_p[i+1];
12531 arg_type[i] = builtin_mode_to_type[m][uns_p];
12532 if (!arg_type[i] && uns_p)
12533 arg_type[i] = builtin_mode_to_type[m][0];
12535 if (!arg_type[i])
12536 fatal_error ("internal error: builtin function %s, argument %d "
12537 "had unexpected argument type %s", name, i,
12538 GET_MODE_NAME (m));
12541 found = htab_find_slot (builtin_hash_table, &h, INSERT);
12542 if (*found == NULL)
12544 h2 = ggc_alloc_builtin_hash_struct ();
12545 *h2 = h;
12546 *found = (void *)h2;
12548 h2->type = build_function_type_list (ret_type, arg_type[0], arg_type[1],
12549 arg_type[2], NULL_TREE);
12552 return ((struct builtin_hash_struct *)(*found))->type;
12555 static void
12556 rs6000_common_init_builtins (void)
12558 const struct builtin_description *d;
12559 size_t i;
12561 tree opaque_ftype_opaque = NULL_TREE;
12562 tree opaque_ftype_opaque_opaque = NULL_TREE;
12563 tree opaque_ftype_opaque_opaque_opaque = NULL_TREE;
12564 tree v2si_ftype_qi = NULL_TREE;
12565 tree v2si_ftype_v2si_qi = NULL_TREE;
12566 tree v2si_ftype_int_qi = NULL_TREE;
12567 HOST_WIDE_INT builtin_mask = rs6000_builtin_mask;
12569 if (!TARGET_PAIRED_FLOAT)
12571 builtin_mode_to_type[V2SImode][0] = opaque_V2SI_type_node;
12572 builtin_mode_to_type[V2SFmode][0] = opaque_V2SF_type_node;
12575 /* Paired and SPE builtins are only available if you build a compiler with
12576 the appropriate options, so only create those builtins with the
12577 appropriate compiler option. Create Altivec and VSX builtins on machines
12578 with at least the general purpose extensions (970 and newer) to allow the
12579 use of the target attribute.. */
12581 if (TARGET_EXTRA_BUILTINS)
12582 builtin_mask |= RS6000_BTM_COMMON;
12584 /* Add the ternary operators. */
12585 d = bdesc_3arg;
12586 for (i = 0; i < ARRAY_SIZE (bdesc_3arg); i++, d++)
12588 tree type;
12589 HOST_WIDE_INT mask = d->mask;
12591 if ((mask & builtin_mask) != mask)
12593 if (TARGET_DEBUG_BUILTIN)
12594 fprintf (stderr, "rs6000_builtin, skip ternary %s\n", d->name);
12595 continue;
12598 if (rs6000_overloaded_builtin_p (d->code))
12600 if (! (type = opaque_ftype_opaque_opaque_opaque))
12601 type = opaque_ftype_opaque_opaque_opaque
12602 = build_function_type_list (opaque_V4SI_type_node,
12603 opaque_V4SI_type_node,
12604 opaque_V4SI_type_node,
12605 opaque_V4SI_type_node,
12606 NULL_TREE);
12608 else
12610 enum insn_code icode = d->icode;
12611 if (d->name == 0 || icode == CODE_FOR_nothing)
12612 continue;
12614 type = builtin_function_type (insn_data[icode].operand[0].mode,
12615 insn_data[icode].operand[1].mode,
12616 insn_data[icode].operand[2].mode,
12617 insn_data[icode].operand[3].mode,
12618 d->code, d->name);
12621 def_builtin (d->name, type, d->code);
12624 /* Add the binary operators. */
12625 d = bdesc_2arg;
12626 for (i = 0; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
12628 enum machine_mode mode0, mode1, mode2;
12629 tree type;
12630 HOST_WIDE_INT mask = d->mask;
12632 if ((mask & builtin_mask) != mask)
12634 if (TARGET_DEBUG_BUILTIN)
12635 fprintf (stderr, "rs6000_builtin, skip binary %s\n", d->name);
12636 continue;
12639 if (rs6000_overloaded_builtin_p (d->code))
12641 if (! (type = opaque_ftype_opaque_opaque))
12642 type = opaque_ftype_opaque_opaque
12643 = build_function_type_list (opaque_V4SI_type_node,
12644 opaque_V4SI_type_node,
12645 opaque_V4SI_type_node,
12646 NULL_TREE);
12648 else
12650 enum insn_code icode = d->icode;
12651 if (d->name == 0 || icode == CODE_FOR_nothing)
12652 continue;
12654 mode0 = insn_data[icode].operand[0].mode;
12655 mode1 = insn_data[icode].operand[1].mode;
12656 mode2 = insn_data[icode].operand[2].mode;
12658 if (mode0 == V2SImode && mode1 == V2SImode && mode2 == QImode)
12660 if (! (type = v2si_ftype_v2si_qi))
12661 type = v2si_ftype_v2si_qi
12662 = build_function_type_list (opaque_V2SI_type_node,
12663 opaque_V2SI_type_node,
12664 char_type_node,
12665 NULL_TREE);
12668 else if (mode0 == V2SImode && GET_MODE_CLASS (mode1) == MODE_INT
12669 && mode2 == QImode)
12671 if (! (type = v2si_ftype_int_qi))
12672 type = v2si_ftype_int_qi
12673 = build_function_type_list (opaque_V2SI_type_node,
12674 integer_type_node,
12675 char_type_node,
12676 NULL_TREE);
12679 else
12680 type = builtin_function_type (mode0, mode1, mode2, VOIDmode,
12681 d->code, d->name);
12684 def_builtin (d->name, type, d->code);
12687 /* Add the simple unary operators. */
12688 d = bdesc_1arg;
12689 for (i = 0; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
12691 enum machine_mode mode0, mode1;
12692 tree type;
12693 HOST_WIDE_INT mask = d->mask;
12695 if ((mask & builtin_mask) != mask)
12697 if (TARGET_DEBUG_BUILTIN)
12698 fprintf (stderr, "rs6000_builtin, skip unary %s\n", d->name);
12699 continue;
12702 if (rs6000_overloaded_builtin_p (d->code))
12704 if (! (type = opaque_ftype_opaque))
12705 type = opaque_ftype_opaque
12706 = build_function_type_list (opaque_V4SI_type_node,
12707 opaque_V4SI_type_node,
12708 NULL_TREE);
12710 else
12712 enum insn_code icode = d->icode;
12713 if (d->name == 0 || icode == CODE_FOR_nothing)
12714 continue;
12716 mode0 = insn_data[icode].operand[0].mode;
12717 mode1 = insn_data[icode].operand[1].mode;
12719 if (mode0 == V2SImode && mode1 == QImode)
12721 if (! (type = v2si_ftype_qi))
12722 type = v2si_ftype_qi
12723 = build_function_type_list (opaque_V2SI_type_node,
12724 char_type_node,
12725 NULL_TREE);
12728 else
12729 type = builtin_function_type (mode0, mode1, VOIDmode, VOIDmode,
12730 d->code, d->name);
12733 def_builtin (d->name, type, d->code);
12737 static void
12738 rs6000_init_libfuncs (void)
12740 if (!TARGET_IEEEQUAD)
12741 /* AIX/Darwin/64-bit Linux quad floating point routines. */
12742 if (!TARGET_XL_COMPAT)
12744 set_optab_libfunc (add_optab, TFmode, "__gcc_qadd");
12745 set_optab_libfunc (sub_optab, TFmode, "__gcc_qsub");
12746 set_optab_libfunc (smul_optab, TFmode, "__gcc_qmul");
12747 set_optab_libfunc (sdiv_optab, TFmode, "__gcc_qdiv");
12749 if (!(TARGET_HARD_FLOAT && (TARGET_FPRS || TARGET_E500_DOUBLE)))
12751 set_optab_libfunc (neg_optab, TFmode, "__gcc_qneg");
12752 set_optab_libfunc (eq_optab, TFmode, "__gcc_qeq");
12753 set_optab_libfunc (ne_optab, TFmode, "__gcc_qne");
12754 set_optab_libfunc (gt_optab, TFmode, "__gcc_qgt");
12755 set_optab_libfunc (ge_optab, TFmode, "__gcc_qge");
12756 set_optab_libfunc (lt_optab, TFmode, "__gcc_qlt");
12757 set_optab_libfunc (le_optab, TFmode, "__gcc_qle");
12759 set_conv_libfunc (sext_optab, TFmode, SFmode, "__gcc_stoq");
12760 set_conv_libfunc (sext_optab, TFmode, DFmode, "__gcc_dtoq");
12761 set_conv_libfunc (trunc_optab, SFmode, TFmode, "__gcc_qtos");
12762 set_conv_libfunc (trunc_optab, DFmode, TFmode, "__gcc_qtod");
12763 set_conv_libfunc (sfix_optab, SImode, TFmode, "__gcc_qtoi");
12764 set_conv_libfunc (ufix_optab, SImode, TFmode, "__gcc_qtou");
12765 set_conv_libfunc (sfloat_optab, TFmode, SImode, "__gcc_itoq");
12766 set_conv_libfunc (ufloat_optab, TFmode, SImode, "__gcc_utoq");
12769 if (!(TARGET_HARD_FLOAT && TARGET_FPRS))
12770 set_optab_libfunc (unord_optab, TFmode, "__gcc_qunord");
12772 else
12774 set_optab_libfunc (add_optab, TFmode, "_xlqadd");
12775 set_optab_libfunc (sub_optab, TFmode, "_xlqsub");
12776 set_optab_libfunc (smul_optab, TFmode, "_xlqmul");
12777 set_optab_libfunc (sdiv_optab, TFmode, "_xlqdiv");
12779 else
12781 /* 32-bit SVR4 quad floating point routines. */
12783 set_optab_libfunc (add_optab, TFmode, "_q_add");
12784 set_optab_libfunc (sub_optab, TFmode, "_q_sub");
12785 set_optab_libfunc (neg_optab, TFmode, "_q_neg");
12786 set_optab_libfunc (smul_optab, TFmode, "_q_mul");
12787 set_optab_libfunc (sdiv_optab, TFmode, "_q_div");
12788 if (TARGET_PPC_GPOPT)
12789 set_optab_libfunc (sqrt_optab, TFmode, "_q_sqrt");
12791 set_optab_libfunc (eq_optab, TFmode, "_q_feq");
12792 set_optab_libfunc (ne_optab, TFmode, "_q_fne");
12793 set_optab_libfunc (gt_optab, TFmode, "_q_fgt");
12794 set_optab_libfunc (ge_optab, TFmode, "_q_fge");
12795 set_optab_libfunc (lt_optab, TFmode, "_q_flt");
12796 set_optab_libfunc (le_optab, TFmode, "_q_fle");
12798 set_conv_libfunc (sext_optab, TFmode, SFmode, "_q_stoq");
12799 set_conv_libfunc (sext_optab, TFmode, DFmode, "_q_dtoq");
12800 set_conv_libfunc (trunc_optab, SFmode, TFmode, "_q_qtos");
12801 set_conv_libfunc (trunc_optab, DFmode, TFmode, "_q_qtod");
12802 set_conv_libfunc (sfix_optab, SImode, TFmode, "_q_qtoi");
12803 set_conv_libfunc (ufix_optab, SImode, TFmode, "_q_qtou");
12804 set_conv_libfunc (sfloat_optab, TFmode, SImode, "_q_itoq");
12805 set_conv_libfunc (ufloat_optab, TFmode, SImode, "_q_utoq");
12810 /* Expand a block clear operation, and return 1 if successful. Return 0
12811 if we should let the compiler generate normal code.
12813 operands[0] is the destination
12814 operands[1] is the length
12815 operands[3] is the alignment */
12818 expand_block_clear (rtx operands[])
12820 rtx orig_dest = operands[0];
12821 rtx bytes_rtx = operands[1];
12822 rtx align_rtx = operands[3];
12823 bool constp = (GET_CODE (bytes_rtx) == CONST_INT);
12824 HOST_WIDE_INT align;
12825 HOST_WIDE_INT bytes;
12826 int offset;
12827 int clear_bytes;
12828 int clear_step;
12830 /* If this is not a fixed size move, just call memcpy */
12831 if (! constp)
12832 return 0;
12834 /* This must be a fixed size alignment */
12835 gcc_assert (GET_CODE (align_rtx) == CONST_INT);
12836 align = INTVAL (align_rtx) * BITS_PER_UNIT;
12838 /* Anything to clear? */
12839 bytes = INTVAL (bytes_rtx);
12840 if (bytes <= 0)
12841 return 1;
12843 /* Use the builtin memset after a point, to avoid huge code bloat.
12844 When optimize_size, avoid any significant code bloat; calling
12845 memset is about 4 instructions, so allow for one instruction to
12846 load zero and three to do clearing. */
12847 if (TARGET_ALTIVEC && align >= 128)
12848 clear_step = 16;
12849 else if (TARGET_POWERPC64 && align >= 32)
12850 clear_step = 8;
12851 else if (TARGET_SPE && align >= 64)
12852 clear_step = 8;
12853 else
12854 clear_step = 4;
12856 if (optimize_size && bytes > 3 * clear_step)
12857 return 0;
12858 if (! optimize_size && bytes > 8 * clear_step)
12859 return 0;
12861 for (offset = 0; bytes > 0; offset += clear_bytes, bytes -= clear_bytes)
12863 enum machine_mode mode = BLKmode;
12864 rtx dest;
12866 if (bytes >= 16 && TARGET_ALTIVEC && align >= 128)
12868 clear_bytes = 16;
12869 mode = V4SImode;
12871 else if (bytes >= 8 && TARGET_SPE && align >= 64)
12873 clear_bytes = 8;
12874 mode = V2SImode;
12876 else if (bytes >= 8 && TARGET_POWERPC64
12877 /* 64-bit loads and stores require word-aligned
12878 displacements. */
12879 && (align >= 64 || (!STRICT_ALIGNMENT && align >= 32)))
12881 clear_bytes = 8;
12882 mode = DImode;
12884 else if (bytes >= 4 && (align >= 32 || !STRICT_ALIGNMENT))
12885 { /* move 4 bytes */
12886 clear_bytes = 4;
12887 mode = SImode;
12889 else if (bytes >= 2 && (align >= 16 || !STRICT_ALIGNMENT))
12890 { /* move 2 bytes */
12891 clear_bytes = 2;
12892 mode = HImode;
12894 else /* move 1 byte at a time */
12896 clear_bytes = 1;
12897 mode = QImode;
12900 dest = adjust_address (orig_dest, mode, offset);
12902 emit_move_insn (dest, CONST0_RTX (mode));
12905 return 1;
12909 /* Expand a block move operation, and return 1 if successful. Return 0
12910 if we should let the compiler generate normal code.
12912 operands[0] is the destination
12913 operands[1] is the source
12914 operands[2] is the length
12915 operands[3] is the alignment */
12917 #define MAX_MOVE_REG 4
12920 expand_block_move (rtx operands[])
12922 rtx orig_dest = operands[0];
12923 rtx orig_src = operands[1];
12924 rtx bytes_rtx = operands[2];
12925 rtx align_rtx = operands[3];
12926 int constp = (GET_CODE (bytes_rtx) == CONST_INT);
12927 int align;
12928 int bytes;
12929 int offset;
12930 int move_bytes;
12931 rtx stores[MAX_MOVE_REG];
12932 int num_reg = 0;
12934 /* If this is not a fixed size move, just call memcpy */
12935 if (! constp)
12936 return 0;
12938 /* This must be a fixed size alignment */
12939 gcc_assert (GET_CODE (align_rtx) == CONST_INT);
12940 align = INTVAL (align_rtx) * BITS_PER_UNIT;
12942 /* Anything to move? */
12943 bytes = INTVAL (bytes_rtx);
12944 if (bytes <= 0)
12945 return 1;
12947 if (bytes > rs6000_block_move_inline_limit)
12948 return 0;
12950 for (offset = 0; bytes > 0; offset += move_bytes, bytes -= move_bytes)
12952 union {
12953 rtx (*movmemsi) (rtx, rtx, rtx, rtx);
12954 rtx (*mov) (rtx, rtx);
12955 } gen_func;
12956 enum machine_mode mode = BLKmode;
12957 rtx src, dest;
12959 /* Altivec first, since it will be faster than a string move
12960 when it applies, and usually not significantly larger. */
12961 if (TARGET_ALTIVEC && bytes >= 16 && align >= 128)
12963 move_bytes = 16;
12964 mode = V4SImode;
12965 gen_func.mov = gen_movv4si;
12967 else if (TARGET_SPE && bytes >= 8 && align >= 64)
12969 move_bytes = 8;
12970 mode = V2SImode;
12971 gen_func.mov = gen_movv2si;
12973 else if (TARGET_STRING
12974 && bytes > 24 /* move up to 32 bytes at a time */
12975 && ! fixed_regs[5]
12976 && ! fixed_regs[6]
12977 && ! fixed_regs[7]
12978 && ! fixed_regs[8]
12979 && ! fixed_regs[9]
12980 && ! fixed_regs[10]
12981 && ! fixed_regs[11]
12982 && ! fixed_regs[12])
12984 move_bytes = (bytes > 32) ? 32 : bytes;
12985 gen_func.movmemsi = gen_movmemsi_8reg;
12987 else if (TARGET_STRING
12988 && bytes > 16 /* move up to 24 bytes at a time */
12989 && ! fixed_regs[5]
12990 && ! fixed_regs[6]
12991 && ! fixed_regs[7]
12992 && ! fixed_regs[8]
12993 && ! fixed_regs[9]
12994 && ! fixed_regs[10])
12996 move_bytes = (bytes > 24) ? 24 : bytes;
12997 gen_func.movmemsi = gen_movmemsi_6reg;
12999 else if (TARGET_STRING
13000 && bytes > 8 /* move up to 16 bytes at a time */
13001 && ! fixed_regs[5]
13002 && ! fixed_regs[6]
13003 && ! fixed_regs[7]
13004 && ! fixed_regs[8])
13006 move_bytes = (bytes > 16) ? 16 : bytes;
13007 gen_func.movmemsi = gen_movmemsi_4reg;
13009 else if (bytes >= 8 && TARGET_POWERPC64
13010 /* 64-bit loads and stores require word-aligned
13011 displacements. */
13012 && (align >= 64 || (!STRICT_ALIGNMENT && align >= 32)))
13014 move_bytes = 8;
13015 mode = DImode;
13016 gen_func.mov = gen_movdi;
13018 else if (TARGET_STRING && bytes > 4 && !TARGET_POWERPC64)
13019 { /* move up to 8 bytes at a time */
13020 move_bytes = (bytes > 8) ? 8 : bytes;
13021 gen_func.movmemsi = gen_movmemsi_2reg;
13023 else if (bytes >= 4 && (align >= 32 || !STRICT_ALIGNMENT))
13024 { /* move 4 bytes */
13025 move_bytes = 4;
13026 mode = SImode;
13027 gen_func.mov = gen_movsi;
13029 else if (bytes >= 2 && (align >= 16 || !STRICT_ALIGNMENT))
13030 { /* move 2 bytes */
13031 move_bytes = 2;
13032 mode = HImode;
13033 gen_func.mov = gen_movhi;
13035 else if (TARGET_STRING && bytes > 1)
13036 { /* move up to 4 bytes at a time */
13037 move_bytes = (bytes > 4) ? 4 : bytes;
13038 gen_func.movmemsi = gen_movmemsi_1reg;
13040 else /* move 1 byte at a time */
13042 move_bytes = 1;
13043 mode = QImode;
13044 gen_func.mov = gen_movqi;
13047 src = adjust_address (orig_src, mode, offset);
13048 dest = adjust_address (orig_dest, mode, offset);
13050 if (mode != BLKmode)
13052 rtx tmp_reg = gen_reg_rtx (mode);
13054 emit_insn ((*gen_func.mov) (tmp_reg, src));
13055 stores[num_reg++] = (*gen_func.mov) (dest, tmp_reg);
13058 if (mode == BLKmode || num_reg >= MAX_MOVE_REG || bytes == move_bytes)
13060 int i;
13061 for (i = 0; i < num_reg; i++)
13062 emit_insn (stores[i]);
13063 num_reg = 0;
13066 if (mode == BLKmode)
13068 /* Move the address into scratch registers. The movmemsi
13069 patterns require zero offset. */
13070 if (!REG_P (XEXP (src, 0)))
13072 rtx src_reg = copy_addr_to_reg (XEXP (src, 0));
13073 src = replace_equiv_address (src, src_reg);
13075 set_mem_size (src, move_bytes);
13077 if (!REG_P (XEXP (dest, 0)))
13079 rtx dest_reg = copy_addr_to_reg (XEXP (dest, 0));
13080 dest = replace_equiv_address (dest, dest_reg);
13082 set_mem_size (dest, move_bytes);
13084 emit_insn ((*gen_func.movmemsi) (dest, src,
13085 GEN_INT (move_bytes & 31),
13086 align_rtx));
13090 return 1;
13094 /* Return a string to perform a load_multiple operation.
13095 operands[0] is the vector.
13096 operands[1] is the source address.
13097 operands[2] is the first destination register. */
13099 const char *
13100 rs6000_output_load_multiple (rtx operands[3])
13102 /* We have to handle the case where the pseudo used to contain the address
13103 is assigned to one of the output registers. */
13104 int i, j;
13105 int words = XVECLEN (operands[0], 0);
13106 rtx xop[10];
13108 if (XVECLEN (operands[0], 0) == 1)
13109 return "lwz %2,0(%1)";
13111 for (i = 0; i < words; i++)
13112 if (refers_to_regno_p (REGNO (operands[2]) + i,
13113 REGNO (operands[2]) + i + 1, operands[1], 0))
13115 if (i == words-1)
13117 xop[0] = GEN_INT (4 * (words-1));
13118 xop[1] = operands[1];
13119 xop[2] = operands[2];
13120 output_asm_insn ("lswi %2,%1,%0\n\tlwz %1,%0(%1)", xop);
13121 return "";
13123 else if (i == 0)
13125 xop[0] = GEN_INT (4 * (words-1));
13126 xop[1] = operands[1];
13127 xop[2] = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
13128 output_asm_insn ("addi %1,%1,4\n\tlswi %2,%1,%0\n\tlwz %1,-4(%1)", xop);
13129 return "";
13131 else
13133 for (j = 0; j < words; j++)
13134 if (j != i)
13136 xop[0] = GEN_INT (j * 4);
13137 xop[1] = operands[1];
13138 xop[2] = gen_rtx_REG (SImode, REGNO (operands[2]) + j);
13139 output_asm_insn ("lwz %2,%0(%1)", xop);
13141 xop[0] = GEN_INT (i * 4);
13142 xop[1] = operands[1];
13143 output_asm_insn ("lwz %1,%0(%1)", xop);
13144 return "";
13148 return "lswi %2,%1,%N0";
13152 /* A validation routine: say whether CODE, a condition code, and MODE
13153 match. The other alternatives either don't make sense or should
13154 never be generated. */
13156 void
13157 validate_condition_mode (enum rtx_code code, enum machine_mode mode)
13159 gcc_assert ((GET_RTX_CLASS (code) == RTX_COMPARE
13160 || GET_RTX_CLASS (code) == RTX_COMM_COMPARE)
13161 && GET_MODE_CLASS (mode) == MODE_CC);
13163 /* These don't make sense. */
13164 gcc_assert ((code != GT && code != LT && code != GE && code != LE)
13165 || mode != CCUNSmode);
13167 gcc_assert ((code != GTU && code != LTU && code != GEU && code != LEU)
13168 || mode == CCUNSmode);
13170 gcc_assert (mode == CCFPmode
13171 || (code != ORDERED && code != UNORDERED
13172 && code != UNEQ && code != LTGT
13173 && code != UNGT && code != UNLT
13174 && code != UNGE && code != UNLE));
13176 /* These should never be generated except for
13177 flag_finite_math_only. */
13178 gcc_assert (mode != CCFPmode
13179 || flag_finite_math_only
13180 || (code != LE && code != GE
13181 && code != UNEQ && code != LTGT
13182 && code != UNGT && code != UNLT));
13184 /* These are invalid; the information is not there. */
13185 gcc_assert (mode != CCEQmode || code == EQ || code == NE);
13189 /* Return 1 if ANDOP is a mask that has no bits on that are not in the
13190 mask required to convert the result of a rotate insn into a shift
13191 left insn of SHIFTOP bits. Both are known to be SImode CONST_INT. */
13194 includes_lshift_p (rtx shiftop, rtx andop)
13196 unsigned HOST_WIDE_INT shift_mask = ~(unsigned HOST_WIDE_INT) 0;
13198 shift_mask <<= INTVAL (shiftop);
13200 return (INTVAL (andop) & 0xffffffff & ~shift_mask) == 0;
13203 /* Similar, but for right shift. */
13206 includes_rshift_p (rtx shiftop, rtx andop)
13208 unsigned HOST_WIDE_INT shift_mask = ~(unsigned HOST_WIDE_INT) 0;
13210 shift_mask >>= INTVAL (shiftop);
13212 return (INTVAL (andop) & 0xffffffff & ~shift_mask) == 0;
13215 /* Return 1 if ANDOP is a mask suitable for use with an rldic insn
13216 to perform a left shift. It must have exactly SHIFTOP least
13217 significant 0's, then one or more 1's, then zero or more 0's. */
13220 includes_rldic_lshift_p (rtx shiftop, rtx andop)
13222 if (GET_CODE (andop) == CONST_INT)
13224 HOST_WIDE_INT c, lsb, shift_mask;
13226 c = INTVAL (andop);
13227 if (c == 0 || c == ~0)
13228 return 0;
13230 shift_mask = ~0;
13231 shift_mask <<= INTVAL (shiftop);
13233 /* Find the least significant one bit. */
13234 lsb = c & -c;
13236 /* It must coincide with the LSB of the shift mask. */
13237 if (-lsb != shift_mask)
13238 return 0;
13240 /* Invert to look for the next transition (if any). */
13241 c = ~c;
13243 /* Remove the low group of ones (originally low group of zeros). */
13244 c &= -lsb;
13246 /* Again find the lsb, and check we have all 1's above. */
13247 lsb = c & -c;
13248 return c == -lsb;
13250 else if (GET_CODE (andop) == CONST_DOUBLE
13251 && (GET_MODE (andop) == VOIDmode || GET_MODE (andop) == DImode))
13253 HOST_WIDE_INT low, high, lsb;
13254 HOST_WIDE_INT shift_mask_low, shift_mask_high;
13256 low = CONST_DOUBLE_LOW (andop);
13257 if (HOST_BITS_PER_WIDE_INT < 64)
13258 high = CONST_DOUBLE_HIGH (andop);
13260 if ((low == 0 && (HOST_BITS_PER_WIDE_INT >= 64 || high == 0))
13261 || (low == ~0 && (HOST_BITS_PER_WIDE_INT >= 64 || high == ~0)))
13262 return 0;
13264 if (HOST_BITS_PER_WIDE_INT < 64 && low == 0)
13266 shift_mask_high = ~0;
13267 if (INTVAL (shiftop) > 32)
13268 shift_mask_high <<= INTVAL (shiftop) - 32;
13270 lsb = high & -high;
13272 if (-lsb != shift_mask_high || INTVAL (shiftop) < 32)
13273 return 0;
13275 high = ~high;
13276 high &= -lsb;
13278 lsb = high & -high;
13279 return high == -lsb;
13282 shift_mask_low = ~0;
13283 shift_mask_low <<= INTVAL (shiftop);
13285 lsb = low & -low;
13287 if (-lsb != shift_mask_low)
13288 return 0;
13290 if (HOST_BITS_PER_WIDE_INT < 64)
13291 high = ~high;
13292 low = ~low;
13293 low &= -lsb;
13295 if (HOST_BITS_PER_WIDE_INT < 64 && low == 0)
13297 lsb = high & -high;
13298 return high == -lsb;
13301 lsb = low & -low;
13302 return low == -lsb && (HOST_BITS_PER_WIDE_INT >= 64 || high == ~0);
13304 else
13305 return 0;
13308 /* Return 1 if ANDOP is a mask suitable for use with an rldicr insn
13309 to perform a left shift. It must have SHIFTOP or more least
13310 significant 0's, with the remainder of the word 1's. */
13313 includes_rldicr_lshift_p (rtx shiftop, rtx andop)
13315 if (GET_CODE (andop) == CONST_INT)
13317 HOST_WIDE_INT c, lsb, shift_mask;
13319 shift_mask = ~0;
13320 shift_mask <<= INTVAL (shiftop);
13321 c = INTVAL (andop);
13323 /* Find the least significant one bit. */
13324 lsb = c & -c;
13326 /* It must be covered by the shift mask.
13327 This test also rejects c == 0. */
13328 if ((lsb & shift_mask) == 0)
13329 return 0;
13331 /* Check we have all 1's above the transition, and reject all 1's. */
13332 return c == -lsb && lsb != 1;
13334 else if (GET_CODE (andop) == CONST_DOUBLE
13335 && (GET_MODE (andop) == VOIDmode || GET_MODE (andop) == DImode))
13337 HOST_WIDE_INT low, lsb, shift_mask_low;
13339 low = CONST_DOUBLE_LOW (andop);
13341 if (HOST_BITS_PER_WIDE_INT < 64)
13343 HOST_WIDE_INT high, shift_mask_high;
13345 high = CONST_DOUBLE_HIGH (andop);
13347 if (low == 0)
13349 shift_mask_high = ~0;
13350 if (INTVAL (shiftop) > 32)
13351 shift_mask_high <<= INTVAL (shiftop) - 32;
13353 lsb = high & -high;
13355 if ((lsb & shift_mask_high) == 0)
13356 return 0;
13358 return high == -lsb;
13360 if (high != ~0)
13361 return 0;
13364 shift_mask_low = ~0;
13365 shift_mask_low <<= INTVAL (shiftop);
13367 lsb = low & -low;
13369 if ((lsb & shift_mask_low) == 0)
13370 return 0;
13372 return low == -lsb && lsb != 1;
13374 else
13375 return 0;
13378 /* Return 1 if operands will generate a valid arguments to rlwimi
13379 instruction for insert with right shift in 64-bit mode. The mask may
13380 not start on the first bit or stop on the last bit because wrap-around
13381 effects of instruction do not correspond to semantics of RTL insn. */
13384 insvdi_rshift_rlwimi_p (rtx sizeop, rtx startop, rtx shiftop)
13386 if (INTVAL (startop) > 32
13387 && INTVAL (startop) < 64
13388 && INTVAL (sizeop) > 1
13389 && INTVAL (sizeop) + INTVAL (startop) < 64
13390 && INTVAL (shiftop) > 0
13391 && INTVAL (sizeop) + INTVAL (shiftop) < 32
13392 && (64 - (INTVAL (shiftop) & 63)) >= INTVAL (sizeop))
13393 return 1;
13395 return 0;
13398 /* Return 1 if REGNO (reg1) == REGNO (reg2) - 1 making them candidates
13399 for lfq and stfq insns iff the registers are hard registers. */
13402 registers_ok_for_quad_peep (rtx reg1, rtx reg2)
13404 /* We might have been passed a SUBREG. */
13405 if (GET_CODE (reg1) != REG || GET_CODE (reg2) != REG)
13406 return 0;
13408 /* We might have been passed non floating point registers. */
13409 if (!FP_REGNO_P (REGNO (reg1))
13410 || !FP_REGNO_P (REGNO (reg2)))
13411 return 0;
13413 return (REGNO (reg1) == REGNO (reg2) - 1);
13416 /* Return 1 if addr1 and addr2 are suitable for lfq or stfq insn.
13417 addr1 and addr2 must be in consecutive memory locations
13418 (addr2 == addr1 + 8). */
13421 mems_ok_for_quad_peep (rtx mem1, rtx mem2)
13423 rtx addr1, addr2;
13424 unsigned int reg1, reg2;
13425 int offset1, offset2;
13427 /* The mems cannot be volatile. */
13428 if (MEM_VOLATILE_P (mem1) || MEM_VOLATILE_P (mem2))
13429 return 0;
13431 addr1 = XEXP (mem1, 0);
13432 addr2 = XEXP (mem2, 0);
13434 /* Extract an offset (if used) from the first addr. */
13435 if (GET_CODE (addr1) == PLUS)
13437 /* If not a REG, return zero. */
13438 if (GET_CODE (XEXP (addr1, 0)) != REG)
13439 return 0;
13440 else
13442 reg1 = REGNO (XEXP (addr1, 0));
13443 /* The offset must be constant! */
13444 if (GET_CODE (XEXP (addr1, 1)) != CONST_INT)
13445 return 0;
13446 offset1 = INTVAL (XEXP (addr1, 1));
13449 else if (GET_CODE (addr1) != REG)
13450 return 0;
13451 else
13453 reg1 = REGNO (addr1);
13454 /* This was a simple (mem (reg)) expression. Offset is 0. */
13455 offset1 = 0;
13458 /* And now for the second addr. */
13459 if (GET_CODE (addr2) == PLUS)
13461 /* If not a REG, return zero. */
13462 if (GET_CODE (XEXP (addr2, 0)) != REG)
13463 return 0;
13464 else
13466 reg2 = REGNO (XEXP (addr2, 0));
13467 /* The offset must be constant. */
13468 if (GET_CODE (XEXP (addr2, 1)) != CONST_INT)
13469 return 0;
13470 offset2 = INTVAL (XEXP (addr2, 1));
13473 else if (GET_CODE (addr2) != REG)
13474 return 0;
13475 else
13477 reg2 = REGNO (addr2);
13478 /* This was a simple (mem (reg)) expression. Offset is 0. */
13479 offset2 = 0;
13482 /* Both of these must have the same base register. */
13483 if (reg1 != reg2)
13484 return 0;
13486 /* The offset for the second addr must be 8 more than the first addr. */
13487 if (offset2 != offset1 + 8)
13488 return 0;
13490 /* All the tests passed. addr1 and addr2 are valid for lfq or stfq
13491 instructions. */
13492 return 1;
13497 rs6000_secondary_memory_needed_rtx (enum machine_mode mode)
13499 static bool eliminated = false;
13500 rtx ret;
13502 if (mode != SDmode)
13503 ret = assign_stack_local (mode, GET_MODE_SIZE (mode), 0);
13504 else
13506 rtx mem = cfun->machine->sdmode_stack_slot;
13507 gcc_assert (mem != NULL_RTX);
13509 if (!eliminated)
13511 mem = eliminate_regs (mem, VOIDmode, NULL_RTX);
13512 cfun->machine->sdmode_stack_slot = mem;
13513 eliminated = true;
13515 ret = mem;
13518 if (TARGET_DEBUG_ADDR)
13520 fprintf (stderr, "\nrs6000_secondary_memory_needed_rtx, mode %s, rtx:\n",
13521 GET_MODE_NAME (mode));
13522 if (!ret)
13523 fprintf (stderr, "\tNULL_RTX\n");
13524 else
13525 debug_rtx (ret);
13528 return ret;
13531 static tree
13532 rs6000_check_sdmode (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
13534 /* Don't walk into types. */
13535 if (*tp == NULL_TREE || *tp == error_mark_node || TYPE_P (*tp))
13537 *walk_subtrees = 0;
13538 return NULL_TREE;
13541 switch (TREE_CODE (*tp))
13543 case VAR_DECL:
13544 case PARM_DECL:
13545 case FIELD_DECL:
13546 case RESULT_DECL:
13547 case SSA_NAME:
13548 case REAL_CST:
13549 case MEM_REF:
13550 case VIEW_CONVERT_EXPR:
13551 if (TYPE_MODE (TREE_TYPE (*tp)) == SDmode)
13552 return *tp;
13553 break;
13554 default:
13555 break;
13558 return NULL_TREE;
13561 enum reload_reg_type {
13562 GPR_REGISTER_TYPE,
13563 VECTOR_REGISTER_TYPE,
13564 OTHER_REGISTER_TYPE
13567 static enum reload_reg_type
13568 rs6000_reload_register_type (enum reg_class rclass)
13570 switch (rclass)
13572 case GENERAL_REGS:
13573 case BASE_REGS:
13574 return GPR_REGISTER_TYPE;
13576 case FLOAT_REGS:
13577 case ALTIVEC_REGS:
13578 case VSX_REGS:
13579 return VECTOR_REGISTER_TYPE;
13581 default:
13582 return OTHER_REGISTER_TYPE;
13586 /* Inform reload about cases where moving X with a mode MODE to a register in
13587 RCLASS requires an extra scratch or immediate register. Return the class
13588 needed for the immediate register.
13590 For VSX and Altivec, we may need a register to convert sp+offset into
13591 reg+sp.
13593 For misaligned 64-bit gpr loads and stores we need a register to
13594 convert an offset address to indirect. */
13596 static reg_class_t
13597 rs6000_secondary_reload (bool in_p,
13598 rtx x,
13599 reg_class_t rclass_i,
13600 enum machine_mode mode,
13601 secondary_reload_info *sri)
13603 enum reg_class rclass = (enum reg_class) rclass_i;
13604 reg_class_t ret = ALL_REGS;
13605 enum insn_code icode;
13606 bool default_p = false;
13608 sri->icode = CODE_FOR_nothing;
13610 /* Convert vector loads and stores into gprs to use an additional base
13611 register. */
13612 icode = rs6000_vector_reload[mode][in_p != false];
13613 if (icode != CODE_FOR_nothing)
13615 ret = NO_REGS;
13616 sri->icode = CODE_FOR_nothing;
13617 sri->extra_cost = 0;
13619 if (GET_CODE (x) == MEM)
13621 rtx addr = XEXP (x, 0);
13623 /* Loads to and stores from gprs can do reg+offset, and wouldn't need
13624 an extra register in that case, but it would need an extra
13625 register if the addressing is reg+reg or (reg+reg)&(-16). */
13626 if (rclass == GENERAL_REGS || rclass == BASE_REGS)
13628 if (!legitimate_indirect_address_p (addr, false)
13629 && !rs6000_legitimate_offset_address_p (TImode, addr,
13630 false, true))
13632 sri->icode = icode;
13633 /* account for splitting the loads, and converting the
13634 address from reg+reg to reg. */
13635 sri->extra_cost = (((TARGET_64BIT) ? 3 : 5)
13636 + ((GET_CODE (addr) == AND) ? 1 : 0));
13639 /* Loads to and stores from vector registers can only do reg+reg
13640 addressing. Altivec registers can also do (reg+reg)&(-16). */
13641 else if (rclass == VSX_REGS || rclass == ALTIVEC_REGS
13642 || rclass == FLOAT_REGS || rclass == NO_REGS)
13644 if (!VECTOR_MEM_ALTIVEC_P (mode)
13645 && GET_CODE (addr) == AND
13646 && GET_CODE (XEXP (addr, 1)) == CONST_INT
13647 && INTVAL (XEXP (addr, 1)) == -16
13648 && (legitimate_indirect_address_p (XEXP (addr, 0), false)
13649 || legitimate_indexed_address_p (XEXP (addr, 0), false)))
13651 sri->icode = icode;
13652 sri->extra_cost = ((GET_CODE (XEXP (addr, 0)) == PLUS)
13653 ? 2 : 1);
13655 else if (!legitimate_indirect_address_p (addr, false)
13656 && (rclass == NO_REGS
13657 || !legitimate_indexed_address_p (addr, false)))
13659 sri->icode = icode;
13660 sri->extra_cost = 1;
13662 else
13663 icode = CODE_FOR_nothing;
13665 /* Any other loads, including to pseudo registers which haven't been
13666 assigned to a register yet, default to require a scratch
13667 register. */
13668 else
13670 sri->icode = icode;
13671 sri->extra_cost = 2;
13674 else if (REG_P (x))
13676 int regno = true_regnum (x);
13678 icode = CODE_FOR_nothing;
13679 if (regno < 0 || regno >= FIRST_PSEUDO_REGISTER)
13680 default_p = true;
13681 else
13683 enum reg_class xclass = REGNO_REG_CLASS (regno);
13684 enum reload_reg_type rtype1 = rs6000_reload_register_type (rclass);
13685 enum reload_reg_type rtype2 = rs6000_reload_register_type (xclass);
13687 /* If memory is needed, use default_secondary_reload to create the
13688 stack slot. */
13689 if (rtype1 != rtype2 || rtype1 == OTHER_REGISTER_TYPE)
13690 default_p = true;
13691 else
13692 ret = NO_REGS;
13695 else
13696 default_p = true;
13698 else if (TARGET_POWERPC64
13699 && rs6000_reload_register_type (rclass) == GPR_REGISTER_TYPE
13700 && MEM_P (x)
13701 && GET_MODE_SIZE (GET_MODE (x)) >= UNITS_PER_WORD)
13703 rtx off = address_offset (XEXP (x, 0));
13704 unsigned int extra = GET_MODE_SIZE (GET_MODE (x)) - UNITS_PER_WORD;
13706 if (off != NULL_RTX
13707 && (INTVAL (off) & 3) != 0
13708 && (unsigned HOST_WIDE_INT) INTVAL (off) + 0x8000 < 0x10000 - extra)
13710 if (in_p)
13711 sri->icode = CODE_FOR_reload_di_load;
13712 else
13713 sri->icode = CODE_FOR_reload_di_store;
13714 sri->extra_cost = 2;
13715 ret = NO_REGS;
13717 else
13718 default_p = true;
13720 else if (!TARGET_POWERPC64
13721 && rs6000_reload_register_type (rclass) == GPR_REGISTER_TYPE
13722 && MEM_P (x)
13723 && GET_MODE_SIZE (GET_MODE (x)) > UNITS_PER_WORD)
13725 rtx off = address_offset (XEXP (x, 0));
13726 unsigned int extra = GET_MODE_SIZE (GET_MODE (x)) - UNITS_PER_WORD;
13728 /* We need a secondary reload only when our legitimate_address_p
13729 says the address is good (as otherwise the entire address
13730 will be reloaded). So for mode sizes of 8 and 16 this will
13731 be when the offset is in the ranges [0x7ffc,0x7fff] and
13732 [0x7ff4,0x7ff7] respectively. Note that the address we see
13733 here may have been manipulated by legitimize_reload_address. */
13734 if (off != NULL_RTX
13735 && ((unsigned HOST_WIDE_INT) INTVAL (off) - (0x8000 - extra)
13736 < UNITS_PER_WORD))
13738 if (in_p)
13739 sri->icode = CODE_FOR_reload_si_load;
13740 else
13741 sri->icode = CODE_FOR_reload_si_store;
13742 sri->extra_cost = 2;
13743 ret = NO_REGS;
13745 else
13746 default_p = true;
13748 else
13749 default_p = true;
13751 if (default_p)
13752 ret = default_secondary_reload (in_p, x, rclass, mode, sri);
13754 gcc_assert (ret != ALL_REGS);
13756 if (TARGET_DEBUG_ADDR)
13758 fprintf (stderr,
13759 "\nrs6000_secondary_reload, return %s, in_p = %s, rclass = %s, "
13760 "mode = %s",
13761 reg_class_names[ret],
13762 in_p ? "true" : "false",
13763 reg_class_names[rclass],
13764 GET_MODE_NAME (mode));
13766 if (default_p)
13767 fprintf (stderr, ", default secondary reload");
13769 if (sri->icode != CODE_FOR_nothing)
13770 fprintf (stderr, ", reload func = %s, extra cost = %d\n",
13771 insn_data[sri->icode].name, sri->extra_cost);
13772 else
13773 fprintf (stderr, "\n");
13775 debug_rtx (x);
13778 return ret;
13781 /* Fixup reload addresses for Altivec or VSX loads/stores to change SP+offset
13782 to SP+reg addressing. */
13784 void
13785 rs6000_secondary_reload_inner (rtx reg, rtx mem, rtx scratch, bool store_p)
13787 int regno = true_regnum (reg);
13788 enum machine_mode mode = GET_MODE (reg);
13789 enum reg_class rclass;
13790 rtx addr;
13791 rtx and_op2 = NULL_RTX;
13792 rtx addr_op1;
13793 rtx addr_op2;
13794 rtx scratch_or_premodify = scratch;
13795 rtx and_rtx;
13796 rtx cc_clobber;
13798 if (TARGET_DEBUG_ADDR)
13800 fprintf (stderr, "\nrs6000_secondary_reload_inner, type = %s\n",
13801 store_p ? "store" : "load");
13802 fprintf (stderr, "reg:\n");
13803 debug_rtx (reg);
13804 fprintf (stderr, "mem:\n");
13805 debug_rtx (mem);
13806 fprintf (stderr, "scratch:\n");
13807 debug_rtx (scratch);
13810 gcc_assert (regno >= 0 && regno < FIRST_PSEUDO_REGISTER);
13811 gcc_assert (GET_CODE (mem) == MEM);
13812 rclass = REGNO_REG_CLASS (regno);
13813 addr = XEXP (mem, 0);
13815 switch (rclass)
13817 /* GPRs can handle reg + small constant, all other addresses need to use
13818 the scratch register. */
13819 case GENERAL_REGS:
13820 case BASE_REGS:
13821 if (GET_CODE (addr) == AND)
13823 and_op2 = XEXP (addr, 1);
13824 addr = XEXP (addr, 0);
13827 if (GET_CODE (addr) == PRE_MODIFY)
13829 scratch_or_premodify = XEXP (addr, 0);
13830 gcc_assert (REG_P (scratch_or_premodify));
13831 gcc_assert (GET_CODE (XEXP (addr, 1)) == PLUS);
13832 addr = XEXP (addr, 1);
13835 if (GET_CODE (addr) == PLUS
13836 && (and_op2 != NULL_RTX
13837 || !rs6000_legitimate_offset_address_p (TImode, addr,
13838 false, true)))
13840 addr_op1 = XEXP (addr, 0);
13841 addr_op2 = XEXP (addr, 1);
13842 gcc_assert (legitimate_indirect_address_p (addr_op1, false));
13844 if (!REG_P (addr_op2)
13845 && (GET_CODE (addr_op2) != CONST_INT
13846 || !satisfies_constraint_I (addr_op2)))
13848 if (TARGET_DEBUG_ADDR)
13850 fprintf (stderr,
13851 "\nMove plus addr to register %s, mode = %s: ",
13852 rs6000_reg_names[REGNO (scratch)],
13853 GET_MODE_NAME (mode));
13854 debug_rtx (addr_op2);
13856 rs6000_emit_move (scratch, addr_op2, Pmode);
13857 addr_op2 = scratch;
13860 emit_insn (gen_rtx_SET (VOIDmode,
13861 scratch_or_premodify,
13862 gen_rtx_PLUS (Pmode,
13863 addr_op1,
13864 addr_op2)));
13866 addr = scratch_or_premodify;
13867 scratch_or_premodify = scratch;
13869 else if (!legitimate_indirect_address_p (addr, false)
13870 && !rs6000_legitimate_offset_address_p (TImode, addr,
13871 false, true))
13873 if (TARGET_DEBUG_ADDR)
13875 fprintf (stderr, "\nMove addr to register %s, mode = %s: ",
13876 rs6000_reg_names[REGNO (scratch_or_premodify)],
13877 GET_MODE_NAME (mode));
13878 debug_rtx (addr);
13880 rs6000_emit_move (scratch_or_premodify, addr, Pmode);
13881 addr = scratch_or_premodify;
13882 scratch_or_premodify = scratch;
13884 break;
13886 /* Float/Altivec registers can only handle reg+reg addressing. Move
13887 other addresses into a scratch register. */
13888 case FLOAT_REGS:
13889 case VSX_REGS:
13890 case ALTIVEC_REGS:
13892 /* With float regs, we need to handle the AND ourselves, since we can't
13893 use the Altivec instruction with an implicit AND -16. Allow scalar
13894 loads to float registers to use reg+offset even if VSX. */
13895 if (GET_CODE (addr) == AND
13896 && (rclass != ALTIVEC_REGS || GET_MODE_SIZE (mode) != 16
13897 || GET_CODE (XEXP (addr, 1)) != CONST_INT
13898 || INTVAL (XEXP (addr, 1)) != -16
13899 || !VECTOR_MEM_ALTIVEC_P (mode)))
13901 and_op2 = XEXP (addr, 1);
13902 addr = XEXP (addr, 0);
13905 /* If we aren't using a VSX load, save the PRE_MODIFY register and use it
13906 as the address later. */
13907 if (GET_CODE (addr) == PRE_MODIFY
13908 && (!VECTOR_MEM_VSX_P (mode)
13909 || and_op2 != NULL_RTX
13910 || !legitimate_indexed_address_p (XEXP (addr, 1), false)))
13912 scratch_or_premodify = XEXP (addr, 0);
13913 gcc_assert (legitimate_indirect_address_p (scratch_or_premodify,
13914 false));
13915 gcc_assert (GET_CODE (XEXP (addr, 1)) == PLUS);
13916 addr = XEXP (addr, 1);
13919 if (legitimate_indirect_address_p (addr, false) /* reg */
13920 || legitimate_indexed_address_p (addr, false) /* reg+reg */
13921 || GET_CODE (addr) == PRE_MODIFY /* VSX pre-modify */
13922 || (GET_CODE (addr) == AND /* Altivec memory */
13923 && GET_CODE (XEXP (addr, 1)) == CONST_INT
13924 && INTVAL (XEXP (addr, 1)) == -16
13925 && VECTOR_MEM_ALTIVEC_P (mode))
13926 || (rclass == FLOAT_REGS /* legacy float mem */
13927 && GET_MODE_SIZE (mode) == 8
13928 && and_op2 == NULL_RTX
13929 && scratch_or_premodify == scratch
13930 && rs6000_legitimate_offset_address_p (mode, addr, false, false)))
13933 else if (GET_CODE (addr) == PLUS)
13935 addr_op1 = XEXP (addr, 0);
13936 addr_op2 = XEXP (addr, 1);
13937 gcc_assert (REG_P (addr_op1));
13939 if (TARGET_DEBUG_ADDR)
13941 fprintf (stderr, "\nMove plus addr to register %s, mode = %s: ",
13942 rs6000_reg_names[REGNO (scratch)], GET_MODE_NAME (mode));
13943 debug_rtx (addr_op2);
13945 rs6000_emit_move (scratch, addr_op2, Pmode);
13946 emit_insn (gen_rtx_SET (VOIDmode,
13947 scratch_or_premodify,
13948 gen_rtx_PLUS (Pmode,
13949 addr_op1,
13950 scratch)));
13951 addr = scratch_or_premodify;
13952 scratch_or_premodify = scratch;
13955 else if (GET_CODE (addr) == SYMBOL_REF || GET_CODE (addr) == CONST
13956 || GET_CODE (addr) == CONST_INT || REG_P (addr))
13958 if (TARGET_DEBUG_ADDR)
13960 fprintf (stderr, "\nMove addr to register %s, mode = %s: ",
13961 rs6000_reg_names[REGNO (scratch_or_premodify)],
13962 GET_MODE_NAME (mode));
13963 debug_rtx (addr);
13966 rs6000_emit_move (scratch_or_premodify, addr, Pmode);
13967 addr = scratch_or_premodify;
13968 scratch_or_premodify = scratch;
13971 else
13972 gcc_unreachable ();
13974 break;
13976 default:
13977 gcc_unreachable ();
13980 /* If the original address involved a pre-modify that we couldn't use the VSX
13981 memory instruction with update, and we haven't taken care of already,
13982 store the address in the pre-modify register and use that as the
13983 address. */
13984 if (scratch_or_premodify != scratch && scratch_or_premodify != addr)
13986 emit_insn (gen_rtx_SET (VOIDmode, scratch_or_premodify, addr));
13987 addr = scratch_or_premodify;
13990 /* If the original address involved an AND -16 and we couldn't use an ALTIVEC
13991 memory instruction, recreate the AND now, including the clobber which is
13992 generated by the general ANDSI3/ANDDI3 patterns for the
13993 andi. instruction. */
13994 if (and_op2 != NULL_RTX)
13996 if (! legitimate_indirect_address_p (addr, false))
13998 emit_insn (gen_rtx_SET (VOIDmode, scratch, addr));
13999 addr = scratch;
14002 if (TARGET_DEBUG_ADDR)
14004 fprintf (stderr, "\nAnd addr to register %s, mode = %s: ",
14005 rs6000_reg_names[REGNO (scratch)], GET_MODE_NAME (mode));
14006 debug_rtx (and_op2);
14009 and_rtx = gen_rtx_SET (VOIDmode,
14010 scratch,
14011 gen_rtx_AND (Pmode,
14012 addr,
14013 and_op2));
14015 cc_clobber = gen_rtx_CLOBBER (CCmode, gen_rtx_SCRATCH (CCmode));
14016 emit_insn (gen_rtx_PARALLEL (VOIDmode,
14017 gen_rtvec (2, and_rtx, cc_clobber)));
14018 addr = scratch;
14021 /* Adjust the address if it changed. */
14022 if (addr != XEXP (mem, 0))
14024 mem = change_address (mem, mode, addr);
14025 if (TARGET_DEBUG_ADDR)
14026 fprintf (stderr, "\nrs6000_secondary_reload_inner, mem adjusted.\n");
14029 /* Now create the move. */
14030 if (store_p)
14031 emit_insn (gen_rtx_SET (VOIDmode, mem, reg));
14032 else
14033 emit_insn (gen_rtx_SET (VOIDmode, reg, mem));
14035 return;
14038 /* Convert reloads involving 64-bit gprs and misaligned offset
14039 addressing, or multiple 32-bit gprs and offsets that are too large,
14040 to use indirect addressing. */
14042 void
14043 rs6000_secondary_reload_gpr (rtx reg, rtx mem, rtx scratch, bool store_p)
14045 int regno = true_regnum (reg);
14046 enum reg_class rclass;
14047 rtx addr;
14048 rtx scratch_or_premodify = scratch;
14050 if (TARGET_DEBUG_ADDR)
14052 fprintf (stderr, "\nrs6000_secondary_reload_gpr, type = %s\n",
14053 store_p ? "store" : "load");
14054 fprintf (stderr, "reg:\n");
14055 debug_rtx (reg);
14056 fprintf (stderr, "mem:\n");
14057 debug_rtx (mem);
14058 fprintf (stderr, "scratch:\n");
14059 debug_rtx (scratch);
14062 gcc_assert (regno >= 0 && regno < FIRST_PSEUDO_REGISTER);
14063 gcc_assert (GET_CODE (mem) == MEM);
14064 rclass = REGNO_REG_CLASS (regno);
14065 gcc_assert (rclass == GENERAL_REGS || rclass == BASE_REGS);
14066 addr = XEXP (mem, 0);
14068 if (GET_CODE (addr) == PRE_MODIFY)
14070 scratch_or_premodify = XEXP (addr, 0);
14071 gcc_assert (REG_P (scratch_or_premodify));
14072 addr = XEXP (addr, 1);
14074 gcc_assert (GET_CODE (addr) == PLUS || GET_CODE (addr) == LO_SUM);
14076 rs6000_emit_move (scratch_or_premodify, addr, Pmode);
14078 mem = replace_equiv_address_nv (mem, scratch_or_premodify);
14080 /* Now create the move. */
14081 if (store_p)
14082 emit_insn (gen_rtx_SET (VOIDmode, mem, reg));
14083 else
14084 emit_insn (gen_rtx_SET (VOIDmode, reg, mem));
14086 return;
14089 /* Allocate a 64-bit stack slot to be used for copying SDmode
14090 values through if this function has any SDmode references. */
14092 static void
14093 rs6000_alloc_sdmode_stack_slot (void)
14095 tree t;
14096 basic_block bb;
14097 gimple_stmt_iterator gsi;
14099 gcc_assert (cfun->machine->sdmode_stack_slot == NULL_RTX);
14101 FOR_EACH_BB (bb)
14102 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
14104 tree ret = walk_gimple_op (gsi_stmt (gsi), rs6000_check_sdmode, NULL);
14105 if (ret)
14107 rtx stack = assign_stack_local (DDmode, GET_MODE_SIZE (DDmode), 0);
14108 cfun->machine->sdmode_stack_slot = adjust_address_nv (stack,
14109 SDmode, 0);
14110 return;
14114 /* Check for any SDmode parameters of the function. */
14115 for (t = DECL_ARGUMENTS (cfun->decl); t; t = DECL_CHAIN (t))
14117 if (TREE_TYPE (t) == error_mark_node)
14118 continue;
14120 if (TYPE_MODE (TREE_TYPE (t)) == SDmode
14121 || TYPE_MODE (DECL_ARG_TYPE (t)) == SDmode)
14123 rtx stack = assign_stack_local (DDmode, GET_MODE_SIZE (DDmode), 0);
14124 cfun->machine->sdmode_stack_slot = adjust_address_nv (stack,
14125 SDmode, 0);
14126 return;
14131 static void
14132 rs6000_instantiate_decls (void)
14134 if (cfun->machine->sdmode_stack_slot != NULL_RTX)
14135 instantiate_decl_rtl (cfun->machine->sdmode_stack_slot);
14138 /* Given an rtx X being reloaded into a reg required to be
14139 in class CLASS, return the class of reg to actually use.
14140 In general this is just CLASS; but on some machines
14141 in some cases it is preferable to use a more restrictive class.
14143 On the RS/6000, we have to return NO_REGS when we want to reload a
14144 floating-point CONST_DOUBLE to force it to be copied to memory.
14146 We also don't want to reload integer values into floating-point
14147 registers if we can at all help it. In fact, this can
14148 cause reload to die, if it tries to generate a reload of CTR
14149 into a FP register and discovers it doesn't have the memory location
14150 required.
14152 ??? Would it be a good idea to have reload do the converse, that is
14153 try to reload floating modes into FP registers if possible?
14156 static enum reg_class
14157 rs6000_preferred_reload_class (rtx x, enum reg_class rclass)
14159 enum machine_mode mode = GET_MODE (x);
14161 if (VECTOR_UNIT_VSX_P (mode)
14162 && x == CONST0_RTX (mode) && VSX_REG_CLASS_P (rclass))
14163 return rclass;
14165 if (VECTOR_UNIT_ALTIVEC_OR_VSX_P (mode)
14166 && (rclass == ALTIVEC_REGS || rclass == VSX_REGS)
14167 && easy_vector_constant (x, mode))
14168 return ALTIVEC_REGS;
14170 if (CONSTANT_P (x) && reg_classes_intersect_p (rclass, FLOAT_REGS))
14171 return NO_REGS;
14173 if (GET_MODE_CLASS (mode) == MODE_INT && rclass == NON_SPECIAL_REGS)
14174 return GENERAL_REGS;
14176 /* For VSX, prefer the traditional registers for 64-bit values because we can
14177 use the non-VSX loads. Prefer the Altivec registers if Altivec is
14178 handling the vector operations (i.e. V16QI, V8HI, and V4SI), or if we
14179 prefer Altivec loads.. */
14180 if (rclass == VSX_REGS)
14182 if (GET_MODE_SIZE (mode) <= 8)
14183 return FLOAT_REGS;
14185 if (VECTOR_UNIT_ALTIVEC_P (mode) || VECTOR_MEM_ALTIVEC_P (mode))
14186 return ALTIVEC_REGS;
14188 return rclass;
14191 return rclass;
14194 /* Debug version of rs6000_preferred_reload_class. */
14195 static enum reg_class
14196 rs6000_debug_preferred_reload_class (rtx x, enum reg_class rclass)
14198 enum reg_class ret = rs6000_preferred_reload_class (x, rclass);
14200 fprintf (stderr,
14201 "\nrs6000_preferred_reload_class, return %s, rclass = %s, "
14202 "mode = %s, x:\n",
14203 reg_class_names[ret], reg_class_names[rclass],
14204 GET_MODE_NAME (GET_MODE (x)));
14205 debug_rtx (x);
14207 return ret;
14210 /* If we are copying between FP or AltiVec registers and anything else, we need
14211 a memory location. The exception is when we are targeting ppc64 and the
14212 move to/from fpr to gpr instructions are available. Also, under VSX, you
14213 can copy vector registers from the FP register set to the Altivec register
14214 set and vice versa. */
14216 static bool
14217 rs6000_secondary_memory_needed (enum reg_class class1,
14218 enum reg_class class2,
14219 enum machine_mode mode)
14221 if (class1 == class2)
14222 return false;
14224 /* Under VSX, there are 3 register classes that values could be in (VSX_REGS,
14225 ALTIVEC_REGS, and FLOAT_REGS). We don't need to use memory to copy
14226 between these classes. But we need memory for other things that can go in
14227 FLOAT_REGS like SFmode. */
14228 if (TARGET_VSX
14229 && (VECTOR_MEM_VSX_P (mode) || VECTOR_UNIT_VSX_P (mode))
14230 && (class1 == VSX_REGS || class1 == ALTIVEC_REGS
14231 || class1 == FLOAT_REGS))
14232 return (class2 != VSX_REGS && class2 != ALTIVEC_REGS
14233 && class2 != FLOAT_REGS);
14235 if (class1 == VSX_REGS || class2 == VSX_REGS)
14236 return true;
14238 if (class1 == FLOAT_REGS
14239 && (!TARGET_MFPGPR || !TARGET_POWERPC64
14240 || ((mode != DFmode)
14241 && (mode != DDmode)
14242 && (mode != DImode))))
14243 return true;
14245 if (class2 == FLOAT_REGS
14246 && (!TARGET_MFPGPR || !TARGET_POWERPC64
14247 || ((mode != DFmode)
14248 && (mode != DDmode)
14249 && (mode != DImode))))
14250 return true;
14252 if (class1 == ALTIVEC_REGS || class2 == ALTIVEC_REGS)
14253 return true;
14255 return false;
14258 /* Debug version of rs6000_secondary_memory_needed. */
14259 static bool
14260 rs6000_debug_secondary_memory_needed (enum reg_class class1,
14261 enum reg_class class2,
14262 enum machine_mode mode)
14264 bool ret = rs6000_secondary_memory_needed (class1, class2, mode);
14266 fprintf (stderr,
14267 "rs6000_secondary_memory_needed, return: %s, class1 = %s, "
14268 "class2 = %s, mode = %s\n",
14269 ret ? "true" : "false", reg_class_names[class1],
14270 reg_class_names[class2], GET_MODE_NAME (mode));
14272 return ret;
14275 /* Return the register class of a scratch register needed to copy IN into
14276 or out of a register in RCLASS in MODE. If it can be done directly,
14277 NO_REGS is returned. */
14279 static enum reg_class
14280 rs6000_secondary_reload_class (enum reg_class rclass, enum machine_mode mode,
14281 rtx in)
14283 int regno;
14285 if (TARGET_ELF || (DEFAULT_ABI == ABI_DARWIN
14286 #if TARGET_MACHO
14287 && MACHOPIC_INDIRECT
14288 #endif
14291 /* We cannot copy a symbolic operand directly into anything
14292 other than BASE_REGS for TARGET_ELF. So indicate that a
14293 register from BASE_REGS is needed as an intermediate
14294 register.
14296 On Darwin, pic addresses require a load from memory, which
14297 needs a base register. */
14298 if (rclass != BASE_REGS
14299 && (GET_CODE (in) == SYMBOL_REF
14300 || GET_CODE (in) == HIGH
14301 || GET_CODE (in) == LABEL_REF
14302 || GET_CODE (in) == CONST))
14303 return BASE_REGS;
14306 if (GET_CODE (in) == REG)
14308 regno = REGNO (in);
14309 if (regno >= FIRST_PSEUDO_REGISTER)
14311 regno = true_regnum (in);
14312 if (regno >= FIRST_PSEUDO_REGISTER)
14313 regno = -1;
14316 else if (GET_CODE (in) == SUBREG)
14318 regno = true_regnum (in);
14319 if (regno >= FIRST_PSEUDO_REGISTER)
14320 regno = -1;
14322 else
14323 regno = -1;
14325 /* We can place anything into GENERAL_REGS and can put GENERAL_REGS
14326 into anything. */
14327 if (rclass == GENERAL_REGS || rclass == BASE_REGS
14328 || (regno >= 0 && INT_REGNO_P (regno)))
14329 return NO_REGS;
14331 /* Constants, memory, and FP registers can go into FP registers. */
14332 if ((regno == -1 || FP_REGNO_P (regno))
14333 && (rclass == FLOAT_REGS || rclass == NON_SPECIAL_REGS))
14334 return (mode != SDmode) ? NO_REGS : GENERAL_REGS;
14336 /* Memory, and FP/altivec registers can go into fp/altivec registers under
14337 VSX. */
14338 if (TARGET_VSX
14339 && (regno == -1 || VSX_REGNO_P (regno))
14340 && VSX_REG_CLASS_P (rclass))
14341 return NO_REGS;
14343 /* Memory, and AltiVec registers can go into AltiVec registers. */
14344 if ((regno == -1 || ALTIVEC_REGNO_P (regno))
14345 && rclass == ALTIVEC_REGS)
14346 return NO_REGS;
14348 /* We can copy among the CR registers. */
14349 if ((rclass == CR_REGS || rclass == CR0_REGS)
14350 && regno >= 0 && CR_REGNO_P (regno))
14351 return NO_REGS;
14353 /* Otherwise, we need GENERAL_REGS. */
14354 return GENERAL_REGS;
14357 /* Debug version of rs6000_secondary_reload_class. */
14358 static enum reg_class
14359 rs6000_debug_secondary_reload_class (enum reg_class rclass,
14360 enum machine_mode mode, rtx in)
14362 enum reg_class ret = rs6000_secondary_reload_class (rclass, mode, in);
14363 fprintf (stderr,
14364 "\nrs6000_secondary_reload_class, return %s, rclass = %s, "
14365 "mode = %s, input rtx:\n",
14366 reg_class_names[ret], reg_class_names[rclass],
14367 GET_MODE_NAME (mode));
14368 debug_rtx (in);
14370 return ret;
14373 /* Return nonzero if for CLASS a mode change from FROM to TO is invalid. */
14375 static bool
14376 rs6000_cannot_change_mode_class (enum machine_mode from,
14377 enum machine_mode to,
14378 enum reg_class rclass)
14380 unsigned from_size = GET_MODE_SIZE (from);
14381 unsigned to_size = GET_MODE_SIZE (to);
14383 if (from_size != to_size)
14385 enum reg_class xclass = (TARGET_VSX) ? VSX_REGS : FLOAT_REGS;
14386 return ((from_size < 8 || to_size < 8 || TARGET_IEEEQUAD)
14387 && reg_classes_intersect_p (xclass, rclass));
14390 if (TARGET_E500_DOUBLE
14391 && ((((to) == DFmode) + ((from) == DFmode)) == 1
14392 || (((to) == TFmode) + ((from) == TFmode)) == 1
14393 || (((to) == DDmode) + ((from) == DDmode)) == 1
14394 || (((to) == TDmode) + ((from) == TDmode)) == 1
14395 || (((to) == DImode) + ((from) == DImode)) == 1))
14396 return true;
14398 /* Since the VSX register set includes traditional floating point registers
14399 and altivec registers, just check for the size being different instead of
14400 trying to check whether the modes are vector modes. Otherwise it won't
14401 allow say DF and DI to change classes. */
14402 if (TARGET_VSX && VSX_REG_CLASS_P (rclass))
14403 return (from_size != 8 && from_size != 16);
14405 if (TARGET_ALTIVEC && rclass == ALTIVEC_REGS
14406 && (ALTIVEC_VECTOR_MODE (from) + ALTIVEC_VECTOR_MODE (to)) == 1)
14407 return true;
14409 if (TARGET_SPE && (SPE_VECTOR_MODE (from) + SPE_VECTOR_MODE (to)) == 1
14410 && reg_classes_intersect_p (GENERAL_REGS, rclass))
14411 return true;
14413 return false;
14416 /* Debug version of rs6000_cannot_change_mode_class. */
14417 static bool
14418 rs6000_debug_cannot_change_mode_class (enum machine_mode from,
14419 enum machine_mode to,
14420 enum reg_class rclass)
14422 bool ret = rs6000_cannot_change_mode_class (from, to, rclass);
14424 fprintf (stderr,
14425 "rs6000_cannot_change_mode_class, return %s, from = %s, "
14426 "to = %s, rclass = %s\n",
14427 ret ? "true" : "false",
14428 GET_MODE_NAME (from), GET_MODE_NAME (to),
14429 reg_class_names[rclass]);
14431 return ret;
14434 /* Given a comparison operation, return the bit number in CCR to test. We
14435 know this is a valid comparison.
14437 SCC_P is 1 if this is for an scc. That means that %D will have been
14438 used instead of %C, so the bits will be in different places.
14440 Return -1 if OP isn't a valid comparison for some reason. */
14443 ccr_bit (rtx op, int scc_p)
14445 enum rtx_code code = GET_CODE (op);
14446 enum machine_mode cc_mode;
14447 int cc_regnum;
14448 int base_bit;
14449 rtx reg;
14451 if (!COMPARISON_P (op))
14452 return -1;
14454 reg = XEXP (op, 0);
14456 gcc_assert (GET_CODE (reg) == REG && CR_REGNO_P (REGNO (reg)));
14458 cc_mode = GET_MODE (reg);
14459 cc_regnum = REGNO (reg);
14460 base_bit = 4 * (cc_regnum - CR0_REGNO);
14462 validate_condition_mode (code, cc_mode);
14464 /* When generating a sCOND operation, only positive conditions are
14465 allowed. */
14466 gcc_assert (!scc_p
14467 || code == EQ || code == GT || code == LT || code == UNORDERED
14468 || code == GTU || code == LTU);
14470 switch (code)
14472 case NE:
14473 return scc_p ? base_bit + 3 : base_bit + 2;
14474 case EQ:
14475 return base_bit + 2;
14476 case GT: case GTU: case UNLE:
14477 return base_bit + 1;
14478 case LT: case LTU: case UNGE:
14479 return base_bit;
14480 case ORDERED: case UNORDERED:
14481 return base_bit + 3;
14483 case GE: case GEU:
14484 /* If scc, we will have done a cror to put the bit in the
14485 unordered position. So test that bit. For integer, this is ! LT
14486 unless this is an scc insn. */
14487 return scc_p ? base_bit + 3 : base_bit;
14489 case LE: case LEU:
14490 return scc_p ? base_bit + 3 : base_bit + 1;
14492 default:
14493 gcc_unreachable ();
14497 /* Return the GOT register. */
14500 rs6000_got_register (rtx value ATTRIBUTE_UNUSED)
14502 /* The second flow pass currently (June 1999) can't update
14503 regs_ever_live without disturbing other parts of the compiler, so
14504 update it here to make the prolog/epilogue code happy. */
14505 if (!can_create_pseudo_p ()
14506 && !df_regs_ever_live_p (RS6000_PIC_OFFSET_TABLE_REGNUM))
14507 df_set_regs_ever_live (RS6000_PIC_OFFSET_TABLE_REGNUM, true);
14509 crtl->uses_pic_offset_table = 1;
14511 return pic_offset_table_rtx;
14514 static rs6000_stack_t stack_info;
14516 /* Function to init struct machine_function.
14517 This will be called, via a pointer variable,
14518 from push_function_context. */
14520 static struct machine_function *
14521 rs6000_init_machine_status (void)
14523 stack_info.reload_completed = 0;
14524 return ggc_alloc_cleared_machine_function ();
14527 /* These macros test for integers and extract the low-order bits. */
14528 #define INT_P(X) \
14529 ((GET_CODE (X) == CONST_INT || GET_CODE (X) == CONST_DOUBLE) \
14530 && GET_MODE (X) == VOIDmode)
14532 #define INT_LOWPART(X) \
14533 (GET_CODE (X) == CONST_INT ? INTVAL (X) : CONST_DOUBLE_LOW (X))
14536 extract_MB (rtx op)
14538 int i;
14539 unsigned long val = INT_LOWPART (op);
14541 /* If the high bit is zero, the value is the first 1 bit we find
14542 from the left. */
14543 if ((val & 0x80000000) == 0)
14545 gcc_assert (val & 0xffffffff);
14547 i = 1;
14548 while (((val <<= 1) & 0x80000000) == 0)
14549 ++i;
14550 return i;
14553 /* If the high bit is set and the low bit is not, or the mask is all
14554 1's, the value is zero. */
14555 if ((val & 1) == 0 || (val & 0xffffffff) == 0xffffffff)
14556 return 0;
14558 /* Otherwise we have a wrap-around mask. Look for the first 0 bit
14559 from the right. */
14560 i = 31;
14561 while (((val >>= 1) & 1) != 0)
14562 --i;
14564 return i;
14568 extract_ME (rtx op)
14570 int i;
14571 unsigned long val = INT_LOWPART (op);
14573 /* If the low bit is zero, the value is the first 1 bit we find from
14574 the right. */
14575 if ((val & 1) == 0)
14577 gcc_assert (val & 0xffffffff);
14579 i = 30;
14580 while (((val >>= 1) & 1) == 0)
14581 --i;
14583 return i;
14586 /* If the low bit is set and the high bit is not, or the mask is all
14587 1's, the value is 31. */
14588 if ((val & 0x80000000) == 0 || (val & 0xffffffff) == 0xffffffff)
14589 return 31;
14591 /* Otherwise we have a wrap-around mask. Look for the first 0 bit
14592 from the left. */
14593 i = 0;
14594 while (((val <<= 1) & 0x80000000) != 0)
14595 ++i;
14597 return i;
14600 /* Locate some local-dynamic symbol still in use by this function
14601 so that we can print its name in some tls_ld pattern. */
14603 static const char *
14604 rs6000_get_some_local_dynamic_name (void)
14606 rtx insn;
14608 if (cfun->machine->some_ld_name)
14609 return cfun->machine->some_ld_name;
14611 for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
14612 if (INSN_P (insn)
14613 && for_each_rtx (&PATTERN (insn),
14614 rs6000_get_some_local_dynamic_name_1, 0))
14615 return cfun->machine->some_ld_name;
14617 gcc_unreachable ();
14620 /* Helper function for rs6000_get_some_local_dynamic_name. */
14622 static int
14623 rs6000_get_some_local_dynamic_name_1 (rtx *px, void *data ATTRIBUTE_UNUSED)
14625 rtx x = *px;
14627 if (GET_CODE (x) == SYMBOL_REF)
14629 const char *str = XSTR (x, 0);
14630 if (SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_DYNAMIC)
14632 cfun->machine->some_ld_name = str;
14633 return 1;
14637 return 0;
14640 /* Write out a function code label. */
14642 void
14643 rs6000_output_function_entry (FILE *file, const char *fname)
14645 if (fname[0] != '.')
14647 switch (DEFAULT_ABI)
14649 default:
14650 gcc_unreachable ();
14652 case ABI_AIX:
14653 if (DOT_SYMBOLS)
14654 putc ('.', file);
14655 else
14656 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "L.");
14657 break;
14659 case ABI_V4:
14660 case ABI_DARWIN:
14661 break;
14665 RS6000_OUTPUT_BASENAME (file, fname);
14668 /* Print an operand. Recognize special options, documented below. */
14670 #if TARGET_ELF
14671 #define SMALL_DATA_RELOC ((rs6000_sdata == SDATA_EABI) ? "sda21" : "sdarel")
14672 #define SMALL_DATA_REG ((rs6000_sdata == SDATA_EABI) ? 0 : 13)
14673 #else
14674 #define SMALL_DATA_RELOC "sda21"
14675 #define SMALL_DATA_REG 0
14676 #endif
14678 void
14679 print_operand (FILE *file, rtx x, int code)
14681 int i;
14682 unsigned HOST_WIDE_INT uval;
14684 switch (code)
14686 /* %a is output_address. */
14688 case 'b':
14689 /* If constant, low-order 16 bits of constant, unsigned.
14690 Otherwise, write normally. */
14691 if (INT_P (x))
14692 fprintf (file, HOST_WIDE_INT_PRINT_DEC, INT_LOWPART (x) & 0xffff);
14693 else
14694 print_operand (file, x, 0);
14695 return;
14697 case 'B':
14698 /* If the low-order bit is zero, write 'r'; otherwise, write 'l'
14699 for 64-bit mask direction. */
14700 putc (((INT_LOWPART (x) & 1) == 0 ? 'r' : 'l'), file);
14701 return;
14703 /* %c is output_addr_const if a CONSTANT_ADDRESS_P, otherwise
14704 output_operand. */
14706 case 'D':
14707 /* Like 'J' but get to the GT bit only. */
14708 gcc_assert (REG_P (x));
14710 /* Bit 1 is GT bit. */
14711 i = 4 * (REGNO (x) - CR0_REGNO) + 1;
14713 /* Add one for shift count in rlinm for scc. */
14714 fprintf (file, "%d", i + 1);
14715 return;
14717 case 'E':
14718 /* X is a CR register. Print the number of the EQ bit of the CR */
14719 if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
14720 output_operand_lossage ("invalid %%E value");
14721 else
14722 fprintf (file, "%d", 4 * (REGNO (x) - CR0_REGNO) + 2);
14723 return;
14725 case 'f':
14726 /* X is a CR register. Print the shift count needed to move it
14727 to the high-order four bits. */
14728 if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
14729 output_operand_lossage ("invalid %%f value");
14730 else
14731 fprintf (file, "%d", 4 * (REGNO (x) - CR0_REGNO));
14732 return;
14734 case 'F':
14735 /* Similar, but print the count for the rotate in the opposite
14736 direction. */
14737 if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
14738 output_operand_lossage ("invalid %%F value");
14739 else
14740 fprintf (file, "%d", 32 - 4 * (REGNO (x) - CR0_REGNO));
14741 return;
14743 case 'G':
14744 /* X is a constant integer. If it is negative, print "m",
14745 otherwise print "z". This is to make an aze or ame insn. */
14746 if (GET_CODE (x) != CONST_INT)
14747 output_operand_lossage ("invalid %%G value");
14748 else if (INTVAL (x) >= 0)
14749 putc ('z', file);
14750 else
14751 putc ('m', file);
14752 return;
14754 case 'h':
14755 /* If constant, output low-order five bits. Otherwise, write
14756 normally. */
14757 if (INT_P (x))
14758 fprintf (file, HOST_WIDE_INT_PRINT_DEC, INT_LOWPART (x) & 31);
14759 else
14760 print_operand (file, x, 0);
14761 return;
14763 case 'H':
14764 /* If constant, output low-order six bits. Otherwise, write
14765 normally. */
14766 if (INT_P (x))
14767 fprintf (file, HOST_WIDE_INT_PRINT_DEC, INT_LOWPART (x) & 63);
14768 else
14769 print_operand (file, x, 0);
14770 return;
14772 case 'I':
14773 /* Print `i' if this is a constant, else nothing. */
14774 if (INT_P (x))
14775 putc ('i', file);
14776 return;
14778 case 'j':
14779 /* Write the bit number in CCR for jump. */
14780 i = ccr_bit (x, 0);
14781 if (i == -1)
14782 output_operand_lossage ("invalid %%j code");
14783 else
14784 fprintf (file, "%d", i);
14785 return;
14787 case 'J':
14788 /* Similar, but add one for shift count in rlinm for scc and pass
14789 scc flag to `ccr_bit'. */
14790 i = ccr_bit (x, 1);
14791 if (i == -1)
14792 output_operand_lossage ("invalid %%J code");
14793 else
14794 /* If we want bit 31, write a shift count of zero, not 32. */
14795 fprintf (file, "%d", i == 31 ? 0 : i + 1);
14796 return;
14798 case 'k':
14799 /* X must be a constant. Write the 1's complement of the
14800 constant. */
14801 if (! INT_P (x))
14802 output_operand_lossage ("invalid %%k value");
14803 else
14804 fprintf (file, HOST_WIDE_INT_PRINT_DEC, ~ INT_LOWPART (x));
14805 return;
14807 case 'K':
14808 /* X must be a symbolic constant on ELF. Write an
14809 expression suitable for an 'addi' that adds in the low 16
14810 bits of the MEM. */
14811 if (GET_CODE (x) == CONST)
14813 if (GET_CODE (XEXP (x, 0)) != PLUS
14814 || (GET_CODE (XEXP (XEXP (x, 0), 0)) != SYMBOL_REF
14815 && GET_CODE (XEXP (XEXP (x, 0), 0)) != LABEL_REF)
14816 || GET_CODE (XEXP (XEXP (x, 0), 1)) != CONST_INT)
14817 output_operand_lossage ("invalid %%K value");
14819 print_operand_address (file, x);
14820 fputs ("@l", file);
14821 return;
14823 /* %l is output_asm_label. */
14825 case 'L':
14826 /* Write second word of DImode or DFmode reference. Works on register
14827 or non-indexed memory only. */
14828 if (REG_P (x))
14829 fputs (reg_names[REGNO (x) + 1], file);
14830 else if (MEM_P (x))
14832 /* Handle possible auto-increment. Since it is pre-increment and
14833 we have already done it, we can just use an offset of word. */
14834 if (GET_CODE (XEXP (x, 0)) == PRE_INC
14835 || GET_CODE (XEXP (x, 0)) == PRE_DEC)
14836 output_address (plus_constant (Pmode, XEXP (XEXP (x, 0), 0),
14837 UNITS_PER_WORD));
14838 else if (GET_CODE (XEXP (x, 0)) == PRE_MODIFY)
14839 output_address (plus_constant (Pmode, XEXP (XEXP (x, 0), 0),
14840 UNITS_PER_WORD));
14841 else
14842 output_address (XEXP (adjust_address_nv (x, SImode,
14843 UNITS_PER_WORD),
14844 0));
14846 if (small_data_operand (x, GET_MODE (x)))
14847 fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
14848 reg_names[SMALL_DATA_REG]);
14850 return;
14852 case 'm':
14853 /* MB value for a mask operand. */
14854 if (! mask_operand (x, SImode))
14855 output_operand_lossage ("invalid %%m value");
14857 fprintf (file, "%d", extract_MB (x));
14858 return;
14860 case 'M':
14861 /* ME value for a mask operand. */
14862 if (! mask_operand (x, SImode))
14863 output_operand_lossage ("invalid %%M value");
14865 fprintf (file, "%d", extract_ME (x));
14866 return;
14868 /* %n outputs the negative of its operand. */
14870 case 'N':
14871 /* Write the number of elements in the vector times 4. */
14872 if (GET_CODE (x) != PARALLEL)
14873 output_operand_lossage ("invalid %%N value");
14874 else
14875 fprintf (file, "%d", XVECLEN (x, 0) * 4);
14876 return;
14878 case 'O':
14879 /* Similar, but subtract 1 first. */
14880 if (GET_CODE (x) != PARALLEL)
14881 output_operand_lossage ("invalid %%O value");
14882 else
14883 fprintf (file, "%d", (XVECLEN (x, 0) - 1) * 4);
14884 return;
14886 case 'p':
14887 /* X is a CONST_INT that is a power of two. Output the logarithm. */
14888 if (! INT_P (x)
14889 || INT_LOWPART (x) < 0
14890 || (i = exact_log2 (INT_LOWPART (x))) < 0)
14891 output_operand_lossage ("invalid %%p value");
14892 else
14893 fprintf (file, "%d", i);
14894 return;
14896 case 'P':
14897 /* The operand must be an indirect memory reference. The result
14898 is the register name. */
14899 if (GET_CODE (x) != MEM || GET_CODE (XEXP (x, 0)) != REG
14900 || REGNO (XEXP (x, 0)) >= 32)
14901 output_operand_lossage ("invalid %%P value");
14902 else
14903 fputs (reg_names[REGNO (XEXP (x, 0))], file);
14904 return;
14906 case 'q':
14907 /* This outputs the logical code corresponding to a boolean
14908 expression. The expression may have one or both operands
14909 negated (if one, only the first one). For condition register
14910 logical operations, it will also treat the negated
14911 CR codes as NOTs, but not handle NOTs of them. */
14913 const char *const *t = 0;
14914 const char *s;
14915 enum rtx_code code = GET_CODE (x);
14916 static const char * const tbl[3][3] = {
14917 { "and", "andc", "nor" },
14918 { "or", "orc", "nand" },
14919 { "xor", "eqv", "xor" } };
14921 if (code == AND)
14922 t = tbl[0];
14923 else if (code == IOR)
14924 t = tbl[1];
14925 else if (code == XOR)
14926 t = tbl[2];
14927 else
14928 output_operand_lossage ("invalid %%q value");
14930 if (GET_CODE (XEXP (x, 0)) != NOT)
14931 s = t[0];
14932 else
14934 if (GET_CODE (XEXP (x, 1)) == NOT)
14935 s = t[2];
14936 else
14937 s = t[1];
14940 fputs (s, file);
14942 return;
14944 case 'Q':
14945 if (! TARGET_MFCRF)
14946 return;
14947 fputc (',', file);
14948 /* FALLTHRU */
14950 case 'R':
14951 /* X is a CR register. Print the mask for `mtcrf'. */
14952 if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
14953 output_operand_lossage ("invalid %%R value");
14954 else
14955 fprintf (file, "%d", 128 >> (REGNO (x) - CR0_REGNO));
14956 return;
14958 case 's':
14959 /* Low 5 bits of 32 - value */
14960 if (! INT_P (x))
14961 output_operand_lossage ("invalid %%s value");
14962 else
14963 fprintf (file, HOST_WIDE_INT_PRINT_DEC, (32 - INT_LOWPART (x)) & 31);
14964 return;
14966 case 'S':
14967 /* PowerPC64 mask position. All 0's is excluded.
14968 CONST_INT 32-bit mask is considered sign-extended so any
14969 transition must occur within the CONST_INT, not on the boundary. */
14970 if (! mask64_operand (x, DImode))
14971 output_operand_lossage ("invalid %%S value");
14973 uval = INT_LOWPART (x);
14975 if (uval & 1) /* Clear Left */
14977 #if HOST_BITS_PER_WIDE_INT > 64
14978 uval &= ((unsigned HOST_WIDE_INT) 1 << 64) - 1;
14979 #endif
14980 i = 64;
14982 else /* Clear Right */
14984 uval = ~uval;
14985 #if HOST_BITS_PER_WIDE_INT > 64
14986 uval &= ((unsigned HOST_WIDE_INT) 1 << 64) - 1;
14987 #endif
14988 i = 63;
14990 while (uval != 0)
14991 --i, uval >>= 1;
14992 gcc_assert (i >= 0);
14993 fprintf (file, "%d", i);
14994 return;
14996 case 't':
14997 /* Like 'J' but get to the OVERFLOW/UNORDERED bit. */
14998 gcc_assert (REG_P (x) && GET_MODE (x) == CCmode);
15000 /* Bit 3 is OV bit. */
15001 i = 4 * (REGNO (x) - CR0_REGNO) + 3;
15003 /* If we want bit 31, write a shift count of zero, not 32. */
15004 fprintf (file, "%d", i == 31 ? 0 : i + 1);
15005 return;
15007 case 'T':
15008 /* Print the symbolic name of a branch target register. */
15009 if (GET_CODE (x) != REG || (REGNO (x) != LR_REGNO
15010 && REGNO (x) != CTR_REGNO))
15011 output_operand_lossage ("invalid %%T value");
15012 else if (REGNO (x) == LR_REGNO)
15013 fputs ("lr", file);
15014 else
15015 fputs ("ctr", file);
15016 return;
15018 case 'u':
15019 /* High-order 16 bits of constant for use in unsigned operand. */
15020 if (! INT_P (x))
15021 output_operand_lossage ("invalid %%u value");
15022 else
15023 fprintf (file, HOST_WIDE_INT_PRINT_HEX,
15024 (INT_LOWPART (x) >> 16) & 0xffff);
15025 return;
15027 case 'v':
15028 /* High-order 16 bits of constant for use in signed operand. */
15029 if (! INT_P (x))
15030 output_operand_lossage ("invalid %%v value");
15031 else
15032 fprintf (file, HOST_WIDE_INT_PRINT_HEX,
15033 (INT_LOWPART (x) >> 16) & 0xffff);
15034 return;
15036 case 'U':
15037 /* Print `u' if this has an auto-increment or auto-decrement. */
15038 if (MEM_P (x)
15039 && (GET_CODE (XEXP (x, 0)) == PRE_INC
15040 || GET_CODE (XEXP (x, 0)) == PRE_DEC
15041 || GET_CODE (XEXP (x, 0)) == PRE_MODIFY))
15042 putc ('u', file);
15043 return;
15045 case 'V':
15046 /* Print the trap code for this operand. */
15047 switch (GET_CODE (x))
15049 case EQ:
15050 fputs ("eq", file); /* 4 */
15051 break;
15052 case NE:
15053 fputs ("ne", file); /* 24 */
15054 break;
15055 case LT:
15056 fputs ("lt", file); /* 16 */
15057 break;
15058 case LE:
15059 fputs ("le", file); /* 20 */
15060 break;
15061 case GT:
15062 fputs ("gt", file); /* 8 */
15063 break;
15064 case GE:
15065 fputs ("ge", file); /* 12 */
15066 break;
15067 case LTU:
15068 fputs ("llt", file); /* 2 */
15069 break;
15070 case LEU:
15071 fputs ("lle", file); /* 6 */
15072 break;
15073 case GTU:
15074 fputs ("lgt", file); /* 1 */
15075 break;
15076 case GEU:
15077 fputs ("lge", file); /* 5 */
15078 break;
15079 default:
15080 gcc_unreachable ();
15082 break;
15084 case 'w':
15085 /* If constant, low-order 16 bits of constant, signed. Otherwise, write
15086 normally. */
15087 if (INT_P (x))
15088 fprintf (file, HOST_WIDE_INT_PRINT_DEC,
15089 ((INT_LOWPART (x) & 0xffff) ^ 0x8000) - 0x8000);
15090 else
15091 print_operand (file, x, 0);
15092 return;
15094 case 'W':
15095 /* MB value for a PowerPC64 rldic operand. */
15096 i = clz_hwi (GET_CODE (x) == CONST_INT
15097 ? INTVAL (x) : CONST_DOUBLE_HIGH (x));
15099 #if HOST_BITS_PER_WIDE_INT == 32
15100 if (GET_CODE (x) == CONST_INT && i > 0)
15101 i += 32; /* zero-extend high-part was all 0's */
15102 else if (GET_CODE (x) == CONST_DOUBLE && i == 32)
15103 i = clz_hwi (CONST_DOUBLE_LOW (x)) + 32;
15104 #endif
15106 fprintf (file, "%d", i);
15107 return;
15109 case 'x':
15110 /* X is a FPR or Altivec register used in a VSX context. */
15111 if (GET_CODE (x) != REG || !VSX_REGNO_P (REGNO (x)))
15112 output_operand_lossage ("invalid %%x value");
15113 else
15115 int reg = REGNO (x);
15116 int vsx_reg = (FP_REGNO_P (reg)
15117 ? reg - 32
15118 : reg - FIRST_ALTIVEC_REGNO + 32);
15120 #ifdef TARGET_REGNAMES
15121 if (TARGET_REGNAMES)
15122 fprintf (file, "%%vs%d", vsx_reg);
15123 else
15124 #endif
15125 fprintf (file, "%d", vsx_reg);
15127 return;
15129 case 'X':
15130 if (MEM_P (x)
15131 && (legitimate_indexed_address_p (XEXP (x, 0), 0)
15132 || (GET_CODE (XEXP (x, 0)) == PRE_MODIFY
15133 && legitimate_indexed_address_p (XEXP (XEXP (x, 0), 1), 0))))
15134 putc ('x', file);
15135 return;
15137 case 'Y':
15138 /* Like 'L', for third word of TImode */
15139 if (REG_P (x))
15140 fputs (reg_names[REGNO (x) + 2], file);
15141 else if (MEM_P (x))
15143 if (GET_CODE (XEXP (x, 0)) == PRE_INC
15144 || GET_CODE (XEXP (x, 0)) == PRE_DEC)
15145 output_address (plus_constant (Pmode, XEXP (XEXP (x, 0), 0), 8));
15146 else if (GET_CODE (XEXP (x, 0)) == PRE_MODIFY)
15147 output_address (plus_constant (Pmode, XEXP (XEXP (x, 0), 0), 8));
15148 else
15149 output_address (XEXP (adjust_address_nv (x, SImode, 8), 0));
15150 if (small_data_operand (x, GET_MODE (x)))
15151 fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
15152 reg_names[SMALL_DATA_REG]);
15154 return;
15156 case 'z':
15157 /* X is a SYMBOL_REF. Write out the name preceded by a
15158 period and without any trailing data in brackets. Used for function
15159 names. If we are configured for System V (or the embedded ABI) on
15160 the PowerPC, do not emit the period, since those systems do not use
15161 TOCs and the like. */
15162 gcc_assert (GET_CODE (x) == SYMBOL_REF);
15164 /* Mark the decl as referenced so that cgraph will output the
15165 function. */
15166 if (SYMBOL_REF_DECL (x))
15167 mark_decl_referenced (SYMBOL_REF_DECL (x));
15169 /* For macho, check to see if we need a stub. */
15170 if (TARGET_MACHO)
15172 const char *name = XSTR (x, 0);
15173 #if TARGET_MACHO
15174 if (darwin_emit_branch_islands
15175 && MACHOPIC_INDIRECT
15176 && machopic_classify_symbol (x) == MACHOPIC_UNDEFINED_FUNCTION)
15177 name = machopic_indirection_name (x, /*stub_p=*/true);
15178 #endif
15179 assemble_name (file, name);
15181 else if (!DOT_SYMBOLS)
15182 assemble_name (file, XSTR (x, 0));
15183 else
15184 rs6000_output_function_entry (file, XSTR (x, 0));
15185 return;
15187 case 'Z':
15188 /* Like 'L', for last word of TImode. */
15189 if (REG_P (x))
15190 fputs (reg_names[REGNO (x) + 3], file);
15191 else if (MEM_P (x))
15193 if (GET_CODE (XEXP (x, 0)) == PRE_INC
15194 || GET_CODE (XEXP (x, 0)) == PRE_DEC)
15195 output_address (plus_constant (Pmode, XEXP (XEXP (x, 0), 0), 12));
15196 else if (GET_CODE (XEXP (x, 0)) == PRE_MODIFY)
15197 output_address (plus_constant (Pmode, XEXP (XEXP (x, 0), 0), 12));
15198 else
15199 output_address (XEXP (adjust_address_nv (x, SImode, 12), 0));
15200 if (small_data_operand (x, GET_MODE (x)))
15201 fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
15202 reg_names[SMALL_DATA_REG]);
15204 return;
15206 /* Print AltiVec or SPE memory operand. */
15207 case 'y':
15209 rtx tmp;
15211 gcc_assert (MEM_P (x));
15213 tmp = XEXP (x, 0);
15215 /* Ugly hack because %y is overloaded. */
15216 if ((TARGET_SPE || TARGET_E500_DOUBLE)
15217 && (GET_MODE_SIZE (GET_MODE (x)) == 8
15218 || GET_MODE (x) == TFmode
15219 || GET_MODE (x) == TImode))
15221 /* Handle [reg]. */
15222 if (REG_P (tmp))
15224 fprintf (file, "0(%s)", reg_names[REGNO (tmp)]);
15225 break;
15227 /* Handle [reg+UIMM]. */
15228 else if (GET_CODE (tmp) == PLUS &&
15229 GET_CODE (XEXP (tmp, 1)) == CONST_INT)
15231 int x;
15233 gcc_assert (REG_P (XEXP (tmp, 0)));
15235 x = INTVAL (XEXP (tmp, 1));
15236 fprintf (file, "%d(%s)", x, reg_names[REGNO (XEXP (tmp, 0))]);
15237 break;
15240 /* Fall through. Must be [reg+reg]. */
15242 if (VECTOR_MEM_ALTIVEC_P (GET_MODE (x))
15243 && GET_CODE (tmp) == AND
15244 && GET_CODE (XEXP (tmp, 1)) == CONST_INT
15245 && INTVAL (XEXP (tmp, 1)) == -16)
15246 tmp = XEXP (tmp, 0);
15247 else if (VECTOR_MEM_VSX_P (GET_MODE (x))
15248 && GET_CODE (tmp) == PRE_MODIFY)
15249 tmp = XEXP (tmp, 1);
15250 if (REG_P (tmp))
15251 fprintf (file, "0,%s", reg_names[REGNO (tmp)]);
15252 else
15254 if (!GET_CODE (tmp) == PLUS
15255 || !REG_P (XEXP (tmp, 0))
15256 || !REG_P (XEXP (tmp, 1)))
15258 output_operand_lossage ("invalid %%y value, try using the 'Z' constraint");
15259 break;
15262 if (REGNO (XEXP (tmp, 0)) == 0)
15263 fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (tmp, 1)) ],
15264 reg_names[ REGNO (XEXP (tmp, 0)) ]);
15265 else
15266 fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (tmp, 0)) ],
15267 reg_names[ REGNO (XEXP (tmp, 1)) ]);
15269 break;
15272 case 0:
15273 if (REG_P (x))
15274 fprintf (file, "%s", reg_names[REGNO (x)]);
15275 else if (MEM_P (x))
15277 /* We need to handle PRE_INC and PRE_DEC here, since we need to
15278 know the width from the mode. */
15279 if (GET_CODE (XEXP (x, 0)) == PRE_INC)
15280 fprintf (file, "%d(%s)", GET_MODE_SIZE (GET_MODE (x)),
15281 reg_names[REGNO (XEXP (XEXP (x, 0), 0))]);
15282 else if (GET_CODE (XEXP (x, 0)) == PRE_DEC)
15283 fprintf (file, "%d(%s)", - GET_MODE_SIZE (GET_MODE (x)),
15284 reg_names[REGNO (XEXP (XEXP (x, 0), 0))]);
15285 else if (GET_CODE (XEXP (x, 0)) == PRE_MODIFY)
15286 output_address (XEXP (XEXP (x, 0), 1));
15287 else
15288 output_address (XEXP (x, 0));
15290 else
15292 if (toc_relative_expr_p (x, false))
15293 /* This hack along with a corresponding hack in
15294 rs6000_output_addr_const_extra arranges to output addends
15295 where the assembler expects to find them. eg.
15296 (plus (unspec [(symbol_ref ("x")) (reg 2)] tocrel) 4)
15297 without this hack would be output as "x@toc+4". We
15298 want "x+4@toc". */
15299 output_addr_const (file, CONST_CAST_RTX (tocrel_base));
15300 else
15301 output_addr_const (file, x);
15303 return;
15305 case '&':
15306 assemble_name (file, rs6000_get_some_local_dynamic_name ());
15307 return;
15309 default:
15310 output_operand_lossage ("invalid %%xn code");
15314 /* Print the address of an operand. */
15316 void
15317 print_operand_address (FILE *file, rtx x)
15319 if (REG_P (x))
15320 fprintf (file, "0(%s)", reg_names[ REGNO (x) ]);
15321 else if (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == CONST
15322 || GET_CODE (x) == LABEL_REF)
15324 output_addr_const (file, x);
15325 if (small_data_operand (x, GET_MODE (x)))
15326 fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
15327 reg_names[SMALL_DATA_REG]);
15328 else
15329 gcc_assert (!TARGET_TOC);
15331 else if (GET_CODE (x) == PLUS && REG_P (XEXP (x, 0))
15332 && REG_P (XEXP (x, 1)))
15334 if (REGNO (XEXP (x, 0)) == 0)
15335 fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (x, 1)) ],
15336 reg_names[ REGNO (XEXP (x, 0)) ]);
15337 else
15338 fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (x, 0)) ],
15339 reg_names[ REGNO (XEXP (x, 1)) ]);
15341 else if (GET_CODE (x) == PLUS && REG_P (XEXP (x, 0))
15342 && GET_CODE (XEXP (x, 1)) == CONST_INT)
15343 fprintf (file, HOST_WIDE_INT_PRINT_DEC "(%s)",
15344 INTVAL (XEXP (x, 1)), reg_names[ REGNO (XEXP (x, 0)) ]);
15345 #if TARGET_MACHO
15346 else if (GET_CODE (x) == LO_SUM && REG_P (XEXP (x, 0))
15347 && CONSTANT_P (XEXP (x, 1)))
15349 fprintf (file, "lo16(");
15350 output_addr_const (file, XEXP (x, 1));
15351 fprintf (file, ")(%s)", reg_names[ REGNO (XEXP (x, 0)) ]);
15353 #endif
15354 #if TARGET_ELF
15355 else if (GET_CODE (x) == LO_SUM && REG_P (XEXP (x, 0))
15356 && CONSTANT_P (XEXP (x, 1)))
15358 output_addr_const (file, XEXP (x, 1));
15359 fprintf (file, "@l(%s)", reg_names[ REGNO (XEXP (x, 0)) ]);
15361 #endif
15362 else if (toc_relative_expr_p (x, false))
15364 /* This hack along with a corresponding hack in
15365 rs6000_output_addr_const_extra arranges to output addends
15366 where the assembler expects to find them. eg.
15367 (lo_sum (reg 9)
15368 . (plus (unspec [(symbol_ref ("x")) (reg 2)] tocrel) 8))
15369 without this hack would be output as "x@toc+8@l(9)". We
15370 want "x+8@toc@l(9)". */
15371 output_addr_const (file, CONST_CAST_RTX (tocrel_base));
15372 if (GET_CODE (x) == LO_SUM)
15373 fprintf (file, "@l(%s)", reg_names[REGNO (XEXP (x, 0))]);
15374 else
15375 fprintf (file, "(%s)", reg_names[REGNO (XVECEXP (tocrel_base, 0, 1))]);
15377 else
15378 gcc_unreachable ();
15381 /* Implement TARGET_OUTPUT_ADDR_CONST_EXTRA. */
15383 static bool
15384 rs6000_output_addr_const_extra (FILE *file, rtx x)
15386 if (GET_CODE (x) == UNSPEC)
15387 switch (XINT (x, 1))
15389 case UNSPEC_TOCREL:
15390 gcc_checking_assert (GET_CODE (XVECEXP (x, 0, 0)) == SYMBOL_REF
15391 && REG_P (XVECEXP (x, 0, 1))
15392 && REGNO (XVECEXP (x, 0, 1)) == TOC_REGISTER);
15393 output_addr_const (file, XVECEXP (x, 0, 0));
15394 if (x == tocrel_base && tocrel_offset != const0_rtx)
15396 if (INTVAL (tocrel_offset) >= 0)
15397 fprintf (file, "+");
15398 output_addr_const (file, CONST_CAST_RTX (tocrel_offset));
15400 if (!TARGET_AIX || (TARGET_ELF && TARGET_MINIMAL_TOC))
15402 putc ('-', file);
15403 assemble_name (file, toc_label_name);
15405 else if (TARGET_ELF)
15406 fputs ("@toc", file);
15407 return true;
15409 #if TARGET_MACHO
15410 case UNSPEC_MACHOPIC_OFFSET:
15411 output_addr_const (file, XVECEXP (x, 0, 0));
15412 putc ('-', file);
15413 machopic_output_function_base_name (file);
15414 return true;
15415 #endif
15417 return false;
15420 /* Target hook for assembling integer objects. The PowerPC version has
15421 to handle fixup entries for relocatable code if RELOCATABLE_NEEDS_FIXUP
15422 is defined. It also needs to handle DI-mode objects on 64-bit
15423 targets. */
15425 static bool
15426 rs6000_assemble_integer (rtx x, unsigned int size, int aligned_p)
15428 #ifdef RELOCATABLE_NEEDS_FIXUP
15429 /* Special handling for SI values. */
15430 if (RELOCATABLE_NEEDS_FIXUP && size == 4 && aligned_p)
15432 static int recurse = 0;
15434 /* For -mrelocatable, we mark all addresses that need to be fixed up in
15435 the .fixup section. Since the TOC section is already relocated, we
15436 don't need to mark it here. We used to skip the text section, but it
15437 should never be valid for relocated addresses to be placed in the text
15438 section. */
15439 if (TARGET_RELOCATABLE
15440 && in_section != toc_section
15441 && !recurse
15442 && GET_CODE (x) != CONST_INT
15443 && GET_CODE (x) != CONST_DOUBLE
15444 && CONSTANT_P (x))
15446 char buf[256];
15448 recurse = 1;
15449 ASM_GENERATE_INTERNAL_LABEL (buf, "LCP", fixuplabelno);
15450 fixuplabelno++;
15451 ASM_OUTPUT_LABEL (asm_out_file, buf);
15452 fprintf (asm_out_file, "\t.long\t(");
15453 output_addr_const (asm_out_file, x);
15454 fprintf (asm_out_file, ")@fixup\n");
15455 fprintf (asm_out_file, "\t.section\t\".fixup\",\"aw\"\n");
15456 ASM_OUTPUT_ALIGN (asm_out_file, 2);
15457 fprintf (asm_out_file, "\t.long\t");
15458 assemble_name (asm_out_file, buf);
15459 fprintf (asm_out_file, "\n\t.previous\n");
15460 recurse = 0;
15461 return true;
15463 /* Remove initial .'s to turn a -mcall-aixdesc function
15464 address into the address of the descriptor, not the function
15465 itself. */
15466 else if (GET_CODE (x) == SYMBOL_REF
15467 && XSTR (x, 0)[0] == '.'
15468 && DEFAULT_ABI == ABI_AIX)
15470 const char *name = XSTR (x, 0);
15471 while (*name == '.')
15472 name++;
15474 fprintf (asm_out_file, "\t.long\t%s\n", name);
15475 return true;
15478 #endif /* RELOCATABLE_NEEDS_FIXUP */
15479 return default_assemble_integer (x, size, aligned_p);
15482 #if defined (HAVE_GAS_HIDDEN) && !TARGET_MACHO
15483 /* Emit an assembler directive to set symbol visibility for DECL to
15484 VISIBILITY_TYPE. */
15486 static void
15487 rs6000_assemble_visibility (tree decl, int vis)
15489 /* Functions need to have their entry point symbol visibility set as
15490 well as their descriptor symbol visibility. */
15491 if (DEFAULT_ABI == ABI_AIX
15492 && DOT_SYMBOLS
15493 && TREE_CODE (decl) == FUNCTION_DECL)
15495 static const char * const visibility_types[] = {
15496 NULL, "internal", "hidden", "protected"
15499 const char *name, *type;
15501 name = ((* targetm.strip_name_encoding)
15502 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl))));
15503 type = visibility_types[vis];
15505 fprintf (asm_out_file, "\t.%s\t%s\n", type, name);
15506 fprintf (asm_out_file, "\t.%s\t.%s\n", type, name);
15508 else
15509 default_assemble_visibility (decl, vis);
15511 #endif
15513 enum rtx_code
15514 rs6000_reverse_condition (enum machine_mode mode, enum rtx_code code)
15516 /* Reversal of FP compares takes care -- an ordered compare
15517 becomes an unordered compare and vice versa. */
15518 if (mode == CCFPmode
15519 && (!flag_finite_math_only
15520 || code == UNLT || code == UNLE || code == UNGT || code == UNGE
15521 || code == UNEQ || code == LTGT))
15522 return reverse_condition_maybe_unordered (code);
15523 else
15524 return reverse_condition (code);
15527 /* Generate a compare for CODE. Return a brand-new rtx that
15528 represents the result of the compare. */
15530 static rtx
15531 rs6000_generate_compare (rtx cmp, enum machine_mode mode)
15533 enum machine_mode comp_mode;
15534 rtx compare_result;
15535 enum rtx_code code = GET_CODE (cmp);
15536 rtx op0 = XEXP (cmp, 0);
15537 rtx op1 = XEXP (cmp, 1);
15539 if (FLOAT_MODE_P (mode))
15540 comp_mode = CCFPmode;
15541 else if (code == GTU || code == LTU
15542 || code == GEU || code == LEU)
15543 comp_mode = CCUNSmode;
15544 else if ((code == EQ || code == NE)
15545 && unsigned_reg_p (op0)
15546 && (unsigned_reg_p (op1)
15547 || (CONST_INT_P (op1) && INTVAL (op1) != 0)))
15548 /* These are unsigned values, perhaps there will be a later
15549 ordering compare that can be shared with this one. */
15550 comp_mode = CCUNSmode;
15551 else
15552 comp_mode = CCmode;
15554 /* If we have an unsigned compare, make sure we don't have a signed value as
15555 an immediate. */
15556 if (comp_mode == CCUNSmode && GET_CODE (op1) == CONST_INT
15557 && INTVAL (op1) < 0)
15559 op0 = copy_rtx_if_shared (op0);
15560 op1 = force_reg (GET_MODE (op0), op1);
15561 cmp = gen_rtx_fmt_ee (code, GET_MODE (cmp), op0, op1);
15564 /* First, the compare. */
15565 compare_result = gen_reg_rtx (comp_mode);
15567 /* E500 FP compare instructions on the GPRs. Yuck! */
15568 if ((!TARGET_FPRS && TARGET_HARD_FLOAT)
15569 && FLOAT_MODE_P (mode))
15571 rtx cmp, or_result, compare_result2;
15572 enum machine_mode op_mode = GET_MODE (op0);
15574 if (op_mode == VOIDmode)
15575 op_mode = GET_MODE (op1);
15577 /* The E500 FP compare instructions toggle the GT bit (CR bit 1) only.
15578 This explains the following mess. */
15580 switch (code)
15582 case EQ: case UNEQ: case NE: case LTGT:
15583 switch (op_mode)
15585 case SFmode:
15586 cmp = (flag_finite_math_only && !flag_trapping_math)
15587 ? gen_tstsfeq_gpr (compare_result, op0, op1)
15588 : gen_cmpsfeq_gpr (compare_result, op0, op1);
15589 break;
15591 case DFmode:
15592 cmp = (flag_finite_math_only && !flag_trapping_math)
15593 ? gen_tstdfeq_gpr (compare_result, op0, op1)
15594 : gen_cmpdfeq_gpr (compare_result, op0, op1);
15595 break;
15597 case TFmode:
15598 cmp = (flag_finite_math_only && !flag_trapping_math)
15599 ? gen_tsttfeq_gpr (compare_result, op0, op1)
15600 : gen_cmptfeq_gpr (compare_result, op0, op1);
15601 break;
15603 default:
15604 gcc_unreachable ();
15606 break;
15608 case GT: case GTU: case UNGT: case UNGE: case GE: case GEU:
15609 switch (op_mode)
15611 case SFmode:
15612 cmp = (flag_finite_math_only && !flag_trapping_math)
15613 ? gen_tstsfgt_gpr (compare_result, op0, op1)
15614 : gen_cmpsfgt_gpr (compare_result, op0, op1);
15615 break;
15617 case DFmode:
15618 cmp = (flag_finite_math_only && !flag_trapping_math)
15619 ? gen_tstdfgt_gpr (compare_result, op0, op1)
15620 : gen_cmpdfgt_gpr (compare_result, op0, op1);
15621 break;
15623 case TFmode:
15624 cmp = (flag_finite_math_only && !flag_trapping_math)
15625 ? gen_tsttfgt_gpr (compare_result, op0, op1)
15626 : gen_cmptfgt_gpr (compare_result, op0, op1);
15627 break;
15629 default:
15630 gcc_unreachable ();
15632 break;
15634 case LT: case LTU: case UNLT: case UNLE: case LE: case LEU:
15635 switch (op_mode)
15637 case SFmode:
15638 cmp = (flag_finite_math_only && !flag_trapping_math)
15639 ? gen_tstsflt_gpr (compare_result, op0, op1)
15640 : gen_cmpsflt_gpr (compare_result, op0, op1);
15641 break;
15643 case DFmode:
15644 cmp = (flag_finite_math_only && !flag_trapping_math)
15645 ? gen_tstdflt_gpr (compare_result, op0, op1)
15646 : gen_cmpdflt_gpr (compare_result, op0, op1);
15647 break;
15649 case TFmode:
15650 cmp = (flag_finite_math_only && !flag_trapping_math)
15651 ? gen_tsttflt_gpr (compare_result, op0, op1)
15652 : gen_cmptflt_gpr (compare_result, op0, op1);
15653 break;
15655 default:
15656 gcc_unreachable ();
15658 break;
15659 default:
15660 gcc_unreachable ();
15663 /* Synthesize LE and GE from LT/GT || EQ. */
15664 if (code == LE || code == GE || code == LEU || code == GEU)
15666 emit_insn (cmp);
15668 switch (code)
15670 case LE: code = LT; break;
15671 case GE: code = GT; break;
15672 case LEU: code = LT; break;
15673 case GEU: code = GT; break;
15674 default: gcc_unreachable ();
15677 compare_result2 = gen_reg_rtx (CCFPmode);
15679 /* Do the EQ. */
15680 switch (op_mode)
15682 case SFmode:
15683 cmp = (flag_finite_math_only && !flag_trapping_math)
15684 ? gen_tstsfeq_gpr (compare_result2, op0, op1)
15685 : gen_cmpsfeq_gpr (compare_result2, op0, op1);
15686 break;
15688 case DFmode:
15689 cmp = (flag_finite_math_only && !flag_trapping_math)
15690 ? gen_tstdfeq_gpr (compare_result2, op0, op1)
15691 : gen_cmpdfeq_gpr (compare_result2, op0, op1);
15692 break;
15694 case TFmode:
15695 cmp = (flag_finite_math_only && !flag_trapping_math)
15696 ? gen_tsttfeq_gpr (compare_result2, op0, op1)
15697 : gen_cmptfeq_gpr (compare_result2, op0, op1);
15698 break;
15700 default:
15701 gcc_unreachable ();
15703 emit_insn (cmp);
15705 /* OR them together. */
15706 or_result = gen_reg_rtx (CCFPmode);
15707 cmp = gen_e500_cr_ior_compare (or_result, compare_result,
15708 compare_result2);
15709 compare_result = or_result;
15710 code = EQ;
15712 else
15714 if (code == NE || code == LTGT)
15715 code = NE;
15716 else
15717 code = EQ;
15720 emit_insn (cmp);
15722 else
15724 /* Generate XLC-compatible TFmode compare as PARALLEL with extra
15725 CLOBBERs to match cmptf_internal2 pattern. */
15726 if (comp_mode == CCFPmode && TARGET_XL_COMPAT
15727 && GET_MODE (op0) == TFmode
15728 && !TARGET_IEEEQUAD
15729 && TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_LONG_DOUBLE_128)
15730 emit_insn (gen_rtx_PARALLEL (VOIDmode,
15731 gen_rtvec (10,
15732 gen_rtx_SET (VOIDmode,
15733 compare_result,
15734 gen_rtx_COMPARE (comp_mode, op0, op1)),
15735 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
15736 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
15737 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
15738 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
15739 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
15740 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
15741 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
15742 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
15743 gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (Pmode)))));
15744 else if (GET_CODE (op1) == UNSPEC
15745 && XINT (op1, 1) == UNSPEC_SP_TEST)
15747 rtx op1b = XVECEXP (op1, 0, 0);
15748 comp_mode = CCEQmode;
15749 compare_result = gen_reg_rtx (CCEQmode);
15750 if (TARGET_64BIT)
15751 emit_insn (gen_stack_protect_testdi (compare_result, op0, op1b));
15752 else
15753 emit_insn (gen_stack_protect_testsi (compare_result, op0, op1b));
15755 else
15756 emit_insn (gen_rtx_SET (VOIDmode, compare_result,
15757 gen_rtx_COMPARE (comp_mode, op0, op1)));
15760 /* Some kinds of FP comparisons need an OR operation;
15761 under flag_finite_math_only we don't bother. */
15762 if (FLOAT_MODE_P (mode)
15763 && !flag_finite_math_only
15764 && !(TARGET_HARD_FLOAT && !TARGET_FPRS)
15765 && (code == LE || code == GE
15766 || code == UNEQ || code == LTGT
15767 || code == UNGT || code == UNLT))
15769 enum rtx_code or1, or2;
15770 rtx or1_rtx, or2_rtx, compare2_rtx;
15771 rtx or_result = gen_reg_rtx (CCEQmode);
15773 switch (code)
15775 case LE: or1 = LT; or2 = EQ; break;
15776 case GE: or1 = GT; or2 = EQ; break;
15777 case UNEQ: or1 = UNORDERED; or2 = EQ; break;
15778 case LTGT: or1 = LT; or2 = GT; break;
15779 case UNGT: or1 = UNORDERED; or2 = GT; break;
15780 case UNLT: or1 = UNORDERED; or2 = LT; break;
15781 default: gcc_unreachable ();
15783 validate_condition_mode (or1, comp_mode);
15784 validate_condition_mode (or2, comp_mode);
15785 or1_rtx = gen_rtx_fmt_ee (or1, SImode, compare_result, const0_rtx);
15786 or2_rtx = gen_rtx_fmt_ee (or2, SImode, compare_result, const0_rtx);
15787 compare2_rtx = gen_rtx_COMPARE (CCEQmode,
15788 gen_rtx_IOR (SImode, or1_rtx, or2_rtx),
15789 const_true_rtx);
15790 emit_insn (gen_rtx_SET (VOIDmode, or_result, compare2_rtx));
15792 compare_result = or_result;
15793 code = EQ;
15796 validate_condition_mode (code, GET_MODE (compare_result));
15798 return gen_rtx_fmt_ee (code, VOIDmode, compare_result, const0_rtx);
15802 /* Emit the RTL for an sISEL pattern. */
15804 void
15805 rs6000_emit_sISEL (enum machine_mode mode ATTRIBUTE_UNUSED, rtx operands[])
15807 rs6000_emit_int_cmove (operands[0], operands[1], const1_rtx, const0_rtx);
15810 void
15811 rs6000_emit_sCOND (enum machine_mode mode, rtx operands[])
15813 rtx condition_rtx;
15814 enum machine_mode op_mode;
15815 enum rtx_code cond_code;
15816 rtx result = operands[0];
15818 if (TARGET_ISEL && (mode == SImode || mode == DImode))
15820 rs6000_emit_sISEL (mode, operands);
15821 return;
15824 condition_rtx = rs6000_generate_compare (operands[1], mode);
15825 cond_code = GET_CODE (condition_rtx);
15827 if (FLOAT_MODE_P (mode)
15828 && !TARGET_FPRS && TARGET_HARD_FLOAT)
15830 rtx t;
15832 PUT_MODE (condition_rtx, SImode);
15833 t = XEXP (condition_rtx, 0);
15835 gcc_assert (cond_code == NE || cond_code == EQ);
15837 if (cond_code == NE)
15838 emit_insn (gen_e500_flip_gt_bit (t, t));
15840 emit_insn (gen_move_from_CR_gt_bit (result, t));
15841 return;
15844 if (cond_code == NE
15845 || cond_code == GE || cond_code == LE
15846 || cond_code == GEU || cond_code == LEU
15847 || cond_code == ORDERED || cond_code == UNGE || cond_code == UNLE)
15849 rtx not_result = gen_reg_rtx (CCEQmode);
15850 rtx not_op, rev_cond_rtx;
15851 enum machine_mode cc_mode;
15853 cc_mode = GET_MODE (XEXP (condition_rtx, 0));
15855 rev_cond_rtx = gen_rtx_fmt_ee (rs6000_reverse_condition (cc_mode, cond_code),
15856 SImode, XEXP (condition_rtx, 0), const0_rtx);
15857 not_op = gen_rtx_COMPARE (CCEQmode, rev_cond_rtx, const0_rtx);
15858 emit_insn (gen_rtx_SET (VOIDmode, not_result, not_op));
15859 condition_rtx = gen_rtx_EQ (VOIDmode, not_result, const0_rtx);
15862 op_mode = GET_MODE (XEXP (operands[1], 0));
15863 if (op_mode == VOIDmode)
15864 op_mode = GET_MODE (XEXP (operands[1], 1));
15866 if (TARGET_POWERPC64 && (op_mode == DImode || FLOAT_MODE_P (mode)))
15868 PUT_MODE (condition_rtx, DImode);
15869 convert_move (result, condition_rtx, 0);
15871 else
15873 PUT_MODE (condition_rtx, SImode);
15874 emit_insn (gen_rtx_SET (VOIDmode, result, condition_rtx));
15878 /* Emit a branch of kind CODE to location LOC. */
15880 void
15881 rs6000_emit_cbranch (enum machine_mode mode, rtx operands[])
15883 rtx condition_rtx, loc_ref;
15885 condition_rtx = rs6000_generate_compare (operands[0], mode);
15886 loc_ref = gen_rtx_LABEL_REF (VOIDmode, operands[3]);
15887 emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx,
15888 gen_rtx_IF_THEN_ELSE (VOIDmode, condition_rtx,
15889 loc_ref, pc_rtx)));
15892 /* Return the string to output a conditional branch to LABEL, which is
15893 the operand template of the label, or NULL if the branch is really a
15894 conditional return.
15896 OP is the conditional expression. XEXP (OP, 0) is assumed to be a
15897 condition code register and its mode specifies what kind of
15898 comparison we made.
15900 REVERSED is nonzero if we should reverse the sense of the comparison.
15902 INSN is the insn. */
15904 char *
15905 output_cbranch (rtx op, const char *label, int reversed, rtx insn)
15907 static char string[64];
15908 enum rtx_code code = GET_CODE (op);
15909 rtx cc_reg = XEXP (op, 0);
15910 enum machine_mode mode = GET_MODE (cc_reg);
15911 int cc_regno = REGNO (cc_reg) - CR0_REGNO;
15912 int need_longbranch = label != NULL && get_attr_length (insn) == 8;
15913 int really_reversed = reversed ^ need_longbranch;
15914 char *s = string;
15915 const char *ccode;
15916 const char *pred;
15917 rtx note;
15919 validate_condition_mode (code, mode);
15921 /* Work out which way this really branches. We could use
15922 reverse_condition_maybe_unordered here always but this
15923 makes the resulting assembler clearer. */
15924 if (really_reversed)
15926 /* Reversal of FP compares takes care -- an ordered compare
15927 becomes an unordered compare and vice versa. */
15928 if (mode == CCFPmode)
15929 code = reverse_condition_maybe_unordered (code);
15930 else
15931 code = reverse_condition (code);
15934 if ((!TARGET_FPRS && TARGET_HARD_FLOAT) && mode == CCFPmode)
15936 /* The efscmp/tst* instructions twiddle bit 2, which maps nicely
15937 to the GT bit. */
15938 switch (code)
15940 case EQ:
15941 /* Opposite of GT. */
15942 code = GT;
15943 break;
15945 case NE:
15946 code = UNLE;
15947 break;
15949 default:
15950 gcc_unreachable ();
15954 switch (code)
15956 /* Not all of these are actually distinct opcodes, but
15957 we distinguish them for clarity of the resulting assembler. */
15958 case NE: case LTGT:
15959 ccode = "ne"; break;
15960 case EQ: case UNEQ:
15961 ccode = "eq"; break;
15962 case GE: case GEU:
15963 ccode = "ge"; break;
15964 case GT: case GTU: case UNGT:
15965 ccode = "gt"; break;
15966 case LE: case LEU:
15967 ccode = "le"; break;
15968 case LT: case LTU: case UNLT:
15969 ccode = "lt"; break;
15970 case UNORDERED: ccode = "un"; break;
15971 case ORDERED: ccode = "nu"; break;
15972 case UNGE: ccode = "nl"; break;
15973 case UNLE: ccode = "ng"; break;
15974 default:
15975 gcc_unreachable ();
15978 /* Maybe we have a guess as to how likely the branch is. */
15979 pred = "";
15980 note = find_reg_note (insn, REG_BR_PROB, NULL_RTX);
15981 if (note != NULL_RTX)
15983 /* PROB is the difference from 50%. */
15984 int prob = INTVAL (XEXP (note, 0)) - REG_BR_PROB_BASE / 2;
15986 /* Only hint for highly probable/improbable branches on newer
15987 cpus as static prediction overrides processor dynamic
15988 prediction. For older cpus we may as well always hint, but
15989 assume not taken for branches that are very close to 50% as a
15990 mispredicted taken branch is more expensive than a
15991 mispredicted not-taken branch. */
15992 if (rs6000_always_hint
15993 || (abs (prob) > REG_BR_PROB_BASE / 100 * 48
15994 && br_prob_note_reliable_p (note)))
15996 if (abs (prob) > REG_BR_PROB_BASE / 20
15997 && ((prob > 0) ^ need_longbranch))
15998 pred = "+";
15999 else
16000 pred = "-";
16004 if (label == NULL)
16005 s += sprintf (s, "b%slr%s ", ccode, pred);
16006 else
16007 s += sprintf (s, "b%s%s ", ccode, pred);
16009 /* We need to escape any '%' characters in the reg_names string.
16010 Assume they'd only be the first character.... */
16011 if (reg_names[cc_regno + CR0_REGNO][0] == '%')
16012 *s++ = '%';
16013 s += sprintf (s, "%s", reg_names[cc_regno + CR0_REGNO]);
16015 if (label != NULL)
16017 /* If the branch distance was too far, we may have to use an
16018 unconditional branch to go the distance. */
16019 if (need_longbranch)
16020 s += sprintf (s, ",$+8\n\tb %s", label);
16021 else
16022 s += sprintf (s, ",%s", label);
16025 return string;
16028 /* Return the string to flip the GT bit on a CR. */
16029 char *
16030 output_e500_flip_gt_bit (rtx dst, rtx src)
16032 static char string[64];
16033 int a, b;
16035 gcc_assert (GET_CODE (dst) == REG && CR_REGNO_P (REGNO (dst))
16036 && GET_CODE (src) == REG && CR_REGNO_P (REGNO (src)));
16038 /* GT bit. */
16039 a = 4 * (REGNO (dst) - CR0_REGNO) + 1;
16040 b = 4 * (REGNO (src) - CR0_REGNO) + 1;
16042 sprintf (string, "crnot %d,%d", a, b);
16043 return string;
16046 /* Return insn for VSX or Altivec comparisons. */
16048 static rtx
16049 rs6000_emit_vector_compare_inner (enum rtx_code code, rtx op0, rtx op1)
16051 rtx mask;
16052 enum machine_mode mode = GET_MODE (op0);
16054 switch (code)
16056 default:
16057 break;
16059 case GE:
16060 if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
16061 return NULL_RTX;
16063 case EQ:
16064 case GT:
16065 case GTU:
16066 case ORDERED:
16067 case UNORDERED:
16068 case UNEQ:
16069 case LTGT:
16070 mask = gen_reg_rtx (mode);
16071 emit_insn (gen_rtx_SET (VOIDmode,
16072 mask,
16073 gen_rtx_fmt_ee (code, mode, op0, op1)));
16074 return mask;
16077 return NULL_RTX;
16080 /* Emit vector compare for operands OP0 and OP1 using code RCODE.
16081 DMODE is expected destination mode. This is a recursive function. */
16083 static rtx
16084 rs6000_emit_vector_compare (enum rtx_code rcode,
16085 rtx op0, rtx op1,
16086 enum machine_mode dmode)
16088 rtx mask;
16089 bool swap_operands = false;
16090 bool try_again = false;
16092 gcc_assert (VECTOR_UNIT_ALTIVEC_OR_VSX_P (dmode));
16093 gcc_assert (GET_MODE (op0) == GET_MODE (op1));
16095 /* See if the comparison works as is. */
16096 mask = rs6000_emit_vector_compare_inner (rcode, op0, op1);
16097 if (mask)
16098 return mask;
16100 switch (rcode)
16102 case LT:
16103 rcode = GT;
16104 swap_operands = true;
16105 try_again = true;
16106 break;
16107 case LTU:
16108 rcode = GTU;
16109 swap_operands = true;
16110 try_again = true;
16111 break;
16112 case NE:
16113 case UNLE:
16114 case UNLT:
16115 case UNGE:
16116 case UNGT:
16117 /* Invert condition and try again.
16118 e.g., A != B becomes ~(A==B). */
16120 enum rtx_code rev_code;
16121 enum insn_code nor_code;
16122 rtx mask2;
16124 rev_code = reverse_condition_maybe_unordered (rcode);
16125 if (rev_code == UNKNOWN)
16126 return NULL_RTX;
16128 nor_code = optab_handler (one_cmpl_optab, dmode);
16129 if (nor_code == CODE_FOR_nothing)
16130 return NULL_RTX;
16132 mask2 = rs6000_emit_vector_compare (rev_code, op0, op1, dmode);
16133 if (!mask2)
16134 return NULL_RTX;
16136 mask = gen_reg_rtx (dmode);
16137 emit_insn (GEN_FCN (nor_code) (mask, mask2));
16138 return mask;
16140 break;
16141 case GE:
16142 case GEU:
16143 case LE:
16144 case LEU:
16145 /* Try GT/GTU/LT/LTU OR EQ */
16147 rtx c_rtx, eq_rtx;
16148 enum insn_code ior_code;
16149 enum rtx_code new_code;
16151 switch (rcode)
16153 case GE:
16154 new_code = GT;
16155 break;
16157 case GEU:
16158 new_code = GTU;
16159 break;
16161 case LE:
16162 new_code = LT;
16163 break;
16165 case LEU:
16166 new_code = LTU;
16167 break;
16169 default:
16170 gcc_unreachable ();
16173 ior_code = optab_handler (ior_optab, dmode);
16174 if (ior_code == CODE_FOR_nothing)
16175 return NULL_RTX;
16177 c_rtx = rs6000_emit_vector_compare (new_code, op0, op1, dmode);
16178 if (!c_rtx)
16179 return NULL_RTX;
16181 eq_rtx = rs6000_emit_vector_compare (EQ, op0, op1, dmode);
16182 if (!eq_rtx)
16183 return NULL_RTX;
16185 mask = gen_reg_rtx (dmode);
16186 emit_insn (GEN_FCN (ior_code) (mask, c_rtx, eq_rtx));
16187 return mask;
16189 break;
16190 default:
16191 return NULL_RTX;
16194 if (try_again)
16196 if (swap_operands)
16198 rtx tmp;
16199 tmp = op0;
16200 op0 = op1;
16201 op1 = tmp;
16204 mask = rs6000_emit_vector_compare_inner (rcode, op0, op1);
16205 if (mask)
16206 return mask;
16209 /* You only get two chances. */
16210 return NULL_RTX;
16213 /* Emit vector conditional expression. DEST is destination. OP_TRUE and
16214 OP_FALSE are two VEC_COND_EXPR operands. CC_OP0 and CC_OP1 are the two
16215 operands for the relation operation COND. */
16218 rs6000_emit_vector_cond_expr (rtx dest, rtx op_true, rtx op_false,
16219 rtx cond, rtx cc_op0, rtx cc_op1)
16221 enum machine_mode dest_mode = GET_MODE (dest);
16222 enum machine_mode mask_mode = GET_MODE (cc_op0);
16223 enum rtx_code rcode = GET_CODE (cond);
16224 enum machine_mode cc_mode = CCmode;
16225 rtx mask;
16226 rtx cond2;
16227 rtx tmp;
16228 bool invert_move = false;
16230 if (VECTOR_UNIT_NONE_P (dest_mode))
16231 return 0;
16233 gcc_assert (GET_MODE_SIZE (dest_mode) == GET_MODE_SIZE (mask_mode)
16234 && GET_MODE_NUNITS (dest_mode) == GET_MODE_NUNITS (mask_mode));
16236 switch (rcode)
16238 /* Swap operands if we can, and fall back to doing the operation as
16239 specified, and doing a NOR to invert the test. */
16240 case NE:
16241 case UNLE:
16242 case UNLT:
16243 case UNGE:
16244 case UNGT:
16245 /* Invert condition and try again.
16246 e.g., A = (B != C) ? D : E becomes A = (B == C) ? E : D. */
16247 invert_move = true;
16248 rcode = reverse_condition_maybe_unordered (rcode);
16249 if (rcode == UNKNOWN)
16250 return 0;
16251 break;
16253 /* Mark unsigned tests with CCUNSmode. */
16254 case GTU:
16255 case GEU:
16256 case LTU:
16257 case LEU:
16258 cc_mode = CCUNSmode;
16259 break;
16261 default:
16262 break;
16265 /* Get the vector mask for the given relational operations. */
16266 mask = rs6000_emit_vector_compare (rcode, cc_op0, cc_op1, mask_mode);
16268 if (!mask)
16269 return 0;
16271 if (invert_move)
16273 tmp = op_true;
16274 op_true = op_false;
16275 op_false = tmp;
16278 cond2 = gen_rtx_fmt_ee (NE, cc_mode, gen_lowpart (dest_mode, mask),
16279 CONST0_RTX (dest_mode));
16280 emit_insn (gen_rtx_SET (VOIDmode,
16281 dest,
16282 gen_rtx_IF_THEN_ELSE (dest_mode,
16283 cond2,
16284 op_true,
16285 op_false)));
16286 return 1;
16289 /* Emit a conditional move: move TRUE_COND to DEST if OP of the
16290 operands of the last comparison is nonzero/true, FALSE_COND if it
16291 is zero/false. Return 0 if the hardware has no such operation. */
16294 rs6000_emit_cmove (rtx dest, rtx op, rtx true_cond, rtx false_cond)
16296 enum rtx_code code = GET_CODE (op);
16297 rtx op0 = XEXP (op, 0);
16298 rtx op1 = XEXP (op, 1);
16299 REAL_VALUE_TYPE c1;
16300 enum machine_mode compare_mode = GET_MODE (op0);
16301 enum machine_mode result_mode = GET_MODE (dest);
16302 rtx temp;
16303 bool is_against_zero;
16305 /* These modes should always match. */
16306 if (GET_MODE (op1) != compare_mode
16307 /* In the isel case however, we can use a compare immediate, so
16308 op1 may be a small constant. */
16309 && (!TARGET_ISEL || !short_cint_operand (op1, VOIDmode)))
16310 return 0;
16311 if (GET_MODE (true_cond) != result_mode)
16312 return 0;
16313 if (GET_MODE (false_cond) != result_mode)
16314 return 0;
16316 /* Don't allow using floating point comparisons for integer results for
16317 now. */
16318 if (FLOAT_MODE_P (compare_mode) && !FLOAT_MODE_P (result_mode))
16319 return 0;
16321 /* First, work out if the hardware can do this at all, or
16322 if it's too slow.... */
16323 if (!FLOAT_MODE_P (compare_mode))
16325 if (TARGET_ISEL)
16326 return rs6000_emit_int_cmove (dest, op, true_cond, false_cond);
16327 return 0;
16329 else if (TARGET_HARD_FLOAT && !TARGET_FPRS
16330 && SCALAR_FLOAT_MODE_P (compare_mode))
16331 return 0;
16333 is_against_zero = op1 == CONST0_RTX (compare_mode);
16335 /* A floating-point subtract might overflow, underflow, or produce
16336 an inexact result, thus changing the floating-point flags, so it
16337 can't be generated if we care about that. It's safe if one side
16338 of the construct is zero, since then no subtract will be
16339 generated. */
16340 if (SCALAR_FLOAT_MODE_P (compare_mode)
16341 && flag_trapping_math && ! is_against_zero)
16342 return 0;
16344 /* Eliminate half of the comparisons by switching operands, this
16345 makes the remaining code simpler. */
16346 if (code == UNLT || code == UNGT || code == UNORDERED || code == NE
16347 || code == LTGT || code == LT || code == UNLE)
16349 code = reverse_condition_maybe_unordered (code);
16350 temp = true_cond;
16351 true_cond = false_cond;
16352 false_cond = temp;
16355 /* UNEQ and LTGT take four instructions for a comparison with zero,
16356 it'll probably be faster to use a branch here too. */
16357 if (code == UNEQ && HONOR_NANS (compare_mode))
16358 return 0;
16360 if (GET_CODE (op1) == CONST_DOUBLE)
16361 REAL_VALUE_FROM_CONST_DOUBLE (c1, op1);
16363 /* We're going to try to implement comparisons by performing
16364 a subtract, then comparing against zero. Unfortunately,
16365 Inf - Inf is NaN which is not zero, and so if we don't
16366 know that the operand is finite and the comparison
16367 would treat EQ different to UNORDERED, we can't do it. */
16368 if (HONOR_INFINITIES (compare_mode)
16369 && code != GT && code != UNGE
16370 && (GET_CODE (op1) != CONST_DOUBLE || real_isinf (&c1))
16371 /* Constructs of the form (a OP b ? a : b) are safe. */
16372 && ((! rtx_equal_p (op0, false_cond) && ! rtx_equal_p (op1, false_cond))
16373 || (! rtx_equal_p (op0, true_cond)
16374 && ! rtx_equal_p (op1, true_cond))))
16375 return 0;
16377 /* At this point we know we can use fsel. */
16379 /* Reduce the comparison to a comparison against zero. */
16380 if (! is_against_zero)
16382 temp = gen_reg_rtx (compare_mode);
16383 emit_insn (gen_rtx_SET (VOIDmode, temp,
16384 gen_rtx_MINUS (compare_mode, op0, op1)));
16385 op0 = temp;
16386 op1 = CONST0_RTX (compare_mode);
16389 /* If we don't care about NaNs we can reduce some of the comparisons
16390 down to faster ones. */
16391 if (! HONOR_NANS (compare_mode))
16392 switch (code)
16394 case GT:
16395 code = LE;
16396 temp = true_cond;
16397 true_cond = false_cond;
16398 false_cond = temp;
16399 break;
16400 case UNGE:
16401 code = GE;
16402 break;
16403 case UNEQ:
16404 code = EQ;
16405 break;
16406 default:
16407 break;
16410 /* Now, reduce everything down to a GE. */
16411 switch (code)
16413 case GE:
16414 break;
16416 case LE:
16417 temp = gen_reg_rtx (compare_mode);
16418 emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_NEG (compare_mode, op0)));
16419 op0 = temp;
16420 break;
16422 case ORDERED:
16423 temp = gen_reg_rtx (compare_mode);
16424 emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_ABS (compare_mode, op0)));
16425 op0 = temp;
16426 break;
16428 case EQ:
16429 temp = gen_reg_rtx (compare_mode);
16430 emit_insn (gen_rtx_SET (VOIDmode, temp,
16431 gen_rtx_NEG (compare_mode,
16432 gen_rtx_ABS (compare_mode, op0))));
16433 op0 = temp;
16434 break;
16436 case UNGE:
16437 /* a UNGE 0 <-> (a GE 0 || -a UNLT 0) */
16438 temp = gen_reg_rtx (result_mode);
16439 emit_insn (gen_rtx_SET (VOIDmode, temp,
16440 gen_rtx_IF_THEN_ELSE (result_mode,
16441 gen_rtx_GE (VOIDmode,
16442 op0, op1),
16443 true_cond, false_cond)));
16444 false_cond = true_cond;
16445 true_cond = temp;
16447 temp = gen_reg_rtx (compare_mode);
16448 emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_NEG (compare_mode, op0)));
16449 op0 = temp;
16450 break;
16452 case GT:
16453 /* a GT 0 <-> (a GE 0 && -a UNLT 0) */
16454 temp = gen_reg_rtx (result_mode);
16455 emit_insn (gen_rtx_SET (VOIDmode, temp,
16456 gen_rtx_IF_THEN_ELSE (result_mode,
16457 gen_rtx_GE (VOIDmode,
16458 op0, op1),
16459 true_cond, false_cond)));
16460 true_cond = false_cond;
16461 false_cond = temp;
16463 temp = gen_reg_rtx (compare_mode);
16464 emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_NEG (compare_mode, op0)));
16465 op0 = temp;
16466 break;
16468 default:
16469 gcc_unreachable ();
16472 emit_insn (gen_rtx_SET (VOIDmode, dest,
16473 gen_rtx_IF_THEN_ELSE (result_mode,
16474 gen_rtx_GE (VOIDmode,
16475 op0, op1),
16476 true_cond, false_cond)));
16477 return 1;
16480 /* Same as above, but for ints (isel). */
16482 static int
16483 rs6000_emit_int_cmove (rtx dest, rtx op, rtx true_cond, rtx false_cond)
16485 rtx condition_rtx, cr;
16486 enum machine_mode mode = GET_MODE (dest);
16487 enum rtx_code cond_code;
16488 rtx (*isel_func) (rtx, rtx, rtx, rtx, rtx);
16489 bool signedp;
16491 if (mode != SImode && (!TARGET_POWERPC64 || mode != DImode))
16492 return 0;
16494 /* We still have to do the compare, because isel doesn't do a
16495 compare, it just looks at the CRx bits set by a previous compare
16496 instruction. */
16497 condition_rtx = rs6000_generate_compare (op, mode);
16498 cond_code = GET_CODE (condition_rtx);
16499 cr = XEXP (condition_rtx, 0);
16500 signedp = GET_MODE (cr) == CCmode;
16502 isel_func = (mode == SImode
16503 ? (signedp ? gen_isel_signed_si : gen_isel_unsigned_si)
16504 : (signedp ? gen_isel_signed_di : gen_isel_unsigned_di));
16506 switch (cond_code)
16508 case LT: case GT: case LTU: case GTU: case EQ:
16509 /* isel handles these directly. */
16510 break;
16512 default:
16513 /* We need to swap the sense of the comparison. */
16515 rtx t = true_cond;
16516 true_cond = false_cond;
16517 false_cond = t;
16518 PUT_CODE (condition_rtx, reverse_condition (cond_code));
16520 break;
16523 false_cond = force_reg (mode, false_cond);
16524 if (true_cond != const0_rtx)
16525 true_cond = force_reg (mode, true_cond);
16527 emit_insn (isel_func (dest, condition_rtx, true_cond, false_cond, cr));
16529 return 1;
16532 const char *
16533 output_isel (rtx *operands)
16535 enum rtx_code code;
16537 code = GET_CODE (operands[1]);
16539 if (code == GE || code == GEU || code == LE || code == LEU || code == NE)
16541 gcc_assert (GET_CODE (operands[2]) == REG
16542 && GET_CODE (operands[3]) == REG);
16543 PUT_CODE (operands[1], reverse_condition (code));
16544 return "isel %0,%3,%2,%j1";
16547 return "isel %0,%2,%3,%j1";
16550 void
16551 rs6000_emit_minmax (rtx dest, enum rtx_code code, rtx op0, rtx op1)
16553 enum machine_mode mode = GET_MODE (op0);
16554 enum rtx_code c;
16555 rtx target;
16557 /* VSX/altivec have direct min/max insns. */
16558 if ((code == SMAX || code == SMIN)
16559 && (VECTOR_UNIT_ALTIVEC_OR_VSX_P (mode)
16560 || (mode == SFmode && VECTOR_UNIT_VSX_P (DFmode))))
16562 emit_insn (gen_rtx_SET (VOIDmode,
16563 dest,
16564 gen_rtx_fmt_ee (code, mode, op0, op1)));
16565 return;
16568 if (code == SMAX || code == SMIN)
16569 c = GE;
16570 else
16571 c = GEU;
16573 if (code == SMAX || code == UMAX)
16574 target = emit_conditional_move (dest, c, op0, op1, mode,
16575 op0, op1, mode, 0);
16576 else
16577 target = emit_conditional_move (dest, c, op0, op1, mode,
16578 op1, op0, mode, 0);
16579 gcc_assert (target);
16580 if (target != dest)
16581 emit_move_insn (dest, target);
16584 /* A subroutine of the atomic operation splitters. Jump to LABEL if
16585 COND is true. Mark the jump as unlikely to be taken. */
16587 static void
16588 emit_unlikely_jump (rtx cond, rtx label)
16590 rtx very_unlikely = GEN_INT (REG_BR_PROB_BASE / 100 - 1);
16591 rtx x;
16593 x = gen_rtx_IF_THEN_ELSE (VOIDmode, cond, label, pc_rtx);
16594 x = emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, x));
16595 add_reg_note (x, REG_BR_PROB, very_unlikely);
16598 /* A subroutine of the atomic operation splitters. Emit a load-locked
16599 instruction in MODE. */
16601 static void
16602 emit_load_locked (enum machine_mode mode, rtx reg, rtx mem)
16604 rtx (*fn) (rtx, rtx) = NULL;
16606 switch (mode)
16608 case SImode:
16609 fn = gen_load_lockedsi;
16610 break;
16611 case DImode:
16612 fn = gen_load_lockeddi;
16613 break;
16614 default:
16615 gcc_unreachable ();
16617 emit_insn (fn (reg, mem));
16620 /* A subroutine of the atomic operation splitters. Emit a store-conditional
16621 instruction in MODE. */
16623 static void
16624 emit_store_conditional (enum machine_mode mode, rtx res, rtx mem, rtx val)
16626 rtx (*fn) (rtx, rtx, rtx) = NULL;
16628 switch (mode)
16630 case SImode:
16631 fn = gen_store_conditionalsi;
16632 break;
16633 case DImode:
16634 fn = gen_store_conditionaldi;
16635 break;
16636 default:
16637 gcc_unreachable ();
16640 /* Emit sync before stwcx. to address PPC405 Erratum. */
16641 if (PPC405_ERRATUM77)
16642 emit_insn (gen_hwsync ());
16644 emit_insn (fn (res, mem, val));
16647 /* Expand barriers before and after a load_locked/store_cond sequence. */
16649 static rtx
16650 rs6000_pre_atomic_barrier (rtx mem, enum memmodel model)
16652 rtx addr = XEXP (mem, 0);
16653 int strict_p = (reload_in_progress || reload_completed);
16655 if (!legitimate_indirect_address_p (addr, strict_p)
16656 && !legitimate_indexed_address_p (addr, strict_p))
16658 addr = force_reg (Pmode, addr);
16659 mem = replace_equiv_address_nv (mem, addr);
16662 switch (model)
16664 case MEMMODEL_RELAXED:
16665 case MEMMODEL_CONSUME:
16666 case MEMMODEL_ACQUIRE:
16667 break;
16668 case MEMMODEL_RELEASE:
16669 case MEMMODEL_ACQ_REL:
16670 emit_insn (gen_lwsync ());
16671 break;
16672 case MEMMODEL_SEQ_CST:
16673 emit_insn (gen_hwsync ());
16674 break;
16675 default:
16676 gcc_unreachable ();
16678 return mem;
16681 static void
16682 rs6000_post_atomic_barrier (enum memmodel model)
16684 switch (model)
16686 case MEMMODEL_RELAXED:
16687 case MEMMODEL_CONSUME:
16688 case MEMMODEL_RELEASE:
16689 break;
16690 case MEMMODEL_ACQUIRE:
16691 case MEMMODEL_ACQ_REL:
16692 case MEMMODEL_SEQ_CST:
16693 emit_insn (gen_isync ());
16694 break;
16695 default:
16696 gcc_unreachable ();
16700 /* A subroutine of the various atomic expanders. For sub-word operations,
16701 we must adjust things to operate on SImode. Given the original MEM,
16702 return a new aligned memory. Also build and return the quantities by
16703 which to shift and mask. */
16705 static rtx
16706 rs6000_adjust_atomic_subword (rtx orig_mem, rtx *pshift, rtx *pmask)
16708 rtx addr, align, shift, mask, mem;
16709 HOST_WIDE_INT shift_mask;
16710 enum machine_mode mode = GET_MODE (orig_mem);
16712 /* For smaller modes, we have to implement this via SImode. */
16713 shift_mask = (mode == QImode ? 0x18 : 0x10);
16715 addr = XEXP (orig_mem, 0);
16716 addr = force_reg (GET_MODE (addr), addr);
16718 /* Aligned memory containing subword. Generate a new memory. We
16719 do not want any of the existing MEM_ATTR data, as we're now
16720 accessing memory outside the original object. */
16721 align = expand_simple_binop (Pmode, AND, addr, GEN_INT (-4),
16722 NULL_RTX, 1, OPTAB_LIB_WIDEN);
16723 mem = gen_rtx_MEM (SImode, align);
16724 MEM_VOLATILE_P (mem) = MEM_VOLATILE_P (orig_mem);
16725 if (MEM_ALIAS_SET (orig_mem) == ALIAS_SET_MEMORY_BARRIER)
16726 set_mem_alias_set (mem, ALIAS_SET_MEMORY_BARRIER);
16728 /* Shift amount for subword relative to aligned word. */
16729 shift = gen_reg_rtx (SImode);
16730 addr = gen_lowpart (SImode, addr);
16731 emit_insn (gen_rlwinm (shift, addr, GEN_INT (3), GEN_INT (shift_mask)));
16732 shift = expand_simple_binop (SImode, XOR, shift, GEN_INT (shift_mask),
16733 shift, 1, OPTAB_LIB_WIDEN);
16734 *pshift = shift;
16736 /* Mask for insertion. */
16737 mask = expand_simple_binop (SImode, ASHIFT, GEN_INT (GET_MODE_MASK (mode)),
16738 shift, NULL_RTX, 1, OPTAB_LIB_WIDEN);
16739 *pmask = mask;
16741 return mem;
16744 /* A subroutine of the various atomic expanders. For sub-word operands,
16745 combine OLDVAL and NEWVAL via MASK. Returns a new pseduo. */
16747 static rtx
16748 rs6000_mask_atomic_subword (rtx oldval, rtx newval, rtx mask)
16750 rtx x;
16752 x = gen_reg_rtx (SImode);
16753 emit_insn (gen_rtx_SET (VOIDmode, x,
16754 gen_rtx_AND (SImode,
16755 gen_rtx_NOT (SImode, mask),
16756 oldval)));
16758 x = expand_simple_binop (SImode, IOR, newval, x, x, 1, OPTAB_LIB_WIDEN);
16760 return x;
16763 /* A subroutine of the various atomic expanders. For sub-word operands,
16764 extract WIDE to NARROW via SHIFT. */
16766 static void
16767 rs6000_finish_atomic_subword (rtx narrow, rtx wide, rtx shift)
16769 wide = expand_simple_binop (SImode, LSHIFTRT, wide, shift,
16770 wide, 1, OPTAB_LIB_WIDEN);
16771 emit_move_insn (narrow, gen_lowpart (GET_MODE (narrow), wide));
16774 /* Expand an atomic compare and swap operation. */
16776 void
16777 rs6000_expand_atomic_compare_and_swap (rtx operands[])
16779 rtx boolval, retval, mem, oldval, newval, cond;
16780 rtx label1, label2, x, mask, shift;
16781 enum machine_mode mode;
16782 enum memmodel mod_s, mod_f;
16783 bool is_weak;
16785 boolval = operands[0];
16786 retval = operands[1];
16787 mem = operands[2];
16788 oldval = operands[3];
16789 newval = operands[4];
16790 is_weak = (INTVAL (operands[5]) != 0);
16791 mod_s = (enum memmodel) INTVAL (operands[6]);
16792 mod_f = (enum memmodel) INTVAL (operands[7]);
16793 mode = GET_MODE (mem);
16795 mask = shift = NULL_RTX;
16796 if (mode == QImode || mode == HImode)
16798 mem = rs6000_adjust_atomic_subword (mem, &shift, &mask);
16800 /* Shift and mask OLDVAL into position with the word. */
16801 oldval = convert_modes (SImode, mode, oldval, 1);
16802 oldval = expand_simple_binop (SImode, ASHIFT, oldval, shift,
16803 NULL_RTX, 1, OPTAB_LIB_WIDEN);
16805 /* Shift and mask NEWVAL into position within the word. */
16806 newval = convert_modes (SImode, mode, newval, 1);
16807 newval = expand_simple_binop (SImode, ASHIFT, newval, shift,
16808 NULL_RTX, 1, OPTAB_LIB_WIDEN);
16810 /* Prepare to adjust the return value. */
16811 retval = gen_reg_rtx (SImode);
16812 mode = SImode;
16814 else if (reg_overlap_mentioned_p (retval, oldval))
16815 oldval = copy_to_reg (oldval);
16817 mem = rs6000_pre_atomic_barrier (mem, mod_s);
16819 label1 = NULL_RTX;
16820 if (!is_weak)
16822 label1 = gen_rtx_LABEL_REF (VOIDmode, gen_label_rtx ());
16823 emit_label (XEXP (label1, 0));
16825 label2 = gen_rtx_LABEL_REF (VOIDmode, gen_label_rtx ());
16827 emit_load_locked (mode, retval, mem);
16829 x = retval;
16830 if (mask)
16832 x = expand_simple_binop (SImode, AND, retval, mask,
16833 NULL_RTX, 1, OPTAB_LIB_WIDEN);
16836 cond = gen_reg_rtx (CCmode);
16837 x = gen_rtx_COMPARE (CCmode, x, oldval);
16838 emit_insn (gen_rtx_SET (VOIDmode, cond, x));
16840 x = gen_rtx_NE (VOIDmode, cond, const0_rtx);
16841 emit_unlikely_jump (x, label2);
16843 x = newval;
16844 if (mask)
16845 x = rs6000_mask_atomic_subword (retval, newval, mask);
16847 emit_store_conditional (mode, cond, mem, x);
16849 if (!is_weak)
16851 x = gen_rtx_NE (VOIDmode, cond, const0_rtx);
16852 emit_unlikely_jump (x, label1);
16855 if (mod_f != MEMMODEL_RELAXED)
16856 emit_label (XEXP (label2, 0));
16858 rs6000_post_atomic_barrier (mod_s);
16860 if (mod_f == MEMMODEL_RELAXED)
16861 emit_label (XEXP (label2, 0));
16863 if (shift)
16864 rs6000_finish_atomic_subword (operands[1], retval, shift);
16866 /* In all cases, CR0 contains EQ on success, and NE on failure. */
16867 x = gen_rtx_EQ (SImode, cond, const0_rtx);
16868 emit_insn (gen_rtx_SET (VOIDmode, boolval, x));
16871 /* Expand an atomic exchange operation. */
16873 void
16874 rs6000_expand_atomic_exchange (rtx operands[])
16876 rtx retval, mem, val, cond;
16877 enum machine_mode mode;
16878 enum memmodel model;
16879 rtx label, x, mask, shift;
16881 retval = operands[0];
16882 mem = operands[1];
16883 val = operands[2];
16884 model = (enum memmodel) INTVAL (operands[3]);
16885 mode = GET_MODE (mem);
16887 mask = shift = NULL_RTX;
16888 if (mode == QImode || mode == HImode)
16890 mem = rs6000_adjust_atomic_subword (mem, &shift, &mask);
16892 /* Shift and mask VAL into position with the word. */
16893 val = convert_modes (SImode, mode, val, 1);
16894 val = expand_simple_binop (SImode, ASHIFT, val, shift,
16895 NULL_RTX, 1, OPTAB_LIB_WIDEN);
16897 /* Prepare to adjust the return value. */
16898 retval = gen_reg_rtx (SImode);
16899 mode = SImode;
16902 mem = rs6000_pre_atomic_barrier (mem, model);
16904 label = gen_rtx_LABEL_REF (VOIDmode, gen_label_rtx ());
16905 emit_label (XEXP (label, 0));
16907 emit_load_locked (mode, retval, mem);
16909 x = val;
16910 if (mask)
16911 x = rs6000_mask_atomic_subword (retval, val, mask);
16913 cond = gen_reg_rtx (CCmode);
16914 emit_store_conditional (mode, cond, mem, x);
16916 x = gen_rtx_NE (VOIDmode, cond, const0_rtx);
16917 emit_unlikely_jump (x, label);
16919 rs6000_post_atomic_barrier (model);
16921 if (shift)
16922 rs6000_finish_atomic_subword (operands[0], retval, shift);
16925 /* Expand an atomic fetch-and-operate pattern. CODE is the binary operation
16926 to perform. MEM is the memory on which to operate. VAL is the second
16927 operand of the binary operator. BEFORE and AFTER are optional locations to
16928 return the value of MEM either before of after the operation. MODEL_RTX
16929 is a CONST_INT containing the memory model to use. */
16931 void
16932 rs6000_expand_atomic_op (enum rtx_code code, rtx mem, rtx val,
16933 rtx orig_before, rtx orig_after, rtx model_rtx)
16935 enum memmodel model = (enum memmodel) INTVAL (model_rtx);
16936 enum machine_mode mode = GET_MODE (mem);
16937 rtx label, x, cond, mask, shift;
16938 rtx before = orig_before, after = orig_after;
16940 mask = shift = NULL_RTX;
16941 if (mode == QImode || mode == HImode)
16943 mem = rs6000_adjust_atomic_subword (mem, &shift, &mask);
16945 /* Shift and mask VAL into position with the word. */
16946 val = convert_modes (SImode, mode, val, 1);
16947 val = expand_simple_binop (SImode, ASHIFT, val, shift,
16948 NULL_RTX, 1, OPTAB_LIB_WIDEN);
16950 switch (code)
16952 case IOR:
16953 case XOR:
16954 /* We've already zero-extended VAL. That is sufficient to
16955 make certain that it does not affect other bits. */
16956 mask = NULL;
16957 break;
16959 case AND:
16960 /* If we make certain that all of the other bits in VAL are
16961 set, that will be sufficient to not affect other bits. */
16962 x = gen_rtx_NOT (SImode, mask);
16963 x = gen_rtx_IOR (SImode, x, val);
16964 emit_insn (gen_rtx_SET (VOIDmode, val, x));
16965 mask = NULL;
16966 break;
16968 case NOT:
16969 case PLUS:
16970 case MINUS:
16971 /* These will all affect bits outside the field and need
16972 adjustment via MASK within the loop. */
16973 break;
16975 default:
16976 gcc_unreachable ();
16979 /* Prepare to adjust the return value. */
16980 before = gen_reg_rtx (SImode);
16981 if (after)
16982 after = gen_reg_rtx (SImode);
16983 mode = SImode;
16986 mem = rs6000_pre_atomic_barrier (mem, model);
16988 label = gen_label_rtx ();
16989 emit_label (label);
16990 label = gen_rtx_LABEL_REF (VOIDmode, label);
16992 if (before == NULL_RTX)
16993 before = gen_reg_rtx (mode);
16995 emit_load_locked (mode, before, mem);
16997 if (code == NOT)
16999 x = expand_simple_binop (mode, AND, before, val,
17000 NULL_RTX, 1, OPTAB_LIB_WIDEN);
17001 after = expand_simple_unop (mode, NOT, x, after, 1);
17003 else
17005 after = expand_simple_binop (mode, code, before, val,
17006 after, 1, OPTAB_LIB_WIDEN);
17009 x = after;
17010 if (mask)
17012 x = expand_simple_binop (SImode, AND, after, mask,
17013 NULL_RTX, 1, OPTAB_LIB_WIDEN);
17014 x = rs6000_mask_atomic_subword (before, x, mask);
17017 cond = gen_reg_rtx (CCmode);
17018 emit_store_conditional (mode, cond, mem, x);
17020 x = gen_rtx_NE (VOIDmode, cond, const0_rtx);
17021 emit_unlikely_jump (x, label);
17023 rs6000_post_atomic_barrier (model);
17025 if (shift)
17027 if (orig_before)
17028 rs6000_finish_atomic_subword (orig_before, before, shift);
17029 if (orig_after)
17030 rs6000_finish_atomic_subword (orig_after, after, shift);
17032 else if (orig_after && after != orig_after)
17033 emit_move_insn (orig_after, after);
17036 /* Emit instructions to move SRC to DST. Called by splitters for
17037 multi-register moves. It will emit at most one instruction for
17038 each register that is accessed; that is, it won't emit li/lis pairs
17039 (or equivalent for 64-bit code). One of SRC or DST must be a hard
17040 register. */
17042 void
17043 rs6000_split_multireg_move (rtx dst, rtx src)
17045 /* The register number of the first register being moved. */
17046 int reg;
17047 /* The mode that is to be moved. */
17048 enum machine_mode mode;
17049 /* The mode that the move is being done in, and its size. */
17050 enum machine_mode reg_mode;
17051 int reg_mode_size;
17052 /* The number of registers that will be moved. */
17053 int nregs;
17055 reg = REG_P (dst) ? REGNO (dst) : REGNO (src);
17056 mode = GET_MODE (dst);
17057 nregs = hard_regno_nregs[reg][mode];
17058 if (FP_REGNO_P (reg))
17059 reg_mode = DECIMAL_FLOAT_MODE_P (mode) ? DDmode :
17060 ((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT) ? DFmode : SFmode);
17061 else if (ALTIVEC_REGNO_P (reg))
17062 reg_mode = V16QImode;
17063 else if (TARGET_E500_DOUBLE && mode == TFmode)
17064 reg_mode = DFmode;
17065 else
17066 reg_mode = word_mode;
17067 reg_mode_size = GET_MODE_SIZE (reg_mode);
17069 gcc_assert (reg_mode_size * nregs == GET_MODE_SIZE (mode));
17071 if (REG_P (src) && REG_P (dst) && (REGNO (src) < REGNO (dst)))
17073 /* Move register range backwards, if we might have destructive
17074 overlap. */
17075 int i;
17076 for (i = nregs - 1; i >= 0; i--)
17077 emit_insn (gen_rtx_SET (VOIDmode,
17078 simplify_gen_subreg (reg_mode, dst, mode,
17079 i * reg_mode_size),
17080 simplify_gen_subreg (reg_mode, src, mode,
17081 i * reg_mode_size)));
17083 else
17085 int i;
17086 int j = -1;
17087 bool used_update = false;
17088 rtx restore_basereg = NULL_RTX;
17090 if (MEM_P (src) && INT_REGNO_P (reg))
17092 rtx breg;
17094 if (GET_CODE (XEXP (src, 0)) == PRE_INC
17095 || GET_CODE (XEXP (src, 0)) == PRE_DEC)
17097 rtx delta_rtx;
17098 breg = XEXP (XEXP (src, 0), 0);
17099 delta_rtx = (GET_CODE (XEXP (src, 0)) == PRE_INC
17100 ? GEN_INT (GET_MODE_SIZE (GET_MODE (src)))
17101 : GEN_INT (-GET_MODE_SIZE (GET_MODE (src))));
17102 emit_insn (gen_add3_insn (breg, breg, delta_rtx));
17103 src = replace_equiv_address (src, breg);
17105 else if (! rs6000_offsettable_memref_p (src, reg_mode))
17107 if (GET_CODE (XEXP (src, 0)) == PRE_MODIFY)
17109 rtx basereg = XEXP (XEXP (src, 0), 0);
17110 if (TARGET_UPDATE)
17112 rtx ndst = simplify_gen_subreg (reg_mode, dst, mode, 0);
17113 emit_insn (gen_rtx_SET (VOIDmode, ndst,
17114 gen_rtx_MEM (reg_mode, XEXP (src, 0))));
17115 used_update = true;
17117 else
17118 emit_insn (gen_rtx_SET (VOIDmode, basereg,
17119 XEXP (XEXP (src, 0), 1)));
17120 src = replace_equiv_address (src, basereg);
17122 else
17124 rtx basereg = gen_rtx_REG (Pmode, reg);
17125 emit_insn (gen_rtx_SET (VOIDmode, basereg, XEXP (src, 0)));
17126 src = replace_equiv_address (src, basereg);
17130 breg = XEXP (src, 0);
17131 if (GET_CODE (breg) == PLUS || GET_CODE (breg) == LO_SUM)
17132 breg = XEXP (breg, 0);
17134 /* If the base register we are using to address memory is
17135 also a destination reg, then change that register last. */
17136 if (REG_P (breg)
17137 && REGNO (breg) >= REGNO (dst)
17138 && REGNO (breg) < REGNO (dst) + nregs)
17139 j = REGNO (breg) - REGNO (dst);
17141 else if (MEM_P (dst) && INT_REGNO_P (reg))
17143 rtx breg;
17145 if (GET_CODE (XEXP (dst, 0)) == PRE_INC
17146 || GET_CODE (XEXP (dst, 0)) == PRE_DEC)
17148 rtx delta_rtx;
17149 breg = XEXP (XEXP (dst, 0), 0);
17150 delta_rtx = (GET_CODE (XEXP (dst, 0)) == PRE_INC
17151 ? GEN_INT (GET_MODE_SIZE (GET_MODE (dst)))
17152 : GEN_INT (-GET_MODE_SIZE (GET_MODE (dst))));
17154 /* We have to update the breg before doing the store.
17155 Use store with update, if available. */
17157 if (TARGET_UPDATE)
17159 rtx nsrc = simplify_gen_subreg (reg_mode, src, mode, 0);
17160 emit_insn (TARGET_32BIT
17161 ? (TARGET_POWERPC64
17162 ? gen_movdi_si_update (breg, breg, delta_rtx, nsrc)
17163 : gen_movsi_update (breg, breg, delta_rtx, nsrc))
17164 : gen_movdi_di_update (breg, breg, delta_rtx, nsrc));
17165 used_update = true;
17167 else
17168 emit_insn (gen_add3_insn (breg, breg, delta_rtx));
17169 dst = replace_equiv_address (dst, breg);
17171 else if (!rs6000_offsettable_memref_p (dst, reg_mode)
17172 && GET_CODE (XEXP (dst, 0)) != LO_SUM)
17174 if (GET_CODE (XEXP (dst, 0)) == PRE_MODIFY)
17176 rtx basereg = XEXP (XEXP (dst, 0), 0);
17177 if (TARGET_UPDATE)
17179 rtx nsrc = simplify_gen_subreg (reg_mode, src, mode, 0);
17180 emit_insn (gen_rtx_SET (VOIDmode,
17181 gen_rtx_MEM (reg_mode, XEXP (dst, 0)), nsrc));
17182 used_update = true;
17184 else
17185 emit_insn (gen_rtx_SET (VOIDmode, basereg,
17186 XEXP (XEXP (dst, 0), 1)));
17187 dst = replace_equiv_address (dst, basereg);
17189 else
17191 rtx basereg = XEXP (XEXP (dst, 0), 0);
17192 rtx offsetreg = XEXP (XEXP (dst, 0), 1);
17193 gcc_assert (GET_CODE (XEXP (dst, 0)) == PLUS
17194 && REG_P (basereg)
17195 && REG_P (offsetreg)
17196 && REGNO (basereg) != REGNO (offsetreg));
17197 if (REGNO (basereg) == 0)
17199 rtx tmp = offsetreg;
17200 offsetreg = basereg;
17201 basereg = tmp;
17203 emit_insn (gen_add3_insn (basereg, basereg, offsetreg));
17204 restore_basereg = gen_sub3_insn (basereg, basereg, offsetreg);
17205 dst = replace_equiv_address (dst, basereg);
17208 else if (GET_CODE (XEXP (dst, 0)) != LO_SUM)
17209 gcc_assert (rs6000_offsettable_memref_p (dst, reg_mode));
17212 for (i = 0; i < nregs; i++)
17214 /* Calculate index to next subword. */
17215 ++j;
17216 if (j == nregs)
17217 j = 0;
17219 /* If compiler already emitted move of first word by
17220 store with update, no need to do anything. */
17221 if (j == 0 && used_update)
17222 continue;
17224 emit_insn (gen_rtx_SET (VOIDmode,
17225 simplify_gen_subreg (reg_mode, dst, mode,
17226 j * reg_mode_size),
17227 simplify_gen_subreg (reg_mode, src, mode,
17228 j * reg_mode_size)));
17230 if (restore_basereg != NULL_RTX)
17231 emit_insn (restore_basereg);
17236 /* This page contains routines that are used to determine what the
17237 function prologue and epilogue code will do and write them out. */
17239 static inline bool
17240 save_reg_p (int r)
17242 return !call_used_regs[r] && df_regs_ever_live_p (r);
17245 /* Return the first fixed-point register that is required to be
17246 saved. 32 if none. */
17249 first_reg_to_save (void)
17251 int first_reg;
17253 /* Find lowest numbered live register. */
17254 for (first_reg = 13; first_reg <= 31; first_reg++)
17255 if (save_reg_p (first_reg))
17256 break;
17258 if (first_reg > RS6000_PIC_OFFSET_TABLE_REGNUM
17259 && ((DEFAULT_ABI == ABI_V4 && flag_pic != 0)
17260 || (DEFAULT_ABI == ABI_DARWIN && flag_pic)
17261 || (TARGET_TOC && TARGET_MINIMAL_TOC))
17262 && df_regs_ever_live_p (RS6000_PIC_OFFSET_TABLE_REGNUM))
17263 first_reg = RS6000_PIC_OFFSET_TABLE_REGNUM;
17265 #if TARGET_MACHO
17266 if (flag_pic
17267 && crtl->uses_pic_offset_table
17268 && first_reg > RS6000_PIC_OFFSET_TABLE_REGNUM)
17269 return RS6000_PIC_OFFSET_TABLE_REGNUM;
17270 #endif
17272 return first_reg;
17275 /* Similar, for FP regs. */
17278 first_fp_reg_to_save (void)
17280 int first_reg;
17282 /* Find lowest numbered live register. */
17283 for (first_reg = 14 + 32; first_reg <= 63; first_reg++)
17284 if (save_reg_p (first_reg))
17285 break;
17287 return first_reg;
17290 /* Similar, for AltiVec regs. */
17292 static int
17293 first_altivec_reg_to_save (void)
17295 int i;
17297 /* Stack frame remains as is unless we are in AltiVec ABI. */
17298 if (! TARGET_ALTIVEC_ABI)
17299 return LAST_ALTIVEC_REGNO + 1;
17301 /* On Darwin, the unwind routines are compiled without
17302 TARGET_ALTIVEC, and use save_world to save/restore the
17303 altivec registers when necessary. */
17304 if (DEFAULT_ABI == ABI_DARWIN && crtl->calls_eh_return
17305 && ! TARGET_ALTIVEC)
17306 return FIRST_ALTIVEC_REGNO + 20;
17308 /* Find lowest numbered live register. */
17309 for (i = FIRST_ALTIVEC_REGNO + 20; i <= LAST_ALTIVEC_REGNO; ++i)
17310 if (save_reg_p (i))
17311 break;
17313 return i;
17316 /* Return a 32-bit mask of the AltiVec registers we need to set in
17317 VRSAVE. Bit n of the return value is 1 if Vn is live. The MSB in
17318 the 32-bit word is 0. */
17320 static unsigned int
17321 compute_vrsave_mask (void)
17323 unsigned int i, mask = 0;
17325 /* On Darwin, the unwind routines are compiled without
17326 TARGET_ALTIVEC, and use save_world to save/restore the
17327 call-saved altivec registers when necessary. */
17328 if (DEFAULT_ABI == ABI_DARWIN && crtl->calls_eh_return
17329 && ! TARGET_ALTIVEC)
17330 mask |= 0xFFF;
17332 /* First, find out if we use _any_ altivec registers. */
17333 for (i = FIRST_ALTIVEC_REGNO; i <= LAST_ALTIVEC_REGNO; ++i)
17334 if (df_regs_ever_live_p (i))
17335 mask |= ALTIVEC_REG_BIT (i);
17337 if (mask == 0)
17338 return mask;
17340 /* Next, remove the argument registers from the set. These must
17341 be in the VRSAVE mask set by the caller, so we don't need to add
17342 them in again. More importantly, the mask we compute here is
17343 used to generate CLOBBERs in the set_vrsave insn, and we do not
17344 wish the argument registers to die. */
17345 for (i = crtl->args.info.vregno - 1; i >= ALTIVEC_ARG_MIN_REG; --i)
17346 mask &= ~ALTIVEC_REG_BIT (i);
17348 /* Similarly, remove the return value from the set. */
17350 bool yes = false;
17351 diddle_return_value (is_altivec_return_reg, &yes);
17352 if (yes)
17353 mask &= ~ALTIVEC_REG_BIT (ALTIVEC_ARG_RETURN);
17356 return mask;
17359 /* For a very restricted set of circumstances, we can cut down the
17360 size of prologues/epilogues by calling our own save/restore-the-world
17361 routines. */
17363 static void
17364 compute_save_world_info (rs6000_stack_t *info_ptr)
17366 info_ptr->world_save_p = 1;
17367 info_ptr->world_save_p
17368 = (WORLD_SAVE_P (info_ptr)
17369 && DEFAULT_ABI == ABI_DARWIN
17370 && !cfun->has_nonlocal_label
17371 && info_ptr->first_fp_reg_save == FIRST_SAVED_FP_REGNO
17372 && info_ptr->first_gp_reg_save == FIRST_SAVED_GP_REGNO
17373 && info_ptr->first_altivec_reg_save == FIRST_SAVED_ALTIVEC_REGNO
17374 && info_ptr->cr_save_p);
17376 /* This will not work in conjunction with sibcalls. Make sure there
17377 are none. (This check is expensive, but seldom executed.) */
17378 if (WORLD_SAVE_P (info_ptr))
17380 rtx insn;
17381 for ( insn = get_last_insn_anywhere (); insn; insn = PREV_INSN (insn))
17382 if ( GET_CODE (insn) == CALL_INSN
17383 && SIBLING_CALL_P (insn))
17385 info_ptr->world_save_p = 0;
17386 break;
17390 if (WORLD_SAVE_P (info_ptr))
17392 /* Even if we're not touching VRsave, make sure there's room on the
17393 stack for it, if it looks like we're calling SAVE_WORLD, which
17394 will attempt to save it. */
17395 info_ptr->vrsave_size = 4;
17397 /* If we are going to save the world, we need to save the link register too. */
17398 info_ptr->lr_save_p = 1;
17400 /* "Save" the VRsave register too if we're saving the world. */
17401 if (info_ptr->vrsave_mask == 0)
17402 info_ptr->vrsave_mask = compute_vrsave_mask ();
17404 /* Because the Darwin register save/restore routines only handle
17405 F14 .. F31 and V20 .. V31 as per the ABI, perform a consistency
17406 check. */
17407 gcc_assert (info_ptr->first_fp_reg_save >= FIRST_SAVED_FP_REGNO
17408 && (info_ptr->first_altivec_reg_save
17409 >= FIRST_SAVED_ALTIVEC_REGNO));
17411 return;
17415 static void
17416 is_altivec_return_reg (rtx reg, void *xyes)
17418 bool *yes = (bool *) xyes;
17419 if (REGNO (reg) == ALTIVEC_ARG_RETURN)
17420 *yes = true;
17424 /* Look for user-defined global regs in the range FIRST to LAST-1.
17425 We should not restore these, and so cannot use lmw or out-of-line
17426 restore functions if there are any. We also can't save them
17427 (well, emit frame notes for them), because frame unwinding during
17428 exception handling will restore saved registers. */
17430 static bool
17431 global_regs_p (unsigned first, unsigned last)
17433 while (first < last)
17434 if (global_regs[first++])
17435 return true;
17436 return false;
17439 /* Determine the strategy for savings/restoring registers. */
17441 enum {
17442 SAVRES_MULTIPLE = 0x1,
17443 SAVE_INLINE_FPRS = 0x2,
17444 SAVE_INLINE_GPRS = 0x4,
17445 REST_INLINE_FPRS = 0x8,
17446 REST_INLINE_GPRS = 0x10,
17447 SAVE_NOINLINE_GPRS_SAVES_LR = 0x20,
17448 SAVE_NOINLINE_FPRS_SAVES_LR = 0x40,
17449 REST_NOINLINE_FPRS_DOESNT_RESTORE_LR = 0x80,
17450 SAVE_INLINE_VRS = 0x100,
17451 REST_INLINE_VRS = 0x200
17454 static int
17455 rs6000_savres_strategy (rs6000_stack_t *info,
17456 bool using_static_chain_p)
17458 int strategy = 0;
17459 bool lr_save_p;
17461 if (TARGET_MULTIPLE
17462 && !TARGET_POWERPC64
17463 && !(TARGET_SPE_ABI && info->spe_64bit_regs_used)
17464 && info->first_gp_reg_save < 31
17465 && !global_regs_p (info->first_gp_reg_save, 32))
17466 strategy |= SAVRES_MULTIPLE;
17468 if (crtl->calls_eh_return
17469 || cfun->machine->ra_need_lr)
17470 strategy |= (SAVE_INLINE_FPRS | REST_INLINE_FPRS
17471 | SAVE_INLINE_GPRS | REST_INLINE_GPRS
17472 | SAVE_INLINE_VRS | REST_INLINE_VRS);
17474 if (info->first_fp_reg_save == 64
17475 /* The out-of-line FP routines use double-precision stores;
17476 we can't use those routines if we don't have such stores. */
17477 || (TARGET_HARD_FLOAT && !TARGET_DOUBLE_FLOAT)
17478 || global_regs_p (info->first_fp_reg_save, 64))
17479 strategy |= SAVE_INLINE_FPRS | REST_INLINE_FPRS;
17481 if (info->first_gp_reg_save == 32
17482 || (!(strategy & SAVRES_MULTIPLE)
17483 && global_regs_p (info->first_gp_reg_save, 32)))
17484 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
17486 if (info->first_altivec_reg_save == LAST_ALTIVEC_REGNO + 1
17487 || global_regs_p (info->first_altivec_reg_save, LAST_ALTIVEC_REGNO + 1))
17488 strategy |= SAVE_INLINE_VRS | REST_INLINE_VRS;
17490 /* Define cutoff for using out-of-line functions to save registers. */
17491 if (DEFAULT_ABI == ABI_V4 || TARGET_ELF)
17493 if (!optimize_size)
17495 strategy |= SAVE_INLINE_FPRS | REST_INLINE_FPRS;
17496 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
17497 strategy |= SAVE_INLINE_VRS | REST_INLINE_VRS;
17499 else
17501 /* Prefer out-of-line restore if it will exit. */
17502 if (info->first_fp_reg_save > 61)
17503 strategy |= SAVE_INLINE_FPRS;
17504 if (info->first_gp_reg_save > 29)
17506 if (info->first_fp_reg_save == 64)
17507 strategy |= SAVE_INLINE_GPRS;
17508 else
17509 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
17511 if (info->first_altivec_reg_save == LAST_ALTIVEC_REGNO)
17512 strategy |= SAVE_INLINE_VRS | REST_INLINE_VRS;
17515 else if (DEFAULT_ABI == ABI_DARWIN)
17517 if (info->first_fp_reg_save > 60)
17518 strategy |= SAVE_INLINE_FPRS | REST_INLINE_FPRS;
17519 if (info->first_gp_reg_save > 29)
17520 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
17521 strategy |= SAVE_INLINE_VRS | REST_INLINE_VRS;
17523 else
17525 gcc_checking_assert (DEFAULT_ABI == ABI_AIX);
17526 if (info->first_fp_reg_save > 61)
17527 strategy |= SAVE_INLINE_FPRS | REST_INLINE_FPRS;
17528 strategy |= SAVE_INLINE_GPRS | REST_INLINE_GPRS;
17529 strategy |= SAVE_INLINE_VRS | REST_INLINE_VRS;
17532 /* Don't bother to try to save things out-of-line if r11 is occupied
17533 by the static chain. It would require too much fiddling and the
17534 static chain is rarely used anyway. FPRs are saved w.r.t the stack
17535 pointer on Darwin, and AIX uses r1 or r12. */
17536 if (using_static_chain_p && DEFAULT_ABI != ABI_AIX)
17537 strategy |= ((DEFAULT_ABI == ABI_DARWIN ? 0 : SAVE_INLINE_FPRS)
17538 | SAVE_INLINE_GPRS
17539 | SAVE_INLINE_VRS | REST_INLINE_VRS);
17541 /* We can only use the out-of-line routines to restore if we've
17542 saved all the registers from first_fp_reg_save in the prologue.
17543 Otherwise, we risk loading garbage. */
17544 if ((strategy & (SAVE_INLINE_FPRS | REST_INLINE_FPRS)) == SAVE_INLINE_FPRS)
17546 int i;
17548 for (i = info->first_fp_reg_save; i < 64; i++)
17549 if (!save_reg_p (i))
17551 strategy |= REST_INLINE_FPRS;
17552 break;
17556 /* If we are going to use store multiple, then don't even bother
17557 with the out-of-line routines, since the store-multiple
17558 instruction will always be smaller. */
17559 if ((strategy & SAVRES_MULTIPLE))
17560 strategy |= SAVE_INLINE_GPRS;
17562 /* info->lr_save_p isn't yet set if the only reason lr needs to be
17563 saved is an out-of-line save or restore. Set up the value for
17564 the next test (excluding out-of-line gpr restore). */
17565 lr_save_p = (info->lr_save_p
17566 || !(strategy & SAVE_INLINE_GPRS)
17567 || !(strategy & SAVE_INLINE_FPRS)
17568 || !(strategy & SAVE_INLINE_VRS)
17569 || !(strategy & REST_INLINE_FPRS)
17570 || !(strategy & REST_INLINE_VRS));
17572 /* The situation is more complicated with load multiple. We'd
17573 prefer to use the out-of-line routines for restores, since the
17574 "exit" out-of-line routines can handle the restore of LR and the
17575 frame teardown. However if doesn't make sense to use the
17576 out-of-line routine if that is the only reason we'd need to save
17577 LR, and we can't use the "exit" out-of-line gpr restore if we
17578 have saved some fprs; In those cases it is advantageous to use
17579 load multiple when available. */
17580 if ((strategy & SAVRES_MULTIPLE)
17581 && (!lr_save_p
17582 || info->first_fp_reg_save != 64))
17583 strategy |= REST_INLINE_GPRS;
17585 /* Saving CR interferes with the exit routines used on the SPE, so
17586 just punt here. */
17587 if (TARGET_SPE_ABI
17588 && info->spe_64bit_regs_used
17589 && info->cr_save_p)
17590 strategy |= REST_INLINE_GPRS;
17592 /* We can only use load multiple or the out-of-line routines to
17593 restore if we've used store multiple or out-of-line routines
17594 in the prologue, i.e. if we've saved all the registers from
17595 first_gp_reg_save. Otherwise, we risk loading garbage. */
17596 if ((strategy & (SAVE_INLINE_GPRS | REST_INLINE_GPRS | SAVRES_MULTIPLE))
17597 == SAVE_INLINE_GPRS)
17599 int i;
17601 for (i = info->first_gp_reg_save; i < 32; i++)
17602 if (!save_reg_p (i))
17604 strategy |= REST_INLINE_GPRS;
17605 break;
17609 if (TARGET_ELF && TARGET_64BIT)
17611 if (!(strategy & SAVE_INLINE_FPRS))
17612 strategy |= SAVE_NOINLINE_FPRS_SAVES_LR;
17613 else if (!(strategy & SAVE_INLINE_GPRS)
17614 && info->first_fp_reg_save == 64)
17615 strategy |= SAVE_NOINLINE_GPRS_SAVES_LR;
17617 else if (TARGET_AIX && !(strategy & REST_INLINE_FPRS))
17618 strategy |= REST_NOINLINE_FPRS_DOESNT_RESTORE_LR;
17620 if (TARGET_MACHO && !(strategy & SAVE_INLINE_FPRS))
17621 strategy |= SAVE_NOINLINE_FPRS_SAVES_LR;
17623 return strategy;
17626 /* Calculate the stack information for the current function. This is
17627 complicated by having two separate calling sequences, the AIX calling
17628 sequence and the V.4 calling sequence.
17630 AIX (and Darwin/Mac OS X) stack frames look like:
17631 32-bit 64-bit
17632 SP----> +---------------------------------------+
17633 | back chain to caller | 0 0
17634 +---------------------------------------+
17635 | saved CR | 4 8 (8-11)
17636 +---------------------------------------+
17637 | saved LR | 8 16
17638 +---------------------------------------+
17639 | reserved for compilers | 12 24
17640 +---------------------------------------+
17641 | reserved for binders | 16 32
17642 +---------------------------------------+
17643 | saved TOC pointer | 20 40
17644 +---------------------------------------+
17645 | Parameter save area (P) | 24 48
17646 +---------------------------------------+
17647 | Alloca space (A) | 24+P etc.
17648 +---------------------------------------+
17649 | Local variable space (L) | 24+P+A
17650 +---------------------------------------+
17651 | Float/int conversion temporary (X) | 24+P+A+L
17652 +---------------------------------------+
17653 | Save area for AltiVec registers (W) | 24+P+A+L+X
17654 +---------------------------------------+
17655 | AltiVec alignment padding (Y) | 24+P+A+L+X+W
17656 +---------------------------------------+
17657 | Save area for VRSAVE register (Z) | 24+P+A+L+X+W+Y
17658 +---------------------------------------+
17659 | Save area for GP registers (G) | 24+P+A+X+L+X+W+Y+Z
17660 +---------------------------------------+
17661 | Save area for FP registers (F) | 24+P+A+X+L+X+W+Y+Z+G
17662 +---------------------------------------+
17663 old SP->| back chain to caller's caller |
17664 +---------------------------------------+
17666 The required alignment for AIX configurations is two words (i.e., 8
17667 or 16 bytes).
17670 V.4 stack frames look like:
17672 SP----> +---------------------------------------+
17673 | back chain to caller | 0
17674 +---------------------------------------+
17675 | caller's saved LR | 4
17676 +---------------------------------------+
17677 | Parameter save area (P) | 8
17678 +---------------------------------------+
17679 | Alloca space (A) | 8+P
17680 +---------------------------------------+
17681 | Varargs save area (V) | 8+P+A
17682 +---------------------------------------+
17683 | Local variable space (L) | 8+P+A+V
17684 +---------------------------------------+
17685 | Float/int conversion temporary (X) | 8+P+A+V+L
17686 +---------------------------------------+
17687 | Save area for AltiVec registers (W) | 8+P+A+V+L+X
17688 +---------------------------------------+
17689 | AltiVec alignment padding (Y) | 8+P+A+V+L+X+W
17690 +---------------------------------------+
17691 | Save area for VRSAVE register (Z) | 8+P+A+V+L+X+W+Y
17692 +---------------------------------------+
17693 | SPE: area for 64-bit GP registers |
17694 +---------------------------------------+
17695 | SPE alignment padding |
17696 +---------------------------------------+
17697 | saved CR (C) | 8+P+A+V+L+X+W+Y+Z
17698 +---------------------------------------+
17699 | Save area for GP registers (G) | 8+P+A+V+L+X+W+Y+Z+C
17700 +---------------------------------------+
17701 | Save area for FP registers (F) | 8+P+A+V+L+X+W+Y+Z+C+G
17702 +---------------------------------------+
17703 old SP->| back chain to caller's caller |
17704 +---------------------------------------+
17706 The required alignment for V.4 is 16 bytes, or 8 bytes if -meabi is
17707 given. (But note below and in sysv4.h that we require only 8 and
17708 may round up the size of our stack frame anyways. The historical
17709 reason is early versions of powerpc-linux which didn't properly
17710 align the stack at program startup. A happy side-effect is that
17711 -mno-eabi libraries can be used with -meabi programs.)
17713 The EABI configuration defaults to the V.4 layout. However,
17714 the stack alignment requirements may differ. If -mno-eabi is not
17715 given, the required stack alignment is 8 bytes; if -mno-eabi is
17716 given, the required alignment is 16 bytes. (But see V.4 comment
17717 above.) */
17719 #ifndef ABI_STACK_BOUNDARY
17720 #define ABI_STACK_BOUNDARY STACK_BOUNDARY
17721 #endif
17723 static rs6000_stack_t *
17724 rs6000_stack_info (void)
17726 rs6000_stack_t *info_ptr = &stack_info;
17727 int reg_size = TARGET_32BIT ? 4 : 8;
17728 int ehrd_size;
17729 int save_align;
17730 int first_gp;
17731 HOST_WIDE_INT non_fixed_size;
17732 bool using_static_chain_p;
17734 if (reload_completed && info_ptr->reload_completed)
17735 return info_ptr;
17737 memset (info_ptr, 0, sizeof (*info_ptr));
17738 info_ptr->reload_completed = reload_completed;
17740 if (TARGET_SPE)
17742 /* Cache value so we don't rescan instruction chain over and over. */
17743 if (cfun->machine->insn_chain_scanned_p == 0)
17744 cfun->machine->insn_chain_scanned_p
17745 = spe_func_has_64bit_regs_p () + 1;
17746 info_ptr->spe_64bit_regs_used = cfun->machine->insn_chain_scanned_p - 1;
17749 /* Select which calling sequence. */
17750 info_ptr->abi = DEFAULT_ABI;
17752 /* Calculate which registers need to be saved & save area size. */
17753 info_ptr->first_gp_reg_save = first_reg_to_save ();
17754 /* Assume that we will have to save RS6000_PIC_OFFSET_TABLE_REGNUM,
17755 even if it currently looks like we won't. Reload may need it to
17756 get at a constant; if so, it will have already created a constant
17757 pool entry for it. */
17758 if (((TARGET_TOC && TARGET_MINIMAL_TOC)
17759 || (flag_pic == 1 && DEFAULT_ABI == ABI_V4)
17760 || (flag_pic && DEFAULT_ABI == ABI_DARWIN))
17761 && crtl->uses_const_pool
17762 && info_ptr->first_gp_reg_save > RS6000_PIC_OFFSET_TABLE_REGNUM)
17763 first_gp = RS6000_PIC_OFFSET_TABLE_REGNUM;
17764 else
17765 first_gp = info_ptr->first_gp_reg_save;
17767 info_ptr->gp_size = reg_size * (32 - first_gp);
17769 /* For the SPE, we have an additional upper 32-bits on each GPR.
17770 Ideally we should save the entire 64-bits only when the upper
17771 half is used in SIMD instructions. Since we only record
17772 registers live (not the size they are used in), this proves
17773 difficult because we'd have to traverse the instruction chain at
17774 the right time, taking reload into account. This is a real pain,
17775 so we opt to save the GPRs in 64-bits always if but one register
17776 gets used in 64-bits. Otherwise, all the registers in the frame
17777 get saved in 32-bits.
17779 So... since when we save all GPRs (except the SP) in 64-bits, the
17780 traditional GP save area will be empty. */
17781 if (TARGET_SPE_ABI && info_ptr->spe_64bit_regs_used != 0)
17782 info_ptr->gp_size = 0;
17784 info_ptr->first_fp_reg_save = first_fp_reg_to_save ();
17785 info_ptr->fp_size = 8 * (64 - info_ptr->first_fp_reg_save);
17787 info_ptr->first_altivec_reg_save = first_altivec_reg_to_save ();
17788 info_ptr->altivec_size = 16 * (LAST_ALTIVEC_REGNO + 1
17789 - info_ptr->first_altivec_reg_save);
17791 /* Does this function call anything? */
17792 info_ptr->calls_p = (! crtl->is_leaf
17793 || cfun->machine->ra_needs_full_frame);
17795 /* Determine if we need to save the condition code registers. */
17796 if (df_regs_ever_live_p (CR2_REGNO)
17797 || df_regs_ever_live_p (CR3_REGNO)
17798 || df_regs_ever_live_p (CR4_REGNO))
17800 info_ptr->cr_save_p = 1;
17801 if (DEFAULT_ABI == ABI_V4)
17802 info_ptr->cr_size = reg_size;
17805 /* If the current function calls __builtin_eh_return, then we need
17806 to allocate stack space for registers that will hold data for
17807 the exception handler. */
17808 if (crtl->calls_eh_return)
17810 unsigned int i;
17811 for (i = 0; EH_RETURN_DATA_REGNO (i) != INVALID_REGNUM; ++i)
17812 continue;
17814 /* SPE saves EH registers in 64-bits. */
17815 ehrd_size = i * (TARGET_SPE_ABI
17816 && info_ptr->spe_64bit_regs_used != 0
17817 ? UNITS_PER_SPE_WORD : UNITS_PER_WORD);
17819 else
17820 ehrd_size = 0;
17822 /* Determine various sizes. */
17823 info_ptr->reg_size = reg_size;
17824 info_ptr->fixed_size = RS6000_SAVE_AREA;
17825 info_ptr->vars_size = RS6000_ALIGN (get_frame_size (), 8);
17826 info_ptr->parm_size = RS6000_ALIGN (crtl->outgoing_args_size,
17827 TARGET_ALTIVEC ? 16 : 8);
17828 if (FRAME_GROWS_DOWNWARD)
17829 info_ptr->vars_size
17830 += RS6000_ALIGN (info_ptr->fixed_size + info_ptr->vars_size
17831 + info_ptr->parm_size,
17832 ABI_STACK_BOUNDARY / BITS_PER_UNIT)
17833 - (info_ptr->fixed_size + info_ptr->vars_size
17834 + info_ptr->parm_size);
17836 if (TARGET_SPE_ABI && info_ptr->spe_64bit_regs_used != 0)
17837 info_ptr->spe_gp_size = 8 * (32 - first_gp);
17838 else
17839 info_ptr->spe_gp_size = 0;
17841 if (TARGET_ALTIVEC_ABI)
17842 info_ptr->vrsave_mask = compute_vrsave_mask ();
17843 else
17844 info_ptr->vrsave_mask = 0;
17846 if (TARGET_ALTIVEC_VRSAVE && info_ptr->vrsave_mask)
17847 info_ptr->vrsave_size = 4;
17848 else
17849 info_ptr->vrsave_size = 0;
17851 compute_save_world_info (info_ptr);
17853 /* Calculate the offsets. */
17854 switch (DEFAULT_ABI)
17856 case ABI_NONE:
17857 default:
17858 gcc_unreachable ();
17860 case ABI_AIX:
17861 case ABI_DARWIN:
17862 info_ptr->fp_save_offset = - info_ptr->fp_size;
17863 info_ptr->gp_save_offset = info_ptr->fp_save_offset - info_ptr->gp_size;
17865 if (TARGET_ALTIVEC_ABI)
17867 info_ptr->vrsave_save_offset
17868 = info_ptr->gp_save_offset - info_ptr->vrsave_size;
17870 /* Align stack so vector save area is on a quadword boundary.
17871 The padding goes above the vectors. */
17872 if (info_ptr->altivec_size != 0)
17873 info_ptr->altivec_padding_size
17874 = info_ptr->vrsave_save_offset & 0xF;
17875 else
17876 info_ptr->altivec_padding_size = 0;
17878 info_ptr->altivec_save_offset
17879 = info_ptr->vrsave_save_offset
17880 - info_ptr->altivec_padding_size
17881 - info_ptr->altivec_size;
17882 gcc_assert (info_ptr->altivec_size == 0
17883 || info_ptr->altivec_save_offset % 16 == 0);
17885 /* Adjust for AltiVec case. */
17886 info_ptr->ehrd_offset = info_ptr->altivec_save_offset - ehrd_size;
17888 else
17889 info_ptr->ehrd_offset = info_ptr->gp_save_offset - ehrd_size;
17890 info_ptr->cr_save_offset = reg_size; /* first word when 64-bit. */
17891 info_ptr->lr_save_offset = 2*reg_size;
17892 break;
17894 case ABI_V4:
17895 info_ptr->fp_save_offset = - info_ptr->fp_size;
17896 info_ptr->gp_save_offset = info_ptr->fp_save_offset - info_ptr->gp_size;
17897 info_ptr->cr_save_offset = info_ptr->gp_save_offset - info_ptr->cr_size;
17899 if (TARGET_SPE_ABI && info_ptr->spe_64bit_regs_used != 0)
17901 /* Align stack so SPE GPR save area is aligned on a
17902 double-word boundary. */
17903 if (info_ptr->spe_gp_size != 0 && info_ptr->cr_save_offset != 0)
17904 info_ptr->spe_padding_size
17905 = 8 - (-info_ptr->cr_save_offset % 8);
17906 else
17907 info_ptr->spe_padding_size = 0;
17909 info_ptr->spe_gp_save_offset
17910 = info_ptr->cr_save_offset
17911 - info_ptr->spe_padding_size
17912 - info_ptr->spe_gp_size;
17914 /* Adjust for SPE case. */
17915 info_ptr->ehrd_offset = info_ptr->spe_gp_save_offset;
17917 else if (TARGET_ALTIVEC_ABI)
17919 info_ptr->vrsave_save_offset
17920 = info_ptr->cr_save_offset - info_ptr->vrsave_size;
17922 /* Align stack so vector save area is on a quadword boundary. */
17923 if (info_ptr->altivec_size != 0)
17924 info_ptr->altivec_padding_size
17925 = 16 - (-info_ptr->vrsave_save_offset % 16);
17926 else
17927 info_ptr->altivec_padding_size = 0;
17929 info_ptr->altivec_save_offset
17930 = info_ptr->vrsave_save_offset
17931 - info_ptr->altivec_padding_size
17932 - info_ptr->altivec_size;
17934 /* Adjust for AltiVec case. */
17935 info_ptr->ehrd_offset = info_ptr->altivec_save_offset;
17937 else
17938 info_ptr->ehrd_offset = info_ptr->cr_save_offset;
17939 info_ptr->ehrd_offset -= ehrd_size;
17940 info_ptr->lr_save_offset = reg_size;
17941 break;
17944 save_align = (TARGET_ALTIVEC_ABI || DEFAULT_ABI == ABI_DARWIN) ? 16 : 8;
17945 info_ptr->save_size = RS6000_ALIGN (info_ptr->fp_size
17946 + info_ptr->gp_size
17947 + info_ptr->altivec_size
17948 + info_ptr->altivec_padding_size
17949 + info_ptr->spe_gp_size
17950 + info_ptr->spe_padding_size
17951 + ehrd_size
17952 + info_ptr->cr_size
17953 + info_ptr->vrsave_size,
17954 save_align);
17956 non_fixed_size = (info_ptr->vars_size
17957 + info_ptr->parm_size
17958 + info_ptr->save_size);
17960 info_ptr->total_size = RS6000_ALIGN (non_fixed_size + info_ptr->fixed_size,
17961 ABI_STACK_BOUNDARY / BITS_PER_UNIT);
17963 /* Determine if we need to save the link register. */
17964 if (info_ptr->calls_p
17965 || (DEFAULT_ABI == ABI_AIX
17966 && crtl->profile
17967 && !TARGET_PROFILE_KERNEL)
17968 || (DEFAULT_ABI == ABI_V4 && cfun->calls_alloca)
17969 #ifdef TARGET_RELOCATABLE
17970 || (TARGET_RELOCATABLE && (get_pool_size () != 0))
17971 #endif
17972 || rs6000_ra_ever_killed ())
17973 info_ptr->lr_save_p = 1;
17975 using_static_chain_p = (cfun->static_chain_decl != NULL_TREE
17976 && df_regs_ever_live_p (STATIC_CHAIN_REGNUM)
17977 && call_used_regs[STATIC_CHAIN_REGNUM]);
17978 info_ptr->savres_strategy = rs6000_savres_strategy (info_ptr,
17979 using_static_chain_p);
17981 if (!(info_ptr->savres_strategy & SAVE_INLINE_GPRS)
17982 || !(info_ptr->savres_strategy & SAVE_INLINE_FPRS)
17983 || !(info_ptr->savres_strategy & SAVE_INLINE_VRS)
17984 || !(info_ptr->savres_strategy & REST_INLINE_GPRS)
17985 || !(info_ptr->savres_strategy & REST_INLINE_FPRS)
17986 || !(info_ptr->savres_strategy & REST_INLINE_VRS))
17987 info_ptr->lr_save_p = 1;
17989 if (info_ptr->lr_save_p)
17990 df_set_regs_ever_live (LR_REGNO, true);
17992 /* Determine if we need to allocate any stack frame:
17994 For AIX we need to push the stack if a frame pointer is needed
17995 (because the stack might be dynamically adjusted), if we are
17996 debugging, if we make calls, or if the sum of fp_save, gp_save,
17997 and local variables are more than the space needed to save all
17998 non-volatile registers: 32-bit: 18*8 + 19*4 = 220 or 64-bit: 18*8
17999 + 18*8 = 288 (GPR13 reserved).
18001 For V.4 we don't have the stack cushion that AIX uses, but assume
18002 that the debugger can handle stackless frames. */
18004 if (info_ptr->calls_p)
18005 info_ptr->push_p = 1;
18007 else if (DEFAULT_ABI == ABI_V4)
18008 info_ptr->push_p = non_fixed_size != 0;
18010 else if (frame_pointer_needed)
18011 info_ptr->push_p = 1;
18013 else if (TARGET_XCOFF && write_symbols != NO_DEBUG)
18014 info_ptr->push_p = 1;
18016 else
18017 info_ptr->push_p = non_fixed_size > (TARGET_32BIT ? 220 : 288);
18019 /* Zero offsets if we're not saving those registers. */
18020 if (info_ptr->fp_size == 0)
18021 info_ptr->fp_save_offset = 0;
18023 if (info_ptr->gp_size == 0)
18024 info_ptr->gp_save_offset = 0;
18026 if (! TARGET_ALTIVEC_ABI || info_ptr->altivec_size == 0)
18027 info_ptr->altivec_save_offset = 0;
18029 /* Zero VRSAVE offset if not saved and restored. */
18030 if (! TARGET_ALTIVEC_VRSAVE || info_ptr->vrsave_mask == 0)
18031 info_ptr->vrsave_save_offset = 0;
18033 if (! TARGET_SPE_ABI
18034 || info_ptr->spe_64bit_regs_used == 0
18035 || info_ptr->spe_gp_size == 0)
18036 info_ptr->spe_gp_save_offset = 0;
18038 if (! info_ptr->lr_save_p)
18039 info_ptr->lr_save_offset = 0;
18041 if (! info_ptr->cr_save_p)
18042 info_ptr->cr_save_offset = 0;
18044 return info_ptr;
18047 /* Return true if the current function uses any GPRs in 64-bit SIMD
18048 mode. */
18050 static bool
18051 spe_func_has_64bit_regs_p (void)
18053 rtx insns, insn;
18055 /* Functions that save and restore all the call-saved registers will
18056 need to save/restore the registers in 64-bits. */
18057 if (crtl->calls_eh_return
18058 || cfun->calls_setjmp
18059 || crtl->has_nonlocal_goto)
18060 return true;
18062 insns = get_insns ();
18064 for (insn = NEXT_INSN (insns); insn != NULL_RTX; insn = NEXT_INSN (insn))
18066 if (INSN_P (insn))
18068 rtx i;
18070 /* FIXME: This should be implemented with attributes...
18072 (set_attr "spe64" "true")....then,
18073 if (get_spe64(insn)) return true;
18075 It's the only reliable way to do the stuff below. */
18077 i = PATTERN (insn);
18078 if (GET_CODE (i) == SET)
18080 enum machine_mode mode = GET_MODE (SET_SRC (i));
18082 if (SPE_VECTOR_MODE (mode))
18083 return true;
18084 if (TARGET_E500_DOUBLE && (mode == DFmode || mode == TFmode))
18085 return true;
18090 return false;
18093 static void
18094 debug_stack_info (rs6000_stack_t *info)
18096 const char *abi_string;
18098 if (! info)
18099 info = rs6000_stack_info ();
18101 fprintf (stderr, "\nStack information for function %s:\n",
18102 ((current_function_decl && DECL_NAME (current_function_decl))
18103 ? IDENTIFIER_POINTER (DECL_NAME (current_function_decl))
18104 : "<unknown>"));
18106 switch (info->abi)
18108 default: abi_string = "Unknown"; break;
18109 case ABI_NONE: abi_string = "NONE"; break;
18110 case ABI_AIX: abi_string = "AIX"; break;
18111 case ABI_DARWIN: abi_string = "Darwin"; break;
18112 case ABI_V4: abi_string = "V.4"; break;
18115 fprintf (stderr, "\tABI = %5s\n", abi_string);
18117 if (TARGET_ALTIVEC_ABI)
18118 fprintf (stderr, "\tALTIVEC ABI extensions enabled.\n");
18120 if (TARGET_SPE_ABI)
18121 fprintf (stderr, "\tSPE ABI extensions enabled.\n");
18123 if (info->first_gp_reg_save != 32)
18124 fprintf (stderr, "\tfirst_gp_reg_save = %5d\n", info->first_gp_reg_save);
18126 if (info->first_fp_reg_save != 64)
18127 fprintf (stderr, "\tfirst_fp_reg_save = %5d\n", info->first_fp_reg_save);
18129 if (info->first_altivec_reg_save <= LAST_ALTIVEC_REGNO)
18130 fprintf (stderr, "\tfirst_altivec_reg_save = %5d\n",
18131 info->first_altivec_reg_save);
18133 if (info->lr_save_p)
18134 fprintf (stderr, "\tlr_save_p = %5d\n", info->lr_save_p);
18136 if (info->cr_save_p)
18137 fprintf (stderr, "\tcr_save_p = %5d\n", info->cr_save_p);
18139 if (info->vrsave_mask)
18140 fprintf (stderr, "\tvrsave_mask = 0x%x\n", info->vrsave_mask);
18142 if (info->push_p)
18143 fprintf (stderr, "\tpush_p = %5d\n", info->push_p);
18145 if (info->calls_p)
18146 fprintf (stderr, "\tcalls_p = %5d\n", info->calls_p);
18148 if (info->gp_save_offset)
18149 fprintf (stderr, "\tgp_save_offset = %5d\n", info->gp_save_offset);
18151 if (info->fp_save_offset)
18152 fprintf (stderr, "\tfp_save_offset = %5d\n", info->fp_save_offset);
18154 if (info->altivec_save_offset)
18155 fprintf (stderr, "\taltivec_save_offset = %5d\n",
18156 info->altivec_save_offset);
18158 if (info->spe_gp_save_offset)
18159 fprintf (stderr, "\tspe_gp_save_offset = %5d\n",
18160 info->spe_gp_save_offset);
18162 if (info->vrsave_save_offset)
18163 fprintf (stderr, "\tvrsave_save_offset = %5d\n",
18164 info->vrsave_save_offset);
18166 if (info->lr_save_offset)
18167 fprintf (stderr, "\tlr_save_offset = %5d\n", info->lr_save_offset);
18169 if (info->cr_save_offset)
18170 fprintf (stderr, "\tcr_save_offset = %5d\n", info->cr_save_offset);
18172 if (info->varargs_save_offset)
18173 fprintf (stderr, "\tvarargs_save_offset = %5d\n", info->varargs_save_offset);
18175 if (info->total_size)
18176 fprintf (stderr, "\ttotal_size = "HOST_WIDE_INT_PRINT_DEC"\n",
18177 info->total_size);
18179 if (info->vars_size)
18180 fprintf (stderr, "\tvars_size = "HOST_WIDE_INT_PRINT_DEC"\n",
18181 info->vars_size);
18183 if (info->parm_size)
18184 fprintf (stderr, "\tparm_size = %5d\n", info->parm_size);
18186 if (info->fixed_size)
18187 fprintf (stderr, "\tfixed_size = %5d\n", info->fixed_size);
18189 if (info->gp_size)
18190 fprintf (stderr, "\tgp_size = %5d\n", info->gp_size);
18192 if (info->spe_gp_size)
18193 fprintf (stderr, "\tspe_gp_size = %5d\n", info->spe_gp_size);
18195 if (info->fp_size)
18196 fprintf (stderr, "\tfp_size = %5d\n", info->fp_size);
18198 if (info->altivec_size)
18199 fprintf (stderr, "\taltivec_size = %5d\n", info->altivec_size);
18201 if (info->vrsave_size)
18202 fprintf (stderr, "\tvrsave_size = %5d\n", info->vrsave_size);
18204 if (info->altivec_padding_size)
18205 fprintf (stderr, "\taltivec_padding_size= %5d\n",
18206 info->altivec_padding_size);
18208 if (info->spe_padding_size)
18209 fprintf (stderr, "\tspe_padding_size = %5d\n",
18210 info->spe_padding_size);
18212 if (info->cr_size)
18213 fprintf (stderr, "\tcr_size = %5d\n", info->cr_size);
18215 if (info->save_size)
18216 fprintf (stderr, "\tsave_size = %5d\n", info->save_size);
18218 if (info->reg_size != 4)
18219 fprintf (stderr, "\treg_size = %5d\n", info->reg_size);
18221 fprintf (stderr, "\tsave-strategy = %04x\n", info->savres_strategy);
18223 fprintf (stderr, "\n");
18227 rs6000_return_addr (int count, rtx frame)
18229 /* Currently we don't optimize very well between prolog and body
18230 code and for PIC code the code can be actually quite bad, so
18231 don't try to be too clever here. */
18232 if (count != 0 || (DEFAULT_ABI != ABI_AIX && flag_pic))
18234 cfun->machine->ra_needs_full_frame = 1;
18236 return
18237 gen_rtx_MEM
18238 (Pmode,
18239 memory_address
18240 (Pmode,
18241 plus_constant (Pmode,
18242 copy_to_reg
18243 (gen_rtx_MEM (Pmode,
18244 memory_address (Pmode, frame))),
18245 RETURN_ADDRESS_OFFSET)));
18248 cfun->machine->ra_need_lr = 1;
18249 return get_hard_reg_initial_val (Pmode, LR_REGNO);
18252 /* Say whether a function is a candidate for sibcall handling or not. */
18254 static bool
18255 rs6000_function_ok_for_sibcall (tree decl, tree exp)
18257 tree fntype;
18259 if (decl)
18260 fntype = TREE_TYPE (decl);
18261 else
18262 fntype = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (exp)));
18264 /* We can't do it if the called function has more vector parameters
18265 than the current function; there's nowhere to put the VRsave code. */
18266 if (TARGET_ALTIVEC_ABI
18267 && TARGET_ALTIVEC_VRSAVE
18268 && !(decl && decl == current_function_decl))
18270 function_args_iterator args_iter;
18271 tree type;
18272 int nvreg = 0;
18274 /* Functions with vector parameters are required to have a
18275 prototype, so the argument type info must be available
18276 here. */
18277 FOREACH_FUNCTION_ARGS(fntype, type, args_iter)
18278 if (TREE_CODE (type) == VECTOR_TYPE
18279 && ALTIVEC_OR_VSX_VECTOR_MODE (TYPE_MODE (type)))
18280 nvreg++;
18282 FOREACH_FUNCTION_ARGS(TREE_TYPE (current_function_decl), type, args_iter)
18283 if (TREE_CODE (type) == VECTOR_TYPE
18284 && ALTIVEC_OR_VSX_VECTOR_MODE (TYPE_MODE (type)))
18285 nvreg--;
18287 if (nvreg > 0)
18288 return false;
18291 /* Under the AIX ABI we can't allow calls to non-local functions,
18292 because the callee may have a different TOC pointer to the
18293 caller and there's no way to ensure we restore the TOC when we
18294 return. With the secure-plt SYSV ABI we can't make non-local
18295 calls when -fpic/PIC because the plt call stubs use r30. */
18296 if (DEFAULT_ABI == ABI_DARWIN
18297 || (DEFAULT_ABI == ABI_AIX
18298 && decl
18299 && !DECL_EXTERNAL (decl)
18300 && (*targetm.binds_local_p) (decl))
18301 || (DEFAULT_ABI == ABI_V4
18302 && (!TARGET_SECURE_PLT
18303 || !flag_pic
18304 || (decl
18305 && (*targetm.binds_local_p) (decl)))))
18307 tree attr_list = TYPE_ATTRIBUTES (fntype);
18309 if (!lookup_attribute ("longcall", attr_list)
18310 || lookup_attribute ("shortcall", attr_list))
18311 return true;
18314 return false;
18317 /* NULL if INSN insn is valid within a low-overhead loop.
18318 Otherwise return why doloop cannot be applied.
18319 PowerPC uses the COUNT register for branch on table instructions. */
18321 static const char *
18322 rs6000_invalid_within_doloop (const_rtx insn)
18324 if (CALL_P (insn))
18325 return "Function call in the loop.";
18327 if (JUMP_P (insn)
18328 && (GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC
18329 || GET_CODE (PATTERN (insn)) == ADDR_VEC))
18330 return "Computed branch in the loop.";
18332 return NULL;
18335 static int
18336 rs6000_ra_ever_killed (void)
18338 rtx top;
18339 rtx reg;
18340 rtx insn;
18342 if (cfun->is_thunk)
18343 return 0;
18345 if (cfun->machine->lr_save_state)
18346 return cfun->machine->lr_save_state - 1;
18348 /* regs_ever_live has LR marked as used if any sibcalls are present,
18349 but this should not force saving and restoring in the
18350 pro/epilogue. Likewise, reg_set_between_p thinks a sibcall
18351 clobbers LR, so that is inappropriate. */
18353 /* Also, the prologue can generate a store into LR that
18354 doesn't really count, like this:
18356 move LR->R0
18357 bcl to set PIC register
18358 move LR->R31
18359 move R0->LR
18361 When we're called from the epilogue, we need to avoid counting
18362 this as a store. */
18364 push_topmost_sequence ();
18365 top = get_insns ();
18366 pop_topmost_sequence ();
18367 reg = gen_rtx_REG (Pmode, LR_REGNO);
18369 for (insn = NEXT_INSN (top); insn != NULL_RTX; insn = NEXT_INSN (insn))
18371 if (INSN_P (insn))
18373 if (CALL_P (insn))
18375 if (!SIBLING_CALL_P (insn))
18376 return 1;
18378 else if (find_regno_note (insn, REG_INC, LR_REGNO))
18379 return 1;
18380 else if (set_of (reg, insn) != NULL_RTX
18381 && !prologue_epilogue_contains (insn))
18382 return 1;
18385 return 0;
18388 /* Emit instructions needed to load the TOC register.
18389 This is only needed when TARGET_TOC, TARGET_MINIMAL_TOC, and there is
18390 a constant pool; or for SVR4 -fpic. */
18392 void
18393 rs6000_emit_load_toc_table (int fromprolog)
18395 rtx dest;
18396 dest = gen_rtx_REG (Pmode, RS6000_PIC_OFFSET_TABLE_REGNUM);
18398 if (TARGET_ELF && TARGET_SECURE_PLT && DEFAULT_ABI != ABI_AIX && flag_pic)
18400 char buf[30];
18401 rtx lab, tmp1, tmp2, got;
18403 lab = gen_label_rtx ();
18404 ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (lab));
18405 lab = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
18406 if (flag_pic == 2)
18407 got = gen_rtx_SYMBOL_REF (Pmode, toc_label_name);
18408 else
18409 got = rs6000_got_sym ();
18410 tmp1 = tmp2 = dest;
18411 if (!fromprolog)
18413 tmp1 = gen_reg_rtx (Pmode);
18414 tmp2 = gen_reg_rtx (Pmode);
18416 emit_insn (gen_load_toc_v4_PIC_1 (lab));
18417 emit_move_insn (tmp1, gen_rtx_REG (Pmode, LR_REGNO));
18418 emit_insn (gen_load_toc_v4_PIC_3b (tmp2, tmp1, got, lab));
18419 emit_insn (gen_load_toc_v4_PIC_3c (dest, tmp2, got, lab));
18421 else if (TARGET_ELF && DEFAULT_ABI == ABI_V4 && flag_pic == 1)
18423 emit_insn (gen_load_toc_v4_pic_si ());
18424 emit_move_insn (dest, gen_rtx_REG (Pmode, LR_REGNO));
18426 else if (TARGET_ELF && DEFAULT_ABI != ABI_AIX && flag_pic == 2)
18428 char buf[30];
18429 rtx temp0 = (fromprolog
18430 ? gen_rtx_REG (Pmode, 0)
18431 : gen_reg_rtx (Pmode));
18433 if (fromprolog)
18435 rtx symF, symL;
18437 ASM_GENERATE_INTERNAL_LABEL (buf, "LCF", rs6000_pic_labelno);
18438 symF = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
18440 ASM_GENERATE_INTERNAL_LABEL (buf, "LCL", rs6000_pic_labelno);
18441 symL = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
18443 emit_insn (gen_load_toc_v4_PIC_1 (symF));
18444 emit_move_insn (dest, gen_rtx_REG (Pmode, LR_REGNO));
18445 emit_insn (gen_load_toc_v4_PIC_2 (temp0, dest, symL, symF));
18447 else
18449 rtx tocsym, lab;
18451 tocsym = gen_rtx_SYMBOL_REF (Pmode, toc_label_name);
18452 lab = gen_label_rtx ();
18453 emit_insn (gen_load_toc_v4_PIC_1b (tocsym, lab));
18454 emit_move_insn (dest, gen_rtx_REG (Pmode, LR_REGNO));
18455 if (TARGET_LINK_STACK)
18456 emit_insn (gen_addsi3 (dest, dest, GEN_INT (4)));
18457 emit_move_insn (temp0, gen_rtx_MEM (Pmode, dest));
18459 emit_insn (gen_addsi3 (dest, temp0, dest));
18461 else if (TARGET_ELF && !TARGET_AIX && flag_pic == 0 && TARGET_MINIMAL_TOC)
18463 /* This is for AIX code running in non-PIC ELF32. */
18464 char buf[30];
18465 rtx realsym;
18466 ASM_GENERATE_INTERNAL_LABEL (buf, "LCTOC", 1);
18467 realsym = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
18469 emit_insn (gen_elf_high (dest, realsym));
18470 emit_insn (gen_elf_low (dest, dest, realsym));
18472 else
18474 gcc_assert (DEFAULT_ABI == ABI_AIX);
18476 if (TARGET_32BIT)
18477 emit_insn (gen_load_toc_aix_si (dest));
18478 else
18479 emit_insn (gen_load_toc_aix_di (dest));
18483 /* Emit instructions to restore the link register after determining where
18484 its value has been stored. */
18486 void
18487 rs6000_emit_eh_reg_restore (rtx source, rtx scratch)
18489 rs6000_stack_t *info = rs6000_stack_info ();
18490 rtx operands[2];
18492 operands[0] = source;
18493 operands[1] = scratch;
18495 if (info->lr_save_p)
18497 rtx frame_rtx = stack_pointer_rtx;
18498 HOST_WIDE_INT sp_offset = 0;
18499 rtx tmp;
18501 if (frame_pointer_needed
18502 || cfun->calls_alloca
18503 || info->total_size > 32767)
18505 tmp = gen_frame_mem (Pmode, frame_rtx);
18506 emit_move_insn (operands[1], tmp);
18507 frame_rtx = operands[1];
18509 else if (info->push_p)
18510 sp_offset = info->total_size;
18512 tmp = plus_constant (Pmode, frame_rtx,
18513 info->lr_save_offset + sp_offset);
18514 tmp = gen_frame_mem (Pmode, tmp);
18515 emit_move_insn (tmp, operands[0]);
18517 else
18518 emit_move_insn (gen_rtx_REG (Pmode, LR_REGNO), operands[0]);
18520 /* Freeze lr_save_p. We've just emitted rtl that depends on the
18521 state of lr_save_p so any change from here on would be a bug. In
18522 particular, stop rs6000_ra_ever_killed from considering the SET
18523 of lr we may have added just above. */
18524 cfun->machine->lr_save_state = info->lr_save_p + 1;
18527 static GTY(()) alias_set_type set = -1;
18529 alias_set_type
18530 get_TOC_alias_set (void)
18532 if (set == -1)
18533 set = new_alias_set ();
18534 return set;
18537 /* This returns nonzero if the current function uses the TOC. This is
18538 determined by the presence of (use (unspec ... UNSPEC_TOC)), which
18539 is generated by the ABI_V4 load_toc_* patterns. */
18540 #if TARGET_ELF
18541 static int
18542 uses_TOC (void)
18544 rtx insn;
18546 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
18547 if (INSN_P (insn))
18549 rtx pat = PATTERN (insn);
18550 int i;
18552 if (GET_CODE (pat) == PARALLEL)
18553 for (i = 0; i < XVECLEN (pat, 0); i++)
18555 rtx sub = XVECEXP (pat, 0, i);
18556 if (GET_CODE (sub) == USE)
18558 sub = XEXP (sub, 0);
18559 if (GET_CODE (sub) == UNSPEC
18560 && XINT (sub, 1) == UNSPEC_TOC)
18561 return 1;
18565 return 0;
18567 #endif
18570 create_TOC_reference (rtx symbol, rtx largetoc_reg)
18572 rtx tocrel, tocreg, hi;
18574 if (TARGET_DEBUG_ADDR)
18576 if (GET_CODE (symbol) == SYMBOL_REF)
18577 fprintf (stderr, "\ncreate_TOC_reference, (symbol_ref %s)\n",
18578 XSTR (symbol, 0));
18579 else
18581 fprintf (stderr, "\ncreate_TOC_reference, code %s:\n",
18582 GET_RTX_NAME (GET_CODE (symbol)));
18583 debug_rtx (symbol);
18587 if (!can_create_pseudo_p ())
18588 df_set_regs_ever_live (TOC_REGISTER, true);
18590 tocreg = gen_rtx_REG (Pmode, TOC_REGISTER);
18591 tocrel = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, symbol, tocreg), UNSPEC_TOCREL);
18592 if (TARGET_CMODEL == CMODEL_SMALL || can_create_pseudo_p ())
18593 return tocrel;
18595 hi = gen_rtx_HIGH (Pmode, copy_rtx (tocrel));
18596 if (largetoc_reg != NULL)
18598 emit_move_insn (largetoc_reg, hi);
18599 hi = largetoc_reg;
18601 return gen_rtx_LO_SUM (Pmode, hi, tocrel);
18604 /* Issue assembly directives that create a reference to the given DWARF
18605 FRAME_TABLE_LABEL from the current function section. */
18606 void
18607 rs6000_aix_asm_output_dwarf_table_ref (char * frame_table_label)
18609 fprintf (asm_out_file, "\t.ref %s\n",
18610 (* targetm.strip_name_encoding) (frame_table_label));
18613 /* This ties together stack memory (MEM with an alias set of frame_alias_set)
18614 and the change to the stack pointer. */
18616 static void
18617 rs6000_emit_stack_tie (rtx fp, bool hard_frame_needed)
18619 rtvec p;
18620 int i;
18621 rtx regs[3];
18623 i = 0;
18624 regs[i++] = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
18625 if (hard_frame_needed)
18626 regs[i++] = gen_rtx_REG (Pmode, HARD_FRAME_POINTER_REGNUM);
18627 if (!(REGNO (fp) == STACK_POINTER_REGNUM
18628 || (hard_frame_needed
18629 && REGNO (fp) == HARD_FRAME_POINTER_REGNUM)))
18630 regs[i++] = fp;
18632 p = rtvec_alloc (i);
18633 while (--i >= 0)
18635 rtx mem = gen_frame_mem (BLKmode, regs[i]);
18636 RTVEC_ELT (p, i) = gen_rtx_SET (VOIDmode, mem, const0_rtx);
18639 emit_insn (gen_stack_tie (gen_rtx_PARALLEL (VOIDmode, p)));
18642 /* Emit the correct code for allocating stack space, as insns.
18643 If COPY_REG, make sure a copy of the old frame is left there.
18644 The generated code may use hard register 0 as a temporary. */
18646 static void
18647 rs6000_emit_allocate_stack (HOST_WIDE_INT size, rtx copy_reg, int copy_off)
18649 rtx insn;
18650 rtx stack_reg = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
18651 rtx tmp_reg = gen_rtx_REG (Pmode, 0);
18652 rtx todec = gen_int_mode (-size, Pmode);
18653 rtx par, set, mem;
18655 if (INTVAL (todec) != -size)
18657 warning (0, "stack frame too large");
18658 emit_insn (gen_trap ());
18659 return;
18662 if (crtl->limit_stack)
18664 if (REG_P (stack_limit_rtx)
18665 && REGNO (stack_limit_rtx) > 1
18666 && REGNO (stack_limit_rtx) <= 31)
18668 emit_insn (gen_add3_insn (tmp_reg, stack_limit_rtx, GEN_INT (size)));
18669 emit_insn (gen_cond_trap (LTU, stack_reg, tmp_reg,
18670 const0_rtx));
18672 else if (GET_CODE (stack_limit_rtx) == SYMBOL_REF
18673 && TARGET_32BIT
18674 && DEFAULT_ABI == ABI_V4)
18676 rtx toload = gen_rtx_CONST (VOIDmode,
18677 gen_rtx_PLUS (Pmode,
18678 stack_limit_rtx,
18679 GEN_INT (size)));
18681 emit_insn (gen_elf_high (tmp_reg, toload));
18682 emit_insn (gen_elf_low (tmp_reg, tmp_reg, toload));
18683 emit_insn (gen_cond_trap (LTU, stack_reg, tmp_reg,
18684 const0_rtx));
18686 else
18687 warning (0, "stack limit expression is not supported");
18690 if (copy_reg)
18692 if (copy_off != 0)
18693 emit_insn (gen_add3_insn (copy_reg, stack_reg, GEN_INT (copy_off)));
18694 else
18695 emit_move_insn (copy_reg, stack_reg);
18698 if (size > 32767)
18700 /* Need a note here so that try_split doesn't get confused. */
18701 if (get_last_insn () == NULL_RTX)
18702 emit_note (NOTE_INSN_DELETED);
18703 insn = emit_move_insn (tmp_reg, todec);
18704 try_split (PATTERN (insn), insn, 0);
18705 todec = tmp_reg;
18708 insn = emit_insn (TARGET_32BIT
18709 ? gen_movsi_update_stack (stack_reg, stack_reg,
18710 todec, stack_reg)
18711 : gen_movdi_di_update_stack (stack_reg, stack_reg,
18712 todec, stack_reg));
18713 /* Since we didn't use gen_frame_mem to generate the MEM, grab
18714 it now and set the alias set/attributes. The above gen_*_update
18715 calls will generate a PARALLEL with the MEM set being the first
18716 operation. */
18717 par = PATTERN (insn);
18718 gcc_assert (GET_CODE (par) == PARALLEL);
18719 set = XVECEXP (par, 0, 0);
18720 gcc_assert (GET_CODE (set) == SET);
18721 mem = SET_DEST (set);
18722 gcc_assert (MEM_P (mem));
18723 MEM_NOTRAP_P (mem) = 1;
18724 set_mem_alias_set (mem, get_frame_alias_set ());
18726 RTX_FRAME_RELATED_P (insn) = 1;
18727 add_reg_note (insn, REG_FRAME_RELATED_EXPR,
18728 gen_rtx_SET (VOIDmode, stack_reg,
18729 gen_rtx_PLUS (Pmode, stack_reg,
18730 GEN_INT (-size))));
18733 #define PROBE_INTERVAL (1 << STACK_CHECK_PROBE_INTERVAL_EXP)
18735 #if PROBE_INTERVAL > 32768
18736 #error Cannot use indexed addressing mode for stack probing
18737 #endif
18739 /* Emit code to probe a range of stack addresses from FIRST to FIRST+SIZE,
18740 inclusive. These are offsets from the current stack pointer. */
18742 static void
18743 rs6000_emit_probe_stack_range (HOST_WIDE_INT first, HOST_WIDE_INT size)
18745 /* See if we have a constant small number of probes to generate. If so,
18746 that's the easy case. */
18747 if (first + size <= 32768)
18749 HOST_WIDE_INT i;
18751 /* Probe at FIRST + N * PROBE_INTERVAL for values of N from 1 until
18752 it exceeds SIZE. If only one probe is needed, this will not
18753 generate any code. Then probe at FIRST + SIZE. */
18754 for (i = PROBE_INTERVAL; i < size; i += PROBE_INTERVAL)
18755 emit_stack_probe (plus_constant (Pmode, stack_pointer_rtx,
18756 -(first + i)));
18758 emit_stack_probe (plus_constant (Pmode, stack_pointer_rtx,
18759 -(first + size)));
18762 /* Otherwise, do the same as above, but in a loop. Note that we must be
18763 extra careful with variables wrapping around because we might be at
18764 the very top (or the very bottom) of the address space and we have
18765 to be able to handle this case properly; in particular, we use an
18766 equality test for the loop condition. */
18767 else
18769 HOST_WIDE_INT rounded_size;
18770 rtx r12 = gen_rtx_REG (Pmode, 12);
18771 rtx r0 = gen_rtx_REG (Pmode, 0);
18773 /* Sanity check for the addressing mode we're going to use. */
18774 gcc_assert (first <= 32768);
18776 /* Step 1: round SIZE to the previous multiple of the interval. */
18778 rounded_size = size & -PROBE_INTERVAL;
18781 /* Step 2: compute initial and final value of the loop counter. */
18783 /* TEST_ADDR = SP + FIRST. */
18784 emit_insn (gen_rtx_SET (VOIDmode, r12,
18785 plus_constant (Pmode, stack_pointer_rtx,
18786 -first)));
18788 /* LAST_ADDR = SP + FIRST + ROUNDED_SIZE. */
18789 if (rounded_size > 32768)
18791 emit_move_insn (r0, GEN_INT (-rounded_size));
18792 emit_insn (gen_rtx_SET (VOIDmode, r0,
18793 gen_rtx_PLUS (Pmode, r12, r0)));
18795 else
18796 emit_insn (gen_rtx_SET (VOIDmode, r0,
18797 plus_constant (Pmode, r12, -rounded_size)));
18800 /* Step 3: the loop
18802 while (TEST_ADDR != LAST_ADDR)
18804 TEST_ADDR = TEST_ADDR + PROBE_INTERVAL
18805 probe at TEST_ADDR
18808 probes at FIRST + N * PROBE_INTERVAL for values of N from 1
18809 until it is equal to ROUNDED_SIZE. */
18811 if (TARGET_64BIT)
18812 emit_insn (gen_probe_stack_rangedi (r12, r12, r0));
18813 else
18814 emit_insn (gen_probe_stack_rangesi (r12, r12, r0));
18817 /* Step 4: probe at FIRST + SIZE if we cannot assert at compile-time
18818 that SIZE is equal to ROUNDED_SIZE. */
18820 if (size != rounded_size)
18821 emit_stack_probe (plus_constant (Pmode, r12, rounded_size - size));
18825 /* Probe a range of stack addresses from REG1 to REG2 inclusive. These are
18826 absolute addresses. */
18828 const char *
18829 output_probe_stack_range (rtx reg1, rtx reg2)
18831 static int labelno = 0;
18832 char loop_lab[32], end_lab[32];
18833 rtx xops[2];
18835 ASM_GENERATE_INTERNAL_LABEL (loop_lab, "LPSRL", labelno);
18836 ASM_GENERATE_INTERNAL_LABEL (end_lab, "LPSRE", labelno++);
18838 ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, loop_lab);
18840 /* Jump to END_LAB if TEST_ADDR == LAST_ADDR. */
18841 xops[0] = reg1;
18842 xops[1] = reg2;
18843 if (TARGET_64BIT)
18844 output_asm_insn ("cmpd 0,%0,%1", xops);
18845 else
18846 output_asm_insn ("cmpw 0,%0,%1", xops);
18848 fputs ("\tbeq 0,", asm_out_file);
18849 assemble_name_raw (asm_out_file, end_lab);
18850 fputc ('\n', asm_out_file);
18852 /* TEST_ADDR = TEST_ADDR + PROBE_INTERVAL. */
18853 xops[1] = GEN_INT (-PROBE_INTERVAL);
18854 output_asm_insn ("addi %0,%0,%1", xops);
18856 /* Probe at TEST_ADDR and branch. */
18857 xops[1] = gen_rtx_REG (Pmode, 0);
18858 output_asm_insn ("stw %1,0(%0)", xops);
18859 fprintf (asm_out_file, "\tb ");
18860 assemble_name_raw (asm_out_file, loop_lab);
18861 fputc ('\n', asm_out_file);
18863 ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, end_lab);
18865 return "";
18868 /* Add to 'insn' a note which is PATTERN (INSN) but with REG replaced
18869 with (plus:P (reg 1) VAL), and with REG2 replaced with RREG if REG2
18870 is not NULL. It would be nice if dwarf2out_frame_debug_expr could
18871 deduce these equivalences by itself so it wasn't necessary to hold
18872 its hand so much. Don't be tempted to always supply d2_f_d_e with
18873 the actual cfa register, ie. r31 when we are using a hard frame
18874 pointer. That fails when saving regs off r1, and sched moves the
18875 r31 setup past the reg saves. */
18877 static rtx
18878 rs6000_frame_related (rtx insn, rtx reg, HOST_WIDE_INT val,
18879 rtx reg2, rtx rreg)
18881 rtx real, temp;
18883 if (REGNO (reg) == STACK_POINTER_REGNUM && reg2 == NULL_RTX)
18885 /* No need for any replacement. Just set RTX_FRAME_RELATED_P. */
18886 int i;
18888 gcc_checking_assert (val == 0);
18889 real = PATTERN (insn);
18890 if (GET_CODE (real) == PARALLEL)
18891 for (i = 0; i < XVECLEN (real, 0); i++)
18892 if (GET_CODE (XVECEXP (real, 0, i)) == SET)
18894 rtx set = XVECEXP (real, 0, i);
18896 RTX_FRAME_RELATED_P (set) = 1;
18898 RTX_FRAME_RELATED_P (insn) = 1;
18899 return insn;
18902 /* copy_rtx will not make unique copies of registers, so we need to
18903 ensure we don't have unwanted sharing here. */
18904 if (reg == reg2)
18905 reg = gen_raw_REG (GET_MODE (reg), REGNO (reg));
18907 if (reg == rreg)
18908 reg = gen_raw_REG (GET_MODE (reg), REGNO (reg));
18910 real = copy_rtx (PATTERN (insn));
18912 if (reg2 != NULL_RTX)
18913 real = replace_rtx (real, reg2, rreg);
18915 if (REGNO (reg) == STACK_POINTER_REGNUM)
18916 gcc_checking_assert (val == 0);
18917 else
18918 real = replace_rtx (real, reg,
18919 gen_rtx_PLUS (Pmode, gen_rtx_REG (Pmode,
18920 STACK_POINTER_REGNUM),
18921 GEN_INT (val)));
18923 /* We expect that 'real' is either a SET or a PARALLEL containing
18924 SETs (and possibly other stuff). In a PARALLEL, all the SETs
18925 are important so they all have to be marked RTX_FRAME_RELATED_P. */
18927 if (GET_CODE (real) == SET)
18929 rtx set = real;
18931 temp = simplify_rtx (SET_SRC (set));
18932 if (temp)
18933 SET_SRC (set) = temp;
18934 temp = simplify_rtx (SET_DEST (set));
18935 if (temp)
18936 SET_DEST (set) = temp;
18937 if (GET_CODE (SET_DEST (set)) == MEM)
18939 temp = simplify_rtx (XEXP (SET_DEST (set), 0));
18940 if (temp)
18941 XEXP (SET_DEST (set), 0) = temp;
18944 else
18946 int i;
18948 gcc_assert (GET_CODE (real) == PARALLEL);
18949 for (i = 0; i < XVECLEN (real, 0); i++)
18950 if (GET_CODE (XVECEXP (real, 0, i)) == SET)
18952 rtx set = XVECEXP (real, 0, i);
18954 temp = simplify_rtx (SET_SRC (set));
18955 if (temp)
18956 SET_SRC (set) = temp;
18957 temp = simplify_rtx (SET_DEST (set));
18958 if (temp)
18959 SET_DEST (set) = temp;
18960 if (GET_CODE (SET_DEST (set)) == MEM)
18962 temp = simplify_rtx (XEXP (SET_DEST (set), 0));
18963 if (temp)
18964 XEXP (SET_DEST (set), 0) = temp;
18966 RTX_FRAME_RELATED_P (set) = 1;
18970 RTX_FRAME_RELATED_P (insn) = 1;
18971 add_reg_note (insn, REG_FRAME_RELATED_EXPR, real);
18973 return insn;
18976 /* Returns an insn that has a vrsave set operation with the
18977 appropriate CLOBBERs. */
18979 static rtx
18980 generate_set_vrsave (rtx reg, rs6000_stack_t *info, int epiloguep)
18982 int nclobs, i;
18983 rtx insn, clobs[TOTAL_ALTIVEC_REGS + 1];
18984 rtx vrsave = gen_rtx_REG (SImode, VRSAVE_REGNO);
18986 clobs[0]
18987 = gen_rtx_SET (VOIDmode,
18988 vrsave,
18989 gen_rtx_UNSPEC_VOLATILE (SImode,
18990 gen_rtvec (2, reg, vrsave),
18991 UNSPECV_SET_VRSAVE));
18993 nclobs = 1;
18995 /* We need to clobber the registers in the mask so the scheduler
18996 does not move sets to VRSAVE before sets of AltiVec registers.
18998 However, if the function receives nonlocal gotos, reload will set
18999 all call saved registers live. We will end up with:
19001 (set (reg 999) (mem))
19002 (parallel [ (set (reg vrsave) (unspec blah))
19003 (clobber (reg 999))])
19005 The clobber will cause the store into reg 999 to be dead, and
19006 flow will attempt to delete an epilogue insn. In this case, we
19007 need an unspec use/set of the register. */
19009 for (i = FIRST_ALTIVEC_REGNO; i <= LAST_ALTIVEC_REGNO; ++i)
19010 if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
19012 if (!epiloguep || call_used_regs [i])
19013 clobs[nclobs++] = gen_rtx_CLOBBER (VOIDmode,
19014 gen_rtx_REG (V4SImode, i));
19015 else
19017 rtx reg = gen_rtx_REG (V4SImode, i);
19019 clobs[nclobs++]
19020 = gen_rtx_SET (VOIDmode,
19021 reg,
19022 gen_rtx_UNSPEC (V4SImode,
19023 gen_rtvec (1, reg), 27));
19027 insn = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (nclobs));
19029 for (i = 0; i < nclobs; ++i)
19030 XVECEXP (insn, 0, i) = clobs[i];
19032 return insn;
19035 static rtx
19036 gen_frame_set (rtx reg, rtx frame_reg, int offset, bool store)
19038 rtx addr, mem;
19040 addr = gen_rtx_PLUS (Pmode, frame_reg, GEN_INT (offset));
19041 mem = gen_frame_mem (GET_MODE (reg), addr);
19042 return gen_rtx_SET (VOIDmode, store ? mem : reg, store ? reg : mem);
19045 static rtx
19046 gen_frame_load (rtx reg, rtx frame_reg, int offset)
19048 return gen_frame_set (reg, frame_reg, offset, false);
19051 static rtx
19052 gen_frame_store (rtx reg, rtx frame_reg, int offset)
19054 return gen_frame_set (reg, frame_reg, offset, true);
19057 /* Save a register into the frame, and emit RTX_FRAME_RELATED_P notes.
19058 Save REGNO into [FRAME_REG + OFFSET] in mode MODE. */
19060 static rtx
19061 emit_frame_save (rtx frame_reg, enum machine_mode mode,
19062 unsigned int regno, int offset, HOST_WIDE_INT frame_reg_to_sp)
19064 rtx reg, insn;
19066 /* Some cases that need register indexed addressing. */
19067 gcc_checking_assert (!((TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (mode))
19068 || (TARGET_VSX && ALTIVEC_OR_VSX_VECTOR_MODE (mode))
19069 || (TARGET_E500_DOUBLE && mode == DFmode)
19070 || (TARGET_SPE_ABI
19071 && SPE_VECTOR_MODE (mode)
19072 && !SPE_CONST_OFFSET_OK (offset))));
19074 reg = gen_rtx_REG (mode, regno);
19075 insn = emit_insn (gen_frame_store (reg, frame_reg, offset));
19076 return rs6000_frame_related (insn, frame_reg, frame_reg_to_sp,
19077 NULL_RTX, NULL_RTX);
19080 /* Emit an offset memory reference suitable for a frame store, while
19081 converting to a valid addressing mode. */
19083 static rtx
19084 gen_frame_mem_offset (enum machine_mode mode, rtx reg, int offset)
19086 rtx int_rtx, offset_rtx;
19088 int_rtx = GEN_INT (offset);
19090 if ((TARGET_SPE_ABI && SPE_VECTOR_MODE (mode) && !SPE_CONST_OFFSET_OK (offset))
19091 || (TARGET_E500_DOUBLE && mode == DFmode))
19093 offset_rtx = gen_rtx_REG (Pmode, FIXED_SCRATCH);
19094 emit_move_insn (offset_rtx, int_rtx);
19096 else
19097 offset_rtx = int_rtx;
19099 return gen_frame_mem (mode, gen_rtx_PLUS (Pmode, reg, offset_rtx));
19102 #ifndef TARGET_FIX_AND_CONTINUE
19103 #define TARGET_FIX_AND_CONTINUE 0
19104 #endif
19106 /* It's really GPR 13 or 14, FPR 14 and VR 20. We need the smallest. */
19107 #define FIRST_SAVRES_REGISTER FIRST_SAVED_GP_REGNO
19108 #define LAST_SAVRES_REGISTER 31
19109 #define N_SAVRES_REGISTERS (LAST_SAVRES_REGISTER - FIRST_SAVRES_REGISTER + 1)
19111 enum {
19112 SAVRES_LR = 0x1,
19113 SAVRES_SAVE = 0x2,
19114 SAVRES_REG = 0x0c,
19115 SAVRES_GPR = 0,
19116 SAVRES_FPR = 4,
19117 SAVRES_VR = 8
19120 static GTY(()) rtx savres_routine_syms[N_SAVRES_REGISTERS][12];
19122 /* Temporary holding space for an out-of-line register save/restore
19123 routine name. */
19124 static char savres_routine_name[30];
19126 /* Return the name for an out-of-line register save/restore routine.
19127 We are saving/restoring GPRs if GPR is true. */
19129 static char *
19130 rs6000_savres_routine_name (rs6000_stack_t *info, int regno, int sel)
19132 const char *prefix = "";
19133 const char *suffix = "";
19135 /* Different targets are supposed to define
19136 {SAVE,RESTORE}_FP_{PREFIX,SUFFIX} with the idea that the needed
19137 routine name could be defined with:
19139 sprintf (name, "%s%d%s", SAVE_FP_PREFIX, regno, SAVE_FP_SUFFIX)
19141 This is a nice idea in practice, but in reality, things are
19142 complicated in several ways:
19144 - ELF targets have save/restore routines for GPRs.
19146 - SPE targets use different prefixes for 32/64-bit registers, and
19147 neither of them fit neatly in the FOO_{PREFIX,SUFFIX} regimen.
19149 - PPC64 ELF targets have routines for save/restore of GPRs that
19150 differ in what they do with the link register, so having a set
19151 prefix doesn't work. (We only use one of the save routines at
19152 the moment, though.)
19154 - PPC32 elf targets have "exit" versions of the restore routines
19155 that restore the link register and can save some extra space.
19156 These require an extra suffix. (There are also "tail" versions
19157 of the restore routines and "GOT" versions of the save routines,
19158 but we don't generate those at present. Same problems apply,
19159 though.)
19161 We deal with all this by synthesizing our own prefix/suffix and
19162 using that for the simple sprintf call shown above. */
19163 if (TARGET_SPE)
19165 /* No floating point saves on the SPE. */
19166 gcc_assert ((sel & SAVRES_REG) == SAVRES_GPR);
19168 if ((sel & SAVRES_SAVE))
19169 prefix = info->spe_64bit_regs_used ? "_save64gpr_" : "_save32gpr_";
19170 else
19171 prefix = info->spe_64bit_regs_used ? "_rest64gpr_" : "_rest32gpr_";
19173 if ((sel & SAVRES_LR))
19174 suffix = "_x";
19176 else if (DEFAULT_ABI == ABI_V4)
19178 if (TARGET_64BIT)
19179 goto aix_names;
19181 if ((sel & SAVRES_REG) == SAVRES_GPR)
19182 prefix = (sel & SAVRES_SAVE) ? "_savegpr_" : "_restgpr_";
19183 else if ((sel & SAVRES_REG) == SAVRES_FPR)
19184 prefix = (sel & SAVRES_SAVE) ? "_savefpr_" : "_restfpr_";
19185 else if ((sel & SAVRES_REG) == SAVRES_VR)
19186 prefix = (sel & SAVRES_SAVE) ? "_savevr_" : "_restvr_";
19187 else
19188 abort ();
19190 if ((sel & SAVRES_LR))
19191 suffix = "_x";
19193 else if (DEFAULT_ABI == ABI_AIX)
19195 #if !defined (POWERPC_LINUX) && !defined (POWERPC_FREEBSD)
19196 /* No out-of-line save/restore routines for GPRs on AIX. */
19197 gcc_assert (!TARGET_AIX || (sel & SAVRES_REG) != SAVRES_GPR);
19198 #endif
19200 aix_names:
19201 if ((sel & SAVRES_REG) == SAVRES_GPR)
19202 prefix = ((sel & SAVRES_SAVE)
19203 ? ((sel & SAVRES_LR) ? "_savegpr0_" : "_savegpr1_")
19204 : ((sel & SAVRES_LR) ? "_restgpr0_" : "_restgpr1_"));
19205 else if ((sel & SAVRES_REG) == SAVRES_FPR)
19207 #if defined (POWERPC_LINUX) || defined (POWERPC_FREEBSD)
19208 if ((sel & SAVRES_LR))
19209 prefix = ((sel & SAVRES_SAVE) ? "_savefpr_" : "_restfpr_");
19210 else
19211 #endif
19213 prefix = (sel & SAVRES_SAVE) ? SAVE_FP_PREFIX : RESTORE_FP_PREFIX;
19214 suffix = (sel & SAVRES_SAVE) ? SAVE_FP_SUFFIX : RESTORE_FP_SUFFIX;
19217 else if ((sel & SAVRES_REG) == SAVRES_VR)
19218 prefix = (sel & SAVRES_SAVE) ? "_savevr_" : "_restvr_";
19219 else
19220 abort ();
19223 if (DEFAULT_ABI == ABI_DARWIN)
19225 /* The Darwin approach is (slightly) different, in order to be
19226 compatible with code generated by the system toolchain. There is a
19227 single symbol for the start of save sequence, and the code here
19228 embeds an offset into that code on the basis of the first register
19229 to be saved. */
19230 prefix = (sel & SAVRES_SAVE) ? "save" : "rest" ;
19231 if ((sel & SAVRES_REG) == SAVRES_GPR)
19232 sprintf (savres_routine_name, "*%sGPR%s%s%.0d ; %s r%d-r31", prefix,
19233 ((sel & SAVRES_LR) ? "x" : ""), (regno == 13 ? "" : "+"),
19234 (regno - 13) * 4, prefix, regno);
19235 else if ((sel & SAVRES_REG) == SAVRES_FPR)
19236 sprintf (savres_routine_name, "*%sFP%s%.0d ; %s f%d-f31", prefix,
19237 (regno == 14 ? "" : "+"), (regno - 14) * 4, prefix, regno);
19238 else if ((sel & SAVRES_REG) == SAVRES_VR)
19239 sprintf (savres_routine_name, "*%sVEC%s%.0d ; %s v%d-v31", prefix,
19240 (regno == 20 ? "" : "+"), (regno - 20) * 8, prefix, regno);
19241 else
19242 abort ();
19244 else
19245 sprintf (savres_routine_name, "%s%d%s", prefix, regno, suffix);
19247 return savres_routine_name;
19250 /* Return an RTL SYMBOL_REF for an out-of-line register save/restore routine.
19251 We are saving/restoring GPRs if GPR is true. */
19253 static rtx
19254 rs6000_savres_routine_sym (rs6000_stack_t *info, int sel)
19256 int regno = ((sel & SAVRES_REG) == SAVRES_GPR
19257 ? info->first_gp_reg_save
19258 : (sel & SAVRES_REG) == SAVRES_FPR
19259 ? info->first_fp_reg_save - 32
19260 : (sel & SAVRES_REG) == SAVRES_VR
19261 ? info->first_altivec_reg_save - FIRST_ALTIVEC_REGNO
19262 : -1);
19263 rtx sym;
19264 int select = sel;
19266 /* On the SPE, we never have any FPRs, but we do have 32/64-bit
19267 versions of the gpr routines. */
19268 if (TARGET_SPE_ABI && (sel & SAVRES_REG) == SAVRES_GPR
19269 && info->spe_64bit_regs_used)
19270 select ^= SAVRES_FPR ^ SAVRES_GPR;
19272 /* Don't generate bogus routine names. */
19273 gcc_assert (FIRST_SAVRES_REGISTER <= regno
19274 && regno <= LAST_SAVRES_REGISTER
19275 && select >= 0 && select <= 12);
19277 sym = savres_routine_syms[regno-FIRST_SAVRES_REGISTER][select];
19279 if (sym == NULL)
19281 char *name;
19283 name = rs6000_savres_routine_name (info, regno, sel);
19285 sym = savres_routine_syms[regno-FIRST_SAVRES_REGISTER][select]
19286 = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (name));
19287 SYMBOL_REF_FLAGS (sym) |= SYMBOL_FLAG_FUNCTION;
19290 return sym;
19293 /* Emit a sequence of insns, including a stack tie if needed, for
19294 resetting the stack pointer. If UPDT_REGNO is not 1, then don't
19295 reset the stack pointer, but move the base of the frame into
19296 reg UPDT_REGNO for use by out-of-line register restore routines. */
19298 static rtx
19299 rs6000_emit_stack_reset (rs6000_stack_t *info,
19300 rtx frame_reg_rtx, HOST_WIDE_INT frame_off,
19301 unsigned updt_regno)
19303 rtx updt_reg_rtx;
19305 /* This blockage is needed so that sched doesn't decide to move
19306 the sp change before the register restores. */
19307 if (DEFAULT_ABI == ABI_V4
19308 || (TARGET_SPE_ABI
19309 && info->spe_64bit_regs_used != 0
19310 && info->first_gp_reg_save != 32))
19311 rs6000_emit_stack_tie (frame_reg_rtx, frame_pointer_needed);
19313 /* If we are restoring registers out-of-line, we will be using the
19314 "exit" variants of the restore routines, which will reset the
19315 stack for us. But we do need to point updt_reg into the
19316 right place for those routines. */
19317 updt_reg_rtx = gen_rtx_REG (Pmode, updt_regno);
19319 if (frame_off != 0)
19320 return emit_insn (gen_add3_insn (updt_reg_rtx,
19321 frame_reg_rtx, GEN_INT (frame_off)));
19322 else if (REGNO (frame_reg_rtx) != updt_regno)
19323 return emit_move_insn (updt_reg_rtx, frame_reg_rtx);
19325 return NULL_RTX;
19328 /* Return the register number used as a pointer by out-of-line
19329 save/restore functions. */
19331 static inline unsigned
19332 ptr_regno_for_savres (int sel)
19334 if (DEFAULT_ABI == ABI_AIX)
19335 return (sel & SAVRES_REG) == SAVRES_FPR || (sel & SAVRES_LR) ? 1 : 12;
19336 return DEFAULT_ABI == ABI_DARWIN && (sel & SAVRES_REG) == SAVRES_FPR ? 1 : 11;
19339 /* Construct a parallel rtx describing the effect of a call to an
19340 out-of-line register save/restore routine, and emit the insn
19341 or jump_insn as appropriate. */
19343 static rtx
19344 rs6000_emit_savres_rtx (rs6000_stack_t *info,
19345 rtx frame_reg_rtx, int save_area_offset, int lr_offset,
19346 enum machine_mode reg_mode, int sel)
19348 int i;
19349 int offset, start_reg, end_reg, n_regs, use_reg;
19350 int reg_size = GET_MODE_SIZE (reg_mode);
19351 rtx sym;
19352 rtvec p;
19353 rtx par, insn;
19355 offset = 0;
19356 start_reg = ((sel & SAVRES_REG) == SAVRES_GPR
19357 ? info->first_gp_reg_save
19358 : (sel & SAVRES_REG) == SAVRES_FPR
19359 ? info->first_fp_reg_save
19360 : (sel & SAVRES_REG) == SAVRES_VR
19361 ? info->first_altivec_reg_save
19362 : -1);
19363 end_reg = ((sel & SAVRES_REG) == SAVRES_GPR
19364 ? 32
19365 : (sel & SAVRES_REG) == SAVRES_FPR
19366 ? 64
19367 : (sel & SAVRES_REG) == SAVRES_VR
19368 ? LAST_ALTIVEC_REGNO + 1
19369 : -1);
19370 n_regs = end_reg - start_reg;
19371 p = rtvec_alloc (3 + ((sel & SAVRES_LR) ? 1 : 0)
19372 + ((sel & SAVRES_REG) == SAVRES_VR ? 1 : 0)
19373 + n_regs);
19375 if (!(sel & SAVRES_SAVE) && (sel & SAVRES_LR))
19376 RTVEC_ELT (p, offset++) = ret_rtx;
19378 RTVEC_ELT (p, offset++)
19379 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, LR_REGNO));
19381 sym = rs6000_savres_routine_sym (info, sel);
19382 RTVEC_ELT (p, offset++) = gen_rtx_USE (VOIDmode, sym);
19384 use_reg = ptr_regno_for_savres (sel);
19385 if ((sel & SAVRES_REG) == SAVRES_VR)
19387 /* Vector regs are saved/restored using [reg+reg] addressing. */
19388 RTVEC_ELT (p, offset++)
19389 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, use_reg));
19390 RTVEC_ELT (p, offset++)
19391 = gen_rtx_USE (VOIDmode, gen_rtx_REG (Pmode, 0));
19393 else
19394 RTVEC_ELT (p, offset++)
19395 = gen_rtx_USE (VOIDmode, gen_rtx_REG (Pmode, use_reg));
19397 for (i = 0; i < end_reg - start_reg; i++)
19398 RTVEC_ELT (p, i + offset)
19399 = gen_frame_set (gen_rtx_REG (reg_mode, start_reg + i),
19400 frame_reg_rtx, save_area_offset + reg_size * i,
19401 (sel & SAVRES_SAVE) != 0);
19403 if ((sel & SAVRES_SAVE) && (sel & SAVRES_LR))
19404 RTVEC_ELT (p, i + offset)
19405 = gen_frame_store (gen_rtx_REG (Pmode, 0), frame_reg_rtx, lr_offset);
19407 par = gen_rtx_PARALLEL (VOIDmode, p);
19409 if (!(sel & SAVRES_SAVE) && (sel & SAVRES_LR))
19411 insn = emit_jump_insn (par);
19412 JUMP_LABEL (insn) = ret_rtx;
19414 else
19415 insn = emit_insn (par);
19416 return insn;
19419 /* Determine whether the gp REG is really used. */
19421 static bool
19422 rs6000_reg_live_or_pic_offset_p (int reg)
19424 /* If the function calls eh_return, claim used all the registers that would
19425 be checked for liveness otherwise. This is required for the PIC offset
19426 register with -mminimal-toc on AIX, as it is advertised as "fixed" for
19427 register allocation purposes in this case. */
19429 return (((crtl->calls_eh_return || df_regs_ever_live_p (reg))
19430 && (!call_used_regs[reg]
19431 || (reg == RS6000_PIC_OFFSET_TABLE_REGNUM
19432 && !TARGET_SINGLE_PIC_BASE
19433 && TARGET_TOC && TARGET_MINIMAL_TOC)))
19434 || (reg == RS6000_PIC_OFFSET_TABLE_REGNUM
19435 && !TARGET_SINGLE_PIC_BASE
19436 && ((DEFAULT_ABI == ABI_V4 && flag_pic != 0)
19437 || (DEFAULT_ABI == ABI_DARWIN && flag_pic))));
19440 /* Emit function prologue as insns. */
19442 void
19443 rs6000_emit_prologue (void)
19445 rs6000_stack_t *info = rs6000_stack_info ();
19446 enum machine_mode reg_mode = Pmode;
19447 int reg_size = TARGET_32BIT ? 4 : 8;
19448 rtx sp_reg_rtx = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
19449 rtx frame_reg_rtx = sp_reg_rtx;
19450 unsigned int cr_save_regno;
19451 rtx cr_save_rtx = NULL_RTX;
19452 rtx insn;
19453 int strategy;
19454 int using_static_chain_p = (cfun->static_chain_decl != NULL_TREE
19455 && df_regs_ever_live_p (STATIC_CHAIN_REGNUM)
19456 && call_used_regs[STATIC_CHAIN_REGNUM]);
19457 /* Offset to top of frame for frame_reg and sp respectively. */
19458 HOST_WIDE_INT frame_off = 0;
19459 HOST_WIDE_INT sp_off = 0;
19461 #ifdef ENABLE_CHECKING
19462 /* Track and check usage of r0, r11, r12. */
19463 int reg_inuse = using_static_chain_p ? 1 << 11 : 0;
19464 #define START_USE(R) do \
19466 gcc_assert ((reg_inuse & (1 << (R))) == 0); \
19467 reg_inuse |= 1 << (R); \
19468 } while (0)
19469 #define END_USE(R) do \
19471 gcc_assert ((reg_inuse & (1 << (R))) != 0); \
19472 reg_inuse &= ~(1 << (R)); \
19473 } while (0)
19474 #define NOT_INUSE(R) do \
19476 gcc_assert ((reg_inuse & (1 << (R))) == 0); \
19477 } while (0)
19478 #else
19479 #define START_USE(R) do {} while (0)
19480 #define END_USE(R) do {} while (0)
19481 #define NOT_INUSE(R) do {} while (0)
19482 #endif
19484 if (flag_stack_usage_info)
19485 current_function_static_stack_size = info->total_size;
19487 if (flag_stack_check == STATIC_BUILTIN_STACK_CHECK && info->total_size)
19488 rs6000_emit_probe_stack_range (STACK_CHECK_PROTECT, info->total_size);
19490 if (TARGET_FIX_AND_CONTINUE)
19492 /* gdb on darwin arranges to forward a function from the old
19493 address by modifying the first 5 instructions of the function
19494 to branch to the overriding function. This is necessary to
19495 permit function pointers that point to the old function to
19496 actually forward to the new function. */
19497 emit_insn (gen_nop ());
19498 emit_insn (gen_nop ());
19499 emit_insn (gen_nop ());
19500 emit_insn (gen_nop ());
19501 emit_insn (gen_nop ());
19504 if (TARGET_SPE_ABI && info->spe_64bit_regs_used != 0)
19506 reg_mode = V2SImode;
19507 reg_size = 8;
19510 /* Handle world saves specially here. */
19511 if (WORLD_SAVE_P (info))
19513 int i, j, sz;
19514 rtx treg;
19515 rtvec p;
19516 rtx reg0;
19518 /* save_world expects lr in r0. */
19519 reg0 = gen_rtx_REG (Pmode, 0);
19520 if (info->lr_save_p)
19522 insn = emit_move_insn (reg0,
19523 gen_rtx_REG (Pmode, LR_REGNO));
19524 RTX_FRAME_RELATED_P (insn) = 1;
19527 /* The SAVE_WORLD and RESTORE_WORLD routines make a number of
19528 assumptions about the offsets of various bits of the stack
19529 frame. */
19530 gcc_assert (info->gp_save_offset == -220
19531 && info->fp_save_offset == -144
19532 && info->lr_save_offset == 8
19533 && info->cr_save_offset == 4
19534 && info->push_p
19535 && info->lr_save_p
19536 && (!crtl->calls_eh_return
19537 || info->ehrd_offset == -432)
19538 && info->vrsave_save_offset == -224
19539 && info->altivec_save_offset == -416);
19541 treg = gen_rtx_REG (SImode, 11);
19542 emit_move_insn (treg, GEN_INT (-info->total_size));
19544 /* SAVE_WORLD takes the caller's LR in R0 and the frame size
19545 in R11. It also clobbers R12, so beware! */
19547 /* Preserve CR2 for save_world prologues */
19548 sz = 5;
19549 sz += 32 - info->first_gp_reg_save;
19550 sz += 64 - info->first_fp_reg_save;
19551 sz += LAST_ALTIVEC_REGNO - info->first_altivec_reg_save + 1;
19552 p = rtvec_alloc (sz);
19553 j = 0;
19554 RTVEC_ELT (p, j++) = gen_rtx_CLOBBER (VOIDmode,
19555 gen_rtx_REG (SImode,
19556 LR_REGNO));
19557 RTVEC_ELT (p, j++) = gen_rtx_USE (VOIDmode,
19558 gen_rtx_SYMBOL_REF (Pmode,
19559 "*save_world"));
19560 /* We do floats first so that the instruction pattern matches
19561 properly. */
19562 for (i = 0; i < 64 - info->first_fp_reg_save; i++)
19563 RTVEC_ELT (p, j++)
19564 = gen_frame_store (gen_rtx_REG (TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT
19565 ? DFmode : SFmode,
19566 info->first_fp_reg_save + i),
19567 frame_reg_rtx,
19568 info->fp_save_offset + frame_off + 8 * i);
19569 for (i = 0; info->first_altivec_reg_save + i <= LAST_ALTIVEC_REGNO; i++)
19570 RTVEC_ELT (p, j++)
19571 = gen_frame_store (gen_rtx_REG (V4SImode,
19572 info->first_altivec_reg_save + i),
19573 frame_reg_rtx,
19574 info->altivec_save_offset + frame_off + 16 * i);
19575 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
19576 RTVEC_ELT (p, j++)
19577 = gen_frame_store (gen_rtx_REG (reg_mode, info->first_gp_reg_save + i),
19578 frame_reg_rtx,
19579 info->gp_save_offset + frame_off + reg_size * i);
19581 /* CR register traditionally saved as CR2. */
19582 RTVEC_ELT (p, j++)
19583 = gen_frame_store (gen_rtx_REG (SImode, CR2_REGNO),
19584 frame_reg_rtx, info->cr_save_offset + frame_off);
19585 /* Explain about use of R0. */
19586 if (info->lr_save_p)
19587 RTVEC_ELT (p, j++)
19588 = gen_frame_store (reg0,
19589 frame_reg_rtx, info->lr_save_offset + frame_off);
19590 /* Explain what happens to the stack pointer. */
19592 rtx newval = gen_rtx_PLUS (Pmode, sp_reg_rtx, treg);
19593 RTVEC_ELT (p, j++) = gen_rtx_SET (VOIDmode, sp_reg_rtx, newval);
19596 insn = emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
19597 rs6000_frame_related (insn, frame_reg_rtx, sp_off - frame_off,
19598 treg, GEN_INT (-info->total_size));
19599 sp_off = frame_off = info->total_size;
19602 strategy = info->savres_strategy;
19604 /* For V.4, update stack before we do any saving and set back pointer. */
19605 if (! WORLD_SAVE_P (info)
19606 && info->push_p
19607 && (DEFAULT_ABI == ABI_V4
19608 || crtl->calls_eh_return))
19610 bool need_r11 = (TARGET_SPE
19611 ? (!(strategy & SAVE_INLINE_GPRS)
19612 && info->spe_64bit_regs_used == 0)
19613 : (!(strategy & SAVE_INLINE_FPRS)
19614 || !(strategy & SAVE_INLINE_GPRS)
19615 || !(strategy & SAVE_INLINE_VRS)));
19616 int ptr_regno = -1;
19617 rtx ptr_reg = NULL_RTX;
19618 int ptr_off = 0;
19620 if (info->total_size < 32767)
19621 frame_off = info->total_size;
19622 else if (need_r11)
19623 ptr_regno = 11;
19624 else if (info->cr_save_p
19625 || info->lr_save_p
19626 || info->first_fp_reg_save < 64
19627 || info->first_gp_reg_save < 32
19628 || info->altivec_size != 0
19629 || info->vrsave_mask != 0
19630 || crtl->calls_eh_return)
19631 ptr_regno = 12;
19632 else
19634 /* The prologue won't be saving any regs so there is no need
19635 to set up a frame register to access any frame save area.
19636 We also won't be using frame_off anywhere below, but set
19637 the correct value anyway to protect against future
19638 changes to this function. */
19639 frame_off = info->total_size;
19641 if (ptr_regno != -1)
19643 /* Set up the frame offset to that needed by the first
19644 out-of-line save function. */
19645 START_USE (ptr_regno);
19646 ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
19647 frame_reg_rtx = ptr_reg;
19648 if (!(strategy & SAVE_INLINE_FPRS) && info->fp_size != 0)
19649 gcc_checking_assert (info->fp_save_offset + info->fp_size == 0);
19650 else if (!(strategy & SAVE_INLINE_GPRS) && info->first_gp_reg_save < 32)
19651 ptr_off = info->gp_save_offset + info->gp_size;
19652 else if (!(strategy & SAVE_INLINE_VRS) && info->altivec_size != 0)
19653 ptr_off = info->altivec_save_offset + info->altivec_size;
19654 frame_off = -ptr_off;
19656 rs6000_emit_allocate_stack (info->total_size, ptr_reg, ptr_off);
19657 sp_off = info->total_size;
19658 if (frame_reg_rtx != sp_reg_rtx)
19659 rs6000_emit_stack_tie (frame_reg_rtx, false);
19662 /* If we use the link register, get it into r0. */
19663 if (!WORLD_SAVE_P (info) && info->lr_save_p)
19665 rtx addr, reg, mem;
19667 reg = gen_rtx_REG (Pmode, 0);
19668 START_USE (0);
19669 insn = emit_move_insn (reg, gen_rtx_REG (Pmode, LR_REGNO));
19670 RTX_FRAME_RELATED_P (insn) = 1;
19672 if (!(strategy & (SAVE_NOINLINE_GPRS_SAVES_LR
19673 | SAVE_NOINLINE_FPRS_SAVES_LR)))
19675 addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
19676 GEN_INT (info->lr_save_offset + frame_off));
19677 mem = gen_rtx_MEM (Pmode, addr);
19678 /* This should not be of rs6000_sr_alias_set, because of
19679 __builtin_return_address. */
19681 insn = emit_move_insn (mem, reg);
19682 rs6000_frame_related (insn, frame_reg_rtx, sp_off - frame_off,
19683 NULL_RTX, NULL_RTX);
19684 END_USE (0);
19688 /* If we need to save CR, put it into r12 or r11. Choose r12 except when
19689 r12 will be needed by out-of-line gpr restore. */
19690 cr_save_regno = (DEFAULT_ABI == ABI_AIX
19691 && !(strategy & (SAVE_INLINE_GPRS
19692 | SAVE_NOINLINE_GPRS_SAVES_LR))
19693 ? 11 : 12);
19694 if (!WORLD_SAVE_P (info)
19695 && info->cr_save_p
19696 && REGNO (frame_reg_rtx) != cr_save_regno
19697 && !(using_static_chain_p && cr_save_regno == 11))
19699 rtx set;
19701 cr_save_rtx = gen_rtx_REG (SImode, cr_save_regno);
19702 START_USE (cr_save_regno);
19703 insn = emit_insn (gen_movesi_from_cr (cr_save_rtx));
19704 RTX_FRAME_RELATED_P (insn) = 1;
19705 /* Now, there's no way that dwarf2out_frame_debug_expr is going
19706 to understand '(unspec:SI [(reg:CC 68) ...] UNSPEC_MOVESI_FROM_CR)'.
19707 But that's OK. All we have to do is specify that _one_ condition
19708 code register is saved in this stack slot. The thrower's epilogue
19709 will then restore all the call-saved registers.
19710 We use CR2_REGNO (70) to be compatible with gcc-2.95 on Linux. */
19711 set = gen_rtx_SET (VOIDmode, cr_save_rtx,
19712 gen_rtx_REG (SImode, CR2_REGNO));
19713 add_reg_note (insn, REG_FRAME_RELATED_EXPR, set);
19716 /* Do any required saving of fpr's. If only one or two to save, do
19717 it ourselves. Otherwise, call function. */
19718 if (!WORLD_SAVE_P (info) && (strategy & SAVE_INLINE_FPRS))
19720 int i;
19721 for (i = 0; i < 64 - info->first_fp_reg_save; i++)
19722 if (save_reg_p (info->first_fp_reg_save + i))
19723 emit_frame_save (frame_reg_rtx,
19724 (TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT
19725 ? DFmode : SFmode),
19726 info->first_fp_reg_save + i,
19727 info->fp_save_offset + frame_off + 8 * i,
19728 sp_off - frame_off);
19730 else if (!WORLD_SAVE_P (info) && info->first_fp_reg_save != 64)
19732 bool lr = (strategy & SAVE_NOINLINE_FPRS_SAVES_LR) != 0;
19733 int sel = SAVRES_SAVE | SAVRES_FPR | (lr ? SAVRES_LR : 0);
19734 unsigned ptr_regno = ptr_regno_for_savres (sel);
19735 rtx ptr_reg = frame_reg_rtx;
19737 if (REGNO (frame_reg_rtx) == ptr_regno)
19738 gcc_checking_assert (frame_off == 0);
19739 else
19741 ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
19742 NOT_INUSE (ptr_regno);
19743 emit_insn (gen_add3_insn (ptr_reg,
19744 frame_reg_rtx, GEN_INT (frame_off)));
19746 insn = rs6000_emit_savres_rtx (info, ptr_reg,
19747 info->fp_save_offset,
19748 info->lr_save_offset,
19749 DFmode, sel);
19750 rs6000_frame_related (insn, ptr_reg, sp_off,
19751 NULL_RTX, NULL_RTX);
19752 if (lr)
19753 END_USE (0);
19756 /* Save GPRs. This is done as a PARALLEL if we are using
19757 the store-multiple instructions. */
19758 if (!WORLD_SAVE_P (info)
19759 && TARGET_SPE_ABI
19760 && info->spe_64bit_regs_used != 0
19761 && info->first_gp_reg_save != 32)
19763 int i;
19764 rtx spe_save_area_ptr;
19765 HOST_WIDE_INT save_off;
19766 int ool_adjust = 0;
19768 /* Determine whether we can address all of the registers that need
19769 to be saved with an offset from frame_reg_rtx that fits in
19770 the small const field for SPE memory instructions. */
19771 int spe_regs_addressable
19772 = (SPE_CONST_OFFSET_OK (info->spe_gp_save_offset + frame_off
19773 + reg_size * (32 - info->first_gp_reg_save - 1))
19774 && (strategy & SAVE_INLINE_GPRS));
19776 if (spe_regs_addressable)
19778 spe_save_area_ptr = frame_reg_rtx;
19779 save_off = frame_off;
19781 else
19783 /* Make r11 point to the start of the SPE save area. We need
19784 to be careful here if r11 is holding the static chain. If
19785 it is, then temporarily save it in r0. */
19786 HOST_WIDE_INT offset;
19788 if (!(strategy & SAVE_INLINE_GPRS))
19789 ool_adjust = 8 * (info->first_gp_reg_save
19790 - (FIRST_SAVRES_REGISTER + 1));
19791 offset = info->spe_gp_save_offset + frame_off - ool_adjust;
19792 spe_save_area_ptr = gen_rtx_REG (Pmode, 11);
19793 save_off = frame_off - offset;
19795 if (using_static_chain_p)
19797 rtx r0 = gen_rtx_REG (Pmode, 0);
19799 START_USE (0);
19800 gcc_assert (info->first_gp_reg_save > 11);
19802 emit_move_insn (r0, spe_save_area_ptr);
19804 else if (REGNO (frame_reg_rtx) != 11)
19805 START_USE (11);
19807 emit_insn (gen_addsi3 (spe_save_area_ptr,
19808 frame_reg_rtx, GEN_INT (offset)));
19809 if (!using_static_chain_p && REGNO (frame_reg_rtx) == 11)
19810 frame_off = -info->spe_gp_save_offset + ool_adjust;
19813 if ((strategy & SAVE_INLINE_GPRS))
19815 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
19816 if (rs6000_reg_live_or_pic_offset_p (info->first_gp_reg_save + i))
19817 emit_frame_save (spe_save_area_ptr, reg_mode,
19818 info->first_gp_reg_save + i,
19819 (info->spe_gp_save_offset + save_off
19820 + reg_size * i),
19821 sp_off - save_off);
19823 else
19825 insn = rs6000_emit_savres_rtx (info, spe_save_area_ptr,
19826 info->spe_gp_save_offset + save_off,
19827 0, reg_mode,
19828 SAVRES_SAVE | SAVRES_GPR);
19830 rs6000_frame_related (insn, spe_save_area_ptr, sp_off - save_off,
19831 NULL_RTX, NULL_RTX);
19834 /* Move the static chain pointer back. */
19835 if (!spe_regs_addressable)
19837 if (using_static_chain_p)
19839 emit_move_insn (spe_save_area_ptr, gen_rtx_REG (Pmode, 0));
19840 END_USE (0);
19842 else if (REGNO (frame_reg_rtx) != 11)
19843 END_USE (11);
19846 else if (!WORLD_SAVE_P (info) && !(strategy & SAVE_INLINE_GPRS))
19848 bool lr = (strategy & SAVE_NOINLINE_GPRS_SAVES_LR) != 0;
19849 int sel = SAVRES_SAVE | SAVRES_GPR | (lr ? SAVRES_LR : 0);
19850 unsigned ptr_regno = ptr_regno_for_savres (sel);
19851 rtx ptr_reg = frame_reg_rtx;
19852 bool ptr_set_up = REGNO (ptr_reg) == ptr_regno;
19853 int end_save = info->gp_save_offset + info->gp_size;
19854 int ptr_off;
19856 if (!ptr_set_up)
19857 ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
19859 /* Need to adjust r11 (r12) if we saved any FPRs. */
19860 if (end_save + frame_off != 0)
19862 rtx offset = GEN_INT (end_save + frame_off);
19864 if (ptr_set_up)
19865 frame_off = -end_save;
19866 else
19867 NOT_INUSE (ptr_regno);
19868 emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx, offset));
19870 else if (!ptr_set_up)
19872 NOT_INUSE (ptr_regno);
19873 emit_move_insn (ptr_reg, frame_reg_rtx);
19875 ptr_off = -end_save;
19876 insn = rs6000_emit_savres_rtx (info, ptr_reg,
19877 info->gp_save_offset + ptr_off,
19878 info->lr_save_offset + ptr_off,
19879 reg_mode, sel);
19880 rs6000_frame_related (insn, ptr_reg, sp_off - ptr_off,
19881 NULL_RTX, NULL_RTX);
19882 if (lr)
19883 END_USE (0);
19885 else if (!WORLD_SAVE_P (info) && (strategy & SAVRES_MULTIPLE))
19887 rtvec p;
19888 int i;
19889 p = rtvec_alloc (32 - info->first_gp_reg_save);
19890 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
19891 RTVEC_ELT (p, i)
19892 = gen_frame_store (gen_rtx_REG (reg_mode, info->first_gp_reg_save + i),
19893 frame_reg_rtx,
19894 info->gp_save_offset + frame_off + reg_size * i);
19895 insn = emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
19896 rs6000_frame_related (insn, frame_reg_rtx, sp_off - frame_off,
19897 NULL_RTX, NULL_RTX);
19899 else if (!WORLD_SAVE_P (info))
19901 int i;
19902 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
19903 if (rs6000_reg_live_or_pic_offset_p (info->first_gp_reg_save + i))
19904 emit_frame_save (frame_reg_rtx, reg_mode,
19905 info->first_gp_reg_save + i,
19906 info->gp_save_offset + frame_off + reg_size * i,
19907 sp_off - frame_off);
19910 if (crtl->calls_eh_return)
19912 unsigned int i;
19913 rtvec p;
19915 for (i = 0; ; ++i)
19917 unsigned int regno = EH_RETURN_DATA_REGNO (i);
19918 if (regno == INVALID_REGNUM)
19919 break;
19922 p = rtvec_alloc (i);
19924 for (i = 0; ; ++i)
19926 unsigned int regno = EH_RETURN_DATA_REGNO (i);
19927 if (regno == INVALID_REGNUM)
19928 break;
19930 insn
19931 = gen_frame_store (gen_rtx_REG (reg_mode, regno),
19932 sp_reg_rtx,
19933 info->ehrd_offset + sp_off + reg_size * (int) i);
19934 RTVEC_ELT (p, i) = insn;
19935 RTX_FRAME_RELATED_P (insn) = 1;
19938 insn = emit_insn (gen_blockage ());
19939 RTX_FRAME_RELATED_P (insn) = 1;
19940 add_reg_note (insn, REG_FRAME_RELATED_EXPR, gen_rtx_PARALLEL (VOIDmode, p));
19943 /* In AIX ABI we need to make sure r2 is really saved. */
19944 if (TARGET_AIX && crtl->calls_eh_return)
19946 rtx tmp_reg, tmp_reg_si, hi, lo, compare_result, toc_save_done, jump;
19947 rtx save_insn, join_insn, note;
19948 long toc_restore_insn;
19950 tmp_reg = gen_rtx_REG (Pmode, 11);
19951 tmp_reg_si = gen_rtx_REG (SImode, 11);
19952 if (using_static_chain_p)
19954 START_USE (0);
19955 emit_move_insn (gen_rtx_REG (Pmode, 0), tmp_reg);
19957 else
19958 START_USE (11);
19959 emit_move_insn (tmp_reg, gen_rtx_REG (Pmode, LR_REGNO));
19960 /* Peek at instruction to which this function returns. If it's
19961 restoring r2, then we know we've already saved r2. We can't
19962 unconditionally save r2 because the value we have will already
19963 be updated if we arrived at this function via a plt call or
19964 toc adjusting stub. */
19965 emit_move_insn (tmp_reg_si, gen_rtx_MEM (SImode, tmp_reg));
19966 toc_restore_insn = TARGET_32BIT ? 0x80410014 : 0xE8410028;
19967 hi = gen_int_mode (toc_restore_insn & ~0xffff, SImode);
19968 emit_insn (gen_xorsi3 (tmp_reg_si, tmp_reg_si, hi));
19969 compare_result = gen_rtx_REG (CCUNSmode, CR0_REGNO);
19970 validate_condition_mode (EQ, CCUNSmode);
19971 lo = gen_int_mode (toc_restore_insn & 0xffff, SImode);
19972 emit_insn (gen_rtx_SET (VOIDmode, compare_result,
19973 gen_rtx_COMPARE (CCUNSmode, tmp_reg_si, lo)));
19974 toc_save_done = gen_label_rtx ();
19975 jump = gen_rtx_IF_THEN_ELSE (VOIDmode,
19976 gen_rtx_EQ (VOIDmode, compare_result,
19977 const0_rtx),
19978 gen_rtx_LABEL_REF (VOIDmode, toc_save_done),
19979 pc_rtx);
19980 jump = emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, jump));
19981 JUMP_LABEL (jump) = toc_save_done;
19982 LABEL_NUSES (toc_save_done) += 1;
19984 save_insn = emit_frame_save (frame_reg_rtx, reg_mode,
19985 TOC_REGNUM, frame_off + 5 * reg_size,
19986 sp_off - frame_off);
19988 emit_label (toc_save_done);
19990 /* ??? If we leave SAVE_INSN as marked as saving R2, then we'll
19991 have a CFG that has different saves along different paths.
19992 Move the note to a dummy blockage insn, which describes that
19993 R2 is unconditionally saved after the label. */
19994 /* ??? An alternate representation might be a special insn pattern
19995 containing both the branch and the store. That might let the
19996 code that minimizes the number of DW_CFA_advance opcodes better
19997 freedom in placing the annotations. */
19998 note = find_reg_note (save_insn, REG_FRAME_RELATED_EXPR, NULL);
19999 if (note)
20000 remove_note (save_insn, note);
20001 else
20002 note = alloc_reg_note (REG_FRAME_RELATED_EXPR,
20003 copy_rtx (PATTERN (save_insn)), NULL_RTX);
20004 RTX_FRAME_RELATED_P (save_insn) = 0;
20006 join_insn = emit_insn (gen_blockage ());
20007 REG_NOTES (join_insn) = note;
20008 RTX_FRAME_RELATED_P (join_insn) = 1;
20010 if (using_static_chain_p)
20012 emit_move_insn (tmp_reg, gen_rtx_REG (Pmode, 0));
20013 END_USE (0);
20015 else
20016 END_USE (11);
20019 /* Save CR if we use any that must be preserved. */
20020 if (!WORLD_SAVE_P (info) && info->cr_save_p)
20022 rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
20023 GEN_INT (info->cr_save_offset + frame_off));
20024 rtx mem = gen_frame_mem (SImode, addr);
20025 /* See the large comment above about why CR2_REGNO is used. */
20026 rtx magic_eh_cr_reg = gen_rtx_REG (SImode, CR2_REGNO);
20028 /* If we didn't copy cr before, do so now using r0. */
20029 if (cr_save_rtx == NULL_RTX)
20031 rtx set;
20033 START_USE (0);
20034 cr_save_rtx = gen_rtx_REG (SImode, 0);
20035 insn = emit_insn (gen_movesi_from_cr (cr_save_rtx));
20036 RTX_FRAME_RELATED_P (insn) = 1;
20037 set = gen_rtx_SET (VOIDmode, cr_save_rtx, magic_eh_cr_reg);
20038 add_reg_note (insn, REG_FRAME_RELATED_EXPR, set);
20040 insn = emit_move_insn (mem, cr_save_rtx);
20041 END_USE (REGNO (cr_save_rtx));
20043 rs6000_frame_related (insn, frame_reg_rtx, sp_off - frame_off,
20044 NULL_RTX, NULL_RTX);
20047 /* Update stack and set back pointer unless this is V.4,
20048 for which it was done previously. */
20049 if (!WORLD_SAVE_P (info) && info->push_p
20050 && !(DEFAULT_ABI == ABI_V4 || crtl->calls_eh_return))
20052 rtx ptr_reg = NULL;
20053 int ptr_off = 0;
20055 /* If saving altivec regs we need to be able to address all save
20056 locations using a 16-bit offset. */
20057 if ((strategy & SAVE_INLINE_VRS) == 0
20058 || (info->altivec_size != 0
20059 && (info->altivec_save_offset + info->altivec_size - 16
20060 + info->total_size - frame_off) > 32767)
20061 || (info->vrsave_size != 0
20062 && (info->vrsave_save_offset
20063 + info->total_size - frame_off) > 32767))
20065 int sel = SAVRES_SAVE | SAVRES_VR;
20066 unsigned ptr_regno = ptr_regno_for_savres (sel);
20068 if (using_static_chain_p
20069 && ptr_regno == STATIC_CHAIN_REGNUM)
20070 ptr_regno = 12;
20071 if (REGNO (frame_reg_rtx) != ptr_regno)
20072 START_USE (ptr_regno);
20073 ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
20074 frame_reg_rtx = ptr_reg;
20075 ptr_off = info->altivec_save_offset + info->altivec_size;
20076 frame_off = -ptr_off;
20078 else if (REGNO (frame_reg_rtx) == 1)
20079 frame_off = info->total_size;
20080 rs6000_emit_allocate_stack (info->total_size, ptr_reg, ptr_off);
20081 sp_off = info->total_size;
20082 if (frame_reg_rtx != sp_reg_rtx)
20083 rs6000_emit_stack_tie (frame_reg_rtx, false);
20086 /* Set frame pointer, if needed. */
20087 if (frame_pointer_needed)
20089 insn = emit_move_insn (gen_rtx_REG (Pmode, HARD_FRAME_POINTER_REGNUM),
20090 sp_reg_rtx);
20091 RTX_FRAME_RELATED_P (insn) = 1;
20094 /* Save AltiVec registers if needed. Save here because the red zone does
20095 not always include AltiVec registers. */
20096 if (!WORLD_SAVE_P (info) && TARGET_ALTIVEC_ABI
20097 && info->altivec_size != 0 && (strategy & SAVE_INLINE_VRS) == 0)
20099 int end_save = info->altivec_save_offset + info->altivec_size;
20100 int ptr_off;
20101 /* Oddly, the vector save/restore functions point r0 at the end
20102 of the save area, then use r11 or r12 to load offsets for
20103 [reg+reg] addressing. */
20104 rtx ptr_reg = gen_rtx_REG (Pmode, 0);
20105 int scratch_regno = ptr_regno_for_savres (SAVRES_SAVE | SAVRES_VR);
20106 rtx scratch_reg = gen_rtx_REG (Pmode, scratch_regno);
20108 gcc_checking_assert (scratch_regno == 11 || scratch_regno == 12);
20109 NOT_INUSE (0);
20110 if (end_save + frame_off != 0)
20112 rtx offset = GEN_INT (end_save + frame_off);
20114 emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx, offset));
20116 else
20117 emit_move_insn (ptr_reg, frame_reg_rtx);
20119 ptr_off = -end_save;
20120 insn = rs6000_emit_savres_rtx (info, scratch_reg,
20121 info->altivec_save_offset + ptr_off,
20122 0, V4SImode, SAVRES_SAVE | SAVRES_VR);
20123 rs6000_frame_related (insn, scratch_reg, sp_off - ptr_off,
20124 NULL_RTX, NULL_RTX);
20125 if (REGNO (frame_reg_rtx) == REGNO (scratch_reg))
20127 /* The oddity mentioned above clobbered our frame reg. */
20128 emit_move_insn (frame_reg_rtx, ptr_reg);
20129 frame_off = ptr_off;
20132 else if (!WORLD_SAVE_P (info) && TARGET_ALTIVEC_ABI
20133 && info->altivec_size != 0)
20135 int i;
20137 for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
20138 if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
20140 rtx areg, savereg, mem;
20141 int offset;
20143 offset = (info->altivec_save_offset + frame_off
20144 + 16 * (i - info->first_altivec_reg_save));
20146 savereg = gen_rtx_REG (V4SImode, i);
20148 NOT_INUSE (0);
20149 areg = gen_rtx_REG (Pmode, 0);
20150 emit_move_insn (areg, GEN_INT (offset));
20152 /* AltiVec addressing mode is [reg+reg]. */
20153 mem = gen_frame_mem (V4SImode,
20154 gen_rtx_PLUS (Pmode, frame_reg_rtx, areg));
20156 insn = emit_move_insn (mem, savereg);
20158 rs6000_frame_related (insn, frame_reg_rtx, sp_off - frame_off,
20159 areg, GEN_INT (offset));
20163 /* VRSAVE is a bit vector representing which AltiVec registers
20164 are used. The OS uses this to determine which vector
20165 registers to save on a context switch. We need to save
20166 VRSAVE on the stack frame, add whatever AltiVec registers we
20167 used in this function, and do the corresponding magic in the
20168 epilogue. */
20170 if (!WORLD_SAVE_P (info)
20171 && TARGET_ALTIVEC
20172 && TARGET_ALTIVEC_VRSAVE
20173 && info->vrsave_mask != 0)
20175 rtx reg, vrsave;
20176 int offset;
20177 int save_regno;
20179 /* Get VRSAVE onto a GPR. Note that ABI_V4 and ABI_DARWIN might
20180 be using r12 as frame_reg_rtx and r11 as the static chain
20181 pointer for nested functions. */
20182 save_regno = 12;
20183 if (DEFAULT_ABI == ABI_AIX && !using_static_chain_p)
20184 save_regno = 11;
20185 else if (REGNO (frame_reg_rtx) == 12)
20187 save_regno = 11;
20188 if (using_static_chain_p)
20189 save_regno = 0;
20192 NOT_INUSE (save_regno);
20193 reg = gen_rtx_REG (SImode, save_regno);
20194 vrsave = gen_rtx_REG (SImode, VRSAVE_REGNO);
20195 if (TARGET_MACHO)
20196 emit_insn (gen_get_vrsave_internal (reg));
20197 else
20198 emit_insn (gen_rtx_SET (VOIDmode, reg, vrsave));
20200 /* Save VRSAVE. */
20201 offset = info->vrsave_save_offset + frame_off;
20202 insn = emit_insn (gen_frame_store (reg, frame_reg_rtx, offset));
20204 /* Include the registers in the mask. */
20205 emit_insn (gen_iorsi3 (reg, reg, GEN_INT ((int) info->vrsave_mask)));
20207 insn = emit_insn (generate_set_vrsave (reg, info, 0));
20210 /* If we are using RS6000_PIC_OFFSET_TABLE_REGNUM, we need to set it up. */
20211 if (!TARGET_SINGLE_PIC_BASE
20212 && ((TARGET_TOC && TARGET_MINIMAL_TOC && get_pool_size () != 0)
20213 || (DEFAULT_ABI == ABI_V4
20214 && (flag_pic == 1 || (flag_pic && TARGET_SECURE_PLT))
20215 && df_regs_ever_live_p (RS6000_PIC_OFFSET_TABLE_REGNUM))))
20217 /* If emit_load_toc_table will use the link register, we need to save
20218 it. We use R12 for this purpose because emit_load_toc_table
20219 can use register 0. This allows us to use a plain 'blr' to return
20220 from the procedure more often. */
20221 int save_LR_around_toc_setup = (TARGET_ELF
20222 && DEFAULT_ABI != ABI_AIX
20223 && flag_pic
20224 && ! info->lr_save_p
20225 && EDGE_COUNT (EXIT_BLOCK_PTR->preds) > 0);
20226 if (save_LR_around_toc_setup)
20228 rtx lr = gen_rtx_REG (Pmode, LR_REGNO);
20229 rtx tmp = gen_rtx_REG (Pmode, 12);
20231 insn = emit_move_insn (tmp, lr);
20232 RTX_FRAME_RELATED_P (insn) = 1;
20234 rs6000_emit_load_toc_table (TRUE);
20236 insn = emit_move_insn (lr, tmp);
20237 add_reg_note (insn, REG_CFA_RESTORE, lr);
20238 RTX_FRAME_RELATED_P (insn) = 1;
20240 else
20241 rs6000_emit_load_toc_table (TRUE);
20244 #if TARGET_MACHO
20245 if (!TARGET_SINGLE_PIC_BASE
20246 && DEFAULT_ABI == ABI_DARWIN
20247 && flag_pic && crtl->uses_pic_offset_table)
20249 rtx lr = gen_rtx_REG (Pmode, LR_REGNO);
20250 rtx src = gen_rtx_SYMBOL_REF (Pmode, MACHOPIC_FUNCTION_BASE_NAME);
20252 /* Save and restore LR locally around this call (in R0). */
20253 if (!info->lr_save_p)
20254 emit_move_insn (gen_rtx_REG (Pmode, 0), lr);
20256 emit_insn (gen_load_macho_picbase (src));
20258 emit_move_insn (gen_rtx_REG (Pmode,
20259 RS6000_PIC_OFFSET_TABLE_REGNUM),
20260 lr);
20262 if (!info->lr_save_p)
20263 emit_move_insn (lr, gen_rtx_REG (Pmode, 0));
20265 #endif
20267 /* If we need to, save the TOC register after doing the stack setup.
20268 Do not emit eh frame info for this save. The unwinder wants info,
20269 conceptually attached to instructions in this function, about
20270 register values in the caller of this function. This R2 may have
20271 already been changed from the value in the caller.
20272 We don't attempt to write accurate DWARF EH frame info for R2
20273 because code emitted by gcc for a (non-pointer) function call
20274 doesn't save and restore R2. Instead, R2 is managed out-of-line
20275 by a linker generated plt call stub when the function resides in
20276 a shared library. This behaviour is costly to describe in DWARF,
20277 both in terms of the size of DWARF info and the time taken in the
20278 unwinder to interpret it. R2 changes, apart from the
20279 calls_eh_return case earlier in this function, are handled by
20280 linux-unwind.h frob_update_context. */
20281 if (rs6000_save_toc_in_prologue_p ())
20283 rtx reg = gen_rtx_REG (reg_mode, TOC_REGNUM);
20284 emit_insn (gen_frame_store (reg, sp_reg_rtx, 5 * reg_size));
20288 /* Write function prologue. */
20290 static void
20291 rs6000_output_function_prologue (FILE *file,
20292 HOST_WIDE_INT size ATTRIBUTE_UNUSED)
20294 rs6000_stack_t *info = rs6000_stack_info ();
20296 if (TARGET_DEBUG_STACK)
20297 debug_stack_info (info);
20299 /* Write .extern for any function we will call to save and restore
20300 fp values. */
20301 if (info->first_fp_reg_save < 64
20302 && !TARGET_MACHO
20303 && !TARGET_ELF)
20305 char *name;
20306 int regno = info->first_fp_reg_save - 32;
20308 if ((info->savres_strategy & SAVE_INLINE_FPRS) == 0)
20310 bool lr = (info->savres_strategy & SAVE_NOINLINE_FPRS_SAVES_LR) != 0;
20311 int sel = SAVRES_SAVE | SAVRES_FPR | (lr ? SAVRES_LR : 0);
20312 name = rs6000_savres_routine_name (info, regno, sel);
20313 fprintf (file, "\t.extern %s\n", name);
20315 if ((info->savres_strategy & REST_INLINE_FPRS) == 0)
20317 bool lr = (info->savres_strategy
20318 & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR) == 0;
20319 int sel = SAVRES_FPR | (lr ? SAVRES_LR : 0);
20320 name = rs6000_savres_routine_name (info, regno, sel);
20321 fprintf (file, "\t.extern %s\n", name);
20325 rs6000_pic_labelno++;
20328 /* Non-zero if vmx regs are restored before the frame pop, zero if
20329 we restore after the pop when possible. */
20330 #define ALWAYS_RESTORE_ALTIVEC_BEFORE_POP 0
20332 /* Restoring cr is a two step process: loading a reg from the frame
20333 save, then moving the reg to cr. For ABI_V4 we must let the
20334 unwinder know that the stack location is no longer valid at or
20335 before the stack deallocation, but we can't emit a cfa_restore for
20336 cr at the stack deallocation like we do for other registers.
20337 The trouble is that it is possible for the move to cr to be
20338 scheduled after the stack deallocation. So say exactly where cr
20339 is located on each of the two insns. */
20341 static rtx
20342 load_cr_save (int regno, rtx frame_reg_rtx, int offset, bool exit_func)
20344 rtx mem = gen_frame_mem_offset (SImode, frame_reg_rtx, offset);
20345 rtx reg = gen_rtx_REG (SImode, regno);
20346 rtx insn = emit_move_insn (reg, mem);
20348 if (!exit_func && DEFAULT_ABI == ABI_V4)
20350 rtx cr = gen_rtx_REG (SImode, CR2_REGNO);
20351 rtx set = gen_rtx_SET (VOIDmode, reg, cr);
20353 add_reg_note (insn, REG_CFA_REGISTER, set);
20354 RTX_FRAME_RELATED_P (insn) = 1;
20356 return reg;
20359 /* Reload CR from REG. */
20361 static void
20362 restore_saved_cr (rtx reg, int using_mfcr_multiple, bool exit_func)
20364 int count = 0;
20365 int i;
20367 if (using_mfcr_multiple)
20369 for (i = 0; i < 8; i++)
20370 if (save_reg_p (CR0_REGNO + i))
20371 count++;
20372 gcc_assert (count);
20375 if (using_mfcr_multiple && count > 1)
20377 rtvec p;
20378 int ndx;
20380 p = rtvec_alloc (count);
20382 ndx = 0;
20383 for (i = 0; i < 8; i++)
20384 if (save_reg_p (CR0_REGNO + i))
20386 rtvec r = rtvec_alloc (2);
20387 RTVEC_ELT (r, 0) = reg;
20388 RTVEC_ELT (r, 1) = GEN_INT (1 << (7-i));
20389 RTVEC_ELT (p, ndx) =
20390 gen_rtx_SET (VOIDmode, gen_rtx_REG (CCmode, CR0_REGNO + i),
20391 gen_rtx_UNSPEC (CCmode, r, UNSPEC_MOVESI_TO_CR));
20392 ndx++;
20394 emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
20395 gcc_assert (ndx == count);
20397 else
20398 for (i = 0; i < 8; i++)
20399 if (save_reg_p (CR0_REGNO + i))
20400 emit_insn (gen_movsi_to_cr_one (gen_rtx_REG (CCmode, CR0_REGNO + i),
20401 reg));
20403 if (!exit_func && (DEFAULT_ABI == ABI_V4 || flag_shrink_wrap))
20405 rtx insn = get_last_insn ();
20406 rtx cr = gen_rtx_REG (SImode, CR2_REGNO);
20408 add_reg_note (insn, REG_CFA_RESTORE, cr);
20409 RTX_FRAME_RELATED_P (insn) = 1;
20413 /* Like cr, the move to lr instruction can be scheduled after the
20414 stack deallocation, but unlike cr, its stack frame save is still
20415 valid. So we only need to emit the cfa_restore on the correct
20416 instruction. */
20418 static void
20419 load_lr_save (int regno, rtx frame_reg_rtx, int offset)
20421 rtx mem = gen_frame_mem_offset (Pmode, frame_reg_rtx, offset);
20422 rtx reg = gen_rtx_REG (Pmode, regno);
20424 emit_move_insn (reg, mem);
20427 static void
20428 restore_saved_lr (int regno, bool exit_func)
20430 rtx reg = gen_rtx_REG (Pmode, regno);
20431 rtx lr = gen_rtx_REG (Pmode, LR_REGNO);
20432 rtx insn = emit_move_insn (lr, reg);
20434 if (!exit_func && flag_shrink_wrap)
20436 add_reg_note (insn, REG_CFA_RESTORE, lr);
20437 RTX_FRAME_RELATED_P (insn) = 1;
20441 static rtx
20442 add_crlr_cfa_restore (const rs6000_stack_t *info, rtx cfa_restores)
20444 if (info->cr_save_p)
20445 cfa_restores = alloc_reg_note (REG_CFA_RESTORE,
20446 gen_rtx_REG (SImode, CR2_REGNO),
20447 cfa_restores);
20448 if (info->lr_save_p)
20449 cfa_restores = alloc_reg_note (REG_CFA_RESTORE,
20450 gen_rtx_REG (Pmode, LR_REGNO),
20451 cfa_restores);
20452 return cfa_restores;
20455 /* Return true if OFFSET from stack pointer can be clobbered by signals.
20456 V.4 doesn't have any stack cushion, AIX ABIs have 220 or 288 bytes
20457 below stack pointer not cloberred by signals. */
20459 static inline bool
20460 offset_below_red_zone_p (HOST_WIDE_INT offset)
20462 return offset < (DEFAULT_ABI == ABI_V4
20464 : TARGET_32BIT ? -220 : -288);
20467 /* Append CFA_RESTORES to any existing REG_NOTES on the last insn. */
20469 static void
20470 emit_cfa_restores (rtx cfa_restores)
20472 rtx insn = get_last_insn ();
20473 rtx *loc = &REG_NOTES (insn);
20475 while (*loc)
20476 loc = &XEXP (*loc, 1);
20477 *loc = cfa_restores;
20478 RTX_FRAME_RELATED_P (insn) = 1;
20481 /* Emit function epilogue as insns. */
20483 void
20484 rs6000_emit_epilogue (int sibcall)
20486 rs6000_stack_t *info;
20487 int restoring_GPRs_inline;
20488 int restoring_FPRs_inline;
20489 int using_load_multiple;
20490 int using_mtcr_multiple;
20491 int use_backchain_to_restore_sp;
20492 int restore_lr;
20493 int strategy;
20494 HOST_WIDE_INT frame_off = 0;
20495 rtx sp_reg_rtx = gen_rtx_REG (Pmode, 1);
20496 rtx frame_reg_rtx = sp_reg_rtx;
20497 rtx cfa_restores = NULL_RTX;
20498 rtx insn;
20499 rtx cr_save_reg = NULL_RTX;
20500 enum machine_mode reg_mode = Pmode;
20501 int reg_size = TARGET_32BIT ? 4 : 8;
20502 int i;
20503 bool exit_func;
20504 unsigned ptr_regno;
20506 info = rs6000_stack_info ();
20508 if (TARGET_SPE_ABI && info->spe_64bit_regs_used != 0)
20510 reg_mode = V2SImode;
20511 reg_size = 8;
20514 strategy = info->savres_strategy;
20515 using_load_multiple = strategy & SAVRES_MULTIPLE;
20516 restoring_FPRs_inline = sibcall || (strategy & REST_INLINE_FPRS);
20517 restoring_GPRs_inline = sibcall || (strategy & REST_INLINE_GPRS);
20518 using_mtcr_multiple = (rs6000_cpu == PROCESSOR_PPC601
20519 || rs6000_cpu == PROCESSOR_PPC603
20520 || rs6000_cpu == PROCESSOR_PPC750
20521 || optimize_size);
20522 /* Restore via the backchain when we have a large frame, since this
20523 is more efficient than an addis, addi pair. The second condition
20524 here will not trigger at the moment; We don't actually need a
20525 frame pointer for alloca, but the generic parts of the compiler
20526 give us one anyway. */
20527 use_backchain_to_restore_sp = (info->total_size > 32767 - info->lr_save_offset
20528 || (cfun->calls_alloca
20529 && !frame_pointer_needed));
20530 restore_lr = (info->lr_save_p
20531 && (restoring_FPRs_inline
20532 || (strategy & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR))
20533 && (restoring_GPRs_inline
20534 || info->first_fp_reg_save < 64));
20536 if (WORLD_SAVE_P (info))
20538 int i, j;
20539 char rname[30];
20540 const char *alloc_rname;
20541 rtvec p;
20543 /* eh_rest_world_r10 will return to the location saved in the LR
20544 stack slot (which is not likely to be our caller.)
20545 Input: R10 -- stack adjustment. Clobbers R0, R11, R12, R7, R8.
20546 rest_world is similar, except any R10 parameter is ignored.
20547 The exception-handling stuff that was here in 2.95 is no
20548 longer necessary. */
20550 p = rtvec_alloc (9
20552 + 32 - info->first_gp_reg_save
20553 + LAST_ALTIVEC_REGNO + 1 - info->first_altivec_reg_save
20554 + 63 + 1 - info->first_fp_reg_save);
20556 strcpy (rname, ((crtl->calls_eh_return) ?
20557 "*eh_rest_world_r10" : "*rest_world"));
20558 alloc_rname = ggc_strdup (rname);
20560 j = 0;
20561 RTVEC_ELT (p, j++) = ret_rtx;
20562 RTVEC_ELT (p, j++) = gen_rtx_USE (VOIDmode,
20563 gen_rtx_REG (Pmode,
20564 LR_REGNO));
20565 RTVEC_ELT (p, j++)
20566 = gen_rtx_USE (VOIDmode, gen_rtx_SYMBOL_REF (Pmode, alloc_rname));
20567 /* The instruction pattern requires a clobber here;
20568 it is shared with the restVEC helper. */
20569 RTVEC_ELT (p, j++)
20570 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, 11));
20573 /* CR register traditionally saved as CR2. */
20574 rtx reg = gen_rtx_REG (SImode, CR2_REGNO);
20575 RTVEC_ELT (p, j++)
20576 = gen_frame_load (reg, frame_reg_rtx, info->cr_save_offset);
20577 if (flag_shrink_wrap)
20579 cfa_restores = alloc_reg_note (REG_CFA_RESTORE,
20580 gen_rtx_REG (Pmode, LR_REGNO),
20581 cfa_restores);
20582 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
20586 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
20588 rtx reg = gen_rtx_REG (reg_mode, info->first_gp_reg_save + i);
20589 RTVEC_ELT (p, j++)
20590 = gen_frame_load (reg,
20591 frame_reg_rtx, info->gp_save_offset + reg_size * i);
20592 if (flag_shrink_wrap)
20593 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
20595 for (i = 0; info->first_altivec_reg_save + i <= LAST_ALTIVEC_REGNO; i++)
20597 rtx reg = gen_rtx_REG (V4SImode, info->first_altivec_reg_save + i);
20598 RTVEC_ELT (p, j++)
20599 = gen_frame_load (reg,
20600 frame_reg_rtx, info->altivec_save_offset + 16 * i);
20601 if (flag_shrink_wrap)
20602 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
20604 for (i = 0; info->first_fp_reg_save + i <= 63; i++)
20606 rtx reg = gen_rtx_REG ((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT
20607 ? DFmode : SFmode),
20608 info->first_fp_reg_save + i);
20609 RTVEC_ELT (p, j++)
20610 = gen_frame_load (reg, frame_reg_rtx, info->fp_save_offset + 8 * i);
20611 if (flag_shrink_wrap)
20612 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
20614 RTVEC_ELT (p, j++)
20615 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, 0));
20616 RTVEC_ELT (p, j++)
20617 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (SImode, 12));
20618 RTVEC_ELT (p, j++)
20619 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (SImode, 7));
20620 RTVEC_ELT (p, j++)
20621 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (SImode, 8));
20622 RTVEC_ELT (p, j++)
20623 = gen_rtx_USE (VOIDmode, gen_rtx_REG (SImode, 10));
20624 insn = emit_jump_insn (gen_rtx_PARALLEL (VOIDmode, p));
20626 if (flag_shrink_wrap)
20628 REG_NOTES (insn) = cfa_restores;
20629 add_reg_note (insn, REG_CFA_DEF_CFA, sp_reg_rtx);
20630 RTX_FRAME_RELATED_P (insn) = 1;
20632 return;
20635 /* frame_reg_rtx + frame_off points to the top of this stack frame. */
20636 if (info->push_p)
20637 frame_off = info->total_size;
20639 /* Restore AltiVec registers if we must do so before adjusting the
20640 stack. */
20641 if (TARGET_ALTIVEC_ABI
20642 && info->altivec_size != 0
20643 && (ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
20644 || (DEFAULT_ABI != ABI_V4
20645 && offset_below_red_zone_p (info->altivec_save_offset))))
20647 int i;
20648 int scratch_regno = ptr_regno_for_savres (SAVRES_VR);
20650 gcc_checking_assert (scratch_regno == 11 || scratch_regno == 12);
20651 if (use_backchain_to_restore_sp)
20653 int frame_regno = 11;
20655 if ((strategy & REST_INLINE_VRS) == 0)
20657 /* Of r11 and r12, select the one not clobbered by an
20658 out-of-line restore function for the frame register. */
20659 frame_regno = 11 + 12 - scratch_regno;
20661 frame_reg_rtx = gen_rtx_REG (Pmode, frame_regno);
20662 emit_move_insn (frame_reg_rtx,
20663 gen_rtx_MEM (Pmode, sp_reg_rtx));
20664 frame_off = 0;
20666 else if (frame_pointer_needed)
20667 frame_reg_rtx = hard_frame_pointer_rtx;
20669 if ((strategy & REST_INLINE_VRS) == 0)
20671 int end_save = info->altivec_save_offset + info->altivec_size;
20672 int ptr_off;
20673 rtx ptr_reg = gen_rtx_REG (Pmode, 0);
20674 rtx scratch_reg = gen_rtx_REG (Pmode, scratch_regno);
20676 if (end_save + frame_off != 0)
20678 rtx offset = GEN_INT (end_save + frame_off);
20680 emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx, offset));
20682 else
20683 emit_move_insn (ptr_reg, frame_reg_rtx);
20685 ptr_off = -end_save;
20686 insn = rs6000_emit_savres_rtx (info, scratch_reg,
20687 info->altivec_save_offset + ptr_off,
20688 0, V4SImode, SAVRES_VR);
20690 else
20692 for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
20693 if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
20695 rtx addr, areg, mem, reg;
20697 areg = gen_rtx_REG (Pmode, 0);
20698 emit_move_insn
20699 (areg, GEN_INT (info->altivec_save_offset
20700 + frame_off
20701 + 16 * (i - info->first_altivec_reg_save)));
20703 /* AltiVec addressing mode is [reg+reg]. */
20704 addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, areg);
20705 mem = gen_frame_mem (V4SImode, addr);
20707 reg = gen_rtx_REG (V4SImode, i);
20708 emit_move_insn (reg, mem);
20712 for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
20713 if (((strategy & REST_INLINE_VRS) == 0
20714 || (info->vrsave_mask & ALTIVEC_REG_BIT (i)) != 0)
20715 && (flag_shrink_wrap
20716 || (offset_below_red_zone_p
20717 (info->altivec_save_offset
20718 + 16 * (i - info->first_altivec_reg_save)))))
20720 rtx reg = gen_rtx_REG (V4SImode, i);
20721 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
20725 /* Restore VRSAVE if we must do so before adjusting the stack. */
20726 if (TARGET_ALTIVEC
20727 && TARGET_ALTIVEC_VRSAVE
20728 && info->vrsave_mask != 0
20729 && (ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
20730 || (DEFAULT_ABI != ABI_V4
20731 && offset_below_red_zone_p (info->vrsave_save_offset))))
20733 rtx reg;
20735 if (frame_reg_rtx == sp_reg_rtx)
20737 if (use_backchain_to_restore_sp)
20739 frame_reg_rtx = gen_rtx_REG (Pmode, 11);
20740 emit_move_insn (frame_reg_rtx,
20741 gen_rtx_MEM (Pmode, sp_reg_rtx));
20742 frame_off = 0;
20744 else if (frame_pointer_needed)
20745 frame_reg_rtx = hard_frame_pointer_rtx;
20748 reg = gen_rtx_REG (SImode, 12);
20749 emit_insn (gen_frame_load (reg, frame_reg_rtx,
20750 info->vrsave_save_offset + frame_off));
20752 emit_insn (generate_set_vrsave (reg, info, 1));
20755 insn = NULL_RTX;
20756 /* If we have a large stack frame, restore the old stack pointer
20757 using the backchain. */
20758 if (use_backchain_to_restore_sp)
20760 if (frame_reg_rtx == sp_reg_rtx)
20762 /* Under V.4, don't reset the stack pointer until after we're done
20763 loading the saved registers. */
20764 if (DEFAULT_ABI == ABI_V4)
20765 frame_reg_rtx = gen_rtx_REG (Pmode, 11);
20767 insn = emit_move_insn (frame_reg_rtx,
20768 gen_rtx_MEM (Pmode, sp_reg_rtx));
20769 frame_off = 0;
20771 else if (ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
20772 && DEFAULT_ABI == ABI_V4)
20773 /* frame_reg_rtx has been set up by the altivec restore. */
20775 else
20777 insn = emit_move_insn (sp_reg_rtx, frame_reg_rtx);
20778 frame_reg_rtx = sp_reg_rtx;
20781 /* If we have a frame pointer, we can restore the old stack pointer
20782 from it. */
20783 else if (frame_pointer_needed)
20785 frame_reg_rtx = sp_reg_rtx;
20786 if (DEFAULT_ABI == ABI_V4)
20787 frame_reg_rtx = gen_rtx_REG (Pmode, 11);
20788 /* Prevent reordering memory accesses against stack pointer restore. */
20789 else if (cfun->calls_alloca
20790 || offset_below_red_zone_p (-info->total_size))
20791 rs6000_emit_stack_tie (frame_reg_rtx, true);
20793 insn = emit_insn (gen_add3_insn (frame_reg_rtx, hard_frame_pointer_rtx,
20794 GEN_INT (info->total_size)));
20795 frame_off = 0;
20797 else if (info->push_p
20798 && DEFAULT_ABI != ABI_V4
20799 && !crtl->calls_eh_return)
20801 /* Prevent reordering memory accesses against stack pointer restore. */
20802 if (cfun->calls_alloca
20803 || offset_below_red_zone_p (-info->total_size))
20804 rs6000_emit_stack_tie (frame_reg_rtx, false);
20805 insn = emit_insn (gen_add3_insn (sp_reg_rtx, sp_reg_rtx,
20806 GEN_INT (info->total_size)));
20807 frame_off = 0;
20809 if (insn && frame_reg_rtx == sp_reg_rtx)
20811 if (cfa_restores)
20813 REG_NOTES (insn) = cfa_restores;
20814 cfa_restores = NULL_RTX;
20816 add_reg_note (insn, REG_CFA_DEF_CFA, sp_reg_rtx);
20817 RTX_FRAME_RELATED_P (insn) = 1;
20820 /* Restore AltiVec registers if we have not done so already. */
20821 if (!ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
20822 && TARGET_ALTIVEC_ABI
20823 && info->altivec_size != 0
20824 && (DEFAULT_ABI == ABI_V4
20825 || !offset_below_red_zone_p (info->altivec_save_offset)))
20827 int i;
20829 if ((strategy & REST_INLINE_VRS) == 0)
20831 int end_save = info->altivec_save_offset + info->altivec_size;
20832 int ptr_off;
20833 rtx ptr_reg = gen_rtx_REG (Pmode, 0);
20834 int scratch_regno = ptr_regno_for_savres (SAVRES_VR);
20835 rtx scratch_reg = gen_rtx_REG (Pmode, scratch_regno);
20837 if (end_save + frame_off != 0)
20839 rtx offset = GEN_INT (end_save + frame_off);
20841 emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx, offset));
20843 else
20844 emit_move_insn (ptr_reg, frame_reg_rtx);
20846 ptr_off = -end_save;
20847 insn = rs6000_emit_savres_rtx (info, scratch_reg,
20848 info->altivec_save_offset + ptr_off,
20849 0, V4SImode, SAVRES_VR);
20850 if (REGNO (frame_reg_rtx) == REGNO (scratch_reg))
20852 /* Frame reg was clobbered by out-of-line save. Restore it
20853 from ptr_reg, and if we are calling out-of-line gpr or
20854 fpr restore set up the correct pointer and offset. */
20855 unsigned newptr_regno = 1;
20856 if (!restoring_GPRs_inline)
20858 bool lr = info->gp_save_offset + info->gp_size == 0;
20859 int sel = SAVRES_GPR | (lr ? SAVRES_LR : 0);
20860 newptr_regno = ptr_regno_for_savres (sel);
20861 end_save = info->gp_save_offset + info->gp_size;
20863 else if (!restoring_FPRs_inline)
20865 bool lr = !(strategy & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR);
20866 int sel = SAVRES_FPR | (lr ? SAVRES_LR : 0);
20867 newptr_regno = ptr_regno_for_savres (sel);
20868 end_save = info->gp_save_offset + info->gp_size;
20871 if (newptr_regno != 1 && REGNO (frame_reg_rtx) != newptr_regno)
20872 frame_reg_rtx = gen_rtx_REG (Pmode, newptr_regno);
20874 if (end_save + ptr_off != 0)
20876 rtx offset = GEN_INT (end_save + ptr_off);
20878 frame_off = -end_save;
20879 emit_insn (gen_add3_insn (frame_reg_rtx, ptr_reg, offset));
20881 else
20883 frame_off = ptr_off;
20884 emit_move_insn (frame_reg_rtx, ptr_reg);
20888 else
20890 for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
20891 if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
20893 rtx addr, areg, mem, reg;
20895 areg = gen_rtx_REG (Pmode, 0);
20896 emit_move_insn
20897 (areg, GEN_INT (info->altivec_save_offset
20898 + frame_off
20899 + 16 * (i - info->first_altivec_reg_save)));
20901 /* AltiVec addressing mode is [reg+reg]. */
20902 addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, areg);
20903 mem = gen_frame_mem (V4SImode, addr);
20905 reg = gen_rtx_REG (V4SImode, i);
20906 emit_move_insn (reg, mem);
20910 for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
20911 if (((strategy & REST_INLINE_VRS) == 0
20912 || (info->vrsave_mask & ALTIVEC_REG_BIT (i)) != 0)
20913 && (DEFAULT_ABI == ABI_V4 || flag_shrink_wrap))
20915 rtx reg = gen_rtx_REG (V4SImode, i);
20916 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
20920 /* Restore VRSAVE if we have not done so already. */
20921 if (!ALWAYS_RESTORE_ALTIVEC_BEFORE_POP
20922 && TARGET_ALTIVEC
20923 && TARGET_ALTIVEC_VRSAVE
20924 && info->vrsave_mask != 0
20925 && (DEFAULT_ABI == ABI_V4
20926 || !offset_below_red_zone_p (info->vrsave_save_offset)))
20928 rtx reg;
20930 reg = gen_rtx_REG (SImode, 12);
20931 emit_insn (gen_frame_load (reg, frame_reg_rtx,
20932 info->vrsave_save_offset + frame_off));
20934 emit_insn (generate_set_vrsave (reg, info, 1));
20937 /* If we exit by an out-of-line restore function on ABI_V4 then that
20938 function will deallocate the stack, so we don't need to worry
20939 about the unwinder restoring cr from an invalid stack frame
20940 location. */
20941 exit_func = (!restoring_FPRs_inline
20942 || (!restoring_GPRs_inline
20943 && info->first_fp_reg_save == 64));
20945 /* Get the old lr if we saved it. If we are restoring registers
20946 out-of-line, then the out-of-line routines can do this for us. */
20947 if (restore_lr && restoring_GPRs_inline)
20948 load_lr_save (0, frame_reg_rtx, info->lr_save_offset + frame_off);
20950 /* Get the old cr if we saved it. */
20951 if (info->cr_save_p)
20953 unsigned cr_save_regno = 12;
20955 if (!restoring_GPRs_inline)
20957 /* Ensure we don't use the register used by the out-of-line
20958 gpr register restore below. */
20959 bool lr = info->gp_save_offset + info->gp_size == 0;
20960 int sel = SAVRES_GPR | (lr ? SAVRES_LR : 0);
20961 int gpr_ptr_regno = ptr_regno_for_savres (sel);
20963 if (gpr_ptr_regno == 12)
20964 cr_save_regno = 11;
20965 gcc_checking_assert (REGNO (frame_reg_rtx) != cr_save_regno);
20967 else if (REGNO (frame_reg_rtx) == 12)
20968 cr_save_regno = 11;
20970 cr_save_reg = load_cr_save (cr_save_regno, frame_reg_rtx,
20971 info->cr_save_offset + frame_off,
20972 exit_func);
20975 /* Set LR here to try to overlap restores below. */
20976 if (restore_lr && restoring_GPRs_inline)
20977 restore_saved_lr (0, exit_func);
20979 /* Load exception handler data registers, if needed. */
20980 if (crtl->calls_eh_return)
20982 unsigned int i, regno;
20984 if (TARGET_AIX)
20986 rtx reg = gen_rtx_REG (reg_mode, 2);
20987 emit_insn (gen_frame_load (reg, frame_reg_rtx,
20988 frame_off + 5 * reg_size));
20991 for (i = 0; ; ++i)
20993 rtx mem;
20995 regno = EH_RETURN_DATA_REGNO (i);
20996 if (regno == INVALID_REGNUM)
20997 break;
20999 /* Note: possible use of r0 here to address SPE regs. */
21000 mem = gen_frame_mem_offset (reg_mode, frame_reg_rtx,
21001 info->ehrd_offset + frame_off
21002 + reg_size * (int) i);
21004 emit_move_insn (gen_rtx_REG (reg_mode, regno), mem);
21008 /* Restore GPRs. This is done as a PARALLEL if we are using
21009 the load-multiple instructions. */
21010 if (TARGET_SPE_ABI
21011 && info->spe_64bit_regs_used
21012 && info->first_gp_reg_save != 32)
21014 /* Determine whether we can address all of the registers that need
21015 to be saved with an offset from frame_reg_rtx that fits in
21016 the small const field for SPE memory instructions. */
21017 int spe_regs_addressable
21018 = (SPE_CONST_OFFSET_OK (info->spe_gp_save_offset + frame_off
21019 + reg_size * (32 - info->first_gp_reg_save - 1))
21020 && restoring_GPRs_inline);
21022 if (!spe_regs_addressable)
21024 int ool_adjust = 0;
21025 rtx old_frame_reg_rtx = frame_reg_rtx;
21026 /* Make r11 point to the start of the SPE save area. We worried about
21027 not clobbering it when we were saving registers in the prologue.
21028 There's no need to worry here because the static chain is passed
21029 anew to every function. */
21031 if (!restoring_GPRs_inline)
21032 ool_adjust = 8 * (info->first_gp_reg_save
21033 - (FIRST_SAVRES_REGISTER + 1));
21034 frame_reg_rtx = gen_rtx_REG (Pmode, 11);
21035 emit_insn (gen_addsi3 (frame_reg_rtx, old_frame_reg_rtx,
21036 GEN_INT (info->spe_gp_save_offset
21037 + frame_off
21038 - ool_adjust)));
21039 /* Keep the invariant that frame_reg_rtx + frame_off points
21040 at the top of the stack frame. */
21041 frame_off = -info->spe_gp_save_offset + ool_adjust;
21044 if (restoring_GPRs_inline)
21046 HOST_WIDE_INT spe_offset = info->spe_gp_save_offset + frame_off;
21048 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
21049 if (rs6000_reg_live_or_pic_offset_p (info->first_gp_reg_save + i))
21051 rtx offset, addr, mem, reg;
21053 /* We're doing all this to ensure that the immediate offset
21054 fits into the immediate field of 'evldd'. */
21055 gcc_assert (SPE_CONST_OFFSET_OK (spe_offset + reg_size * i));
21057 offset = GEN_INT (spe_offset + reg_size * i);
21058 addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, offset);
21059 mem = gen_rtx_MEM (V2SImode, addr);
21060 reg = gen_rtx_REG (reg_mode, info->first_gp_reg_save + i);
21062 emit_move_insn (reg, mem);
21065 else
21066 rs6000_emit_savres_rtx (info, frame_reg_rtx,
21067 info->spe_gp_save_offset + frame_off,
21068 info->lr_save_offset + frame_off,
21069 reg_mode,
21070 SAVRES_GPR | SAVRES_LR);
21072 else if (!restoring_GPRs_inline)
21074 /* We are jumping to an out-of-line function. */
21075 rtx ptr_reg;
21076 int end_save = info->gp_save_offset + info->gp_size;
21077 bool can_use_exit = end_save == 0;
21078 int sel = SAVRES_GPR | (can_use_exit ? SAVRES_LR : 0);
21079 int ptr_off;
21081 /* Emit stack reset code if we need it. */
21082 ptr_regno = ptr_regno_for_savres (sel);
21083 ptr_reg = gen_rtx_REG (Pmode, ptr_regno);
21084 if (can_use_exit)
21085 rs6000_emit_stack_reset (info, frame_reg_rtx, frame_off, ptr_regno);
21086 else if (end_save + frame_off != 0)
21087 emit_insn (gen_add3_insn (ptr_reg, frame_reg_rtx,
21088 GEN_INT (end_save + frame_off)));
21089 else if (REGNO (frame_reg_rtx) != ptr_regno)
21090 emit_move_insn (ptr_reg, frame_reg_rtx);
21091 if (REGNO (frame_reg_rtx) == ptr_regno)
21092 frame_off = -end_save;
21094 if (can_use_exit && info->cr_save_p)
21095 restore_saved_cr (cr_save_reg, using_mtcr_multiple, true);
21097 ptr_off = -end_save;
21098 rs6000_emit_savres_rtx (info, ptr_reg,
21099 info->gp_save_offset + ptr_off,
21100 info->lr_save_offset + ptr_off,
21101 reg_mode, sel);
21103 else if (using_load_multiple)
21105 rtvec p;
21106 p = rtvec_alloc (32 - info->first_gp_reg_save);
21107 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
21108 RTVEC_ELT (p, i)
21109 = gen_frame_load (gen_rtx_REG (reg_mode, info->first_gp_reg_save + i),
21110 frame_reg_rtx,
21111 info->gp_save_offset + frame_off + reg_size * i);
21112 emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
21114 else
21116 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
21117 if (rs6000_reg_live_or_pic_offset_p (info->first_gp_reg_save + i))
21118 emit_insn (gen_frame_load
21119 (gen_rtx_REG (reg_mode, info->first_gp_reg_save + i),
21120 frame_reg_rtx,
21121 info->gp_save_offset + frame_off + reg_size * i));
21124 if (DEFAULT_ABI == ABI_V4 || flag_shrink_wrap)
21126 /* If the frame pointer was used then we can't delay emitting
21127 a REG_CFA_DEF_CFA note. This must happen on the insn that
21128 restores the frame pointer, r31. We may have already emitted
21129 a REG_CFA_DEF_CFA note, but that's OK; A duplicate is
21130 discarded by dwarf2cfi.c/dwarf2out.c, and in any case would
21131 be harmless if emitted. */
21132 if (frame_pointer_needed)
21134 insn = get_last_insn ();
21135 add_reg_note (insn, REG_CFA_DEF_CFA,
21136 plus_constant (Pmode, frame_reg_rtx, frame_off));
21137 RTX_FRAME_RELATED_P (insn) = 1;
21140 /* Set up cfa_restores. We always need these when
21141 shrink-wrapping. If not shrink-wrapping then we only need
21142 the cfa_restore when the stack location is no longer valid.
21143 The cfa_restores must be emitted on or before the insn that
21144 invalidates the stack, and of course must not be emitted
21145 before the insn that actually does the restore. The latter
21146 is why it is a bad idea to emit the cfa_restores as a group
21147 on the last instruction here that actually does a restore:
21148 That insn may be reordered with respect to others doing
21149 restores. */
21150 if (flag_shrink_wrap
21151 && !restoring_GPRs_inline
21152 && info->first_fp_reg_save == 64)
21153 cfa_restores = add_crlr_cfa_restore (info, cfa_restores);
21155 for (i = info->first_gp_reg_save; i < 32; i++)
21156 if (!restoring_GPRs_inline
21157 || using_load_multiple
21158 || rs6000_reg_live_or_pic_offset_p (i))
21160 rtx reg = gen_rtx_REG (reg_mode, i);
21162 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
21166 if (!restoring_GPRs_inline
21167 && info->first_fp_reg_save == 64)
21169 /* We are jumping to an out-of-line function. */
21170 if (cfa_restores)
21171 emit_cfa_restores (cfa_restores);
21172 return;
21175 if (restore_lr && !restoring_GPRs_inline)
21177 load_lr_save (0, frame_reg_rtx, info->lr_save_offset + frame_off);
21178 restore_saved_lr (0, exit_func);
21181 /* Restore fpr's if we need to do it without calling a function. */
21182 if (restoring_FPRs_inline)
21183 for (i = 0; i < 64 - info->first_fp_reg_save; i++)
21184 if (save_reg_p (info->first_fp_reg_save + i))
21186 rtx reg = gen_rtx_REG ((TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT
21187 ? DFmode : SFmode),
21188 info->first_fp_reg_save + i);
21189 emit_insn (gen_frame_load (reg, frame_reg_rtx,
21190 info->fp_save_offset + frame_off + 8 * i));
21191 if (DEFAULT_ABI == ABI_V4 || flag_shrink_wrap)
21192 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
21195 /* If we saved cr, restore it here. Just those that were used. */
21196 if (info->cr_save_p)
21197 restore_saved_cr (cr_save_reg, using_mtcr_multiple, exit_func);
21199 /* If this is V.4, unwind the stack pointer after all of the loads
21200 have been done, or set up r11 if we are restoring fp out of line. */
21201 ptr_regno = 1;
21202 if (!restoring_FPRs_inline)
21204 bool lr = (strategy & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR) == 0;
21205 int sel = SAVRES_FPR | (lr ? SAVRES_LR : 0);
21206 ptr_regno = ptr_regno_for_savres (sel);
21209 insn = rs6000_emit_stack_reset (info, frame_reg_rtx, frame_off, ptr_regno);
21210 if (REGNO (frame_reg_rtx) == ptr_regno)
21211 frame_off = 0;
21213 if (insn && restoring_FPRs_inline)
21215 if (cfa_restores)
21217 REG_NOTES (insn) = cfa_restores;
21218 cfa_restores = NULL_RTX;
21220 add_reg_note (insn, REG_CFA_DEF_CFA, sp_reg_rtx);
21221 RTX_FRAME_RELATED_P (insn) = 1;
21224 if (crtl->calls_eh_return)
21226 rtx sa = EH_RETURN_STACKADJ_RTX;
21227 emit_insn (gen_add3_insn (sp_reg_rtx, sp_reg_rtx, sa));
21230 if (!sibcall)
21232 rtvec p;
21233 bool lr = (strategy & REST_NOINLINE_FPRS_DOESNT_RESTORE_LR) == 0;
21234 if (! restoring_FPRs_inline)
21236 p = rtvec_alloc (4 + 64 - info->first_fp_reg_save);
21237 RTVEC_ELT (p, 0) = ret_rtx;
21239 else
21241 if (cfa_restores)
21243 /* We can't hang the cfa_restores off a simple return,
21244 since the shrink-wrap code sometimes uses an existing
21245 return. This means there might be a path from
21246 pre-prologue code to this return, and dwarf2cfi code
21247 wants the eh_frame unwinder state to be the same on
21248 all paths to any point. So we need to emit the
21249 cfa_restores before the return. For -m64 we really
21250 don't need epilogue cfa_restores at all, except for
21251 this irritating dwarf2cfi with shrink-wrap
21252 requirement; The stack red-zone means eh_frame info
21253 from the prologue telling the unwinder to restore
21254 from the stack is perfectly good right to the end of
21255 the function. */
21256 emit_insn (gen_blockage ());
21257 emit_cfa_restores (cfa_restores);
21258 cfa_restores = NULL_RTX;
21260 p = rtvec_alloc (2);
21261 RTVEC_ELT (p, 0) = simple_return_rtx;
21264 RTVEC_ELT (p, 1) = ((restoring_FPRs_inline || !lr)
21265 ? gen_rtx_USE (VOIDmode,
21266 gen_rtx_REG (Pmode, LR_REGNO))
21267 : gen_rtx_CLOBBER (VOIDmode,
21268 gen_rtx_REG (Pmode, LR_REGNO)));
21270 /* If we have to restore more than two FP registers, branch to the
21271 restore function. It will return to our caller. */
21272 if (! restoring_FPRs_inline)
21274 int i;
21275 rtx sym;
21277 if (flag_shrink_wrap)
21278 cfa_restores = add_crlr_cfa_restore (info, cfa_restores);
21280 sym = rs6000_savres_routine_sym (info,
21281 SAVRES_FPR | (lr ? SAVRES_LR : 0));
21282 RTVEC_ELT (p, 2) = gen_rtx_USE (VOIDmode, sym);
21283 RTVEC_ELT (p, 3) = gen_rtx_USE (VOIDmode,
21284 gen_rtx_REG (Pmode,
21285 DEFAULT_ABI == ABI_AIX
21286 ? 1 : 11));
21287 for (i = 0; i < 64 - info->first_fp_reg_save; i++)
21289 rtx reg = gen_rtx_REG (DFmode, info->first_fp_reg_save + i);
21291 RTVEC_ELT (p, i + 4)
21292 = gen_frame_load (reg, sp_reg_rtx, info->fp_save_offset + 8 * i);
21293 if (flag_shrink_wrap)
21294 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg,
21295 cfa_restores);
21299 emit_jump_insn (gen_rtx_PARALLEL (VOIDmode, p));
21302 if (cfa_restores)
21304 if (sibcall)
21305 /* Ensure the cfa_restores are hung off an insn that won't
21306 be reordered above other restores. */
21307 emit_insn (gen_blockage ());
21309 emit_cfa_restores (cfa_restores);
21313 /* Write function epilogue. */
21315 static void
21316 rs6000_output_function_epilogue (FILE *file,
21317 HOST_WIDE_INT size ATTRIBUTE_UNUSED)
21319 #if TARGET_MACHO
21320 macho_branch_islands ();
21321 /* Mach-O doesn't support labels at the end of objects, so if
21322 it looks like we might want one, insert a NOP. */
21324 rtx insn = get_last_insn ();
21325 rtx deleted_debug_label = NULL_RTX;
21326 while (insn
21327 && NOTE_P (insn)
21328 && NOTE_KIND (insn) != NOTE_INSN_DELETED_LABEL)
21330 /* Don't insert a nop for NOTE_INSN_DELETED_DEBUG_LABEL
21331 notes only, instead set their CODE_LABEL_NUMBER to -1,
21332 otherwise there would be code generation differences
21333 in between -g and -g0. */
21334 if (NOTE_P (insn) && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL)
21335 deleted_debug_label = insn;
21336 insn = PREV_INSN (insn);
21338 if (insn
21339 && (LABEL_P (insn)
21340 || (NOTE_P (insn)
21341 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL)))
21342 fputs ("\tnop\n", file);
21343 else if (deleted_debug_label)
21344 for (insn = deleted_debug_label; insn; insn = NEXT_INSN (insn))
21345 if (NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL)
21346 CODE_LABEL_NUMBER (insn) = -1;
21348 #endif
21350 /* Output a traceback table here. See /usr/include/sys/debug.h for info
21351 on its format.
21353 We don't output a traceback table if -finhibit-size-directive was
21354 used. The documentation for -finhibit-size-directive reads
21355 ``don't output a @code{.size} assembler directive, or anything
21356 else that would cause trouble if the function is split in the
21357 middle, and the two halves are placed at locations far apart in
21358 memory.'' The traceback table has this property, since it
21359 includes the offset from the start of the function to the
21360 traceback table itself.
21362 System V.4 Powerpc's (and the embedded ABI derived from it) use a
21363 different traceback table. */
21364 if (DEFAULT_ABI == ABI_AIX && ! flag_inhibit_size_directive
21365 && rs6000_traceback != traceback_none && !cfun->is_thunk)
21367 const char *fname = NULL;
21368 const char *language_string = lang_hooks.name;
21369 int fixed_parms = 0, float_parms = 0, parm_info = 0;
21370 int i;
21371 int optional_tbtab;
21372 rs6000_stack_t *info = rs6000_stack_info ();
21374 if (rs6000_traceback == traceback_full)
21375 optional_tbtab = 1;
21376 else if (rs6000_traceback == traceback_part)
21377 optional_tbtab = 0;
21378 else
21379 optional_tbtab = !optimize_size && !TARGET_ELF;
21381 if (optional_tbtab)
21383 fname = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
21384 while (*fname == '.') /* V.4 encodes . in the name */
21385 fname++;
21387 /* Need label immediately before tbtab, so we can compute
21388 its offset from the function start. */
21389 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LT");
21390 ASM_OUTPUT_LABEL (file, fname);
21393 /* The .tbtab pseudo-op can only be used for the first eight
21394 expressions, since it can't handle the possibly variable
21395 length fields that follow. However, if you omit the optional
21396 fields, the assembler outputs zeros for all optional fields
21397 anyways, giving each variable length field is minimum length
21398 (as defined in sys/debug.h). Thus we can not use the .tbtab
21399 pseudo-op at all. */
21401 /* An all-zero word flags the start of the tbtab, for debuggers
21402 that have to find it by searching forward from the entry
21403 point or from the current pc. */
21404 fputs ("\t.long 0\n", file);
21406 /* Tbtab format type. Use format type 0. */
21407 fputs ("\t.byte 0,", file);
21409 /* Language type. Unfortunately, there does not seem to be any
21410 official way to discover the language being compiled, so we
21411 use language_string.
21412 C is 0. Fortran is 1. Pascal is 2. Ada is 3. C++ is 9.
21413 Java is 13. Objective-C is 14. Objective-C++ isn't assigned
21414 a number, so for now use 9. LTO and Go aren't assigned numbers
21415 either, so for now use 0. */
21416 if (! strcmp (language_string, "GNU C")
21417 || ! strcmp (language_string, "GNU GIMPLE")
21418 || ! strcmp (language_string, "GNU Go"))
21419 i = 0;
21420 else if (! strcmp (language_string, "GNU F77")
21421 || ! strcmp (language_string, "GNU Fortran"))
21422 i = 1;
21423 else if (! strcmp (language_string, "GNU Pascal"))
21424 i = 2;
21425 else if (! strcmp (language_string, "GNU Ada"))
21426 i = 3;
21427 else if (! strcmp (language_string, "GNU C++")
21428 || ! strcmp (language_string, "GNU Objective-C++"))
21429 i = 9;
21430 else if (! strcmp (language_string, "GNU Java"))
21431 i = 13;
21432 else if (! strcmp (language_string, "GNU Objective-C"))
21433 i = 14;
21434 else
21435 gcc_unreachable ();
21436 fprintf (file, "%d,", i);
21438 /* 8 single bit fields: global linkage (not set for C extern linkage,
21439 apparently a PL/I convention?), out-of-line epilogue/prologue, offset
21440 from start of procedure stored in tbtab, internal function, function
21441 has controlled storage, function has no toc, function uses fp,
21442 function logs/aborts fp operations. */
21443 /* Assume that fp operations are used if any fp reg must be saved. */
21444 fprintf (file, "%d,",
21445 (optional_tbtab << 5) | ((info->first_fp_reg_save != 64) << 1));
21447 /* 6 bitfields: function is interrupt handler, name present in
21448 proc table, function calls alloca, on condition directives
21449 (controls stack walks, 3 bits), saves condition reg, saves
21450 link reg. */
21451 /* The `function calls alloca' bit seems to be set whenever reg 31 is
21452 set up as a frame pointer, even when there is no alloca call. */
21453 fprintf (file, "%d,",
21454 ((optional_tbtab << 6)
21455 | ((optional_tbtab & frame_pointer_needed) << 5)
21456 | (info->cr_save_p << 1)
21457 | (info->lr_save_p)));
21459 /* 3 bitfields: saves backchain, fixup code, number of fpr saved
21460 (6 bits). */
21461 fprintf (file, "%d,",
21462 (info->push_p << 7) | (64 - info->first_fp_reg_save));
21464 /* 2 bitfields: spare bits (2 bits), number of gpr saved (6 bits). */
21465 fprintf (file, "%d,", (32 - first_reg_to_save ()));
21467 if (optional_tbtab)
21469 /* Compute the parameter info from the function decl argument
21470 list. */
21471 tree decl;
21472 int next_parm_info_bit = 31;
21474 for (decl = DECL_ARGUMENTS (current_function_decl);
21475 decl; decl = DECL_CHAIN (decl))
21477 rtx parameter = DECL_INCOMING_RTL (decl);
21478 enum machine_mode mode = GET_MODE (parameter);
21480 if (GET_CODE (parameter) == REG)
21482 if (SCALAR_FLOAT_MODE_P (mode))
21484 int bits;
21486 float_parms++;
21488 switch (mode)
21490 case SFmode:
21491 case SDmode:
21492 bits = 0x2;
21493 break;
21495 case DFmode:
21496 case DDmode:
21497 case TFmode:
21498 case TDmode:
21499 bits = 0x3;
21500 break;
21502 default:
21503 gcc_unreachable ();
21506 /* If only one bit will fit, don't or in this entry. */
21507 if (next_parm_info_bit > 0)
21508 parm_info |= (bits << (next_parm_info_bit - 1));
21509 next_parm_info_bit -= 2;
21511 else
21513 fixed_parms += ((GET_MODE_SIZE (mode)
21514 + (UNITS_PER_WORD - 1))
21515 / UNITS_PER_WORD);
21516 next_parm_info_bit -= 1;
21522 /* Number of fixed point parameters. */
21523 /* This is actually the number of words of fixed point parameters; thus
21524 an 8 byte struct counts as 2; and thus the maximum value is 8. */
21525 fprintf (file, "%d,", fixed_parms);
21527 /* 2 bitfields: number of floating point parameters (7 bits), parameters
21528 all on stack. */
21529 /* This is actually the number of fp registers that hold parameters;
21530 and thus the maximum value is 13. */
21531 /* Set parameters on stack bit if parameters are not in their original
21532 registers, regardless of whether they are on the stack? Xlc
21533 seems to set the bit when not optimizing. */
21534 fprintf (file, "%d\n", ((float_parms << 1) | (! optimize)));
21536 if (! optional_tbtab)
21537 return;
21539 /* Optional fields follow. Some are variable length. */
21541 /* Parameter types, left adjusted bit fields: 0 fixed, 10 single float,
21542 11 double float. */
21543 /* There is an entry for each parameter in a register, in the order that
21544 they occur in the parameter list. Any intervening arguments on the
21545 stack are ignored. If the list overflows a long (max possible length
21546 34 bits) then completely leave off all elements that don't fit. */
21547 /* Only emit this long if there was at least one parameter. */
21548 if (fixed_parms || float_parms)
21549 fprintf (file, "\t.long %d\n", parm_info);
21551 /* Offset from start of code to tb table. */
21552 fputs ("\t.long ", file);
21553 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LT");
21554 RS6000_OUTPUT_BASENAME (file, fname);
21555 putc ('-', file);
21556 rs6000_output_function_entry (file, fname);
21557 putc ('\n', file);
21559 /* Interrupt handler mask. */
21560 /* Omit this long, since we never set the interrupt handler bit
21561 above. */
21563 /* Number of CTL (controlled storage) anchors. */
21564 /* Omit this long, since the has_ctl bit is never set above. */
21566 /* Displacement into stack of each CTL anchor. */
21567 /* Omit this list of longs, because there are no CTL anchors. */
21569 /* Length of function name. */
21570 if (*fname == '*')
21571 ++fname;
21572 fprintf (file, "\t.short %d\n", (int) strlen (fname));
21574 /* Function name. */
21575 assemble_string (fname, strlen (fname));
21577 /* Register for alloca automatic storage; this is always reg 31.
21578 Only emit this if the alloca bit was set above. */
21579 if (frame_pointer_needed)
21580 fputs ("\t.byte 31\n", file);
21582 fputs ("\t.align 2\n", file);
21586 /* A C compound statement that outputs the assembler code for a thunk
21587 function, used to implement C++ virtual function calls with
21588 multiple inheritance. The thunk acts as a wrapper around a virtual
21589 function, adjusting the implicit object parameter before handing
21590 control off to the real function.
21592 First, emit code to add the integer DELTA to the location that
21593 contains the incoming first argument. Assume that this argument
21594 contains a pointer, and is the one used to pass the `this' pointer
21595 in C++. This is the incoming argument *before* the function
21596 prologue, e.g. `%o0' on a sparc. The addition must preserve the
21597 values of all other incoming arguments.
21599 After the addition, emit code to jump to FUNCTION, which is a
21600 `FUNCTION_DECL'. This is a direct pure jump, not a call, and does
21601 not touch the return address. Hence returning from FUNCTION will
21602 return to whoever called the current `thunk'.
21604 The effect must be as if FUNCTION had been called directly with the
21605 adjusted first argument. This macro is responsible for emitting
21606 all of the code for a thunk function; output_function_prologue()
21607 and output_function_epilogue() are not invoked.
21609 The THUNK_FNDECL is redundant. (DELTA and FUNCTION have already
21610 been extracted from it.) It might possibly be useful on some
21611 targets, but probably not.
21613 If you do not define this macro, the target-independent code in the
21614 C++ frontend will generate a less efficient heavyweight thunk that
21615 calls FUNCTION instead of jumping to it. The generic approach does
21616 not support varargs. */
21618 static void
21619 rs6000_output_mi_thunk (FILE *file, tree thunk_fndecl ATTRIBUTE_UNUSED,
21620 HOST_WIDE_INT delta, HOST_WIDE_INT vcall_offset,
21621 tree function)
21623 rtx this_rtx, insn, funexp;
21625 reload_completed = 1;
21626 epilogue_completed = 1;
21628 /* Mark the end of the (empty) prologue. */
21629 emit_note (NOTE_INSN_PROLOGUE_END);
21631 /* Find the "this" pointer. If the function returns a structure,
21632 the structure return pointer is in r3. */
21633 if (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function))
21634 this_rtx = gen_rtx_REG (Pmode, 4);
21635 else
21636 this_rtx = gen_rtx_REG (Pmode, 3);
21638 /* Apply the constant offset, if required. */
21639 if (delta)
21640 emit_insn (gen_add3_insn (this_rtx, this_rtx, GEN_INT (delta)));
21642 /* Apply the offset from the vtable, if required. */
21643 if (vcall_offset)
21645 rtx vcall_offset_rtx = GEN_INT (vcall_offset);
21646 rtx tmp = gen_rtx_REG (Pmode, 12);
21648 emit_move_insn (tmp, gen_rtx_MEM (Pmode, this_rtx));
21649 if (((unsigned HOST_WIDE_INT) vcall_offset) + 0x8000 >= 0x10000)
21651 emit_insn (gen_add3_insn (tmp, tmp, vcall_offset_rtx));
21652 emit_move_insn (tmp, gen_rtx_MEM (Pmode, tmp));
21654 else
21656 rtx loc = gen_rtx_PLUS (Pmode, tmp, vcall_offset_rtx);
21658 emit_move_insn (tmp, gen_rtx_MEM (Pmode, loc));
21660 emit_insn (gen_add3_insn (this_rtx, this_rtx, tmp));
21663 /* Generate a tail call to the target function. */
21664 if (!TREE_USED (function))
21666 assemble_external (function);
21667 TREE_USED (function) = 1;
21669 funexp = XEXP (DECL_RTL (function), 0);
21670 funexp = gen_rtx_MEM (FUNCTION_MODE, funexp);
21672 #if TARGET_MACHO
21673 if (MACHOPIC_INDIRECT)
21674 funexp = machopic_indirect_call_target (funexp);
21675 #endif
21677 /* gen_sibcall expects reload to convert scratch pseudo to LR so we must
21678 generate sibcall RTL explicitly. */
21679 insn = emit_call_insn (
21680 gen_rtx_PARALLEL (VOIDmode,
21681 gen_rtvec (4,
21682 gen_rtx_CALL (VOIDmode,
21683 funexp, const0_rtx),
21684 gen_rtx_USE (VOIDmode, const0_rtx),
21685 gen_rtx_USE (VOIDmode,
21686 gen_rtx_REG (SImode,
21687 LR_REGNO)),
21688 simple_return_rtx)));
21689 SIBLING_CALL_P (insn) = 1;
21690 emit_barrier ();
21692 /* Run just enough of rest_of_compilation to get the insns emitted.
21693 There's not really enough bulk here to make other passes such as
21694 instruction scheduling worth while. Note that use_thunk calls
21695 assemble_start_function and assemble_end_function. */
21696 insn = get_insns ();
21697 shorten_branches (insn);
21698 final_start_function (insn, file, 1);
21699 final (insn, file, 1);
21700 final_end_function ();
21702 reload_completed = 0;
21703 epilogue_completed = 0;
21706 /* A quick summary of the various types of 'constant-pool tables'
21707 under PowerPC:
21709 Target Flags Name One table per
21710 AIX (none) AIX TOC object file
21711 AIX -mfull-toc AIX TOC object file
21712 AIX -mminimal-toc AIX minimal TOC translation unit
21713 SVR4/EABI (none) SVR4 SDATA object file
21714 SVR4/EABI -fpic SVR4 pic object file
21715 SVR4/EABI -fPIC SVR4 PIC translation unit
21716 SVR4/EABI -mrelocatable EABI TOC function
21717 SVR4/EABI -maix AIX TOC object file
21718 SVR4/EABI -maix -mminimal-toc
21719 AIX minimal TOC translation unit
21721 Name Reg. Set by entries contains:
21722 made by addrs? fp? sum?
21724 AIX TOC 2 crt0 as Y option option
21725 AIX minimal TOC 30 prolog gcc Y Y option
21726 SVR4 SDATA 13 crt0 gcc N Y N
21727 SVR4 pic 30 prolog ld Y not yet N
21728 SVR4 PIC 30 prolog gcc Y option option
21729 EABI TOC 30 prolog gcc Y option option
21733 /* Hash functions for the hash table. */
21735 static unsigned
21736 rs6000_hash_constant (rtx k)
21738 enum rtx_code code = GET_CODE (k);
21739 enum machine_mode mode = GET_MODE (k);
21740 unsigned result = (code << 3) ^ mode;
21741 const char *format;
21742 int flen, fidx;
21744 format = GET_RTX_FORMAT (code);
21745 flen = strlen (format);
21746 fidx = 0;
21748 switch (code)
21750 case LABEL_REF:
21751 return result * 1231 + (unsigned) INSN_UID (XEXP (k, 0));
21753 case CONST_DOUBLE:
21754 if (mode != VOIDmode)
21755 return real_hash (CONST_DOUBLE_REAL_VALUE (k)) * result;
21756 flen = 2;
21757 break;
21759 case CODE_LABEL:
21760 fidx = 3;
21761 break;
21763 default:
21764 break;
21767 for (; fidx < flen; fidx++)
21768 switch (format[fidx])
21770 case 's':
21772 unsigned i, len;
21773 const char *str = XSTR (k, fidx);
21774 len = strlen (str);
21775 result = result * 613 + len;
21776 for (i = 0; i < len; i++)
21777 result = result * 613 + (unsigned) str[i];
21778 break;
21780 case 'u':
21781 case 'e':
21782 result = result * 1231 + rs6000_hash_constant (XEXP (k, fidx));
21783 break;
21784 case 'i':
21785 case 'n':
21786 result = result * 613 + (unsigned) XINT (k, fidx);
21787 break;
21788 case 'w':
21789 if (sizeof (unsigned) >= sizeof (HOST_WIDE_INT))
21790 result = result * 613 + (unsigned) XWINT (k, fidx);
21791 else
21793 size_t i;
21794 for (i = 0; i < sizeof (HOST_WIDE_INT) / sizeof (unsigned); i++)
21795 result = result * 613 + (unsigned) (XWINT (k, fidx)
21796 >> CHAR_BIT * i);
21798 break;
21799 case '0':
21800 break;
21801 default:
21802 gcc_unreachable ();
21805 return result;
21808 static unsigned
21809 toc_hash_function (const void *hash_entry)
21811 const struct toc_hash_struct *thc =
21812 (const struct toc_hash_struct *) hash_entry;
21813 return rs6000_hash_constant (thc->key) ^ thc->key_mode;
21816 /* Compare H1 and H2 for equivalence. */
21818 static int
21819 toc_hash_eq (const void *h1, const void *h2)
21821 rtx r1 = ((const struct toc_hash_struct *) h1)->key;
21822 rtx r2 = ((const struct toc_hash_struct *) h2)->key;
21824 if (((const struct toc_hash_struct *) h1)->key_mode
21825 != ((const struct toc_hash_struct *) h2)->key_mode)
21826 return 0;
21828 return rtx_equal_p (r1, r2);
21831 /* These are the names given by the C++ front-end to vtables, and
21832 vtable-like objects. Ideally, this logic should not be here;
21833 instead, there should be some programmatic way of inquiring as
21834 to whether or not an object is a vtable. */
21836 #define VTABLE_NAME_P(NAME) \
21837 (strncmp ("_vt.", name, strlen ("_vt.")) == 0 \
21838 || strncmp ("_ZTV", name, strlen ("_ZTV")) == 0 \
21839 || strncmp ("_ZTT", name, strlen ("_ZTT")) == 0 \
21840 || strncmp ("_ZTI", name, strlen ("_ZTI")) == 0 \
21841 || strncmp ("_ZTC", name, strlen ("_ZTC")) == 0)
21843 #ifdef NO_DOLLAR_IN_LABEL
21844 /* Return a GGC-allocated character string translating dollar signs in
21845 input NAME to underscores. Used by XCOFF ASM_OUTPUT_LABELREF. */
21847 const char *
21848 rs6000_xcoff_strip_dollar (const char *name)
21850 char *strip, *p;
21851 const char *q;
21852 size_t len;
21854 q = (const char *) strchr (name, '$');
21856 if (q == 0 || q == name)
21857 return name;
21859 len = strlen (name);
21860 strip = XALLOCAVEC (char, len + 1);
21861 strcpy (strip, name);
21862 p = strip + (q - name);
21863 while (p)
21865 *p = '_';
21866 p = strchr (p + 1, '$');
21869 return ggc_alloc_string (strip, len);
21871 #endif
21873 void
21874 rs6000_output_symbol_ref (FILE *file, rtx x)
21876 /* Currently C++ toc references to vtables can be emitted before it
21877 is decided whether the vtable is public or private. If this is
21878 the case, then the linker will eventually complain that there is
21879 a reference to an unknown section. Thus, for vtables only,
21880 we emit the TOC reference to reference the symbol and not the
21881 section. */
21882 const char *name = XSTR (x, 0);
21884 if (VTABLE_NAME_P (name))
21886 RS6000_OUTPUT_BASENAME (file, name);
21888 else
21889 assemble_name (file, name);
21892 /* Output a TOC entry. We derive the entry name from what is being
21893 written. */
21895 void
21896 output_toc (FILE *file, rtx x, int labelno, enum machine_mode mode)
21898 char buf[256];
21899 const char *name = buf;
21900 rtx base = x;
21901 HOST_WIDE_INT offset = 0;
21903 gcc_assert (!TARGET_NO_TOC);
21905 /* When the linker won't eliminate them, don't output duplicate
21906 TOC entries (this happens on AIX if there is any kind of TOC,
21907 and on SVR4 under -fPIC or -mrelocatable). Don't do this for
21908 CODE_LABELs. */
21909 if (TARGET_TOC && GET_CODE (x) != LABEL_REF)
21911 struct toc_hash_struct *h;
21912 void * * found;
21914 /* Create toc_hash_table. This can't be done at TARGET_OPTION_OVERRIDE
21915 time because GGC is not initialized at that point. */
21916 if (toc_hash_table == NULL)
21917 toc_hash_table = htab_create_ggc (1021, toc_hash_function,
21918 toc_hash_eq, NULL);
21920 h = ggc_alloc_toc_hash_struct ();
21921 h->key = x;
21922 h->key_mode = mode;
21923 h->labelno = labelno;
21925 found = htab_find_slot (toc_hash_table, h, INSERT);
21926 if (*found == NULL)
21927 *found = h;
21928 else /* This is indeed a duplicate.
21929 Set this label equal to that label. */
21931 fputs ("\t.set ", file);
21932 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LC");
21933 fprintf (file, "%d,", labelno);
21934 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LC");
21935 fprintf (file, "%d\n", ((*(const struct toc_hash_struct **)
21936 found)->labelno));
21937 return;
21941 /* If we're going to put a double constant in the TOC, make sure it's
21942 aligned properly when strict alignment is on. */
21943 if (GET_CODE (x) == CONST_DOUBLE
21944 && STRICT_ALIGNMENT
21945 && GET_MODE_BITSIZE (mode) >= 64
21946 && ! (TARGET_NO_FP_IN_TOC && ! TARGET_MINIMAL_TOC)) {
21947 ASM_OUTPUT_ALIGN (file, 3);
21950 (*targetm.asm_out.internal_label) (file, "LC", labelno);
21952 /* Handle FP constants specially. Note that if we have a minimal
21953 TOC, things we put here aren't actually in the TOC, so we can allow
21954 FP constants. */
21955 if (GET_CODE (x) == CONST_DOUBLE &&
21956 (GET_MODE (x) == TFmode || GET_MODE (x) == TDmode))
21958 REAL_VALUE_TYPE rv;
21959 long k[4];
21961 REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
21962 if (DECIMAL_FLOAT_MODE_P (GET_MODE (x)))
21963 REAL_VALUE_TO_TARGET_DECIMAL128 (rv, k);
21964 else
21965 REAL_VALUE_TO_TARGET_LONG_DOUBLE (rv, k);
21967 if (TARGET_64BIT)
21969 if (TARGET_MINIMAL_TOC)
21970 fputs (DOUBLE_INT_ASM_OP, file);
21971 else
21972 fprintf (file, "\t.tc FT_%lx_%lx_%lx_%lx[TC],",
21973 k[0] & 0xffffffff, k[1] & 0xffffffff,
21974 k[2] & 0xffffffff, k[3] & 0xffffffff);
21975 fprintf (file, "0x%lx%08lx,0x%lx%08lx\n",
21976 k[0] & 0xffffffff, k[1] & 0xffffffff,
21977 k[2] & 0xffffffff, k[3] & 0xffffffff);
21978 return;
21980 else
21982 if (TARGET_MINIMAL_TOC)
21983 fputs ("\t.long ", file);
21984 else
21985 fprintf (file, "\t.tc FT_%lx_%lx_%lx_%lx[TC],",
21986 k[0] & 0xffffffff, k[1] & 0xffffffff,
21987 k[2] & 0xffffffff, k[3] & 0xffffffff);
21988 fprintf (file, "0x%lx,0x%lx,0x%lx,0x%lx\n",
21989 k[0] & 0xffffffff, k[1] & 0xffffffff,
21990 k[2] & 0xffffffff, k[3] & 0xffffffff);
21991 return;
21994 else if (GET_CODE (x) == CONST_DOUBLE &&
21995 (GET_MODE (x) == DFmode || GET_MODE (x) == DDmode))
21997 REAL_VALUE_TYPE rv;
21998 long k[2];
22000 REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
22002 if (DECIMAL_FLOAT_MODE_P (GET_MODE (x)))
22003 REAL_VALUE_TO_TARGET_DECIMAL64 (rv, k);
22004 else
22005 REAL_VALUE_TO_TARGET_DOUBLE (rv, k);
22007 if (TARGET_64BIT)
22009 if (TARGET_MINIMAL_TOC)
22010 fputs (DOUBLE_INT_ASM_OP, file);
22011 else
22012 fprintf (file, "\t.tc FD_%lx_%lx[TC],",
22013 k[0] & 0xffffffff, k[1] & 0xffffffff);
22014 fprintf (file, "0x%lx%08lx\n",
22015 k[0] & 0xffffffff, k[1] & 0xffffffff);
22016 return;
22018 else
22020 if (TARGET_MINIMAL_TOC)
22021 fputs ("\t.long ", file);
22022 else
22023 fprintf (file, "\t.tc FD_%lx_%lx[TC],",
22024 k[0] & 0xffffffff, k[1] & 0xffffffff);
22025 fprintf (file, "0x%lx,0x%lx\n",
22026 k[0] & 0xffffffff, k[1] & 0xffffffff);
22027 return;
22030 else if (GET_CODE (x) == CONST_DOUBLE &&
22031 (GET_MODE (x) == SFmode || GET_MODE (x) == SDmode))
22033 REAL_VALUE_TYPE rv;
22034 long l;
22036 REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
22037 if (DECIMAL_FLOAT_MODE_P (GET_MODE (x)))
22038 REAL_VALUE_TO_TARGET_DECIMAL32 (rv, l);
22039 else
22040 REAL_VALUE_TO_TARGET_SINGLE (rv, l);
22042 if (TARGET_64BIT)
22044 if (TARGET_MINIMAL_TOC)
22045 fputs (DOUBLE_INT_ASM_OP, file);
22046 else
22047 fprintf (file, "\t.tc FS_%lx[TC],", l & 0xffffffff);
22048 fprintf (file, "0x%lx00000000\n", l & 0xffffffff);
22049 return;
22051 else
22053 if (TARGET_MINIMAL_TOC)
22054 fputs ("\t.long ", file);
22055 else
22056 fprintf (file, "\t.tc FS_%lx[TC],", l & 0xffffffff);
22057 fprintf (file, "0x%lx\n", l & 0xffffffff);
22058 return;
22061 else if (GET_MODE (x) == VOIDmode
22062 && (GET_CODE (x) == CONST_INT || GET_CODE (x) == CONST_DOUBLE))
22064 unsigned HOST_WIDE_INT low;
22065 HOST_WIDE_INT high;
22067 if (GET_CODE (x) == CONST_DOUBLE)
22069 low = CONST_DOUBLE_LOW (x);
22070 high = CONST_DOUBLE_HIGH (x);
22072 else
22073 #if HOST_BITS_PER_WIDE_INT == 32
22075 low = INTVAL (x);
22076 high = (low & 0x80000000) ? ~0 : 0;
22078 #else
22080 low = INTVAL (x) & 0xffffffff;
22081 high = (HOST_WIDE_INT) INTVAL (x) >> 32;
22083 #endif
22085 /* TOC entries are always Pmode-sized, but since this
22086 is a bigendian machine then if we're putting smaller
22087 integer constants in the TOC we have to pad them.
22088 (This is still a win over putting the constants in
22089 a separate constant pool, because then we'd have
22090 to have both a TOC entry _and_ the actual constant.)
22092 For a 32-bit target, CONST_INT values are loaded and shifted
22093 entirely within `low' and can be stored in one TOC entry. */
22095 /* It would be easy to make this work, but it doesn't now. */
22096 gcc_assert (!TARGET_64BIT || POINTER_SIZE >= GET_MODE_BITSIZE (mode));
22098 if (POINTER_SIZE > GET_MODE_BITSIZE (mode))
22100 #if HOST_BITS_PER_WIDE_INT == 32
22101 lshift_double (low, high, POINTER_SIZE - GET_MODE_BITSIZE (mode),
22102 POINTER_SIZE, &low, &high, 0);
22103 #else
22104 low |= high << 32;
22105 low <<= POINTER_SIZE - GET_MODE_BITSIZE (mode);
22106 high = (HOST_WIDE_INT) low >> 32;
22107 low &= 0xffffffff;
22108 #endif
22111 if (TARGET_64BIT)
22113 if (TARGET_MINIMAL_TOC)
22114 fputs (DOUBLE_INT_ASM_OP, file);
22115 else
22116 fprintf (file, "\t.tc ID_%lx_%lx[TC],",
22117 (long) high & 0xffffffff, (long) low & 0xffffffff);
22118 fprintf (file, "0x%lx%08lx\n",
22119 (long) high & 0xffffffff, (long) low & 0xffffffff);
22120 return;
22122 else
22124 if (POINTER_SIZE < GET_MODE_BITSIZE (mode))
22126 if (TARGET_MINIMAL_TOC)
22127 fputs ("\t.long ", file);
22128 else
22129 fprintf (file, "\t.tc ID_%lx_%lx[TC],",
22130 (long) high & 0xffffffff, (long) low & 0xffffffff);
22131 fprintf (file, "0x%lx,0x%lx\n",
22132 (long) high & 0xffffffff, (long) low & 0xffffffff);
22134 else
22136 if (TARGET_MINIMAL_TOC)
22137 fputs ("\t.long ", file);
22138 else
22139 fprintf (file, "\t.tc IS_%lx[TC],", (long) low & 0xffffffff);
22140 fprintf (file, "0x%lx\n", (long) low & 0xffffffff);
22142 return;
22146 if (GET_CODE (x) == CONST)
22148 gcc_assert (GET_CODE (XEXP (x, 0)) == PLUS
22149 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT);
22151 base = XEXP (XEXP (x, 0), 0);
22152 offset = INTVAL (XEXP (XEXP (x, 0), 1));
22155 switch (GET_CODE (base))
22157 case SYMBOL_REF:
22158 name = XSTR (base, 0);
22159 break;
22161 case LABEL_REF:
22162 ASM_GENERATE_INTERNAL_LABEL (buf, "L",
22163 CODE_LABEL_NUMBER (XEXP (base, 0)));
22164 break;
22166 case CODE_LABEL:
22167 ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (base));
22168 break;
22170 default:
22171 gcc_unreachable ();
22174 if (TARGET_MINIMAL_TOC)
22175 fputs (TARGET_32BIT ? "\t.long " : DOUBLE_INT_ASM_OP, file);
22176 else
22178 fputs ("\t.tc ", file);
22179 RS6000_OUTPUT_BASENAME (file, name);
22181 if (offset < 0)
22182 fprintf (file, ".N" HOST_WIDE_INT_PRINT_UNSIGNED, - offset);
22183 else if (offset)
22184 fprintf (file, ".P" HOST_WIDE_INT_PRINT_UNSIGNED, offset);
22186 /* Mark large TOC symbols on AIX with [TE] so they are mapped
22187 after other TOC symbols, reducing overflow of small TOC access
22188 to [TC] symbols. */
22189 fputs (TARGET_XCOFF && TARGET_CMODEL != CMODEL_SMALL
22190 ? "[TE]," : "[TC],", file);
22193 /* Currently C++ toc references to vtables can be emitted before it
22194 is decided whether the vtable is public or private. If this is
22195 the case, then the linker will eventually complain that there is
22196 a TOC reference to an unknown section. Thus, for vtables only,
22197 we emit the TOC reference to reference the symbol and not the
22198 section. */
22199 if (VTABLE_NAME_P (name))
22201 RS6000_OUTPUT_BASENAME (file, name);
22202 if (offset < 0)
22203 fprintf (file, HOST_WIDE_INT_PRINT_DEC, offset);
22204 else if (offset > 0)
22205 fprintf (file, "+" HOST_WIDE_INT_PRINT_DEC, offset);
22207 else
22208 output_addr_const (file, x);
22209 putc ('\n', file);
22212 /* Output an assembler pseudo-op to write an ASCII string of N characters
22213 starting at P to FILE.
22215 On the RS/6000, we have to do this using the .byte operation and
22216 write out special characters outside the quoted string.
22217 Also, the assembler is broken; very long strings are truncated,
22218 so we must artificially break them up early. */
22220 void
22221 output_ascii (FILE *file, const char *p, int n)
22223 char c;
22224 int i, count_string;
22225 const char *for_string = "\t.byte \"";
22226 const char *for_decimal = "\t.byte ";
22227 const char *to_close = NULL;
22229 count_string = 0;
22230 for (i = 0; i < n; i++)
22232 c = *p++;
22233 if (c >= ' ' && c < 0177)
22235 if (for_string)
22236 fputs (for_string, file);
22237 putc (c, file);
22239 /* Write two quotes to get one. */
22240 if (c == '"')
22242 putc (c, file);
22243 ++count_string;
22246 for_string = NULL;
22247 for_decimal = "\"\n\t.byte ";
22248 to_close = "\"\n";
22249 ++count_string;
22251 if (count_string >= 512)
22253 fputs (to_close, file);
22255 for_string = "\t.byte \"";
22256 for_decimal = "\t.byte ";
22257 to_close = NULL;
22258 count_string = 0;
22261 else
22263 if (for_decimal)
22264 fputs (for_decimal, file);
22265 fprintf (file, "%d", c);
22267 for_string = "\n\t.byte \"";
22268 for_decimal = ", ";
22269 to_close = "\n";
22270 count_string = 0;
22274 /* Now close the string if we have written one. Then end the line. */
22275 if (to_close)
22276 fputs (to_close, file);
22279 /* Generate a unique section name for FILENAME for a section type
22280 represented by SECTION_DESC. Output goes into BUF.
22282 SECTION_DESC can be any string, as long as it is different for each
22283 possible section type.
22285 We name the section in the same manner as xlc. The name begins with an
22286 underscore followed by the filename (after stripping any leading directory
22287 names) with the last period replaced by the string SECTION_DESC. If
22288 FILENAME does not contain a period, SECTION_DESC is appended to the end of
22289 the name. */
22291 void
22292 rs6000_gen_section_name (char **buf, const char *filename,
22293 const char *section_desc)
22295 const char *q, *after_last_slash, *last_period = 0;
22296 char *p;
22297 int len;
22299 after_last_slash = filename;
22300 for (q = filename; *q; q++)
22302 if (*q == '/')
22303 after_last_slash = q + 1;
22304 else if (*q == '.')
22305 last_period = q;
22308 len = strlen (after_last_slash) + strlen (section_desc) + 2;
22309 *buf = (char *) xmalloc (len);
22311 p = *buf;
22312 *p++ = '_';
22314 for (q = after_last_slash; *q; q++)
22316 if (q == last_period)
22318 strcpy (p, section_desc);
22319 p += strlen (section_desc);
22320 break;
22323 else if (ISALNUM (*q))
22324 *p++ = *q;
22327 if (last_period == 0)
22328 strcpy (p, section_desc);
22329 else
22330 *p = '\0';
22333 /* Emit profile function. */
22335 void
22336 output_profile_hook (int labelno ATTRIBUTE_UNUSED)
22338 /* Non-standard profiling for kernels, which just saves LR then calls
22339 _mcount without worrying about arg saves. The idea is to change
22340 the function prologue as little as possible as it isn't easy to
22341 account for arg save/restore code added just for _mcount. */
22342 if (TARGET_PROFILE_KERNEL)
22343 return;
22345 if (DEFAULT_ABI == ABI_AIX)
22347 #ifndef NO_PROFILE_COUNTERS
22348 # define NO_PROFILE_COUNTERS 0
22349 #endif
22350 if (NO_PROFILE_COUNTERS)
22351 emit_library_call (init_one_libfunc (RS6000_MCOUNT),
22352 LCT_NORMAL, VOIDmode, 0);
22353 else
22355 char buf[30];
22356 const char *label_name;
22357 rtx fun;
22359 ASM_GENERATE_INTERNAL_LABEL (buf, "LP", labelno);
22360 label_name = ggc_strdup ((*targetm.strip_name_encoding) (buf));
22361 fun = gen_rtx_SYMBOL_REF (Pmode, label_name);
22363 emit_library_call (init_one_libfunc (RS6000_MCOUNT),
22364 LCT_NORMAL, VOIDmode, 1, fun, Pmode);
22367 else if (DEFAULT_ABI == ABI_DARWIN)
22369 const char *mcount_name = RS6000_MCOUNT;
22370 int caller_addr_regno = LR_REGNO;
22372 /* Be conservative and always set this, at least for now. */
22373 crtl->uses_pic_offset_table = 1;
22375 #if TARGET_MACHO
22376 /* For PIC code, set up a stub and collect the caller's address
22377 from r0, which is where the prologue puts it. */
22378 if (MACHOPIC_INDIRECT
22379 && crtl->uses_pic_offset_table)
22380 caller_addr_regno = 0;
22381 #endif
22382 emit_library_call (gen_rtx_SYMBOL_REF (Pmode, mcount_name),
22383 LCT_NORMAL, VOIDmode, 1,
22384 gen_rtx_REG (Pmode, caller_addr_regno), Pmode);
22388 /* Write function profiler code. */
22390 void
22391 output_function_profiler (FILE *file, int labelno)
22393 char buf[100];
22395 switch (DEFAULT_ABI)
22397 default:
22398 gcc_unreachable ();
22400 case ABI_V4:
22401 if (!TARGET_32BIT)
22403 warning (0, "no profiling of 64-bit code for this ABI");
22404 return;
22406 ASM_GENERATE_INTERNAL_LABEL (buf, "LP", labelno);
22407 fprintf (file, "\tmflr %s\n", reg_names[0]);
22408 if (NO_PROFILE_COUNTERS)
22410 asm_fprintf (file, "\tstw %s,4(%s)\n",
22411 reg_names[0], reg_names[1]);
22413 else if (TARGET_SECURE_PLT && flag_pic)
22415 if (TARGET_LINK_STACK)
22417 char name[32];
22418 get_ppc476_thunk_name (name);
22419 asm_fprintf (file, "\tbl %s\n", name);
22421 else
22422 asm_fprintf (file, "\tbcl 20,31,1f\n1:\n");
22423 asm_fprintf (file, "\tstw %s,4(%s)\n",
22424 reg_names[0], reg_names[1]);
22425 asm_fprintf (file, "\tmflr %s\n", reg_names[12]);
22426 asm_fprintf (file, "\taddis %s,%s,",
22427 reg_names[12], reg_names[12]);
22428 assemble_name (file, buf);
22429 asm_fprintf (file, "-1b@ha\n\tla %s,", reg_names[0]);
22430 assemble_name (file, buf);
22431 asm_fprintf (file, "-1b@l(%s)\n", reg_names[12]);
22433 else if (flag_pic == 1)
22435 fputs ("\tbl _GLOBAL_OFFSET_TABLE_@local-4\n", file);
22436 asm_fprintf (file, "\tstw %s,4(%s)\n",
22437 reg_names[0], reg_names[1]);
22438 asm_fprintf (file, "\tmflr %s\n", reg_names[12]);
22439 asm_fprintf (file, "\tlwz %s,", reg_names[0]);
22440 assemble_name (file, buf);
22441 asm_fprintf (file, "@got(%s)\n", reg_names[12]);
22443 else if (flag_pic > 1)
22445 asm_fprintf (file, "\tstw %s,4(%s)\n",
22446 reg_names[0], reg_names[1]);
22447 /* Now, we need to get the address of the label. */
22448 if (TARGET_LINK_STACK)
22450 char name[32];
22451 get_ppc476_thunk_name (name);
22452 asm_fprintf (file, "\tbl %s\n\tb 1f\n\t.long ", name);
22453 assemble_name (file, buf);
22454 fputs ("-.\n1:", file);
22455 asm_fprintf (file, "\tmflr %s\n", reg_names[11]);
22456 asm_fprintf (file, "\taddi %s,%s,4\n",
22457 reg_names[11], reg_names[11]);
22459 else
22461 fputs ("\tbcl 20,31,1f\n\t.long ", file);
22462 assemble_name (file, buf);
22463 fputs ("-.\n1:", file);
22464 asm_fprintf (file, "\tmflr %s\n", reg_names[11]);
22466 asm_fprintf (file, "\tlwz %s,0(%s)\n",
22467 reg_names[0], reg_names[11]);
22468 asm_fprintf (file, "\tadd %s,%s,%s\n",
22469 reg_names[0], reg_names[0], reg_names[11]);
22471 else
22473 asm_fprintf (file, "\tlis %s,", reg_names[12]);
22474 assemble_name (file, buf);
22475 fputs ("@ha\n", file);
22476 asm_fprintf (file, "\tstw %s,4(%s)\n",
22477 reg_names[0], reg_names[1]);
22478 asm_fprintf (file, "\tla %s,", reg_names[0]);
22479 assemble_name (file, buf);
22480 asm_fprintf (file, "@l(%s)\n", reg_names[12]);
22483 /* ABI_V4 saves the static chain reg with ASM_OUTPUT_REG_PUSH. */
22484 fprintf (file, "\tbl %s%s\n",
22485 RS6000_MCOUNT, flag_pic ? "@plt" : "");
22486 break;
22488 case ABI_AIX:
22489 case ABI_DARWIN:
22490 if (!TARGET_PROFILE_KERNEL)
22492 /* Don't do anything, done in output_profile_hook (). */
22494 else
22496 gcc_assert (!TARGET_32BIT);
22498 asm_fprintf (file, "\tmflr %s\n", reg_names[0]);
22499 asm_fprintf (file, "\tstd %s,16(%s)\n", reg_names[0], reg_names[1]);
22501 if (cfun->static_chain_decl != NULL)
22503 asm_fprintf (file, "\tstd %s,24(%s)\n",
22504 reg_names[STATIC_CHAIN_REGNUM], reg_names[1]);
22505 fprintf (file, "\tbl %s\n", RS6000_MCOUNT);
22506 asm_fprintf (file, "\tld %s,24(%s)\n",
22507 reg_names[STATIC_CHAIN_REGNUM], reg_names[1]);
22509 else
22510 fprintf (file, "\tbl %s\n", RS6000_MCOUNT);
22512 break;
22518 /* The following variable value is the last issued insn. */
22520 static rtx last_scheduled_insn;
22522 /* The following variable helps to balance issuing of load and
22523 store instructions */
22525 static int load_store_pendulum;
22527 /* Power4 load update and store update instructions are cracked into a
22528 load or store and an integer insn which are executed in the same cycle.
22529 Branches have their own dispatch slot which does not count against the
22530 GCC issue rate, but it changes the program flow so there are no other
22531 instructions to issue in this cycle. */
22533 static int
22534 rs6000_variable_issue_1 (rtx insn, int more)
22536 last_scheduled_insn = insn;
22537 if (GET_CODE (PATTERN (insn)) == USE
22538 || GET_CODE (PATTERN (insn)) == CLOBBER)
22540 cached_can_issue_more = more;
22541 return cached_can_issue_more;
22544 if (insn_terminates_group_p (insn, current_group))
22546 cached_can_issue_more = 0;
22547 return cached_can_issue_more;
22550 /* If no reservation, but reach here */
22551 if (recog_memoized (insn) < 0)
22552 return more;
22554 if (rs6000_sched_groups)
22556 if (is_microcoded_insn (insn))
22557 cached_can_issue_more = 0;
22558 else if (is_cracked_insn (insn))
22559 cached_can_issue_more = more > 2 ? more - 2 : 0;
22560 else
22561 cached_can_issue_more = more - 1;
22563 return cached_can_issue_more;
22566 if (rs6000_cpu_attr == CPU_CELL && is_nonpipeline_insn (insn))
22567 return 0;
22569 cached_can_issue_more = more - 1;
22570 return cached_can_issue_more;
22573 static int
22574 rs6000_variable_issue (FILE *stream, int verbose, rtx insn, int more)
22576 int r = rs6000_variable_issue_1 (insn, more);
22577 if (verbose)
22578 fprintf (stream, "// rs6000_variable_issue (more = %d) = %d\n", more, r);
22579 return r;
22582 /* Adjust the cost of a scheduling dependency. Return the new cost of
22583 a dependency LINK or INSN on DEP_INSN. COST is the current cost. */
22585 static int
22586 rs6000_adjust_cost (rtx insn, rtx link, rtx dep_insn, int cost)
22588 enum attr_type attr_type;
22590 if (! recog_memoized (insn))
22591 return 0;
22593 switch (REG_NOTE_KIND (link))
22595 case REG_DEP_TRUE:
22597 /* Data dependency; DEP_INSN writes a register that INSN reads
22598 some cycles later. */
22600 /* Separate a load from a narrower, dependent store. */
22601 if (rs6000_sched_groups
22602 && GET_CODE (PATTERN (insn)) == SET
22603 && GET_CODE (PATTERN (dep_insn)) == SET
22604 && GET_CODE (XEXP (PATTERN (insn), 1)) == MEM
22605 && GET_CODE (XEXP (PATTERN (dep_insn), 0)) == MEM
22606 && (GET_MODE_SIZE (GET_MODE (XEXP (PATTERN (insn), 1)))
22607 > GET_MODE_SIZE (GET_MODE (XEXP (PATTERN (dep_insn), 0)))))
22608 return cost + 14;
22610 attr_type = get_attr_type (insn);
22612 switch (attr_type)
22614 case TYPE_JMPREG:
22615 /* Tell the first scheduling pass about the latency between
22616 a mtctr and bctr (and mtlr and br/blr). The first
22617 scheduling pass will not know about this latency since
22618 the mtctr instruction, which has the latency associated
22619 to it, will be generated by reload. */
22620 return 4;
22621 case TYPE_BRANCH:
22622 /* Leave some extra cycles between a compare and its
22623 dependent branch, to inhibit expensive mispredicts. */
22624 if ((rs6000_cpu_attr == CPU_PPC603
22625 || rs6000_cpu_attr == CPU_PPC604
22626 || rs6000_cpu_attr == CPU_PPC604E
22627 || rs6000_cpu_attr == CPU_PPC620
22628 || rs6000_cpu_attr == CPU_PPC630
22629 || rs6000_cpu_attr == CPU_PPC750
22630 || rs6000_cpu_attr == CPU_PPC7400
22631 || rs6000_cpu_attr == CPU_PPC7450
22632 || rs6000_cpu_attr == CPU_PPCE5500
22633 || rs6000_cpu_attr == CPU_PPCE6500
22634 || rs6000_cpu_attr == CPU_POWER4
22635 || rs6000_cpu_attr == CPU_POWER5
22636 || rs6000_cpu_attr == CPU_POWER7
22637 || rs6000_cpu_attr == CPU_CELL)
22638 && recog_memoized (dep_insn)
22639 && (INSN_CODE (dep_insn) >= 0))
22641 switch (get_attr_type (dep_insn))
22643 case TYPE_CMP:
22644 case TYPE_COMPARE:
22645 case TYPE_DELAYED_COMPARE:
22646 case TYPE_IMUL_COMPARE:
22647 case TYPE_LMUL_COMPARE:
22648 case TYPE_FPCOMPARE:
22649 case TYPE_CR_LOGICAL:
22650 case TYPE_DELAYED_CR:
22651 return cost + 2;
22652 default:
22653 break;
22655 break;
22657 case TYPE_STORE:
22658 case TYPE_STORE_U:
22659 case TYPE_STORE_UX:
22660 case TYPE_FPSTORE:
22661 case TYPE_FPSTORE_U:
22662 case TYPE_FPSTORE_UX:
22663 if ((rs6000_cpu == PROCESSOR_POWER6)
22664 && recog_memoized (dep_insn)
22665 && (INSN_CODE (dep_insn) >= 0))
22668 if (GET_CODE (PATTERN (insn)) != SET)
22669 /* If this happens, we have to extend this to schedule
22670 optimally. Return default for now. */
22671 return cost;
22673 /* Adjust the cost for the case where the value written
22674 by a fixed point operation is used as the address
22675 gen value on a store. */
22676 switch (get_attr_type (dep_insn))
22678 case TYPE_LOAD:
22679 case TYPE_LOAD_U:
22680 case TYPE_LOAD_UX:
22681 case TYPE_CNTLZ:
22683 if (! store_data_bypass_p (dep_insn, insn))
22684 return 4;
22685 break;
22687 case TYPE_LOAD_EXT:
22688 case TYPE_LOAD_EXT_U:
22689 case TYPE_LOAD_EXT_UX:
22690 case TYPE_VAR_SHIFT_ROTATE:
22691 case TYPE_VAR_DELAYED_COMPARE:
22693 if (! store_data_bypass_p (dep_insn, insn))
22694 return 6;
22695 break;
22697 case TYPE_INTEGER:
22698 case TYPE_COMPARE:
22699 case TYPE_FAST_COMPARE:
22700 case TYPE_EXTS:
22701 case TYPE_SHIFT:
22702 case TYPE_INSERT_WORD:
22703 case TYPE_INSERT_DWORD:
22704 case TYPE_FPLOAD_U:
22705 case TYPE_FPLOAD_UX:
22706 case TYPE_STORE_U:
22707 case TYPE_STORE_UX:
22708 case TYPE_FPSTORE_U:
22709 case TYPE_FPSTORE_UX:
22711 if (! store_data_bypass_p (dep_insn, insn))
22712 return 3;
22713 break;
22715 case TYPE_IMUL:
22716 case TYPE_IMUL2:
22717 case TYPE_IMUL3:
22718 case TYPE_LMUL:
22719 case TYPE_IMUL_COMPARE:
22720 case TYPE_LMUL_COMPARE:
22722 if (! store_data_bypass_p (dep_insn, insn))
22723 return 17;
22724 break;
22726 case TYPE_IDIV:
22728 if (! store_data_bypass_p (dep_insn, insn))
22729 return 45;
22730 break;
22732 case TYPE_LDIV:
22734 if (! store_data_bypass_p (dep_insn, insn))
22735 return 57;
22736 break;
22738 default:
22739 break;
22742 break;
22744 case TYPE_LOAD:
22745 case TYPE_LOAD_U:
22746 case TYPE_LOAD_UX:
22747 case TYPE_LOAD_EXT:
22748 case TYPE_LOAD_EXT_U:
22749 case TYPE_LOAD_EXT_UX:
22750 if ((rs6000_cpu == PROCESSOR_POWER6)
22751 && recog_memoized (dep_insn)
22752 && (INSN_CODE (dep_insn) >= 0))
22755 /* Adjust the cost for the case where the value written
22756 by a fixed point instruction is used within the address
22757 gen portion of a subsequent load(u)(x) */
22758 switch (get_attr_type (dep_insn))
22760 case TYPE_LOAD:
22761 case TYPE_LOAD_U:
22762 case TYPE_LOAD_UX:
22763 case TYPE_CNTLZ:
22765 if (set_to_load_agen (dep_insn, insn))
22766 return 4;
22767 break;
22769 case TYPE_LOAD_EXT:
22770 case TYPE_LOAD_EXT_U:
22771 case TYPE_LOAD_EXT_UX:
22772 case TYPE_VAR_SHIFT_ROTATE:
22773 case TYPE_VAR_DELAYED_COMPARE:
22775 if (set_to_load_agen (dep_insn, insn))
22776 return 6;
22777 break;
22779 case TYPE_INTEGER:
22780 case TYPE_COMPARE:
22781 case TYPE_FAST_COMPARE:
22782 case TYPE_EXTS:
22783 case TYPE_SHIFT:
22784 case TYPE_INSERT_WORD:
22785 case TYPE_INSERT_DWORD:
22786 case TYPE_FPLOAD_U:
22787 case TYPE_FPLOAD_UX:
22788 case TYPE_STORE_U:
22789 case TYPE_STORE_UX:
22790 case TYPE_FPSTORE_U:
22791 case TYPE_FPSTORE_UX:
22793 if (set_to_load_agen (dep_insn, insn))
22794 return 3;
22795 break;
22797 case TYPE_IMUL:
22798 case TYPE_IMUL2:
22799 case TYPE_IMUL3:
22800 case TYPE_LMUL:
22801 case TYPE_IMUL_COMPARE:
22802 case TYPE_LMUL_COMPARE:
22804 if (set_to_load_agen (dep_insn, insn))
22805 return 17;
22806 break;
22808 case TYPE_IDIV:
22810 if (set_to_load_agen (dep_insn, insn))
22811 return 45;
22812 break;
22814 case TYPE_LDIV:
22816 if (set_to_load_agen (dep_insn, insn))
22817 return 57;
22818 break;
22820 default:
22821 break;
22824 break;
22826 case TYPE_FPLOAD:
22827 if ((rs6000_cpu == PROCESSOR_POWER6)
22828 && recog_memoized (dep_insn)
22829 && (INSN_CODE (dep_insn) >= 0)
22830 && (get_attr_type (dep_insn) == TYPE_MFFGPR))
22831 return 2;
22833 default:
22834 break;
22837 /* Fall out to return default cost. */
22839 break;
22841 case REG_DEP_OUTPUT:
22842 /* Output dependency; DEP_INSN writes a register that INSN writes some
22843 cycles later. */
22844 if ((rs6000_cpu == PROCESSOR_POWER6)
22845 && recog_memoized (dep_insn)
22846 && (INSN_CODE (dep_insn) >= 0))
22848 attr_type = get_attr_type (insn);
22850 switch (attr_type)
22852 case TYPE_FP:
22853 if (get_attr_type (dep_insn) == TYPE_FP)
22854 return 1;
22855 break;
22856 case TYPE_FPLOAD:
22857 if (get_attr_type (dep_insn) == TYPE_MFFGPR)
22858 return 2;
22859 break;
22860 default:
22861 break;
22864 case REG_DEP_ANTI:
22865 /* Anti dependency; DEP_INSN reads a register that INSN writes some
22866 cycles later. */
22867 return 0;
22869 default:
22870 gcc_unreachable ();
22873 return cost;
22876 /* Debug version of rs6000_adjust_cost. */
22878 static int
22879 rs6000_debug_adjust_cost (rtx insn, rtx link, rtx dep_insn, int cost)
22881 int ret = rs6000_adjust_cost (insn, link, dep_insn, cost);
22883 if (ret != cost)
22885 const char *dep;
22887 switch (REG_NOTE_KIND (link))
22889 default: dep = "unknown depencency"; break;
22890 case REG_DEP_TRUE: dep = "data dependency"; break;
22891 case REG_DEP_OUTPUT: dep = "output dependency"; break;
22892 case REG_DEP_ANTI: dep = "anti depencency"; break;
22895 fprintf (stderr,
22896 "\nrs6000_adjust_cost, final cost = %d, orig cost = %d, "
22897 "%s, insn:\n", ret, cost, dep);
22899 debug_rtx (insn);
22902 return ret;
22905 /* The function returns a true if INSN is microcoded.
22906 Return false otherwise. */
22908 static bool
22909 is_microcoded_insn (rtx insn)
22911 if (!insn || !NONDEBUG_INSN_P (insn)
22912 || GET_CODE (PATTERN (insn)) == USE
22913 || GET_CODE (PATTERN (insn)) == CLOBBER)
22914 return false;
22916 if (rs6000_cpu_attr == CPU_CELL)
22917 return get_attr_cell_micro (insn) == CELL_MICRO_ALWAYS;
22919 if (rs6000_sched_groups)
22921 enum attr_type type = get_attr_type (insn);
22922 if (type == TYPE_LOAD_EXT_U
22923 || type == TYPE_LOAD_EXT_UX
22924 || type == TYPE_LOAD_UX
22925 || type == TYPE_STORE_UX
22926 || type == TYPE_MFCR)
22927 return true;
22930 return false;
22933 /* The function returns true if INSN is cracked into 2 instructions
22934 by the processor (and therefore occupies 2 issue slots). */
22936 static bool
22937 is_cracked_insn (rtx insn)
22939 if (!insn || !NONDEBUG_INSN_P (insn)
22940 || GET_CODE (PATTERN (insn)) == USE
22941 || GET_CODE (PATTERN (insn)) == CLOBBER)
22942 return false;
22944 if (rs6000_sched_groups)
22946 enum attr_type type = get_attr_type (insn);
22947 if (type == TYPE_LOAD_U || type == TYPE_STORE_U
22948 || type == TYPE_FPLOAD_U || type == TYPE_FPSTORE_U
22949 || type == TYPE_FPLOAD_UX || type == TYPE_FPSTORE_UX
22950 || type == TYPE_LOAD_EXT || type == TYPE_DELAYED_CR
22951 || type == TYPE_COMPARE || type == TYPE_DELAYED_COMPARE
22952 || type == TYPE_IMUL_COMPARE || type == TYPE_LMUL_COMPARE
22953 || type == TYPE_IDIV || type == TYPE_LDIV
22954 || type == TYPE_INSERT_WORD)
22955 return true;
22958 return false;
22961 /* The function returns true if INSN can be issued only from
22962 the branch slot. */
22964 static bool
22965 is_branch_slot_insn (rtx insn)
22967 if (!insn || !NONDEBUG_INSN_P (insn)
22968 || GET_CODE (PATTERN (insn)) == USE
22969 || GET_CODE (PATTERN (insn)) == CLOBBER)
22970 return false;
22972 if (rs6000_sched_groups)
22974 enum attr_type type = get_attr_type (insn);
22975 if (type == TYPE_BRANCH || type == TYPE_JMPREG)
22976 return true;
22977 return false;
22980 return false;
22983 /* The function returns true if out_inst sets a value that is
22984 used in the address generation computation of in_insn */
22985 static bool
22986 set_to_load_agen (rtx out_insn, rtx in_insn)
22988 rtx out_set, in_set;
22990 /* For performance reasons, only handle the simple case where
22991 both loads are a single_set. */
22992 out_set = single_set (out_insn);
22993 if (out_set)
22995 in_set = single_set (in_insn);
22996 if (in_set)
22997 return reg_mentioned_p (SET_DEST (out_set), SET_SRC (in_set));
23000 return false;
23003 /* Try to determine base/offset/size parts of the given MEM.
23004 Return true if successful, false if all the values couldn't
23005 be determined.
23007 This function only looks for REG or REG+CONST address forms.
23008 REG+REG address form will return false. */
23010 static bool
23011 get_memref_parts (rtx mem, rtx *base, HOST_WIDE_INT *offset,
23012 HOST_WIDE_INT *size)
23014 rtx addr_rtx;
23015 if MEM_SIZE_KNOWN_P (mem)
23016 *size = MEM_SIZE (mem);
23017 else
23018 return false;
23020 if (GET_CODE (XEXP (mem, 0)) == PRE_MODIFY)
23021 addr_rtx = XEXP (XEXP (mem, 0), 1);
23022 else
23023 addr_rtx = (XEXP (mem, 0));
23025 if (GET_CODE (addr_rtx) == REG)
23027 *base = addr_rtx;
23028 *offset = 0;
23030 else if (GET_CODE (addr_rtx) == PLUS
23031 && CONST_INT_P (XEXP (addr_rtx, 1)))
23033 *base = XEXP (addr_rtx, 0);
23034 *offset = INTVAL (XEXP (addr_rtx, 1));
23036 else
23037 return false;
23039 return true;
23042 /* The function returns true if the target storage location of
23043 mem1 is adjacent to the target storage location of mem2 */
23044 /* Return 1 if memory locations are adjacent. */
23046 static bool
23047 adjacent_mem_locations (rtx mem1, rtx mem2)
23049 rtx reg1, reg2;
23050 HOST_WIDE_INT off1, size1, off2, size2;
23052 if (get_memref_parts (mem1, &reg1, &off1, &size1)
23053 && get_memref_parts (mem2, &reg2, &off2, &size2))
23054 return ((REGNO (reg1) == REGNO (reg2))
23055 && ((off1 + size1 == off2)
23056 || (off2 + size2 == off1)));
23058 return false;
23061 /* This function returns true if it can be determined that the two MEM
23062 locations overlap by at least 1 byte based on base reg/offset/size. */
23064 static bool
23065 mem_locations_overlap (rtx mem1, rtx mem2)
23067 rtx reg1, reg2;
23068 HOST_WIDE_INT off1, size1, off2, size2;
23070 if (get_memref_parts (mem1, &reg1, &off1, &size1)
23071 && get_memref_parts (mem2, &reg2, &off2, &size2))
23072 return ((REGNO (reg1) == REGNO (reg2))
23073 && (((off1 <= off2) && (off1 + size1 > off2))
23074 || ((off2 <= off1) && (off2 + size2 > off1))));
23076 return false;
23079 /* A C statement (sans semicolon) to update the integer scheduling
23080 priority INSN_PRIORITY (INSN). Increase the priority to execute the
23081 INSN earlier, reduce the priority to execute INSN later. Do not
23082 define this macro if you do not need to adjust the scheduling
23083 priorities of insns. */
23085 static int
23086 rs6000_adjust_priority (rtx insn ATTRIBUTE_UNUSED, int priority)
23088 rtx load_mem, str_mem;
23089 /* On machines (like the 750) which have asymmetric integer units,
23090 where one integer unit can do multiply and divides and the other
23091 can't, reduce the priority of multiply/divide so it is scheduled
23092 before other integer operations. */
23094 #if 0
23095 if (! INSN_P (insn))
23096 return priority;
23098 if (GET_CODE (PATTERN (insn)) == USE)
23099 return priority;
23101 switch (rs6000_cpu_attr) {
23102 case CPU_PPC750:
23103 switch (get_attr_type (insn))
23105 default:
23106 break;
23108 case TYPE_IMUL:
23109 case TYPE_IDIV:
23110 fprintf (stderr, "priority was %#x (%d) before adjustment\n",
23111 priority, priority);
23112 if (priority >= 0 && priority < 0x01000000)
23113 priority >>= 3;
23114 break;
23117 #endif
23119 if (insn_must_be_first_in_group (insn)
23120 && reload_completed
23121 && current_sched_info->sched_max_insns_priority
23122 && rs6000_sched_restricted_insns_priority)
23125 /* Prioritize insns that can be dispatched only in the first
23126 dispatch slot. */
23127 if (rs6000_sched_restricted_insns_priority == 1)
23128 /* Attach highest priority to insn. This means that in
23129 haifa-sched.c:ready_sort(), dispatch-slot restriction considerations
23130 precede 'priority' (critical path) considerations. */
23131 return current_sched_info->sched_max_insns_priority;
23132 else if (rs6000_sched_restricted_insns_priority == 2)
23133 /* Increase priority of insn by a minimal amount. This means that in
23134 haifa-sched.c:ready_sort(), only 'priority' (critical path)
23135 considerations precede dispatch-slot restriction considerations. */
23136 return (priority + 1);
23139 if (rs6000_cpu == PROCESSOR_POWER6
23140 && ((load_store_pendulum == -2 && is_load_insn (insn, &load_mem))
23141 || (load_store_pendulum == 2 && is_store_insn (insn, &str_mem))))
23142 /* Attach highest priority to insn if the scheduler has just issued two
23143 stores and this instruction is a load, or two loads and this instruction
23144 is a store. Power6 wants loads and stores scheduled alternately
23145 when possible */
23146 return current_sched_info->sched_max_insns_priority;
23148 return priority;
23151 /* Return true if the instruction is nonpipelined on the Cell. */
23152 static bool
23153 is_nonpipeline_insn (rtx insn)
23155 enum attr_type type;
23156 if (!insn || !NONDEBUG_INSN_P (insn)
23157 || GET_CODE (PATTERN (insn)) == USE
23158 || GET_CODE (PATTERN (insn)) == CLOBBER)
23159 return false;
23161 type = get_attr_type (insn);
23162 if (type == TYPE_IMUL
23163 || type == TYPE_IMUL2
23164 || type == TYPE_IMUL3
23165 || type == TYPE_LMUL
23166 || type == TYPE_IDIV
23167 || type == TYPE_LDIV
23168 || type == TYPE_SDIV
23169 || type == TYPE_DDIV
23170 || type == TYPE_SSQRT
23171 || type == TYPE_DSQRT
23172 || type == TYPE_MFCR
23173 || type == TYPE_MFCRF
23174 || type == TYPE_MFJMPR)
23176 return true;
23178 return false;
23182 /* Return how many instructions the machine can issue per cycle. */
23184 static int
23185 rs6000_issue_rate (void)
23187 /* Unless scheduling for register pressure, use issue rate of 1 for
23188 first scheduling pass to decrease degradation. */
23189 if (!reload_completed && !flag_sched_pressure)
23190 return 1;
23192 switch (rs6000_cpu_attr) {
23193 case CPU_RS64A:
23194 case CPU_PPC601: /* ? */
23195 case CPU_PPC7450:
23196 return 3;
23197 case CPU_PPC440:
23198 case CPU_PPC603:
23199 case CPU_PPC750:
23200 case CPU_PPC7400:
23201 case CPU_PPC8540:
23202 case CPU_PPC8548:
23203 case CPU_CELL:
23204 case CPU_PPCE300C2:
23205 case CPU_PPCE300C3:
23206 case CPU_PPCE500MC:
23207 case CPU_PPCE500MC64:
23208 case CPU_PPCE5500:
23209 case CPU_PPCE6500:
23210 case CPU_TITAN:
23211 return 2;
23212 case CPU_PPC476:
23213 case CPU_PPC604:
23214 case CPU_PPC604E:
23215 case CPU_PPC620:
23216 case CPU_PPC630:
23217 return 4;
23218 case CPU_POWER4:
23219 case CPU_POWER5:
23220 case CPU_POWER6:
23221 case CPU_POWER7:
23222 return 5;
23223 default:
23224 return 1;
23228 /* Return how many instructions to look ahead for better insn
23229 scheduling. */
23231 static int
23232 rs6000_use_sched_lookahead (void)
23234 switch (rs6000_cpu_attr)
23236 case CPU_PPC8540:
23237 case CPU_PPC8548:
23238 return 4;
23240 case CPU_CELL:
23241 return (reload_completed ? 8 : 0);
23243 default:
23244 return 0;
23248 /* We are choosing insn from the ready queue. Return nonzero if INSN can be chosen. */
23249 static int
23250 rs6000_use_sched_lookahead_guard (rtx insn)
23252 if (rs6000_cpu_attr != CPU_CELL)
23253 return 1;
23255 if (insn == NULL_RTX || !INSN_P (insn))
23256 abort ();
23258 if (!reload_completed
23259 || is_nonpipeline_insn (insn)
23260 || is_microcoded_insn (insn))
23261 return 0;
23263 return 1;
23266 /* Determine if PAT refers to memory. If so, set MEM_REF to the MEM rtx
23267 and return true. */
23269 static bool
23270 find_mem_ref (rtx pat, rtx *mem_ref)
23272 const char * fmt;
23273 int i, j;
23275 /* stack_tie does not produce any real memory traffic. */
23276 if (tie_operand (pat, VOIDmode))
23277 return false;
23279 if (GET_CODE (pat) == MEM)
23281 *mem_ref = pat;
23282 return true;
23285 /* Recursively process the pattern. */
23286 fmt = GET_RTX_FORMAT (GET_CODE (pat));
23288 for (i = GET_RTX_LENGTH (GET_CODE (pat)) - 1; i >= 0; i--)
23290 if (fmt[i] == 'e')
23292 if (find_mem_ref (XEXP (pat, i), mem_ref))
23293 return true;
23295 else if (fmt[i] == 'E')
23296 for (j = XVECLEN (pat, i) - 1; j >= 0; j--)
23298 if (find_mem_ref (XVECEXP (pat, i, j), mem_ref))
23299 return true;
23303 return false;
23306 /* Determine if PAT is a PATTERN of a load insn. */
23308 static bool
23309 is_load_insn1 (rtx pat, rtx *load_mem)
23311 if (!pat || pat == NULL_RTX)
23312 return false;
23314 if (GET_CODE (pat) == SET)
23315 return find_mem_ref (SET_SRC (pat), load_mem);
23317 if (GET_CODE (pat) == PARALLEL)
23319 int i;
23321 for (i = 0; i < XVECLEN (pat, 0); i++)
23322 if (is_load_insn1 (XVECEXP (pat, 0, i), load_mem))
23323 return true;
23326 return false;
23329 /* Determine if INSN loads from memory. */
23331 static bool
23332 is_load_insn (rtx insn, rtx *load_mem)
23334 if (!insn || !INSN_P (insn))
23335 return false;
23337 if (GET_CODE (insn) == CALL_INSN)
23338 return false;
23340 return is_load_insn1 (PATTERN (insn), load_mem);
23343 /* Determine if PAT is a PATTERN of a store insn. */
23345 static bool
23346 is_store_insn1 (rtx pat, rtx *str_mem)
23348 if (!pat || pat == NULL_RTX)
23349 return false;
23351 if (GET_CODE (pat) == SET)
23352 return find_mem_ref (SET_DEST (pat), str_mem);
23354 if (GET_CODE (pat) == PARALLEL)
23356 int i;
23358 for (i = 0; i < XVECLEN (pat, 0); i++)
23359 if (is_store_insn1 (XVECEXP (pat, 0, i), str_mem))
23360 return true;
23363 return false;
23366 /* Determine if INSN stores to memory. */
23368 static bool
23369 is_store_insn (rtx insn, rtx *str_mem)
23371 if (!insn || !INSN_P (insn))
23372 return false;
23374 return is_store_insn1 (PATTERN (insn), str_mem);
23377 /* Returns whether the dependence between INSN and NEXT is considered
23378 costly by the given target. */
23380 static bool
23381 rs6000_is_costly_dependence (dep_t dep, int cost, int distance)
23383 rtx insn;
23384 rtx next;
23385 rtx load_mem, str_mem;
23387 /* If the flag is not enabled - no dependence is considered costly;
23388 allow all dependent insns in the same group.
23389 This is the most aggressive option. */
23390 if (rs6000_sched_costly_dep == no_dep_costly)
23391 return false;
23393 /* If the flag is set to 1 - a dependence is always considered costly;
23394 do not allow dependent instructions in the same group.
23395 This is the most conservative option. */
23396 if (rs6000_sched_costly_dep == all_deps_costly)
23397 return true;
23399 insn = DEP_PRO (dep);
23400 next = DEP_CON (dep);
23402 if (rs6000_sched_costly_dep == store_to_load_dep_costly
23403 && is_load_insn (next, &load_mem)
23404 && is_store_insn (insn, &str_mem))
23405 /* Prevent load after store in the same group. */
23406 return true;
23408 if (rs6000_sched_costly_dep == true_store_to_load_dep_costly
23409 && is_load_insn (next, &load_mem)
23410 && is_store_insn (insn, &str_mem)
23411 && DEP_TYPE (dep) == REG_DEP_TRUE
23412 && mem_locations_overlap(str_mem, load_mem))
23413 /* Prevent load after store in the same group if it is a true
23414 dependence. */
23415 return true;
23417 /* The flag is set to X; dependences with latency >= X are considered costly,
23418 and will not be scheduled in the same group. */
23419 if (rs6000_sched_costly_dep <= max_dep_latency
23420 && ((cost - distance) >= (int)rs6000_sched_costly_dep))
23421 return true;
23423 return false;
23426 /* Return the next insn after INSN that is found before TAIL is reached,
23427 skipping any "non-active" insns - insns that will not actually occupy
23428 an issue slot. Return NULL_RTX if such an insn is not found. */
23430 static rtx
23431 get_next_active_insn (rtx insn, rtx tail)
23433 if (insn == NULL_RTX || insn == tail)
23434 return NULL_RTX;
23436 while (1)
23438 insn = NEXT_INSN (insn);
23439 if (insn == NULL_RTX || insn == tail)
23440 return NULL_RTX;
23442 if (CALL_P (insn)
23443 || JUMP_P (insn)
23444 || (NONJUMP_INSN_P (insn)
23445 && GET_CODE (PATTERN (insn)) != USE
23446 && GET_CODE (PATTERN (insn)) != CLOBBER
23447 && INSN_CODE (insn) != CODE_FOR_stack_tie))
23448 break;
23450 return insn;
23453 /* We are about to begin issuing insns for this clock cycle. */
23455 static int
23456 rs6000_sched_reorder (FILE *dump ATTRIBUTE_UNUSED, int sched_verbose,
23457 rtx *ready ATTRIBUTE_UNUSED,
23458 int *pn_ready ATTRIBUTE_UNUSED,
23459 int clock_var ATTRIBUTE_UNUSED)
23461 int n_ready = *pn_ready;
23463 if (sched_verbose)
23464 fprintf (dump, "// rs6000_sched_reorder :\n");
23466 /* Reorder the ready list, if the second to last ready insn
23467 is a nonepipeline insn. */
23468 if (rs6000_cpu_attr == CPU_CELL && n_ready > 1)
23470 if (is_nonpipeline_insn (ready[n_ready - 1])
23471 && (recog_memoized (ready[n_ready - 2]) > 0))
23472 /* Simply swap first two insns. */
23474 rtx tmp = ready[n_ready - 1];
23475 ready[n_ready - 1] = ready[n_ready - 2];
23476 ready[n_ready - 2] = tmp;
23480 if (rs6000_cpu == PROCESSOR_POWER6)
23481 load_store_pendulum = 0;
23483 return rs6000_issue_rate ();
23486 /* Like rs6000_sched_reorder, but called after issuing each insn. */
23488 static int
23489 rs6000_sched_reorder2 (FILE *dump, int sched_verbose, rtx *ready,
23490 int *pn_ready, int clock_var ATTRIBUTE_UNUSED)
23492 if (sched_verbose)
23493 fprintf (dump, "// rs6000_sched_reorder2 :\n");
23495 /* For Power6, we need to handle some special cases to try and keep the
23496 store queue from overflowing and triggering expensive flushes.
23498 This code monitors how load and store instructions are being issued
23499 and skews the ready list one way or the other to increase the likelihood
23500 that a desired instruction is issued at the proper time.
23502 A couple of things are done. First, we maintain a "load_store_pendulum"
23503 to track the current state of load/store issue.
23505 - If the pendulum is at zero, then no loads or stores have been
23506 issued in the current cycle so we do nothing.
23508 - If the pendulum is 1, then a single load has been issued in this
23509 cycle and we attempt to locate another load in the ready list to
23510 issue with it.
23512 - If the pendulum is -2, then two stores have already been
23513 issued in this cycle, so we increase the priority of the first load
23514 in the ready list to increase it's likelihood of being chosen first
23515 in the next cycle.
23517 - If the pendulum is -1, then a single store has been issued in this
23518 cycle and we attempt to locate another store in the ready list to
23519 issue with it, preferring a store to an adjacent memory location to
23520 facilitate store pairing in the store queue.
23522 - If the pendulum is 2, then two loads have already been
23523 issued in this cycle, so we increase the priority of the first store
23524 in the ready list to increase it's likelihood of being chosen first
23525 in the next cycle.
23527 - If the pendulum < -2 or > 2, then do nothing.
23529 Note: This code covers the most common scenarios. There exist non
23530 load/store instructions which make use of the LSU and which
23531 would need to be accounted for to strictly model the behavior
23532 of the machine. Those instructions are currently unaccounted
23533 for to help minimize compile time overhead of this code.
23535 if (rs6000_cpu == PROCESSOR_POWER6 && last_scheduled_insn)
23537 int pos;
23538 int i;
23539 rtx tmp, load_mem, str_mem;
23541 if (is_store_insn (last_scheduled_insn, &str_mem))
23542 /* Issuing a store, swing the load_store_pendulum to the left */
23543 load_store_pendulum--;
23544 else if (is_load_insn (last_scheduled_insn, &load_mem))
23545 /* Issuing a load, swing the load_store_pendulum to the right */
23546 load_store_pendulum++;
23547 else
23548 return cached_can_issue_more;
23550 /* If the pendulum is balanced, or there is only one instruction on
23551 the ready list, then all is well, so return. */
23552 if ((load_store_pendulum == 0) || (*pn_ready <= 1))
23553 return cached_can_issue_more;
23555 if (load_store_pendulum == 1)
23557 /* A load has been issued in this cycle. Scan the ready list
23558 for another load to issue with it */
23559 pos = *pn_ready-1;
23561 while (pos >= 0)
23563 if (is_load_insn (ready[pos], &load_mem))
23565 /* Found a load. Move it to the head of the ready list,
23566 and adjust it's priority so that it is more likely to
23567 stay there */
23568 tmp = ready[pos];
23569 for (i=pos; i<*pn_ready-1; i++)
23570 ready[i] = ready[i + 1];
23571 ready[*pn_ready-1] = tmp;
23573 if (!sel_sched_p () && INSN_PRIORITY_KNOWN (tmp))
23574 INSN_PRIORITY (tmp)++;
23575 break;
23577 pos--;
23580 else if (load_store_pendulum == -2)
23582 /* Two stores have been issued in this cycle. Increase the
23583 priority of the first load in the ready list to favor it for
23584 issuing in the next cycle. */
23585 pos = *pn_ready-1;
23587 while (pos >= 0)
23589 if (is_load_insn (ready[pos], &load_mem)
23590 && !sel_sched_p ()
23591 && INSN_PRIORITY_KNOWN (ready[pos]))
23593 INSN_PRIORITY (ready[pos])++;
23595 /* Adjust the pendulum to account for the fact that a load
23596 was found and increased in priority. This is to prevent
23597 increasing the priority of multiple loads */
23598 load_store_pendulum--;
23600 break;
23602 pos--;
23605 else if (load_store_pendulum == -1)
23607 /* A store has been issued in this cycle. Scan the ready list for
23608 another store to issue with it, preferring a store to an adjacent
23609 memory location */
23610 int first_store_pos = -1;
23612 pos = *pn_ready-1;
23614 while (pos >= 0)
23616 if (is_store_insn (ready[pos], &str_mem))
23618 rtx str_mem2;
23619 /* Maintain the index of the first store found on the
23620 list */
23621 if (first_store_pos == -1)
23622 first_store_pos = pos;
23624 if (is_store_insn (last_scheduled_insn, &str_mem2)
23625 && adjacent_mem_locations (str_mem, str_mem2))
23627 /* Found an adjacent store. Move it to the head of the
23628 ready list, and adjust it's priority so that it is
23629 more likely to stay there */
23630 tmp = ready[pos];
23631 for (i=pos; i<*pn_ready-1; i++)
23632 ready[i] = ready[i + 1];
23633 ready[*pn_ready-1] = tmp;
23635 if (!sel_sched_p () && INSN_PRIORITY_KNOWN (tmp))
23636 INSN_PRIORITY (tmp)++;
23638 first_store_pos = -1;
23640 break;
23643 pos--;
23646 if (first_store_pos >= 0)
23648 /* An adjacent store wasn't found, but a non-adjacent store was,
23649 so move the non-adjacent store to the front of the ready
23650 list, and adjust its priority so that it is more likely to
23651 stay there. */
23652 tmp = ready[first_store_pos];
23653 for (i=first_store_pos; i<*pn_ready-1; i++)
23654 ready[i] = ready[i + 1];
23655 ready[*pn_ready-1] = tmp;
23656 if (!sel_sched_p () && INSN_PRIORITY_KNOWN (tmp))
23657 INSN_PRIORITY (tmp)++;
23660 else if (load_store_pendulum == 2)
23662 /* Two loads have been issued in this cycle. Increase the priority
23663 of the first store in the ready list to favor it for issuing in
23664 the next cycle. */
23665 pos = *pn_ready-1;
23667 while (pos >= 0)
23669 if (is_store_insn (ready[pos], &str_mem)
23670 && !sel_sched_p ()
23671 && INSN_PRIORITY_KNOWN (ready[pos]))
23673 INSN_PRIORITY (ready[pos])++;
23675 /* Adjust the pendulum to account for the fact that a store
23676 was found and increased in priority. This is to prevent
23677 increasing the priority of multiple stores */
23678 load_store_pendulum++;
23680 break;
23682 pos--;
23687 return cached_can_issue_more;
23690 /* Return whether the presence of INSN causes a dispatch group termination
23691 of group WHICH_GROUP.
23693 If WHICH_GROUP == current_group, this function will return true if INSN
23694 causes the termination of the current group (i.e, the dispatch group to
23695 which INSN belongs). This means that INSN will be the last insn in the
23696 group it belongs to.
23698 If WHICH_GROUP == previous_group, this function will return true if INSN
23699 causes the termination of the previous group (i.e, the dispatch group that
23700 precedes the group to which INSN belongs). This means that INSN will be
23701 the first insn in the group it belongs to). */
23703 static bool
23704 insn_terminates_group_p (rtx insn, enum group_termination which_group)
23706 bool first, last;
23708 if (! insn)
23709 return false;
23711 first = insn_must_be_first_in_group (insn);
23712 last = insn_must_be_last_in_group (insn);
23714 if (first && last)
23715 return true;
23717 if (which_group == current_group)
23718 return last;
23719 else if (which_group == previous_group)
23720 return first;
23722 return false;
23726 static bool
23727 insn_must_be_first_in_group (rtx insn)
23729 enum attr_type type;
23731 if (!insn
23732 || GET_CODE (insn) == NOTE
23733 || DEBUG_INSN_P (insn)
23734 || GET_CODE (PATTERN (insn)) == USE
23735 || GET_CODE (PATTERN (insn)) == CLOBBER)
23736 return false;
23738 switch (rs6000_cpu)
23740 case PROCESSOR_POWER5:
23741 if (is_cracked_insn (insn))
23742 return true;
23743 case PROCESSOR_POWER4:
23744 if (is_microcoded_insn (insn))
23745 return true;
23747 if (!rs6000_sched_groups)
23748 return false;
23750 type = get_attr_type (insn);
23752 switch (type)
23754 case TYPE_MFCR:
23755 case TYPE_MFCRF:
23756 case TYPE_MTCR:
23757 case TYPE_DELAYED_CR:
23758 case TYPE_CR_LOGICAL:
23759 case TYPE_MTJMPR:
23760 case TYPE_MFJMPR:
23761 case TYPE_IDIV:
23762 case TYPE_LDIV:
23763 case TYPE_LOAD_L:
23764 case TYPE_STORE_C:
23765 case TYPE_ISYNC:
23766 case TYPE_SYNC:
23767 return true;
23768 default:
23769 break;
23771 break;
23772 case PROCESSOR_POWER6:
23773 type = get_attr_type (insn);
23775 switch (type)
23777 case TYPE_INSERT_DWORD:
23778 case TYPE_EXTS:
23779 case TYPE_CNTLZ:
23780 case TYPE_SHIFT:
23781 case TYPE_VAR_SHIFT_ROTATE:
23782 case TYPE_TRAP:
23783 case TYPE_IMUL:
23784 case TYPE_IMUL2:
23785 case TYPE_IMUL3:
23786 case TYPE_LMUL:
23787 case TYPE_IDIV:
23788 case TYPE_INSERT_WORD:
23789 case TYPE_DELAYED_COMPARE:
23790 case TYPE_IMUL_COMPARE:
23791 case TYPE_LMUL_COMPARE:
23792 case TYPE_FPCOMPARE:
23793 case TYPE_MFCR:
23794 case TYPE_MTCR:
23795 case TYPE_MFJMPR:
23796 case TYPE_MTJMPR:
23797 case TYPE_ISYNC:
23798 case TYPE_SYNC:
23799 case TYPE_LOAD_L:
23800 case TYPE_STORE_C:
23801 case TYPE_LOAD_U:
23802 case TYPE_LOAD_UX:
23803 case TYPE_LOAD_EXT_UX:
23804 case TYPE_STORE_U:
23805 case TYPE_STORE_UX:
23806 case TYPE_FPLOAD_U:
23807 case TYPE_FPLOAD_UX:
23808 case TYPE_FPSTORE_U:
23809 case TYPE_FPSTORE_UX:
23810 return true;
23811 default:
23812 break;
23814 break;
23815 case PROCESSOR_POWER7:
23816 type = get_attr_type (insn);
23818 switch (type)
23820 case TYPE_CR_LOGICAL:
23821 case TYPE_MFCR:
23822 case TYPE_MFCRF:
23823 case TYPE_MTCR:
23824 case TYPE_IDIV:
23825 case TYPE_LDIV:
23826 case TYPE_COMPARE:
23827 case TYPE_DELAYED_COMPARE:
23828 case TYPE_VAR_DELAYED_COMPARE:
23829 case TYPE_ISYNC:
23830 case TYPE_LOAD_L:
23831 case TYPE_STORE_C:
23832 case TYPE_LOAD_U:
23833 case TYPE_LOAD_UX:
23834 case TYPE_LOAD_EXT:
23835 case TYPE_LOAD_EXT_U:
23836 case TYPE_LOAD_EXT_UX:
23837 case TYPE_STORE_U:
23838 case TYPE_STORE_UX:
23839 case TYPE_FPLOAD_U:
23840 case TYPE_FPLOAD_UX:
23841 case TYPE_FPSTORE_U:
23842 case TYPE_FPSTORE_UX:
23843 case TYPE_MFJMPR:
23844 case TYPE_MTJMPR:
23845 return true;
23846 default:
23847 break;
23849 break;
23850 default:
23851 break;
23854 return false;
23857 static bool
23858 insn_must_be_last_in_group (rtx insn)
23860 enum attr_type type;
23862 if (!insn
23863 || GET_CODE (insn) == NOTE
23864 || DEBUG_INSN_P (insn)
23865 || GET_CODE (PATTERN (insn)) == USE
23866 || GET_CODE (PATTERN (insn)) == CLOBBER)
23867 return false;
23869 switch (rs6000_cpu) {
23870 case PROCESSOR_POWER4:
23871 case PROCESSOR_POWER5:
23872 if (is_microcoded_insn (insn))
23873 return true;
23875 if (is_branch_slot_insn (insn))
23876 return true;
23878 break;
23879 case PROCESSOR_POWER6:
23880 type = get_attr_type (insn);
23882 switch (type)
23884 case TYPE_EXTS:
23885 case TYPE_CNTLZ:
23886 case TYPE_SHIFT:
23887 case TYPE_VAR_SHIFT_ROTATE:
23888 case TYPE_TRAP:
23889 case TYPE_IMUL:
23890 case TYPE_IMUL2:
23891 case TYPE_IMUL3:
23892 case TYPE_LMUL:
23893 case TYPE_IDIV:
23894 case TYPE_DELAYED_COMPARE:
23895 case TYPE_IMUL_COMPARE:
23896 case TYPE_LMUL_COMPARE:
23897 case TYPE_FPCOMPARE:
23898 case TYPE_MFCR:
23899 case TYPE_MTCR:
23900 case TYPE_MFJMPR:
23901 case TYPE_MTJMPR:
23902 case TYPE_ISYNC:
23903 case TYPE_SYNC:
23904 case TYPE_LOAD_L:
23905 case TYPE_STORE_C:
23906 return true;
23907 default:
23908 break;
23910 break;
23911 case PROCESSOR_POWER7:
23912 type = get_attr_type (insn);
23914 switch (type)
23916 case TYPE_ISYNC:
23917 case TYPE_SYNC:
23918 case TYPE_LOAD_L:
23919 case TYPE_STORE_C:
23920 case TYPE_LOAD_EXT_U:
23921 case TYPE_LOAD_EXT_UX:
23922 case TYPE_STORE_UX:
23923 return true;
23924 default:
23925 break;
23927 break;
23928 default:
23929 break;
23932 return false;
23935 /* Return true if it is recommended to keep NEXT_INSN "far" (in a separate
23936 dispatch group) from the insns in GROUP_INSNS. Return false otherwise. */
23938 static bool
23939 is_costly_group (rtx *group_insns, rtx next_insn)
23941 int i;
23942 int issue_rate = rs6000_issue_rate ();
23944 for (i = 0; i < issue_rate; i++)
23946 sd_iterator_def sd_it;
23947 dep_t dep;
23948 rtx insn = group_insns[i];
23950 if (!insn)
23951 continue;
23953 FOR_EACH_DEP (insn, SD_LIST_RES_FORW, sd_it, dep)
23955 rtx next = DEP_CON (dep);
23957 if (next == next_insn
23958 && rs6000_is_costly_dependence (dep, dep_cost (dep), 0))
23959 return true;
23963 return false;
23966 /* Utility of the function redefine_groups.
23967 Check if it is too costly to schedule NEXT_INSN together with GROUP_INSNS
23968 in the same dispatch group. If so, insert nops before NEXT_INSN, in order
23969 to keep it "far" (in a separate group) from GROUP_INSNS, following
23970 one of the following schemes, depending on the value of the flag
23971 -minsert_sched_nops = X:
23972 (1) X == sched_finish_regroup_exact: insert exactly as many nops as needed
23973 in order to force NEXT_INSN into a separate group.
23974 (2) X < sched_finish_regroup_exact: insert exactly X nops.
23975 GROUP_END, CAN_ISSUE_MORE and GROUP_COUNT record the state after nop
23976 insertion (has a group just ended, how many vacant issue slots remain in the
23977 last group, and how many dispatch groups were encountered so far). */
23979 static int
23980 force_new_group (int sched_verbose, FILE *dump, rtx *group_insns,
23981 rtx next_insn, bool *group_end, int can_issue_more,
23982 int *group_count)
23984 rtx nop;
23985 bool force;
23986 int issue_rate = rs6000_issue_rate ();
23987 bool end = *group_end;
23988 int i;
23990 if (next_insn == NULL_RTX || DEBUG_INSN_P (next_insn))
23991 return can_issue_more;
23993 if (rs6000_sched_insert_nops > sched_finish_regroup_exact)
23994 return can_issue_more;
23996 force = is_costly_group (group_insns, next_insn);
23997 if (!force)
23998 return can_issue_more;
24000 if (sched_verbose > 6)
24001 fprintf (dump,"force: group count = %d, can_issue_more = %d\n",
24002 *group_count ,can_issue_more);
24004 if (rs6000_sched_insert_nops == sched_finish_regroup_exact)
24006 if (*group_end)
24007 can_issue_more = 0;
24009 /* Since only a branch can be issued in the last issue_slot, it is
24010 sufficient to insert 'can_issue_more - 1' nops if next_insn is not
24011 a branch. If next_insn is a branch, we insert 'can_issue_more' nops;
24012 in this case the last nop will start a new group and the branch
24013 will be forced to the new group. */
24014 if (can_issue_more && !is_branch_slot_insn (next_insn))
24015 can_issue_more--;
24017 /* Power6 and Power7 have special group ending nop. */
24018 if (rs6000_cpu_attr == CPU_POWER6 || rs6000_cpu_attr == CPU_POWER7)
24020 nop = gen_group_ending_nop ();
24021 emit_insn_before (nop, next_insn);
24022 can_issue_more = 0;
24024 else
24025 while (can_issue_more > 0)
24027 nop = gen_nop ();
24028 emit_insn_before (nop, next_insn);
24029 can_issue_more--;
24032 *group_end = true;
24033 return 0;
24036 if (rs6000_sched_insert_nops < sched_finish_regroup_exact)
24038 int n_nops = rs6000_sched_insert_nops;
24040 /* Nops can't be issued from the branch slot, so the effective
24041 issue_rate for nops is 'issue_rate - 1'. */
24042 if (can_issue_more == 0)
24043 can_issue_more = issue_rate;
24044 can_issue_more--;
24045 if (can_issue_more == 0)
24047 can_issue_more = issue_rate - 1;
24048 (*group_count)++;
24049 end = true;
24050 for (i = 0; i < issue_rate; i++)
24052 group_insns[i] = 0;
24056 while (n_nops > 0)
24058 nop = gen_nop ();
24059 emit_insn_before (nop, next_insn);
24060 if (can_issue_more == issue_rate - 1) /* new group begins */
24061 end = false;
24062 can_issue_more--;
24063 if (can_issue_more == 0)
24065 can_issue_more = issue_rate - 1;
24066 (*group_count)++;
24067 end = true;
24068 for (i = 0; i < issue_rate; i++)
24070 group_insns[i] = 0;
24073 n_nops--;
24076 /* Scale back relative to 'issue_rate' (instead of 'issue_rate - 1'). */
24077 can_issue_more++;
24079 /* Is next_insn going to start a new group? */
24080 *group_end
24081 = (end
24082 || (can_issue_more == 1 && !is_branch_slot_insn (next_insn))
24083 || (can_issue_more <= 2 && is_cracked_insn (next_insn))
24084 || (can_issue_more < issue_rate &&
24085 insn_terminates_group_p (next_insn, previous_group)));
24086 if (*group_end && end)
24087 (*group_count)--;
24089 if (sched_verbose > 6)
24090 fprintf (dump, "done force: group count = %d, can_issue_more = %d\n",
24091 *group_count, can_issue_more);
24092 return can_issue_more;
24095 return can_issue_more;
24098 /* This function tries to synch the dispatch groups that the compiler "sees"
24099 with the dispatch groups that the processor dispatcher is expected to
24100 form in practice. It tries to achieve this synchronization by forcing the
24101 estimated processor grouping on the compiler (as opposed to the function
24102 'pad_goups' which tries to force the scheduler's grouping on the processor).
24104 The function scans the insn sequence between PREV_HEAD_INSN and TAIL and
24105 examines the (estimated) dispatch groups that will be formed by the processor
24106 dispatcher. It marks these group boundaries to reflect the estimated
24107 processor grouping, overriding the grouping that the scheduler had marked.
24108 Depending on the value of the flag '-minsert-sched-nops' this function can
24109 force certain insns into separate groups or force a certain distance between
24110 them by inserting nops, for example, if there exists a "costly dependence"
24111 between the insns.
24113 The function estimates the group boundaries that the processor will form as
24114 follows: It keeps track of how many vacant issue slots are available after
24115 each insn. A subsequent insn will start a new group if one of the following
24116 4 cases applies:
24117 - no more vacant issue slots remain in the current dispatch group.
24118 - only the last issue slot, which is the branch slot, is vacant, but the next
24119 insn is not a branch.
24120 - only the last 2 or less issue slots, including the branch slot, are vacant,
24121 which means that a cracked insn (which occupies two issue slots) can't be
24122 issued in this group.
24123 - less than 'issue_rate' slots are vacant, and the next insn always needs to
24124 start a new group. */
24126 static int
24127 redefine_groups (FILE *dump, int sched_verbose, rtx prev_head_insn, rtx tail)
24129 rtx insn, next_insn;
24130 int issue_rate;
24131 int can_issue_more;
24132 int slot, i;
24133 bool group_end;
24134 int group_count = 0;
24135 rtx *group_insns;
24137 /* Initialize. */
24138 issue_rate = rs6000_issue_rate ();
24139 group_insns = XALLOCAVEC (rtx, issue_rate);
24140 for (i = 0; i < issue_rate; i++)
24142 group_insns[i] = 0;
24144 can_issue_more = issue_rate;
24145 slot = 0;
24146 insn = get_next_active_insn (prev_head_insn, tail);
24147 group_end = false;
24149 while (insn != NULL_RTX)
24151 slot = (issue_rate - can_issue_more);
24152 group_insns[slot] = insn;
24153 can_issue_more =
24154 rs6000_variable_issue (dump, sched_verbose, insn, can_issue_more);
24155 if (insn_terminates_group_p (insn, current_group))
24156 can_issue_more = 0;
24158 next_insn = get_next_active_insn (insn, tail);
24159 if (next_insn == NULL_RTX)
24160 return group_count + 1;
24162 /* Is next_insn going to start a new group? */
24163 group_end
24164 = (can_issue_more == 0
24165 || (can_issue_more == 1 && !is_branch_slot_insn (next_insn))
24166 || (can_issue_more <= 2 && is_cracked_insn (next_insn))
24167 || (can_issue_more < issue_rate &&
24168 insn_terminates_group_p (next_insn, previous_group)));
24170 can_issue_more = force_new_group (sched_verbose, dump, group_insns,
24171 next_insn, &group_end, can_issue_more,
24172 &group_count);
24174 if (group_end)
24176 group_count++;
24177 can_issue_more = 0;
24178 for (i = 0; i < issue_rate; i++)
24180 group_insns[i] = 0;
24184 if (GET_MODE (next_insn) == TImode && can_issue_more)
24185 PUT_MODE (next_insn, VOIDmode);
24186 else if (!can_issue_more && GET_MODE (next_insn) != TImode)
24187 PUT_MODE (next_insn, TImode);
24189 insn = next_insn;
24190 if (can_issue_more == 0)
24191 can_issue_more = issue_rate;
24192 } /* while */
24194 return group_count;
24197 /* Scan the insn sequence between PREV_HEAD_INSN and TAIL and examine the
24198 dispatch group boundaries that the scheduler had marked. Pad with nops
24199 any dispatch groups which have vacant issue slots, in order to force the
24200 scheduler's grouping on the processor dispatcher. The function
24201 returns the number of dispatch groups found. */
24203 static int
24204 pad_groups (FILE *dump, int sched_verbose, rtx prev_head_insn, rtx tail)
24206 rtx insn, next_insn;
24207 rtx nop;
24208 int issue_rate;
24209 int can_issue_more;
24210 int group_end;
24211 int group_count = 0;
24213 /* Initialize issue_rate. */
24214 issue_rate = rs6000_issue_rate ();
24215 can_issue_more = issue_rate;
24217 insn = get_next_active_insn (prev_head_insn, tail);
24218 next_insn = get_next_active_insn (insn, tail);
24220 while (insn != NULL_RTX)
24222 can_issue_more =
24223 rs6000_variable_issue (dump, sched_verbose, insn, can_issue_more);
24225 group_end = (next_insn == NULL_RTX || GET_MODE (next_insn) == TImode);
24227 if (next_insn == NULL_RTX)
24228 break;
24230 if (group_end)
24232 /* If the scheduler had marked group termination at this location
24233 (between insn and next_insn), and neither insn nor next_insn will
24234 force group termination, pad the group with nops to force group
24235 termination. */
24236 if (can_issue_more
24237 && (rs6000_sched_insert_nops == sched_finish_pad_groups)
24238 && !insn_terminates_group_p (insn, current_group)
24239 && !insn_terminates_group_p (next_insn, previous_group))
24241 if (!is_branch_slot_insn (next_insn))
24242 can_issue_more--;
24244 while (can_issue_more)
24246 nop = gen_nop ();
24247 emit_insn_before (nop, next_insn);
24248 can_issue_more--;
24252 can_issue_more = issue_rate;
24253 group_count++;
24256 insn = next_insn;
24257 next_insn = get_next_active_insn (insn, tail);
24260 return group_count;
24263 /* We're beginning a new block. Initialize data structures as necessary. */
24265 static void
24266 rs6000_sched_init (FILE *dump ATTRIBUTE_UNUSED,
24267 int sched_verbose ATTRIBUTE_UNUSED,
24268 int max_ready ATTRIBUTE_UNUSED)
24270 last_scheduled_insn = NULL_RTX;
24271 load_store_pendulum = 0;
24274 /* The following function is called at the end of scheduling BB.
24275 After reload, it inserts nops at insn group bundling. */
24277 static void
24278 rs6000_sched_finish (FILE *dump, int sched_verbose)
24280 int n_groups;
24282 if (sched_verbose)
24283 fprintf (dump, "=== Finishing schedule.\n");
24285 if (reload_completed && rs6000_sched_groups)
24287 /* Do not run sched_finish hook when selective scheduling enabled. */
24288 if (sel_sched_p ())
24289 return;
24291 if (rs6000_sched_insert_nops == sched_finish_none)
24292 return;
24294 if (rs6000_sched_insert_nops == sched_finish_pad_groups)
24295 n_groups = pad_groups (dump, sched_verbose,
24296 current_sched_info->prev_head,
24297 current_sched_info->next_tail);
24298 else
24299 n_groups = redefine_groups (dump, sched_verbose,
24300 current_sched_info->prev_head,
24301 current_sched_info->next_tail);
24303 if (sched_verbose >= 6)
24305 fprintf (dump, "ngroups = %d\n", n_groups);
24306 print_rtl (dump, current_sched_info->prev_head);
24307 fprintf (dump, "Done finish_sched\n");
24312 struct _rs6000_sched_context
24314 short cached_can_issue_more;
24315 rtx last_scheduled_insn;
24316 int load_store_pendulum;
24319 typedef struct _rs6000_sched_context rs6000_sched_context_def;
24320 typedef rs6000_sched_context_def *rs6000_sched_context_t;
24322 /* Allocate store for new scheduling context. */
24323 static void *
24324 rs6000_alloc_sched_context (void)
24326 return xmalloc (sizeof (rs6000_sched_context_def));
24329 /* If CLEAN_P is true then initializes _SC with clean data,
24330 and from the global context otherwise. */
24331 static void
24332 rs6000_init_sched_context (void *_sc, bool clean_p)
24334 rs6000_sched_context_t sc = (rs6000_sched_context_t) _sc;
24336 if (clean_p)
24338 sc->cached_can_issue_more = 0;
24339 sc->last_scheduled_insn = NULL_RTX;
24340 sc->load_store_pendulum = 0;
24342 else
24344 sc->cached_can_issue_more = cached_can_issue_more;
24345 sc->last_scheduled_insn = last_scheduled_insn;
24346 sc->load_store_pendulum = load_store_pendulum;
24350 /* Sets the global scheduling context to the one pointed to by _SC. */
24351 static void
24352 rs6000_set_sched_context (void *_sc)
24354 rs6000_sched_context_t sc = (rs6000_sched_context_t) _sc;
24356 gcc_assert (sc != NULL);
24358 cached_can_issue_more = sc->cached_can_issue_more;
24359 last_scheduled_insn = sc->last_scheduled_insn;
24360 load_store_pendulum = sc->load_store_pendulum;
24363 /* Free _SC. */
24364 static void
24365 rs6000_free_sched_context (void *_sc)
24367 gcc_assert (_sc != NULL);
24369 free (_sc);
24373 /* Length in units of the trampoline for entering a nested function. */
24376 rs6000_trampoline_size (void)
24378 int ret = 0;
24380 switch (DEFAULT_ABI)
24382 default:
24383 gcc_unreachable ();
24385 case ABI_AIX:
24386 ret = (TARGET_32BIT) ? 12 : 24;
24387 break;
24389 case ABI_DARWIN:
24390 case ABI_V4:
24391 ret = (TARGET_32BIT) ? 40 : 48;
24392 break;
24395 return ret;
24398 /* Emit RTL insns to initialize the variable parts of a trampoline.
24399 FNADDR is an RTX for the address of the function's pure code.
24400 CXT is an RTX for the static chain value for the function. */
24402 static void
24403 rs6000_trampoline_init (rtx m_tramp, tree fndecl, rtx cxt)
24405 int regsize = (TARGET_32BIT) ? 4 : 8;
24406 rtx fnaddr = XEXP (DECL_RTL (fndecl), 0);
24407 rtx ctx_reg = force_reg (Pmode, cxt);
24408 rtx addr = force_reg (Pmode, XEXP (m_tramp, 0));
24410 switch (DEFAULT_ABI)
24412 default:
24413 gcc_unreachable ();
24415 /* Under AIX, just build the 3 word function descriptor */
24416 case ABI_AIX:
24418 rtx fnmem, fn_reg, toc_reg;
24420 if (!TARGET_POINTERS_TO_NESTED_FUNCTIONS)
24421 error ("You cannot take the address of a nested function if you use "
24422 "the -mno-pointers-to-nested-functions option.");
24424 fnmem = gen_const_mem (Pmode, force_reg (Pmode, fnaddr));
24425 fn_reg = gen_reg_rtx (Pmode);
24426 toc_reg = gen_reg_rtx (Pmode);
24428 /* Macro to shorten the code expansions below. */
24429 # define MEM_PLUS(MEM, OFFSET) adjust_address (MEM, Pmode, OFFSET)
24431 m_tramp = replace_equiv_address (m_tramp, addr);
24433 emit_move_insn (fn_reg, MEM_PLUS (fnmem, 0));
24434 emit_move_insn (toc_reg, MEM_PLUS (fnmem, regsize));
24435 emit_move_insn (MEM_PLUS (m_tramp, 0), fn_reg);
24436 emit_move_insn (MEM_PLUS (m_tramp, regsize), toc_reg);
24437 emit_move_insn (MEM_PLUS (m_tramp, 2*regsize), ctx_reg);
24439 # undef MEM_PLUS
24441 break;
24443 /* Under V.4/eabi/darwin, __trampoline_setup does the real work. */
24444 case ABI_DARWIN:
24445 case ABI_V4:
24446 emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "__trampoline_setup"),
24447 LCT_NORMAL, VOIDmode, 4,
24448 addr, Pmode,
24449 GEN_INT (rs6000_trampoline_size ()), SImode,
24450 fnaddr, Pmode,
24451 ctx_reg, Pmode);
24452 break;
24457 /* Returns TRUE iff the target attribute indicated by ATTR_ID takes a plain
24458 identifier as an argument, so the front end shouldn't look it up. */
24460 static bool
24461 rs6000_attribute_takes_identifier_p (const_tree attr_id)
24463 return is_attribute_p ("altivec", attr_id);
24466 /* Handle the "altivec" attribute. The attribute may have
24467 arguments as follows:
24469 __attribute__((altivec(vector__)))
24470 __attribute__((altivec(pixel__))) (always followed by 'unsigned short')
24471 __attribute__((altivec(bool__))) (always followed by 'unsigned')
24473 and may appear more than once (e.g., 'vector bool char') in a
24474 given declaration. */
24476 static tree
24477 rs6000_handle_altivec_attribute (tree *node,
24478 tree name ATTRIBUTE_UNUSED,
24479 tree args,
24480 int flags ATTRIBUTE_UNUSED,
24481 bool *no_add_attrs)
24483 tree type = *node, result = NULL_TREE;
24484 enum machine_mode mode;
24485 int unsigned_p;
24486 char altivec_type
24487 = ((args && TREE_CODE (args) == TREE_LIST && TREE_VALUE (args)
24488 && TREE_CODE (TREE_VALUE (args)) == IDENTIFIER_NODE)
24489 ? *IDENTIFIER_POINTER (TREE_VALUE (args))
24490 : '?');
24492 while (POINTER_TYPE_P (type)
24493 || TREE_CODE (type) == FUNCTION_TYPE
24494 || TREE_CODE (type) == METHOD_TYPE
24495 || TREE_CODE (type) == ARRAY_TYPE)
24496 type = TREE_TYPE (type);
24498 mode = TYPE_MODE (type);
24500 /* Check for invalid AltiVec type qualifiers. */
24501 if (type == long_double_type_node)
24502 error ("use of %<long double%> in AltiVec types is invalid");
24503 else if (type == boolean_type_node)
24504 error ("use of boolean types in AltiVec types is invalid");
24505 else if (TREE_CODE (type) == COMPLEX_TYPE)
24506 error ("use of %<complex%> in AltiVec types is invalid");
24507 else if (DECIMAL_FLOAT_MODE_P (mode))
24508 error ("use of decimal floating point types in AltiVec types is invalid");
24509 else if (!TARGET_VSX)
24511 if (type == long_unsigned_type_node || type == long_integer_type_node)
24513 if (TARGET_64BIT)
24514 error ("use of %<long%> in AltiVec types is invalid for "
24515 "64-bit code without -mvsx");
24516 else if (rs6000_warn_altivec_long)
24517 warning (0, "use of %<long%> in AltiVec types is deprecated; "
24518 "use %<int%>");
24520 else if (type == long_long_unsigned_type_node
24521 || type == long_long_integer_type_node)
24522 error ("use of %<long long%> in AltiVec types is invalid without "
24523 "-mvsx");
24524 else if (type == double_type_node)
24525 error ("use of %<double%> in AltiVec types is invalid without -mvsx");
24528 switch (altivec_type)
24530 case 'v':
24531 unsigned_p = TYPE_UNSIGNED (type);
24532 switch (mode)
24534 case DImode:
24535 result = (unsigned_p ? unsigned_V2DI_type_node : V2DI_type_node);
24536 break;
24537 case SImode:
24538 result = (unsigned_p ? unsigned_V4SI_type_node : V4SI_type_node);
24539 break;
24540 case HImode:
24541 result = (unsigned_p ? unsigned_V8HI_type_node : V8HI_type_node);
24542 break;
24543 case QImode:
24544 result = (unsigned_p ? unsigned_V16QI_type_node : V16QI_type_node);
24545 break;
24546 case SFmode: result = V4SF_type_node; break;
24547 case DFmode: result = V2DF_type_node; break;
24548 /* If the user says 'vector int bool', we may be handed the 'bool'
24549 attribute _before_ the 'vector' attribute, and so select the
24550 proper type in the 'b' case below. */
24551 case V4SImode: case V8HImode: case V16QImode: case V4SFmode:
24552 case V2DImode: case V2DFmode:
24553 result = type;
24554 default: break;
24556 break;
24557 case 'b':
24558 switch (mode)
24560 case DImode: case V2DImode: result = bool_V2DI_type_node; break;
24561 case SImode: case V4SImode: result = bool_V4SI_type_node; break;
24562 case HImode: case V8HImode: result = bool_V8HI_type_node; break;
24563 case QImode: case V16QImode: result = bool_V16QI_type_node;
24564 default: break;
24566 break;
24567 case 'p':
24568 switch (mode)
24570 case V8HImode: result = pixel_V8HI_type_node;
24571 default: break;
24573 default: break;
24576 /* Propagate qualifiers attached to the element type
24577 onto the vector type. */
24578 if (result && result != type && TYPE_QUALS (type))
24579 result = build_qualified_type (result, TYPE_QUALS (type));
24581 *no_add_attrs = true; /* No need to hang on to the attribute. */
24583 if (result)
24584 *node = lang_hooks.types.reconstruct_complex_type (*node, result);
24586 return NULL_TREE;
24589 /* AltiVec defines four built-in scalar types that serve as vector
24590 elements; we must teach the compiler how to mangle them. */
24592 static const char *
24593 rs6000_mangle_type (const_tree type)
24595 type = TYPE_MAIN_VARIANT (type);
24597 if (TREE_CODE (type) != VOID_TYPE && TREE_CODE (type) != BOOLEAN_TYPE
24598 && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
24599 return NULL;
24601 if (type == bool_char_type_node) return "U6__boolc";
24602 if (type == bool_short_type_node) return "U6__bools";
24603 if (type == pixel_type_node) return "u7__pixel";
24604 if (type == bool_int_type_node) return "U6__booli";
24605 if (type == bool_long_type_node) return "U6__booll";
24607 /* Mangle IBM extended float long double as `g' (__float128) on
24608 powerpc*-linux where long-double-64 previously was the default. */
24609 if (TYPE_MAIN_VARIANT (type) == long_double_type_node
24610 && TARGET_ELF
24611 && TARGET_LONG_DOUBLE_128
24612 && !TARGET_IEEEQUAD)
24613 return "g";
24615 /* For all other types, use normal C++ mangling. */
24616 return NULL;
24619 /* Handle a "longcall" or "shortcall" attribute; arguments as in
24620 struct attribute_spec.handler. */
24622 static tree
24623 rs6000_handle_longcall_attribute (tree *node, tree name,
24624 tree args ATTRIBUTE_UNUSED,
24625 int flags ATTRIBUTE_UNUSED,
24626 bool *no_add_attrs)
24628 if (TREE_CODE (*node) != FUNCTION_TYPE
24629 && TREE_CODE (*node) != FIELD_DECL
24630 && TREE_CODE (*node) != TYPE_DECL)
24632 warning (OPT_Wattributes, "%qE attribute only applies to functions",
24633 name);
24634 *no_add_attrs = true;
24637 return NULL_TREE;
24640 /* Set longcall attributes on all functions declared when
24641 rs6000_default_long_calls is true. */
24642 static void
24643 rs6000_set_default_type_attributes (tree type)
24645 if (rs6000_default_long_calls
24646 && (TREE_CODE (type) == FUNCTION_TYPE
24647 || TREE_CODE (type) == METHOD_TYPE))
24648 TYPE_ATTRIBUTES (type) = tree_cons (get_identifier ("longcall"),
24649 NULL_TREE,
24650 TYPE_ATTRIBUTES (type));
24652 #if TARGET_MACHO
24653 darwin_set_default_type_attributes (type);
24654 #endif
24657 /* Return a reference suitable for calling a function with the
24658 longcall attribute. */
24661 rs6000_longcall_ref (rtx call_ref)
24663 const char *call_name;
24664 tree node;
24666 if (GET_CODE (call_ref) != SYMBOL_REF)
24667 return call_ref;
24669 /* System V adds '.' to the internal name, so skip them. */
24670 call_name = XSTR (call_ref, 0);
24671 if (*call_name == '.')
24673 while (*call_name == '.')
24674 call_name++;
24676 node = get_identifier (call_name);
24677 call_ref = gen_rtx_SYMBOL_REF (VOIDmode, IDENTIFIER_POINTER (node));
24680 return force_reg (Pmode, call_ref);
24683 #ifndef TARGET_USE_MS_BITFIELD_LAYOUT
24684 #define TARGET_USE_MS_BITFIELD_LAYOUT 0
24685 #endif
24687 /* Handle a "ms_struct" or "gcc_struct" attribute; arguments as in
24688 struct attribute_spec.handler. */
24689 static tree
24690 rs6000_handle_struct_attribute (tree *node, tree name,
24691 tree args ATTRIBUTE_UNUSED,
24692 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
24694 tree *type = NULL;
24695 if (DECL_P (*node))
24697 if (TREE_CODE (*node) == TYPE_DECL)
24698 type = &TREE_TYPE (*node);
24700 else
24701 type = node;
24703 if (!(type && (TREE_CODE (*type) == RECORD_TYPE
24704 || TREE_CODE (*type) == UNION_TYPE)))
24706 warning (OPT_Wattributes, "%qE attribute ignored", name);
24707 *no_add_attrs = true;
24710 else if ((is_attribute_p ("ms_struct", name)
24711 && lookup_attribute ("gcc_struct", TYPE_ATTRIBUTES (*type)))
24712 || ((is_attribute_p ("gcc_struct", name)
24713 && lookup_attribute ("ms_struct", TYPE_ATTRIBUTES (*type)))))
24715 warning (OPT_Wattributes, "%qE incompatible attribute ignored",
24716 name);
24717 *no_add_attrs = true;
24720 return NULL_TREE;
24723 static bool
24724 rs6000_ms_bitfield_layout_p (const_tree record_type)
24726 return (TARGET_USE_MS_BITFIELD_LAYOUT &&
24727 !lookup_attribute ("gcc_struct", TYPE_ATTRIBUTES (record_type)))
24728 || lookup_attribute ("ms_struct", TYPE_ATTRIBUTES (record_type));
24731 #ifdef USING_ELFOS_H
24733 /* A get_unnamed_section callback, used for switching to toc_section. */
24735 static void
24736 rs6000_elf_output_toc_section_asm_op (const void *data ATTRIBUTE_UNUSED)
24738 if (DEFAULT_ABI == ABI_AIX
24739 && TARGET_MINIMAL_TOC
24740 && !TARGET_RELOCATABLE)
24742 if (!toc_initialized)
24744 toc_initialized = 1;
24745 fprintf (asm_out_file, "%s\n", TOC_SECTION_ASM_OP);
24746 (*targetm.asm_out.internal_label) (asm_out_file, "LCTOC", 0);
24747 fprintf (asm_out_file, "\t.tc ");
24748 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (asm_out_file, "LCTOC1[TC],");
24749 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (asm_out_file, "LCTOC1");
24750 fprintf (asm_out_file, "\n");
24752 fprintf (asm_out_file, "%s\n", MINIMAL_TOC_SECTION_ASM_OP);
24753 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (asm_out_file, "LCTOC1");
24754 fprintf (asm_out_file, " = .+32768\n");
24756 else
24757 fprintf (asm_out_file, "%s\n", MINIMAL_TOC_SECTION_ASM_OP);
24759 else if (DEFAULT_ABI == ABI_AIX && !TARGET_RELOCATABLE)
24760 fprintf (asm_out_file, "%s\n", TOC_SECTION_ASM_OP);
24761 else
24763 fprintf (asm_out_file, "%s\n", MINIMAL_TOC_SECTION_ASM_OP);
24764 if (!toc_initialized)
24766 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (asm_out_file, "LCTOC1");
24767 fprintf (asm_out_file, " = .+32768\n");
24768 toc_initialized = 1;
24773 /* Implement TARGET_ASM_INIT_SECTIONS. */
24775 static void
24776 rs6000_elf_asm_init_sections (void)
24778 toc_section
24779 = get_unnamed_section (0, rs6000_elf_output_toc_section_asm_op, NULL);
24781 sdata2_section
24782 = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
24783 SDATA2_SECTION_ASM_OP);
24786 /* Implement TARGET_SELECT_RTX_SECTION. */
24788 static section *
24789 rs6000_elf_select_rtx_section (enum machine_mode mode, rtx x,
24790 unsigned HOST_WIDE_INT align)
24792 if (ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (x, mode))
24793 return toc_section;
24794 else
24795 return default_elf_select_rtx_section (mode, x, align);
24798 /* For a SYMBOL_REF, set generic flags and then perform some
24799 target-specific processing.
24801 When the AIX ABI is requested on a non-AIX system, replace the
24802 function name with the real name (with a leading .) rather than the
24803 function descriptor name. This saves a lot of overriding code to
24804 read the prefixes. */
24806 static void rs6000_elf_encode_section_info (tree, rtx, int) ATTRIBUTE_UNUSED;
24807 static void
24808 rs6000_elf_encode_section_info (tree decl, rtx rtl, int first)
24810 default_encode_section_info (decl, rtl, first);
24812 if (first
24813 && TREE_CODE (decl) == FUNCTION_DECL
24814 && !TARGET_AIX
24815 && DEFAULT_ABI == ABI_AIX)
24817 rtx sym_ref = XEXP (rtl, 0);
24818 size_t len = strlen (XSTR (sym_ref, 0));
24819 char *str = XALLOCAVEC (char, len + 2);
24820 str[0] = '.';
24821 memcpy (str + 1, XSTR (sym_ref, 0), len + 1);
24822 XSTR (sym_ref, 0) = ggc_alloc_string (str, len + 1);
24826 static inline bool
24827 compare_section_name (const char *section, const char *templ)
24829 int len;
24831 len = strlen (templ);
24832 return (strncmp (section, templ, len) == 0
24833 && (section[len] == 0 || section[len] == '.'));
24836 bool
24837 rs6000_elf_in_small_data_p (const_tree decl)
24839 if (rs6000_sdata == SDATA_NONE)
24840 return false;
24842 /* We want to merge strings, so we never consider them small data. */
24843 if (TREE_CODE (decl) == STRING_CST)
24844 return false;
24846 /* Functions are never in the small data area. */
24847 if (TREE_CODE (decl) == FUNCTION_DECL)
24848 return false;
24850 if (TREE_CODE (decl) == VAR_DECL && DECL_SECTION_NAME (decl))
24852 const char *section = TREE_STRING_POINTER (DECL_SECTION_NAME (decl));
24853 if (compare_section_name (section, ".sdata")
24854 || compare_section_name (section, ".sdata2")
24855 || compare_section_name (section, ".gnu.linkonce.s")
24856 || compare_section_name (section, ".sbss")
24857 || compare_section_name (section, ".sbss2")
24858 || compare_section_name (section, ".gnu.linkonce.sb")
24859 || strcmp (section, ".PPC.EMB.sdata0") == 0
24860 || strcmp (section, ".PPC.EMB.sbss0") == 0)
24861 return true;
24863 else
24865 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (decl));
24867 if (size > 0
24868 && size <= g_switch_value
24869 /* If it's not public, and we're not going to reference it there,
24870 there's no need to put it in the small data section. */
24871 && (rs6000_sdata != SDATA_DATA || TREE_PUBLIC (decl)))
24872 return true;
24875 return false;
24878 #endif /* USING_ELFOS_H */
24880 /* Implement TARGET_USE_BLOCKS_FOR_CONSTANT_P. */
24882 static bool
24883 rs6000_use_blocks_for_constant_p (enum machine_mode mode, const_rtx x)
24885 return !ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (x, mode);
24888 /* Return a REG that occurs in ADDR with coefficient 1.
24889 ADDR can be effectively incremented by incrementing REG.
24891 r0 is special and we must not select it as an address
24892 register by this routine since our caller will try to
24893 increment the returned register via an "la" instruction. */
24896 find_addr_reg (rtx addr)
24898 while (GET_CODE (addr) == PLUS)
24900 if (GET_CODE (XEXP (addr, 0)) == REG
24901 && REGNO (XEXP (addr, 0)) != 0)
24902 addr = XEXP (addr, 0);
24903 else if (GET_CODE (XEXP (addr, 1)) == REG
24904 && REGNO (XEXP (addr, 1)) != 0)
24905 addr = XEXP (addr, 1);
24906 else if (CONSTANT_P (XEXP (addr, 0)))
24907 addr = XEXP (addr, 1);
24908 else if (CONSTANT_P (XEXP (addr, 1)))
24909 addr = XEXP (addr, 0);
24910 else
24911 gcc_unreachable ();
24913 gcc_assert (GET_CODE (addr) == REG && REGNO (addr) != 0);
24914 return addr;
24917 void
24918 rs6000_fatal_bad_address (rtx op)
24920 fatal_insn ("bad address", op);
24923 #if TARGET_MACHO
24925 typedef struct branch_island_d {
24926 tree function_name;
24927 tree label_name;
24928 int line_number;
24929 } branch_island;
24932 static vec<branch_island, va_gc> *branch_islands;
24934 /* Remember to generate a branch island for far calls to the given
24935 function. */
24937 static void
24938 add_compiler_branch_island (tree label_name, tree function_name,
24939 int line_number)
24941 branch_island bi = {function_name, label_name, line_number};
24942 vec_safe_push (branch_islands, bi);
24945 /* Generate far-jump branch islands for everything recorded in
24946 branch_islands. Invoked immediately after the last instruction of
24947 the epilogue has been emitted; the branch islands must be appended
24948 to, and contiguous with, the function body. Mach-O stubs are
24949 generated in machopic_output_stub(). */
24951 static void
24952 macho_branch_islands (void)
24954 char tmp_buf[512];
24956 while (!vec_safe_is_empty (branch_islands))
24958 branch_island *bi = &branch_islands->last ();
24959 const char *label = IDENTIFIER_POINTER (bi->label_name);
24960 const char *name = IDENTIFIER_POINTER (bi->function_name);
24961 char name_buf[512];
24962 /* Cheap copy of the details from the Darwin ASM_OUTPUT_LABELREF(). */
24963 if (name[0] == '*' || name[0] == '&')
24964 strcpy (name_buf, name+1);
24965 else
24967 name_buf[0] = '_';
24968 strcpy (name_buf+1, name);
24970 strcpy (tmp_buf, "\n");
24971 strcat (tmp_buf, label);
24972 #if defined (DBX_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)
24973 if (write_symbols == DBX_DEBUG || write_symbols == XCOFF_DEBUG)
24974 dbxout_stabd (N_SLINE, bi->line_number);
24975 #endif /* DBX_DEBUGGING_INFO || XCOFF_DEBUGGING_INFO */
24976 if (flag_pic)
24978 if (TARGET_LINK_STACK)
24980 char name[32];
24981 get_ppc476_thunk_name (name);
24982 strcat (tmp_buf, ":\n\tmflr r0\n\tbl ");
24983 strcat (tmp_buf, name);
24984 strcat (tmp_buf, "\n");
24985 strcat (tmp_buf, label);
24986 strcat (tmp_buf, "_pic:\n\tmflr r11\n");
24988 else
24990 strcat (tmp_buf, ":\n\tmflr r0\n\tbcl 20,31,");
24991 strcat (tmp_buf, label);
24992 strcat (tmp_buf, "_pic\n");
24993 strcat (tmp_buf, label);
24994 strcat (tmp_buf, "_pic:\n\tmflr r11\n");
24997 strcat (tmp_buf, "\taddis r11,r11,ha16(");
24998 strcat (tmp_buf, name_buf);
24999 strcat (tmp_buf, " - ");
25000 strcat (tmp_buf, label);
25001 strcat (tmp_buf, "_pic)\n");
25003 strcat (tmp_buf, "\tmtlr r0\n");
25005 strcat (tmp_buf, "\taddi r12,r11,lo16(");
25006 strcat (tmp_buf, name_buf);
25007 strcat (tmp_buf, " - ");
25008 strcat (tmp_buf, label);
25009 strcat (tmp_buf, "_pic)\n");
25011 strcat (tmp_buf, "\tmtctr r12\n\tbctr\n");
25013 else
25015 strcat (tmp_buf, ":\nlis r12,hi16(");
25016 strcat (tmp_buf, name_buf);
25017 strcat (tmp_buf, ")\n\tori r12,r12,lo16(");
25018 strcat (tmp_buf, name_buf);
25019 strcat (tmp_buf, ")\n\tmtctr r12\n\tbctr");
25021 output_asm_insn (tmp_buf, 0);
25022 #if defined (DBX_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)
25023 if (write_symbols == DBX_DEBUG || write_symbols == XCOFF_DEBUG)
25024 dbxout_stabd (N_SLINE, bi->line_number);
25025 #endif /* DBX_DEBUGGING_INFO || XCOFF_DEBUGGING_INFO */
25026 branch_islands->pop ();
25030 /* NO_PREVIOUS_DEF checks in the link list whether the function name is
25031 already there or not. */
25033 static int
25034 no_previous_def (tree function_name)
25036 branch_island *bi;
25037 unsigned ix;
25039 FOR_EACH_VEC_SAFE_ELT (branch_islands, ix, bi)
25040 if (function_name == bi->function_name)
25041 return 0;
25042 return 1;
25045 /* GET_PREV_LABEL gets the label name from the previous definition of
25046 the function. */
25048 static tree
25049 get_prev_label (tree function_name)
25051 branch_island *bi;
25052 unsigned ix;
25054 FOR_EACH_VEC_SAFE_ELT (branch_islands, ix, bi)
25055 if (function_name == bi->function_name)
25056 return bi->label_name;
25057 return NULL_TREE;
25060 /* INSN is either a function call or a millicode call. It may have an
25061 unconditional jump in its delay slot.
25063 CALL_DEST is the routine we are calling. */
25065 char *
25066 output_call (rtx insn, rtx *operands, int dest_operand_number,
25067 int cookie_operand_number)
25069 static char buf[256];
25070 if (darwin_emit_branch_islands
25071 && GET_CODE (operands[dest_operand_number]) == SYMBOL_REF
25072 && (INTVAL (operands[cookie_operand_number]) & CALL_LONG))
25074 tree labelname;
25075 tree funname = get_identifier (XSTR (operands[dest_operand_number], 0));
25077 if (no_previous_def (funname))
25079 rtx label_rtx = gen_label_rtx ();
25080 char *label_buf, temp_buf[256];
25081 ASM_GENERATE_INTERNAL_LABEL (temp_buf, "L",
25082 CODE_LABEL_NUMBER (label_rtx));
25083 label_buf = temp_buf[0] == '*' ? temp_buf + 1 : temp_buf;
25084 labelname = get_identifier (label_buf);
25085 add_compiler_branch_island (labelname, funname, insn_line (insn));
25087 else
25088 labelname = get_prev_label (funname);
25090 /* "jbsr foo, L42" is Mach-O for "Link as 'bl foo' if a 'bl'
25091 instruction will reach 'foo', otherwise link as 'bl L42'".
25092 "L42" should be a 'branch island', that will do a far jump to
25093 'foo'. Branch islands are generated in
25094 macho_branch_islands(). */
25095 sprintf (buf, "jbsr %%z%d,%.246s",
25096 dest_operand_number, IDENTIFIER_POINTER (labelname));
25098 else
25099 sprintf (buf, "bl %%z%d", dest_operand_number);
25100 return buf;
25103 /* Generate PIC and indirect symbol stubs. */
25105 void
25106 machopic_output_stub (FILE *file, const char *symb, const char *stub)
25108 unsigned int length;
25109 char *symbol_name, *lazy_ptr_name;
25110 char *local_label_0;
25111 static int label = 0;
25113 /* Lose our funky encoding stuff so it doesn't contaminate the stub. */
25114 symb = (*targetm.strip_name_encoding) (symb);
25117 length = strlen (symb);
25118 symbol_name = XALLOCAVEC (char, length + 32);
25119 GEN_SYMBOL_NAME_FOR_SYMBOL (symbol_name, symb, length);
25121 lazy_ptr_name = XALLOCAVEC (char, length + 32);
25122 GEN_LAZY_PTR_NAME_FOR_SYMBOL (lazy_ptr_name, symb, length);
25124 if (flag_pic == 2)
25125 switch_to_section (darwin_sections[machopic_picsymbol_stub1_section]);
25126 else
25127 switch_to_section (darwin_sections[machopic_symbol_stub1_section]);
25129 if (flag_pic == 2)
25131 fprintf (file, "\t.align 5\n");
25133 fprintf (file, "%s:\n", stub);
25134 fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
25136 label++;
25137 local_label_0 = XALLOCAVEC (char, sizeof ("\"L00000000000$spb\""));
25138 sprintf (local_label_0, "\"L%011d$spb\"", label);
25140 fprintf (file, "\tmflr r0\n");
25141 if (TARGET_LINK_STACK)
25143 char name[32];
25144 get_ppc476_thunk_name (name);
25145 fprintf (file, "\tbl %s\n", name);
25146 fprintf (file, "%s:\n\tmflr r11\n", local_label_0);
25148 else
25150 fprintf (file, "\tbcl 20,31,%s\n", local_label_0);
25151 fprintf (file, "%s:\n\tmflr r11\n", local_label_0);
25153 fprintf (file, "\taddis r11,r11,ha16(%s-%s)\n",
25154 lazy_ptr_name, local_label_0);
25155 fprintf (file, "\tmtlr r0\n");
25156 fprintf (file, "\t%s r12,lo16(%s-%s)(r11)\n",
25157 (TARGET_64BIT ? "ldu" : "lwzu"),
25158 lazy_ptr_name, local_label_0);
25159 fprintf (file, "\tmtctr r12\n");
25160 fprintf (file, "\tbctr\n");
25162 else
25164 fprintf (file, "\t.align 4\n");
25166 fprintf (file, "%s:\n", stub);
25167 fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
25169 fprintf (file, "\tlis r11,ha16(%s)\n", lazy_ptr_name);
25170 fprintf (file, "\t%s r12,lo16(%s)(r11)\n",
25171 (TARGET_64BIT ? "ldu" : "lwzu"),
25172 lazy_ptr_name);
25173 fprintf (file, "\tmtctr r12\n");
25174 fprintf (file, "\tbctr\n");
25177 switch_to_section (darwin_sections[machopic_lazy_symbol_ptr_section]);
25178 fprintf (file, "%s:\n", lazy_ptr_name);
25179 fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
25180 fprintf (file, "%sdyld_stub_binding_helper\n",
25181 (TARGET_64BIT ? DOUBLE_INT_ASM_OP : "\t.long\t"));
25184 /* Legitimize PIC addresses. If the address is already
25185 position-independent, we return ORIG. Newly generated
25186 position-independent addresses go into a reg. This is REG if non
25187 zero, otherwise we allocate register(s) as necessary. */
25189 #define SMALL_INT(X) ((UINTVAL (X) + 0x8000) < 0x10000)
25192 rs6000_machopic_legitimize_pic_address (rtx orig, enum machine_mode mode,
25193 rtx reg)
25195 rtx base, offset;
25197 if (reg == NULL && ! reload_in_progress && ! reload_completed)
25198 reg = gen_reg_rtx (Pmode);
25200 if (GET_CODE (orig) == CONST)
25202 rtx reg_temp;
25204 if (GET_CODE (XEXP (orig, 0)) == PLUS
25205 && XEXP (XEXP (orig, 0), 0) == pic_offset_table_rtx)
25206 return orig;
25208 gcc_assert (GET_CODE (XEXP (orig, 0)) == PLUS);
25210 /* Use a different reg for the intermediate value, as
25211 it will be marked UNCHANGING. */
25212 reg_temp = !can_create_pseudo_p () ? reg : gen_reg_rtx (Pmode);
25213 base = rs6000_machopic_legitimize_pic_address (XEXP (XEXP (orig, 0), 0),
25214 Pmode, reg_temp);
25215 offset =
25216 rs6000_machopic_legitimize_pic_address (XEXP (XEXP (orig, 0), 1),
25217 Pmode, reg);
25219 if (GET_CODE (offset) == CONST_INT)
25221 if (SMALL_INT (offset))
25222 return plus_constant (Pmode, base, INTVAL (offset));
25223 else if (! reload_in_progress && ! reload_completed)
25224 offset = force_reg (Pmode, offset);
25225 else
25227 rtx mem = force_const_mem (Pmode, orig);
25228 return machopic_legitimize_pic_address (mem, Pmode, reg);
25231 return gen_rtx_PLUS (Pmode, base, offset);
25234 /* Fall back on generic machopic code. */
25235 return machopic_legitimize_pic_address (orig, mode, reg);
25238 /* Output a .machine directive for the Darwin assembler, and call
25239 the generic start_file routine. */
25241 static void
25242 rs6000_darwin_file_start (void)
25244 static const struct
25246 const char *arg;
25247 const char *name;
25248 HOST_WIDE_INT if_set;
25249 } mapping[] = {
25250 { "ppc64", "ppc64", MASK_64BIT },
25251 { "970", "ppc970", MASK_PPC_GPOPT | MASK_MFCRF | MASK_POWERPC64 },
25252 { "power4", "ppc970", 0 },
25253 { "G5", "ppc970", 0 },
25254 { "7450", "ppc7450", 0 },
25255 { "7400", "ppc7400", MASK_ALTIVEC },
25256 { "G4", "ppc7400", 0 },
25257 { "750", "ppc750", 0 },
25258 { "740", "ppc750", 0 },
25259 { "G3", "ppc750", 0 },
25260 { "604e", "ppc604e", 0 },
25261 { "604", "ppc604", 0 },
25262 { "603e", "ppc603", 0 },
25263 { "603", "ppc603", 0 },
25264 { "601", "ppc601", 0 },
25265 { NULL, "ppc", 0 } };
25266 const char *cpu_id = "";
25267 size_t i;
25269 rs6000_file_start ();
25270 darwin_file_start ();
25272 /* Determine the argument to -mcpu=. Default to G3 if not specified. */
25274 if (rs6000_default_cpu != 0 && rs6000_default_cpu[0] != '\0')
25275 cpu_id = rs6000_default_cpu;
25277 if (global_options_set.x_rs6000_cpu_index)
25278 cpu_id = processor_target_table[rs6000_cpu_index].name;
25280 /* Look through the mapping array. Pick the first name that either
25281 matches the argument, has a bit set in IF_SET that is also set
25282 in the target flags, or has a NULL name. */
25284 i = 0;
25285 while (mapping[i].arg != NULL
25286 && strcmp (mapping[i].arg, cpu_id) != 0
25287 && (mapping[i].if_set & rs6000_isa_flags) == 0)
25288 i++;
25290 fprintf (asm_out_file, "\t.machine %s\n", mapping[i].name);
25293 #endif /* TARGET_MACHO */
25295 #if TARGET_ELF
25296 static int
25297 rs6000_elf_reloc_rw_mask (void)
25299 if (flag_pic)
25300 return 3;
25301 else if (DEFAULT_ABI == ABI_AIX)
25302 return 2;
25303 else
25304 return 0;
25307 /* Record an element in the table of global constructors. SYMBOL is
25308 a SYMBOL_REF of the function to be called; PRIORITY is a number
25309 between 0 and MAX_INIT_PRIORITY.
25311 This differs from default_named_section_asm_out_constructor in
25312 that we have special handling for -mrelocatable. */
25314 static void rs6000_elf_asm_out_constructor (rtx, int) ATTRIBUTE_UNUSED;
25315 static void
25316 rs6000_elf_asm_out_constructor (rtx symbol, int priority)
25318 const char *section = ".ctors";
25319 char buf[16];
25321 if (priority != DEFAULT_INIT_PRIORITY)
25323 sprintf (buf, ".ctors.%.5u",
25324 /* Invert the numbering so the linker puts us in the proper
25325 order; constructors are run from right to left, and the
25326 linker sorts in increasing order. */
25327 MAX_INIT_PRIORITY - priority);
25328 section = buf;
25331 switch_to_section (get_section (section, SECTION_WRITE, NULL));
25332 assemble_align (POINTER_SIZE);
25334 if (TARGET_RELOCATABLE)
25336 fputs ("\t.long (", asm_out_file);
25337 output_addr_const (asm_out_file, symbol);
25338 fputs (")@fixup\n", asm_out_file);
25340 else
25341 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
25344 static void rs6000_elf_asm_out_destructor (rtx, int) ATTRIBUTE_UNUSED;
25345 static void
25346 rs6000_elf_asm_out_destructor (rtx symbol, int priority)
25348 const char *section = ".dtors";
25349 char buf[16];
25351 if (priority != DEFAULT_INIT_PRIORITY)
25353 sprintf (buf, ".dtors.%.5u",
25354 /* Invert the numbering so the linker puts us in the proper
25355 order; constructors are run from right to left, and the
25356 linker sorts in increasing order. */
25357 MAX_INIT_PRIORITY - priority);
25358 section = buf;
25361 switch_to_section (get_section (section, SECTION_WRITE, NULL));
25362 assemble_align (POINTER_SIZE);
25364 if (TARGET_RELOCATABLE)
25366 fputs ("\t.long (", asm_out_file);
25367 output_addr_const (asm_out_file, symbol);
25368 fputs (")@fixup\n", asm_out_file);
25370 else
25371 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
25374 void
25375 rs6000_elf_declare_function_name (FILE *file, const char *name, tree decl)
25377 if (TARGET_64BIT)
25379 fputs ("\t.section\t\".opd\",\"aw\"\n\t.align 3\n", file);
25380 ASM_OUTPUT_LABEL (file, name);
25381 fputs (DOUBLE_INT_ASM_OP, file);
25382 rs6000_output_function_entry (file, name);
25383 fputs (",.TOC.@tocbase,0\n\t.previous\n", file);
25384 if (DOT_SYMBOLS)
25386 fputs ("\t.size\t", file);
25387 assemble_name (file, name);
25388 fputs (",24\n\t.type\t.", file);
25389 assemble_name (file, name);
25390 fputs (",@function\n", file);
25391 if (TREE_PUBLIC (decl) && ! DECL_WEAK (decl))
25393 fputs ("\t.globl\t.", file);
25394 assemble_name (file, name);
25395 putc ('\n', file);
25398 else
25399 ASM_OUTPUT_TYPE_DIRECTIVE (file, name, "function");
25400 ASM_DECLARE_RESULT (file, DECL_RESULT (decl));
25401 rs6000_output_function_entry (file, name);
25402 fputs (":\n", file);
25403 return;
25406 if (TARGET_RELOCATABLE
25407 && !TARGET_SECURE_PLT
25408 && (get_pool_size () != 0 || crtl->profile)
25409 && uses_TOC ())
25411 char buf[256];
25413 (*targetm.asm_out.internal_label) (file, "LCL", rs6000_pic_labelno);
25415 ASM_GENERATE_INTERNAL_LABEL (buf, "LCTOC", 1);
25416 fprintf (file, "\t.long ");
25417 assemble_name (file, buf);
25418 putc ('-', file);
25419 ASM_GENERATE_INTERNAL_LABEL (buf, "LCF", rs6000_pic_labelno);
25420 assemble_name (file, buf);
25421 putc ('\n', file);
25424 ASM_OUTPUT_TYPE_DIRECTIVE (file, name, "function");
25425 ASM_DECLARE_RESULT (file, DECL_RESULT (decl));
25427 if (DEFAULT_ABI == ABI_AIX)
25429 const char *desc_name, *orig_name;
25431 orig_name = (*targetm.strip_name_encoding) (name);
25432 desc_name = orig_name;
25433 while (*desc_name == '.')
25434 desc_name++;
25436 if (TREE_PUBLIC (decl))
25437 fprintf (file, "\t.globl %s\n", desc_name);
25439 fprintf (file, "%s\n", MINIMAL_TOC_SECTION_ASM_OP);
25440 fprintf (file, "%s:\n", desc_name);
25441 fprintf (file, "\t.long %s\n", orig_name);
25442 fputs ("\t.long _GLOBAL_OFFSET_TABLE_\n", file);
25443 if (DEFAULT_ABI == ABI_AIX)
25444 fputs ("\t.long 0\n", file);
25445 fprintf (file, "\t.previous\n");
25447 ASM_OUTPUT_LABEL (file, name);
25450 static void rs6000_elf_file_end (void) ATTRIBUTE_UNUSED;
25451 static void
25452 rs6000_elf_file_end (void)
25454 #ifdef HAVE_AS_GNU_ATTRIBUTE
25455 if (TARGET_32BIT && DEFAULT_ABI == ABI_V4)
25457 if (rs6000_passes_float)
25458 fprintf (asm_out_file, "\t.gnu_attribute 4, %d\n",
25459 ((TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_DOUBLE_FLOAT) ? 1
25460 : (TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_SINGLE_FLOAT) ? 3
25461 : 2));
25462 if (rs6000_passes_vector)
25463 fprintf (asm_out_file, "\t.gnu_attribute 8, %d\n",
25464 (TARGET_ALTIVEC_ABI ? 2
25465 : TARGET_SPE_ABI ? 3
25466 : 1));
25467 if (rs6000_returns_struct)
25468 fprintf (asm_out_file, "\t.gnu_attribute 12, %d\n",
25469 aix_struct_return ? 2 : 1);
25471 #endif
25472 #if defined (POWERPC_LINUX) || defined (POWERPC_FREEBSD)
25473 if (TARGET_32BIT)
25474 file_end_indicate_exec_stack ();
25475 #endif
25477 #endif
25479 #if TARGET_XCOFF
25480 static void
25481 rs6000_xcoff_asm_output_anchor (rtx symbol)
25483 char buffer[100];
25485 sprintf (buffer, "$ + " HOST_WIDE_INT_PRINT_DEC,
25486 SYMBOL_REF_BLOCK_OFFSET (symbol));
25487 ASM_OUTPUT_DEF (asm_out_file, XSTR (symbol, 0), buffer);
25490 static void
25491 rs6000_xcoff_asm_globalize_label (FILE *stream, const char *name)
25493 fputs (GLOBAL_ASM_OP, stream);
25494 RS6000_OUTPUT_BASENAME (stream, name);
25495 putc ('\n', stream);
25498 /* A get_unnamed_decl callback, used for read-only sections. PTR
25499 points to the section string variable. */
25501 static void
25502 rs6000_xcoff_output_readonly_section_asm_op (const void *directive)
25504 fprintf (asm_out_file, "\t.csect %s[RO],%s\n",
25505 *(const char *const *) directive,
25506 XCOFF_CSECT_DEFAULT_ALIGNMENT_STR);
25509 /* Likewise for read-write sections. */
25511 static void
25512 rs6000_xcoff_output_readwrite_section_asm_op (const void *directive)
25514 fprintf (asm_out_file, "\t.csect %s[RW],%s\n",
25515 *(const char *const *) directive,
25516 XCOFF_CSECT_DEFAULT_ALIGNMENT_STR);
25519 /* A get_unnamed_section callback, used for switching to toc_section. */
25521 static void
25522 rs6000_xcoff_output_toc_section_asm_op (const void *data ATTRIBUTE_UNUSED)
25524 if (TARGET_MINIMAL_TOC)
25526 /* toc_section is always selected at least once from
25527 rs6000_xcoff_file_start, so this is guaranteed to
25528 always be defined once and only once in each file. */
25529 if (!toc_initialized)
25531 fputs ("\t.toc\nLCTOC..1:\n", asm_out_file);
25532 fputs ("\t.tc toc_table[TC],toc_table[RW]\n", asm_out_file);
25533 toc_initialized = 1;
25535 fprintf (asm_out_file, "\t.csect toc_table[RW]%s\n",
25536 (TARGET_32BIT ? "" : ",3"));
25538 else
25539 fputs ("\t.toc\n", asm_out_file);
25542 /* Implement TARGET_ASM_INIT_SECTIONS. */
25544 static void
25545 rs6000_xcoff_asm_init_sections (void)
25547 read_only_data_section
25548 = get_unnamed_section (0, rs6000_xcoff_output_readonly_section_asm_op,
25549 &xcoff_read_only_section_name);
25551 private_data_section
25552 = get_unnamed_section (SECTION_WRITE,
25553 rs6000_xcoff_output_readwrite_section_asm_op,
25554 &xcoff_private_data_section_name);
25556 read_only_private_data_section
25557 = get_unnamed_section (0, rs6000_xcoff_output_readonly_section_asm_op,
25558 &xcoff_private_data_section_name);
25560 toc_section
25561 = get_unnamed_section (0, rs6000_xcoff_output_toc_section_asm_op, NULL);
25563 readonly_data_section = read_only_data_section;
25564 exception_section = data_section;
25567 static int
25568 rs6000_xcoff_reloc_rw_mask (void)
25570 return 3;
25573 static void
25574 rs6000_xcoff_asm_named_section (const char *name, unsigned int flags,
25575 tree decl ATTRIBUTE_UNUSED)
25577 int smclass;
25578 static const char * const suffix[4] = { "PR", "RO", "RW", "TL" };
25580 if (flags & SECTION_CODE)
25581 smclass = 0;
25582 else if (flags & SECTION_TLS)
25583 smclass = 3;
25584 else if (flags & SECTION_WRITE)
25585 smclass = 2;
25586 else
25587 smclass = 1;
25589 fprintf (asm_out_file, "\t.csect %s%s[%s],%u\n",
25590 (flags & SECTION_CODE) ? "." : "",
25591 name, suffix[smclass], flags & SECTION_ENTSIZE);
25594 static section *
25595 rs6000_xcoff_select_section (tree decl, int reloc,
25596 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
25598 if (decl_readonly_section (decl, reloc))
25600 if (TREE_PUBLIC (decl))
25601 return read_only_data_section;
25602 else
25603 return read_only_private_data_section;
25605 else
25607 if (TREE_PUBLIC (decl))
25608 return data_section;
25609 else
25610 return private_data_section;
25614 static void
25615 rs6000_xcoff_unique_section (tree decl, int reloc ATTRIBUTE_UNUSED)
25617 const char *name;
25619 /* Use select_section for private and uninitialized data. */
25620 if (!TREE_PUBLIC (decl)
25621 || DECL_COMMON (decl)
25622 || DECL_INITIAL (decl) == NULL_TREE
25623 || DECL_INITIAL (decl) == error_mark_node
25624 || (flag_zero_initialized_in_bss
25625 && initializer_zerop (DECL_INITIAL (decl))))
25626 return;
25628 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
25629 name = (*targetm.strip_name_encoding) (name);
25630 DECL_SECTION_NAME (decl) = build_string (strlen (name), name);
25633 /* Select section for constant in constant pool.
25635 On RS/6000, all constants are in the private read-only data area.
25636 However, if this is being placed in the TOC it must be output as a
25637 toc entry. */
25639 static section *
25640 rs6000_xcoff_select_rtx_section (enum machine_mode mode, rtx x,
25641 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
25643 if (ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (x, mode))
25644 return toc_section;
25645 else
25646 return read_only_private_data_section;
25649 /* Remove any trailing [DS] or the like from the symbol name. */
25651 static const char *
25652 rs6000_xcoff_strip_name_encoding (const char *name)
25654 size_t len;
25655 if (*name == '*')
25656 name++;
25657 len = strlen (name);
25658 if (name[len - 1] == ']')
25659 return ggc_alloc_string (name, len - 4);
25660 else
25661 return name;
25664 /* Section attributes. AIX is always PIC. */
25666 static unsigned int
25667 rs6000_xcoff_section_type_flags (tree decl, const char *name, int reloc)
25669 unsigned int align;
25670 unsigned int flags = default_section_type_flags (decl, name, reloc);
25672 /* Align to at least UNIT size. */
25673 if (flags & SECTION_CODE || !decl)
25674 align = MIN_UNITS_PER_WORD;
25675 else
25676 /* Increase alignment of large objects if not already stricter. */
25677 align = MAX ((DECL_ALIGN (decl) / BITS_PER_UNIT),
25678 int_size_in_bytes (TREE_TYPE (decl)) > MIN_UNITS_PER_WORD
25679 ? UNITS_PER_FP_WORD : MIN_UNITS_PER_WORD);
25681 return flags | (exact_log2 (align) & SECTION_ENTSIZE);
25684 /* Output at beginning of assembler file.
25686 Initialize the section names for the RS/6000 at this point.
25688 Specify filename, including full path, to assembler.
25690 We want to go into the TOC section so at least one .toc will be emitted.
25691 Also, in order to output proper .bs/.es pairs, we need at least one static
25692 [RW] section emitted.
25694 Finally, declare mcount when profiling to make the assembler happy. */
25696 static void
25697 rs6000_xcoff_file_start (void)
25699 rs6000_gen_section_name (&xcoff_bss_section_name,
25700 main_input_filename, ".bss_");
25701 rs6000_gen_section_name (&xcoff_private_data_section_name,
25702 main_input_filename, ".rw_");
25703 rs6000_gen_section_name (&xcoff_read_only_section_name,
25704 main_input_filename, ".ro_");
25706 fputs ("\t.file\t", asm_out_file);
25707 output_quoted_string (asm_out_file, main_input_filename);
25708 fputc ('\n', asm_out_file);
25709 if (write_symbols != NO_DEBUG)
25710 switch_to_section (private_data_section);
25711 switch_to_section (text_section);
25712 if (profile_flag)
25713 fprintf (asm_out_file, "\t.extern %s\n", RS6000_MCOUNT);
25714 rs6000_file_start ();
25717 /* Output at end of assembler file.
25718 On the RS/6000, referencing data should automatically pull in text. */
25720 static void
25721 rs6000_xcoff_file_end (void)
25723 switch_to_section (text_section);
25724 fputs ("_section_.text:\n", asm_out_file);
25725 switch_to_section (data_section);
25726 fputs (TARGET_32BIT
25727 ? "\t.long _section_.text\n" : "\t.llong _section_.text\n",
25728 asm_out_file);
25730 #endif /* TARGET_XCOFF */
25732 /* Compute a (partial) cost for rtx X. Return true if the complete
25733 cost has been computed, and false if subexpressions should be
25734 scanned. In either case, *TOTAL contains the cost result. */
25736 static bool
25737 rs6000_rtx_costs (rtx x, int code, int outer_code, int opno ATTRIBUTE_UNUSED,
25738 int *total, bool speed)
25740 enum machine_mode mode = GET_MODE (x);
25742 switch (code)
25744 /* On the RS/6000, if it is valid in the insn, it is free. */
25745 case CONST_INT:
25746 if (((outer_code == SET
25747 || outer_code == PLUS
25748 || outer_code == MINUS)
25749 && (satisfies_constraint_I (x)
25750 || satisfies_constraint_L (x)))
25751 || (outer_code == AND
25752 && (satisfies_constraint_K (x)
25753 || (mode == SImode
25754 ? satisfies_constraint_L (x)
25755 : satisfies_constraint_J (x))
25756 || mask_operand (x, mode)
25757 || (mode == DImode
25758 && mask64_operand (x, DImode))))
25759 || ((outer_code == IOR || outer_code == XOR)
25760 && (satisfies_constraint_K (x)
25761 || (mode == SImode
25762 ? satisfies_constraint_L (x)
25763 : satisfies_constraint_J (x))))
25764 || outer_code == ASHIFT
25765 || outer_code == ASHIFTRT
25766 || outer_code == LSHIFTRT
25767 || outer_code == ROTATE
25768 || outer_code == ROTATERT
25769 || outer_code == ZERO_EXTRACT
25770 || (outer_code == MULT
25771 && satisfies_constraint_I (x))
25772 || ((outer_code == DIV || outer_code == UDIV
25773 || outer_code == MOD || outer_code == UMOD)
25774 && exact_log2 (INTVAL (x)) >= 0)
25775 || (outer_code == COMPARE
25776 && (satisfies_constraint_I (x)
25777 || satisfies_constraint_K (x)))
25778 || ((outer_code == EQ || outer_code == NE)
25779 && (satisfies_constraint_I (x)
25780 || satisfies_constraint_K (x)
25781 || (mode == SImode
25782 ? satisfies_constraint_L (x)
25783 : satisfies_constraint_J (x))))
25784 || (outer_code == GTU
25785 && satisfies_constraint_I (x))
25786 || (outer_code == LTU
25787 && satisfies_constraint_P (x)))
25789 *total = 0;
25790 return true;
25792 else if ((outer_code == PLUS
25793 && reg_or_add_cint_operand (x, VOIDmode))
25794 || (outer_code == MINUS
25795 && reg_or_sub_cint_operand (x, VOIDmode))
25796 || ((outer_code == SET
25797 || outer_code == IOR
25798 || outer_code == XOR)
25799 && (INTVAL (x)
25800 & ~ (unsigned HOST_WIDE_INT) 0xffffffff) == 0))
25802 *total = COSTS_N_INSNS (1);
25803 return true;
25805 /* FALLTHRU */
25807 case CONST_DOUBLE:
25808 if (mode == DImode && code == CONST_DOUBLE)
25810 if ((outer_code == IOR || outer_code == XOR)
25811 && CONST_DOUBLE_HIGH (x) == 0
25812 && (CONST_DOUBLE_LOW (x)
25813 & ~ (unsigned HOST_WIDE_INT) 0xffff) == 0)
25815 *total = 0;
25816 return true;
25818 else if ((outer_code == AND && and64_2_operand (x, DImode))
25819 || ((outer_code == SET
25820 || outer_code == IOR
25821 || outer_code == XOR)
25822 && CONST_DOUBLE_HIGH (x) == 0))
25824 *total = COSTS_N_INSNS (1);
25825 return true;
25828 /* FALLTHRU */
25830 case CONST:
25831 case HIGH:
25832 case SYMBOL_REF:
25833 case MEM:
25834 /* When optimizing for size, MEM should be slightly more expensive
25835 than generating address, e.g., (plus (reg) (const)).
25836 L1 cache latency is about two instructions. */
25837 *total = !speed ? COSTS_N_INSNS (1) + 1 : COSTS_N_INSNS (2);
25838 return true;
25840 case LABEL_REF:
25841 *total = 0;
25842 return true;
25844 case PLUS:
25845 case MINUS:
25846 if (FLOAT_MODE_P (mode))
25847 *total = rs6000_cost->fp;
25848 else
25849 *total = COSTS_N_INSNS (1);
25850 return false;
25852 case MULT:
25853 if (GET_CODE (XEXP (x, 1)) == CONST_INT
25854 && satisfies_constraint_I (XEXP (x, 1)))
25856 if (INTVAL (XEXP (x, 1)) >= -256
25857 && INTVAL (XEXP (x, 1)) <= 255)
25858 *total = rs6000_cost->mulsi_const9;
25859 else
25860 *total = rs6000_cost->mulsi_const;
25862 else if (mode == SFmode)
25863 *total = rs6000_cost->fp;
25864 else if (FLOAT_MODE_P (mode))
25865 *total = rs6000_cost->dmul;
25866 else if (mode == DImode)
25867 *total = rs6000_cost->muldi;
25868 else
25869 *total = rs6000_cost->mulsi;
25870 return false;
25872 case FMA:
25873 if (mode == SFmode)
25874 *total = rs6000_cost->fp;
25875 else
25876 *total = rs6000_cost->dmul;
25877 break;
25879 case DIV:
25880 case MOD:
25881 if (FLOAT_MODE_P (mode))
25883 *total = mode == DFmode ? rs6000_cost->ddiv
25884 : rs6000_cost->sdiv;
25885 return false;
25887 /* FALLTHRU */
25889 case UDIV:
25890 case UMOD:
25891 if (GET_CODE (XEXP (x, 1)) == CONST_INT
25892 && exact_log2 (INTVAL (XEXP (x, 1))) >= 0)
25894 if (code == DIV || code == MOD)
25895 /* Shift, addze */
25896 *total = COSTS_N_INSNS (2);
25897 else
25898 /* Shift */
25899 *total = COSTS_N_INSNS (1);
25901 else
25903 if (GET_MODE (XEXP (x, 1)) == DImode)
25904 *total = rs6000_cost->divdi;
25905 else
25906 *total = rs6000_cost->divsi;
25908 /* Add in shift and subtract for MOD. */
25909 if (code == MOD || code == UMOD)
25910 *total += COSTS_N_INSNS (2);
25911 return false;
25913 case CTZ:
25914 case FFS:
25915 *total = COSTS_N_INSNS (4);
25916 return false;
25918 case POPCOUNT:
25919 *total = COSTS_N_INSNS (TARGET_POPCNTD ? 1 : 6);
25920 return false;
25922 case PARITY:
25923 *total = COSTS_N_INSNS (TARGET_CMPB ? 2 : 6);
25924 return false;
25926 case NOT:
25927 if (outer_code == AND || outer_code == IOR || outer_code == XOR)
25929 *total = 0;
25930 return false;
25932 /* FALLTHRU */
25934 case AND:
25935 case CLZ:
25936 case IOR:
25937 case XOR:
25938 case ZERO_EXTRACT:
25939 *total = COSTS_N_INSNS (1);
25940 return false;
25942 case ASHIFT:
25943 case ASHIFTRT:
25944 case LSHIFTRT:
25945 case ROTATE:
25946 case ROTATERT:
25947 /* Handle mul_highpart. */
25948 if (outer_code == TRUNCATE
25949 && GET_CODE (XEXP (x, 0)) == MULT)
25951 if (mode == DImode)
25952 *total = rs6000_cost->muldi;
25953 else
25954 *total = rs6000_cost->mulsi;
25955 return true;
25957 else if (outer_code == AND)
25958 *total = 0;
25959 else
25960 *total = COSTS_N_INSNS (1);
25961 return false;
25963 case SIGN_EXTEND:
25964 case ZERO_EXTEND:
25965 if (GET_CODE (XEXP (x, 0)) == MEM)
25966 *total = 0;
25967 else
25968 *total = COSTS_N_INSNS (1);
25969 return false;
25971 case COMPARE:
25972 case NEG:
25973 case ABS:
25974 if (!FLOAT_MODE_P (mode))
25976 *total = COSTS_N_INSNS (1);
25977 return false;
25979 /* FALLTHRU */
25981 case FLOAT:
25982 case UNSIGNED_FLOAT:
25983 case FIX:
25984 case UNSIGNED_FIX:
25985 case FLOAT_TRUNCATE:
25986 *total = rs6000_cost->fp;
25987 return false;
25989 case FLOAT_EXTEND:
25990 if (mode == DFmode)
25991 *total = 0;
25992 else
25993 *total = rs6000_cost->fp;
25994 return false;
25996 case UNSPEC:
25997 switch (XINT (x, 1))
25999 case UNSPEC_FRSP:
26000 *total = rs6000_cost->fp;
26001 return true;
26003 default:
26004 break;
26006 break;
26008 case CALL:
26009 case IF_THEN_ELSE:
26010 if (!speed)
26012 *total = COSTS_N_INSNS (1);
26013 return true;
26015 else if (FLOAT_MODE_P (mode)
26016 && TARGET_PPC_GFXOPT && TARGET_HARD_FLOAT && TARGET_FPRS)
26018 *total = rs6000_cost->fp;
26019 return false;
26021 break;
26023 case EQ:
26024 case GTU:
26025 case LTU:
26026 /* Carry bit requires mode == Pmode.
26027 NEG or PLUS already counted so only add one. */
26028 if (mode == Pmode
26029 && (outer_code == NEG || outer_code == PLUS))
26031 *total = COSTS_N_INSNS (1);
26032 return true;
26034 if (outer_code == SET)
26036 if (XEXP (x, 1) == const0_rtx)
26038 if (TARGET_ISEL && !TARGET_MFCRF)
26039 *total = COSTS_N_INSNS (8);
26040 else
26041 *total = COSTS_N_INSNS (2);
26042 return true;
26044 else if (mode == Pmode)
26046 *total = COSTS_N_INSNS (3);
26047 return false;
26050 /* FALLTHRU */
26052 case GT:
26053 case LT:
26054 case UNORDERED:
26055 if (outer_code == SET && (XEXP (x, 1) == const0_rtx))
26057 if (TARGET_ISEL && !TARGET_MFCRF)
26058 *total = COSTS_N_INSNS (8);
26059 else
26060 *total = COSTS_N_INSNS (2);
26061 return true;
26063 /* CC COMPARE. */
26064 if (outer_code == COMPARE)
26066 *total = 0;
26067 return true;
26069 break;
26071 default:
26072 break;
26075 return false;
26078 /* Debug form of r6000_rtx_costs that is selected if -mdebug=cost. */
26080 static bool
26081 rs6000_debug_rtx_costs (rtx x, int code, int outer_code, int opno, int *total,
26082 bool speed)
26084 bool ret = rs6000_rtx_costs (x, code, outer_code, opno, total, speed);
26086 fprintf (stderr,
26087 "\nrs6000_rtx_costs, return = %s, code = %s, outer_code = %s, "
26088 "opno = %d, total = %d, speed = %s, x:\n",
26089 ret ? "complete" : "scan inner",
26090 GET_RTX_NAME (code),
26091 GET_RTX_NAME (outer_code),
26092 opno,
26093 *total,
26094 speed ? "true" : "false");
26096 debug_rtx (x);
26098 return ret;
26101 /* Debug form of ADDRESS_COST that is selected if -mdebug=cost. */
26103 static int
26104 rs6000_debug_address_cost (rtx x, enum machine_mode mode,
26105 addr_space_t as, bool speed)
26107 int ret = TARGET_ADDRESS_COST (x, mode, as, speed);
26109 fprintf (stderr, "\nrs6000_address_cost, return = %d, speed = %s, x:\n",
26110 ret, speed ? "true" : "false");
26111 debug_rtx (x);
26113 return ret;
26117 /* A C expression returning the cost of moving data from a register of class
26118 CLASS1 to one of CLASS2. */
26120 static int
26121 rs6000_register_move_cost (enum machine_mode mode,
26122 reg_class_t from, reg_class_t to)
26124 int ret;
26126 if (TARGET_DEBUG_COST)
26127 dbg_cost_ctrl++;
26129 /* Moves from/to GENERAL_REGS. */
26130 if (reg_classes_intersect_p (to, GENERAL_REGS)
26131 || reg_classes_intersect_p (from, GENERAL_REGS))
26133 reg_class_t rclass = from;
26135 if (! reg_classes_intersect_p (to, GENERAL_REGS))
26136 rclass = to;
26138 if (rclass == FLOAT_REGS || rclass == ALTIVEC_REGS || rclass == VSX_REGS)
26139 ret = (rs6000_memory_move_cost (mode, rclass, false)
26140 + rs6000_memory_move_cost (mode, GENERAL_REGS, false));
26142 /* It's more expensive to move CR_REGS than CR0_REGS because of the
26143 shift. */
26144 else if (rclass == CR_REGS)
26145 ret = 4;
26147 /* For those processors that have slow LR/CTR moves, make them more
26148 expensive than memory in order to bias spills to memory .*/
26149 else if ((rs6000_cpu == PROCESSOR_POWER6
26150 || rs6000_cpu == PROCESSOR_POWER7)
26151 && reg_classes_intersect_p (rclass, LINK_OR_CTR_REGS))
26152 ret = 6 * hard_regno_nregs[0][mode];
26154 else
26155 /* A move will cost one instruction per GPR moved. */
26156 ret = 2 * hard_regno_nregs[0][mode];
26159 /* If we have VSX, we can easily move between FPR or Altivec registers. */
26160 else if (VECTOR_UNIT_VSX_P (mode)
26161 && reg_classes_intersect_p (to, VSX_REGS)
26162 && reg_classes_intersect_p (from, VSX_REGS))
26163 ret = 2 * hard_regno_nregs[32][mode];
26165 /* Moving between two similar registers is just one instruction. */
26166 else if (reg_classes_intersect_p (to, from))
26167 ret = (mode == TFmode || mode == TDmode) ? 4 : 2;
26169 /* Everything else has to go through GENERAL_REGS. */
26170 else
26171 ret = (rs6000_register_move_cost (mode, GENERAL_REGS, to)
26172 + rs6000_register_move_cost (mode, from, GENERAL_REGS));
26174 if (TARGET_DEBUG_COST)
26176 if (dbg_cost_ctrl == 1)
26177 fprintf (stderr,
26178 "rs6000_register_move_cost:, ret=%d, mode=%s, from=%s, to=%s\n",
26179 ret, GET_MODE_NAME (mode), reg_class_names[from],
26180 reg_class_names[to]);
26181 dbg_cost_ctrl--;
26184 return ret;
26187 /* A C expressions returning the cost of moving data of MODE from a register to
26188 or from memory. */
26190 static int
26191 rs6000_memory_move_cost (enum machine_mode mode, reg_class_t rclass,
26192 bool in ATTRIBUTE_UNUSED)
26194 int ret;
26196 if (TARGET_DEBUG_COST)
26197 dbg_cost_ctrl++;
26199 if (reg_classes_intersect_p (rclass, GENERAL_REGS))
26200 ret = 4 * hard_regno_nregs[0][mode];
26201 else if (reg_classes_intersect_p (rclass, FLOAT_REGS))
26202 ret = 4 * hard_regno_nregs[32][mode];
26203 else if (reg_classes_intersect_p (rclass, ALTIVEC_REGS))
26204 ret = 4 * hard_regno_nregs[FIRST_ALTIVEC_REGNO][mode];
26205 else
26206 ret = 4 + rs6000_register_move_cost (mode, rclass, GENERAL_REGS);
26208 if (TARGET_DEBUG_COST)
26210 if (dbg_cost_ctrl == 1)
26211 fprintf (stderr,
26212 "rs6000_memory_move_cost: ret=%d, mode=%s, rclass=%s, in=%d\n",
26213 ret, GET_MODE_NAME (mode), reg_class_names[rclass], in);
26214 dbg_cost_ctrl--;
26217 return ret;
26220 /* Returns a code for a target-specific builtin that implements
26221 reciprocal of the function, or NULL_TREE if not available. */
26223 static tree
26224 rs6000_builtin_reciprocal (unsigned int fn, bool md_fn,
26225 bool sqrt ATTRIBUTE_UNUSED)
26227 if (optimize_insn_for_size_p ())
26228 return NULL_TREE;
26230 if (md_fn)
26231 switch (fn)
26233 case VSX_BUILTIN_XVSQRTDP:
26234 if (!RS6000_RECIP_AUTO_RSQRTE_P (V2DFmode))
26235 return NULL_TREE;
26237 return rs6000_builtin_decls[VSX_BUILTIN_RSQRT_2DF];
26239 case VSX_BUILTIN_XVSQRTSP:
26240 if (!RS6000_RECIP_AUTO_RSQRTE_P (V4SFmode))
26241 return NULL_TREE;
26243 return rs6000_builtin_decls[VSX_BUILTIN_RSQRT_4SF];
26245 default:
26246 return NULL_TREE;
26249 else
26250 switch (fn)
26252 case BUILT_IN_SQRT:
26253 if (!RS6000_RECIP_AUTO_RSQRTE_P (DFmode))
26254 return NULL_TREE;
26256 return rs6000_builtin_decls[RS6000_BUILTIN_RSQRT];
26258 case BUILT_IN_SQRTF:
26259 if (!RS6000_RECIP_AUTO_RSQRTE_P (SFmode))
26260 return NULL_TREE;
26262 return rs6000_builtin_decls[RS6000_BUILTIN_RSQRTF];
26264 default:
26265 return NULL_TREE;
26269 /* Load up a constant. If the mode is a vector mode, splat the value across
26270 all of the vector elements. */
26272 static rtx
26273 rs6000_load_constant_and_splat (enum machine_mode mode, REAL_VALUE_TYPE dconst)
26275 rtx reg;
26277 if (mode == SFmode || mode == DFmode)
26279 rtx d = CONST_DOUBLE_FROM_REAL_VALUE (dconst, mode);
26280 reg = force_reg (mode, d);
26282 else if (mode == V4SFmode)
26284 rtx d = CONST_DOUBLE_FROM_REAL_VALUE (dconst, SFmode);
26285 rtvec v = gen_rtvec (4, d, d, d, d);
26286 reg = gen_reg_rtx (mode);
26287 rs6000_expand_vector_init (reg, gen_rtx_PARALLEL (mode, v));
26289 else if (mode == V2DFmode)
26291 rtx d = CONST_DOUBLE_FROM_REAL_VALUE (dconst, DFmode);
26292 rtvec v = gen_rtvec (2, d, d);
26293 reg = gen_reg_rtx (mode);
26294 rs6000_expand_vector_init (reg, gen_rtx_PARALLEL (mode, v));
26296 else
26297 gcc_unreachable ();
26299 return reg;
26302 /* Generate an FMA instruction. */
26304 static void
26305 rs6000_emit_madd (rtx target, rtx m1, rtx m2, rtx a)
26307 enum machine_mode mode = GET_MODE (target);
26308 rtx dst;
26310 dst = expand_ternary_op (mode, fma_optab, m1, m2, a, target, 0);
26311 gcc_assert (dst != NULL);
26313 if (dst != target)
26314 emit_move_insn (target, dst);
26317 /* Generate a FMSUB instruction: dst = fma(m1, m2, -a). */
26319 static void
26320 rs6000_emit_msub (rtx target, rtx m1, rtx m2, rtx a)
26322 enum machine_mode mode = GET_MODE (target);
26323 rtx dst;
26325 /* Altivec does not support fms directly;
26326 generate in terms of fma in that case. */
26327 if (optab_handler (fms_optab, mode) != CODE_FOR_nothing)
26328 dst = expand_ternary_op (mode, fms_optab, m1, m2, a, target, 0);
26329 else
26331 a = expand_unop (mode, neg_optab, a, NULL_RTX, 0);
26332 dst = expand_ternary_op (mode, fma_optab, m1, m2, a, target, 0);
26334 gcc_assert (dst != NULL);
26336 if (dst != target)
26337 emit_move_insn (target, dst);
26340 /* Generate a FNMSUB instruction: dst = -fma(m1, m2, -a). */
26342 static void
26343 rs6000_emit_nmsub (rtx dst, rtx m1, rtx m2, rtx a)
26345 enum machine_mode mode = GET_MODE (dst);
26346 rtx r;
26348 /* This is a tad more complicated, since the fnma_optab is for
26349 a different expression: fma(-m1, m2, a), which is the same
26350 thing except in the case of signed zeros.
26352 Fortunately we know that if FMA is supported that FNMSUB is
26353 also supported in the ISA. Just expand it directly. */
26355 gcc_assert (optab_handler (fma_optab, mode) != CODE_FOR_nothing);
26357 r = gen_rtx_NEG (mode, a);
26358 r = gen_rtx_FMA (mode, m1, m2, r);
26359 r = gen_rtx_NEG (mode, r);
26360 emit_insn (gen_rtx_SET (VOIDmode, dst, r));
26363 /* Newton-Raphson approximation of floating point divide with just 2 passes
26364 (either single precision floating point, or newer machines with higher
26365 accuracy estimates). Support both scalar and vector divide. Assumes no
26366 trapping math and finite arguments. */
26368 static void
26369 rs6000_emit_swdiv_high_precision (rtx dst, rtx n, rtx d)
26371 enum machine_mode mode = GET_MODE (dst);
26372 rtx x0, e0, e1, y1, u0, v0;
26373 enum insn_code code = optab_handler (smul_optab, mode);
26374 gen_2arg_fn_t gen_mul = (gen_2arg_fn_t) GEN_FCN (code);
26375 rtx one = rs6000_load_constant_and_splat (mode, dconst1);
26377 gcc_assert (code != CODE_FOR_nothing);
26379 /* x0 = 1./d estimate */
26380 x0 = gen_reg_rtx (mode);
26381 emit_insn (gen_rtx_SET (VOIDmode, x0,
26382 gen_rtx_UNSPEC (mode, gen_rtvec (1, d),
26383 UNSPEC_FRES)));
26385 e0 = gen_reg_rtx (mode);
26386 rs6000_emit_nmsub (e0, d, x0, one); /* e0 = 1. - (d * x0) */
26388 e1 = gen_reg_rtx (mode);
26389 rs6000_emit_madd (e1, e0, e0, e0); /* e1 = (e0 * e0) + e0 */
26391 y1 = gen_reg_rtx (mode);
26392 rs6000_emit_madd (y1, e1, x0, x0); /* y1 = (e1 * x0) + x0 */
26394 u0 = gen_reg_rtx (mode);
26395 emit_insn (gen_mul (u0, n, y1)); /* u0 = n * y1 */
26397 v0 = gen_reg_rtx (mode);
26398 rs6000_emit_nmsub (v0, d, u0, n); /* v0 = n - (d * u0) */
26400 rs6000_emit_madd (dst, v0, y1, u0); /* dst = (v0 * y1) + u0 */
26403 /* Newton-Raphson approximation of floating point divide that has a low
26404 precision estimate. Assumes no trapping math and finite arguments. */
26406 static void
26407 rs6000_emit_swdiv_low_precision (rtx dst, rtx n, rtx d)
26409 enum machine_mode mode = GET_MODE (dst);
26410 rtx x0, e0, e1, e2, y1, y2, y3, u0, v0, one;
26411 enum insn_code code = optab_handler (smul_optab, mode);
26412 gen_2arg_fn_t gen_mul = (gen_2arg_fn_t) GEN_FCN (code);
26414 gcc_assert (code != CODE_FOR_nothing);
26416 one = rs6000_load_constant_and_splat (mode, dconst1);
26418 /* x0 = 1./d estimate */
26419 x0 = gen_reg_rtx (mode);
26420 emit_insn (gen_rtx_SET (VOIDmode, x0,
26421 gen_rtx_UNSPEC (mode, gen_rtvec (1, d),
26422 UNSPEC_FRES)));
26424 e0 = gen_reg_rtx (mode);
26425 rs6000_emit_nmsub (e0, d, x0, one); /* e0 = 1. - d * x0 */
26427 y1 = gen_reg_rtx (mode);
26428 rs6000_emit_madd (y1, e0, x0, x0); /* y1 = x0 + e0 * x0 */
26430 e1 = gen_reg_rtx (mode);
26431 emit_insn (gen_mul (e1, e0, e0)); /* e1 = e0 * e0 */
26433 y2 = gen_reg_rtx (mode);
26434 rs6000_emit_madd (y2, e1, y1, y1); /* y2 = y1 + e1 * y1 */
26436 e2 = gen_reg_rtx (mode);
26437 emit_insn (gen_mul (e2, e1, e1)); /* e2 = e1 * e1 */
26439 y3 = gen_reg_rtx (mode);
26440 rs6000_emit_madd (y3, e2, y2, y2); /* y3 = y2 + e2 * y2 */
26442 u0 = gen_reg_rtx (mode);
26443 emit_insn (gen_mul (u0, n, y3)); /* u0 = n * y3 */
26445 v0 = gen_reg_rtx (mode);
26446 rs6000_emit_nmsub (v0, d, u0, n); /* v0 = n - d * u0 */
26448 rs6000_emit_madd (dst, v0, y3, u0); /* dst = u0 + v0 * y3 */
26451 /* Newton-Raphson approximation of floating point divide DST = N/D. If NOTE_P,
26452 add a reg_note saying that this was a division. Support both scalar and
26453 vector divide. Assumes no trapping math and finite arguments. */
26455 void
26456 rs6000_emit_swdiv (rtx dst, rtx n, rtx d, bool note_p)
26458 enum machine_mode mode = GET_MODE (dst);
26460 if (RS6000_RECIP_HIGH_PRECISION_P (mode))
26461 rs6000_emit_swdiv_high_precision (dst, n, d);
26462 else
26463 rs6000_emit_swdiv_low_precision (dst, n, d);
26465 if (note_p)
26466 add_reg_note (get_last_insn (), REG_EQUAL, gen_rtx_DIV (mode, n, d));
26469 /* Newton-Raphson approximation of single/double-precision floating point
26470 rsqrt. Assumes no trapping math and finite arguments. */
26472 void
26473 rs6000_emit_swrsqrt (rtx dst, rtx src)
26475 enum machine_mode mode = GET_MODE (src);
26476 rtx x0 = gen_reg_rtx (mode);
26477 rtx y = gen_reg_rtx (mode);
26478 int passes = (TARGET_RECIP_PRECISION) ? 2 : 3;
26479 REAL_VALUE_TYPE dconst3_2;
26480 int i;
26481 rtx halfthree;
26482 enum insn_code code = optab_handler (smul_optab, mode);
26483 gen_2arg_fn_t gen_mul = (gen_2arg_fn_t) GEN_FCN (code);
26485 gcc_assert (code != CODE_FOR_nothing);
26487 /* Load up the constant 1.5 either as a scalar, or as a vector. */
26488 real_from_integer (&dconst3_2, VOIDmode, 3, 0, 0);
26489 SET_REAL_EXP (&dconst3_2, REAL_EXP (&dconst3_2) - 1);
26491 halfthree = rs6000_load_constant_and_splat (mode, dconst3_2);
26493 /* x0 = rsqrt estimate */
26494 emit_insn (gen_rtx_SET (VOIDmode, x0,
26495 gen_rtx_UNSPEC (mode, gen_rtvec (1, src),
26496 UNSPEC_RSQRT)));
26498 /* y = 0.5 * src = 1.5 * src - src -> fewer constants */
26499 rs6000_emit_msub (y, src, halfthree, src);
26501 for (i = 0; i < passes; i++)
26503 rtx x1 = gen_reg_rtx (mode);
26504 rtx u = gen_reg_rtx (mode);
26505 rtx v = gen_reg_rtx (mode);
26507 /* x1 = x0 * (1.5 - y * (x0 * x0)) */
26508 emit_insn (gen_mul (u, x0, x0));
26509 rs6000_emit_nmsub (v, y, u, halfthree);
26510 emit_insn (gen_mul (x1, x0, v));
26511 x0 = x1;
26514 emit_move_insn (dst, x0);
26515 return;
26518 /* Emit popcount intrinsic on TARGET_POPCNTB (Power5) and TARGET_POPCNTD
26519 (Power7) targets. DST is the target, and SRC is the argument operand. */
26521 void
26522 rs6000_emit_popcount (rtx dst, rtx src)
26524 enum machine_mode mode = GET_MODE (dst);
26525 rtx tmp1, tmp2;
26527 /* Use the PPC ISA 2.06 popcnt{w,d} instruction if we can. */
26528 if (TARGET_POPCNTD)
26530 if (mode == SImode)
26531 emit_insn (gen_popcntdsi2 (dst, src));
26532 else
26533 emit_insn (gen_popcntddi2 (dst, src));
26534 return;
26537 tmp1 = gen_reg_rtx (mode);
26539 if (mode == SImode)
26541 emit_insn (gen_popcntbsi2 (tmp1, src));
26542 tmp2 = expand_mult (SImode, tmp1, GEN_INT (0x01010101),
26543 NULL_RTX, 0);
26544 tmp2 = force_reg (SImode, tmp2);
26545 emit_insn (gen_lshrsi3 (dst, tmp2, GEN_INT (24)));
26547 else
26549 emit_insn (gen_popcntbdi2 (tmp1, src));
26550 tmp2 = expand_mult (DImode, tmp1,
26551 GEN_INT ((HOST_WIDE_INT)
26552 0x01010101 << 32 | 0x01010101),
26553 NULL_RTX, 0);
26554 tmp2 = force_reg (DImode, tmp2);
26555 emit_insn (gen_lshrdi3 (dst, tmp2, GEN_INT (56)));
26560 /* Emit parity intrinsic on TARGET_POPCNTB targets. DST is the
26561 target, and SRC is the argument operand. */
26563 void
26564 rs6000_emit_parity (rtx dst, rtx src)
26566 enum machine_mode mode = GET_MODE (dst);
26567 rtx tmp;
26569 tmp = gen_reg_rtx (mode);
26571 /* Use the PPC ISA 2.05 prtyw/prtyd instruction if we can. */
26572 if (TARGET_CMPB)
26574 if (mode == SImode)
26576 emit_insn (gen_popcntbsi2 (tmp, src));
26577 emit_insn (gen_paritysi2_cmpb (dst, tmp));
26579 else
26581 emit_insn (gen_popcntbdi2 (tmp, src));
26582 emit_insn (gen_paritydi2_cmpb (dst, tmp));
26584 return;
26587 if (mode == SImode)
26589 /* Is mult+shift >= shift+xor+shift+xor? */
26590 if (rs6000_cost->mulsi_const >= COSTS_N_INSNS (3))
26592 rtx tmp1, tmp2, tmp3, tmp4;
26594 tmp1 = gen_reg_rtx (SImode);
26595 emit_insn (gen_popcntbsi2 (tmp1, src));
26597 tmp2 = gen_reg_rtx (SImode);
26598 emit_insn (gen_lshrsi3 (tmp2, tmp1, GEN_INT (16)));
26599 tmp3 = gen_reg_rtx (SImode);
26600 emit_insn (gen_xorsi3 (tmp3, tmp1, tmp2));
26602 tmp4 = gen_reg_rtx (SImode);
26603 emit_insn (gen_lshrsi3 (tmp4, tmp3, GEN_INT (8)));
26604 emit_insn (gen_xorsi3 (tmp, tmp3, tmp4));
26606 else
26607 rs6000_emit_popcount (tmp, src);
26608 emit_insn (gen_andsi3 (dst, tmp, const1_rtx));
26610 else
26612 /* Is mult+shift >= shift+xor+shift+xor+shift+xor? */
26613 if (rs6000_cost->muldi >= COSTS_N_INSNS (5))
26615 rtx tmp1, tmp2, tmp3, tmp4, tmp5, tmp6;
26617 tmp1 = gen_reg_rtx (DImode);
26618 emit_insn (gen_popcntbdi2 (tmp1, src));
26620 tmp2 = gen_reg_rtx (DImode);
26621 emit_insn (gen_lshrdi3 (tmp2, tmp1, GEN_INT (32)));
26622 tmp3 = gen_reg_rtx (DImode);
26623 emit_insn (gen_xordi3 (tmp3, tmp1, tmp2));
26625 tmp4 = gen_reg_rtx (DImode);
26626 emit_insn (gen_lshrdi3 (tmp4, tmp3, GEN_INT (16)));
26627 tmp5 = gen_reg_rtx (DImode);
26628 emit_insn (gen_xordi3 (tmp5, tmp3, tmp4));
26630 tmp6 = gen_reg_rtx (DImode);
26631 emit_insn (gen_lshrdi3 (tmp6, tmp5, GEN_INT (8)));
26632 emit_insn (gen_xordi3 (tmp, tmp5, tmp6));
26634 else
26635 rs6000_emit_popcount (tmp, src);
26636 emit_insn (gen_anddi3 (dst, tmp, const1_rtx));
26640 /* Expand an Altivec constant permutation. Return true if we match
26641 an efficient implementation; false to fall back to VPERM. */
26643 bool
26644 altivec_expand_vec_perm_const (rtx operands[4])
26646 struct altivec_perm_insn {
26647 enum insn_code impl;
26648 unsigned char perm[16];
26650 static const struct altivec_perm_insn patterns[] = {
26651 { CODE_FOR_altivec_vpkuhum,
26652 { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31 } },
26653 { CODE_FOR_altivec_vpkuwum,
26654 { 2, 3, 6, 7, 10, 11, 14, 15, 18, 19, 22, 23, 26, 27, 30, 31 } },
26655 { CODE_FOR_altivec_vmrghb,
26656 { 0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23 } },
26657 { CODE_FOR_altivec_vmrghh,
26658 { 0, 1, 16, 17, 2, 3, 18, 19, 4, 5, 20, 21, 6, 7, 22, 23 } },
26659 { CODE_FOR_altivec_vmrghw,
26660 { 0, 1, 2, 3, 16, 17, 18, 19, 4, 5, 6, 7, 20, 21, 22, 23 } },
26661 { CODE_FOR_altivec_vmrglb,
26662 { 8, 24, 9, 25, 10, 26, 11, 27, 12, 28, 13, 29, 14, 30, 15, 31 } },
26663 { CODE_FOR_altivec_vmrglh,
26664 { 8, 9, 24, 25, 10, 11, 26, 27, 12, 13, 28, 29, 14, 15, 30, 31 } },
26665 { CODE_FOR_altivec_vmrglw,
26666 { 8, 9, 10, 11, 24, 25, 26, 27, 12, 13, 14, 15, 28, 29, 30, 31 } }
26669 unsigned int i, j, elt, which;
26670 unsigned char perm[16];
26671 rtx target, op0, op1, sel, x;
26672 bool one_vec;
26674 target = operands[0];
26675 op0 = operands[1];
26676 op1 = operands[2];
26677 sel = operands[3];
26679 /* Unpack the constant selector. */
26680 for (i = which = 0; i < 16; ++i)
26682 rtx e = XVECEXP (sel, 0, i);
26683 elt = INTVAL (e) & 31;
26684 which |= (elt < 16 ? 1 : 2);
26685 perm[i] = elt;
26688 /* Simplify the constant selector based on operands. */
26689 switch (which)
26691 default:
26692 gcc_unreachable ();
26694 case 3:
26695 one_vec = false;
26696 if (!rtx_equal_p (op0, op1))
26697 break;
26698 /* FALLTHRU */
26700 case 2:
26701 for (i = 0; i < 16; ++i)
26702 perm[i] &= 15;
26703 op0 = op1;
26704 one_vec = true;
26705 break;
26707 case 1:
26708 op1 = op0;
26709 one_vec = true;
26710 break;
26713 /* Look for splat patterns. */
26714 if (one_vec)
26716 elt = perm[0];
26718 for (i = 0; i < 16; ++i)
26719 if (perm[i] != elt)
26720 break;
26721 if (i == 16)
26723 emit_insn (gen_altivec_vspltb (target, op0, GEN_INT (elt)));
26724 return true;
26727 if (elt % 2 == 0)
26729 for (i = 0; i < 16; i += 2)
26730 if (perm[i] != elt || perm[i + 1] != elt + 1)
26731 break;
26732 if (i == 16)
26734 x = gen_reg_rtx (V8HImode);
26735 emit_insn (gen_altivec_vsplth (x, gen_lowpart (V8HImode, op0),
26736 GEN_INT (elt / 2)));
26737 emit_move_insn (target, gen_lowpart (V16QImode, x));
26738 return true;
26742 if (elt % 4 == 0)
26744 for (i = 0; i < 16; i += 4)
26745 if (perm[i] != elt
26746 || perm[i + 1] != elt + 1
26747 || perm[i + 2] != elt + 2
26748 || perm[i + 3] != elt + 3)
26749 break;
26750 if (i == 16)
26752 x = gen_reg_rtx (V4SImode);
26753 emit_insn (gen_altivec_vspltw (x, gen_lowpart (V4SImode, op0),
26754 GEN_INT (elt / 4)));
26755 emit_move_insn (target, gen_lowpart (V16QImode, x));
26756 return true;
26761 /* Look for merge and pack patterns. */
26762 for (j = 0; j < ARRAY_SIZE (patterns); ++j)
26764 bool swapped;
26766 elt = patterns[j].perm[0];
26767 if (perm[0] == elt)
26768 swapped = false;
26769 else if (perm[0] == elt + 16)
26770 swapped = true;
26771 else
26772 continue;
26773 for (i = 1; i < 16; ++i)
26775 elt = patterns[j].perm[i];
26776 if (swapped)
26777 elt = (elt >= 16 ? elt - 16 : elt + 16);
26778 else if (one_vec && elt >= 16)
26779 elt -= 16;
26780 if (perm[i] != elt)
26781 break;
26783 if (i == 16)
26785 enum insn_code icode = patterns[j].impl;
26786 enum machine_mode omode = insn_data[icode].operand[0].mode;
26787 enum machine_mode imode = insn_data[icode].operand[1].mode;
26789 if (swapped)
26790 x = op0, op0 = op1, op1 = x;
26791 if (imode != V16QImode)
26793 op0 = gen_lowpart (imode, op0);
26794 op1 = gen_lowpart (imode, op1);
26796 if (omode == V16QImode)
26797 x = target;
26798 else
26799 x = gen_reg_rtx (omode);
26800 emit_insn (GEN_FCN (icode) (x, op0, op1));
26801 if (omode != V16QImode)
26802 emit_move_insn (target, gen_lowpart (V16QImode, x));
26803 return true;
26807 return false;
26810 /* Expand a Paired Single, VSX Permute Doubleword, or SPE constant permutation.
26811 Return true if we match an efficient implementation. */
26813 static bool
26814 rs6000_expand_vec_perm_const_1 (rtx target, rtx op0, rtx op1,
26815 unsigned char perm0, unsigned char perm1)
26817 rtx x;
26819 /* If both selectors come from the same operand, fold to single op. */
26820 if ((perm0 & 2) == (perm1 & 2))
26822 if (perm0 & 2)
26823 op0 = op1;
26824 else
26825 op1 = op0;
26827 /* If both operands are equal, fold to simpler permutation. */
26828 if (rtx_equal_p (op0, op1))
26830 perm0 = perm0 & 1;
26831 perm1 = (perm1 & 1) + 2;
26833 /* If the first selector comes from the second operand, swap. */
26834 else if (perm0 & 2)
26836 if (perm1 & 2)
26837 return false;
26838 perm0 -= 2;
26839 perm1 += 2;
26840 x = op0, op0 = op1, op1 = x;
26842 /* If the second selector does not come from the second operand, fail. */
26843 else if ((perm1 & 2) == 0)
26844 return false;
26846 /* Success! */
26847 if (target != NULL)
26849 enum machine_mode vmode, dmode;
26850 rtvec v;
26852 vmode = GET_MODE (target);
26853 gcc_assert (GET_MODE_NUNITS (vmode) == 2);
26854 dmode = mode_for_vector (GET_MODE_INNER (vmode), 4);
26856 x = gen_rtx_VEC_CONCAT (dmode, op0, op1);
26857 v = gen_rtvec (2, GEN_INT (perm0), GEN_INT (perm1));
26858 x = gen_rtx_VEC_SELECT (vmode, x, gen_rtx_PARALLEL (VOIDmode, v));
26859 emit_insn (gen_rtx_SET (VOIDmode, target, x));
26861 return true;
26864 bool
26865 rs6000_expand_vec_perm_const (rtx operands[4])
26867 rtx target, op0, op1, sel;
26868 unsigned char perm0, perm1;
26870 target = operands[0];
26871 op0 = operands[1];
26872 op1 = operands[2];
26873 sel = operands[3];
26875 /* Unpack the constant selector. */
26876 perm0 = INTVAL (XVECEXP (sel, 0, 0)) & 3;
26877 perm1 = INTVAL (XVECEXP (sel, 0, 1)) & 3;
26879 return rs6000_expand_vec_perm_const_1 (target, op0, op1, perm0, perm1);
26882 /* Test whether a constant permutation is supported. */
26884 static bool
26885 rs6000_vectorize_vec_perm_const_ok (enum machine_mode vmode,
26886 const unsigned char *sel)
26888 /* AltiVec (and thus VSX) can handle arbitrary permutations. */
26889 if (TARGET_ALTIVEC)
26890 return true;
26892 /* Check for ps_merge* or evmerge* insns. */
26893 if ((TARGET_PAIRED_FLOAT && vmode == V2SFmode)
26894 || (TARGET_SPE && vmode == V2SImode))
26896 rtx op0 = gen_raw_REG (vmode, LAST_VIRTUAL_REGISTER + 1);
26897 rtx op1 = gen_raw_REG (vmode, LAST_VIRTUAL_REGISTER + 2);
26898 return rs6000_expand_vec_perm_const_1 (NULL, op0, op1, sel[0], sel[1]);
26901 return false;
26904 /* A subroutine for rs6000_expand_extract_even & rs6000_expand_interleave. */
26906 static void
26907 rs6000_do_expand_vec_perm (rtx target, rtx op0, rtx op1,
26908 enum machine_mode vmode, unsigned nelt, rtx perm[])
26910 enum machine_mode imode;
26911 rtx x;
26913 imode = vmode;
26914 if (GET_MODE_CLASS (vmode) != MODE_VECTOR_INT)
26916 imode = GET_MODE_INNER (vmode);
26917 imode = mode_for_size (GET_MODE_BITSIZE (imode), MODE_INT, 0);
26918 imode = mode_for_vector (imode, nelt);
26921 x = gen_rtx_CONST_VECTOR (imode, gen_rtvec_v (nelt, perm));
26922 x = expand_vec_perm (vmode, op0, op1, x, target);
26923 if (x != target)
26924 emit_move_insn (target, x);
26927 /* Expand an extract even operation. */
26929 void
26930 rs6000_expand_extract_even (rtx target, rtx op0, rtx op1)
26932 enum machine_mode vmode = GET_MODE (target);
26933 unsigned i, nelt = GET_MODE_NUNITS (vmode);
26934 rtx perm[16];
26936 for (i = 0; i < nelt; i++)
26937 perm[i] = GEN_INT (i * 2);
26939 rs6000_do_expand_vec_perm (target, op0, op1, vmode, nelt, perm);
26942 /* Expand a vector interleave operation. */
26944 void
26945 rs6000_expand_interleave (rtx target, rtx op0, rtx op1, bool highp)
26947 enum machine_mode vmode = GET_MODE (target);
26948 unsigned i, high, nelt = GET_MODE_NUNITS (vmode);
26949 rtx perm[16];
26951 high = (highp == BYTES_BIG_ENDIAN ? 0 : nelt / 2);
26952 for (i = 0; i < nelt / 2; i++)
26954 perm[i * 2] = GEN_INT (i + high);
26955 perm[i * 2 + 1] = GEN_INT (i + nelt + high);
26958 rs6000_do_expand_vec_perm (target, op0, op1, vmode, nelt, perm);
26961 /* Return an RTX representing where to find the function value of a
26962 function returning MODE. */
26963 static rtx
26964 rs6000_complex_function_value (enum machine_mode mode)
26966 unsigned int regno;
26967 rtx r1, r2;
26968 enum machine_mode inner = GET_MODE_INNER (mode);
26969 unsigned int inner_bytes = GET_MODE_SIZE (inner);
26971 if (FLOAT_MODE_P (mode) && TARGET_HARD_FLOAT && TARGET_FPRS)
26972 regno = FP_ARG_RETURN;
26973 else
26975 regno = GP_ARG_RETURN;
26977 /* 32-bit is OK since it'll go in r3/r4. */
26978 if (TARGET_32BIT && inner_bytes >= 4)
26979 return gen_rtx_REG (mode, regno);
26982 if (inner_bytes >= 8)
26983 return gen_rtx_REG (mode, regno);
26985 r1 = gen_rtx_EXPR_LIST (inner, gen_rtx_REG (inner, regno),
26986 const0_rtx);
26987 r2 = gen_rtx_EXPR_LIST (inner, gen_rtx_REG (inner, regno + 1),
26988 GEN_INT (inner_bytes));
26989 return gen_rtx_PARALLEL (mode, gen_rtvec (2, r1, r2));
26992 /* Target hook for TARGET_FUNCTION_VALUE.
26994 On the SPE, both FPs and vectors are returned in r3.
26996 On RS/6000 an integer value is in r3 and a floating-point value is in
26997 fp1, unless -msoft-float. */
26999 static rtx
27000 rs6000_function_value (const_tree valtype,
27001 const_tree fn_decl_or_type ATTRIBUTE_UNUSED,
27002 bool outgoing ATTRIBUTE_UNUSED)
27004 enum machine_mode mode;
27005 unsigned int regno;
27007 /* Special handling for structs in darwin64. */
27008 if (TARGET_MACHO
27009 && rs6000_darwin64_struct_check_p (TYPE_MODE (valtype), valtype))
27011 CUMULATIVE_ARGS valcum;
27012 rtx valret;
27014 valcum.words = 0;
27015 valcum.fregno = FP_ARG_MIN_REG;
27016 valcum.vregno = ALTIVEC_ARG_MIN_REG;
27017 /* Do a trial code generation as if this were going to be passed as
27018 an argument; if any part goes in memory, we return NULL. */
27019 valret = rs6000_darwin64_record_arg (&valcum, valtype, true, /* retval= */ true);
27020 if (valret)
27021 return valret;
27022 /* Otherwise fall through to standard ABI rules. */
27025 if (TARGET_32BIT && TARGET_POWERPC64 && TYPE_MODE (valtype) == DImode)
27027 /* Long long return value need be split in -mpowerpc64, 32bit ABI. */
27028 return gen_rtx_PARALLEL (DImode,
27029 gen_rtvec (2,
27030 gen_rtx_EXPR_LIST (VOIDmode,
27031 gen_rtx_REG (SImode, GP_ARG_RETURN),
27032 const0_rtx),
27033 gen_rtx_EXPR_LIST (VOIDmode,
27034 gen_rtx_REG (SImode,
27035 GP_ARG_RETURN + 1),
27036 GEN_INT (4))));
27038 if (TARGET_32BIT && TARGET_POWERPC64 && TYPE_MODE (valtype) == DCmode)
27040 return gen_rtx_PARALLEL (DCmode,
27041 gen_rtvec (4,
27042 gen_rtx_EXPR_LIST (VOIDmode,
27043 gen_rtx_REG (SImode, GP_ARG_RETURN),
27044 const0_rtx),
27045 gen_rtx_EXPR_LIST (VOIDmode,
27046 gen_rtx_REG (SImode,
27047 GP_ARG_RETURN + 1),
27048 GEN_INT (4)),
27049 gen_rtx_EXPR_LIST (VOIDmode,
27050 gen_rtx_REG (SImode,
27051 GP_ARG_RETURN + 2),
27052 GEN_INT (8)),
27053 gen_rtx_EXPR_LIST (VOIDmode,
27054 gen_rtx_REG (SImode,
27055 GP_ARG_RETURN + 3),
27056 GEN_INT (12))));
27059 mode = TYPE_MODE (valtype);
27060 if ((INTEGRAL_TYPE_P (valtype) && GET_MODE_BITSIZE (mode) < BITS_PER_WORD)
27061 || POINTER_TYPE_P (valtype))
27062 mode = TARGET_32BIT ? SImode : DImode;
27064 if (DECIMAL_FLOAT_MODE_P (mode) && TARGET_HARD_FLOAT && TARGET_FPRS)
27065 /* _Decimal128 must use an even/odd register pair. */
27066 regno = (mode == TDmode) ? FP_ARG_RETURN + 1 : FP_ARG_RETURN;
27067 else if (SCALAR_FLOAT_TYPE_P (valtype) && TARGET_HARD_FLOAT && TARGET_FPRS
27068 && ((TARGET_SINGLE_FLOAT && (mode == SFmode)) || TARGET_DOUBLE_FLOAT))
27069 regno = FP_ARG_RETURN;
27070 else if (TREE_CODE (valtype) == COMPLEX_TYPE
27071 && targetm.calls.split_complex_arg)
27072 return rs6000_complex_function_value (mode);
27073 /* VSX is a superset of Altivec and adds V2DImode/V2DFmode. Since the same
27074 return register is used in both cases, and we won't see V2DImode/V2DFmode
27075 for pure altivec, combine the two cases. */
27076 else if (TREE_CODE (valtype) == VECTOR_TYPE
27077 && TARGET_ALTIVEC && TARGET_ALTIVEC_ABI
27078 && ALTIVEC_OR_VSX_VECTOR_MODE (mode))
27079 regno = ALTIVEC_ARG_RETURN;
27080 else if (TARGET_E500_DOUBLE && TARGET_HARD_FLOAT
27081 && (mode == DFmode || mode == DCmode
27082 || mode == TFmode || mode == TCmode))
27083 return spe_build_register_parallel (mode, GP_ARG_RETURN);
27084 else
27085 regno = GP_ARG_RETURN;
27087 return gen_rtx_REG (mode, regno);
27090 /* Define how to find the value returned by a library function
27091 assuming the value has mode MODE. */
27093 rs6000_libcall_value (enum machine_mode mode)
27095 unsigned int regno;
27097 if (TARGET_32BIT && TARGET_POWERPC64 && mode == DImode)
27099 /* Long long return value need be split in -mpowerpc64, 32bit ABI. */
27100 return gen_rtx_PARALLEL (DImode,
27101 gen_rtvec (2,
27102 gen_rtx_EXPR_LIST (VOIDmode,
27103 gen_rtx_REG (SImode, GP_ARG_RETURN),
27104 const0_rtx),
27105 gen_rtx_EXPR_LIST (VOIDmode,
27106 gen_rtx_REG (SImode,
27107 GP_ARG_RETURN + 1),
27108 GEN_INT (4))));
27111 if (DECIMAL_FLOAT_MODE_P (mode) && TARGET_HARD_FLOAT && TARGET_FPRS)
27112 /* _Decimal128 must use an even/odd register pair. */
27113 regno = (mode == TDmode) ? FP_ARG_RETURN + 1 : FP_ARG_RETURN;
27114 else if (SCALAR_FLOAT_MODE_P (mode)
27115 && TARGET_HARD_FLOAT && TARGET_FPRS
27116 && ((TARGET_SINGLE_FLOAT && mode == SFmode) || TARGET_DOUBLE_FLOAT))
27117 regno = FP_ARG_RETURN;
27118 /* VSX is a superset of Altivec and adds V2DImode/V2DFmode. Since the same
27119 return register is used in both cases, and we won't see V2DImode/V2DFmode
27120 for pure altivec, combine the two cases. */
27121 else if (ALTIVEC_OR_VSX_VECTOR_MODE (mode)
27122 && TARGET_ALTIVEC && TARGET_ALTIVEC_ABI)
27123 regno = ALTIVEC_ARG_RETURN;
27124 else if (COMPLEX_MODE_P (mode) && targetm.calls.split_complex_arg)
27125 return rs6000_complex_function_value (mode);
27126 else if (TARGET_E500_DOUBLE && TARGET_HARD_FLOAT
27127 && (mode == DFmode || mode == DCmode
27128 || mode == TFmode || mode == TCmode))
27129 return spe_build_register_parallel (mode, GP_ARG_RETURN);
27130 else
27131 regno = GP_ARG_RETURN;
27133 return gen_rtx_REG (mode, regno);
27137 /* Given FROM and TO register numbers, say whether this elimination is allowed.
27138 Frame pointer elimination is automatically handled.
27140 For the RS/6000, if frame pointer elimination is being done, we would like
27141 to convert ap into fp, not sp.
27143 We need r30 if -mminimal-toc was specified, and there are constant pool
27144 references. */
27146 static bool
27147 rs6000_can_eliminate (const int from, const int to)
27149 return (from == ARG_POINTER_REGNUM && to == STACK_POINTER_REGNUM
27150 ? ! frame_pointer_needed
27151 : from == RS6000_PIC_OFFSET_TABLE_REGNUM
27152 ? ! TARGET_MINIMAL_TOC || TARGET_NO_TOC || get_pool_size () == 0
27153 : true);
27156 /* Define the offset between two registers, FROM to be eliminated and its
27157 replacement TO, at the start of a routine. */
27158 HOST_WIDE_INT
27159 rs6000_initial_elimination_offset (int from, int to)
27161 rs6000_stack_t *info = rs6000_stack_info ();
27162 HOST_WIDE_INT offset;
27164 if (from == HARD_FRAME_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
27165 offset = info->push_p ? 0 : -info->total_size;
27166 else if (from == FRAME_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
27168 offset = info->push_p ? 0 : -info->total_size;
27169 if (FRAME_GROWS_DOWNWARD)
27170 offset += info->fixed_size + info->vars_size + info->parm_size;
27172 else if (from == FRAME_POINTER_REGNUM && to == HARD_FRAME_POINTER_REGNUM)
27173 offset = FRAME_GROWS_DOWNWARD
27174 ? info->fixed_size + info->vars_size + info->parm_size
27175 : 0;
27176 else if (from == ARG_POINTER_REGNUM && to == HARD_FRAME_POINTER_REGNUM)
27177 offset = info->total_size;
27178 else if (from == ARG_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
27179 offset = info->push_p ? info->total_size : 0;
27180 else if (from == RS6000_PIC_OFFSET_TABLE_REGNUM)
27181 offset = 0;
27182 else
27183 gcc_unreachable ();
27185 return offset;
27188 static rtx
27189 rs6000_dwarf_register_span (rtx reg)
27191 rtx parts[8];
27192 int i, words;
27193 unsigned regno = REGNO (reg);
27194 enum machine_mode mode = GET_MODE (reg);
27196 if (TARGET_SPE
27197 && regno < 32
27198 && (SPE_VECTOR_MODE (GET_MODE (reg))
27199 || (TARGET_E500_DOUBLE && FLOAT_MODE_P (mode)
27200 && mode != SFmode && mode != SDmode && mode != SCmode)))
27202 else
27203 return NULL_RTX;
27205 regno = REGNO (reg);
27207 /* The duality of the SPE register size wreaks all kinds of havoc.
27208 This is a way of distinguishing r0 in 32-bits from r0 in
27209 64-bits. */
27210 words = (GET_MODE_SIZE (mode) + UNITS_PER_FP_WORD - 1) / UNITS_PER_FP_WORD;
27211 gcc_assert (words <= 4);
27212 for (i = 0; i < words; i++, regno++)
27214 if (BYTES_BIG_ENDIAN)
27216 parts[2 * i] = gen_rtx_REG (SImode, regno + 1200);
27217 parts[2 * i + 1] = gen_rtx_REG (SImode, regno);
27219 else
27221 parts[2 * i] = gen_rtx_REG (SImode, regno);
27222 parts[2 * i + 1] = gen_rtx_REG (SImode, regno + 1200);
27226 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (words * 2, parts));
27229 /* Fill in sizes for SPE register high parts in table used by unwinder. */
27231 static void
27232 rs6000_init_dwarf_reg_sizes_extra (tree address)
27234 if (TARGET_SPE)
27236 int i;
27237 enum machine_mode mode = TYPE_MODE (char_type_node);
27238 rtx addr = expand_expr (address, NULL_RTX, VOIDmode, EXPAND_NORMAL);
27239 rtx mem = gen_rtx_MEM (BLKmode, addr);
27240 rtx value = gen_int_mode (4, mode);
27242 for (i = 1201; i < 1232; i++)
27244 int column = DWARF_REG_TO_UNWIND_COLUMN (i);
27245 HOST_WIDE_INT offset
27246 = DWARF_FRAME_REGNUM (column) * GET_MODE_SIZE (mode);
27248 emit_move_insn (adjust_address (mem, mode, offset), value);
27253 /* Map internal gcc register numbers to DWARF2 register numbers. */
27255 unsigned int
27256 rs6000_dbx_register_number (unsigned int regno)
27258 if (regno <= 63 || write_symbols != DWARF2_DEBUG)
27259 return regno;
27260 if (regno == LR_REGNO)
27261 return 108;
27262 if (regno == CTR_REGNO)
27263 return 109;
27264 if (CR_REGNO_P (regno))
27265 return regno - CR0_REGNO + 86;
27266 if (regno == CA_REGNO)
27267 return 101; /* XER */
27268 if (ALTIVEC_REGNO_P (regno))
27269 return regno - FIRST_ALTIVEC_REGNO + 1124;
27270 if (regno == VRSAVE_REGNO)
27271 return 356;
27272 if (regno == VSCR_REGNO)
27273 return 67;
27274 if (regno == SPE_ACC_REGNO)
27275 return 99;
27276 if (regno == SPEFSCR_REGNO)
27277 return 612;
27278 /* SPE high reg number. We get these values of regno from
27279 rs6000_dwarf_register_span. */
27280 gcc_assert (regno >= 1200 && regno < 1232);
27281 return regno;
27284 /* target hook eh_return_filter_mode */
27285 static enum machine_mode
27286 rs6000_eh_return_filter_mode (void)
27288 return TARGET_32BIT ? SImode : word_mode;
27291 /* Target hook for scalar_mode_supported_p. */
27292 static bool
27293 rs6000_scalar_mode_supported_p (enum machine_mode mode)
27295 if (DECIMAL_FLOAT_MODE_P (mode))
27296 return default_decimal_float_supported_p ();
27297 else
27298 return default_scalar_mode_supported_p (mode);
27301 /* Target hook for vector_mode_supported_p. */
27302 static bool
27303 rs6000_vector_mode_supported_p (enum machine_mode mode)
27306 if (TARGET_PAIRED_FLOAT && PAIRED_VECTOR_MODE (mode))
27307 return true;
27309 if (TARGET_SPE && SPE_VECTOR_MODE (mode))
27310 return true;
27312 else if (VECTOR_MEM_ALTIVEC_OR_VSX_P (mode))
27313 return true;
27315 else
27316 return false;
27319 /* Target hook for invalid_arg_for_unprototyped_fn. */
27320 static const char *
27321 invalid_arg_for_unprototyped_fn (const_tree typelist, const_tree funcdecl, const_tree val)
27323 return (!rs6000_darwin64_abi
27324 && typelist == 0
27325 && TREE_CODE (TREE_TYPE (val)) == VECTOR_TYPE
27326 && (funcdecl == NULL_TREE
27327 || (TREE_CODE (funcdecl) == FUNCTION_DECL
27328 && DECL_BUILT_IN_CLASS (funcdecl) != BUILT_IN_MD)))
27329 ? N_("AltiVec argument passed to unprototyped function")
27330 : NULL;
27333 /* For TARGET_SECURE_PLT 32-bit PIC code we can save PIC register
27334 setup by using __stack_chk_fail_local hidden function instead of
27335 calling __stack_chk_fail directly. Otherwise it is better to call
27336 __stack_chk_fail directly. */
27338 static tree ATTRIBUTE_UNUSED
27339 rs6000_stack_protect_fail (void)
27341 return (DEFAULT_ABI == ABI_V4 && TARGET_SECURE_PLT && flag_pic)
27342 ? default_hidden_stack_protect_fail ()
27343 : default_external_stack_protect_fail ();
27346 void
27347 rs6000_final_prescan_insn (rtx insn, rtx *operand ATTRIBUTE_UNUSED,
27348 int num_operands ATTRIBUTE_UNUSED)
27350 if (rs6000_warn_cell_microcode)
27352 const char *temp;
27353 int insn_code_number = recog_memoized (insn);
27354 location_t location = INSN_LOCATION (insn);
27356 /* Punt on insns we cannot recognize. */
27357 if (insn_code_number < 0)
27358 return;
27360 temp = get_insn_template (insn_code_number, insn);
27362 if (get_attr_cell_micro (insn) == CELL_MICRO_ALWAYS)
27363 warning_at (location, OPT_mwarn_cell_microcode,
27364 "emitting microcode insn %s\t[%s] #%d",
27365 temp, insn_data[INSN_CODE (insn)].name, INSN_UID (insn));
27366 else if (get_attr_cell_micro (insn) == CELL_MICRO_CONDITIONAL)
27367 warning_at (location, OPT_mwarn_cell_microcode,
27368 "emitting conditional microcode insn %s\t[%s] #%d",
27369 temp, insn_data[INSN_CODE (insn)].name, INSN_UID (insn));
27374 /* Mask options that we want to support inside of attribute((target)) and
27375 #pragma GCC target operations. Note, we do not include things like
27376 64/32-bit, endianess, hard/soft floating point, etc. that would have
27377 different calling sequences. */
27379 struct rs6000_opt_mask {
27380 const char *name; /* option name */
27381 HOST_WIDE_INT mask; /* mask to set */
27382 bool invert; /* invert sense of mask */
27383 bool valid_target; /* option is a target option */
27386 static struct rs6000_opt_mask const rs6000_opt_masks[] =
27388 { "altivec", OPTION_MASK_ALTIVEC, false, true },
27389 { "cmpb", OPTION_MASK_CMPB, false, true },
27390 { "dlmzb", OPTION_MASK_DLMZB, false, true },
27391 { "fprnd", OPTION_MASK_FPRND, false, true },
27392 { "hard-dfp", OPTION_MASK_DFP, false, true },
27393 { "isel", OPTION_MASK_ISEL, false, true },
27394 { "mfcrf", OPTION_MASK_MFCRF, false, true },
27395 { "mfpgpr", OPTION_MASK_MFPGPR, false, true },
27396 { "mulhw", OPTION_MASK_MULHW, false, true },
27397 { "multiple", OPTION_MASK_MULTIPLE, false, true },
27398 { "update", OPTION_MASK_NO_UPDATE, true , true },
27399 { "popcntb", OPTION_MASK_POPCNTB, false, true },
27400 { "popcntd", OPTION_MASK_POPCNTD, false, true },
27401 { "powerpc-gfxopt", OPTION_MASK_PPC_GFXOPT, false, true },
27402 { "powerpc-gpopt", OPTION_MASK_PPC_GPOPT, false, true },
27403 { "recip-precision", OPTION_MASK_RECIP_PRECISION, false, true },
27404 { "string", OPTION_MASK_STRING, false, true },
27405 { "vsx", OPTION_MASK_VSX, false, true },
27406 #ifdef OPTION_MASK_64BIT
27407 #if TARGET_AIX_OS
27408 { "aix64", OPTION_MASK_64BIT, false, false },
27409 { "aix32", OPTION_MASK_64BIT, true, false },
27410 #else
27411 { "64", OPTION_MASK_64BIT, false, false },
27412 { "32", OPTION_MASK_64BIT, true, false },
27413 #endif
27414 #endif
27415 #ifdef OPTION_MASK_EABI
27416 { "eabi", OPTION_MASK_EABI, false, false },
27417 #endif
27418 #ifdef OPTION_MASK_LITTLE_ENDIAN
27419 { "little", OPTION_MASK_LITTLE_ENDIAN, false, false },
27420 { "big", OPTION_MASK_LITTLE_ENDIAN, true, false },
27421 #endif
27422 #ifdef OPTION_MASK_RELOCATABLE
27423 { "relocatable", OPTION_MASK_RELOCATABLE, false, false },
27424 #endif
27425 #ifdef OPTION_MASK_STRICT_ALIGN
27426 { "strict-align", OPTION_MASK_STRICT_ALIGN, false, false },
27427 #endif
27428 { "soft-float", OPTION_MASK_SOFT_FLOAT, false, false },
27429 { "string", OPTION_MASK_STRING, false, false },
27432 /* Builtin mask mapping for printing the flags. */
27433 static struct rs6000_opt_mask const rs6000_builtin_mask_names[] =
27435 { "altivec", RS6000_BTM_ALTIVEC, false, false },
27436 { "vsx", RS6000_BTM_VSX, false, false },
27437 { "spe", RS6000_BTM_SPE, false, false },
27438 { "paired", RS6000_BTM_PAIRED, false, false },
27439 { "fre", RS6000_BTM_FRE, false, false },
27440 { "fres", RS6000_BTM_FRES, false, false },
27441 { "frsqrte", RS6000_BTM_FRSQRTE, false, false },
27442 { "frsqrtes", RS6000_BTM_FRSQRTES, false, false },
27443 { "popcntd", RS6000_BTM_POPCNTD, false, false },
27444 { "cell", RS6000_BTM_CELL, false, false },
27447 /* Option variables that we want to support inside attribute((target)) and
27448 #pragma GCC target operations. */
27450 struct rs6000_opt_var {
27451 const char *name; /* option name */
27452 size_t global_offset; /* offset of the option in global_options. */
27453 size_t target_offset; /* offset of the option in target optiosn. */
27456 static struct rs6000_opt_var const rs6000_opt_vars[] =
27458 { "friz",
27459 offsetof (struct gcc_options, x_TARGET_FRIZ),
27460 offsetof (struct cl_target_option, x_TARGET_FRIZ), },
27461 { "avoid-indexed-addresses",
27462 offsetof (struct gcc_options, x_TARGET_AVOID_XFORM),
27463 offsetof (struct cl_target_option, x_TARGET_AVOID_XFORM) },
27464 { "paired",
27465 offsetof (struct gcc_options, x_rs6000_paired_float),
27466 offsetof (struct cl_target_option, x_rs6000_paired_float), },
27467 { "longcall",
27468 offsetof (struct gcc_options, x_rs6000_default_long_calls),
27469 offsetof (struct cl_target_option, x_rs6000_default_long_calls), },
27472 /* Inner function to handle attribute((target("..."))) and #pragma GCC target
27473 parsing. Return true if there were no errors. */
27475 static bool
27476 rs6000_inner_target_options (tree args, bool attr_p)
27478 bool ret = true;
27480 if (args == NULL_TREE)
27483 else if (TREE_CODE (args) == STRING_CST)
27485 char *p = ASTRDUP (TREE_STRING_POINTER (args));
27486 char *q;
27488 while ((q = strtok (p, ",")) != NULL)
27490 bool error_p = false;
27491 bool not_valid_p = false;
27492 const char *cpu_opt = NULL;
27494 p = NULL;
27495 if (strncmp (q, "cpu=", 4) == 0)
27497 int cpu_index = rs6000_cpu_name_lookup (q+4);
27498 if (cpu_index >= 0)
27499 rs6000_cpu_index = cpu_index;
27500 else
27502 error_p = true;
27503 cpu_opt = q+4;
27506 else if (strncmp (q, "tune=", 5) == 0)
27508 int tune_index = rs6000_cpu_name_lookup (q+5);
27509 if (tune_index >= 0)
27510 rs6000_tune_index = tune_index;
27511 else
27513 error_p = true;
27514 cpu_opt = q+5;
27517 else
27519 size_t i;
27520 bool invert = false;
27521 char *r = q;
27523 error_p = true;
27524 if (strncmp (r, "no-", 3) == 0)
27526 invert = true;
27527 r += 3;
27530 for (i = 0; i < ARRAY_SIZE (rs6000_opt_masks); i++)
27531 if (strcmp (r, rs6000_opt_masks[i].name) == 0)
27533 HOST_WIDE_INT mask = rs6000_opt_masks[i].mask;
27535 if (!rs6000_opt_masks[i].valid_target)
27536 not_valid_p = true;
27537 else
27539 error_p = false;
27540 rs6000_isa_flags_explicit |= mask;
27542 /* VSX needs altivec, so -mvsx automagically sets
27543 altivec. */
27544 if (mask == OPTION_MASK_VSX && !invert)
27545 mask |= OPTION_MASK_ALTIVEC;
27547 if (rs6000_opt_masks[i].invert)
27548 invert = !invert;
27550 if (invert)
27551 rs6000_isa_flags &= ~mask;
27552 else
27553 rs6000_isa_flags |= mask;
27555 break;
27558 if (error_p && !not_valid_p)
27560 for (i = 0; i < ARRAY_SIZE (rs6000_opt_vars); i++)
27561 if (strcmp (r, rs6000_opt_vars[i].name) == 0)
27563 size_t j = rs6000_opt_vars[i].global_offset;
27564 *((int *) ((char *)&global_options + j)) = !invert;
27565 error_p = false;
27566 break;
27571 if (error_p)
27573 const char *eprefix, *esuffix;
27575 ret = false;
27576 if (attr_p)
27578 eprefix = "__attribute__((__target__(";
27579 esuffix = ")))";
27581 else
27583 eprefix = "#pragma GCC target ";
27584 esuffix = "";
27587 if (cpu_opt)
27588 error ("invalid cpu \"%s\" for %s\"%s\"%s", cpu_opt, eprefix,
27589 q, esuffix);
27590 else if (not_valid_p)
27591 error ("%s\"%s\"%s is not allowed", eprefix, q, esuffix);
27592 else
27593 error ("%s\"%s\"%s is invalid", eprefix, q, esuffix);
27598 else if (TREE_CODE (args) == TREE_LIST)
27602 tree value = TREE_VALUE (args);
27603 if (value)
27605 bool ret2 = rs6000_inner_target_options (value, attr_p);
27606 if (!ret2)
27607 ret = false;
27609 args = TREE_CHAIN (args);
27611 while (args != NULL_TREE);
27614 else
27615 gcc_unreachable ();
27617 return ret;
27620 /* Print out the target options as a list for -mdebug=target. */
27622 static void
27623 rs6000_debug_target_options (tree args, const char *prefix)
27625 if (args == NULL_TREE)
27626 fprintf (stderr, "%s<NULL>", prefix);
27628 else if (TREE_CODE (args) == STRING_CST)
27630 char *p = ASTRDUP (TREE_STRING_POINTER (args));
27631 char *q;
27633 while ((q = strtok (p, ",")) != NULL)
27635 p = NULL;
27636 fprintf (stderr, "%s\"%s\"", prefix, q);
27637 prefix = ", ";
27641 else if (TREE_CODE (args) == TREE_LIST)
27645 tree value = TREE_VALUE (args);
27646 if (value)
27648 rs6000_debug_target_options (value, prefix);
27649 prefix = ", ";
27651 args = TREE_CHAIN (args);
27653 while (args != NULL_TREE);
27656 else
27657 gcc_unreachable ();
27659 return;
27663 /* Hook to validate attribute((target("..."))). */
27665 static bool
27666 rs6000_valid_attribute_p (tree fndecl,
27667 tree ARG_UNUSED (name),
27668 tree args,
27669 int flags)
27671 struct cl_target_option cur_target;
27672 bool ret;
27673 tree old_optimize = build_optimization_node ();
27674 tree new_target, new_optimize;
27675 tree func_optimize = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl);
27677 gcc_assert ((fndecl != NULL_TREE) && (args != NULL_TREE));
27679 if (TARGET_DEBUG_TARGET)
27681 tree tname = DECL_NAME (fndecl);
27682 fprintf (stderr, "\n==================== rs6000_valid_attribute_p:\n");
27683 if (tname)
27684 fprintf (stderr, "function: %.*s\n",
27685 (int) IDENTIFIER_LENGTH (tname),
27686 IDENTIFIER_POINTER (tname));
27687 else
27688 fprintf (stderr, "function: unknown\n");
27690 fprintf (stderr, "args:");
27691 rs6000_debug_target_options (args, " ");
27692 fprintf (stderr, "\n");
27694 if (flags)
27695 fprintf (stderr, "flags: 0x%x\n", flags);
27697 fprintf (stderr, "--------------------\n");
27700 old_optimize = build_optimization_node ();
27701 func_optimize = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl);
27703 /* If the function changed the optimization levels as well as setting target
27704 options, start with the optimizations specified. */
27705 if (func_optimize && func_optimize != old_optimize)
27706 cl_optimization_restore (&global_options,
27707 TREE_OPTIMIZATION (func_optimize));
27709 /* The target attributes may also change some optimization flags, so update
27710 the optimization options if necessary. */
27711 cl_target_option_save (&cur_target, &global_options);
27712 rs6000_cpu_index = rs6000_tune_index = -1;
27713 ret = rs6000_inner_target_options (args, true);
27715 /* Set up any additional state. */
27716 if (ret)
27718 ret = rs6000_option_override_internal (false);
27719 new_target = build_target_option_node ();
27721 else
27722 new_target = NULL;
27724 new_optimize = build_optimization_node ();
27726 if (!new_target)
27727 ret = false;
27729 else if (fndecl)
27731 DECL_FUNCTION_SPECIFIC_TARGET (fndecl) = new_target;
27733 if (old_optimize != new_optimize)
27734 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl) = new_optimize;
27737 cl_target_option_restore (&global_options, &cur_target);
27739 if (old_optimize != new_optimize)
27740 cl_optimization_restore (&global_options,
27741 TREE_OPTIMIZATION (old_optimize));
27743 return ret;
27747 /* Hook to validate the current #pragma GCC target and set the state, and
27748 update the macros based on what was changed. If ARGS is NULL, then
27749 POP_TARGET is used to reset the options. */
27751 bool
27752 rs6000_pragma_target_parse (tree args, tree pop_target)
27754 tree prev_tree = build_target_option_node ();
27755 tree cur_tree;
27756 struct cl_target_option *prev_opt, *cur_opt;
27757 HOST_WIDE_INT prev_flags, cur_flags, diff_flags;
27758 HOST_WIDE_INT prev_bumask, cur_bumask, diff_bumask;
27760 if (TARGET_DEBUG_TARGET)
27762 fprintf (stderr, "\n==================== rs6000_pragma_target_parse\n");
27763 fprintf (stderr, "args:");
27764 rs6000_debug_target_options (args, " ");
27765 fprintf (stderr, "\n");
27767 if (pop_target)
27769 fprintf (stderr, "pop_target:\n");
27770 debug_tree (pop_target);
27772 else
27773 fprintf (stderr, "pop_target: <NULL>\n");
27775 fprintf (stderr, "--------------------\n");
27778 if (! args)
27780 cur_tree = ((pop_target)
27781 ? pop_target
27782 : target_option_default_node);
27783 cl_target_option_restore (&global_options,
27784 TREE_TARGET_OPTION (cur_tree));
27786 else
27788 rs6000_cpu_index = rs6000_tune_index = -1;
27789 if (!rs6000_inner_target_options (args, false)
27790 || !rs6000_option_override_internal (false)
27791 || (cur_tree = build_target_option_node ()) == NULL_TREE)
27793 if (TARGET_DEBUG_BUILTIN || TARGET_DEBUG_TARGET)
27794 fprintf (stderr, "invalid pragma\n");
27796 return false;
27800 target_option_current_node = cur_tree;
27802 /* If we have the preprocessor linked in (i.e. C or C++ languages), possibly
27803 change the macros that are defined. */
27804 if (rs6000_target_modify_macros_ptr)
27806 prev_opt = TREE_TARGET_OPTION (prev_tree);
27807 prev_bumask = prev_opt->x_rs6000_builtin_mask;
27808 prev_flags = prev_opt->x_rs6000_isa_flags;
27810 cur_opt = TREE_TARGET_OPTION (cur_tree);
27811 cur_flags = cur_opt->x_rs6000_isa_flags;
27812 cur_bumask = cur_opt->x_rs6000_builtin_mask;
27814 diff_bumask = (prev_bumask ^ cur_bumask);
27815 diff_flags = (prev_flags ^ cur_flags);
27817 if ((diff_flags != 0) || (diff_bumask != 0))
27819 /* Delete old macros. */
27820 rs6000_target_modify_macros_ptr (false,
27821 prev_flags & diff_flags,
27822 prev_bumask & diff_bumask);
27824 /* Define new macros. */
27825 rs6000_target_modify_macros_ptr (true,
27826 cur_flags & diff_flags,
27827 cur_bumask & diff_bumask);
27831 return true;
27835 /* Remember the last target of rs6000_set_current_function. */
27836 static GTY(()) tree rs6000_previous_fndecl;
27838 /* Establish appropriate back-end context for processing the function
27839 FNDECL. The argument might be NULL to indicate processing at top
27840 level, outside of any function scope. */
27841 static void
27842 rs6000_set_current_function (tree fndecl)
27844 tree old_tree = (rs6000_previous_fndecl
27845 ? DECL_FUNCTION_SPECIFIC_TARGET (rs6000_previous_fndecl)
27846 : NULL_TREE);
27848 tree new_tree = (fndecl
27849 ? DECL_FUNCTION_SPECIFIC_TARGET (fndecl)
27850 : NULL_TREE);
27852 if (TARGET_DEBUG_TARGET)
27854 bool print_final = false;
27855 fprintf (stderr, "\n==================== rs6000_set_current_function");
27857 if (fndecl)
27858 fprintf (stderr, ", fndecl %s (%p)",
27859 (DECL_NAME (fndecl)
27860 ? IDENTIFIER_POINTER (DECL_NAME (fndecl))
27861 : "<unknown>"), (void *)fndecl);
27863 if (rs6000_previous_fndecl)
27864 fprintf (stderr, ", prev_fndecl (%p)", (void *)rs6000_previous_fndecl);
27866 fprintf (stderr, "\n");
27867 if (new_tree)
27869 fprintf (stderr, "\nnew fndecl target specific options:\n");
27870 debug_tree (new_tree);
27871 print_final = true;
27874 if (old_tree)
27876 fprintf (stderr, "\nold fndecl target specific options:\n");
27877 debug_tree (old_tree);
27878 print_final = true;
27881 if (print_final)
27882 fprintf (stderr, "--------------------\n");
27885 /* Only change the context if the function changes. This hook is called
27886 several times in the course of compiling a function, and we don't want to
27887 slow things down too much or call target_reinit when it isn't safe. */
27888 if (fndecl && fndecl != rs6000_previous_fndecl)
27890 rs6000_previous_fndecl = fndecl;
27891 if (old_tree == new_tree)
27894 else if (new_tree)
27896 cl_target_option_restore (&global_options,
27897 TREE_TARGET_OPTION (new_tree));
27898 target_reinit ();
27901 else if (old_tree)
27903 struct cl_target_option *def
27904 = TREE_TARGET_OPTION (target_option_current_node);
27906 cl_target_option_restore (&global_options, def);
27907 target_reinit ();
27913 /* Save the current options */
27915 static void
27916 rs6000_function_specific_save (struct cl_target_option *ptr)
27918 ptr->x_rs6000_isa_flags = rs6000_isa_flags;
27919 ptr->x_rs6000_isa_flags_explicit = rs6000_isa_flags_explicit;
27922 /* Restore the current options */
27924 static void
27925 rs6000_function_specific_restore (struct cl_target_option *ptr)
27927 rs6000_isa_flags = ptr->x_rs6000_isa_flags;
27928 rs6000_isa_flags_explicit = ptr->x_rs6000_isa_flags_explicit;
27929 (void) rs6000_option_override_internal (false);
27932 /* Print the current options */
27934 static void
27935 rs6000_function_specific_print (FILE *file, int indent,
27936 struct cl_target_option *ptr)
27938 rs6000_print_isa_options (file, indent, "Isa options set",
27939 ptr->x_rs6000_isa_flags);
27941 rs6000_print_isa_options (file, indent, "Isa options explicit",
27942 ptr->x_rs6000_isa_flags_explicit);
27945 /* Helper function to print the current isa or misc options on a line. */
27947 static void
27948 rs6000_print_options_internal (FILE *file,
27949 int indent,
27950 const char *string,
27951 HOST_WIDE_INT flags,
27952 const char *prefix,
27953 const struct rs6000_opt_mask *opts,
27954 size_t num_elements)
27956 size_t i;
27957 size_t start_column = 0;
27958 size_t cur_column;
27959 size_t max_column = 76;
27960 const char *comma = "";
27961 const char *nl = "\n";
27963 if (indent)
27964 start_column += fprintf (file, "%*s", indent, "");
27966 if (!flags)
27968 fprintf (stderr, DEBUG_FMT_S, string, "<none>");
27969 return;
27972 start_column += fprintf (stderr, DEBUG_FMT_WX, string, flags);
27974 /* Print the various mask options. */
27975 cur_column = start_column;
27976 for (i = 0; i < num_elements; i++)
27978 if ((flags & opts[i].mask) != 0)
27980 const char *no_str = rs6000_opt_masks[i].invert ? "no-" : "";
27981 size_t len = (strlen (comma)
27982 + strlen (prefix)
27983 + strlen (no_str)
27984 + strlen (rs6000_opt_masks[i].name));
27986 cur_column += len;
27987 if (cur_column > max_column)
27989 fprintf (stderr, ", \\\n%*s", (int)start_column, "");
27990 cur_column = start_column + len;
27991 comma = "";
27992 nl = "\n\n";
27995 fprintf (file, "%s%s%s%s", comma, prefix, no_str,
27996 rs6000_opt_masks[i].name);
27997 flags &= ~ opts[i].mask;
27998 comma = ", ";
28002 fputs (nl, file);
28005 /* Helper function to print the current isa options on a line. */
28007 static void
28008 rs6000_print_isa_options (FILE *file, int indent, const char *string,
28009 HOST_WIDE_INT flags)
28011 rs6000_print_options_internal (file, indent, string, flags, "-m",
28012 &rs6000_opt_masks[0],
28013 ARRAY_SIZE (rs6000_opt_masks));
28016 static void
28017 rs6000_print_builtin_options (FILE *file, int indent, const char *string,
28018 HOST_WIDE_INT flags)
28020 rs6000_print_options_internal (file, indent, string, flags, "",
28021 &rs6000_builtin_mask_names[0],
28022 ARRAY_SIZE (rs6000_builtin_mask_names));
28026 /* Hook to determine if one function can safely inline another. */
28028 static bool
28029 rs6000_can_inline_p (tree caller, tree callee)
28031 bool ret = false;
28032 tree caller_tree = DECL_FUNCTION_SPECIFIC_TARGET (caller);
28033 tree callee_tree = DECL_FUNCTION_SPECIFIC_TARGET (callee);
28035 /* If callee has no option attributes, then it is ok to inline. */
28036 if (!callee_tree)
28037 ret = true;
28039 /* If caller has no option attributes, but callee does then it is not ok to
28040 inline. */
28041 else if (!caller_tree)
28042 ret = false;
28044 else
28046 struct cl_target_option *caller_opts = TREE_TARGET_OPTION (caller_tree);
28047 struct cl_target_option *callee_opts = TREE_TARGET_OPTION (callee_tree);
28049 /* Callee's options should a subset of the caller's, i.e. a vsx function
28050 can inline an altivec function but a non-vsx function can't inline a
28051 vsx function. */
28052 if ((caller_opts->x_rs6000_isa_flags & callee_opts->x_rs6000_isa_flags)
28053 == callee_opts->x_rs6000_isa_flags)
28054 ret = true;
28057 if (TARGET_DEBUG_TARGET)
28058 fprintf (stderr, "rs6000_can_inline_p:, caller %s, callee %s, %s inline\n",
28059 (DECL_NAME (caller)
28060 ? IDENTIFIER_POINTER (DECL_NAME (caller))
28061 : "<unknown>"),
28062 (DECL_NAME (callee)
28063 ? IDENTIFIER_POINTER (DECL_NAME (callee))
28064 : "<unknown>"),
28065 (ret ? "can" : "cannot"));
28067 return ret;
28070 /* Allocate a stack temp and fixup the address so it meets the particular
28071 memory requirements (either offetable or REG+REG addressing). */
28074 rs6000_allocate_stack_temp (enum machine_mode mode,
28075 bool offsettable_p,
28076 bool reg_reg_p)
28078 rtx stack = assign_stack_temp (mode, GET_MODE_SIZE (mode));
28079 rtx addr = XEXP (stack, 0);
28080 int strict_p = (reload_in_progress || reload_completed);
28082 if (!legitimate_indirect_address_p (addr, strict_p))
28084 if (offsettable_p
28085 && !rs6000_legitimate_offset_address_p (mode, addr, strict_p, true))
28086 stack = replace_equiv_address (stack, copy_addr_to_reg (addr));
28088 else if (reg_reg_p && !legitimate_indexed_address_p (addr, strict_p))
28089 stack = replace_equiv_address (stack, copy_addr_to_reg (addr));
28092 return stack;
28095 /* Given a memory reference, if it is not a reg or reg+reg addressing, convert
28096 to such a form to deal with memory reference instructions like STFIWX that
28097 only take reg+reg addressing. */
28100 rs6000_address_for_fpconvert (rtx x)
28102 int strict_p = (reload_in_progress || reload_completed);
28103 rtx addr;
28105 gcc_assert (MEM_P (x));
28106 addr = XEXP (x, 0);
28107 if (! legitimate_indirect_address_p (addr, strict_p)
28108 && ! legitimate_indexed_address_p (addr, strict_p))
28110 if (GET_CODE (addr) == PRE_INC || GET_CODE (addr) == PRE_DEC)
28112 rtx reg = XEXP (addr, 0);
28113 HOST_WIDE_INT size = GET_MODE_SIZE (GET_MODE (x));
28114 rtx size_rtx = GEN_INT ((GET_CODE (addr) == PRE_DEC) ? -size : size);
28115 gcc_assert (REG_P (reg));
28116 emit_insn (gen_add3_insn (reg, reg, size_rtx));
28117 addr = reg;
28119 else if (GET_CODE (addr) == PRE_MODIFY)
28121 rtx reg = XEXP (addr, 0);
28122 rtx expr = XEXP (addr, 1);
28123 gcc_assert (REG_P (reg));
28124 gcc_assert (GET_CODE (expr) == PLUS);
28125 emit_insn (gen_add3_insn (reg, XEXP (expr, 0), XEXP (expr, 1)));
28126 addr = reg;
28129 x = replace_equiv_address (x, copy_addr_to_reg (addr));
28132 return x;
28135 /* Given a memory reference, if it is not in the form for altivec memory
28136 reference instructions (i.e. reg or reg+reg addressing with AND of -16),
28137 convert to the altivec format. */
28140 rs6000_address_for_altivec (rtx x)
28142 gcc_assert (MEM_P (x));
28143 if (!altivec_indexed_or_indirect_operand (x, GET_MODE (x)))
28145 rtx addr = XEXP (x, 0);
28146 int strict_p = (reload_in_progress || reload_completed);
28148 if (!legitimate_indexed_address_p (addr, strict_p)
28149 && !legitimate_indirect_address_p (addr, strict_p))
28150 addr = copy_to_mode_reg (Pmode, addr);
28152 addr = gen_rtx_AND (Pmode, addr, GEN_INT (-16));
28153 x = change_address (x, GET_MODE (x), addr);
28156 return x;
28159 /* Implement TARGET_LEGITIMATE_CONSTANT_P.
28161 On the RS/6000, all integer constants are acceptable, most won't be valid
28162 for particular insns, though. Only easy FP constants are acceptable. */
28164 static bool
28165 rs6000_legitimate_constant_p (enum machine_mode mode, rtx x)
28167 if (rs6000_tls_referenced_p (x))
28168 return false;
28170 return ((GET_CODE (x) != CONST_DOUBLE && GET_CODE (x) != CONST_VECTOR)
28171 || GET_MODE (x) == VOIDmode
28172 || (TARGET_POWERPC64 && mode == DImode)
28173 || easy_fp_constant (x, mode)
28174 || easy_vector_constant (x, mode));
28178 /* A function pointer under AIX is a pointer to a data area whose first word
28179 contains the actual address of the function, whose second word contains a
28180 pointer to its TOC, and whose third word contains a value to place in the
28181 static chain register (r11). Note that if we load the static chain, our
28182 "trampoline" need not have any executable code. */
28184 void
28185 rs6000_call_indirect_aix (rtx value, rtx func_desc, rtx flag)
28187 rtx func_addr;
28188 rtx toc_reg;
28189 rtx sc_reg;
28190 rtx stack_ptr;
28191 rtx stack_toc_offset;
28192 rtx stack_toc_mem;
28193 rtx func_toc_offset;
28194 rtx func_toc_mem;
28195 rtx func_sc_offset;
28196 rtx func_sc_mem;
28197 rtx insn;
28198 rtx (*call_func) (rtx, rtx, rtx, rtx);
28199 rtx (*call_value_func) (rtx, rtx, rtx, rtx, rtx);
28201 stack_ptr = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
28202 toc_reg = gen_rtx_REG (Pmode, TOC_REGNUM);
28204 /* Load up address of the actual function. */
28205 func_desc = force_reg (Pmode, func_desc);
28206 func_addr = gen_reg_rtx (Pmode);
28207 emit_move_insn (func_addr, gen_rtx_MEM (Pmode, func_desc));
28209 if (TARGET_32BIT)
28212 stack_toc_offset = GEN_INT (TOC_SAVE_OFFSET_32BIT);
28213 func_toc_offset = GEN_INT (AIX_FUNC_DESC_TOC_32BIT);
28214 func_sc_offset = GEN_INT (AIX_FUNC_DESC_SC_32BIT);
28215 if (TARGET_POINTERS_TO_NESTED_FUNCTIONS)
28217 call_func = gen_call_indirect_aix32bit;
28218 call_value_func = gen_call_value_indirect_aix32bit;
28220 else
28222 call_func = gen_call_indirect_aix32bit_nor11;
28223 call_value_func = gen_call_value_indirect_aix32bit_nor11;
28226 else
28228 stack_toc_offset = GEN_INT (TOC_SAVE_OFFSET_64BIT);
28229 func_toc_offset = GEN_INT (AIX_FUNC_DESC_TOC_64BIT);
28230 func_sc_offset = GEN_INT (AIX_FUNC_DESC_SC_64BIT);
28231 if (TARGET_POINTERS_TO_NESTED_FUNCTIONS)
28233 call_func = gen_call_indirect_aix64bit;
28234 call_value_func = gen_call_value_indirect_aix64bit;
28236 else
28238 call_func = gen_call_indirect_aix64bit_nor11;
28239 call_value_func = gen_call_value_indirect_aix64bit_nor11;
28243 /* Reserved spot to store the TOC. */
28244 stack_toc_mem = gen_frame_mem (Pmode,
28245 gen_rtx_PLUS (Pmode,
28246 stack_ptr,
28247 stack_toc_offset));
28249 gcc_assert (cfun);
28250 gcc_assert (cfun->machine);
28252 /* Can we optimize saving the TOC in the prologue or do we need to do it at
28253 every call? */
28254 if (TARGET_SAVE_TOC_INDIRECT && !cfun->calls_alloca)
28255 cfun->machine->save_toc_in_prologue = true;
28257 else
28259 MEM_VOLATILE_P (stack_toc_mem) = 1;
28260 emit_move_insn (stack_toc_mem, toc_reg);
28263 /* Calculate the address to load the TOC of the called function. We don't
28264 actually load this until the split after reload. */
28265 func_toc_mem = gen_rtx_MEM (Pmode,
28266 gen_rtx_PLUS (Pmode,
28267 func_desc,
28268 func_toc_offset));
28270 /* If we have a static chain, load it up. */
28271 if (TARGET_POINTERS_TO_NESTED_FUNCTIONS)
28273 func_sc_mem = gen_rtx_MEM (Pmode,
28274 gen_rtx_PLUS (Pmode,
28275 func_desc,
28276 func_sc_offset));
28278 sc_reg = gen_rtx_REG (Pmode, STATIC_CHAIN_REGNUM);
28279 emit_move_insn (sc_reg, func_sc_mem);
28282 /* Create the call. */
28283 if (value)
28284 insn = call_value_func (value, func_addr, flag, func_toc_mem,
28285 stack_toc_mem);
28286 else
28287 insn = call_func (func_addr, flag, func_toc_mem, stack_toc_mem);
28289 emit_call_insn (insn);
28292 /* Return whether we need to always update the saved TOC pointer when we update
28293 the stack pointer. */
28295 static bool
28296 rs6000_save_toc_in_prologue_p (void)
28298 return (cfun && cfun->machine && cfun->machine->save_toc_in_prologue);
28301 #ifdef HAVE_GAS_HIDDEN
28302 # define USE_HIDDEN_LINKONCE 1
28303 #else
28304 # define USE_HIDDEN_LINKONCE 0
28305 #endif
28307 /* Fills in the label name that should be used for a 476 link stack thunk. */
28309 void
28310 get_ppc476_thunk_name (char name[32])
28312 gcc_assert (TARGET_LINK_STACK);
28314 if (USE_HIDDEN_LINKONCE)
28315 sprintf (name, "__ppc476.get_thunk");
28316 else
28317 ASM_GENERATE_INTERNAL_LABEL (name, "LPPC476_", 0);
28320 /* This function emits the simple thunk routine that is used to preserve
28321 the link stack on the 476 cpu. */
28323 static void rs6000_code_end (void) ATTRIBUTE_UNUSED;
28324 static void
28325 rs6000_code_end (void)
28327 char name[32];
28328 tree decl;
28330 if (!TARGET_LINK_STACK)
28331 return;
28333 get_ppc476_thunk_name (name);
28335 decl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL, get_identifier (name),
28336 build_function_type_list (void_type_node, NULL_TREE));
28337 DECL_RESULT (decl) = build_decl (BUILTINS_LOCATION, RESULT_DECL,
28338 NULL_TREE, void_type_node);
28339 TREE_PUBLIC (decl) = 1;
28340 TREE_STATIC (decl) = 1;
28342 #if RS6000_WEAK
28343 if (USE_HIDDEN_LINKONCE)
28345 DECL_COMDAT_GROUP (decl) = DECL_ASSEMBLER_NAME (decl);
28346 targetm.asm_out.unique_section (decl, 0);
28347 switch_to_section (get_named_section (decl, NULL, 0));
28348 DECL_WEAK (decl) = 1;
28349 ASM_WEAKEN_DECL (asm_out_file, decl, name, 0);
28350 targetm.asm_out.globalize_label (asm_out_file, name);
28351 targetm.asm_out.assemble_visibility (decl, VISIBILITY_HIDDEN);
28352 ASM_DECLARE_FUNCTION_NAME (asm_out_file, name, decl);
28354 else
28355 #endif
28357 switch_to_section (text_section);
28358 ASM_OUTPUT_LABEL (asm_out_file, name);
28361 DECL_INITIAL (decl) = make_node (BLOCK);
28362 current_function_decl = decl;
28363 init_function_start (decl);
28364 first_function_block_is_cold = false;
28365 /* Make sure unwind info is emitted for the thunk if needed. */
28366 final_start_function (emit_barrier (), asm_out_file, 1);
28368 fputs ("\tblr\n", asm_out_file);
28370 final_end_function ();
28371 init_insn_lengths ();
28372 free_after_compilation (cfun);
28373 set_cfun (NULL);
28374 current_function_decl = NULL;
28377 /* Add r30 to hard reg set if the prologue sets it up and it is not
28378 pic_offset_table_rtx. */
28380 static void
28381 rs6000_set_up_by_prologue (struct hard_reg_set_container *set)
28383 if (!TARGET_SINGLE_PIC_BASE
28384 && TARGET_TOC
28385 && TARGET_MINIMAL_TOC
28386 && get_pool_size () != 0)
28387 add_to_hard_reg_set (&set->set, Pmode, RS6000_PIC_OFFSET_TABLE_REGNUM);
28390 struct gcc_target targetm = TARGET_INITIALIZER;
28392 #include "gt-rs6000.h"