]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/config/rs6000/rs6000.c
re PR target/11591 ([3.4 only] ICE in gcc.dg/altivec-5.c)
[thirdparty/gcc.git] / gcc / config / rs6000 / rs6000.c
CommitLineData
9878760c 1/* Subroutines used for code generation on IBM RS/6000.
9ebbca7d 2 Copyright (C) 1991, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
93c9d1ba 3 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
fab3bcc3 4 Contributed by Richard Kenner (kenner@vlsi1.ultra.nyu.edu)
9878760c 5
5de601cf 6 This file is part of GCC.
9878760c 7
5de601cf
NC
8 GCC is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published
10 by the Free Software Foundation; either version 2, or (at your
11 option) any later version.
9878760c 12
5de601cf
NC
13 GCC is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
16 License for more details.
9878760c 17
5de601cf
NC
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the
20 Free Software Foundation, 59 Temple Place - Suite 330, Boston,
21 MA 02111-1307, USA. */
9878760c 22
956d6950 23#include "config.h"
c4d38ccb 24#include "system.h"
4977bab6
ZW
25#include "coretypes.h"
26#include "tm.h"
9878760c
RK
27#include "rtl.h"
28#include "regs.h"
29#include "hard-reg-set.h"
30#include "real.h"
31#include "insn-config.h"
32#include "conditions.h"
9878760c
RK
33#include "insn-attr.h"
34#include "flags.h"
35#include "recog.h"
9878760c 36#include "obstack.h"
9b30bae2 37#include "tree.h"
dfafc897 38#include "expr.h"
2fc1c679 39#include "optabs.h"
2a430ec1 40#include "except.h"
a7df97e6 41#include "function.h"
296b8152 42#include "output.h"
d5fa86ba 43#include "basic-block.h"
d0101753 44#include "integrate.h"
296b8152 45#include "toplev.h"
c8023011 46#include "ggc.h"
9ebbca7d
GK
47#include "hashtab.h"
48#include "tm_p.h"
672a6f42
NB
49#include "target.h"
50#include "target-def.h"
3ac88239 51#include "langhooks.h"
24ea750e 52#include "reload.h"
117dca74 53#include "cfglayout.h"
79ae11c4 54#include "sched-int.h"
1bc7c5b6
ZW
55#if TARGET_XCOFF
56#include "xcoffout.h" /* get declarations of xcoff_*_section_name */
57#endif
9b30bae2 58
7509c759
MM
59#ifndef TARGET_NO_PROTOTYPE
60#define TARGET_NO_PROTOTYPE 0
61#endif
62
d744e06e
AH
63#define EASY_VECTOR_15(n, x, y) ((n) >= -16 && (n) <= 15 \
64 && easy_vector_same (x, y))
65
66#define EASY_VECTOR_15_ADD_SELF(n, x, y) ((n) >= 0x10 && (n) <= 0x1e \
67 && !((n) & 1) \
68 && easy_vector_same (x, y))
69
9878760c
RK
70#define min(A,B) ((A) < (B) ? (A) : (B))
71#define max(A,B) ((A) > (B) ? (A) : (B))
72
d1d0c603
JJ
73/* Structure used to define the rs6000 stack */
74typedef struct rs6000_stack {
75 int first_gp_reg_save; /* first callee saved GP register used */
76 int first_fp_reg_save; /* first callee saved FP register used */
77 int first_altivec_reg_save; /* first callee saved AltiVec register used */
78 int lr_save_p; /* true if the link reg needs to be saved */
79 int cr_save_p; /* true if the CR reg needs to be saved */
80 unsigned int vrsave_mask; /* mask of vec registers to save */
81 int toc_save_p; /* true if the TOC needs to be saved */
82 int push_p; /* true if we need to allocate stack space */
83 int calls_p; /* true if the function makes any calls */
84 enum rs6000_abi abi; /* which ABI to use */
85 int gp_save_offset; /* offset to save GP regs from initial SP */
86 int fp_save_offset; /* offset to save FP regs from initial SP */
87 int altivec_save_offset; /* offset to save AltiVec regs from initial SP */
88 int lr_save_offset; /* offset to save LR from initial SP */
89 int cr_save_offset; /* offset to save CR from initial SP */
90 int vrsave_save_offset; /* offset to save VRSAVE from initial SP */
91 int spe_gp_save_offset; /* offset to save spe 64-bit gprs */
92 int toc_save_offset; /* offset to save the TOC pointer */
93 int varargs_save_offset; /* offset to save the varargs registers */
94 int ehrd_offset; /* offset to EH return data */
95 int reg_size; /* register size (4 or 8) */
96 int varargs_size; /* size to hold V.4 args passed in regs */
97 HOST_WIDE_INT vars_size; /* variable save area size */
98 int parm_size; /* outgoing parameter size */
99 int save_size; /* save area size */
100 int fixed_size; /* fixed size of stack frame */
101 int gp_size; /* size of saved GP registers */
102 int fp_size; /* size of saved FP registers */
103 int altivec_size; /* size of saved AltiVec registers */
104 int cr_size; /* size to hold CR if not in save_size */
105 int lr_size; /* size to hold LR if not in save_size */
106 int vrsave_size; /* size to hold VRSAVE if not in save_size */
107 int altivec_padding_size; /* size of altivec alignment padding if
108 not in save_size */
109 int spe_gp_size; /* size of 64-bit GPR save size for SPE */
110 int spe_padding_size;
111 int toc_size; /* size to hold TOC if not in save_size */
112 HOST_WIDE_INT total_size; /* total bytes allocated for stack */
113 int spe_64bit_regs_used;
114} rs6000_stack_t;
115
5248c961
RK
116/* Target cpu type */
117
118enum processor_type rs6000_cpu;
8e3f41e7
MM
119struct rs6000_cpu_select rs6000_select[3] =
120{
815cdc52
MM
121 /* switch name, tune arch */
122 { (const char *)0, "--with-cpu=", 1, 1 },
123 { (const char *)0, "-mcpu=", 1, 1 },
124 { (const char *)0, "-mtune=", 1, 0 },
8e3f41e7 125};
5248c961 126
ec507f2d
DE
127/* Always emit branch hint bits. */
128static GTY(()) bool rs6000_always_hint;
129
130/* Schedule instructions for group formation. */
131static GTY(()) bool rs6000_sched_groups;
132
79ae11c4
DN
133/* Support adjust_priority scheduler hook
134 and -mprioritize-restricted-insns= option. */
135const char *rs6000_sched_restricted_insns_priority_str;
136int rs6000_sched_restricted_insns_priority;
137
569fa502
DN
138/* Support for -msched-costly-dep option. */
139const char *rs6000_sched_costly_dep_str;
140enum rs6000_dependence_cost rs6000_sched_costly_dep;
141
cbe26ab8
DN
142/* Support for -minsert-sched-nops option. */
143const char *rs6000_sched_insert_nops_str;
144enum rs6000_nop_insertion rs6000_sched_insert_nops;
145
6fa3f289
ZW
146/* Size of long double */
147const char *rs6000_long_double_size_string;
148int rs6000_long_double_type_size;
149
150/* Whether -mabi=altivec has appeared */
151int rs6000_altivec_abi;
152
08b57fb3
AH
153/* Whether VRSAVE instructions should be generated. */
154int rs6000_altivec_vrsave;
155
156/* String from -mvrsave= option. */
157const char *rs6000_altivec_vrsave_string;
158
a3170dc6
AH
159/* Nonzero if we want SPE ABI extensions. */
160int rs6000_spe_abi;
161
162/* Whether isel instructions should be generated. */
163int rs6000_isel;
164
993f19a8
AH
165/* Whether SPE simd instructions should be generated. */
166int rs6000_spe;
167
5da702b1
AH
168/* Nonzero if floating point operations are done in the GPRs. */
169int rs6000_float_gprs = 0;
170
171/* String from -mfloat-gprs=. */
172const char *rs6000_float_gprs_string;
a3170dc6
AH
173
174/* String from -misel=. */
175const char *rs6000_isel_string;
176
993f19a8
AH
177/* String from -mspe=. */
178const char *rs6000_spe_string;
179
a0ab749a 180/* Set to nonzero once AIX common-mode calls have been defined. */
bbfb86aa 181static GTY(()) int common_mode_defined;
c81bebd7 182
9878760c
RK
183/* Save information from a "cmpxx" operation until the branch or scc is
184 emitted. */
9878760c
RK
185rtx rs6000_compare_op0, rs6000_compare_op1;
186int rs6000_compare_fp_p;
874a0744 187
874a0744
MM
188/* Label number of label created for -mrelocatable, to call to so we can
189 get the address of the GOT section */
190int rs6000_pic_labelno;
c81bebd7 191
b91da81f 192#ifdef USING_ELFOS_H
c81bebd7 193/* Which abi to adhere to */
9739c90c 194const char *rs6000_abi_name;
d9407988
MM
195
196/* Semantics of the small data area */
197enum rs6000_sdata_type rs6000_sdata = SDATA_DATA;
198
199/* Which small data model to use */
815cdc52 200const char *rs6000_sdata_name = (char *)0;
9ebbca7d
GK
201
202/* Counter for labels which are to be placed in .fixup. */
203int fixuplabelno = 0;
874a0744 204#endif
4697a36c 205
c4501e62
JJ
206/* Bit size of immediate TLS offsets and string from which it is decoded. */
207int rs6000_tls_size = 32;
208const char *rs6000_tls_size_string;
209
b6c9286a
MM
210/* ABI enumeration available for subtarget to use. */
211enum rs6000_abi rs6000_current_abi;
212
0ac081f6
AH
213/* ABI string from -mabi= option. */
214const char *rs6000_abi_string;
215
38c1f2d7 216/* Debug flags */
815cdc52 217const char *rs6000_debug_name;
38c1f2d7
MM
218int rs6000_debug_stack; /* debug stack applications */
219int rs6000_debug_arg; /* debug argument handling */
220
6035d635 221/* Opaque types. */
2abe3e28 222static GTY(()) tree opaque_V2SI_type_node;
2abe3e28 223static GTY(()) tree opaque_V2SF_type_node;
6035d635 224static GTY(()) tree opaque_p_V2SI_type_node;
4a5eab38
PB
225static GTY(()) tree V16QI_type_node;
226static GTY(()) tree V2SI_type_node;
227static GTY(()) tree V2SF_type_node;
228static GTY(()) tree V4HI_type_node;
229static GTY(()) tree V4SI_type_node;
230static GTY(()) tree V4SF_type_node;
231static GTY(()) tree V8HI_type_node;
232static GTY(()) tree unsigned_V16QI_type_node;
233static GTY(()) tree unsigned_V8HI_type_node;
234static GTY(()) tree unsigned_V4SI_type_node;
8bb418a3
ZL
235static GTY(()) tree bool_char_type_node; /* __bool char */
236static GTY(()) tree bool_short_type_node; /* __bool short */
237static GTY(()) tree bool_int_type_node; /* __bool int */
238static GTY(()) tree pixel_type_node; /* __pixel */
239static GTY(()) tree bool_V16QI_type_node; /* __vector __bool char */
240static GTY(()) tree bool_V8HI_type_node; /* __vector __bool short */
241static GTY(()) tree bool_V4SI_type_node; /* __vector __bool int */
242static GTY(()) tree pixel_V8HI_type_node; /* __vector __pixel */
243
244int rs6000_warn_altivec_long = 1; /* On by default. */
245const char *rs6000_warn_altivec_long_switch;
246
57ac7be9
AM
247const char *rs6000_traceback_name;
248static enum {
249 traceback_default = 0,
250 traceback_none,
251 traceback_part,
252 traceback_full
253} rs6000_traceback;
254
38c1f2d7
MM
255/* Flag to say the TOC is initialized */
256int toc_initialized;
9ebbca7d 257char toc_label_name[10];
38c1f2d7 258
9ebbca7d 259/* Alias set for saves and restores from the rs6000 stack. */
f103e34d 260static GTY(()) int rs6000_sr_alias_set;
c8023011 261
a5c76ee6
ZW
262/* Call distance, overridden by -mlongcall and #pragma longcall(1).
263 The only place that looks at this is rs6000_set_default_type_attributes;
264 everywhere else should rely on the presence or absence of a longcall
265 attribute on the function declaration. */
266int rs6000_default_long_calls;
267const char *rs6000_longcall_switch;
268
a3c9585f
KH
269/* Control alignment for fields within structures. */
270/* String from -malign-XXXXX. */
025d9908
KH
271const char *rs6000_alignment_string;
272int rs6000_alignment_flags;
273
a3170dc6
AH
274struct builtin_description
275{
276 /* mask is not const because we're going to alter it below. This
277 nonsense will go away when we rewrite the -march infrastructure
278 to give us more target flag bits. */
279 unsigned int mask;
280 const enum insn_code icode;
281 const char *const name;
282 const enum rs6000_builtins code;
283};
284
a2369ed3
DJ
285static bool rs6000_function_ok_for_sibcall (tree, tree);
286static int num_insns_constant_wide (HOST_WIDE_INT);
287static void validate_condition_mode (enum rtx_code, enum machine_mode);
288static rtx rs6000_generate_compare (enum rtx_code);
289static void rs6000_maybe_dead (rtx);
290static void rs6000_emit_stack_tie (void);
291static void rs6000_frame_related (rtx, rtx, HOST_WIDE_INT, rtx, rtx);
292static rtx spe_synthesize_frame_save (rtx);
293static bool spe_func_has_64bit_regs_p (void);
b20a9cca 294static void emit_frame_save (rtx, rtx, enum machine_mode, unsigned int,
d1d0c603 295 int, HOST_WIDE_INT);
a2369ed3
DJ
296static rtx gen_frame_mem_offset (enum machine_mode, rtx, int);
297static void rs6000_emit_allocate_stack (HOST_WIDE_INT, int);
298static unsigned rs6000_hash_constant (rtx);
299static unsigned toc_hash_function (const void *);
300static int toc_hash_eq (const void *, const void *);
301static int constant_pool_expr_1 (rtx, int *, int *);
302static bool constant_pool_expr_p (rtx);
303static bool toc_relative_expr_p (rtx);
304static bool legitimate_small_data_p (enum machine_mode, rtx);
305static bool legitimate_offset_address_p (enum machine_mode, rtx, int);
306static bool legitimate_indexed_address_p (rtx, int);
307static bool legitimate_indirect_address_p (rtx, int);
4c81e946 308static bool macho_lo_sum_memory_operand (rtx x, enum machine_mode mode);
a2369ed3
DJ
309static bool legitimate_lo_sum_address_p (enum machine_mode, rtx, int);
310static struct machine_function * rs6000_init_machine_status (void);
311static bool rs6000_assemble_integer (rtx, unsigned int, int);
5add3202 312#ifdef HAVE_GAS_HIDDEN
a2369ed3 313static void rs6000_assemble_visibility (tree, int);
5add3202 314#endif
a2369ed3
DJ
315static int rs6000_ra_ever_killed (void);
316static tree rs6000_handle_longcall_attribute (tree *, tree, tree, int, bool *);
8bb418a3 317static tree rs6000_handle_altivec_attribute (tree *, tree, tree, int, bool *);
f18eca82 318static const char *rs6000_mangle_fundamental_type (tree);
b86fe7b4 319extern const struct attribute_spec rs6000_attribute_table[];
a2369ed3
DJ
320static void rs6000_set_default_type_attributes (tree);
321static void rs6000_output_function_prologue (FILE *, HOST_WIDE_INT);
322static void rs6000_output_function_epilogue (FILE *, HOST_WIDE_INT);
b20a9cca
AM
323static void rs6000_output_mi_thunk (FILE *, tree, HOST_WIDE_INT, HOST_WIDE_INT,
324 tree);
a2369ed3 325static rtx rs6000_emit_set_long_const (rtx, HOST_WIDE_INT, HOST_WIDE_INT);
c6e8c921 326static bool rs6000_return_in_memory (tree, tree);
a2369ed3 327static void rs6000_file_start (void);
7c262518 328#if TARGET_ELF
a2369ed3
DJ
329static unsigned int rs6000_elf_section_type_flags (tree, const char *, int);
330static void rs6000_elf_asm_out_constructor (rtx, int);
331static void rs6000_elf_asm_out_destructor (rtx, int);
332static void rs6000_elf_select_section (tree, int, unsigned HOST_WIDE_INT);
333static void rs6000_elf_unique_section (tree, int);
334static void rs6000_elf_select_rtx_section (enum machine_mode, rtx,
b20a9cca 335 unsigned HOST_WIDE_INT);
a56d7372 336static void rs6000_elf_encode_section_info (tree, rtx, int)
0e5dbd9b 337 ATTRIBUTE_UNUSED;
a2369ed3 338static bool rs6000_elf_in_small_data_p (tree);
7c262518 339#endif
cbaaba19 340#if TARGET_XCOFF
a2369ed3
DJ
341static void rs6000_xcoff_asm_globalize_label (FILE *, const char *);
342static void rs6000_xcoff_asm_named_section (const char *, unsigned int);
343static void rs6000_xcoff_select_section (tree, int, unsigned HOST_WIDE_INT);
344static void rs6000_xcoff_unique_section (tree, int);
345static void rs6000_xcoff_select_rtx_section (enum machine_mode, rtx,
b20a9cca 346 unsigned HOST_WIDE_INT);
a2369ed3
DJ
347static const char * rs6000_xcoff_strip_name_encoding (const char *);
348static unsigned int rs6000_xcoff_section_type_flags (tree, const char *, int);
349static void rs6000_xcoff_file_start (void);
350static void rs6000_xcoff_file_end (void);
f1384257
AM
351#endif
352#if TARGET_MACHO
a2369ed3 353static bool rs6000_binds_local_p (tree);
f1384257 354#endif
a2369ed3
DJ
355static int rs6000_use_dfa_pipeline_interface (void);
356static int rs6000_variable_issue (FILE *, int, rtx, int);
357static bool rs6000_rtx_costs (rtx, int, int, int *);
358static int rs6000_adjust_cost (rtx, rtx, rtx, int);
cbe26ab8 359static bool is_microcoded_insn (rtx);
79ae11c4 360static int is_dispatch_slot_restricted (rtx);
cbe26ab8
DN
361static bool is_cracked_insn (rtx);
362static bool is_branch_slot_insn (rtx);
a2369ed3
DJ
363static int rs6000_adjust_priority (rtx, int);
364static int rs6000_issue_rate (void);
569fa502 365static bool rs6000_is_costly_dependence (rtx, rtx, rtx, int, int);
cbe26ab8
DN
366static rtx get_next_active_insn (rtx, rtx);
367static bool insn_terminates_group_p (rtx , enum group_termination);
368static bool is_costly_group (rtx *, rtx);
369static int force_new_group (int, FILE *, rtx *, rtx, bool *, int, int *);
370static int redefine_groups (FILE *, int, rtx, rtx);
371static int pad_groups (FILE *, int, rtx, rtx);
372static void rs6000_sched_finish (FILE *, int);
a2369ed3
DJ
373static int rs6000_use_sched_lookahead (void);
374
375static void rs6000_init_builtins (void);
376static rtx rs6000_expand_unop_builtin (enum insn_code, tree, rtx);
377static rtx rs6000_expand_binop_builtin (enum insn_code, tree, rtx);
378static rtx rs6000_expand_ternop_builtin (enum insn_code, tree, rtx);
379static rtx rs6000_expand_builtin (tree, rtx, rtx, enum machine_mode, int);
380static void altivec_init_builtins (void);
381static void rs6000_common_init_builtins (void);
c15c90bb 382static void rs6000_init_libfuncs (void);
a2369ed3 383
b20a9cca
AM
384static void enable_mask_for_builtins (struct builtin_description *, int,
385 enum rs6000_builtins,
386 enum rs6000_builtins);
a2369ed3
DJ
387static void spe_init_builtins (void);
388static rtx spe_expand_builtin (tree, rtx, bool *);
389static rtx spe_expand_predicate_builtin (enum insn_code, tree, rtx);
390static rtx spe_expand_evsel_builtin (enum insn_code, tree, rtx);
391static int rs6000_emit_int_cmove (rtx, rtx, rtx, rtx);
d1d0c603
JJ
392static rs6000_stack_t *rs6000_stack_info (void);
393static void debug_stack_info (rs6000_stack_t *);
a2369ed3
DJ
394
395static rtx altivec_expand_builtin (tree, rtx, bool *);
396static rtx altivec_expand_ld_builtin (tree, rtx, bool *);
397static rtx altivec_expand_st_builtin (tree, rtx, bool *);
398static rtx altivec_expand_dst_builtin (tree, rtx, bool *);
399static rtx altivec_expand_abs_builtin (enum insn_code, tree, rtx);
400static rtx altivec_expand_predicate_builtin (enum insn_code,
401 const char *, tree, rtx);
b4a62fa0 402static rtx altivec_expand_lv_builtin (enum insn_code, tree, rtx);
a2369ed3
DJ
403static rtx altivec_expand_stv_builtin (enum insn_code, tree);
404static void rs6000_parse_abi_options (void);
405static void rs6000_parse_alignment_option (void);
406static void rs6000_parse_tls_size_option (void);
5da702b1 407static void rs6000_parse_yes_no_option (const char *, const char *, int *);
a2369ed3
DJ
408static int first_altivec_reg_to_save (void);
409static unsigned int compute_vrsave_mask (void);
410static void is_altivec_return_reg (rtx, void *);
411static rtx generate_set_vrsave (rtx, rs6000_stack_t *, int);
412int easy_vector_constant (rtx, enum machine_mode);
413static int easy_vector_same (rtx, enum machine_mode);
414static bool is_ev64_opaque_type (tree);
415static rtx rs6000_dwarf_register_span (rtx);
416static rtx rs6000_legitimize_tls_address (rtx, enum tls_model);
417static rtx rs6000_tls_get_addr (void);
418static rtx rs6000_got_sym (void);
419static inline int rs6000_tls_symbol_ref_1 (rtx *, void *);
420static const char *rs6000_get_some_local_dynamic_name (void);
421static int rs6000_get_some_local_dynamic_name_1 (rtx *, void *);
ded9bf77 422static rtx rs6000_complex_function_value (enum machine_mode);
b20a9cca 423static rtx rs6000_spe_function_arg (CUMULATIVE_ARGS *,
a2369ed3 424 enum machine_mode, tree);
b78d48dd
FJ
425static rtx rs6000_mixed_function_arg (CUMULATIVE_ARGS *,
426 enum machine_mode, tree, int);
b1917422 427static void rs6000_move_block_from_reg (int regno, rtx x, int nregs);
c6e8c921
GK
428static void setup_incoming_varargs (CUMULATIVE_ARGS *,
429 enum machine_mode, tree,
430 int *, int);
efdba735
SH
431#if TARGET_MACHO
432static void macho_branch_islands (void);
433static void add_compiler_branch_island (tree, tree, int);
434static int no_previous_def (tree function_name);
435static tree get_prev_label (tree function_name);
436#endif
437
c35d187f 438static tree rs6000_build_builtin_va_list (void);
17211ab5
GK
439
440/* Hash table stuff for keeping track of TOC entries. */
441
442struct toc_hash_struct GTY(())
443{
444 /* `key' will satisfy CONSTANT_P; in fact, it will satisfy
445 ASM_OUTPUT_SPECIAL_POOL_ENTRY_P. */
446 rtx key;
447 enum machine_mode key_mode;
448 int labelno;
449};
450
451static GTY ((param_is (struct toc_hash_struct))) htab_t toc_hash_table;
c81bebd7
MM
452\f
453/* Default register names. */
454char rs6000_reg_names[][8] =
455{
802a0058
MM
456 "0", "1", "2", "3", "4", "5", "6", "7",
457 "8", "9", "10", "11", "12", "13", "14", "15",
458 "16", "17", "18", "19", "20", "21", "22", "23",
459 "24", "25", "26", "27", "28", "29", "30", "31",
460 "0", "1", "2", "3", "4", "5", "6", "7",
461 "8", "9", "10", "11", "12", "13", "14", "15",
462 "16", "17", "18", "19", "20", "21", "22", "23",
463 "24", "25", "26", "27", "28", "29", "30", "31",
464 "mq", "lr", "ctr","ap",
465 "0", "1", "2", "3", "4", "5", "6", "7",
0ac081f6
AH
466 "xer",
467 /* AltiVec registers. */
0cd5e3a1
AH
468 "0", "1", "2", "3", "4", "5", "6", "7",
469 "8", "9", "10", "11", "12", "13", "14", "15",
470 "16", "17", "18", "19", "20", "21", "22", "23",
471 "24", "25", "26", "27", "28", "29", "30", "31",
59a4c851
AH
472 "vrsave", "vscr",
473 /* SPE registers. */
474 "spe_acc", "spefscr"
c81bebd7
MM
475};
476
477#ifdef TARGET_REGNAMES
8b60264b 478static const char alt_reg_names[][8] =
c81bebd7 479{
802a0058
MM
480 "%r0", "%r1", "%r2", "%r3", "%r4", "%r5", "%r6", "%r7",
481 "%r8", "%r9", "%r10", "%r11", "%r12", "%r13", "%r14", "%r15",
482 "%r16", "%r17", "%r18", "%r19", "%r20", "%r21", "%r22", "%r23",
483 "%r24", "%r25", "%r26", "%r27", "%r28", "%r29", "%r30", "%r31",
484 "%f0", "%f1", "%f2", "%f3", "%f4", "%f5", "%f6", "%f7",
485 "%f8", "%f9", "%f10", "%f11", "%f12", "%f13", "%f14", "%f15",
486 "%f16", "%f17", "%f18", "%f19", "%f20", "%f21", "%f22", "%f23",
487 "%f24", "%f25", "%f26", "%f27", "%f28", "%f29", "%f30", "%f31",
488 "mq", "lr", "ctr", "ap",
489 "%cr0", "%cr1", "%cr2", "%cr3", "%cr4", "%cr5", "%cr6", "%cr7",
0ac081f6 490 "xer",
59a4c851 491 /* AltiVec registers. */
0ac081f6 492 "%v0", "%v1", "%v2", "%v3", "%v4", "%v5", "%v6", "%v7",
59a4c851
AH
493 "%v8", "%v9", "%v10", "%v11", "%v12", "%v13", "%v14", "%v15",
494 "%v16", "%v17", "%v18", "%v19", "%v20", "%v21", "%v22", "%v23",
495 "%v24", "%v25", "%v26", "%v27", "%v28", "%v29", "%v30", "%v31",
496 "vrsave", "vscr",
497 /* SPE registers. */
498 "spe_acc", "spefscr"
c81bebd7
MM
499};
500#endif
9878760c 501\f
daf11973
MM
502#ifndef MASK_STRICT_ALIGN
503#define MASK_STRICT_ALIGN 0
504#endif
ffcfcb5f
AM
505#ifndef TARGET_PROFILE_KERNEL
506#define TARGET_PROFILE_KERNEL 0
507#endif
3961e8fe
RH
508
509/* The VRSAVE bitmask puts bit %v0 as the most significant bit. */
510#define ALTIVEC_REG_BIT(REGNO) (0x80000000 >> ((REGNO) - FIRST_ALTIVEC_REGNO))
c4501e62
JJ
511
512/* Return 1 for a symbol ref for a thread-local storage symbol. */
513#define RS6000_SYMBOL_REF_TLS_P(RTX) \
514 (GET_CODE (RTX) == SYMBOL_REF && SYMBOL_REF_TLS_MODEL (RTX) != 0)
672a6f42
NB
515\f
516/* Initialize the GCC target structure. */
91d231cb
JM
517#undef TARGET_ATTRIBUTE_TABLE
518#define TARGET_ATTRIBUTE_TABLE rs6000_attribute_table
a5c76ee6
ZW
519#undef TARGET_SET_DEFAULT_TYPE_ATTRIBUTES
520#define TARGET_SET_DEFAULT_TYPE_ATTRIBUTES rs6000_set_default_type_attributes
daf11973 521
301d03af
RS
522#undef TARGET_ASM_ALIGNED_DI_OP
523#define TARGET_ASM_ALIGNED_DI_OP DOUBLE_INT_ASM_OP
524
525/* Default unaligned ops are only provided for ELF. Find the ops needed
526 for non-ELF systems. */
527#ifndef OBJECT_FORMAT_ELF
cbaaba19 528#if TARGET_XCOFF
ae6c1efd 529/* For XCOFF. rs6000_assemble_integer will handle unaligned DIs on
301d03af
RS
530 64-bit targets. */
531#undef TARGET_ASM_UNALIGNED_HI_OP
532#define TARGET_ASM_UNALIGNED_HI_OP "\t.vbyte\t2,"
533#undef TARGET_ASM_UNALIGNED_SI_OP
534#define TARGET_ASM_UNALIGNED_SI_OP "\t.vbyte\t4,"
535#undef TARGET_ASM_UNALIGNED_DI_OP
536#define TARGET_ASM_UNALIGNED_DI_OP "\t.vbyte\t8,"
537#else
538/* For Darwin. */
539#undef TARGET_ASM_UNALIGNED_HI_OP
540#define TARGET_ASM_UNALIGNED_HI_OP "\t.short\t"
541#undef TARGET_ASM_UNALIGNED_SI_OP
542#define TARGET_ASM_UNALIGNED_SI_OP "\t.long\t"
543#endif
544#endif
545
546/* This hook deals with fixups for relocatable code and DI-mode objects
547 in 64-bit code. */
548#undef TARGET_ASM_INTEGER
549#define TARGET_ASM_INTEGER rs6000_assemble_integer
550
93638d7a
AM
551#ifdef HAVE_GAS_HIDDEN
552#undef TARGET_ASM_ASSEMBLE_VISIBILITY
553#define TARGET_ASM_ASSEMBLE_VISIBILITY rs6000_assemble_visibility
554#endif
555
c4501e62
JJ
556#undef TARGET_HAVE_TLS
557#define TARGET_HAVE_TLS HAVE_AS_TLS
558
559#undef TARGET_CANNOT_FORCE_CONST_MEM
560#define TARGET_CANNOT_FORCE_CONST_MEM rs6000_tls_referenced_p
561
08c148a8
NB
562#undef TARGET_ASM_FUNCTION_PROLOGUE
563#define TARGET_ASM_FUNCTION_PROLOGUE rs6000_output_function_prologue
564#undef TARGET_ASM_FUNCTION_EPILOGUE
565#define TARGET_ASM_FUNCTION_EPILOGUE rs6000_output_function_epilogue
566
b54cf83a
DE
567#undef TARGET_SCHED_USE_DFA_PIPELINE_INTERFACE
568#define TARGET_SCHED_USE_DFA_PIPELINE_INTERFACE rs6000_use_dfa_pipeline_interface
b54cf83a
DE
569#undef TARGET_SCHED_VARIABLE_ISSUE
570#define TARGET_SCHED_VARIABLE_ISSUE rs6000_variable_issue
571
c237e94a
ZW
572#undef TARGET_SCHED_ISSUE_RATE
573#define TARGET_SCHED_ISSUE_RATE rs6000_issue_rate
574#undef TARGET_SCHED_ADJUST_COST
575#define TARGET_SCHED_ADJUST_COST rs6000_adjust_cost
576#undef TARGET_SCHED_ADJUST_PRIORITY
577#define TARGET_SCHED_ADJUST_PRIORITY rs6000_adjust_priority
569fa502
DN
578#undef TARGET_SCHED_IS_COSTLY_DEPENDENCE
579#define TARGET_SCHED_IS_COSTLY_DEPENDENCE rs6000_is_costly_dependence
cbe26ab8
DN
580#undef TARGET_SCHED_FINISH
581#define TARGET_SCHED_FINISH rs6000_sched_finish
c237e94a 582
be12c2b0
VM
583#undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD
584#define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD rs6000_use_sched_lookahead
585
0ac081f6
AH
586#undef TARGET_INIT_BUILTINS
587#define TARGET_INIT_BUILTINS rs6000_init_builtins
588
589#undef TARGET_EXPAND_BUILTIN
590#define TARGET_EXPAND_BUILTIN rs6000_expand_builtin
591
f18eca82
ZL
592#undef TARGET_MANGLE_FUNDAMENTAL_TYPE
593#define TARGET_MANGLE_FUNDAMENTAL_TYPE rs6000_mangle_fundamental_type
594
c15c90bb
ZW
595#undef TARGET_INIT_LIBFUNCS
596#define TARGET_INIT_LIBFUNCS rs6000_init_libfuncs
597
f1384257 598#if TARGET_MACHO
0e5dbd9b
DE
599#undef TARGET_BINDS_LOCAL_P
600#define TARGET_BINDS_LOCAL_P rs6000_binds_local_p
f1384257 601#endif
0e5dbd9b 602
3961e8fe
RH
603#undef TARGET_ASM_OUTPUT_MI_THUNK
604#define TARGET_ASM_OUTPUT_MI_THUNK rs6000_output_mi_thunk
605
3961e8fe 606#undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
5b71a4e7 607#define TARGET_ASM_CAN_OUTPUT_MI_THUNK hook_bool_tree_hwi_hwi_tree_true
00b960c7 608
4977bab6
ZW
609#undef TARGET_FUNCTION_OK_FOR_SIBCALL
610#define TARGET_FUNCTION_OK_FOR_SIBCALL rs6000_function_ok_for_sibcall
611
3c50106f
RH
612#undef TARGET_RTX_COSTS
613#define TARGET_RTX_COSTS rs6000_rtx_costs
dcefdf67
RH
614#undef TARGET_ADDRESS_COST
615#define TARGET_ADDRESS_COST hook_int_rtx_0
3c50106f 616
c8e4f0e9
AH
617#undef TARGET_VECTOR_OPAQUE_P
618#define TARGET_VECTOR_OPAQUE_P is_ev64_opaque_type
62e1dfcf 619
96714395
AH
620#undef TARGET_DWARF_REGISTER_SPAN
621#define TARGET_DWARF_REGISTER_SPAN rs6000_dwarf_register_span
622
c6e8c921
GK
623/* On rs6000, function arguments are promoted, as are function return
624 values. */
625#undef TARGET_PROMOTE_FUNCTION_ARGS
626#define TARGET_PROMOTE_FUNCTION_ARGS hook_bool_tree_true
627#undef TARGET_PROMOTE_FUNCTION_RETURN
628#define TARGET_PROMOTE_FUNCTION_RETURN hook_bool_tree_true
629
c6e8c921
GK
630#undef TARGET_RETURN_IN_MEMORY
631#define TARGET_RETURN_IN_MEMORY rs6000_return_in_memory
632
633#undef TARGET_SETUP_INCOMING_VARARGS
634#define TARGET_SETUP_INCOMING_VARARGS setup_incoming_varargs
635
636/* Always strict argument naming on rs6000. */
637#undef TARGET_STRICT_ARGUMENT_NAMING
638#define TARGET_STRICT_ARGUMENT_NAMING hook_bool_CUMULATIVE_ARGS_true
639#undef TARGET_PRETEND_OUTGOING_VARARGS_NAMED
640#define TARGET_PRETEND_OUTGOING_VARARGS_NAMED hook_bool_CUMULATIVE_ARGS_true
42ba5130
RH
641#undef TARGET_SPLIT_COMPLEX_ARG
642#define TARGET_SPLIT_COMPLEX_ARG hook_bool_tree_true
c6e8c921 643
c35d187f
RH
644#undef TARGET_BUILD_BUILTIN_VA_LIST
645#define TARGET_BUILD_BUILTIN_VA_LIST rs6000_build_builtin_va_list
646
f6897b10 647struct gcc_target targetm = TARGET_INITIALIZER;
672a6f42 648\f
5248c961
RK
649/* Override command line options. Mostly we process the processor
650 type and sometimes adjust other TARGET_ options. */
651
652void
d779d0dc 653rs6000_override_options (const char *default_cpu)
5248c961 654{
c4d38ccb 655 size_t i, j;
8e3f41e7 656 struct rs6000_cpu_select *ptr;
66188a7e 657 int set_masks;
5248c961 658
66188a7e 659 /* Simplifications for entries below. */
85638c0d 660
66188a7e
GK
661 enum {
662 POWERPC_BASE_MASK = MASK_POWERPC | MASK_NEW_MNEMONICS,
663 POWERPC_7400_MASK = POWERPC_BASE_MASK | MASK_PPC_GFXOPT | MASK_ALTIVEC
664 };
85638c0d 665
66188a7e
GK
666 /* This table occasionally claims that a processor does not support
667 a particular feature even though it does, but the feature is slower
668 than the alternative. Thus, it shouldn't be relied on as a
669 complete description of the processor's support.
670
671 Please keep this list in order, and don't forget to update the
672 documentation in invoke.texi when adding a new processor or
673 flag. */
5248c961
RK
674 static struct ptt
675 {
8b60264b
KG
676 const char *const name; /* Canonical processor name. */
677 const enum processor_type processor; /* Processor type enum value. */
678 const int target_enable; /* Target flags to enable. */
8b60264b 679 } const processor_target_table[]
66188a7e 680 = {{"401", PROCESSOR_PPC403, POWERPC_BASE_MASK | MASK_SOFT_FLOAT},
49a0b204 681 {"403", PROCESSOR_PPC403,
66188a7e
GK
682 POWERPC_BASE_MASK | MASK_SOFT_FLOAT | MASK_STRICT_ALIGN},
683 {"405", PROCESSOR_PPC405, POWERPC_BASE_MASK | MASK_SOFT_FLOAT},
684 {"405fp", PROCESSOR_PPC405, POWERPC_BASE_MASK},
685 {"440", PROCESSOR_PPC440, POWERPC_BASE_MASK | MASK_SOFT_FLOAT},
686 {"440fp", PROCESSOR_PPC440, POWERPC_BASE_MASK},
687 {"505", PROCESSOR_MPCCORE, POWERPC_BASE_MASK},
5248c961 688 {"601", PROCESSOR_PPC601,
66188a7e
GK
689 MASK_POWER | POWERPC_BASE_MASK | MASK_MULTIPLE | MASK_STRING},
690 {"602", PROCESSOR_PPC603, POWERPC_BASE_MASK | MASK_PPC_GFXOPT},
691 {"603", PROCESSOR_PPC603, POWERPC_BASE_MASK | MASK_PPC_GFXOPT},
692 {"603e", PROCESSOR_PPC603, POWERPC_BASE_MASK | MASK_PPC_GFXOPT},
693 {"604", PROCESSOR_PPC604, POWERPC_BASE_MASK | MASK_PPC_GFXOPT},
694 {"604e", PROCESSOR_PPC604e, POWERPC_BASE_MASK | MASK_PPC_GFXOPT},
7ddb6568
AM
695 {"620", PROCESSOR_PPC620,
696 POWERPC_BASE_MASK | MASK_PPC_GFXOPT | MASK_POWERPC64},
697 {"630", PROCESSOR_PPC630,
698 POWERPC_BASE_MASK | MASK_PPC_GFXOPT | MASK_POWERPC64},
66188a7e
GK
699 {"740", PROCESSOR_PPC750, POWERPC_BASE_MASK | MASK_PPC_GFXOPT},
700 {"7400", PROCESSOR_PPC7400, POWERPC_7400_MASK},
701 {"7450", PROCESSOR_PPC7450, POWERPC_7400_MASK},
702 {"750", PROCESSOR_PPC750, POWERPC_BASE_MASK | MASK_PPC_GFXOPT},
703 {"801", PROCESSOR_MPCCORE, POWERPC_BASE_MASK | MASK_SOFT_FLOAT},
704 {"821", PROCESSOR_MPCCORE, POWERPC_BASE_MASK | MASK_SOFT_FLOAT},
705 {"823", PROCESSOR_MPCCORE, POWERPC_BASE_MASK | MASK_SOFT_FLOAT},
706 {"8540", PROCESSOR_PPC8540, POWERPC_BASE_MASK | MASK_PPC_GFXOPT},
707 {"860", PROCESSOR_MPCCORE, POWERPC_BASE_MASK | MASK_SOFT_FLOAT},
7177e720 708 {"970", PROCESSOR_POWER4,
66188a7e
GK
709 POWERPC_7400_MASK | MASK_PPC_GPOPT | MASK_MFCRF | MASK_POWERPC64},
710 {"common", PROCESSOR_COMMON, MASK_NEW_MNEMONICS},
711 {"ec603e", PROCESSOR_PPC603, POWERPC_BASE_MASK | MASK_SOFT_FLOAT},
712 {"G3", PROCESSOR_PPC750, POWERPC_BASE_MASK | MASK_PPC_GFXOPT},
713 {"G4", PROCESSOR_PPC7450, POWERPC_7400_MASK},
49ffe578 714 {"G5", PROCESSOR_POWER4,
66188a7e
GK
715 POWERPC_7400_MASK | MASK_PPC_GPOPT | MASK_MFCRF | MASK_POWERPC64},
716 {"power", PROCESSOR_POWER, MASK_POWER | MASK_MULTIPLE | MASK_STRING},
717 {"power2", PROCESSOR_POWER,
718 MASK_POWER | MASK_POWER2 | MASK_MULTIPLE | MASK_STRING},
7ddb6568
AM
719 {"power3", PROCESSOR_PPC630,
720 POWERPC_BASE_MASK | MASK_PPC_GFXOPT | MASK_POWERPC64},
721 {"power4", PROCESSOR_POWER4,
722 POWERPC_BASE_MASK | MASK_PPC_GFXOPT | MASK_POWERPC64},
ec507f2d
DE
723 {"power5", PROCESSOR_POWER5,
724 POWERPC_BASE_MASK | MASK_PPC_GFXOPT | MASK_POWERPC64},
66188a7e
GK
725 {"powerpc", PROCESSOR_POWERPC, POWERPC_BASE_MASK},
726 {"powerpc64", PROCESSOR_POWERPC64,
727 POWERPC_BASE_MASK | MASK_POWERPC64},
728 {"rios", PROCESSOR_RIOS1, MASK_POWER | MASK_MULTIPLE | MASK_STRING},
729 {"rios1", PROCESSOR_RIOS1, MASK_POWER | MASK_MULTIPLE | MASK_STRING},
730 {"rios2", PROCESSOR_RIOS2,
731 MASK_POWER | MASK_POWER2 | MASK_MULTIPLE | MASK_STRING},
732 {"rsc", PROCESSOR_PPC601, MASK_POWER | MASK_MULTIPLE | MASK_STRING},
733 {"rsc1", PROCESSOR_PPC601, MASK_POWER | MASK_MULTIPLE | MASK_STRING},
7ddb6568 734 {"rs64a", PROCESSOR_RS64A, POWERPC_BASE_MASK | MASK_POWERPC64},
66188a7e 735 };
5248c961 736
ca7558fc 737 const size_t ptt_size = ARRAY_SIZE (processor_target_table);
5248c961 738
66188a7e
GK
739 /* Some OSs don't support saving the high part of 64-bit registers on
740 context switch. Other OSs don't support saving Altivec registers.
741 On those OSs, we don't touch the MASK_POWERPC64 or MASK_ALTIVEC
742 settings; if the user wants either, the user must explicitly specify
743 them and we won't interfere with the user's specification. */
744
745 enum {
746 POWER_MASKS = MASK_POWER | MASK_POWER2 | MASK_MULTIPLE | MASK_STRING,
747 POWERPC_MASKS = (POWERPC_BASE_MASK | MASK_PPC_GPOPT
748 | MASK_PPC_GFXOPT | MASK_POWERPC64 | MASK_ALTIVEC
749 | MASK_MFCRF)
750 };
751 set_masks = POWER_MASKS | POWERPC_MASKS | MASK_SOFT_FLOAT;
752#ifdef OS_MISSING_POWERPC64
753 if (OS_MISSING_POWERPC64)
754 set_masks &= ~MASK_POWERPC64;
755#endif
756#ifdef OS_MISSING_ALTIVEC
757 if (OS_MISSING_ALTIVEC)
758 set_masks &= ~MASK_ALTIVEC;
759#endif
760
957211c3
AM
761 /* Don't override these by the processor default if given explicitly. */
762 set_masks &= ~(target_flags_explicit
763 & (MASK_MULTIPLE | MASK_STRING | MASK_SOFT_FLOAT));
764
a4f6c312 765 /* Identify the processor type. */
8e3f41e7 766 rs6000_select[0].string = default_cpu;
3cb999d8 767 rs6000_cpu = TARGET_POWERPC64 ? PROCESSOR_DEFAULT64 : PROCESSOR_DEFAULT;
8e3f41e7 768
b6a1cbae 769 for (i = 0; i < ARRAY_SIZE (rs6000_select); i++)
5248c961 770 {
8e3f41e7
MM
771 ptr = &rs6000_select[i];
772 if (ptr->string != (char *)0 && ptr->string[0] != '\0')
5248c961 773 {
8e3f41e7
MM
774 for (j = 0; j < ptt_size; j++)
775 if (! strcmp (ptr->string, processor_target_table[j].name))
776 {
777 if (ptr->set_tune_p)
778 rs6000_cpu = processor_target_table[j].processor;
779
780 if (ptr->set_arch_p)
781 {
66188a7e
GK
782 target_flags &= ~set_masks;
783 target_flags |= (processor_target_table[j].target_enable
784 & set_masks);
8e3f41e7
MM
785 }
786 break;
787 }
788
4406229e 789 if (j == ptt_size)
8e3f41e7 790 error ("bad value (%s) for %s switch", ptr->string, ptr->name);
5248c961
RK
791 }
792 }
8a61d227 793
993f19a8 794 if (TARGET_E500)
a3170dc6
AH
795 rs6000_isel = 1;
796
dff9f1b6
DE
797 /* If we are optimizing big endian systems for space, use the load/store
798 multiple and string instructions. */
ef792183 799 if (BYTES_BIG_ENDIAN && optimize_size)
957211c3 800 target_flags |= ~target_flags_explicit & (MASK_MULTIPLE | MASK_STRING);
938937d8 801
a4f6c312
SS
802 /* Don't allow -mmultiple or -mstring on little endian systems
803 unless the cpu is a 750, because the hardware doesn't support the
804 instructions used in little endian mode, and causes an alignment
805 trap. The 750 does not cause an alignment trap (except when the
806 target is unaligned). */
bef84347 807
b21fb038 808 if (!BYTES_BIG_ENDIAN && rs6000_cpu != PROCESSOR_PPC750)
7e69e155
MM
809 {
810 if (TARGET_MULTIPLE)
811 {
812 target_flags &= ~MASK_MULTIPLE;
b21fb038 813 if ((target_flags_explicit & MASK_MULTIPLE) != 0)
7e69e155
MM
814 warning ("-mmultiple is not supported on little endian systems");
815 }
816
817 if (TARGET_STRING)
818 {
819 target_flags &= ~MASK_STRING;
b21fb038 820 if ((target_flags_explicit & MASK_STRING) != 0)
938937d8 821 warning ("-mstring is not supported on little endian systems");
7e69e155
MM
822 }
823 }
3933e0e1 824
38c1f2d7
MM
825 /* Set debug flags */
826 if (rs6000_debug_name)
827 {
bfc79d3b 828 if (! strcmp (rs6000_debug_name, "all"))
38c1f2d7 829 rs6000_debug_stack = rs6000_debug_arg = 1;
bfc79d3b 830 else if (! strcmp (rs6000_debug_name, "stack"))
38c1f2d7 831 rs6000_debug_stack = 1;
bfc79d3b 832 else if (! strcmp (rs6000_debug_name, "arg"))
38c1f2d7
MM
833 rs6000_debug_arg = 1;
834 else
c725bd79 835 error ("unknown -mdebug-%s switch", rs6000_debug_name);
38c1f2d7
MM
836 }
837
57ac7be9
AM
838 if (rs6000_traceback_name)
839 {
840 if (! strncmp (rs6000_traceback_name, "full", 4))
841 rs6000_traceback = traceback_full;
842 else if (! strncmp (rs6000_traceback_name, "part", 4))
843 rs6000_traceback = traceback_part;
844 else if (! strncmp (rs6000_traceback_name, "no", 2))
845 rs6000_traceback = traceback_none;
846 else
847 error ("unknown -mtraceback arg `%s'; expecting `full', `partial' or `none'",
848 rs6000_traceback_name);
849 }
850
6fa3f289
ZW
851 /* Set size of long double */
852 rs6000_long_double_type_size = 64;
853 if (rs6000_long_double_size_string)
854 {
855 char *tail;
856 int size = strtol (rs6000_long_double_size_string, &tail, 10);
857 if (*tail != '\0' || (size != 64 && size != 128))
858 error ("Unknown switch -mlong-double-%s",
859 rs6000_long_double_size_string);
860 else
861 rs6000_long_double_type_size = size;
862 }
863
6d0ef01e
HP
864 /* Set Altivec ABI as default for powerpc64 linux. */
865 if (TARGET_ELF && TARGET_64BIT)
866 {
867 rs6000_altivec_abi = 1;
868 rs6000_altivec_vrsave = 1;
869 }
870
0ac081f6
AH
871 /* Handle -mabi= options. */
872 rs6000_parse_abi_options ();
873
025d9908
KH
874 /* Handle -malign-XXXXX option. */
875 rs6000_parse_alignment_option ();
876
5da702b1
AH
877 /* Handle generic -mFOO=YES/NO options. */
878 rs6000_parse_yes_no_option ("vrsave", rs6000_altivec_vrsave_string,
879 &rs6000_altivec_vrsave);
880 rs6000_parse_yes_no_option ("isel", rs6000_isel_string,
881 &rs6000_isel);
882 rs6000_parse_yes_no_option ("spe", rs6000_spe_string, &rs6000_spe);
883 rs6000_parse_yes_no_option ("float-gprs", rs6000_float_gprs_string,
884 &rs6000_float_gprs);
993f19a8 885
c4501e62
JJ
886 /* Handle -mtls-size option. */
887 rs6000_parse_tls_size_option ();
888
a7ae18e2
AH
889#ifdef SUBTARGET_OVERRIDE_OPTIONS
890 SUBTARGET_OVERRIDE_OPTIONS;
891#endif
892#ifdef SUBSUBTARGET_OVERRIDE_OPTIONS
893 SUBSUBTARGET_OVERRIDE_OPTIONS;
894#endif
895
5da702b1
AH
896 if (TARGET_E500)
897 {
898 /* The e500 does not have string instructions, and we set
899 MASK_STRING above when optimizing for size. */
900 if ((target_flags & MASK_STRING) != 0)
901 target_flags = target_flags & ~MASK_STRING;
b6e59a3a
AH
902
903 /* No SPE means 64-bit long doubles, even if an E500. */
904 if (rs6000_spe_string != 0
905 && !strcmp (rs6000_spe_string, "no"))
906 rs6000_long_double_type_size = 64;
5da702b1
AH
907 }
908 else if (rs6000_select[1].string != NULL)
909 {
910 /* For the powerpc-eabispe configuration, we set all these by
911 default, so let's unset them if we manually set another
912 CPU that is not the E500. */
913 if (rs6000_abi_string == 0)
914 rs6000_spe_abi = 0;
915 if (rs6000_spe_string == 0)
916 rs6000_spe = 0;
917 if (rs6000_float_gprs_string == 0)
918 rs6000_float_gprs = 0;
919 if (rs6000_isel_string == 0)
920 rs6000_isel = 0;
b6e59a3a
AH
921 if (rs6000_long_double_size_string == 0)
922 rs6000_long_double_type_size = 64;
5da702b1 923 }
b5044283 924
ec507f2d
DE
925 rs6000_always_hint = (rs6000_cpu != PROCESSOR_POWER4
926 && rs6000_cpu != PROCESSOR_POWER5);
927 rs6000_sched_groups = (rs6000_cpu == PROCESSOR_POWER4
928 || rs6000_cpu == PROCESSOR_POWER5);
929
a5c76ee6
ZW
930 /* Handle -m(no-)longcall option. This is a bit of a cheap hack,
931 using TARGET_OPTIONS to handle a toggle switch, but we're out of
932 bits in target_flags so TARGET_SWITCHES cannot be used.
933 Assumption here is that rs6000_longcall_switch points into the
934 text of the complete option, rather than being a copy, so we can
935 scan back for the presence or absence of the no- modifier. */
936 if (rs6000_longcall_switch)
937 {
938 const char *base = rs6000_longcall_switch;
939 while (base[-1] != 'm') base--;
940
941 if (*rs6000_longcall_switch != '\0')
942 error ("invalid option `%s'", base);
943 rs6000_default_long_calls = (base[0] != 'n');
944 }
945
8bb418a3
ZL
946 /* Handle -m(no-)warn-altivec-long similarly. */
947 if (rs6000_warn_altivec_long_switch)
948 {
949 const char *base = rs6000_warn_altivec_long_switch;
950 while (base[-1] != 'm') base--;
951
952 if (*rs6000_warn_altivec_long_switch != '\0')
953 error ("invalid option `%s'", base);
954 rs6000_warn_altivec_long = (base[0] != 'n');
955 }
956
cbe26ab8 957 /* Handle -mprioritize-restricted-insns option. */
ec507f2d
DE
958 rs6000_sched_restricted_insns_priority
959 = (rs6000_sched_groups ? 1 : 0);
79ae11c4
DN
960 if (rs6000_sched_restricted_insns_priority_str)
961 rs6000_sched_restricted_insns_priority =
962 atoi (rs6000_sched_restricted_insns_priority_str);
963
569fa502 964 /* Handle -msched-costly-dep option. */
ec507f2d
DE
965 rs6000_sched_costly_dep
966 = (rs6000_sched_groups ? store_to_load_dep_costly : no_dep_costly);
569fa502
DN
967 if (rs6000_sched_costly_dep_str)
968 {
969 if (! strcmp (rs6000_sched_costly_dep_str, "no"))
970 rs6000_sched_costly_dep = no_dep_costly;
971 else if (! strcmp (rs6000_sched_costly_dep_str, "all"))
972 rs6000_sched_costly_dep = all_deps_costly;
973 else if (! strcmp (rs6000_sched_costly_dep_str, "true_store_to_load"))
974 rs6000_sched_costly_dep = true_store_to_load_dep_costly;
975 else if (! strcmp (rs6000_sched_costly_dep_str, "store_to_load"))
976 rs6000_sched_costly_dep = store_to_load_dep_costly;
cbe26ab8
DN
977 else
978 rs6000_sched_costly_dep = atoi (rs6000_sched_costly_dep_str);
979 }
980
981 /* Handle -minsert-sched-nops option. */
ec507f2d
DE
982 rs6000_sched_insert_nops
983 = (rs6000_sched_groups ? sched_finish_regroup_exact : sched_finish_none);
cbe26ab8
DN
984 if (rs6000_sched_insert_nops_str)
985 {
986 if (! strcmp (rs6000_sched_insert_nops_str, "no"))
987 rs6000_sched_insert_nops = sched_finish_none;
988 else if (! strcmp (rs6000_sched_insert_nops_str, "pad"))
989 rs6000_sched_insert_nops = sched_finish_pad_groups;
990 else if (! strcmp (rs6000_sched_insert_nops_str, "regroup_exact"))
991 rs6000_sched_insert_nops = sched_finish_regroup_exact;
992 else
993 rs6000_sched_insert_nops = atoi (rs6000_sched_insert_nops_str);
569fa502
DN
994 }
995
c81bebd7 996#ifdef TARGET_REGNAMES
a4f6c312
SS
997 /* If the user desires alternate register names, copy in the
998 alternate names now. */
c81bebd7 999 if (TARGET_REGNAMES)
4e135bdd 1000 memcpy (rs6000_reg_names, alt_reg_names, sizeof (rs6000_reg_names));
c81bebd7
MM
1001#endif
1002
6fa3f289
ZW
1003 /* Set TARGET_AIX_STRUCT_RET last, after the ABI is determined.
1004 If -maix-struct-return or -msvr4-struct-return was explicitly
1005 used, don't override with the ABI default. */
b21fb038 1006 if ((target_flags_explicit & MASK_AIX_STRUCT_RET) == 0)
6fa3f289
ZW
1007 {
1008 if (DEFAULT_ABI == ABI_V4 && !DRAFT_V4_STRUCT_RET)
1009 target_flags = (target_flags & ~MASK_AIX_STRUCT_RET);
1010 else
1011 target_flags |= MASK_AIX_STRUCT_RET;
1012 }
1013
fcce224d
DE
1014 if (TARGET_LONG_DOUBLE_128
1015 && (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_DARWIN))
70a01792 1016 REAL_MODE_FORMAT (TFmode) = &ibm_extended_format;
fcce224d 1017
9ebbca7d
GK
1018 /* Allocate an alias set for register saves & restores from stack. */
1019 rs6000_sr_alias_set = new_alias_set ();
1020
1021 if (TARGET_TOC)
1022 ASM_GENERATE_INTERNAL_LABEL (toc_label_name, "LCTOC", 1);
71f123ca 1023
301d03af
RS
1024 /* We can only guarantee the availability of DI pseudo-ops when
1025 assembling for 64-bit targets. */
ae6c1efd 1026 if (!TARGET_64BIT)
301d03af
RS
1027 {
1028 targetm.asm_out.aligned_op.di = NULL;
1029 targetm.asm_out.unaligned_op.di = NULL;
1030 }
1031
2792d578
DE
1032 /* Set maximum branch target alignment at two instructions, eight bytes. */
1033 align_jumps_max_skip = 8;
1034 align_loops_max_skip = 8;
1035
71f123ca
FS
1036 /* Arrange to save and restore machine status around nested functions. */
1037 init_machine_status = rs6000_init_machine_status;
42ba5130
RH
1038
1039 /* We should always be splitting complex arguments, but we can't break
1040 Linux and Darwin ABIs at the moment. For now, only AIX is fixed. */
1041 if (DEFAULT_ABI != ABI_AIX)
1042 targetm.calls.split_complex_arg = NULL;
5248c961 1043}
5accd822 1044
5da702b1
AH
1045/* Handle generic options of the form -mfoo=yes/no.
1046 NAME is the option name.
1047 VALUE is the option value.
1048 FLAG is the pointer to the flag where to store a 1 or 0, depending on
1049 whether the option value is 'yes' or 'no' respectively. */
993f19a8 1050static void
5da702b1 1051rs6000_parse_yes_no_option (const char *name, const char *value, int *flag)
993f19a8 1052{
5da702b1 1053 if (value == 0)
993f19a8 1054 return;
5da702b1
AH
1055 else if (!strcmp (value, "yes"))
1056 *flag = 1;
1057 else if (!strcmp (value, "no"))
1058 *flag = 0;
08b57fb3 1059 else
5da702b1 1060 error ("unknown -m%s= option specified: '%s'", name, value);
08b57fb3
AH
1061}
1062
0ac081f6 1063/* Handle -mabi= options. */
00b960c7 1064static void
863d938c 1065rs6000_parse_abi_options (void)
0ac081f6
AH
1066{
1067 if (rs6000_abi_string == 0)
1068 return;
1069 else if (! strcmp (rs6000_abi_string, "altivec"))
5cc73f91
AH
1070 {
1071 rs6000_altivec_abi = 1;
1072 rs6000_spe_abi = 0;
1073 }
76a773f3
AH
1074 else if (! strcmp (rs6000_abi_string, "no-altivec"))
1075 rs6000_altivec_abi = 0;
a3170dc6 1076 else if (! strcmp (rs6000_abi_string, "spe"))
01f4962d
NS
1077 {
1078 rs6000_spe_abi = 1;
5cc73f91 1079 rs6000_altivec_abi = 0;
01f4962d
NS
1080 if (!TARGET_SPE_ABI)
1081 error ("not configured for ABI: '%s'", rs6000_abi_string);
1082 }
1083
a3170dc6
AH
1084 else if (! strcmp (rs6000_abi_string, "no-spe"))
1085 rs6000_spe_abi = 0;
0ac081f6 1086 else
c725bd79 1087 error ("unknown ABI specified: '%s'", rs6000_abi_string);
0ac081f6
AH
1088}
1089
025d9908
KH
1090/* Handle -malign-XXXXXX options. */
1091static void
863d938c 1092rs6000_parse_alignment_option (void)
025d9908 1093{
b20a9cca
AM
1094 if (rs6000_alignment_string == 0)
1095 return;
1096 else if (! strcmp (rs6000_alignment_string, "power"))
025d9908
KH
1097 rs6000_alignment_flags = MASK_ALIGN_POWER;
1098 else if (! strcmp (rs6000_alignment_string, "natural"))
1099 rs6000_alignment_flags = MASK_ALIGN_NATURAL;
1100 else
1101 error ("unknown -malign-XXXXX option specified: '%s'",
1102 rs6000_alignment_string);
1103}
1104
c4501e62
JJ
1105/* Validate and record the size specified with the -mtls-size option. */
1106
1107static void
863d938c 1108rs6000_parse_tls_size_option (void)
c4501e62
JJ
1109{
1110 if (rs6000_tls_size_string == 0)
1111 return;
1112 else if (strcmp (rs6000_tls_size_string, "16") == 0)
1113 rs6000_tls_size = 16;
1114 else if (strcmp (rs6000_tls_size_string, "32") == 0)
1115 rs6000_tls_size = 32;
1116 else if (strcmp (rs6000_tls_size_string, "64") == 0)
1117 rs6000_tls_size = 64;
1118 else
1119 error ("bad value `%s' for -mtls-size switch", rs6000_tls_size_string);
1120}
1121
5accd822 1122void
a2369ed3 1123optimization_options (int level ATTRIBUTE_UNUSED, int size ATTRIBUTE_UNUSED)
5accd822 1124{
5accd822 1125}
3cfa4909
MM
1126\f
1127/* Do anything needed at the start of the asm file. */
1128
1bc7c5b6 1129static void
863d938c 1130rs6000_file_start (void)
3cfa4909 1131{
c4d38ccb 1132 size_t i;
3cfa4909 1133 char buffer[80];
d330fd93 1134 const char *start = buffer;
3cfa4909 1135 struct rs6000_cpu_select *ptr;
1bc7c5b6
ZW
1136 const char *default_cpu = TARGET_CPU_DEFAULT;
1137 FILE *file = asm_out_file;
1138
1139 default_file_start ();
1140
1141#ifdef TARGET_BI_ARCH
1142 if ((TARGET_DEFAULT ^ target_flags) & MASK_64BIT)
1143 default_cpu = 0;
1144#endif
3cfa4909
MM
1145
1146 if (flag_verbose_asm)
1147 {
1148 sprintf (buffer, "\n%s rs6000/powerpc options:", ASM_COMMENT_START);
1149 rs6000_select[0].string = default_cpu;
1150
b6a1cbae 1151 for (i = 0; i < ARRAY_SIZE (rs6000_select); i++)
3cfa4909
MM
1152 {
1153 ptr = &rs6000_select[i];
1154 if (ptr->string != (char *)0 && ptr->string[0] != '\0')
1155 {
1156 fprintf (file, "%s %s%s", start, ptr->name, ptr->string);
1157 start = "";
1158 }
1159 }
1160
b91da81f 1161#ifdef USING_ELFOS_H
3cfa4909
MM
1162 switch (rs6000_sdata)
1163 {
1164 case SDATA_NONE: fprintf (file, "%s -msdata=none", start); start = ""; break;
1165 case SDATA_DATA: fprintf (file, "%s -msdata=data", start); start = ""; break;
1166 case SDATA_SYSV: fprintf (file, "%s -msdata=sysv", start); start = ""; break;
1167 case SDATA_EABI: fprintf (file, "%s -msdata=eabi", start); start = ""; break;
1168 }
1169
1170 if (rs6000_sdata && g_switch_value)
1171 {
307b599c
MK
1172 fprintf (file, "%s -G " HOST_WIDE_INT_PRINT_UNSIGNED, start,
1173 g_switch_value);
3cfa4909
MM
1174 start = "";
1175 }
1176#endif
1177
1178 if (*start == '\0')
949ea356 1179 putc ('\n', file);
3cfa4909
MM
1180 }
1181}
5248c961 1182\f
a0ab749a 1183/* Return nonzero if this function is known to have a null epilogue. */
9878760c
RK
1184
1185int
863d938c 1186direct_return (void)
9878760c 1187{
4697a36c
MM
1188 if (reload_completed)
1189 {
1190 rs6000_stack_t *info = rs6000_stack_info ();
1191
1192 if (info->first_gp_reg_save == 32
1193 && info->first_fp_reg_save == 64
00b960c7 1194 && info->first_altivec_reg_save == LAST_ALTIVEC_REGNO + 1
c81fc13e
DE
1195 && ! info->lr_save_p
1196 && ! info->cr_save_p
00b960c7 1197 && info->vrsave_mask == 0
c81fc13e 1198 && ! info->push_p)
4697a36c
MM
1199 return 1;
1200 }
1201
1202 return 0;
9878760c
RK
1203}
1204
1205/* Returns 1 always. */
1206
1207int
a2369ed3
DJ
1208any_operand (rtx op ATTRIBUTE_UNUSED,
1209 enum machine_mode mode ATTRIBUTE_UNUSED)
9878760c
RK
1210{
1211 return 1;
1212}
1213
a4f6c312 1214/* Returns 1 if op is the count register. */
38c1f2d7 1215int
a2369ed3 1216count_register_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
b6c9286a
MM
1217{
1218 if (GET_CODE (op) != REG)
1219 return 0;
1220
1221 if (REGNO (op) == COUNT_REGISTER_REGNUM)
1222 return 1;
1223
1224 if (REGNO (op) > FIRST_PSEUDO_REGISTER)
1225 return 1;
1226
1227 return 0;
1228}
1229
0ec4e2a8
AH
1230/* Returns 1 if op is an altivec register. */
1231int
a2369ed3 1232altivec_register_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
0ec4e2a8
AH
1233{
1234
1235 return (register_operand (op, mode)
1236 && (GET_CODE (op) != REG
1237 || REGNO (op) > FIRST_PSEUDO_REGISTER
1238 || ALTIVEC_REGNO_P (REGNO (op))));
1239}
1240
38c1f2d7 1241int
a2369ed3 1242xer_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
802a0058
MM
1243{
1244 if (GET_CODE (op) != REG)
1245 return 0;
1246
9ebbca7d 1247 if (XER_REGNO_P (REGNO (op)))
802a0058
MM
1248 return 1;
1249
802a0058
MM
1250 return 0;
1251}
1252
c859cda6 1253/* Return 1 if OP is a signed 8-bit constant. Int multiplication
6f317ef3 1254 by such constants completes more quickly. */
c859cda6
DJ
1255
1256int
a2369ed3 1257s8bit_cint_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
c859cda6
DJ
1258{
1259 return ( GET_CODE (op) == CONST_INT
1260 && (INTVAL (op) >= -128 && INTVAL (op) <= 127));
1261}
1262
9878760c
RK
1263/* Return 1 if OP is a constant that can fit in a D field. */
1264
1265int
a2369ed3 1266short_cint_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
9878760c 1267{
5f59ecb7
DE
1268 return (GET_CODE (op) == CONST_INT
1269 && CONST_OK_FOR_LETTER_P (INTVAL (op), 'I'));
9878760c
RK
1270}
1271
5519a4f9 1272/* Similar for an unsigned D field. */
9878760c
RK
1273
1274int
a2369ed3 1275u_short_cint_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
9878760c 1276{
19684119 1277 return (GET_CODE (op) == CONST_INT
c1f11548 1278 && CONST_OK_FOR_LETTER_P (INTVAL (op) & GET_MODE_MASK (mode), 'K'));
9878760c
RK
1279}
1280
dcfedcd0
RK
1281/* Return 1 if OP is a CONST_INT that cannot fit in a signed D field. */
1282
1283int
a2369ed3 1284non_short_cint_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
dcfedcd0
RK
1285{
1286 return (GET_CODE (op) == CONST_INT
a7653a2c 1287 && (unsigned HOST_WIDE_INT) (INTVAL (op) + 0x8000) >= 0x10000);
dcfedcd0
RK
1288}
1289
2bfcf297
DB
1290/* Returns 1 if OP is a CONST_INT that is a positive value
1291 and an exact power of 2. */
1292
1293int
a2369ed3 1294exact_log2_cint_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2bfcf297
DB
1295{
1296 return (GET_CODE (op) == CONST_INT
1297 && INTVAL (op) > 0
1298 && exact_log2 (INTVAL (op)) >= 0);
1299}
1300
9878760c
RK
1301/* Returns 1 if OP is a register that is not special (i.e., not MQ,
1302 ctr, or lr). */
1303
1304int
a2369ed3 1305gpc_reg_operand (rtx op, enum machine_mode mode)
9878760c
RK
1306{
1307 return (register_operand (op, mode)
802a0058 1308 && (GET_CODE (op) != REG
9ebbca7d
GK
1309 || (REGNO (op) >= ARG_POINTER_REGNUM
1310 && !XER_REGNO_P (REGNO (op)))
1311 || REGNO (op) < MQ_REGNO));
9878760c
RK
1312}
1313
1314/* Returns 1 if OP is either a pseudo-register or a register denoting a
1315 CR field. */
1316
1317int
a2369ed3 1318cc_reg_operand (rtx op, enum machine_mode mode)
9878760c
RK
1319{
1320 return (register_operand (op, mode)
1321 && (GET_CODE (op) != REG
1322 || REGNO (op) >= FIRST_PSEUDO_REGISTER
1323 || CR_REGNO_P (REGNO (op))));
1324}
1325
815cdc52
MM
1326/* Returns 1 if OP is either a pseudo-register or a register denoting a
1327 CR field that isn't CR0. */
1328
1329int
a2369ed3 1330cc_reg_not_cr0_operand (rtx op, enum machine_mode mode)
815cdc52
MM
1331{
1332 return (register_operand (op, mode)
1333 && (GET_CODE (op) != REG
1334 || REGNO (op) >= FIRST_PSEUDO_REGISTER
1335 || CR_REGNO_NOT_CR0_P (REGNO (op))));
1336}
1337
a4f6c312
SS
1338/* Returns 1 if OP is either a constant integer valid for a D-field or
1339 a non-special register. If a register, it must be in the proper
1340 mode unless MODE is VOIDmode. */
9878760c
RK
1341
1342int
a2369ed3 1343reg_or_short_operand (rtx op, enum machine_mode mode)
9878760c 1344{
f5a28898 1345 return short_cint_operand (op, mode) || gpc_reg_operand (op, mode);
9878760c
RK
1346}
1347
a4f6c312
SS
1348/* Similar, except check if the negation of the constant would be
1349 valid for a D-field. */
9878760c
RK
1350
1351int
a2369ed3 1352reg_or_neg_short_operand (rtx op, enum machine_mode mode)
9878760c
RK
1353{
1354 if (GET_CODE (op) == CONST_INT)
1355 return CONST_OK_FOR_LETTER_P (INTVAL (op), 'P');
1356
cd2b37d9 1357 return gpc_reg_operand (op, mode);
9878760c
RK
1358}
1359
768070a0
TR
1360/* Returns 1 if OP is either a constant integer valid for a DS-field or
1361 a non-special register. If a register, it must be in the proper
1362 mode unless MODE is VOIDmode. */
1363
1364int
a2369ed3 1365reg_or_aligned_short_operand (rtx op, enum machine_mode mode)
768070a0
TR
1366{
1367 if (gpc_reg_operand (op, mode))
1368 return 1;
1369 else if (short_cint_operand (op, mode) && !(INTVAL (op) & 3))
1370 return 1;
1371
1372 return 0;
1373}
1374
1375
a4f6c312
SS
1376/* Return 1 if the operand is either a register or an integer whose
1377 high-order 16 bits are zero. */
9878760c
RK
1378
1379int
a2369ed3 1380reg_or_u_short_operand (rtx op, enum machine_mode mode)
9878760c 1381{
e675f625 1382 return u_short_cint_operand (op, mode) || gpc_reg_operand (op, mode);
9878760c
RK
1383}
1384
1385/* Return 1 is the operand is either a non-special register or ANY
1386 constant integer. */
1387
1388int
a2369ed3 1389reg_or_cint_operand (rtx op, enum machine_mode mode)
9878760c 1390{
a4f6c312 1391 return (GET_CODE (op) == CONST_INT || gpc_reg_operand (op, mode));
f6bf7de2
DE
1392}
1393
1394/* Return 1 is the operand is either a non-special register or ANY
1395 32-bit signed constant integer. */
1396
1397int
a2369ed3 1398reg_or_arith_cint_operand (rtx op, enum machine_mode mode)
f6bf7de2 1399{
a4f6c312
SS
1400 return (gpc_reg_operand (op, mode)
1401 || (GET_CODE (op) == CONST_INT
f6bf7de2 1402#if HOST_BITS_PER_WIDE_INT != 32
a4f6c312
SS
1403 && ((unsigned HOST_WIDE_INT) (INTVAL (op) + 0x80000000)
1404 < (unsigned HOST_WIDE_INT) 0x100000000ll)
f6bf7de2 1405#endif
a4f6c312 1406 ));
9878760c
RK
1407}
1408
2bfcf297
DB
1409/* Return 1 is the operand is either a non-special register or a 32-bit
1410 signed constant integer valid for 64-bit addition. */
1411
1412int
a2369ed3 1413reg_or_add_cint64_operand (rtx op, enum machine_mode mode)
2bfcf297 1414{
a4f6c312
SS
1415 return (gpc_reg_operand (op, mode)
1416 || (GET_CODE (op) == CONST_INT
a65c591c 1417#if HOST_BITS_PER_WIDE_INT == 32
a4f6c312 1418 && INTVAL (op) < 0x7fff8000
a65c591c 1419#else
a4f6c312
SS
1420 && ((unsigned HOST_WIDE_INT) (INTVAL (op) + 0x80008000)
1421 < 0x100000000ll)
2bfcf297 1422#endif
a4f6c312 1423 ));
2bfcf297
DB
1424}
1425
1426/* Return 1 is the operand is either a non-special register or a 32-bit
1427 signed constant integer valid for 64-bit subtraction. */
1428
1429int
a2369ed3 1430reg_or_sub_cint64_operand (rtx op, enum machine_mode mode)
2bfcf297 1431{
a4f6c312
SS
1432 return (gpc_reg_operand (op, mode)
1433 || (GET_CODE (op) == CONST_INT
a65c591c 1434#if HOST_BITS_PER_WIDE_INT == 32
a4f6c312 1435 && (- INTVAL (op)) < 0x7fff8000
a65c591c 1436#else
a4f6c312
SS
1437 && ((unsigned HOST_WIDE_INT) ((- INTVAL (op)) + 0x80008000)
1438 < 0x100000000ll)
2bfcf297 1439#endif
a4f6c312 1440 ));
2bfcf297
DB
1441}
1442
9ebbca7d
GK
1443/* Return 1 is the operand is either a non-special register or ANY
1444 32-bit unsigned constant integer. */
1445
1446int
a2369ed3 1447reg_or_logical_cint_operand (rtx op, enum machine_mode mode)
9ebbca7d 1448{
1d328b19
GK
1449 if (GET_CODE (op) == CONST_INT)
1450 {
1451 if (GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT)
1452 {
1453 if (GET_MODE_BITSIZE (mode) <= 32)
a4f6c312 1454 abort ();
1d328b19
GK
1455
1456 if (INTVAL (op) < 0)
1457 return 0;
1458 }
1459
1460 return ((INTVAL (op) & GET_MODE_MASK (mode)
0858c623 1461 & (~ (unsigned HOST_WIDE_INT) 0xffffffff)) == 0);
1d328b19
GK
1462 }
1463 else if (GET_CODE (op) == CONST_DOUBLE)
1464 {
1465 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
1466 || mode != DImode)
a4f6c312 1467 abort ();
1d328b19
GK
1468
1469 return CONST_DOUBLE_HIGH (op) == 0;
1470 }
1471 else
1472 return gpc_reg_operand (op, mode);
9ebbca7d
GK
1473}
1474
51d3e7d6 1475/* Return 1 if the operand is an operand that can be loaded via the GOT. */
766a866c
MM
1476
1477int
a2369ed3 1478got_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
766a866c
MM
1479{
1480 return (GET_CODE (op) == SYMBOL_REF
1481 || GET_CODE (op) == CONST
1482 || GET_CODE (op) == LABEL_REF);
1483}
1484
38c1f2d7
MM
1485/* Return 1 if the operand is a simple references that can be loaded via
1486 the GOT (labels involving addition aren't allowed). */
1487
1488int
a2369ed3 1489got_no_const_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
38c1f2d7
MM
1490{
1491 return (GET_CODE (op) == SYMBOL_REF || GET_CODE (op) == LABEL_REF);
1492}
1493
4e74d8ec
MM
1494/* Return the number of instructions it takes to form a constant in an
1495 integer register. */
1496
1497static int
a2369ed3 1498num_insns_constant_wide (HOST_WIDE_INT value)
4e74d8ec
MM
1499{
1500 /* signed constant loadable with {cal|addi} */
5f59ecb7 1501 if (CONST_OK_FOR_LETTER_P (value, 'I'))
0865c631
GK
1502 return 1;
1503
4e74d8ec 1504 /* constant loadable with {cau|addis} */
5f59ecb7 1505 else if (CONST_OK_FOR_LETTER_P (value, 'L'))
4e74d8ec
MM
1506 return 1;
1507
5f59ecb7 1508#if HOST_BITS_PER_WIDE_INT == 64
c81fc13e 1509 else if (TARGET_POWERPC64)
4e74d8ec 1510 {
a65c591c
DE
1511 HOST_WIDE_INT low = ((value & 0xffffffff) ^ 0x80000000) - 0x80000000;
1512 HOST_WIDE_INT high = value >> 31;
4e74d8ec 1513
a65c591c 1514 if (high == 0 || high == -1)
4e74d8ec
MM
1515 return 2;
1516
a65c591c 1517 high >>= 1;
4e74d8ec 1518
a65c591c 1519 if (low == 0)
4e74d8ec 1520 return num_insns_constant_wide (high) + 1;
4e74d8ec
MM
1521 else
1522 return (num_insns_constant_wide (high)
e396202a 1523 + num_insns_constant_wide (low) + 1);
4e74d8ec
MM
1524 }
1525#endif
1526
1527 else
1528 return 2;
1529}
1530
1531int
a2369ed3 1532num_insns_constant (rtx op, enum machine_mode mode)
4e74d8ec 1533{
4e74d8ec 1534 if (GET_CODE (op) == CONST_INT)
0d30d435
DE
1535 {
1536#if HOST_BITS_PER_WIDE_INT == 64
4e2c1c44
DE
1537 if ((INTVAL (op) >> 31) != 0 && (INTVAL (op) >> 31) != -1
1538 && mask64_operand (op, mode))
0d30d435
DE
1539 return 2;
1540 else
1541#endif
1542 return num_insns_constant_wide (INTVAL (op));
1543 }
4e74d8ec 1544
6fc48950
MM
1545 else if (GET_CODE (op) == CONST_DOUBLE && mode == SFmode)
1546 {
1547 long l;
1548 REAL_VALUE_TYPE rv;
1549
1550 REAL_VALUE_FROM_CONST_DOUBLE (rv, op);
1551 REAL_VALUE_TO_TARGET_SINGLE (rv, l);
e72247f4 1552 return num_insns_constant_wide ((HOST_WIDE_INT) l);
6fc48950
MM
1553 }
1554
47ad8c61 1555 else if (GET_CODE (op) == CONST_DOUBLE)
4e74d8ec 1556 {
47ad8c61
MM
1557 HOST_WIDE_INT low;
1558 HOST_WIDE_INT high;
1559 long l[2];
1560 REAL_VALUE_TYPE rv;
1561 int endian = (WORDS_BIG_ENDIAN == 0);
4e74d8ec 1562
47ad8c61
MM
1563 if (mode == VOIDmode || mode == DImode)
1564 {
1565 high = CONST_DOUBLE_HIGH (op);
1566 low = CONST_DOUBLE_LOW (op);
1567 }
1568 else
1569 {
1570 REAL_VALUE_FROM_CONST_DOUBLE (rv, op);
1571 REAL_VALUE_TO_TARGET_DOUBLE (rv, l);
1572 high = l[endian];
1573 low = l[1 - endian];
1574 }
4e74d8ec 1575
47ad8c61
MM
1576 if (TARGET_32BIT)
1577 return (num_insns_constant_wide (low)
1578 + num_insns_constant_wide (high));
4e74d8ec
MM
1579
1580 else
47ad8c61 1581 {
e72247f4 1582 if (high == 0 && low >= 0)
47ad8c61
MM
1583 return num_insns_constant_wide (low);
1584
e72247f4 1585 else if (high == -1 && low < 0)
47ad8c61
MM
1586 return num_insns_constant_wide (low);
1587
a260abc9
DE
1588 else if (mask64_operand (op, mode))
1589 return 2;
1590
47ad8c61
MM
1591 else if (low == 0)
1592 return num_insns_constant_wide (high) + 1;
1593
1594 else
1595 return (num_insns_constant_wide (high)
1596 + num_insns_constant_wide (low) + 1);
1597 }
4e74d8ec
MM
1598 }
1599
1600 else
1601 abort ();
1602}
1603
a4f6c312
SS
1604/* Return 1 if the operand is a CONST_DOUBLE and it can be put into a
1605 register with one instruction per word. We only do this if we can
1606 safely read CONST_DOUBLE_{LOW,HIGH}. */
9878760c
RK
1607
1608int
a2369ed3 1609easy_fp_constant (rtx op, enum machine_mode mode)
9878760c 1610{
9878760c
RK
1611 if (GET_CODE (op) != CONST_DOUBLE
1612 || GET_MODE (op) != mode
4e74d8ec 1613 || (GET_MODE_CLASS (mode) != MODE_FLOAT && mode != DImode))
9878760c
RK
1614 return 0;
1615
a4f6c312 1616 /* Consider all constants with -msoft-float to be easy. */
a3170dc6
AH
1617 if ((TARGET_SOFT_FLOAT || !TARGET_FPRS)
1618 && mode != DImode)
b6c9286a
MM
1619 return 1;
1620
a4f6c312 1621 /* If we are using V.4 style PIC, consider all constants to be hard. */
f607bc57 1622 if (flag_pic && DEFAULT_ABI == ABI_V4)
a7273471
MM
1623 return 0;
1624
5ae4759c 1625#ifdef TARGET_RELOCATABLE
a4f6c312
SS
1626 /* Similarly if we are using -mrelocatable, consider all constants
1627 to be hard. */
5ae4759c
MM
1628 if (TARGET_RELOCATABLE)
1629 return 0;
1630#endif
1631
fcce224d
DE
1632 if (mode == TFmode)
1633 {
1634 long k[4];
1635 REAL_VALUE_TYPE rv;
1636
1637 REAL_VALUE_FROM_CONST_DOUBLE (rv, op);
1638 REAL_VALUE_TO_TARGET_LONG_DOUBLE (rv, k);
1639
1640 return (num_insns_constant_wide ((HOST_WIDE_INT) k[0]) == 1
1641 && num_insns_constant_wide ((HOST_WIDE_INT) k[1]) == 1
1642 && num_insns_constant_wide ((HOST_WIDE_INT) k[2]) == 1
1643 && num_insns_constant_wide ((HOST_WIDE_INT) k[3]) == 1);
1644 }
1645
1646 else if (mode == DFmode)
042259f2
DE
1647 {
1648 long k[2];
1649 REAL_VALUE_TYPE rv;
1650
1651 REAL_VALUE_FROM_CONST_DOUBLE (rv, op);
1652 REAL_VALUE_TO_TARGET_DOUBLE (rv, k);
9878760c 1653
a65c591c
DE
1654 return (num_insns_constant_wide ((HOST_WIDE_INT) k[0]) == 1
1655 && num_insns_constant_wide ((HOST_WIDE_INT) k[1]) == 1);
042259f2 1656 }
4e74d8ec
MM
1657
1658 else if (mode == SFmode)
042259f2
DE
1659 {
1660 long l;
1661 REAL_VALUE_TYPE rv;
1662
1663 REAL_VALUE_FROM_CONST_DOUBLE (rv, op);
1664 REAL_VALUE_TO_TARGET_SINGLE (rv, l);
9878760c 1665
4e74d8ec 1666 return num_insns_constant_wide (l) == 1;
042259f2 1667 }
4e74d8ec 1668
a260abc9 1669 else if (mode == DImode)
c81fc13e 1670 return ((TARGET_POWERPC64
a260abc9
DE
1671 && GET_CODE (op) == CONST_DOUBLE && CONST_DOUBLE_LOW (op) == 0)
1672 || (num_insns_constant (op, DImode) <= 2));
4e74d8ec 1673
a9098fd0
GK
1674 else if (mode == SImode)
1675 return 1;
4e74d8ec
MM
1676 else
1677 abort ();
9878760c 1678}
8f75773e 1679
72ac76be 1680/* Return nonzero if all elements of a vector have the same value. */
69ef87e2
AH
1681
1682static int
a2369ed3 1683easy_vector_same (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
d744e06e
AH
1684{
1685 int units, i, cst;
1686
1687 units = CONST_VECTOR_NUNITS (op);
1688
1689 cst = INTVAL (CONST_VECTOR_ELT (op, 0));
1690 for (i = 1; i < units; ++i)
1691 if (INTVAL (CONST_VECTOR_ELT (op, i)) != cst)
1692 break;
1693 if (i == units)
1694 return 1;
1695 return 0;
1696}
1697
1698/* Return 1 if the operand is a CONST_INT and can be put into a
1699 register without using memory. */
1700
1701int
a2369ed3 1702easy_vector_constant (rtx op, enum machine_mode mode)
69ef87e2 1703{
d744e06e 1704 int cst, cst2;
69ef87e2 1705
d744e06e
AH
1706 if (GET_CODE (op) != CONST_VECTOR
1707 || (!TARGET_ALTIVEC
1708 && !TARGET_SPE))
69ef87e2
AH
1709 return 0;
1710
d744e06e
AH
1711 if (zero_constant (op, mode)
1712 && ((TARGET_ALTIVEC && ALTIVEC_VECTOR_MODE (mode))
1713 || (TARGET_SPE && SPE_VECTOR_MODE (mode))))
1714 return 1;
69ef87e2 1715
d744e06e
AH
1716 if (GET_MODE_CLASS (mode) != MODE_VECTOR_INT)
1717 return 0;
1718
f5119d10
AH
1719 if (TARGET_SPE && mode == V1DImode)
1720 return 0;
1721
d744e06e
AH
1722 cst = INTVAL (CONST_VECTOR_ELT (op, 0));
1723 cst2 = INTVAL (CONST_VECTOR_ELT (op, 1));
1724
1725 /* Limit SPE vectors to 15 bits signed. These we can generate with:
1726 li r0, CONSTANT1
1727 evmergelo r0, r0, r0
1728 li r0, CONSTANT2
1729
1730 I don't know how efficient it would be to allow bigger constants,
1731 considering we'll have an extra 'ori' for every 'li'. I doubt 5
1732 instructions is better than a 64-bit memory load, but I don't
1733 have the e500 timing specs. */
1734 if (TARGET_SPE && mode == V2SImode
1735 && cst >= -0x7fff && cst <= 0x7fff
f5119d10 1736 && cst2 >= -0x7fff && cst2 <= 0x7fff)
d744e06e
AH
1737 return 1;
1738
ce1f50b2
HP
1739 if (TARGET_ALTIVEC)
1740 switch (mode)
1741 {
1742 case V4SImode:
1743 if (EASY_VECTOR_15 (cst, op, mode))
1744 return 1;
1745 if ((cst & 0xffff) != ((cst >> 16) & 0xffff))
1746 break;
1747 cst = cst >> 16;
1748 case V8HImode:
1749 if (EASY_VECTOR_15 (cst, op, mode))
1750 return 1;
1751 if ((cst & 0xff) != ((cst >> 8) & 0xff))
1752 break;
1753 cst = cst >> 8;
1754 case V16QImode:
1755 if (EASY_VECTOR_15 (cst, op, mode))
1756 return 1;
1757 default:
1758 break;
1759 }
d744e06e
AH
1760
1761 if (TARGET_ALTIVEC && EASY_VECTOR_15_ADD_SELF (cst, op, mode))
1762 return 1;
1763
1764 return 0;
1765}
1766
1767/* Same as easy_vector_constant but only for EASY_VECTOR_15_ADD_SELF. */
1768
1769int
a2369ed3 1770easy_vector_constant_add_self (rtx op, enum machine_mode mode)
d744e06e
AH
1771{
1772 int cst;
1773
1774 if (!easy_vector_constant (op, mode))
1775 return 0;
1776
1777 cst = INTVAL (CONST_VECTOR_ELT (op, 0));
1778
1779 return TARGET_ALTIVEC && EASY_VECTOR_15_ADD_SELF (cst, op, mode);
1780}
1781
1782const char *
a2369ed3 1783output_vec_const_move (rtx *operands)
d744e06e
AH
1784{
1785 int cst, cst2;
1786 enum machine_mode mode;
1787 rtx dest, vec;
1788
1789 dest = operands[0];
1790 vec = operands[1];
69ef87e2 1791
d744e06e
AH
1792 cst = INTVAL (CONST_VECTOR_ELT (vec, 0));
1793 cst2 = INTVAL (CONST_VECTOR_ELT (vec, 1));
1794 mode = GET_MODE (dest);
69ef87e2 1795
d744e06e
AH
1796 if (TARGET_ALTIVEC)
1797 {
1798 if (zero_constant (vec, mode))
1799 return "vxor %0,%0,%0";
ce1f50b2
HP
1800 else if (EASY_VECTOR_15_ADD_SELF (cst, vec, mode))
1801 return "#";
1802 else if (easy_vector_constant (vec, mode))
98ef3137 1803 {
d744e06e
AH
1804 operands[1] = GEN_INT (cst);
1805 switch (mode)
1806 {
1807 case V4SImode:
ce1f50b2
HP
1808 if (EASY_VECTOR_15 (cst, vec, mode))
1809 {
1810 operands[1] = GEN_INT (cst);
1811 return "vspltisw %0,%1";
1812 }
1813 cst = cst >> 16;
d744e06e 1814 case V8HImode:
ce1f50b2
HP
1815 if (EASY_VECTOR_15 (cst, vec, mode))
1816 {
1817 operands[1] = GEN_INT (cst);
1818 return "vspltish %0,%1";
1819 }
1820 cst = cst >> 8;
d744e06e 1821 case V16QImode:
ce1f50b2
HP
1822 if (EASY_VECTOR_15 (cst, vec, mode))
1823 {
1824 operands[1] = GEN_INT (cst);
1825 return "vspltisb %0,%1";
1826 }
d744e06e
AH
1827 default:
1828 abort ();
1829 }
98ef3137 1830 }
d744e06e
AH
1831 else
1832 abort ();
69ef87e2
AH
1833 }
1834
d744e06e
AH
1835 if (TARGET_SPE)
1836 {
1837 /* Vector constant 0 is handled as a splitter of V2SI, and in the
1838 pattern of V1DI, V4HI, and V2SF.
1839
c1207243 1840 FIXME: We should probably return # and add post reload
d744e06e
AH
1841 splitters for these, but this way is so easy ;-).
1842 */
1843 operands[1] = GEN_INT (cst);
1844 operands[2] = GEN_INT (cst2);
1845 if (cst == cst2)
1846 return "li %0,%1\n\tevmergelo %0,%0,%0";
1847 else
1848 return "li %0,%1\n\tevmergelo %0,%0,%0\n\tli %0,%2";
1849 }
1850
1851 abort ();
69ef87e2
AH
1852}
1853
1854/* Return 1 if the operand is the constant 0. This works for scalars
1855 as well as vectors. */
1856int
a2369ed3 1857zero_constant (rtx op, enum machine_mode mode)
69ef87e2
AH
1858{
1859 return op == CONST0_RTX (mode);
1860}
1861
50a0b056
GK
1862/* Return 1 if the operand is 0.0. */
1863int
a2369ed3 1864zero_fp_constant (rtx op, enum machine_mode mode)
50a0b056
GK
1865{
1866 return GET_MODE_CLASS (mode) == MODE_FLOAT && op == CONST0_RTX (mode);
1867}
1868
a4f6c312
SS
1869/* Return 1 if the operand is in volatile memory. Note that during
1870 the RTL generation phase, memory_operand does not return TRUE for
b6c9286a
MM
1871 volatile memory references. So this function allows us to
1872 recognize volatile references where its safe. */
1873
1874int
a2369ed3 1875volatile_mem_operand (rtx op, enum machine_mode mode)
b6c9286a
MM
1876{
1877 if (GET_CODE (op) != MEM)
1878 return 0;
1879
1880 if (!MEM_VOLATILE_P (op))
1881 return 0;
1882
1883 if (mode != GET_MODE (op))
1884 return 0;
1885
1886 if (reload_completed)
1887 return memory_operand (op, mode);
1888
1889 if (reload_in_progress)
1890 return strict_memory_address_p (mode, XEXP (op, 0));
1891
1892 return memory_address_p (mode, XEXP (op, 0));
1893}
1894
97f6e72f 1895/* Return 1 if the operand is an offsettable memory operand. */
914c2e77
RK
1896
1897int
a2369ed3 1898offsettable_mem_operand (rtx op, enum machine_mode mode)
914c2e77 1899{
97f6e72f 1900 return ((GET_CODE (op) == MEM)
677a9668 1901 && offsettable_address_p (reload_completed || reload_in_progress,
97f6e72f 1902 mode, XEXP (op, 0)));
914c2e77
RK
1903}
1904
9878760c
RK
1905/* Return 1 if the operand is either an easy FP constant (see above) or
1906 memory. */
1907
1908int
a2369ed3 1909mem_or_easy_const_operand (rtx op, enum machine_mode mode)
9878760c
RK
1910{
1911 return memory_operand (op, mode) || easy_fp_constant (op, mode);
1912}
1913
1914/* Return 1 if the operand is either a non-special register or an item
5f59ecb7 1915 that can be used as the operand of a `mode' add insn. */
9878760c
RK
1916
1917int
a2369ed3 1918add_operand (rtx op, enum machine_mode mode)
9878760c 1919{
2bfcf297 1920 if (GET_CODE (op) == CONST_INT)
e72247f4
DE
1921 return (CONST_OK_FOR_LETTER_P (INTVAL (op), 'I')
1922 || CONST_OK_FOR_LETTER_P (INTVAL (op), 'L'));
2bfcf297
DB
1923
1924 return gpc_reg_operand (op, mode);
9878760c
RK
1925}
1926
dcfedcd0
RK
1927/* Return 1 if OP is a constant but not a valid add_operand. */
1928
1929int
a2369ed3 1930non_add_cint_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
dcfedcd0
RK
1931{
1932 return (GET_CODE (op) == CONST_INT
e72247f4
DE
1933 && !CONST_OK_FOR_LETTER_P (INTVAL (op), 'I')
1934 && !CONST_OK_FOR_LETTER_P (INTVAL (op), 'L'));
dcfedcd0
RK
1935}
1936
9878760c
RK
1937/* Return 1 if the operand is a non-special register or a constant that
1938 can be used as the operand of an OR or XOR insn on the RS/6000. */
1939
1940int
a2369ed3 1941logical_operand (rtx op, enum machine_mode mode)
9878760c 1942{
40501e5f 1943 HOST_WIDE_INT opl, oph;
1d328b19 1944
dfbdccdb
GK
1945 if (gpc_reg_operand (op, mode))
1946 return 1;
1d328b19 1947
dfbdccdb 1948 if (GET_CODE (op) == CONST_INT)
40501e5f
AM
1949 {
1950 opl = INTVAL (op) & GET_MODE_MASK (mode);
1951
1952#if HOST_BITS_PER_WIDE_INT <= 32
1953 if (GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT && opl < 0)
1954 return 0;
1955#endif
1956 }
dfbdccdb
GK
1957 else if (GET_CODE (op) == CONST_DOUBLE)
1958 {
1d328b19 1959 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
40501e5f 1960 abort ();
1d328b19
GK
1961
1962 opl = CONST_DOUBLE_LOW (op);
1963 oph = CONST_DOUBLE_HIGH (op);
40501e5f 1964 if (oph != 0)
38886f37 1965 return 0;
dfbdccdb
GK
1966 }
1967 else
1968 return 0;
1d328b19 1969
40501e5f
AM
1970 return ((opl & ~ (unsigned HOST_WIDE_INT) 0xffff) == 0
1971 || (opl & ~ (unsigned HOST_WIDE_INT) 0xffff0000) == 0);
9878760c
RK
1972}
1973
dcfedcd0 1974/* Return 1 if C is a constant that is not a logical operand (as
1d328b19 1975 above), but could be split into one. */
dcfedcd0
RK
1976
1977int
a2369ed3 1978non_logical_cint_operand (rtx op, enum machine_mode mode)
dcfedcd0 1979{
dfbdccdb 1980 return ((GET_CODE (op) == CONST_INT || GET_CODE (op) == CONST_DOUBLE)
1d328b19
GK
1981 && ! logical_operand (op, mode)
1982 && reg_or_logical_cint_operand (op, mode));
dcfedcd0
RK
1983}
1984
19ba8161 1985/* Return 1 if C is a constant that can be encoded in a 32-bit mask on the
9878760c
RK
1986 RS/6000. It is if there are no more than two 1->0 or 0->1 transitions.
1987 Reject all ones and all zeros, since these should have been optimized
1988 away and confuse the making of MB and ME. */
1989
1990int
a2369ed3 1991mask_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
9878760c 1992{
02071907 1993 HOST_WIDE_INT c, lsb;
9878760c 1994
19ba8161
DE
1995 if (GET_CODE (op) != CONST_INT)
1996 return 0;
1997
1998 c = INTVAL (op);
1999
57deb3a1
AM
2000 /* Fail in 64-bit mode if the mask wraps around because the upper
2001 32-bits of the mask will all be 1s, contrary to GCC's internal view. */
2002 if (TARGET_POWERPC64 && (c & 0x80000001) == 0x80000001)
2003 return 0;
2004
c5059423
AM
2005 /* We don't change the number of transitions by inverting,
2006 so make sure we start with the LS bit zero. */
2007 if (c & 1)
2008 c = ~c;
2009
2010 /* Reject all zeros or all ones. */
2011 if (c == 0)
9878760c
RK
2012 return 0;
2013
c5059423
AM
2014 /* Find the first transition. */
2015 lsb = c & -c;
2016
2017 /* Invert to look for a second transition. */
2018 c = ~c;
9878760c 2019
c5059423
AM
2020 /* Erase first transition. */
2021 c &= -lsb;
9878760c 2022
c5059423
AM
2023 /* Find the second transition (if any). */
2024 lsb = c & -c;
2025
2026 /* Match if all the bits above are 1's (or c is zero). */
2027 return c == -lsb;
9878760c
RK
2028}
2029
0ba1b2ff
AM
2030/* Return 1 for the PowerPC64 rlwinm corner case. */
2031
2032int
a2369ed3 2033mask_operand_wrap (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
0ba1b2ff
AM
2034{
2035 HOST_WIDE_INT c, lsb;
2036
2037 if (GET_CODE (op) != CONST_INT)
2038 return 0;
2039
2040 c = INTVAL (op);
2041
2042 if ((c & 0x80000001) != 0x80000001)
2043 return 0;
2044
2045 c = ~c;
2046 if (c == 0)
2047 return 0;
2048
2049 lsb = c & -c;
2050 c = ~c;
2051 c &= -lsb;
2052 lsb = c & -c;
2053 return c == -lsb;
2054}
2055
a260abc9
DE
2056/* Return 1 if the operand is a constant that is a PowerPC64 mask.
2057 It is if there are no more than one 1->0 or 0->1 transitions.
0ba1b2ff
AM
2058 Reject all zeros, since zero should have been optimized away and
2059 confuses the making of MB and ME. */
9878760c
RK
2060
2061int
a2369ed3 2062mask64_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
a260abc9
DE
2063{
2064 if (GET_CODE (op) == CONST_INT)
2065 {
02071907 2066 HOST_WIDE_INT c, lsb;
a260abc9 2067
c5059423 2068 c = INTVAL (op);
a260abc9 2069
0ba1b2ff 2070 /* Reject all zeros. */
c5059423 2071 if (c == 0)
e2c953b6
DE
2072 return 0;
2073
0ba1b2ff
AM
2074 /* We don't change the number of transitions by inverting,
2075 so make sure we start with the LS bit zero. */
2076 if (c & 1)
2077 c = ~c;
2078
c5059423
AM
2079 /* Find the transition, and check that all bits above are 1's. */
2080 lsb = c & -c;
e3981aab
DE
2081
2082 /* Match if all the bits above are 1's (or c is zero). */
c5059423 2083 return c == -lsb;
e2c953b6 2084 }
0ba1b2ff
AM
2085 return 0;
2086}
2087
2088/* Like mask64_operand, but allow up to three transitions. This
2089 predicate is used by insn patterns that generate two rldicl or
2090 rldicr machine insns. */
2091
2092int
a2369ed3 2093mask64_2_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
0ba1b2ff
AM
2094{
2095 if (GET_CODE (op) == CONST_INT)
a260abc9 2096 {
0ba1b2ff 2097 HOST_WIDE_INT c, lsb;
a260abc9 2098
0ba1b2ff 2099 c = INTVAL (op);
a260abc9 2100
0ba1b2ff
AM
2101 /* Disallow all zeros. */
2102 if (c == 0)
2103 return 0;
a260abc9 2104
0ba1b2ff
AM
2105 /* We don't change the number of transitions by inverting,
2106 so make sure we start with the LS bit zero. */
2107 if (c & 1)
2108 c = ~c;
a260abc9 2109
0ba1b2ff
AM
2110 /* Find the first transition. */
2111 lsb = c & -c;
a260abc9 2112
0ba1b2ff
AM
2113 /* Invert to look for a second transition. */
2114 c = ~c;
2115
2116 /* Erase first transition. */
2117 c &= -lsb;
2118
2119 /* Find the second transition. */
2120 lsb = c & -c;
2121
2122 /* Invert to look for a third transition. */
2123 c = ~c;
2124
2125 /* Erase second transition. */
2126 c &= -lsb;
2127
2128 /* Find the third transition (if any). */
2129 lsb = c & -c;
2130
2131 /* Match if all the bits above are 1's (or c is zero). */
2132 return c == -lsb;
2133 }
2134 return 0;
2135}
2136
2137/* Generates shifts and masks for a pair of rldicl or rldicr insns to
2138 implement ANDing by the mask IN. */
2139void
a2369ed3 2140build_mask64_2_operands (rtx in, rtx *out)
0ba1b2ff
AM
2141{
2142#if HOST_BITS_PER_WIDE_INT >= 64
2143 unsigned HOST_WIDE_INT c, lsb, m1, m2;
2144 int shift;
2145
2146 if (GET_CODE (in) != CONST_INT)
2147 abort ();
2148
2149 c = INTVAL (in);
2150 if (c & 1)
2151 {
2152 /* Assume c initially something like 0x00fff000000fffff. The idea
2153 is to rotate the word so that the middle ^^^^^^ group of zeros
2154 is at the MS end and can be cleared with an rldicl mask. We then
2155 rotate back and clear off the MS ^^ group of zeros with a
2156 second rldicl. */
2157 c = ~c; /* c == 0xff000ffffff00000 */
2158 lsb = c & -c; /* lsb == 0x0000000000100000 */
2159 m1 = -lsb; /* m1 == 0xfffffffffff00000 */
2160 c = ~c; /* c == 0x00fff000000fffff */
2161 c &= -lsb; /* c == 0x00fff00000000000 */
2162 lsb = c & -c; /* lsb == 0x0000100000000000 */
2163 c = ~c; /* c == 0xff000fffffffffff */
2164 c &= -lsb; /* c == 0xff00000000000000 */
2165 shift = 0;
2166 while ((lsb >>= 1) != 0)
2167 shift++; /* shift == 44 on exit from loop */
2168 m1 <<= 64 - shift; /* m1 == 0xffffff0000000000 */
2169 m1 = ~m1; /* m1 == 0x000000ffffffffff */
2170 m2 = ~c; /* m2 == 0x00ffffffffffffff */
a260abc9
DE
2171 }
2172 else
0ba1b2ff
AM
2173 {
2174 /* Assume c initially something like 0xff000f0000000000. The idea
2175 is to rotate the word so that the ^^^ middle group of zeros
2176 is at the LS end and can be cleared with an rldicr mask. We then
2177 rotate back and clear off the LS group of ^^^^^^^^^^ zeros with
2178 a second rldicr. */
2179 lsb = c & -c; /* lsb == 0x0000010000000000 */
2180 m2 = -lsb; /* m2 == 0xffffff0000000000 */
2181 c = ~c; /* c == 0x00fff0ffffffffff */
2182 c &= -lsb; /* c == 0x00fff00000000000 */
2183 lsb = c & -c; /* lsb == 0x0000100000000000 */
2184 c = ~c; /* c == 0xff000fffffffffff */
2185 c &= -lsb; /* c == 0xff00000000000000 */
2186 shift = 0;
2187 while ((lsb >>= 1) != 0)
2188 shift++; /* shift == 44 on exit from loop */
2189 m1 = ~c; /* m1 == 0x00ffffffffffffff */
2190 m1 >>= shift; /* m1 == 0x0000000000000fff */
2191 m1 = ~m1; /* m1 == 0xfffffffffffff000 */
2192 }
2193
2194 /* Note that when we only have two 0->1 and 1->0 transitions, one of the
2195 masks will be all 1's. We are guaranteed more than one transition. */
2196 out[0] = GEN_INT (64 - shift);
2197 out[1] = GEN_INT (m1);
2198 out[2] = GEN_INT (shift);
2199 out[3] = GEN_INT (m2);
2200#else
045572c7
GK
2201 (void)in;
2202 (void)out;
0ba1b2ff
AM
2203 abort ();
2204#endif
a260abc9
DE
2205}
2206
2207/* Return 1 if the operand is either a non-special register or a constant
2208 that can be used as the operand of a PowerPC64 logical AND insn. */
2209
2210int
a2369ed3 2211and64_operand (rtx op, enum machine_mode mode)
9878760c 2212{
a4f6c312 2213 if (fixed_regs[CR0_REGNO]) /* CR0 not available, don't do andi./andis. */
52d3af72
DE
2214 return (gpc_reg_operand (op, mode) || mask64_operand (op, mode));
2215
2216 return (logical_operand (op, mode) || mask64_operand (op, mode));
9878760c
RK
2217}
2218
0ba1b2ff
AM
2219/* Like the above, but also match constants that can be implemented
2220 with two rldicl or rldicr insns. */
2221
2222int
a2369ed3 2223and64_2_operand (rtx op, enum machine_mode mode)
0ba1b2ff 2224{
a3c9585f 2225 if (fixed_regs[CR0_REGNO]) /* CR0 not available, don't do andi./andis. */
0ba1b2ff
AM
2226 return gpc_reg_operand (op, mode) || mask64_2_operand (op, mode);
2227
2228 return logical_operand (op, mode) || mask64_2_operand (op, mode);
2229}
2230
a260abc9
DE
2231/* Return 1 if the operand is either a non-special register or a
2232 constant that can be used as the operand of an RS/6000 logical AND insn. */
dcfedcd0
RK
2233
2234int
a2369ed3 2235and_operand (rtx op, enum machine_mode mode)
dcfedcd0 2236{
a4f6c312 2237 if (fixed_regs[CR0_REGNO]) /* CR0 not available, don't do andi./andis. */
52d3af72
DE
2238 return (gpc_reg_operand (op, mode) || mask_operand (op, mode));
2239
2240 return (logical_operand (op, mode) || mask_operand (op, mode));
dcfedcd0
RK
2241}
2242
9878760c
RK
2243/* Return 1 if the operand is a general register or memory operand. */
2244
2245int
a2369ed3 2246reg_or_mem_operand (rtx op, enum machine_mode mode)
9878760c 2247{
b6c9286a
MM
2248 return (gpc_reg_operand (op, mode)
2249 || memory_operand (op, mode)
4c81e946 2250 || macho_lo_sum_memory_operand (op, mode)
b6c9286a 2251 || volatile_mem_operand (op, mode));
9878760c
RK
2252}
2253
a7a813f7 2254/* Return 1 if the operand is a general register or memory operand without
3cb999d8 2255 pre_inc or pre_dec which produces invalid form of PowerPC lwa
a7a813f7
RK
2256 instruction. */
2257
2258int
a2369ed3 2259lwa_operand (rtx op, enum machine_mode mode)
a7a813f7
RK
2260{
2261 rtx inner = op;
2262
2263 if (reload_completed && GET_CODE (inner) == SUBREG)
2264 inner = SUBREG_REG (inner);
2265
2266 return gpc_reg_operand (inner, mode)
2267 || (memory_operand (inner, mode)
2268 && GET_CODE (XEXP (inner, 0)) != PRE_INC
6a40a9d6
DE
2269 && GET_CODE (XEXP (inner, 0)) != PRE_DEC
2270 && (GET_CODE (XEXP (inner, 0)) != PLUS
e903c96a
DE
2271 || GET_CODE (XEXP (XEXP (inner, 0), 1)) != CONST_INT
2272 || INTVAL (XEXP (XEXP (inner, 0), 1)) % 4 == 0));
a7a813f7
RK
2273}
2274
cc4d5fec
JH
2275/* Return 1 if the operand, used inside a MEM, is a SYMBOL_REF. */
2276
2277int
a2369ed3 2278symbol_ref_operand (rtx op, enum machine_mode mode)
cc4d5fec
JH
2279{
2280 if (mode != VOIDmode && GET_MODE (op) != mode)
2281 return 0;
2282
aa6d25c9
AP
2283#if TARGET_MACHO
2284 if (GET_CODE (op) == SYMBOL_REF && TARGET_MACHO && MACHOPIC_INDIRECT)
2285 {
2286 /* Macho says it has to go through a stub or be local
2287 when indirect mode. Stubs are considered local. */
2288 const char *t = XSTR (op, 0);
2289 /* "&" means that it is it a local defined symbol
2290 so it is okay to call to. */
2291 if (t[0] == '&')
2292 return true;
2293
2294 /* "!T" means that the function is local defined. */
2295 return (t[0] == '!' && t[1] == 'T');
2296 }
2297#endif
2298
2299
473f51b6
DE
2300 return (GET_CODE (op) == SYMBOL_REF
2301 && (DEFAULT_ABI != ABI_AIX || SYMBOL_REF_FUNCTION_P (op)));
cc4d5fec
JH
2302}
2303
9878760c 2304/* Return 1 if the operand, used inside a MEM, is a valid first argument
cc4d5fec 2305 to CALL. This is a SYMBOL_REF, a pseudo-register, LR or CTR. */
9878760c
RK
2306
2307int
a2369ed3 2308call_operand (rtx op, enum machine_mode mode)
9878760c
RK
2309{
2310 if (mode != VOIDmode && GET_MODE (op) != mode)
2311 return 0;
2312
2313 return (GET_CODE (op) == SYMBOL_REF
cc4d5fec
JH
2314 || (GET_CODE (op) == REG
2315 && (REGNO (op) == LINK_REGISTER_REGNUM
2316 || REGNO (op) == COUNT_REGISTER_REGNUM
2317 || REGNO (op) >= FIRST_PSEUDO_REGISTER)));
9878760c
RK
2318}
2319
2af3d377 2320/* Return 1 if the operand is a SYMBOL_REF for a function known to be in
d1908feb 2321 this file. */
2af3d377
RK
2322
2323int
a2369ed3
DJ
2324current_file_function_operand (rtx op,
2325 enum machine_mode mode ATTRIBUTE_UNUSED)
2af3d377 2326{
473f51b6
DE
2327 return (GET_CODE (op) == SYMBOL_REF
2328 && (DEFAULT_ABI != ABI_AIX || SYMBOL_REF_FUNCTION_P (op))
2329 && (SYMBOL_REF_LOCAL_P (op)
2330 || (op == XEXP (DECL_RTL (current_function_decl), 0))));
2af3d377
RK
2331}
2332
9878760c
RK
2333/* Return 1 if this operand is a valid input for a move insn. */
2334
2335int
a2369ed3 2336input_operand (rtx op, enum machine_mode mode)
9878760c 2337{
eb4e8003 2338 /* Memory is always valid. */
9878760c
RK
2339 if (memory_operand (op, mode))
2340 return 1;
2341
34792e82 2342 /* Only a tiny bit of handling for CONSTANT_P_RTX is necessary. */
01b4cf2b 2343 if (GET_CODE (op) == CONSTANT_P_RTX)
34792e82
JL
2344 return 1;
2345
eb4e8003
RK
2346 /* For floating-point, easy constants are valid. */
2347 if (GET_MODE_CLASS (mode) == MODE_FLOAT
2348 && CONSTANT_P (op)
2349 && easy_fp_constant (op, mode))
2350 return 1;
2351
4e74d8ec
MM
2352 /* Allow any integer constant. */
2353 if (GET_MODE_CLASS (mode) == MODE_INT
e675f625 2354 && (GET_CODE (op) == CONST_INT
e675f625 2355 || GET_CODE (op) == CONST_DOUBLE))
4e74d8ec
MM
2356 return 1;
2357
d744e06e
AH
2358 /* Allow easy vector constants. */
2359 if (GET_CODE (op) == CONST_VECTOR
2360 && easy_vector_constant (op, mode))
2361 return 1;
2362
eb4e8003
RK
2363 /* For floating-point or multi-word mode, the only remaining valid type
2364 is a register. */
9878760c
RK
2365 if (GET_MODE_CLASS (mode) == MODE_FLOAT
2366 || GET_MODE_SIZE (mode) > UNITS_PER_WORD)
eb4e8003 2367 return register_operand (op, mode);
9878760c 2368
88fe15a1
RK
2369 /* The only cases left are integral modes one word or smaller (we
2370 do not get called for MODE_CC values). These can be in any
2371 register. */
2372 if (register_operand (op, mode))
a8b3aeda 2373 return 1;
88fe15a1 2374
84cf9dda 2375 /* A SYMBOL_REF referring to the TOC is valid. */
4d588c14 2376 if (legitimate_constant_pool_address_p (op))
84cf9dda
RK
2377 return 1;
2378
9ebbca7d 2379 /* A constant pool expression (relative to the TOC) is valid */
4d588c14 2380 if (toc_relative_expr_p (op))
b6c9286a
MM
2381 return 1;
2382
88228c4b
MM
2383 /* V.4 allows SYMBOL_REFs and CONSTs that are in the small data region
2384 to be valid. */
f607bc57 2385 if (DEFAULT_ABI == ABI_V4
88228c4b
MM
2386 && (GET_CODE (op) == SYMBOL_REF || GET_CODE (op) == CONST)
2387 && small_data_operand (op, Pmode))
2388 return 1;
2389
042259f2 2390 return 0;
9878760c 2391}
7509c759 2392
95727fb8
AP
2393
2394/* Darwin, AIX increases natural record alignment to doubleword if the first
2395 field is an FP double while the FP fields remain word aligned. */
2396
19d66194 2397unsigned int
95727fb8
AP
2398rs6000_special_round_type_align (tree type, int computed, int specified)
2399{
2400 tree field = TYPE_FIELDS (type);
95727fb8
AP
2401
2402 /* Skip all the static variables only if ABI is greater than
71cc389b 2403 1 or equal to 0. */
3ce5437a 2404 while (field != NULL && TREE_CODE (field) == VAR_DECL)
95727fb8
AP
2405 field = TREE_CHAIN (field);
2406
3ce5437a 2407 if (field == NULL || field == type || DECL_MODE (field) != DFmode)
95727fb8
AP
2408 return MAX (computed, specified);
2409
2410 return MAX (MAX (computed, specified), 64);
2411}
2412
a4f6c312 2413/* Return 1 for an operand in small memory on V.4/eabi. */
7509c759
MM
2414
2415int
a2369ed3
DJ
2416small_data_operand (rtx op ATTRIBUTE_UNUSED,
2417 enum machine_mode mode ATTRIBUTE_UNUSED)
7509c759 2418{
38c1f2d7 2419#if TARGET_ELF
5f59ecb7 2420 rtx sym_ref;
7509c759 2421
d9407988 2422 if (rs6000_sdata == SDATA_NONE || rs6000_sdata == SDATA_DATA)
a54d04b7 2423 return 0;
a54d04b7 2424
f607bc57 2425 if (DEFAULT_ABI != ABI_V4)
7509c759
MM
2426 return 0;
2427
88228c4b
MM
2428 if (GET_CODE (op) == SYMBOL_REF)
2429 sym_ref = op;
2430
2431 else if (GET_CODE (op) != CONST
2432 || GET_CODE (XEXP (op, 0)) != PLUS
2433 || GET_CODE (XEXP (XEXP (op, 0), 0)) != SYMBOL_REF
2434 || GET_CODE (XEXP (XEXP (op, 0), 1)) != CONST_INT)
7509c759
MM
2435 return 0;
2436
88228c4b 2437 else
dbf55e53
MM
2438 {
2439 rtx sum = XEXP (op, 0);
2440 HOST_WIDE_INT summand;
2441
2442 /* We have to be careful here, because it is the referenced address
2443 that must be 32k from _SDA_BASE_, not just the symbol. */
2444 summand = INTVAL (XEXP (sum, 1));
307b599c 2445 if (summand < 0 || (unsigned HOST_WIDE_INT) summand > g_switch_value)
dbf55e53
MM
2446 return 0;
2447
2448 sym_ref = XEXP (sum, 0);
2449 }
88228c4b 2450
20bfcd69 2451 return SYMBOL_REF_SMALL_P (sym_ref);
d9407988
MM
2452#else
2453 return 0;
2454#endif
7509c759 2455}
46c07df8 2456
d2288d5d
HP
2457/* Return true, if operand is a memory operand and has a
2458 displacement divisible by 4. */
2459
2460int
2461word_offset_memref_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2462{
2463 rtx addr;
2464 int off = 0;
2465
2466 if (!memory_operand (op, mode))
2467 return 0;
2468
2469 addr = XEXP (op, 0);
2470 if (GET_CODE (addr) == PLUS
2471 && GET_CODE (XEXP (addr, 0)) == REG
2472 && GET_CODE (XEXP (addr, 1)) == CONST_INT)
2473 off = INTVAL (XEXP (addr, 1));
2474
2475 return (off % 4) == 0;
2476}
2477
3a1f863f 2478/* Return true if either operand is a general purpose register. */
46c07df8 2479
3a1f863f
DE
2480bool
2481gpr_or_gpr_p (rtx op0, rtx op1)
46c07df8 2482{
3a1f863f
DE
2483 return ((REG_P (op0) && INT_REGNO_P (REGNO (op0)))
2484 || (REG_P (op1) && INT_REGNO_P (REGNO (op1))));
46c07df8
HP
2485}
2486
9ebbca7d 2487\f
4d588c14
RH
2488/* Subroutines of rs6000_legitimize_address and rs6000_legitimate_address. */
2489
9ebbca7d 2490static int
a2369ed3 2491constant_pool_expr_1 (rtx op, int *have_sym, int *have_toc)
9ebbca7d
GK
2492{
2493 switch (GET_CODE(op))
2494 {
2495 case SYMBOL_REF:
c4501e62
JJ
2496 if (RS6000_SYMBOL_REF_TLS_P (op))
2497 return 0;
2498 else if (CONSTANT_POOL_ADDRESS_P (op))
a4f6c312
SS
2499 {
2500 if (ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (op), Pmode))
2501 {
2502 *have_sym = 1;
2503 return 1;
2504 }
2505 else
2506 return 0;
2507 }
2508 else if (! strcmp (XSTR (op, 0), toc_label_name))
2509 {
2510 *have_toc = 1;
2511 return 1;
2512 }
2513 else
2514 return 0;
9ebbca7d
GK
2515 case PLUS:
2516 case MINUS:
c1f11548
DE
2517 return (constant_pool_expr_1 (XEXP (op, 0), have_sym, have_toc)
2518 && constant_pool_expr_1 (XEXP (op, 1), have_sym, have_toc));
9ebbca7d 2519 case CONST:
a4f6c312 2520 return constant_pool_expr_1 (XEXP (op, 0), have_sym, have_toc);
9ebbca7d 2521 case CONST_INT:
a4f6c312 2522 return 1;
9ebbca7d 2523 default:
a4f6c312 2524 return 0;
9ebbca7d
GK
2525 }
2526}
2527
4d588c14 2528static bool
a2369ed3 2529constant_pool_expr_p (rtx op)
9ebbca7d
GK
2530{
2531 int have_sym = 0;
2532 int have_toc = 0;
2533 return constant_pool_expr_1 (op, &have_sym, &have_toc) && have_sym;
2534}
2535
4d588c14 2536static bool
a2369ed3 2537toc_relative_expr_p (rtx op)
9ebbca7d 2538{
4d588c14
RH
2539 int have_sym = 0;
2540 int have_toc = 0;
2541 return constant_pool_expr_1 (op, &have_sym, &have_toc) && have_toc;
2542}
2543
2544/* SPE offset addressing is limited to 5-bits worth of double words. */
2545#define SPE_CONST_OFFSET_OK(x) (((x) & ~0xf8) == 0)
2546
2547bool
a2369ed3 2548legitimate_constant_pool_address_p (rtx x)
4d588c14
RH
2549{
2550 return (TARGET_TOC
2551 && GET_CODE (x) == PLUS
2552 && GET_CODE (XEXP (x, 0)) == REG
2553 && (TARGET_MINIMAL_TOC || REGNO (XEXP (x, 0)) == TOC_REGISTER)
2554 && constant_pool_expr_p (XEXP (x, 1)));
2555}
2556
2557static bool
a2369ed3 2558legitimate_small_data_p (enum machine_mode mode, rtx x)
4d588c14
RH
2559{
2560 return (DEFAULT_ABI == ABI_V4
2561 && !flag_pic && !TARGET_TOC
2562 && (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == CONST)
2563 && small_data_operand (x, mode));
2564}
2565
2566static bool
a2369ed3 2567legitimate_offset_address_p (enum machine_mode mode, rtx x, int strict)
4d588c14
RH
2568{
2569 unsigned HOST_WIDE_INT offset, extra;
2570
2571 if (GET_CODE (x) != PLUS)
2572 return false;
2573 if (GET_CODE (XEXP (x, 0)) != REG)
2574 return false;
2575 if (!INT_REG_OK_FOR_BASE_P (XEXP (x, 0), strict))
2576 return false;
2577 if (GET_CODE (XEXP (x, 1)) != CONST_INT)
2578 return false;
2579
2580 offset = INTVAL (XEXP (x, 1));
2581 extra = 0;
2582 switch (mode)
2583 {
2584 case V16QImode:
2585 case V8HImode:
2586 case V4SFmode:
2587 case V4SImode:
2588 /* AltiVec vector modes. Only reg+reg addressing is valid here,
2589 which leaves the only valid constant offset of zero, which by
2590 canonicalization rules is also invalid. */
2591 return false;
2592
2593 case V4HImode:
2594 case V2SImode:
2595 case V1DImode:
2596 case V2SFmode:
2597 /* SPE vector modes. */
2598 return SPE_CONST_OFFSET_OK (offset);
2599
2600 case DFmode:
2601 case DImode:
3364872d 2602 if (mode == DFmode || !TARGET_POWERPC64)
4d588c14
RH
2603 extra = 4;
2604 else if (offset & 3)
2605 return false;
2606 break;
2607
2608 case TFmode:
2609 case TImode:
3364872d 2610 if (mode == TFmode || !TARGET_POWERPC64)
4d588c14
RH
2611 extra = 12;
2612 else if (offset & 3)
2613 return false;
2614 else
2615 extra = 8;
2616 break;
2617
2618 default:
2619 break;
2620 }
2621
b1917422
AM
2622 offset += 0x8000;
2623 return (offset < 0x10000) && (offset + extra < 0x10000);
4d588c14
RH
2624}
2625
2626static bool
a2369ed3 2627legitimate_indexed_address_p (rtx x, int strict)
4d588c14
RH
2628{
2629 rtx op0, op1;
2630
2631 if (GET_CODE (x) != PLUS)
2632 return false;
2633 op0 = XEXP (x, 0);
2634 op1 = XEXP (x, 1);
2635
2636 if (!REG_P (op0) || !REG_P (op1))
2637 return false;
2638
2639 return ((INT_REG_OK_FOR_BASE_P (op0, strict)
2640 && INT_REG_OK_FOR_INDEX_P (op1, strict))
2641 || (INT_REG_OK_FOR_BASE_P (op1, strict)
2642 && INT_REG_OK_FOR_INDEX_P (op0, strict)));
9ebbca7d
GK
2643}
2644
4d588c14 2645static inline bool
a2369ed3 2646legitimate_indirect_address_p (rtx x, int strict)
4d588c14
RH
2647{
2648 return GET_CODE (x) == REG && INT_REG_OK_FOR_BASE_P (x, strict);
2649}
2650
4c81e946
FJ
2651static bool
2652macho_lo_sum_memory_operand (rtx x, enum machine_mode mode)
2653{
2654 if (!TARGET_MACHO || !flag_pic
2655 || mode != SImode || GET_CODE(x) != MEM)
2656 return false;
2657 x = XEXP (x, 0);
2658
2659 if (GET_CODE (x) != LO_SUM)
2660 return false;
2661 if (GET_CODE (XEXP (x, 0)) != REG)
2662 return false;
2663 if (!INT_REG_OK_FOR_BASE_P (XEXP (x, 0), 0))
2664 return false;
2665 x = XEXP (x, 1);
2666
2667 return CONSTANT_P (x);
2668}
2669
4d588c14 2670static bool
a2369ed3 2671legitimate_lo_sum_address_p (enum machine_mode mode, rtx x, int strict)
4d588c14
RH
2672{
2673 if (GET_CODE (x) != LO_SUM)
2674 return false;
2675 if (GET_CODE (XEXP (x, 0)) != REG)
2676 return false;
2677 if (!INT_REG_OK_FOR_BASE_P (XEXP (x, 0), strict))
2678 return false;
2679 x = XEXP (x, 1);
2680
8622e235 2681 if (TARGET_ELF || TARGET_MACHO)
4d588c14 2682 {
a29077da 2683 if (DEFAULT_ABI != ABI_AIX && DEFAULT_ABI != ABI_DARWIN && flag_pic)
4d588c14
RH
2684 return false;
2685 if (TARGET_TOC)
2686 return false;
2687 if (GET_MODE_NUNITS (mode) != 1)
2688 return false;
2689 if (GET_MODE_BITSIZE (mode) > 32
2690 && !(TARGET_HARD_FLOAT && TARGET_FPRS && mode == DFmode))
2691 return false;
2692
2693 return CONSTANT_P (x);
2694 }
2695
2696 return false;
2697}
2698
2699
9ebbca7d
GK
2700/* Try machine-dependent ways of modifying an illegitimate address
2701 to be legitimate. If we find one, return the new, valid address.
2702 This is used from only one place: `memory_address' in explow.c.
2703
a4f6c312
SS
2704 OLDX is the address as it was before break_out_memory_refs was
2705 called. In some cases it is useful to look at this to decide what
2706 needs to be done.
9ebbca7d 2707
a4f6c312 2708 MODE is passed so that this function can use GO_IF_LEGITIMATE_ADDRESS.
9ebbca7d 2709
a4f6c312
SS
2710 It is always safe for this function to do nothing. It exists to
2711 recognize opportunities to optimize the output.
9ebbca7d
GK
2712
2713 On RS/6000, first check for the sum of a register with a constant
2714 integer that is out of range. If so, generate code to add the
2715 constant with the low-order 16 bits masked to the register and force
2716 this result into another register (this can be done with `cau').
2717 Then generate an address of REG+(CONST&0xffff), allowing for the
2718 possibility of bit 16 being a one.
2719
2720 Then check for the sum of a register and something not constant, try to
2721 load the other things into a register and return the sum. */
4d588c14 2722
9ebbca7d 2723rtx
a2369ed3
DJ
2724rs6000_legitimize_address (rtx x, rtx oldx ATTRIBUTE_UNUSED,
2725 enum machine_mode mode)
0ac081f6 2726{
c4501e62
JJ
2727 if (GET_CODE (x) == SYMBOL_REF)
2728 {
2729 enum tls_model model = SYMBOL_REF_TLS_MODEL (x);
2730 if (model != 0)
2731 return rs6000_legitimize_tls_address (x, model);
2732 }
2733
9ebbca7d
GK
2734 if (GET_CODE (x) == PLUS
2735 && GET_CODE (XEXP (x, 0)) == REG
2736 && GET_CODE (XEXP (x, 1)) == CONST_INT
2737 && (unsigned HOST_WIDE_INT) (INTVAL (XEXP (x, 1)) + 0x8000) >= 0x10000)
2738 {
2739 HOST_WIDE_INT high_int, low_int;
2740 rtx sum;
a65c591c
DE
2741 low_int = ((INTVAL (XEXP (x, 1)) & 0xffff) ^ 0x8000) - 0x8000;
2742 high_int = INTVAL (XEXP (x, 1)) - low_int;
9ebbca7d
GK
2743 sum = force_operand (gen_rtx_PLUS (Pmode, XEXP (x, 0),
2744 GEN_INT (high_int)), 0);
2745 return gen_rtx_PLUS (Pmode, sum, GEN_INT (low_int));
2746 }
2747 else if (GET_CODE (x) == PLUS
2748 && GET_CODE (XEXP (x, 0)) == REG
2749 && GET_CODE (XEXP (x, 1)) != CONST_INT
6ac7bf2c 2750 && GET_MODE_NUNITS (mode) == 1
a3170dc6
AH
2751 && ((TARGET_HARD_FLOAT && TARGET_FPRS)
2752 || TARGET_POWERPC64
fcce224d 2753 || (mode != DFmode && mode != TFmode))
9ebbca7d
GK
2754 && (TARGET_POWERPC64 || mode != DImode)
2755 && mode != TImode)
2756 {
2757 return gen_rtx_PLUS (Pmode, XEXP (x, 0),
2758 force_reg (Pmode, force_operand (XEXP (x, 1), 0)));
2759 }
0ac081f6
AH
2760 else if (ALTIVEC_VECTOR_MODE (mode))
2761 {
2762 rtx reg;
2763
2764 /* Make sure both operands are registers. */
2765 if (GET_CODE (x) == PLUS)
9f85ed45 2766 return gen_rtx_PLUS (Pmode, force_reg (Pmode, XEXP (x, 0)),
0ac081f6
AH
2767 force_reg (Pmode, XEXP (x, 1)));
2768
2769 reg = force_reg (Pmode, x);
2770 return reg;
2771 }
a3170dc6
AH
2772 else if (SPE_VECTOR_MODE (mode))
2773 {
2774 /* We accept [reg + reg] and [reg + OFFSET]. */
2775
2776 if (GET_CODE (x) == PLUS)
2777 {
2778 rtx op1 = XEXP (x, 0);
2779 rtx op2 = XEXP (x, 1);
2780
2781 op1 = force_reg (Pmode, op1);
2782
2783 if (GET_CODE (op2) != REG
2784 && (GET_CODE (op2) != CONST_INT
2785 || !SPE_CONST_OFFSET_OK (INTVAL (op2))))
2786 op2 = force_reg (Pmode, op2);
2787
2788 return gen_rtx_PLUS (Pmode, op1, op2);
2789 }
2790
2791 return force_reg (Pmode, x);
2792 }
f1384257
AM
2793 else if (TARGET_ELF
2794 && TARGET_32BIT
2795 && TARGET_NO_TOC
2796 && ! flag_pic
9ebbca7d
GK
2797 && GET_CODE (x) != CONST_INT
2798 && GET_CODE (x) != CONST_DOUBLE
2799 && CONSTANT_P (x)
6ac7bf2c
GK
2800 && GET_MODE_NUNITS (mode) == 1
2801 && (GET_MODE_BITSIZE (mode) <= 32
a3170dc6 2802 || ((TARGET_HARD_FLOAT && TARGET_FPRS) && mode == DFmode)))
9ebbca7d
GK
2803 {
2804 rtx reg = gen_reg_rtx (Pmode);
8a1977f3
GK
2805 emit_insn (gen_elf_high (reg, x));
2806 return gen_rtx_LO_SUM (Pmode, reg, x);
9ebbca7d 2807 }
ee890fe2
SS
2808 else if (TARGET_MACHO && TARGET_32BIT && TARGET_NO_TOC
2809 && ! flag_pic
ab82a49f
AP
2810#if TARGET_MACHO
2811 && ! MACHO_DYNAMIC_NO_PIC_P
2812#endif
ee890fe2
SS
2813 && GET_CODE (x) != CONST_INT
2814 && GET_CODE (x) != CONST_DOUBLE
2815 && CONSTANT_P (x)
a3170dc6 2816 && ((TARGET_HARD_FLOAT && TARGET_FPRS) || mode != DFmode)
ee890fe2
SS
2817 && mode != DImode
2818 && mode != TImode)
2819 {
2820 rtx reg = gen_reg_rtx (Pmode);
8a1977f3
GK
2821 emit_insn (gen_macho_high (reg, x));
2822 return gen_rtx_LO_SUM (Pmode, reg, x);
ee890fe2 2823 }
9ebbca7d 2824 else if (TARGET_TOC
4d588c14 2825 && constant_pool_expr_p (x)
a9098fd0 2826 && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (x), Pmode))
9ebbca7d
GK
2827 {
2828 return create_TOC_reference (x);
2829 }
2830 else
2831 return NULL_RTX;
2832}
258bfae2 2833
c973d557
JJ
2834/* This is called from dwarf2out.c via ASM_OUTPUT_DWARF_DTPREL.
2835 We need to emit DTP-relative relocations. */
2836
2837void
2838rs6000_output_dwarf_dtprel (FILE *file, int size, rtx x)
2839{
2840 switch (size)
2841 {
2842 case 4:
2843 fputs ("\t.long\t", file);
2844 break;
2845 case 8:
2846 fputs (DOUBLE_INT_ASM_OP, file);
2847 break;
2848 default:
2849 abort ();
2850 }
2851 output_addr_const (file, x);
2852 fputs ("@dtprel+0x8000", file);
2853}
2854
c4501e62
JJ
2855/* Construct the SYMBOL_REF for the tls_get_addr function. */
2856
2857static GTY(()) rtx rs6000_tls_symbol;
2858static rtx
863d938c 2859rs6000_tls_get_addr (void)
c4501e62
JJ
2860{
2861 if (!rs6000_tls_symbol)
2862 rs6000_tls_symbol = init_one_libfunc ("__tls_get_addr");
2863
2864 return rs6000_tls_symbol;
2865}
2866
2867/* Construct the SYMBOL_REF for TLS GOT references. */
2868
2869static GTY(()) rtx rs6000_got_symbol;
2870static rtx
863d938c 2871rs6000_got_sym (void)
c4501e62
JJ
2872{
2873 if (!rs6000_got_symbol)
2874 {
2875 rs6000_got_symbol = gen_rtx_SYMBOL_REF (Pmode, "_GLOBAL_OFFSET_TABLE_");
2876 SYMBOL_REF_FLAGS (rs6000_got_symbol) |= SYMBOL_FLAG_LOCAL;
2877 SYMBOL_REF_FLAGS (rs6000_got_symbol) |= SYMBOL_FLAG_EXTERNAL;
2878 }
2879
2880 return rs6000_got_symbol;
2881}
2882
2883/* ADDR contains a thread-local SYMBOL_REF. Generate code to compute
2884 this (thread-local) address. */
2885
2886static rtx
a2369ed3 2887rs6000_legitimize_tls_address (rtx addr, enum tls_model model)
c4501e62
JJ
2888{
2889 rtx dest, insn;
2890
2891 dest = gen_reg_rtx (Pmode);
2892 if (model == TLS_MODEL_LOCAL_EXEC && rs6000_tls_size == 16)
2893 {
2894 rtx tlsreg;
2895
2896 if (TARGET_64BIT)
2897 {
2898 tlsreg = gen_rtx_REG (Pmode, 13);
2899 insn = gen_tls_tprel_64 (dest, tlsreg, addr);
2900 }
2901 else
2902 {
2903 tlsreg = gen_rtx_REG (Pmode, 2);
2904 insn = gen_tls_tprel_32 (dest, tlsreg, addr);
2905 }
2906 emit_insn (insn);
2907 }
2908 else if (model == TLS_MODEL_LOCAL_EXEC && rs6000_tls_size == 32)
2909 {
2910 rtx tlsreg, tmp;
2911
2912 tmp = gen_reg_rtx (Pmode);
2913 if (TARGET_64BIT)
2914 {
2915 tlsreg = gen_rtx_REG (Pmode, 13);
2916 insn = gen_tls_tprel_ha_64 (tmp, tlsreg, addr);
2917 }
2918 else
2919 {
2920 tlsreg = gen_rtx_REG (Pmode, 2);
2921 insn = gen_tls_tprel_ha_32 (tmp, tlsreg, addr);
2922 }
2923 emit_insn (insn);
2924 if (TARGET_64BIT)
2925 insn = gen_tls_tprel_lo_64 (dest, tmp, addr);
2926 else
2927 insn = gen_tls_tprel_lo_32 (dest, tmp, addr);
2928 emit_insn (insn);
2929 }
2930 else
2931 {
2932 rtx r3, got, tga, tmp1, tmp2, eqv;
2933
2934 if (TARGET_64BIT)
2935 got = gen_rtx_REG (Pmode, TOC_REGISTER);
2936 else
2937 {
2938 if (flag_pic == 1)
2939 got = gen_rtx_REG (Pmode, RS6000_PIC_OFFSET_TABLE_REGNUM);
2940 else
2941 {
2942 rtx gsym = rs6000_got_sym ();
2943 got = gen_reg_rtx (Pmode);
2944 if (flag_pic == 0)
2945 rs6000_emit_move (got, gsym, Pmode);
2946 else
2947 {
2948 char buf[30];
2949 static int tls_got_labelno = 0;
2950 rtx tempLR, lab, tmp3, mem;
2951 rtx first, last;
2952
2953 ASM_GENERATE_INTERNAL_LABEL (buf, "LTLS", tls_got_labelno++);
2954 lab = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
2955 tempLR = gen_reg_rtx (Pmode);
2956 tmp1 = gen_reg_rtx (Pmode);
2957 tmp2 = gen_reg_rtx (Pmode);
2958 tmp3 = gen_reg_rtx (Pmode);
2959 mem = gen_rtx_MEM (Pmode, tmp1);
2960 RTX_UNCHANGING_P (mem) = 1;
2961
2962 first = emit_insn (gen_load_toc_v4_PIC_1b (tempLR, lab,
2963 gsym));
2964 emit_move_insn (tmp1, tempLR);
2965 emit_move_insn (tmp2, mem);
2966 emit_insn (gen_addsi3 (tmp3, tmp1, tmp2));
2967 last = emit_move_insn (got, tmp3);
2968 REG_NOTES (last) = gen_rtx_EXPR_LIST (REG_EQUAL, gsym,
2969 REG_NOTES (last));
2970 REG_NOTES (first) = gen_rtx_INSN_LIST (REG_LIBCALL, last,
2971 REG_NOTES (first));
2972 REG_NOTES (last) = gen_rtx_INSN_LIST (REG_RETVAL, first,
2973 REG_NOTES (last));
2974 }
2975 }
2976 }
2977
2978 if (model == TLS_MODEL_GLOBAL_DYNAMIC)
2979 {
2980 r3 = gen_rtx_REG (Pmode, 3);
2981 if (TARGET_64BIT)
2982 insn = gen_tls_gd_64 (r3, got, addr);
2983 else
2984 insn = gen_tls_gd_32 (r3, got, addr);
2985 start_sequence ();
2986 emit_insn (insn);
2987 tga = gen_rtx_MEM (Pmode, rs6000_tls_get_addr ());
2988 insn = gen_call_value (r3, tga, const0_rtx, const0_rtx);
2989 insn = emit_call_insn (insn);
2990 CONST_OR_PURE_CALL_P (insn) = 1;
2991 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), r3);
2992 insn = get_insns ();
2993 end_sequence ();
2994 emit_libcall_block (insn, dest, r3, addr);
2995 }
2996 else if (model == TLS_MODEL_LOCAL_DYNAMIC)
2997 {
2998 r3 = gen_rtx_REG (Pmode, 3);
2999 if (TARGET_64BIT)
3000 insn = gen_tls_ld_64 (r3, got);
3001 else
3002 insn = gen_tls_ld_32 (r3, got);
3003 start_sequence ();
3004 emit_insn (insn);
3005 tga = gen_rtx_MEM (Pmode, rs6000_tls_get_addr ());
3006 insn = gen_call_value (r3, tga, const0_rtx, const0_rtx);
3007 insn = emit_call_insn (insn);
3008 CONST_OR_PURE_CALL_P (insn) = 1;
3009 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), r3);
3010 insn = get_insns ();
3011 end_sequence ();
3012 tmp1 = gen_reg_rtx (Pmode);
3013 eqv = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx),
3014 UNSPEC_TLSLD);
3015 emit_libcall_block (insn, tmp1, r3, eqv);
3016 if (rs6000_tls_size == 16)
3017 {
3018 if (TARGET_64BIT)
3019 insn = gen_tls_dtprel_64 (dest, tmp1, addr);
3020 else
3021 insn = gen_tls_dtprel_32 (dest, tmp1, addr);
3022 }
3023 else if (rs6000_tls_size == 32)
3024 {
3025 tmp2 = gen_reg_rtx (Pmode);
3026 if (TARGET_64BIT)
3027 insn = gen_tls_dtprel_ha_64 (tmp2, tmp1, addr);
3028 else
3029 insn = gen_tls_dtprel_ha_32 (tmp2, tmp1, addr);
3030 emit_insn (insn);
3031 if (TARGET_64BIT)
3032 insn = gen_tls_dtprel_lo_64 (dest, tmp2, addr);
3033 else
3034 insn = gen_tls_dtprel_lo_32 (dest, tmp2, addr);
3035 }
3036 else
3037 {
3038 tmp2 = gen_reg_rtx (Pmode);
3039 if (TARGET_64BIT)
3040 insn = gen_tls_got_dtprel_64 (tmp2, got, addr);
3041 else
3042 insn = gen_tls_got_dtprel_32 (tmp2, got, addr);
3043 emit_insn (insn);
3044 insn = gen_rtx_SET (Pmode, dest,
3045 gen_rtx_PLUS (Pmode, tmp2, tmp1));
3046 }
3047 emit_insn (insn);
3048 }
3049 else
3050 {
3051 /* IE, or 64 bit offset LE. */
3052 tmp2 = gen_reg_rtx (Pmode);
3053 if (TARGET_64BIT)
3054 insn = gen_tls_got_tprel_64 (tmp2, got, addr);
3055 else
3056 insn = gen_tls_got_tprel_32 (tmp2, got, addr);
3057 emit_insn (insn);
3058 if (TARGET_64BIT)
3059 insn = gen_tls_tls_64 (dest, tmp2, addr);
3060 else
3061 insn = gen_tls_tls_32 (dest, tmp2, addr);
3062 emit_insn (insn);
3063 }
3064 }
3065
3066 return dest;
3067}
3068
3069/* Return 1 if X is a SYMBOL_REF for a TLS symbol. This is used in
3070 instruction definitions. */
3071
3072int
a2369ed3 3073rs6000_tls_symbol_ref (rtx x, enum machine_mode mode ATTRIBUTE_UNUSED)
c4501e62
JJ
3074{
3075 return RS6000_SYMBOL_REF_TLS_P (x);
3076}
3077
3078/* Return 1 if X contains a thread-local symbol. */
3079
3080bool
a2369ed3 3081rs6000_tls_referenced_p (rtx x)
c4501e62 3082{
cd413cab
AP
3083 if (! TARGET_HAVE_TLS)
3084 return false;
3085
c4501e62
JJ
3086 return for_each_rtx (&x, &rs6000_tls_symbol_ref_1, 0);
3087}
3088
3089/* Return 1 if *X is a thread-local symbol. This is the same as
3090 rs6000_tls_symbol_ref except for the type of the unused argument. */
3091
3092static inline int
a2369ed3 3093rs6000_tls_symbol_ref_1 (rtx *x, void *data ATTRIBUTE_UNUSED)
c4501e62
JJ
3094{
3095 return RS6000_SYMBOL_REF_TLS_P (*x);
3096}
3097
24ea750e
DJ
3098/* The convention appears to be to define this wherever it is used.
3099 With legitimize_reload_address now defined here, REG_MODE_OK_FOR_BASE_P
3100 is now used here. */
3101#ifndef REG_MODE_OK_FOR_BASE_P
3102#define REG_MODE_OK_FOR_BASE_P(REGNO, MODE) REG_OK_FOR_BASE_P (REGNO)
3103#endif
3104
3105/* Our implementation of LEGITIMIZE_RELOAD_ADDRESS. Returns a value to
3106 replace the input X, or the original X if no replacement is called for.
3107 The output parameter *WIN is 1 if the calling macro should goto WIN,
3108 0 if it should not.
3109
3110 For RS/6000, we wish to handle large displacements off a base
3111 register by splitting the addend across an addiu/addis and the mem insn.
3112 This cuts number of extra insns needed from 3 to 1.
3113
3114 On Darwin, we use this to generate code for floating point constants.
3115 A movsf_low is generated so we wind up with 2 instructions rather than 3.
3116 The Darwin code is inside #if TARGET_MACHO because only then is
3117 machopic_function_base_name() defined. */
3118rtx
a2369ed3
DJ
3119rs6000_legitimize_reload_address (rtx x, enum machine_mode mode,
3120 int opnum, int type, int ind_levels ATTRIBUTE_UNUSED, int *win)
24ea750e
DJ
3121{
3122 /* We must recognize output that we have already generated ourselves. */
3123 if (GET_CODE (x) == PLUS
3124 && GET_CODE (XEXP (x, 0)) == PLUS
3125 && GET_CODE (XEXP (XEXP (x, 0), 0)) == REG
3126 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3127 && GET_CODE (XEXP (x, 1)) == CONST_INT)
3128 {
3129 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
3130 BASE_REG_CLASS, GET_MODE (x), VOIDmode, 0, 0,
3131 opnum, (enum reload_type)type);
3132 *win = 1;
3133 return x;
3134 }
3deb2758 3135
24ea750e
DJ
3136#if TARGET_MACHO
3137 if (DEFAULT_ABI == ABI_DARWIN && flag_pic
3138 && GET_CODE (x) == LO_SUM
3139 && GET_CODE (XEXP (x, 0)) == PLUS
3140 && XEXP (XEXP (x, 0), 0) == pic_offset_table_rtx
3141 && GET_CODE (XEXP (XEXP (x, 0), 1)) == HIGH
3142 && GET_CODE (XEXP (XEXP (XEXP (x, 0), 1), 0)) == CONST
3143 && XEXP (XEXP (XEXP (x, 0), 1), 0) == XEXP (x, 1)
3144 && GET_CODE (XEXP (XEXP (x, 1), 0)) == MINUS
3145 && GET_CODE (XEXP (XEXP (XEXP (x, 1), 0), 0)) == SYMBOL_REF
3146 && GET_CODE (XEXP (XEXP (XEXP (x, 1), 0), 1)) == SYMBOL_REF)
3147 {
3148 /* Result of previous invocation of this function on Darwin
6f317ef3 3149 floating point constant. */
24ea750e
DJ
3150 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
3151 BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
3152 opnum, (enum reload_type)type);
3153 *win = 1;
3154 return x;
3155 }
3156#endif
3157 if (GET_CODE (x) == PLUS
3158 && GET_CODE (XEXP (x, 0)) == REG
3159 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER
3160 && REG_MODE_OK_FOR_BASE_P (XEXP (x, 0), mode)
78c875e8 3161 && GET_CODE (XEXP (x, 1)) == CONST_INT
93638d7a 3162 && !SPE_VECTOR_MODE (mode)
78c875e8 3163 && !ALTIVEC_VECTOR_MODE (mode))
24ea750e
DJ
3164 {
3165 HOST_WIDE_INT val = INTVAL (XEXP (x, 1));
3166 HOST_WIDE_INT low = ((val & 0xffff) ^ 0x8000) - 0x8000;
3167 HOST_WIDE_INT high
3168 = (((val - low) & 0xffffffff) ^ 0x80000000) - 0x80000000;
3169
3170 /* Check for 32-bit overflow. */
3171 if (high + low != val)
3172 {
3173 *win = 0;
3174 return x;
3175 }
3176
3177 /* Reload the high part into a base reg; leave the low part
3178 in the mem directly. */
3179
3180 x = gen_rtx_PLUS (GET_MODE (x),
3181 gen_rtx_PLUS (GET_MODE (x), XEXP (x, 0),
3182 GEN_INT (high)),
3183 GEN_INT (low));
3184
3185 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
3186 BASE_REG_CLASS, GET_MODE (x), VOIDmode, 0, 0,
3187 opnum, (enum reload_type)type);
3188 *win = 1;
3189 return x;
3190 }
3191#if TARGET_MACHO
3192 if (GET_CODE (x) == SYMBOL_REF
3193 && DEFAULT_ABI == ABI_DARWIN
69ef87e2 3194 && !ALTIVEC_VECTOR_MODE (mode)
a29077da
GK
3195 && (flag_pic || MACHO_DYNAMIC_NO_PIC_P)
3196 /* Don't do this for TFmode, since the result isn't offsettable. */
3197 && mode != TFmode)
24ea750e 3198 {
a29077da
GK
3199 if (flag_pic)
3200 {
3201 rtx offset = gen_rtx_CONST (Pmode,
3202 gen_rtx_MINUS (Pmode, x,
3203 gen_rtx_SYMBOL_REF (Pmode,
3204 machopic_function_base_name ())));
3205 x = gen_rtx_LO_SUM (GET_MODE (x),
3206 gen_rtx_PLUS (Pmode, pic_offset_table_rtx,
3207 gen_rtx_HIGH (Pmode, offset)), offset);
3208 }
3209 else
3210 x = gen_rtx_LO_SUM (GET_MODE (x),
3211 gen_rtx_HIGH (Pmode, x), x);
3212
24ea750e 3213 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
a29077da
GK
3214 BASE_REG_CLASS, Pmode, VOIDmode, 0, 0,
3215 opnum, (enum reload_type)type);
24ea750e
DJ
3216 *win = 1;
3217 return x;
3218 }
3219#endif
3220 if (TARGET_TOC
4d588c14 3221 && constant_pool_expr_p (x)
c1f11548 3222 && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (x), mode))
24ea750e
DJ
3223 {
3224 (x) = create_TOC_reference (x);
3225 *win = 1;
3226 return x;
3227 }
3228 *win = 0;
3229 return x;
3230}
3231
258bfae2
FS
3232/* GO_IF_LEGITIMATE_ADDRESS recognizes an RTL expression
3233 that is a valid memory address for an instruction.
3234 The MODE argument is the machine mode for the MEM expression
3235 that wants to use this address.
3236
3237 On the RS/6000, there are four valid address: a SYMBOL_REF that
3238 refers to a constant pool entry of an address (or the sum of it
3239 plus a constant), a short (16-bit signed) constant plus a register,
3240 the sum of two registers, or a register indirect, possibly with an
5bdc5878 3241 auto-increment. For DFmode and DImode with a constant plus register,
258bfae2
FS
3242 we must ensure that both words are addressable or PowerPC64 with offset
3243 word aligned.
3244
3245 For modes spanning multiple registers (DFmode in 32-bit GPRs,
3246 32-bit DImode, TImode), indexed addressing cannot be used because
3247 adjacent memory cells are accessed by adding word-sized offsets
3248 during assembly output. */
3249int
a2369ed3 3250rs6000_legitimate_address (enum machine_mode mode, rtx x, int reg_ok_strict)
258bfae2 3251{
c4501e62
JJ
3252 if (RS6000_SYMBOL_REF_TLS_P (x))
3253 return 0;
4d588c14 3254 if (legitimate_indirect_address_p (x, reg_ok_strict))
258bfae2
FS
3255 return 1;
3256 if ((GET_CODE (x) == PRE_INC || GET_CODE (x) == PRE_DEC)
0d6d6892 3257 && !ALTIVEC_VECTOR_MODE (mode)
a3170dc6 3258 && !SPE_VECTOR_MODE (mode)
258bfae2 3259 && TARGET_UPDATE
4d588c14 3260 && legitimate_indirect_address_p (XEXP (x, 0), reg_ok_strict))
258bfae2 3261 return 1;
4d588c14 3262 if (legitimate_small_data_p (mode, x))
258bfae2 3263 return 1;
4d588c14 3264 if (legitimate_constant_pool_address_p (x))
258bfae2
FS
3265 return 1;
3266 /* If not REG_OK_STRICT (before reload) let pass any stack offset. */
3267 if (! reg_ok_strict
3268 && GET_CODE (x) == PLUS
3269 && GET_CODE (XEXP (x, 0)) == REG
708d2456
HP
3270 && (XEXP (x, 0) == virtual_stack_vars_rtx
3271 || XEXP (x, 0) == arg_pointer_rtx)
258bfae2
FS
3272 && GET_CODE (XEXP (x, 1)) == CONST_INT)
3273 return 1;
4d588c14 3274 if (legitimate_offset_address_p (mode, x, reg_ok_strict))
258bfae2
FS
3275 return 1;
3276 if (mode != TImode
a3170dc6
AH
3277 && ((TARGET_HARD_FLOAT && TARGET_FPRS)
3278 || TARGET_POWERPC64
fcce224d 3279 || (mode != DFmode && mode != TFmode))
258bfae2 3280 && (TARGET_POWERPC64 || mode != DImode)
4d588c14 3281 && legitimate_indexed_address_p (x, reg_ok_strict))
258bfae2 3282 return 1;
4d588c14 3283 if (legitimate_lo_sum_address_p (mode, x, reg_ok_strict))
258bfae2
FS
3284 return 1;
3285 return 0;
3286}
4d588c14
RH
3287
3288/* Go to LABEL if ADDR (a legitimate address expression)
3289 has an effect that depends on the machine mode it is used for.
3290
3291 On the RS/6000 this is true of all integral offsets (since AltiVec
3292 modes don't allow them) or is a pre-increment or decrement.
3293
3294 ??? Except that due to conceptual problems in offsettable_address_p
3295 we can't really report the problems of integral offsets. So leave
3296 this assuming that the adjustable offset must be valid for the
3297 sub-words of a TFmode operand, which is what we had before. */
3298
3299bool
a2369ed3 3300rs6000_mode_dependent_address (rtx addr)
4d588c14
RH
3301{
3302 switch (GET_CODE (addr))
3303 {
3304 case PLUS:
3305 if (GET_CODE (XEXP (addr, 1)) == CONST_INT)
3306 {
3307 unsigned HOST_WIDE_INT val = INTVAL (XEXP (addr, 1));
3308 return val + 12 + 0x8000 >= 0x10000;
3309 }
3310 break;
3311
3312 case LO_SUM:
3313 return true;
3314
3315 case PRE_INC:
3316 case PRE_DEC:
3317 return TARGET_UPDATE;
3318
3319 default:
3320 break;
3321 }
3322
3323 return false;
3324}
fb4d4348 3325\f
a4f6c312
SS
3326/* Try to output insns to set TARGET equal to the constant C if it can
3327 be done in less than N insns. Do all computations in MODE.
3328 Returns the place where the output has been placed if it can be
3329 done and the insns have been emitted. If it would take more than N
3330 insns, zero is returned and no insns and emitted. */
2bfcf297
DB
3331
3332rtx
a2369ed3
DJ
3333rs6000_emit_set_const (rtx dest, enum machine_mode mode,
3334 rtx source, int n ATTRIBUTE_UNUSED)
2bfcf297 3335{
af8cb5c5 3336 rtx result, insn, set;
2bfcf297
DB
3337 HOST_WIDE_INT c0, c1;
3338
af8cb5c5 3339 if (mode == QImode || mode == HImode)
2bfcf297
DB
3340 {
3341 if (dest == NULL)
3342 dest = gen_reg_rtx (mode);
3343 emit_insn (gen_rtx_SET (VOIDmode, dest, source));
3344 return dest;
3345 }
af8cb5c5 3346 else if (mode == SImode)
2bfcf297 3347 {
af8cb5c5
DE
3348 result = no_new_pseudos ? dest : gen_reg_rtx (SImode);
3349
3350 emit_insn (gen_rtx_SET (VOIDmode, result,
3351 GEN_INT (INTVAL (source)
3352 & (~ (HOST_WIDE_INT) 0xffff))));
3353 emit_insn (gen_rtx_SET (VOIDmode, dest,
3354 gen_rtx_IOR (SImode, result,
3355 GEN_INT (INTVAL (source) & 0xffff))));
3356 result = dest;
2bfcf297 3357 }
af8cb5c5 3358 else if (mode == DImode)
2bfcf297 3359 {
af8cb5c5
DE
3360 if (GET_CODE (source) == CONST_INT)
3361 {
3362 c0 = INTVAL (source);
3363 c1 = -(c0 < 0);
3364 }
3365 else if (GET_CODE (source) == CONST_DOUBLE)
3366 {
2bfcf297 3367#if HOST_BITS_PER_WIDE_INT >= 64
af8cb5c5
DE
3368 c0 = CONST_DOUBLE_LOW (source);
3369 c1 = -(c0 < 0);
2bfcf297 3370#else
af8cb5c5
DE
3371 c0 = CONST_DOUBLE_LOW (source);
3372 c1 = CONST_DOUBLE_HIGH (source);
2bfcf297 3373#endif
af8cb5c5
DE
3374 }
3375 else
3376 abort ();
3377
3378 result = rs6000_emit_set_long_const (dest, c0, c1);
2bfcf297
DB
3379 }
3380 else
a4f6c312 3381 abort ();
2bfcf297 3382
af8cb5c5
DE
3383 insn = get_last_insn ();
3384 set = single_set (insn);
3385 if (! CONSTANT_P (SET_SRC (set)))
3386 set_unique_reg_note (insn, REG_EQUAL, source);
3387
3388 return result;
2bfcf297
DB
3389}
3390
3391/* Having failed to find a 3 insn sequence in rs6000_emit_set_const,
3392 fall back to a straight forward decomposition. We do this to avoid
3393 exponential run times encountered when looking for longer sequences
3394 with rs6000_emit_set_const. */
3395static rtx
a2369ed3 3396rs6000_emit_set_long_const (rtx dest, HOST_WIDE_INT c1, HOST_WIDE_INT c2)
2bfcf297
DB
3397{
3398 if (!TARGET_POWERPC64)
3399 {
3400 rtx operand1, operand2;
3401
3402 operand1 = operand_subword_force (dest, WORDS_BIG_ENDIAN == 0,
3403 DImode);
3404 operand2 = operand_subword_force (dest, WORDS_BIG_ENDIAN != 0,
3405 DImode);
3406 emit_move_insn (operand1, GEN_INT (c1));
3407 emit_move_insn (operand2, GEN_INT (c2));
3408 }
3409 else
3410 {
bc06712d 3411 HOST_WIDE_INT ud1, ud2, ud3, ud4;
252b88f7 3412
bc06712d 3413 ud1 = c1 & 0xffff;
f921c9c9 3414 ud2 = (c1 & 0xffff0000) >> 16;
2bfcf297 3415#if HOST_BITS_PER_WIDE_INT >= 64
bc06712d 3416 c2 = c1 >> 32;
2bfcf297 3417#endif
bc06712d 3418 ud3 = c2 & 0xffff;
f921c9c9 3419 ud4 = (c2 & 0xffff0000) >> 16;
2bfcf297 3420
bc06712d
TR
3421 if ((ud4 == 0xffff && ud3 == 0xffff && ud2 == 0xffff && (ud1 & 0x8000))
3422 || (ud4 == 0 && ud3 == 0 && ud2 == 0 && ! (ud1 & 0x8000)))
2bfcf297 3423 {
bc06712d 3424 if (ud1 & 0x8000)
b78d48dd 3425 emit_move_insn (dest, GEN_INT (((ud1 ^ 0x8000) - 0x8000)));
bc06712d
TR
3426 else
3427 emit_move_insn (dest, GEN_INT (ud1));
2bfcf297 3428 }
2bfcf297 3429
bc06712d
TR
3430 else if ((ud4 == 0xffff && ud3 == 0xffff && (ud2 & 0x8000))
3431 || (ud4 == 0 && ud3 == 0 && ! (ud2 & 0x8000)))
252b88f7 3432 {
bc06712d
TR
3433 if (ud2 & 0x8000)
3434 emit_move_insn (dest, GEN_INT (((ud2 << 16) ^ 0x80000000)
3435 - 0x80000000));
252b88f7 3436 else
bc06712d
TR
3437 emit_move_insn (dest, GEN_INT (ud2 << 16));
3438 if (ud1 != 0)
3439 emit_move_insn (dest, gen_rtx_IOR (DImode, dest, GEN_INT (ud1)));
252b88f7 3440 }
bc06712d
TR
3441 else if ((ud4 == 0xffff && (ud3 & 0x8000))
3442 || (ud4 == 0 && ! (ud3 & 0x8000)))
3443 {
3444 if (ud3 & 0x8000)
3445 emit_move_insn (dest, GEN_INT (((ud3 << 16) ^ 0x80000000)
3446 - 0x80000000));
3447 else
3448 emit_move_insn (dest, GEN_INT (ud3 << 16));
3449
3450 if (ud2 != 0)
3451 emit_move_insn (dest, gen_rtx_IOR (DImode, dest, GEN_INT (ud2)));
3452 emit_move_insn (dest, gen_rtx_ASHIFT (DImode, dest, GEN_INT (16)));
3453 if (ud1 != 0)
3454 emit_move_insn (dest, gen_rtx_IOR (DImode, dest, GEN_INT (ud1)));
3455 }
3456 else
3457 {
3458 if (ud4 & 0x8000)
3459 emit_move_insn (dest, GEN_INT (((ud4 << 16) ^ 0x80000000)
3460 - 0x80000000));
3461 else
3462 emit_move_insn (dest, GEN_INT (ud4 << 16));
3463
3464 if (ud3 != 0)
3465 emit_move_insn (dest, gen_rtx_IOR (DImode, dest, GEN_INT (ud3)));
2bfcf297 3466
bc06712d
TR
3467 emit_move_insn (dest, gen_rtx_ASHIFT (DImode, dest, GEN_INT (32)));
3468 if (ud2 != 0)
3469 emit_move_insn (dest, gen_rtx_IOR (DImode, dest,
3470 GEN_INT (ud2 << 16)));
3471 if (ud1 != 0)
3472 emit_move_insn (dest, gen_rtx_IOR (DImode, dest, GEN_INT (ud1)));
3473 }
3474 }
2bfcf297
DB
3475 return dest;
3476}
3477
fb4d4348
GK
3478/* Emit a move from SOURCE to DEST in mode MODE. */
3479void
a2369ed3 3480rs6000_emit_move (rtx dest, rtx source, enum machine_mode mode)
fb4d4348
GK
3481{
3482 rtx operands[2];
3483 operands[0] = dest;
3484 operands[1] = source;
3485
3486 /* Sanity checks. Check that we get CONST_DOUBLE only when we should. */
3487 if (GET_CODE (operands[1]) == CONST_DOUBLE
3488 && ! FLOAT_MODE_P (mode)
3489 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
3490 {
3491 /* FIXME. This should never happen. */
3492 /* Since it seems that it does, do the safe thing and convert
3493 to a CONST_INT. */
2496c7bd 3494 operands[1] = gen_int_mode (CONST_DOUBLE_LOW (operands[1]), mode);
fb4d4348
GK
3495 }
3496 if (GET_CODE (operands[1]) == CONST_DOUBLE
3497 && ! FLOAT_MODE_P (mode)
3498 && ((CONST_DOUBLE_HIGH (operands[1]) == 0
3499 && CONST_DOUBLE_LOW (operands[1]) >= 0)
3500 || (CONST_DOUBLE_HIGH (operands[1]) == -1
3501 && CONST_DOUBLE_LOW (operands[1]) < 0)))
3502 abort ();
c9e8cb32
DD
3503
3504 /* Check if GCC is setting up a block move that will end up using FP
3505 registers as temporaries. We must make sure this is acceptable. */
3506 if (GET_CODE (operands[0]) == MEM
3507 && GET_CODE (operands[1]) == MEM
3508 && mode == DImode
41543739
GK
3509 && (SLOW_UNALIGNED_ACCESS (DImode, MEM_ALIGN (operands[0]))
3510 || SLOW_UNALIGNED_ACCESS (DImode, MEM_ALIGN (operands[1])))
3511 && ! (SLOW_UNALIGNED_ACCESS (SImode, (MEM_ALIGN (operands[0]) > 32
3512 ? 32 : MEM_ALIGN (operands[0])))
3513 || SLOW_UNALIGNED_ACCESS (SImode, (MEM_ALIGN (operands[1]) > 32
3514 ? 32
3515 : MEM_ALIGN (operands[1]))))
3516 && ! MEM_VOLATILE_P (operands [0])
3517 && ! MEM_VOLATILE_P (operands [1]))
c9e8cb32 3518 {
41543739
GK
3519 emit_move_insn (adjust_address (operands[0], SImode, 0),
3520 adjust_address (operands[1], SImode, 0));
3521 emit_move_insn (adjust_address (operands[0], SImode, 4),
3522 adjust_address (operands[1], SImode, 4));
c9e8cb32
DD
3523 return;
3524 }
630d42a0 3525
67cef334
DE
3526 if (!no_new_pseudos)
3527 {
3528 if (GET_CODE (operands[1]) == MEM && optimize > 0
3529 && (mode == QImode || mode == HImode || mode == SImode)
3530 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (word_mode))
3531 {
3532 rtx reg = gen_reg_rtx (word_mode);
3533
3534 emit_insn (gen_rtx_SET (word_mode, reg,
3535 gen_rtx_ZERO_EXTEND (word_mode,
3536 operands[1])));
3537 operands[1] = gen_lowpart (mode, reg);
3538 }
3539 if (GET_CODE (operands[0]) != REG)
3540 operands[1] = force_reg (mode, operands[1]);
3541 }
a9098fd0 3542
a3170dc6
AH
3543 if (mode == SFmode && ! TARGET_POWERPC
3544 && TARGET_HARD_FLOAT && TARGET_FPRS
ffc14f31 3545 && GET_CODE (operands[0]) == MEM)
fb4d4348 3546 {
ffc14f31
GK
3547 int regnum;
3548
3549 if (reload_in_progress || reload_completed)
3550 regnum = true_regnum (operands[1]);
3551 else if (GET_CODE (operands[1]) == REG)
3552 regnum = REGNO (operands[1]);
3553 else
3554 regnum = -1;
fb4d4348
GK
3555
3556 /* If operands[1] is a register, on POWER it may have
3557 double-precision data in it, so truncate it to single
3558 precision. */
3559 if (FP_REGNO_P (regnum) || regnum >= FIRST_PSEUDO_REGISTER)
3560 {
3561 rtx newreg;
3562 newreg = (no_new_pseudos ? operands[1] : gen_reg_rtx (mode));
3563 emit_insn (gen_aux_truncdfsf2 (newreg, operands[1]));
3564 operands[1] = newreg;
3565 }
3566 }
3567
c4501e62
JJ
3568 /* Recognize the case where operand[1] is a reference to thread-local
3569 data and load its address to a register. */
3570 if (GET_CODE (operands[1]) == SYMBOL_REF)
3571 {
3572 enum tls_model model = SYMBOL_REF_TLS_MODEL (operands[1]);
3573 if (model != 0)
3574 operands[1] = rs6000_legitimize_tls_address (operands[1], model);
3575 }
3576
8f4e6caf
RH
3577 /* Handle the case where reload calls us with an invalid address. */
3578 if (reload_in_progress && mode == Pmode
69ef87e2 3579 && (! general_operand (operands[1], mode)
8f4e6caf
RH
3580 || ! nonimmediate_operand (operands[0], mode)))
3581 goto emit_set;
3582
3583 /* Handle the case of CONSTANT_P_RTX. */
3584 if (GET_CODE (operands[1]) == CONSTANT_P_RTX)
3585 goto emit_set;
a9baceb1
GK
3586
3587 /* 128-bit constant floating-point values on Darwin should really be
3588 loaded as two parts. */
3589 if ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_DARWIN)
3590 && TARGET_HARD_FLOAT && TARGET_FPRS && TARGET_LONG_DOUBLE_128
3591 && mode == TFmode && GET_CODE (operands[1]) == CONST_DOUBLE)
3592 {
3593 /* DImode is used, not DFmode, because simplify_gen_subreg doesn't
3594 know how to get a DFmode SUBREG of a TFmode. */
3595 rs6000_emit_move (simplify_gen_subreg (DImode, operands[0], mode, 0),
3596 simplify_gen_subreg (DImode, operands[1], mode, 0),
3597 DImode);
3598 rs6000_emit_move (simplify_gen_subreg (DImode, operands[0], mode,
3599 GET_MODE_SIZE (DImode)),
3600 simplify_gen_subreg (DImode, operands[1], mode,
3601 GET_MODE_SIZE (DImode)),
3602 DImode);
3603 return;
3604 }
3605
fb4d4348
GK
3606 /* FIXME: In the long term, this switch statement should go away
3607 and be replaced by a sequence of tests based on things like
3608 mode == Pmode. */
3609 switch (mode)
3610 {
3611 case HImode:
3612 case QImode:
3613 if (CONSTANT_P (operands[1])
3614 && GET_CODE (operands[1]) != CONST_INT)
a9098fd0 3615 operands[1] = force_const_mem (mode, operands[1]);
fb4d4348
GK
3616 break;
3617
06f4e019 3618 case TFmode:
fb4d4348
GK
3619 case DFmode:
3620 case SFmode:
3621 if (CONSTANT_P (operands[1])
3622 && ! easy_fp_constant (operands[1], mode))
a9098fd0 3623 operands[1] = force_const_mem (mode, operands[1]);
fb4d4348
GK
3624 break;
3625
0ac081f6
AH
3626 case V16QImode:
3627 case V8HImode:
3628 case V4SFmode:
3629 case V4SImode:
a3170dc6
AH
3630 case V4HImode:
3631 case V2SFmode:
3632 case V2SImode:
00a892b8 3633 case V1DImode:
69ef87e2 3634 if (CONSTANT_P (operands[1])
d744e06e 3635 && !easy_vector_constant (operands[1], mode))
0ac081f6
AH
3636 operands[1] = force_const_mem (mode, operands[1]);
3637 break;
3638
fb4d4348 3639 case SImode:
a9098fd0 3640 case DImode:
fb4d4348
GK
3641 /* Use default pattern for address of ELF small data */
3642 if (TARGET_ELF
a9098fd0 3643 && mode == Pmode
f607bc57 3644 && DEFAULT_ABI == ABI_V4
a9098fd0
GK
3645 && (GET_CODE (operands[1]) == SYMBOL_REF
3646 || GET_CODE (operands[1]) == CONST)
3647 && small_data_operand (operands[1], mode))
fb4d4348
GK
3648 {
3649 emit_insn (gen_rtx_SET (VOIDmode, operands[0], operands[1]));
3650 return;
3651 }
3652
f607bc57 3653 if (DEFAULT_ABI == ABI_V4
a9098fd0
GK
3654 && mode == Pmode && mode == SImode
3655 && flag_pic == 1 && got_operand (operands[1], mode))
fb4d4348
GK
3656 {
3657 emit_insn (gen_movsi_got (operands[0], operands[1]));
3658 return;
3659 }
3660
ee890fe2 3661 if ((TARGET_ELF || DEFAULT_ABI == ABI_DARWIN)
f1384257
AM
3662 && TARGET_NO_TOC
3663 && ! flag_pic
a9098fd0 3664 && mode == Pmode
fb4d4348
GK
3665 && CONSTANT_P (operands[1])
3666 && GET_CODE (operands[1]) != HIGH
3667 && GET_CODE (operands[1]) != CONST_INT)
3668 {
a9098fd0 3669 rtx target = (no_new_pseudos ? operands[0] : gen_reg_rtx (mode));
fb4d4348
GK
3670
3671 /* If this is a function address on -mcall-aixdesc,
3672 convert it to the address of the descriptor. */
3673 if (DEFAULT_ABI == ABI_AIX
3674 && GET_CODE (operands[1]) == SYMBOL_REF
3675 && XSTR (operands[1], 0)[0] == '.')
3676 {
3677 const char *name = XSTR (operands[1], 0);
3678 rtx new_ref;
3679 while (*name == '.')
3680 name++;
3681 new_ref = gen_rtx_SYMBOL_REF (Pmode, name);
3682 CONSTANT_POOL_ADDRESS_P (new_ref)
3683 = CONSTANT_POOL_ADDRESS_P (operands[1]);
d1908feb 3684 SYMBOL_REF_FLAGS (new_ref) = SYMBOL_REF_FLAGS (operands[1]);
fb4d4348 3685 SYMBOL_REF_USED (new_ref) = SYMBOL_REF_USED (operands[1]);
d1908feb 3686 SYMBOL_REF_DECL (new_ref) = SYMBOL_REF_DECL (operands[1]);
fb4d4348
GK
3687 operands[1] = new_ref;
3688 }
7509c759 3689
ee890fe2
SS
3690 if (DEFAULT_ABI == ABI_DARWIN)
3691 {
ab82a49f
AP
3692#if TARGET_MACHO
3693 if (MACHO_DYNAMIC_NO_PIC_P)
3694 {
3695 /* Take care of any required data indirection. */
3696 operands[1] = rs6000_machopic_legitimize_pic_address (
3697 operands[1], mode, operands[0]);
3698 if (operands[0] != operands[1])
3699 emit_insn (gen_rtx_SET (VOIDmode,
3700 operands[0], operands[1]));
3701 return;
3702 }
3703#endif
ee890fe2
SS
3704 emit_insn (gen_macho_high (target, operands[1]));
3705 emit_insn (gen_macho_low (operands[0], target, operands[1]));
3706 return;
3707 }
3708
fb4d4348
GK
3709 emit_insn (gen_elf_high (target, operands[1]));
3710 emit_insn (gen_elf_low (operands[0], target, operands[1]));
3711 return;
3712 }
3713
a9098fd0
GK
3714 /* If this is a SYMBOL_REF that refers to a constant pool entry,
3715 and we have put it in the TOC, we just need to make a TOC-relative
3716 reference to it. */
3717 if (TARGET_TOC
3718 && GET_CODE (operands[1]) == SYMBOL_REF
4d588c14 3719 && constant_pool_expr_p (operands[1])
a9098fd0
GK
3720 && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (operands[1]),
3721 get_pool_mode (operands[1])))
fb4d4348 3722 {
a9098fd0 3723 operands[1] = create_TOC_reference (operands[1]);
fb4d4348 3724 }
a9098fd0
GK
3725 else if (mode == Pmode
3726 && CONSTANT_P (operands[1])
38886f37
AO
3727 && ((GET_CODE (operands[1]) != CONST_INT
3728 && ! easy_fp_constant (operands[1], mode))
3729 || (GET_CODE (operands[1]) == CONST_INT
3730 && num_insns_constant (operands[1], mode) > 2)
3731 || (GET_CODE (operands[0]) == REG
3732 && FP_REGNO_P (REGNO (operands[0]))))
a9098fd0 3733 && GET_CODE (operands[1]) != HIGH
4d588c14
RH
3734 && ! legitimate_constant_pool_address_p (operands[1])
3735 && ! toc_relative_expr_p (operands[1]))
fb4d4348
GK
3736 {
3737 /* Emit a USE operation so that the constant isn't deleted if
3738 expensive optimizations are turned on because nobody
3739 references it. This should only be done for operands that
3740 contain SYMBOL_REFs with CONSTANT_POOL_ADDRESS_P set.
3741 This should not be done for operands that contain LABEL_REFs.
3742 For now, we just handle the obvious case. */
3743 if (GET_CODE (operands[1]) != LABEL_REF)
3744 emit_insn (gen_rtx_USE (VOIDmode, operands[1]));
3745
c859cda6 3746#if TARGET_MACHO
ee890fe2 3747 /* Darwin uses a special PIC legitimizer. */
ab82a49f 3748 if (DEFAULT_ABI == ABI_DARWIN && MACHOPIC_INDIRECT)
ee890fe2 3749 {
ee890fe2
SS
3750 operands[1] =
3751 rs6000_machopic_legitimize_pic_address (operands[1], mode,
c859cda6
DJ
3752 operands[0]);
3753 if (operands[0] != operands[1])
3754 emit_insn (gen_rtx_SET (VOIDmode, operands[0], operands[1]));
ee890fe2
SS
3755 return;
3756 }
c859cda6 3757#endif
ee890fe2 3758
fb4d4348
GK
3759 /* If we are to limit the number of things we put in the TOC and
3760 this is a symbol plus a constant we can add in one insn,
3761 just put the symbol in the TOC and add the constant. Don't do
3762 this if reload is in progress. */
3763 if (GET_CODE (operands[1]) == CONST
3764 && TARGET_NO_SUM_IN_TOC && ! reload_in_progress
3765 && GET_CODE (XEXP (operands[1], 0)) == PLUS
a9098fd0 3766 && add_operand (XEXP (XEXP (operands[1], 0), 1), mode)
fb4d4348
GK
3767 && (GET_CODE (XEXP (XEXP (operands[1], 0), 0)) == LABEL_REF
3768 || GET_CODE (XEXP (XEXP (operands[1], 0), 0)) == SYMBOL_REF)
3769 && ! side_effects_p (operands[0]))
3770 {
a4f6c312
SS
3771 rtx sym =
3772 force_const_mem (mode, XEXP (XEXP (operands[1], 0), 0));
fb4d4348
GK
3773 rtx other = XEXP (XEXP (operands[1], 0), 1);
3774
a9098fd0
GK
3775 sym = force_reg (mode, sym);
3776 if (mode == SImode)
3777 emit_insn (gen_addsi3 (operands[0], sym, other));
3778 else
3779 emit_insn (gen_adddi3 (operands[0], sym, other));
fb4d4348
GK
3780 return;
3781 }
3782
a9098fd0 3783 operands[1] = force_const_mem (mode, operands[1]);
fb4d4348
GK
3784
3785 if (TARGET_TOC
4d588c14 3786 && constant_pool_expr_p (XEXP (operands[1], 0))
d34c5b80
DE
3787 && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (
3788 get_pool_constant (XEXP (operands[1], 0)),
3789 get_pool_mode (XEXP (operands[1], 0))))
a9098fd0 3790 {
ba4828e0
RK
3791 operands[1]
3792 = gen_rtx_MEM (mode,
3793 create_TOC_reference (XEXP (operands[1], 0)));
3794 set_mem_alias_set (operands[1], get_TOC_alias_set ());
fb4d4348 3795 RTX_UNCHANGING_P (operands[1]) = 1;
a9098fd0 3796 }
fb4d4348
GK
3797 }
3798 break;
a9098fd0 3799
fb4d4348
GK
3800 case TImode:
3801 if (GET_CODE (operands[0]) == MEM
3802 && GET_CODE (XEXP (operands[0], 0)) != REG
3803 && ! reload_in_progress)
792760b9
RK
3804 operands[0]
3805 = replace_equiv_address (operands[0],
3806 copy_addr_to_reg (XEXP (operands[0], 0)));
fb4d4348
GK
3807
3808 if (GET_CODE (operands[1]) == MEM
3809 && GET_CODE (XEXP (operands[1], 0)) != REG
3810 && ! reload_in_progress)
792760b9
RK
3811 operands[1]
3812 = replace_equiv_address (operands[1],
3813 copy_addr_to_reg (XEXP (operands[1], 0)));
27dc0551
DE
3814 if (TARGET_POWER)
3815 {
3816 emit_insn (gen_rtx_PARALLEL (VOIDmode,
3817 gen_rtvec (2,
3818 gen_rtx_SET (VOIDmode,
3819 operands[0], operands[1]),
3820 gen_rtx_CLOBBER (VOIDmode,
3821 gen_rtx_SCRATCH (SImode)))));
3822 return;
3823 }
fb4d4348
GK
3824 break;
3825
3826 default:
3827 abort ();
3828 }
3829
a9098fd0
GK
3830 /* Above, we may have called force_const_mem which may have returned
3831 an invalid address. If we can, fix this up; otherwise, reload will
3832 have to deal with it. */
8f4e6caf
RH
3833 if (GET_CODE (operands[1]) == MEM && ! reload_in_progress)
3834 operands[1] = validize_mem (operands[1]);
a9098fd0 3835
8f4e6caf 3836 emit_set:
fb4d4348
GK
3837 emit_insn (gen_rtx_SET (VOIDmode, operands[0], operands[1]));
3838}
4697a36c 3839\f
2858f73a
GK
3840/* Nonzero if we can use a floating-point register to pass this arg. */
3841#define USE_FP_FOR_ARG_P(CUM,MODE,TYPE) \
3842 (GET_MODE_CLASS (MODE) == MODE_FLOAT \
3843 && (CUM)->fregno <= FP_ARG_MAX_REG \
3844 && TARGET_HARD_FLOAT && TARGET_FPRS)
3845
3846/* Nonzero if we can use an AltiVec register to pass this arg. */
3847#define USE_ALTIVEC_FOR_ARG_P(CUM,MODE,TYPE,NAMED) \
3848 (ALTIVEC_VECTOR_MODE (MODE) \
3849 && (CUM)->vregno <= ALTIVEC_ARG_MAX_REG \
3850 && TARGET_ALTIVEC_ABI \
83953138 3851 && (NAMED))
2858f73a 3852
c6e8c921
GK
3853/* Return a nonzero value to say to return the function value in
3854 memory, just as large structures are always returned. TYPE will be
3855 the data type of the value, and FNTYPE will be the type of the
3856 function doing the returning, or @code{NULL} for libcalls.
3857
3858 The AIX ABI for the RS/6000 specifies that all structures are
3859 returned in memory. The Darwin ABI does the same. The SVR4 ABI
3860 specifies that structures <= 8 bytes are returned in r3/r4, but a
3861 draft put them in memory, and GCC used to implement the draft
3862 instead of the final standard. Therefore, TARGET_AIX_STRUCT_RET
3863 controls this instead of DEFAULT_ABI; V.4 targets needing backward
3864 compatibility can change DRAFT_V4_STRUCT_RET to override the
3865 default, and -m switches get the final word. See
3866 rs6000_override_options for more details.
3867
3868 The PPC32 SVR4 ABI uses IEEE double extended for long double, if 128-bit
3869 long double support is enabled. These values are returned in memory.
3870
3871 int_size_in_bytes returns -1 for variable size objects, which go in
3872 memory always. The cast to unsigned makes -1 > 8. */
3873
3874static bool
3875rs6000_return_in_memory (tree type, tree fntype ATTRIBUTE_UNUSED)
3876{
3877 if (AGGREGATE_TYPE_P (type)
3878 && (TARGET_AIX_STRUCT_RET
3879 || (unsigned HOST_WIDE_INT) int_size_in_bytes (type) > 8))
3880 return true;
3881 if (DEFAULT_ABI == ABI_V4 && TYPE_MODE (type) == TFmode)
3882 return true;
3883 return false;
3884}
3885
4697a36c
MM
3886/* Initialize a variable CUM of type CUMULATIVE_ARGS
3887 for a call to a function whose data type is FNTYPE.
3888 For a library call, FNTYPE is 0.
3889
3890 For incoming args we set the number of arguments in the prototype large
1c20ae99 3891 so we never return a PARALLEL. */
4697a36c
MM
3892
3893void
a2369ed3 3894init_cumulative_args (CUMULATIVE_ARGS *cum, tree fntype,
0f6937fe
AM
3895 rtx libname ATTRIBUTE_UNUSED, int incoming,
3896 int libcall, int n_named_args)
4697a36c
MM
3897{
3898 static CUMULATIVE_ARGS zero_cumulative;
3899
3900 *cum = zero_cumulative;
3901 cum->words = 0;
3902 cum->fregno = FP_ARG_MIN_REG;
0ac081f6 3903 cum->vregno = ALTIVEC_ARG_MIN_REG;
4697a36c 3904 cum->prototype = (fntype && TYPE_ARG_TYPES (fntype));
ddcc8263
DE
3905 cum->call_cookie = ((DEFAULT_ABI == ABI_V4 && libcall)
3906 ? CALL_LIBCALL : CALL_NORMAL);
4cc833b7 3907 cum->sysv_gregno = GP_ARG_MIN_REG;
a6c9bed4
AH
3908 cum->stdarg = fntype
3909 && (TYPE_ARG_TYPES (fntype) != 0
3910 && (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype)))
3911 != void_type_node));
4697a36c 3912
0f6937fe
AM
3913 cum->nargs_prototype = 0;
3914 if (incoming || cum->prototype)
3915 cum->nargs_prototype = n_named_args;
4697a36c 3916
a5c76ee6
ZW
3917 /* Check for a longcall attribute. */
3918 if (fntype
3919 && lookup_attribute ("longcall", TYPE_ATTRIBUTES (fntype))
3920 && !lookup_attribute ("shortcall", TYPE_ATTRIBUTES (fntype)))
6a4cee5f
MM
3921 cum->call_cookie = CALL_LONG;
3922
4697a36c
MM
3923 if (TARGET_DEBUG_ARG)
3924 {
3925 fprintf (stderr, "\ninit_cumulative_args:");
3926 if (fntype)
3927 {
3928 tree ret_type = TREE_TYPE (fntype);
3929 fprintf (stderr, " ret code = %s,",
3930 tree_code_name[ (int)TREE_CODE (ret_type) ]);
3931 }
3932
6a4cee5f
MM
3933 if (cum->call_cookie & CALL_LONG)
3934 fprintf (stderr, " longcall,");
3935
4697a36c
MM
3936 fprintf (stderr, " proto = %d, nargs = %d\n",
3937 cum->prototype, cum->nargs_prototype);
3938 }
6d0ef01e
HP
3939
3940 if (fntype
3941 && !TARGET_ALTIVEC
3942 && TARGET_ALTIVEC_ABI
3943 && ALTIVEC_VECTOR_MODE (TYPE_MODE (TREE_TYPE (fntype))))
3944 {
3945 error ("Cannot return value in vector register because"
3946 " altivec instructions are disabled, use -maltivec"
3947 " to enable them.");
3948 }
4697a36c
MM
3949}
3950\f
c229cba9
DE
3951/* If defined, a C expression which determines whether, and in which
3952 direction, to pad out an argument with extra space. The value
3953 should be of type `enum direction': either `upward' to pad above
3954 the argument, `downward' to pad below, or `none' to inhibit
3955 padding.
3956
3957 For the AIX ABI structs are always stored left shifted in their
3958 argument slot. */
3959
9ebbca7d 3960enum direction
a2369ed3 3961function_arg_padding (enum machine_mode mode, tree type)
c229cba9 3962{
6e985040
AM
3963#ifndef AGGREGATE_PADDING_FIXED
3964#define AGGREGATE_PADDING_FIXED 0
3965#endif
3966#ifndef AGGREGATES_PAD_UPWARD_ALWAYS
3967#define AGGREGATES_PAD_UPWARD_ALWAYS 0
3968#endif
3969
3970 if (!AGGREGATE_PADDING_FIXED)
3971 {
3972 /* GCC used to pass structures of the same size as integer types as
3973 if they were in fact integers, ignoring FUNCTION_ARG_PADDING.
3974 ie. Structures of size 1 or 2 (or 4 when TARGET_64BIT) were
3975 passed padded downward, except that -mstrict-align further
3976 muddied the water in that multi-component structures of 2 and 4
3977 bytes in size were passed padded upward.
3978
3979 The following arranges for best compatibility with previous
3980 versions of gcc, but removes the -mstrict-align dependency. */
3981 if (BYTES_BIG_ENDIAN)
3982 {
3983 HOST_WIDE_INT size = 0;
3984
3985 if (mode == BLKmode)
3986 {
3987 if (type && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
3988 size = int_size_in_bytes (type);
3989 }
3990 else
3991 size = GET_MODE_SIZE (mode);
3992
3993 if (size == 1 || size == 2 || size == 4)
3994 return downward;
3995 }
3996 return upward;
3997 }
3998
3999 if (AGGREGATES_PAD_UPWARD_ALWAYS)
4000 {
4001 if (type != 0 && AGGREGATE_TYPE_P (type))
4002 return upward;
4003 }
c229cba9 4004
d3704c46
KH
4005 /* Fall back to the default. */
4006 return DEFAULT_FUNCTION_ARG_PADDING (mode, type);
c229cba9
DE
4007}
4008
b6c9286a
MM
4009/* If defined, a C expression that gives the alignment boundary, in bits,
4010 of an argument with the specified mode and type. If it is not defined,
4011 PARM_BOUNDARY is used for all arguments.
4012
2310f99a 4013 V.4 wants long longs to be double word aligned. */
b6c9286a
MM
4014
4015int
a2369ed3 4016function_arg_boundary (enum machine_mode mode, tree type ATTRIBUTE_UNUSED)
b6c9286a 4017{
f607bc57 4018 if (DEFAULT_ABI == ABI_V4 && (mode == DImode || mode == DFmode))
e1f83b4d 4019 return 64;
a3170dc6
AH
4020 else if (SPE_VECTOR_MODE (mode))
4021 return 64;
0ac081f6
AH
4022 else if (TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (mode))
4023 return 128;
9ebbca7d 4024 else
b6c9286a 4025 return PARM_BOUNDARY;
b6c9286a 4026}
c53bdcf5
AM
4027
4028/* Compute the size (in words) of a function argument. */
4029
4030static unsigned long
4031rs6000_arg_size (enum machine_mode mode, tree type)
4032{
4033 unsigned long size;
4034
4035 if (mode != BLKmode)
4036 size = GET_MODE_SIZE (mode);
4037 else
4038 size = int_size_in_bytes (type);
4039
4040 if (TARGET_32BIT)
4041 return (size + 3) >> 2;
4042 else
4043 return (size + 7) >> 3;
4044}
b6c9286a 4045\f
4697a36c
MM
4046/* Update the data in CUM to advance over an argument
4047 of mode MODE and data type TYPE.
4048 (TYPE is null for libcalls where that information may not be available.) */
4049
4050void
a2369ed3
DJ
4051function_arg_advance (CUMULATIVE_ARGS *cum, enum machine_mode mode,
4052 tree type, int named)
4697a36c
MM
4053{
4054 cum->nargs_prototype--;
4055
0ac081f6
AH
4056 if (TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (mode))
4057 {
2858f73a 4058 if (USE_ALTIVEC_FOR_ARG_P (cum, mode, type, named))
6d0ef01e
HP
4059 {
4060 cum->vregno++;
4061 if (!TARGET_ALTIVEC)
4062 error ("Cannot pass argument in vector register because"
4063 " altivec instructions are disabled, use -maltivec"
4064 " to enable them.");
4065 }
c72d6c26
HP
4066 /* PowerPC64 Linux and AIX allocates GPRs for a vector argument
4067 even if it is going to be passed in a vector register.
4068 Darwin does the same for variable-argument functions. */
4069 if ((DEFAULT_ABI == ABI_AIX && TARGET_64BIT)
4070 || (cum->stdarg && DEFAULT_ABI != ABI_V4))
4071 {
a594a19c
GK
4072 int align;
4073
2858f73a
GK
4074 /* Vector parameters must be 16-byte aligned. This places
4075 them at 2 mod 4 in terms of words in 32-bit mode, since
4076 the parameter save area starts at offset 24 from the
4077 stack. In 64-bit mode, they just have to start on an
4078 even word, since the parameter save area is 16-byte
4079 aligned. Space for GPRs is reserved even if the argument
4080 will be passed in memory. */
4081 if (TARGET_32BIT)
4082 align = ((6 - (cum->words & 3)) & 3);
4083 else
4084 align = cum->words & 1;
c53bdcf5 4085 cum->words += align + rs6000_arg_size (mode, type);
2858f73a 4086
a594a19c
GK
4087 if (TARGET_DEBUG_ARG)
4088 {
4089 fprintf (stderr, "function_adv: words = %2d, align=%d, ",
4090 cum->words, align);
4091 fprintf (stderr, "nargs = %4d, proto = %d, mode = %4s\n",
2858f73a
GK
4092 cum->nargs_prototype, cum->prototype,
4093 GET_MODE_NAME (mode));
a594a19c
GK
4094 }
4095 }
0ac081f6 4096 }
a4b0320c 4097 else if (TARGET_SPE_ABI && TARGET_SPE && SPE_VECTOR_MODE (mode)
a6c9bed4
AH
4098 && !cum->stdarg
4099 && cum->sysv_gregno <= GP_ARG_MAX_REG)
a4b0320c 4100 cum->sysv_gregno++;
f607bc57 4101 else if (DEFAULT_ABI == ABI_V4)
4697a36c 4102 {
a3170dc6 4103 if (TARGET_HARD_FLOAT && TARGET_FPRS
4cc833b7 4104 && (mode == SFmode || mode == DFmode))
4697a36c 4105 {
4cc833b7
RH
4106 if (cum->fregno <= FP_ARG_V4_MAX_REG)
4107 cum->fregno++;
4108 else
4109 {
4110 if (mode == DFmode)
4111 cum->words += cum->words & 1;
c53bdcf5 4112 cum->words += rs6000_arg_size (mode, type);
4cc833b7 4113 }
4697a36c 4114 }
4cc833b7
RH
4115 else
4116 {
4117 int n_words;
4118 int gregno = cum->sysv_gregno;
4119
4120 /* Aggregates and IEEE quad get passed by reference. */
4121 if ((type && AGGREGATE_TYPE_P (type))
4122 || mode == TFmode)
4123 n_words = 1;
4124 else
c53bdcf5 4125 n_words = rs6000_arg_size (mode, type);
4cc833b7 4126
a4b0320c 4127 /* Long long and SPE vectors are put in odd registers. */
4cc833b7
RH
4128 if (n_words == 2 && (gregno & 1) == 0)
4129 gregno += 1;
4130
a4b0320c
AH
4131 /* Long long and SPE vectors are not split between registers
4132 and stack. */
4cc833b7
RH
4133 if (gregno + n_words - 1 > GP_ARG_MAX_REG)
4134 {
4135 /* Long long is aligned on the stack. */
4136 if (n_words == 2)
4137 cum->words += cum->words & 1;
4138 cum->words += n_words;
4139 }
4697a36c 4140
4cc833b7
RH
4141 /* Note: continuing to accumulate gregno past when we've started
4142 spilling to the stack indicates the fact that we've started
4143 spilling to the stack to expand_builtin_saveregs. */
4144 cum->sysv_gregno = gregno + n_words;
4145 }
4697a36c 4146
4cc833b7
RH
4147 if (TARGET_DEBUG_ARG)
4148 {
4149 fprintf (stderr, "function_adv: words = %2d, fregno = %2d, ",
4150 cum->words, cum->fregno);
4151 fprintf (stderr, "gregno = %2d, nargs = %4d, proto = %d, ",
4152 cum->sysv_gregno, cum->nargs_prototype, cum->prototype);
4153 fprintf (stderr, "mode = %4s, named = %d\n",
4154 GET_MODE_NAME (mode), named);
4155 }
4697a36c
MM
4156 }
4157 else
4cc833b7
RH
4158 {
4159 int align = (TARGET_32BIT && (cum->words & 1) != 0
4160 && function_arg_boundary (mode, type) == 64) ? 1 : 0;
a4f6c312 4161
c53bdcf5 4162 cum->words += align + rs6000_arg_size (mode, type);
4697a36c 4163
a3170dc6
AH
4164 if (GET_MODE_CLASS (mode) == MODE_FLOAT
4165 && TARGET_HARD_FLOAT && TARGET_FPRS)
c53bdcf5 4166 cum->fregno += (GET_MODE_SIZE (mode) + 7) >> 3;
4cc833b7
RH
4167
4168 if (TARGET_DEBUG_ARG)
4169 {
4170 fprintf (stderr, "function_adv: words = %2d, fregno = %2d, ",
4171 cum->words, cum->fregno);
4172 fprintf (stderr, "nargs = %4d, proto = %d, mode = %4s, ",
4173 cum->nargs_prototype, cum->prototype, GET_MODE_NAME (mode));
4174 fprintf (stderr, "named = %d, align = %d\n", named, align);
4175 }
4176 }
4697a36c 4177}
a6c9bed4
AH
4178
4179/* Determine where to put a SIMD argument on the SPE. */
b78d48dd 4180
a6c9bed4 4181static rtx
a2369ed3
DJ
4182rs6000_spe_function_arg (CUMULATIVE_ARGS *cum, enum machine_mode mode,
4183 tree type)
a6c9bed4
AH
4184{
4185 if (cum->stdarg)
4186 {
4187 int gregno = cum->sysv_gregno;
c53bdcf5 4188 int n_words = rs6000_arg_size (mode, type);
a6c9bed4
AH
4189
4190 /* SPE vectors are put in odd registers. */
4191 if (n_words == 2 && (gregno & 1) == 0)
4192 gregno += 1;
4193
4194 if (gregno + n_words - 1 <= GP_ARG_MAX_REG)
4195 {
4196 rtx r1, r2;
4197 enum machine_mode m = SImode;
4198
4199 r1 = gen_rtx_REG (m, gregno);
4200 r1 = gen_rtx_EXPR_LIST (m, r1, const0_rtx);
4201 r2 = gen_rtx_REG (m, gregno + 1);
4202 r2 = gen_rtx_EXPR_LIST (m, r2, GEN_INT (4));
4203 return gen_rtx_PARALLEL (mode, gen_rtvec (2, r1, r2));
4204 }
4205 else
b78d48dd 4206 return NULL_RTX;
a6c9bed4
AH
4207 }
4208 else
4209 {
4210 if (cum->sysv_gregno <= GP_ARG_MAX_REG)
4211 return gen_rtx_REG (mode, cum->sysv_gregno);
4212 else
b78d48dd 4213 return NULL_RTX;
a6c9bed4
AH
4214 }
4215}
4216
b78d48dd
FJ
4217/* Determine where to place an argument in 64-bit mode with 32-bit ABI. */
4218
4219static rtx
4220rs6000_mixed_function_arg (CUMULATIVE_ARGS *cum, enum machine_mode mode,
4221 tree type, int align_words)
4222{
4223 if (mode == DFmode)
4224 {
4225 /* -mpowerpc64 with 32bit ABI splits up a DFmode argument
4226 in vararg list into zero, one or two GPRs */
4227 if (align_words >= GP_ARG_NUM_REG)
4228 return gen_rtx_PARALLEL (DFmode,
4229 gen_rtvec (2,
4230 gen_rtx_EXPR_LIST (VOIDmode,
4231 NULL_RTX, const0_rtx),
4232 gen_rtx_EXPR_LIST (VOIDmode,
4233 gen_rtx_REG (mode,
4234 cum->fregno),
4235 const0_rtx)));
c53bdcf5 4236 else if (align_words + rs6000_arg_size (mode, type)
b78d48dd
FJ
4237 > GP_ARG_NUM_REG)
4238 /* If this is partially on the stack, then we only
a3c9585f 4239 include the portion actually in registers here. */
b78d48dd
FJ
4240 return gen_rtx_PARALLEL (DFmode,
4241 gen_rtvec (2,
4242 gen_rtx_EXPR_LIST (VOIDmode,
4243 gen_rtx_REG (SImode,
4244 GP_ARG_MIN_REG
4245 + align_words),
4246 const0_rtx),
4247 gen_rtx_EXPR_LIST (VOIDmode,
4248 gen_rtx_REG (mode,
4249 cum->fregno),
4250 const0_rtx)));
4251
4252 /* split a DFmode arg into two GPRs */
4253 return gen_rtx_PARALLEL (DFmode,
4254 gen_rtvec (3,
4255 gen_rtx_EXPR_LIST (VOIDmode,
4256 gen_rtx_REG (SImode,
4257 GP_ARG_MIN_REG
4258 + align_words),
4259 const0_rtx),
4260 gen_rtx_EXPR_LIST (VOIDmode,
4261 gen_rtx_REG (SImode,
4262 GP_ARG_MIN_REG
4263 + align_words + 1),
4264 GEN_INT (4)),
4265 gen_rtx_EXPR_LIST (VOIDmode,
4266 gen_rtx_REG (mode, cum->fregno),
4267 const0_rtx)));
4268 }
4269 /* -mpowerpc64 with 32bit ABI splits up a DImode argument into one
4270 or two GPRs */
4271 else if (mode == DImode)
4272 {
4273 if (align_words < GP_ARG_NUM_REG - 1)
4274 return gen_rtx_PARALLEL (DImode,
4275 gen_rtvec (2,
4276 gen_rtx_EXPR_LIST (VOIDmode,
4277 gen_rtx_REG (SImode,
4278 GP_ARG_MIN_REG
4279 + align_words),
4280 const0_rtx),
4281 gen_rtx_EXPR_LIST (VOIDmode,
4282 gen_rtx_REG (SImode,
4283 GP_ARG_MIN_REG
4284 + align_words + 1),
4285 GEN_INT (4))));
4286 else if (align_words == GP_ARG_NUM_REG - 1)
4287 return gen_rtx_PARALLEL (DImode,
4288 gen_rtvec (2,
4289 gen_rtx_EXPR_LIST (VOIDmode,
4290 NULL_RTX, const0_rtx),
4291 gen_rtx_EXPR_LIST (VOIDmode,
4292 gen_rtx_REG (SImode,
4293 GP_ARG_MIN_REG
4294 + align_words),
4295 const0_rtx)));
4296 }
4297 else if (mode == BLKmode && align_words <= (GP_ARG_NUM_REG - 1))
4298 {
4299 int k;
4300 int size = int_size_in_bytes (type);
2f7b62ef 4301 int no_units = ((size - 1) / 4) + 1;
b78d48dd
FJ
4302 int max_no_words = GP_ARG_NUM_REG - align_words;
4303 int rtlvec_len = no_units < max_no_words ? no_units : max_no_words;
4304 rtx *rtlvec = (rtx *) alloca (rtlvec_len * sizeof (rtx));
4305
4306 memset ((char *) rtlvec, 0, rtlvec_len * sizeof (rtx));
4307
4308 for (k=0; k < rtlvec_len; k++)
4309 rtlvec[k] = gen_rtx_EXPR_LIST (VOIDmode,
4310 gen_rtx_REG (SImode,
4311 GP_ARG_MIN_REG
4312 + align_words + k),
4313 k == 0 ? const0_rtx : GEN_INT (k*4));
4314
4315 return gen_rtx_PARALLEL (BLKmode, gen_rtvec_v (k, rtlvec));
4316 }
4317
4318 return NULL_RTX;
4319}
4320
4697a36c
MM
4321/* Determine where to put an argument to a function.
4322 Value is zero to push the argument on the stack,
4323 or a hard register in which to store the argument.
4324
4325 MODE is the argument's machine mode.
4326 TYPE is the data type of the argument (as a tree).
4327 This is null for libcalls where that information may
4328 not be available.
4329 CUM is a variable of type CUMULATIVE_ARGS which gives info about
4330 the preceding args and about the function being called.
4331 NAMED is nonzero if this argument is a named parameter
4332 (otherwise it is an extra parameter matching an ellipsis).
4333
4334 On RS/6000 the first eight words of non-FP are normally in registers
4335 and the rest are pushed. Under AIX, the first 13 FP args are in registers.
4336 Under V.4, the first 8 FP args are in registers.
4337
4338 If this is floating-point and no prototype is specified, we use
4339 both an FP and integer register (or possibly FP reg and stack). Library
b9599e46 4340 functions (when CALL_LIBCALL is set) always have the proper types for args,
4697a36c 4341 so we can pass the FP value just in one register. emit_library_function
1c20ae99 4342 doesn't support PARALLEL anyway. */
4697a36c
MM
4343
4344struct rtx_def *
a2369ed3
DJ
4345function_arg (CUMULATIVE_ARGS *cum, enum machine_mode mode,
4346 tree type, int named)
4697a36c 4347{
4cc833b7 4348 enum rs6000_abi abi = DEFAULT_ABI;
4697a36c 4349
a4f6c312
SS
4350 /* Return a marker to indicate whether CR1 needs to set or clear the
4351 bit that V.4 uses to say fp args were passed in registers.
4352 Assume that we don't need the marker for software floating point,
4353 or compiler generated library calls. */
4697a36c
MM
4354 if (mode == VOIDmode)
4355 {
f607bc57 4356 if (abi == ABI_V4
7509c759 4357 && cum->nargs_prototype < 0
b9599e46
FS
4358 && (cum->call_cookie & CALL_LIBCALL) == 0
4359 && (cum->prototype || TARGET_NO_PROTOTYPE))
7509c759 4360 {
a3170dc6
AH
4361 /* For the SPE, we need to crxor CR6 always. */
4362 if (TARGET_SPE_ABI)
4363 return GEN_INT (cum->call_cookie | CALL_V4_SET_FP_ARGS);
4364 else if (TARGET_HARD_FLOAT && TARGET_FPRS)
4365 return GEN_INT (cum->call_cookie
4366 | ((cum->fregno == FP_ARG_MIN_REG)
4367 ? CALL_V4_SET_FP_ARGS
4368 : CALL_V4_CLEAR_FP_ARGS));
7509c759 4369 }
4697a36c 4370
7509c759 4371 return GEN_INT (cum->call_cookie);
4697a36c
MM
4372 }
4373
2858f73a 4374 if (USE_ALTIVEC_FOR_ARG_P (cum, mode, type, named))
c72d6c26
HP
4375 if (TARGET_64BIT && ! cum->prototype)
4376 {
4377 /* Vector parameters get passed in vector register
4378 and also in GPRs or memory, in absence of prototype. */
4379 int align_words;
4380 rtx slot;
4381 align_words = (cum->words + 1) & ~1;
4382
4383 if (align_words >= GP_ARG_NUM_REG)
4384 {
4385 slot = NULL_RTX;
4386 }
4387 else
4388 {
4389 slot = gen_rtx_REG (mode, GP_ARG_MIN_REG + align_words);
4390 }
4391 return gen_rtx_PARALLEL (mode,
4392 gen_rtvec (2,
4393 gen_rtx_EXPR_LIST (VOIDmode,
4394 slot, const0_rtx),
4395 gen_rtx_EXPR_LIST (VOIDmode,
4396 gen_rtx_REG (mode, cum->vregno),
4397 const0_rtx)));
4398 }
4399 else
4400 return gen_rtx_REG (mode, cum->vregno);
2858f73a 4401 else if (TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (mode))
0ac081f6 4402 {
2858f73a 4403 if (named || abi == ABI_V4)
a594a19c 4404 return NULL_RTX;
0ac081f6 4405 else
a594a19c
GK
4406 {
4407 /* Vector parameters to varargs functions under AIX or Darwin
4408 get passed in memory and possibly also in GPRs. */
4409 int align, align_words;
2858f73a 4410 enum machine_mode part_mode = mode;
a594a19c
GK
4411
4412 /* Vector parameters must be 16-byte aligned. This places them at
2858f73a
GK
4413 2 mod 4 in terms of words in 32-bit mode, since the parameter
4414 save area starts at offset 24 from the stack. In 64-bit mode,
4415 they just have to start on an even word, since the parameter
4416 save area is 16-byte aligned. */
4417 if (TARGET_32BIT)
4418 align = ((6 - (cum->words & 3)) & 3);
4419 else
4420 align = cum->words & 1;
a594a19c
GK
4421 align_words = cum->words + align;
4422
4423 /* Out of registers? Memory, then. */
4424 if (align_words >= GP_ARG_NUM_REG)
4425 return NULL_RTX;
4426
2858f73a
GK
4427 /* The vector value goes in GPRs. Only the part of the
4428 value in GPRs is reported here. */
4429 if (align_words + CLASS_MAX_NREGS (mode, GENERAL_REGS)
4430 > GP_ARG_NUM_REG)
839a4992 4431 /* Fortunately, there are only two possibilities, the value
2858f73a
GK
4432 is either wholly in GPRs or half in GPRs and half not. */
4433 part_mode = DImode;
4434
4435 return gen_rtx_REG (part_mode, GP_ARG_MIN_REG + align_words);
a594a19c 4436 }
0ac081f6 4437 }
a6c9bed4
AH
4438 else if (TARGET_SPE_ABI && TARGET_SPE && SPE_VECTOR_MODE (mode))
4439 return rs6000_spe_function_arg (cum, mode, type);
f607bc57 4440 else if (abi == ABI_V4)
4697a36c 4441 {
a3170dc6 4442 if (TARGET_HARD_FLOAT && TARGET_FPRS
4cc833b7
RH
4443 && (mode == SFmode || mode == DFmode))
4444 {
4445 if (cum->fregno <= FP_ARG_V4_MAX_REG)
4446 return gen_rtx_REG (mode, cum->fregno);
4447 else
b78d48dd 4448 return NULL_RTX;
4cc833b7
RH
4449 }
4450 else
4451 {
4452 int n_words;
4453 int gregno = cum->sysv_gregno;
4454
4455 /* Aggregates and IEEE quad get passed by reference. */
4456 if ((type && AGGREGATE_TYPE_P (type))
4457 || mode == TFmode)
4458 n_words = 1;
4459 else
c53bdcf5 4460 n_words = rs6000_arg_size (mode, type);
4cc833b7 4461
a4b0320c 4462 /* Long long and SPE vectors are put in odd registers. */
4cc833b7
RH
4463 if (n_words == 2 && (gregno & 1) == 0)
4464 gregno += 1;
4465
c53bdcf5 4466 /* Long long does not split between registers and stack. */
4cc833b7 4467 if (gregno + n_words - 1 <= GP_ARG_MAX_REG)
a6c9bed4 4468 return gen_rtx_REG (mode, gregno);
4cc833b7 4469 else
b78d48dd 4470 return NULL_RTX;
4cc833b7 4471 }
4697a36c 4472 }
4cc833b7
RH
4473 else
4474 {
4475 int align = (TARGET_32BIT && (cum->words & 1) != 0
4476 && function_arg_boundary (mode, type) == 64) ? 1 : 0;
4477 int align_words = cum->words + align;
4697a36c 4478
4cc833b7
RH
4479 if (type && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4480 return NULL_RTX;
4481
b78d48dd 4482 if (TARGET_32BIT && TARGET_POWERPC64
e91fa876 4483 && (mode == DImode || mode == BLKmode))
b78d48dd
FJ
4484 return rs6000_mixed_function_arg (cum, mode, type, align_words);
4485
2858f73a 4486 if (USE_FP_FOR_ARG_P (cum, mode, type))
4cc833b7 4487 {
c53bdcf5
AM
4488 rtx fpr[2];
4489 rtx *r;
4490 bool needs_psave;
4491 enum machine_mode fmode = mode;
4492 int n;
4493 unsigned long n_fpreg = (GET_MODE_SIZE (mode) + 7) >> 3;
4494
4495 if (cum->fregno + n_fpreg > FP_ARG_MAX_REG + 1)
4496 {
4497 /* Long double split over regs and memory. */
4498 if (fmode == TFmode)
4499 fmode = DFmode;
4500
4501 /* Currently, we only ever need one reg here because complex
4502 doubles are split. */
4503 if (cum->fregno != FP_ARG_MAX_REG - 1)
4504 abort ();
4505 }
4506 fpr[1] = gen_rtx_REG (fmode, cum->fregno);
4507
4508 /* Do we also need to pass this arg in the parameter save
4509 area? */
4510 needs_psave = (type
4511 && (cum->nargs_prototype <= 0
4512 || (DEFAULT_ABI == ABI_AIX
4513 && TARGET_XL_CALL
4514 && align_words >= GP_ARG_NUM_REG)));
4515
4516 if (!needs_psave && mode == fmode)
4517 return fpr[1];
4cc833b7 4518
e91fa876
FJ
4519 if (TARGET_32BIT && TARGET_POWERPC64
4520 && mode == DFmode && cum->stdarg)
4521 return rs6000_mixed_function_arg (cum, mode, type, align_words);
4522
c53bdcf5
AM
4523 /* Describe where this piece goes. */
4524 r = fpr + 1;
4525 *r = gen_rtx_EXPR_LIST (VOIDmode, *r, const0_rtx);
4526 n = 1;
4527
4528 if (needs_psave)
4529 {
4530 /* Now describe the part that goes in gprs or the stack.
4531 This piece must come first, before the fprs. */
4532 rtx reg = NULL_RTX;
4533 if (align_words < GP_ARG_NUM_REG)
4534 {
4535 unsigned long n_words = rs6000_arg_size (mode, type);
4536 enum machine_mode rmode = mode;
4537
4538 if (align_words + n_words > GP_ARG_NUM_REG)
4539 /* If this is partially on the stack, then we only
4540 include the portion actually in registers here.
4541 We know this can only be one register because
4542 complex doubles are splt. */
4543 rmode = Pmode;
4544 reg = gen_rtx_REG (rmode, GP_ARG_MIN_REG + align_words);
4545 }
4546 *--r = gen_rtx_EXPR_LIST (VOIDmode, reg, const0_rtx);
4547 ++n;
4548 }
4549
4550 return gen_rtx_PARALLEL (mode, gen_rtvec_v (n, r));
4cc833b7
RH
4551 }
4552 else if (align_words < GP_ARG_NUM_REG)
4553 return gen_rtx_REG (mode, GP_ARG_MIN_REG + align_words);
4554 else
4555 return NULL_RTX;
4697a36c 4556 }
4697a36c
MM
4557}
4558\f
4559/* For an arg passed partly in registers and partly in memory,
4560 this is the number of registers used.
4561 For args passed entirely in registers or entirely in memory, zero. */
4562
4563int
a2369ed3 4564function_arg_partial_nregs (CUMULATIVE_ARGS *cum, enum machine_mode mode,
2858f73a 4565 tree type, int named)
4697a36c 4566{
c53bdcf5
AM
4567 int ret = 0;
4568
f607bc57 4569 if (DEFAULT_ABI == ABI_V4)
4697a36c 4570 return 0;
4697a36c 4571
c53bdcf5
AM
4572 if (USE_ALTIVEC_FOR_ARG_P (cum, mode, type, named)
4573 && cum->nargs_prototype >= 0)
4574 return 0;
4575
4576 if (USE_FP_FOR_ARG_P (cum, mode, type))
4697a36c 4577 {
c53bdcf5
AM
4578 if (cum->fregno + ((GET_MODE_SIZE (mode) + 7) >> 3) > FP_ARG_MAX_REG + 1)
4579 ret = FP_ARG_MAX_REG - cum->fregno;
4580 else if (cum->nargs_prototype >= 0)
4697a36c
MM
4581 return 0;
4582 }
4583
4584 if (cum->words < GP_ARG_NUM_REG
c53bdcf5
AM
4585 && GP_ARG_NUM_REG < cum->words + rs6000_arg_size (mode, type))
4586 ret = GP_ARG_NUM_REG - cum->words;
4697a36c 4587
c53bdcf5
AM
4588 if (ret != 0 && TARGET_DEBUG_ARG)
4589 fprintf (stderr, "function_arg_partial_nregs: %d\n", ret);
4697a36c 4590
c53bdcf5 4591 return ret;
4697a36c
MM
4592}
4593\f
4594/* A C expression that indicates when an argument must be passed by
4595 reference. If nonzero for an argument, a copy of that argument is
4596 made in memory and a pointer to the argument is passed instead of
4597 the argument itself. The pointer is passed in whatever way is
4598 appropriate for passing a pointer to that type.
4599
c8c99a68
DE
4600 Under V.4, structures and unions are passed by reference.
4601
4602 As an extension to all ABIs, variable sized types are passed by
4603 reference. */
4697a36c
MM
4604
4605int
a2369ed3
DJ
4606function_arg_pass_by_reference (CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED,
4607 enum machine_mode mode ATTRIBUTE_UNUSED,
d779d0dc 4608 tree type, int named ATTRIBUTE_UNUSED)
4697a36c 4609{
f607bc57 4610 if (DEFAULT_ABI == ABI_V4
4cc833b7
RH
4611 && ((type && AGGREGATE_TYPE_P (type))
4612 || mode == TFmode))
4697a36c
MM
4613 {
4614 if (TARGET_DEBUG_ARG)
4615 fprintf (stderr, "function_arg_pass_by_reference: aggregate\n");
4616
4617 return 1;
4618 }
fff2cb99 4619 return type && int_size_in_bytes (type) < 0;
4697a36c 4620}
5985c7a6
FJ
4621
4622static void
2d9db8eb 4623rs6000_move_block_from_reg (int regno, rtx x, int nregs)
5985c7a6
FJ
4624{
4625 int i;
4626 enum machine_mode reg_mode = TARGET_32BIT ? SImode : DImode;
4627
4628 if (nregs == 0)
4629 return;
4630
4631 for (i = 0; i < nregs; i++)
4632 {
4633 rtx tem = adjust_address_nv (x, reg_mode, i*GET_MODE_SIZE(reg_mode));
4634 if (reload_completed)
4635 {
4636 if (! strict_memory_address_p (reg_mode, XEXP (tem, 0)))
4637 tem = NULL_RTX;
4638 else
4639 tem = simplify_gen_subreg (reg_mode, x, BLKmode,
4640 i * GET_MODE_SIZE(reg_mode));
4641 }
4642 else
4643 tem = replace_equiv_address (tem, XEXP (tem, 0));
4644
4645 if (tem == NULL_RTX)
4646 abort ();
4647
4648 emit_move_insn (tem, gen_rtx_REG (reg_mode, regno + i));
4649 }
4650}
4651
4697a36c
MM
4652\f
4653/* Perform any needed actions needed for a function that is receiving a
4654 variable number of arguments.
4655
4656 CUM is as above.
4657
4658 MODE and TYPE are the mode and type of the current parameter.
4659
4660 PRETEND_SIZE is a variable that should be set to the amount of stack
4661 that must be pushed by the prolog to pretend that our caller pushed
4662 it.
4663
4664 Normally, this macro will push all remaining incoming registers on the
4665 stack and set PRETEND_SIZE to the length of the registers pushed. */
4666
c6e8c921 4667static void
a2369ed3
DJ
4668setup_incoming_varargs (CUMULATIVE_ARGS *cum, enum machine_mode mode,
4669 tree type, int *pretend_size ATTRIBUTE_UNUSED, int no_rtl)
4697a36c 4670{
4cc833b7
RH
4671 CUMULATIVE_ARGS next_cum;
4672 int reg_size = TARGET_32BIT ? 4 : 8;
ca5adc63 4673 rtx save_area = NULL_RTX, mem;
dfafc897 4674 int first_reg_offset, set;
4697a36c 4675
f31bf321 4676 /* Skip the last named argument. */
d34c5b80 4677 next_cum = *cum;
f31bf321 4678 function_arg_advance (&next_cum, mode, type, 1);
4cc833b7 4679
f607bc57 4680 if (DEFAULT_ABI == ABI_V4)
d34c5b80 4681 {
4cc833b7 4682 /* Indicate to allocate space on the stack for varargs save area. */
00dba523 4683 cfun->machine->sysv_varargs_p = 1;
60e2d0ca 4684 if (! no_rtl)
2c4974b7 4685 save_area = plus_constant (virtual_stack_vars_rtx,
bd227acc 4686 - RS6000_VARARGS_SIZE);
4cc833b7
RH
4687
4688 first_reg_offset = next_cum.sysv_gregno - GP_ARG_MIN_REG;
4697a36c 4689 }
60e2d0ca 4690 else
4697a36c 4691 {
d34c5b80 4692 first_reg_offset = next_cum.words;
4cc833b7 4693 save_area = virtual_incoming_args_rtx;
00dba523 4694 cfun->machine->sysv_varargs_p = 0;
4697a36c
MM
4695
4696 if (MUST_PASS_IN_STACK (mode, type))
c53bdcf5 4697 first_reg_offset += rs6000_arg_size (TYPE_MODE (type), type);
4cc833b7 4698 }
4697a36c 4699
dfafc897 4700 set = get_varargs_alias_set ();
c81fc13e 4701 if (! no_rtl && first_reg_offset < GP_ARG_NUM_REG)
4cc833b7 4702 {
dfafc897
FS
4703 mem = gen_rtx_MEM (BLKmode,
4704 plus_constant (save_area,
4705 first_reg_offset * reg_size)),
ba4828e0 4706 set_mem_alias_set (mem, set);
8ac61af7 4707 set_mem_align (mem, BITS_PER_WORD);
dfafc897 4708
5985c7a6
FJ
4709 rs6000_move_block_from_reg (GP_ARG_MIN_REG + first_reg_offset, mem,
4710 GP_ARG_NUM_REG - first_reg_offset);
4697a36c
MM
4711 }
4712
4697a36c 4713 /* Save FP registers if needed. */
f607bc57 4714 if (DEFAULT_ABI == ABI_V4
a3170dc6
AH
4715 && TARGET_HARD_FLOAT && TARGET_FPRS
4716 && ! no_rtl
4cc833b7 4717 && next_cum.fregno <= FP_ARG_V4_MAX_REG)
4697a36c 4718 {
4cc833b7 4719 int fregno = next_cum.fregno;
9ebbca7d 4720 rtx cr1 = gen_rtx_REG (CCmode, CR1_REGNO);
4cc833b7
RH
4721 rtx lab = gen_label_rtx ();
4722 int off = (GP_ARG_NUM_REG * reg_size) + ((fregno - FP_ARG_MIN_REG) * 8);
4697a36c 4723
4cc833b7 4724 emit_jump_insn (gen_rtx_SET (VOIDmode,
4697a36c 4725 pc_rtx,
39403d82 4726 gen_rtx_IF_THEN_ELSE (VOIDmode,
4cc833b7
RH
4727 gen_rtx_NE (VOIDmode, cr1,
4728 const0_rtx),
39403d82 4729 gen_rtx_LABEL_REF (VOIDmode, lab),
4697a36c
MM
4730 pc_rtx)));
4731
4cc833b7
RH
4732 while (fregno <= FP_ARG_V4_MAX_REG)
4733 {
dfafc897 4734 mem = gen_rtx_MEM (DFmode, plus_constant (save_area, off));
ba4828e0 4735 set_mem_alias_set (mem, set);
dfafc897 4736 emit_move_insn (mem, gen_rtx_REG (DFmode, fregno));
4cc833b7
RH
4737 fregno++;
4738 off += 8;
4697a36c 4739 }
4cc833b7
RH
4740
4741 emit_label (lab);
4697a36c 4742 }
4697a36c 4743}
4697a36c 4744
dfafc897 4745/* Create the va_list data type. */
2c4974b7 4746
c35d187f
RH
4747static tree
4748rs6000_build_builtin_va_list (void)
dfafc897 4749{
64c2816f 4750 tree f_gpr, f_fpr, f_res, f_ovf, f_sav, record, type_decl;
4697a36c 4751
9ebbca7d
GK
4752 /* For AIX, prefer 'char *' because that's what the system
4753 header files like. */
f607bc57 4754 if (DEFAULT_ABI != ABI_V4)
9ebbca7d 4755 return build_pointer_type (char_type_node);
dfafc897 4756
f1e639b1 4757 record = (*lang_hooks.types.make_type) (RECORD_TYPE);
bab45a51 4758 type_decl = build_decl (TYPE_DECL, get_identifier ("__va_list_tag"), record);
0f4fd75d 4759
9ebbca7d
GK
4760 f_gpr = build_decl (FIELD_DECL, get_identifier ("gpr"),
4761 unsigned_char_type_node);
4762 f_fpr = build_decl (FIELD_DECL, get_identifier ("fpr"),
4763 unsigned_char_type_node);
64c2816f
DT
4764 /* Give the two bytes of padding a name, so that -Wpadded won't warn on
4765 every user file. */
4766 f_res = build_decl (FIELD_DECL, get_identifier ("reserved"),
4767 short_unsigned_type_node);
dfafc897
FS
4768 f_ovf = build_decl (FIELD_DECL, get_identifier ("overflow_arg_area"),
4769 ptr_type_node);
4770 f_sav = build_decl (FIELD_DECL, get_identifier ("reg_save_area"),
4771 ptr_type_node);
4772
4773 DECL_FIELD_CONTEXT (f_gpr) = record;
4774 DECL_FIELD_CONTEXT (f_fpr) = record;
64c2816f 4775 DECL_FIELD_CONTEXT (f_res) = record;
dfafc897
FS
4776 DECL_FIELD_CONTEXT (f_ovf) = record;
4777 DECL_FIELD_CONTEXT (f_sav) = record;
4778
bab45a51
FS
4779 TREE_CHAIN (record) = type_decl;
4780 TYPE_NAME (record) = type_decl;
dfafc897
FS
4781 TYPE_FIELDS (record) = f_gpr;
4782 TREE_CHAIN (f_gpr) = f_fpr;
64c2816f
DT
4783 TREE_CHAIN (f_fpr) = f_res;
4784 TREE_CHAIN (f_res) = f_ovf;
dfafc897
FS
4785 TREE_CHAIN (f_ovf) = f_sav;
4786
4787 layout_type (record);
4788
4789 /* The correct type is an array type of one element. */
4790 return build_array_type (record, build_index_type (size_zero_node));
4791}
4792
4793/* Implement va_start. */
4794
4795void
a2369ed3 4796rs6000_va_start (tree valist, rtx nextarg)
4697a36c 4797{
dfafc897 4798 HOST_WIDE_INT words, n_gpr, n_fpr;
c566f9bd 4799 tree f_gpr, f_fpr, f_res, f_ovf, f_sav;
dfafc897 4800 tree gpr, fpr, ovf, sav, t;
2c4974b7 4801
dfafc897 4802 /* Only SVR4 needs something special. */
f607bc57 4803 if (DEFAULT_ABI != ABI_V4)
dfafc897 4804 {
e5faf155 4805 std_expand_builtin_va_start (valist, nextarg);
dfafc897
FS
4806 return;
4807 }
4808
973a648b 4809 f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
dfafc897 4810 f_fpr = TREE_CHAIN (f_gpr);
c566f9bd
DT
4811 f_res = TREE_CHAIN (f_fpr);
4812 f_ovf = TREE_CHAIN (f_res);
dfafc897
FS
4813 f_sav = TREE_CHAIN (f_ovf);
4814
8ebecc3b 4815 valist = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (valist)), valist);
dfafc897
FS
4816 gpr = build (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr);
4817 fpr = build (COMPONENT_REF, TREE_TYPE (f_fpr), valist, f_fpr);
4818 ovf = build (COMPONENT_REF, TREE_TYPE (f_ovf), valist, f_ovf);
4819 sav = build (COMPONENT_REF, TREE_TYPE (f_sav), valist, f_sav);
4820
4821 /* Count number of gp and fp argument registers used. */
4cc833b7 4822 words = current_function_args_info.words;
dfafc897
FS
4823 n_gpr = current_function_args_info.sysv_gregno - GP_ARG_MIN_REG;
4824 n_fpr = current_function_args_info.fregno - FP_ARG_MIN_REG;
4825
4826 if (TARGET_DEBUG_ARG)
4a0a75dd
KG
4827 fprintf (stderr, "va_start: words = "HOST_WIDE_INT_PRINT_DEC", n_gpr = "
4828 HOST_WIDE_INT_PRINT_DEC", n_fpr = "HOST_WIDE_INT_PRINT_DEC"\n",
4829 words, n_gpr, n_fpr);
dfafc897
FS
4830
4831 t = build (MODIFY_EXPR, TREE_TYPE (gpr), gpr, build_int_2 (n_gpr, 0));
4832 TREE_SIDE_EFFECTS (t) = 1;
4833 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4834
4835 t = build (MODIFY_EXPR, TREE_TYPE (fpr), fpr, build_int_2 (n_fpr, 0));
4836 TREE_SIDE_EFFECTS (t) = 1;
4837 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4838
4839 /* Find the overflow area. */
4840 t = make_tree (TREE_TYPE (ovf), virtual_incoming_args_rtx);
4841 if (words != 0)
4842 t = build (PLUS_EXPR, TREE_TYPE (ovf), t,
4843 build_int_2 (words * UNITS_PER_WORD, 0));
4844 t = build (MODIFY_EXPR, TREE_TYPE (ovf), ovf, t);
4845 TREE_SIDE_EFFECTS (t) = 1;
4846 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4847
4848 /* Find the register save area. */
4849 t = make_tree (TREE_TYPE (sav), virtual_stack_vars_rtx);
4850 t = build (PLUS_EXPR, TREE_TYPE (sav), t,
4851 build_int_2 (-RS6000_VARARGS_SIZE, -1));
4852 t = build (MODIFY_EXPR, TREE_TYPE (sav), sav, t);
4853 TREE_SIDE_EFFECTS (t) = 1;
4854 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4855}
4856
4857/* Implement va_arg. */
4858
4859rtx
a2369ed3 4860rs6000_va_arg (tree valist, tree type)
dfafc897 4861{
c566f9bd 4862 tree f_gpr, f_fpr, f_res, f_ovf, f_sav;
dfafc897
FS
4863 tree gpr, fpr, ovf, sav, reg, t, u;
4864 int indirect_p, size, rsize, n_reg, sav_ofs, sav_scale;
4865 rtx lab_false, lab_over, addr_rtx, r;
4866
f607bc57 4867 if (DEFAULT_ABI != ABI_V4)
c8c99a68
DE
4868 {
4869 /* Variable sized types are passed by reference. */
fff2cb99 4870 if (int_size_in_bytes (type) < 0)
c8c99a68
DE
4871 {
4872 u = build_pointer_type (type);
4873
4874 /* Args grow upward. */
4875 t = build (POSTINCREMENT_EXPR, TREE_TYPE (valist), valist,
4876 build_int_2 (POINTER_SIZE / BITS_PER_UNIT, 0));
4877 TREE_SIDE_EFFECTS (t) = 1;
4878
4879 t = build1 (NOP_EXPR, build_pointer_type (u), t);
4880 TREE_SIDE_EFFECTS (t) = 1;
4881
4882 t = build1 (INDIRECT_REF, u, t);
4883 TREE_SIDE_EFFECTS (t) = 1;
4884
4885 return expand_expr (t, NULL_RTX, VOIDmode, EXPAND_NORMAL);
4886 }
fa78dbea 4887 if (targetm.calls.split_complex_arg
c53bdcf5
AM
4888 && TREE_CODE (type) == COMPLEX_TYPE)
4889 {
4890 tree elem_type = TREE_TYPE (type);
4891 enum machine_mode elem_mode = TYPE_MODE (elem_type);
4892 int elem_size = GET_MODE_SIZE (elem_mode);
4893
4894 if (elem_size < UNITS_PER_WORD)
4895 {
4896 rtx real_part, imag_part, dest_real, rr;
4897
4898 real_part = rs6000_va_arg (valist, elem_type);
4899 imag_part = rs6000_va_arg (valist, elem_type);
4900
4901 /* We're not returning the value here, but the address.
4902 real_part and imag_part are not contiguous, and we know
4903 there is space available to pack real_part next to
4904 imag_part. float _Complex is not promoted to
4905 double _Complex by the default promotion rules that
4906 promote float to double. */
4907 if (2 * elem_size > UNITS_PER_WORD)
4908 abort ();
4909
4910 real_part = gen_rtx_MEM (elem_mode, real_part);
4911 imag_part = gen_rtx_MEM (elem_mode, imag_part);
4912
4913 dest_real = adjust_address (imag_part, elem_mode, -elem_size);
4914 rr = gen_reg_rtx (elem_mode);
4915 emit_move_insn (rr, real_part);
4916 emit_move_insn (dest_real, rr);
4917
4918 return XEXP (dest_real, 0);
4919 }
4920 }
4921
4922 return std_expand_builtin_va_arg (valist, type);
c8c99a68 4923 }
dfafc897 4924
973a648b 4925 f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
dfafc897 4926 f_fpr = TREE_CHAIN (f_gpr);
c566f9bd
DT
4927 f_res = TREE_CHAIN (f_fpr);
4928 f_ovf = TREE_CHAIN (f_res);
dfafc897
FS
4929 f_sav = TREE_CHAIN (f_ovf);
4930
8ebecc3b 4931 valist = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (valist)), valist);
dfafc897
FS
4932 gpr = build (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr);
4933 fpr = build (COMPONENT_REF, TREE_TYPE (f_fpr), valist, f_fpr);
4934 ovf = build (COMPONENT_REF, TREE_TYPE (f_ovf), valist, f_ovf);
4935 sav = build (COMPONENT_REF, TREE_TYPE (f_sav), valist, f_sav);
4936
4937 size = int_size_in_bytes (type);
4938 rsize = (size + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
4cc833b7 4939
dfafc897 4940 if (AGGREGATE_TYPE_P (type) || TYPE_MODE (type) == TFmode)
4cc833b7 4941 {
dfafc897
FS
4942 /* Aggregates and long doubles are passed by reference. */
4943 indirect_p = 1;
4944 reg = gpr;
4945 n_reg = 1;
4946 sav_ofs = 0;
4947 sav_scale = 4;
d3294cd9
FS
4948 size = UNITS_PER_WORD;
4949 rsize = 1;
dfafc897 4950 }
a3170dc6 4951 else if (FLOAT_TYPE_P (type) && TARGET_HARD_FLOAT && TARGET_FPRS)
dfafc897
FS
4952 {
4953 /* FP args go in FP registers, if present. */
4954 indirect_p = 0;
4955 reg = fpr;
4956 n_reg = 1;
4957 sav_ofs = 8*4;
4958 sav_scale = 8;
4cc833b7 4959 }
dfafc897
FS
4960 else
4961 {
4962 /* Otherwise into GP registers. */
4963 indirect_p = 0;
4964 reg = gpr;
4965 n_reg = rsize;
4966 sav_ofs = 0;
4967 sav_scale = 4;
4968 }
4969
a3c9585f 4970 /* Pull the value out of the saved registers.... */
dfafc897
FS
4971
4972 lab_false = gen_label_rtx ();
4973 lab_over = gen_label_rtx ();
4974 addr_rtx = gen_reg_rtx (Pmode);
4975
16861f33
AH
4976 /* AltiVec vectors never go in registers. */
4977 if (!TARGET_ALTIVEC || TREE_CODE (type) != VECTOR_TYPE)
2c4974b7 4978 {
41daaf0e
AH
4979 TREE_THIS_VOLATILE (reg) = 1;
4980 emit_cmp_and_jump_insns
4981 (expand_expr (reg, NULL_RTX, QImode, EXPAND_NORMAL),
4982 GEN_INT (8 - n_reg + 1), GE, const1_rtx, QImode, 1,
4983 lab_false);
dfafc897 4984
41daaf0e
AH
4985 /* Long long is aligned in the registers. */
4986 if (n_reg > 1)
4987 {
4988 u = build (BIT_AND_EXPR, TREE_TYPE (reg), reg,
4989 build_int_2 (n_reg - 1, 0));
4990 u = build (PLUS_EXPR, TREE_TYPE (reg), reg, u);
4991 u = build (MODIFY_EXPR, TREE_TYPE (reg), reg, u);
4992 TREE_SIDE_EFFECTS (u) = 1;
4993 expand_expr (u, const0_rtx, VOIDmode, EXPAND_NORMAL);
4994 }
2c4974b7 4995
41daaf0e
AH
4996 if (sav_ofs)
4997 t = build (PLUS_EXPR, ptr_type_node, sav, build_int_2 (sav_ofs, 0));
4998 else
4999 t = sav;
2c4974b7 5000
41daaf0e
AH
5001 u = build (POSTINCREMENT_EXPR, TREE_TYPE (reg), reg,
5002 build_int_2 (n_reg, 0));
5003 TREE_SIDE_EFFECTS (u) = 1;
2c4974b7 5004
41daaf0e
AH
5005 u = build1 (CONVERT_EXPR, integer_type_node, u);
5006 TREE_SIDE_EFFECTS (u) = 1;
dfafc897 5007
41daaf0e
AH
5008 u = build (MULT_EXPR, integer_type_node, u, build_int_2 (sav_scale, 0));
5009 TREE_SIDE_EFFECTS (u) = 1;
dfafc897 5010
41daaf0e
AH
5011 t = build (PLUS_EXPR, ptr_type_node, t, u);
5012 TREE_SIDE_EFFECTS (t) = 1;
5013
5014 r = expand_expr (t, addr_rtx, Pmode, EXPAND_NORMAL);
5015 if (r != addr_rtx)
5016 emit_move_insn (addr_rtx, r);
5017
5018 emit_jump_insn (gen_jump (lab_over));
5019 emit_barrier ();
5020 }
dfafc897 5021
dfafc897
FS
5022 emit_label (lab_false);
5023
a4f6c312 5024 /* ... otherwise out of the overflow area. */
dfafc897 5025
41daaf0e
AH
5026 /* Make sure we don't find reg 7 for the next int arg.
5027
5028 All AltiVec vectors go in the overflow area. So in the AltiVec
5029 case we need to get the vectors from the overflow area, but
5030 remember where the GPRs and FPRs are. */
16861f33
AH
5031 if (n_reg > 1 && (TREE_CODE (type) != VECTOR_TYPE
5032 || !TARGET_ALTIVEC))
dfafc897
FS
5033 {
5034 t = build (MODIFY_EXPR, TREE_TYPE (reg), reg, build_int_2 (8, 0));
5035 TREE_SIDE_EFFECTS (t) = 1;
5036 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5037 }
5038
5039 /* Care for on-stack alignment if needed. */
5040 if (rsize <= 1)
5041 t = ovf;
5042 else
5043 {
41daaf0e
AH
5044 int align;
5045
16861f33
AH
5046 /* AltiVec vectors are 16 byte aligned. */
5047 if (TARGET_ALTIVEC && TREE_CODE (type) == VECTOR_TYPE)
41daaf0e
AH
5048 align = 15;
5049 else
5050 align = 7;
5051
5052 t = build (PLUS_EXPR, TREE_TYPE (ovf), ovf, build_int_2 (align, 0));
5053 t = build (BIT_AND_EXPR, TREE_TYPE (t), t, build_int_2 (-align-1, -1));
dfafc897
FS
5054 }
5055 t = save_expr (t);
5056
5057 r = expand_expr (t, addr_rtx, Pmode, EXPAND_NORMAL);
5058 if (r != addr_rtx)
5059 emit_move_insn (addr_rtx, r);
5060
5061 t = build (PLUS_EXPR, TREE_TYPE (t), t, build_int_2 (size, 0));
5062 t = build (MODIFY_EXPR, TREE_TYPE (ovf), ovf, t);
5063 TREE_SIDE_EFFECTS (t) = 1;
5064 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5065
5066 emit_label (lab_over);
5067
5068 if (indirect_p)
5069 {
5070 r = gen_rtx_MEM (Pmode, addr_rtx);
ba4828e0 5071 set_mem_alias_set (r, get_varargs_alias_set ());
dfafc897
FS
5072 emit_move_insn (addr_rtx, r);
5073 }
5074
5075 return addr_rtx;
4697a36c 5076}
0ac081f6
AH
5077
5078/* Builtins. */
5079
6a2dd09a
RS
5080#define def_builtin(MASK, NAME, TYPE, CODE) \
5081do { \
5082 if ((MASK) & target_flags) \
5083 builtin_function ((NAME), (TYPE), (CODE), BUILT_IN_MD, \
5084 NULL, NULL_TREE); \
0ac081f6
AH
5085} while (0)
5086
24408032
AH
5087/* Simple ternary operations: VECd = foo (VECa, VECb, VECc). */
5088
2212663f 5089static const struct builtin_description bdesc_3arg[] =
24408032
AH
5090{
5091 { MASK_ALTIVEC, CODE_FOR_altivec_vmaddfp, "__builtin_altivec_vmaddfp", ALTIVEC_BUILTIN_VMADDFP },
5092 { MASK_ALTIVEC, CODE_FOR_altivec_vmhaddshs, "__builtin_altivec_vmhaddshs", ALTIVEC_BUILTIN_VMHADDSHS },
5093 { MASK_ALTIVEC, CODE_FOR_altivec_vmhraddshs, "__builtin_altivec_vmhraddshs", ALTIVEC_BUILTIN_VMHRADDSHS },
5094 { MASK_ALTIVEC, CODE_FOR_altivec_vmladduhm, "__builtin_altivec_vmladduhm", ALTIVEC_BUILTIN_VMLADDUHM},
5095 { MASK_ALTIVEC, CODE_FOR_altivec_vmsumubm, "__builtin_altivec_vmsumubm", ALTIVEC_BUILTIN_VMSUMUBM },
5096 { MASK_ALTIVEC, CODE_FOR_altivec_vmsummbm, "__builtin_altivec_vmsummbm", ALTIVEC_BUILTIN_VMSUMMBM },
5097 { MASK_ALTIVEC, CODE_FOR_altivec_vmsumuhm, "__builtin_altivec_vmsumuhm", ALTIVEC_BUILTIN_VMSUMUHM },
5098 { MASK_ALTIVEC, CODE_FOR_altivec_vmsumshm, "__builtin_altivec_vmsumshm", ALTIVEC_BUILTIN_VMSUMSHM },
5099 { MASK_ALTIVEC, CODE_FOR_altivec_vmsumuhs, "__builtin_altivec_vmsumuhs", ALTIVEC_BUILTIN_VMSUMUHS },
5100 { MASK_ALTIVEC, CODE_FOR_altivec_vmsumshs, "__builtin_altivec_vmsumshs", ALTIVEC_BUILTIN_VMSUMSHS },
5101 { MASK_ALTIVEC, CODE_FOR_altivec_vnmsubfp, "__builtin_altivec_vnmsubfp", ALTIVEC_BUILTIN_VNMSUBFP },
5102 { MASK_ALTIVEC, CODE_FOR_altivec_vperm_4sf, "__builtin_altivec_vperm_4sf", ALTIVEC_BUILTIN_VPERM_4SF },
5103 { MASK_ALTIVEC, CODE_FOR_altivec_vperm_4si, "__builtin_altivec_vperm_4si", ALTIVEC_BUILTIN_VPERM_4SI },
5104 { MASK_ALTIVEC, CODE_FOR_altivec_vperm_8hi, "__builtin_altivec_vperm_8hi", ALTIVEC_BUILTIN_VPERM_8HI },
5105 { MASK_ALTIVEC, CODE_FOR_altivec_vperm_16qi, "__builtin_altivec_vperm_16qi", ALTIVEC_BUILTIN_VPERM_16QI },
5106 { MASK_ALTIVEC, CODE_FOR_altivec_vsel_4sf, "__builtin_altivec_vsel_4sf", ALTIVEC_BUILTIN_VSEL_4SF },
5107 { MASK_ALTIVEC, CODE_FOR_altivec_vsel_4si, "__builtin_altivec_vsel_4si", ALTIVEC_BUILTIN_VSEL_4SI },
5108 { MASK_ALTIVEC, CODE_FOR_altivec_vsel_8hi, "__builtin_altivec_vsel_8hi", ALTIVEC_BUILTIN_VSEL_8HI },
5109 { MASK_ALTIVEC, CODE_FOR_altivec_vsel_16qi, "__builtin_altivec_vsel_16qi", ALTIVEC_BUILTIN_VSEL_16QI },
5110 { MASK_ALTIVEC, CODE_FOR_altivec_vsldoi_16qi, "__builtin_altivec_vsldoi_16qi", ALTIVEC_BUILTIN_VSLDOI_16QI },
5111 { MASK_ALTIVEC, CODE_FOR_altivec_vsldoi_8hi, "__builtin_altivec_vsldoi_8hi", ALTIVEC_BUILTIN_VSLDOI_8HI },
5112 { MASK_ALTIVEC, CODE_FOR_altivec_vsldoi_4si, "__builtin_altivec_vsldoi_4si", ALTIVEC_BUILTIN_VSLDOI_4SI },
5113 { MASK_ALTIVEC, CODE_FOR_altivec_vsldoi_4sf, "__builtin_altivec_vsldoi_4sf", ALTIVEC_BUILTIN_VSLDOI_4SF },
5114};
2212663f 5115
95385cbb
AH
5116/* DST operations: void foo (void *, const int, const char). */
5117
5118static const struct builtin_description bdesc_dst[] =
5119{
5120 { MASK_ALTIVEC, CODE_FOR_altivec_dst, "__builtin_altivec_dst", ALTIVEC_BUILTIN_DST },
5121 { MASK_ALTIVEC, CODE_FOR_altivec_dstt, "__builtin_altivec_dstt", ALTIVEC_BUILTIN_DSTT },
5122 { MASK_ALTIVEC, CODE_FOR_altivec_dstst, "__builtin_altivec_dstst", ALTIVEC_BUILTIN_DSTST },
5123 { MASK_ALTIVEC, CODE_FOR_altivec_dststt, "__builtin_altivec_dststt", ALTIVEC_BUILTIN_DSTSTT }
5124};
5125
2212663f 5126/* Simple binary operations: VECc = foo (VECa, VECb). */
24408032 5127
a3170dc6 5128static struct builtin_description bdesc_2arg[] =
0ac081f6 5129{
f18c054f
DB
5130 { MASK_ALTIVEC, CODE_FOR_addv16qi3, "__builtin_altivec_vaddubm", ALTIVEC_BUILTIN_VADDUBM },
5131 { MASK_ALTIVEC, CODE_FOR_addv8hi3, "__builtin_altivec_vadduhm", ALTIVEC_BUILTIN_VADDUHM },
5132 { MASK_ALTIVEC, CODE_FOR_addv4si3, "__builtin_altivec_vadduwm", ALTIVEC_BUILTIN_VADDUWM },
5133 { MASK_ALTIVEC, CODE_FOR_addv4sf3, "__builtin_altivec_vaddfp", ALTIVEC_BUILTIN_VADDFP },
0ac081f6
AH
5134 { MASK_ALTIVEC, CODE_FOR_altivec_vaddcuw, "__builtin_altivec_vaddcuw", ALTIVEC_BUILTIN_VADDCUW },
5135 { MASK_ALTIVEC, CODE_FOR_altivec_vaddubs, "__builtin_altivec_vaddubs", ALTIVEC_BUILTIN_VADDUBS },
5136 { MASK_ALTIVEC, CODE_FOR_altivec_vaddsbs, "__builtin_altivec_vaddsbs", ALTIVEC_BUILTIN_VADDSBS },
5137 { MASK_ALTIVEC, CODE_FOR_altivec_vadduhs, "__builtin_altivec_vadduhs", ALTIVEC_BUILTIN_VADDUHS },
5138 { MASK_ALTIVEC, CODE_FOR_altivec_vaddshs, "__builtin_altivec_vaddshs", ALTIVEC_BUILTIN_VADDSHS },
5139 { MASK_ALTIVEC, CODE_FOR_altivec_vadduws, "__builtin_altivec_vadduws", ALTIVEC_BUILTIN_VADDUWS },
5140 { MASK_ALTIVEC, CODE_FOR_altivec_vaddsws, "__builtin_altivec_vaddsws", ALTIVEC_BUILTIN_VADDSWS },
f18c054f 5141 { MASK_ALTIVEC, CODE_FOR_andv4si3, "__builtin_altivec_vand", ALTIVEC_BUILTIN_VAND },
0ac081f6
AH
5142 { MASK_ALTIVEC, CODE_FOR_altivec_vandc, "__builtin_altivec_vandc", ALTIVEC_BUILTIN_VANDC },
5143 { MASK_ALTIVEC, CODE_FOR_altivec_vavgub, "__builtin_altivec_vavgub", ALTIVEC_BUILTIN_VAVGUB },
5144 { MASK_ALTIVEC, CODE_FOR_altivec_vavgsb, "__builtin_altivec_vavgsb", ALTIVEC_BUILTIN_VAVGSB },
5145 { MASK_ALTIVEC, CODE_FOR_altivec_vavguh, "__builtin_altivec_vavguh", ALTIVEC_BUILTIN_VAVGUH },
5146 { MASK_ALTIVEC, CODE_FOR_altivec_vavgsh, "__builtin_altivec_vavgsh", ALTIVEC_BUILTIN_VAVGSH },
5147 { MASK_ALTIVEC, CODE_FOR_altivec_vavguw, "__builtin_altivec_vavguw", ALTIVEC_BUILTIN_VAVGUW },
5148 { MASK_ALTIVEC, CODE_FOR_altivec_vavgsw, "__builtin_altivec_vavgsw", ALTIVEC_BUILTIN_VAVGSW },
617e0e1d
DB
5149 { MASK_ALTIVEC, CODE_FOR_altivec_vcfux, "__builtin_altivec_vcfux", ALTIVEC_BUILTIN_VCFUX },
5150 { MASK_ALTIVEC, CODE_FOR_altivec_vcfsx, "__builtin_altivec_vcfsx", ALTIVEC_BUILTIN_VCFSX },
0ac081f6
AH
5151 { MASK_ALTIVEC, CODE_FOR_altivec_vcmpbfp, "__builtin_altivec_vcmpbfp", ALTIVEC_BUILTIN_VCMPBFP },
5152 { MASK_ALTIVEC, CODE_FOR_altivec_vcmpequb, "__builtin_altivec_vcmpequb", ALTIVEC_BUILTIN_VCMPEQUB },
5153 { MASK_ALTIVEC, CODE_FOR_altivec_vcmpequh, "__builtin_altivec_vcmpequh", ALTIVEC_BUILTIN_VCMPEQUH },
5154 { MASK_ALTIVEC, CODE_FOR_altivec_vcmpequw, "__builtin_altivec_vcmpequw", ALTIVEC_BUILTIN_VCMPEQUW },
5155 { MASK_ALTIVEC, CODE_FOR_altivec_vcmpeqfp, "__builtin_altivec_vcmpeqfp", ALTIVEC_BUILTIN_VCMPEQFP },
5156 { MASK_ALTIVEC, CODE_FOR_altivec_vcmpgefp, "__builtin_altivec_vcmpgefp", ALTIVEC_BUILTIN_VCMPGEFP },
5157 { MASK_ALTIVEC, CODE_FOR_altivec_vcmpgtub, "__builtin_altivec_vcmpgtub", ALTIVEC_BUILTIN_VCMPGTUB },
5158 { MASK_ALTIVEC, CODE_FOR_altivec_vcmpgtsb, "__builtin_altivec_vcmpgtsb", ALTIVEC_BUILTIN_VCMPGTSB },
5159 { MASK_ALTIVEC, CODE_FOR_altivec_vcmpgtuh, "__builtin_altivec_vcmpgtuh", ALTIVEC_BUILTIN_VCMPGTUH },
5160 { MASK_ALTIVEC, CODE_FOR_altivec_vcmpgtsh, "__builtin_altivec_vcmpgtsh", ALTIVEC_BUILTIN_VCMPGTSH },
5161 { MASK_ALTIVEC, CODE_FOR_altivec_vcmpgtuw, "__builtin_altivec_vcmpgtuw", ALTIVEC_BUILTIN_VCMPGTUW },
5162 { MASK_ALTIVEC, CODE_FOR_altivec_vcmpgtsw, "__builtin_altivec_vcmpgtsw", ALTIVEC_BUILTIN_VCMPGTSW },
5163 { MASK_ALTIVEC, CODE_FOR_altivec_vcmpgtfp, "__builtin_altivec_vcmpgtfp", ALTIVEC_BUILTIN_VCMPGTFP },
617e0e1d
DB
5164 { MASK_ALTIVEC, CODE_FOR_altivec_vctsxs, "__builtin_altivec_vctsxs", ALTIVEC_BUILTIN_VCTSXS },
5165 { MASK_ALTIVEC, CODE_FOR_altivec_vctuxs, "__builtin_altivec_vctuxs", ALTIVEC_BUILTIN_VCTUXS },
f18c054f
DB
5166 { MASK_ALTIVEC, CODE_FOR_umaxv16qi3, "__builtin_altivec_vmaxub", ALTIVEC_BUILTIN_VMAXUB },
5167 { MASK_ALTIVEC, CODE_FOR_smaxv16qi3, "__builtin_altivec_vmaxsb", ALTIVEC_BUILTIN_VMAXSB },
df966bff
AH
5168 { MASK_ALTIVEC, CODE_FOR_umaxv8hi3, "__builtin_altivec_vmaxuh", ALTIVEC_BUILTIN_VMAXUH },
5169 { MASK_ALTIVEC, CODE_FOR_smaxv8hi3, "__builtin_altivec_vmaxsh", ALTIVEC_BUILTIN_VMAXSH },
5170 { MASK_ALTIVEC, CODE_FOR_umaxv4si3, "__builtin_altivec_vmaxuw", ALTIVEC_BUILTIN_VMAXUW },
5171 { MASK_ALTIVEC, CODE_FOR_smaxv4si3, "__builtin_altivec_vmaxsw", ALTIVEC_BUILTIN_VMAXSW },
5172 { MASK_ALTIVEC, CODE_FOR_smaxv4sf3, "__builtin_altivec_vmaxfp", ALTIVEC_BUILTIN_VMAXFP },
0ac081f6
AH
5173 { MASK_ALTIVEC, CODE_FOR_altivec_vmrghb, "__builtin_altivec_vmrghb", ALTIVEC_BUILTIN_VMRGHB },
5174 { MASK_ALTIVEC, CODE_FOR_altivec_vmrghh, "__builtin_altivec_vmrghh", ALTIVEC_BUILTIN_VMRGHH },
5175 { MASK_ALTIVEC, CODE_FOR_altivec_vmrghw, "__builtin_altivec_vmrghw", ALTIVEC_BUILTIN_VMRGHW },
5176 { MASK_ALTIVEC, CODE_FOR_altivec_vmrglb, "__builtin_altivec_vmrglb", ALTIVEC_BUILTIN_VMRGLB },
5177 { MASK_ALTIVEC, CODE_FOR_altivec_vmrglh, "__builtin_altivec_vmrglh", ALTIVEC_BUILTIN_VMRGLH },
5178 { MASK_ALTIVEC, CODE_FOR_altivec_vmrglw, "__builtin_altivec_vmrglw", ALTIVEC_BUILTIN_VMRGLW },
f18c054f
DB
5179 { MASK_ALTIVEC, CODE_FOR_uminv16qi3, "__builtin_altivec_vminub", ALTIVEC_BUILTIN_VMINUB },
5180 { MASK_ALTIVEC, CODE_FOR_sminv16qi3, "__builtin_altivec_vminsb", ALTIVEC_BUILTIN_VMINSB },
5181 { MASK_ALTIVEC, CODE_FOR_uminv8hi3, "__builtin_altivec_vminuh", ALTIVEC_BUILTIN_VMINUH },
5182 { MASK_ALTIVEC, CODE_FOR_sminv8hi3, "__builtin_altivec_vminsh", ALTIVEC_BUILTIN_VMINSH },
5183 { MASK_ALTIVEC, CODE_FOR_uminv4si3, "__builtin_altivec_vminuw", ALTIVEC_BUILTIN_VMINUW },
5184 { MASK_ALTIVEC, CODE_FOR_sminv4si3, "__builtin_altivec_vminsw", ALTIVEC_BUILTIN_VMINSW },
5185 { MASK_ALTIVEC, CODE_FOR_sminv4sf3, "__builtin_altivec_vminfp", ALTIVEC_BUILTIN_VMINFP },
0ac081f6
AH
5186 { MASK_ALTIVEC, CODE_FOR_altivec_vmuleub, "__builtin_altivec_vmuleub", ALTIVEC_BUILTIN_VMULEUB },
5187 { MASK_ALTIVEC, CODE_FOR_altivec_vmulesb, "__builtin_altivec_vmulesb", ALTIVEC_BUILTIN_VMULESB },
5188 { MASK_ALTIVEC, CODE_FOR_altivec_vmuleuh, "__builtin_altivec_vmuleuh", ALTIVEC_BUILTIN_VMULEUH },
5189 { MASK_ALTIVEC, CODE_FOR_altivec_vmulesh, "__builtin_altivec_vmulesh", ALTIVEC_BUILTIN_VMULESH },
5190 { MASK_ALTIVEC, CODE_FOR_altivec_vmuloub, "__builtin_altivec_vmuloub", ALTIVEC_BUILTIN_VMULOUB },
5191 { MASK_ALTIVEC, CODE_FOR_altivec_vmulosb, "__builtin_altivec_vmulosb", ALTIVEC_BUILTIN_VMULOSB },
5192 { MASK_ALTIVEC, CODE_FOR_altivec_vmulouh, "__builtin_altivec_vmulouh", ALTIVEC_BUILTIN_VMULOUH },
5193 { MASK_ALTIVEC, CODE_FOR_altivec_vmulosh, "__builtin_altivec_vmulosh", ALTIVEC_BUILTIN_VMULOSH },
5194 { MASK_ALTIVEC, CODE_FOR_altivec_vnor, "__builtin_altivec_vnor", ALTIVEC_BUILTIN_VNOR },
f18c054f 5195 { MASK_ALTIVEC, CODE_FOR_iorv4si3, "__builtin_altivec_vor", ALTIVEC_BUILTIN_VOR },
0ac081f6
AH
5196 { MASK_ALTIVEC, CODE_FOR_altivec_vpkuhum, "__builtin_altivec_vpkuhum", ALTIVEC_BUILTIN_VPKUHUM },
5197 { MASK_ALTIVEC, CODE_FOR_altivec_vpkuwum, "__builtin_altivec_vpkuwum", ALTIVEC_BUILTIN_VPKUWUM },
5198 { MASK_ALTIVEC, CODE_FOR_altivec_vpkpx, "__builtin_altivec_vpkpx", ALTIVEC_BUILTIN_VPKPX },
5199 { MASK_ALTIVEC, CODE_FOR_altivec_vpkuhss, "__builtin_altivec_vpkuhss", ALTIVEC_BUILTIN_VPKUHSS },
5200 { MASK_ALTIVEC, CODE_FOR_altivec_vpkshss, "__builtin_altivec_vpkshss", ALTIVEC_BUILTIN_VPKSHSS },
5201 { MASK_ALTIVEC, CODE_FOR_altivec_vpkuwss, "__builtin_altivec_vpkuwss", ALTIVEC_BUILTIN_VPKUWSS },
5202 { MASK_ALTIVEC, CODE_FOR_altivec_vpkswss, "__builtin_altivec_vpkswss", ALTIVEC_BUILTIN_VPKSWSS },
5203 { MASK_ALTIVEC, CODE_FOR_altivec_vpkuhus, "__builtin_altivec_vpkuhus", ALTIVEC_BUILTIN_VPKUHUS },
5204 { MASK_ALTIVEC, CODE_FOR_altivec_vpkshus, "__builtin_altivec_vpkshus", ALTIVEC_BUILTIN_VPKSHUS },
5205 { MASK_ALTIVEC, CODE_FOR_altivec_vpkuwus, "__builtin_altivec_vpkuwus", ALTIVEC_BUILTIN_VPKUWUS },
5206 { MASK_ALTIVEC, CODE_FOR_altivec_vpkswus, "__builtin_altivec_vpkswus", ALTIVEC_BUILTIN_VPKSWUS },
5207 { MASK_ALTIVEC, CODE_FOR_altivec_vrlb, "__builtin_altivec_vrlb", ALTIVEC_BUILTIN_VRLB },
5208 { MASK_ALTIVEC, CODE_FOR_altivec_vrlh, "__builtin_altivec_vrlh", ALTIVEC_BUILTIN_VRLH },
5209 { MASK_ALTIVEC, CODE_FOR_altivec_vrlw, "__builtin_altivec_vrlw", ALTIVEC_BUILTIN_VRLW },
5210 { MASK_ALTIVEC, CODE_FOR_altivec_vslb, "__builtin_altivec_vslb", ALTIVEC_BUILTIN_VSLB },
5211 { MASK_ALTIVEC, CODE_FOR_altivec_vslh, "__builtin_altivec_vslh", ALTIVEC_BUILTIN_VSLH },
5212 { MASK_ALTIVEC, CODE_FOR_altivec_vslw, "__builtin_altivec_vslw", ALTIVEC_BUILTIN_VSLW },
5213 { MASK_ALTIVEC, CODE_FOR_altivec_vsl, "__builtin_altivec_vsl", ALTIVEC_BUILTIN_VSL },
5214 { MASK_ALTIVEC, CODE_FOR_altivec_vslo, "__builtin_altivec_vslo", ALTIVEC_BUILTIN_VSLO },
2212663f
DB
5215 { MASK_ALTIVEC, CODE_FOR_altivec_vspltb, "__builtin_altivec_vspltb", ALTIVEC_BUILTIN_VSPLTB },
5216 { MASK_ALTIVEC, CODE_FOR_altivec_vsplth, "__builtin_altivec_vsplth", ALTIVEC_BUILTIN_VSPLTH },
5217 { MASK_ALTIVEC, CODE_FOR_altivec_vspltw, "__builtin_altivec_vspltw", ALTIVEC_BUILTIN_VSPLTW },
0ac081f6 5218 { MASK_ALTIVEC, CODE_FOR_altivec_vsrb, "__builtin_altivec_vsrb", ALTIVEC_BUILTIN_VSRB },
f18c054f
DB
5219 { MASK_ALTIVEC, CODE_FOR_altivec_vsrh, "__builtin_altivec_vsrh", ALTIVEC_BUILTIN_VSRH },
5220 { MASK_ALTIVEC, CODE_FOR_altivec_vsrw, "__builtin_altivec_vsrw", ALTIVEC_BUILTIN_VSRW },
0ac081f6
AH
5221 { MASK_ALTIVEC, CODE_FOR_altivec_vsrab, "__builtin_altivec_vsrab", ALTIVEC_BUILTIN_VSRAB },
5222 { MASK_ALTIVEC, CODE_FOR_altivec_vsrah, "__builtin_altivec_vsrah", ALTIVEC_BUILTIN_VSRAH },
5223 { MASK_ALTIVEC, CODE_FOR_altivec_vsraw, "__builtin_altivec_vsraw", ALTIVEC_BUILTIN_VSRAW },
5224 { MASK_ALTIVEC, CODE_FOR_altivec_vsr, "__builtin_altivec_vsr", ALTIVEC_BUILTIN_VSR },
5225 { MASK_ALTIVEC, CODE_FOR_altivec_vsro, "__builtin_altivec_vsro", ALTIVEC_BUILTIN_VSRO },
f18c054f
DB
5226 { MASK_ALTIVEC, CODE_FOR_subv16qi3, "__builtin_altivec_vsububm", ALTIVEC_BUILTIN_VSUBUBM },
5227 { MASK_ALTIVEC, CODE_FOR_subv8hi3, "__builtin_altivec_vsubuhm", ALTIVEC_BUILTIN_VSUBUHM },
5228 { MASK_ALTIVEC, CODE_FOR_subv4si3, "__builtin_altivec_vsubuwm", ALTIVEC_BUILTIN_VSUBUWM },
5229 { MASK_ALTIVEC, CODE_FOR_subv4sf3, "__builtin_altivec_vsubfp", ALTIVEC_BUILTIN_VSUBFP },
0ac081f6
AH
5230 { MASK_ALTIVEC, CODE_FOR_altivec_vsubcuw, "__builtin_altivec_vsubcuw", ALTIVEC_BUILTIN_VSUBCUW },
5231 { MASK_ALTIVEC, CODE_FOR_altivec_vsububs, "__builtin_altivec_vsububs", ALTIVEC_BUILTIN_VSUBUBS },
5232 { MASK_ALTIVEC, CODE_FOR_altivec_vsubsbs, "__builtin_altivec_vsubsbs", ALTIVEC_BUILTIN_VSUBSBS },
5233 { MASK_ALTIVEC, CODE_FOR_altivec_vsubuhs, "__builtin_altivec_vsubuhs", ALTIVEC_BUILTIN_VSUBUHS },
5234 { MASK_ALTIVEC, CODE_FOR_altivec_vsubshs, "__builtin_altivec_vsubshs", ALTIVEC_BUILTIN_VSUBSHS },
5235 { MASK_ALTIVEC, CODE_FOR_altivec_vsubuws, "__builtin_altivec_vsubuws", ALTIVEC_BUILTIN_VSUBUWS },
5236 { MASK_ALTIVEC, CODE_FOR_altivec_vsubsws, "__builtin_altivec_vsubsws", ALTIVEC_BUILTIN_VSUBSWS },
5237 { MASK_ALTIVEC, CODE_FOR_altivec_vsum4ubs, "__builtin_altivec_vsum4ubs", ALTIVEC_BUILTIN_VSUM4UBS },
5238 { MASK_ALTIVEC, CODE_FOR_altivec_vsum4sbs, "__builtin_altivec_vsum4sbs", ALTIVEC_BUILTIN_VSUM4SBS },
5239 { MASK_ALTIVEC, CODE_FOR_altivec_vsum4shs, "__builtin_altivec_vsum4shs", ALTIVEC_BUILTIN_VSUM4SHS },
5240 { MASK_ALTIVEC, CODE_FOR_altivec_vsum2sws, "__builtin_altivec_vsum2sws", ALTIVEC_BUILTIN_VSUM2SWS },
5241 { MASK_ALTIVEC, CODE_FOR_altivec_vsumsws, "__builtin_altivec_vsumsws", ALTIVEC_BUILTIN_VSUMSWS },
f18c054f 5242 { MASK_ALTIVEC, CODE_FOR_xorv4si3, "__builtin_altivec_vxor", ALTIVEC_BUILTIN_VXOR },
a3170dc6
AH
5243
5244 /* Place holder, leave as first spe builtin. */
5245 { 0, CODE_FOR_spe_evaddw, "__builtin_spe_evaddw", SPE_BUILTIN_EVADDW },
5246 { 0, CODE_FOR_spe_evand, "__builtin_spe_evand", SPE_BUILTIN_EVAND },
5247 { 0, CODE_FOR_spe_evandc, "__builtin_spe_evandc", SPE_BUILTIN_EVANDC },
5248 { 0, CODE_FOR_spe_evdivws, "__builtin_spe_evdivws", SPE_BUILTIN_EVDIVWS },
5249 { 0, CODE_FOR_spe_evdivwu, "__builtin_spe_evdivwu", SPE_BUILTIN_EVDIVWU },
5250 { 0, CODE_FOR_spe_eveqv, "__builtin_spe_eveqv", SPE_BUILTIN_EVEQV },
5251 { 0, CODE_FOR_spe_evfsadd, "__builtin_spe_evfsadd", SPE_BUILTIN_EVFSADD },
5252 { 0, CODE_FOR_spe_evfsdiv, "__builtin_spe_evfsdiv", SPE_BUILTIN_EVFSDIV },
5253 { 0, CODE_FOR_spe_evfsmul, "__builtin_spe_evfsmul", SPE_BUILTIN_EVFSMUL },
5254 { 0, CODE_FOR_spe_evfssub, "__builtin_spe_evfssub", SPE_BUILTIN_EVFSSUB },
5255 { 0, CODE_FOR_spe_evmergehi, "__builtin_spe_evmergehi", SPE_BUILTIN_EVMERGEHI },
5256 { 0, CODE_FOR_spe_evmergehilo, "__builtin_spe_evmergehilo", SPE_BUILTIN_EVMERGEHILO },
5257 { 0, CODE_FOR_spe_evmergelo, "__builtin_spe_evmergelo", SPE_BUILTIN_EVMERGELO },
5258 { 0, CODE_FOR_spe_evmergelohi, "__builtin_spe_evmergelohi", SPE_BUILTIN_EVMERGELOHI },
5259 { 0, CODE_FOR_spe_evmhegsmfaa, "__builtin_spe_evmhegsmfaa", SPE_BUILTIN_EVMHEGSMFAA },
5260 { 0, CODE_FOR_spe_evmhegsmfan, "__builtin_spe_evmhegsmfan", SPE_BUILTIN_EVMHEGSMFAN },
5261 { 0, CODE_FOR_spe_evmhegsmiaa, "__builtin_spe_evmhegsmiaa", SPE_BUILTIN_EVMHEGSMIAA },
5262 { 0, CODE_FOR_spe_evmhegsmian, "__builtin_spe_evmhegsmian", SPE_BUILTIN_EVMHEGSMIAN },
5263 { 0, CODE_FOR_spe_evmhegumiaa, "__builtin_spe_evmhegumiaa", SPE_BUILTIN_EVMHEGUMIAA },
5264 { 0, CODE_FOR_spe_evmhegumian, "__builtin_spe_evmhegumian", SPE_BUILTIN_EVMHEGUMIAN },
5265 { 0, CODE_FOR_spe_evmhesmf, "__builtin_spe_evmhesmf", SPE_BUILTIN_EVMHESMF },
5266 { 0, CODE_FOR_spe_evmhesmfa, "__builtin_spe_evmhesmfa", SPE_BUILTIN_EVMHESMFA },
5267 { 0, CODE_FOR_spe_evmhesmfaaw, "__builtin_spe_evmhesmfaaw", SPE_BUILTIN_EVMHESMFAAW },
5268 { 0, CODE_FOR_spe_evmhesmfanw, "__builtin_spe_evmhesmfanw", SPE_BUILTIN_EVMHESMFANW },
5269 { 0, CODE_FOR_spe_evmhesmi, "__builtin_spe_evmhesmi", SPE_BUILTIN_EVMHESMI },
5270 { 0, CODE_FOR_spe_evmhesmia, "__builtin_spe_evmhesmia", SPE_BUILTIN_EVMHESMIA },
5271 { 0, CODE_FOR_spe_evmhesmiaaw, "__builtin_spe_evmhesmiaaw", SPE_BUILTIN_EVMHESMIAAW },
5272 { 0, CODE_FOR_spe_evmhesmianw, "__builtin_spe_evmhesmianw", SPE_BUILTIN_EVMHESMIANW },
5273 { 0, CODE_FOR_spe_evmhessf, "__builtin_spe_evmhessf", SPE_BUILTIN_EVMHESSF },
5274 { 0, CODE_FOR_spe_evmhessfa, "__builtin_spe_evmhessfa", SPE_BUILTIN_EVMHESSFA },
5275 { 0, CODE_FOR_spe_evmhessfaaw, "__builtin_spe_evmhessfaaw", SPE_BUILTIN_EVMHESSFAAW },
5276 { 0, CODE_FOR_spe_evmhessfanw, "__builtin_spe_evmhessfanw", SPE_BUILTIN_EVMHESSFANW },
5277 { 0, CODE_FOR_spe_evmhessiaaw, "__builtin_spe_evmhessiaaw", SPE_BUILTIN_EVMHESSIAAW },
5278 { 0, CODE_FOR_spe_evmhessianw, "__builtin_spe_evmhessianw", SPE_BUILTIN_EVMHESSIANW },
5279 { 0, CODE_FOR_spe_evmheumi, "__builtin_spe_evmheumi", SPE_BUILTIN_EVMHEUMI },
5280 { 0, CODE_FOR_spe_evmheumia, "__builtin_spe_evmheumia", SPE_BUILTIN_EVMHEUMIA },
5281 { 0, CODE_FOR_spe_evmheumiaaw, "__builtin_spe_evmheumiaaw", SPE_BUILTIN_EVMHEUMIAAW },
5282 { 0, CODE_FOR_spe_evmheumianw, "__builtin_spe_evmheumianw", SPE_BUILTIN_EVMHEUMIANW },
5283 { 0, CODE_FOR_spe_evmheusiaaw, "__builtin_spe_evmheusiaaw", SPE_BUILTIN_EVMHEUSIAAW },
5284 { 0, CODE_FOR_spe_evmheusianw, "__builtin_spe_evmheusianw", SPE_BUILTIN_EVMHEUSIANW },
5285 { 0, CODE_FOR_spe_evmhogsmfaa, "__builtin_spe_evmhogsmfaa", SPE_BUILTIN_EVMHOGSMFAA },
5286 { 0, CODE_FOR_spe_evmhogsmfan, "__builtin_spe_evmhogsmfan", SPE_BUILTIN_EVMHOGSMFAN },
5287 { 0, CODE_FOR_spe_evmhogsmiaa, "__builtin_spe_evmhogsmiaa", SPE_BUILTIN_EVMHOGSMIAA },
5288 { 0, CODE_FOR_spe_evmhogsmian, "__builtin_spe_evmhogsmian", SPE_BUILTIN_EVMHOGSMIAN },
5289 { 0, CODE_FOR_spe_evmhogumiaa, "__builtin_spe_evmhogumiaa", SPE_BUILTIN_EVMHOGUMIAA },
5290 { 0, CODE_FOR_spe_evmhogumian, "__builtin_spe_evmhogumian", SPE_BUILTIN_EVMHOGUMIAN },
5291 { 0, CODE_FOR_spe_evmhosmf, "__builtin_spe_evmhosmf", SPE_BUILTIN_EVMHOSMF },
5292 { 0, CODE_FOR_spe_evmhosmfa, "__builtin_spe_evmhosmfa", SPE_BUILTIN_EVMHOSMFA },
5293 { 0, CODE_FOR_spe_evmhosmfaaw, "__builtin_spe_evmhosmfaaw", SPE_BUILTIN_EVMHOSMFAAW },
5294 { 0, CODE_FOR_spe_evmhosmfanw, "__builtin_spe_evmhosmfanw", SPE_BUILTIN_EVMHOSMFANW },
5295 { 0, CODE_FOR_spe_evmhosmi, "__builtin_spe_evmhosmi", SPE_BUILTIN_EVMHOSMI },
5296 { 0, CODE_FOR_spe_evmhosmia, "__builtin_spe_evmhosmia", SPE_BUILTIN_EVMHOSMIA },
5297 { 0, CODE_FOR_spe_evmhosmiaaw, "__builtin_spe_evmhosmiaaw", SPE_BUILTIN_EVMHOSMIAAW },
5298 { 0, CODE_FOR_spe_evmhosmianw, "__builtin_spe_evmhosmianw", SPE_BUILTIN_EVMHOSMIANW },
5299 { 0, CODE_FOR_spe_evmhossf, "__builtin_spe_evmhossf", SPE_BUILTIN_EVMHOSSF },
5300 { 0, CODE_FOR_spe_evmhossfa, "__builtin_spe_evmhossfa", SPE_BUILTIN_EVMHOSSFA },
5301 { 0, CODE_FOR_spe_evmhossfaaw, "__builtin_spe_evmhossfaaw", SPE_BUILTIN_EVMHOSSFAAW },
5302 { 0, CODE_FOR_spe_evmhossfanw, "__builtin_spe_evmhossfanw", SPE_BUILTIN_EVMHOSSFANW },
5303 { 0, CODE_FOR_spe_evmhossiaaw, "__builtin_spe_evmhossiaaw", SPE_BUILTIN_EVMHOSSIAAW },
5304 { 0, CODE_FOR_spe_evmhossianw, "__builtin_spe_evmhossianw", SPE_BUILTIN_EVMHOSSIANW },
5305 { 0, CODE_FOR_spe_evmhoumi, "__builtin_spe_evmhoumi", SPE_BUILTIN_EVMHOUMI },
5306 { 0, CODE_FOR_spe_evmhoumia, "__builtin_spe_evmhoumia", SPE_BUILTIN_EVMHOUMIA },
5307 { 0, CODE_FOR_spe_evmhoumiaaw, "__builtin_spe_evmhoumiaaw", SPE_BUILTIN_EVMHOUMIAAW },
5308 { 0, CODE_FOR_spe_evmhoumianw, "__builtin_spe_evmhoumianw", SPE_BUILTIN_EVMHOUMIANW },
5309 { 0, CODE_FOR_spe_evmhousiaaw, "__builtin_spe_evmhousiaaw", SPE_BUILTIN_EVMHOUSIAAW },
5310 { 0, CODE_FOR_spe_evmhousianw, "__builtin_spe_evmhousianw", SPE_BUILTIN_EVMHOUSIANW },
5311 { 0, CODE_FOR_spe_evmwhsmf, "__builtin_spe_evmwhsmf", SPE_BUILTIN_EVMWHSMF },
5312 { 0, CODE_FOR_spe_evmwhsmfa, "__builtin_spe_evmwhsmfa", SPE_BUILTIN_EVMWHSMFA },
5313 { 0, CODE_FOR_spe_evmwhsmi, "__builtin_spe_evmwhsmi", SPE_BUILTIN_EVMWHSMI },
5314 { 0, CODE_FOR_spe_evmwhsmia, "__builtin_spe_evmwhsmia", SPE_BUILTIN_EVMWHSMIA },
5315 { 0, CODE_FOR_spe_evmwhssf, "__builtin_spe_evmwhssf", SPE_BUILTIN_EVMWHSSF },
5316 { 0, CODE_FOR_spe_evmwhssfa, "__builtin_spe_evmwhssfa", SPE_BUILTIN_EVMWHSSFA },
5317 { 0, CODE_FOR_spe_evmwhumi, "__builtin_spe_evmwhumi", SPE_BUILTIN_EVMWHUMI },
5318 { 0, CODE_FOR_spe_evmwhumia, "__builtin_spe_evmwhumia", SPE_BUILTIN_EVMWHUMIA },
a3170dc6
AH
5319 { 0, CODE_FOR_spe_evmwlsmiaaw, "__builtin_spe_evmwlsmiaaw", SPE_BUILTIN_EVMWLSMIAAW },
5320 { 0, CODE_FOR_spe_evmwlsmianw, "__builtin_spe_evmwlsmianw", SPE_BUILTIN_EVMWLSMIANW },
a3170dc6
AH
5321 { 0, CODE_FOR_spe_evmwlssiaaw, "__builtin_spe_evmwlssiaaw", SPE_BUILTIN_EVMWLSSIAAW },
5322 { 0, CODE_FOR_spe_evmwlssianw, "__builtin_spe_evmwlssianw", SPE_BUILTIN_EVMWLSSIANW },
5323 { 0, CODE_FOR_spe_evmwlumi, "__builtin_spe_evmwlumi", SPE_BUILTIN_EVMWLUMI },
5324 { 0, CODE_FOR_spe_evmwlumia, "__builtin_spe_evmwlumia", SPE_BUILTIN_EVMWLUMIA },
5325 { 0, CODE_FOR_spe_evmwlumiaaw, "__builtin_spe_evmwlumiaaw", SPE_BUILTIN_EVMWLUMIAAW },
5326 { 0, CODE_FOR_spe_evmwlumianw, "__builtin_spe_evmwlumianw", SPE_BUILTIN_EVMWLUMIANW },
5327 { 0, CODE_FOR_spe_evmwlusiaaw, "__builtin_spe_evmwlusiaaw", SPE_BUILTIN_EVMWLUSIAAW },
5328 { 0, CODE_FOR_spe_evmwlusianw, "__builtin_spe_evmwlusianw", SPE_BUILTIN_EVMWLUSIANW },
5329 { 0, CODE_FOR_spe_evmwsmf, "__builtin_spe_evmwsmf", SPE_BUILTIN_EVMWSMF },
5330 { 0, CODE_FOR_spe_evmwsmfa, "__builtin_spe_evmwsmfa", SPE_BUILTIN_EVMWSMFA },
5331 { 0, CODE_FOR_spe_evmwsmfaa, "__builtin_spe_evmwsmfaa", SPE_BUILTIN_EVMWSMFAA },
5332 { 0, CODE_FOR_spe_evmwsmfan, "__builtin_spe_evmwsmfan", SPE_BUILTIN_EVMWSMFAN },
5333 { 0, CODE_FOR_spe_evmwsmi, "__builtin_spe_evmwsmi", SPE_BUILTIN_EVMWSMI },
5334 { 0, CODE_FOR_spe_evmwsmia, "__builtin_spe_evmwsmia", SPE_BUILTIN_EVMWSMIA },
5335 { 0, CODE_FOR_spe_evmwsmiaa, "__builtin_spe_evmwsmiaa", SPE_BUILTIN_EVMWSMIAA },
5336 { 0, CODE_FOR_spe_evmwsmian, "__builtin_spe_evmwsmian", SPE_BUILTIN_EVMWSMIAN },
5337 { 0, CODE_FOR_spe_evmwssf, "__builtin_spe_evmwssf", SPE_BUILTIN_EVMWSSF },
5338 { 0, CODE_FOR_spe_evmwssfa, "__builtin_spe_evmwssfa", SPE_BUILTIN_EVMWSSFA },
5339 { 0, CODE_FOR_spe_evmwssfaa, "__builtin_spe_evmwssfaa", SPE_BUILTIN_EVMWSSFAA },
5340 { 0, CODE_FOR_spe_evmwssfan, "__builtin_spe_evmwssfan", SPE_BUILTIN_EVMWSSFAN },
5341 { 0, CODE_FOR_spe_evmwumi, "__builtin_spe_evmwumi", SPE_BUILTIN_EVMWUMI },
5342 { 0, CODE_FOR_spe_evmwumia, "__builtin_spe_evmwumia", SPE_BUILTIN_EVMWUMIA },
5343 { 0, CODE_FOR_spe_evmwumiaa, "__builtin_spe_evmwumiaa", SPE_BUILTIN_EVMWUMIAA },
5344 { 0, CODE_FOR_spe_evmwumian, "__builtin_spe_evmwumian", SPE_BUILTIN_EVMWUMIAN },
5345 { 0, CODE_FOR_spe_evnand, "__builtin_spe_evnand", SPE_BUILTIN_EVNAND },
5346 { 0, CODE_FOR_spe_evnor, "__builtin_spe_evnor", SPE_BUILTIN_EVNOR },
5347 { 0, CODE_FOR_spe_evor, "__builtin_spe_evor", SPE_BUILTIN_EVOR },
5348 { 0, CODE_FOR_spe_evorc, "__builtin_spe_evorc", SPE_BUILTIN_EVORC },
5349 { 0, CODE_FOR_spe_evrlw, "__builtin_spe_evrlw", SPE_BUILTIN_EVRLW },
5350 { 0, CODE_FOR_spe_evslw, "__builtin_spe_evslw", SPE_BUILTIN_EVSLW },
5351 { 0, CODE_FOR_spe_evsrws, "__builtin_spe_evsrws", SPE_BUILTIN_EVSRWS },
5352 { 0, CODE_FOR_spe_evsrwu, "__builtin_spe_evsrwu", SPE_BUILTIN_EVSRWU },
5353 { 0, CODE_FOR_spe_evsubfw, "__builtin_spe_evsubfw", SPE_BUILTIN_EVSUBFW },
5354
5355 /* SPE binary operations expecting a 5-bit unsigned literal. */
5356 { 0, CODE_FOR_spe_evaddiw, "__builtin_spe_evaddiw", SPE_BUILTIN_EVADDIW },
5357
5358 { 0, CODE_FOR_spe_evrlwi, "__builtin_spe_evrlwi", SPE_BUILTIN_EVRLWI },
5359 { 0, CODE_FOR_spe_evslwi, "__builtin_spe_evslwi", SPE_BUILTIN_EVSLWI },
5360 { 0, CODE_FOR_spe_evsrwis, "__builtin_spe_evsrwis", SPE_BUILTIN_EVSRWIS },
5361 { 0, CODE_FOR_spe_evsrwiu, "__builtin_spe_evsrwiu", SPE_BUILTIN_EVSRWIU },
5362 { 0, CODE_FOR_spe_evsubifw, "__builtin_spe_evsubifw", SPE_BUILTIN_EVSUBIFW },
5363 { 0, CODE_FOR_spe_evmwhssfaa, "__builtin_spe_evmwhssfaa", SPE_BUILTIN_EVMWHSSFAA },
5364 { 0, CODE_FOR_spe_evmwhssmaa, "__builtin_spe_evmwhssmaa", SPE_BUILTIN_EVMWHSSMAA },
5365 { 0, CODE_FOR_spe_evmwhsmfaa, "__builtin_spe_evmwhsmfaa", SPE_BUILTIN_EVMWHSMFAA },
5366 { 0, CODE_FOR_spe_evmwhsmiaa, "__builtin_spe_evmwhsmiaa", SPE_BUILTIN_EVMWHSMIAA },
5367 { 0, CODE_FOR_spe_evmwhusiaa, "__builtin_spe_evmwhusiaa", SPE_BUILTIN_EVMWHUSIAA },
5368 { 0, CODE_FOR_spe_evmwhumiaa, "__builtin_spe_evmwhumiaa", SPE_BUILTIN_EVMWHUMIAA },
5369 { 0, CODE_FOR_spe_evmwhssfan, "__builtin_spe_evmwhssfan", SPE_BUILTIN_EVMWHSSFAN },
5370 { 0, CODE_FOR_spe_evmwhssian, "__builtin_spe_evmwhssian", SPE_BUILTIN_EVMWHSSIAN },
5371 { 0, CODE_FOR_spe_evmwhsmfan, "__builtin_spe_evmwhsmfan", SPE_BUILTIN_EVMWHSMFAN },
5372 { 0, CODE_FOR_spe_evmwhsmian, "__builtin_spe_evmwhsmian", SPE_BUILTIN_EVMWHSMIAN },
5373 { 0, CODE_FOR_spe_evmwhusian, "__builtin_spe_evmwhusian", SPE_BUILTIN_EVMWHUSIAN },
5374 { 0, CODE_FOR_spe_evmwhumian, "__builtin_spe_evmwhumian", SPE_BUILTIN_EVMWHUMIAN },
5375 { 0, CODE_FOR_spe_evmwhgssfaa, "__builtin_spe_evmwhgssfaa", SPE_BUILTIN_EVMWHGSSFAA },
5376 { 0, CODE_FOR_spe_evmwhgsmfaa, "__builtin_spe_evmwhgsmfaa", SPE_BUILTIN_EVMWHGSMFAA },
5377 { 0, CODE_FOR_spe_evmwhgsmiaa, "__builtin_spe_evmwhgsmiaa", SPE_BUILTIN_EVMWHGSMIAA },
5378 { 0, CODE_FOR_spe_evmwhgumiaa, "__builtin_spe_evmwhgumiaa", SPE_BUILTIN_EVMWHGUMIAA },
5379 { 0, CODE_FOR_spe_evmwhgssfan, "__builtin_spe_evmwhgssfan", SPE_BUILTIN_EVMWHGSSFAN },
5380 { 0, CODE_FOR_spe_evmwhgsmfan, "__builtin_spe_evmwhgsmfan", SPE_BUILTIN_EVMWHGSMFAN },
5381 { 0, CODE_FOR_spe_evmwhgsmian, "__builtin_spe_evmwhgsmian", SPE_BUILTIN_EVMWHGSMIAN },
5382 { 0, CODE_FOR_spe_evmwhgumian, "__builtin_spe_evmwhgumian", SPE_BUILTIN_EVMWHGUMIAN },
5383 { 0, CODE_FOR_spe_brinc, "__builtin_spe_brinc", SPE_BUILTIN_BRINC },
5384
5385 /* Place-holder. Leave as last binary SPE builtin. */
17edbda5 5386 { 0, CODE_FOR_xorv2si3, "__builtin_spe_evxor", SPE_BUILTIN_EVXOR },
ae4b4a02
AH
5387};
5388
5389/* AltiVec predicates. */
5390
5391struct builtin_description_predicates
5392{
5393 const unsigned int mask;
5394 const enum insn_code icode;
5395 const char *opcode;
5396 const char *const name;
5397 const enum rs6000_builtins code;
5398};
5399
5400static const struct builtin_description_predicates bdesc_altivec_preds[] =
5401{
5402 { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v4sf, "*vcmpbfp.", "__builtin_altivec_vcmpbfp_p", ALTIVEC_BUILTIN_VCMPBFP_P },
5403 { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v4sf, "*vcmpeqfp.", "__builtin_altivec_vcmpeqfp_p", ALTIVEC_BUILTIN_VCMPEQFP_P },
5404 { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v4sf, "*vcmpgefp.", "__builtin_altivec_vcmpgefp_p", ALTIVEC_BUILTIN_VCMPGEFP_P },
5405 { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v4sf, "*vcmpgtfp.", "__builtin_altivec_vcmpgtfp_p", ALTIVEC_BUILTIN_VCMPGTFP_P },
5406 { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v4si, "*vcmpequw.", "__builtin_altivec_vcmpequw_p", ALTIVEC_BUILTIN_VCMPEQUW_P },
5407 { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v4si, "*vcmpgtsw.", "__builtin_altivec_vcmpgtsw_p", ALTIVEC_BUILTIN_VCMPGTSW_P },
5408 { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v4si, "*vcmpgtuw.", "__builtin_altivec_vcmpgtuw_p", ALTIVEC_BUILTIN_VCMPGTUW_P },
5409 { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v8hi, "*vcmpgtuh.", "__builtin_altivec_vcmpgtuh_p", ALTIVEC_BUILTIN_VCMPGTUH_P },
5410 { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v8hi, "*vcmpgtsh.", "__builtin_altivec_vcmpgtsh_p", ALTIVEC_BUILTIN_VCMPGTSH_P },
5411 { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v8hi, "*vcmpequh.", "__builtin_altivec_vcmpequh_p", ALTIVEC_BUILTIN_VCMPEQUH_P },
5412 { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v16qi, "*vcmpequb.", "__builtin_altivec_vcmpequb_p", ALTIVEC_BUILTIN_VCMPEQUB_P },
5413 { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v16qi, "*vcmpgtsb.", "__builtin_altivec_vcmpgtsb_p", ALTIVEC_BUILTIN_VCMPGTSB_P },
5414 { MASK_ALTIVEC, CODE_FOR_altivec_predicate_v16qi, "*vcmpgtub.", "__builtin_altivec_vcmpgtub_p", ALTIVEC_BUILTIN_VCMPGTUB_P }
0ac081f6 5415};
24408032 5416
a3170dc6
AH
5417/* SPE predicates. */
5418static struct builtin_description bdesc_spe_predicates[] =
5419{
5420 /* Place-holder. Leave as first. */
5421 { 0, CODE_FOR_spe_evcmpeq, "__builtin_spe_evcmpeq", SPE_BUILTIN_EVCMPEQ },
5422 { 0, CODE_FOR_spe_evcmpgts, "__builtin_spe_evcmpgts", SPE_BUILTIN_EVCMPGTS },
5423 { 0, CODE_FOR_spe_evcmpgtu, "__builtin_spe_evcmpgtu", SPE_BUILTIN_EVCMPGTU },
5424 { 0, CODE_FOR_spe_evcmplts, "__builtin_spe_evcmplts", SPE_BUILTIN_EVCMPLTS },
5425 { 0, CODE_FOR_spe_evcmpltu, "__builtin_spe_evcmpltu", SPE_BUILTIN_EVCMPLTU },
5426 { 0, CODE_FOR_spe_evfscmpeq, "__builtin_spe_evfscmpeq", SPE_BUILTIN_EVFSCMPEQ },
5427 { 0, CODE_FOR_spe_evfscmpgt, "__builtin_spe_evfscmpgt", SPE_BUILTIN_EVFSCMPGT },
5428 { 0, CODE_FOR_spe_evfscmplt, "__builtin_spe_evfscmplt", SPE_BUILTIN_EVFSCMPLT },
5429 { 0, CODE_FOR_spe_evfststeq, "__builtin_spe_evfststeq", SPE_BUILTIN_EVFSTSTEQ },
5430 { 0, CODE_FOR_spe_evfststgt, "__builtin_spe_evfststgt", SPE_BUILTIN_EVFSTSTGT },
5431 /* Place-holder. Leave as last. */
5432 { 0, CODE_FOR_spe_evfststlt, "__builtin_spe_evfststlt", SPE_BUILTIN_EVFSTSTLT },
5433};
5434
5435/* SPE evsel predicates. */
5436static struct builtin_description bdesc_spe_evsel[] =
5437{
5438 /* Place-holder. Leave as first. */
5439 { 0, CODE_FOR_spe_evcmpgts, "__builtin_spe_evsel_gts", SPE_BUILTIN_EVSEL_CMPGTS },
5440 { 0, CODE_FOR_spe_evcmpgtu, "__builtin_spe_evsel_gtu", SPE_BUILTIN_EVSEL_CMPGTU },
5441 { 0, CODE_FOR_spe_evcmplts, "__builtin_spe_evsel_lts", SPE_BUILTIN_EVSEL_CMPLTS },
5442 { 0, CODE_FOR_spe_evcmpltu, "__builtin_spe_evsel_ltu", SPE_BUILTIN_EVSEL_CMPLTU },
5443 { 0, CODE_FOR_spe_evcmpeq, "__builtin_spe_evsel_eq", SPE_BUILTIN_EVSEL_CMPEQ },
5444 { 0, CODE_FOR_spe_evfscmpgt, "__builtin_spe_evsel_fsgt", SPE_BUILTIN_EVSEL_FSCMPGT },
5445 { 0, CODE_FOR_spe_evfscmplt, "__builtin_spe_evsel_fslt", SPE_BUILTIN_EVSEL_FSCMPLT },
5446 { 0, CODE_FOR_spe_evfscmpeq, "__builtin_spe_evsel_fseq", SPE_BUILTIN_EVSEL_FSCMPEQ },
5447 { 0, CODE_FOR_spe_evfststgt, "__builtin_spe_evsel_fststgt", SPE_BUILTIN_EVSEL_FSTSTGT },
5448 { 0, CODE_FOR_spe_evfststlt, "__builtin_spe_evsel_fststlt", SPE_BUILTIN_EVSEL_FSTSTLT },
5449 /* Place-holder. Leave as last. */
5450 { 0, CODE_FOR_spe_evfststeq, "__builtin_spe_evsel_fststeq", SPE_BUILTIN_EVSEL_FSTSTEQ },
5451};
5452
b6d08ca1 5453/* ABS* operations. */
100c4561
AH
5454
5455static const struct builtin_description bdesc_abs[] =
5456{
5457 { MASK_ALTIVEC, CODE_FOR_absv4si2, "__builtin_altivec_abs_v4si", ALTIVEC_BUILTIN_ABS_V4SI },
5458 { MASK_ALTIVEC, CODE_FOR_absv8hi2, "__builtin_altivec_abs_v8hi", ALTIVEC_BUILTIN_ABS_V8HI },
5459 { MASK_ALTIVEC, CODE_FOR_absv4sf2, "__builtin_altivec_abs_v4sf", ALTIVEC_BUILTIN_ABS_V4SF },
5460 { MASK_ALTIVEC, CODE_FOR_absv16qi2, "__builtin_altivec_abs_v16qi", ALTIVEC_BUILTIN_ABS_V16QI },
5461 { MASK_ALTIVEC, CODE_FOR_altivec_abss_v4si, "__builtin_altivec_abss_v4si", ALTIVEC_BUILTIN_ABSS_V4SI },
5462 { MASK_ALTIVEC, CODE_FOR_altivec_abss_v8hi, "__builtin_altivec_abss_v8hi", ALTIVEC_BUILTIN_ABSS_V8HI },
5463 { MASK_ALTIVEC, CODE_FOR_altivec_abss_v16qi, "__builtin_altivec_abss_v16qi", ALTIVEC_BUILTIN_ABSS_V16QI }
5464};
5465
617e0e1d
DB
5466/* Simple unary operations: VECb = foo (unsigned literal) or VECb =
5467 foo (VECa). */
24408032 5468
a3170dc6 5469static struct builtin_description bdesc_1arg[] =
2212663f 5470{
617e0e1d
DB
5471 { MASK_ALTIVEC, CODE_FOR_altivec_vexptefp, "__builtin_altivec_vexptefp", ALTIVEC_BUILTIN_VEXPTEFP },
5472 { MASK_ALTIVEC, CODE_FOR_altivec_vlogefp, "__builtin_altivec_vlogefp", ALTIVEC_BUILTIN_VLOGEFP },
5473 { MASK_ALTIVEC, CODE_FOR_altivec_vrefp, "__builtin_altivec_vrefp", ALTIVEC_BUILTIN_VREFP },
5474 { MASK_ALTIVEC, CODE_FOR_altivec_vrfim, "__builtin_altivec_vrfim", ALTIVEC_BUILTIN_VRFIM },
5475 { MASK_ALTIVEC, CODE_FOR_altivec_vrfin, "__builtin_altivec_vrfin", ALTIVEC_BUILTIN_VRFIN },
5476 { MASK_ALTIVEC, CODE_FOR_altivec_vrfip, "__builtin_altivec_vrfip", ALTIVEC_BUILTIN_VRFIP },
5477 { MASK_ALTIVEC, CODE_FOR_ftruncv4sf2, "__builtin_altivec_vrfiz", ALTIVEC_BUILTIN_VRFIZ },
5478 { MASK_ALTIVEC, CODE_FOR_altivec_vrsqrtefp, "__builtin_altivec_vrsqrtefp", ALTIVEC_BUILTIN_VRSQRTEFP },
2212663f
DB
5479 { MASK_ALTIVEC, CODE_FOR_altivec_vspltisb, "__builtin_altivec_vspltisb", ALTIVEC_BUILTIN_VSPLTISB },
5480 { MASK_ALTIVEC, CODE_FOR_altivec_vspltish, "__builtin_altivec_vspltish", ALTIVEC_BUILTIN_VSPLTISH },
5481 { MASK_ALTIVEC, CODE_FOR_altivec_vspltisw, "__builtin_altivec_vspltisw", ALTIVEC_BUILTIN_VSPLTISW },
20e26713
AH
5482 { MASK_ALTIVEC, CODE_FOR_altivec_vupkhsb, "__builtin_altivec_vupkhsb", ALTIVEC_BUILTIN_VUPKHSB },
5483 { MASK_ALTIVEC, CODE_FOR_altivec_vupkhpx, "__builtin_altivec_vupkhpx", ALTIVEC_BUILTIN_VUPKHPX },
5484 { MASK_ALTIVEC, CODE_FOR_altivec_vupkhsh, "__builtin_altivec_vupkhsh", ALTIVEC_BUILTIN_VUPKHSH },
5485 { MASK_ALTIVEC, CODE_FOR_altivec_vupklsb, "__builtin_altivec_vupklsb", ALTIVEC_BUILTIN_VUPKLSB },
5486 { MASK_ALTIVEC, CODE_FOR_altivec_vupklpx, "__builtin_altivec_vupklpx", ALTIVEC_BUILTIN_VUPKLPX },
5487 { MASK_ALTIVEC, CODE_FOR_altivec_vupklsh, "__builtin_altivec_vupklsh", ALTIVEC_BUILTIN_VUPKLSH },
a3170dc6
AH
5488
5489 /* The SPE unary builtins must start with SPE_BUILTIN_EVABS and
5490 end with SPE_BUILTIN_EVSUBFUSIAAW. */
5491 { 0, CODE_FOR_spe_evabs, "__builtin_spe_evabs", SPE_BUILTIN_EVABS },
5492 { 0, CODE_FOR_spe_evaddsmiaaw, "__builtin_spe_evaddsmiaaw", SPE_BUILTIN_EVADDSMIAAW },
5493 { 0, CODE_FOR_spe_evaddssiaaw, "__builtin_spe_evaddssiaaw", SPE_BUILTIN_EVADDSSIAAW },
5494 { 0, CODE_FOR_spe_evaddumiaaw, "__builtin_spe_evaddumiaaw", SPE_BUILTIN_EVADDUMIAAW },
5495 { 0, CODE_FOR_spe_evaddusiaaw, "__builtin_spe_evaddusiaaw", SPE_BUILTIN_EVADDUSIAAW },
5496 { 0, CODE_FOR_spe_evcntlsw, "__builtin_spe_evcntlsw", SPE_BUILTIN_EVCNTLSW },
5497 { 0, CODE_FOR_spe_evcntlzw, "__builtin_spe_evcntlzw", SPE_BUILTIN_EVCNTLZW },
5498 { 0, CODE_FOR_spe_evextsb, "__builtin_spe_evextsb", SPE_BUILTIN_EVEXTSB },
5499 { 0, CODE_FOR_spe_evextsh, "__builtin_spe_evextsh", SPE_BUILTIN_EVEXTSH },
5500 { 0, CODE_FOR_spe_evfsabs, "__builtin_spe_evfsabs", SPE_BUILTIN_EVFSABS },
5501 { 0, CODE_FOR_spe_evfscfsf, "__builtin_spe_evfscfsf", SPE_BUILTIN_EVFSCFSF },
5502 { 0, CODE_FOR_spe_evfscfsi, "__builtin_spe_evfscfsi", SPE_BUILTIN_EVFSCFSI },
5503 { 0, CODE_FOR_spe_evfscfuf, "__builtin_spe_evfscfuf", SPE_BUILTIN_EVFSCFUF },
5504 { 0, CODE_FOR_spe_evfscfui, "__builtin_spe_evfscfui", SPE_BUILTIN_EVFSCFUI },
5505 { 0, CODE_FOR_spe_evfsctsf, "__builtin_spe_evfsctsf", SPE_BUILTIN_EVFSCTSF },
5506 { 0, CODE_FOR_spe_evfsctsi, "__builtin_spe_evfsctsi", SPE_BUILTIN_EVFSCTSI },
5507 { 0, CODE_FOR_spe_evfsctsiz, "__builtin_spe_evfsctsiz", SPE_BUILTIN_EVFSCTSIZ },
5508 { 0, CODE_FOR_spe_evfsctuf, "__builtin_spe_evfsctuf", SPE_BUILTIN_EVFSCTUF },
5509 { 0, CODE_FOR_spe_evfsctui, "__builtin_spe_evfsctui", SPE_BUILTIN_EVFSCTUI },
5510 { 0, CODE_FOR_spe_evfsctuiz, "__builtin_spe_evfsctuiz", SPE_BUILTIN_EVFSCTUIZ },
5511 { 0, CODE_FOR_spe_evfsnabs, "__builtin_spe_evfsnabs", SPE_BUILTIN_EVFSNABS },
5512 { 0, CODE_FOR_spe_evfsneg, "__builtin_spe_evfsneg", SPE_BUILTIN_EVFSNEG },
5513 { 0, CODE_FOR_spe_evmra, "__builtin_spe_evmra", SPE_BUILTIN_EVMRA },
5514 { 0, CODE_FOR_spe_evneg, "__builtin_spe_evneg", SPE_BUILTIN_EVNEG },
5515 { 0, CODE_FOR_spe_evrndw, "__builtin_spe_evrndw", SPE_BUILTIN_EVRNDW },
5516 { 0, CODE_FOR_spe_evsubfsmiaaw, "__builtin_spe_evsubfsmiaaw", SPE_BUILTIN_EVSUBFSMIAAW },
5517 { 0, CODE_FOR_spe_evsubfssiaaw, "__builtin_spe_evsubfssiaaw", SPE_BUILTIN_EVSUBFSSIAAW },
5518 { 0, CODE_FOR_spe_evsubfumiaaw, "__builtin_spe_evsubfumiaaw", SPE_BUILTIN_EVSUBFUMIAAW },
a3170dc6
AH
5519
5520 /* Place-holder. Leave as last unary SPE builtin. */
5521 { 0, CODE_FOR_spe_evsubfusiaaw, "__builtin_spe_evsubfusiaaw", SPE_BUILTIN_EVSUBFUSIAAW },
2212663f
DB
5522};
5523
5524static rtx
a2369ed3 5525rs6000_expand_unop_builtin (enum insn_code icode, tree arglist, rtx target)
2212663f
DB
5526{
5527 rtx pat;
5528 tree arg0 = TREE_VALUE (arglist);
5529 rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
5530 enum machine_mode tmode = insn_data[icode].operand[0].mode;
5531 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
5532
0559cc77
DE
5533 if (icode == CODE_FOR_nothing)
5534 /* Builtin not supported on this processor. */
5535 return 0;
5536
20e26713
AH
5537 /* If we got invalid arguments bail out before generating bad rtl. */
5538 if (arg0 == error_mark_node)
9a171fcd 5539 return const0_rtx;
20e26713 5540
0559cc77
DE
5541 if (icode == CODE_FOR_altivec_vspltisb
5542 || icode == CODE_FOR_altivec_vspltish
5543 || icode == CODE_FOR_altivec_vspltisw
5544 || icode == CODE_FOR_spe_evsplatfi
5545 || icode == CODE_FOR_spe_evsplati)
b44140e7
AH
5546 {
5547 /* Only allow 5-bit *signed* literals. */
b44140e7
AH
5548 if (GET_CODE (op0) != CONST_INT
5549 || INTVAL (op0) > 0x1f
5550 || INTVAL (op0) < -0x1f)
5551 {
5552 error ("argument 1 must be a 5-bit signed literal");
9a171fcd 5553 return const0_rtx;
b44140e7 5554 }
b44140e7
AH
5555 }
5556
c62f2db5 5557 if (target == 0
2212663f
DB
5558 || GET_MODE (target) != tmode
5559 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
5560 target = gen_reg_rtx (tmode);
5561
5562 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
5563 op0 = copy_to_mode_reg (mode0, op0);
5564
5565 pat = GEN_FCN (icode) (target, op0);
5566 if (! pat)
5567 return 0;
5568 emit_insn (pat);
0ac081f6 5569
2212663f
DB
5570 return target;
5571}
ae4b4a02 5572
100c4561 5573static rtx
a2369ed3 5574altivec_expand_abs_builtin (enum insn_code icode, tree arglist, rtx target)
100c4561
AH
5575{
5576 rtx pat, scratch1, scratch2;
5577 tree arg0 = TREE_VALUE (arglist);
5578 rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
5579 enum machine_mode tmode = insn_data[icode].operand[0].mode;
5580 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
5581
5582 /* If we have invalid arguments, bail out before generating bad rtl. */
5583 if (arg0 == error_mark_node)
9a171fcd 5584 return const0_rtx;
100c4561
AH
5585
5586 if (target == 0
5587 || GET_MODE (target) != tmode
5588 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
5589 target = gen_reg_rtx (tmode);
5590
5591 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
5592 op0 = copy_to_mode_reg (mode0, op0);
5593
5594 scratch1 = gen_reg_rtx (mode0);
5595 scratch2 = gen_reg_rtx (mode0);
5596
5597 pat = GEN_FCN (icode) (target, op0, scratch1, scratch2);
5598 if (! pat)
5599 return 0;
5600 emit_insn (pat);
5601
5602 return target;
5603}
5604
0ac081f6 5605static rtx
a2369ed3 5606rs6000_expand_binop_builtin (enum insn_code icode, tree arglist, rtx target)
0ac081f6
AH
5607{
5608 rtx pat;
5609 tree arg0 = TREE_VALUE (arglist);
5610 tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
5611 rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
5612 rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
5613 enum machine_mode tmode = insn_data[icode].operand[0].mode;
5614 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
5615 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
5616
0559cc77
DE
5617 if (icode == CODE_FOR_nothing)
5618 /* Builtin not supported on this processor. */
5619 return 0;
5620
20e26713
AH
5621 /* If we got invalid arguments bail out before generating bad rtl. */
5622 if (arg0 == error_mark_node || arg1 == error_mark_node)
9a171fcd 5623 return const0_rtx;
20e26713 5624
0559cc77
DE
5625 if (icode == CODE_FOR_altivec_vcfux
5626 || icode == CODE_FOR_altivec_vcfsx
5627 || icode == CODE_FOR_altivec_vctsxs
5628 || icode == CODE_FOR_altivec_vctuxs
5629 || icode == CODE_FOR_altivec_vspltb
5630 || icode == CODE_FOR_altivec_vsplth
5631 || icode == CODE_FOR_altivec_vspltw
5632 || icode == CODE_FOR_spe_evaddiw
5633 || icode == CODE_FOR_spe_evldd
5634 || icode == CODE_FOR_spe_evldh
5635 || icode == CODE_FOR_spe_evldw
5636 || icode == CODE_FOR_spe_evlhhesplat
5637 || icode == CODE_FOR_spe_evlhhossplat
5638 || icode == CODE_FOR_spe_evlhhousplat
5639 || icode == CODE_FOR_spe_evlwhe
5640 || icode == CODE_FOR_spe_evlwhos
5641 || icode == CODE_FOR_spe_evlwhou
5642 || icode == CODE_FOR_spe_evlwhsplat
5643 || icode == CODE_FOR_spe_evlwwsplat
5644 || icode == CODE_FOR_spe_evrlwi
5645 || icode == CODE_FOR_spe_evslwi
5646 || icode == CODE_FOR_spe_evsrwis
f5119d10 5647 || icode == CODE_FOR_spe_evsubifw
0559cc77 5648 || icode == CODE_FOR_spe_evsrwiu)
b44140e7
AH
5649 {
5650 /* Only allow 5-bit unsigned literals. */
8bb418a3 5651 STRIP_NOPS (arg1);
b44140e7
AH
5652 if (TREE_CODE (arg1) != INTEGER_CST
5653 || TREE_INT_CST_LOW (arg1) & ~0x1f)
5654 {
5655 error ("argument 2 must be a 5-bit unsigned literal");
9a171fcd 5656 return const0_rtx;
b44140e7 5657 }
b44140e7
AH
5658 }
5659
c62f2db5 5660 if (target == 0
0ac081f6
AH
5661 || GET_MODE (target) != tmode
5662 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
5663 target = gen_reg_rtx (tmode);
5664
5665 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
5666 op0 = copy_to_mode_reg (mode0, op0);
5667 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
5668 op1 = copy_to_mode_reg (mode1, op1);
5669
5670 pat = GEN_FCN (icode) (target, op0, op1);
5671 if (! pat)
5672 return 0;
5673 emit_insn (pat);
5674
5675 return target;
5676}
6525c0e7 5677
ae4b4a02 5678static rtx
a2369ed3
DJ
5679altivec_expand_predicate_builtin (enum insn_code icode, const char *opcode,
5680 tree arglist, rtx target)
ae4b4a02
AH
5681{
5682 rtx pat, scratch;
5683 tree cr6_form = TREE_VALUE (arglist);
5684 tree arg0 = TREE_VALUE (TREE_CHAIN (arglist));
5685 tree arg1 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
5686 rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
5687 rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
5688 enum machine_mode tmode = SImode;
5689 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
5690 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
5691 int cr6_form_int;
5692
5693 if (TREE_CODE (cr6_form) != INTEGER_CST)
5694 {
5695 error ("argument 1 of __builtin_altivec_predicate must be a constant");
9a171fcd 5696 return const0_rtx;
ae4b4a02
AH
5697 }
5698 else
5699 cr6_form_int = TREE_INT_CST_LOW (cr6_form);
5700
5701 if (mode0 != mode1)
5702 abort ();
5703
5704 /* If we have invalid arguments, bail out before generating bad rtl. */
5705 if (arg0 == error_mark_node || arg1 == error_mark_node)
9a171fcd 5706 return const0_rtx;
ae4b4a02
AH
5707
5708 if (target == 0
5709 || GET_MODE (target) != tmode
5710 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
5711 target = gen_reg_rtx (tmode);
5712
5713 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
5714 op0 = copy_to_mode_reg (mode0, op0);
5715 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
5716 op1 = copy_to_mode_reg (mode1, op1);
5717
5718 scratch = gen_reg_rtx (mode0);
5719
5720 pat = GEN_FCN (icode) (scratch, op0, op1,
f1c25d3b 5721 gen_rtx_SYMBOL_REF (Pmode, opcode));
ae4b4a02
AH
5722 if (! pat)
5723 return 0;
5724 emit_insn (pat);
5725
5726 /* The vec_any* and vec_all* predicates use the same opcodes for two
5727 different operations, but the bits in CR6 will be different
5728 depending on what information we want. So we have to play tricks
5729 with CR6 to get the right bits out.
5730
5731 If you think this is disgusting, look at the specs for the
5732 AltiVec predicates. */
5733
5734 switch (cr6_form_int)
5735 {
5736 case 0:
5737 emit_insn (gen_cr6_test_for_zero (target));
5738 break;
5739 case 1:
5740 emit_insn (gen_cr6_test_for_zero_reverse (target));
5741 break;
5742 case 2:
5743 emit_insn (gen_cr6_test_for_lt (target));
5744 break;
5745 case 3:
5746 emit_insn (gen_cr6_test_for_lt_reverse (target));
5747 break;
5748 default:
5749 error ("argument 1 of __builtin_altivec_predicate is out of range");
5750 break;
5751 }
5752
5753 return target;
5754}
5755
b4a62fa0 5756static rtx
38f391a5 5757altivec_expand_lv_builtin (enum insn_code icode, tree arglist, rtx target)
b4a62fa0
SB
5758{
5759 rtx pat, addr;
5760 tree arg0 = TREE_VALUE (arglist);
5761 tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
5762 enum machine_mode tmode = insn_data[icode].operand[0].mode;
5763 enum machine_mode mode0 = Pmode;
5764 enum machine_mode mode1 = Pmode;
5765 rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
5766 rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
5767
5768 if (icode == CODE_FOR_nothing)
5769 /* Builtin not supported on this processor. */
5770 return 0;
5771
5772 /* If we got invalid arguments bail out before generating bad rtl. */
5773 if (arg0 == error_mark_node || arg1 == error_mark_node)
5774 return const0_rtx;
5775
5776 if (target == 0
5777 || GET_MODE (target) != tmode
5778 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
5779 target = gen_reg_rtx (tmode);
5780
5781 op1 = copy_to_mode_reg (mode1, op1);
5782
5783 if (op0 == const0_rtx)
5784 {
5785 addr = gen_rtx_MEM (tmode, op1);
5786 }
5787 else
5788 {
5789 op0 = copy_to_mode_reg (mode0, op0);
5790 addr = gen_rtx_MEM (tmode, gen_rtx_PLUS (Pmode, op0, op1));
5791 }
5792
5793 pat = GEN_FCN (icode) (target, addr);
5794
5795 if (! pat)
5796 return 0;
5797 emit_insn (pat);
5798
5799 return target;
5800}
5801
6525c0e7 5802static rtx
a2369ed3 5803altivec_expand_stv_builtin (enum insn_code icode, tree arglist)
6525c0e7
AH
5804{
5805 tree arg0 = TREE_VALUE (arglist);
5806 tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
5807 tree arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
5808 rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
5809 rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
5810 rtx op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
b4a62fa0
SB
5811 rtx pat, addr;
5812 enum machine_mode tmode = insn_data[icode].operand[0].mode;
5813 enum machine_mode mode1 = Pmode;
5814 enum machine_mode mode2 = Pmode;
6525c0e7
AH
5815
5816 /* Invalid arguments. Bail before doing anything stoopid! */
5817 if (arg0 == error_mark_node
5818 || arg1 == error_mark_node
5819 || arg2 == error_mark_node)
9a171fcd 5820 return const0_rtx;
6525c0e7 5821
b4a62fa0
SB
5822 if (! (*insn_data[icode].operand[1].predicate) (op0, tmode))
5823 op0 = copy_to_mode_reg (tmode, op0);
5824
5825 op2 = copy_to_mode_reg (mode2, op2);
5826
5827 if (op1 == const0_rtx)
5828 {
5829 addr = gen_rtx_MEM (tmode, op2);
5830 }
5831 else
5832 {
5833 op1 = copy_to_mode_reg (mode1, op1);
5834 addr = gen_rtx_MEM (tmode, gen_rtx_PLUS (Pmode, op1, op2));
5835 }
6525c0e7 5836
b4a62fa0 5837 pat = GEN_FCN (icode) (addr, op0);
6525c0e7
AH
5838 if (pat)
5839 emit_insn (pat);
5840 return NULL_RTX;
5841}
5842
2212663f 5843static rtx
a2369ed3 5844rs6000_expand_ternop_builtin (enum insn_code icode, tree arglist, rtx target)
2212663f
DB
5845{
5846 rtx pat;
5847 tree arg0 = TREE_VALUE (arglist);
5848 tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
5849 tree arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
5850 rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
5851 rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
5852 rtx op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
5853 enum machine_mode tmode = insn_data[icode].operand[0].mode;
5854 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
5855 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
5856 enum machine_mode mode2 = insn_data[icode].operand[3].mode;
0ac081f6 5857
774b5662
DE
5858 if (icode == CODE_FOR_nothing)
5859 /* Builtin not supported on this processor. */
5860 return 0;
5861
20e26713
AH
5862 /* If we got invalid arguments bail out before generating bad rtl. */
5863 if (arg0 == error_mark_node
5864 || arg1 == error_mark_node
5865 || arg2 == error_mark_node)
9a171fcd 5866 return const0_rtx;
20e26713 5867
774b5662
DE
5868 if (icode == CODE_FOR_altivec_vsldoi_4sf
5869 || icode == CODE_FOR_altivec_vsldoi_4si
5870 || icode == CODE_FOR_altivec_vsldoi_8hi
5871 || icode == CODE_FOR_altivec_vsldoi_16qi)
b44140e7
AH
5872 {
5873 /* Only allow 4-bit unsigned literals. */
8bb418a3 5874 STRIP_NOPS (arg2);
b44140e7
AH
5875 if (TREE_CODE (arg2) != INTEGER_CST
5876 || TREE_INT_CST_LOW (arg2) & ~0xf)
5877 {
5878 error ("argument 3 must be a 4-bit unsigned literal");
e3277ffb 5879 return const0_rtx;
b44140e7 5880 }
b44140e7
AH
5881 }
5882
c62f2db5 5883 if (target == 0
2212663f
DB
5884 || GET_MODE (target) != tmode
5885 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
5886 target = gen_reg_rtx (tmode);
5887
5888 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
5889 op0 = copy_to_mode_reg (mode0, op0);
5890 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
5891 op1 = copy_to_mode_reg (mode1, op1);
5892 if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
5893 op2 = copy_to_mode_reg (mode2, op2);
5894
5895 pat = GEN_FCN (icode) (target, op0, op1, op2);
5896 if (! pat)
5897 return 0;
5898 emit_insn (pat);
5899
5900 return target;
5901}
92898235 5902
3a9b8c7e 5903/* Expand the lvx builtins. */
0ac081f6 5904static rtx
a2369ed3 5905altivec_expand_ld_builtin (tree exp, rtx target, bool *expandedp)
0ac081f6 5906{
0ac081f6
AH
5907 tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
5908 tree arglist = TREE_OPERAND (exp, 1);
0ac081f6 5909 unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
3a9b8c7e
AH
5910 tree arg0;
5911 enum machine_mode tmode, mode0;
7c3abc73 5912 rtx pat, op0;
3a9b8c7e 5913 enum insn_code icode;
92898235 5914
0ac081f6
AH
5915 switch (fcode)
5916 {
f18c054f
DB
5917 case ALTIVEC_BUILTIN_LD_INTERNAL_16qi:
5918 icode = CODE_FOR_altivec_lvx_16qi;
3a9b8c7e 5919 break;
f18c054f
DB
5920 case ALTIVEC_BUILTIN_LD_INTERNAL_8hi:
5921 icode = CODE_FOR_altivec_lvx_8hi;
3a9b8c7e
AH
5922 break;
5923 case ALTIVEC_BUILTIN_LD_INTERNAL_4si:
5924 icode = CODE_FOR_altivec_lvx_4si;
5925 break;
5926 case ALTIVEC_BUILTIN_LD_INTERNAL_4sf:
5927 icode = CODE_FOR_altivec_lvx_4sf;
5928 break;
5929 default:
5930 *expandedp = false;
5931 return NULL_RTX;
5932 }
0ac081f6 5933
3a9b8c7e 5934 *expandedp = true;
f18c054f 5935
3a9b8c7e
AH
5936 arg0 = TREE_VALUE (arglist);
5937 op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
5938 tmode = insn_data[icode].operand[0].mode;
5939 mode0 = insn_data[icode].operand[1].mode;
f18c054f 5940
3a9b8c7e
AH
5941 if (target == 0
5942 || GET_MODE (target) != tmode
5943 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
5944 target = gen_reg_rtx (tmode);
24408032 5945
3a9b8c7e
AH
5946 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
5947 op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
f18c054f 5948
3a9b8c7e
AH
5949 pat = GEN_FCN (icode) (target, op0);
5950 if (! pat)
5951 return 0;
5952 emit_insn (pat);
5953 return target;
5954}
f18c054f 5955
3a9b8c7e
AH
5956/* Expand the stvx builtins. */
5957static rtx
a2369ed3
DJ
5958altivec_expand_st_builtin (tree exp, rtx target ATTRIBUTE_UNUSED,
5959 bool *expandedp)
3a9b8c7e
AH
5960{
5961 tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
5962 tree arglist = TREE_OPERAND (exp, 1);
5963 unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
5964 tree arg0, arg1;
5965 enum machine_mode mode0, mode1;
7c3abc73 5966 rtx pat, op0, op1;
3a9b8c7e 5967 enum insn_code icode;
f18c054f 5968
3a9b8c7e
AH
5969 switch (fcode)
5970 {
5971 case ALTIVEC_BUILTIN_ST_INTERNAL_16qi:
5972 icode = CODE_FOR_altivec_stvx_16qi;
5973 break;
5974 case ALTIVEC_BUILTIN_ST_INTERNAL_8hi:
5975 icode = CODE_FOR_altivec_stvx_8hi;
5976 break;
5977 case ALTIVEC_BUILTIN_ST_INTERNAL_4si:
5978 icode = CODE_FOR_altivec_stvx_4si;
5979 break;
5980 case ALTIVEC_BUILTIN_ST_INTERNAL_4sf:
5981 icode = CODE_FOR_altivec_stvx_4sf;
5982 break;
5983 default:
5984 *expandedp = false;
5985 return NULL_RTX;
5986 }
24408032 5987
3a9b8c7e
AH
5988 arg0 = TREE_VALUE (arglist);
5989 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
5990 op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
5991 op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
5992 mode0 = insn_data[icode].operand[0].mode;
5993 mode1 = insn_data[icode].operand[1].mode;
f18c054f 5994
3a9b8c7e
AH
5995 if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
5996 op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
5997 if (! (*insn_data[icode].operand[1].predicate) (op1, mode1))
5998 op1 = copy_to_mode_reg (mode1, op1);
f18c054f 5999
3a9b8c7e
AH
6000 pat = GEN_FCN (icode) (op0, op1);
6001 if (pat)
6002 emit_insn (pat);
f18c054f 6003
3a9b8c7e
AH
6004 *expandedp = true;
6005 return NULL_RTX;
6006}
f18c054f 6007
3a9b8c7e
AH
6008/* Expand the dst builtins. */
6009static rtx
a2369ed3
DJ
6010altivec_expand_dst_builtin (tree exp, rtx target ATTRIBUTE_UNUSED,
6011 bool *expandedp)
3a9b8c7e
AH
6012{
6013 tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
6014 tree arglist = TREE_OPERAND (exp, 1);
6015 unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
6016 tree arg0, arg1, arg2;
6017 enum machine_mode mode0, mode1, mode2;
7c3abc73 6018 rtx pat, op0, op1, op2;
3a9b8c7e 6019 struct builtin_description *d;
a3170dc6 6020 size_t i;
f18c054f 6021
3a9b8c7e 6022 *expandedp = false;
f18c054f 6023
3a9b8c7e
AH
6024 /* Handle DST variants. */
6025 d = (struct builtin_description *) bdesc_dst;
6026 for (i = 0; i < ARRAY_SIZE (bdesc_dst); i++, d++)
6027 if (d->code == fcode)
6028 {
6029 arg0 = TREE_VALUE (arglist);
6030 arg1 = TREE_VALUE (TREE_CHAIN (arglist));
6031 arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
6032 op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
6033 op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
6034 op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
6035 mode0 = insn_data[d->icode].operand[0].mode;
6036 mode1 = insn_data[d->icode].operand[1].mode;
6037 mode2 = insn_data[d->icode].operand[2].mode;
24408032 6038
3a9b8c7e
AH
6039 /* Invalid arguments, bail out before generating bad rtl. */
6040 if (arg0 == error_mark_node
6041 || arg1 == error_mark_node
6042 || arg2 == error_mark_node)
6043 return const0_rtx;
f18c054f 6044
86e7df90 6045 *expandedp = true;
8bb418a3 6046 STRIP_NOPS (arg2);
3a9b8c7e
AH
6047 if (TREE_CODE (arg2) != INTEGER_CST
6048 || TREE_INT_CST_LOW (arg2) & ~0x3)
6049 {
6050 error ("argument to `%s' must be a 2-bit unsigned literal", d->name);
6051 return const0_rtx;
6052 }
f18c054f 6053
3a9b8c7e 6054 if (! (*insn_data[d->icode].operand[0].predicate) (op0, mode0))
b4a62fa0 6055 op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
3a9b8c7e
AH
6056 if (! (*insn_data[d->icode].operand[1].predicate) (op1, mode1))
6057 op1 = copy_to_mode_reg (mode1, op1);
24408032 6058
3a9b8c7e
AH
6059 pat = GEN_FCN (d->icode) (op0, op1, op2);
6060 if (pat != 0)
6061 emit_insn (pat);
f18c054f 6062
3a9b8c7e
AH
6063 return NULL_RTX;
6064 }
f18c054f 6065
3a9b8c7e
AH
6066 return NULL_RTX;
6067}
24408032 6068
3a9b8c7e
AH
6069/* Expand the builtin in EXP and store the result in TARGET. Store
6070 true in *EXPANDEDP if we found a builtin to expand. */
6071static rtx
a2369ed3 6072altivec_expand_builtin (tree exp, rtx target, bool *expandedp)
3a9b8c7e
AH
6073{
6074 struct builtin_description *d;
6075 struct builtin_description_predicates *dp;
6076 size_t i;
6077 enum insn_code icode;
6078 tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
6079 tree arglist = TREE_OPERAND (exp, 1);
7c3abc73
AH
6080 tree arg0;
6081 rtx op0, pat;
6082 enum machine_mode tmode, mode0;
3a9b8c7e 6083 unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
0ac081f6 6084
3a9b8c7e
AH
6085 target = altivec_expand_ld_builtin (exp, target, expandedp);
6086 if (*expandedp)
6087 return target;
0ac081f6 6088
3a9b8c7e
AH
6089 target = altivec_expand_st_builtin (exp, target, expandedp);
6090 if (*expandedp)
6091 return target;
6092
6093 target = altivec_expand_dst_builtin (exp, target, expandedp);
6094 if (*expandedp)
6095 return target;
6096
6097 *expandedp = true;
95385cbb 6098
3a9b8c7e
AH
6099 switch (fcode)
6100 {
6525c0e7
AH
6101 case ALTIVEC_BUILTIN_STVX:
6102 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvx, arglist);
6103 case ALTIVEC_BUILTIN_STVEBX:
6104 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvebx, arglist);
6105 case ALTIVEC_BUILTIN_STVEHX:
6106 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvehx, arglist);
6107 case ALTIVEC_BUILTIN_STVEWX:
6108 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvewx, arglist);
6109 case ALTIVEC_BUILTIN_STVXL:
6110 return altivec_expand_stv_builtin (CODE_FOR_altivec_stvxl, arglist);
3a9b8c7e 6111
95385cbb
AH
6112 case ALTIVEC_BUILTIN_MFVSCR:
6113 icode = CODE_FOR_altivec_mfvscr;
6114 tmode = insn_data[icode].operand[0].mode;
6115
6116 if (target == 0
6117 || GET_MODE (target) != tmode
6118 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
6119 target = gen_reg_rtx (tmode);
6120
6121 pat = GEN_FCN (icode) (target);
0ac081f6
AH
6122 if (! pat)
6123 return 0;
6124 emit_insn (pat);
95385cbb
AH
6125 return target;
6126
6127 case ALTIVEC_BUILTIN_MTVSCR:
6128 icode = CODE_FOR_altivec_mtvscr;
6129 arg0 = TREE_VALUE (arglist);
6130 op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
6131 mode0 = insn_data[icode].operand[0].mode;
6132
6133 /* If we got invalid arguments bail out before generating bad rtl. */
6134 if (arg0 == error_mark_node)
9a171fcd 6135 return const0_rtx;
95385cbb
AH
6136
6137 if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
6138 op0 = copy_to_mode_reg (mode0, op0);
6139
6140 pat = GEN_FCN (icode) (op0);
6141 if (pat)
6142 emit_insn (pat);
6143 return NULL_RTX;
3a9b8c7e 6144
95385cbb
AH
6145 case ALTIVEC_BUILTIN_DSSALL:
6146 emit_insn (gen_altivec_dssall ());
6147 return NULL_RTX;
6148
6149 case ALTIVEC_BUILTIN_DSS:
6150 icode = CODE_FOR_altivec_dss;
6151 arg0 = TREE_VALUE (arglist);
8bb418a3 6152 STRIP_NOPS (arg0);
95385cbb
AH
6153 op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
6154 mode0 = insn_data[icode].operand[0].mode;
6155
6156 /* If we got invalid arguments bail out before generating bad rtl. */
6157 if (arg0 == error_mark_node)
9a171fcd 6158 return const0_rtx;
95385cbb 6159
b44140e7
AH
6160 if (TREE_CODE (arg0) != INTEGER_CST
6161 || TREE_INT_CST_LOW (arg0) & ~0x3)
6162 {
6163 error ("argument to dss must be a 2-bit unsigned literal");
9a171fcd 6164 return const0_rtx;
b44140e7
AH
6165 }
6166
95385cbb
AH
6167 if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
6168 op0 = copy_to_mode_reg (mode0, op0);
6169
6170 emit_insn (gen_altivec_dss (op0));
0ac081f6 6171 return NULL_RTX;
8bb418a3
ZL
6172
6173 case ALTIVEC_BUILTIN_COMPILETIME_ERROR:
6174 arg0 = TREE_VALUE (arglist);
6175 while (TREE_CODE (arg0) == NOP_EXPR || TREE_CODE (arg0) == ADDR_EXPR)
6176 arg0 = TREE_OPERAND (arg0, 0);
6177 error ("invalid parameter combination for `%s' AltiVec intrinsic",
6178 TREE_STRING_POINTER (arg0));
6179
6180 return const0_rtx;
0ac081f6 6181 }
24408032 6182
100c4561
AH
6183 /* Expand abs* operations. */
6184 d = (struct builtin_description *) bdesc_abs;
ca7558fc 6185 for (i = 0; i < ARRAY_SIZE (bdesc_abs); i++, d++)
100c4561
AH
6186 if (d->code == fcode)
6187 return altivec_expand_abs_builtin (d->icode, arglist, target);
6188
ae4b4a02
AH
6189 /* Expand the AltiVec predicates. */
6190 dp = (struct builtin_description_predicates *) bdesc_altivec_preds;
ca7558fc 6191 for (i = 0; i < ARRAY_SIZE (bdesc_altivec_preds); i++, dp++)
ae4b4a02
AH
6192 if (dp->code == fcode)
6193 return altivec_expand_predicate_builtin (dp->icode, dp->opcode, arglist, target);
6194
6525c0e7
AH
6195 /* LV* are funky. We initialized them differently. */
6196 switch (fcode)
6197 {
6198 case ALTIVEC_BUILTIN_LVSL:
b4a62fa0 6199 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvsl,
6525c0e7
AH
6200 arglist, target);
6201 case ALTIVEC_BUILTIN_LVSR:
b4a62fa0 6202 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvsr,
92898235 6203 arglist, target);
6525c0e7 6204 case ALTIVEC_BUILTIN_LVEBX:
b4a62fa0 6205 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvebx,
92898235 6206 arglist, target);
6525c0e7 6207 case ALTIVEC_BUILTIN_LVEHX:
b4a62fa0 6208 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvehx,
92898235 6209 arglist, target);
6525c0e7 6210 case ALTIVEC_BUILTIN_LVEWX:
b4a62fa0 6211 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvewx,
92898235 6212 arglist, target);
6525c0e7 6213 case ALTIVEC_BUILTIN_LVXL:
b4a62fa0 6214 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvxl,
92898235 6215 arglist, target);
6525c0e7 6216 case ALTIVEC_BUILTIN_LVX:
b4a62fa0 6217 return altivec_expand_lv_builtin (CODE_FOR_altivec_lvx,
92898235 6218 arglist, target);
6525c0e7
AH
6219 default:
6220 break;
6221 /* Fall through. */
6222 }
95385cbb 6223
92898235 6224 *expandedp = false;
0ac081f6
AH
6225 return NULL_RTX;
6226}
6227
a3170dc6
AH
6228/* Binops that need to be initialized manually, but can be expanded
6229 automagically by rs6000_expand_binop_builtin. */
6230static struct builtin_description bdesc_2arg_spe[] =
6231{
6232 { 0, CODE_FOR_spe_evlddx, "__builtin_spe_evlddx", SPE_BUILTIN_EVLDDX },
6233 { 0, CODE_FOR_spe_evldwx, "__builtin_spe_evldwx", SPE_BUILTIN_EVLDWX },
6234 { 0, CODE_FOR_spe_evldhx, "__builtin_spe_evldhx", SPE_BUILTIN_EVLDHX },
6235 { 0, CODE_FOR_spe_evlwhex, "__builtin_spe_evlwhex", SPE_BUILTIN_EVLWHEX },
6236 { 0, CODE_FOR_spe_evlwhoux, "__builtin_spe_evlwhoux", SPE_BUILTIN_EVLWHOUX },
6237 { 0, CODE_FOR_spe_evlwhosx, "__builtin_spe_evlwhosx", SPE_BUILTIN_EVLWHOSX },
6238 { 0, CODE_FOR_spe_evlwwsplatx, "__builtin_spe_evlwwsplatx", SPE_BUILTIN_EVLWWSPLATX },
6239 { 0, CODE_FOR_spe_evlwhsplatx, "__builtin_spe_evlwhsplatx", SPE_BUILTIN_EVLWHSPLATX },
6240 { 0, CODE_FOR_spe_evlhhesplatx, "__builtin_spe_evlhhesplatx", SPE_BUILTIN_EVLHHESPLATX },
6241 { 0, CODE_FOR_spe_evlhhousplatx, "__builtin_spe_evlhhousplatx", SPE_BUILTIN_EVLHHOUSPLATX },
6242 { 0, CODE_FOR_spe_evlhhossplatx, "__builtin_spe_evlhhossplatx", SPE_BUILTIN_EVLHHOSSPLATX },
6243 { 0, CODE_FOR_spe_evldd, "__builtin_spe_evldd", SPE_BUILTIN_EVLDD },
6244 { 0, CODE_FOR_spe_evldw, "__builtin_spe_evldw", SPE_BUILTIN_EVLDW },
6245 { 0, CODE_FOR_spe_evldh, "__builtin_spe_evldh", SPE_BUILTIN_EVLDH },
6246 { 0, CODE_FOR_spe_evlwhe, "__builtin_spe_evlwhe", SPE_BUILTIN_EVLWHE },
6247 { 0, CODE_FOR_spe_evlwhou, "__builtin_spe_evlwhou", SPE_BUILTIN_EVLWHOU },
6248 { 0, CODE_FOR_spe_evlwhos, "__builtin_spe_evlwhos", SPE_BUILTIN_EVLWHOS },
6249 { 0, CODE_FOR_spe_evlwwsplat, "__builtin_spe_evlwwsplat", SPE_BUILTIN_EVLWWSPLAT },
6250 { 0, CODE_FOR_spe_evlwhsplat, "__builtin_spe_evlwhsplat", SPE_BUILTIN_EVLWHSPLAT },
6251 { 0, CODE_FOR_spe_evlhhesplat, "__builtin_spe_evlhhesplat", SPE_BUILTIN_EVLHHESPLAT },
6252 { 0, CODE_FOR_spe_evlhhousplat, "__builtin_spe_evlhhousplat", SPE_BUILTIN_EVLHHOUSPLAT },
6253 { 0, CODE_FOR_spe_evlhhossplat, "__builtin_spe_evlhhossplat", SPE_BUILTIN_EVLHHOSSPLAT }
6254};
6255
6256/* Expand the builtin in EXP and store the result in TARGET. Store
6257 true in *EXPANDEDP if we found a builtin to expand.
6258
6259 This expands the SPE builtins that are not simple unary and binary
6260 operations. */
6261static rtx
a2369ed3 6262spe_expand_builtin (tree exp, rtx target, bool *expandedp)
a3170dc6
AH
6263{
6264 tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
6265 tree arglist = TREE_OPERAND (exp, 1);
6266 tree arg1, arg0;
6267 unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
6268 enum insn_code icode;
6269 enum machine_mode tmode, mode0;
6270 rtx pat, op0;
6271 struct builtin_description *d;
6272 size_t i;
6273
6274 *expandedp = true;
6275
6276 /* Syntax check for a 5-bit unsigned immediate. */
6277 switch (fcode)
6278 {
6279 case SPE_BUILTIN_EVSTDD:
6280 case SPE_BUILTIN_EVSTDH:
6281 case SPE_BUILTIN_EVSTDW:
6282 case SPE_BUILTIN_EVSTWHE:
6283 case SPE_BUILTIN_EVSTWHO:
6284 case SPE_BUILTIN_EVSTWWE:
6285 case SPE_BUILTIN_EVSTWWO:
6286 arg1 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
6287 if (TREE_CODE (arg1) != INTEGER_CST
6288 || TREE_INT_CST_LOW (arg1) & ~0x1f)
6289 {
6290 error ("argument 2 must be a 5-bit unsigned literal");
6291 return const0_rtx;
6292 }
6293 break;
6294 default:
6295 break;
6296 }
6297
00332c9f
AH
6298 /* The evsplat*i instructions are not quite generic. */
6299 switch (fcode)
6300 {
6301 case SPE_BUILTIN_EVSPLATFI:
6302 return rs6000_expand_unop_builtin (CODE_FOR_spe_evsplatfi,
6303 arglist, target);
6304 case SPE_BUILTIN_EVSPLATI:
6305 return rs6000_expand_unop_builtin (CODE_FOR_spe_evsplati,
6306 arglist, target);
6307 default:
6308 break;
6309 }
6310
a3170dc6
AH
6311 d = (struct builtin_description *) bdesc_2arg_spe;
6312 for (i = 0; i < ARRAY_SIZE (bdesc_2arg_spe); ++i, ++d)
6313 if (d->code == fcode)
6314 return rs6000_expand_binop_builtin (d->icode, arglist, target);
6315
6316 d = (struct builtin_description *) bdesc_spe_predicates;
6317 for (i = 0; i < ARRAY_SIZE (bdesc_spe_predicates); ++i, ++d)
6318 if (d->code == fcode)
6319 return spe_expand_predicate_builtin (d->icode, arglist, target);
6320
6321 d = (struct builtin_description *) bdesc_spe_evsel;
6322 for (i = 0; i < ARRAY_SIZE (bdesc_spe_evsel); ++i, ++d)
6323 if (d->code == fcode)
6324 return spe_expand_evsel_builtin (d->icode, arglist, target);
6325
6326 switch (fcode)
6327 {
6328 case SPE_BUILTIN_EVSTDDX:
6329 return altivec_expand_stv_builtin (CODE_FOR_spe_evstddx, arglist);
6330 case SPE_BUILTIN_EVSTDHX:
6331 return altivec_expand_stv_builtin (CODE_FOR_spe_evstdhx, arglist);
6332 case SPE_BUILTIN_EVSTDWX:
6333 return altivec_expand_stv_builtin (CODE_FOR_spe_evstdwx, arglist);
6334 case SPE_BUILTIN_EVSTWHEX:
6335 return altivec_expand_stv_builtin (CODE_FOR_spe_evstwhex, arglist);
6336 case SPE_BUILTIN_EVSTWHOX:
6337 return altivec_expand_stv_builtin (CODE_FOR_spe_evstwhox, arglist);
6338 case SPE_BUILTIN_EVSTWWEX:
6339 return altivec_expand_stv_builtin (CODE_FOR_spe_evstwwex, arglist);
6340 case SPE_BUILTIN_EVSTWWOX:
6341 return altivec_expand_stv_builtin (CODE_FOR_spe_evstwwox, arglist);
6342 case SPE_BUILTIN_EVSTDD:
6343 return altivec_expand_stv_builtin (CODE_FOR_spe_evstdd, arglist);
6344 case SPE_BUILTIN_EVSTDH:
6345 return altivec_expand_stv_builtin (CODE_FOR_spe_evstdh, arglist);
6346 case SPE_BUILTIN_EVSTDW:
6347 return altivec_expand_stv_builtin (CODE_FOR_spe_evstdw, arglist);
6348 case SPE_BUILTIN_EVSTWHE:
6349 return altivec_expand_stv_builtin (CODE_FOR_spe_evstwhe, arglist);
6350 case SPE_BUILTIN_EVSTWHO:
6351 return altivec_expand_stv_builtin (CODE_FOR_spe_evstwho, arglist);
6352 case SPE_BUILTIN_EVSTWWE:
6353 return altivec_expand_stv_builtin (CODE_FOR_spe_evstwwe, arglist);
6354 case SPE_BUILTIN_EVSTWWO:
6355 return altivec_expand_stv_builtin (CODE_FOR_spe_evstwwo, arglist);
6356 case SPE_BUILTIN_MFSPEFSCR:
6357 icode = CODE_FOR_spe_mfspefscr;
6358 tmode = insn_data[icode].operand[0].mode;
6359
6360 if (target == 0
6361 || GET_MODE (target) != tmode
6362 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
6363 target = gen_reg_rtx (tmode);
6364
6365 pat = GEN_FCN (icode) (target);
6366 if (! pat)
6367 return 0;
6368 emit_insn (pat);
6369 return target;
6370 case SPE_BUILTIN_MTSPEFSCR:
6371 icode = CODE_FOR_spe_mtspefscr;
6372 arg0 = TREE_VALUE (arglist);
6373 op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
6374 mode0 = insn_data[icode].operand[0].mode;
6375
6376 if (arg0 == error_mark_node)
6377 return const0_rtx;
6378
6379 if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
6380 op0 = copy_to_mode_reg (mode0, op0);
6381
6382 pat = GEN_FCN (icode) (op0);
6383 if (pat)
6384 emit_insn (pat);
6385 return NULL_RTX;
6386 default:
6387 break;
6388 }
6389
6390 *expandedp = false;
6391 return NULL_RTX;
6392}
6393
6394static rtx
a2369ed3 6395spe_expand_predicate_builtin (enum insn_code icode, tree arglist, rtx target)
a3170dc6
AH
6396{
6397 rtx pat, scratch, tmp;
6398 tree form = TREE_VALUE (arglist);
6399 tree arg0 = TREE_VALUE (TREE_CHAIN (arglist));
6400 tree arg1 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
6401 rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
6402 rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
6403 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
6404 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
6405 int form_int;
6406 enum rtx_code code;
6407
6408 if (TREE_CODE (form) != INTEGER_CST)
6409 {
6410 error ("argument 1 of __builtin_spe_predicate must be a constant");
6411 return const0_rtx;
6412 }
6413 else
6414 form_int = TREE_INT_CST_LOW (form);
6415
6416 if (mode0 != mode1)
6417 abort ();
6418
6419 if (arg0 == error_mark_node || arg1 == error_mark_node)
6420 return const0_rtx;
6421
6422 if (target == 0
6423 || GET_MODE (target) != SImode
6424 || ! (*insn_data[icode].operand[0].predicate) (target, SImode))
6425 target = gen_reg_rtx (SImode);
6426
6427 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
6428 op0 = copy_to_mode_reg (mode0, op0);
6429 if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
6430 op1 = copy_to_mode_reg (mode1, op1);
6431
6432 scratch = gen_reg_rtx (CCmode);
6433
6434 pat = GEN_FCN (icode) (scratch, op0, op1);
6435 if (! pat)
6436 return const0_rtx;
6437 emit_insn (pat);
6438
6439 /* There are 4 variants for each predicate: _any_, _all_, _upper_,
6440 _lower_. We use one compare, but look in different bits of the
6441 CR for each variant.
6442
6443 There are 2 elements in each SPE simd type (upper/lower). The CR
6444 bits are set as follows:
6445
6446 BIT0 | BIT 1 | BIT 2 | BIT 3
6447 U | L | (U | L) | (U & L)
6448
6449 So, for an "all" relationship, BIT 3 would be set.
6450 For an "any" relationship, BIT 2 would be set. Etc.
6451
6452 Following traditional nomenclature, these bits map to:
6453
6454 BIT0 | BIT 1 | BIT 2 | BIT 3
6455 LT | GT | EQ | OV
6456
6457 Later, we will generate rtl to look in the LT/EQ/EQ/OV bits.
6458 */
6459
6460 switch (form_int)
6461 {
6462 /* All variant. OV bit. */
6463 case 0:
6464 /* We need to get to the OV bit, which is the ORDERED bit. We
6465 could generate (ordered:SI (reg:CC xx) (const_int 0)), but
6466 that's ugly and will trigger a validate_condition_mode abort.
6467 So let's just use another pattern. */
6468 emit_insn (gen_move_from_CR_ov_bit (target, scratch));
6469 return target;
6470 /* Any variant. EQ bit. */
6471 case 1:
6472 code = EQ;
6473 break;
6474 /* Upper variant. LT bit. */
6475 case 2:
6476 code = LT;
6477 break;
6478 /* Lower variant. GT bit. */
6479 case 3:
6480 code = GT;
6481 break;
6482 default:
6483 error ("argument 1 of __builtin_spe_predicate is out of range");
6484 return const0_rtx;
6485 }
6486
6487 tmp = gen_rtx_fmt_ee (code, SImode, scratch, const0_rtx);
6488 emit_move_insn (target, tmp);
6489
6490 return target;
6491}
6492
6493/* The evsel builtins look like this:
6494
6495 e = __builtin_spe_evsel_OP (a, b, c, d);
6496
6497 and work like this:
6498
6499 e[upper] = a[upper] *OP* b[upper] ? c[upper] : d[upper];
6500 e[lower] = a[lower] *OP* b[lower] ? c[lower] : d[lower];
6501*/
6502
6503static rtx
a2369ed3 6504spe_expand_evsel_builtin (enum insn_code icode, tree arglist, rtx target)
a3170dc6
AH
6505{
6506 rtx pat, scratch;
6507 tree arg0 = TREE_VALUE (arglist);
6508 tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
6509 tree arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
6510 tree arg3 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (arglist))));
6511 rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
6512 rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
6513 rtx op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
6514 rtx op3 = expand_expr (arg3, NULL_RTX, VOIDmode, 0);
6515 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
6516 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
6517
6518 if (mode0 != mode1)
6519 abort ();
6520
6521 if (arg0 == error_mark_node || arg1 == error_mark_node
6522 || arg2 == error_mark_node || arg3 == error_mark_node)
6523 return const0_rtx;
6524
6525 if (target == 0
6526 || GET_MODE (target) != mode0
6527 || ! (*insn_data[icode].operand[0].predicate) (target, mode0))
6528 target = gen_reg_rtx (mode0);
6529
6530 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
6531 op0 = copy_to_mode_reg (mode0, op0);
6532 if (! (*insn_data[icode].operand[1].predicate) (op1, mode1))
6533 op1 = copy_to_mode_reg (mode0, op1);
6534 if (! (*insn_data[icode].operand[1].predicate) (op2, mode1))
6535 op2 = copy_to_mode_reg (mode0, op2);
6536 if (! (*insn_data[icode].operand[1].predicate) (op3, mode1))
6537 op3 = copy_to_mode_reg (mode0, op3);
6538
6539 /* Generate the compare. */
6540 scratch = gen_reg_rtx (CCmode);
6541 pat = GEN_FCN (icode) (scratch, op0, op1);
6542 if (! pat)
6543 return const0_rtx;
6544 emit_insn (pat);
6545
6546 if (mode0 == V2SImode)
6547 emit_insn (gen_spe_evsel (target, op2, op3, scratch));
6548 else
6549 emit_insn (gen_spe_evsel_fs (target, op2, op3, scratch));
6550
6551 return target;
6552}
6553
0ac081f6
AH
6554/* Expand an expression EXP that calls a built-in function,
6555 with result going to TARGET if that's convenient
6556 (and in mode MODE if that's convenient).
6557 SUBTARGET may be used as the target for computing one of EXP's operands.
6558 IGNORE is nonzero if the value is to be ignored. */
6559
6560static rtx
a2369ed3
DJ
6561rs6000_expand_builtin (tree exp, rtx target, rtx subtarget ATTRIBUTE_UNUSED,
6562 enum machine_mode mode ATTRIBUTE_UNUSED,
6563 int ignore ATTRIBUTE_UNUSED)
0ac081f6 6564{
92898235
AH
6565 tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
6566 tree arglist = TREE_OPERAND (exp, 1);
6567 unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
6568 struct builtin_description *d;
6569 size_t i;
6570 rtx ret;
6571 bool success;
6572
0ac081f6 6573 if (TARGET_ALTIVEC)
92898235
AH
6574 {
6575 ret = altivec_expand_builtin (exp, target, &success);
6576
a3170dc6
AH
6577 if (success)
6578 return ret;
6579 }
6580 if (TARGET_SPE)
6581 {
6582 ret = spe_expand_builtin (exp, target, &success);
6583
92898235
AH
6584 if (success)
6585 return ret;
6586 }
6587
0559cc77
DE
6588 if (TARGET_ALTIVEC || TARGET_SPE)
6589 {
6590 /* Handle simple unary operations. */
6591 d = (struct builtin_description *) bdesc_1arg;
6592 for (i = 0; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
6593 if (d->code == fcode)
6594 return rs6000_expand_unop_builtin (d->icode, arglist, target);
6595
6596 /* Handle simple binary operations. */
6597 d = (struct builtin_description *) bdesc_2arg;
6598 for (i = 0; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
6599 if (d->code == fcode)
6600 return rs6000_expand_binop_builtin (d->icode, arglist, target);
6601
6602 /* Handle simple ternary operations. */
6603 d = (struct builtin_description *) bdesc_3arg;
6604 for (i = 0; i < ARRAY_SIZE (bdesc_3arg); i++, d++)
6605 if (d->code == fcode)
6606 return rs6000_expand_ternop_builtin (d->icode, arglist, target);
6607 }
0ac081f6
AH
6608
6609 abort ();
92898235 6610 return NULL_RTX;
0ac081f6
AH
6611}
6612
6613static void
863d938c 6614rs6000_init_builtins (void)
0ac081f6 6615{
4a5eab38
PB
6616 V2SI_type_node = build_vector_type (intSI_type_node, 2);
6617 V2SF_type_node = build_vector_type (float_type_node, 2);
6618 V4HI_type_node = build_vector_type (intHI_type_node, 4);
6619 V4SI_type_node = build_vector_type (intSI_type_node, 4);
6620 V4SF_type_node = build_vector_type (float_type_node, 4);
7e463bda 6621 V8HI_type_node = build_vector_type (intHI_type_node, 8);
4a5eab38
PB
6622 V16QI_type_node = build_vector_type (intQI_type_node, 16);
6623
6624 unsigned_V16QI_type_node = build_vector_type (unsigned_intQI_type_node, 16);
6625 unsigned_V8HI_type_node = build_vector_type (unsigned_intHI_type_node, 8);
6626 unsigned_V4SI_type_node = build_vector_type (unsigned_intSI_type_node, 4);
6627
3fdaa45a
AH
6628 opaque_V2SI_type_node = copy_node (V2SI_type_node);
6629 opaque_V2SF_type_node = copy_node (V2SF_type_node);
6035d635 6630 opaque_p_V2SI_type_node = build_pointer_type (opaque_V2SI_type_node);
3fdaa45a 6631
8bb418a3
ZL
6632 /* The 'vector bool ...' types must be kept distinct from 'vector unsigned ...'
6633 types, especially in C++ land. Similarly, 'vector pixel' is distinct from
6634 'vector unsigned short'. */
6635
6636 bool_char_type_node = copy_node (unsigned_intQI_type_node);
6637 TYPE_MAIN_VARIANT (bool_char_type_node) = bool_char_type_node;
6638 bool_short_type_node = copy_node (unsigned_intHI_type_node);
6639 TYPE_MAIN_VARIANT (bool_short_type_node) = bool_short_type_node;
6640 bool_int_type_node = copy_node (unsigned_intSI_type_node);
6641 TYPE_MAIN_VARIANT (bool_int_type_node) = bool_int_type_node;
6642 pixel_type_node = copy_node (unsigned_intHI_type_node);
6643 TYPE_MAIN_VARIANT (pixel_type_node) = pixel_type_node;
6644
6645 (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL,
6646 get_identifier ("__bool char"),
6647 bool_char_type_node));
6648 (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL,
6649 get_identifier ("__bool short"),
6650 bool_short_type_node));
6651 (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL,
6652 get_identifier ("__bool int"),
6653 bool_int_type_node));
6654 (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL,
6655 get_identifier ("__pixel"),
6656 pixel_type_node));
6657
4a5eab38
PB
6658 bool_V16QI_type_node = build_vector_type (bool_char_type_node, 16);
6659 bool_V8HI_type_node = build_vector_type (bool_short_type_node, 8);
6660 bool_V4SI_type_node = build_vector_type (bool_int_type_node, 4);
6661 pixel_V8HI_type_node = build_vector_type (pixel_type_node, 8);
8bb418a3
ZL
6662
6663 (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL,
6664 get_identifier ("__vector unsigned char"),
6665 unsigned_V16QI_type_node));
6666 (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL,
6667 get_identifier ("__vector signed char"),
6668 V16QI_type_node));
6669 (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL,
6670 get_identifier ("__vector __bool char"),
6671 bool_V16QI_type_node));
6672
6673 (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL,
6674 get_identifier ("__vector unsigned short"),
6675 unsigned_V8HI_type_node));
6676 (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL,
6677 get_identifier ("__vector signed short"),
6678 V8HI_type_node));
6679 (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL,
6680 get_identifier ("__vector __bool short"),
6681 bool_V8HI_type_node));
6682
6683 (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL,
6684 get_identifier ("__vector unsigned int"),
6685 unsigned_V4SI_type_node));
6686 (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL,
6687 get_identifier ("__vector signed int"),
6688 V4SI_type_node));
6689 (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL,
6690 get_identifier ("__vector __bool int"),
6691 bool_V4SI_type_node));
6692
6693 (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL,
6694 get_identifier ("__vector float"),
6695 V4SF_type_node));
6696 (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL,
6697 get_identifier ("__vector __pixel"),
6698 pixel_V8HI_type_node));
6699
a3170dc6 6700 if (TARGET_SPE)
3fdaa45a 6701 spe_init_builtins ();
0ac081f6
AH
6702 if (TARGET_ALTIVEC)
6703 altivec_init_builtins ();
0559cc77
DE
6704 if (TARGET_ALTIVEC || TARGET_SPE)
6705 rs6000_common_init_builtins ();
0ac081f6
AH
6706}
6707
a3170dc6
AH
6708/* Search through a set of builtins and enable the mask bits.
6709 DESC is an array of builtins.
b6d08ca1 6710 SIZE is the total number of builtins.
a3170dc6
AH
6711 START is the builtin enum at which to start.
6712 END is the builtin enum at which to end. */
0ac081f6 6713static void
a2369ed3
DJ
6714enable_mask_for_builtins (struct builtin_description *desc, int size,
6715 enum rs6000_builtins start,
6716 enum rs6000_builtins end)
a3170dc6
AH
6717{
6718 int i;
6719
6720 for (i = 0; i < size; ++i)
6721 if (desc[i].code == start)
6722 break;
6723
6724 if (i == size)
6725 return;
6726
6727 for (; i < size; ++i)
6728 {
6729 /* Flip all the bits on. */
6730 desc[i].mask = target_flags;
6731 if (desc[i].code == end)
6732 break;
6733 }
6734}
6735
6736static void
863d938c 6737spe_init_builtins (void)
0ac081f6 6738{
a3170dc6
AH
6739 tree endlink = void_list_node;
6740 tree puint_type_node = build_pointer_type (unsigned_type_node);
6741 tree pushort_type_node = build_pointer_type (short_unsigned_type_node);
ae4b4a02 6742 struct builtin_description *d;
0ac081f6
AH
6743 size_t i;
6744
a3170dc6
AH
6745 tree v2si_ftype_4_v2si
6746 = build_function_type
3fdaa45a
AH
6747 (opaque_V2SI_type_node,
6748 tree_cons (NULL_TREE, opaque_V2SI_type_node,
6749 tree_cons (NULL_TREE, opaque_V2SI_type_node,
6750 tree_cons (NULL_TREE, opaque_V2SI_type_node,
6751 tree_cons (NULL_TREE, opaque_V2SI_type_node,
a3170dc6
AH
6752 endlink)))));
6753
6754 tree v2sf_ftype_4_v2sf
6755 = build_function_type
3fdaa45a
AH
6756 (opaque_V2SF_type_node,
6757 tree_cons (NULL_TREE, opaque_V2SF_type_node,
6758 tree_cons (NULL_TREE, opaque_V2SF_type_node,
6759 tree_cons (NULL_TREE, opaque_V2SF_type_node,
6760 tree_cons (NULL_TREE, opaque_V2SF_type_node,
a3170dc6
AH
6761 endlink)))));
6762
6763 tree int_ftype_int_v2si_v2si
6764 = build_function_type
6765 (integer_type_node,
6766 tree_cons (NULL_TREE, integer_type_node,
3fdaa45a
AH
6767 tree_cons (NULL_TREE, opaque_V2SI_type_node,
6768 tree_cons (NULL_TREE, opaque_V2SI_type_node,
a3170dc6
AH
6769 endlink))));
6770
6771 tree int_ftype_int_v2sf_v2sf
6772 = build_function_type
6773 (integer_type_node,
6774 tree_cons (NULL_TREE, integer_type_node,
3fdaa45a
AH
6775 tree_cons (NULL_TREE, opaque_V2SF_type_node,
6776 tree_cons (NULL_TREE, opaque_V2SF_type_node,
a3170dc6
AH
6777 endlink))));
6778
6779 tree void_ftype_v2si_puint_int
6780 = build_function_type (void_type_node,
3fdaa45a 6781 tree_cons (NULL_TREE, opaque_V2SI_type_node,
a3170dc6
AH
6782 tree_cons (NULL_TREE, puint_type_node,
6783 tree_cons (NULL_TREE,
6784 integer_type_node,
6785 endlink))));
6786
6787 tree void_ftype_v2si_puint_char
6788 = build_function_type (void_type_node,
3fdaa45a 6789 tree_cons (NULL_TREE, opaque_V2SI_type_node,
a3170dc6
AH
6790 tree_cons (NULL_TREE, puint_type_node,
6791 tree_cons (NULL_TREE,
6792 char_type_node,
6793 endlink))));
6794
6795 tree void_ftype_v2si_pv2si_int
6796 = build_function_type (void_type_node,
3fdaa45a 6797 tree_cons (NULL_TREE, opaque_V2SI_type_node,
6035d635 6798 tree_cons (NULL_TREE, opaque_p_V2SI_type_node,
a3170dc6
AH
6799 tree_cons (NULL_TREE,
6800 integer_type_node,
6801 endlink))));
6802
6803 tree void_ftype_v2si_pv2si_char
6804 = build_function_type (void_type_node,
3fdaa45a 6805 tree_cons (NULL_TREE, opaque_V2SI_type_node,
6035d635 6806 tree_cons (NULL_TREE, opaque_p_V2SI_type_node,
a3170dc6
AH
6807 tree_cons (NULL_TREE,
6808 char_type_node,
6809 endlink))));
6810
6811 tree void_ftype_int
6812 = build_function_type (void_type_node,
6813 tree_cons (NULL_TREE, integer_type_node, endlink));
6814
6815 tree int_ftype_void
36e8d515 6816 = build_function_type (integer_type_node, endlink);
a3170dc6
AH
6817
6818 tree v2si_ftype_pv2si_int
3fdaa45a 6819 = build_function_type (opaque_V2SI_type_node,
6035d635 6820 tree_cons (NULL_TREE, opaque_p_V2SI_type_node,
a3170dc6
AH
6821 tree_cons (NULL_TREE, integer_type_node,
6822 endlink)));
6823
6824 tree v2si_ftype_puint_int
3fdaa45a 6825 = build_function_type (opaque_V2SI_type_node,
a3170dc6
AH
6826 tree_cons (NULL_TREE, puint_type_node,
6827 tree_cons (NULL_TREE, integer_type_node,
6828 endlink)));
6829
6830 tree v2si_ftype_pushort_int
3fdaa45a 6831 = build_function_type (opaque_V2SI_type_node,
a3170dc6
AH
6832 tree_cons (NULL_TREE, pushort_type_node,
6833 tree_cons (NULL_TREE, integer_type_node,
6834 endlink)));
6835
00332c9f
AH
6836 tree v2si_ftype_signed_char
6837 = build_function_type (opaque_V2SI_type_node,
6838 tree_cons (NULL_TREE, signed_char_type_node,
6839 endlink));
6840
a3170dc6
AH
6841 /* The initialization of the simple binary and unary builtins is
6842 done in rs6000_common_init_builtins, but we have to enable the
6843 mask bits here manually because we have run out of `target_flags'
6844 bits. We really need to redesign this mask business. */
6845
6846 enable_mask_for_builtins ((struct builtin_description *) bdesc_2arg,
6847 ARRAY_SIZE (bdesc_2arg),
6848 SPE_BUILTIN_EVADDW,
6849 SPE_BUILTIN_EVXOR);
6850 enable_mask_for_builtins ((struct builtin_description *) bdesc_1arg,
6851 ARRAY_SIZE (bdesc_1arg),
6852 SPE_BUILTIN_EVABS,
6853 SPE_BUILTIN_EVSUBFUSIAAW);
6854 enable_mask_for_builtins ((struct builtin_description *) bdesc_spe_predicates,
6855 ARRAY_SIZE (bdesc_spe_predicates),
6856 SPE_BUILTIN_EVCMPEQ,
6857 SPE_BUILTIN_EVFSTSTLT);
6858 enable_mask_for_builtins ((struct builtin_description *) bdesc_spe_evsel,
6859 ARRAY_SIZE (bdesc_spe_evsel),
6860 SPE_BUILTIN_EVSEL_CMPGTS,
6861 SPE_BUILTIN_EVSEL_FSTSTEQ);
6862
36252949
AH
6863 (*lang_hooks.decls.pushdecl)
6864 (build_decl (TYPE_DECL, get_identifier ("__ev64_opaque__"),
6865 opaque_V2SI_type_node));
6866
a3170dc6
AH
6867 /* Initialize irregular SPE builtins. */
6868
6869 def_builtin (target_flags, "__builtin_spe_mtspefscr", void_ftype_int, SPE_BUILTIN_MTSPEFSCR);
6870 def_builtin (target_flags, "__builtin_spe_mfspefscr", int_ftype_void, SPE_BUILTIN_MFSPEFSCR);
6871 def_builtin (target_flags, "__builtin_spe_evstddx", void_ftype_v2si_pv2si_int, SPE_BUILTIN_EVSTDDX);
6872 def_builtin (target_flags, "__builtin_spe_evstdhx", void_ftype_v2si_pv2si_int, SPE_BUILTIN_EVSTDHX);
6873 def_builtin (target_flags, "__builtin_spe_evstdwx", void_ftype_v2si_pv2si_int, SPE_BUILTIN_EVSTDWX);
6874 def_builtin (target_flags, "__builtin_spe_evstwhex", void_ftype_v2si_puint_int, SPE_BUILTIN_EVSTWHEX);
6875 def_builtin (target_flags, "__builtin_spe_evstwhox", void_ftype_v2si_puint_int, SPE_BUILTIN_EVSTWHOX);
6876 def_builtin (target_flags, "__builtin_spe_evstwwex", void_ftype_v2si_puint_int, SPE_BUILTIN_EVSTWWEX);
6877 def_builtin (target_flags, "__builtin_spe_evstwwox", void_ftype_v2si_puint_int, SPE_BUILTIN_EVSTWWOX);
6878 def_builtin (target_flags, "__builtin_spe_evstdd", void_ftype_v2si_pv2si_char, SPE_BUILTIN_EVSTDD);
6879 def_builtin (target_flags, "__builtin_spe_evstdh", void_ftype_v2si_pv2si_char, SPE_BUILTIN_EVSTDH);
6880 def_builtin (target_flags, "__builtin_spe_evstdw", void_ftype_v2si_pv2si_char, SPE_BUILTIN_EVSTDW);
6881 def_builtin (target_flags, "__builtin_spe_evstwhe", void_ftype_v2si_puint_char, SPE_BUILTIN_EVSTWHE);
6882 def_builtin (target_flags, "__builtin_spe_evstwho", void_ftype_v2si_puint_char, SPE_BUILTIN_EVSTWHO);
6883 def_builtin (target_flags, "__builtin_spe_evstwwe", void_ftype_v2si_puint_char, SPE_BUILTIN_EVSTWWE);
6884 def_builtin (target_flags, "__builtin_spe_evstwwo", void_ftype_v2si_puint_char, SPE_BUILTIN_EVSTWWO);
00332c9f
AH
6885 def_builtin (target_flags, "__builtin_spe_evsplatfi", v2si_ftype_signed_char, SPE_BUILTIN_EVSPLATFI);
6886 def_builtin (target_flags, "__builtin_spe_evsplati", v2si_ftype_signed_char, SPE_BUILTIN_EVSPLATI);
a3170dc6
AH
6887
6888 /* Loads. */
6889 def_builtin (target_flags, "__builtin_spe_evlddx", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDDX);
6890 def_builtin (target_flags, "__builtin_spe_evldwx", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDWX);
6891 def_builtin (target_flags, "__builtin_spe_evldhx", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDHX);
6892 def_builtin (target_flags, "__builtin_spe_evlwhex", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHEX);
6893 def_builtin (target_flags, "__builtin_spe_evlwhoux", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHOUX);
6894 def_builtin (target_flags, "__builtin_spe_evlwhosx", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHOSX);
6895 def_builtin (target_flags, "__builtin_spe_evlwwsplatx", v2si_ftype_puint_int, SPE_BUILTIN_EVLWWSPLATX);
6896 def_builtin (target_flags, "__builtin_spe_evlwhsplatx", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHSPLATX);
6897 def_builtin (target_flags, "__builtin_spe_evlhhesplatx", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHESPLATX);
6898 def_builtin (target_flags, "__builtin_spe_evlhhousplatx", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHOUSPLATX);
6899 def_builtin (target_flags, "__builtin_spe_evlhhossplatx", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHOSSPLATX);
6900 def_builtin (target_flags, "__builtin_spe_evldd", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDD);
6901 def_builtin (target_flags, "__builtin_spe_evldw", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDW);
6902 def_builtin (target_flags, "__builtin_spe_evldh", v2si_ftype_pv2si_int, SPE_BUILTIN_EVLDH);
6903 def_builtin (target_flags, "__builtin_spe_evlhhesplat", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHESPLAT);
6904 def_builtin (target_flags, "__builtin_spe_evlhhossplat", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHOSSPLAT);
6905 def_builtin (target_flags, "__builtin_spe_evlhhousplat", v2si_ftype_pushort_int, SPE_BUILTIN_EVLHHOUSPLAT);
6906 def_builtin (target_flags, "__builtin_spe_evlwhe", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHE);
6907 def_builtin (target_flags, "__builtin_spe_evlwhos", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHOS);
6908 def_builtin (target_flags, "__builtin_spe_evlwhou", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHOU);
6909 def_builtin (target_flags, "__builtin_spe_evlwhsplat", v2si_ftype_puint_int, SPE_BUILTIN_EVLWHSPLAT);
6910 def_builtin (target_flags, "__builtin_spe_evlwwsplat", v2si_ftype_puint_int, SPE_BUILTIN_EVLWWSPLAT);
6911
6912 /* Predicates. */
6913 d = (struct builtin_description *) bdesc_spe_predicates;
6914 for (i = 0; i < ARRAY_SIZE (bdesc_spe_predicates); ++i, d++)
6915 {
6916 tree type;
6917
6918 switch (insn_data[d->icode].operand[1].mode)
6919 {
6920 case V2SImode:
6921 type = int_ftype_int_v2si_v2si;
6922 break;
6923 case V2SFmode:
6924 type = int_ftype_int_v2sf_v2sf;
6925 break;
6926 default:
6927 abort ();
6928 }
6929
6930 def_builtin (d->mask, d->name, type, d->code);
6931 }
6932
6933 /* Evsel predicates. */
6934 d = (struct builtin_description *) bdesc_spe_evsel;
6935 for (i = 0; i < ARRAY_SIZE (bdesc_spe_evsel); ++i, d++)
6936 {
6937 tree type;
6938
6939 switch (insn_data[d->icode].operand[1].mode)
6940 {
6941 case V2SImode:
6942 type = v2si_ftype_4_v2si;
6943 break;
6944 case V2SFmode:
6945 type = v2sf_ftype_4_v2sf;
6946 break;
6947 default:
6948 abort ();
6949 }
6950
6951 def_builtin (d->mask, d->name, type, d->code);
6952 }
6953}
6954
6955static void
863d938c 6956altivec_init_builtins (void)
a3170dc6
AH
6957{
6958 struct builtin_description *d;
6959 struct builtin_description_predicates *dp;
6960 size_t i;
6961 tree pfloat_type_node = build_pointer_type (float_type_node);
6962 tree pint_type_node = build_pointer_type (integer_type_node);
6963 tree pshort_type_node = build_pointer_type (short_integer_type_node);
6964 tree pchar_type_node = build_pointer_type (char_type_node);
6965
6966 tree pvoid_type_node = build_pointer_type (void_type_node);
6967
0dbc3651
ZW
6968 tree pcfloat_type_node = build_pointer_type (build_qualified_type (float_type_node, TYPE_QUAL_CONST));
6969 tree pcint_type_node = build_pointer_type (build_qualified_type (integer_type_node, TYPE_QUAL_CONST));
6970 tree pcshort_type_node = build_pointer_type (build_qualified_type (short_integer_type_node, TYPE_QUAL_CONST));
6971 tree pcchar_type_node = build_pointer_type (build_qualified_type (char_type_node, TYPE_QUAL_CONST));
6972
6973 tree pcvoid_type_node = build_pointer_type (build_qualified_type (void_type_node, TYPE_QUAL_CONST));
6974
a3170dc6
AH
6975 tree int_ftype_int_v4si_v4si
6976 = build_function_type_list (integer_type_node,
6977 integer_type_node, V4SI_type_node,
6978 V4SI_type_node, NULL_TREE);
0dbc3651
ZW
6979 tree v4sf_ftype_pcfloat
6980 = build_function_type_list (V4SF_type_node, pcfloat_type_node, NULL_TREE);
a3170dc6 6981 tree void_ftype_pfloat_v4sf
b4de2f7d 6982 = build_function_type_list (void_type_node,
a3170dc6 6983 pfloat_type_node, V4SF_type_node, NULL_TREE);
0dbc3651
ZW
6984 tree v4si_ftype_pcint
6985 = build_function_type_list (V4SI_type_node, pcint_type_node, NULL_TREE);
6986 tree void_ftype_pint_v4si
b4de2f7d
AH
6987 = build_function_type_list (void_type_node,
6988 pint_type_node, V4SI_type_node, NULL_TREE);
0dbc3651
ZW
6989 tree v8hi_ftype_pcshort
6990 = build_function_type_list (V8HI_type_node, pcshort_type_node, NULL_TREE);
f18c054f 6991 tree void_ftype_pshort_v8hi
b4de2f7d
AH
6992 = build_function_type_list (void_type_node,
6993 pshort_type_node, V8HI_type_node, NULL_TREE);
0dbc3651
ZW
6994 tree v16qi_ftype_pcchar
6995 = build_function_type_list (V16QI_type_node, pcchar_type_node, NULL_TREE);
f18c054f 6996 tree void_ftype_pchar_v16qi
b4de2f7d
AH
6997 = build_function_type_list (void_type_node,
6998 pchar_type_node, V16QI_type_node, NULL_TREE);
95385cbb 6999 tree void_ftype_v4si
b4de2f7d 7000 = build_function_type_list (void_type_node, V4SI_type_node, NULL_TREE);
a3170dc6
AH
7001 tree v8hi_ftype_void
7002 = build_function_type (V8HI_type_node, void_list_node);
7003 tree void_ftype_void
7004 = build_function_type (void_type_node, void_list_node);
7005 tree void_ftype_qi
7006 = build_function_type_list (void_type_node, char_type_node, NULL_TREE);
0dbc3651 7007
b4a62fa0 7008 tree v16qi_ftype_long_pcvoid
a3170dc6 7009 = build_function_type_list (V16QI_type_node,
b4a62fa0
SB
7010 long_integer_type_node, pcvoid_type_node, NULL_TREE);
7011 tree v8hi_ftype_long_pcvoid
a3170dc6 7012 = build_function_type_list (V8HI_type_node,
b4a62fa0
SB
7013 long_integer_type_node, pcvoid_type_node, NULL_TREE);
7014 tree v4si_ftype_long_pcvoid
a3170dc6 7015 = build_function_type_list (V4SI_type_node,
b4a62fa0 7016 long_integer_type_node, pcvoid_type_node, NULL_TREE);
0dbc3651 7017
b4a62fa0 7018 tree void_ftype_v4si_long_pvoid
b4de2f7d 7019 = build_function_type_list (void_type_node,
b4a62fa0 7020 V4SI_type_node, long_integer_type_node,
b4de2f7d 7021 pvoid_type_node, NULL_TREE);
b4a62fa0 7022 tree void_ftype_v16qi_long_pvoid
b4de2f7d 7023 = build_function_type_list (void_type_node,
b4a62fa0 7024 V16QI_type_node, long_integer_type_node,
b4de2f7d 7025 pvoid_type_node, NULL_TREE);
b4a62fa0 7026 tree void_ftype_v8hi_long_pvoid
b4de2f7d 7027 = build_function_type_list (void_type_node,
b4a62fa0 7028 V8HI_type_node, long_integer_type_node,
b4de2f7d 7029 pvoid_type_node, NULL_TREE);
a3170dc6
AH
7030 tree int_ftype_int_v8hi_v8hi
7031 = build_function_type_list (integer_type_node,
7032 integer_type_node, V8HI_type_node,
7033 V8HI_type_node, NULL_TREE);
7034 tree int_ftype_int_v16qi_v16qi
7035 = build_function_type_list (integer_type_node,
7036 integer_type_node, V16QI_type_node,
7037 V16QI_type_node, NULL_TREE);
7038 tree int_ftype_int_v4sf_v4sf
7039 = build_function_type_list (integer_type_node,
7040 integer_type_node, V4SF_type_node,
7041 V4SF_type_node, NULL_TREE);
7042 tree v4si_ftype_v4si
7043 = build_function_type_list (V4SI_type_node, V4SI_type_node, NULL_TREE);
7044 tree v8hi_ftype_v8hi
7045 = build_function_type_list (V8HI_type_node, V8HI_type_node, NULL_TREE);
7046 tree v16qi_ftype_v16qi
7047 = build_function_type_list (V16QI_type_node, V16QI_type_node, NULL_TREE);
7048 tree v4sf_ftype_v4sf
7049 = build_function_type_list (V4SF_type_node, V4SF_type_node, NULL_TREE);
8bb418a3 7050 tree void_ftype_pcvoid_int_int
a3170dc6 7051 = build_function_type_list (void_type_node,
0dbc3651 7052 pcvoid_type_node, integer_type_node,
8bb418a3
ZL
7053 integer_type_node, NULL_TREE);
7054 tree int_ftype_pcchar
7055 = build_function_type_list (integer_type_node,
7056 pcchar_type_node, NULL_TREE);
7057
0dbc3651
ZW
7058 def_builtin (MASK_ALTIVEC, "__builtin_altivec_ld_internal_4sf", v4sf_ftype_pcfloat,
7059 ALTIVEC_BUILTIN_LD_INTERNAL_4sf);
7060 def_builtin (MASK_ALTIVEC, "__builtin_altivec_st_internal_4sf", void_ftype_pfloat_v4sf,
7061 ALTIVEC_BUILTIN_ST_INTERNAL_4sf);
7062 def_builtin (MASK_ALTIVEC, "__builtin_altivec_ld_internal_4si", v4si_ftype_pcint,
7063 ALTIVEC_BUILTIN_LD_INTERNAL_4si);
7064 def_builtin (MASK_ALTIVEC, "__builtin_altivec_st_internal_4si", void_ftype_pint_v4si,
7065 ALTIVEC_BUILTIN_ST_INTERNAL_4si);
7066 def_builtin (MASK_ALTIVEC, "__builtin_altivec_ld_internal_8hi", v8hi_ftype_pcshort,
7067 ALTIVEC_BUILTIN_LD_INTERNAL_8hi);
7068 def_builtin (MASK_ALTIVEC, "__builtin_altivec_st_internal_8hi", void_ftype_pshort_v8hi,
7069 ALTIVEC_BUILTIN_ST_INTERNAL_8hi);
7070 def_builtin (MASK_ALTIVEC, "__builtin_altivec_ld_internal_16qi", v16qi_ftype_pcchar,
7071 ALTIVEC_BUILTIN_LD_INTERNAL_16qi);
7072 def_builtin (MASK_ALTIVEC, "__builtin_altivec_st_internal_16qi", void_ftype_pchar_v16qi,
7073 ALTIVEC_BUILTIN_ST_INTERNAL_16qi);
a3170dc6
AH
7074 def_builtin (MASK_ALTIVEC, "__builtin_altivec_mtvscr", void_ftype_v4si, ALTIVEC_BUILTIN_MTVSCR);
7075 def_builtin (MASK_ALTIVEC, "__builtin_altivec_mfvscr", v8hi_ftype_void, ALTIVEC_BUILTIN_MFVSCR);
7076 def_builtin (MASK_ALTIVEC, "__builtin_altivec_dssall", void_ftype_void, ALTIVEC_BUILTIN_DSSALL);
7077 def_builtin (MASK_ALTIVEC, "__builtin_altivec_dss", void_ftype_qi, ALTIVEC_BUILTIN_DSS);
b4a62fa0
SB
7078 def_builtin (MASK_ALTIVEC, "__builtin_altivec_lvsl", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVSL);
7079 def_builtin (MASK_ALTIVEC, "__builtin_altivec_lvsr", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVSR);
7080 def_builtin (MASK_ALTIVEC, "__builtin_altivec_lvebx", v16qi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVEBX);
7081 def_builtin (MASK_ALTIVEC, "__builtin_altivec_lvehx", v8hi_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVEHX);
7082 def_builtin (MASK_ALTIVEC, "__builtin_altivec_lvewx", v4si_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVEWX);
7083 def_builtin (MASK_ALTIVEC, "__builtin_altivec_lvxl", v4si_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVXL);
7084 def_builtin (MASK_ALTIVEC, "__builtin_altivec_lvx", v4si_ftype_long_pcvoid, ALTIVEC_BUILTIN_LVX);
7085 def_builtin (MASK_ALTIVEC, "__builtin_altivec_stvx", void_ftype_v4si_long_pvoid, ALTIVEC_BUILTIN_STVX);
7086 def_builtin (MASK_ALTIVEC, "__builtin_altivec_stvewx", void_ftype_v4si_long_pvoid, ALTIVEC_BUILTIN_STVEWX);
7087 def_builtin (MASK_ALTIVEC, "__builtin_altivec_stvxl", void_ftype_v4si_long_pvoid, ALTIVEC_BUILTIN_STVXL);
7088 def_builtin (MASK_ALTIVEC, "__builtin_altivec_stvebx", void_ftype_v16qi_long_pvoid, ALTIVEC_BUILTIN_STVEBX);
7089 def_builtin (MASK_ALTIVEC, "__builtin_altivec_stvehx", void_ftype_v8hi_long_pvoid, ALTIVEC_BUILTIN_STVEHX);
a3170dc6 7090
8bb418a3
ZL
7091 /* See altivec.h for usage of "__builtin_altivec_compiletime_error". */
7092 def_builtin (MASK_ALTIVEC, "__builtin_altivec_compiletime_error", int_ftype_pcchar,
7093 ALTIVEC_BUILTIN_COMPILETIME_ERROR);
7094
a3170dc6
AH
7095 /* Add the DST variants. */
7096 d = (struct builtin_description *) bdesc_dst;
7097 for (i = 0; i < ARRAY_SIZE (bdesc_dst); i++, d++)
8bb418a3 7098 def_builtin (d->mask, d->name, void_ftype_pcvoid_int_int, d->code);
a3170dc6
AH
7099
7100 /* Initialize the predicates. */
7101 dp = (struct builtin_description_predicates *) bdesc_altivec_preds;
7102 for (i = 0; i < ARRAY_SIZE (bdesc_altivec_preds); i++, dp++)
7103 {
7104 enum machine_mode mode1;
7105 tree type;
7106
7107 mode1 = insn_data[dp->icode].operand[1].mode;
7108
7109 switch (mode1)
7110 {
7111 case V4SImode:
7112 type = int_ftype_int_v4si_v4si;
7113 break;
7114 case V8HImode:
7115 type = int_ftype_int_v8hi_v8hi;
7116 break;
7117 case V16QImode:
7118 type = int_ftype_int_v16qi_v16qi;
7119 break;
7120 case V4SFmode:
7121 type = int_ftype_int_v4sf_v4sf;
7122 break;
7123 default:
7124 abort ();
7125 }
7126
7127 def_builtin (dp->mask, dp->name, type, dp->code);
7128 }
7129
7130 /* Initialize the abs* operators. */
7131 d = (struct builtin_description *) bdesc_abs;
7132 for (i = 0; i < ARRAY_SIZE (bdesc_abs); i++, d++)
7133 {
7134 enum machine_mode mode0;
7135 tree type;
7136
7137 mode0 = insn_data[d->icode].operand[0].mode;
7138
7139 switch (mode0)
7140 {
7141 case V4SImode:
7142 type = v4si_ftype_v4si;
7143 break;
7144 case V8HImode:
7145 type = v8hi_ftype_v8hi;
7146 break;
7147 case V16QImode:
7148 type = v16qi_ftype_v16qi;
7149 break;
7150 case V4SFmode:
7151 type = v4sf_ftype_v4sf;
7152 break;
7153 default:
7154 abort ();
7155 }
7156
7157 def_builtin (d->mask, d->name, type, d->code);
7158 }
7159}
7160
7161static void
863d938c 7162rs6000_common_init_builtins (void)
a3170dc6
AH
7163{
7164 struct builtin_description *d;
7165 size_t i;
7166
7167 tree v4sf_ftype_v4sf_v4sf_v16qi
7168 = build_function_type_list (V4SF_type_node,
7169 V4SF_type_node, V4SF_type_node,
7170 V16QI_type_node, NULL_TREE);
7171 tree v4si_ftype_v4si_v4si_v16qi
7172 = build_function_type_list (V4SI_type_node,
7173 V4SI_type_node, V4SI_type_node,
7174 V16QI_type_node, NULL_TREE);
7175 tree v8hi_ftype_v8hi_v8hi_v16qi
7176 = build_function_type_list (V8HI_type_node,
7177 V8HI_type_node, V8HI_type_node,
7178 V16QI_type_node, NULL_TREE);
7179 tree v16qi_ftype_v16qi_v16qi_v16qi
7180 = build_function_type_list (V16QI_type_node,
7181 V16QI_type_node, V16QI_type_node,
7182 V16QI_type_node, NULL_TREE);
b9e4e5d1
ZL
7183 tree v4si_ftype_int
7184 = build_function_type_list (V4SI_type_node, integer_type_node, NULL_TREE);
7185 tree v8hi_ftype_int
7186 = build_function_type_list (V8HI_type_node, integer_type_node, NULL_TREE);
7187 tree v16qi_ftype_int
7188 = build_function_type_list (V16QI_type_node, integer_type_node, NULL_TREE);
a3170dc6
AH
7189 tree v8hi_ftype_v16qi
7190 = build_function_type_list (V8HI_type_node, V16QI_type_node, NULL_TREE);
7191 tree v4sf_ftype_v4sf
7192 = build_function_type_list (V4SF_type_node, V4SF_type_node, NULL_TREE);
7193
7194 tree v2si_ftype_v2si_v2si
2abe3e28
AH
7195 = build_function_type_list (opaque_V2SI_type_node,
7196 opaque_V2SI_type_node,
7197 opaque_V2SI_type_node, NULL_TREE);
a3170dc6
AH
7198
7199 tree v2sf_ftype_v2sf_v2sf
2abe3e28
AH
7200 = build_function_type_list (opaque_V2SF_type_node,
7201 opaque_V2SF_type_node,
7202 opaque_V2SF_type_node, NULL_TREE);
a3170dc6
AH
7203
7204 tree v2si_ftype_int_int
2abe3e28 7205 = build_function_type_list (opaque_V2SI_type_node,
a3170dc6
AH
7206 integer_type_node, integer_type_node,
7207 NULL_TREE);
7208
7209 tree v2si_ftype_v2si
2abe3e28
AH
7210 = build_function_type_list (opaque_V2SI_type_node,
7211 opaque_V2SI_type_node, NULL_TREE);
a3170dc6
AH
7212
7213 tree v2sf_ftype_v2sf
2abe3e28
AH
7214 = build_function_type_list (opaque_V2SF_type_node,
7215 opaque_V2SF_type_node, NULL_TREE);
a3170dc6
AH
7216
7217 tree v2sf_ftype_v2si
2abe3e28
AH
7218 = build_function_type_list (opaque_V2SF_type_node,
7219 opaque_V2SI_type_node, NULL_TREE);
a3170dc6
AH
7220
7221 tree v2si_ftype_v2sf
2abe3e28
AH
7222 = build_function_type_list (opaque_V2SI_type_node,
7223 opaque_V2SF_type_node, NULL_TREE);
a3170dc6
AH
7224
7225 tree v2si_ftype_v2si_char
2abe3e28
AH
7226 = build_function_type_list (opaque_V2SI_type_node,
7227 opaque_V2SI_type_node,
7228 char_type_node, NULL_TREE);
a3170dc6
AH
7229
7230 tree v2si_ftype_int_char
2abe3e28 7231 = build_function_type_list (opaque_V2SI_type_node,
a3170dc6
AH
7232 integer_type_node, char_type_node, NULL_TREE);
7233
7234 tree v2si_ftype_char
2abe3e28
AH
7235 = build_function_type_list (opaque_V2SI_type_node,
7236 char_type_node, NULL_TREE);
a3170dc6
AH
7237
7238 tree int_ftype_int_int
7239 = build_function_type_list (integer_type_node,
7240 integer_type_node, integer_type_node,
7241 NULL_TREE);
95385cbb 7242
0ac081f6 7243 tree v4si_ftype_v4si_v4si
b4de2f7d
AH
7244 = build_function_type_list (V4SI_type_node,
7245 V4SI_type_node, V4SI_type_node, NULL_TREE);
b9e4e5d1 7246 tree v4sf_ftype_v4si_int
b4de2f7d 7247 = build_function_type_list (V4SF_type_node,
b9e4e5d1
ZL
7248 V4SI_type_node, integer_type_node, NULL_TREE);
7249 tree v4si_ftype_v4sf_int
b4de2f7d 7250 = build_function_type_list (V4SI_type_node,
b9e4e5d1
ZL
7251 V4SF_type_node, integer_type_node, NULL_TREE);
7252 tree v4si_ftype_v4si_int
b4de2f7d 7253 = build_function_type_list (V4SI_type_node,
b9e4e5d1
ZL
7254 V4SI_type_node, integer_type_node, NULL_TREE);
7255 tree v8hi_ftype_v8hi_int
b4de2f7d 7256 = build_function_type_list (V8HI_type_node,
b9e4e5d1
ZL
7257 V8HI_type_node, integer_type_node, NULL_TREE);
7258 tree v16qi_ftype_v16qi_int
b4de2f7d 7259 = build_function_type_list (V16QI_type_node,
b9e4e5d1
ZL
7260 V16QI_type_node, integer_type_node, NULL_TREE);
7261 tree v16qi_ftype_v16qi_v16qi_int
b4de2f7d
AH
7262 = build_function_type_list (V16QI_type_node,
7263 V16QI_type_node, V16QI_type_node,
b9e4e5d1
ZL
7264 integer_type_node, NULL_TREE);
7265 tree v8hi_ftype_v8hi_v8hi_int
b4de2f7d
AH
7266 = build_function_type_list (V8HI_type_node,
7267 V8HI_type_node, V8HI_type_node,
b9e4e5d1
ZL
7268 integer_type_node, NULL_TREE);
7269 tree v4si_ftype_v4si_v4si_int
b4de2f7d
AH
7270 = build_function_type_list (V4SI_type_node,
7271 V4SI_type_node, V4SI_type_node,
b9e4e5d1
ZL
7272 integer_type_node, NULL_TREE);
7273 tree v4sf_ftype_v4sf_v4sf_int
b4de2f7d
AH
7274 = build_function_type_list (V4SF_type_node,
7275 V4SF_type_node, V4SF_type_node,
b9e4e5d1 7276 integer_type_node, NULL_TREE);
0ac081f6 7277 tree v4sf_ftype_v4sf_v4sf
b4de2f7d
AH
7278 = build_function_type_list (V4SF_type_node,
7279 V4SF_type_node, V4SF_type_node, NULL_TREE);
617e0e1d 7280 tree v4sf_ftype_v4sf_v4sf_v4si
b4de2f7d
AH
7281 = build_function_type_list (V4SF_type_node,
7282 V4SF_type_node, V4SF_type_node,
7283 V4SI_type_node, NULL_TREE);
2212663f 7284 tree v4sf_ftype_v4sf_v4sf_v4sf
b4de2f7d
AH
7285 = build_function_type_list (V4SF_type_node,
7286 V4SF_type_node, V4SF_type_node,
7287 V4SF_type_node, NULL_TREE);
617e0e1d 7288 tree v4si_ftype_v4si_v4si_v4si
b4de2f7d
AH
7289 = build_function_type_list (V4SI_type_node,
7290 V4SI_type_node, V4SI_type_node,
7291 V4SI_type_node, NULL_TREE);
0ac081f6 7292 tree v8hi_ftype_v8hi_v8hi
b4de2f7d
AH
7293 = build_function_type_list (V8HI_type_node,
7294 V8HI_type_node, V8HI_type_node, NULL_TREE);
2212663f 7295 tree v8hi_ftype_v8hi_v8hi_v8hi
b4de2f7d
AH
7296 = build_function_type_list (V8HI_type_node,
7297 V8HI_type_node, V8HI_type_node,
7298 V8HI_type_node, NULL_TREE);
2212663f 7299 tree v4si_ftype_v8hi_v8hi_v4si
b4de2f7d
AH
7300 = build_function_type_list (V4SI_type_node,
7301 V8HI_type_node, V8HI_type_node,
7302 V4SI_type_node, NULL_TREE);
2212663f 7303 tree v4si_ftype_v16qi_v16qi_v4si
b4de2f7d
AH
7304 = build_function_type_list (V4SI_type_node,
7305 V16QI_type_node, V16QI_type_node,
7306 V4SI_type_node, NULL_TREE);
0ac081f6 7307 tree v16qi_ftype_v16qi_v16qi
b4de2f7d
AH
7308 = build_function_type_list (V16QI_type_node,
7309 V16QI_type_node, V16QI_type_node, NULL_TREE);
0ac081f6 7310 tree v4si_ftype_v4sf_v4sf
b4de2f7d
AH
7311 = build_function_type_list (V4SI_type_node,
7312 V4SF_type_node, V4SF_type_node, NULL_TREE);
0ac081f6 7313 tree v8hi_ftype_v16qi_v16qi
b4de2f7d
AH
7314 = build_function_type_list (V8HI_type_node,
7315 V16QI_type_node, V16QI_type_node, NULL_TREE);
0ac081f6 7316 tree v4si_ftype_v8hi_v8hi
b4de2f7d
AH
7317 = build_function_type_list (V4SI_type_node,
7318 V8HI_type_node, V8HI_type_node, NULL_TREE);
0ac081f6 7319 tree v8hi_ftype_v4si_v4si
b4de2f7d
AH
7320 = build_function_type_list (V8HI_type_node,
7321 V4SI_type_node, V4SI_type_node, NULL_TREE);
0ac081f6 7322 tree v16qi_ftype_v8hi_v8hi
b4de2f7d
AH
7323 = build_function_type_list (V16QI_type_node,
7324 V8HI_type_node, V8HI_type_node, NULL_TREE);
0ac081f6 7325 tree v4si_ftype_v16qi_v4si
b4de2f7d
AH
7326 = build_function_type_list (V4SI_type_node,
7327 V16QI_type_node, V4SI_type_node, NULL_TREE);
fa066a23 7328 tree v4si_ftype_v16qi_v16qi
b4de2f7d
AH
7329 = build_function_type_list (V4SI_type_node,
7330 V16QI_type_node, V16QI_type_node, NULL_TREE);
0ac081f6 7331 tree v4si_ftype_v8hi_v4si
b4de2f7d
AH
7332 = build_function_type_list (V4SI_type_node,
7333 V8HI_type_node, V4SI_type_node, NULL_TREE);
a3170dc6
AH
7334 tree v4si_ftype_v8hi
7335 = build_function_type_list (V4SI_type_node, V8HI_type_node, NULL_TREE);
7336 tree int_ftype_v4si_v4si
7337 = build_function_type_list (integer_type_node,
7338 V4SI_type_node, V4SI_type_node, NULL_TREE);
7339 tree int_ftype_v4sf_v4sf
7340 = build_function_type_list (integer_type_node,
7341 V4SF_type_node, V4SF_type_node, NULL_TREE);
7342 tree int_ftype_v16qi_v16qi
7343 = build_function_type_list (integer_type_node,
7344 V16QI_type_node, V16QI_type_node, NULL_TREE);
0ac081f6 7345 tree int_ftype_v8hi_v8hi
b4de2f7d
AH
7346 = build_function_type_list (integer_type_node,
7347 V8HI_type_node, V8HI_type_node, NULL_TREE);
0ac081f6 7348
6f317ef3 7349 /* Add the simple ternary operators. */
2212663f 7350 d = (struct builtin_description *) bdesc_3arg;
ca7558fc 7351 for (i = 0; i < ARRAY_SIZE (bdesc_3arg); i++, d++)
2212663f
DB
7352 {
7353
7354 enum machine_mode mode0, mode1, mode2, mode3;
7355 tree type;
7356
0559cc77 7357 if (d->name == 0 || d->icode == CODE_FOR_nothing)
2212663f
DB
7358 continue;
7359
7360 mode0 = insn_data[d->icode].operand[0].mode;
7361 mode1 = insn_data[d->icode].operand[1].mode;
7362 mode2 = insn_data[d->icode].operand[2].mode;
7363 mode3 = insn_data[d->icode].operand[3].mode;
7364
7365 /* When all four are of the same mode. */
7366 if (mode0 == mode1 && mode1 == mode2 && mode2 == mode3)
7367 {
7368 switch (mode0)
7369 {
617e0e1d
DB
7370 case V4SImode:
7371 type = v4si_ftype_v4si_v4si_v4si;
7372 break;
2212663f
DB
7373 case V4SFmode:
7374 type = v4sf_ftype_v4sf_v4sf_v4sf;
7375 break;
7376 case V8HImode:
7377 type = v8hi_ftype_v8hi_v8hi_v8hi;
7378 break;
7379 case V16QImode:
7380 type = v16qi_ftype_v16qi_v16qi_v16qi;
7381 break;
7382 default:
7383 abort();
7384 }
7385 }
7386 else if (mode0 == mode1 && mode1 == mode2 && mode3 == V16QImode)
7387 {
7388 switch (mode0)
7389 {
7390 case V4SImode:
7391 type = v4si_ftype_v4si_v4si_v16qi;
7392 break;
7393 case V4SFmode:
7394 type = v4sf_ftype_v4sf_v4sf_v16qi;
7395 break;
7396 case V8HImode:
7397 type = v8hi_ftype_v8hi_v8hi_v16qi;
7398 break;
7399 case V16QImode:
7400 type = v16qi_ftype_v16qi_v16qi_v16qi;
7401 break;
7402 default:
7403 abort();
7404 }
7405 }
7406 else if (mode0 == V4SImode && mode1 == V16QImode && mode2 == V16QImode
7407 && mode3 == V4SImode)
24408032 7408 type = v4si_ftype_v16qi_v16qi_v4si;
2212663f
DB
7409 else if (mode0 == V4SImode && mode1 == V8HImode && mode2 == V8HImode
7410 && mode3 == V4SImode)
24408032 7411 type = v4si_ftype_v8hi_v8hi_v4si;
617e0e1d
DB
7412 else if (mode0 == V4SFmode && mode1 == V4SFmode && mode2 == V4SFmode
7413 && mode3 == V4SImode)
24408032
AH
7414 type = v4sf_ftype_v4sf_v4sf_v4si;
7415
7416 /* vchar, vchar, vchar, 4 bit literal. */
7417 else if (mode0 == V16QImode && mode1 == mode0 && mode2 == mode0
7418 && mode3 == QImode)
b9e4e5d1 7419 type = v16qi_ftype_v16qi_v16qi_int;
24408032
AH
7420
7421 /* vshort, vshort, vshort, 4 bit literal. */
7422 else if (mode0 == V8HImode && mode1 == mode0 && mode2 == mode0
7423 && mode3 == QImode)
b9e4e5d1 7424 type = v8hi_ftype_v8hi_v8hi_int;
24408032
AH
7425
7426 /* vint, vint, vint, 4 bit literal. */
7427 else if (mode0 == V4SImode && mode1 == mode0 && mode2 == mode0
7428 && mode3 == QImode)
b9e4e5d1 7429 type = v4si_ftype_v4si_v4si_int;
24408032
AH
7430
7431 /* vfloat, vfloat, vfloat, 4 bit literal. */
7432 else if (mode0 == V4SFmode && mode1 == mode0 && mode2 == mode0
7433 && mode3 == QImode)
b9e4e5d1 7434 type = v4sf_ftype_v4sf_v4sf_int;
24408032 7435
2212663f
DB
7436 else
7437 abort ();
7438
7439 def_builtin (d->mask, d->name, type, d->code);
7440 }
7441
0ac081f6 7442 /* Add the simple binary operators. */
00b960c7 7443 d = (struct builtin_description *) bdesc_2arg;
ca7558fc 7444 for (i = 0; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
0ac081f6
AH
7445 {
7446 enum machine_mode mode0, mode1, mode2;
7447 tree type;
7448
0559cc77 7449 if (d->name == 0 || d->icode == CODE_FOR_nothing)
0ac081f6
AH
7450 continue;
7451
7452 mode0 = insn_data[d->icode].operand[0].mode;
7453 mode1 = insn_data[d->icode].operand[1].mode;
7454 mode2 = insn_data[d->icode].operand[2].mode;
7455
7456 /* When all three operands are of the same mode. */
7457 if (mode0 == mode1 && mode1 == mode2)
7458 {
7459 switch (mode0)
7460 {
7461 case V4SFmode:
7462 type = v4sf_ftype_v4sf_v4sf;
7463 break;
7464 case V4SImode:
7465 type = v4si_ftype_v4si_v4si;
7466 break;
7467 case V16QImode:
7468 type = v16qi_ftype_v16qi_v16qi;
7469 break;
7470 case V8HImode:
7471 type = v8hi_ftype_v8hi_v8hi;
7472 break;
a3170dc6
AH
7473 case V2SImode:
7474 type = v2si_ftype_v2si_v2si;
7475 break;
7476 case V2SFmode:
7477 type = v2sf_ftype_v2sf_v2sf;
7478 break;
7479 case SImode:
7480 type = int_ftype_int_int;
7481 break;
0ac081f6
AH
7482 default:
7483 abort ();
7484 }
7485 }
7486
7487 /* A few other combos we really don't want to do manually. */
7488
7489 /* vint, vfloat, vfloat. */
7490 else if (mode0 == V4SImode && mode1 == V4SFmode && mode2 == V4SFmode)
7491 type = v4si_ftype_v4sf_v4sf;
7492
7493 /* vshort, vchar, vchar. */
7494 else if (mode0 == V8HImode && mode1 == V16QImode && mode2 == V16QImode)
7495 type = v8hi_ftype_v16qi_v16qi;
7496
7497 /* vint, vshort, vshort. */
7498 else if (mode0 == V4SImode && mode1 == V8HImode && mode2 == V8HImode)
7499 type = v4si_ftype_v8hi_v8hi;
7500
7501 /* vshort, vint, vint. */
7502 else if (mode0 == V8HImode && mode1 == V4SImode && mode2 == V4SImode)
7503 type = v8hi_ftype_v4si_v4si;
7504
7505 /* vchar, vshort, vshort. */
7506 else if (mode0 == V16QImode && mode1 == V8HImode && mode2 == V8HImode)
7507 type = v16qi_ftype_v8hi_v8hi;
7508
7509 /* vint, vchar, vint. */
7510 else if (mode0 == V4SImode && mode1 == V16QImode && mode2 == V4SImode)
7511 type = v4si_ftype_v16qi_v4si;
7512
fa066a23
AH
7513 /* vint, vchar, vchar. */
7514 else if (mode0 == V4SImode && mode1 == V16QImode && mode2 == V16QImode)
7515 type = v4si_ftype_v16qi_v16qi;
7516
0ac081f6
AH
7517 /* vint, vshort, vint. */
7518 else if (mode0 == V4SImode && mode1 == V8HImode && mode2 == V4SImode)
7519 type = v4si_ftype_v8hi_v4si;
2212663f
DB
7520
7521 /* vint, vint, 5 bit literal. */
7522 else if (mode0 == V4SImode && mode1 == V4SImode && mode2 == QImode)
b9e4e5d1 7523 type = v4si_ftype_v4si_int;
2212663f
DB
7524
7525 /* vshort, vshort, 5 bit literal. */
7526 else if (mode0 == V8HImode && mode1 == V8HImode && mode2 == QImode)
b9e4e5d1 7527 type = v8hi_ftype_v8hi_int;
2212663f
DB
7528
7529 /* vchar, vchar, 5 bit literal. */
7530 else if (mode0 == V16QImode && mode1 == V16QImode && mode2 == QImode)
b9e4e5d1 7531 type = v16qi_ftype_v16qi_int;
0ac081f6 7532
617e0e1d
DB
7533 /* vfloat, vint, 5 bit literal. */
7534 else if (mode0 == V4SFmode && mode1 == V4SImode && mode2 == QImode)
b9e4e5d1 7535 type = v4sf_ftype_v4si_int;
617e0e1d
DB
7536
7537 /* vint, vfloat, 5 bit literal. */
7538 else if (mode0 == V4SImode && mode1 == V4SFmode && mode2 == QImode)
b9e4e5d1 7539 type = v4si_ftype_v4sf_int;
617e0e1d 7540
a3170dc6
AH
7541 else if (mode0 == V2SImode && mode1 == SImode && mode2 == SImode)
7542 type = v2si_ftype_int_int;
7543
7544 else if (mode0 == V2SImode && mode1 == V2SImode && mode2 == QImode)
7545 type = v2si_ftype_v2si_char;
7546
7547 else if (mode0 == V2SImode && mode1 == SImode && mode2 == QImode)
7548 type = v2si_ftype_int_char;
7549
0ac081f6
AH
7550 /* int, x, x. */
7551 else if (mode0 == SImode)
7552 {
7553 switch (mode1)
7554 {
7555 case V4SImode:
7556 type = int_ftype_v4si_v4si;
7557 break;
7558 case V4SFmode:
7559 type = int_ftype_v4sf_v4sf;
7560 break;
7561 case V16QImode:
7562 type = int_ftype_v16qi_v16qi;
7563 break;
7564 case V8HImode:
7565 type = int_ftype_v8hi_v8hi;
7566 break;
7567 default:
7568 abort ();
7569 }
7570 }
7571
7572 else
7573 abort ();
7574
2212663f
DB
7575 def_builtin (d->mask, d->name, type, d->code);
7576 }
24408032 7577
2212663f
DB
7578 /* Add the simple unary operators. */
7579 d = (struct builtin_description *) bdesc_1arg;
ca7558fc 7580 for (i = 0; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
2212663f
DB
7581 {
7582 enum machine_mode mode0, mode1;
7583 tree type;
7584
0559cc77 7585 if (d->name == 0 || d->icode == CODE_FOR_nothing)
2212663f
DB
7586 continue;
7587
7588 mode0 = insn_data[d->icode].operand[0].mode;
7589 mode1 = insn_data[d->icode].operand[1].mode;
7590
7591 if (mode0 == V4SImode && mode1 == QImode)
b9e4e5d1 7592 type = v4si_ftype_int;
2212663f 7593 else if (mode0 == V8HImode && mode1 == QImode)
b9e4e5d1 7594 type = v8hi_ftype_int;
2212663f 7595 else if (mode0 == V16QImode && mode1 == QImode)
b9e4e5d1 7596 type = v16qi_ftype_int;
617e0e1d
DB
7597 else if (mode0 == V4SFmode && mode1 == V4SFmode)
7598 type = v4sf_ftype_v4sf;
20e26713
AH
7599 else if (mode0 == V8HImode && mode1 == V16QImode)
7600 type = v8hi_ftype_v16qi;
7601 else if (mode0 == V4SImode && mode1 == V8HImode)
7602 type = v4si_ftype_v8hi;
a3170dc6
AH
7603 else if (mode0 == V2SImode && mode1 == V2SImode)
7604 type = v2si_ftype_v2si;
7605 else if (mode0 == V2SFmode && mode1 == V2SFmode)
7606 type = v2sf_ftype_v2sf;
7607 else if (mode0 == V2SFmode && mode1 == V2SImode)
7608 type = v2sf_ftype_v2si;
7609 else if (mode0 == V2SImode && mode1 == V2SFmode)
7610 type = v2si_ftype_v2sf;
7611 else if (mode0 == V2SImode && mode1 == QImode)
7612 type = v2si_ftype_char;
2212663f
DB
7613 else
7614 abort ();
7615
0ac081f6
AH
7616 def_builtin (d->mask, d->name, type, d->code);
7617 }
7618}
7619
c15c90bb
ZW
7620static void
7621rs6000_init_libfuncs (void)
7622{
7623 if (!TARGET_HARD_FLOAT)
7624 return;
7625
c9034561 7626 if (DEFAULT_ABI != ABI_V4)
c15c90bb 7627 {
c9034561 7628 if (TARGET_XCOFF && ! TARGET_POWER2 && ! TARGET_POWERPC)
c15c90bb 7629 {
c9034561 7630 /* AIX library routines for float->int conversion. */
85363ca0
ZW
7631 set_conv_libfunc (sfix_optab, SImode, DFmode, "__itrunc");
7632 set_conv_libfunc (ufix_optab, SImode, DFmode, "__uitrunc");
4274207b
DE
7633 set_conv_libfunc (sfix_optab, SImode, TFmode, "_qitrunc");
7634 set_conv_libfunc (ufix_optab, SImode, TFmode, "_quitrunc");
c15c90bb
ZW
7635 }
7636
c9034561 7637 /* Standard AIX/Darwin/64-bit SVR4 quad floating point routines. */
c15c90bb
ZW
7638 set_optab_libfunc (add_optab, TFmode, "_xlqadd");
7639 set_optab_libfunc (sub_optab, TFmode, "_xlqsub");
7640 set_optab_libfunc (smul_optab, TFmode, "_xlqmul");
7641 set_optab_libfunc (sdiv_optab, TFmode, "_xlqdiv");
7642 }
c9034561 7643 else
c15c90bb 7644 {
c9034561 7645 /* 32-bit SVR4 quad floating point routines. */
c15c90bb
ZW
7646
7647 set_optab_libfunc (add_optab, TFmode, "_q_add");
7648 set_optab_libfunc (sub_optab, TFmode, "_q_sub");
7649 set_optab_libfunc (neg_optab, TFmode, "_q_neg");
7650 set_optab_libfunc (smul_optab, TFmode, "_q_mul");
7651 set_optab_libfunc (sdiv_optab, TFmode, "_q_div");
7652 if (TARGET_PPC_GPOPT || TARGET_POWER2)
7653 set_optab_libfunc (sqrt_optab, TFmode, "_q_sqrt");
7654
c9034561
ZW
7655 set_optab_libfunc (eq_optab, TFmode, "_q_feq");
7656 set_optab_libfunc (ne_optab, TFmode, "_q_fne");
7657 set_optab_libfunc (gt_optab, TFmode, "_q_fgt");
7658 set_optab_libfunc (ge_optab, TFmode, "_q_fge");
7659 set_optab_libfunc (lt_optab, TFmode, "_q_flt");
7660 set_optab_libfunc (le_optab, TFmode, "_q_fle");
7661
85363ca0
ZW
7662 set_conv_libfunc (sext_optab, TFmode, SFmode, "_q_stoq");
7663 set_conv_libfunc (sext_optab, TFmode, DFmode, "_q_dtoq");
7664 set_conv_libfunc (trunc_optab, SFmode, TFmode, "_q_qtos");
7665 set_conv_libfunc (trunc_optab, DFmode, TFmode, "_q_qtod");
7666 set_conv_libfunc (sfix_optab, SImode, TFmode, "_q_qtoi");
7667 set_conv_libfunc (ufix_optab, SImode, TFmode, "_q_qtou");
7668 set_conv_libfunc (sfloat_optab, TFmode, SImode, "_q_itoq");
c15c90bb
ZW
7669 }
7670}
35aff10b 7671\f
7e69e155
MM
7672/* Expand a block move operation, and return 1 if successful. Return 0
7673 if we should let the compiler generate normal code.
7674
7675 operands[0] is the destination
7676 operands[1] is the source
7677 operands[2] is the length
7678 operands[3] is the alignment */
7679
3933e0e1
MM
7680#define MAX_MOVE_REG 4
7681
7e69e155 7682int
a2369ed3 7683expand_block_move (rtx operands[])
7e69e155 7684{
b6c9286a
MM
7685 rtx orig_dest = operands[0];
7686 rtx orig_src = operands[1];
7e69e155 7687 rtx bytes_rtx = operands[2];
7e69e155 7688 rtx align_rtx = operands[3];
3933e0e1 7689 int constp = (GET_CODE (bytes_rtx) == CONST_INT);
5ee95df6 7690 int align;
3933e0e1
MM
7691 int bytes;
7692 int offset;
7e69e155 7693 int move_bytes;
cabfd258
GK
7694 rtx stores[MAX_MOVE_REG];
7695 int num_reg = 0;
7e69e155 7696
3933e0e1 7697 /* If this is not a fixed size move, just call memcpy */
cc0d9ba8 7698 if (! constp)
3933e0e1
MM
7699 return 0;
7700
5ee95df6
FS
7701 /* If this is not a fixed size alignment, abort */
7702 if (GET_CODE (align_rtx) != CONST_INT)
7703 abort ();
7704 align = INTVAL (align_rtx);
7705
7e69e155 7706 /* Anything to move? */
3933e0e1
MM
7707 bytes = INTVAL (bytes_rtx);
7708 if (bytes <= 0)
7e69e155
MM
7709 return 1;
7710
ea9982a8 7711 /* store_one_arg depends on expand_block_move to handle at least the size of
6f317ef3 7712 reg_parm_stack_space. */
ea9982a8 7713 if (bytes > (TARGET_POWERPC64 ? 64 : 32))
7e69e155
MM
7714 return 0;
7715
cabfd258 7716 for (offset = 0; bytes > 0; offset += move_bytes, bytes -= move_bytes)
7e69e155 7717 {
cabfd258 7718 union {
a2369ed3
DJ
7719 rtx (*movstrsi) (rtx, rtx, rtx, rtx);
7720 rtx (*mov) (rtx, rtx);
cabfd258
GK
7721 } gen_func;
7722 enum machine_mode mode = BLKmode;
7723 rtx src, dest;
7724
7725 if (TARGET_STRING
7726 && bytes > 24 /* move up to 32 bytes at a time */
7727 && ! fixed_regs[5]
7728 && ! fixed_regs[6]
7729 && ! fixed_regs[7]
7730 && ! fixed_regs[8]
7731 && ! fixed_regs[9]
7732 && ! fixed_regs[10]
7733 && ! fixed_regs[11]
7734 && ! fixed_regs[12])
7e69e155 7735 {
cabfd258
GK
7736 move_bytes = (bytes > 32) ? 32 : bytes;
7737 gen_func.movstrsi = gen_movstrsi_8reg;
7738 }
7739 else if (TARGET_STRING
7740 && bytes > 16 /* move up to 24 bytes at a time */
7741 && ! fixed_regs[5]
7742 && ! fixed_regs[6]
7743 && ! fixed_regs[7]
7744 && ! fixed_regs[8]
7745 && ! fixed_regs[9]
7746 && ! fixed_regs[10])
7747 {
7748 move_bytes = (bytes > 24) ? 24 : bytes;
7749 gen_func.movstrsi = gen_movstrsi_6reg;
7750 }
7751 else if (TARGET_STRING
7752 && bytes > 8 /* move up to 16 bytes at a time */
7753 && ! fixed_regs[5]
7754 && ! fixed_regs[6]
7755 && ! fixed_regs[7]
7756 && ! fixed_regs[8])
7757 {
7758 move_bytes = (bytes > 16) ? 16 : bytes;
7759 gen_func.movstrsi = gen_movstrsi_4reg;
7760 }
7761 else if (bytes >= 8 && TARGET_POWERPC64
7762 /* 64-bit loads and stores require word-aligned
7763 displacements. */
7764 && (align >= 8 || (! STRICT_ALIGNMENT && align >= 4)))
7765 {
7766 move_bytes = 8;
7767 mode = DImode;
7768 gen_func.mov = gen_movdi;
7769 }
7770 else if (TARGET_STRING && bytes > 4 && !TARGET_POWERPC64)
7771 { /* move up to 8 bytes at a time */
7772 move_bytes = (bytes > 8) ? 8 : bytes;
7773 gen_func.movstrsi = gen_movstrsi_2reg;
7774 }
7775 else if (bytes >= 4 && (align >= 4 || ! STRICT_ALIGNMENT))
7776 { /* move 4 bytes */
7777 move_bytes = 4;
7778 mode = SImode;
7779 gen_func.mov = gen_movsi;
7780 }
7781 else if (bytes == 2 && (align >= 2 || ! STRICT_ALIGNMENT))
7782 { /* move 2 bytes */
7783 move_bytes = 2;
7784 mode = HImode;
7785 gen_func.mov = gen_movhi;
7786 }
7787 else if (TARGET_STRING && bytes > 1)
7788 { /* move up to 4 bytes at a time */
7789 move_bytes = (bytes > 4) ? 4 : bytes;
7790 gen_func.movstrsi = gen_movstrsi_1reg;
7791 }
7792 else /* move 1 byte at a time */
7793 {
7794 move_bytes = 1;
7795 mode = QImode;
7796 gen_func.mov = gen_movqi;
7797 }
7798
7799 src = adjust_address (orig_src, mode, offset);
7800 dest = adjust_address (orig_dest, mode, offset);
7801
7802 if (mode != BLKmode)
7803 {
7804 rtx tmp_reg = gen_reg_rtx (mode);
7805
7806 emit_insn ((*gen_func.mov) (tmp_reg, src));
7807 stores[num_reg++] = (*gen_func.mov) (dest, tmp_reg);
4c64a852 7808 }
3933e0e1 7809
cabfd258
GK
7810 if (mode == BLKmode || num_reg >= MAX_MOVE_REG || bytes == move_bytes)
7811 {
7812 int i;
7813 for (i = 0; i < num_reg; i++)
7814 emit_insn (stores[i]);
7815 num_reg = 0;
7816 }
35aff10b 7817
cabfd258 7818 if (mode == BLKmode)
7e69e155 7819 {
cabfd258
GK
7820 /* Move the address into scratch registers. The movstrsi
7821 patterns require zero offset. */
7822 if (!REG_P (XEXP (src, 0)))
b6c9286a 7823 {
cabfd258
GK
7824 rtx src_reg = copy_addr_to_reg (XEXP (src, 0));
7825 src = replace_equiv_address (src, src_reg);
b6c9286a 7826 }
cabfd258
GK
7827 set_mem_size (src, GEN_INT (move_bytes));
7828
7829 if (!REG_P (XEXP (dest, 0)))
3933e0e1 7830 {
cabfd258
GK
7831 rtx dest_reg = copy_addr_to_reg (XEXP (dest, 0));
7832 dest = replace_equiv_address (dest, dest_reg);
7e69e155 7833 }
cabfd258
GK
7834 set_mem_size (dest, GEN_INT (move_bytes));
7835
7836 emit_insn ((*gen_func.movstrsi) (dest, src,
7837 GEN_INT (move_bytes & 31),
7838 align_rtx));
7e69e155 7839 }
7e69e155
MM
7840 }
7841
7842 return 1;
7843}
7844
9878760c
RK
7845\f
7846/* Return 1 if OP is a load multiple operation. It is known to be a
7847 PARALLEL and the first section will be tested. */
7848
7849int
a2369ed3 7850load_multiple_operation (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
9878760c
RK
7851{
7852 int count = XVECLEN (op, 0);
e2c953b6 7853 unsigned int dest_regno;
9878760c
RK
7854 rtx src_addr;
7855 int i;
7856
7857 /* Perform a quick check so we don't blow up below. */
7858 if (count <= 1
7859 || GET_CODE (XVECEXP (op, 0, 0)) != SET
7860 || GET_CODE (SET_DEST (XVECEXP (op, 0, 0))) != REG
7861 || GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != MEM)
7862 return 0;
7863
7864 dest_regno = REGNO (SET_DEST (XVECEXP (op, 0, 0)));
7865 src_addr = XEXP (SET_SRC (XVECEXP (op, 0, 0)), 0);
7866
7867 for (i = 1; i < count; i++)
7868 {
7869 rtx elt = XVECEXP (op, 0, i);
7870
7871 if (GET_CODE (elt) != SET
7872 || GET_CODE (SET_DEST (elt)) != REG
7873 || GET_MODE (SET_DEST (elt)) != SImode
7874 || REGNO (SET_DEST (elt)) != dest_regno + i
7875 || GET_CODE (SET_SRC (elt)) != MEM
7876 || GET_MODE (SET_SRC (elt)) != SImode
7877 || GET_CODE (XEXP (SET_SRC (elt), 0)) != PLUS
7878 || ! rtx_equal_p (XEXP (XEXP (SET_SRC (elt), 0), 0), src_addr)
7879 || GET_CODE (XEXP (XEXP (SET_SRC (elt), 0), 1)) != CONST_INT
7880 || INTVAL (XEXP (XEXP (SET_SRC (elt), 0), 1)) != i * 4)
7881 return 0;
7882 }
7883
7884 return 1;
7885}
7886
7887/* Similar, but tests for store multiple. Here, the second vector element
7888 is a CLOBBER. It will be tested later. */
7889
7890int
a2369ed3 7891store_multiple_operation (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
9878760c
RK
7892{
7893 int count = XVECLEN (op, 0) - 1;
e2c953b6 7894 unsigned int src_regno;
9878760c
RK
7895 rtx dest_addr;
7896 int i;
7897
7898 /* Perform a quick check so we don't blow up below. */
7899 if (count <= 1
7900 || GET_CODE (XVECEXP (op, 0, 0)) != SET
7901 || GET_CODE (SET_DEST (XVECEXP (op, 0, 0))) != MEM
7902 || GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != REG)
7903 return 0;
7904
7905 src_regno = REGNO (SET_SRC (XVECEXP (op, 0, 0)));
7906 dest_addr = XEXP (SET_DEST (XVECEXP (op, 0, 0)), 0);
7907
7908 for (i = 1; i < count; i++)
7909 {
7910 rtx elt = XVECEXP (op, 0, i + 1);
7911
7912 if (GET_CODE (elt) != SET
7913 || GET_CODE (SET_SRC (elt)) != REG
7914 || GET_MODE (SET_SRC (elt)) != SImode
7915 || REGNO (SET_SRC (elt)) != src_regno + i
7916 || GET_CODE (SET_DEST (elt)) != MEM
7917 || GET_MODE (SET_DEST (elt)) != SImode
7918 || GET_CODE (XEXP (SET_DEST (elt), 0)) != PLUS
7919 || ! rtx_equal_p (XEXP (XEXP (SET_DEST (elt), 0), 0), dest_addr)
7920 || GET_CODE (XEXP (XEXP (SET_DEST (elt), 0), 1)) != CONST_INT
7921 || INTVAL (XEXP (XEXP (SET_DEST (elt), 0), 1)) != i * 4)
7922 return 0;
7923 }
7924
7925 return 1;
7926}
9ebbca7d 7927
9caa3eb2
DE
7928/* Return a string to perform a load_multiple operation.
7929 operands[0] is the vector.
7930 operands[1] is the source address.
7931 operands[2] is the first destination register. */
7932
7933const char *
a2369ed3 7934rs6000_output_load_multiple (rtx operands[3])
9caa3eb2
DE
7935{
7936 /* We have to handle the case where the pseudo used to contain the address
7937 is assigned to one of the output registers. */
7938 int i, j;
7939 int words = XVECLEN (operands[0], 0);
7940 rtx xop[10];
7941
7942 if (XVECLEN (operands[0], 0) == 1)
7943 return "{l|lwz} %2,0(%1)";
7944
7945 for (i = 0; i < words; i++)
7946 if (refers_to_regno_p (REGNO (operands[2]) + i,
7947 REGNO (operands[2]) + i + 1, operands[1], 0))
7948 {
7949 if (i == words-1)
7950 {
7951 xop[0] = GEN_INT (4 * (words-1));
7952 xop[1] = operands[1];
7953 xop[2] = operands[2];
7954 output_asm_insn ("{lsi|lswi} %2,%1,%0\n\t{l|lwz} %1,%0(%1)", xop);
7955 return "";
7956 }
7957 else if (i == 0)
7958 {
7959 xop[0] = GEN_INT (4 * (words-1));
7960 xop[1] = operands[1];
7961 xop[2] = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
7962 output_asm_insn ("{cal %1,4(%1)|addi %1,%1,4}\n\t{lsi|lswi} %2,%1,%0\n\t{l|lwz} %1,-4(%1)", xop);
7963 return "";
7964 }
7965 else
7966 {
7967 for (j = 0; j < words; j++)
7968 if (j != i)
7969 {
7970 xop[0] = GEN_INT (j * 4);
7971 xop[1] = operands[1];
7972 xop[2] = gen_rtx_REG (SImode, REGNO (operands[2]) + j);
7973 output_asm_insn ("{l|lwz} %2,%0(%1)", xop);
7974 }
7975 xop[0] = GEN_INT (i * 4);
7976 xop[1] = operands[1];
7977 output_asm_insn ("{l|lwz} %1,%0(%1)", xop);
7978 return "";
7979 }
7980 }
7981
7982 return "{lsi|lswi} %2,%1,%N0";
7983}
7984
00b960c7
AH
7985/* Return 1 for a parallel vrsave operation. */
7986
7987int
a2369ed3 7988vrsave_operation (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
00b960c7
AH
7989{
7990 int count = XVECLEN (op, 0);
7991 unsigned int dest_regno, src_regno;
7992 int i;
7993
7994 if (count <= 1
7995 || GET_CODE (XVECEXP (op, 0, 0)) != SET
7996 || GET_CODE (SET_DEST (XVECEXP (op, 0, 0))) != REG
a004eb82 7997 || GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != UNSPEC_VOLATILE)
00b960c7
AH
7998 return 0;
7999
8000 dest_regno = REGNO (SET_DEST (XVECEXP (op, 0, 0)));
8001 src_regno = REGNO (SET_SRC (XVECEXP (op, 0, 0)));
8002
8003 if (dest_regno != VRSAVE_REGNO
8004 && src_regno != VRSAVE_REGNO)
8005 return 0;
8006
8007 for (i = 1; i < count; i++)
8008 {
8009 rtx elt = XVECEXP (op, 0, i);
8010
9aa86737
AH
8011 if (GET_CODE (elt) != CLOBBER
8012 && GET_CODE (elt) != SET)
00b960c7
AH
8013 return 0;
8014 }
8015
8016 return 1;
8017}
8018
2c4a9cff
DE
8019/* Return 1 for an PARALLEL suitable for mfcr. */
8020
8021int
a2369ed3 8022mfcr_operation (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2c4a9cff
DE
8023{
8024 int count = XVECLEN (op, 0);
8025 int i;
8026
8027 /* Perform a quick check so we don't blow up below. */
8028 if (count < 1
8029 || GET_CODE (XVECEXP (op, 0, 0)) != SET
8030 || GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != UNSPEC
8031 || XVECLEN (SET_SRC (XVECEXP (op, 0, 0)), 0) != 2)
8032 return 0;
8033
8034 for (i = 0; i < count; i++)
8035 {
8036 rtx exp = XVECEXP (op, 0, i);
8037 rtx unspec;
8038 int maskval;
8039 rtx src_reg;
8040
8041 src_reg = XVECEXP (SET_SRC (exp), 0, 0);
8042
8043 if (GET_CODE (src_reg) != REG
8044 || GET_MODE (src_reg) != CCmode
8045 || ! CR_REGNO_P (REGNO (src_reg)))
8046 return 0;
8047
8048 if (GET_CODE (exp) != SET
8049 || GET_CODE (SET_DEST (exp)) != REG
8050 || GET_MODE (SET_DEST (exp)) != SImode
8051 || ! INT_REGNO_P (REGNO (SET_DEST (exp))))
8052 return 0;
8053 unspec = SET_SRC (exp);
8054 maskval = 1 << (MAX_CR_REGNO - REGNO (src_reg));
8055
8056 if (GET_CODE (unspec) != UNSPEC
8057 || XINT (unspec, 1) != UNSPEC_MOVESI_FROM_CR
8058 || XVECLEN (unspec, 0) != 2
8059 || XVECEXP (unspec, 0, 0) != src_reg
8060 || GET_CODE (XVECEXP (unspec, 0, 1)) != CONST_INT
8061 || INTVAL (XVECEXP (unspec, 0, 1)) != maskval)
8062 return 0;
8063 }
8064 return 1;
8065}
8066
a4f6c312 8067/* Return 1 for an PARALLEL suitable for mtcrf. */
9ebbca7d
GK
8068
8069int
a2369ed3 8070mtcrf_operation (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
9ebbca7d
GK
8071{
8072 int count = XVECLEN (op, 0);
8073 int i;
9ebbca7d
GK
8074 rtx src_reg;
8075
8076 /* Perform a quick check so we don't blow up below. */
e35b9579
GK
8077 if (count < 1
8078 || GET_CODE (XVECEXP (op, 0, 0)) != SET
8079 || GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != UNSPEC
8080 || XVECLEN (SET_SRC (XVECEXP (op, 0, 0)), 0) != 2)
9ebbca7d 8081 return 0;
e35b9579 8082 src_reg = XVECEXP (SET_SRC (XVECEXP (op, 0, 0)), 0, 0);
9ebbca7d
GK
8083
8084 if (GET_CODE (src_reg) != REG
8085 || GET_MODE (src_reg) != SImode
8086 || ! INT_REGNO_P (REGNO (src_reg)))
8087 return 0;
8088
e35b9579 8089 for (i = 0; i < count; i++)
9ebbca7d
GK
8090 {
8091 rtx exp = XVECEXP (op, 0, i);
8092 rtx unspec;
8093 int maskval;
8094
8095 if (GET_CODE (exp) != SET
8096 || GET_CODE (SET_DEST (exp)) != REG
8097 || GET_MODE (SET_DEST (exp)) != CCmode
8098 || ! CR_REGNO_P (REGNO (SET_DEST (exp))))
8099 return 0;
8100 unspec = SET_SRC (exp);
8101 maskval = 1 << (MAX_CR_REGNO - REGNO (SET_DEST (exp)));
9ebbca7d
GK
8102
8103 if (GET_CODE (unspec) != UNSPEC
615158e2 8104 || XINT (unspec, 1) != UNSPEC_MOVESI_TO_CR
9ebbca7d
GK
8105 || XVECLEN (unspec, 0) != 2
8106 || XVECEXP (unspec, 0, 0) != src_reg
8107 || GET_CODE (XVECEXP (unspec, 0, 1)) != CONST_INT
8108 || INTVAL (XVECEXP (unspec, 0, 1)) != maskval)
8109 return 0;
8110 }
e35b9579 8111 return 1;
9ebbca7d
GK
8112}
8113
a4f6c312 8114/* Return 1 for an PARALLEL suitable for lmw. */
9ebbca7d
GK
8115
8116int
a2369ed3 8117lmw_operation (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
9ebbca7d
GK
8118{
8119 int count = XVECLEN (op, 0);
e2c953b6 8120 unsigned int dest_regno;
9ebbca7d 8121 rtx src_addr;
e2c953b6 8122 unsigned int base_regno;
9ebbca7d
GK
8123 HOST_WIDE_INT offset;
8124 int i;
8125
8126 /* Perform a quick check so we don't blow up below. */
8127 if (count <= 1
8128 || GET_CODE (XVECEXP (op, 0, 0)) != SET
8129 || GET_CODE (SET_DEST (XVECEXP (op, 0, 0))) != REG
8130 || GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != MEM)
8131 return 0;
8132
8133 dest_regno = REGNO (SET_DEST (XVECEXP (op, 0, 0)));
8134 src_addr = XEXP (SET_SRC (XVECEXP (op, 0, 0)), 0);
8135
8136 if (dest_regno > 31
e2c953b6 8137 || count != 32 - (int) dest_regno)
9ebbca7d
GK
8138 return 0;
8139
4d588c14 8140 if (legitimate_indirect_address_p (src_addr, 0))
9ebbca7d
GK
8141 {
8142 offset = 0;
8143 base_regno = REGNO (src_addr);
8144 if (base_regno == 0)
8145 return 0;
8146 }
4d588c14 8147 else if (legitimate_offset_address_p (SImode, src_addr, 0))
9ebbca7d
GK
8148 {
8149 offset = INTVAL (XEXP (src_addr, 1));
8150 base_regno = REGNO (XEXP (src_addr, 0));
8151 }
8152 else
8153 return 0;
8154
8155 for (i = 0; i < count; i++)
8156 {
8157 rtx elt = XVECEXP (op, 0, i);
8158 rtx newaddr;
8159 rtx addr_reg;
8160 HOST_WIDE_INT newoffset;
8161
8162 if (GET_CODE (elt) != SET
8163 || GET_CODE (SET_DEST (elt)) != REG
8164 || GET_MODE (SET_DEST (elt)) != SImode
8165 || REGNO (SET_DEST (elt)) != dest_regno + i
8166 || GET_CODE (SET_SRC (elt)) != MEM
8167 || GET_MODE (SET_SRC (elt)) != SImode)
8168 return 0;
8169 newaddr = XEXP (SET_SRC (elt), 0);
4d588c14 8170 if (legitimate_indirect_address_p (newaddr, 0))
9ebbca7d
GK
8171 {
8172 newoffset = 0;
8173 addr_reg = newaddr;
8174 }
4d588c14 8175 else if (legitimate_offset_address_p (SImode, newaddr, 0))
9ebbca7d
GK
8176 {
8177 addr_reg = XEXP (newaddr, 0);
8178 newoffset = INTVAL (XEXP (newaddr, 1));
8179 }
8180 else
8181 return 0;
8182 if (REGNO (addr_reg) != base_regno
8183 || newoffset != offset + 4 * i)
8184 return 0;
8185 }
8186
8187 return 1;
8188}
8189
a4f6c312 8190/* Return 1 for an PARALLEL suitable for stmw. */
9ebbca7d
GK
8191
8192int
a2369ed3 8193stmw_operation (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
9ebbca7d
GK
8194{
8195 int count = XVECLEN (op, 0);
e2c953b6 8196 unsigned int src_regno;
9ebbca7d 8197 rtx dest_addr;
e2c953b6 8198 unsigned int base_regno;
9ebbca7d
GK
8199 HOST_WIDE_INT offset;
8200 int i;
8201
8202 /* Perform a quick check so we don't blow up below. */
8203 if (count <= 1
8204 || GET_CODE (XVECEXP (op, 0, 0)) != SET
8205 || GET_CODE (SET_DEST (XVECEXP (op, 0, 0))) != MEM
8206 || GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != REG)
8207 return 0;
8208
8209 src_regno = REGNO (SET_SRC (XVECEXP (op, 0, 0)));
8210 dest_addr = XEXP (SET_DEST (XVECEXP (op, 0, 0)), 0);
8211
8212 if (src_regno > 31
e2c953b6 8213 || count != 32 - (int) src_regno)
9ebbca7d
GK
8214 return 0;
8215
4d588c14 8216 if (legitimate_indirect_address_p (dest_addr, 0))
9ebbca7d
GK
8217 {
8218 offset = 0;
8219 base_regno = REGNO (dest_addr);
8220 if (base_regno == 0)
8221 return 0;
8222 }
4d588c14 8223 else if (legitimate_offset_address_p (SImode, dest_addr, 0))
9ebbca7d
GK
8224 {
8225 offset = INTVAL (XEXP (dest_addr, 1));
8226 base_regno = REGNO (XEXP (dest_addr, 0));
8227 }
8228 else
8229 return 0;
8230
8231 for (i = 0; i < count; i++)
8232 {
8233 rtx elt = XVECEXP (op, 0, i);
8234 rtx newaddr;
8235 rtx addr_reg;
8236 HOST_WIDE_INT newoffset;
8237
8238 if (GET_CODE (elt) != SET
8239 || GET_CODE (SET_SRC (elt)) != REG
8240 || GET_MODE (SET_SRC (elt)) != SImode
8241 || REGNO (SET_SRC (elt)) != src_regno + i
8242 || GET_CODE (SET_DEST (elt)) != MEM
8243 || GET_MODE (SET_DEST (elt)) != SImode)
8244 return 0;
8245 newaddr = XEXP (SET_DEST (elt), 0);
4d588c14 8246 if (legitimate_indirect_address_p (newaddr, 0))
9ebbca7d
GK
8247 {
8248 newoffset = 0;
8249 addr_reg = newaddr;
8250 }
4d588c14 8251 else if (legitimate_offset_address_p (SImode, newaddr, 0))
9ebbca7d
GK
8252 {
8253 addr_reg = XEXP (newaddr, 0);
8254 newoffset = INTVAL (XEXP (newaddr, 1));
8255 }
8256 else
8257 return 0;
8258 if (REGNO (addr_reg) != base_regno
8259 || newoffset != offset + 4 * i)
8260 return 0;
8261 }
8262
8263 return 1;
8264}
9878760c 8265\f
a4f6c312
SS
8266/* A validation routine: say whether CODE, a condition code, and MODE
8267 match. The other alternatives either don't make sense or should
8268 never be generated. */
39a10a29 8269
39a10a29 8270static void
a2369ed3 8271validate_condition_mode (enum rtx_code code, enum machine_mode mode)
39a10a29 8272{
ec8e098d
PB
8273 if ((GET_RTX_CLASS (code) != RTX_COMPARE
8274 && GET_RTX_CLASS (code) != RTX_COMM_COMPARE)
39a10a29
GK
8275 || GET_MODE_CLASS (mode) != MODE_CC)
8276 abort ();
8277
8278 /* These don't make sense. */
8279 if ((code == GT || code == LT || code == GE || code == LE)
8280 && mode == CCUNSmode)
8281 abort ();
8282
8283 if ((code == GTU || code == LTU || code == GEU || code == LEU)
8284 && mode != CCUNSmode)
8285 abort ();
8286
8287 if (mode != CCFPmode
8288 && (code == ORDERED || code == UNORDERED
8289 || code == UNEQ || code == LTGT
8290 || code == UNGT || code == UNLT
8291 || code == UNGE || code == UNLE))
a4f6c312 8292 abort ();
39a10a29 8293
de6c5979 8294 /* These should never be generated except for
bc9ec0e0 8295 flag_finite_math_only. */
39a10a29 8296 if (mode == CCFPmode
ad72b533 8297 && ! flag_finite_math_only
39a10a29
GK
8298 && (code == LE || code == GE
8299 || code == UNEQ || code == LTGT
8300 || code == UNGT || code == UNLT))
8301 abort ();
8302
8303 /* These are invalid; the information is not there. */
8304 if (mode == CCEQmode
8305 && code != EQ && code != NE)
8306 abort ();
8307}
8308
9878760c
RK
8309/* Return 1 if OP is a comparison operation that is valid for a branch insn.
8310 We only check the opcode against the mode of the CC value here. */
8311
8312int
a2369ed3 8313branch_comparison_operator (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
9878760c
RK
8314{
8315 enum rtx_code code = GET_CODE (op);
8316 enum machine_mode cc_mode;
8317
ec8e098d 8318 if (!COMPARISON_P (op))
9878760c
RK
8319 return 0;
8320
8321 cc_mode = GET_MODE (XEXP (op, 0));
8322 if (GET_MODE_CLASS (cc_mode) != MODE_CC)
8323 return 0;
8324
39a10a29 8325 validate_condition_mode (code, cc_mode);
9878760c 8326
39a10a29
GK
8327 return 1;
8328}
8329
8330/* Return 1 if OP is a comparison operation that is valid for a branch
8331 insn and which is true if the corresponding bit in the CC register
8332 is set. */
8333
8334int
a2369ed3 8335branch_positive_comparison_operator (rtx op, enum machine_mode mode)
39a10a29
GK
8336{
8337 enum rtx_code code;
8338
8daf2e65 8339 if (! branch_comparison_operator (op, mode))
9878760c
RK
8340 return 0;
8341
39a10a29
GK
8342 code = GET_CODE (op);
8343 return (code == EQ || code == LT || code == GT
993f19a8 8344 || (TARGET_E500 && TARGET_HARD_FLOAT && !TARGET_FPRS && code == NE)
39a10a29
GK
8345 || code == LTU || code == GTU
8346 || code == UNORDERED);
9878760c
RK
8347}
8348
b7053a3f
GK
8349/* Return 1 if OP is a comparison operation that is valid for an scc
8350 insn: it must be a positive comparison. */
9878760c
RK
8351
8352int
a2369ed3 8353scc_comparison_operator (rtx op, enum machine_mode mode)
9878760c 8354{
b7053a3f 8355 return branch_positive_comparison_operator (op, mode);
9878760c 8356}
e0cd0770
JC
8357
8358int
a2369ed3 8359trap_comparison_operator (rtx op, enum machine_mode mode)
e0cd0770
JC
8360{
8361 if (mode != VOIDmode && mode != GET_MODE (op))
8362 return 0;
ec8e098d 8363 return COMPARISON_P (op);
e0cd0770 8364}
dfbdccdb
GK
8365
8366int
a2369ed3 8367boolean_operator (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
dfbdccdb
GK
8368{
8369 enum rtx_code code = GET_CODE (op);
8370 return (code == AND || code == IOR || code == XOR);
8371}
1d328b19
GK
8372
8373int
a2369ed3 8374boolean_or_operator (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1d328b19
GK
8375{
8376 enum rtx_code code = GET_CODE (op);
8377 return (code == IOR || code == XOR);
8378}
50a0b056
GK
8379
8380int
a2369ed3 8381min_max_operator (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
50a0b056
GK
8382{
8383 enum rtx_code code = GET_CODE (op);
8384 return (code == SMIN || code == SMAX || code == UMIN || code == UMAX);
8385}
9878760c
RK
8386\f
8387/* Return 1 if ANDOP is a mask that has no bits on that are not in the
8388 mask required to convert the result of a rotate insn into a shift
b1765bde 8389 left insn of SHIFTOP bits. Both are known to be SImode CONST_INT. */
9878760c
RK
8390
8391int
a2369ed3 8392includes_lshift_p (rtx shiftop, rtx andop)
9878760c 8393{
e2c953b6
DE
8394 unsigned HOST_WIDE_INT shift_mask = ~(unsigned HOST_WIDE_INT) 0;
8395
8396 shift_mask <<= INTVAL (shiftop);
9878760c 8397
b1765bde 8398 return (INTVAL (andop) & 0xffffffff & ~shift_mask) == 0;
9878760c
RK
8399}
8400
8401/* Similar, but for right shift. */
8402
8403int
a2369ed3 8404includes_rshift_p (rtx shiftop, rtx andop)
9878760c 8405{
a7653a2c 8406 unsigned HOST_WIDE_INT shift_mask = ~(unsigned HOST_WIDE_INT) 0;
9878760c
RK
8407
8408 shift_mask >>= INTVAL (shiftop);
8409
b1765bde 8410 return (INTVAL (andop) & 0xffffffff & ~shift_mask) == 0;
e2c953b6
DE
8411}
8412
c5059423
AM
8413/* Return 1 if ANDOP is a mask suitable for use with an rldic insn
8414 to perform a left shift. It must have exactly SHIFTOP least
b6d08ca1 8415 significant 0's, then one or more 1's, then zero or more 0's. */
e2c953b6
DE
8416
8417int
a2369ed3 8418includes_rldic_lshift_p (rtx shiftop, rtx andop)
e2c953b6 8419{
c5059423
AM
8420 if (GET_CODE (andop) == CONST_INT)
8421 {
02071907 8422 HOST_WIDE_INT c, lsb, shift_mask;
e2c953b6 8423
c5059423 8424 c = INTVAL (andop);
02071907 8425 if (c == 0 || c == ~0)
c5059423 8426 return 0;
e2c953b6 8427
02071907 8428 shift_mask = ~0;
c5059423
AM
8429 shift_mask <<= INTVAL (shiftop);
8430
b6d08ca1 8431 /* Find the least significant one bit. */
c5059423
AM
8432 lsb = c & -c;
8433
8434 /* It must coincide with the LSB of the shift mask. */
8435 if (-lsb != shift_mask)
8436 return 0;
e2c953b6 8437
c5059423
AM
8438 /* Invert to look for the next transition (if any). */
8439 c = ~c;
8440
8441 /* Remove the low group of ones (originally low group of zeros). */
8442 c &= -lsb;
8443
8444 /* Again find the lsb, and check we have all 1's above. */
8445 lsb = c & -c;
8446 return c == -lsb;
8447 }
8448 else if (GET_CODE (andop) == CONST_DOUBLE
8449 && (GET_MODE (andop) == VOIDmode || GET_MODE (andop) == DImode))
8450 {
02071907
AM
8451 HOST_WIDE_INT low, high, lsb;
8452 HOST_WIDE_INT shift_mask_low, shift_mask_high;
c5059423
AM
8453
8454 low = CONST_DOUBLE_LOW (andop);
8455 if (HOST_BITS_PER_WIDE_INT < 64)
8456 high = CONST_DOUBLE_HIGH (andop);
8457
8458 if ((low == 0 && (HOST_BITS_PER_WIDE_INT >= 64 || high == 0))
02071907 8459 || (low == ~0 && (HOST_BITS_PER_WIDE_INT >= 64 || high == ~0)))
c5059423
AM
8460 return 0;
8461
8462 if (HOST_BITS_PER_WIDE_INT < 64 && low == 0)
8463 {
02071907 8464 shift_mask_high = ~0;
c5059423
AM
8465 if (INTVAL (shiftop) > 32)
8466 shift_mask_high <<= INTVAL (shiftop) - 32;
8467
8468 lsb = high & -high;
8469
8470 if (-lsb != shift_mask_high || INTVAL (shiftop) < 32)
8471 return 0;
8472
8473 high = ~high;
8474 high &= -lsb;
8475
8476 lsb = high & -high;
8477 return high == -lsb;
8478 }
8479
02071907 8480 shift_mask_low = ~0;
c5059423
AM
8481 shift_mask_low <<= INTVAL (shiftop);
8482
8483 lsb = low & -low;
8484
8485 if (-lsb != shift_mask_low)
8486 return 0;
8487
8488 if (HOST_BITS_PER_WIDE_INT < 64)
8489 high = ~high;
8490 low = ~low;
8491 low &= -lsb;
8492
8493 if (HOST_BITS_PER_WIDE_INT < 64 && low == 0)
8494 {
8495 lsb = high & -high;
8496 return high == -lsb;
8497 }
8498
8499 lsb = low & -low;
8500 return low == -lsb && (HOST_BITS_PER_WIDE_INT >= 64 || high == ~0);
8501 }
8502 else
8503 return 0;
8504}
e2c953b6 8505
c5059423
AM
8506/* Return 1 if ANDOP is a mask suitable for use with an rldicr insn
8507 to perform a left shift. It must have SHIFTOP or more least
c1207243 8508 significant 0's, with the remainder of the word 1's. */
e2c953b6 8509
c5059423 8510int
a2369ed3 8511includes_rldicr_lshift_p (rtx shiftop, rtx andop)
c5059423 8512{
e2c953b6 8513 if (GET_CODE (andop) == CONST_INT)
c5059423 8514 {
02071907 8515 HOST_WIDE_INT c, lsb, shift_mask;
c5059423 8516
02071907 8517 shift_mask = ~0;
c5059423
AM
8518 shift_mask <<= INTVAL (shiftop);
8519 c = INTVAL (andop);
8520
c1207243 8521 /* Find the least significant one bit. */
c5059423
AM
8522 lsb = c & -c;
8523
8524 /* It must be covered by the shift mask.
a4f6c312 8525 This test also rejects c == 0. */
c5059423
AM
8526 if ((lsb & shift_mask) == 0)
8527 return 0;
8528
8529 /* Check we have all 1's above the transition, and reject all 1's. */
8530 return c == -lsb && lsb != 1;
8531 }
8532 else if (GET_CODE (andop) == CONST_DOUBLE
8533 && (GET_MODE (andop) == VOIDmode || GET_MODE (andop) == DImode))
8534 {
02071907 8535 HOST_WIDE_INT low, lsb, shift_mask_low;
c5059423
AM
8536
8537 low = CONST_DOUBLE_LOW (andop);
8538
8539 if (HOST_BITS_PER_WIDE_INT < 64)
8540 {
02071907 8541 HOST_WIDE_INT high, shift_mask_high;
c5059423
AM
8542
8543 high = CONST_DOUBLE_HIGH (andop);
8544
8545 if (low == 0)
8546 {
02071907 8547 shift_mask_high = ~0;
c5059423
AM
8548 if (INTVAL (shiftop) > 32)
8549 shift_mask_high <<= INTVAL (shiftop) - 32;
8550
8551 lsb = high & -high;
8552
8553 if ((lsb & shift_mask_high) == 0)
8554 return 0;
8555
8556 return high == -lsb;
8557 }
8558 if (high != ~0)
8559 return 0;
8560 }
8561
02071907 8562 shift_mask_low = ~0;
c5059423
AM
8563 shift_mask_low <<= INTVAL (shiftop);
8564
8565 lsb = low & -low;
8566
8567 if ((lsb & shift_mask_low) == 0)
8568 return 0;
8569
8570 return low == -lsb && lsb != 1;
8571 }
e2c953b6 8572 else
c5059423 8573 return 0;
9878760c 8574}
35068b43
RK
8575
8576/* Return 1 if REGNO (reg1) == REGNO (reg2) - 1 making them candidates
8577 for lfq and stfq insns.
8578
8579 Note reg1 and reg2 *must* be hard registers. To be sure we will
8580 abort if we are passed pseudo registers. */
8581
8582int
a2369ed3 8583registers_ok_for_quad_peep (rtx reg1, rtx reg2)
35068b43
RK
8584{
8585 /* We might have been passed a SUBREG. */
8586 if (GET_CODE (reg1) != REG || GET_CODE (reg2) != REG)
8587 return 0;
8588
8589 return (REGNO (reg1) == REGNO (reg2) - 1);
8590}
8591
a4f6c312
SS
8592/* Return 1 if addr1 and addr2 are suitable for lfq or stfq insn.
8593 addr1 and addr2 must be in consecutive memory locations
8594 (addr2 == addr1 + 8). */
35068b43
RK
8595
8596int
a2369ed3 8597addrs_ok_for_quad_peep (rtx addr1, rtx addr2)
35068b43 8598{
e2c953b6 8599 unsigned int reg1;
35068b43
RK
8600 int offset1;
8601
8602 /* Extract an offset (if used) from the first addr. */
8603 if (GET_CODE (addr1) == PLUS)
8604 {
8605 /* If not a REG, return zero. */
8606 if (GET_CODE (XEXP (addr1, 0)) != REG)
8607 return 0;
8608 else
8609 {
8610 reg1 = REGNO (XEXP (addr1, 0));
8611 /* The offset must be constant! */
8612 if (GET_CODE (XEXP (addr1, 1)) != CONST_INT)
8613 return 0;
8614 offset1 = INTVAL (XEXP (addr1, 1));
8615 }
8616 }
8617 else if (GET_CODE (addr1) != REG)
8618 return 0;
8619 else
8620 {
8621 reg1 = REGNO (addr1);
8622 /* This was a simple (mem (reg)) expression. Offset is 0. */
8623 offset1 = 0;
8624 }
8625
a2369ed3 8626 /* Make sure the second address is a (mem (plus (reg) (const_int)))
0f6937fe
AM
8627 or if it is (mem (reg)) then make sure that offset1 is -8 and the same
8628 register as addr1. */
984e25ac 8629 if (offset1 == -8 && GET_CODE (addr2) == REG && reg1 == REGNO (addr2))
0f6937fe 8630 return 1;
35068b43
RK
8631 if (GET_CODE (addr2) != PLUS)
8632 return 0;
8633
8634 if (GET_CODE (XEXP (addr2, 0)) != REG
8635 || GET_CODE (XEXP (addr2, 1)) != CONST_INT)
8636 return 0;
8637
8638 if (reg1 != REGNO (XEXP (addr2, 0)))
8639 return 0;
8640
8641 /* The offset for the second addr must be 8 more than the first addr. */
8642 if (INTVAL (XEXP (addr2, 1)) != offset1 + 8)
8643 return 0;
8644
8645 /* All the tests passed. addr1 and addr2 are valid for lfq or stfq
8646 instructions. */
8647 return 1;
8648}
9878760c
RK
8649\f
8650/* Return the register class of a scratch register needed to copy IN into
8651 or out of a register in CLASS in MODE. If it can be done directly,
8652 NO_REGS is returned. */
8653
8654enum reg_class
a2369ed3 8655secondary_reload_class (enum reg_class class,
a9baceb1
GK
8656 enum machine_mode mode ATTRIBUTE_UNUSED,
8657 rtx in)
9878760c 8658{
5accd822 8659 int regno;
9878760c 8660
ab82a49f
AP
8661 if (TARGET_ELF || (DEFAULT_ABI == ABI_DARWIN
8662#if TARGET_MACHO
8663 && MACHOPIC_INDIRECT
8664#endif
8665 ))
46fad5b7
DJ
8666 {
8667 /* We cannot copy a symbolic operand directly into anything
8668 other than BASE_REGS for TARGET_ELF. So indicate that a
8669 register from BASE_REGS is needed as an intermediate
8670 register.
8671
8672 On Darwin, pic addresses require a load from memory, which
8673 needs a base register. */
8674 if (class != BASE_REGS
8675 && (GET_CODE (in) == SYMBOL_REF
8676 || GET_CODE (in) == HIGH
8677 || GET_CODE (in) == LABEL_REF
8678 || GET_CODE (in) == CONST))
8679 return BASE_REGS;
8680 }
e7b7998a 8681
5accd822
DE
8682 if (GET_CODE (in) == REG)
8683 {
8684 regno = REGNO (in);
8685 if (regno >= FIRST_PSEUDO_REGISTER)
8686 {
8687 regno = true_regnum (in);
8688 if (regno >= FIRST_PSEUDO_REGISTER)
8689 regno = -1;
8690 }
8691 }
8692 else if (GET_CODE (in) == SUBREG)
8693 {
8694 regno = true_regnum (in);
8695 if (regno >= FIRST_PSEUDO_REGISTER)
8696 regno = -1;
8697 }
8698 else
8699 regno = -1;
8700
9878760c
RK
8701 /* We can place anything into GENERAL_REGS and can put GENERAL_REGS
8702 into anything. */
8703 if (class == GENERAL_REGS || class == BASE_REGS
8704 || (regno >= 0 && INT_REGNO_P (regno)))
8705 return NO_REGS;
8706
8707 /* Constants, memory, and FP registers can go into FP registers. */
8708 if ((regno == -1 || FP_REGNO_P (regno))
8709 && (class == FLOAT_REGS || class == NON_SPECIAL_REGS))
8710 return NO_REGS;
8711
0ac081f6
AH
8712 /* Memory, and AltiVec registers can go into AltiVec registers. */
8713 if ((regno == -1 || ALTIVEC_REGNO_P (regno))
8714 && class == ALTIVEC_REGS)
8715 return NO_REGS;
8716
9878760c
RK
8717 /* We can copy among the CR registers. */
8718 if ((class == CR_REGS || class == CR0_REGS)
8719 && regno >= 0 && CR_REGNO_P (regno))
8720 return NO_REGS;
8721
8722 /* Otherwise, we need GENERAL_REGS. */
8723 return GENERAL_REGS;
8724}
8725\f
8726/* Given a comparison operation, return the bit number in CCR to test. We
8727 know this is a valid comparison.
8728
8729 SCC_P is 1 if this is for an scc. That means that %D will have been
8730 used instead of %C, so the bits will be in different places.
8731
b4ac57ab 8732 Return -1 if OP isn't a valid comparison for some reason. */
9878760c
RK
8733
8734int
a2369ed3 8735ccr_bit (rtx op, int scc_p)
9878760c
RK
8736{
8737 enum rtx_code code = GET_CODE (op);
8738 enum machine_mode cc_mode;
8739 int cc_regnum;
8740 int base_bit;
9ebbca7d 8741 rtx reg;
9878760c 8742
ec8e098d 8743 if (!COMPARISON_P (op))
9878760c
RK
8744 return -1;
8745
9ebbca7d
GK
8746 reg = XEXP (op, 0);
8747
8748 if (GET_CODE (reg) != REG
8749 || ! CR_REGNO_P (REGNO (reg)))
8750 abort ();
8751
8752 cc_mode = GET_MODE (reg);
8753 cc_regnum = REGNO (reg);
8754 base_bit = 4 * (cc_regnum - CR0_REGNO);
9878760c 8755
39a10a29 8756 validate_condition_mode (code, cc_mode);
c5defebb 8757
b7053a3f
GK
8758 /* When generating a sCOND operation, only positive conditions are
8759 allowed. */
8760 if (scc_p && code != EQ && code != GT && code != LT && code != UNORDERED
8761 && code != GTU && code != LTU)
8762 abort ();
8763
9878760c
RK
8764 switch (code)
8765 {
8766 case NE:
993f19a8
AH
8767 if (TARGET_E500 && !TARGET_FPRS
8768 && TARGET_HARD_FLOAT && cc_mode == CCFPmode)
a3170dc6 8769 return base_bit + 1;
9878760c
RK
8770 return scc_p ? base_bit + 3 : base_bit + 2;
8771 case EQ:
993f19a8
AH
8772 if (TARGET_E500 && !TARGET_FPRS
8773 && TARGET_HARD_FLOAT && cc_mode == CCFPmode)
a3170dc6 8774 return base_bit + 1;
9878760c 8775 return base_bit + 2;
1c882ea4 8776 case GT: case GTU: case UNLE:
9878760c 8777 return base_bit + 1;
1c882ea4 8778 case LT: case LTU: case UNGE:
9878760c 8779 return base_bit;
1c882ea4
GK
8780 case ORDERED: case UNORDERED:
8781 return base_bit + 3;
9878760c
RK
8782
8783 case GE: case GEU:
39a10a29 8784 /* If scc, we will have done a cror to put the bit in the
9878760c
RK
8785 unordered position. So test that bit. For integer, this is ! LT
8786 unless this is an scc insn. */
39a10a29 8787 return scc_p ? base_bit + 3 : base_bit;
9878760c
RK
8788
8789 case LE: case LEU:
39a10a29 8790 return scc_p ? base_bit + 3 : base_bit + 1;
1c882ea4 8791
9878760c
RK
8792 default:
8793 abort ();
8794 }
8795}
1ff7789b 8796\f
8d30c4ee 8797/* Return the GOT register. */
1ff7789b
MM
8798
8799struct rtx_def *
a2369ed3 8800rs6000_got_register (rtx value ATTRIBUTE_UNUSED)
1ff7789b 8801{
a4f6c312
SS
8802 /* The second flow pass currently (June 1999) can't update
8803 regs_ever_live without disturbing other parts of the compiler, so
8804 update it here to make the prolog/epilogue code happy. */
1db02437
FS
8805 if (no_new_pseudos && ! regs_ever_live[RS6000_PIC_OFFSET_TABLE_REGNUM])
8806 regs_ever_live[RS6000_PIC_OFFSET_TABLE_REGNUM] = 1;
1ff7789b 8807
8d30c4ee 8808 current_function_uses_pic_offset_table = 1;
3cb999d8 8809
1ff7789b
MM
8810 return pic_offset_table_rtx;
8811}
a7df97e6 8812\f
e2500fed
GK
8813/* Function to init struct machine_function.
8814 This will be called, via a pointer variable,
8815 from push_function_context. */
a7df97e6 8816
e2500fed 8817static struct machine_function *
863d938c 8818rs6000_init_machine_status (void)
a7df97e6 8819{
e2500fed 8820 return ggc_alloc_cleared (sizeof (machine_function));
a7df97e6 8821}
9878760c 8822\f
0ba1b2ff
AM
8823/* These macros test for integers and extract the low-order bits. */
8824#define INT_P(X) \
8825((GET_CODE (X) == CONST_INT || GET_CODE (X) == CONST_DOUBLE) \
8826 && GET_MODE (X) == VOIDmode)
8827
8828#define INT_LOWPART(X) \
8829 (GET_CODE (X) == CONST_INT ? INTVAL (X) : CONST_DOUBLE_LOW (X))
8830
8831int
a2369ed3 8832extract_MB (rtx op)
0ba1b2ff
AM
8833{
8834 int i;
8835 unsigned long val = INT_LOWPART (op);
8836
8837 /* If the high bit is zero, the value is the first 1 bit we find
8838 from the left. */
8839 if ((val & 0x80000000) == 0)
8840 {
8841 if ((val & 0xffffffff) == 0)
8842 abort ();
8843
8844 i = 1;
8845 while (((val <<= 1) & 0x80000000) == 0)
8846 ++i;
8847 return i;
8848 }
8849
8850 /* If the high bit is set and the low bit is not, or the mask is all
8851 1's, the value is zero. */
8852 if ((val & 1) == 0 || (val & 0xffffffff) == 0xffffffff)
8853 return 0;
8854
8855 /* Otherwise we have a wrap-around mask. Look for the first 0 bit
8856 from the right. */
8857 i = 31;
8858 while (((val >>= 1) & 1) != 0)
8859 --i;
8860
8861 return i;
8862}
8863
8864int
a2369ed3 8865extract_ME (rtx op)
0ba1b2ff
AM
8866{
8867 int i;
8868 unsigned long val = INT_LOWPART (op);
8869
8870 /* If the low bit is zero, the value is the first 1 bit we find from
8871 the right. */
8872 if ((val & 1) == 0)
8873 {
8874 if ((val & 0xffffffff) == 0)
8875 abort ();
8876
8877 i = 30;
8878 while (((val >>= 1) & 1) == 0)
8879 --i;
8880
8881 return i;
8882 }
8883
8884 /* If the low bit is set and the high bit is not, or the mask is all
8885 1's, the value is 31. */
8886 if ((val & 0x80000000) == 0 || (val & 0xffffffff) == 0xffffffff)
8887 return 31;
8888
8889 /* Otherwise we have a wrap-around mask. Look for the first 0 bit
8890 from the left. */
8891 i = 0;
8892 while (((val <<= 1) & 0x80000000) != 0)
8893 ++i;
8894
8895 return i;
8896}
8897
c4501e62
JJ
8898/* Locate some local-dynamic symbol still in use by this function
8899 so that we can print its name in some tls_ld pattern. */
8900
8901static const char *
863d938c 8902rs6000_get_some_local_dynamic_name (void)
c4501e62
JJ
8903{
8904 rtx insn;
8905
8906 if (cfun->machine->some_ld_name)
8907 return cfun->machine->some_ld_name;
8908
8909 for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
8910 if (INSN_P (insn)
8911 && for_each_rtx (&PATTERN (insn),
8912 rs6000_get_some_local_dynamic_name_1, 0))
8913 return cfun->machine->some_ld_name;
8914
8915 abort ();
8916}
8917
8918/* Helper function for rs6000_get_some_local_dynamic_name. */
8919
8920static int
a2369ed3 8921rs6000_get_some_local_dynamic_name_1 (rtx *px, void *data ATTRIBUTE_UNUSED)
c4501e62
JJ
8922{
8923 rtx x = *px;
8924
8925 if (GET_CODE (x) == SYMBOL_REF)
8926 {
8927 const char *str = XSTR (x, 0);
8928 if (SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_DYNAMIC)
8929 {
8930 cfun->machine->some_ld_name = str;
8931 return 1;
8932 }
8933 }
8934
8935 return 0;
8936}
8937
9878760c
RK
8938/* Print an operand. Recognize special options, documented below. */
8939
38c1f2d7 8940#if TARGET_ELF
d9407988 8941#define SMALL_DATA_RELOC ((rs6000_sdata == SDATA_EABI) ? "sda21" : "sdarel")
8fbd2dc7 8942#define SMALL_DATA_REG ((rs6000_sdata == SDATA_EABI) ? 0 : 13)
ba5e43aa
MM
8943#else
8944#define SMALL_DATA_RELOC "sda21"
8fbd2dc7 8945#define SMALL_DATA_REG 0
ba5e43aa
MM
8946#endif
8947
9878760c 8948void
a2369ed3 8949print_operand (FILE *file, rtx x, int code)
9878760c
RK
8950{
8951 int i;
a260abc9 8952 HOST_WIDE_INT val;
0ba1b2ff 8953 unsigned HOST_WIDE_INT uval;
9878760c
RK
8954
8955 switch (code)
8956 {
a8b3aeda 8957 case '.':
a85d226b
RK
8958 /* Write out an instruction after the call which may be replaced
8959 with glue code by the loader. This depends on the AIX version. */
8960 asm_fprintf (file, RS6000_CALL_GLUE);
a8b3aeda
RK
8961 return;
8962
81eace42
GK
8963 /* %a is output_address. */
8964
9854d9ed
RK
8965 case 'A':
8966 /* If X is a constant integer whose low-order 5 bits are zero,
8967 write 'l'. Otherwise, write 'r'. This is a kludge to fix a bug
76229ac8 8968 in the AIX assembler where "sri" with a zero shift count
20e26713 8969 writes a trash instruction. */
9854d9ed 8970 if (GET_CODE (x) == CONST_INT && (INTVAL (x) & 31) == 0)
76229ac8 8971 putc ('l', file);
9854d9ed 8972 else
76229ac8 8973 putc ('r', file);
9854d9ed
RK
8974 return;
8975
8976 case 'b':
e2c953b6
DE
8977 /* If constant, low-order 16 bits of constant, unsigned.
8978 Otherwise, write normally. */
8979 if (INT_P (x))
8980 fprintf (file, HOST_WIDE_INT_PRINT_DEC, INT_LOWPART (x) & 0xffff);
8981 else
8982 print_operand (file, x, 0);
cad12a8d
RK
8983 return;
8984
a260abc9
DE
8985 case 'B':
8986 /* If the low-order bit is zero, write 'r'; otherwise, write 'l'
8987 for 64-bit mask direction. */
296b8152 8988 putc (((INT_LOWPART(x) & 1) == 0 ? 'r' : 'l'), file);
a238cd8b 8989 return;
a260abc9 8990
81eace42
GK
8991 /* %c is output_addr_const if a CONSTANT_ADDRESS_P, otherwise
8992 output_operand. */
8993
9854d9ed 8994 case 'E':
39a10a29 8995 /* X is a CR register. Print the number of the EQ bit of the CR */
9854d9ed
RK
8996 if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
8997 output_operand_lossage ("invalid %%E value");
78fbdbf7 8998 else
39a10a29 8999 fprintf (file, "%d", 4 * (REGNO (x) - CR0_REGNO) + 2);
a85d226b 9000 return;
9854d9ed
RK
9001
9002 case 'f':
9003 /* X is a CR register. Print the shift count needed to move it
9004 to the high-order four bits. */
9005 if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
9006 output_operand_lossage ("invalid %%f value");
9007 else
9ebbca7d 9008 fprintf (file, "%d", 4 * (REGNO (x) - CR0_REGNO));
9854d9ed
RK
9009 return;
9010
9011 case 'F':
9012 /* Similar, but print the count for the rotate in the opposite
9013 direction. */
9014 if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
9015 output_operand_lossage ("invalid %%F value");
9016 else
9ebbca7d 9017 fprintf (file, "%d", 32 - 4 * (REGNO (x) - CR0_REGNO));
9854d9ed
RK
9018 return;
9019
9020 case 'G':
9021 /* X is a constant integer. If it is negative, print "m",
43aa4e05 9022 otherwise print "z". This is to make an aze or ame insn. */
9854d9ed
RK
9023 if (GET_CODE (x) != CONST_INT)
9024 output_operand_lossage ("invalid %%G value");
9025 else if (INTVAL (x) >= 0)
76229ac8 9026 putc ('z', file);
9854d9ed 9027 else
76229ac8 9028 putc ('m', file);
9854d9ed 9029 return;
e2c953b6 9030
9878760c 9031 case 'h':
a4f6c312
SS
9032 /* If constant, output low-order five bits. Otherwise, write
9033 normally. */
9878760c 9034 if (INT_P (x))
5f59ecb7 9035 fprintf (file, HOST_WIDE_INT_PRINT_DEC, INT_LOWPART (x) & 31);
9878760c
RK
9036 else
9037 print_operand (file, x, 0);
9038 return;
9039
64305719 9040 case 'H':
a4f6c312
SS
9041 /* If constant, output low-order six bits. Otherwise, write
9042 normally. */
64305719 9043 if (INT_P (x))
5f59ecb7 9044 fprintf (file, HOST_WIDE_INT_PRINT_DEC, INT_LOWPART (x) & 63);
64305719
DE
9045 else
9046 print_operand (file, x, 0);
9047 return;
9048
9854d9ed
RK
9049 case 'I':
9050 /* Print `i' if this is a constant, else nothing. */
9878760c 9051 if (INT_P (x))
76229ac8 9052 putc ('i', file);
9878760c
RK
9053 return;
9054
9854d9ed
RK
9055 case 'j':
9056 /* Write the bit number in CCR for jump. */
9057 i = ccr_bit (x, 0);
9058 if (i == -1)
9059 output_operand_lossage ("invalid %%j code");
9878760c 9060 else
9854d9ed 9061 fprintf (file, "%d", i);
9878760c
RK
9062 return;
9063
9854d9ed
RK
9064 case 'J':
9065 /* Similar, but add one for shift count in rlinm for scc and pass
9066 scc flag to `ccr_bit'. */
9067 i = ccr_bit (x, 1);
9068 if (i == -1)
9069 output_operand_lossage ("invalid %%J code");
9070 else
a0466a68
RK
9071 /* If we want bit 31, write a shift count of zero, not 32. */
9072 fprintf (file, "%d", i == 31 ? 0 : i + 1);
9878760c
RK
9073 return;
9074
9854d9ed
RK
9075 case 'k':
9076 /* X must be a constant. Write the 1's complement of the
9077 constant. */
9878760c 9078 if (! INT_P (x))
9854d9ed 9079 output_operand_lossage ("invalid %%k value");
e2c953b6
DE
9080 else
9081 fprintf (file, HOST_WIDE_INT_PRINT_DEC, ~ INT_LOWPART (x));
9878760c
RK
9082 return;
9083
81eace42 9084 case 'K':
9ebbca7d
GK
9085 /* X must be a symbolic constant on ELF. Write an
9086 expression suitable for an 'addi' that adds in the low 16
9087 bits of the MEM. */
9088 if (GET_CODE (x) != CONST)
9089 {
9090 print_operand_address (file, x);
9091 fputs ("@l", file);
9092 }
9093 else
9094 {
9095 if (GET_CODE (XEXP (x, 0)) != PLUS
9096 || (GET_CODE (XEXP (XEXP (x, 0), 0)) != SYMBOL_REF
9097 && GET_CODE (XEXP (XEXP (x, 0), 0)) != LABEL_REF)
9098 || GET_CODE (XEXP (XEXP (x, 0), 1)) != CONST_INT)
53cd5d6c 9099 output_operand_lossage ("invalid %%K value");
9ebbca7d
GK
9100 print_operand_address (file, XEXP (XEXP (x, 0), 0));
9101 fputs ("@l", file);
ed8d2920
MM
9102 /* For GNU as, there must be a non-alphanumeric character
9103 between 'l' and the number. The '-' is added by
9104 print_operand() already. */
9105 if (INTVAL (XEXP (XEXP (x, 0), 1)) >= 0)
9106 fputs ("+", file);
9ebbca7d
GK
9107 print_operand (file, XEXP (XEXP (x, 0), 1), 0);
9108 }
81eace42
GK
9109 return;
9110
9111 /* %l is output_asm_label. */
9ebbca7d 9112
9854d9ed
RK
9113 case 'L':
9114 /* Write second word of DImode or DFmode reference. Works on register
9115 or non-indexed memory only. */
9116 if (GET_CODE (x) == REG)
5ebfb2ba 9117 fprintf (file, "%s", reg_names[REGNO (x) + 1]);
9854d9ed
RK
9118 else if (GET_CODE (x) == MEM)
9119 {
9120 /* Handle possible auto-increment. Since it is pre-increment and
1427100a 9121 we have already done it, we can just use an offset of word. */
9854d9ed
RK
9122 if (GET_CODE (XEXP (x, 0)) == PRE_INC
9123 || GET_CODE (XEXP (x, 0)) == PRE_DEC)
ed8908e7
RK
9124 output_address (plus_constant (XEXP (XEXP (x, 0), 0),
9125 UNITS_PER_WORD));
9854d9ed 9126 else
d7624dc0
RK
9127 output_address (XEXP (adjust_address_nv (x, SImode,
9128 UNITS_PER_WORD),
9129 0));
ed8908e7 9130
ba5e43aa 9131 if (small_data_operand (x, GET_MODE (x)))
8fbd2dc7
MM
9132 fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
9133 reg_names[SMALL_DATA_REG]);
9854d9ed 9134 }
9878760c 9135 return;
9854d9ed 9136
9878760c
RK
9137 case 'm':
9138 /* MB value for a mask operand. */
b1765bde 9139 if (! mask_operand (x, SImode))
9878760c
RK
9140 output_operand_lossage ("invalid %%m value");
9141
0ba1b2ff 9142 fprintf (file, "%d", extract_MB (x));
9878760c
RK
9143 return;
9144
9145 case 'M':
9146 /* ME value for a mask operand. */
b1765bde 9147 if (! mask_operand (x, SImode))
a260abc9 9148 output_operand_lossage ("invalid %%M value");
9878760c 9149
0ba1b2ff 9150 fprintf (file, "%d", extract_ME (x));
9878760c
RK
9151 return;
9152
81eace42
GK
9153 /* %n outputs the negative of its operand. */
9154
9878760c
RK
9155 case 'N':
9156 /* Write the number of elements in the vector times 4. */
9157 if (GET_CODE (x) != PARALLEL)
9158 output_operand_lossage ("invalid %%N value");
e2c953b6
DE
9159 else
9160 fprintf (file, "%d", XVECLEN (x, 0) * 4);
9878760c
RK
9161 return;
9162
9163 case 'O':
9164 /* Similar, but subtract 1 first. */
9165 if (GET_CODE (x) != PARALLEL)
1427100a 9166 output_operand_lossage ("invalid %%O value");
e2c953b6
DE
9167 else
9168 fprintf (file, "%d", (XVECLEN (x, 0) - 1) * 4);
9878760c
RK
9169 return;
9170
9854d9ed
RK
9171 case 'p':
9172 /* X is a CONST_INT that is a power of two. Output the logarithm. */
9173 if (! INT_P (x)
2bfcf297 9174 || INT_LOWPART (x) < 0
9854d9ed
RK
9175 || (i = exact_log2 (INT_LOWPART (x))) < 0)
9176 output_operand_lossage ("invalid %%p value");
e2c953b6
DE
9177 else
9178 fprintf (file, "%d", i);
9854d9ed
RK
9179 return;
9180
9878760c
RK
9181 case 'P':
9182 /* The operand must be an indirect memory reference. The result
8bb418a3 9183 is the register name. */
9878760c
RK
9184 if (GET_CODE (x) != MEM || GET_CODE (XEXP (x, 0)) != REG
9185 || REGNO (XEXP (x, 0)) >= 32)
9186 output_operand_lossage ("invalid %%P value");
e2c953b6 9187 else
8bb418a3 9188 fprintf (file, "%s", reg_names[REGNO (XEXP (x, 0))]);
9878760c
RK
9189 return;
9190
dfbdccdb
GK
9191 case 'q':
9192 /* This outputs the logical code corresponding to a boolean
9193 expression. The expression may have one or both operands
39a10a29
GK
9194 negated (if one, only the first one). For condition register
9195 logical operations, it will also treat the negated
9196 CR codes as NOTs, but not handle NOTs of them. */
dfbdccdb 9197 {
63bc1d05 9198 const char *const *t = 0;
dfbdccdb
GK
9199 const char *s;
9200 enum rtx_code code = GET_CODE (x);
9201 static const char * const tbl[3][3] = {
9202 { "and", "andc", "nor" },
9203 { "or", "orc", "nand" },
9204 { "xor", "eqv", "xor" } };
9205
9206 if (code == AND)
9207 t = tbl[0];
9208 else if (code == IOR)
9209 t = tbl[1];
9210 else if (code == XOR)
9211 t = tbl[2];
9212 else
9213 output_operand_lossage ("invalid %%q value");
9214
9215 if (GET_CODE (XEXP (x, 0)) != NOT)
9216 s = t[0];
9217 else
9218 {
9219 if (GET_CODE (XEXP (x, 1)) == NOT)
9220 s = t[2];
9221 else
9222 s = t[1];
9223 }
9224
9225 fputs (s, file);
9226 }
9227 return;
9228
2c4a9cff
DE
9229 case 'Q':
9230 if (TARGET_MFCRF)
3b6ce0af 9231 fputc (',', file);
5efb1046 9232 /* FALLTHRU */
2c4a9cff
DE
9233 else
9234 return;
9235
9854d9ed
RK
9236 case 'R':
9237 /* X is a CR register. Print the mask for `mtcrf'. */
9238 if (GET_CODE (x) != REG || ! CR_REGNO_P (REGNO (x)))
9239 output_operand_lossage ("invalid %%R value");
9240 else
9ebbca7d 9241 fprintf (file, "%d", 128 >> (REGNO (x) - CR0_REGNO));
9878760c 9242 return;
9854d9ed
RK
9243
9244 case 's':
9245 /* Low 5 bits of 32 - value */
9246 if (! INT_P (x))
9247 output_operand_lossage ("invalid %%s value");
e2c953b6
DE
9248 else
9249 fprintf (file, HOST_WIDE_INT_PRINT_DEC, (32 - INT_LOWPART (x)) & 31);
9878760c 9250 return;
9854d9ed 9251
a260abc9 9252 case 'S':
0ba1b2ff 9253 /* PowerPC64 mask position. All 0's is excluded.
a260abc9
DE
9254 CONST_INT 32-bit mask is considered sign-extended so any
9255 transition must occur within the CONST_INT, not on the boundary. */
b1765bde 9256 if (! mask64_operand (x, DImode))
a260abc9
DE
9257 output_operand_lossage ("invalid %%S value");
9258
0ba1b2ff 9259 uval = INT_LOWPART (x);
a260abc9 9260
0ba1b2ff 9261 if (uval & 1) /* Clear Left */
a260abc9 9262 {
f099d360
GK
9263#if HOST_BITS_PER_WIDE_INT > 64
9264 uval &= ((unsigned HOST_WIDE_INT) 1 << 64) - 1;
9265#endif
0ba1b2ff 9266 i = 64;
a260abc9 9267 }
0ba1b2ff 9268 else /* Clear Right */
a260abc9 9269 {
0ba1b2ff 9270 uval = ~uval;
f099d360
GK
9271#if HOST_BITS_PER_WIDE_INT > 64
9272 uval &= ((unsigned HOST_WIDE_INT) 1 << 64) - 1;
9273#endif
0ba1b2ff 9274 i = 63;
a260abc9 9275 }
0ba1b2ff
AM
9276 while (uval != 0)
9277 --i, uval >>= 1;
9278 if (i < 0)
9279 abort ();
9280 fprintf (file, "%d", i);
9281 return;
a260abc9 9282
a3170dc6
AH
9283 case 't':
9284 /* Like 'J' but get to the OVERFLOW/UNORDERED bit. */
9285 if (GET_CODE (x) != REG || GET_MODE (x) != CCmode)
9286 abort ();
9287
9288 /* Bit 3 is OV bit. */
9289 i = 4 * (REGNO (x) - CR0_REGNO) + 3;
9290
9291 /* If we want bit 31, write a shift count of zero, not 32. */
9292 fprintf (file, "%d", i == 31 ? 0 : i + 1);
9293 return;
9294
cccf3bdc
DE
9295 case 'T':
9296 /* Print the symbolic name of a branch target register. */
9297 if (GET_CODE (x) != REG || (REGNO (x) != LINK_REGISTER_REGNUM
9298 && REGNO (x) != COUNT_REGISTER_REGNUM))
9299 output_operand_lossage ("invalid %%T value");
e2c953b6 9300 else if (REGNO (x) == LINK_REGISTER_REGNUM)
cccf3bdc
DE
9301 fputs (TARGET_NEW_MNEMONICS ? "lr" : "r", file);
9302 else
9303 fputs ("ctr", file);
9304 return;
9305
9854d9ed 9306 case 'u':
802a0058 9307 /* High-order 16 bits of constant for use in unsigned operand. */
9854d9ed
RK
9308 if (! INT_P (x))
9309 output_operand_lossage ("invalid %%u value");
e2c953b6
DE
9310 else
9311 fprintf (file, HOST_WIDE_INT_PRINT_HEX,
9312 (INT_LOWPART (x) >> 16) & 0xffff);
9878760c
RK
9313 return;
9314
802a0058
MM
9315 case 'v':
9316 /* High-order 16 bits of constant for use in signed operand. */
9317 if (! INT_P (x))
9318 output_operand_lossage ("invalid %%v value");
e2c953b6 9319 else
134c32f6
DE
9320 fprintf (file, HOST_WIDE_INT_PRINT_HEX,
9321 (INT_LOWPART (x) >> 16) & 0xffff);
9322 return;
802a0058 9323
9854d9ed
RK
9324 case 'U':
9325 /* Print `u' if this has an auto-increment or auto-decrement. */
9326 if (GET_CODE (x) == MEM
9327 && (GET_CODE (XEXP (x, 0)) == PRE_INC
9328 || GET_CODE (XEXP (x, 0)) == PRE_DEC))
76229ac8 9329 putc ('u', file);
9854d9ed 9330 return;
9878760c 9331
e0cd0770
JC
9332 case 'V':
9333 /* Print the trap code for this operand. */
9334 switch (GET_CODE (x))
9335 {
9336 case EQ:
9337 fputs ("eq", file); /* 4 */
9338 break;
9339 case NE:
9340 fputs ("ne", file); /* 24 */
9341 break;
9342 case LT:
9343 fputs ("lt", file); /* 16 */
9344 break;
9345 case LE:
9346 fputs ("le", file); /* 20 */
9347 break;
9348 case GT:
9349 fputs ("gt", file); /* 8 */
9350 break;
9351 case GE:
9352 fputs ("ge", file); /* 12 */
9353 break;
9354 case LTU:
9355 fputs ("llt", file); /* 2 */
9356 break;
9357 case LEU:
9358 fputs ("lle", file); /* 6 */
9359 break;
9360 case GTU:
9361 fputs ("lgt", file); /* 1 */
9362 break;
9363 case GEU:
9364 fputs ("lge", file); /* 5 */
9365 break;
9366 default:
9367 abort ();
9368 }
9369 break;
9370
9854d9ed
RK
9371 case 'w':
9372 /* If constant, low-order 16 bits of constant, signed. Otherwise, write
9373 normally. */
9374 if (INT_P (x))
5f59ecb7
DE
9375 fprintf (file, HOST_WIDE_INT_PRINT_DEC,
9376 ((INT_LOWPART (x) & 0xffff) ^ 0x8000) - 0x8000);
9854d9ed
RK
9377 else
9378 print_operand (file, x, 0);
9878760c
RK
9379 return;
9380
9854d9ed 9381 case 'W':
e2c953b6 9382 /* MB value for a PowerPC64 rldic operand. */
e2c953b6
DE
9383 val = (GET_CODE (x) == CONST_INT
9384 ? INTVAL (x) : CONST_DOUBLE_HIGH (x));
9385
9386 if (val < 0)
9387 i = -1;
9854d9ed 9388 else
e2c953b6
DE
9389 for (i = 0; i < HOST_BITS_PER_WIDE_INT; i++)
9390 if ((val <<= 1) < 0)
9391 break;
9392
9393#if HOST_BITS_PER_WIDE_INT == 32
9394 if (GET_CODE (x) == CONST_INT && i >= 0)
9395 i += 32; /* zero-extend high-part was all 0's */
9396 else if (GET_CODE (x) == CONST_DOUBLE && i == 32)
9397 {
9398 val = CONST_DOUBLE_LOW (x);
9399
9400 if (val == 0)
a4f6c312 9401 abort ();
e2c953b6
DE
9402 else if (val < 0)
9403 --i;
9404 else
9405 for ( ; i < 64; i++)
9406 if ((val <<= 1) < 0)
9407 break;
9408 }
9409#endif
9410
9411 fprintf (file, "%d", i + 1);
9854d9ed 9412 return;
9878760c 9413
9854d9ed
RK
9414 case 'X':
9415 if (GET_CODE (x) == MEM
4d588c14 9416 && legitimate_indexed_address_p (XEXP (x, 0), 0))
76229ac8 9417 putc ('x', file);
9854d9ed 9418 return;
9878760c 9419
9854d9ed
RK
9420 case 'Y':
9421 /* Like 'L', for third word of TImode */
9422 if (GET_CODE (x) == REG)
5ebfb2ba 9423 fprintf (file, "%s", reg_names[REGNO (x) + 2]);
9854d9ed 9424 else if (GET_CODE (x) == MEM)
9878760c 9425 {
9854d9ed
RK
9426 if (GET_CODE (XEXP (x, 0)) == PRE_INC
9427 || GET_CODE (XEXP (x, 0)) == PRE_DEC)
a54d04b7 9428 output_address (plus_constant (XEXP (XEXP (x, 0), 0), 8));
9854d9ed 9429 else
d7624dc0 9430 output_address (XEXP (adjust_address_nv (x, SImode, 8), 0));
ba5e43aa 9431 if (small_data_operand (x, GET_MODE (x)))
8fbd2dc7
MM
9432 fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
9433 reg_names[SMALL_DATA_REG]);
9878760c
RK
9434 }
9435 return;
9854d9ed 9436
9878760c 9437 case 'z':
b4ac57ab
RS
9438 /* X is a SYMBOL_REF. Write out the name preceded by a
9439 period and without any trailing data in brackets. Used for function
4d30c363
MM
9440 names. If we are configured for System V (or the embedded ABI) on
9441 the PowerPC, do not emit the period, since those systems do not use
9442 TOCs and the like. */
9878760c
RK
9443 if (GET_CODE (x) != SYMBOL_REF)
9444 abort ();
9445
b6c9286a
MM
9446 if (XSTR (x, 0)[0] != '.')
9447 {
9448 switch (DEFAULT_ABI)
9449 {
9450 default:
9451 abort ();
9452
9453 case ABI_AIX:
9454 putc ('.', file);
9455 break;
9456
9457 case ABI_V4:
ee890fe2 9458 case ABI_DARWIN:
b6c9286a 9459 break;
b6c9286a
MM
9460 }
9461 }
9739c90c
JJ
9462 if (TARGET_AIX)
9463 RS6000_OUTPUT_BASENAME (file, XSTR (x, 0));
9464 else
9465 assemble_name (file, XSTR (x, 0));
9878760c
RK
9466 return;
9467
9854d9ed
RK
9468 case 'Z':
9469 /* Like 'L', for last word of TImode. */
9470 if (GET_CODE (x) == REG)
5ebfb2ba 9471 fprintf (file, "%s", reg_names[REGNO (x) + 3]);
9854d9ed
RK
9472 else if (GET_CODE (x) == MEM)
9473 {
9474 if (GET_CODE (XEXP (x, 0)) == PRE_INC
9475 || GET_CODE (XEXP (x, 0)) == PRE_DEC)
a54d04b7 9476 output_address (plus_constant (XEXP (XEXP (x, 0), 0), 12));
9854d9ed 9477 else
d7624dc0 9478 output_address (XEXP (adjust_address_nv (x, SImode, 12), 0));
ba5e43aa 9479 if (small_data_operand (x, GET_MODE (x)))
8fbd2dc7
MM
9480 fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
9481 reg_names[SMALL_DATA_REG]);
9854d9ed 9482 }
5c23c401 9483 return;
0ac081f6 9484
a3170dc6 9485 /* Print AltiVec or SPE memory operand. */
0ac081f6
AH
9486 case 'y':
9487 {
9488 rtx tmp;
9489
9490 if (GET_CODE (x) != MEM)
9491 abort ();
9492
9493 tmp = XEXP (x, 0);
9494
993f19a8 9495 if (TARGET_E500)
a3170dc6
AH
9496 {
9497 /* Handle [reg]. */
9498 if (GET_CODE (tmp) == REG)
9499 {
9500 fprintf (file, "0(%s)", reg_names[REGNO (tmp)]);
9501 break;
9502 }
9503 /* Handle [reg+UIMM]. */
9504 else if (GET_CODE (tmp) == PLUS &&
9505 GET_CODE (XEXP (tmp, 1)) == CONST_INT)
9506 {
9507 int x;
9508
9509 if (GET_CODE (XEXP (tmp, 0)) != REG)
9510 abort ();
9511
9512 x = INTVAL (XEXP (tmp, 1));
9513 fprintf (file, "%d(%s)", x, reg_names[REGNO (XEXP (tmp, 0))]);
9514 break;
9515 }
9516
9517 /* Fall through. Must be [reg+reg]. */
9518 }
0ac081f6 9519 if (GET_CODE (tmp) == REG)
c62f2db5 9520 fprintf (file, "0,%s", reg_names[REGNO (tmp)]);
0ac081f6
AH
9521 else if (GET_CODE (tmp) == PLUS && GET_CODE (XEXP (tmp, 1)) == REG)
9522 {
9523 if (REGNO (XEXP (tmp, 0)) == 0)
9524 fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (tmp, 1)) ],
9525 reg_names[ REGNO (XEXP (tmp, 0)) ]);
9526 else
9527 fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (tmp, 0)) ],
9528 reg_names[ REGNO (XEXP (tmp, 1)) ]);
9529 }
9530 else
9531 abort ();
9532 break;
9533 }
9854d9ed 9534
9878760c
RK
9535 case 0:
9536 if (GET_CODE (x) == REG)
9537 fprintf (file, "%s", reg_names[REGNO (x)]);
9538 else if (GET_CODE (x) == MEM)
9539 {
9540 /* We need to handle PRE_INC and PRE_DEC here, since we need to
9541 know the width from the mode. */
9542 if (GET_CODE (XEXP (x, 0)) == PRE_INC)
79ba6d34
MM
9543 fprintf (file, "%d(%s)", GET_MODE_SIZE (GET_MODE (x)),
9544 reg_names[REGNO (XEXP (XEXP (x, 0), 0))]);
9878760c 9545 else if (GET_CODE (XEXP (x, 0)) == PRE_DEC)
79ba6d34
MM
9546 fprintf (file, "%d(%s)", - GET_MODE_SIZE (GET_MODE (x)),
9547 reg_names[REGNO (XEXP (XEXP (x, 0), 0))]);
9878760c 9548 else
a54d04b7 9549 output_address (XEXP (x, 0));
9878760c
RK
9550 }
9551 else
a54d04b7 9552 output_addr_const (file, x);
a85d226b 9553 return;
9878760c 9554
c4501e62
JJ
9555 case '&':
9556 assemble_name (file, rs6000_get_some_local_dynamic_name ());
9557 return;
9558
9878760c
RK
9559 default:
9560 output_operand_lossage ("invalid %%xn code");
9561 }
9562}
9563\f
9564/* Print the address of an operand. */
9565
9566void
a2369ed3 9567print_operand_address (FILE *file, rtx x)
9878760c
RK
9568{
9569 if (GET_CODE (x) == REG)
4697a36c 9570 fprintf (file, "0(%s)", reg_names[ REGNO (x) ]);
9ebbca7d
GK
9571 else if (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == CONST
9572 || GET_CODE (x) == LABEL_REF)
9878760c
RK
9573 {
9574 output_addr_const (file, x);
ba5e43aa 9575 if (small_data_operand (x, GET_MODE (x)))
8fbd2dc7
MM
9576 fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
9577 reg_names[SMALL_DATA_REG]);
9ebbca7d 9578 else if (TARGET_TOC)
a4f6c312 9579 abort ();
9878760c
RK
9580 }
9581 else if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 1)) == REG)
9582 {
9583 if (REGNO (XEXP (x, 0)) == 0)
4697a36c
MM
9584 fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (x, 1)) ],
9585 reg_names[ REGNO (XEXP (x, 0)) ]);
9878760c 9586 else
4697a36c
MM
9587 fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (x, 0)) ],
9588 reg_names[ REGNO (XEXP (x, 1)) ]);
9878760c
RK
9589 }
9590 else if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 1)) == CONST_INT)
4a0a75dd
KG
9591 fprintf (file, HOST_WIDE_INT_PRINT_DEC "(%s)",
9592 INTVAL (XEXP (x, 1)), reg_names[ REGNO (XEXP (x, 0)) ]);
3cb999d8
DE
9593#if TARGET_ELF
9594 else if (GET_CODE (x) == LO_SUM && GET_CODE (XEXP (x, 0)) == REG
9595 && CONSTANT_P (XEXP (x, 1)))
4697a36c
MM
9596 {
9597 output_addr_const (file, XEXP (x, 1));
9598 fprintf (file, "@l(%s)", reg_names[ REGNO (XEXP (x, 0)) ]);
9599 }
c859cda6
DJ
9600#endif
9601#if TARGET_MACHO
9602 else if (GET_CODE (x) == LO_SUM && GET_CODE (XEXP (x, 0)) == REG
9603 && CONSTANT_P (XEXP (x, 1)))
9604 {
9605 fprintf (file, "lo16(");
9606 output_addr_const (file, XEXP (x, 1));
9607 fprintf (file, ")(%s)", reg_names[ REGNO (XEXP (x, 0)) ]);
9608 }
3cb999d8 9609#endif
4d588c14 9610 else if (legitimate_constant_pool_address_p (x))
9ebbca7d 9611 {
2bfcf297 9612 if (TARGET_AIX && (!TARGET_ELF || !TARGET_MINIMAL_TOC))
9ebbca7d 9613 {
2bfcf297
DB
9614 rtx contains_minus = XEXP (x, 1);
9615 rtx minus, symref;
9616 const char *name;
9ebbca7d
GK
9617
9618 /* Find the (minus (sym) (toc)) buried in X, and temporarily
a4f6c312 9619 turn it into (sym) for output_addr_const. */
9ebbca7d
GK
9620 while (GET_CODE (XEXP (contains_minus, 0)) != MINUS)
9621 contains_minus = XEXP (contains_minus, 0);
9622
2bfcf297
DB
9623 minus = XEXP (contains_minus, 0);
9624 symref = XEXP (minus, 0);
9625 XEXP (contains_minus, 0) = symref;
9626 if (TARGET_ELF)
9627 {
9628 char *newname;
9629
9630 name = XSTR (symref, 0);
9631 newname = alloca (strlen (name) + sizeof ("@toc"));
9632 strcpy (newname, name);
9633 strcat (newname, "@toc");
9634 XSTR (symref, 0) = newname;
9635 }
9636 output_addr_const (file, XEXP (x, 1));
9637 if (TARGET_ELF)
9638 XSTR (symref, 0) = name;
9ebbca7d
GK
9639 XEXP (contains_minus, 0) = minus;
9640 }
9641 else
9642 output_addr_const (file, XEXP (x, 1));
9643
9644 fprintf (file, "(%s)", reg_names[REGNO (XEXP (x, 0))]);
9645 }
9878760c
RK
9646 else
9647 abort ();
9648}
9649\f
88cad84b 9650/* Target hook for assembling integer objects. The PowerPC version has
301d03af
RS
9651 to handle fixup entries for relocatable code if RELOCATABLE_NEEDS_FIXUP
9652 is defined. It also needs to handle DI-mode objects on 64-bit
9653 targets. */
9654
9655static bool
a2369ed3 9656rs6000_assemble_integer (rtx x, unsigned int size, int aligned_p)
301d03af
RS
9657{
9658#ifdef RELOCATABLE_NEEDS_FIXUP
9659 /* Special handling for SI values. */
9660 if (size == 4 && aligned_p)
9661 {
a2369ed3 9662 extern int in_toc_section (void);
301d03af
RS
9663 static int recurse = 0;
9664
9665 /* For -mrelocatable, we mark all addresses that need to be fixed up
9666 in the .fixup section. */
9667 if (TARGET_RELOCATABLE
9668 && !in_toc_section ()
9669 && !in_text_section ()
9670 && !recurse
9671 && GET_CODE (x) != CONST_INT
9672 && GET_CODE (x) != CONST_DOUBLE
9673 && CONSTANT_P (x))
9674 {
9675 char buf[256];
9676
9677 recurse = 1;
9678 ASM_GENERATE_INTERNAL_LABEL (buf, "LCP", fixuplabelno);
9679 fixuplabelno++;
9680 ASM_OUTPUT_LABEL (asm_out_file, buf);
9681 fprintf (asm_out_file, "\t.long\t(");
9682 output_addr_const (asm_out_file, x);
9683 fprintf (asm_out_file, ")@fixup\n");
9684 fprintf (asm_out_file, "\t.section\t\".fixup\",\"aw\"\n");
9685 ASM_OUTPUT_ALIGN (asm_out_file, 2);
9686 fprintf (asm_out_file, "\t.long\t");
9687 assemble_name (asm_out_file, buf);
9688 fprintf (asm_out_file, "\n\t.previous\n");
9689 recurse = 0;
9690 return true;
9691 }
9692 /* Remove initial .'s to turn a -mcall-aixdesc function
9693 address into the address of the descriptor, not the function
9694 itself. */
9695 else if (GET_CODE (x) == SYMBOL_REF
9696 && XSTR (x, 0)[0] == '.'
9697 && DEFAULT_ABI == ABI_AIX)
9698 {
9699 const char *name = XSTR (x, 0);
9700 while (*name == '.')
9701 name++;
9702
9703 fprintf (asm_out_file, "\t.long\t%s\n", name);
9704 return true;
9705 }
9706 }
9707#endif /* RELOCATABLE_NEEDS_FIXUP */
9708 return default_assemble_integer (x, size, aligned_p);
9709}
93638d7a
AM
9710
9711#ifdef HAVE_GAS_HIDDEN
9712/* Emit an assembler directive to set symbol visibility for DECL to
9713 VISIBILITY_TYPE. */
9714
5add3202 9715static void
a2369ed3 9716rs6000_assemble_visibility (tree decl, int vis)
93638d7a 9717{
93638d7a
AM
9718 /* Functions need to have their entry point symbol visibility set as
9719 well as their descriptor symbol visibility. */
9720 if (DEFAULT_ABI == ABI_AIX && TREE_CODE (decl) == FUNCTION_DECL)
9721 {
25fdb4dc
RH
9722 static const char * const visibility_types[] = {
9723 NULL, "internal", "hidden", "protected"
9724 };
9725
9726 const char *name, *type;
93638d7a
AM
9727
9728 name = ((* targetm.strip_name_encoding)
9729 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl))));
25fdb4dc 9730 type = visibility_types[vis];
93638d7a 9731
25fdb4dc
RH
9732 fprintf (asm_out_file, "\t.%s\t%s\n", type, name);
9733 fprintf (asm_out_file, "\t.%s\t.%s\n", type, name);
93638d7a 9734 }
25fdb4dc
RH
9735 else
9736 default_assemble_visibility (decl, vis);
93638d7a
AM
9737}
9738#endif
301d03af 9739\f
39a10a29 9740enum rtx_code
a2369ed3 9741rs6000_reverse_condition (enum machine_mode mode, enum rtx_code code)
39a10a29
GK
9742{
9743 /* Reversal of FP compares takes care -- an ordered compare
9744 becomes an unordered compare and vice versa. */
bc9ec0e0
GK
9745 if (mode == CCFPmode
9746 && (!flag_finite_math_only
9747 || code == UNLT || code == UNLE || code == UNGT || code == UNGE
9748 || code == UNEQ || code == LTGT))
bab6226b 9749 return reverse_condition_maybe_unordered (code);
39a10a29 9750 else
bab6226b 9751 return reverse_condition (code);
39a10a29
GK
9752}
9753
39a10a29
GK
9754/* Generate a compare for CODE. Return a brand-new rtx that
9755 represents the result of the compare. */
a4f6c312 9756
39a10a29 9757static rtx
a2369ed3 9758rs6000_generate_compare (enum rtx_code code)
39a10a29
GK
9759{
9760 enum machine_mode comp_mode;
9761 rtx compare_result;
9762
9763 if (rs6000_compare_fp_p)
9764 comp_mode = CCFPmode;
9765 else if (code == GTU || code == LTU
9766 || code == GEU || code == LEU)
9767 comp_mode = CCUNSmode;
9768 else
9769 comp_mode = CCmode;
9770
9771 /* First, the compare. */
9772 compare_result = gen_reg_rtx (comp_mode);
a3170dc6
AH
9773
9774 /* SPE FP compare instructions on the GPRs. Yuck! */
993f19a8
AH
9775 if ((TARGET_E500 && !TARGET_FPRS && TARGET_HARD_FLOAT)
9776 && rs6000_compare_fp_p)
a3170dc6
AH
9777 {
9778 rtx cmp, or1, or2, or_result, compare_result2;
9779
9780 switch (code)
9781 {
9782 case EQ:
9783 case UNEQ:
9784 case NE:
9785 case LTGT:
bc9ec0e0 9786 cmp = flag_finite_math_only
a3170dc6
AH
9787 ? gen_tstsfeq_gpr (compare_result, rs6000_compare_op0,
9788 rs6000_compare_op1)
9789 : gen_cmpsfeq_gpr (compare_result, rs6000_compare_op0,
9790 rs6000_compare_op1);
9791 break;
9792 case GT:
9793 case GTU:
9794 case UNGT:
9795 case UNGE:
9796 case GE:
9797 case GEU:
bc9ec0e0 9798 cmp = flag_finite_math_only
a3170dc6
AH
9799 ? gen_tstsfgt_gpr (compare_result, rs6000_compare_op0,
9800 rs6000_compare_op1)
9801 : gen_cmpsfgt_gpr (compare_result, rs6000_compare_op0,
9802 rs6000_compare_op1);
9803 break;
9804 case LT:
9805 case LTU:
9806 case UNLT:
9807 case UNLE:
9808 case LE:
9809 case LEU:
bc9ec0e0 9810 cmp = flag_finite_math_only
a3170dc6
AH
9811 ? gen_tstsflt_gpr (compare_result, rs6000_compare_op0,
9812 rs6000_compare_op1)
9813 : gen_cmpsflt_gpr (compare_result, rs6000_compare_op0,
9814 rs6000_compare_op1);
9815 break;
9816 default:
9817 abort ();
9818 }
9819
9820 /* Synthesize LE and GE from LT/GT || EQ. */
9821 if (code == LE || code == GE || code == LEU || code == GEU)
9822 {
9823 /* Synthesize GE/LE frome GT/LT || EQ. */
9824
9825 emit_insn (cmp);
9826
9827 switch (code)
9828 {
9829 case LE: code = LT; break;
9830 case GE: code = GT; break;
9831 case LEU: code = LT; break;
9832 case GEU: code = GT; break;
9833 default: abort ();
9834 }
9835
9836 or1 = gen_reg_rtx (SImode);
9837 or2 = gen_reg_rtx (SImode);
9838 or_result = gen_reg_rtx (CCEQmode);
9839 compare_result2 = gen_reg_rtx (CCFPmode);
9840
9841 /* Do the EQ. */
bc9ec0e0 9842 cmp = flag_finite_math_only
a3170dc6
AH
9843 ? gen_tstsfeq_gpr (compare_result2, rs6000_compare_op0,
9844 rs6000_compare_op1)
9845 : gen_cmpsfeq_gpr (compare_result2, rs6000_compare_op0,
9846 rs6000_compare_op1);
9847 emit_insn (cmp);
9848
9849 /* The MC8540 FP compare instructions set the CR bits
9850 differently than other PPC compare instructions. For
9851 that matter, there is no generic test instruction, but a
9852 testgt, testlt, and testeq. For a true condition, bit 2
9853 is set (x1xx) in the CR. Following the traditional CR
9854 values:
9855
9856 LT GT EQ OV
9857 bit3 bit2 bit1 bit0
9858
9859 ... bit 2 would be a GT CR alias, so later on we
b6d08ca1 9860 look in the GT bits for the branch instructions.
a3170dc6
AH
9861 However, we must be careful to emit correct RTL in
9862 the meantime, so optimizations don't get confused. */
9863
f1c25d3b
KH
9864 or1 = gen_rtx_NE (SImode, compare_result, const0_rtx);
9865 or2 = gen_rtx_NE (SImode, compare_result2, const0_rtx);
a3170dc6
AH
9866
9867 /* OR them together. */
9868 cmp = gen_rtx_SET (VOIDmode, or_result,
9869 gen_rtx_COMPARE (CCEQmode,
9870 gen_rtx_IOR (SImode, or1, or2),
9871 const_true_rtx));
9872 compare_result = or_result;
9873 code = EQ;
9874 }
9875 else
9876 {
9877 /* We only care about 1 bit (x1xx), so map everything to NE to
9878 maintain rtl sanity. We'll get to the right bit (x1xx) at
9879 code output time. */
9880 if (code == NE || code == LTGT)
9881 /* Do the inverse here because we have no cmpne
9882 instruction. We use the cmpeq instruction and expect
9883 to get a 0 instead. */
9884 code = EQ;
9885 else
9886 code = NE;
9887 }
9888
9889 emit_insn (cmp);
9890 }
9891 else
9892 emit_insn (gen_rtx_SET (VOIDmode, compare_result,
9893 gen_rtx_COMPARE (comp_mode,
9894 rs6000_compare_op0,
9895 rs6000_compare_op1)));
39a10a29 9896
ca5adc63 9897 /* Some kinds of FP comparisons need an OR operation;
bc9ec0e0 9898 under flag_finite_math_only we don't bother. */
39a10a29 9899 if (rs6000_compare_fp_p
bc9ec0e0 9900 && ! flag_finite_math_only
993f19a8 9901 && ! (TARGET_HARD_FLOAT && TARGET_E500 && !TARGET_FPRS)
39a10a29
GK
9902 && (code == LE || code == GE
9903 || code == UNEQ || code == LTGT
9904 || code == UNGT || code == UNLT))
9905 {
9906 enum rtx_code or1, or2;
9907 rtx or1_rtx, or2_rtx, compare2_rtx;
9908 rtx or_result = gen_reg_rtx (CCEQmode);
9909
9910 switch (code)
9911 {
9912 case LE: or1 = LT; or2 = EQ; break;
9913 case GE: or1 = GT; or2 = EQ; break;
9914 case UNEQ: or1 = UNORDERED; or2 = EQ; break;
9915 case LTGT: or1 = LT; or2 = GT; break;
9916 case UNGT: or1 = UNORDERED; or2 = GT; break;
9917 case UNLT: or1 = UNORDERED; or2 = LT; break;
9918 default: abort ();
9919 }
9920 validate_condition_mode (or1, comp_mode);
9921 validate_condition_mode (or2, comp_mode);
1c563bed
KH
9922 or1_rtx = gen_rtx_fmt_ee (or1, SImode, compare_result, const0_rtx);
9923 or2_rtx = gen_rtx_fmt_ee (or2, SImode, compare_result, const0_rtx);
39a10a29
GK
9924 compare2_rtx = gen_rtx_COMPARE (CCEQmode,
9925 gen_rtx_IOR (SImode, or1_rtx, or2_rtx),
9926 const_true_rtx);
9927 emit_insn (gen_rtx_SET (VOIDmode, or_result, compare2_rtx));
9928
9929 compare_result = or_result;
9930 code = EQ;
9931 }
9932
9933 validate_condition_mode (code, GET_MODE (compare_result));
9934
1c563bed 9935 return gen_rtx_fmt_ee (code, VOIDmode, compare_result, const0_rtx);
39a10a29
GK
9936}
9937
9938
9939/* Emit the RTL for an sCOND pattern. */
9940
9941void
a2369ed3 9942rs6000_emit_sCOND (enum rtx_code code, rtx result)
39a10a29
GK
9943{
9944 rtx condition_rtx;
9945 enum machine_mode op_mode;
b7053a3f 9946 enum rtx_code cond_code;
39a10a29
GK
9947
9948 condition_rtx = rs6000_generate_compare (code);
b7053a3f
GK
9949 cond_code = GET_CODE (condition_rtx);
9950
9951 if (cond_code == NE
9952 || cond_code == GE || cond_code == LE
9953 || cond_code == GEU || cond_code == LEU
9954 || cond_code == ORDERED || cond_code == UNGE || cond_code == UNLE)
9955 {
9956 rtx not_result = gen_reg_rtx (CCEQmode);
9957 rtx not_op, rev_cond_rtx;
9958 enum machine_mode cc_mode;
9959
9960 cc_mode = GET_MODE (XEXP (condition_rtx, 0));
9961
1c563bed 9962 rev_cond_rtx = gen_rtx_fmt_ee (rs6000_reverse_condition (cc_mode, cond_code),
0f4c242b 9963 SImode, XEXP (condition_rtx, 0), const0_rtx);
b7053a3f
GK
9964 not_op = gen_rtx_COMPARE (CCEQmode, rev_cond_rtx, const0_rtx);
9965 emit_insn (gen_rtx_SET (VOIDmode, not_result, not_op));
9966 condition_rtx = gen_rtx_EQ (VOIDmode, not_result, const0_rtx);
9967 }
39a10a29
GK
9968
9969 op_mode = GET_MODE (rs6000_compare_op0);
9970 if (op_mode == VOIDmode)
9971 op_mode = GET_MODE (rs6000_compare_op1);
9972
9973 if (TARGET_POWERPC64 && (op_mode == DImode || rs6000_compare_fp_p))
9974 {
9975 PUT_MODE (condition_rtx, DImode);
9976 convert_move (result, condition_rtx, 0);
9977 }
9978 else
9979 {
9980 PUT_MODE (condition_rtx, SImode);
9981 emit_insn (gen_rtx_SET (VOIDmode, result, condition_rtx));
9982 }
9983}
9984
39a10a29
GK
9985/* Emit a branch of kind CODE to location LOC. */
9986
9987void
a2369ed3 9988rs6000_emit_cbranch (enum rtx_code code, rtx loc)
39a10a29
GK
9989{
9990 rtx condition_rtx, loc_ref;
9991
9992 condition_rtx = rs6000_generate_compare (code);
9993 loc_ref = gen_rtx_LABEL_REF (VOIDmode, loc);
9994 emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx,
9995 gen_rtx_IF_THEN_ELSE (VOIDmode, condition_rtx,
9996 loc_ref, pc_rtx)));
9997}
9998
12a4e8c5
GK
9999/* Return the string to output a conditional branch to LABEL, which is
10000 the operand number of the label, or -1 if the branch is really a
10001 conditional return.
10002
10003 OP is the conditional expression. XEXP (OP, 0) is assumed to be a
10004 condition code register and its mode specifies what kind of
10005 comparison we made.
10006
a0ab749a 10007 REVERSED is nonzero if we should reverse the sense of the comparison.
12a4e8c5
GK
10008
10009 INSN is the insn. */
10010
10011char *
a2369ed3 10012output_cbranch (rtx op, const char *label, int reversed, rtx insn)
12a4e8c5
GK
10013{
10014 static char string[64];
10015 enum rtx_code code = GET_CODE (op);
10016 rtx cc_reg = XEXP (op, 0);
10017 enum machine_mode mode = GET_MODE (cc_reg);
10018 int cc_regno = REGNO (cc_reg) - CR0_REGNO;
39a10a29 10019 int need_longbranch = label != NULL && get_attr_length (insn) == 8;
12a4e8c5
GK
10020 int really_reversed = reversed ^ need_longbranch;
10021 char *s = string;
10022 const char *ccode;
10023 const char *pred;
10024 rtx note;
10025
39a10a29
GK
10026 validate_condition_mode (code, mode);
10027
10028 /* Work out which way this really branches. We could use
10029 reverse_condition_maybe_unordered here always but this
10030 makes the resulting assembler clearer. */
12a4e8c5 10031 if (really_reversed)
de40e1df
DJ
10032 {
10033 /* Reversal of FP compares takes care -- an ordered compare
10034 becomes an unordered compare and vice versa. */
10035 if (mode == CCFPmode)
10036 code = reverse_condition_maybe_unordered (code);
10037 else
10038 code = reverse_condition (code);
10039 }
12a4e8c5 10040
993f19a8 10041 if ((TARGET_E500 && !TARGET_FPRS && TARGET_HARD_FLOAT) && mode == CCFPmode)
a3170dc6
AH
10042 {
10043 /* The efscmp/tst* instructions twiddle bit 2, which maps nicely
10044 to the GT bit. */
10045 if (code == EQ)
10046 /* Opposite of GT. */
10047 code = UNLE;
10048 else if (code == NE)
10049 code = GT;
10050 else
10051 abort ();
10052 }
10053
39a10a29 10054 switch (code)
12a4e8c5
GK
10055 {
10056 /* Not all of these are actually distinct opcodes, but
10057 we distinguish them for clarity of the resulting assembler. */
50a0b056
GK
10058 case NE: case LTGT:
10059 ccode = "ne"; break;
10060 case EQ: case UNEQ:
10061 ccode = "eq"; break;
10062 case GE: case GEU:
10063 ccode = "ge"; break;
10064 case GT: case GTU: case UNGT:
10065 ccode = "gt"; break;
10066 case LE: case LEU:
10067 ccode = "le"; break;
10068 case LT: case LTU: case UNLT:
10069 ccode = "lt"; break;
12a4e8c5
GK
10070 case UNORDERED: ccode = "un"; break;
10071 case ORDERED: ccode = "nu"; break;
10072 case UNGE: ccode = "nl"; break;
10073 case UNLE: ccode = "ng"; break;
10074 default:
a4f6c312 10075 abort ();
12a4e8c5
GK
10076 }
10077
94a54f47
GK
10078 /* Maybe we have a guess as to how likely the branch is.
10079 The old mnemonics don't have a way to specify this information. */
f4857b9b 10080 pred = "";
12a4e8c5
GK
10081 note = find_reg_note (insn, REG_BR_PROB, NULL_RTX);
10082 if (note != NULL_RTX)
10083 {
10084 /* PROB is the difference from 50%. */
10085 int prob = INTVAL (XEXP (note, 0)) - REG_BR_PROB_BASE / 2;
f4857b9b
AM
10086
10087 /* Only hint for highly probable/improbable branches on newer
10088 cpus as static prediction overrides processor dynamic
10089 prediction. For older cpus we may as well always hint, but
10090 assume not taken for branches that are very close to 50% as a
10091 mispredicted taken branch is more expensive than a
10092 mispredicted not-taken branch. */
ec507f2d 10093 if (rs6000_always_hint
f4857b9b
AM
10094 || abs (prob) > REG_BR_PROB_BASE / 100 * 48)
10095 {
10096 if (abs (prob) > REG_BR_PROB_BASE / 20
10097 && ((prob > 0) ^ need_longbranch))
7f3d8013 10098 pred = "+";
f4857b9b
AM
10099 else
10100 pred = "-";
10101 }
12a4e8c5 10102 }
12a4e8c5
GK
10103
10104 if (label == NULL)
94a54f47 10105 s += sprintf (s, "{b%sr|b%slr%s} ", ccode, ccode, pred);
12a4e8c5 10106 else
94a54f47 10107 s += sprintf (s, "{b%s|b%s%s} ", ccode, ccode, pred);
12a4e8c5 10108
37c67319 10109 /* We need to escape any '%' characters in the reg_names string.
a3c9585f 10110 Assume they'd only be the first character.... */
37c67319
GK
10111 if (reg_names[cc_regno + CR0_REGNO][0] == '%')
10112 *s++ = '%';
94a54f47 10113 s += sprintf (s, "%s", reg_names[cc_regno + CR0_REGNO]);
12a4e8c5
GK
10114
10115 if (label != NULL)
10116 {
10117 /* If the branch distance was too far, we may have to use an
10118 unconditional branch to go the distance. */
10119 if (need_longbranch)
44518ddd 10120 s += sprintf (s, ",$+8\n\tb %s", label);
12a4e8c5
GK
10121 else
10122 s += sprintf (s, ",%s", label);
10123 }
10124
10125 return string;
10126}
50a0b056
GK
10127
10128/* Emit a conditional move: move TRUE_COND to DEST if OP of the
10129 operands of the last comparison is nonzero/true, FALSE_COND if it
10130 is zero/false. Return 0 if the hardware has no such operation. */
a4f6c312 10131
50a0b056 10132int
a2369ed3 10133rs6000_emit_cmove (rtx dest, rtx op, rtx true_cond, rtx false_cond)
50a0b056
GK
10134{
10135 enum rtx_code code = GET_CODE (op);
10136 rtx op0 = rs6000_compare_op0;
10137 rtx op1 = rs6000_compare_op1;
10138 REAL_VALUE_TYPE c1;
3148ad6d
DJ
10139 enum machine_mode compare_mode = GET_MODE (op0);
10140 enum machine_mode result_mode = GET_MODE (dest);
50a0b056
GK
10141 rtx temp;
10142
a3c9585f 10143 /* These modes should always match. */
a3170dc6
AH
10144 if (GET_MODE (op1) != compare_mode
10145 /* In the isel case however, we can use a compare immediate, so
10146 op1 may be a small constant. */
10147 && (!TARGET_ISEL || !short_cint_operand (op1, VOIDmode)))
3148ad6d 10148 return 0;
178c3eff 10149 if (GET_MODE (true_cond) != result_mode)
3148ad6d 10150 return 0;
178c3eff 10151 if (GET_MODE (false_cond) != result_mode)
3148ad6d
DJ
10152 return 0;
10153
50a0b056 10154 /* First, work out if the hardware can do this at all, or
a3c9585f 10155 if it's too slow.... */
50a0b056 10156 if (! rs6000_compare_fp_p)
a3170dc6
AH
10157 {
10158 if (TARGET_ISEL)
10159 return rs6000_emit_int_cmove (dest, op, true_cond, false_cond);
10160 return 0;
10161 }
50a0b056
GK
10162
10163 /* Eliminate half of the comparisons by switching operands, this
10164 makes the remaining code simpler. */
10165 if (code == UNLT || code == UNGT || code == UNORDERED || code == NE
bc9ec0e0 10166 || code == LTGT || code == LT || code == UNLE)
50a0b056
GK
10167 {
10168 code = reverse_condition_maybe_unordered (code);
10169 temp = true_cond;
10170 true_cond = false_cond;
10171 false_cond = temp;
10172 }
10173
10174 /* UNEQ and LTGT take four instructions for a comparison with zero,
10175 it'll probably be faster to use a branch here too. */
bc9ec0e0 10176 if (code == UNEQ && HONOR_NANS (compare_mode))
50a0b056
GK
10177 return 0;
10178
10179 if (GET_CODE (op1) == CONST_DOUBLE)
10180 REAL_VALUE_FROM_CONST_DOUBLE (c1, op1);
10181
b6d08ca1 10182 /* We're going to try to implement comparisons by performing
50a0b056
GK
10183 a subtract, then comparing against zero. Unfortunately,
10184 Inf - Inf is NaN which is not zero, and so if we don't
27d30956 10185 know that the operand is finite and the comparison
50a0b056 10186 would treat EQ different to UNORDERED, we can't do it. */
bc9ec0e0 10187 if (HONOR_INFINITIES (compare_mode)
50a0b056 10188 && code != GT && code != UNGE
045572c7 10189 && (GET_CODE (op1) != CONST_DOUBLE || real_isinf (&c1))
50a0b056
GK
10190 /* Constructs of the form (a OP b ? a : b) are safe. */
10191 && ((! rtx_equal_p (op0, false_cond) && ! rtx_equal_p (op1, false_cond))
10192 || (! rtx_equal_p (op0, true_cond)
10193 && ! rtx_equal_p (op1, true_cond))))
10194 return 0;
10195 /* At this point we know we can use fsel. */
10196
10197 /* Reduce the comparison to a comparison against zero. */
3148ad6d 10198 temp = gen_reg_rtx (compare_mode);
50a0b056 10199 emit_insn (gen_rtx_SET (VOIDmode, temp,
3148ad6d 10200 gen_rtx_MINUS (compare_mode, op0, op1)));
50a0b056 10201 op0 = temp;
3148ad6d 10202 op1 = CONST0_RTX (compare_mode);
50a0b056
GK
10203
10204 /* If we don't care about NaNs we can reduce some of the comparisons
10205 down to faster ones. */
bc9ec0e0 10206 if (! HONOR_NANS (compare_mode))
50a0b056
GK
10207 switch (code)
10208 {
10209 case GT:
10210 code = LE;
10211 temp = true_cond;
10212 true_cond = false_cond;
10213 false_cond = temp;
10214 break;
10215 case UNGE:
10216 code = GE;
10217 break;
10218 case UNEQ:
10219 code = EQ;
10220 break;
10221 default:
10222 break;
10223 }
10224
10225 /* Now, reduce everything down to a GE. */
10226 switch (code)
10227 {
10228 case GE:
10229 break;
10230
10231 case LE:
3148ad6d
DJ
10232 temp = gen_reg_rtx (compare_mode);
10233 emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_NEG (compare_mode, op0)));
50a0b056
GK
10234 op0 = temp;
10235 break;
10236
10237 case ORDERED:
3148ad6d
DJ
10238 temp = gen_reg_rtx (compare_mode);
10239 emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_ABS (compare_mode, op0)));
50a0b056
GK
10240 op0 = temp;
10241 break;
10242
10243 case EQ:
3148ad6d 10244 temp = gen_reg_rtx (compare_mode);
50a0b056 10245 emit_insn (gen_rtx_SET (VOIDmode, temp,
3148ad6d
DJ
10246 gen_rtx_NEG (compare_mode,
10247 gen_rtx_ABS (compare_mode, op0))));
50a0b056
GK
10248 op0 = temp;
10249 break;
10250
10251 case UNGE:
bc9ec0e0 10252 /* a UNGE 0 <-> (a GE 0 || -a UNLT 0) */
3148ad6d 10253 temp = gen_reg_rtx (result_mode);
50a0b056 10254 emit_insn (gen_rtx_SET (VOIDmode, temp,
3148ad6d 10255 gen_rtx_IF_THEN_ELSE (result_mode,
50a0b056
GK
10256 gen_rtx_GE (VOIDmode,
10257 op0, op1),
10258 true_cond, false_cond)));
bc9ec0e0
GK
10259 false_cond = true_cond;
10260 true_cond = temp;
50a0b056 10261
3148ad6d
DJ
10262 temp = gen_reg_rtx (compare_mode);
10263 emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_NEG (compare_mode, op0)));
50a0b056
GK
10264 op0 = temp;
10265 break;
10266
10267 case GT:
bc9ec0e0 10268 /* a GT 0 <-> (a GE 0 && -a UNLT 0) */
3148ad6d 10269 temp = gen_reg_rtx (result_mode);
50a0b056 10270 emit_insn (gen_rtx_SET (VOIDmode, temp,
3148ad6d 10271 gen_rtx_IF_THEN_ELSE (result_mode,
50a0b056
GK
10272 gen_rtx_GE (VOIDmode,
10273 op0, op1),
10274 true_cond, false_cond)));
bc9ec0e0
GK
10275 true_cond = false_cond;
10276 false_cond = temp;
50a0b056 10277
3148ad6d
DJ
10278 temp = gen_reg_rtx (compare_mode);
10279 emit_insn (gen_rtx_SET (VOIDmode, temp, gen_rtx_NEG (compare_mode, op0)));
50a0b056
GK
10280 op0 = temp;
10281 break;
10282
10283 default:
10284 abort ();
10285 }
10286
10287 emit_insn (gen_rtx_SET (VOIDmode, dest,
3148ad6d 10288 gen_rtx_IF_THEN_ELSE (result_mode,
50a0b056
GK
10289 gen_rtx_GE (VOIDmode,
10290 op0, op1),
10291 true_cond, false_cond)));
10292 return 1;
10293}
10294
a3170dc6
AH
10295/* Same as above, but for ints (isel). */
10296
10297static int
a2369ed3 10298rs6000_emit_int_cmove (rtx dest, rtx op, rtx true_cond, rtx false_cond)
a3170dc6
AH
10299{
10300 rtx condition_rtx, cr;
10301
10302 /* All isel implementations thus far are 32-bits. */
10303 if (GET_MODE (rs6000_compare_op0) != SImode)
10304 return 0;
10305
10306 /* We still have to do the compare, because isel doesn't do a
10307 compare, it just looks at the CRx bits set by a previous compare
10308 instruction. */
10309 condition_rtx = rs6000_generate_compare (GET_CODE (op));
10310 cr = XEXP (condition_rtx, 0);
10311
10312 if (GET_MODE (cr) == CCmode)
10313 emit_insn (gen_isel_signed (dest, condition_rtx,
10314 true_cond, false_cond, cr));
10315 else
10316 emit_insn (gen_isel_unsigned (dest, condition_rtx,
10317 true_cond, false_cond, cr));
10318
10319 return 1;
10320}
10321
10322const char *
a2369ed3 10323output_isel (rtx *operands)
a3170dc6
AH
10324{
10325 enum rtx_code code;
10326
10327 code = GET_CODE (operands[1]);
10328 if (code == GE || code == GEU || code == LE || code == LEU || code == NE)
10329 {
10330 PUT_CODE (operands[1], reverse_condition (code));
10331 return "isel %0,%3,%2,%j1";
10332 }
10333 else
10334 return "isel %0,%2,%3,%j1";
10335}
10336
50a0b056 10337void
a2369ed3 10338rs6000_emit_minmax (rtx dest, enum rtx_code code, rtx op0, rtx op1)
50a0b056
GK
10339{
10340 enum machine_mode mode = GET_MODE (op0);
5dc8d536 10341 enum rtx_code c;
50a0b056 10342 rtx target;
5dc8d536
AH
10343
10344 if (code == SMAX || code == SMIN)
10345 c = GE;
10346 else
10347 c = GEU;
10348
50a0b056 10349 if (code == SMAX || code == UMAX)
5dc8d536 10350 target = emit_conditional_move (dest, c, op0, op1, mode,
50a0b056
GK
10351 op0, op1, mode, 0);
10352 else
5dc8d536 10353 target = emit_conditional_move (dest, c, op0, op1, mode,
50a0b056
GK
10354 op1, op0, mode, 0);
10355 if (target == NULL_RTX)
10356 abort ();
10357 if (target != dest)
10358 emit_move_insn (dest, target);
10359}
46c07df8 10360
a9baceb1
GK
10361/* Emit instructions to move SRC to DST. Called by splitters for
10362 multi-register moves. It will emit at most one instruction for
10363 each register that is accessed; that is, it won't emit li/lis pairs
10364 (or equivalent for 64-bit code). One of SRC or DST must be a hard
10365 register. */
46c07df8 10366
46c07df8 10367void
a9baceb1 10368rs6000_split_multireg_move (rtx dst, rtx src)
46c07df8 10369{
a9baceb1
GK
10370 /* The register number of the first register being moved. */
10371 int reg;
10372 /* The mode that is to be moved. */
10373 enum machine_mode mode;
10374 /* The mode that the move is being done in, and its size. */
10375 enum machine_mode reg_mode;
10376 int reg_mode_size;
10377 /* The number of registers that will be moved. */
10378 int nregs;
10379
10380 reg = REG_P (dst) ? REGNO (dst) : REGNO (src);
10381 mode = GET_MODE (dst);
10382 nregs = HARD_REGNO_NREGS (reg, mode);
10383 if (FP_REGNO_P (reg))
10384 reg_mode = DFmode;
10385 else if (ALTIVEC_REGNO_P (reg))
10386 reg_mode = V16QImode;
10387 else
10388 reg_mode = word_mode;
10389 reg_mode_size = GET_MODE_SIZE (reg_mode);
46c07df8 10390
a9baceb1
GK
10391 if (reg_mode_size * nregs != GET_MODE_SIZE (mode))
10392 abort ();
10393
10394 if (REG_P (src) && REG_P (dst) && (REGNO (src) < REGNO (dst)))
10395 {
10396 /* Move register range backwards, if we might have destructive
10397 overlap. */
10398 int i;
10399 for (i = nregs - 1; i >= 0; i--)
10400 emit_insn (gen_rtx_SET (VOIDmode,
10401 simplify_gen_subreg (reg_mode, dst, mode,
10402 i * reg_mode_size),
10403 simplify_gen_subreg (reg_mode, src, mode,
10404 i * reg_mode_size)));
10405 }
46c07df8
HP
10406 else
10407 {
a9baceb1
GK
10408 int i;
10409 int j = -1;
10410 bool used_update = false;
46c07df8 10411
a9baceb1 10412 if (GET_CODE (src) == MEM && INT_REGNO_P (reg))
46c07df8
HP
10413 {
10414 rtx breg;
3a1f863f 10415
a9baceb1
GK
10416 if (GET_CODE (XEXP (src, 0)) == PRE_INC
10417 || GET_CODE (XEXP (src, 0)) == PRE_DEC)
3a1f863f
DE
10418 {
10419 rtx delta_rtx;
a9baceb1
GK
10420 breg = XEXP (XEXP (src, 0), 0);
10421 delta_rtx = GET_CODE (XEXP (src, 0)) == PRE_INC
10422 ? GEN_INT (GET_MODE_SIZE (GET_MODE (src)))
10423 : GEN_INT (-GET_MODE_SIZE (GET_MODE (src)));
10424 emit_insn (TARGET_32BIT
10425 ? gen_addsi3 (breg, breg, delta_rtx)
10426 : gen_adddi3 (breg, breg, delta_rtx));
3a1f863f
DE
10427 src = gen_rtx_MEM (mode, breg);
10428 }
10429
10430 /* We have now address involving an base register only.
10431 If we use one of the registers to address memory,
10432 we have change that register last. */
10433
10434 breg = (GET_CODE (XEXP (src, 0)) == PLUS
10435 ? XEXP (XEXP (src, 0), 0)
10436 : XEXP (src, 0));
10437
10438 if (!REG_P (breg))
10439 abort();
10440
10441 if (REGNO (breg) >= REGNO (dst)
10442 && REGNO (breg) < REGNO (dst) + nregs)
10443 j = REGNO (breg) - REGNO (dst);
46c07df8
HP
10444 }
10445
a9baceb1 10446 if (GET_CODE (dst) == MEM && INT_REGNO_P (reg))
3a1f863f
DE
10447 {
10448 rtx breg;
10449
a9baceb1
GK
10450 if (GET_CODE (XEXP (dst, 0)) == PRE_INC
10451 || GET_CODE (XEXP (dst, 0)) == PRE_DEC)
3a1f863f
DE
10452 {
10453 rtx delta_rtx;
a9baceb1
GK
10454 breg = XEXP (XEXP (dst, 0), 0);
10455 delta_rtx = GET_CODE (XEXP (dst, 0)) == PRE_INC
10456 ? GEN_INT (GET_MODE_SIZE (GET_MODE (dst)))
10457 : GEN_INT (-GET_MODE_SIZE (GET_MODE (dst)));
3a1f863f
DE
10458
10459 /* We have to update the breg before doing the store.
10460 Use store with update, if available. */
10461
10462 if (TARGET_UPDATE)
10463 {
a9baceb1
GK
10464 rtx nsrc = simplify_gen_subreg (reg_mode, src, mode, 0);
10465 emit_insn (TARGET_32BIT
10466 ? gen_movsi_update (breg, breg, delta_rtx, nsrc)
10467 : gen_movdi_update (breg, breg, delta_rtx, nsrc));
10468 used_update = true;
3a1f863f
DE
10469 }
10470 else
a9baceb1
GK
10471 emit_insn (TARGET_32BIT
10472 ? gen_addsi3 (breg, breg, delta_rtx)
10473 : gen_adddi3 (breg, breg, delta_rtx));
3a1f863f
DE
10474 dst = gen_rtx_MEM (mode, breg);
10475 }
10476 }
10477
46c07df8 10478 for (i = 0; i < nregs; i++)
3a1f863f
DE
10479 {
10480 /* Calculate index to next subword. */
10481 ++j;
10482 if (j == nregs)
10483 j = 0;
46c07df8 10484
a9baceb1
GK
10485 /* If compiler already emited move of first word by
10486 store with update, no need to do anything. */
3a1f863f 10487 if (j == 0 && used_update)
a9baceb1
GK
10488 continue;
10489
10490 emit_insn (gen_rtx_SET (VOIDmode,
10491 simplify_gen_subreg (reg_mode, dst, mode,
10492 j * reg_mode_size),
10493 simplify_gen_subreg (reg_mode, src, mode,
10494 j * reg_mode_size)));
3a1f863f 10495 }
46c07df8
HP
10496 }
10497}
10498
12a4e8c5 10499\f
a4f6c312
SS
10500/* This page contains routines that are used to determine what the
10501 function prologue and epilogue code will do and write them out. */
9878760c 10502
a4f6c312
SS
10503/* Return the first fixed-point register that is required to be
10504 saved. 32 if none. */
9878760c
RK
10505
10506int
863d938c 10507first_reg_to_save (void)
9878760c
RK
10508{
10509 int first_reg;
10510
10511 /* Find lowest numbered live register. */
10512 for (first_reg = 13; first_reg <= 31; first_reg++)
a38d360d
GK
10513 if (regs_ever_live[first_reg]
10514 && (! call_used_regs[first_reg]
1db02437 10515 || (first_reg == RS6000_PIC_OFFSET_TABLE_REGNUM
14f00213 10516 && ((DEFAULT_ABI == ABI_V4 && flag_pic != 0)
f607bc57 10517 || (DEFAULT_ABI == ABI_DARWIN && flag_pic)))))
9878760c
RK
10518 break;
10519
ee890fe2 10520#if TARGET_MACHO
93638d7a
AM
10521 if (flag_pic
10522 && current_function_uses_pic_offset_table
10523 && first_reg > RS6000_PIC_OFFSET_TABLE_REGNUM)
1db02437 10524 return RS6000_PIC_OFFSET_TABLE_REGNUM;
ee890fe2
SS
10525#endif
10526
9878760c
RK
10527 return first_reg;
10528}
10529
10530/* Similar, for FP regs. */
10531
10532int
863d938c 10533first_fp_reg_to_save (void)
9878760c
RK
10534{
10535 int first_reg;
10536
10537 /* Find lowest numbered live register. */
10538 for (first_reg = 14 + 32; first_reg <= 63; first_reg++)
10539 if (regs_ever_live[first_reg])
10540 break;
10541
10542 return first_reg;
10543}
00b960c7
AH
10544
10545/* Similar, for AltiVec regs. */
10546
10547static int
863d938c 10548first_altivec_reg_to_save (void)
00b960c7
AH
10549{
10550 int i;
10551
10552 /* Stack frame remains as is unless we are in AltiVec ABI. */
10553 if (! TARGET_ALTIVEC_ABI)
10554 return LAST_ALTIVEC_REGNO + 1;
10555
10556 /* Find lowest numbered live register. */
10557 for (i = FIRST_ALTIVEC_REGNO + 20; i <= LAST_ALTIVEC_REGNO; ++i)
10558 if (regs_ever_live[i])
10559 break;
10560
10561 return i;
10562}
10563
10564/* Return a 32-bit mask of the AltiVec registers we need to set in
10565 VRSAVE. Bit n of the return value is 1 if Vn is live. The MSB in
10566 the 32-bit word is 0. */
10567
10568static unsigned int
863d938c 10569compute_vrsave_mask (void)
00b960c7
AH
10570{
10571 unsigned int i, mask = 0;
10572
10573 /* First, find out if we use _any_ altivec registers. */
10574 for (i = FIRST_ALTIVEC_REGNO; i <= LAST_ALTIVEC_REGNO; ++i)
10575 if (regs_ever_live[i])
10576 mask |= ALTIVEC_REG_BIT (i);
10577
10578 if (mask == 0)
10579 return mask;
10580
00b960c7
AH
10581 /* Next, remove the argument registers from the set. These must
10582 be in the VRSAVE mask set by the caller, so we don't need to add
10583 them in again. More importantly, the mask we compute here is
10584 used to generate CLOBBERs in the set_vrsave insn, and we do not
10585 wish the argument registers to die. */
a6cf80f2 10586 for (i = cfun->args_info.vregno - 1; i >= ALTIVEC_ARG_MIN_REG; --i)
00b960c7
AH
10587 mask &= ~ALTIVEC_REG_BIT (i);
10588
10589 /* Similarly, remove the return value from the set. */
10590 {
10591 bool yes = false;
10592 diddle_return_value (is_altivec_return_reg, &yes);
10593 if (yes)
10594 mask &= ~ALTIVEC_REG_BIT (ALTIVEC_ARG_RETURN);
10595 }
10596
10597 return mask;
10598}
10599
10600static void
a2369ed3 10601is_altivec_return_reg (rtx reg, void *xyes)
00b960c7
AH
10602{
10603 bool *yes = (bool *) xyes;
10604 if (REGNO (reg) == ALTIVEC_ARG_RETURN)
10605 *yes = true;
10606}
10607
4697a36c
MM
10608\f
10609/* Calculate the stack information for the current function. This is
10610 complicated by having two separate calling sequences, the AIX calling
10611 sequence and the V.4 calling sequence.
10612
592696dd 10613 AIX (and Darwin/Mac OS X) stack frames look like:
a260abc9 10614 32-bit 64-bit
4697a36c 10615 SP----> +---------------------------------------+
a260abc9 10616 | back chain to caller | 0 0
4697a36c 10617 +---------------------------------------+
a260abc9 10618 | saved CR | 4 8 (8-11)
4697a36c 10619 +---------------------------------------+
a260abc9 10620 | saved LR | 8 16
4697a36c 10621 +---------------------------------------+
a260abc9 10622 | reserved for compilers | 12 24
4697a36c 10623 +---------------------------------------+
a260abc9 10624 | reserved for binders | 16 32
4697a36c 10625 +---------------------------------------+
a260abc9 10626 | saved TOC pointer | 20 40
4697a36c 10627 +---------------------------------------+
a260abc9 10628 | Parameter save area (P) | 24 48
4697a36c 10629 +---------------------------------------+
a260abc9 10630 | Alloca space (A) | 24+P etc.
802a0058 10631 +---------------------------------------+
a7df97e6 10632 | Local variable space (L) | 24+P+A
4697a36c 10633 +---------------------------------------+
a7df97e6 10634 | Float/int conversion temporary (X) | 24+P+A+L
4697a36c 10635 +---------------------------------------+
00b960c7
AH
10636 | Save area for AltiVec registers (W) | 24+P+A+L+X
10637 +---------------------------------------+
10638 | AltiVec alignment padding (Y) | 24+P+A+L+X+W
10639 +---------------------------------------+
10640 | Save area for VRSAVE register (Z) | 24+P+A+L+X+W+Y
4697a36c 10641 +---------------------------------------+
00b960c7
AH
10642 | Save area for GP registers (G) | 24+P+A+X+L+X+W+Y+Z
10643 +---------------------------------------+
10644 | Save area for FP registers (F) | 24+P+A+X+L+X+W+Y+Z+G
4697a36c
MM
10645 +---------------------------------------+
10646 old SP->| back chain to caller's caller |
10647 +---------------------------------------+
10648
5376a30c
KR
10649 The required alignment for AIX configurations is two words (i.e., 8
10650 or 16 bytes).
10651
10652
4697a36c
MM
10653 V.4 stack frames look like:
10654
10655 SP----> +---------------------------------------+
10656 | back chain to caller | 0
10657 +---------------------------------------+
5eb387b8 10658 | caller's saved LR | 4
4697a36c
MM
10659 +---------------------------------------+
10660 | Parameter save area (P) | 8
10661 +---------------------------------------+
a7df97e6
MM
10662 | Alloca space (A) | 8+P
10663 +---------------------------------------+
10664 | Varargs save area (V) | 8+P+A
10665 +---------------------------------------+
10666 | Local variable space (L) | 8+P+A+V
10667 +---------------------------------------+
10668 | Float/int conversion temporary (X) | 8+P+A+V+L
4697a36c 10669 +---------------------------------------+
00b960c7
AH
10670 | Save area for AltiVec registers (W) | 8+P+A+V+L+X
10671 +---------------------------------------+
10672 | AltiVec alignment padding (Y) | 8+P+A+V+L+X+W
10673 +---------------------------------------+
10674 | Save area for VRSAVE register (Z) | 8+P+A+V+L+X+W+Y
10675 +---------------------------------------+
a3170dc6
AH
10676 | SPE: area for 64-bit GP registers |
10677 +---------------------------------------+
10678 | SPE alignment padding |
10679 +---------------------------------------+
00b960c7 10680 | saved CR (C) | 8+P+A+V+L+X+W+Y+Z
a7df97e6 10681 +---------------------------------------+
00b960c7 10682 | Save area for GP registers (G) | 8+P+A+V+L+X+W+Y+Z+C
a7df97e6 10683 +---------------------------------------+
00b960c7 10684 | Save area for FP registers (F) | 8+P+A+V+L+X+W+Y+Z+C+G
4697a36c
MM
10685 +---------------------------------------+
10686 old SP->| back chain to caller's caller |
10687 +---------------------------------------+
b6c9286a 10688
5376a30c
KR
10689 The required alignment for V.4 is 16 bytes, or 8 bytes if -meabi is
10690 given. (But note below and in sysv4.h that we require only 8 and
10691 may round up the size of our stack frame anyways. The historical
10692 reason is early versions of powerpc-linux which didn't properly
10693 align the stack at program startup. A happy side-effect is that
10694 -mno-eabi libraries can be used with -meabi programs.)
10695
50d440bc 10696 The EABI configuration defaults to the V.4 layout. However,
5376a30c
KR
10697 the stack alignment requirements may differ. If -mno-eabi is not
10698 given, the required stack alignment is 8 bytes; if -mno-eabi is
10699 given, the required alignment is 16 bytes. (But see V.4 comment
10700 above.) */
4697a36c 10701
61b2fbe7
MM
10702#ifndef ABI_STACK_BOUNDARY
10703#define ABI_STACK_BOUNDARY STACK_BOUNDARY
10704#endif
10705
d1d0c603 10706static rs6000_stack_t *
863d938c 10707rs6000_stack_info (void)
4697a36c
MM
10708{
10709 static rs6000_stack_t info, zero_info;
10710 rs6000_stack_t *info_ptr = &info;
327e5343 10711 int reg_size = TARGET_32BIT ? 4 : 8;
83720594 10712 int ehrd_size;
d1d0c603 10713 HOST_WIDE_INT total_raw_size;
4697a36c 10714
a4f6c312 10715 /* Zero all fields portably. */
4697a36c
MM
10716 info = zero_info;
10717
c19de7aa
AH
10718 if (TARGET_SPE)
10719 {
10720 /* Cache value so we don't rescan instruction chain over and over. */
9b7b447f
AH
10721 if (cfun->machine->insn_chain_scanned_p == 0)
10722 {
10723 cfun->machine->insn_chain_scanned_p = 1;
10724 info_ptr->spe_64bit_regs_used = (int) spe_func_has_64bit_regs_p ();
10725 }
c19de7aa
AH
10726 }
10727
a4f6c312 10728 /* Select which calling sequence. */
178274da 10729 info_ptr->abi = DEFAULT_ABI;
9878760c 10730
a4f6c312 10731 /* Calculate which registers need to be saved & save area size. */
4697a36c 10732 info_ptr->first_gp_reg_save = first_reg_to_save ();
1db02437 10733 /* Assume that we will have to save RS6000_PIC_OFFSET_TABLE_REGNUM,
906fb125 10734 even if it currently looks like we won't. */
2bfcf297 10735 if (((TARGET_TOC && TARGET_MINIMAL_TOC)
178274da
AM
10736 || (flag_pic == 1 && DEFAULT_ABI == ABI_V4)
10737 || (flag_pic && DEFAULT_ABI == ABI_DARWIN))
1db02437
FS
10738 && info_ptr->first_gp_reg_save > RS6000_PIC_OFFSET_TABLE_REGNUM)
10739 info_ptr->gp_size = reg_size * (32 - RS6000_PIC_OFFSET_TABLE_REGNUM);
906fb125
GK
10740 else
10741 info_ptr->gp_size = reg_size * (32 - info_ptr->first_gp_reg_save);
4697a36c 10742
a3170dc6
AH
10743 /* For the SPE, we have an additional upper 32-bits on each GPR.
10744 Ideally we should save the entire 64-bits only when the upper
10745 half is used in SIMD instructions. Since we only record
10746 registers live (not the size they are used in), this proves
10747 difficult because we'd have to traverse the instruction chain at
10748 the right time, taking reload into account. This is a real pain,
c19de7aa
AH
10749 so we opt to save the GPRs in 64-bits always if but one register
10750 gets used in 64-bits. Otherwise, all the registers in the frame
10751 get saved in 32-bits.
a3170dc6 10752
c19de7aa 10753 So... since when we save all GPRs (except the SP) in 64-bits, the
a3170dc6 10754 traditional GP save area will be empty. */
c19de7aa 10755 if (TARGET_SPE_ABI && info_ptr->spe_64bit_regs_used != 0)
a3170dc6
AH
10756 info_ptr->gp_size = 0;
10757
4697a36c
MM
10758 info_ptr->first_fp_reg_save = first_fp_reg_to_save ();
10759 info_ptr->fp_size = 8 * (64 - info_ptr->first_fp_reg_save);
10760
00b960c7
AH
10761 info_ptr->first_altivec_reg_save = first_altivec_reg_to_save ();
10762 info_ptr->altivec_size = 16 * (LAST_ALTIVEC_REGNO + 1
10763 - info_ptr->first_altivec_reg_save);
10764
592696dd 10765 /* Does this function call anything? */
71f123ca
FS
10766 info_ptr->calls_p = (! current_function_is_leaf
10767 || cfun->machine->ra_needs_full_frame);
b6c9286a 10768
a4f6c312 10769 /* Determine if we need to save the link register. */
71f123ca 10770 if (rs6000_ra_ever_killed ()
ffcfcb5f
AM
10771 || (DEFAULT_ABI == ABI_AIX
10772 && current_function_profile
10773 && !TARGET_PROFILE_KERNEL)
4697a36c
MM
10774#ifdef TARGET_RELOCATABLE
10775 || (TARGET_RELOCATABLE && (get_pool_size () != 0))
10776#endif
10777 || (info_ptr->first_fp_reg_save != 64
10778 && !FP_SAVE_INLINE (info_ptr->first_fp_reg_save))
00b960c7 10779 || info_ptr->first_altivec_reg_save <= LAST_ALTIVEC_REGNO
178274da 10780 || (DEFAULT_ABI == ABI_V4 && current_function_calls_alloca)
a4f6c312
SS
10781 || (DEFAULT_ABI == ABI_DARWIN
10782 && flag_pic
10783 && current_function_uses_pic_offset_table)
4697a36c
MM
10784 || info_ptr->calls_p)
10785 {
10786 info_ptr->lr_save_p = 1;
9ebbca7d 10787 regs_ever_live[LINK_REGISTER_REGNUM] = 1;
4697a36c
MM
10788 }
10789
9ebbca7d
GK
10790 /* Determine if we need to save the condition code registers. */
10791 if (regs_ever_live[CR2_REGNO]
10792 || regs_ever_live[CR3_REGNO]
10793 || regs_ever_live[CR4_REGNO])
4697a36c
MM
10794 {
10795 info_ptr->cr_save_p = 1;
178274da 10796 if (DEFAULT_ABI == ABI_V4)
4697a36c
MM
10797 info_ptr->cr_size = reg_size;
10798 }
10799
83720594
RH
10800 /* If the current function calls __builtin_eh_return, then we need
10801 to allocate stack space for registers that will hold data for
10802 the exception handler. */
10803 if (current_function_calls_eh_return)
10804 {
10805 unsigned int i;
10806 for (i = 0; EH_RETURN_DATA_REGNO (i) != INVALID_REGNUM; ++i)
10807 continue;
a3170dc6
AH
10808
10809 /* SPE saves EH registers in 64-bits. */
c19de7aa
AH
10810 ehrd_size = i * (TARGET_SPE_ABI
10811 && info_ptr->spe_64bit_regs_used != 0
10812 ? UNITS_PER_SPE_WORD : UNITS_PER_WORD);
83720594
RH
10813 }
10814 else
10815 ehrd_size = 0;
10816
592696dd 10817 /* Determine various sizes. */
4697a36c
MM
10818 info_ptr->reg_size = reg_size;
10819 info_ptr->fixed_size = RS6000_SAVE_AREA;
10820 info_ptr->varargs_size = RS6000_VARARGS_AREA;
189e03e3 10821 info_ptr->vars_size = RS6000_ALIGN (get_frame_size (), 8);
a4f6c312
SS
10822 info_ptr->parm_size = RS6000_ALIGN (current_function_outgoing_args_size,
10823 8);
00b960c7 10824
c19de7aa 10825 if (TARGET_SPE_ABI && info_ptr->spe_64bit_regs_used != 0)
a3170dc6
AH
10826 info_ptr->spe_gp_size = 8 * (32 - info_ptr->first_gp_reg_save);
10827 else
10828 info_ptr->spe_gp_size = 0;
10829
4d774ff8
HP
10830 if (TARGET_ALTIVEC_ABI)
10831 info_ptr->vrsave_mask = compute_vrsave_mask ();
00b960c7 10832 else
4d774ff8
HP
10833 info_ptr->vrsave_mask = 0;
10834
10835 if (TARGET_ALTIVEC_VRSAVE && info_ptr->vrsave_mask)
10836 info_ptr->vrsave_size = 4;
10837 else
10838 info_ptr->vrsave_size = 0;
b6c9286a 10839
592696dd 10840 /* Calculate the offsets. */
178274da 10841 switch (DEFAULT_ABI)
4697a36c 10842 {
b6c9286a 10843 case ABI_NONE:
24d304eb 10844 default:
b6c9286a
MM
10845 abort ();
10846
10847 case ABI_AIX:
ee890fe2 10848 case ABI_DARWIN:
b6c9286a
MM
10849 info_ptr->fp_save_offset = - info_ptr->fp_size;
10850 info_ptr->gp_save_offset = info_ptr->fp_save_offset - info_ptr->gp_size;
00b960c7
AH
10851
10852 if (TARGET_ALTIVEC_ABI)
10853 {
10854 info_ptr->vrsave_save_offset
10855 = info_ptr->gp_save_offset - info_ptr->vrsave_size;
10856
10857 /* Align stack so vector save area is on a quadword boundary. */
10858 if (info_ptr->altivec_size != 0)
10859 info_ptr->altivec_padding_size
10860 = 16 - (-info_ptr->vrsave_save_offset % 16);
10861 else
10862 info_ptr->altivec_padding_size = 0;
10863
10864 info_ptr->altivec_save_offset
10865 = info_ptr->vrsave_save_offset
10866 - info_ptr->altivec_padding_size
10867 - info_ptr->altivec_size;
10868
10869 /* Adjust for AltiVec case. */
10870 info_ptr->ehrd_offset = info_ptr->altivec_save_offset - ehrd_size;
10871 }
10872 else
10873 info_ptr->ehrd_offset = info_ptr->gp_save_offset - ehrd_size;
a260abc9
DE
10874 info_ptr->cr_save_offset = reg_size; /* first word when 64-bit. */
10875 info_ptr->lr_save_offset = 2*reg_size;
24d304eb
RK
10876 break;
10877
10878 case ABI_V4:
b6c9286a
MM
10879 info_ptr->fp_save_offset = - info_ptr->fp_size;
10880 info_ptr->gp_save_offset = info_ptr->fp_save_offset - info_ptr->gp_size;
a7df97e6 10881 info_ptr->cr_save_offset = info_ptr->gp_save_offset - info_ptr->cr_size;
00b960c7 10882
c19de7aa 10883 if (TARGET_SPE_ABI && info_ptr->spe_64bit_regs_used != 0)
a3170dc6
AH
10884 {
10885 /* Align stack so SPE GPR save area is aligned on a
10886 double-word boundary. */
10887 if (info_ptr->spe_gp_size != 0)
10888 info_ptr->spe_padding_size
10889 = 8 - (-info_ptr->cr_save_offset % 8);
10890 else
10891 info_ptr->spe_padding_size = 0;
10892
10893 info_ptr->spe_gp_save_offset
10894 = info_ptr->cr_save_offset
10895 - info_ptr->spe_padding_size
10896 - info_ptr->spe_gp_size;
10897
10898 /* Adjust for SPE case. */
10899 info_ptr->toc_save_offset
10900 = info_ptr->spe_gp_save_offset - info_ptr->toc_size;
10901 }
10902 else if (TARGET_ALTIVEC_ABI)
00b960c7
AH
10903 {
10904 info_ptr->vrsave_save_offset
10905 = info_ptr->cr_save_offset - info_ptr->vrsave_size;
10906
10907 /* Align stack so vector save area is on a quadword boundary. */
10908 if (info_ptr->altivec_size != 0)
10909 info_ptr->altivec_padding_size
10910 = 16 - (-info_ptr->vrsave_save_offset % 16);
10911 else
10912 info_ptr->altivec_padding_size = 0;
10913
10914 info_ptr->altivec_save_offset
10915 = info_ptr->vrsave_save_offset
10916 - info_ptr->altivec_padding_size
10917 - info_ptr->altivec_size;
10918
10919 /* Adjust for AltiVec case. */
10920 info_ptr->toc_save_offset
10921 = info_ptr->altivec_save_offset - info_ptr->toc_size;
10922 }
10923 else
10924 info_ptr->toc_save_offset = info_ptr->cr_save_offset - info_ptr->toc_size;
83720594 10925 info_ptr->ehrd_offset = info_ptr->toc_save_offset - ehrd_size;
b6c9286a
MM
10926 info_ptr->lr_save_offset = reg_size;
10927 break;
4697a36c
MM
10928 }
10929
00b960c7
AH
10930 info_ptr->save_size = RS6000_ALIGN (info_ptr->fp_size
10931 + info_ptr->gp_size
10932 + info_ptr->altivec_size
10933 + info_ptr->altivec_padding_size
a3170dc6
AH
10934 + info_ptr->spe_gp_size
10935 + info_ptr->spe_padding_size
00b960c7
AH
10936 + ehrd_size
10937 + info_ptr->cr_size
10938 + info_ptr->lr_size
10939 + info_ptr->vrsave_size
10940 + info_ptr->toc_size,
10941 (TARGET_ALTIVEC_ABI || ABI_DARWIN)
10942 ? 16 : 8);
10943
ff381587
MM
10944 total_raw_size = (info_ptr->vars_size
10945 + info_ptr->parm_size
ff381587
MM
10946 + info_ptr->save_size
10947 + info_ptr->varargs_size
10948 + info_ptr->fixed_size);
10949
a4f6c312
SS
10950 info_ptr->total_size =
10951 RS6000_ALIGN (total_raw_size, ABI_STACK_BOUNDARY / BITS_PER_UNIT);
ff381587
MM
10952
10953 /* Determine if we need to allocate any stack frame:
10954
a4f6c312
SS
10955 For AIX we need to push the stack if a frame pointer is needed
10956 (because the stack might be dynamically adjusted), if we are
10957 debugging, if we make calls, or if the sum of fp_save, gp_save,
10958 and local variables are more than the space needed to save all
10959 non-volatile registers: 32-bit: 18*8 + 19*4 = 220 or 64-bit: 18*8
10960 + 18*8 = 288 (GPR13 reserved).
ff381587 10961
a4f6c312
SS
10962 For V.4 we don't have the stack cushion that AIX uses, but assume
10963 that the debugger can handle stackless frames. */
ff381587
MM
10964
10965 if (info_ptr->calls_p)
10966 info_ptr->push_p = 1;
10967
178274da 10968 else if (DEFAULT_ABI == ABI_V4)
e72247f4 10969 info_ptr->push_p = total_raw_size > info_ptr->fixed_size;
ff381587 10970
178274da
AM
10971 else if (frame_pointer_needed)
10972 info_ptr->push_p = 1;
10973
10974 else if (TARGET_XCOFF && write_symbols != NO_DEBUG)
10975 info_ptr->push_p = 1;
10976
ff381587 10977 else
178274da
AM
10978 info_ptr->push_p
10979 = total_raw_size - info_ptr->fixed_size > (TARGET_32BIT ? 220 : 288);
ff381587 10980
a4f6c312 10981 /* Zero offsets if we're not saving those registers. */
8dda1a21 10982 if (info_ptr->fp_size == 0)
4697a36c
MM
10983 info_ptr->fp_save_offset = 0;
10984
8dda1a21 10985 if (info_ptr->gp_size == 0)
4697a36c
MM
10986 info_ptr->gp_save_offset = 0;
10987
00b960c7
AH
10988 if (! TARGET_ALTIVEC_ABI || info_ptr->altivec_size == 0)
10989 info_ptr->altivec_save_offset = 0;
10990
10991 if (! TARGET_ALTIVEC_ABI || info_ptr->vrsave_mask == 0)
10992 info_ptr->vrsave_save_offset = 0;
10993
c19de7aa
AH
10994 if (! TARGET_SPE_ABI
10995 || info_ptr->spe_64bit_regs_used == 0
10996 || info_ptr->spe_gp_size == 0)
a3170dc6
AH
10997 info_ptr->spe_gp_save_offset = 0;
10998
c81fc13e 10999 if (! info_ptr->lr_save_p)
4697a36c
MM
11000 info_ptr->lr_save_offset = 0;
11001
c81fc13e 11002 if (! info_ptr->cr_save_p)
4697a36c
MM
11003 info_ptr->cr_save_offset = 0;
11004
c81fc13e 11005 if (! info_ptr->toc_save_p)
b6c9286a
MM
11006 info_ptr->toc_save_offset = 0;
11007
4697a36c
MM
11008 return info_ptr;
11009}
11010
c19de7aa
AH
11011/* Return true if the current function uses any GPRs in 64-bit SIMD
11012 mode. */
11013
11014static bool
863d938c 11015spe_func_has_64bit_regs_p (void)
c19de7aa
AH
11016{
11017 rtx insns, insn;
11018
11019 /* Functions that save and restore all the call-saved registers will
11020 need to save/restore the registers in 64-bits. */
11021 if (current_function_calls_eh_return
11022 || current_function_calls_setjmp
11023 || current_function_has_nonlocal_goto)
11024 return true;
11025
11026 insns = get_insns ();
11027
11028 for (insn = NEXT_INSN (insns); insn != NULL_RTX; insn = NEXT_INSN (insn))
11029 {
11030 if (INSN_P (insn))
11031 {
11032 rtx i;
11033
11034 i = PATTERN (insn);
11035 if (GET_CODE (i) == SET
11036 && SPE_VECTOR_MODE (GET_MODE (SET_SRC (i))))
11037 return true;
11038 }
11039 }
11040
11041 return false;
11042}
11043
d1d0c603 11044static void
a2369ed3 11045debug_stack_info (rs6000_stack_t *info)
9878760c 11046{
d330fd93 11047 const char *abi_string;
24d304eb 11048
c81fc13e 11049 if (! info)
4697a36c
MM
11050 info = rs6000_stack_info ();
11051
11052 fprintf (stderr, "\nStack information for function %s:\n",
11053 ((current_function_decl && DECL_NAME (current_function_decl))
11054 ? IDENTIFIER_POINTER (DECL_NAME (current_function_decl))
11055 : "<unknown>"));
11056
24d304eb
RK
11057 switch (info->abi)
11058 {
b6c9286a
MM
11059 default: abi_string = "Unknown"; break;
11060 case ABI_NONE: abi_string = "NONE"; break;
50d440bc 11061 case ABI_AIX: abi_string = "AIX"; break;
ee890fe2 11062 case ABI_DARWIN: abi_string = "Darwin"; break;
b6c9286a 11063 case ABI_V4: abi_string = "V.4"; break;
24d304eb
RK
11064 }
11065
11066 fprintf (stderr, "\tABI = %5s\n", abi_string);
11067
00b960c7
AH
11068 if (TARGET_ALTIVEC_ABI)
11069 fprintf (stderr, "\tALTIVEC ABI extensions enabled.\n");
11070
a3170dc6
AH
11071 if (TARGET_SPE_ABI)
11072 fprintf (stderr, "\tSPE ABI extensions enabled.\n");
11073
4697a36c
MM
11074 if (info->first_gp_reg_save != 32)
11075 fprintf (stderr, "\tfirst_gp_reg_save = %5d\n", info->first_gp_reg_save);
11076
11077 if (info->first_fp_reg_save != 64)
11078 fprintf (stderr, "\tfirst_fp_reg_save = %5d\n", info->first_fp_reg_save);
9878760c 11079
00b960c7
AH
11080 if (info->first_altivec_reg_save <= LAST_ALTIVEC_REGNO)
11081 fprintf (stderr, "\tfirst_altivec_reg_save = %5d\n",
11082 info->first_altivec_reg_save);
11083
4697a36c
MM
11084 if (info->lr_save_p)
11085 fprintf (stderr, "\tlr_save_p = %5d\n", info->lr_save_p);
9878760c 11086
4697a36c
MM
11087 if (info->cr_save_p)
11088 fprintf (stderr, "\tcr_save_p = %5d\n", info->cr_save_p);
11089
b6c9286a
MM
11090 if (info->toc_save_p)
11091 fprintf (stderr, "\ttoc_save_p = %5d\n", info->toc_save_p);
11092
00b960c7
AH
11093 if (info->vrsave_mask)
11094 fprintf (stderr, "\tvrsave_mask = 0x%x\n", info->vrsave_mask);
11095
4697a36c
MM
11096 if (info->push_p)
11097 fprintf (stderr, "\tpush_p = %5d\n", info->push_p);
11098
11099 if (info->calls_p)
11100 fprintf (stderr, "\tcalls_p = %5d\n", info->calls_p);
11101
4697a36c
MM
11102 if (info->gp_save_offset)
11103 fprintf (stderr, "\tgp_save_offset = %5d\n", info->gp_save_offset);
11104
11105 if (info->fp_save_offset)
11106 fprintf (stderr, "\tfp_save_offset = %5d\n", info->fp_save_offset);
11107
00b960c7
AH
11108 if (info->altivec_save_offset)
11109 fprintf (stderr, "\taltivec_save_offset = %5d\n",
11110 info->altivec_save_offset);
11111
a3170dc6
AH
11112 if (info->spe_gp_save_offset)
11113 fprintf (stderr, "\tspe_gp_save_offset = %5d\n",
11114 info->spe_gp_save_offset);
11115
00b960c7
AH
11116 if (info->vrsave_save_offset)
11117 fprintf (stderr, "\tvrsave_save_offset = %5d\n",
11118 info->vrsave_save_offset);
11119
4697a36c
MM
11120 if (info->lr_save_offset)
11121 fprintf (stderr, "\tlr_save_offset = %5d\n", info->lr_save_offset);
11122
11123 if (info->cr_save_offset)
11124 fprintf (stderr, "\tcr_save_offset = %5d\n", info->cr_save_offset);
11125
b6c9286a
MM
11126 if (info->toc_save_offset)
11127 fprintf (stderr, "\ttoc_save_offset = %5d\n", info->toc_save_offset);
11128
4697a36c
MM
11129 if (info->varargs_save_offset)
11130 fprintf (stderr, "\tvarargs_save_offset = %5d\n", info->varargs_save_offset);
11131
11132 if (info->total_size)
d1d0c603
JJ
11133 fprintf (stderr, "\ttotal_size = "HOST_WIDE_INT_PRINT_DEC"\n",
11134 info->total_size);
4697a36c
MM
11135
11136 if (info->varargs_size)
11137 fprintf (stderr, "\tvarargs_size = %5d\n", info->varargs_size);
11138
11139 if (info->vars_size)
d1d0c603
JJ
11140 fprintf (stderr, "\tvars_size = "HOST_WIDE_INT_PRINT_DEC"\n",
11141 info->vars_size);
4697a36c
MM
11142
11143 if (info->parm_size)
11144 fprintf (stderr, "\tparm_size = %5d\n", info->parm_size);
11145
11146 if (info->fixed_size)
11147 fprintf (stderr, "\tfixed_size = %5d\n", info->fixed_size);
11148
11149 if (info->gp_size)
11150 fprintf (stderr, "\tgp_size = %5d\n", info->gp_size);
11151
a3170dc6
AH
11152 if (info->spe_gp_size)
11153 fprintf (stderr, "\tspe_gp_size = %5d\n", info->spe_gp_size);
11154
4697a36c
MM
11155 if (info->fp_size)
11156 fprintf (stderr, "\tfp_size = %5d\n", info->fp_size);
11157
00b960c7
AH
11158 if (info->altivec_size)
11159 fprintf (stderr, "\taltivec_size = %5d\n", info->altivec_size);
11160
11161 if (info->vrsave_size)
11162 fprintf (stderr, "\tvrsave_size = %5d\n", info->vrsave_size);
11163
11164 if (info->altivec_padding_size)
11165 fprintf (stderr, "\taltivec_padding_size= %5d\n",
11166 info->altivec_padding_size);
11167
a3170dc6
AH
11168 if (info->spe_padding_size)
11169 fprintf (stderr, "\tspe_padding_size = %5d\n",
11170 info->spe_padding_size);
11171
a4f6c312 11172 if (info->lr_size)
ed947a96 11173 fprintf (stderr, "\tlr_size = %5d\n", info->lr_size);
b6c9286a 11174
4697a36c
MM
11175 if (info->cr_size)
11176 fprintf (stderr, "\tcr_size = %5d\n", info->cr_size);
11177
a4f6c312 11178 if (info->toc_size)
b6c9286a
MM
11179 fprintf (stderr, "\ttoc_size = %5d\n", info->toc_size);
11180
4697a36c
MM
11181 if (info->save_size)
11182 fprintf (stderr, "\tsave_size = %5d\n", info->save_size);
11183
11184 if (info->reg_size != 4)
11185 fprintf (stderr, "\treg_size = %5d\n", info->reg_size);
11186
11187 fprintf (stderr, "\n");
9878760c 11188}
71f123ca
FS
11189
11190rtx
a2369ed3 11191rs6000_return_addr (int count, rtx frame)
71f123ca 11192{
a4f6c312
SS
11193 /* Currently we don't optimize very well between prolog and body
11194 code and for PIC code the code can be actually quite bad, so
11195 don't try to be too clever here. */
f1384257 11196 if (count != 0 || (DEFAULT_ABI != ABI_AIX && flag_pic))
71f123ca
FS
11197 {
11198 cfun->machine->ra_needs_full_frame = 1;
8ac61af7
RK
11199
11200 return
11201 gen_rtx_MEM
11202 (Pmode,
11203 memory_address
11204 (Pmode,
11205 plus_constant (copy_to_reg
11206 (gen_rtx_MEM (Pmode,
11207 memory_address (Pmode, frame))),
11208 RETURN_ADDRESS_OFFSET)));
71f123ca
FS
11209 }
11210
8c29550d 11211 cfun->machine->ra_need_lr = 1;
9e2f7ec7 11212 return get_hard_reg_initial_val (Pmode, LINK_REGISTER_REGNUM);
71f123ca
FS
11213}
11214
5e1bf043
DJ
11215/* Say whether a function is a candidate for sibcall handling or not.
11216 We do not allow indirect calls to be optimized into sibling calls.
11217 Also, we can't do it if there are any vector parameters; there's
11218 nowhere to put the VRsave code so it works; note that functions with
11219 vector parameters are required to have a prototype, so the argument
11220 type info must be available here. (The tail recursion case can work
11221 with vector parameters, but there's no way to distinguish here.) */
4977bab6 11222static bool
a2369ed3 11223rs6000_function_ok_for_sibcall (tree decl, tree exp ATTRIBUTE_UNUSED)
5e1bf043
DJ
11224{
11225 tree type;
4977bab6 11226 if (decl)
5e1bf043
DJ
11227 {
11228 if (TARGET_ALTIVEC_VRSAVE)
11229 {
4977bab6 11230 for (type = TYPE_ARG_TYPES (TREE_TYPE (decl));
5e1bf043
DJ
11231 type; type = TREE_CHAIN (type))
11232 {
c15b529f 11233 if (TREE_CODE (TREE_VALUE (type)) == VECTOR_TYPE)
4977bab6 11234 return false;
5e1bf043
DJ
11235 }
11236 }
11237 if (DEFAULT_ABI == ABI_DARWIN
4977bab6 11238 || (*targetm.binds_local_p) (decl))
2bcc50d0 11239 {
4977bab6 11240 tree attr_list = TYPE_ATTRIBUTES (TREE_TYPE (decl));
2bcc50d0
AM
11241
11242 if (!lookup_attribute ("longcall", attr_list)
11243 || lookup_attribute ("shortcall", attr_list))
4977bab6 11244 return true;
2bcc50d0 11245 }
5e1bf043 11246 }
4977bab6 11247 return false;
5e1bf043
DJ
11248}
11249
71f123ca 11250static int
863d938c 11251rs6000_ra_ever_killed (void)
71f123ca
FS
11252{
11253 rtx top;
5e1bf043
DJ
11254 rtx reg;
11255 rtx insn;
71f123ca 11256
dd292d0a 11257 if (current_function_is_thunk)
71f123ca 11258 return 0;
eb0424da 11259
36f7e964
AH
11260 /* regs_ever_live has LR marked as used if any sibcalls are present,
11261 but this should not force saving and restoring in the
11262 pro/epilogue. Likewise, reg_set_between_p thinks a sibcall
a3c9585f 11263 clobbers LR, so that is inappropriate. */
36f7e964 11264
5e1bf043
DJ
11265 /* Also, the prologue can generate a store into LR that
11266 doesn't really count, like this:
36f7e964 11267
5e1bf043
DJ
11268 move LR->R0
11269 bcl to set PIC register
11270 move LR->R31
11271 move R0->LR
36f7e964
AH
11272
11273 When we're called from the epilogue, we need to avoid counting
11274 this as a store. */
5e1bf043 11275
71f123ca
FS
11276 push_topmost_sequence ();
11277 top = get_insns ();
11278 pop_topmost_sequence ();
5e1bf043 11279 reg = gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM);
71f123ca 11280
5e1bf043
DJ
11281 for (insn = NEXT_INSN (top); insn != NULL_RTX; insn = NEXT_INSN (insn))
11282 {
11283 if (INSN_P (insn))
11284 {
11285 if (FIND_REG_INC_NOTE (insn, reg))
11286 return 1;
11287 else if (GET_CODE (insn) == CALL_INSN
c15b529f 11288 && !SIBLING_CALL_P (insn))
5e1bf043 11289 return 1;
36f7e964
AH
11290 else if (set_of (reg, insn) != NULL_RTX
11291 && !prologue_epilogue_contains (insn))
5e1bf043
DJ
11292 return 1;
11293 }
11294 }
11295 return 0;
71f123ca 11296}
4697a36c 11297\f
8cd8f856
GK
11298/* Add a REG_MAYBE_DEAD note to the insn. */
11299static void
a2369ed3 11300rs6000_maybe_dead (rtx insn)
8cd8f856
GK
11301{
11302 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_MAYBE_DEAD,
11303 const0_rtx,
11304 REG_NOTES (insn));
11305}
11306
9ebbca7d 11307/* Emit instructions needed to load the TOC register.
c7ca610e 11308 This is only needed when TARGET_TOC, TARGET_MINIMAL_TOC, and there is
9ebbca7d 11309 a constant pool; or for SVR4 -fpic. */
c7ca610e
RK
11310
11311void
a2369ed3 11312rs6000_emit_load_toc_table (int fromprolog)
c7ca610e 11313{
027fbf43 11314 rtx dest, insn;
1db02437 11315 dest = gen_rtx_REG (Pmode, RS6000_PIC_OFFSET_TABLE_REGNUM);
c7ca610e 11316
20b71b17
AM
11317 if (TARGET_ELF && DEFAULT_ABI == ABI_V4 && flag_pic == 1)
11318 {
11319 rtx temp = (fromprolog
11320 ? gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM)
11321 : gen_reg_rtx (Pmode));
027fbf43
JJ
11322 insn = emit_insn (gen_load_toc_v4_pic_si (temp));
11323 if (fromprolog)
11324 rs6000_maybe_dead (insn);
11325 insn = emit_move_insn (dest, temp);
11326 if (fromprolog)
11327 rs6000_maybe_dead (insn);
20b71b17
AM
11328 }
11329 else if (TARGET_ELF && DEFAULT_ABI != ABI_AIX && flag_pic == 2)
11330 {
11331 char buf[30];
11332 rtx tempLR = (fromprolog
11333 ? gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM)
11334 : gen_reg_rtx (Pmode));
11335 rtx temp0 = (fromprolog
11336 ? gen_rtx_REG (Pmode, 0)
11337 : gen_reg_rtx (Pmode));
11338 rtx symF;
11339
11340 /* possibly create the toc section */
11341 if (! toc_initialized)
38c1f2d7 11342 {
20b71b17
AM
11343 toc_section ();
11344 function_section (current_function_decl);
38c1f2d7 11345 }
9ebbca7d 11346
20b71b17
AM
11347 if (fromprolog)
11348 {
11349 rtx symL;
38c1f2d7 11350
20b71b17
AM
11351 ASM_GENERATE_INTERNAL_LABEL (buf, "LCF", rs6000_pic_labelno);
11352 symF = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
9ebbca7d 11353
20b71b17
AM
11354 ASM_GENERATE_INTERNAL_LABEL (buf, "LCL", rs6000_pic_labelno);
11355 symL = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
11356
11357 rs6000_maybe_dead (emit_insn (gen_load_toc_v4_PIC_1 (tempLR,
11358 symF)));
11359 rs6000_maybe_dead (emit_move_insn (dest, tempLR));
11360 rs6000_maybe_dead (emit_insn (gen_load_toc_v4_PIC_2 (temp0, dest,
11361 symL,
11362 symF)));
9ebbca7d
GK
11363 }
11364 else
20b71b17
AM
11365 {
11366 rtx tocsym;
11367 static int reload_toc_labelno = 0;
11368
11369 tocsym = gen_rtx_SYMBOL_REF (Pmode, toc_label_name);
11370
11371 ASM_GENERATE_INTERNAL_LABEL (buf, "LCG", reload_toc_labelno++);
11372 symF = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
11373
027fbf43
JJ
11374 emit_insn (gen_load_toc_v4_PIC_1b (tempLR, symF, tocsym));
11375 emit_move_insn (dest, tempLR);
11376 emit_move_insn (temp0, gen_rtx_MEM (Pmode, dest));
20b71b17 11377 }
027fbf43
JJ
11378 insn = emit_insn (gen_addsi3 (dest, temp0, dest));
11379 if (fromprolog)
11380 rs6000_maybe_dead (insn);
9ebbca7d 11381 }
20b71b17
AM
11382 else if (TARGET_ELF && !TARGET_AIX && flag_pic == 0 && TARGET_MINIMAL_TOC)
11383 {
11384 /* This is for AIX code running in non-PIC ELF32. */
11385 char buf[30];
11386 rtx realsym;
11387 ASM_GENERATE_INTERNAL_LABEL (buf, "LCTOC", 1);
11388 realsym = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
11389
027fbf43
JJ
11390 insn = emit_insn (gen_elf_high (dest, realsym));
11391 if (fromprolog)
11392 rs6000_maybe_dead (insn);
11393 insn = emit_insn (gen_elf_low (dest, dest, realsym));
11394 if (fromprolog)
11395 rs6000_maybe_dead (insn);
20b71b17
AM
11396 }
11397 else if (DEFAULT_ABI == ABI_AIX)
9ebbca7d
GK
11398 {
11399 if (TARGET_32BIT)
027fbf43 11400 insn = emit_insn (gen_load_toc_aix_si (dest));
9ebbca7d 11401 else
027fbf43
JJ
11402 insn = emit_insn (gen_load_toc_aix_di (dest));
11403 if (fromprolog)
11404 rs6000_maybe_dead (insn);
9ebbca7d 11405 }
20b71b17
AM
11406 else
11407 abort ();
9ebbca7d
GK
11408}
11409
d1d0c603
JJ
11410/* Emit instructions to restore the link register after determining where
11411 its value has been stored. */
11412
11413void
11414rs6000_emit_eh_reg_restore (rtx source, rtx scratch)
11415{
11416 rs6000_stack_t *info = rs6000_stack_info ();
11417 rtx operands[2];
11418
11419 operands[0] = source;
11420 operands[1] = scratch;
11421
11422 if (info->lr_save_p)
11423 {
11424 rtx frame_rtx = stack_pointer_rtx;
11425 HOST_WIDE_INT sp_offset = 0;
11426 rtx tmp;
11427
11428 if (frame_pointer_needed
11429 || current_function_calls_alloca
11430 || info->total_size > 32767)
11431 {
11432 emit_move_insn (operands[1], gen_rtx_MEM (Pmode, frame_rtx));
11433 frame_rtx = operands[1];
11434 }
11435 else if (info->push_p)
11436 sp_offset = info->total_size;
11437
11438 tmp = plus_constant (frame_rtx, info->lr_save_offset + sp_offset);
11439 tmp = gen_rtx_MEM (Pmode, tmp);
11440 emit_move_insn (tmp, operands[0]);
11441 }
11442 else
11443 emit_move_insn (gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM), operands[0]);
11444}
11445
f103e34d
GK
11446static GTY(()) int set = -1;
11447
9ebbca7d 11448int
863d938c 11449get_TOC_alias_set (void)
9ebbca7d 11450{
f103e34d
GK
11451 if (set == -1)
11452 set = new_alias_set ();
11453 return set;
9ebbca7d
GK
11454}
11455
c1207243 11456/* This returns nonzero if the current function uses the TOC. This is
3c9eb5f4
AM
11457 determined by the presence of (use (unspec ... UNSPEC_TOC)), which
11458 is generated by the ABI_V4 load_toc_* patterns. */
c954844a 11459#if TARGET_ELF
3c9eb5f4 11460static int
38f391a5 11461uses_TOC (void)
9ebbca7d 11462{
c4501e62 11463 rtx insn;
38c1f2d7 11464
c4501e62
JJ
11465 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
11466 if (INSN_P (insn))
11467 {
11468 rtx pat = PATTERN (insn);
11469 int i;
9ebbca7d 11470
c4501e62
JJ
11471 if (GET_CODE (pat) == PARALLEL)
11472 for (i = 0; i < XVECLEN (pat, 0); i++)
11473 {
11474 rtx sub = XVECEXP (pat, 0, i);
11475 if (GET_CODE (sub) == USE)
11476 {
11477 sub = XEXP (sub, 0);
11478 if (GET_CODE (sub) == UNSPEC
11479 && XINT (sub, 1) == UNSPEC_TOC)
11480 return 1;
11481 }
11482 }
11483 }
11484 return 0;
9ebbca7d 11485}
c954844a 11486#endif
38c1f2d7 11487
9ebbca7d 11488rtx
a2369ed3 11489create_TOC_reference (rtx symbol)
9ebbca7d 11490{
a8a05998
ZW
11491 return gen_rtx_PLUS (Pmode,
11492 gen_rtx_REG (Pmode, TOC_REGISTER),
11493 gen_rtx_CONST (Pmode,
11494 gen_rtx_MINUS (Pmode, symbol,
b999aaeb 11495 gen_rtx_SYMBOL_REF (Pmode, toc_label_name))));
9ebbca7d 11496}
38c1f2d7 11497
fc4767bb
JJ
11498/* If _Unwind_* has been called from within the same module,
11499 toc register is not guaranteed to be saved to 40(1) on function
11500 entry. Save it there in that case. */
c7ca610e 11501
9ebbca7d 11502void
863d938c 11503rs6000_aix_emit_builtin_unwind_init (void)
9ebbca7d
GK
11504{
11505 rtx mem;
11506 rtx stack_top = gen_reg_rtx (Pmode);
11507 rtx opcode_addr = gen_reg_rtx (Pmode);
fc4767bb
JJ
11508 rtx opcode = gen_reg_rtx (SImode);
11509 rtx tocompare = gen_reg_rtx (SImode);
11510 rtx no_toc_save_needed = gen_label_rtx ();
9ebbca7d
GK
11511
11512 mem = gen_rtx_MEM (Pmode, hard_frame_pointer_rtx);
11513 emit_move_insn (stack_top, mem);
11514
fc4767bb
JJ
11515 mem = gen_rtx_MEM (Pmode,
11516 gen_rtx_PLUS (Pmode, stack_top,
9ebbca7d
GK
11517 GEN_INT (2 * GET_MODE_SIZE (Pmode))));
11518 emit_move_insn (opcode_addr, mem);
fc4767bb
JJ
11519 emit_move_insn (opcode, gen_rtx_MEM (SImode, opcode_addr));
11520 emit_move_insn (tocompare, gen_int_mode (TARGET_32BIT ? 0x80410014
2496c7bd 11521 : 0xE8410028, SImode));
9ebbca7d 11522
fc4767bb 11523 do_compare_rtx_and_jump (opcode, tocompare, EQ, 1,
06f4e019 11524 SImode, NULL_RTX, NULL_RTX,
fc4767bb 11525 no_toc_save_needed);
9ebbca7d 11526
fc4767bb
JJ
11527 mem = gen_rtx_MEM (Pmode,
11528 gen_rtx_PLUS (Pmode, stack_top,
11529 GEN_INT (5 * GET_MODE_SIZE (Pmode))));
11530 emit_move_insn (mem, gen_rtx_REG (Pmode, 2));
11531 emit_label (no_toc_save_needed);
9ebbca7d 11532}
38c1f2d7 11533\f
ba4828e0
RK
11534/* This ties together stack memory (MEM with an alias set of
11535 rs6000_sr_alias_set) and the change to the stack pointer. */
11536
9ebbca7d 11537static void
863d938c 11538rs6000_emit_stack_tie (void)
9ebbca7d 11539{
ba4828e0
RK
11540 rtx mem = gen_rtx_MEM (BLKmode, gen_rtx_REG (Pmode, STACK_POINTER_REGNUM));
11541
11542 set_mem_alias_set (mem, rs6000_sr_alias_set);
9ebbca7d
GK
11543 emit_insn (gen_stack_tie (mem));
11544}
38c1f2d7 11545
9ebbca7d
GK
11546/* Emit the correct code for allocating stack space, as insns.
11547 If COPY_R12, make sure a copy of the old frame is left in r12.
11548 The generated code may use hard register 0 as a temporary. */
11549
11550static void
a2369ed3 11551rs6000_emit_allocate_stack (HOST_WIDE_INT size, int copy_r12)
38c1f2d7 11552{
9ebbca7d
GK
11553 rtx insn;
11554 rtx stack_reg = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
11555 rtx tmp_reg = gen_rtx_REG (Pmode, 0);
11556 rtx todec = GEN_INT (-size);
a157febd
GK
11557
11558 if (current_function_limit_stack)
11559 {
11560 if (REG_P (stack_limit_rtx)
11561 && REGNO (stack_limit_rtx) > 1
11562 && REGNO (stack_limit_rtx) <= 31)
11563 {
5b71a4e7 11564 emit_insn (TARGET_32BIT
9ebbca7d
GK
11565 ? gen_addsi3 (tmp_reg,
11566 stack_limit_rtx,
11567 GEN_INT (size))
11568 : gen_adddi3 (tmp_reg,
11569 stack_limit_rtx,
11570 GEN_INT (size)));
5b71a4e7 11571
9ebbca7d
GK
11572 emit_insn (gen_cond_trap (LTU, stack_reg, tmp_reg,
11573 const0_rtx));
a157febd
GK
11574 }
11575 else if (GET_CODE (stack_limit_rtx) == SYMBOL_REF
9ebbca7d 11576 && TARGET_32BIT
f607bc57 11577 && DEFAULT_ABI == ABI_V4)
a157febd 11578 {
9ebbca7d
GK
11579 rtx toload = gen_rtx_CONST (VOIDmode,
11580 gen_rtx_PLUS (Pmode,
11581 stack_limit_rtx,
11582 GEN_INT (size)));
5b71a4e7 11583
9ebbca7d
GK
11584 emit_insn (gen_elf_high (tmp_reg, toload));
11585 emit_insn (gen_elf_low (tmp_reg, tmp_reg, toload));
11586 emit_insn (gen_cond_trap (LTU, stack_reg, tmp_reg,
11587 const0_rtx));
a157febd
GK
11588 }
11589 else
11590 warning ("stack limit expression is not supported");
11591 }
11592
9ebbca7d
GK
11593 if (copy_r12 || ! TARGET_UPDATE)
11594 emit_move_insn (gen_rtx_REG (Pmode, 12), stack_reg);
11595
38c1f2d7
MM
11596 if (TARGET_UPDATE)
11597 {
9ebbca7d 11598 if (size > 32767)
38c1f2d7 11599 {
9ebbca7d
GK
11600 /* Need a note here so that try_split doesn't get confused. */
11601 if (get_last_insn() == NULL_RTX)
2e040219 11602 emit_note (NOTE_INSN_DELETED);
9ebbca7d
GK
11603 insn = emit_move_insn (tmp_reg, todec);
11604 try_split (PATTERN (insn), insn, 0);
11605 todec = tmp_reg;
38c1f2d7 11606 }
5b71a4e7
DE
11607
11608 insn = emit_insn (TARGET_32BIT
11609 ? gen_movsi_update (stack_reg, stack_reg,
11610 todec, stack_reg)
11611 : gen_movdi_update (stack_reg, stack_reg,
9ebbca7d 11612 todec, stack_reg));
38c1f2d7
MM
11613 }
11614 else
11615 {
5b71a4e7
DE
11616 insn = emit_insn (TARGET_32BIT
11617 ? gen_addsi3 (stack_reg, stack_reg, todec)
11618 : gen_adddi3 (stack_reg, stack_reg, todec));
9ebbca7d
GK
11619 emit_move_insn (gen_rtx_MEM (Pmode, stack_reg),
11620 gen_rtx_REG (Pmode, 12));
11621 }
5b71a4e7 11622
9ebbca7d
GK
11623 RTX_FRAME_RELATED_P (insn) = 1;
11624 REG_NOTES (insn) =
11625 gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
11626 gen_rtx_SET (VOIDmode, stack_reg,
11627 gen_rtx_PLUS (Pmode, stack_reg,
11628 GEN_INT (-size))),
11629 REG_NOTES (insn));
11630}
11631
a4f6c312
SS
11632/* Add to 'insn' a note which is PATTERN (INSN) but with REG replaced
11633 with (plus:P (reg 1) VAL), and with REG2 replaced with RREG if REG2
11634 is not NULL. It would be nice if dwarf2out_frame_debug_expr could
11635 deduce these equivalences by itself so it wasn't necessary to hold
11636 its hand so much. */
9ebbca7d
GK
11637
11638static void
a2369ed3
DJ
11639rs6000_frame_related (rtx insn, rtx reg, HOST_WIDE_INT val,
11640 rtx reg2, rtx rreg)
9ebbca7d
GK
11641{
11642 rtx real, temp;
11643
e56c4463
JL
11644 /* copy_rtx will not make unique copies of registers, so we need to
11645 ensure we don't have unwanted sharing here. */
11646 if (reg == reg2)
11647 reg = gen_raw_REG (GET_MODE (reg), REGNO (reg));
11648
11649 if (reg == rreg)
11650 reg = gen_raw_REG (GET_MODE (reg), REGNO (reg));
11651
9ebbca7d
GK
11652 real = copy_rtx (PATTERN (insn));
11653
89e7058f
AH
11654 if (reg2 != NULL_RTX)
11655 real = replace_rtx (real, reg2, rreg);
11656
9ebbca7d
GK
11657 real = replace_rtx (real, reg,
11658 gen_rtx_PLUS (Pmode, gen_rtx_REG (Pmode,
11659 STACK_POINTER_REGNUM),
11660 GEN_INT (val)));
11661
11662 /* We expect that 'real' is either a SET or a PARALLEL containing
11663 SETs (and possibly other stuff). In a PARALLEL, all the SETs
11664 are important so they all have to be marked RTX_FRAME_RELATED_P. */
11665
11666 if (GET_CODE (real) == SET)
11667 {
11668 rtx set = real;
11669
11670 temp = simplify_rtx (SET_SRC (set));
11671 if (temp)
11672 SET_SRC (set) = temp;
11673 temp = simplify_rtx (SET_DEST (set));
11674 if (temp)
11675 SET_DEST (set) = temp;
11676 if (GET_CODE (SET_DEST (set)) == MEM)
38c1f2d7 11677 {
9ebbca7d
GK
11678 temp = simplify_rtx (XEXP (SET_DEST (set), 0));
11679 if (temp)
11680 XEXP (SET_DEST (set), 0) = temp;
38c1f2d7 11681 }
38c1f2d7 11682 }
9ebbca7d
GK
11683 else if (GET_CODE (real) == PARALLEL)
11684 {
11685 int i;
11686 for (i = 0; i < XVECLEN (real, 0); i++)
11687 if (GET_CODE (XVECEXP (real, 0, i)) == SET)
11688 {
11689 rtx set = XVECEXP (real, 0, i);
11690
11691 temp = simplify_rtx (SET_SRC (set));
11692 if (temp)
11693 SET_SRC (set) = temp;
11694 temp = simplify_rtx (SET_DEST (set));
11695 if (temp)
11696 SET_DEST (set) = temp;
11697 if (GET_CODE (SET_DEST (set)) == MEM)
11698 {
11699 temp = simplify_rtx (XEXP (SET_DEST (set), 0));
11700 if (temp)
11701 XEXP (SET_DEST (set), 0) = temp;
11702 }
11703 RTX_FRAME_RELATED_P (set) = 1;
11704 }
11705 }
11706 else
a4f6c312 11707 abort ();
c19de7aa
AH
11708
11709 if (TARGET_SPE)
11710 real = spe_synthesize_frame_save (real);
11711
9ebbca7d
GK
11712 RTX_FRAME_RELATED_P (insn) = 1;
11713 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
11714 real,
11715 REG_NOTES (insn));
38c1f2d7
MM
11716}
11717
c19de7aa
AH
11718/* Given an SPE frame note, return a PARALLEL of SETs with the
11719 original note, plus a synthetic register save. */
11720
11721static rtx
a2369ed3 11722spe_synthesize_frame_save (rtx real)
c19de7aa
AH
11723{
11724 rtx synth, offset, reg, real2;
11725
11726 if (GET_CODE (real) != SET
11727 || GET_MODE (SET_SRC (real)) != V2SImode)
11728 return real;
11729
11730 /* For the SPE, registers saved in 64-bits, get a PARALLEL for their
11731 frame related note. The parallel contains a set of the register
41f3a930 11732 being saved, and another set to a synthetic register (n+1200).
c19de7aa
AH
11733 This is so we can differentiate between 64-bit and 32-bit saves.
11734 Words cannot describe this nastiness. */
11735
11736 if (GET_CODE (SET_DEST (real)) != MEM
11737 || GET_CODE (XEXP (SET_DEST (real), 0)) != PLUS
11738 || GET_CODE (SET_SRC (real)) != REG)
11739 abort ();
11740
11741 /* Transform:
11742 (set (mem (plus (reg x) (const y)))
11743 (reg z))
11744 into:
11745 (set (mem (plus (reg x) (const y+4)))
41f3a930 11746 (reg z+1200))
c19de7aa
AH
11747 */
11748
11749 real2 = copy_rtx (real);
11750 PUT_MODE (SET_DEST (real2), SImode);
11751 reg = SET_SRC (real2);
11752 real2 = replace_rtx (real2, reg, gen_rtx_REG (SImode, REGNO (reg)));
11753 synth = copy_rtx (real2);
11754
11755 if (BYTES_BIG_ENDIAN)
11756 {
11757 offset = XEXP (XEXP (SET_DEST (real2), 0), 1);
11758 real2 = replace_rtx (real2, offset, GEN_INT (INTVAL (offset) + 4));
11759 }
11760
11761 reg = SET_SRC (synth);
41f3a930 11762
c19de7aa 11763 synth = replace_rtx (synth, reg,
41f3a930 11764 gen_rtx_REG (SImode, REGNO (reg) + 1200));
c19de7aa
AH
11765
11766 offset = XEXP (XEXP (SET_DEST (synth), 0), 1);
11767 synth = replace_rtx (synth, offset,
11768 GEN_INT (INTVAL (offset)
11769 + (BYTES_BIG_ENDIAN ? 0 : 4)));
11770
11771 RTX_FRAME_RELATED_P (synth) = 1;
11772 RTX_FRAME_RELATED_P (real2) = 1;
11773 if (BYTES_BIG_ENDIAN)
11774 real = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, synth, real2));
11775 else
11776 real = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, real2, synth));
11777
11778 return real;
11779}
11780
00b960c7
AH
11781/* Returns an insn that has a vrsave set operation with the
11782 appropriate CLOBBERs. */
11783
11784static rtx
a2369ed3 11785generate_set_vrsave (rtx reg, rs6000_stack_t *info, int epiloguep)
00b960c7
AH
11786{
11787 int nclobs, i;
11788 rtx insn, clobs[TOTAL_ALTIVEC_REGS + 1];
a004eb82 11789 rtx vrsave = gen_rtx_REG (SImode, VRSAVE_REGNO);
00b960c7 11790
a004eb82
AH
11791 clobs[0]
11792 = gen_rtx_SET (VOIDmode,
11793 vrsave,
11794 gen_rtx_UNSPEC_VOLATILE (SImode,
11795 gen_rtvec (2, reg, vrsave),
11796 30));
00b960c7
AH
11797
11798 nclobs = 1;
11799
9aa86737
AH
11800 /* We need to clobber the registers in the mask so the scheduler
11801 does not move sets to VRSAVE before sets of AltiVec registers.
11802
11803 However, if the function receives nonlocal gotos, reload will set
11804 all call saved registers live. We will end up with:
11805
11806 (set (reg 999) (mem))
11807 (parallel [ (set (reg vrsave) (unspec blah))
11808 (clobber (reg 999))])
11809
11810 The clobber will cause the store into reg 999 to be dead, and
11811 flow will attempt to delete an epilogue insn. In this case, we
11812 need an unspec use/set of the register. */
00b960c7
AH
11813
11814 for (i = FIRST_ALTIVEC_REGNO; i <= LAST_ALTIVEC_REGNO; ++i)
11815 if (info->vrsave_mask != 0 && ALTIVEC_REG_BIT (i) != 0)
9aa86737
AH
11816 {
11817 if (!epiloguep || call_used_regs [i])
11818 clobs[nclobs++] = gen_rtx_CLOBBER (VOIDmode,
11819 gen_rtx_REG (V4SImode, i));
11820 else
11821 {
11822 rtx reg = gen_rtx_REG (V4SImode, i);
9aa86737
AH
11823
11824 clobs[nclobs++]
a004eb82
AH
11825 = gen_rtx_SET (VOIDmode,
11826 reg,
11827 gen_rtx_UNSPEC (V4SImode,
11828 gen_rtvec (1, reg), 27));
9aa86737
AH
11829 }
11830 }
00b960c7
AH
11831
11832 insn = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (nclobs));
11833
11834 for (i = 0; i < nclobs; ++i)
11835 XVECEXP (insn, 0, i) = clobs[i];
11836
11837 return insn;
11838}
11839
89e7058f
AH
11840/* Save a register into the frame, and emit RTX_FRAME_RELATED_P notes.
11841 Save REGNO into [FRAME_REG + OFFSET] in mode MODE. */
11842
11843static void
a2369ed3 11844emit_frame_save (rtx frame_reg, rtx frame_ptr, enum machine_mode mode,
d1d0c603 11845 unsigned int regno, int offset, HOST_WIDE_INT total_size)
89e7058f
AH
11846{
11847 rtx reg, offset_rtx, insn, mem, addr, int_rtx;
11848 rtx replacea, replaceb;
11849
11850 int_rtx = GEN_INT (offset);
11851
11852 /* Some cases that need register indexed addressing. */
11853 if ((TARGET_ALTIVEC_ABI && ALTIVEC_VECTOR_MODE (mode))
a3170dc6
AH
11854 || (TARGET_SPE_ABI
11855 && SPE_VECTOR_MODE (mode)
11856 && !SPE_CONST_OFFSET_OK (offset)))
89e7058f
AH
11857 {
11858 /* Whomever calls us must make sure r11 is available in the
11859 flow path of instructions in the prologue. */
11860 offset_rtx = gen_rtx_REG (Pmode, 11);
11861 emit_move_insn (offset_rtx, int_rtx);
11862
11863 replacea = offset_rtx;
11864 replaceb = int_rtx;
11865 }
11866 else
11867 {
11868 offset_rtx = int_rtx;
11869 replacea = NULL_RTX;
11870 replaceb = NULL_RTX;
11871 }
11872
11873 reg = gen_rtx_REG (mode, regno);
11874 addr = gen_rtx_PLUS (Pmode, frame_reg, offset_rtx);
11875 mem = gen_rtx_MEM (mode, addr);
11876 set_mem_alias_set (mem, rs6000_sr_alias_set);
11877
11878 insn = emit_move_insn (mem, reg);
11879
11880 rs6000_frame_related (insn, frame_ptr, total_size, replacea, replaceb);
11881}
11882
a3170dc6
AH
11883/* Emit an offset memory reference suitable for a frame store, while
11884 converting to a valid addressing mode. */
11885
11886static rtx
a2369ed3 11887gen_frame_mem_offset (enum machine_mode mode, rtx reg, int offset)
a3170dc6
AH
11888{
11889 rtx int_rtx, offset_rtx;
11890
11891 int_rtx = GEN_INT (offset);
11892
11893 if (TARGET_SPE_ABI && SPE_VECTOR_MODE (mode))
11894 {
11895 offset_rtx = gen_rtx_REG (Pmode, FIXED_SCRATCH);
11896 emit_move_insn (offset_rtx, int_rtx);
11897 }
11898 else
11899 offset_rtx = int_rtx;
11900
11901 return gen_rtx_MEM (mode, gen_rtx_PLUS (Pmode, reg, offset_rtx));
11902}
11903
9ebbca7d
GK
11904/* Emit function prologue as insns. */
11905
9878760c 11906void
863d938c 11907rs6000_emit_prologue (void)
9878760c 11908{
4697a36c 11909 rs6000_stack_t *info = rs6000_stack_info ();
0e67400a 11910 enum machine_mode reg_mode = Pmode;
327e5343 11911 int reg_size = TARGET_32BIT ? 4 : 8;
9ebbca7d
GK
11912 rtx sp_reg_rtx = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
11913 rtx frame_ptr_rtx = gen_rtx_REG (Pmode, 12);
11914 rtx frame_reg_rtx = sp_reg_rtx;
b78d48dd 11915 rtx cr_save_rtx = NULL_RTX;
9ebbca7d
GK
11916 rtx insn;
11917 int saving_FPRs_inline;
11918 int using_store_multiple;
11919 HOST_WIDE_INT sp_offset = 0;
11920
c19de7aa 11921 if (TARGET_SPE_ABI && info->spe_64bit_regs_used != 0)
a3170dc6
AH
11922 {
11923 reg_mode = V2SImode;
11924 reg_size = 8;
11925 }
11926
9ebbca7d 11927 using_store_multiple = (TARGET_MULTIPLE && ! TARGET_POWERPC64
c19de7aa
AH
11928 && (!TARGET_SPE_ABI
11929 || info->spe_64bit_regs_used == 0)
9ebbca7d
GK
11930 && info->first_gp_reg_save < 31);
11931 saving_FPRs_inline = (info->first_fp_reg_save == 64
8c29550d 11932 || FP_SAVE_INLINE (info->first_fp_reg_save)
acd0b319 11933 || current_function_calls_eh_return
8c29550d 11934 || cfun->machine->ra_need_lr);
9ebbca7d
GK
11935
11936 /* For V.4, update stack before we do any saving and set back pointer. */
fc4767bb 11937 if (info->push_p
acd0b319
AM
11938 && (DEFAULT_ABI == ABI_V4
11939 || current_function_calls_eh_return))
9ebbca7d
GK
11940 {
11941 if (info->total_size < 32767)
11942 sp_offset = info->total_size;
11943 else
11944 frame_reg_rtx = frame_ptr_rtx;
11945 rs6000_emit_allocate_stack (info->total_size,
11946 (frame_reg_rtx != sp_reg_rtx
11947 && (info->cr_save_p
11948 || info->lr_save_p
11949 || info->first_fp_reg_save < 64
11950 || info->first_gp_reg_save < 32
11951 )));
11952 if (frame_reg_rtx != sp_reg_rtx)
11953 rs6000_emit_stack_tie ();
11954 }
11955
9aa86737
AH
11956 /* Save AltiVec registers if needed. */
11957 if (TARGET_ALTIVEC_ABI && info->altivec_size != 0)
11958 {
11959 int i;
11960
11961 /* There should be a non inline version of this, for when we
11962 are saving lots of vector registers. */
11963 for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
11964 if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
11965 {
11966 rtx areg, savereg, mem;
11967 int offset;
11968
11969 offset = info->altivec_save_offset + sp_offset
11970 + 16 * (i - info->first_altivec_reg_save);
11971
11972 savereg = gen_rtx_REG (V4SImode, i);
11973
11974 areg = gen_rtx_REG (Pmode, 0);
11975 emit_move_insn (areg, GEN_INT (offset));
11976
11977 /* AltiVec addressing mode is [reg+reg]. */
11978 mem = gen_rtx_MEM (V4SImode,
11979 gen_rtx_PLUS (Pmode, frame_reg_rtx, areg));
11980
11981 set_mem_alias_set (mem, rs6000_sr_alias_set);
11982
11983 insn = emit_move_insn (mem, savereg);
11984
5c242421
SB
11985 rs6000_frame_related (insn, frame_ptr_rtx, info->total_size,
11986 areg, GEN_INT (offset));
9aa86737
AH
11987 }
11988 }
11989
11990 /* VRSAVE is a bit vector representing which AltiVec registers
11991 are used. The OS uses this to determine which vector
11992 registers to save on a context switch. We need to save
11993 VRSAVE on the stack frame, add whatever AltiVec registers we
11994 used in this function, and do the corresponding magic in the
11995 epilogue. */
11996
4d774ff8
HP
11997 if (TARGET_ALTIVEC && TARGET_ALTIVEC_VRSAVE
11998 && info->vrsave_mask != 0)
9aa86737 11999 {
a004eb82 12000 rtx reg, mem, vrsave;
9aa86737
AH
12001 int offset;
12002
12003 /* Get VRSAVE onto a GPR. */
12004 reg = gen_rtx_REG (SImode, 12);
a004eb82 12005 vrsave = gen_rtx_REG (SImode, VRSAVE_REGNO);
b188f760
AH
12006 if (TARGET_MACHO)
12007 emit_insn (gen_get_vrsave_internal (reg));
12008 else
12009 emit_insn (gen_rtx_SET (VOIDmode, reg, vrsave));
9aa86737
AH
12010
12011 /* Save VRSAVE. */
12012 offset = info->vrsave_save_offset + sp_offset;
12013 mem
12014 = gen_rtx_MEM (SImode,
12015 gen_rtx_PLUS (Pmode, frame_reg_rtx, GEN_INT (offset)));
12016 set_mem_alias_set (mem, rs6000_sr_alias_set);
12017 insn = emit_move_insn (mem, reg);
12018
12019 /* Include the registers in the mask. */
12020 emit_insn (gen_iorsi3 (reg, reg, GEN_INT ((int) info->vrsave_mask)));
12021
12022 insn = emit_insn (generate_set_vrsave (reg, info, 0));
12023 }
12024
9ebbca7d
GK
12025 /* If we use the link register, get it into r0. */
12026 if (info->lr_save_p)
71f123ca 12027 emit_move_insn (gen_rtx_REG (Pmode, 0),
9ebbca7d
GK
12028 gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM));
12029
12030 /* If we need to save CR, put it into r12. */
12031 if (info->cr_save_p && frame_reg_rtx != frame_ptr_rtx)
12032 {
12033 cr_save_rtx = gen_rtx_REG (SImode, 12);
12034 emit_insn (gen_movesi_from_cr (cr_save_rtx));
12035 }
12036
a4f6c312
SS
12037 /* Do any required saving of fpr's. If only one or two to save, do
12038 it ourselves. Otherwise, call function. */
9ebbca7d
GK
12039 if (saving_FPRs_inline)
12040 {
12041 int i;
12042 for (i = 0; i < 64 - info->first_fp_reg_save; i++)
12043 if ((regs_ever_live[info->first_fp_reg_save+i]
12044 && ! call_used_regs[info->first_fp_reg_save+i]))
89e7058f
AH
12045 emit_frame_save (frame_reg_rtx, frame_ptr_rtx, DFmode,
12046 info->first_fp_reg_save + i,
12047 info->fp_save_offset + sp_offset + 8 * i,
12048 info->total_size);
9ebbca7d
GK
12049 }
12050 else if (info->first_fp_reg_save != 64)
12051 {
12052 int i;
12053 char rname[30];
520a57c8 12054 const char *alloc_rname;
9ebbca7d
GK
12055 rtvec p;
12056 p = rtvec_alloc (2 + 64 - info->first_fp_reg_save);
12057
12058 RTVEC_ELT (p, 0) = gen_rtx_CLOBBER (VOIDmode,
12059 gen_rtx_REG (Pmode,
12060 LINK_REGISTER_REGNUM));
12061 sprintf (rname, "%s%d%s", SAVE_FP_PREFIX,
12062 info->first_fp_reg_save - 32, SAVE_FP_SUFFIX);
a8a05998 12063 alloc_rname = ggc_strdup (rname);
9ebbca7d
GK
12064 RTVEC_ELT (p, 1) = gen_rtx_USE (VOIDmode,
12065 gen_rtx_SYMBOL_REF (Pmode,
12066 alloc_rname));
12067 for (i = 0; i < 64 - info->first_fp_reg_save; i++)
12068 {
12069 rtx addr, reg, mem;
12070 reg = gen_rtx_REG (DFmode, info->first_fp_reg_save + i);
12071 addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
12072 GEN_INT (info->fp_save_offset
12073 + sp_offset + 8*i));
12074 mem = gen_rtx_MEM (DFmode, addr);
ba4828e0 12075 set_mem_alias_set (mem, rs6000_sr_alias_set);
9ebbca7d
GK
12076
12077 RTVEC_ELT (p, i + 2) = gen_rtx_SET (VOIDmode, mem, reg);
12078 }
12079 insn = emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
12080 rs6000_frame_related (insn, frame_ptr_rtx, info->total_size,
12081 NULL_RTX, NULL_RTX);
12082 }
b6c9286a 12083
9ebbca7d
GK
12084 /* Save GPRs. This is done as a PARALLEL if we are using
12085 the store-multiple instructions. */
12086 if (using_store_multiple)
b6c9286a 12087 {
308c142a 12088 rtvec p;
9ebbca7d
GK
12089 int i;
12090 p = rtvec_alloc (32 - info->first_gp_reg_save);
9ebbca7d
GK
12091 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
12092 {
12093 rtx addr, reg, mem;
12094 reg = gen_rtx_REG (reg_mode, info->first_gp_reg_save + i);
12095 addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
12096 GEN_INT (info->gp_save_offset
12097 + sp_offset
12098 + reg_size * i));
12099 mem = gen_rtx_MEM (reg_mode, addr);
ba4828e0 12100 set_mem_alias_set (mem, rs6000_sr_alias_set);
9ebbca7d
GK
12101
12102 RTVEC_ELT (p, i) = gen_rtx_SET (VOIDmode, mem, reg);
12103 }
12104 insn = emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
12105 rs6000_frame_related (insn, frame_ptr_rtx, info->total_size,
12106 NULL_RTX, NULL_RTX);
b6c9286a
MM
12107 }
12108 else
12109 {
9ebbca7d
GK
12110 int i;
12111 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
12112 if ((regs_ever_live[info->first_gp_reg_save+i]
12113 && ! call_used_regs[info->first_gp_reg_save+i])
1db02437 12114 || (i+info->first_gp_reg_save == RS6000_PIC_OFFSET_TABLE_REGNUM
14f00213 12115 && ((DEFAULT_ABI == ABI_V4 && flag_pic != 0)
f607bc57 12116 || (DEFAULT_ABI == ABI_DARWIN && flag_pic))))
a3170dc6
AH
12117 {
12118 rtx addr, reg, mem;
12119 reg = gen_rtx_REG (reg_mode, info->first_gp_reg_save + i);
12120
c19de7aa 12121 if (TARGET_SPE_ABI && info->spe_64bit_regs_used != 0)
a3170dc6
AH
12122 {
12123 int offset = info->spe_gp_save_offset + sp_offset + 8 * i;
12124 rtx b;
12125
12126 if (!SPE_CONST_OFFSET_OK (offset))
12127 {
12128 b = gen_rtx_REG (Pmode, FIXED_SCRATCH);
12129 emit_move_insn (b, GEN_INT (offset));
12130 }
12131 else
12132 b = GEN_INT (offset);
12133
12134 addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, b);
12135 mem = gen_rtx_MEM (V2SImode, addr);
12136 set_mem_alias_set (mem, rs6000_sr_alias_set);
12137 insn = emit_move_insn (mem, reg);
12138
12139 if (GET_CODE (b) == CONST_INT)
12140 rs6000_frame_related (insn, frame_ptr_rtx, info->total_size,
12141 NULL_RTX, NULL_RTX);
12142 else
12143 rs6000_frame_related (insn, frame_ptr_rtx, info->total_size,
12144 b, GEN_INT (offset));
12145 }
12146 else
12147 {
12148 addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
12149 GEN_INT (info->gp_save_offset
12150 + sp_offset
12151 + reg_size * i));
12152 mem = gen_rtx_MEM (reg_mode, addr);
12153 set_mem_alias_set (mem, rs6000_sr_alias_set);
12154
12155 insn = emit_move_insn (mem, reg);
12156 rs6000_frame_related (insn, frame_ptr_rtx, info->total_size,
12157 NULL_RTX, NULL_RTX);
12158 }
12159 }
9ebbca7d
GK
12160 }
12161
83720594
RH
12162 /* ??? There's no need to emit actual instructions here, but it's the
12163 easiest way to get the frame unwind information emitted. */
12164 if (current_function_calls_eh_return)
12165 {
78e1b90d
DE
12166 unsigned int i, regno;
12167
fc4767bb
JJ
12168 /* In AIX ABI we need to pretend we save r2 here. */
12169 if (TARGET_AIX)
12170 {
12171 rtx addr, reg, mem;
12172
12173 reg = gen_rtx_REG (reg_mode, 2);
12174 addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
12175 GEN_INT (sp_offset + 5 * reg_size));
12176 mem = gen_rtx_MEM (reg_mode, addr);
12177 set_mem_alias_set (mem, rs6000_sr_alias_set);
12178
12179 insn = emit_move_insn (mem, reg);
12180 rs6000_frame_related (insn, frame_ptr_rtx, info->total_size,
12181 NULL_RTX, NULL_RTX);
12182 PATTERN (insn) = gen_blockage ();
12183 }
12184
83720594
RH
12185 for (i = 0; ; ++i)
12186 {
83720594
RH
12187 regno = EH_RETURN_DATA_REGNO (i);
12188 if (regno == INVALID_REGNUM)
12189 break;
12190
89e7058f
AH
12191 emit_frame_save (frame_reg_rtx, frame_ptr_rtx, reg_mode, regno,
12192 info->ehrd_offset + sp_offset
12193 + reg_size * (int) i,
12194 info->total_size);
83720594
RH
12195 }
12196 }
12197
9ebbca7d
GK
12198 /* Save lr if we used it. */
12199 if (info->lr_save_p)
12200 {
12201 rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
12202 GEN_INT (info->lr_save_offset + sp_offset));
12203 rtx reg = gen_rtx_REG (Pmode, 0);
12204 rtx mem = gen_rtx_MEM (Pmode, addr);
12205 /* This should not be of rs6000_sr_alias_set, because of
12206 __builtin_return_address. */
12207
12208 insn = emit_move_insn (mem, reg);
12209 rs6000_frame_related (insn, frame_ptr_rtx, info->total_size,
12210 reg, gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM));
12211 }
12212
12213 /* Save CR if we use any that must be preserved. */
12214 if (info->cr_save_p)
12215 {
12216 rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
12217 GEN_INT (info->cr_save_offset + sp_offset));
12218 rtx mem = gen_rtx_MEM (SImode, addr);
ba4828e0
RK
12219
12220 set_mem_alias_set (mem, rs6000_sr_alias_set);
9ebbca7d
GK
12221
12222 /* If r12 was used to hold the original sp, copy cr into r0 now
12223 that it's free. */
12224 if (REGNO (frame_reg_rtx) == 12)
12225 {
12226 cr_save_rtx = gen_rtx_REG (SImode, 0);
12227 emit_insn (gen_movesi_from_cr (cr_save_rtx));
12228 }
12229 insn = emit_move_insn (mem, cr_save_rtx);
12230
12231 /* Now, there's no way that dwarf2out_frame_debug_expr is going
615158e2
JJ
12232 to understand '(unspec:SI [(reg:CC 68) ...] UNSPEC_MOVESI_FROM_CR)'.
12233 But that's OK. All we have to do is specify that _one_ condition
12234 code register is saved in this stack slot. The thrower's epilogue
a1dc9455
FS
12235 will then restore all the call-saved registers.
12236 We use CR2_REGNO (70) to be compatible with gcc-2.95 on Linux. */
9ebbca7d 12237 rs6000_frame_related (insn, frame_ptr_rtx, info->total_size,
a1dc9455 12238 cr_save_rtx, gen_rtx_REG (SImode, CR2_REGNO));
9ebbca7d
GK
12239 }
12240
12241 /* Update stack and set back pointer unless this is V.4,
12242 for which it was done previously. */
fc4767bb
JJ
12243 if (info->push_p
12244 && !(DEFAULT_ABI == ABI_V4 || current_function_calls_eh_return))
9ebbca7d
GK
12245 rs6000_emit_allocate_stack (info->total_size, FALSE);
12246
12247 /* Set frame pointer, if needed. */
12248 if (frame_pointer_needed)
12249 {
a3170dc6 12250 insn = emit_move_insn (gen_rtx_REG (Pmode, FRAME_POINTER_REGNUM),
9ebbca7d
GK
12251 sp_reg_rtx);
12252 RTX_FRAME_RELATED_P (insn) = 1;
b6c9286a 12253 }
9878760c 12254
1db02437 12255 /* If we are using RS6000_PIC_OFFSET_TABLE_REGNUM, we need to set it up. */
9ebbca7d 12256 if ((TARGET_TOC && TARGET_MINIMAL_TOC && get_pool_size () != 0)
f607bc57 12257 || (DEFAULT_ABI == ABI_V4 && flag_pic == 1
1db02437 12258 && regs_ever_live[RS6000_PIC_OFFSET_TABLE_REGNUM]))
9ebbca7d
GK
12259 {
12260 /* If emit_load_toc_table will use the link register, we need to save
c4501e62 12261 it. We use R12 for this purpose because emit_load_toc_table
9ebbca7d
GK
12262 can use register 0. This allows us to use a plain 'blr' to return
12263 from the procedure more often. */
f1384257
AM
12264 int save_LR_around_toc_setup = (TARGET_ELF
12265 && DEFAULT_ABI != ABI_AIX
12266 && flag_pic
d5fa86ba
GK
12267 && ! info->lr_save_p
12268 && EXIT_BLOCK_PTR->pred != NULL);
9ebbca7d 12269 if (save_LR_around_toc_setup)
c4501e62
JJ
12270 {
12271 rtx lr = gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM);
12272 rs6000_maybe_dead (emit_move_insn (frame_ptr_rtx, lr));
12273 rs6000_emit_load_toc_table (TRUE);
12274 rs6000_maybe_dead (emit_move_insn (lr, frame_ptr_rtx));
12275 }
12276 else
12277 rs6000_emit_load_toc_table (TRUE);
9ebbca7d 12278 }
ee890fe2 12279
fcce224d 12280#if TARGET_MACHO
ee890fe2
SS
12281 if (DEFAULT_ABI == ABI_DARWIN
12282 && flag_pic && current_function_uses_pic_offset_table)
12283 {
12284 rtx dest = gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM);
f099d360 12285 const char *picbase = machopic_function_base_name ();
6788f5ca 12286 rtx src = gen_rtx_SYMBOL_REF (Pmode, picbase);
ee890fe2 12287
f51eee6a 12288 rs6000_maybe_dead (emit_insn (gen_load_macho_picbase (dest, src)));
ee890fe2
SS
12289
12290 rs6000_maybe_dead (
1db02437 12291 emit_move_insn (gen_rtx_REG (Pmode, RS6000_PIC_OFFSET_TABLE_REGNUM),
ee890fe2
SS
12292 gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM)));
12293 }
fcce224d 12294#endif
9ebbca7d
GK
12295}
12296
9ebbca7d 12297/* Write function prologue. */
a4f6c312 12298
08c148a8 12299static void
a2369ed3
DJ
12300rs6000_output_function_prologue (FILE *file,
12301 HOST_WIDE_INT size ATTRIBUTE_UNUSED)
9ebbca7d
GK
12302{
12303 rs6000_stack_t *info = rs6000_stack_info ();
12304
4697a36c
MM
12305 if (TARGET_DEBUG_STACK)
12306 debug_stack_info (info);
9878760c 12307
a4f6c312
SS
12308 /* Write .extern for any function we will call to save and restore
12309 fp values. */
12310 if (info->first_fp_reg_save < 64
12311 && !FP_SAVE_INLINE (info->first_fp_reg_save))
4d30c363 12312 fprintf (file, "\t.extern %s%d%s\n\t.extern %s%d%s\n",
4697a36c 12313 SAVE_FP_PREFIX, info->first_fp_reg_save - 32, SAVE_FP_SUFFIX,
a4f6c312
SS
12314 RESTORE_FP_PREFIX, info->first_fp_reg_save - 32,
12315 RESTORE_FP_SUFFIX);
9878760c 12316
c764f757
RK
12317 /* Write .extern for AIX common mode routines, if needed. */
12318 if (! TARGET_POWER && ! TARGET_POWERPC && ! common_mode_defined)
12319 {
f6709c70
JW
12320 fputs ("\t.extern __mulh\n", file);
12321 fputs ("\t.extern __mull\n", file);
12322 fputs ("\t.extern __divss\n", file);
12323 fputs ("\t.extern __divus\n", file);
12324 fputs ("\t.extern __quoss\n", file);
12325 fputs ("\t.extern __quous\n", file);
c764f757
RK
12326 common_mode_defined = 1;
12327 }
9878760c 12328
9ebbca7d 12329 if (! HAVE_prologue)
979721f8 12330 {
9ebbca7d 12331 start_sequence ();
9dda4cc8 12332
a4f6c312
SS
12333 /* A NOTE_INSN_DELETED is supposed to be at the start and end of
12334 the "toplevel" insn chain. */
2e040219 12335 emit_note (NOTE_INSN_DELETED);
9ebbca7d 12336 rs6000_emit_prologue ();
2e040219 12337 emit_note (NOTE_INSN_DELETED);
178c3eff 12338
a3c9585f 12339 /* Expand INSN_ADDRESSES so final() doesn't crash. */
178c3eff
DJ
12340 {
12341 rtx insn;
12342 unsigned addr = 0;
12343 for (insn = get_insns (); insn != 0; insn = NEXT_INSN (insn))
12344 {
12345 INSN_ADDRESSES_NEW (insn, addr);
12346 addr += 4;
12347 }
12348 }
9dda4cc8 12349
9ebbca7d 12350 if (TARGET_DEBUG_STACK)
a4f6c312
SS
12351 debug_rtx_list (get_insns (), 100);
12352 final (get_insns (), file, FALSE, FALSE);
9ebbca7d 12353 end_sequence ();
979721f8
MM
12354 }
12355
9ebbca7d
GK
12356 rs6000_pic_labelno++;
12357}
12358
12359/* Emit function epilogue as insns.
9878760c 12360
9ebbca7d
GK
12361 At present, dwarf2out_frame_debug_expr doesn't understand
12362 register restores, so we don't bother setting RTX_FRAME_RELATED_P
12363 anywhere in the epilogue. Most of the insns below would in any case
12364 need special notes to explain where r11 is in relation to the stack. */
9878760c 12365
9ebbca7d 12366void
a2369ed3 12367rs6000_emit_epilogue (int sibcall)
9ebbca7d
GK
12368{
12369 rs6000_stack_t *info;
12370 int restoring_FPRs_inline;
12371 int using_load_multiple;
12372 int using_mfcr_multiple;
12373 int use_backchain_to_restore_sp;
12374 int sp_offset = 0;
12375 rtx sp_reg_rtx = gen_rtx_REG (Pmode, 1);
12376 rtx frame_reg_rtx = sp_reg_rtx;
0e67400a 12377 enum machine_mode reg_mode = Pmode;
327e5343 12378 int reg_size = TARGET_32BIT ? 4 : 8;
9ebbca7d
GK
12379 int i;
12380
c19de7aa
AH
12381 info = rs6000_stack_info ();
12382
12383 if (TARGET_SPE_ABI && info->spe_64bit_regs_used != 0)
a3170dc6
AH
12384 {
12385 reg_mode = V2SImode;
12386 reg_size = 8;
12387 }
12388
9ebbca7d 12389 using_load_multiple = (TARGET_MULTIPLE && ! TARGET_POWERPC64
c19de7aa
AH
12390 && (!TARGET_SPE_ABI
12391 || info->spe_64bit_regs_used == 0)
9ebbca7d
GK
12392 && info->first_gp_reg_save < 31);
12393 restoring_FPRs_inline = (sibcall
83720594 12394 || current_function_calls_eh_return
9ebbca7d
GK
12395 || info->first_fp_reg_save == 64
12396 || FP_SAVE_INLINE (info->first_fp_reg_save));
12397 use_backchain_to_restore_sp = (frame_pointer_needed
12398 || current_function_calls_alloca
12399 || info->total_size > 32767);
12400 using_mfcr_multiple = (rs6000_cpu == PROCESSOR_PPC601
12401 || rs6000_cpu == PROCESSOR_PPC603
12402 || rs6000_cpu == PROCESSOR_PPC750
12403 || optimize_size);
12404
12405 /* If we have a frame pointer, a call to alloca, or a large stack
12406 frame, restore the old stack pointer using the backchain. Otherwise,
12407 we know what size to update it with. */
12408 if (use_backchain_to_restore_sp)
bacbde18 12409 {
9ebbca7d
GK
12410 /* Under V.4, don't reset the stack pointer until after we're done
12411 loading the saved registers. */
f607bc57 12412 if (DEFAULT_ABI == ABI_V4)
9ebbca7d 12413 frame_reg_rtx = gen_rtx_REG (Pmode, 11);
4697a36c 12414
9ebbca7d
GK
12415 emit_move_insn (frame_reg_rtx,
12416 gen_rtx_MEM (Pmode, sp_reg_rtx));
12417
bacbde18 12418 }
9ebbca7d 12419 else if (info->push_p)
85638c0d 12420 {
fc4767bb
JJ
12421 if (DEFAULT_ABI == ABI_V4
12422 || current_function_calls_eh_return)
9ebbca7d
GK
12423 sp_offset = info->total_size;
12424 else
12425 {
12426 emit_insn (TARGET_32BIT
12427 ? gen_addsi3 (sp_reg_rtx, sp_reg_rtx,
12428 GEN_INT (info->total_size))
12429 : gen_adddi3 (sp_reg_rtx, sp_reg_rtx,
12430 GEN_INT (info->total_size)));
12431 }
85638c0d 12432 }
9ebbca7d 12433
9aa86737
AH
12434 /* Restore AltiVec registers if needed. */
12435 if (TARGET_ALTIVEC_ABI && info->altivec_size != 0)
12436 {
12437 int i;
12438
12439 for (i = info->first_altivec_reg_save; i <= LAST_ALTIVEC_REGNO; ++i)
12440 if (info->vrsave_mask & ALTIVEC_REG_BIT (i))
12441 {
12442 rtx addr, areg, mem;
12443
12444 areg = gen_rtx_REG (Pmode, 0);
12445 emit_move_insn
12446 (areg, GEN_INT (info->altivec_save_offset
12447 + sp_offset
12448 + 16 * (i - info->first_altivec_reg_save)));
12449
12450 /* AltiVec addressing mode is [reg+reg]. */
12451 addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, areg);
12452 mem = gen_rtx_MEM (V4SImode, addr);
12453 set_mem_alias_set (mem, rs6000_sr_alias_set);
12454
12455 emit_move_insn (gen_rtx_REG (V4SImode, i), mem);
12456 }
12457 }
12458
12459 /* Restore VRSAVE if needed. */
4d774ff8
HP
12460 if (TARGET_ALTIVEC_ABI && TARGET_ALTIVEC_VRSAVE
12461 && info->vrsave_mask != 0)
9aa86737
AH
12462 {
12463 rtx addr, mem, reg;
12464
12465 addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
12466 GEN_INT (info->vrsave_save_offset + sp_offset));
12467 mem = gen_rtx_MEM (SImode, addr);
12468 set_mem_alias_set (mem, rs6000_sr_alias_set);
12469 reg = gen_rtx_REG (SImode, 12);
12470 emit_move_insn (reg, mem);
12471
12472 emit_insn (generate_set_vrsave (reg, info, 1));
12473 }
12474
9ebbca7d
GK
12475 /* Get the old lr if we saved it. */
12476 if (info->lr_save_p)
b6c9286a 12477 {
a3170dc6
AH
12478 rtx mem = gen_frame_mem_offset (Pmode, frame_reg_rtx,
12479 info->lr_save_offset + sp_offset);
ba4828e0
RK
12480
12481 set_mem_alias_set (mem, rs6000_sr_alias_set);
b6c9286a 12482
9ebbca7d 12483 emit_move_insn (gen_rtx_REG (Pmode, 0), mem);
b6c9286a 12484 }
9ebbca7d
GK
12485
12486 /* Get the old cr if we saved it. */
12487 if (info->cr_save_p)
12488 {
12489 rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
12490 GEN_INT (info->cr_save_offset + sp_offset));
12491 rtx mem = gen_rtx_MEM (SImode, addr);
ba4828e0
RK
12492
12493 set_mem_alias_set (mem, rs6000_sr_alias_set);
b6c9286a 12494
9ebbca7d
GK
12495 emit_move_insn (gen_rtx_REG (SImode, 12), mem);
12496 }
12497
12498 /* Set LR here to try to overlap restores below. */
4697a36c 12499 if (info->lr_save_p)
9ebbca7d
GK
12500 emit_move_insn (gen_rtx_REG (Pmode, LINK_REGISTER_REGNUM),
12501 gen_rtx_REG (Pmode, 0));
12502
83720594
RH
12503 /* Load exception handler data registers, if needed. */
12504 if (current_function_calls_eh_return)
12505 {
78e1b90d
DE
12506 unsigned int i, regno;
12507
fc4767bb
JJ
12508 if (TARGET_AIX)
12509 {
12510 rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
12511 GEN_INT (sp_offset + 5 * reg_size));
12512 rtx mem = gen_rtx_MEM (reg_mode, addr);
12513
12514 set_mem_alias_set (mem, rs6000_sr_alias_set);
12515
12516 emit_move_insn (gen_rtx_REG (reg_mode, 2), mem);
12517 }
12518
83720594
RH
12519 for (i = 0; ; ++i)
12520 {
a3170dc6 12521 rtx mem;
83720594
RH
12522
12523 regno = EH_RETURN_DATA_REGNO (i);
12524 if (regno == INVALID_REGNUM)
12525 break;
12526
a3170dc6
AH
12527 mem = gen_frame_mem_offset (reg_mode, frame_reg_rtx,
12528 info->ehrd_offset + sp_offset
12529 + reg_size * (int) i);
ba4828e0 12530 set_mem_alias_set (mem, rs6000_sr_alias_set);
83720594
RH
12531
12532 emit_move_insn (gen_rtx_REG (reg_mode, regno), mem);
12533 }
12534 }
9ebbca7d
GK
12535
12536 /* Restore GPRs. This is done as a PARALLEL if we are using
12537 the load-multiple instructions. */
12538 if (using_load_multiple)
979721f8 12539 {
9ebbca7d
GK
12540 rtvec p;
12541 p = rtvec_alloc (32 - info->first_gp_reg_save);
12542 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
979721f8 12543 {
9ebbca7d
GK
12544 rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
12545 GEN_INT (info->gp_save_offset
12546 + sp_offset
12547 + reg_size * i));
12548 rtx mem = gen_rtx_MEM (reg_mode, addr);
ba4828e0
RK
12549
12550 set_mem_alias_set (mem, rs6000_sr_alias_set);
9ebbca7d
GK
12551
12552 RTVEC_ELT (p, i) =
12553 gen_rtx_SET (VOIDmode,
12554 gen_rtx_REG (reg_mode, info->first_gp_reg_save + i),
12555 mem);
979721f8 12556 }
9ebbca7d 12557 emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
979721f8 12558 }
9ebbca7d
GK
12559 else
12560 for (i = 0; i < 32 - info->first_gp_reg_save; i++)
12561 if ((regs_ever_live[info->first_gp_reg_save+i]
12562 && ! call_used_regs[info->first_gp_reg_save+i])
1db02437 12563 || (i+info->first_gp_reg_save == RS6000_PIC_OFFSET_TABLE_REGNUM
14f00213 12564 && ((DEFAULT_ABI == ABI_V4 && flag_pic != 0)
f607bc57 12565 || (DEFAULT_ABI == ABI_DARWIN && flag_pic))))
9ebbca7d
GK
12566 {
12567 rtx addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
12568 GEN_INT (info->gp_save_offset
12569 + sp_offset
12570 + reg_size * i));
12571 rtx mem = gen_rtx_MEM (reg_mode, addr);
ba4828e0 12572
a3170dc6 12573 /* Restore 64-bit quantities for SPE. */
c19de7aa 12574 if (TARGET_SPE_ABI && info->spe_64bit_regs_used != 0)
a3170dc6
AH
12575 {
12576 int offset = info->spe_gp_save_offset + sp_offset + 8 * i;
12577 rtx b;
12578
12579 if (!SPE_CONST_OFFSET_OK (offset))
12580 {
12581 b = gen_rtx_REG (Pmode, FIXED_SCRATCH);
12582 emit_move_insn (b, GEN_INT (offset));
12583 }
12584 else
12585 b = GEN_INT (offset);
12586
12587 addr = gen_rtx_PLUS (Pmode, frame_reg_rtx, b);
12588 mem = gen_rtx_MEM (V2SImode, addr);
12589 }
12590
ba4828e0 12591 set_mem_alias_set (mem, rs6000_sr_alias_set);
9ebbca7d
GK
12592
12593 emit_move_insn (gen_rtx_REG (reg_mode,
a3170dc6 12594 info->first_gp_reg_save + i), mem);
9ebbca7d 12595 }
9878760c 12596
9ebbca7d
GK
12597 /* Restore fpr's if we need to do it without calling a function. */
12598 if (restoring_FPRs_inline)
12599 for (i = 0; i < 64 - info->first_fp_reg_save; i++)
12600 if ((regs_ever_live[info->first_fp_reg_save+i]
12601 && ! call_used_regs[info->first_fp_reg_save+i]))
12602 {
12603 rtx addr, mem;
12604 addr = gen_rtx_PLUS (Pmode, frame_reg_rtx,
12605 GEN_INT (info->fp_save_offset
12606 + sp_offset
a4f6c312 12607 + 8 * i));
9ebbca7d 12608 mem = gen_rtx_MEM (DFmode, addr);
ba4828e0 12609 set_mem_alias_set (mem, rs6000_sr_alias_set);
9ebbca7d
GK
12610
12611 emit_move_insn (gen_rtx_REG (DFmode,
12612 info->first_fp_reg_save + i),
12613 mem);
12614 }
8d30c4ee 12615
9ebbca7d
GK
12616 /* If we saved cr, restore it here. Just those that were used. */
12617 if (info->cr_save_p)
979721f8 12618 {
9ebbca7d 12619 rtx r12_rtx = gen_rtx_REG (SImode, 12);
e35b9579 12620 int count = 0;
9ebbca7d
GK
12621
12622 if (using_mfcr_multiple)
979721f8 12623 {
9ebbca7d
GK
12624 for (i = 0; i < 8; i++)
12625 if (regs_ever_live[CR0_REGNO+i] && ! call_used_regs[CR0_REGNO+i])
e35b9579 12626 count++;
9ebbca7d 12627 if (count == 0)
e35b9579
GK
12628 abort ();
12629 }
12630
12631 if (using_mfcr_multiple && count > 1)
12632 {
12633 rtvec p;
12634 int ndx;
9ebbca7d 12635
e35b9579 12636 p = rtvec_alloc (count);
9ebbca7d 12637
e35b9579 12638 ndx = 0;
9ebbca7d
GK
12639 for (i = 0; i < 8; i++)
12640 if (regs_ever_live[CR0_REGNO+i] && ! call_used_regs[CR0_REGNO+i])
12641 {
12642 rtvec r = rtvec_alloc (2);
12643 RTVEC_ELT (r, 0) = r12_rtx;
12644 RTVEC_ELT (r, 1) = GEN_INT (1 << (7-i));
e35b9579 12645 RTVEC_ELT (p, ndx) =
9ebbca7d 12646 gen_rtx_SET (VOIDmode, gen_rtx_REG (CCmode, CR0_REGNO+i),
615158e2 12647 gen_rtx_UNSPEC (CCmode, r, UNSPEC_MOVESI_TO_CR));
e35b9579 12648 ndx++;
9ebbca7d
GK
12649 }
12650 emit_insn (gen_rtx_PARALLEL (VOIDmode, p));
e35b9579
GK
12651 if (ndx != count)
12652 abort ();
979721f8
MM
12653 }
12654 else
9ebbca7d
GK
12655 for (i = 0; i < 8; i++)
12656 if (regs_ever_live[CR0_REGNO+i] && ! call_used_regs[CR0_REGNO+i])
979721f8 12657 {
9ebbca7d
GK
12658 emit_insn (gen_movsi_to_cr_one (gen_rtx_REG (CCmode,
12659 CR0_REGNO+i),
12660 r12_rtx));
979721f8 12661 }
979721f8
MM
12662 }
12663
9ebbca7d
GK
12664 /* If this is V.4, unwind the stack pointer after all of the loads
12665 have been done. We need to emit a block here so that sched
12666 doesn't decide to move the sp change before the register restores
12667 (which may not have any obvious dependency on the stack). This
12668 doesn't hurt performance, because there is no scheduling that can
12669 be done after this point. */
fc4767bb
JJ
12670 if (DEFAULT_ABI == ABI_V4
12671 || current_function_calls_eh_return)
b6c9286a 12672 {
9ebbca7d
GK
12673 if (frame_reg_rtx != sp_reg_rtx)
12674 rs6000_emit_stack_tie ();
b6c9286a 12675
9ebbca7d 12676 if (use_backchain_to_restore_sp)
b6c9286a 12677 {
9ebbca7d 12678 emit_move_insn (sp_reg_rtx, frame_reg_rtx);
b6c9286a 12679 }
9ebbca7d 12680 else if (sp_offset != 0)
13f1623b 12681 {
5b71a4e7 12682 emit_insn (TARGET_32BIT
9ebbca7d
GK
12683 ? gen_addsi3 (sp_reg_rtx, sp_reg_rtx,
12684 GEN_INT (sp_offset))
12685 : gen_adddi3 (sp_reg_rtx, sp_reg_rtx,
12686 GEN_INT (sp_offset)));
13f1623b 12687 }
9ebbca7d 12688 }
b6c9286a 12689
83720594
RH
12690 if (current_function_calls_eh_return)
12691 {
12692 rtx sa = EH_RETURN_STACKADJ_RTX;
5b71a4e7 12693 emit_insn (TARGET_32BIT
83720594
RH
12694 ? gen_addsi3 (sp_reg_rtx, sp_reg_rtx, sa)
12695 : gen_adddi3 (sp_reg_rtx, sp_reg_rtx, sa));
12696 }
12697
9ebbca7d
GK
12698 if (!sibcall)
12699 {
12700 rtvec p;
12701 if (! restoring_FPRs_inline)
12702 p = rtvec_alloc (3 + 64 - info->first_fp_reg_save);
12703 else
12704 p = rtvec_alloc (2);
b6c9286a 12705
e35b9579
GK
12706 RTVEC_ELT (p, 0) = gen_rtx_RETURN (VOIDmode);
12707 RTVEC_ELT (p, 1) = gen_rtx_USE (VOIDmode,
9ebbca7d
GK
12708 gen_rtx_REG (Pmode,
12709 LINK_REGISTER_REGNUM));
9ebbca7d
GK
12710
12711 /* If we have to restore more than two FP registers, branch to the
12712 restore function. It will return to our caller. */
12713 if (! restoring_FPRs_inline)
12714 {
12715 int i;
12716 char rname[30];
520a57c8 12717 const char *alloc_rname;
979721f8 12718
9ebbca7d
GK
12719 sprintf (rname, "%s%d%s", RESTORE_FP_PREFIX,
12720 info->first_fp_reg_save - 32, RESTORE_FP_SUFFIX);
a8a05998 12721 alloc_rname = ggc_strdup (rname);
9ebbca7d
GK
12722 RTVEC_ELT (p, 2) = gen_rtx_USE (VOIDmode,
12723 gen_rtx_SYMBOL_REF (Pmode,
12724 alloc_rname));
b6c9286a 12725
9ebbca7d
GK
12726 for (i = 0; i < 64 - info->first_fp_reg_save; i++)
12727 {
12728 rtx addr, mem;
12729 addr = gen_rtx_PLUS (Pmode, sp_reg_rtx,
12730 GEN_INT (info->fp_save_offset + 8*i));
12731 mem = gen_rtx_MEM (DFmode, addr);
ba4828e0 12732 set_mem_alias_set (mem, rs6000_sr_alias_set);
9ebbca7d
GK
12733
12734 RTVEC_ELT (p, i+3) =
12735 gen_rtx_SET (VOIDmode,
12736 gen_rtx_REG (DFmode, info->first_fp_reg_save + i),
12737 mem);
b6c9286a
MM
12738 }
12739 }
9ebbca7d
GK
12740
12741 emit_jump_insn (gen_rtx_PARALLEL (VOIDmode, p));
3daf36a4 12742 }
9878760c
RK
12743}
12744
12745/* Write function epilogue. */
12746
08c148a8 12747static void
a2369ed3
DJ
12748rs6000_output_function_epilogue (FILE *file,
12749 HOST_WIDE_INT size ATTRIBUTE_UNUSED)
9878760c 12750{
4697a36c 12751 rs6000_stack_t *info = rs6000_stack_info ();
9878760c 12752
9ebbca7d 12753 if (! HAVE_epilogue)
9878760c 12754 {
9ebbca7d
GK
12755 rtx insn = get_last_insn ();
12756 /* If the last insn was a BARRIER, we don't have to write anything except
12757 the trace table. */
12758 if (GET_CODE (insn) == NOTE)
12759 insn = prev_nonnote_insn (insn);
12760 if (insn == 0 || GET_CODE (insn) != BARRIER)
4697a36c 12761 {
9ebbca7d
GK
12762 /* This is slightly ugly, but at least we don't have two
12763 copies of the epilogue-emitting code. */
12764 start_sequence ();
12765
12766 /* A NOTE_INSN_DELETED is supposed to be at the start
12767 and end of the "toplevel" insn chain. */
2e040219 12768 emit_note (NOTE_INSN_DELETED);
9ebbca7d 12769 rs6000_emit_epilogue (FALSE);
2e040219 12770 emit_note (NOTE_INSN_DELETED);
9ebbca7d 12771
a3c9585f 12772 /* Expand INSN_ADDRESSES so final() doesn't crash. */
178c3eff
DJ
12773 {
12774 rtx insn;
12775 unsigned addr = 0;
12776 for (insn = get_insns (); insn != 0; insn = NEXT_INSN (insn))
12777 {
12778 INSN_ADDRESSES_NEW (insn, addr);
12779 addr += 4;
12780 }
12781 }
12782
9ebbca7d 12783 if (TARGET_DEBUG_STACK)
a4f6c312
SS
12784 debug_rtx_list (get_insns (), 100);
12785 final (get_insns (), file, FALSE, FALSE);
9ebbca7d 12786 end_sequence ();
4697a36c 12787 }
9878760c 12788 }
b4ac57ab 12789
efdba735
SH
12790#if TARGET_MACHO
12791 macho_branch_islands ();
0e5da0be
GK
12792 /* Mach-O doesn't support labels at the end of objects, so if
12793 it looks like we might want one, insert a NOP. */
12794 {
12795 rtx insn = get_last_insn ();
12796 while (insn
12797 && NOTE_P (insn)
12798 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_DELETED_LABEL)
12799 insn = PREV_INSN (insn);
12800 if (insn
12801 && (LABEL_P (insn)
12802 || (NOTE_P (insn)
12803 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_DELETED_LABEL)))
12804 fputs ("\tnop\n", file);
12805 }
12806#endif
12807
9b30bae2 12808 /* Output a traceback table here. See /usr/include/sys/debug.h for info
314fc5a9
ILT
12809 on its format.
12810
12811 We don't output a traceback table if -finhibit-size-directive was
12812 used. The documentation for -finhibit-size-directive reads
12813 ``don't output a @code{.size} assembler directive, or anything
12814 else that would cause trouble if the function is split in the
12815 middle, and the two halves are placed at locations far apart in
12816 memory.'' The traceback table has this property, since it
12817 includes the offset from the start of the function to the
4d30c363
MM
12818 traceback table itself.
12819
12820 System V.4 Powerpc's (and the embedded ABI derived from it) use a
b6c9286a 12821 different traceback table. */
57ac7be9
AM
12822 if (DEFAULT_ABI == ABI_AIX && ! flag_inhibit_size_directive
12823 && rs6000_traceback != traceback_none)
9b30bae2 12824 {
69c75916 12825 const char *fname = NULL;
3ac88239 12826 const char *language_string = lang_hooks.name;
6041bf2f 12827 int fixed_parms = 0, float_parms = 0, parm_info = 0;
314fc5a9 12828 int i;
57ac7be9
AM
12829 int optional_tbtab;
12830
12831 if (rs6000_traceback == traceback_full)
12832 optional_tbtab = 1;
12833 else if (rs6000_traceback == traceback_part)
12834 optional_tbtab = 0;
12835 else
12836 optional_tbtab = !optimize_size && !TARGET_ELF;
314fc5a9 12837
69c75916
AM
12838 if (optional_tbtab)
12839 {
12840 fname = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
12841 while (*fname == '.') /* V.4 encodes . in the name */
12842 fname++;
12843
12844 /* Need label immediately before tbtab, so we can compute
12845 its offset from the function start. */
12846 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LT");
12847 ASM_OUTPUT_LABEL (file, fname);
12848 }
314fc5a9
ILT
12849
12850 /* The .tbtab pseudo-op can only be used for the first eight
12851 expressions, since it can't handle the possibly variable
12852 length fields that follow. However, if you omit the optional
12853 fields, the assembler outputs zeros for all optional fields
12854 anyways, giving each variable length field is minimum length
12855 (as defined in sys/debug.h). Thus we can not use the .tbtab
12856 pseudo-op at all. */
12857
12858 /* An all-zero word flags the start of the tbtab, for debuggers
12859 that have to find it by searching forward from the entry
12860 point or from the current pc. */
19d2d16f 12861 fputs ("\t.long 0\n", file);
314fc5a9
ILT
12862
12863 /* Tbtab format type. Use format type 0. */
19d2d16f 12864 fputs ("\t.byte 0,", file);
314fc5a9 12865
5fc921c1
DE
12866 /* Language type. Unfortunately, there does not seem to be any
12867 official way to discover the language being compiled, so we
12868 use language_string.
12869 C is 0. Fortran is 1. Pascal is 2. Ada is 3. C++ is 9.
12870 Java is 13. Objective-C is 14. */
12871 if (! strcmp (language_string, "GNU C"))
314fc5a9
ILT
12872 i = 0;
12873 else if (! strcmp (language_string, "GNU F77"))
12874 i = 1;
8b83775b 12875 else if (! strcmp (language_string, "GNU Pascal"))
314fc5a9 12876 i = 2;
5fc921c1
DE
12877 else if (! strcmp (language_string, "GNU Ada"))
12878 i = 3;
314fc5a9
ILT
12879 else if (! strcmp (language_string, "GNU C++"))
12880 i = 9;
9517ead8
AG
12881 else if (! strcmp (language_string, "GNU Java"))
12882 i = 13;
5fc921c1
DE
12883 else if (! strcmp (language_string, "GNU Objective-C"))
12884 i = 14;
314fc5a9
ILT
12885 else
12886 abort ();
12887 fprintf (file, "%d,", i);
12888
12889 /* 8 single bit fields: global linkage (not set for C extern linkage,
12890 apparently a PL/I convention?), out-of-line epilogue/prologue, offset
12891 from start of procedure stored in tbtab, internal function, function
12892 has controlled storage, function has no toc, function uses fp,
12893 function logs/aborts fp operations. */
12894 /* Assume that fp operations are used if any fp reg must be saved. */
6041bf2f
DE
12895 fprintf (file, "%d,",
12896 (optional_tbtab << 5) | ((info->first_fp_reg_save != 64) << 1));
314fc5a9
ILT
12897
12898 /* 6 bitfields: function is interrupt handler, name present in
12899 proc table, function calls alloca, on condition directives
12900 (controls stack walks, 3 bits), saves condition reg, saves
12901 link reg. */
12902 /* The `function calls alloca' bit seems to be set whenever reg 31 is
12903 set up as a frame pointer, even when there is no alloca call. */
12904 fprintf (file, "%d,",
6041bf2f
DE
12905 ((optional_tbtab << 6)
12906 | ((optional_tbtab & frame_pointer_needed) << 5)
12907 | (info->cr_save_p << 1)
12908 | (info->lr_save_p)));
314fc5a9 12909
6041bf2f 12910 /* 3 bitfields: saves backchain, fixup code, number of fpr saved
314fc5a9
ILT
12911 (6 bits). */
12912 fprintf (file, "%d,",
4697a36c 12913 (info->push_p << 7) | (64 - info->first_fp_reg_save));
314fc5a9
ILT
12914
12915 /* 2 bitfields: spare bits (2 bits), number of gpr saved (6 bits). */
12916 fprintf (file, "%d,", (32 - first_reg_to_save ()));
12917
6041bf2f
DE
12918 if (optional_tbtab)
12919 {
12920 /* Compute the parameter info from the function decl argument
12921 list. */
12922 tree decl;
12923 int next_parm_info_bit = 31;
314fc5a9 12924
6041bf2f
DE
12925 for (decl = DECL_ARGUMENTS (current_function_decl);
12926 decl; decl = TREE_CHAIN (decl))
12927 {
12928 rtx parameter = DECL_INCOMING_RTL (decl);
12929 enum machine_mode mode = GET_MODE (parameter);
314fc5a9 12930
6041bf2f
DE
12931 if (GET_CODE (parameter) == REG)
12932 {
12933 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
12934 {
12935 int bits;
12936
12937 float_parms++;
12938
12939 if (mode == SFmode)
12940 bits = 0x2;
fcce224d 12941 else if (mode == DFmode || mode == TFmode)
6041bf2f
DE
12942 bits = 0x3;
12943 else
12944 abort ();
12945
12946 /* If only one bit will fit, don't or in this entry. */
12947 if (next_parm_info_bit > 0)
12948 parm_info |= (bits << (next_parm_info_bit - 1));
12949 next_parm_info_bit -= 2;
12950 }
12951 else
12952 {
12953 fixed_parms += ((GET_MODE_SIZE (mode)
12954 + (UNITS_PER_WORD - 1))
12955 / UNITS_PER_WORD);
12956 next_parm_info_bit -= 1;
12957 }
12958 }
12959 }
12960 }
314fc5a9
ILT
12961
12962 /* Number of fixed point parameters. */
12963 /* This is actually the number of words of fixed point parameters; thus
12964 an 8 byte struct counts as 2; and thus the maximum value is 8. */
12965 fprintf (file, "%d,", fixed_parms);
12966
12967 /* 2 bitfields: number of floating point parameters (7 bits), parameters
12968 all on stack. */
12969 /* This is actually the number of fp registers that hold parameters;
12970 and thus the maximum value is 13. */
12971 /* Set parameters on stack bit if parameters are not in their original
12972 registers, regardless of whether they are on the stack? Xlc
12973 seems to set the bit when not optimizing. */
12974 fprintf (file, "%d\n", ((float_parms << 1) | (! optimize)));
12975
6041bf2f
DE
12976 if (! optional_tbtab)
12977 return;
12978
314fc5a9
ILT
12979 /* Optional fields follow. Some are variable length. */
12980
12981 /* Parameter types, left adjusted bit fields: 0 fixed, 10 single float,
12982 11 double float. */
12983 /* There is an entry for each parameter in a register, in the order that
12984 they occur in the parameter list. Any intervening arguments on the
12985 stack are ignored. If the list overflows a long (max possible length
12986 34 bits) then completely leave off all elements that don't fit. */
12987 /* Only emit this long if there was at least one parameter. */
12988 if (fixed_parms || float_parms)
12989 fprintf (file, "\t.long %d\n", parm_info);
12990
12991 /* Offset from start of code to tb table. */
19d2d16f 12992 fputs ("\t.long ", file);
314fc5a9 12993 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LT");
54ee9799
DE
12994#if TARGET_AIX
12995 RS6000_OUTPUT_BASENAME (file, fname);
12996#else
9ebbca7d 12997 assemble_name (file, fname);
54ee9799 12998#endif
19d2d16f 12999 fputs ("-.", file);
54ee9799
DE
13000#if TARGET_AIX
13001 RS6000_OUTPUT_BASENAME (file, fname);
13002#else
9ebbca7d 13003 assemble_name (file, fname);
54ee9799 13004#endif
19d2d16f 13005 putc ('\n', file);
314fc5a9
ILT
13006
13007 /* Interrupt handler mask. */
13008 /* Omit this long, since we never set the interrupt handler bit
13009 above. */
13010
13011 /* Number of CTL (controlled storage) anchors. */
13012 /* Omit this long, since the has_ctl bit is never set above. */
13013
13014 /* Displacement into stack of each CTL anchor. */
13015 /* Omit this list of longs, because there are no CTL anchors. */
13016
13017 /* Length of function name. */
69c75916
AM
13018 if (*fname == '*')
13019 ++fname;
296b8152 13020 fprintf (file, "\t.short %d\n", (int) strlen (fname));
314fc5a9
ILT
13021
13022 /* Function name. */
13023 assemble_string (fname, strlen (fname));
13024
13025 /* Register for alloca automatic storage; this is always reg 31.
13026 Only emit this if the alloca bit was set above. */
13027 if (frame_pointer_needed)
19d2d16f 13028 fputs ("\t.byte 31\n", file);
b1765bde
DE
13029
13030 fputs ("\t.align 2\n", file);
9b30bae2 13031 }
9878760c 13032}
17167fd8 13033\f
a4f6c312
SS
13034/* A C compound statement that outputs the assembler code for a thunk
13035 function, used to implement C++ virtual function calls with
13036 multiple inheritance. The thunk acts as a wrapper around a virtual
13037 function, adjusting the implicit object parameter before handing
13038 control off to the real function.
13039
13040 First, emit code to add the integer DELTA to the location that
13041 contains the incoming first argument. Assume that this argument
13042 contains a pointer, and is the one used to pass the `this' pointer
13043 in C++. This is the incoming argument *before* the function
13044 prologue, e.g. `%o0' on a sparc. The addition must preserve the
13045 values of all other incoming arguments.
17167fd8
MM
13046
13047 After the addition, emit code to jump to FUNCTION, which is a
a4f6c312
SS
13048 `FUNCTION_DECL'. This is a direct pure jump, not a call, and does
13049 not touch the return address. Hence returning from FUNCTION will
13050 return to whoever called the current `thunk'.
17167fd8 13051
a4f6c312
SS
13052 The effect must be as if FUNCTION had been called directly with the
13053 adjusted first argument. This macro is responsible for emitting
13054 all of the code for a thunk function; output_function_prologue()
13055 and output_function_epilogue() are not invoked.
17167fd8 13056
a4f6c312
SS
13057 The THUNK_FNDECL is redundant. (DELTA and FUNCTION have already
13058 been extracted from it.) It might possibly be useful on some
13059 targets, but probably not.
17167fd8 13060
a4f6c312
SS
13061 If you do not define this macro, the target-independent code in the
13062 C++ frontend will generate a less efficient heavyweight thunk that
13063 calls FUNCTION instead of jumping to it. The generic approach does
13064 not support varargs. */
17167fd8 13065
3961e8fe 13066static void
a2369ed3
DJ
13067rs6000_output_mi_thunk (FILE *file, tree thunk_fndecl ATTRIBUTE_UNUSED,
13068 HOST_WIDE_INT delta, HOST_WIDE_INT vcall_offset,
13069 tree function)
17167fd8 13070{
5b71a4e7 13071 rtx this, insn, funexp;
17167fd8 13072
5b71a4e7 13073 reload_completed = 1;
fe3ad572 13074 epilogue_completed = 1;
5b71a4e7 13075 no_new_pseudos = 1;
56a7189a 13076
5b71a4e7 13077 /* Mark the end of the (empty) prologue. */
2e040219 13078 emit_note (NOTE_INSN_PROLOGUE_END);
17167fd8 13079
5b71a4e7
DE
13080 /* Find the "this" pointer. If the function returns a structure,
13081 the structure return pointer is in r3. */
61f71b34 13082 if (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function))
5b71a4e7 13083 this = gen_rtx_REG (Pmode, 4);
56a7189a 13084 else
5b71a4e7 13085 this = gen_rtx_REG (Pmode, 3);
17167fd8 13086
5b71a4e7
DE
13087 /* Apply the constant offset, if required. */
13088 if (delta)
13089 {
13090 rtx delta_rtx = GEN_INT (delta);
13091 emit_insn (TARGET_32BIT
13092 ? gen_addsi3 (this, this, delta_rtx)
13093 : gen_adddi3 (this, this, delta_rtx));
17167fd8
MM
13094 }
13095
5b71a4e7
DE
13096 /* Apply the offset from the vtable, if required. */
13097 if (vcall_offset)
17167fd8 13098 {
5b71a4e7
DE
13099 rtx vcall_offset_rtx = GEN_INT (vcall_offset);
13100 rtx tmp = gen_rtx_REG (Pmode, 12);
17167fd8 13101
5b71a4e7 13102 emit_move_insn (tmp, gen_rtx_MEM (Pmode, this));
eeff9307
JJ
13103 if (((unsigned HOST_WIDE_INT) vcall_offset) + 0x8000 >= 0x10000)
13104 {
13105 emit_insn (TARGET_32BIT
13106 ? gen_addsi3 (tmp, tmp, vcall_offset_rtx)
13107 : gen_adddi3 (tmp, tmp, vcall_offset_rtx));
13108 emit_move_insn (tmp, gen_rtx_MEM (Pmode, tmp));
13109 }
13110 else
13111 {
13112 rtx loc = gen_rtx_PLUS (Pmode, tmp, vcall_offset_rtx);
13113
13114 emit_move_insn (tmp, gen_rtx_MEM (Pmode, loc));
13115 }
5b71a4e7
DE
13116 emit_insn (TARGET_32BIT
13117 ? gen_addsi3 (this, this, tmp)
13118 : gen_adddi3 (this, this, tmp));
17167fd8
MM
13119 }
13120
5b71a4e7
DE
13121 /* Generate a tail call to the target function. */
13122 if (!TREE_USED (function))
13123 {
13124 assemble_external (function);
13125 TREE_USED (function) = 1;
13126 }
13127 funexp = XEXP (DECL_RTL (function), 0);
5b71a4e7 13128 funexp = gen_rtx_MEM (FUNCTION_MODE, funexp);
ee890fe2
SS
13129
13130#if TARGET_MACHO
ab82a49f 13131 if (MACHOPIC_INDIRECT)
5b71a4e7 13132 funexp = machopic_indirect_call_target (funexp);
ee890fe2 13133#endif
5b71a4e7
DE
13134
13135 /* gen_sibcall expects reload to convert scratch pseudo to LR so we must
13136 generate sibcall RTL explicitly to avoid constraint abort. */
13137 insn = emit_call_insn (
13138 gen_rtx_PARALLEL (VOIDmode,
13139 gen_rtvec (4,
13140 gen_rtx_CALL (VOIDmode,
13141 funexp, const0_rtx),
13142 gen_rtx_USE (VOIDmode, const0_rtx),
13143 gen_rtx_USE (VOIDmode,
13144 gen_rtx_REG (SImode,
13145 LINK_REGISTER_REGNUM)),
13146 gen_rtx_RETURN (VOIDmode))));
13147 SIBLING_CALL_P (insn) = 1;
13148 emit_barrier ();
13149
13150 /* Run just enough of rest_of_compilation to get the insns emitted.
13151 There's not really enough bulk here to make other passes such as
13152 instruction scheduling worth while. Note that use_thunk calls
13153 assemble_start_function and assemble_end_function. */
13154 insn = get_insns ();
a2855205 13155 insn_locators_initialize ();
5b71a4e7
DE
13156 shorten_branches (insn);
13157 final_start_function (insn, file, 1);
13158 final (insn, file, 1, 0);
13159 final_end_function ();
13160
13161 reload_completed = 0;
fe3ad572 13162 epilogue_completed = 0;
5b71a4e7 13163 no_new_pseudos = 0;
9ebbca7d 13164}
9ebbca7d
GK
13165\f
13166/* A quick summary of the various types of 'constant-pool tables'
13167 under PowerPC:
13168
13169 Target Flags Name One table per
13170 AIX (none) AIX TOC object file
13171 AIX -mfull-toc AIX TOC object file
13172 AIX -mminimal-toc AIX minimal TOC translation unit
13173 SVR4/EABI (none) SVR4 SDATA object file
13174 SVR4/EABI -fpic SVR4 pic object file
13175 SVR4/EABI -fPIC SVR4 PIC translation unit
13176 SVR4/EABI -mrelocatable EABI TOC function
13177 SVR4/EABI -maix AIX TOC object file
13178 SVR4/EABI -maix -mminimal-toc
13179 AIX minimal TOC translation unit
13180
13181 Name Reg. Set by entries contains:
13182 made by addrs? fp? sum?
13183
13184 AIX TOC 2 crt0 as Y option option
13185 AIX minimal TOC 30 prolog gcc Y Y option
13186 SVR4 SDATA 13 crt0 gcc N Y N
13187 SVR4 pic 30 prolog ld Y not yet N
13188 SVR4 PIC 30 prolog gcc Y option option
13189 EABI TOC 30 prolog gcc Y option option
13190
13191*/
13192
9ebbca7d
GK
13193/* Hash functions for the hash table. */
13194
13195static unsigned
a2369ed3 13196rs6000_hash_constant (rtx k)
9ebbca7d 13197{
46b33600
RH
13198 enum rtx_code code = GET_CODE (k);
13199 enum machine_mode mode = GET_MODE (k);
13200 unsigned result = (code << 3) ^ mode;
13201 const char *format;
13202 int flen, fidx;
9ebbca7d 13203
46b33600
RH
13204 format = GET_RTX_FORMAT (code);
13205 flen = strlen (format);
13206 fidx = 0;
9ebbca7d 13207
46b33600
RH
13208 switch (code)
13209 {
13210 case LABEL_REF:
13211 return result * 1231 + (unsigned) INSN_UID (XEXP (k, 0));
13212
13213 case CONST_DOUBLE:
13214 if (mode != VOIDmode)
13215 return real_hash (CONST_DOUBLE_REAL_VALUE (k)) * result;
13216 flen = 2;
13217 break;
13218
13219 case CODE_LABEL:
13220 fidx = 3;
13221 break;
13222
13223 default:
13224 break;
13225 }
9ebbca7d
GK
13226
13227 for (; fidx < flen; fidx++)
13228 switch (format[fidx])
13229 {
13230 case 's':
13231 {
13232 unsigned i, len;
13233 const char *str = XSTR (k, fidx);
13234 len = strlen (str);
13235 result = result * 613 + len;
13236 for (i = 0; i < len; i++)
13237 result = result * 613 + (unsigned) str[i];
17167fd8
MM
13238 break;
13239 }
9ebbca7d
GK
13240 case 'u':
13241 case 'e':
13242 result = result * 1231 + rs6000_hash_constant (XEXP (k, fidx));
13243 break;
13244 case 'i':
13245 case 'n':
13246 result = result * 613 + (unsigned) XINT (k, fidx);
13247 break;
13248 case 'w':
13249 if (sizeof (unsigned) >= sizeof (HOST_WIDE_INT))
13250 result = result * 613 + (unsigned) XWINT (k, fidx);
13251 else
13252 {
13253 size_t i;
13254 for (i = 0; i < sizeof(HOST_WIDE_INT)/sizeof(unsigned); i++)
13255 result = result * 613 + (unsigned) (XWINT (k, fidx)
13256 >> CHAR_BIT * i);
13257 }
13258 break;
09501938
DE
13259 case '0':
13260 break;
9ebbca7d 13261 default:
a4f6c312 13262 abort ();
9ebbca7d 13263 }
46b33600 13264
9ebbca7d
GK
13265 return result;
13266}
13267
13268static unsigned
a2369ed3 13269toc_hash_function (const void *hash_entry)
9ebbca7d 13270{
a9098fd0
GK
13271 const struct toc_hash_struct *thc =
13272 (const struct toc_hash_struct *) hash_entry;
13273 return rs6000_hash_constant (thc->key) ^ thc->key_mode;
9ebbca7d
GK
13274}
13275
13276/* Compare H1 and H2 for equivalence. */
13277
13278static int
a2369ed3 13279toc_hash_eq (const void *h1, const void *h2)
9ebbca7d
GK
13280{
13281 rtx r1 = ((const struct toc_hash_struct *) h1)->key;
13282 rtx r2 = ((const struct toc_hash_struct *) h2)->key;
13283
a9098fd0
GK
13284 if (((const struct toc_hash_struct *) h1)->key_mode
13285 != ((const struct toc_hash_struct *) h2)->key_mode)
13286 return 0;
13287
5692c7bc 13288 return rtx_equal_p (r1, r2);
9ebbca7d
GK
13289}
13290
28e510bd
MM
13291/* These are the names given by the C++ front-end to vtables, and
13292 vtable-like objects. Ideally, this logic should not be here;
13293 instead, there should be some programmatic way of inquiring as
13294 to whether or not an object is a vtable. */
13295
13296#define VTABLE_NAME_P(NAME) \
13297 (strncmp ("_vt.", name, strlen("_vt.")) == 0 \
13298 || strncmp ("_ZTV", name, strlen ("_ZTV")) == 0 \
13299 || strncmp ("_ZTT", name, strlen ("_ZTT")) == 0 \
13300 || strncmp ("_ZTC", name, strlen ("_ZTC")) == 0)
13301
13302void
a2369ed3 13303rs6000_output_symbol_ref (FILE *file, rtx x)
28e510bd
MM
13304{
13305 /* Currently C++ toc references to vtables can be emitted before it
13306 is decided whether the vtable is public or private. If this is
13307 the case, then the linker will eventually complain that there is
13308 a reference to an unknown section. Thus, for vtables only,
13309 we emit the TOC reference to reference the symbol and not the
13310 section. */
13311 const char *name = XSTR (x, 0);
54ee9799
DE
13312
13313 if (VTABLE_NAME_P (name))
13314 {
13315 RS6000_OUTPUT_BASENAME (file, name);
13316 }
13317 else
13318 assemble_name (file, name);
28e510bd
MM
13319}
13320
a4f6c312
SS
13321/* Output a TOC entry. We derive the entry name from what is being
13322 written. */
9878760c
RK
13323
13324void
a2369ed3 13325output_toc (FILE *file, rtx x, int labelno, enum machine_mode mode)
9878760c
RK
13326{
13327 char buf[256];
3cce094d 13328 const char *name = buf;
ec940faa 13329 const char *real_name;
9878760c
RK
13330 rtx base = x;
13331 int offset = 0;
13332
4697a36c
MM
13333 if (TARGET_NO_TOC)
13334 abort ();
13335
9ebbca7d
GK
13336 /* When the linker won't eliminate them, don't output duplicate
13337 TOC entries (this happens on AIX if there is any kind of TOC,
17211ab5
GK
13338 and on SVR4 under -fPIC or -mrelocatable). Don't do this for
13339 CODE_LABELs. */
13340 if (TARGET_TOC && GET_CODE (x) != LABEL_REF)
9ebbca7d
GK
13341 {
13342 struct toc_hash_struct *h;
13343 void * * found;
13344
17211ab5 13345 /* Create toc_hash_table. This can't be done at OVERRIDE_OPTIONS
39e3f58c 13346 time because GGC is not initialized at that point. */
17211ab5
GK
13347 if (toc_hash_table == NULL)
13348 toc_hash_table = htab_create_ggc (1021, toc_hash_function,
13349 toc_hash_eq, NULL);
13350
9ebbca7d
GK
13351 h = ggc_alloc (sizeof (*h));
13352 h->key = x;
a9098fd0 13353 h->key_mode = mode;
9ebbca7d
GK
13354 h->labelno = labelno;
13355
13356 found = htab_find_slot (toc_hash_table, h, 1);
13357 if (*found == NULL)
13358 *found = h;
13359 else /* This is indeed a duplicate.
13360 Set this label equal to that label. */
13361 {
13362 fputs ("\t.set ", file);
13363 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LC");
13364 fprintf (file, "%d,", labelno);
13365 ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LC");
13366 fprintf (file, "%d\n", ((*(const struct toc_hash_struct **)
13367 found)->labelno));
13368 return;
13369 }
13370 }
13371
13372 /* If we're going to put a double constant in the TOC, make sure it's
13373 aligned properly when strict alignment is on. */
ff1720ed
RK
13374 if (GET_CODE (x) == CONST_DOUBLE
13375 && STRICT_ALIGNMENT
a9098fd0 13376 && GET_MODE_BITSIZE (mode) >= 64
ff1720ed
RK
13377 && ! (TARGET_NO_FP_IN_TOC && ! TARGET_MINIMAL_TOC)) {
13378 ASM_OUTPUT_ALIGN (file, 3);
13379 }
13380
4977bab6 13381 (*targetm.asm_out.internal_label) (file, "LC", labelno);
9878760c 13382
37c37a57
RK
13383 /* Handle FP constants specially. Note that if we have a minimal
13384 TOC, things we put here aren't actually in the TOC, so we can allow
13385 FP constants. */
fcce224d
DE
13386 if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) == TFmode)
13387 {
13388 REAL_VALUE_TYPE rv;
13389 long k[4];
13390
13391 REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
13392 REAL_VALUE_TO_TARGET_LONG_DOUBLE (rv, k);
13393
13394 if (TARGET_64BIT)
13395 {
13396 if (TARGET_MINIMAL_TOC)
13397 fputs (DOUBLE_INT_ASM_OP, file);
13398 else
13399 fprintf (file, "\t.tc FT_%lx_%lx_%lx_%lx[TC],",
13400 k[0] & 0xffffffff, k[1] & 0xffffffff,
13401 k[2] & 0xffffffff, k[3] & 0xffffffff);
13402 fprintf (file, "0x%lx%08lx,0x%lx%08lx\n",
13403 k[0] & 0xffffffff, k[1] & 0xffffffff,
13404 k[2] & 0xffffffff, k[3] & 0xffffffff);
13405 return;
13406 }
13407 else
13408 {
13409 if (TARGET_MINIMAL_TOC)
13410 fputs ("\t.long ", file);
13411 else
13412 fprintf (file, "\t.tc FT_%lx_%lx_%lx_%lx[TC],",
13413 k[0] & 0xffffffff, k[1] & 0xffffffff,
13414 k[2] & 0xffffffff, k[3] & 0xffffffff);
13415 fprintf (file, "0x%lx,0x%lx,0x%lx,0x%lx\n",
13416 k[0] & 0xffffffff, k[1] & 0xffffffff,
13417 k[2] & 0xffffffff, k[3] & 0xffffffff);
13418 return;
13419 }
13420 }
13421 else if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) == DFmode)
9878760c 13422 {
042259f2
DE
13423 REAL_VALUE_TYPE rv;
13424 long k[2];
0adc764e 13425
042259f2
DE
13426 REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
13427 REAL_VALUE_TO_TARGET_DOUBLE (rv, k);
31bfaa0b 13428
13ded975
DE
13429 if (TARGET_64BIT)
13430 {
13431 if (TARGET_MINIMAL_TOC)
2bfcf297 13432 fputs (DOUBLE_INT_ASM_OP, file);
13ded975 13433 else
2f0552b6
AM
13434 fprintf (file, "\t.tc FD_%lx_%lx[TC],",
13435 k[0] & 0xffffffff, k[1] & 0xffffffff);
13436 fprintf (file, "0x%lx%08lx\n",
13437 k[0] & 0xffffffff, k[1] & 0xffffffff);
13ded975
DE
13438 return;
13439 }
1875cc88 13440 else
13ded975
DE
13441 {
13442 if (TARGET_MINIMAL_TOC)
2bfcf297 13443 fputs ("\t.long ", file);
13ded975 13444 else
2f0552b6
AM
13445 fprintf (file, "\t.tc FD_%lx_%lx[TC],",
13446 k[0] & 0xffffffff, k[1] & 0xffffffff);
13447 fprintf (file, "0x%lx,0x%lx\n",
13448 k[0] & 0xffffffff, k[1] & 0xffffffff);
13ded975
DE
13449 return;
13450 }
9878760c 13451 }
a9098fd0 13452 else if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) == SFmode)
9878760c 13453 {
042259f2
DE
13454 REAL_VALUE_TYPE rv;
13455 long l;
9878760c 13456
042259f2
DE
13457 REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
13458 REAL_VALUE_TO_TARGET_SINGLE (rv, l);
13459
31bfaa0b
DE
13460 if (TARGET_64BIT)
13461 {
13462 if (TARGET_MINIMAL_TOC)
2bfcf297 13463 fputs (DOUBLE_INT_ASM_OP, file);
31bfaa0b 13464 else
2f0552b6
AM
13465 fprintf (file, "\t.tc FS_%lx[TC],", l & 0xffffffff);
13466 fprintf (file, "0x%lx00000000\n", l & 0xffffffff);
31bfaa0b
DE
13467 return;
13468 }
042259f2 13469 else
31bfaa0b
DE
13470 {
13471 if (TARGET_MINIMAL_TOC)
2bfcf297 13472 fputs ("\t.long ", file);
31bfaa0b 13473 else
2f0552b6
AM
13474 fprintf (file, "\t.tc FS_%lx[TC],", l & 0xffffffff);
13475 fprintf (file, "0x%lx\n", l & 0xffffffff);
31bfaa0b
DE
13476 return;
13477 }
042259f2 13478 }
f176e826 13479 else if (GET_MODE (x) == VOIDmode
a9098fd0 13480 && (GET_CODE (x) == CONST_INT || GET_CODE (x) == CONST_DOUBLE))
042259f2 13481 {
e2c953b6 13482 unsigned HOST_WIDE_INT low;
042259f2
DE
13483 HOST_WIDE_INT high;
13484
13485 if (GET_CODE (x) == CONST_DOUBLE)
13486 {
13487 low = CONST_DOUBLE_LOW (x);
13488 high = CONST_DOUBLE_HIGH (x);
13489 }
13490 else
13491#if HOST_BITS_PER_WIDE_INT == 32
13492 {
13493 low = INTVAL (x);
0858c623 13494 high = (low & 0x80000000) ? ~0 : 0;
042259f2
DE
13495 }
13496#else
13497 {
0858c623 13498 low = INTVAL (x) & 0xffffffff;
042259f2
DE
13499 high = (HOST_WIDE_INT) INTVAL (x) >> 32;
13500 }
13501#endif
9878760c 13502
a9098fd0
GK
13503 /* TOC entries are always Pmode-sized, but since this
13504 is a bigendian machine then if we're putting smaller
13505 integer constants in the TOC we have to pad them.
13506 (This is still a win over putting the constants in
13507 a separate constant pool, because then we'd have
02a4ec28
FS
13508 to have both a TOC entry _and_ the actual constant.)
13509
13510 For a 32-bit target, CONST_INT values are loaded and shifted
13511 entirely within `low' and can be stored in one TOC entry. */
13512
13513 if (TARGET_64BIT && POINTER_SIZE < GET_MODE_BITSIZE (mode))
a9098fd0 13514 abort ();/* It would be easy to make this work, but it doesn't now. */
02a4ec28
FS
13515
13516 if (POINTER_SIZE > GET_MODE_BITSIZE (mode))
fb52d8de
AM
13517 {
13518#if HOST_BITS_PER_WIDE_INT == 32
13519 lshift_double (low, high, POINTER_SIZE - GET_MODE_BITSIZE (mode),
13520 POINTER_SIZE, &low, &high, 0);
13521#else
13522 low |= high << 32;
13523 low <<= POINTER_SIZE - GET_MODE_BITSIZE (mode);
13524 high = (HOST_WIDE_INT) low >> 32;
13525 low &= 0xffffffff;
13526#endif
13527 }
a9098fd0 13528
13ded975
DE
13529 if (TARGET_64BIT)
13530 {
13531 if (TARGET_MINIMAL_TOC)
2bfcf297 13532 fputs (DOUBLE_INT_ASM_OP, file);
13ded975 13533 else
2f0552b6
AM
13534 fprintf (file, "\t.tc ID_%lx_%lx[TC],",
13535 (long) high & 0xffffffff, (long) low & 0xffffffff);
13536 fprintf (file, "0x%lx%08lx\n",
13537 (long) high & 0xffffffff, (long) low & 0xffffffff);
13ded975
DE
13538 return;
13539 }
1875cc88 13540 else
13ded975 13541 {
02a4ec28
FS
13542 if (POINTER_SIZE < GET_MODE_BITSIZE (mode))
13543 {
13544 if (TARGET_MINIMAL_TOC)
2bfcf297 13545 fputs ("\t.long ", file);
02a4ec28 13546 else
2bfcf297 13547 fprintf (file, "\t.tc ID_%lx_%lx[TC],",
2f0552b6
AM
13548 (long) high & 0xffffffff, (long) low & 0xffffffff);
13549 fprintf (file, "0x%lx,0x%lx\n",
13550 (long) high & 0xffffffff, (long) low & 0xffffffff);
02a4ec28 13551 }
13ded975 13552 else
02a4ec28
FS
13553 {
13554 if (TARGET_MINIMAL_TOC)
2bfcf297 13555 fputs ("\t.long ", file);
02a4ec28 13556 else
2f0552b6
AM
13557 fprintf (file, "\t.tc IS_%lx[TC],", (long) low & 0xffffffff);
13558 fprintf (file, "0x%lx\n", (long) low & 0xffffffff);
02a4ec28 13559 }
13ded975
DE
13560 return;
13561 }
9878760c
RK
13562 }
13563
13564 if (GET_CODE (x) == CONST)
13565 {
2bfcf297
DB
13566 if (GET_CODE (XEXP (x, 0)) != PLUS)
13567 abort ();
13568
9878760c
RK
13569 base = XEXP (XEXP (x, 0), 0);
13570 offset = INTVAL (XEXP (XEXP (x, 0), 1));
13571 }
13572
13573 if (GET_CODE (base) == SYMBOL_REF)
13574 name = XSTR (base, 0);
13575 else if (GET_CODE (base) == LABEL_REF)
13576 ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (XEXP (base, 0)));
13577 else if (GET_CODE (base) == CODE_LABEL)
13578 ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (base));
13579 else
13580 abort ();
13581
772c5265 13582 real_name = (*targetm.strip_name_encoding) (name);
1875cc88 13583 if (TARGET_MINIMAL_TOC)
2bfcf297 13584 fputs (TARGET_32BIT ? "\t.long " : DOUBLE_INT_ASM_OP, file);
1875cc88
JW
13585 else
13586 {
b6c9286a 13587 fprintf (file, "\t.tc %s", real_name);
9878760c 13588
1875cc88
JW
13589 if (offset < 0)
13590 fprintf (file, ".N%d", - offset);
13591 else if (offset)
13592 fprintf (file, ".P%d", offset);
9878760c 13593
19d2d16f 13594 fputs ("[TC],", file);
1875cc88 13595 }
581bc4de
MM
13596
13597 /* Currently C++ toc references to vtables can be emitted before it
13598 is decided whether the vtable is public or private. If this is
13599 the case, then the linker will eventually complain that there is
13600 a TOC reference to an unknown section. Thus, for vtables only,
13601 we emit the TOC reference to reference the symbol and not the
13602 section. */
28e510bd 13603 if (VTABLE_NAME_P (name))
581bc4de 13604 {
54ee9799 13605 RS6000_OUTPUT_BASENAME (file, name);
581bc4de
MM
13606 if (offset < 0)
13607 fprintf (file, "%d", offset);
13608 else if (offset > 0)
13609 fprintf (file, "+%d", offset);
13610 }
13611 else
13612 output_addr_const (file, x);
19d2d16f 13613 putc ('\n', file);
9878760c
RK
13614}
13615\f
13616/* Output an assembler pseudo-op to write an ASCII string of N characters
13617 starting at P to FILE.
13618
13619 On the RS/6000, we have to do this using the .byte operation and
13620 write out special characters outside the quoted string.
13621 Also, the assembler is broken; very long strings are truncated,
a4f6c312 13622 so we must artificially break them up early. */
9878760c
RK
13623
13624void
a2369ed3 13625output_ascii (FILE *file, const char *p, int n)
9878760c
RK
13626{
13627 char c;
13628 int i, count_string;
d330fd93
KG
13629 const char *for_string = "\t.byte \"";
13630 const char *for_decimal = "\t.byte ";
13631 const char *to_close = NULL;
9878760c
RK
13632
13633 count_string = 0;
13634 for (i = 0; i < n; i++)
13635 {
13636 c = *p++;
13637 if (c >= ' ' && c < 0177)
13638 {
13639 if (for_string)
13640 fputs (for_string, file);
13641 putc (c, file);
13642
13643 /* Write two quotes to get one. */
13644 if (c == '"')
13645 {
13646 putc (c, file);
13647 ++count_string;
13648 }
13649
13650 for_string = NULL;
13651 for_decimal = "\"\n\t.byte ";
13652 to_close = "\"\n";
13653 ++count_string;
13654
13655 if (count_string >= 512)
13656 {
13657 fputs (to_close, file);
13658
13659 for_string = "\t.byte \"";
13660 for_decimal = "\t.byte ";
13661 to_close = NULL;
13662 count_string = 0;
13663 }
13664 }
13665 else
13666 {
13667 if (for_decimal)
13668 fputs (for_decimal, file);
13669 fprintf (file, "%d", c);
13670
13671 for_string = "\n\t.byte \"";
13672 for_decimal = ", ";
13673 to_close = "\n";
13674 count_string = 0;
13675 }
13676 }
13677
13678 /* Now close the string if we have written one. Then end the line. */
13679 if (to_close)
9ebbca7d 13680 fputs (to_close, file);
9878760c
RK
13681}
13682\f
13683/* Generate a unique section name for FILENAME for a section type
13684 represented by SECTION_DESC. Output goes into BUF.
13685
13686 SECTION_DESC can be any string, as long as it is different for each
13687 possible section type.
13688
13689 We name the section in the same manner as xlc. The name begins with an
13690 underscore followed by the filename (after stripping any leading directory
11e5fe42
RK
13691 names) with the last period replaced by the string SECTION_DESC. If
13692 FILENAME does not contain a period, SECTION_DESC is appended to the end of
13693 the name. */
9878760c
RK
13694
13695void
a2369ed3
DJ
13696rs6000_gen_section_name (char **buf, const char *filename,
13697 const char *section_desc)
9878760c 13698{
9ebbca7d 13699 const char *q, *after_last_slash, *last_period = 0;
9878760c
RK
13700 char *p;
13701 int len;
9878760c
RK
13702
13703 after_last_slash = filename;
13704 for (q = filename; *q; q++)
11e5fe42
RK
13705 {
13706 if (*q == '/')
13707 after_last_slash = q + 1;
13708 else if (*q == '.')
13709 last_period = q;
13710 }
9878760c 13711
11e5fe42 13712 len = strlen (after_last_slash) + strlen (section_desc) + 2;
6d9f628e 13713 *buf = (char *) xmalloc (len);
9878760c
RK
13714
13715 p = *buf;
13716 *p++ = '_';
13717
13718 for (q = after_last_slash; *q; q++)
13719 {
11e5fe42 13720 if (q == last_period)
9878760c
RK
13721 {
13722 strcpy (p, section_desc);
13723 p += strlen (section_desc);
e3981aab 13724 break;
9878760c
RK
13725 }
13726
e9a780ec 13727 else if (ISALNUM (*q))
9878760c
RK
13728 *p++ = *q;
13729 }
13730
11e5fe42 13731 if (last_period == 0)
9878760c
RK
13732 strcpy (p, section_desc);
13733 else
13734 *p = '\0';
13735}
e165f3f0 13736\f
a4f6c312 13737/* Emit profile function. */
411707f4 13738
411707f4 13739void
a2369ed3 13740output_profile_hook (int labelno ATTRIBUTE_UNUSED)
411707f4 13741{
ffcfcb5f
AM
13742 if (TARGET_PROFILE_KERNEL)
13743 return;
13744
8480e480
CC
13745 if (DEFAULT_ABI == ABI_AIX)
13746 {
9739c90c
JJ
13747#ifndef NO_PROFILE_COUNTERS
13748# define NO_PROFILE_COUNTERS 0
13749#endif
13750 if (NO_PROFILE_COUNTERS)
13751 emit_library_call (init_one_libfunc (RS6000_MCOUNT), 0, VOIDmode, 0);
13752 else
13753 {
13754 char buf[30];
13755 const char *label_name;
13756 rtx fun;
411707f4 13757
9739c90c
JJ
13758 ASM_GENERATE_INTERNAL_LABEL (buf, "LP", labelno);
13759 label_name = (*targetm.strip_name_encoding) (ggc_strdup (buf));
13760 fun = gen_rtx_SYMBOL_REF (Pmode, label_name);
411707f4 13761
9739c90c
JJ
13762 emit_library_call (init_one_libfunc (RS6000_MCOUNT), 0, VOIDmode, 1,
13763 fun, Pmode);
13764 }
8480e480 13765 }
ee890fe2
SS
13766 else if (DEFAULT_ABI == ABI_DARWIN)
13767 {
d5fa86ba 13768 const char *mcount_name = RS6000_MCOUNT;
ee890fe2
SS
13769 int caller_addr_regno = LINK_REGISTER_REGNUM;
13770
13771 /* Be conservative and always set this, at least for now. */
13772 current_function_uses_pic_offset_table = 1;
13773
13774#if TARGET_MACHO
13775 /* For PIC code, set up a stub and collect the caller's address
13776 from r0, which is where the prologue puts it. */
ab82a49f 13777 if (MACHOPIC_INDIRECT)
ee890fe2
SS
13778 {
13779 mcount_name = machopic_stub_name (mcount_name);
13780 if (current_function_uses_pic_offset_table)
13781 caller_addr_regno = 0;
13782 }
13783#endif
13784 emit_library_call (gen_rtx_SYMBOL_REF (Pmode, mcount_name),
13785 0, VOIDmode, 1,
13786 gen_rtx_REG (Pmode, caller_addr_regno), Pmode);
13787 }
411707f4
CC
13788}
13789
a4f6c312 13790/* Write function profiler code. */
e165f3f0
RK
13791
13792void
a2369ed3 13793output_function_profiler (FILE *file, int labelno)
e165f3f0 13794{
3daf36a4 13795 char buf[100];
09eeeacb 13796 int save_lr = 8;
e165f3f0 13797
38c1f2d7 13798 switch (DEFAULT_ABI)
3daf36a4 13799 {
38c1f2d7
MM
13800 default:
13801 abort ();
13802
13803 case ABI_V4:
09eeeacb 13804 save_lr = 4;
09eeeacb
AM
13805 if (!TARGET_32BIT)
13806 {
13807 warning ("no profiling of 64-bit code for this ABI");
13808 return;
13809 }
ffcfcb5f 13810 ASM_GENERATE_INTERNAL_LABEL (buf, "LP", labelno);
38c1f2d7
MM
13811 fprintf (file, "\tmflr %s\n", reg_names[0]);
13812 if (flag_pic == 1)
13813 {
dfdfa60f 13814 fputs ("\tbl _GLOBAL_OFFSET_TABLE_@local-4\n", file);
09eeeacb
AM
13815 asm_fprintf (file, "\t{st|stw} %s,%d(%s)\n",
13816 reg_names[0], save_lr, reg_names[1]);
17167fd8 13817 asm_fprintf (file, "\tmflr %s\n", reg_names[12]);
dfdfa60f 13818 asm_fprintf (file, "\t{l|lwz} %s,", reg_names[0]);
38c1f2d7 13819 assemble_name (file, buf);
17167fd8 13820 asm_fprintf (file, "@got(%s)\n", reg_names[12]);
38c1f2d7 13821 }
9ebbca7d 13822 else if (flag_pic > 1)
38c1f2d7 13823 {
09eeeacb
AM
13824 asm_fprintf (file, "\t{st|stw} %s,%d(%s)\n",
13825 reg_names[0], save_lr, reg_names[1]);
9ebbca7d
GK
13826 /* Now, we need to get the address of the label. */
13827 fputs ("\tbl 1f\n\t.long ", file);
034e84c4 13828 assemble_name (file, buf);
9ebbca7d
GK
13829 fputs ("-.\n1:", file);
13830 asm_fprintf (file, "\tmflr %s\n", reg_names[11]);
13831 asm_fprintf (file, "\t{l|lwz} %s,0(%s)\n",
13832 reg_names[0], reg_names[11]);
13833 asm_fprintf (file, "\t{cax|add} %s,%s,%s\n",
13834 reg_names[0], reg_names[0], reg_names[11]);
38c1f2d7 13835 }
38c1f2d7
MM
13836 else
13837 {
17167fd8 13838 asm_fprintf (file, "\t{liu|lis} %s,", reg_names[12]);
38c1f2d7 13839 assemble_name (file, buf);
dfdfa60f 13840 fputs ("@ha\n", file);
09eeeacb
AM
13841 asm_fprintf (file, "\t{st|stw} %s,%d(%s)\n",
13842 reg_names[0], save_lr, reg_names[1]);
a260abc9 13843 asm_fprintf (file, "\t{cal|la} %s,", reg_names[0]);
38c1f2d7 13844 assemble_name (file, buf);
17167fd8 13845 asm_fprintf (file, "@l(%s)\n", reg_names[12]);
38c1f2d7
MM
13846 }
13847
50d440bc 13848 /* ABI_V4 saves the static chain reg with ASM_OUTPUT_REG_PUSH. */
3b6ce0af
DE
13849 fprintf (file, "\tbl %s%s\n",
13850 RS6000_MCOUNT, flag_pic ? "@plt" : "");
38c1f2d7
MM
13851 break;
13852
13853 case ABI_AIX:
ee890fe2 13854 case ABI_DARWIN:
ffcfcb5f
AM
13855 if (!TARGET_PROFILE_KERNEL)
13856 {
a3c9585f 13857 /* Don't do anything, done in output_profile_hook (). */
ffcfcb5f
AM
13858 }
13859 else
13860 {
13861 if (TARGET_32BIT)
13862 abort ();
13863
13864 asm_fprintf (file, "\tmflr %s\n", reg_names[0]);
13865 asm_fprintf (file, "\tstd %s,16(%s)\n", reg_names[0], reg_names[1]);
13866
13867 if (current_function_needs_context)
13868 {
13869 asm_fprintf (file, "\tstd %s,24(%s)\n",
13870 reg_names[STATIC_CHAIN_REGNUM], reg_names[1]);
13871 fprintf (file, "\tbl %s\n", RS6000_MCOUNT);
13872 asm_fprintf (file, "\tld %s,24(%s)\n",
13873 reg_names[STATIC_CHAIN_REGNUM], reg_names[1]);
13874 }
13875 else
13876 fprintf (file, "\tbl %s\n", RS6000_MCOUNT);
13877 }
38c1f2d7
MM
13878 break;
13879 }
e165f3f0 13880}
a251ffd0 13881
b54cf83a
DE
13882\f
13883static int
863d938c 13884rs6000_use_dfa_pipeline_interface (void)
b54cf83a
DE
13885{
13886 return 1;
13887}
13888
b54cf83a
DE
13889/* Power4 load update and store update instructions are cracked into a
13890 load or store and an integer insn which are executed in the same cycle.
13891 Branches have their own dispatch slot which does not count against the
13892 GCC issue rate, but it changes the program flow so there are no other
13893 instructions to issue in this cycle. */
13894
13895static int
a2369ed3
DJ
13896rs6000_variable_issue (FILE *stream ATTRIBUTE_UNUSED,
13897 int verbose ATTRIBUTE_UNUSED,
13898 rtx insn, int more)
b54cf83a
DE
13899{
13900 if (GET_CODE (PATTERN (insn)) == USE
13901 || GET_CODE (PATTERN (insn)) == CLOBBER)
13902 return more;
13903
ec507f2d 13904 if (rs6000_sched_groups)
b54cf83a 13905 {
cbe26ab8
DN
13906 if (is_microcoded_insn (insn))
13907 return 0;
13908 else if (is_cracked_insn (insn))
13909 return more > 2 ? more - 2 : 0;
b54cf83a 13910 }
165b263e
DE
13911
13912 return more - 1;
b54cf83a
DE
13913}
13914
a251ffd0
TG
13915/* Adjust the cost of a scheduling dependency. Return the new cost of
13916 a dependency LINK or INSN on DEP_INSN. COST is the current cost. */
13917
c237e94a 13918static int
a2369ed3
DJ
13919rs6000_adjust_cost (rtx insn, rtx link, rtx dep_insn ATTRIBUTE_UNUSED,
13920 int cost)
a251ffd0
TG
13921{
13922 if (! recog_memoized (insn))
13923 return 0;
13924
13925 if (REG_NOTE_KIND (link) != 0)
13926 return 0;
13927
13928 if (REG_NOTE_KIND (link) == 0)
13929 {
ed947a96
DJ
13930 /* Data dependency; DEP_INSN writes a register that INSN reads
13931 some cycles later. */
13932 switch (get_attr_type (insn))
13933 {
13934 case TYPE_JMPREG:
309323c2 13935 /* Tell the first scheduling pass about the latency between
ed947a96
DJ
13936 a mtctr and bctr (and mtlr and br/blr). The first
13937 scheduling pass will not know about this latency since
13938 the mtctr instruction, which has the latency associated
13939 to it, will be generated by reload. */
309323c2 13940 return TARGET_POWER ? 5 : 4;
ed947a96
DJ
13941 case TYPE_BRANCH:
13942 /* Leave some extra cycles between a compare and its
13943 dependent branch, to inhibit expensive mispredicts. */
309323c2
DE
13944 if ((rs6000_cpu_attr == CPU_PPC603
13945 || rs6000_cpu_attr == CPU_PPC604
13946 || rs6000_cpu_attr == CPU_PPC604E
13947 || rs6000_cpu_attr == CPU_PPC620
13948 || rs6000_cpu_attr == CPU_PPC630
13949 || rs6000_cpu_attr == CPU_PPC750
13950 || rs6000_cpu_attr == CPU_PPC7400
13951 || rs6000_cpu_attr == CPU_PPC7450
ec507f2d
DE
13952 || rs6000_cpu_attr == CPU_POWER4
13953 || rs6000_cpu_attr == CPU_POWER5)
ed947a96
DJ
13954 && recog_memoized (dep_insn)
13955 && (INSN_CODE (dep_insn) >= 0)
b54cf83a
DE
13956 && (get_attr_type (dep_insn) == TYPE_CMP
13957 || get_attr_type (dep_insn) == TYPE_COMPARE
ed947a96 13958 || get_attr_type (dep_insn) == TYPE_DELAYED_COMPARE
9259f3b0
DE
13959 || get_attr_type (dep_insn) == TYPE_IMUL_COMPARE
13960 || get_attr_type (dep_insn) == TYPE_LMUL_COMPARE
ed947a96 13961 || get_attr_type (dep_insn) == TYPE_FPCOMPARE
b54cf83a
DE
13962 || get_attr_type (dep_insn) == TYPE_CR_LOGICAL
13963 || get_attr_type (dep_insn) == TYPE_DELAYED_CR))
ed947a96
DJ
13964 return cost + 2;
13965 default:
13966 break;
13967 }
a251ffd0
TG
13968 /* Fall out to return default cost. */
13969 }
13970
13971 return cost;
13972}
b6c9286a 13973
cbe26ab8 13974/* The function returns a true if INSN is microcoded.
839a4992 13975 Return false otherwise. */
cbe26ab8
DN
13976
13977static bool
13978is_microcoded_insn (rtx insn)
13979{
13980 if (!insn || !INSN_P (insn)
13981 || GET_CODE (PATTERN (insn)) == USE
13982 || GET_CODE (PATTERN (insn)) == CLOBBER)
13983 return false;
13984
ec507f2d 13985 if (rs6000_sched_groups)
cbe26ab8
DN
13986 {
13987 enum attr_type type = get_attr_type (insn);
13988 if (type == TYPE_LOAD_EXT_U
13989 || type == TYPE_LOAD_EXT_UX
13990 || type == TYPE_LOAD_UX
13991 || type == TYPE_STORE_UX
13992 || type == TYPE_MFCR)
13993 return true;
13994 }
13995
13996 return false;
13997}
13998
5c425df5 13999/* The function returns a nonzero value if INSN can be scheduled only
cbe26ab8
DN
14000 as the first insn in a dispatch group ("dispatch-slot restricted").
14001 In this case, the returned value indicates how many dispatch slots
14002 the insn occupies (at the beginning of the group).
79ae11c4
DN
14003 Return 0 otherwise. */
14004
cbe26ab8 14005static int
79ae11c4
DN
14006is_dispatch_slot_restricted (rtx insn)
14007{
14008 enum attr_type type;
14009
ec507f2d 14010 if (!rs6000_sched_groups)
79ae11c4
DN
14011 return 0;
14012
14013 if (!insn
14014 || insn == NULL_RTX
14015 || GET_CODE (insn) == NOTE
14016 || GET_CODE (PATTERN (insn)) == USE
14017 || GET_CODE (PATTERN (insn)) == CLOBBER)
14018 return 0;
14019
14020 type = get_attr_type (insn);
14021
ec507f2d
DE
14022 switch (type)
14023 {
14024 case TYPE_MFCR:
14025 case TYPE_MFCRF:
14026 case TYPE_MTCR:
14027 case TYPE_DELAYED_CR:
14028 case TYPE_CR_LOGICAL:
14029 case TYPE_MTJMPR:
14030 case TYPE_MFJMPR:
14031 return 1;
14032 case TYPE_IDIV:
14033 case TYPE_LDIV:
14034 return 2;
14035 default:
14036 if (rs6000_cpu == PROCESSOR_POWER5
14037 && is_cracked_insn (insn))
14038 return 2;
14039 return 0;
14040 }
79ae11c4
DN
14041}
14042
cbe26ab8
DN
14043/* The function returns true if INSN is cracked into 2 instructions
14044 by the processor (and therefore occupies 2 issue slots). */
14045
14046static bool
14047is_cracked_insn (rtx insn)
14048{
14049 if (!insn || !INSN_P (insn)
14050 || GET_CODE (PATTERN (insn)) == USE
14051 || GET_CODE (PATTERN (insn)) == CLOBBER)
14052 return false;
14053
ec507f2d 14054 if (rs6000_sched_groups)
cbe26ab8
DN
14055 {
14056 enum attr_type type = get_attr_type (insn);
14057 if (type == TYPE_LOAD_U || type == TYPE_STORE_U
14058 || type == TYPE_FPLOAD_U || type == TYPE_FPSTORE_U
14059 || type == TYPE_FPLOAD_UX || type == TYPE_FPSTORE_UX
14060 || type == TYPE_LOAD_EXT || type == TYPE_DELAYED_CR
14061 || type == TYPE_COMPARE || type == TYPE_DELAYED_COMPARE
14062 || type == TYPE_IMUL_COMPARE || type == TYPE_LMUL_COMPARE
14063 || type == TYPE_IDIV || type == TYPE_LDIV
14064 || type == TYPE_INSERT_WORD)
14065 return true;
14066 }
14067
14068 return false;
14069}
14070
14071/* The function returns true if INSN can be issued only from
a3c9585f 14072 the branch slot. */
cbe26ab8
DN
14073
14074static bool
14075is_branch_slot_insn (rtx insn)
14076{
14077 if (!insn || !INSN_P (insn)
14078 || GET_CODE (PATTERN (insn)) == USE
14079 || GET_CODE (PATTERN (insn)) == CLOBBER)
14080 return false;
14081
ec507f2d 14082 if (rs6000_sched_groups)
cbe26ab8
DN
14083 {
14084 enum attr_type type = get_attr_type (insn);
14085 if (type == TYPE_BRANCH || type == TYPE_JMPREG)
14086 return true;
14087 return false;
14088 }
14089
14090 return false;
14091}
79ae11c4 14092
a4f6c312 14093/* A C statement (sans semicolon) to update the integer scheduling
79ae11c4
DN
14094 priority INSN_PRIORITY (INSN). Increase the priority to execute the
14095 INSN earlier, reduce the priority to execute INSN later. Do not
a4f6c312
SS
14096 define this macro if you do not need to adjust the scheduling
14097 priorities of insns. */
bef84347 14098
c237e94a 14099static int
a2369ed3 14100rs6000_adjust_priority (rtx insn ATTRIBUTE_UNUSED, int priority)
bef84347 14101{
a4f6c312
SS
14102 /* On machines (like the 750) which have asymmetric integer units,
14103 where one integer unit can do multiply and divides and the other
14104 can't, reduce the priority of multiply/divide so it is scheduled
14105 before other integer operations. */
bef84347
VM
14106
14107#if 0
2c3c49de 14108 if (! INSN_P (insn))
bef84347
VM
14109 return priority;
14110
14111 if (GET_CODE (PATTERN (insn)) == USE)
14112 return priority;
14113
14114 switch (rs6000_cpu_attr) {
14115 case CPU_PPC750:
14116 switch (get_attr_type (insn))
14117 {
14118 default:
14119 break;
14120
14121 case TYPE_IMUL:
14122 case TYPE_IDIV:
3cb999d8
DE
14123 fprintf (stderr, "priority was %#x (%d) before adjustment\n",
14124 priority, priority);
bef84347
VM
14125 if (priority >= 0 && priority < 0x01000000)
14126 priority >>= 3;
14127 break;
14128 }
14129 }
14130#endif
14131
79ae11c4
DN
14132 if (is_dispatch_slot_restricted (insn)
14133 && reload_completed
14134 && current_sched_info->sched_max_insns_priority
14135 && rs6000_sched_restricted_insns_priority)
14136 {
14137
14138 /* Prioritize insns that can be dispatched only in the first dispatch slot. */
14139 if (rs6000_sched_restricted_insns_priority == 1)
14140 /* Attach highest priority to insn. This means that in
14141 haifa-sched.c:ready_sort(), dispatch-slot restriction considerations
14142 precede 'priority' (critical path) considerations. */
14143 return current_sched_info->sched_max_insns_priority;
14144 else if (rs6000_sched_restricted_insns_priority == 2)
14145 /* Increase priority of insn by a minimal amount. This means that in
14146 haifa-sched.c:ready_sort(), only 'priority' (critical path) considerations
14147 precede dispatch-slot restriction considerations. */
14148 return (priority + 1);
14149 }
14150
bef84347
VM
14151 return priority;
14152}
14153
a4f6c312
SS
14154/* Return how many instructions the machine can issue per cycle. */
14155
c237e94a 14156static int
863d938c 14157rs6000_issue_rate (void)
b6c9286a 14158{
3317bab1
DE
14159 /* Use issue rate of 1 for first scheduling pass to decrease degradation. */
14160 if (!reload_completed)
14161 return 1;
14162
b6c9286a 14163 switch (rs6000_cpu_attr) {
3cb999d8
DE
14164 case CPU_RIOS1: /* ? */
14165 case CPU_RS64A:
14166 case CPU_PPC601: /* ? */
ed947a96 14167 case CPU_PPC7450:
3cb999d8 14168 return 3;
b54cf83a 14169 case CPU_PPC440:
b6c9286a 14170 case CPU_PPC603:
bef84347 14171 case CPU_PPC750:
ed947a96 14172 case CPU_PPC7400:
be12c2b0 14173 case CPU_PPC8540:
bef84347 14174 return 2;
3cb999d8 14175 case CPU_RIOS2:
b6c9286a 14176 case CPU_PPC604:
19684119 14177 case CPU_PPC604E:
b6c9286a 14178 case CPU_PPC620:
3cb999d8 14179 case CPU_PPC630:
b6c9286a 14180 return 4;
cbe26ab8 14181 case CPU_POWER4:
ec507f2d 14182 case CPU_POWER5:
cbe26ab8 14183 return 5;
b6c9286a
MM
14184 default:
14185 return 1;
14186 }
14187}
14188
be12c2b0
VM
14189/* Return how many instructions to look ahead for better insn
14190 scheduling. */
14191
14192static int
863d938c 14193rs6000_use_sched_lookahead (void)
be12c2b0
VM
14194{
14195 if (rs6000_cpu_attr == CPU_PPC8540)
14196 return 4;
14197 return 0;
14198}
14199
569fa502
DN
14200/* Determine is PAT refers to memory. */
14201
14202static bool
14203is_mem_ref (rtx pat)
14204{
14205 const char * fmt;
14206 int i, j;
14207 bool ret = false;
14208
14209 if (GET_CODE (pat) == MEM)
14210 return true;
14211
14212 /* Recursively process the pattern. */
14213 fmt = GET_RTX_FORMAT (GET_CODE (pat));
14214
14215 for (i = GET_RTX_LENGTH (GET_CODE (pat)) - 1; i >= 0 && !ret; i--)
14216 {
14217 if (fmt[i] == 'e')
14218 ret |= is_mem_ref (XEXP (pat, i));
14219 else if (fmt[i] == 'E')
14220 for (j = XVECLEN (pat, i) - 1; j >= 0; j--)
14221 ret |= is_mem_ref (XVECEXP (pat, i, j));
14222 }
14223
14224 return ret;
14225}
14226
14227/* Determine if PAT is a PATTERN of a load insn. */
14228
14229static bool
14230is_load_insn1 (rtx pat)
14231{
14232 if (!pat || pat == NULL_RTX)
14233 return false;
14234
14235 if (GET_CODE (pat) == SET)
14236 return is_mem_ref (SET_SRC (pat));
14237
14238 if (GET_CODE (pat) == PARALLEL)
14239 {
14240 int i;
14241
14242 for (i = 0; i < XVECLEN (pat, 0); i++)
14243 if (is_load_insn1 (XVECEXP (pat, 0, i)))
14244 return true;
14245 }
14246
14247 return false;
14248}
14249
14250/* Determine if INSN loads from memory. */
14251
14252static bool
14253is_load_insn (rtx insn)
14254{
14255 if (!insn || !INSN_P (insn))
14256 return false;
14257
14258 if (GET_CODE (insn) == CALL_INSN)
14259 return false;
14260
14261 return is_load_insn1 (PATTERN (insn));
14262}
14263
14264/* Determine if PAT is a PATTERN of a store insn. */
14265
14266static bool
14267is_store_insn1 (rtx pat)
14268{
14269 if (!pat || pat == NULL_RTX)
14270 return false;
14271
14272 if (GET_CODE (pat) == SET)
14273 return is_mem_ref (SET_DEST (pat));
14274
14275 if (GET_CODE (pat) == PARALLEL)
14276 {
14277 int i;
14278
14279 for (i = 0; i < XVECLEN (pat, 0); i++)
14280 if (is_store_insn1 (XVECEXP (pat, 0, i)))
14281 return true;
14282 }
14283
14284 return false;
14285}
14286
14287/* Determine if INSN stores to memory. */
14288
14289static bool
14290is_store_insn (rtx insn)
14291{
14292 if (!insn || !INSN_P (insn))
14293 return false;
14294
14295 return is_store_insn1 (PATTERN (insn));
14296}
14297
14298/* Returns whether the dependence between INSN and NEXT is considered
14299 costly by the given target. */
14300
14301static bool
14302rs6000_is_costly_dependence (rtx insn, rtx next, rtx link, int cost, int distance)
14303{
14304 /* If the flag is not enbled - no dependence is considered costly;
14305 allow all dependent insns in the same group.
14306 This is the most aggressive option. */
14307 if (rs6000_sched_costly_dep == no_dep_costly)
14308 return false;
14309
14310 /* If the flag is set to 1 - a dependence is always considered costly;
14311 do not allow dependent instructions in the same group.
14312 This is the most conservative option. */
14313 if (rs6000_sched_costly_dep == all_deps_costly)
14314 return true;
14315
14316 if (rs6000_sched_costly_dep == store_to_load_dep_costly
14317 && is_load_insn (next)
14318 && is_store_insn (insn))
14319 /* Prevent load after store in the same group. */
14320 return true;
14321
14322 if (rs6000_sched_costly_dep == true_store_to_load_dep_costly
14323 && is_load_insn (next)
14324 && is_store_insn (insn)
14325 && (!link || (int) REG_NOTE_KIND (link) == 0))
14326 /* Prevent load after store in the same group if it is a true dependence. */
14327 return true;
14328
14329 /* The flag is set to X; dependences with latency >= X are considered costly,
14330 and will not be scheduled in the same group. */
14331 if (rs6000_sched_costly_dep <= max_dep_latency
14332 && ((cost - distance) >= (int)rs6000_sched_costly_dep))
14333 return true;
14334
14335 return false;
14336}
14337
cbe26ab8
DN
14338/* Return the next insn after INSN that is found before TAIL is reached,
14339 skipping any "non-active" insns - insns that will not actually occupy
14340 an issue slot. Return NULL_RTX if such an insn is not found. */
14341
14342static rtx
14343get_next_active_insn (rtx insn, rtx tail)
14344{
14345 rtx next_insn;
14346
14347 if (!insn || insn == tail)
14348 return NULL_RTX;
14349
14350 next_insn = NEXT_INSN (insn);
14351
14352 while (next_insn
14353 && next_insn != tail
14354 && (GET_CODE(next_insn) == NOTE
14355 || GET_CODE (PATTERN (next_insn)) == USE
14356 || GET_CODE (PATTERN (next_insn)) == CLOBBER))
14357 {
14358 next_insn = NEXT_INSN (next_insn);
14359 }
14360
14361 if (!next_insn || next_insn == tail)
14362 return NULL_RTX;
14363
14364 return next_insn;
14365}
14366
839a4992 14367/* Return whether the presence of INSN causes a dispatch group termination
cbe26ab8
DN
14368 of group WHICH_GROUP.
14369
14370 If WHICH_GROUP == current_group, this function will return true if INSN
14371 causes the termination of the current group (i.e, the dispatch group to
14372 which INSN belongs). This means that INSN will be the last insn in the
14373 group it belongs to.
14374
14375 If WHICH_GROUP == previous_group, this function will return true if INSN
14376 causes the termination of the previous group (i.e, the dispatch group that
14377 precedes the group to which INSN belongs). This means that INSN will be
14378 the first insn in the group it belongs to). */
14379
14380static bool
14381insn_terminates_group_p (rtx insn, enum group_termination which_group)
14382{
14383 enum attr_type type;
14384
14385 if (! insn)
14386 return false;
569fa502 14387
cbe26ab8
DN
14388 type = get_attr_type (insn);
14389
14390 if (is_microcoded_insn (insn))
14391 return true;
14392
14393 if (which_group == current_group)
14394 {
14395 if (is_branch_slot_insn (insn))
14396 return true;
14397 return false;
14398 }
14399 else if (which_group == previous_group)
14400 {
14401 if (is_dispatch_slot_restricted (insn))
14402 return true;
14403 return false;
14404 }
14405
14406 return false;
14407}
14408
839a4992 14409/* Return true if it is recommended to keep NEXT_INSN "far" (in a separate
cbe26ab8
DN
14410 dispatch group) from the insns in GROUP_INSNS. Return false otherwise. */
14411
14412static bool
14413is_costly_group (rtx *group_insns, rtx next_insn)
14414{
14415 int i;
14416 rtx link;
14417 int cost;
14418 int issue_rate = rs6000_issue_rate ();
14419
14420 for (i = 0; i < issue_rate; i++)
14421 {
14422 rtx insn = group_insns[i];
14423 if (!insn)
14424 continue;
14425 for (link = INSN_DEPEND (insn); link != 0; link = XEXP (link, 1))
14426 {
14427 rtx next = XEXP (link, 0);
14428 if (next == next_insn)
14429 {
14430 cost = insn_cost (insn, link, next_insn);
14431 if (rs6000_is_costly_dependence (insn, next_insn, link, cost, 0))
14432 return true;
14433 }
14434 }
14435 }
14436
14437 return false;
14438}
14439
14440/* Utility of the function redefine_groups.
14441 Check if it is too costly to schedule NEXT_INSN together with GROUP_INSNS
14442 in the same dispatch group. If so, insert nops before NEXT_INSN, in order
14443 to keep it "far" (in a separate group) from GROUP_INSNS, following
14444 one of the following schemes, depending on the value of the flag
14445 -minsert_sched_nops = X:
14446 (1) X == sched_finish_regroup_exact: insert exactly as many nops as needed
839a4992 14447 in order to force NEXT_INSN into a separate group.
cbe26ab8
DN
14448 (2) X < sched_finish_regroup_exact: insert exactly X nops.
14449 GROUP_END, CAN_ISSUE_MORE and GROUP_COUNT record the state after nop
14450 insertion (has a group just ended, how many vacant issue slots remain in the
14451 last group, and how many dispatch groups were encountered so far). */
14452
14453static int
14454force_new_group (int sched_verbose, FILE *dump, rtx *group_insns, rtx next_insn,
14455 bool *group_end, int can_issue_more, int *group_count)
14456{
14457 rtx nop;
14458 bool force;
14459 int issue_rate = rs6000_issue_rate ();
14460 bool end = *group_end;
14461 int i;
14462
14463 if (next_insn == NULL_RTX)
14464 return can_issue_more;
14465
14466 if (rs6000_sched_insert_nops > sched_finish_regroup_exact)
14467 return can_issue_more;
14468
14469 force = is_costly_group (group_insns, next_insn);
14470 if (!force)
14471 return can_issue_more;
14472
14473 if (sched_verbose > 6)
14474 fprintf (dump,"force: group count = %d, can_issue_more = %d\n",
14475 *group_count ,can_issue_more);
14476
14477 if (rs6000_sched_insert_nops == sched_finish_regroup_exact)
14478 {
14479 if (*group_end)
14480 can_issue_more = 0;
14481
14482 /* Since only a branch can be issued in the last issue_slot, it is
14483 sufficient to insert 'can_issue_more - 1' nops if next_insn is not
14484 a branch. If next_insn is a branch, we insert 'can_issue_more' nops;
14485 in this case the last nop will start a new group and the branch will be
14486 forced to the new group. */
14487 if (can_issue_more && !is_branch_slot_insn (next_insn))
14488 can_issue_more--;
14489
14490 while (can_issue_more > 0)
14491 {
14492 nop = gen_nop();
14493 emit_insn_before (nop, next_insn);
14494 can_issue_more--;
14495 }
14496
14497 *group_end = true;
14498 return 0;
14499 }
14500
14501 if (rs6000_sched_insert_nops < sched_finish_regroup_exact)
14502 {
14503 int n_nops = rs6000_sched_insert_nops;
14504
14505 /* Nops can't be issued from the branch slot, so the effective
14506 issue_rate for nops is 'issue_rate - 1'. */
14507 if (can_issue_more == 0)
14508 can_issue_more = issue_rate;
14509 can_issue_more--;
14510 if (can_issue_more == 0)
14511 {
14512 can_issue_more = issue_rate - 1;
14513 (*group_count)++;
14514 end = true;
14515 for (i = 0; i < issue_rate; i++)
14516 {
14517 group_insns[i] = 0;
14518 }
14519 }
14520
14521 while (n_nops > 0)
14522 {
14523 nop = gen_nop ();
14524 emit_insn_before (nop, next_insn);
14525 if (can_issue_more == issue_rate - 1) /* new group begins */
14526 end = false;
14527 can_issue_more--;
14528 if (can_issue_more == 0)
14529 {
14530 can_issue_more = issue_rate - 1;
14531 (*group_count)++;
14532 end = true;
14533 for (i = 0; i < issue_rate; i++)
14534 {
14535 group_insns[i] = 0;
14536 }
14537 }
14538 n_nops--;
14539 }
14540
14541 /* Scale back relative to 'issue_rate' (instead of 'issue_rate - 1'). */
14542 can_issue_more++;
14543
14544 *group_end = /* Is next_insn going to start a new group? */
14545 (end
14546 || (can_issue_more == 1 && !is_branch_slot_insn (next_insn))
14547 || (can_issue_more <= 2 && is_cracked_insn (next_insn))
14548 || (can_issue_more < issue_rate &&
14549 insn_terminates_group_p (next_insn, previous_group)));
14550 if (*group_end && end)
14551 (*group_count)--;
14552
14553 if (sched_verbose > 6)
14554 fprintf (dump, "done force: group count = %d, can_issue_more = %d\n",
14555 *group_count, can_issue_more);
14556 return can_issue_more;
14557 }
14558
14559 return can_issue_more;
14560}
14561
14562/* This function tries to synch the dispatch groups that the compiler "sees"
14563 with the dispatch groups that the processor dispatcher is expected to
14564 form in practice. It tries to achieve this synchronization by forcing the
14565 estimated processor grouping on the compiler (as opposed to the function
14566 'pad_goups' which tries to force the scheduler's grouping on the processor).
14567
14568 The function scans the insn sequence between PREV_HEAD_INSN and TAIL and
14569 examines the (estimated) dispatch groups that will be formed by the processor
14570 dispatcher. It marks these group boundaries to reflect the estimated
14571 processor grouping, overriding the grouping that the scheduler had marked.
14572 Depending on the value of the flag '-minsert-sched-nops' this function can
14573 force certain insns into separate groups or force a certain distance between
14574 them by inserting nops, for example, if there exists a "costly dependence"
14575 between the insns.
14576
14577 The function estimates the group boundaries that the processor will form as
14578 folllows: It keeps track of how many vacant issue slots are available after
14579 each insn. A subsequent insn will start a new group if one of the following
14580 4 cases applies:
14581 - no more vacant issue slots remain in the current dispatch group.
14582 - only the last issue slot, which is the branch slot, is vacant, but the next
14583 insn is not a branch.
14584 - only the last 2 or less issue slots, including the branch slot, are vacant,
14585 which means that a cracked insn (which occupies two issue slots) can't be
14586 issued in this group.
14587 - less than 'issue_rate' slots are vacant, and the next insn always needs to
14588 start a new group. */
14589
14590static int
14591redefine_groups (FILE *dump, int sched_verbose, rtx prev_head_insn, rtx tail)
14592{
14593 rtx insn, next_insn;
14594 int issue_rate;
14595 int can_issue_more;
14596 int slot, i;
14597 bool group_end;
14598 int group_count = 0;
14599 rtx *group_insns;
14600
14601 /* Initialize. */
14602 issue_rate = rs6000_issue_rate ();
14603 group_insns = alloca (issue_rate * sizeof (rtx));
14604 for (i = 0; i < issue_rate; i++)
14605 {
14606 group_insns[i] = 0;
14607 }
14608 can_issue_more = issue_rate;
14609 slot = 0;
14610 insn = get_next_active_insn (prev_head_insn, tail);
14611 group_end = false;
14612
14613 while (insn != NULL_RTX)
14614 {
14615 slot = (issue_rate - can_issue_more);
14616 group_insns[slot] = insn;
14617 can_issue_more =
14618 rs6000_variable_issue (dump, sched_verbose, insn, can_issue_more);
14619 if (insn_terminates_group_p (insn, current_group))
14620 can_issue_more = 0;
14621
14622 next_insn = get_next_active_insn (insn, tail);
14623 if (next_insn == NULL_RTX)
14624 return group_count + 1;
14625
14626 group_end = /* Is next_insn going to start a new group? */
14627 (can_issue_more == 0
14628 || (can_issue_more == 1 && !is_branch_slot_insn (next_insn))
14629 || (can_issue_more <= 2 && is_cracked_insn (next_insn))
14630 || (can_issue_more < issue_rate &&
14631 insn_terminates_group_p (next_insn, previous_group)));
14632
14633 can_issue_more = force_new_group (sched_verbose, dump, group_insns,
14634 next_insn, &group_end, can_issue_more, &group_count);
14635
14636 if (group_end)
14637 {
14638 group_count++;
14639 can_issue_more = 0;
14640 for (i = 0; i < issue_rate; i++)
14641 {
14642 group_insns[i] = 0;
14643 }
14644 }
14645
14646 if (GET_MODE (next_insn) == TImode && can_issue_more)
14647 PUT_MODE(next_insn, VOIDmode);
14648 else if (!can_issue_more && GET_MODE (next_insn) != TImode)
14649 PUT_MODE (next_insn, TImode);
14650
14651 insn = next_insn;
14652 if (can_issue_more == 0)
14653 can_issue_more = issue_rate;
14654 } /* while */
14655
14656 return group_count;
14657}
14658
14659/* Scan the insn sequence between PREV_HEAD_INSN and TAIL and examine the
14660 dispatch group boundaries that the scheduler had marked. Pad with nops
14661 any dispatch groups which have vacant issue slots, in order to force the
14662 scheduler's grouping on the processor dispatcher. The function
14663 returns the number of dispatch groups found. */
14664
14665static int
14666pad_groups (FILE *dump, int sched_verbose, rtx prev_head_insn, rtx tail)
14667{
14668 rtx insn, next_insn;
14669 rtx nop;
14670 int issue_rate;
14671 int can_issue_more;
14672 int group_end;
14673 int group_count = 0;
14674
14675 /* Initialize issue_rate. */
14676 issue_rate = rs6000_issue_rate ();
14677 can_issue_more = issue_rate;
14678
14679 insn = get_next_active_insn (prev_head_insn, tail);
14680 next_insn = get_next_active_insn (insn, tail);
14681
14682 while (insn != NULL_RTX)
14683 {
14684 can_issue_more =
14685 rs6000_variable_issue (dump, sched_verbose, insn, can_issue_more);
14686
14687 group_end = (next_insn == NULL_RTX || GET_MODE (next_insn) == TImode);
14688
14689 if (next_insn == NULL_RTX)
14690 break;
14691
14692 if (group_end)
14693 {
14694 /* If the scheduler had marked group termination at this location
14695 (between insn and next_indn), and neither insn nor next_insn will
14696 force group termination, pad the group with nops to force group
14697 termination. */
14698 if (can_issue_more
14699 && (rs6000_sched_insert_nops == sched_finish_pad_groups)
14700 && !insn_terminates_group_p (insn, current_group)
14701 && !insn_terminates_group_p (next_insn, previous_group))
14702 {
14703 if (!is_branch_slot_insn(next_insn))
14704 can_issue_more--;
14705
14706 while (can_issue_more)
14707 {
14708 nop = gen_nop ();
14709 emit_insn_before (nop, next_insn);
14710 can_issue_more--;
14711 }
14712 }
14713
14714 can_issue_more = issue_rate;
14715 group_count++;
14716 }
14717
14718 insn = next_insn;
14719 next_insn = get_next_active_insn (insn, tail);
14720 }
14721
14722 return group_count;
14723}
14724
14725/* The following function is called at the end of scheduling BB.
14726 After reload, it inserts nops at insn group bundling. */
14727
14728static void
38f391a5 14729rs6000_sched_finish (FILE *dump, int sched_verbose)
cbe26ab8
DN
14730{
14731 int n_groups;
14732
14733 if (sched_verbose)
14734 fprintf (dump, "=== Finishing schedule.\n");
14735
ec507f2d 14736 if (reload_completed && rs6000_sched_groups)
cbe26ab8
DN
14737 {
14738 if (rs6000_sched_insert_nops == sched_finish_none)
14739 return;
14740
14741 if (rs6000_sched_insert_nops == sched_finish_pad_groups)
14742 n_groups = pad_groups (dump, sched_verbose,
14743 current_sched_info->prev_head,
14744 current_sched_info->next_tail);
14745 else
14746 n_groups = redefine_groups (dump, sched_verbose,
14747 current_sched_info->prev_head,
14748 current_sched_info->next_tail);
14749
14750 if (sched_verbose >= 6)
14751 {
14752 fprintf (dump, "ngroups = %d\n", n_groups);
14753 print_rtl (dump, current_sched_info->prev_head);
14754 fprintf (dump, "Done finish_sched\n");
14755 }
14756 }
14757}
b6c9286a 14758\f
b6c9286a
MM
14759/* Length in units of the trampoline for entering a nested function. */
14760
14761int
863d938c 14762rs6000_trampoline_size (void)
b6c9286a
MM
14763{
14764 int ret = 0;
14765
14766 switch (DEFAULT_ABI)
14767 {
14768 default:
14769 abort ();
14770
14771 case ABI_AIX:
8f802bfb 14772 ret = (TARGET_32BIT) ? 12 : 24;
b6c9286a
MM
14773 break;
14774
4dabc42d 14775 case ABI_DARWIN:
b6c9286a 14776 case ABI_V4:
03a7e1a5 14777 ret = (TARGET_32BIT) ? 40 : 48;
b6c9286a 14778 break;
b6c9286a
MM
14779 }
14780
14781 return ret;
14782}
14783
14784/* Emit RTL insns to initialize the variable parts of a trampoline.
14785 FNADDR is an RTX for the address of the function's pure code.
14786 CXT is an RTX for the static chain value for the function. */
14787
14788void
a2369ed3 14789rs6000_initialize_trampoline (rtx addr, rtx fnaddr, rtx cxt)
b6c9286a 14790{
ac2a93a1 14791 enum machine_mode pmode = Pmode;
8bd04c56
MM
14792 int regsize = (TARGET_32BIT) ? 4 : 8;
14793 rtx ctx_reg = force_reg (pmode, cxt);
b6c9286a
MM
14794
14795 switch (DEFAULT_ABI)
14796 {
14797 default:
14798 abort ();
14799
8bd04c56 14800/* Macros to shorten the code expansions below. */
39403d82 14801#define MEM_DEREF(addr) gen_rtx_MEM (pmode, memory_address (pmode, addr))
c5c76735
JL
14802#define MEM_PLUS(addr,offset) \
14803 gen_rtx_MEM (pmode, memory_address (pmode, plus_constant (addr, offset)))
7c59dc5d 14804
b6c9286a
MM
14805 /* Under AIX, just build the 3 word function descriptor */
14806 case ABI_AIX:
8bd04c56
MM
14807 {
14808 rtx fn_reg = gen_reg_rtx (pmode);
14809 rtx toc_reg = gen_reg_rtx (pmode);
14810 emit_move_insn (fn_reg, MEM_DEREF (fnaddr));
1cb18e3c 14811 emit_move_insn (toc_reg, MEM_PLUS (fnaddr, regsize));
8bd04c56
MM
14812 emit_move_insn (MEM_DEREF (addr), fn_reg);
14813 emit_move_insn (MEM_PLUS (addr, regsize), toc_reg);
14814 emit_move_insn (MEM_PLUS (addr, 2*regsize), ctx_reg);
14815 }
b6c9286a
MM
14816 break;
14817
4dabc42d
TC
14818 /* Under V.4/eabi/darwin, __trampoline_setup does the real work. */
14819 case ABI_DARWIN:
b6c9286a 14820 case ABI_V4:
39403d82 14821 emit_library_call (gen_rtx_SYMBOL_REF (SImode, "__trampoline_setup"),
eaf1bcf1
MM
14822 FALSE, VOIDmode, 4,
14823 addr, pmode,
14824 GEN_INT (rs6000_trampoline_size ()), SImode,
14825 fnaddr, pmode,
14826 ctx_reg, pmode);
b6c9286a 14827 break;
b6c9286a
MM
14828 }
14829
14830 return;
14831}
7509c759
MM
14832
14833\f
91d231cb 14834/* Table of valid machine attributes. */
a4f6c312 14835
91d231cb 14836const struct attribute_spec rs6000_attribute_table[] =
7509c759 14837{
91d231cb 14838 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
8bb418a3 14839 { "altivec", 1, 1, false, true, false, rs6000_handle_altivec_attribute },
a5c76ee6
ZW
14840 { "longcall", 0, 0, false, true, true, rs6000_handle_longcall_attribute },
14841 { "shortcall", 0, 0, false, true, true, rs6000_handle_longcall_attribute },
14842 { NULL, 0, 0, false, false, false, NULL }
91d231cb 14843};
7509c759 14844
8bb418a3
ZL
14845/* Handle the "altivec" attribute. The attribute may have
14846 arguments as follows:
14847
14848 __attribute__((altivec(vector__)))
14849 __attribute__((altivec(pixel__))) (always followed by 'unsigned short')
14850 __attribute__((altivec(bool__))) (always followed by 'unsigned')
14851
14852 and may appear more than once (e.g., 'vector bool char') in a
14853 given declaration. */
14854
14855static tree
14856rs6000_handle_altivec_attribute (tree *node, tree name, tree args,
14857 int flags ATTRIBUTE_UNUSED,
14858 bool *no_add_attrs)
14859{
14860 tree type = *node, result = NULL_TREE;
14861 enum machine_mode mode;
14862 int unsigned_p;
14863 char altivec_type
14864 = ((args && TREE_CODE (args) == TREE_LIST && TREE_VALUE (args)
14865 && TREE_CODE (TREE_VALUE (args)) == IDENTIFIER_NODE)
14866 ? *IDENTIFIER_POINTER (TREE_VALUE (args))
14867 : '?');
14868
14869 while (POINTER_TYPE_P (type)
14870 || TREE_CODE (type) == FUNCTION_TYPE
14871 || TREE_CODE (type) == METHOD_TYPE
14872 || TREE_CODE (type) == ARRAY_TYPE)
14873 type = TREE_TYPE (type);
14874
14875 mode = TYPE_MODE (type);
14876
14877 if (rs6000_warn_altivec_long
14878 && (type == long_unsigned_type_node || type == long_integer_type_node))
14879 warning ("use of 'long' in AltiVec types is deprecated; use 'int'");
14880
14881 switch (altivec_type)
14882 {
14883 case 'v':
14884 unsigned_p = TREE_UNSIGNED (type);
14885 switch (mode)
14886 {
14887 case SImode:
14888 result = (unsigned_p ? unsigned_V4SI_type_node : V4SI_type_node);
14889 break;
14890 case HImode:
14891 result = (unsigned_p ? unsigned_V8HI_type_node : V8HI_type_node);
14892 break;
14893 case QImode:
14894 result = (unsigned_p ? unsigned_V16QI_type_node : V16QI_type_node);
14895 break;
14896 case SFmode: result = V4SF_type_node; break;
14897 /* If the user says 'vector int bool', we may be handed the 'bool'
14898 attribute _before_ the 'vector' attribute, and so select the proper
14899 type in the 'b' case below. */
14900 case V4SImode: case V8HImode: case V16QImode: result = type;
14901 default: break;
14902 }
14903 break;
14904 case 'b':
14905 switch (mode)
14906 {
14907 case SImode: case V4SImode: result = bool_V4SI_type_node; break;
14908 case HImode: case V8HImode: result = bool_V8HI_type_node; break;
14909 case QImode: case V16QImode: result = bool_V16QI_type_node;
14910 default: break;
14911 }
14912 break;
14913 case 'p':
14914 switch (mode)
14915 {
14916 case V8HImode: result = pixel_V8HI_type_node;
14917 default: break;
14918 }
14919 default: break;
14920 }
14921
14922 *no_add_attrs = true; /* No need to hang on to the attribute. */
14923
14924 if (!result)
14925 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
14926 else
14927 *node = reconstruct_complex_type (*node, result);
14928
14929 return NULL_TREE;
14930}
14931
f18eca82
ZL
14932/* AltiVec defines four built-in scalar types that serve as vector
14933 elements; we must teach the compiler how to mangle them. */
14934
14935static const char *
14936rs6000_mangle_fundamental_type (tree type)
14937{
14938 if (type == bool_char_type_node) return "U6__boolc";
14939 if (type == bool_short_type_node) return "U6__bools";
14940 if (type == pixel_type_node) return "u7__pixel";
14941 if (type == bool_int_type_node) return "U6__booli";
14942
14943 /* For all other types, use normal C++ mangling. */
14944 return NULL;
14945}
14946
a5c76ee6
ZW
14947/* Handle a "longcall" or "shortcall" attribute; arguments as in
14948 struct attribute_spec.handler. */
a4f6c312 14949
91d231cb 14950static tree
a2369ed3
DJ
14951rs6000_handle_longcall_attribute (tree *node, tree name,
14952 tree args ATTRIBUTE_UNUSED,
14953 int flags ATTRIBUTE_UNUSED,
14954 bool *no_add_attrs)
91d231cb
JM
14955{
14956 if (TREE_CODE (*node) != FUNCTION_TYPE
14957 && TREE_CODE (*node) != FIELD_DECL
14958 && TREE_CODE (*node) != TYPE_DECL)
14959 {
14960 warning ("`%s' attribute only applies to functions",
14961 IDENTIFIER_POINTER (name));
14962 *no_add_attrs = true;
14963 }
6a4cee5f 14964
91d231cb 14965 return NULL_TREE;
7509c759
MM
14966}
14967
a5c76ee6
ZW
14968/* Set longcall attributes on all functions declared when
14969 rs6000_default_long_calls is true. */
14970static void
a2369ed3 14971rs6000_set_default_type_attributes (tree type)
a5c76ee6
ZW
14972{
14973 if (rs6000_default_long_calls
14974 && (TREE_CODE (type) == FUNCTION_TYPE
14975 || TREE_CODE (type) == METHOD_TYPE))
14976 TYPE_ATTRIBUTES (type) = tree_cons (get_identifier ("longcall"),
14977 NULL_TREE,
14978 TYPE_ATTRIBUTES (type));
14979}
14980
3cb999d8
DE
14981/* Return a reference suitable for calling a function with the
14982 longcall attribute. */
a4f6c312 14983
6a4cee5f 14984struct rtx_def *
a2369ed3 14985rs6000_longcall_ref (rtx call_ref)
6a4cee5f 14986{
d330fd93 14987 const char *call_name;
6a4cee5f
MM
14988 tree node;
14989
14990 if (GET_CODE (call_ref) != SYMBOL_REF)
14991 return call_ref;
14992
14993 /* System V adds '.' to the internal name, so skip them. */
14994 call_name = XSTR (call_ref, 0);
14995 if (*call_name == '.')
14996 {
14997 while (*call_name == '.')
14998 call_name++;
14999
15000 node = get_identifier (call_name);
39403d82 15001 call_ref = gen_rtx_SYMBOL_REF (VOIDmode, IDENTIFIER_POINTER (node));
6a4cee5f
MM
15002 }
15003
15004 return force_reg (Pmode, call_ref);
15005}
7509c759 15006\f
b64a1b53
RH
15007#ifdef USING_ELFOS_H
15008
7509c759
MM
15009/* A C statement or statements to switch to the appropriate section
15010 for output of RTX in mode MODE. You can assume that RTX is some
15011 kind of constant in RTL. The argument MODE is redundant except in
15012 the case of a `const_int' rtx. Select the section by calling
15013 `text_section' or one of the alternatives for other sections.
15014
15015 Do not define this macro if you put all constants in the read-only
15016 data section. */
15017
b64a1b53 15018static void
a2369ed3
DJ
15019rs6000_elf_select_rtx_section (enum machine_mode mode, rtx x,
15020 unsigned HOST_WIDE_INT align)
7509c759 15021{
a9098fd0 15022 if (ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (x, mode))
7509c759 15023 toc_section ();
7509c759 15024 else
b64a1b53 15025 default_elf_select_rtx_section (mode, x, align);
7509c759
MM
15026}
15027
15028/* A C statement or statements to switch to the appropriate
15029 section for output of DECL. DECL is either a `VAR_DECL' node
15030 or a constant of some sort. RELOC indicates whether forming
15031 the initial value of DECL requires link-time relocations. */
15032
ae46c4e0 15033static void
a2369ed3
DJ
15034rs6000_elf_select_section (tree decl, int reloc,
15035 unsigned HOST_WIDE_INT align)
7509c759 15036{
f1384257
AM
15037 /* Pretend that we're always building for a shared library when
15038 ABI_AIX, because otherwise we end up with dynamic relocations
15039 in read-only sections. This happens for function pointers,
15040 references to vtables in typeinfo, and probably other cases. */
0e5dbd9b
DE
15041 default_elf_select_section_1 (decl, reloc, align,
15042 flag_pic || DEFAULT_ABI == ABI_AIX);
63019373
GK
15043}
15044
15045/* A C statement to build up a unique section name, expressed as a
15046 STRING_CST node, and assign it to DECL_SECTION_NAME (decl).
15047 RELOC indicates whether the initial value of EXP requires
15048 link-time relocations. If you do not define this macro, GCC will use
15049 the symbol name prefixed by `.' as the section name. Note - this
f5143c46 15050 macro can now be called for uninitialized data items as well as
4912a07c 15051 initialized data and functions. */
63019373 15052
ae46c4e0 15053static void
a2369ed3 15054rs6000_elf_unique_section (tree decl, int reloc)
63019373 15055{
f1384257
AM
15056 /* As above, pretend that we're always building for a shared library
15057 when ABI_AIX, to avoid dynamic relocations in read-only sections. */
0e5dbd9b
DE
15058 default_unique_section_1 (decl, reloc,
15059 flag_pic || DEFAULT_ABI == ABI_AIX);
7509c759 15060}
d9407988 15061\f
d1908feb
JJ
15062/* For a SYMBOL_REF, set generic flags and then perform some
15063 target-specific processing.
15064
d1908feb
JJ
15065 When the AIX ABI is requested on a non-AIX system, replace the
15066 function name with the real name (with a leading .) rather than the
15067 function descriptor name. This saves a lot of overriding code to
15068 read the prefixes. */
d9407988 15069
fb49053f 15070static void
a2369ed3 15071rs6000_elf_encode_section_info (tree decl, rtx rtl, int first)
d9407988 15072{
d1908feb 15073 default_encode_section_info (decl, rtl, first);
b2003250 15074
d1908feb
JJ
15075 if (first
15076 && TREE_CODE (decl) == FUNCTION_DECL
15077 && !TARGET_AIX
15078 && DEFAULT_ABI == ABI_AIX)
d9407988 15079 {
c6a2438a 15080 rtx sym_ref = XEXP (rtl, 0);
d1908feb
JJ
15081 size_t len = strlen (XSTR (sym_ref, 0));
15082 char *str = alloca (len + 2);
15083 str[0] = '.';
15084 memcpy (str + 1, XSTR (sym_ref, 0), len + 1);
15085 XSTR (sym_ref, 0) = ggc_alloc_string (str, len + 1);
d9407988 15086 }
d9407988
MM
15087}
15088
0e5dbd9b 15089static bool
a2369ed3 15090rs6000_elf_in_small_data_p (tree decl)
0e5dbd9b
DE
15091{
15092 if (rs6000_sdata == SDATA_NONE)
15093 return false;
15094
15095 if (TREE_CODE (decl) == VAR_DECL && DECL_SECTION_NAME (decl))
15096 {
15097 const char *section = TREE_STRING_POINTER (DECL_SECTION_NAME (decl));
15098 if (strcmp (section, ".sdata") == 0
15099 || strcmp (section, ".sdata2") == 0
20bfcd69
GK
15100 || strcmp (section, ".sbss") == 0
15101 || strcmp (section, ".sbss2") == 0
15102 || strcmp (section, ".PPC.EMB.sdata0") == 0
15103 || strcmp (section, ".PPC.EMB.sbss0") == 0)
0e5dbd9b
DE
15104 return true;
15105 }
15106 else
15107 {
15108 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (decl));
15109
15110 if (size > 0
307b599c 15111 && (unsigned HOST_WIDE_INT) size <= g_switch_value
20bfcd69
GK
15112 /* If it's not public, and we're not going to reference it there,
15113 there's no need to put it in the small data section. */
0e5dbd9b
DE
15114 && (rs6000_sdata != SDATA_DATA || TREE_PUBLIC (decl)))
15115 return true;
15116 }
15117
15118 return false;
15119}
15120
b91da81f 15121#endif /* USING_ELFOS_H */
000034eb 15122
a6c2a102 15123\f
000034eb 15124/* Return a REG that occurs in ADDR with coefficient 1.
02441cd6
JL
15125 ADDR can be effectively incremented by incrementing REG.
15126
15127 r0 is special and we must not select it as an address
15128 register by this routine since our caller will try to
15129 increment the returned register via an "la" instruction. */
000034eb
DE
15130
15131struct rtx_def *
a2369ed3 15132find_addr_reg (rtx addr)
000034eb
DE
15133{
15134 while (GET_CODE (addr) == PLUS)
15135 {
02441cd6
JL
15136 if (GET_CODE (XEXP (addr, 0)) == REG
15137 && REGNO (XEXP (addr, 0)) != 0)
000034eb 15138 addr = XEXP (addr, 0);
02441cd6
JL
15139 else if (GET_CODE (XEXP (addr, 1)) == REG
15140 && REGNO (XEXP (addr, 1)) != 0)
000034eb
DE
15141 addr = XEXP (addr, 1);
15142 else if (CONSTANT_P (XEXP (addr, 0)))
15143 addr = XEXP (addr, 1);
15144 else if (CONSTANT_P (XEXP (addr, 1)))
15145 addr = XEXP (addr, 0);
15146 else
15147 abort ();
15148 }
02441cd6 15149 if (GET_CODE (addr) == REG && REGNO (addr) != 0)
000034eb
DE
15150 return addr;
15151 abort ();
15152}
15153
a6c2a102 15154void
a2369ed3 15155rs6000_fatal_bad_address (rtx op)
a6c2a102
DE
15156{
15157 fatal_insn ("bad address", op);
15158}
c8023011 15159
ee890fe2
SS
15160#if TARGET_MACHO
15161
15162#if 0
15163/* Returns 1 if OP is either a symbol reference or a sum of a symbol
15164 reference and a constant. */
15165
15166int
a2369ed3 15167symbolic_operand (rtx op)
ee890fe2
SS
15168{
15169 switch (GET_CODE (op))
15170 {
15171 case SYMBOL_REF:
15172 case LABEL_REF:
15173 return 1;
15174 case CONST:
15175 op = XEXP (op, 0);
15176 return (GET_CODE (op) == SYMBOL_REF ||
15177 (GET_CODE (XEXP (op, 0)) == SYMBOL_REF
15178 || GET_CODE (XEXP (op, 0)) == LABEL_REF)
15179 && GET_CODE (XEXP (op, 1)) == CONST_INT);
15180 default:
15181 return 0;
15182 }
c8023011 15183}
ee890fe2
SS
15184#endif
15185
efdba735 15186#if TARGET_MACHO
ee890fe2 15187
efdba735 15188static tree branch_island_list = 0;
ee890fe2 15189
efdba735
SH
15190/* Remember to generate a branch island for far calls to the given
15191 function. */
ee890fe2 15192
efdba735
SH
15193static void
15194add_compiler_branch_island (tree label_name, tree function_name, int line_number)
ee890fe2 15195{
efdba735
SH
15196 tree branch_island = build_tree_list (function_name, label_name);
15197 TREE_TYPE (branch_island) = build_int_2 (line_number, 0);
15198 TREE_CHAIN (branch_island) = branch_island_list;
15199 branch_island_list = branch_island;
ee890fe2
SS
15200}
15201
efdba735
SH
15202#define BRANCH_ISLAND_LABEL_NAME(BRANCH_ISLAND) TREE_VALUE (BRANCH_ISLAND)
15203#define BRANCH_ISLAND_FUNCTION_NAME(BRANCH_ISLAND) TREE_PURPOSE (BRANCH_ISLAND)
15204#define BRANCH_ISLAND_LINE_NUMBER(BRANCH_ISLAND) \
15205 TREE_INT_CST_LOW (TREE_TYPE (BRANCH_ISLAND))
ee890fe2 15206
efdba735
SH
15207/* Generate far-jump branch islands for everything on the
15208 branch_island_list. Invoked immediately after the last instruction
15209 of the epilogue has been emitted; the branch-islands must be
15210 appended to, and contiguous with, the function body. Mach-O stubs
15211 are generated in machopic_output_stub(). */
ee890fe2 15212
efdba735
SH
15213static void
15214macho_branch_islands (void)
15215{
15216 char tmp_buf[512];
15217 tree branch_island;
15218
15219 for (branch_island = branch_island_list;
15220 branch_island;
15221 branch_island = TREE_CHAIN (branch_island))
15222 {
15223 const char *label =
15224 IDENTIFIER_POINTER (BRANCH_ISLAND_LABEL_NAME (branch_island));
15225 const char *name =
15226 darwin_strip_name_encoding (
15227 IDENTIFIER_POINTER (BRANCH_ISLAND_FUNCTION_NAME (branch_island)));
15228 char name_buf[512];
15229 /* Cheap copy of the details from the Darwin ASM_OUTPUT_LABELREF(). */
15230 if (name[0] == '*' || name[0] == '&')
15231 strcpy (name_buf, name+1);
15232 else
15233 {
15234 name_buf[0] = '_';
15235 strcpy (name_buf+1, name);
15236 }
15237 strcpy (tmp_buf, "\n");
15238 strcat (tmp_buf, label);
ee890fe2 15239#if defined (DBX_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)
efdba735 15240 if (write_symbols == DBX_DEBUG || write_symbols == XCOFF_DEBUG)
f7efd730 15241 fprintf (asm_out_file, "\t.stabd 68,0," HOST_WIDE_INT_PRINT_UNSIGNED "\n",
efdba735 15242 BRANCH_ISLAND_LINE_NUMBER(branch_island));
ee890fe2 15243#endif /* DBX_DEBUGGING_INFO || XCOFF_DEBUGGING_INFO */
efdba735
SH
15244 if (flag_pic)
15245 {
15246 strcat (tmp_buf, ":\n\tmflr r0\n\tbcl 20,31,");
15247 strcat (tmp_buf, label);
15248 strcat (tmp_buf, "_pic\n");
15249 strcat (tmp_buf, label);
15250 strcat (tmp_buf, "_pic:\n\tmflr r11\n");
15251
15252 strcat (tmp_buf, "\taddis r11,r11,ha16(");
15253 strcat (tmp_buf, name_buf);
15254 strcat (tmp_buf, " - ");
15255 strcat (tmp_buf, label);
15256 strcat (tmp_buf, "_pic)\n");
15257
15258 strcat (tmp_buf, "\tmtlr r0\n");
15259
15260 strcat (tmp_buf, "\taddi r12,r11,lo16(");
15261 strcat (tmp_buf, name_buf);
15262 strcat (tmp_buf, " - ");
15263 strcat (tmp_buf, label);
15264 strcat (tmp_buf, "_pic)\n");
15265
15266 strcat (tmp_buf, "\tmtctr r12\n\tbctr\n");
15267 }
15268 else
15269 {
15270 strcat (tmp_buf, ":\nlis r12,hi16(");
15271 strcat (tmp_buf, name_buf);
15272 strcat (tmp_buf, ")\n\tori r12,r12,lo16(");
15273 strcat (tmp_buf, name_buf);
15274 strcat (tmp_buf, ")\n\tmtctr r12\n\tbctr");
15275 }
15276 output_asm_insn (tmp_buf, 0);
ee890fe2 15277#if defined (DBX_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)
efdba735 15278 if (write_symbols == DBX_DEBUG || write_symbols == XCOFF_DEBUG)
f7efd730 15279 fprintf(asm_out_file, "\t.stabd 68,0," HOST_WIDE_INT_PRINT_UNSIGNED "\n",
efdba735 15280 BRANCH_ISLAND_LINE_NUMBER (branch_island));
ee890fe2 15281#endif /* DBX_DEBUGGING_INFO || XCOFF_DEBUGGING_INFO */
efdba735 15282 }
ee890fe2 15283
efdba735 15284 branch_island_list = 0;
ee890fe2
SS
15285}
15286
15287/* NO_PREVIOUS_DEF checks in the link list whether the function name is
15288 already there or not. */
15289
efdba735 15290static int
a2369ed3 15291no_previous_def (tree function_name)
ee890fe2 15292{
efdba735
SH
15293 tree branch_island;
15294 for (branch_island = branch_island_list;
15295 branch_island;
15296 branch_island = TREE_CHAIN (branch_island))
15297 if (function_name == BRANCH_ISLAND_FUNCTION_NAME (branch_island))
ee890fe2
SS
15298 return 0;
15299 return 1;
15300}
15301
15302/* GET_PREV_LABEL gets the label name from the previous definition of
15303 the function. */
15304
efdba735 15305static tree
a2369ed3 15306get_prev_label (tree function_name)
ee890fe2 15307{
efdba735
SH
15308 tree branch_island;
15309 for (branch_island = branch_island_list;
15310 branch_island;
15311 branch_island = TREE_CHAIN (branch_island))
15312 if (function_name == BRANCH_ISLAND_FUNCTION_NAME (branch_island))
15313 return BRANCH_ISLAND_LABEL_NAME (branch_island);
ee890fe2
SS
15314 return 0;
15315}
15316
15317/* INSN is either a function call or a millicode call. It may have an
15318 unconditional jump in its delay slot.
15319
15320 CALL_DEST is the routine we are calling. */
15321
15322char *
efdba735 15323output_call (rtx insn, rtx *operands, int dest_operand_number, int cookie_operand_number)
ee890fe2
SS
15324{
15325 static char buf[256];
efdba735
SH
15326 if (GET_CODE (operands[dest_operand_number]) == SYMBOL_REF
15327 && (INTVAL (operands[cookie_operand_number]) & CALL_LONG))
ee890fe2
SS
15328 {
15329 tree labelname;
efdba735 15330 tree funname = get_identifier (XSTR (operands[dest_operand_number], 0));
ee890fe2
SS
15331
15332 if (no_previous_def (funname))
15333 {
308c142a 15334 int line_number = 0;
ee890fe2
SS
15335 rtx label_rtx = gen_label_rtx ();
15336 char *label_buf, temp_buf[256];
15337 ASM_GENERATE_INTERNAL_LABEL (temp_buf, "L",
15338 CODE_LABEL_NUMBER (label_rtx));
15339 label_buf = temp_buf[0] == '*' ? temp_buf + 1 : temp_buf;
15340 labelname = get_identifier (label_buf);
15341 for (; insn && GET_CODE (insn) != NOTE; insn = PREV_INSN (insn));
15342 if (insn)
15343 line_number = NOTE_LINE_NUMBER (insn);
efdba735 15344 add_compiler_branch_island (labelname, funname, line_number);
ee890fe2
SS
15345 }
15346 else
15347 labelname = get_prev_label (funname);
15348
efdba735
SH
15349 /* "jbsr foo, L42" is Mach-O for "Link as 'bl foo' if a 'bl'
15350 instruction will reach 'foo', otherwise link as 'bl L42'".
15351 "L42" should be a 'branch island', that will do a far jump to
15352 'foo'. Branch islands are generated in
15353 macho_branch_islands(). */
ee890fe2 15354 sprintf (buf, "jbsr %%z%d,%.246s",
efdba735 15355 dest_operand_number, IDENTIFIER_POINTER (labelname));
ee890fe2
SS
15356 }
15357 else
efdba735
SH
15358 sprintf (buf, "bl %%z%d", dest_operand_number);
15359 return buf;
ee890fe2
SS
15360}
15361
efdba735 15362#endif /* TARGET_MACHO */
ee890fe2 15363
ee890fe2
SS
15364/* Generate PIC and indirect symbol stubs. */
15365
15366void
a2369ed3 15367machopic_output_stub (FILE *file, const char *symb, const char *stub)
ee890fe2
SS
15368{
15369 unsigned int length;
a4f6c312
SS
15370 char *symbol_name, *lazy_ptr_name;
15371 char *local_label_0;
ee890fe2
SS
15372 static int label = 0;
15373
df56a27f 15374 /* Lose our funky encoding stuff so it doesn't contaminate the stub. */
772c5265 15375 symb = (*targetm.strip_name_encoding) (symb);
df56a27f 15376
ee890fe2 15377
ee890fe2
SS
15378 length = strlen (symb);
15379 symbol_name = alloca (length + 32);
15380 GEN_SYMBOL_NAME_FOR_SYMBOL (symbol_name, symb, length);
15381
15382 lazy_ptr_name = alloca (length + 32);
15383 GEN_LAZY_PTR_NAME_FOR_SYMBOL (lazy_ptr_name, symb, length);
15384
ee890fe2 15385 if (flag_pic == 2)
d3c300d2 15386 machopic_picsymbol_stub1_section ();
ee890fe2 15387 else
d3c300d2
DJ
15388 machopic_symbol_stub1_section ();
15389 fprintf (file, "\t.align 2\n");
ee890fe2
SS
15390
15391 fprintf (file, "%s:\n", stub);
15392 fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
15393
15394 if (flag_pic == 2)
15395 {
876455fa
AP
15396 label++;
15397 local_label_0 = alloca (sizeof("\"L0000000000$spb\""));
15398 sprintf (local_label_0, "\"L%011d$spb\"", label);
15399
ee890fe2
SS
15400 fprintf (file, "\tmflr r0\n");
15401 fprintf (file, "\tbcl 20,31,%s\n", local_label_0);
15402 fprintf (file, "%s:\n\tmflr r11\n", local_label_0);
15403 fprintf (file, "\taddis r11,r11,ha16(%s-%s)\n",
15404 lazy_ptr_name, local_label_0);
15405 fprintf (file, "\tmtlr r0\n");
d3c300d2 15406 fprintf (file, "\tlwzu r12,lo16(%s-%s)(r11)\n",
ee890fe2
SS
15407 lazy_ptr_name, local_label_0);
15408 fprintf (file, "\tmtctr r12\n");
ee890fe2
SS
15409 fprintf (file, "\tbctr\n");
15410 }
15411 else
ab82a49f
AP
15412 {
15413 fprintf (file, "\tlis r11,ha16(%s)\n", lazy_ptr_name);
15414 fprintf (file, "\tlwzu r12,lo16(%s)(r11)\n", lazy_ptr_name);
15415 fprintf (file, "\tmtctr r12\n");
15416 fprintf (file, "\tbctr\n");
15417 }
ee890fe2
SS
15418
15419 machopic_lazy_symbol_ptr_section ();
15420 fprintf (file, "%s:\n", lazy_ptr_name);
15421 fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
15422 fprintf (file, "\t.long dyld_stub_binding_helper\n");
15423}
15424
15425/* Legitimize PIC addresses. If the address is already
15426 position-independent, we return ORIG. Newly generated
15427 position-independent addresses go into a reg. This is REG if non
15428 zero, otherwise we allocate register(s) as necessary. */
15429
c859cda6 15430#define SMALL_INT(X) ((unsigned) (INTVAL(X) + 0x8000) < 0x10000)
ee890fe2
SS
15431
15432rtx
a2369ed3
DJ
15433rs6000_machopic_legitimize_pic_address (rtx orig, enum machine_mode mode,
15434 rtx reg)
ee890fe2
SS
15435{
15436 rtx base, offset;
15437
15438 if (reg == NULL && ! reload_in_progress && ! reload_completed)
15439 reg = gen_reg_rtx (Pmode);
15440
15441 if (GET_CODE (orig) == CONST)
15442 {
15443 if (GET_CODE (XEXP (orig, 0)) == PLUS
15444 && XEXP (XEXP (orig, 0), 0) == pic_offset_table_rtx)
15445 return orig;
15446
15447 if (GET_CODE (XEXP (orig, 0)) == PLUS)
15448 {
2cf520bf 15449 /* Use a different reg for the intermediate value, as
a3c9585f 15450 it will be marked UNCHANGING. */
2cf520bf
GK
15451 rtx reg_temp = no_new_pseudos ? reg : gen_reg_rtx (Pmode);
15452
a4f6c312
SS
15453 base =
15454 rs6000_machopic_legitimize_pic_address (XEXP (XEXP (orig, 0), 0),
2cf520bf 15455 Pmode, reg_temp);
a4f6c312
SS
15456 offset =
15457 rs6000_machopic_legitimize_pic_address (XEXP (XEXP (orig, 0), 1),
15458 Pmode, reg);
ee890fe2
SS
15459 }
15460 else
15461 abort ();
15462
15463 if (GET_CODE (offset) == CONST_INT)
15464 {
15465 if (SMALL_INT (offset))
ed8908e7 15466 return plus_constant (base, INTVAL (offset));
ee890fe2
SS
15467 else if (! reload_in_progress && ! reload_completed)
15468 offset = force_reg (Pmode, offset);
15469 else
c859cda6
DJ
15470 {
15471 rtx mem = force_const_mem (Pmode, orig);
15472 return machopic_legitimize_pic_address (mem, Pmode, reg);
15473 }
ee890fe2 15474 }
f1c25d3b 15475 return gen_rtx_PLUS (Pmode, base, offset);
ee890fe2
SS
15476 }
15477
15478 /* Fall back on generic machopic code. */
15479 return machopic_legitimize_pic_address (orig, mode, reg);
15480}
15481
15482/* This is just a placeholder to make linking work without having to
15483 add this to the generic Darwin EXTRA_SECTIONS. If -mcall-aix is
15484 ever needed for Darwin (not too likely!) this would have to get a
15485 real definition. */
15486
15487void
863d938c 15488toc_section (void)
ee890fe2
SS
15489{
15490}
15491
15492#endif /* TARGET_MACHO */
7c262518
RH
15493
15494#if TARGET_ELF
15495static unsigned int
a2369ed3 15496rs6000_elf_section_type_flags (tree decl, const char *name, int reloc)
7c262518 15497{
1ff8f81a
AM
15498 return default_section_type_flags_1 (decl, name, reloc,
15499 flag_pic || DEFAULT_ABI == ABI_AIX);
7c262518 15500}
d9f6800d
RH
15501
15502/* Record an element in the table of global constructors. SYMBOL is
15503 a SYMBOL_REF of the function to be called; PRIORITY is a number
15504 between 0 and MAX_INIT_PRIORITY.
15505
15506 This differs from default_named_section_asm_out_constructor in
15507 that we have special handling for -mrelocatable. */
15508
15509static void
a2369ed3 15510rs6000_elf_asm_out_constructor (rtx symbol, int priority)
d9f6800d
RH
15511{
15512 const char *section = ".ctors";
15513 char buf[16];
15514
15515 if (priority != DEFAULT_INIT_PRIORITY)
15516 {
15517 sprintf (buf, ".ctors.%.5u",
15518 /* Invert the numbering so the linker puts us in the proper
15519 order; constructors are run from right to left, and the
15520 linker sorts in increasing order. */
15521 MAX_INIT_PRIORITY - priority);
15522 section = buf;
15523 }
15524
715bdd29
RH
15525 named_section_flags (section, SECTION_WRITE);
15526 assemble_align (POINTER_SIZE);
d9f6800d
RH
15527
15528 if (TARGET_RELOCATABLE)
15529 {
15530 fputs ("\t.long (", asm_out_file);
15531 output_addr_const (asm_out_file, symbol);
15532 fputs (")@fixup\n", asm_out_file);
15533 }
15534 else
c8af3574 15535 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
d9f6800d
RH
15536}
15537
15538static void
a2369ed3 15539rs6000_elf_asm_out_destructor (rtx symbol, int priority)
d9f6800d
RH
15540{
15541 const char *section = ".dtors";
15542 char buf[16];
15543
15544 if (priority != DEFAULT_INIT_PRIORITY)
15545 {
15546 sprintf (buf, ".dtors.%.5u",
15547 /* Invert the numbering so the linker puts us in the proper
15548 order; constructors are run from right to left, and the
15549 linker sorts in increasing order. */
15550 MAX_INIT_PRIORITY - priority);
15551 section = buf;
15552 }
15553
715bdd29
RH
15554 named_section_flags (section, SECTION_WRITE);
15555 assemble_align (POINTER_SIZE);
d9f6800d
RH
15556
15557 if (TARGET_RELOCATABLE)
15558 {
15559 fputs ("\t.long (", asm_out_file);
15560 output_addr_const (asm_out_file, symbol);
15561 fputs (")@fixup\n", asm_out_file);
15562 }
15563 else
c8af3574 15564 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
d9f6800d 15565}
9739c90c
JJ
15566
15567void
a2369ed3 15568rs6000_elf_declare_function_name (FILE *file, const char *name, tree decl)
9739c90c
JJ
15569{
15570 if (TARGET_64BIT)
15571 {
15572 fputs ("\t.section\t\".opd\",\"aw\"\n\t.align 3\n", file);
15573 ASM_OUTPUT_LABEL (file, name);
15574 fputs (DOUBLE_INT_ASM_OP, file);
15575 putc ('.', file);
15576 assemble_name (file, name);
15577 fputs (",.TOC.@tocbase,0\n\t.previous\n\t.size\t", file);
15578 assemble_name (file, name);
15579 fputs (",24\n\t.type\t.", file);
15580 assemble_name (file, name);
15581 fputs (",@function\n", file);
15582 if (TREE_PUBLIC (decl) && ! DECL_WEAK (decl))
15583 {
15584 fputs ("\t.globl\t.", file);
15585 assemble_name (file, name);
15586 putc ('\n', file);
15587 }
15588 ASM_DECLARE_RESULT (file, DECL_RESULT (decl));
15589 putc ('.', file);
15590 ASM_OUTPUT_LABEL (file, name);
15591 return;
15592 }
15593
15594 if (TARGET_RELOCATABLE
15595 && (get_pool_size () != 0 || current_function_profile)
3c9eb5f4 15596 && uses_TOC ())
9739c90c
JJ
15597 {
15598 char buf[256];
15599
15600 (*targetm.asm_out.internal_label) (file, "LCL", rs6000_pic_labelno);
15601
15602 ASM_GENERATE_INTERNAL_LABEL (buf, "LCTOC", 1);
15603 fprintf (file, "\t.long ");
15604 assemble_name (file, buf);
15605 putc ('-', file);
15606 ASM_GENERATE_INTERNAL_LABEL (buf, "LCF", rs6000_pic_labelno);
15607 assemble_name (file, buf);
15608 putc ('\n', file);
15609 }
15610
15611 ASM_OUTPUT_TYPE_DIRECTIVE (file, name, "function");
15612 ASM_DECLARE_RESULT (file, DECL_RESULT (decl));
15613
15614 if (DEFAULT_ABI == ABI_AIX)
15615 {
15616 const char *desc_name, *orig_name;
15617
15618 orig_name = (*targetm.strip_name_encoding) (name);
15619 desc_name = orig_name;
15620 while (*desc_name == '.')
15621 desc_name++;
15622
15623 if (TREE_PUBLIC (decl))
15624 fprintf (file, "\t.globl %s\n", desc_name);
15625
15626 fprintf (file, "%s\n", MINIMAL_TOC_SECTION_ASM_OP);
15627 fprintf (file, "%s:\n", desc_name);
15628 fprintf (file, "\t.long %s\n", orig_name);
15629 fputs ("\t.long _GLOBAL_OFFSET_TABLE_\n", file);
15630 if (DEFAULT_ABI == ABI_AIX)
15631 fputs ("\t.long 0\n", file);
15632 fprintf (file, "\t.previous\n");
15633 }
15634 ASM_OUTPUT_LABEL (file, name);
15635}
7c262518
RH
15636#endif
15637
cbaaba19 15638#if TARGET_XCOFF
7c262518 15639static void
a2369ed3 15640rs6000_xcoff_asm_globalize_label (FILE *stream, const char *name)
b275d088
DE
15641{
15642 fputs (GLOBAL_ASM_OP, stream);
15643 RS6000_OUTPUT_BASENAME (stream, name);
15644 putc ('\n', stream);
15645}
15646
15647static void
a2369ed3 15648rs6000_xcoff_asm_named_section (const char *name, unsigned int flags)
7c262518 15649{
0e5dbd9b
DE
15650 int smclass;
15651 static const char * const suffix[3] = { "PR", "RO", "RW" };
15652
15653 if (flags & SECTION_CODE)
15654 smclass = 0;
15655 else if (flags & SECTION_WRITE)
15656 smclass = 2;
15657 else
15658 smclass = 1;
15659
5b5198f7 15660 fprintf (asm_out_file, "\t.csect %s%s[%s],%u\n",
0e5dbd9b 15661 (flags & SECTION_CODE) ? "." : "",
5b5198f7 15662 name, suffix[smclass], flags & SECTION_ENTSIZE);
7c262518 15663}
ae46c4e0
RH
15664
15665static void
a2369ed3
DJ
15666rs6000_xcoff_select_section (tree decl, int reloc,
15667 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
ae46c4e0 15668{
5add3202 15669 if (decl_readonly_section_1 (decl, reloc, 1))
ae46c4e0 15670 {
0e5dbd9b 15671 if (TREE_PUBLIC (decl))
ae46c4e0
RH
15672 read_only_data_section ();
15673 else
15674 read_only_private_data_section ();
15675 }
15676 else
15677 {
0e5dbd9b 15678 if (TREE_PUBLIC (decl))
ae46c4e0
RH
15679 data_section ();
15680 else
15681 private_data_section ();
15682 }
15683}
15684
15685static void
a2369ed3 15686rs6000_xcoff_unique_section (tree decl, int reloc ATTRIBUTE_UNUSED)
ae46c4e0
RH
15687{
15688 const char *name;
ae46c4e0 15689
5b5198f7
DE
15690 /* Use select_section for private and uninitialized data. */
15691 if (!TREE_PUBLIC (decl)
15692 || DECL_COMMON (decl)
0e5dbd9b
DE
15693 || DECL_INITIAL (decl) == NULL_TREE
15694 || DECL_INITIAL (decl) == error_mark_node
15695 || (flag_zero_initialized_in_bss
15696 && initializer_zerop (DECL_INITIAL (decl))))
15697 return;
15698
15699 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
15700 name = (*targetm.strip_name_encoding) (name);
15701 DECL_SECTION_NAME (decl) = build_string (strlen (name), name);
ae46c4e0 15702}
b64a1b53 15703
fb49053f
RH
15704/* Select section for constant in constant pool.
15705
15706 On RS/6000, all constants are in the private read-only data area.
15707 However, if this is being placed in the TOC it must be output as a
15708 toc entry. */
15709
b64a1b53 15710static void
a2369ed3
DJ
15711rs6000_xcoff_select_rtx_section (enum machine_mode mode, rtx x,
15712 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
b64a1b53
RH
15713{
15714 if (ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (x, mode))
15715 toc_section ();
15716 else
15717 read_only_private_data_section ();
15718}
772c5265
RH
15719
15720/* Remove any trailing [DS] or the like from the symbol name. */
15721
15722static const char *
a2369ed3 15723rs6000_xcoff_strip_name_encoding (const char *name)
772c5265
RH
15724{
15725 size_t len;
15726 if (*name == '*')
15727 name++;
15728 len = strlen (name);
15729 if (name[len - 1] == ']')
15730 return ggc_alloc_string (name, len - 4);
15731 else
15732 return name;
15733}
15734
5add3202
DE
15735/* Section attributes. AIX is always PIC. */
15736
15737static unsigned int
a2369ed3 15738rs6000_xcoff_section_type_flags (tree decl, const char *name, int reloc)
5add3202 15739{
5b5198f7
DE
15740 unsigned int align;
15741 unsigned int flags = default_section_type_flags_1 (decl, name, reloc, 1);
15742
15743 /* Align to at least UNIT size. */
15744 if (flags & SECTION_CODE)
15745 align = MIN_UNITS_PER_WORD;
15746 else
15747 /* Increase alignment of large objects if not already stricter. */
15748 align = MAX ((DECL_ALIGN (decl) / BITS_PER_UNIT),
15749 int_size_in_bytes (TREE_TYPE (decl)) > MIN_UNITS_PER_WORD
15750 ? UNITS_PER_FP_WORD : MIN_UNITS_PER_WORD);
15751
15752 return flags | (exact_log2 (align) & SECTION_ENTSIZE);
5add3202 15753}
a5fe455b 15754
1bc7c5b6
ZW
15755/* Output at beginning of assembler file.
15756
15757 Initialize the section names for the RS/6000 at this point.
15758
15759 Specify filename, including full path, to assembler.
15760
15761 We want to go into the TOC section so at least one .toc will be emitted.
15762 Also, in order to output proper .bs/.es pairs, we need at least one static
15763 [RW] section emitted.
15764
15765 Finally, declare mcount when profiling to make the assembler happy. */
15766
15767static void
863d938c 15768rs6000_xcoff_file_start (void)
1bc7c5b6
ZW
15769{
15770 rs6000_gen_section_name (&xcoff_bss_section_name,
15771 main_input_filename, ".bss_");
15772 rs6000_gen_section_name (&xcoff_private_data_section_name,
15773 main_input_filename, ".rw_");
15774 rs6000_gen_section_name (&xcoff_read_only_section_name,
15775 main_input_filename, ".ro_");
15776
15777 fputs ("\t.file\t", asm_out_file);
15778 output_quoted_string (asm_out_file, main_input_filename);
15779 fputc ('\n', asm_out_file);
15780 toc_section ();
15781 if (write_symbols != NO_DEBUG)
15782 private_data_section ();
15783 text_section ();
15784 if (profile_flag)
15785 fprintf (asm_out_file, "\t.extern %s\n", RS6000_MCOUNT);
15786 rs6000_file_start ();
15787}
15788
a5fe455b
ZW
15789/* Output at end of assembler file.
15790 On the RS/6000, referencing data should automatically pull in text. */
15791
15792static void
863d938c 15793rs6000_xcoff_file_end (void)
a5fe455b
ZW
15794{
15795 text_section ();
15796 fputs ("_section_.text:\n", asm_out_file);
15797 data_section ();
15798 fputs (TARGET_32BIT
15799 ? "\t.long _section_.text\n" : "\t.llong _section_.text\n",
15800 asm_out_file);
15801}
f1384257 15802#endif /* TARGET_XCOFF */
0e5dbd9b 15803
f1384257
AM
15804#if TARGET_MACHO
15805/* Cross-module name binding. Darwin does not support overriding
7f3d8013 15806 functions at dynamic-link time. */
0e5dbd9b 15807
2bcc50d0 15808static bool
a2369ed3 15809rs6000_binds_local_p (tree decl)
0e5dbd9b 15810{
f1384257 15811 return default_binds_local_p_1 (decl, 0);
0e5dbd9b 15812}
f1384257 15813#endif
34bb030a 15814
3c50106f
RH
15815/* Compute a (partial) cost for rtx X. Return true if the complete
15816 cost has been computed, and false if subexpressions should be
15817 scanned. In either case, *TOTAL contains the cost result. */
15818
15819static bool
a2369ed3
DJ
15820rs6000_rtx_costs (rtx x, int code, int outer_code ATTRIBUTE_UNUSED,
15821 int *total)
3c50106f
RH
15822{
15823 switch (code)
15824 {
15825 /* On the RS/6000, if it is valid in the insn, it is free.
15826 So this always returns 0. */
15827 case CONST_INT:
15828 case CONST:
15829 case LABEL_REF:
15830 case SYMBOL_REF:
15831 case CONST_DOUBLE:
15832 case HIGH:
15833 *total = 0;
15834 return true;
15835
15836 case PLUS:
15837 *total = ((GET_CODE (XEXP (x, 1)) == CONST_INT
15838 && ((unsigned HOST_WIDE_INT) (INTVAL (XEXP (x, 1))
15839 + 0x8000) >= 0x10000)
15840 && ((INTVAL (XEXP (x, 1)) & 0xffff) != 0))
15841 ? COSTS_N_INSNS (2)
15842 : COSTS_N_INSNS (1));
15843 return true;
15844
15845 case AND:
15846 case IOR:
15847 case XOR:
15848 *total = ((GET_CODE (XEXP (x, 1)) == CONST_INT
15849 && (INTVAL (XEXP (x, 1)) & (~ (HOST_WIDE_INT) 0xffff)) != 0
15850 && ((INTVAL (XEXP (x, 1)) & 0xffff) != 0))
15851 ? COSTS_N_INSNS (2)
15852 : COSTS_N_INSNS (1));
15853 return true;
15854
15855 case MULT:
15856 if (optimize_size)
15857 {
15858 *total = COSTS_N_INSNS (2);
15859 return true;
15860 }
15861 switch (rs6000_cpu)
15862 {
15863 case PROCESSOR_RIOS1:
15864 case PROCESSOR_PPC405:
15865 *total = (GET_CODE (XEXP (x, 1)) != CONST_INT
15866 ? COSTS_N_INSNS (5)
15867 : (INTVAL (XEXP (x, 1)) >= -256
15868 && INTVAL (XEXP (x, 1)) <= 255)
15869 ? COSTS_N_INSNS (3) : COSTS_N_INSNS (4));
15870 return true;
15871
02ca7595
DE
15872 case PROCESSOR_PPC440:
15873 *total = (GET_CODE (XEXP (x, 1)) != CONST_INT
15874 ? COSTS_N_INSNS (3)
15875 : COSTS_N_INSNS (2));
15876 return true;
15877
3c50106f
RH
15878 case PROCESSOR_RS64A:
15879 *total = (GET_CODE (XEXP (x, 1)) != CONST_INT
15880 ? GET_MODE (XEXP (x, 1)) != DImode
15881 ? COSTS_N_INSNS (20) : COSTS_N_INSNS (34)
15882 : (INTVAL (XEXP (x, 1)) >= -256
15883 && INTVAL (XEXP (x, 1)) <= 255)
15884 ? COSTS_N_INSNS (8) : COSTS_N_INSNS (12));
15885 return true;
15886
15887 case PROCESSOR_RIOS2:
15888 case PROCESSOR_MPCCORE:
15889 case PROCESSOR_PPC604e:
15890 *total = COSTS_N_INSNS (2);
15891 return true;
15892
15893 case PROCESSOR_PPC601:
15894 *total = COSTS_N_INSNS (5);
15895 return true;
15896
15897 case PROCESSOR_PPC603:
15898 case PROCESSOR_PPC7400:
15899 case PROCESSOR_PPC750:
15900 *total = (GET_CODE (XEXP (x, 1)) != CONST_INT
15901 ? COSTS_N_INSNS (5)
15902 : (INTVAL (XEXP (x, 1)) >= -256
15903 && INTVAL (XEXP (x, 1)) <= 255)
15904 ? COSTS_N_INSNS (2) : COSTS_N_INSNS (3));
15905 return true;
15906
15907 case PROCESSOR_PPC7450:
15908 *total = (GET_CODE (XEXP (x, 1)) != CONST_INT
15909 ? COSTS_N_INSNS (4)
15910 : COSTS_N_INSNS (3));
15911 return true;
15912
15913 case PROCESSOR_PPC403:
15914 case PROCESSOR_PPC604:
15915 case PROCESSOR_PPC8540:
15916 *total = COSTS_N_INSNS (4);
15917 return true;
15918
15919 case PROCESSOR_PPC620:
15920 case PROCESSOR_PPC630:
3c50106f
RH
15921 *total = (GET_CODE (XEXP (x, 1)) != CONST_INT
15922 ? GET_MODE (XEXP (x, 1)) != DImode
15923 ? COSTS_N_INSNS (5) : COSTS_N_INSNS (7)
15924 : (INTVAL (XEXP (x, 1)) >= -256
15925 && INTVAL (XEXP (x, 1)) <= 255)
15926 ? COSTS_N_INSNS (3) : COSTS_N_INSNS (4));
15927 return true;
15928
9259f3b0 15929 case PROCESSOR_POWER4:
ec507f2d 15930 case PROCESSOR_POWER5:
9259f3b0
DE
15931 *total = (GET_CODE (XEXP (x, 1)) != CONST_INT
15932 ? GET_MODE (XEXP (x, 1)) != DImode
984e25ac
DE
15933 ? COSTS_N_INSNS (3) : COSTS_N_INSNS (4)
15934 : COSTS_N_INSNS (2));
9259f3b0
DE
15935 return true;
15936
3c50106f
RH
15937 default:
15938 abort ();
15939 }
15940
15941 case DIV:
15942 case MOD:
15943 if (GET_CODE (XEXP (x, 1)) == CONST_INT
15944 && exact_log2 (INTVAL (XEXP (x, 1))) >= 0)
15945 {
15946 *total = COSTS_N_INSNS (2);
15947 return true;
15948 }
5efb1046 15949 /* FALLTHRU */
3c50106f
RH
15950
15951 case UDIV:
15952 case UMOD:
15953 switch (rs6000_cpu)
15954 {
15955 case PROCESSOR_RIOS1:
15956 *total = COSTS_N_INSNS (19);
15957 return true;
15958
15959 case PROCESSOR_RIOS2:
15960 *total = COSTS_N_INSNS (13);
15961 return true;
15962
15963 case PROCESSOR_RS64A:
15964 *total = (GET_MODE (XEXP (x, 1)) != DImode
15965 ? COSTS_N_INSNS (65)
15966 : COSTS_N_INSNS (67));
15967 return true;
15968
15969 case PROCESSOR_MPCCORE:
15970 *total = COSTS_N_INSNS (6);
15971 return true;
15972
15973 case PROCESSOR_PPC403:
15974 *total = COSTS_N_INSNS (33);
15975 return true;
15976
15977 case PROCESSOR_PPC405:
15978 *total = COSTS_N_INSNS (35);
15979 return true;
15980
02ca7595
DE
15981 case PROCESSOR_PPC440:
15982 *total = COSTS_N_INSNS (34);
15983 return true;
15984
3c50106f
RH
15985 case PROCESSOR_PPC601:
15986 *total = COSTS_N_INSNS (36);
15987 return true;
15988
15989 case PROCESSOR_PPC603:
15990 *total = COSTS_N_INSNS (37);
15991 return true;
15992
15993 case PROCESSOR_PPC604:
15994 case PROCESSOR_PPC604e:
15995 *total = COSTS_N_INSNS (20);
15996 return true;
15997
15998 case PROCESSOR_PPC620:
15999 case PROCESSOR_PPC630:
3c50106f
RH
16000 *total = (GET_MODE (XEXP (x, 1)) != DImode
16001 ? COSTS_N_INSNS (21)
16002 : COSTS_N_INSNS (37));
16003 return true;
16004
16005 case PROCESSOR_PPC750:
16006 case PROCESSOR_PPC8540:
16007 case PROCESSOR_PPC7400:
16008 *total = COSTS_N_INSNS (19);
16009 return true;
16010
16011 case PROCESSOR_PPC7450:
16012 *total = COSTS_N_INSNS (23);
16013 return true;
984e25ac
DE
16014
16015 case PROCESSOR_POWER4:
ec507f2d 16016 case PROCESSOR_POWER5:
984e25ac
DE
16017 *total = (GET_MODE (XEXP (x, 1)) != DImode
16018 ? COSTS_N_INSNS (18)
16019 : COSTS_N_INSNS (34));
16020 return true;
3c50106f
RH
16021
16022 default:
16023 abort ();
16024 }
16025
16026 case FFS:
16027 *total = COSTS_N_INSNS (4);
16028 return true;
16029
16030 case MEM:
a3c9585f 16031 /* MEM should be slightly more expensive than (plus (reg) (const)). */
3c50106f
RH
16032 *total = 5;
16033 return true;
16034
16035 default:
16036 return false;
16037 }
16038}
16039
34bb030a
DE
16040/* A C expression returning the cost of moving data from a register of class
16041 CLASS1 to one of CLASS2. */
16042
16043int
a2369ed3
DJ
16044rs6000_register_move_cost (enum machine_mode mode,
16045 enum reg_class from, enum reg_class to)
34bb030a
DE
16046{
16047 /* Moves from/to GENERAL_REGS. */
16048 if (reg_classes_intersect_p (to, GENERAL_REGS)
16049 || reg_classes_intersect_p (from, GENERAL_REGS))
16050 {
16051 if (! reg_classes_intersect_p (to, GENERAL_REGS))
16052 from = to;
16053
16054 if (from == FLOAT_REGS || from == ALTIVEC_REGS)
16055 return (rs6000_memory_move_cost (mode, from, 0)
16056 + rs6000_memory_move_cost (mode, GENERAL_REGS, 0));
16057
a3c9585f 16058/* It's more expensive to move CR_REGS than CR0_REGS because of the shift.... */
34bb030a
DE
16059 else if (from == CR_REGS)
16060 return 4;
16061
16062 else
16063/* A move will cost one instruction per GPR moved. */
16064 return 2 * HARD_REGNO_NREGS (0, mode);
16065 }
16066
16067/* Moving between two similar registers is just one instruction. */
16068 else if (reg_classes_intersect_p (to, from))
16069 return mode == TFmode ? 4 : 2;
16070
16071/* Everything else has to go through GENERAL_REGS. */
16072 else
16073 return (rs6000_register_move_cost (mode, GENERAL_REGS, to)
16074 + rs6000_register_move_cost (mode, from, GENERAL_REGS));
16075}
16076
16077/* A C expressions returning the cost of moving data of MODE from a register to
16078 or from memory. */
16079
16080int
a2369ed3
DJ
16081rs6000_memory_move_cost (enum machine_mode mode, enum reg_class class,
16082 int in ATTRIBUTE_UNUSED)
34bb030a
DE
16083{
16084 if (reg_classes_intersect_p (class, GENERAL_REGS))
16085 return 4 * HARD_REGNO_NREGS (0, mode);
16086 else if (reg_classes_intersect_p (class, FLOAT_REGS))
16087 return 4 * HARD_REGNO_NREGS (32, mode);
16088 else if (reg_classes_intersect_p (class, ALTIVEC_REGS))
16089 return 4 * HARD_REGNO_NREGS (FIRST_ALTIVEC_REGNO, mode);
16090 else
16091 return 4 + rs6000_register_move_cost (mode, class, GENERAL_REGS);
16092}
16093
ded9bf77
AH
16094/* Return an RTX representing where to find the function value of a
16095 function returning MODE. */
16096static rtx
16097rs6000_complex_function_value (enum machine_mode mode)
16098{
16099 unsigned int regno;
16100 rtx r1, r2;
16101 enum machine_mode inner = GET_MODE_INNER (mode);
fb7e4164 16102 unsigned int inner_bytes = GET_MODE_SIZE (inner);
ded9bf77
AH
16103
16104 if (FLOAT_MODE_P (mode))
16105 regno = FP_ARG_RETURN;
16106 else
16107 {
16108 regno = GP_ARG_RETURN;
16109
16110 /* 32-bit is OK since it'll go in r3/r4. */
fb7e4164 16111 if (TARGET_32BIT && inner_bytes >= 4)
ded9bf77
AH
16112 return gen_rtx_REG (mode, regno);
16113 }
16114
fb7e4164
AM
16115 if (inner_bytes >= 8)
16116 return gen_rtx_REG (mode, regno);
16117
ded9bf77
AH
16118 r1 = gen_rtx_EXPR_LIST (inner, gen_rtx_REG (inner, regno),
16119 const0_rtx);
16120 r2 = gen_rtx_EXPR_LIST (inner, gen_rtx_REG (inner, regno + 1),
fb7e4164 16121 GEN_INT (inner_bytes));
ded9bf77
AH
16122 return gen_rtx_PARALLEL (mode, gen_rtvec (2, r1, r2));
16123}
16124
a6ebc39a
AH
16125/* Define how to find the value returned by a function.
16126 VALTYPE is the data type of the value (as a tree).
16127 If the precise function being called is known, FUNC is its FUNCTION_DECL;
16128 otherwise, FUNC is 0.
16129
16130 On the SPE, both FPs and vectors are returned in r3.
16131
16132 On RS/6000 an integer value is in r3 and a floating-point value is in
16133 fp1, unless -msoft-float. */
16134
16135rtx
16136rs6000_function_value (tree valtype, tree func ATTRIBUTE_UNUSED)
16137{
16138 enum machine_mode mode;
2a8fa26c 16139 unsigned int regno;
a6ebc39a 16140
0e67400a
FJ
16141 if (TARGET_32BIT && TARGET_POWERPC64 && TYPE_MODE (valtype) == DImode)
16142 {
16143 /* Long long return value need be split in -mpowerpc64, 32bit ABI. */
16144 return gen_rtx_PARALLEL (DImode,
16145 gen_rtvec (2,
16146 gen_rtx_EXPR_LIST (VOIDmode,
16147 gen_rtx_REG (SImode, GP_ARG_RETURN),
16148 const0_rtx),
16149 gen_rtx_EXPR_LIST (VOIDmode,
16150 gen_rtx_REG (SImode,
16151 GP_ARG_RETURN + 1),
16152 GEN_INT (4))));
16153 }
16154
a6ebc39a
AH
16155 if ((INTEGRAL_TYPE_P (valtype)
16156 && TYPE_PRECISION (valtype) < BITS_PER_WORD)
16157 || POINTER_TYPE_P (valtype))
b78d48dd 16158 mode = TARGET_32BIT ? SImode : DImode;
a6ebc39a
AH
16159 else
16160 mode = TYPE_MODE (valtype);
16161
2a8fa26c
DE
16162 if (TREE_CODE (valtype) == REAL_TYPE && TARGET_HARD_FLOAT && TARGET_FPRS)
16163 regno = FP_ARG_RETURN;
ded9bf77
AH
16164 else if (TREE_CODE (valtype) == COMPLEX_TYPE
16165 && TARGET_HARD_FLOAT
42ba5130 16166 && targetm.calls.split_complex_arg)
ded9bf77 16167 return rs6000_complex_function_value (mode);
2a8fa26c 16168 else if (TREE_CODE (valtype) == VECTOR_TYPE && TARGET_ALTIVEC)
a6ebc39a
AH
16169 regno = ALTIVEC_ARG_RETURN;
16170 else
16171 regno = GP_ARG_RETURN;
16172
16173 return gen_rtx_REG (mode, regno);
16174}
16175
ded9bf77
AH
16176/* Define how to find the value returned by a library function
16177 assuming the value has mode MODE. */
16178rtx
16179rs6000_libcall_value (enum machine_mode mode)
16180{
16181 unsigned int regno;
16182
16183 if (GET_MODE_CLASS (mode) == MODE_FLOAT
16184 && TARGET_HARD_FLOAT && TARGET_FPRS)
16185 regno = FP_ARG_RETURN;
16186 else if (ALTIVEC_VECTOR_MODE (mode))
16187 regno = ALTIVEC_ARG_RETURN;
42ba5130 16188 else if (COMPLEX_MODE_P (mode) && targetm.calls.split_complex_arg)
ded9bf77
AH
16189 return rs6000_complex_function_value (mode);
16190 else
16191 regno = GP_ARG_RETURN;
16192
16193 return gen_rtx_REG (mode, regno);
16194}
16195
d1d0c603
JJ
16196/* Define the offset between two registers, FROM to be eliminated and its
16197 replacement TO, at the start of a routine. */
16198HOST_WIDE_INT
16199rs6000_initial_elimination_offset (int from, int to)
16200{
16201 rs6000_stack_t *info = rs6000_stack_info ();
16202 HOST_WIDE_INT offset;
16203
16204 if (from == FRAME_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
16205 offset = info->push_p ? 0 : -info->total_size;
16206 else if (from == ARG_POINTER_REGNUM && to == FRAME_POINTER_REGNUM)
16207 offset = info->total_size;
16208 else if (from == ARG_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
16209 offset = info->push_p ? info->total_size : 0;
16210 else if (from == RS6000_PIC_OFFSET_TABLE_REGNUM)
16211 offset = 0;
16212 else
16213 abort ();
16214
16215 return offset;
16216}
16217
62e1dfcf
NC
16218/* Return true if TYPE is of type __ev64_opaque__. */
16219
c8e4f0e9 16220static bool
a2369ed3 16221is_ev64_opaque_type (tree type)
62e1dfcf 16222{
c8e4f0e9 16223 return (TARGET_SPE
2abe3e28
AH
16224 && (type == opaque_V2SI_type_node
16225 || type == opaque_V2SF_type_node
36252949 16226 || type == opaque_p_V2SI_type_node));
62e1dfcf
NC
16227}
16228
96714395 16229static rtx
a2369ed3 16230rs6000_dwarf_register_span (rtx reg)
96714395
AH
16231{
16232 unsigned regno;
16233
16234 if (!TARGET_SPE || !SPE_VECTOR_MODE (GET_MODE (reg)))
16235 return NULL_RTX;
16236
16237 regno = REGNO (reg);
16238
16239 /* The duality of the SPE register size wreaks all kinds of havoc.
16240 This is a way of distinguishing r0 in 32-bits from r0 in
16241 64-bits. */
16242 return
16243 gen_rtx_PARALLEL (VOIDmode,
3bd104d1
AH
16244 BYTES_BIG_ENDIAN
16245 ? gen_rtvec (2,
16246 gen_rtx_REG (SImode, regno + 1200),
16247 gen_rtx_REG (SImode, regno))
16248 : gen_rtvec (2,
16249 gen_rtx_REG (SImode, regno),
16250 gen_rtx_REG (SImode, regno + 1200)));
96714395
AH
16251}
16252
93c9d1ba
AM
16253/* Map internal gcc register numbers to DWARF2 register numbers. */
16254
16255unsigned int
16256rs6000_dbx_register_number (unsigned int regno)
16257{
16258 if (regno <= 63 || write_symbols != DWARF2_DEBUG)
16259 return regno;
16260 if (regno == MQ_REGNO)
16261 return 100;
16262 if (regno == LINK_REGISTER_REGNUM)
16263 return 108;
16264 if (regno == COUNT_REGISTER_REGNUM)
16265 return 109;
16266 if (CR_REGNO_P (regno))
16267 return regno - CR0_REGNO + 86;
16268 if (regno == XER_REGNO)
16269 return 101;
16270 if (ALTIVEC_REGNO_P (regno))
16271 return regno - FIRST_ALTIVEC_REGNO + 1124;
16272 if (regno == VRSAVE_REGNO)
16273 return 356;
16274 if (regno == VSCR_REGNO)
16275 return 67;
16276 if (regno == SPE_ACC_REGNO)
16277 return 99;
16278 if (regno == SPEFSCR_REGNO)
16279 return 612;
16280 /* SPE high reg number. We get these values of regno from
16281 rs6000_dwarf_register_span. */
16282 if (regno >= 1200 && regno < 1232)
16283 return regno;
16284
16285 abort ();
16286}
16287
17211ab5 16288#include "gt-rs6000.h"