]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/config/frv/frv.c
frv.c: Include rtl-iter.h.
[thirdparty/gcc.git] / gcc / config / frv / frv.c
CommitLineData
23a5b65a 1/* Copyright (C) 1997-2014 Free Software Foundation, Inc.
36a05131
BS
2 Contributed by Red Hat, Inc.
3
7ec022b2 4This file is part of GCC.
36a05131 5
7ec022b2 6GCC is free software; you can redistribute it and/or modify
36a05131 7it under the terms of the GNU General Public License as published by
2f83c7d6 8the Free Software Foundation; either version 3, or (at your option)
36a05131
BS
9any later version.
10
7ec022b2 11GCC is distributed in the hope that it will be useful,
36a05131
BS
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
2f83c7d6
NC
17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
36a05131
BS
19
20#include "config.h"
21#include "system.h"
4977bab6
ZW
22#include "coretypes.h"
23#include "tm.h"
36a05131 24#include "tree.h"
d8a2d370
DN
25#include "varasm.h"
26#include "stor-layout.h"
27#include "stringpool.h"
36a05131
BS
28#include "regs.h"
29#include "hard-reg-set.h"
7e8e4cf9 30#include "rtl.h"
36a05131
BS
31#include "insn-config.h"
32#include "conditions.h"
33#include "insn-flags.h"
34#include "output.h"
35#include "insn-attr.h"
36#include "flags.h"
37#include "recog.h"
38#include "reload.h"
39#include "expr.h"
40#include "obstack.h"
41#include "except.h"
83685514
AM
42#include "hashtab.h"
43#include "hash-set.h"
44#include "vec.h"
45#include "machmode.h"
46#include "input.h"
36a05131 47#include "function.h"
b0710fe1 48#include "insn-codes.h"
36a05131 49#include "optabs.h"
718f9c0f 50#include "diagnostic-core.h"
60393bbc
AM
51#include "predict.h"
52#include "dominance.h"
53#include "cfg.h"
54#include "cfgrtl.h"
55#include "cfganal.h"
56#include "lcm.h"
57#include "cfgbuild.h"
58#include "cfgcleanup.h"
36a05131
BS
59#include "basic-block.h"
60#include "tm_p.h"
61#include "ggc.h"
36a05131
BS
62#include "target.h"
63#include "target-def.h"
8cd5a4e0 64#include "targhooks.h"
6e34d3a3 65#include "langhooks.h"
6fb5fa3c 66#include "df.h"
7ee2468b 67#include "dumpfile.h"
9b2b7279 68#include "builtins.h"
893479de 69#include "ifcvt.h"
cc665e56 70#include "rtl-iter.h"
36a05131
BS
71
72#ifndef FRV_INLINE
73#define FRV_INLINE inline
74#endif
75
c557edf4
RS
76/* The maximum number of distinct NOP patterns. There are three:
77 nop, fnop and mnop. */
78#define NUM_NOP_PATTERNS 3
79
80/* Classification of instructions and units: integer, floating-point/media,
81 branch and control. */
82enum frv_insn_group { GROUP_I, GROUP_FM, GROUP_B, GROUP_C, NUM_GROUPS };
83
84/* The DFA names of the units, in packet order. */
85static const char *const frv_unit_names[] =
86{
87 "c",
88 "i0", "f0",
89 "i1", "f1",
90 "i2", "f2",
91 "i3", "f3",
92 "b0", "b1"
93};
94
95/* The classification of each unit in frv_unit_names[]. */
96static const enum frv_insn_group frv_unit_groups[ARRAY_SIZE (frv_unit_names)] =
97{
98 GROUP_C,
99 GROUP_I, GROUP_FM,
100 GROUP_I, GROUP_FM,
101 GROUP_I, GROUP_FM,
102 GROUP_I, GROUP_FM,
103 GROUP_B, GROUP_B
104};
105
106/* Return the DFA unit code associated with the Nth unit of integer
107 or floating-point group GROUP, */
108#define NTH_UNIT(GROUP, N) frv_unit_codes[(GROUP) + (N) * 2 + 1]
109
110/* Return the number of integer or floating-point unit UNIT
111 (1 for I1, 2 for F2, etc.). */
112#define UNIT_NUMBER(UNIT) (((UNIT) - 1) / 2)
113
114/* The DFA unit number for each unit in frv_unit_names[]. */
115static int frv_unit_codes[ARRAY_SIZE (frv_unit_names)];
116
117/* FRV_TYPE_TO_UNIT[T] is the last unit in frv_unit_names[] that can issue
118 an instruction of type T. The value is ARRAY_SIZE (frv_unit_names) if
119 no instruction of type T has been seen. */
120static unsigned int frv_type_to_unit[TYPE_UNKNOWN + 1];
121
122/* An array of dummy nop INSNs, one for each type of nop that the
123 target supports. */
e6eda746 124static GTY(()) rtx_insn *frv_nops[NUM_NOP_PATTERNS];
c557edf4
RS
125
126/* The number of nop instructions in frv_nops[]. */
127static unsigned int frv_num_nops;
128
5c5e8419
JR
129 /* The type of access. FRV_IO_UNKNOWN means the access can be either
130 a read or a write. */
131enum frv_io_type { FRV_IO_UNKNOWN, FRV_IO_READ, FRV_IO_WRITE };
132
38c28a25
AH
133/* Information about one __builtin_read or __builtin_write access, or
134 the combination of several such accesses. The most general value
135 is all-zeros (an unknown access to an unknown address). */
136struct frv_io {
5c5e8419 137 enum frv_io_type type;
38c28a25
AH
138
139 /* The constant address being accessed, or zero if not known. */
140 HOST_WIDE_INT const_address;
141
142 /* The run-time address, as used in operand 0 of the membar pattern. */
143 rtx var_address;
144};
145
c557edf4
RS
146/* Return true if instruction INSN should be packed with the following
147 instruction. */
148#define PACKING_FLAG_P(INSN) (GET_MODE (INSN) == TImode)
149
150/* Set the value of PACKING_FLAG_P(INSN). */
151#define SET_PACKING_FLAG(INSN) PUT_MODE (INSN, TImode)
152#define CLEAR_PACKING_FLAG(INSN) PUT_MODE (INSN, VOIDmode)
153
154/* Loop with REG set to each hard register in rtx X. */
155#define FOR_EACH_REGNO(REG, X) \
156 for (REG = REGNO (X); \
157 REG < REGNO (X) + HARD_REGNO_NREGS (REGNO (X), GET_MODE (X)); \
158 REG++)
159
38c28a25 160/* This structure contains machine specific function data. */
d1b38208 161struct GTY(()) machine_function
38c28a25
AH
162{
163 /* True if we have created an rtx that relies on the stack frame. */
164 int frame_needed;
165
166 /* True if this function contains at least one __builtin_{read,write}*. */
167 bool has_membar_p;
168};
169
36a05131
BS
170/* Temporary register allocation support structure. */
171typedef struct frv_tmp_reg_struct
172 {
173 HARD_REG_SET regs; /* possible registers to allocate */
174 int next_reg[N_REG_CLASSES]; /* next register to allocate per class */
175 }
176frv_tmp_reg_t;
177
c557edf4 178/* Register state information for VLIW re-packing phase. */
36a05131 179#define REGSTATE_CC_MASK 0x07 /* Mask to isolate CCn for cond exec */
c557edf4
RS
180#define REGSTATE_MODIFIED 0x08 /* reg modified in current VLIW insn */
181#define REGSTATE_IF_TRUE 0x10 /* reg modified in cond exec true */
182#define REGSTATE_IF_FALSE 0x20 /* reg modified in cond exec false */
183
36a05131
BS
184#define REGSTATE_IF_EITHER (REGSTATE_IF_TRUE | REGSTATE_IF_FALSE)
185
c557edf4 186typedef unsigned char regstate_t;
36a05131
BS
187
188/* Used in frv_frame_accessor_t to indicate the direction of a register-to-
189 memory move. */
190enum frv_stack_op
191{
192 FRV_LOAD,
193 FRV_STORE
194};
195
196/* Information required by frv_frame_access. */
197typedef struct
198{
199 /* This field is FRV_LOAD if registers are to be loaded from the stack and
200 FRV_STORE if they should be stored onto the stack. FRV_STORE implies
201 the move is being done by the prologue code while FRV_LOAD implies it
202 is being done by the epilogue. */
203 enum frv_stack_op op;
204
205 /* The base register to use when accessing the stack. This may be the
206 frame pointer, stack pointer, or a temporary. The choice of register
207 depends on which part of the frame is being accessed and how big the
208 frame is. */
209 rtx base;
210
211 /* The offset of BASE from the bottom of the current frame, in bytes. */
212 int base_offset;
213} frv_frame_accessor_t;
214
87b483a1 215/* Conditional execution support gathered together in one structure. */
36a05131
BS
216typedef struct
217 {
218 /* Linked list of insns to add if the conditional execution conversion was
219 successful. Each link points to an EXPR_LIST which points to the pattern
220 of the insn to add, and the insn to be inserted before. */
221 rtx added_insns_list;
222
223 /* Identify which registers are safe to allocate for if conversions to
224 conditional execution. We keep the last allocated register in the
225 register classes between COND_EXEC statements. This will mean we allocate
226 different registers for each different COND_EXEC group if we can. This
227 might allow the scheduler to intermix two different COND_EXEC sections. */
228 frv_tmp_reg_t tmp_reg;
229
230 /* For nested IFs, identify which CC registers are used outside of setting
231 via a compare isnsn, and using via a check insn. This will allow us to
232 know if we can rewrite the register to use a different register that will
233 be paired with the CR register controlling the nested IF-THEN blocks. */
234 HARD_REG_SET nested_cc_ok_rewrite;
235
236 /* Temporary registers allocated to hold constants during conditional
237 execution. */
238 rtx scratch_regs[FIRST_PSEUDO_REGISTER];
239
240 /* Current number of temp registers available. */
241 int cur_scratch_regs;
242
87b483a1 243 /* Number of nested conditional execution blocks. */
36a05131
BS
244 int num_nested_cond_exec;
245
246 /* Map of insns that set up constants in scratch registers. */
247 bitmap scratch_insns_bitmap;
248
87b483a1 249 /* Conditional execution test register (CC0..CC7). */
36a05131
BS
250 rtx cr_reg;
251
252 /* Conditional execution compare register that is paired with cr_reg, so that
253 nested compares can be done. The csubcc and caddcc instructions don't
254 have enough bits to specify both a CC register to be set and a CR register
255 to do the test on, so the same bit number is used for both. Needless to
839a4992 256 say, this is rather inconvenient for GCC. */
36a05131
BS
257 rtx nested_cc_reg;
258
259 /* Extra CR registers used for &&, ||. */
260 rtx extra_int_cr;
261 rtx extra_fp_cr;
262
263 /* Previous CR used in nested if, to make sure we are dealing with the same
87b483a1 264 nested if as the previous statement. */
36a05131
BS
265 rtx last_nested_if_cr;
266 }
267frv_ifcvt_t;
268
269static /* GTY(()) */ frv_ifcvt_t frv_ifcvt;
270
271/* Map register number to smallest register class. */
272enum reg_class regno_reg_class[FIRST_PSEUDO_REGISTER];
273
87b483a1 274/* Cached value of frv_stack_info. */
36a05131
BS
275static frv_stack_t *frv_stack_cache = (frv_stack_t *)0;
276
36a05131 277/* Forward references */
0b2c18fe 278
c5387660 279static void frv_option_override (void);
ef4bddc2 280static bool frv_legitimate_address_p (machine_mode, rtx, bool);
f2206911 281static int frv_default_flags_for_cpu (void);
f961457f 282static int frv_string_begins_with (const char *, const char *);
34208acf 283static FRV_INLINE bool frv_small_data_reloc_p (rtx, int);
0fb30cb7
NF
284static void frv_print_operand (FILE *, rtx, int);
285static void frv_print_operand_address (FILE *, rtx);
286static bool frv_print_operand_punct_valid_p (unsigned char code);
36a05131 287static void frv_print_operand_memory_reference_reg
f2206911
KC
288 (FILE *, rtx);
289static void frv_print_operand_memory_reference (FILE *, rtx, int);
68a1a6c0 290static int frv_print_operand_jump_hint (rtx_insn *);
036ff63f 291static const char *comparison_string (enum rtx_code, rtx);
219d92a4
AS
292static rtx frv_function_value (const_tree, const_tree,
293 bool);
ef4bddc2 294static rtx frv_libcall_value (machine_mode,
219d92a4 295 const_rtx);
f2206911
KC
296static FRV_INLINE int frv_regno_ok_for_base_p (int, int);
297static rtx single_set_pattern (rtx);
298static int frv_function_contains_far_jump (void);
299static rtx frv_alloc_temp_reg (frv_tmp_reg_t *,
300 enum reg_class,
ef4bddc2 301 machine_mode,
f2206911
KC
302 int, int);
303static rtx frv_frame_offset_rtx (int);
ef4bddc2 304static rtx frv_frame_mem (machine_mode, rtx, int);
f2206911
KC
305static rtx frv_dwarf_store (rtx, int);
306static void frv_frame_insn (rtx, rtx);
307static void frv_frame_access (frv_frame_accessor_t*,
308 rtx, int);
309static void frv_frame_access_multi (frv_frame_accessor_t*,
310 frv_stack_t *, int);
311static void frv_frame_access_standard_regs (enum frv_stack_op,
312 frv_stack_t *);
313static struct machine_function *frv_init_machine_status (void);
f2206911 314static rtx frv_int_to_acc (enum insn_code, int, rtx);
ef4bddc2 315static machine_mode frv_matching_accg_mode (machine_mode);
2396bce1 316static rtx frv_read_argument (tree, unsigned int);
ef4bddc2 317static rtx frv_read_iacc_argument (machine_mode, tree, unsigned int);
f2206911
KC
318static int frv_check_constant_argument (enum insn_code, int, rtx);
319static rtx frv_legitimize_target (enum insn_code, rtx);
320static rtx frv_legitimize_argument (enum insn_code, int, rtx);
bef8809e 321static rtx frv_legitimize_tls_address (rtx, enum tls_model);
ef4bddc2 322static rtx frv_legitimize_address (rtx, rtx, machine_mode);
f2206911
KC
323static rtx frv_expand_set_builtin (enum insn_code, tree, rtx);
324static rtx frv_expand_unop_builtin (enum insn_code, tree, rtx);
325static rtx frv_expand_binop_builtin (enum insn_code, tree, rtx);
326static rtx frv_expand_cut_builtin (enum insn_code, tree, rtx);
327static rtx frv_expand_binopimm_builtin (enum insn_code, tree, rtx);
328static rtx frv_expand_voidbinop_builtin (enum insn_code, tree);
c557edf4
RS
329static rtx frv_expand_int_void2arg (enum insn_code, tree);
330static rtx frv_expand_prefetches (enum insn_code, tree);
f2206911
KC
331static rtx frv_expand_voidtriop_builtin (enum insn_code, tree);
332static rtx frv_expand_voidaccop_builtin (enum insn_code, tree);
333static rtx frv_expand_mclracc_builtin (tree);
334static rtx frv_expand_mrdacc_builtin (enum insn_code, tree);
335static rtx frv_expand_mwtacc_builtin (enum insn_code, tree);
336static rtx frv_expand_noargs_builtin (enum insn_code);
c557edf4 337static void frv_split_iacc_move (rtx, rtx);
f2206911 338static rtx frv_emit_comparison (enum rtx_code, rtx, rtx);
f2206911 339static void frv_ifcvt_add_insn (rtx, rtx, int);
ef4bddc2 340static rtx frv_ifcvt_rewrite_mem (rtx, machine_mode, rtx);
f2206911 341static rtx frv_ifcvt_load_value (rtx, rtx);
84034c69
DM
342static unsigned int frv_insn_unit (rtx_insn *);
343static bool frv_issues_to_branch_unit_p (rtx_insn *);
c557edf4
RS
344static int frv_cond_flags (rtx);
345static bool frv_regstate_conflict_p (regstate_t, regstate_t);
346static int frv_registers_conflict_p_1 (rtx *, void *);
347static bool frv_registers_conflict_p (rtx);
7bc980e1 348static void frv_registers_update_1 (rtx, const_rtx, void *);
c557edf4
RS
349static void frv_registers_update (rtx);
350static void frv_start_packet (void);
351static void frv_start_packet_block (void);
352static void frv_finish_packet (void (*) (void));
84034c69 353static bool frv_pack_insn_p (rtx_insn *);
b32d5189
DM
354static void frv_add_insn_to_packet (rtx_insn *);
355static void frv_insert_nop_in_packet (rtx_insn *);
c557edf4
RS
356static bool frv_for_each_packet (void (*) (void));
357static bool frv_sort_insn_group_1 (enum frv_insn_group,
358 unsigned int, unsigned int,
359 unsigned int, unsigned int,
360 state_t);
361static int frv_compare_insns (const void *, const void *);
362static void frv_sort_insn_group (enum frv_insn_group);
363static void frv_reorder_packet (void);
364static void frv_fill_unused_units (enum frv_insn_group);
365static void frv_align_label (void);
366static void frv_reorg_packet (void);
367static void frv_register_nop (rtx);
368static void frv_reorg (void);
f2206911
KC
369static void frv_pack_insns (void);
370static void frv_function_prologue (FILE *, HOST_WIDE_INT);
371static void frv_function_epilogue (FILE *, HOST_WIDE_INT);
372static bool frv_assemble_integer (rtx, unsigned, int);
373static void frv_init_builtins (void);
ef4bddc2 374static rtx frv_expand_builtin (tree, rtx, rtx, machine_mode, int);
f2206911 375static void frv_init_libfuncs (void);
3101faab 376static bool frv_in_small_data_p (const_tree);
3961e8fe 377static void frv_asm_output_mi_thunk
f2206911 378 (FILE *, tree, HOST_WIDE_INT, HOST_WIDE_INT, tree);
d5cc9181 379static void frv_setup_incoming_varargs (cumulative_args_t,
ef4bddc2 380 machine_mode,
d8c2bed3 381 tree, int *, int);
8ac411c7 382static rtx frv_expand_builtin_saveregs (void);
d7bd8aeb 383static void frv_expand_builtin_va_start (tree, rtx);
68f932c4
RS
384static bool frv_rtx_costs (rtx, int, int, int, int*,
385 bool);
ef4bddc2 386static int frv_register_move_cost (machine_mode,
33124e84 387 reg_class_t, reg_class_t);
ef4bddc2 388static int frv_memory_move_cost (machine_mode,
33124e84 389 reg_class_t, bool);
f2206911
KC
390static void frv_asm_out_constructor (rtx, int);
391static void frv_asm_out_destructor (rtx, int);
34208acf 392static bool frv_function_symbol_referenced_p (rtx);
ef4bddc2
RS
393static bool frv_legitimate_constant_p (machine_mode, rtx);
394static bool frv_cannot_force_const_mem (machine_mode, rtx);
34208acf
AO
395static const char *unspec_got_name (int);
396static void frv_output_const_unspec (FILE *,
397 const struct frv_unspec *);
764678d1 398static bool frv_function_ok_for_sibcall (tree, tree);
8ac411c7 399static rtx frv_struct_value_rtx (tree, int);
ef4bddc2
RS
400static bool frv_must_pass_in_stack (machine_mode mode, const_tree type);
401static int frv_arg_partial_bytes (cumulative_args_t, machine_mode,
78a52f11 402 tree, bool);
ef4bddc2 403static rtx frv_function_arg (cumulative_args_t, machine_mode,
88a1f47f 404 const_tree, bool);
ef4bddc2 405static rtx frv_function_incoming_arg (cumulative_args_t, machine_mode,
88a1f47f 406 const_tree, bool);
ef4bddc2 407static void frv_function_arg_advance (cumulative_args_t, machine_mode,
88a1f47f 408 const_tree, bool);
ef4bddc2 409static unsigned int frv_function_arg_boundary (machine_mode,
c2ed6cf8 410 const_tree);
fdbe66f2
EB
411static void frv_output_dwarf_dtprel (FILE *, int, rtx)
412 ATTRIBUTE_UNUSED;
a87cf97e 413static reg_class_t frv_secondary_reload (bool, rtx, reg_class_t,
ef4bddc2 414 machine_mode,
35f2d8ef 415 secondary_reload_info *);
b52b1749 416static bool frv_frame_pointer_required (void);
7b5cbb57 417static bool frv_can_eliminate (const int, const int);
5efd84c5 418static void frv_conditional_register_usage (void);
e9d5fdb2 419static void frv_trampoline_init (rtx, tree, rtx);
c28350ab 420static bool frv_class_likely_spilled_p (reg_class_t);
0b2c18fe 421\f
36a05131 422/* Initialize the GCC target structure. */
0fb30cb7
NF
423#undef TARGET_PRINT_OPERAND
424#define TARGET_PRINT_OPERAND frv_print_operand
425#undef TARGET_PRINT_OPERAND_ADDRESS
426#define TARGET_PRINT_OPERAND_ADDRESS frv_print_operand_address
427#undef TARGET_PRINT_OPERAND_PUNCT_VALID_P
428#define TARGET_PRINT_OPERAND_PUNCT_VALID_P frv_print_operand_punct_valid_p
36a05131
BS
429#undef TARGET_ASM_FUNCTION_PROLOGUE
430#define TARGET_ASM_FUNCTION_PROLOGUE frv_function_prologue
431#undef TARGET_ASM_FUNCTION_EPILOGUE
432#define TARGET_ASM_FUNCTION_EPILOGUE frv_function_epilogue
433#undef TARGET_ASM_INTEGER
434#define TARGET_ASM_INTEGER frv_assemble_integer
c5387660
JM
435#undef TARGET_OPTION_OVERRIDE
436#define TARGET_OPTION_OVERRIDE frv_option_override
14966b94
KG
437#undef TARGET_INIT_BUILTINS
438#define TARGET_INIT_BUILTINS frv_init_builtins
439#undef TARGET_EXPAND_BUILTIN
440#define TARGET_EXPAND_BUILTIN frv_expand_builtin
c15c90bb
ZW
441#undef TARGET_INIT_LIBFUNCS
442#define TARGET_INIT_LIBFUNCS frv_init_libfuncs
b3fbfc07
KG
443#undef TARGET_IN_SMALL_DATA_P
444#define TARGET_IN_SMALL_DATA_P frv_in_small_data_p
33124e84
AS
445#undef TARGET_REGISTER_MOVE_COST
446#define TARGET_REGISTER_MOVE_COST frv_register_move_cost
447#undef TARGET_MEMORY_MOVE_COST
448#define TARGET_MEMORY_MOVE_COST frv_memory_move_cost
3c50106f
RH
449#undef TARGET_RTX_COSTS
450#define TARGET_RTX_COSTS frv_rtx_costs
90a63880
RH
451#undef TARGET_ASM_CONSTRUCTOR
452#define TARGET_ASM_CONSTRUCTOR frv_asm_out_constructor
453#undef TARGET_ASM_DESTRUCTOR
454#define TARGET_ASM_DESTRUCTOR frv_asm_out_destructor
36a05131 455
c590b625
RH
456#undef TARGET_ASM_OUTPUT_MI_THUNK
457#define TARGET_ASM_OUTPUT_MI_THUNK frv_asm_output_mi_thunk
3961e8fe
RH
458#undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
459#define TARGET_ASM_CAN_OUTPUT_MI_THUNK default_can_output_mi_thunk_no_vcall
c590b625 460
28a60850
RS
461#undef TARGET_SCHED_ISSUE_RATE
462#define TARGET_SCHED_ISSUE_RATE frv_issue_rate
ffb344c1 463
506d7b68
PB
464#undef TARGET_LEGITIMIZE_ADDRESS
465#define TARGET_LEGITIMIZE_ADDRESS frv_legitimize_address
466
764678d1
AO
467#undef TARGET_FUNCTION_OK_FOR_SIBCALL
468#define TARGET_FUNCTION_OK_FOR_SIBCALL frv_function_ok_for_sibcall
1a627b35
RS
469#undef TARGET_LEGITIMATE_CONSTANT_P
470#define TARGET_LEGITIMATE_CONSTANT_P frv_legitimate_constant_p
34208acf
AO
471#undef TARGET_CANNOT_FORCE_CONST_MEM
472#define TARGET_CANNOT_FORCE_CONST_MEM frv_cannot_force_const_mem
473
bef8809e
AH
474#undef TARGET_HAVE_TLS
475#define TARGET_HAVE_TLS HAVE_AS_TLS
476
8ac411c7
KH
477#undef TARGET_STRUCT_VALUE_RTX
478#define TARGET_STRUCT_VALUE_RTX frv_struct_value_rtx
fe984136
RH
479#undef TARGET_MUST_PASS_IN_STACK
480#define TARGET_MUST_PASS_IN_STACK frv_must_pass_in_stack
8cd5a4e0
RH
481#undef TARGET_PASS_BY_REFERENCE
482#define TARGET_PASS_BY_REFERENCE hook_pass_by_reference_must_pass_in_stack
78a52f11
RH
483#undef TARGET_ARG_PARTIAL_BYTES
484#define TARGET_ARG_PARTIAL_BYTES frv_arg_partial_bytes
88a1f47f
NF
485#undef TARGET_FUNCTION_ARG
486#define TARGET_FUNCTION_ARG frv_function_arg
487#undef TARGET_FUNCTION_INCOMING_ARG
488#define TARGET_FUNCTION_INCOMING_ARG frv_function_incoming_arg
489#undef TARGET_FUNCTION_ARG_ADVANCE
490#define TARGET_FUNCTION_ARG_ADVANCE frv_function_arg_advance
c2ed6cf8
NF
491#undef TARGET_FUNCTION_ARG_BOUNDARY
492#define TARGET_FUNCTION_ARG_BOUNDARY frv_function_arg_boundary
8ac411c7
KH
493
494#undef TARGET_EXPAND_BUILTIN_SAVEREGS
495#define TARGET_EXPAND_BUILTIN_SAVEREGS frv_expand_builtin_saveregs
d8c2bed3
KH
496#undef TARGET_SETUP_INCOMING_VARARGS
497#define TARGET_SETUP_INCOMING_VARARGS frv_setup_incoming_varargs
c557edf4
RS
498#undef TARGET_MACHINE_DEPENDENT_REORG
499#define TARGET_MACHINE_DEPENDENT_REORG frv_reorg
8ac411c7 500
d7bd8aeb
JJ
501#undef TARGET_EXPAND_BUILTIN_VA_START
502#define TARGET_EXPAND_BUILTIN_VA_START frv_expand_builtin_va_start
503
fdbe66f2
EB
504#if HAVE_AS_TLS
505#undef TARGET_ASM_OUTPUT_DWARF_DTPREL
506#define TARGET_ASM_OUTPUT_DWARF_DTPREL frv_output_dwarf_dtprel
507#endif
508
c28350ab
AS
509#undef TARGET_CLASS_LIKELY_SPILLED_P
510#define TARGET_CLASS_LIKELY_SPILLED_P frv_class_likely_spilled_p
511
35f2d8ef
NC
512#undef TARGET_SECONDARY_RELOAD
513#define TARGET_SECONDARY_RELOAD frv_secondary_reload
514
c6c3dba9
PB
515#undef TARGET_LEGITIMATE_ADDRESS_P
516#define TARGET_LEGITIMATE_ADDRESS_P frv_legitimate_address_p
517
b52b1749
AS
518#undef TARGET_FRAME_POINTER_REQUIRED
519#define TARGET_FRAME_POINTER_REQUIRED frv_frame_pointer_required
520
7b5cbb57
AS
521#undef TARGET_CAN_ELIMINATE
522#define TARGET_CAN_ELIMINATE frv_can_eliminate
523
5efd84c5
NF
524#undef TARGET_CONDITIONAL_REGISTER_USAGE
525#define TARGET_CONDITIONAL_REGISTER_USAGE frv_conditional_register_usage
526
e9d5fdb2
RH
527#undef TARGET_TRAMPOLINE_INIT
528#define TARGET_TRAMPOLINE_INIT frv_trampoline_init
529
219d92a4
AS
530#undef TARGET_FUNCTION_VALUE
531#define TARGET_FUNCTION_VALUE frv_function_value
532#undef TARGET_LIBCALL_VALUE
533#define TARGET_LIBCALL_VALUE frv_libcall_value
534
36a05131 535struct gcc_target targetm = TARGET_INITIALIZER;
bef8809e
AH
536
537#define FRV_SYMBOL_REF_TLS_P(RTX) \
538 (GET_CODE (RTX) == SYMBOL_REF && SYMBOL_REF_TLS_MODEL (RTX) != 0)
539
36a05131 540\f
764678d1
AO
541/* Any function call that satisfies the machine-independent
542 requirements is eligible on FR-V. */
543
544static bool
545frv_function_ok_for_sibcall (tree decl ATTRIBUTE_UNUSED,
546 tree exp ATTRIBUTE_UNUSED)
547{
548 return true;
549}
550
34208acf
AO
551/* Return true if SYMBOL is a small data symbol and relocation RELOC
552 can be used to access it directly in a load or store. */
36a05131 553
34208acf
AO
554static FRV_INLINE bool
555frv_small_data_reloc_p (rtx symbol, int reloc)
36a05131 556{
34208acf
AO
557 return (GET_CODE (symbol) == SYMBOL_REF
558 && SYMBOL_REF_SMALL_P (symbol)
559 && (!TARGET_FDPIC || flag_pic == 1)
560 && (reloc == R_FRV_GOTOFF12 || reloc == R_FRV_GPREL12));
561}
36a05131 562
34208acf
AO
563/* Return true if X is a valid relocation unspec. If it is, fill in UNSPEC
564 appropriately. */
36a05131 565
6d26dc3b 566bool
34208acf
AO
567frv_const_unspec_p (rtx x, struct frv_unspec *unspec)
568{
569 if (GET_CODE (x) == CONST)
570 {
571 unspec->offset = 0;
572 x = XEXP (x, 0);
573 if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 1)) == CONST_INT)
574 {
575 unspec->offset += INTVAL (XEXP (x, 1));
576 x = XEXP (x, 0);
577 }
578 if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_GOT)
579 {
580 unspec->symbol = XVECEXP (x, 0, 0);
581 unspec->reloc = INTVAL (XVECEXP (x, 0, 1));
36a05131 582
34208acf
AO
583 if (unspec->offset == 0)
584 return true;
36a05131 585
34208acf
AO
586 if (frv_small_data_reloc_p (unspec->symbol, unspec->reloc)
587 && unspec->offset > 0
fa37ed29 588 && unspec->offset < g_switch_value)
34208acf
AO
589 return true;
590 }
591 }
592 return false;
36a05131
BS
593}
594
34208acf
AO
595/* Decide whether we can force certain constants to memory. If we
596 decide we can't, the caller should be able to cope with it in
597 another way.
36a05131 598
34208acf
AO
599 We never allow constants to be forced into memory for TARGET_FDPIC.
600 This is necessary for several reasons:
36a05131 601
1a627b35 602 1. Since frv_legitimate_constant_p rejects constant pool addresses, the
34208acf
AO
603 target-independent code will try to force them into the constant
604 pool, thus leading to infinite recursion.
36a05131 605
34208acf
AO
606 2. We can never introduce new constant pool references during reload.
607 Any such reference would require use of the pseudo FDPIC register.
36a05131 608
34208acf
AO
609 3. We can't represent a constant added to a function pointer (which is
610 not the same as a pointer to a function+constant).
611
612 4. In many cases, it's more efficient to calculate the constant in-line. */
613
614static bool
ef4bddc2 615frv_cannot_force_const_mem (machine_mode mode ATTRIBUTE_UNUSED,
fbbf66e7 616 rtx x ATTRIBUTE_UNUSED)
34208acf
AO
617{
618 return TARGET_FDPIC;
619}
36a05131
BS
620\f
621static int
f2206911 622frv_default_flags_for_cpu (void)
36a05131
BS
623{
624 switch (frv_cpu_type)
625 {
626 case FRV_CPU_GENERIC:
627 return MASK_DEFAULT_FRV;
628
c557edf4
RS
629 case FRV_CPU_FR550:
630 return MASK_DEFAULT_FR550;
631
36a05131
BS
632 case FRV_CPU_FR500:
633 case FRV_CPU_TOMCAT:
634 return MASK_DEFAULT_FR500;
635
c557edf4
RS
636 case FRV_CPU_FR450:
637 return MASK_DEFAULT_FR450;
638
639 case FRV_CPU_FR405:
36a05131
BS
640 case FRV_CPU_FR400:
641 return MASK_DEFAULT_FR400;
642
643 case FRV_CPU_FR300:
644 case FRV_CPU_SIMPLE:
645 return MASK_DEFAULT_SIMPLE;
44e91694
NS
646
647 default:
648 gcc_unreachable ();
36a05131 649 }
36a05131
BS
650}
651
c5387660 652/* Implement TARGET_OPTION_OVERRIDE. */
36a05131 653
c5387660
JM
654static void
655frv_option_override (void)
36a05131 656{
c557edf4
RS
657 int regno;
658 unsigned int i;
36a05131 659
36a05131
BS
660 target_flags |= (frv_default_flags_for_cpu () & ~target_flags_explicit);
661
662 /* -mlibrary-pic sets -fPIC and -G0 and also suppresses warnings from the
663 linker about linking pic and non-pic code. */
664 if (TARGET_LIBPIC)
665 {
666 if (!flag_pic) /* -fPIC */
667 flag_pic = 2;
668
fa37ed29 669 if (!global_options_set.x_g_switch_value) /* -G0 */
36a05131 670 {
36a05131
BS
671 g_switch_value = 0;
672 }
673 }
674
36a05131
BS
675 /* A C expression whose value is a register class containing hard
676 register REGNO. In general there is more than one such class;
677 choose a class which is "minimal", meaning that no smaller class
87b483a1 678 also contains the register. */
36a05131
BS
679
680 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
681 {
0a2aaacc 682 enum reg_class rclass;
36a05131
BS
683
684 if (GPR_P (regno))
685 {
686 int gpr_reg = regno - GPR_FIRST;
bef8809e
AH
687
688 if (gpr_reg == GR8_REG)
0a2aaacc 689 rclass = GR8_REGS;
bef8809e
AH
690
691 else if (gpr_reg == GR9_REG)
0a2aaacc 692 rclass = GR9_REGS;
bef8809e
AH
693
694 else if (gpr_reg == GR14_REG)
0a2aaacc 695 rclass = FDPIC_FPTR_REGS;
bef8809e
AH
696
697 else if (gpr_reg == FDPIC_REGNO)
0a2aaacc 698 rclass = FDPIC_REGS;
bef8809e
AH
699
700 else if ((gpr_reg & 3) == 0)
0a2aaacc 701 rclass = QUAD_REGS;
36a05131
BS
702
703 else if ((gpr_reg & 1) == 0)
0a2aaacc 704 rclass = EVEN_REGS;
36a05131
BS
705
706 else
0a2aaacc 707 rclass = GPR_REGS;
36a05131
BS
708 }
709
710 else if (FPR_P (regno))
711 {
712 int fpr_reg = regno - GPR_FIRST;
713 if ((fpr_reg & 3) == 0)
0a2aaacc 714 rclass = QUAD_FPR_REGS;
36a05131
BS
715
716 else if ((fpr_reg & 1) == 0)
0a2aaacc 717 rclass = FEVEN_REGS;
36a05131
BS
718
719 else
0a2aaacc 720 rclass = FPR_REGS;
36a05131
BS
721 }
722
723 else if (regno == LR_REGNO)
0a2aaacc 724 rclass = LR_REG;
36a05131
BS
725
726 else if (regno == LCR_REGNO)
0a2aaacc 727 rclass = LCR_REG;
36a05131
BS
728
729 else if (ICC_P (regno))
0a2aaacc 730 rclass = ICC_REGS;
36a05131
BS
731
732 else if (FCC_P (regno))
0a2aaacc 733 rclass = FCC_REGS;
36a05131
BS
734
735 else if (ICR_P (regno))
0a2aaacc 736 rclass = ICR_REGS;
36a05131
BS
737
738 else if (FCR_P (regno))
0a2aaacc 739 rclass = FCR_REGS;
36a05131
BS
740
741 else if (ACC_P (regno))
742 {
743 int r = regno - ACC_FIRST;
744 if ((r & 3) == 0)
0a2aaacc 745 rclass = QUAD_ACC_REGS;
36a05131 746 else if ((r & 1) == 0)
0a2aaacc 747 rclass = EVEN_ACC_REGS;
36a05131 748 else
0a2aaacc 749 rclass = ACC_REGS;
36a05131
BS
750 }
751
752 else if (ACCG_P (regno))
0a2aaacc 753 rclass = ACCG_REGS;
36a05131
BS
754
755 else
0a2aaacc 756 rclass = NO_REGS;
36a05131 757
0a2aaacc 758 regno_reg_class[regno] = rclass;
36a05131
BS
759 }
760
761 /* Check for small data option */
fa37ed29 762 if (!global_options_set.x_g_switch_value && !TARGET_LIBPIC)
36a05131
BS
763 g_switch_value = SDATA_DEFAULT_SIZE;
764
36a05131
BS
765 /* There is no single unaligned SI op for PIC code. Sometimes we
766 need to use ".4byte" and sometimes we need to use ".picptr".
767 See frv_assemble_integer for details. */
34208acf 768 if (flag_pic || TARGET_FDPIC)
36a05131
BS
769 targetm.asm_out.unaligned_op.si = 0;
770
34208acf
AO
771 if ((target_flags_explicit & MASK_LINKED_FP) == 0)
772 target_flags |= MASK_LINKED_FP;
773
38c28a25
AH
774 if ((target_flags_explicit & MASK_OPTIMIZE_MEMBAR) == 0)
775 target_flags |= MASK_OPTIMIZE_MEMBAR;
776
c557edf4
RS
777 for (i = 0; i < ARRAY_SIZE (frv_unit_names); i++)
778 frv_unit_codes[i] = get_cpu_unit_code (frv_unit_names[i]);
779
780 for (i = 0; i < ARRAY_SIZE (frv_type_to_unit); i++)
781 frv_type_to_unit[i] = ARRAY_SIZE (frv_unit_codes);
782
36a05131
BS
783 init_machine_status = frv_init_machine_status;
784}
785
36a05131
BS
786\f
787/* Return true if NAME (a STRING_CST node) begins with PREFIX. */
788
789static int
f961457f 790frv_string_begins_with (const char *name, const char *prefix)
36a05131 791{
3101faab 792 const int prefix_len = strlen (prefix);
36a05131
BS
793
794 /* Remember: NAME's length includes the null terminator. */
f961457f 795 return (strncmp (name, prefix, prefix_len) == 0);
36a05131 796}
36a05131 797\f
b48e9677 798/* Implement TARGET_CONDITIONAL_REGISTER_USAGE. */
36a05131 799
5efd84c5 800static void
f2206911 801frv_conditional_register_usage (void)
36a05131
BS
802{
803 int i;
804
805 for (i = GPR_FIRST + NUM_GPRS; i <= GPR_LAST; i++)
806 fixed_regs[i] = call_used_regs[i] = 1;
807
808 for (i = FPR_FIRST + NUM_FPRS; i <= FPR_LAST; i++)
809 fixed_regs[i] = call_used_regs[i] = 1;
810
36a05131
BS
811 /* Reserve the registers used for conditional execution. At present, we need
812 1 ICC and 1 ICR register. */
813 fixed_regs[ICC_TEMP] = call_used_regs[ICC_TEMP] = 1;
814 fixed_regs[ICR_TEMP] = call_used_regs[ICR_TEMP] = 1;
815
816 if (TARGET_FIXED_CC)
817 {
818 fixed_regs[ICC_FIRST] = call_used_regs[ICC_FIRST] = 1;
819 fixed_regs[FCC_FIRST] = call_used_regs[FCC_FIRST] = 1;
820 fixed_regs[ICR_FIRST] = call_used_regs[ICR_FIRST] = 1;
821 fixed_regs[FCR_FIRST] = call_used_regs[FCR_FIRST] = 1;
822 }
823
34208acf
AO
824 if (TARGET_FDPIC)
825 fixed_regs[GPR_FIRST + 16] = fixed_regs[GPR_FIRST + 17] =
826 call_used_regs[GPR_FIRST + 16] = call_used_regs[GPR_FIRST + 17] = 0;
827
36a05131
BS
828#if 0
829 /* If -fpic, SDA_BASE_REG is the PIC register. */
830 if (g_switch_value == 0 && !flag_pic)
831 fixed_regs[SDA_BASE_REG] = call_used_regs[SDA_BASE_REG] = 0;
832
833 if (!flag_pic)
834 fixed_regs[PIC_REGNO] = call_used_regs[PIC_REGNO] = 0;
835#endif
836}
837
838\f
839/*
840 * Compute the stack frame layout
841 *
842 * Register setup:
843 * +---------------+-----------------------+-----------------------+
844 * |Register |type |caller-save/callee-save|
845 * +---------------+-----------------------+-----------------------+
846 * |GR0 |Zero register | - |
847 * |GR1 |Stack pointer(SP) | - |
848 * |GR2 |Frame pointer(FP) | - |
849 * |GR3 |Hidden parameter | caller save |
850 * |GR4-GR7 | - | caller save |
851 * |GR8-GR13 |Argument register | caller save |
852 * |GR14-GR15 | - | caller save |
853 * |GR16-GR31 | - | callee save |
854 * |GR32-GR47 | - | caller save |
855 * |GR48-GR63 | - | callee save |
856 * |FR0-FR15 | - | caller save |
857 * |FR16-FR31 | - | callee save |
858 * |FR32-FR47 | - | caller save |
859 * |FR48-FR63 | - | callee save |
860 * +---------------+-----------------------+-----------------------+
861 *
862 * Stack frame setup:
863 * Low
864 * SP-> |-----------------------------------|
865 * | Argument area |
866 * |-----------------------------------|
867 * | Register save area |
868 * |-----------------------------------|
869 * | Local variable save area |
870 * FP-> |-----------------------------------|
871 * | Old FP |
872 * |-----------------------------------|
873 * | Hidden parameter save area |
874 * |-----------------------------------|
875 * | Return address(LR) storage area |
876 * |-----------------------------------|
877 * | Padding for alignment |
878 * |-----------------------------------|
879 * | Register argument area |
880 * OLD SP-> |-----------------------------------|
881 * | Parameter area |
882 * |-----------------------------------|
883 * High
884 *
885 * Argument area/Parameter area:
886 *
887 * When a function is called, this area is used for argument transfer. When
888 * the argument is set up by the caller function, this area is referred to as
889 * the argument area. When the argument is referenced by the callee function,
890 * this area is referred to as the parameter area. The area is allocated when
891 * all arguments cannot be placed on the argument register at the time of
892 * argument transfer.
893 *
894 * Register save area:
895 *
896 * This is a register save area that must be guaranteed for the caller
897 * function. This area is not secured when the register save operation is not
898 * needed.
899 *
900 * Local variable save area:
901 *
902 * This is the area for local variables and temporary variables.
903 *
904 * Old FP:
905 *
906 * This area stores the FP value of the caller function.
907 *
908 * Hidden parameter save area:
909 *
910 * This area stores the start address of the return value storage
911 * area for a struct/union return function.
912 * When a struct/union is used as the return value, the caller
913 * function stores the return value storage area start address in
914 * register GR3 and passes it to the caller function.
915 * The callee function interprets the address stored in the GR3
916 * as the return value storage area start address.
917 * When register GR3 needs to be saved into memory, the callee
918 * function saves it in the hidden parameter save area. This
919 * area is not secured when the save operation is not needed.
920 *
921 * Return address(LR) storage area:
922 *
923 * This area saves the LR. The LR stores the address of a return to the caller
924 * function for the purpose of function calling.
925 *
926 * Argument register area:
927 *
928 * This area saves the argument register. This area is not secured when the
929 * save operation is not needed.
930 *
931 * Argument:
932 *
933 * Arguments, the count of which equals the count of argument registers (6
934 * words), are positioned in registers GR8 to GR13 and delivered to the callee
935 * function. When a struct/union return function is called, the return value
936 * area address is stored in register GR3. Arguments not placed in the
937 * argument registers will be stored in the stack argument area for transfer
938 * purposes. When an 8-byte type argument is to be delivered using registers,
939 * it is divided into two and placed in two registers for transfer. When
940 * argument registers must be saved to memory, the callee function secures an
941 * argument register save area in the stack. In this case, a continuous
942 * argument register save area must be established in the parameter area. The
943 * argument register save area must be allocated as needed to cover the size of
944 * the argument register to be saved. If the function has a variable count of
945 * arguments, it saves all argument registers in the argument register save
946 * area.
947 *
948 * Argument Extension Format:
949 *
950 * When an argument is to be stored in the stack, its type is converted to an
951 * extended type in accordance with the individual argument type. The argument
952 * is freed by the caller function after the return from the callee function is
953 * made.
954 *
955 * +-----------------------+---------------+------------------------+
956 * | Argument Type |Extended Type |Stack Storage Size(byte)|
957 * +-----------------------+---------------+------------------------+
958 * |char |int | 4 |
959 * |signed char |int | 4 |
960 * |unsigned char |int | 4 |
961 * |[signed] short int |int | 4 |
962 * |unsigned short int |int | 4 |
963 * |[signed] int |No extension | 4 |
964 * |unsigned int |No extension | 4 |
965 * |[signed] long int |No extension | 4 |
966 * |unsigned long int |No extension | 4 |
967 * |[signed] long long int |No extension | 8 |
968 * |unsigned long long int |No extension | 8 |
969 * |float |double | 8 |
970 * |double |No extension | 8 |
971 * |long double |No extension | 8 |
972 * |pointer |No extension | 4 |
973 * |struct/union |- | 4 (*1) |
974 * +-----------------------+---------------+------------------------+
975 *
976 * When a struct/union is to be delivered as an argument, the caller copies it
977 * to the local variable area and delivers the address of that area.
978 *
979 * Return Value:
980 *
981 * +-------------------------------+----------------------+
982 * |Return Value Type |Return Value Interface|
983 * +-------------------------------+----------------------+
984 * |void |None |
985 * |[signed|unsigned] char |GR8 |
986 * |[signed|unsigned] short int |GR8 |
987 * |[signed|unsigned] int |GR8 |
988 * |[signed|unsigned] long int |GR8 |
989 * |pointer |GR8 |
990 * |[signed|unsigned] long long int|GR8 & GR9 |
991 * |float |GR8 |
992 * |double |GR8 & GR9 |
993 * |long double |GR8 & GR9 |
994 * |struct/union |(*1) |
995 * +-------------------------------+----------------------+
996 *
997 * When a struct/union is used as the return value, the caller function stores
998 * the start address of the return value storage area into GR3 and then passes
999 * it to the callee function. The callee function interprets GR3 as the start
1000 * address of the return value storage area. When this address needs to be
1001 * saved in memory, the callee function secures the hidden parameter save area
1002 * and saves the address in that area.
1003 */
1004
1005frv_stack_t *
f2206911 1006frv_stack_info (void)
36a05131
BS
1007{
1008 static frv_stack_t info, zero_info;
1009 frv_stack_t *info_ptr = &info;
1010 tree fndecl = current_function_decl;
1011 int varargs_p = 0;
1012 tree cur_arg;
1013 tree next_arg;
1014 int range;
1015 int alignment;
1016 int offset;
1017
87b483a1
KH
1018 /* If we've already calculated the values and reload is complete,
1019 just return now. */
36a05131
BS
1020 if (frv_stack_cache)
1021 return frv_stack_cache;
1022
87b483a1 1023 /* Zero all fields. */
36a05131
BS
1024 info = zero_info;
1025
87b483a1 1026 /* Set up the register range information. */
36a05131
BS
1027 info_ptr->regs[STACK_REGS_GPR].name = "gpr";
1028 info_ptr->regs[STACK_REGS_GPR].first = LAST_ARG_REGNUM + 1;
1029 info_ptr->regs[STACK_REGS_GPR].last = GPR_LAST;
1030 info_ptr->regs[STACK_REGS_GPR].dword_p = TRUE;
1031
1032 info_ptr->regs[STACK_REGS_FPR].name = "fpr";
1033 info_ptr->regs[STACK_REGS_FPR].first = FPR_FIRST;
1034 info_ptr->regs[STACK_REGS_FPR].last = FPR_LAST;
1035 info_ptr->regs[STACK_REGS_FPR].dword_p = TRUE;
1036
1037 info_ptr->regs[STACK_REGS_LR].name = "lr";
1038 info_ptr->regs[STACK_REGS_LR].first = LR_REGNO;
1039 info_ptr->regs[STACK_REGS_LR].last = LR_REGNO;
1040 info_ptr->regs[STACK_REGS_LR].special_p = 1;
1041
1042 info_ptr->regs[STACK_REGS_CC].name = "cc";
1043 info_ptr->regs[STACK_REGS_CC].first = CC_FIRST;
1044 info_ptr->regs[STACK_REGS_CC].last = CC_LAST;
1045 info_ptr->regs[STACK_REGS_CC].field_p = TRUE;
1046
1047 info_ptr->regs[STACK_REGS_LCR].name = "lcr";
1048 info_ptr->regs[STACK_REGS_LCR].first = LCR_REGNO;
1049 info_ptr->regs[STACK_REGS_LCR].last = LCR_REGNO;
1050
1051 info_ptr->regs[STACK_REGS_STDARG].name = "stdarg";
1052 info_ptr->regs[STACK_REGS_STDARG].first = FIRST_ARG_REGNUM;
1053 info_ptr->regs[STACK_REGS_STDARG].last = LAST_ARG_REGNUM;
1054 info_ptr->regs[STACK_REGS_STDARG].dword_p = 1;
1055 info_ptr->regs[STACK_REGS_STDARG].special_p = 1;
1056
1057 info_ptr->regs[STACK_REGS_STRUCT].name = "struct";
8ac411c7
KH
1058 info_ptr->regs[STACK_REGS_STRUCT].first = FRV_STRUCT_VALUE_REGNUM;
1059 info_ptr->regs[STACK_REGS_STRUCT].last = FRV_STRUCT_VALUE_REGNUM;
36a05131
BS
1060 info_ptr->regs[STACK_REGS_STRUCT].special_p = 1;
1061
1062 info_ptr->regs[STACK_REGS_FP].name = "fp";
1063 info_ptr->regs[STACK_REGS_FP].first = FRAME_POINTER_REGNUM;
1064 info_ptr->regs[STACK_REGS_FP].last = FRAME_POINTER_REGNUM;
1065 info_ptr->regs[STACK_REGS_FP].special_p = 1;
1066
1067 /* Determine if this is a stdarg function. If so, allocate space to store
1068 the 6 arguments. */
1069 if (cfun->stdarg)
1070 varargs_p = 1;
1071
1072 else
1073 {
1074 /* Find the last argument, and see if it is __builtin_va_alist. */
1075 for (cur_arg = DECL_ARGUMENTS (fndecl); cur_arg != (tree)0; cur_arg = next_arg)
1076 {
910ad8de 1077 next_arg = DECL_CHAIN (cur_arg);
36a05131
BS
1078 if (next_arg == (tree)0)
1079 {
1080 if (DECL_NAME (cur_arg)
1081 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (cur_arg)), "__builtin_va_alist"))
1082 varargs_p = 1;
1083
1084 break;
1085 }
1086 }
1087 }
1088
87b483a1 1089 /* Iterate over all of the register ranges. */
36a05131
BS
1090 for (range = 0; range < STACK_REGS_MAX; range++)
1091 {
1092 frv_stack_regs_t *reg_ptr = &(info_ptr->regs[range]);
1093 int first = reg_ptr->first;
1094 int last = reg_ptr->last;
1095 int size_1word = 0;
1096 int size_2words = 0;
1097 int regno;
1098
87b483a1 1099 /* Calculate which registers need to be saved & save area size. */
36a05131
BS
1100 switch (range)
1101 {
1102 default:
1103 for (regno = first; regno <= last; regno++)
1104 {
6fb5fa3c 1105 if ((df_regs_ever_live_p (regno) && !call_used_regs[regno])
e3b5732b 1106 || (crtl->calls_eh_return
36a05131 1107 && (regno >= FIRST_EH_REGNUM && regno <= LAST_EH_REGNUM))
34208acf 1108 || (!TARGET_FDPIC && flag_pic
ad516a74 1109 && crtl->uses_pic_offset_table && regno == PIC_REGNO))
36a05131
BS
1110 {
1111 info_ptr->save_p[regno] = REG_SAVE_1WORD;
1112 size_1word += UNITS_PER_WORD;
1113 }
1114 }
1115 break;
1116
1117 /* Calculate whether we need to create a frame after everything else
1118 has been processed. */
1119 case STACK_REGS_FP:
1120 break;
1121
1122 case STACK_REGS_LR:
6fb5fa3c 1123 if (df_regs_ever_live_p (LR_REGNO)
36a05131 1124 || profile_flag
34208acf
AO
1125 /* This is set for __builtin_return_address, etc. */
1126 || cfun->machine->frame_needed
1127 || (TARGET_LINKED_FP && frame_pointer_needed)
1128 || (!TARGET_FDPIC && flag_pic
ad516a74 1129 && crtl->uses_pic_offset_table))
36a05131
BS
1130 {
1131 info_ptr->save_p[LR_REGNO] = REG_SAVE_1WORD;
1132 size_1word += UNITS_PER_WORD;
1133 }
1134 break;
1135
1136 case STACK_REGS_STDARG:
1137 if (varargs_p)
1138 {
87b483a1
KH
1139 /* If this is a stdarg function with a non varardic
1140 argument split between registers and the stack,
1141 adjust the saved registers downward. */
7dd68986 1142 last -= (ADDR_ALIGN (crtl->args.pretend_args_size, UNITS_PER_WORD)
36a05131
BS
1143 / UNITS_PER_WORD);
1144
1145 for (regno = first; regno <= last; regno++)
1146 {
1147 info_ptr->save_p[regno] = REG_SAVE_1WORD;
1148 size_1word += UNITS_PER_WORD;
1149 }
1150
1151 info_ptr->stdarg_size = size_1word;
1152 }
1153 break;
1154
1155 case STACK_REGS_STRUCT:
1156 if (cfun->returns_struct)
1157 {
8ac411c7 1158 info_ptr->save_p[FRV_STRUCT_VALUE_REGNUM] = REG_SAVE_1WORD;
36a05131
BS
1159 size_1word += UNITS_PER_WORD;
1160 }
1161 break;
1162 }
1163
1164
1165 if (size_1word)
1166 {
87b483a1 1167 /* If this is a field, it only takes one word. */
36a05131
BS
1168 if (reg_ptr->field_p)
1169 size_1word = UNITS_PER_WORD;
1170
87b483a1 1171 /* Determine which register pairs can be saved together. */
36a05131
BS
1172 else if (reg_ptr->dword_p && TARGET_DWORD)
1173 {
1174 for (regno = first; regno < last; regno += 2)
1175 {
1176 if (info_ptr->save_p[regno] && info_ptr->save_p[regno+1])
1177 {
1178 size_2words += 2 * UNITS_PER_WORD;
1179 size_1word -= 2 * UNITS_PER_WORD;
1180 info_ptr->save_p[regno] = REG_SAVE_2WORDS;
1181 info_ptr->save_p[regno+1] = REG_SAVE_NO_SAVE;
1182 }
1183 }
1184 }
1185
1186 reg_ptr->size_1word = size_1word;
1187 reg_ptr->size_2words = size_2words;
1188
1189 if (! reg_ptr->special_p)
1190 {
1191 info_ptr->regs_size_1word += size_1word;
1192 info_ptr->regs_size_2words += size_2words;
1193 }
1194 }
1195 }
1196
1197 /* Set up the sizes of each each field in the frame body, making the sizes
1198 of each be divisible by the size of a dword if dword operations might
1199 be used, or the size of a word otherwise. */
1200 alignment = (TARGET_DWORD? 2 * UNITS_PER_WORD : UNITS_PER_WORD);
1201
7dd68986 1202 info_ptr->parameter_size = ADDR_ALIGN (crtl->outgoing_args_size, alignment);
36a05131
BS
1203 info_ptr->regs_size = ADDR_ALIGN (info_ptr->regs_size_2words
1204 + info_ptr->regs_size_1word,
1205 alignment);
1206 info_ptr->vars_size = ADDR_ALIGN (get_frame_size (), alignment);
1207
7dd68986 1208 info_ptr->pretend_size = crtl->args.pretend_args_size;
36a05131
BS
1209
1210 /* Work out the size of the frame, excluding the header. Both the frame
1211 body and register parameter area will be dword-aligned. */
1212 info_ptr->total_size
1213 = (ADDR_ALIGN (info_ptr->parameter_size
1214 + info_ptr->regs_size
1215 + info_ptr->vars_size,
1216 2 * UNITS_PER_WORD)
1217 + ADDR_ALIGN (info_ptr->pretend_size
1218 + info_ptr->stdarg_size,
1219 2 * UNITS_PER_WORD));
1220
1221 /* See if we need to create a frame at all, if so add header area. */
1222 if (info_ptr->total_size > 0
34208acf 1223 || frame_pointer_needed
36a05131
BS
1224 || info_ptr->regs[STACK_REGS_LR].size_1word > 0
1225 || info_ptr->regs[STACK_REGS_STRUCT].size_1word > 0)
1226 {
1227 offset = info_ptr->parameter_size;
1228 info_ptr->header_size = 4 * UNITS_PER_WORD;
1229 info_ptr->total_size += 4 * UNITS_PER_WORD;
1230
87b483a1 1231 /* Calculate the offsets to save normal register pairs. */
36a05131
BS
1232 for (range = 0; range < STACK_REGS_MAX; range++)
1233 {
1234 frv_stack_regs_t *reg_ptr = &(info_ptr->regs[range]);
1235 if (! reg_ptr->special_p)
1236 {
1237 int first = reg_ptr->first;
1238 int last = reg_ptr->last;
1239 int regno;
1240
1241 for (regno = first; regno <= last; regno++)
1242 if (info_ptr->save_p[regno] == REG_SAVE_2WORDS
1243 && regno != FRAME_POINTER_REGNUM
1244 && (regno < FIRST_ARG_REGNUM
1245 || regno > LAST_ARG_REGNUM))
1246 {
1247 info_ptr->reg_offset[regno] = offset;
1248 offset += 2 * UNITS_PER_WORD;
1249 }
1250 }
1251 }
1252
87b483a1 1253 /* Calculate the offsets to save normal single registers. */
36a05131
BS
1254 for (range = 0; range < STACK_REGS_MAX; range++)
1255 {
1256 frv_stack_regs_t *reg_ptr = &(info_ptr->regs[range]);
1257 if (! reg_ptr->special_p)
1258 {
1259 int first = reg_ptr->first;
1260 int last = reg_ptr->last;
1261 int regno;
1262
1263 for (regno = first; regno <= last; regno++)
1264 if (info_ptr->save_p[regno] == REG_SAVE_1WORD
1265 && regno != FRAME_POINTER_REGNUM
1266 && (regno < FIRST_ARG_REGNUM
1267 || regno > LAST_ARG_REGNUM))
1268 {
1269 info_ptr->reg_offset[regno] = offset;
1270 offset += UNITS_PER_WORD;
1271 }
1272 }
1273 }
1274
1275 /* Calculate the offset to save the local variables at. */
1276 offset = ADDR_ALIGN (offset, alignment);
1277 if (info_ptr->vars_size)
1278 {
1279 info_ptr->vars_offset = offset;
1280 offset += info_ptr->vars_size;
1281 }
1282
1283 /* Align header to a dword-boundary. */
1284 offset = ADDR_ALIGN (offset, 2 * UNITS_PER_WORD);
1285
1286 /* Calculate the offsets in the fixed frame. */
1287 info_ptr->save_p[FRAME_POINTER_REGNUM] = REG_SAVE_1WORD;
1288 info_ptr->reg_offset[FRAME_POINTER_REGNUM] = offset;
1289 info_ptr->regs[STACK_REGS_FP].size_1word = UNITS_PER_WORD;
1290
1291 info_ptr->save_p[LR_REGNO] = REG_SAVE_1WORD;
1292 info_ptr->reg_offset[LR_REGNO] = offset + 2*UNITS_PER_WORD;
1293 info_ptr->regs[STACK_REGS_LR].size_1word = UNITS_PER_WORD;
1294
1295 if (cfun->returns_struct)
1296 {
8ac411c7
KH
1297 info_ptr->save_p[FRV_STRUCT_VALUE_REGNUM] = REG_SAVE_1WORD;
1298 info_ptr->reg_offset[FRV_STRUCT_VALUE_REGNUM] = offset + UNITS_PER_WORD;
36a05131
BS
1299 info_ptr->regs[STACK_REGS_STRUCT].size_1word = UNITS_PER_WORD;
1300 }
1301
1302 /* Calculate the offsets to store the arguments passed in registers
1303 for stdarg functions. The register pairs are first and the single
1304 register if any is last. The register save area starts on a
1305 dword-boundary. */
1306 if (info_ptr->stdarg_size)
1307 {
1308 int first = info_ptr->regs[STACK_REGS_STDARG].first;
1309 int last = info_ptr->regs[STACK_REGS_STDARG].last;
1310 int regno;
1311
1312 /* Skip the header. */
1313 offset += 4 * UNITS_PER_WORD;
1314 for (regno = first; regno <= last; regno++)
1315 {
1316 if (info_ptr->save_p[regno] == REG_SAVE_2WORDS)
1317 {
1318 info_ptr->reg_offset[regno] = offset;
1319 offset += 2 * UNITS_PER_WORD;
1320 }
1321 else if (info_ptr->save_p[regno] == REG_SAVE_1WORD)
1322 {
1323 info_ptr->reg_offset[regno] = offset;
1324 offset += UNITS_PER_WORD;
1325 }
1326 }
1327 }
1328 }
1329
1330 if (reload_completed)
1331 frv_stack_cache = info_ptr;
1332
1333 return info_ptr;
1334}
1335
1336\f
87b483a1 1337/* Print the information about the frv stack offsets, etc. when debugging. */
36a05131
BS
1338
1339void
f2206911 1340frv_debug_stack (frv_stack_t *info)
36a05131
BS
1341{
1342 int range;
1343
1344 if (!info)
1345 info = frv_stack_info ();
1346
1347 fprintf (stderr, "\nStack information for function %s:\n",
1348 ((current_function_decl && DECL_NAME (current_function_decl))
1349 ? IDENTIFIER_POINTER (DECL_NAME (current_function_decl))
1350 : "<unknown>"));
1351
1352 fprintf (stderr, "\ttotal_size\t= %6d\n", info->total_size);
1353 fprintf (stderr, "\tvars_size\t= %6d\n", info->vars_size);
1354 fprintf (stderr, "\tparam_size\t= %6d\n", info->parameter_size);
1355 fprintf (stderr, "\tregs_size\t= %6d, 1w = %3d, 2w = %3d\n",
1356 info->regs_size, info->regs_size_1word, info->regs_size_2words);
1357
1358 fprintf (stderr, "\theader_size\t= %6d\n", info->header_size);
1359 fprintf (stderr, "\tpretend_size\t= %6d\n", info->pretend_size);
1360 fprintf (stderr, "\tvars_offset\t= %6d\n", info->vars_offset);
1361 fprintf (stderr, "\tregs_offset\t= %6d\n", info->regs_offset);
1362
1363 for (range = 0; range < STACK_REGS_MAX; range++)
1364 {
1365 frv_stack_regs_t *regs = &(info->regs[range]);
1366 if ((regs->size_1word + regs->size_2words) > 0)
1367 {
1368 int first = regs->first;
1369 int last = regs->last;
1370 int regno;
1371
1372 fprintf (stderr, "\t%s\tsize\t= %6d, 1w = %3d, 2w = %3d, save =",
1373 regs->name, regs->size_1word + regs->size_2words,
1374 regs->size_1word, regs->size_2words);
1375
1376 for (regno = first; regno <= last; regno++)
1377 {
1378 if (info->save_p[regno] == REG_SAVE_1WORD)
1379 fprintf (stderr, " %s (%d)", reg_names[regno],
1380 info->reg_offset[regno]);
1381
1382 else if (info->save_p[regno] == REG_SAVE_2WORDS)
1383 fprintf (stderr, " %s-%s (%d)", reg_names[regno],
1384 reg_names[regno+1], info->reg_offset[regno]);
1385 }
1386
1387 fputc ('\n', stderr);
1388 }
1389 }
1390
1391 fflush (stderr);
1392}
1393
1394
1395\f
1396
c557edf4
RS
1397/* Used during final to control the packing of insns. The value is
1398 1 if the current instruction should be packed with the next one,
1399 0 if it shouldn't or -1 if packing is disabled altogether. */
36a05131
BS
1400
1401static int frv_insn_packing_flag;
1402
1403/* True if the current function contains a far jump. */
1404
1405static int
f2206911 1406frv_function_contains_far_jump (void)
36a05131 1407{
b32d5189 1408 rtx_insn *insn = get_insns ();
36a05131 1409 while (insn != NULL
b64925dc 1410 && !(JUMP_P (insn)
36a05131
BS
1411 && get_attr_far_jump (insn) == FAR_JUMP_YES))
1412 insn = NEXT_INSN (insn);
1413 return (insn != NULL);
1414}
1415
1416/* For the FRV, this function makes sure that a function with far jumps
1417 will return correctly. It also does the VLIW packing. */
1418
1419static void
f2206911 1420frv_function_prologue (FILE *file, HOST_WIDE_INT size ATTRIBUTE_UNUSED)
36a05131 1421{
e6eda746 1422 rtx_insn *insn, *next, *last_call;
0fad9ab9 1423
36a05131
BS
1424 /* If no frame was created, check whether the function uses a call
1425 instruction to implement a far jump. If so, save the link in gr3 and
1426 replace all returns to LR with returns to GR3. GR3 is used because it
1427 is call-clobbered, because is not available to the register allocator,
1428 and because all functions that take a hidden argument pointer will have
1429 a stack frame. */
1430 if (frv_stack_info ()->total_size == 0 && frv_function_contains_far_jump ())
1431 {
e6eda746 1432 rtx_insn *insn;
36a05131
BS
1433
1434 /* Just to check that the above comment is true. */
6fb5fa3c 1435 gcc_assert (!df_regs_ever_live_p (GPR_FIRST + 3));
36a05131
BS
1436
1437 /* Generate the instruction that saves the link register. */
1438 fprintf (file, "\tmovsg lr,gr3\n");
1439
1440 /* Replace the LR with GR3 in *return_internal patterns. The insn
1441 will now return using jmpl @(gr3,0) rather than bralr. We cannot
1442 simply emit a different assembly directive because bralr and jmpl
1443 execute in different units. */
1444 for (insn = get_insns(); insn != NULL; insn = NEXT_INSN (insn))
b64925dc 1445 if (JUMP_P (insn))
36a05131
BS
1446 {
1447 rtx pattern = PATTERN (insn);
1448 if (GET_CODE (pattern) == PARALLEL
1449 && XVECLEN (pattern, 0) >= 2
1450 && GET_CODE (XVECEXP (pattern, 0, 0)) == RETURN
1451 && GET_CODE (XVECEXP (pattern, 0, 1)) == USE)
1452 {
1453 rtx address = XEXP (XVECEXP (pattern, 0, 1), 0);
1454 if (GET_CODE (address) == REG && REGNO (address) == LR_REGNO)
6fb5fa3c 1455 SET_REGNO (address, GPR_FIRST + 3);
36a05131
BS
1456 }
1457 }
1458 }
1459
1460 frv_pack_insns ();
c557edf4
RS
1461
1462 /* Allow the garbage collector to free the nops created by frv_reorg. */
1463 memset (frv_nops, 0, sizeof (frv_nops));
0fad9ab9
NC
1464
1465 /* Locate CALL_ARG_LOCATION notes that have been misplaced
1466 and move them back to where they should be located. */
e6eda746 1467 last_call = NULL;
0fad9ab9
NC
1468 for (insn = get_insns (); insn; insn = next)
1469 {
1470 next = NEXT_INSN (insn);
1471 if (CALL_P (insn)
1472 || (INSN_P (insn) && GET_CODE (PATTERN (insn)) == SEQUENCE
1473 && CALL_P (XVECEXP (PATTERN (insn), 0, 0))))
1474 last_call = insn;
1475
1476 if (!NOTE_P (insn) || NOTE_KIND (insn) != NOTE_INSN_CALL_ARG_LOCATION)
1477 continue;
1478
1479 if (NEXT_INSN (last_call) == insn)
1480 continue;
1481
0f82e5c9
DM
1482 SET_NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn);
1483 SET_PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn);
1484 SET_PREV_INSN (insn) = last_call;
1485 SET_NEXT_INSN (insn) = NEXT_INSN (last_call);
1486 SET_PREV_INSN (NEXT_INSN (insn)) = insn;
1487 SET_NEXT_INSN (PREV_INSN (insn)) = insn;
0fad9ab9
NC
1488 last_call = insn;
1489 }
36a05131
BS
1490}
1491
1492\f
1493/* Return the next available temporary register in a given class. */
1494
1495static rtx
f2206911
KC
1496frv_alloc_temp_reg (
1497 frv_tmp_reg_t *info, /* which registers are available */
0a2aaacc 1498 enum reg_class rclass, /* register class desired */
ef4bddc2 1499 machine_mode mode, /* mode to allocate register with */
f2206911
KC
1500 int mark_as_used, /* register not available after allocation */
1501 int no_abort) /* return NULL instead of aborting */
36a05131 1502{
0a2aaacc 1503 int regno = info->next_reg[ (int)rclass ];
36a05131 1504 int orig_regno = regno;
0a2aaacc 1505 HARD_REG_SET *reg_in_class = &reg_class_contents[ (int)rclass ];
36a05131
BS
1506 int i, nr;
1507
1508 for (;;)
1509 {
1510 if (TEST_HARD_REG_BIT (*reg_in_class, regno)
1511 && TEST_HARD_REG_BIT (info->regs, regno))
1512 break;
1513
1514 if (++regno >= FIRST_PSEUDO_REGISTER)
1515 regno = 0;
1516 if (regno == orig_regno)
1517 {
44e91694
NS
1518 gcc_assert (no_abort);
1519 return NULL_RTX;
36a05131
BS
1520 }
1521 }
1522
1523 nr = HARD_REGNO_NREGS (regno, mode);
0a2aaacc 1524 info->next_reg[ (int)rclass ] = regno + nr;
36a05131
BS
1525
1526 if (mark_as_used)
1527 for (i = 0; i < nr; i++)
1528 CLEAR_HARD_REG_BIT (info->regs, regno+i);
1529
1530 return gen_rtx_REG (mode, regno);
1531}
1532
1533\f
1534/* Return an rtx with the value OFFSET, which will either be a register or a
1535 signed 12-bit integer. It can be used as the second operand in an "add"
1536 instruction, or as the index in a load or store.
1537
1538 The function returns a constant rtx if OFFSET is small enough, otherwise
1539 it loads the constant into register OFFSET_REGNO and returns that. */
1540static rtx
f2206911 1541frv_frame_offset_rtx (int offset)
36a05131
BS
1542{
1543 rtx offset_rtx = GEN_INT (offset);
2f5b1308 1544 if (IN_RANGE (offset, -2048, 2047))
36a05131
BS
1545 return offset_rtx;
1546 else
1547 {
1548 rtx reg_rtx = gen_rtx_REG (SImode, OFFSET_REGNO);
2f5b1308 1549 if (IN_RANGE (offset, -32768, 32767))
36a05131
BS
1550 emit_insn (gen_movsi (reg_rtx, offset_rtx));
1551 else
1552 {
1553 emit_insn (gen_movsi_high (reg_rtx, offset_rtx));
1554 emit_insn (gen_movsi_lo_sum (reg_rtx, offset_rtx));
1555 }
1556 return reg_rtx;
1557 }
1558}
1559
1560/* Generate (mem:MODE (plus:Pmode BASE (frv_frame_offset OFFSET)))). The
1561 prologue and epilogue uses such expressions to access the stack. */
1562static rtx
ef4bddc2 1563frv_frame_mem (machine_mode mode, rtx base, int offset)
36a05131
BS
1564{
1565 return gen_rtx_MEM (mode, gen_rtx_PLUS (Pmode,
1566 base,
1567 frv_frame_offset_rtx (offset)));
1568}
1569
1570/* Generate a frame-related expression:
1571
1572 (set REG (mem (plus (sp) (const_int OFFSET)))).
1573
1574 Such expressions are used in FRAME_RELATED_EXPR notes for more complex
1575 instructions. Marking the expressions as frame-related is superfluous if
1576 the note contains just a single set. But if the note contains a PARALLEL
1577 or SEQUENCE that has several sets, each set must be individually marked
1578 as frame-related. */
1579static rtx
f2206911 1580frv_dwarf_store (rtx reg, int offset)
36a05131
BS
1581{
1582 rtx set = gen_rtx_SET (VOIDmode,
1583 gen_rtx_MEM (GET_MODE (reg),
0a81f074 1584 plus_constant (Pmode, stack_pointer_rtx,
36a05131
BS
1585 offset)),
1586 reg);
1587 RTX_FRAME_RELATED_P (set) = 1;
1588 return set;
1589}
1590
1591/* Emit a frame-related instruction whose pattern is PATTERN. The
1592 instruction is the last in a sequence that cumulatively performs the
1593 operation described by DWARF_PATTERN. The instruction is marked as
1594 frame-related and has a REG_FRAME_RELATED_EXPR note containing
1595 DWARF_PATTERN. */
1596static void
f2206911 1597frv_frame_insn (rtx pattern, rtx dwarf_pattern)
36a05131
BS
1598{
1599 rtx insn = emit_insn (pattern);
1600 RTX_FRAME_RELATED_P (insn) = 1;
1601 REG_NOTES (insn) = alloc_EXPR_LIST (REG_FRAME_RELATED_EXPR,
1602 dwarf_pattern,
1603 REG_NOTES (insn));
1604}
1605
1606/* Emit instructions that transfer REG to or from the memory location (sp +
1607 STACK_OFFSET). The register is stored in memory if ACCESSOR->OP is
1608 FRV_STORE and loaded if it is FRV_LOAD. Only the prologue uses this
1609 function to store registers and only the epilogue uses it to load them.
1610
1611 The caller sets up ACCESSOR so that BASE is equal to (sp + BASE_OFFSET).
1612 The generated instruction will use BASE as its base register. BASE may
1613 simply be the stack pointer, but if several accesses are being made to a
1614 region far away from the stack pointer, it may be more efficient to set
1615 up a temporary instead.
b16c1435 1616
36a05131
BS
1617 Store instructions will be frame-related and will be annotated with the
1618 overall effect of the store. Load instructions will be followed by a
1619 (use) to prevent later optimizations from zapping them.
1620
1621 The function takes care of the moves to and from SPRs, using TEMP_REGNO
1622 as a temporary in such cases. */
1623static void
f2206911 1624frv_frame_access (frv_frame_accessor_t *accessor, rtx reg, int stack_offset)
36a05131 1625{
ef4bddc2 1626 machine_mode mode = GET_MODE (reg);
36a05131
BS
1627 rtx mem = frv_frame_mem (mode,
1628 accessor->base,
1629 stack_offset - accessor->base_offset);
1630
1631 if (accessor->op == FRV_LOAD)
1632 {
1633 if (SPR_P (REGNO (reg)))
1634 {
1635 rtx temp = gen_rtx_REG (mode, TEMP_REGNO);
1636 emit_insn (gen_rtx_SET (VOIDmode, temp, mem));
1637 emit_insn (gen_rtx_SET (VOIDmode, reg, temp));
1638 }
1639 else
8d8256c1
NC
1640 {
1641 /* We cannot use reg+reg addressing for DImode access. */
1642 if (mode == DImode
1643 && GET_CODE (XEXP (mem, 0)) == PLUS
1644 && GET_CODE (XEXP (XEXP (mem, 0), 0)) == REG
1645 && GET_CODE (XEXP (XEXP (mem, 0), 1)) == REG)
1646 {
1647 rtx temp = gen_rtx_REG (SImode, TEMP_REGNO);
5c5e8419
JR
1648
1649 emit_move_insn (temp,
1650 gen_rtx_PLUS (SImode, XEXP (XEXP (mem, 0), 0),
1651 XEXP (XEXP (mem, 0), 1)));
8d8256c1
NC
1652 mem = gen_rtx_MEM (DImode, temp);
1653 }
1654 emit_insn (gen_rtx_SET (VOIDmode, reg, mem));
1655 }
c41c1387 1656 emit_use (reg);
36a05131
BS
1657 }
1658 else
1659 {
1660 if (SPR_P (REGNO (reg)))
1661 {
1662 rtx temp = gen_rtx_REG (mode, TEMP_REGNO);
1663 emit_insn (gen_rtx_SET (VOIDmode, temp, reg));
1664 frv_frame_insn (gen_rtx_SET (Pmode, mem, temp),
1665 frv_dwarf_store (reg, stack_offset));
1666 }
8d8256c1 1667 else if (mode == DImode)
36a05131
BS
1668 {
1669 /* For DImode saves, the dwarf2 version needs to be a SEQUENCE
1670 with a separate save for each register. */
1671 rtx reg1 = gen_rtx_REG (SImode, REGNO (reg));
1672 rtx reg2 = gen_rtx_REG (SImode, REGNO (reg) + 1);
1673 rtx set1 = frv_dwarf_store (reg1, stack_offset);
1674 rtx set2 = frv_dwarf_store (reg2, stack_offset + 4);
8d8256c1
NC
1675
1676 /* Also we cannot use reg+reg addressing. */
1677 if (GET_CODE (XEXP (mem, 0)) == PLUS
1678 && GET_CODE (XEXP (XEXP (mem, 0), 0)) == REG
1679 && GET_CODE (XEXP (XEXP (mem, 0), 1)) == REG)
1680 {
1681 rtx temp = gen_rtx_REG (SImode, TEMP_REGNO);
5c5e8419
JR
1682 emit_move_insn (temp,
1683 gen_rtx_PLUS (SImode, XEXP (XEXP (mem, 0), 0),
1684 XEXP (XEXP (mem, 0), 1)));
8d8256c1
NC
1685 mem = gen_rtx_MEM (DImode, temp);
1686 }
1687
36a05131
BS
1688 frv_frame_insn (gen_rtx_SET (Pmode, mem, reg),
1689 gen_rtx_PARALLEL (VOIDmode,
1690 gen_rtvec (2, set1, set2)));
1691 }
1692 else
1693 frv_frame_insn (gen_rtx_SET (Pmode, mem, reg),
1694 frv_dwarf_store (reg, stack_offset));
1695 }
1696}
1697
1698/* A function that uses frv_frame_access to transfer a group of registers to
1699 or from the stack. ACCESSOR is passed directly to frv_frame_access, INFO
1700 is the stack information generated by frv_stack_info, and REG_SET is the
1701 number of the register set to transfer. */
1702static void
f2206911
KC
1703frv_frame_access_multi (frv_frame_accessor_t *accessor,
1704 frv_stack_t *info,
1705 int reg_set)
36a05131
BS
1706{
1707 frv_stack_regs_t *regs_info;
1708 int regno;
1709
1710 regs_info = &info->regs[reg_set];
1711 for (regno = regs_info->first; regno <= regs_info->last; regno++)
1712 if (info->save_p[regno])
1713 frv_frame_access (accessor,
1714 info->save_p[regno] == REG_SAVE_2WORDS
1715 ? gen_rtx_REG (DImode, regno)
1716 : gen_rtx_REG (SImode, regno),
1717 info->reg_offset[regno]);
1718}
1719
1720/* Save or restore callee-saved registers that are kept outside the frame
1721 header. The function saves the registers if OP is FRV_STORE and restores
1722 them if OP is FRV_LOAD. INFO is the stack information generated by
1723 frv_stack_info. */
1724static void
f2206911 1725frv_frame_access_standard_regs (enum frv_stack_op op, frv_stack_t *info)
36a05131
BS
1726{
1727 frv_frame_accessor_t accessor;
1728
1729 accessor.op = op;
1730 accessor.base = stack_pointer_rtx;
1731 accessor.base_offset = 0;
1732 frv_frame_access_multi (&accessor, info, STACK_REGS_GPR);
1733 frv_frame_access_multi (&accessor, info, STACK_REGS_FPR);
1734 frv_frame_access_multi (&accessor, info, STACK_REGS_LCR);
b16c1435 1735}
36a05131
BS
1736
1737
1738/* Called after register allocation to add any instructions needed for the
1739 prologue. Using a prologue insn is favored compared to putting all of the
b88cf82e
KH
1740 instructions in the TARGET_ASM_FUNCTION_PROLOGUE target hook, since
1741 it allows the scheduler to intermix instructions with the saves of
1742 the caller saved registers. In some cases, it might be necessary
1743 to emit a barrier instruction as the last insn to prevent such
1744 scheduling.
36a05131
BS
1745
1746 Also any insns generated here should have RTX_FRAME_RELATED_P(insn) = 1
1747 so that the debug info generation code can handle them properly. */
1748void
f2206911 1749frv_expand_prologue (void)
36a05131
BS
1750{
1751 frv_stack_t *info = frv_stack_info ();
1752 rtx sp = stack_pointer_rtx;
1753 rtx fp = frame_pointer_rtx;
1754 frv_frame_accessor_t accessor;
1755
1756 if (TARGET_DEBUG_STACK)
1757 frv_debug_stack (info);
1758
7027164c
NC
1759 if (flag_stack_usage_info)
1760 current_function_static_stack_size = info->total_size;
1761
36a05131
BS
1762 if (info->total_size == 0)
1763 return;
1764
1765 /* We're interested in three areas of the frame here:
1766
1767 A: the register save area
1768 B: the old FP
1769 C: the header after B
1770
1771 If the frame pointer isn't used, we'll have to set up A, B and C
1772 using the stack pointer. If the frame pointer is used, we'll access
1773 them as follows:
1774
1775 A: set up using sp
1776 B: set up using sp or a temporary (see below)
1777 C: set up using fp
1778
1779 We set up B using the stack pointer if the frame is small enough.
1780 Otherwise, it's more efficient to copy the old stack pointer into a
1781 temporary and use that.
1782
1783 Note that it's important to make sure the prologue and epilogue use the
1784 same registers to access A and C, since doing otherwise will confuse
1785 the aliasing code. */
1786
1787 /* Set up ACCESSOR for accessing region B above. If the frame pointer
1788 isn't used, the same method will serve for C. */
1789 accessor.op = FRV_STORE;
1790 if (frame_pointer_needed && info->total_size > 2048)
1791 {
36a05131
BS
1792 accessor.base = gen_rtx_REG (Pmode, OLD_SP_REGNO);
1793 accessor.base_offset = info->total_size;
5c5e8419 1794 emit_insn (gen_movsi (accessor.base, sp));
36a05131
BS
1795 }
1796 else
1797 {
1798 accessor.base = stack_pointer_rtx;
1799 accessor.base_offset = 0;
1800 }
1801
1802 /* Allocate the stack space. */
1803 {
1804 rtx asm_offset = frv_frame_offset_rtx (-info->total_size);
1805 rtx dwarf_offset = GEN_INT (-info->total_size);
1806
1807 frv_frame_insn (gen_stack_adjust (sp, sp, asm_offset),
1808 gen_rtx_SET (Pmode,
1809 sp,
1810 gen_rtx_PLUS (Pmode, sp, dwarf_offset)));
1811 }
1812
1813 /* If the frame pointer is needed, store the old one at (sp + FP_OFFSET)
1814 and point the new one to that location. */
1815 if (frame_pointer_needed)
1816 {
1817 int fp_offset = info->reg_offset[FRAME_POINTER_REGNUM];
1818
1819 /* ASM_SRC and DWARF_SRC both point to the frame header. ASM_SRC is
1820 based on ACCESSOR.BASE but DWARF_SRC is always based on the stack
1821 pointer. */
0a81f074 1822 rtx asm_src = plus_constant (Pmode, accessor.base,
36a05131 1823 fp_offset - accessor.base_offset);
0a81f074 1824 rtx dwarf_src = plus_constant (Pmode, sp, fp_offset);
36a05131
BS
1825
1826 /* Store the old frame pointer at (sp + FP_OFFSET). */
1827 frv_frame_access (&accessor, fp, fp_offset);
1828
1829 /* Set up the new frame pointer. */
1830 frv_frame_insn (gen_rtx_SET (VOIDmode, fp, asm_src),
1831 gen_rtx_SET (VOIDmode, fp, dwarf_src));
1832
1833 /* Access region C from the frame pointer. */
1834 accessor.base = fp;
1835 accessor.base_offset = fp_offset;
1836 }
1837
1838 /* Set up region C. */
1839 frv_frame_access_multi (&accessor, info, STACK_REGS_STRUCT);
1840 frv_frame_access_multi (&accessor, info, STACK_REGS_LR);
1841 frv_frame_access_multi (&accessor, info, STACK_REGS_STDARG);
1842
1843 /* Set up region A. */
1844 frv_frame_access_standard_regs (FRV_STORE, info);
1845
1846 /* If this is a varargs/stdarg function, issue a blockage to prevent the
1847 scheduler from moving loads before the stores saving the registers. */
1848 if (info->stdarg_size > 0)
1849 emit_insn (gen_blockage ());
1850
87b483a1 1851 /* Set up pic register/small data register for this function. */
ad516a74 1852 if (!TARGET_FDPIC && flag_pic && crtl->uses_pic_offset_table)
36a05131
BS
1853 emit_insn (gen_pic_prologue (gen_rtx_REG (Pmode, PIC_REGNO),
1854 gen_rtx_REG (Pmode, LR_REGNO),
1855 gen_rtx_REG (SImode, OFFSET_REGNO)));
1856}
1857
1858\f
1859/* Under frv, all of the work is done via frv_expand_epilogue, but
839a4992 1860 this function provides a convenient place to do cleanup. */
36a05131
BS
1861
1862static void
f2206911
KC
1863frv_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
1864 HOST_WIDE_INT size ATTRIBUTE_UNUSED)
36a05131
BS
1865{
1866 frv_stack_cache = (frv_stack_t *)0;
1867
87b483a1 1868 /* Zap last used registers for conditional execution. */
fad205ff 1869 memset (&frv_ifcvt.tmp_reg, 0, sizeof (frv_ifcvt.tmp_reg));
36a05131 1870
87b483a1 1871 /* Release the bitmap of created insns. */
7b210806 1872 BITMAP_FREE (frv_ifcvt.scratch_insns_bitmap);
36a05131
BS
1873}
1874
1875\f
1876/* Called after register allocation to add any instructions needed for the
43aa4e05 1877 epilogue. Using an epilogue insn is favored compared to putting all of the
b88cf82e
KH
1878 instructions in the TARGET_ASM_FUNCTION_PROLOGUE target hook, since
1879 it allows the scheduler to intermix instructions with the saves of
1880 the caller saved registers. In some cases, it might be necessary
1881 to emit a barrier instruction as the last insn to prevent such
c557edf4 1882 scheduling. */
36a05131
BS
1883
1884void
764678d1 1885frv_expand_epilogue (bool emit_return)
36a05131
BS
1886{
1887 frv_stack_t *info = frv_stack_info ();
1888 rtx fp = frame_pointer_rtx;
1889 rtx sp = stack_pointer_rtx;
1890 rtx return_addr;
1891 int fp_offset;
1892
1893 fp_offset = info->reg_offset[FRAME_POINTER_REGNUM];
1894
1895 /* Restore the stack pointer to its original value if alloca or the like
1896 is used. */
416ff32e 1897 if (! crtl->sp_is_unchanging)
36a05131
BS
1898 emit_insn (gen_addsi3 (sp, fp, frv_frame_offset_rtx (-fp_offset)));
1899
1900 /* Restore the callee-saved registers that were used in this function. */
1901 frv_frame_access_standard_regs (FRV_LOAD, info);
1902
1903 /* Set RETURN_ADDR to the address we should return to. Set it to NULL if
1904 no return instruction should be emitted. */
764678d1 1905 if (info->save_p[LR_REGNO])
36a05131
BS
1906 {
1907 int lr_offset;
1908 rtx mem;
1909
1910 /* Use the same method to access the link register's slot as we did in
1911 the prologue. In other words, use the frame pointer if available,
1912 otherwise use the stack pointer.
1913
1914 LR_OFFSET is the offset of the link register's slot from the start
1915 of the frame and MEM is a memory rtx for it. */
1916 lr_offset = info->reg_offset[LR_REGNO];
1917 if (frame_pointer_needed)
1918 mem = frv_frame_mem (Pmode, fp, lr_offset - fp_offset);
1919 else
1920 mem = frv_frame_mem (Pmode, sp, lr_offset);
1921
1922 /* Load the old link register into a GPR. */
1923 return_addr = gen_rtx_REG (Pmode, TEMP_REGNO);
1924 emit_insn (gen_rtx_SET (VOIDmode, return_addr, mem));
1925 }
1926 else
1927 return_addr = gen_rtx_REG (Pmode, LR_REGNO);
1928
1929 /* Restore the old frame pointer. Emit a USE afterwards to make sure
1930 the load is preserved. */
1931 if (frame_pointer_needed)
1932 {
1933 emit_insn (gen_rtx_SET (VOIDmode, fp, gen_rtx_MEM (Pmode, fp)));
c41c1387 1934 emit_use (fp);
36a05131
BS
1935 }
1936
1937 /* Deallocate the stack frame. */
1938 if (info->total_size != 0)
1939 {
1940 rtx offset = frv_frame_offset_rtx (info->total_size);
1941 emit_insn (gen_stack_adjust (sp, sp, offset));
1942 }
1943
1944 /* If this function uses eh_return, add the final stack adjustment now. */
e3b5732b 1945 if (crtl->calls_eh_return)
36a05131
BS
1946 emit_insn (gen_stack_adjust (sp, sp, EH_RETURN_STACKADJ_RTX));
1947
764678d1 1948 if (emit_return)
36a05131 1949 emit_jump_insn (gen_epilogue_return (return_addr));
764678d1
AO
1950 else
1951 {
1952 rtx lr = return_addr;
1953
1954 if (REGNO (return_addr) != LR_REGNO)
1955 {
1956 lr = gen_rtx_REG (Pmode, LR_REGNO);
1957 emit_move_insn (lr, return_addr);
1958 }
1959
c41c1387 1960 emit_use (lr);
764678d1 1961 }
36a05131
BS
1962}
1963
1964\f
b88cf82e 1965/* Worker function for TARGET_ASM_OUTPUT_MI_THUNK. */
36a05131 1966
c590b625 1967static void
f2206911
KC
1968frv_asm_output_mi_thunk (FILE *file,
1969 tree thunk_fndecl ATTRIBUTE_UNUSED,
1970 HOST_WIDE_INT delta,
1971 HOST_WIDE_INT vcall_offset ATTRIBUTE_UNUSED,
1972 tree function)
36a05131
BS
1973{
1974 const char *name_func = XSTR (XEXP (DECL_RTL (function), 0), 0);
1975 const char *name_arg0 = reg_names[FIRST_ARG_REGNUM];
1976 const char *name_jmp = reg_names[JUMP_REGNO];
c557edf4 1977 const char *parallel = (frv_issue_rate () > 1 ? ".p" : "");
36a05131 1978
87b483a1 1979 /* Do the add using an addi if possible. */
2f5b1308 1980 if (IN_RANGE (delta, -2048, 2047))
eb0424da 1981 fprintf (file, "\taddi %s,#%d,%s\n", name_arg0, (int) delta, name_arg0);
36a05131
BS
1982 else
1983 {
4a0a75dd
KG
1984 const char *const name_add = reg_names[TEMP_REGNO];
1985 fprintf (file, "\tsethi%s #hi(" HOST_WIDE_INT_PRINT_DEC "),%s\n",
1986 parallel, delta, name_add);
1987 fprintf (file, "\tsetlo #lo(" HOST_WIDE_INT_PRINT_DEC "),%s\n",
1988 delta, name_add);
36a05131
BS
1989 fprintf (file, "\tadd %s,%s,%s\n", name_add, name_arg0, name_arg0);
1990 }
1991
34208acf
AO
1992 if (TARGET_FDPIC)
1993 {
1994 const char *name_pic = reg_names[FDPIC_REGNO];
1995 name_jmp = reg_names[FDPIC_FPTR_REGNO];
1996
1997 if (flag_pic != 1)
1998 {
1999 fprintf (file, "\tsethi%s #gotofffuncdeschi(", parallel);
2000 assemble_name (file, name_func);
2001 fprintf (file, "),%s\n", name_jmp);
2002
2003 fprintf (file, "\tsetlo #gotofffuncdesclo(");
2004 assemble_name (file, name_func);
2005 fprintf (file, "),%s\n", name_jmp);
2006
2007 fprintf (file, "\tldd @(%s,%s), %s\n", name_jmp, name_pic, name_jmp);
2008 }
2009 else
2010 {
2011 fprintf (file, "\tlddo @(%s,#gotofffuncdesc12(", name_pic);
2012 assemble_name (file, name_func);
2013 fprintf (file, "\t)), %s\n", name_jmp);
2014 }
2015 }
2016 else if (!flag_pic)
36a05131
BS
2017 {
2018 fprintf (file, "\tsethi%s #hi(", parallel);
2019 assemble_name (file, name_func);
2020 fprintf (file, "),%s\n", name_jmp);
2021
2022 fprintf (file, "\tsetlo #lo(");
2023 assemble_name (file, name_func);
2024 fprintf (file, "),%s\n", name_jmp);
2025 }
2026 else
2027 {
2028 /* Use JUMP_REGNO as a temporary PIC register. */
2029 const char *name_lr = reg_names[LR_REGNO];
2030 const char *name_gppic = name_jmp;
2031 const char *name_tmp = reg_names[TEMP_REGNO];
2032
2033 fprintf (file, "\tmovsg %s,%s\n", name_lr, name_tmp);
2034 fprintf (file, "\tcall 1f\n");
2035 fprintf (file, "1:\tmovsg %s,%s\n", name_lr, name_gppic);
2036 fprintf (file, "\tmovgs %s,%s\n", name_tmp, name_lr);
2037 fprintf (file, "\tsethi%s #gprelhi(1b),%s\n", parallel, name_tmp);
2038 fprintf (file, "\tsetlo #gprello(1b),%s\n", name_tmp);
2039 fprintf (file, "\tsub %s,%s,%s\n", name_gppic, name_tmp, name_gppic);
2040
2041 fprintf (file, "\tsethi%s #gprelhi(", parallel);
2042 assemble_name (file, name_func);
2043 fprintf (file, "),%s\n", name_tmp);
2044
2045 fprintf (file, "\tsetlo #gprello(");
2046 assemble_name (file, name_func);
2047 fprintf (file, "),%s\n", name_tmp);
2048
2049 fprintf (file, "\tadd %s,%s,%s\n", name_gppic, name_tmp, name_jmp);
2050 }
2051
87b483a1 2052 /* Jump to the function address. */
36a05131
BS
2053 fprintf (file, "\tjmpl @(%s,%s)\n", name_jmp, reg_names[GPR_FIRST+0]);
2054}
2055
2056\f
36a05131 2057
87b483a1 2058/* On frv, create a frame whenever we need to create stack. */
36a05131 2059
b52b1749 2060static bool
f2206911 2061frv_frame_pointer_required (void)
36a05131 2062{
34208acf
AO
2063 /* If we forgoing the usual linkage requirements, we only need
2064 a frame pointer if the stack pointer might change. */
2065 if (!TARGET_LINKED_FP)
416ff32e 2066 return !crtl->sp_is_unchanging;
34208acf 2067
416ff32e 2068 if (! crtl->is_leaf)
b52b1749 2069 return true;
36a05131
BS
2070
2071 if (get_frame_size () != 0)
b52b1749 2072 return true;
36a05131
BS
2073
2074 if (cfun->stdarg)
b52b1749 2075 return true;
36a05131 2076
416ff32e 2077 if (!crtl->sp_is_unchanging)
b52b1749 2078 return true;
36a05131 2079
ad516a74 2080 if (!TARGET_FDPIC && flag_pic && crtl->uses_pic_offset_table)
b52b1749 2081 return true;
36a05131
BS
2082
2083 if (profile_flag)
b52b1749 2084 return true;
36a05131
BS
2085
2086 if (cfun->machine->frame_needed)
b52b1749 2087 return true;
36a05131 2088
b52b1749 2089 return false;
36a05131
BS
2090}
2091
2092\f
7b5cbb57
AS
2093/* Worker function for TARGET_CAN_ELIMINATE. */
2094
2095bool
2096frv_can_eliminate (const int from, const int to)
2097{
2098 return (from == ARG_POINTER_REGNUM && to == STACK_POINTER_REGNUM
2099 ? ! frame_pointer_needed
2100 : true);
2101}
2102
36a05131
BS
2103/* This macro is similar to `INITIAL_FRAME_POINTER_OFFSET'. It specifies the
2104 initial difference between the specified pair of registers. This macro must
2105 be defined if `ELIMINABLE_REGS' is defined. */
2106
2107/* See frv_stack_info for more details on the frv stack frame. */
2108
2109int
f2206911 2110frv_initial_elimination_offset (int from, int to)
36a05131
BS
2111{
2112 frv_stack_t *info = frv_stack_info ();
2113 int ret = 0;
2114
2115 if (to == STACK_POINTER_REGNUM && from == ARG_POINTER_REGNUM)
2116 ret = info->total_size - info->pretend_size;
2117
2118 else if (to == STACK_POINTER_REGNUM && from == FRAME_POINTER_REGNUM)
88d6a75f 2119 ret = info->reg_offset[FRAME_POINTER_REGNUM];
36a05131
BS
2120
2121 else if (to == FRAME_POINTER_REGNUM && from == ARG_POINTER_REGNUM)
2122 ret = (info->total_size
2123 - info->reg_offset[FRAME_POINTER_REGNUM]
2124 - info->pretend_size);
2125
2126 else
44e91694 2127 gcc_unreachable ();
36a05131
BS
2128
2129 if (TARGET_DEBUG_STACK)
2130 fprintf (stderr, "Eliminate %s to %s by adding %d\n",
2131 reg_names [from], reg_names[to], ret);
2132
2133 return ret;
2134}
2135
2136\f
d8c2bed3 2137/* Worker function for TARGET_SETUP_INCOMING_VARARGS. */
36a05131 2138
d8c2bed3 2139static void
d5cc9181 2140frv_setup_incoming_varargs (cumulative_args_t cum_v,
ef4bddc2 2141 machine_mode mode,
f2206911
KC
2142 tree type ATTRIBUTE_UNUSED,
2143 int *pretend_size,
2144 int second_time)
36a05131 2145{
d5cc9181
JR
2146 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
2147
36a05131
BS
2148 if (TARGET_DEBUG_ARG)
2149 fprintf (stderr,
2150 "setup_vararg: words = %2d, mode = %4s, pretend_size = %d, second_time = %d\n",
2151 *cum, GET_MODE_NAME (mode), *pretend_size, second_time);
2152}
2153
2154\f
b88cf82e 2155/* Worker function for TARGET_EXPAND_BUILTIN_SAVEREGS. */
36a05131 2156
8ac411c7 2157static rtx
f2206911 2158frv_expand_builtin_saveregs (void)
36a05131
BS
2159{
2160 int offset = UNITS_PER_WORD * FRV_NUM_ARG_REGS;
2161
2162 if (TARGET_DEBUG_ARG)
2163 fprintf (stderr, "expand_builtin_saveregs: offset from ap = %d\n",
2164 offset);
2165
f1c25d3b 2166 return gen_rtx_PLUS (Pmode, virtual_incoming_args_rtx, GEN_INT (- offset));
36a05131
BS
2167}
2168
2169\f
2170/* Expand __builtin_va_start to do the va_start macro. */
2171
d7bd8aeb 2172static void
f2206911 2173frv_expand_builtin_va_start (tree valist, rtx nextarg)
36a05131
BS
2174{
2175 tree t;
7dd68986 2176 int num = crtl->args.info - FIRST_ARG_REGNUM - FRV_NUM_ARG_REGS;
36a05131
BS
2177
2178 nextarg = gen_rtx_PLUS (Pmode, virtual_incoming_args_rtx,
2179 GEN_INT (UNITS_PER_WORD * num));
2180
2181 if (TARGET_DEBUG_ARG)
2182 {
2183 fprintf (stderr, "va_start: args_info = %d, num = %d\n",
7dd68986 2184 crtl->args.info, num);
36a05131
BS
2185
2186 debug_rtx (nextarg);
2187 }
2188
726a989a 2189 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist,
5be014d5
AP
2190 fold_convert (TREE_TYPE (valist),
2191 make_tree (sizetype, nextarg)));
36a05131
BS
2192 TREE_SIDE_EFFECTS (t) = 1;
2193
2194 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
2195}
2196
36a05131
BS
2197\f
2198/* Expand a block move operation, and return 1 if successful. Return 0
2199 if we should let the compiler generate normal code.
2200
2201 operands[0] is the destination
2202 operands[1] is the source
2203 operands[2] is the length
2204 operands[3] is the alignment */
2205
2206/* Maximum number of loads to do before doing the stores */
2207#ifndef MAX_MOVE_REG
2208#define MAX_MOVE_REG 4
2209#endif
2210
2211/* Maximum number of total loads to do. */
2212#ifndef TOTAL_MOVE_REG
2213#define TOTAL_MOVE_REG 8
2214#endif
2215
2216int
f2206911 2217frv_expand_block_move (rtx operands[])
36a05131
BS
2218{
2219 rtx orig_dest = operands[0];
2220 rtx orig_src = operands[1];
2221 rtx bytes_rtx = operands[2];
2222 rtx align_rtx = operands[3];
2223 int constp = (GET_CODE (bytes_rtx) == CONST_INT);
2224 int align;
2225 int bytes;
2226 int offset;
2227 int num_reg;
2228 int i;
2229 rtx src_reg;
2230 rtx dest_reg;
2231 rtx src_addr;
2232 rtx dest_addr;
2233 rtx src_mem;
2234 rtx dest_mem;
2235 rtx tmp_reg;
2236 rtx stores[MAX_MOVE_REG];
2237 int move_bytes;
ef4bddc2 2238 machine_mode mode;
36a05131 2239
87b483a1 2240 /* If this is not a fixed size move, just call memcpy. */
36a05131
BS
2241 if (! constp)
2242 return FALSE;
2243
44e91694
NS
2244 /* This should be a fixed size alignment. */
2245 gcc_assert (GET_CODE (align_rtx) == CONST_INT);
36a05131
BS
2246
2247 align = INTVAL (align_rtx);
2248
2249 /* Anything to move? */
2250 bytes = INTVAL (bytes_rtx);
2251 if (bytes <= 0)
2252 return TRUE;
2253
2254 /* Don't support real large moves. */
2255 if (bytes > TOTAL_MOVE_REG*align)
2256 return FALSE;
2257
2258 /* Move the address into scratch registers. */
2259 dest_reg = copy_addr_to_reg (XEXP (orig_dest, 0));
2260 src_reg = copy_addr_to_reg (XEXP (orig_src, 0));
2261
2262 num_reg = offset = 0;
2263 for ( ; bytes > 0; (bytes -= move_bytes), (offset += move_bytes))
2264 {
87b483a1 2265 /* Calculate the correct offset for src/dest. */
36a05131
BS
2266 if (offset == 0)
2267 {
2268 src_addr = src_reg;
2269 dest_addr = dest_reg;
2270 }
2271 else
2272 {
0a81f074
RS
2273 src_addr = plus_constant (Pmode, src_reg, offset);
2274 dest_addr = plus_constant (Pmode, dest_reg, offset);
36a05131
BS
2275 }
2276
2277 /* Generate the appropriate load and store, saving the stores
2278 for later. */
2279 if (bytes >= 4 && align >= 4)
2280 mode = SImode;
2281 else if (bytes >= 2 && align >= 2)
2282 mode = HImode;
2283 else
2284 mode = QImode;
2285
2286 move_bytes = GET_MODE_SIZE (mode);
2287 tmp_reg = gen_reg_rtx (mode);
2288 src_mem = change_address (orig_src, mode, src_addr);
2289 dest_mem = change_address (orig_dest, mode, dest_addr);
2290 emit_insn (gen_rtx_SET (VOIDmode, tmp_reg, src_mem));
2291 stores[num_reg++] = gen_rtx_SET (VOIDmode, dest_mem, tmp_reg);
2292
2293 if (num_reg >= MAX_MOVE_REG)
2294 {
2295 for (i = 0; i < num_reg; i++)
2296 emit_insn (stores[i]);
2297 num_reg = 0;
2298 }
2299 }
2300
2301 for (i = 0; i < num_reg; i++)
2302 emit_insn (stores[i]);
2303
2304 return TRUE;
2305}
2306
2307\f
2308/* Expand a block clear operation, and return 1 if successful. Return 0
2309 if we should let the compiler generate normal code.
2310
2311 operands[0] is the destination
2312 operands[1] is the length
57e84f18 2313 operands[3] is the alignment */
36a05131
BS
2314
2315int
f2206911 2316frv_expand_block_clear (rtx operands[])
36a05131
BS
2317{
2318 rtx orig_dest = operands[0];
2319 rtx bytes_rtx = operands[1];
57e84f18 2320 rtx align_rtx = operands[3];
36a05131
BS
2321 int constp = (GET_CODE (bytes_rtx) == CONST_INT);
2322 int align;
2323 int bytes;
2324 int offset;
36a05131
BS
2325 rtx dest_reg;
2326 rtx dest_addr;
2327 rtx dest_mem;
2328 int clear_bytes;
ef4bddc2 2329 machine_mode mode;
36a05131 2330
87b483a1 2331 /* If this is not a fixed size move, just call memcpy. */
36a05131
BS
2332 if (! constp)
2333 return FALSE;
2334
44e91694
NS
2335 /* This should be a fixed size alignment. */
2336 gcc_assert (GET_CODE (align_rtx) == CONST_INT);
36a05131
BS
2337
2338 align = INTVAL (align_rtx);
2339
2340 /* Anything to move? */
2341 bytes = INTVAL (bytes_rtx);
2342 if (bytes <= 0)
2343 return TRUE;
2344
2345 /* Don't support real large clears. */
2346 if (bytes > TOTAL_MOVE_REG*align)
2347 return FALSE;
2348
2349 /* Move the address into a scratch register. */
2350 dest_reg = copy_addr_to_reg (XEXP (orig_dest, 0));
2351
5c5e8419 2352 offset = 0;
36a05131
BS
2353 for ( ; bytes > 0; (bytes -= clear_bytes), (offset += clear_bytes))
2354 {
87b483a1 2355 /* Calculate the correct offset for src/dest. */
36a05131
BS
2356 dest_addr = ((offset == 0)
2357 ? dest_reg
0a81f074 2358 : plus_constant (Pmode, dest_reg, offset));
36a05131 2359
87b483a1 2360 /* Generate the appropriate store of gr0. */
36a05131
BS
2361 if (bytes >= 4 && align >= 4)
2362 mode = SImode;
2363 else if (bytes >= 2 && align >= 2)
2364 mode = HImode;
2365 else
2366 mode = QImode;
2367
2368 clear_bytes = GET_MODE_SIZE (mode);
2369 dest_mem = change_address (orig_dest, mode, dest_addr);
2370 emit_insn (gen_rtx_SET (VOIDmode, dest_mem, const0_rtx));
2371 }
2372
2373 return TRUE;
2374}
2375
2376\f
2377/* The following variable is used to output modifiers of assembler
87b483a1 2378 code of the current output insn. */
36a05131
BS
2379
2380static rtx *frv_insn_operands;
2381
2382/* The following function is used to add assembler insn code suffix .p
87b483a1 2383 if it is necessary. */
36a05131
BS
2384
2385const char *
f2206911 2386frv_asm_output_opcode (FILE *f, const char *ptr)
36a05131
BS
2387{
2388 int c;
2389
c557edf4 2390 if (frv_insn_packing_flag <= 0)
36a05131
BS
2391 return ptr;
2392
2393 for (; *ptr && *ptr != ' ' && *ptr != '\t';)
2394 {
2395 c = *ptr++;
2396 if (c == '%' && ((*ptr >= 'a' && *ptr <= 'z')
2397 || (*ptr >= 'A' && *ptr <= 'Z')))
2398 {
2399 int letter = *ptr++;
2400
2401 c = atoi (ptr);
2402 frv_print_operand (f, frv_insn_operands [c], letter);
2403 while ((c = *ptr) >= '0' && c <= '9')
2404 ptr++;
2405 }
2406 else
2407 fputc (c, f);
2408 }
2409
c557edf4 2410 fprintf (f, ".p");
36a05131
BS
2411
2412 return ptr;
2413}
2414
c557edf4
RS
2415/* Set up the packing bit for the current output insn. Note that this
2416 function is not called for asm insns. */
36a05131
BS
2417
2418void
647d790d 2419frv_final_prescan_insn (rtx_insn *insn, rtx *opvec,
c557edf4 2420 int noperands ATTRIBUTE_UNUSED)
36a05131 2421{
c557edf4 2422 if (INSN_P (insn))
36a05131 2423 {
c557edf4
RS
2424 if (frv_insn_packing_flag >= 0)
2425 {
2426 frv_insn_operands = opvec;
2427 frv_insn_packing_flag = PACKING_FLAG_P (insn);
2428 }
2429 else if (recog_memoized (insn) >= 0
2430 && get_attr_acc_group (insn) == ACC_GROUP_ODD)
2431 /* Packing optimizations have been disabled, but INSN can only
2432 be issued in M1. Insert an mnop in M0. */
2433 fprintf (asm_out_file, "\tmnop.p\n");
36a05131 2434 }
36a05131
BS
2435}
2436
2437
2438\f
2439/* A C expression whose value is RTL representing the address in a stack frame
2440 where the pointer to the caller's frame is stored. Assume that FRAMEADDR is
2441 an RTL expression for the address of the stack frame itself.
2442
2443 If you don't define this macro, the default is to return the value of
2444 FRAMEADDR--that is, the stack frame address is also the address of the stack
2445 word that points to the previous frame. */
2446
2447/* The default is correct, but we need to make sure the frame gets created. */
2448rtx
f2206911 2449frv_dynamic_chain_address (rtx frame)
36a05131
BS
2450{
2451 cfun->machine->frame_needed = 1;
2452 return frame;
2453}
2454
2455
2456/* A C expression whose value is RTL representing the value of the return
2457 address for the frame COUNT steps up from the current frame, after the
2458 prologue. FRAMEADDR is the frame pointer of the COUNT frame, or the frame
2459 pointer of the COUNT - 1 frame if `RETURN_ADDR_IN_PREVIOUS_FRAME' is
2460 defined.
2461
2462 The value of the expression must always be the correct address when COUNT is
2463 zero, but may be `NULL_RTX' if there is not way to determine the return
2464 address of other frames. */
2465
2466rtx
34208acf 2467frv_return_addr_rtx (int count, rtx frame)
36a05131 2468{
34208acf
AO
2469 if (count != 0)
2470 return const0_rtx;
36a05131 2471 cfun->machine->frame_needed = 1;
0a81f074 2472 return gen_rtx_MEM (Pmode, plus_constant (Pmode, frame, 8));
36a05131
BS
2473}
2474
2475/* Given a memory reference MEMREF, interpret the referenced memory as
2476 an array of MODE values, and return a reference to the element
2477 specified by INDEX. Assume that any pre-modification implicit in
2478 MEMREF has already happened.
2479
2480 MEMREF must be a legitimate operand for modes larger than SImode.
c6c3dba9 2481 frv_legitimate_address_p forbids register+register addresses, which
36a05131
BS
2482 this function cannot handle. */
2483rtx
ef4bddc2 2484frv_index_memory (rtx memref, machine_mode mode, int index)
36a05131
BS
2485{
2486 rtx base = XEXP (memref, 0);
2487 if (GET_CODE (base) == PRE_MODIFY)
2488 base = XEXP (base, 0);
2489 return change_address (memref, mode,
0a81f074
RS
2490 plus_constant (Pmode, base,
2491 index * GET_MODE_SIZE (mode)));
36a05131
BS
2492}
2493
2494\f
2495/* Print a memory address as an operand to reference that memory location. */
0fb30cb7 2496static void
f2206911 2497frv_print_operand_address (FILE * stream, rtx x)
36a05131
BS
2498{
2499 if (GET_CODE (x) == MEM)
2500 x = XEXP (x, 0);
2501
2502 switch (GET_CODE (x))
2503 {
2504 case REG:
2505 fputs (reg_names [ REGNO (x)], stream);
2506 return;
2507
2508 case CONST_INT:
2509 fprintf (stream, "%ld", (long) INTVAL (x));
2510 return;
2511
2512 case SYMBOL_REF:
2513 assemble_name (stream, XSTR (x, 0));
2514 return;
2515
2516 case LABEL_REF:
2517 case CONST:
2518 output_addr_const (stream, x);
2519 return;
2520
8d8256c1
NC
2521 case PLUS:
2522 /* Poorly constructed asm statements can trigger this alternative.
2523 See gcc/testsuite/gcc.dg/asm-4.c for an example. */
2524 frv_print_operand_memory_reference (stream, x, 0);
2525 return;
2526
36a05131
BS
2527 default:
2528 break;
2529 }
2530
ab532386 2531 fatal_insn ("bad insn to frv_print_operand_address:", x);
36a05131
BS
2532}
2533
2534\f
2535static void
f2206911 2536frv_print_operand_memory_reference_reg (FILE * stream, rtx x)
36a05131
BS
2537{
2538 int regno = true_regnum (x);
2539 if (GPR_P (regno))
2540 fputs (reg_names[regno], stream);
2541 else
ab532386 2542 fatal_insn ("bad register to frv_print_operand_memory_reference_reg:", x);
36a05131
BS
2543}
2544
2545/* Print a memory reference suitable for the ld/st instructions. */
2546
2547static void
f2206911 2548frv_print_operand_memory_reference (FILE * stream, rtx x, int addr_offset)
36a05131 2549{
34208acf 2550 struct frv_unspec unspec;
36a05131
BS
2551 rtx x0 = NULL_RTX;
2552 rtx x1 = NULL_RTX;
2553
2554 switch (GET_CODE (x))
2555 {
2556 case SUBREG:
2557 case REG:
2558 x0 = x;
2559 break;
2560
2561 case PRE_MODIFY: /* (pre_modify (reg) (plus (reg) (reg))) */
2562 x0 = XEXP (x, 0);
2563 x1 = XEXP (XEXP (x, 1), 1);
2564 break;
2565
2566 case CONST_INT:
2567 x1 = x;
2568 break;
2569
2570 case PLUS:
2571 x0 = XEXP (x, 0);
2572 x1 = XEXP (x, 1);
2573 if (GET_CODE (x0) == CONST_INT)
2574 {
2575 x0 = XEXP (x, 1);
2576 x1 = XEXP (x, 0);
2577 }
2578 break;
2579
2580 default:
ab532386 2581 fatal_insn ("bad insn to frv_print_operand_memory_reference:", x);
36a05131
BS
2582 break;
2583
2584 }
2585
2586 if (addr_offset)
2587 {
2588 if (!x1)
2589 x1 = const0_rtx;
2590 else if (GET_CODE (x1) != CONST_INT)
ab532386 2591 fatal_insn ("bad insn to frv_print_operand_memory_reference:", x);
36a05131
BS
2592 }
2593
2594 fputs ("@(", stream);
2595 if (!x0)
2596 fputs (reg_names[GPR_R0], stream);
2597 else if (GET_CODE (x0) == REG || GET_CODE (x0) == SUBREG)
2598 frv_print_operand_memory_reference_reg (stream, x0);
2599 else
ab532386 2600 fatal_insn ("bad insn to frv_print_operand_memory_reference:", x);
36a05131
BS
2601
2602 fputs (",", stream);
2603 if (!x1)
2604 fputs (reg_names [GPR_R0], stream);
2605
2606 else
2607 {
2608 switch (GET_CODE (x1))
2609 {
2610 case SUBREG:
2611 case REG:
2612 frv_print_operand_memory_reference_reg (stream, x1);
2613 break;
2614
2615 case CONST_INT:
2616 fprintf (stream, "%ld", (long) (INTVAL (x1) + addr_offset));
2617 break;
2618
36a05131 2619 case CONST:
34208acf 2620 if (!frv_const_unspec_p (x1, &unspec))
ab532386 2621 fatal_insn ("bad insn to frv_print_operand_memory_reference:", x1);
34208acf 2622 frv_output_const_unspec (stream, &unspec);
36a05131
BS
2623 break;
2624
2625 default:
ab532386 2626 fatal_insn ("bad insn to frv_print_operand_memory_reference:", x);
36a05131
BS
2627 }
2628 }
2629
2630 fputs (")", stream);
2631}
2632
2633\f
2634/* Return 2 for likely branches and 0 for non-likely branches */
2635
2636#define FRV_JUMP_LIKELY 2
2637#define FRV_JUMP_NOT_LIKELY 0
2638
2639static int
68a1a6c0 2640frv_print_operand_jump_hint (rtx_insn *insn)
36a05131
BS
2641{
2642 rtx note;
2643 rtx labelref;
2644 int ret;
e5af9ddd 2645 int prob = -1;
36a05131
BS
2646 enum { UNKNOWN, BACKWARD, FORWARD } jump_type = UNKNOWN;
2647
b64925dc 2648 gcc_assert (JUMP_P (insn));
36a05131
BS
2649
2650 /* Assume any non-conditional jump is likely. */
2651 if (! any_condjump_p (insn))
2652 ret = FRV_JUMP_LIKELY;
2653
2654 else
2655 {
2656 labelref = condjump_label (insn);
2657 if (labelref)
2658 {
2659 rtx label = XEXP (labelref, 0);
2660 jump_type = (insn_current_address > INSN_ADDRESSES (INSN_UID (label))
2661 ? BACKWARD
2662 : FORWARD);
2663 }
2664
2665 note = find_reg_note (insn, REG_BR_PROB, 0);
2666 if (!note)
2667 ret = ((jump_type == BACKWARD) ? FRV_JUMP_LIKELY : FRV_JUMP_NOT_LIKELY);
2668
2669 else
2670 {
e5af9ddd 2671 prob = XINT (note, 0);
36a05131
BS
2672 ret = ((prob >= (REG_BR_PROB_BASE / 2))
2673 ? FRV_JUMP_LIKELY
2674 : FRV_JUMP_NOT_LIKELY);
2675 }
2676 }
2677
2678#if 0
2679 if (TARGET_DEBUG)
2680 {
2681 char *direction;
2682
2683 switch (jump_type)
2684 {
2685 default:
2686 case UNKNOWN: direction = "unknown jump direction"; break;
2687 case BACKWARD: direction = "jump backward"; break;
2688 case FORWARD: direction = "jump forward"; break;
2689 }
2690
2691 fprintf (stderr,
e5af9ddd 2692 "%s: uid %ld, %s, probability = %d, max prob. = %d, hint = %d\n",
36a05131 2693 IDENTIFIER_POINTER (DECL_NAME (current_function_decl)),
e5af9ddd
RS
2694 (long)INSN_UID (insn), direction, prob,
2695 REG_BR_PROB_BASE, ret);
36a05131
BS
2696 }
2697#endif
2698
2699 return ret;
2700}
2701
2702\f
036ff63f
RS
2703/* Return the comparison operator to use for CODE given that the ICC
2704 register is OP0. */
2705
2706static const char *
2707comparison_string (enum rtx_code code, rtx op0)
2708{
2709 bool is_nz_p = GET_MODE (op0) == CC_NZmode;
2710 switch (code)
2711 {
2712 default: output_operand_lossage ("bad condition code");
2713 case EQ: return "eq";
2714 case NE: return "ne";
2715 case LT: return is_nz_p ? "n" : "lt";
2716 case LE: return "le";
2717 case GT: return "gt";
2718 case GE: return is_nz_p ? "p" : "ge";
2719 case LTU: return is_nz_p ? "no" : "c";
2720 case LEU: return is_nz_p ? "eq" : "ls";
2721 case GTU: return is_nz_p ? "ne" : "hi";
2722 case GEU: return is_nz_p ? "ra" : "nc";
2723 }
2724}
2725
43aa4e05 2726/* Print an operand to an assembler instruction.
36a05131
BS
2727
2728 `%' followed by a letter and a digit says to output an operand in an
0fb30cb7
NF
2729 alternate fashion. Four letters have standard, built-in meanings
2730 described below. The hook `TARGET_PRINT_OPERAND' can define
2731 additional letters with nonstandard meanings.
36a05131
BS
2732
2733 `%cDIGIT' can be used to substitute an operand that is a constant value
2734 without the syntax that normally indicates an immediate operand.
2735
2736 `%nDIGIT' is like `%cDIGIT' except that the value of the constant is negated
2737 before printing.
2738
2739 `%aDIGIT' can be used to substitute an operand as if it were a memory
2740 reference, with the actual operand treated as the address. This may be
2741 useful when outputting a "load address" instruction, because often the
2742 assembler syntax for such an instruction requires you to write the operand
2743 as if it were a memory reference.
2744
2745 `%lDIGIT' is used to substitute a `label_ref' into a jump instruction.
2746
2747 `%=' outputs a number which is unique to each instruction in the entire
2748 compilation. This is useful for making local labels to be referred to more
2749 than once in a single template that generates multiple assembler
2750 instructions.
2751
0fb30cb7
NF
2752 `%' followed by a punctuation character specifies a substitution that
2753 does not use an operand. Only one case is standard: `%%' outputs a
2754 `%' into the assembler code. Other nonstandard cases can be defined
2755 in the `TARGET_PRINT_OPERAND' hook. You must also define which
2756 punctuation characters are valid with the
2757 `TARGET_PRINT_OPERAND_PUNCT_VALID_P' hook. */
36a05131 2758
0fb30cb7 2759static void
f2206911 2760frv_print_operand (FILE * file, rtx x, int code)
36a05131 2761{
34208acf 2762 struct frv_unspec unspec;
36a05131
BS
2763 HOST_WIDE_INT value;
2764 int offset;
2765
0a2aaacc 2766 if (code != 0 && !ISALPHA (code))
36a05131
BS
2767 value = 0;
2768
2769 else if (GET_CODE (x) == CONST_INT)
2770 value = INTVAL (x);
2771
2772 else if (GET_CODE (x) == CONST_DOUBLE)
2773 {
2774 if (GET_MODE (x) == SFmode)
2775 {
2776 REAL_VALUE_TYPE rv;
2777 long l;
2778
2779 REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
2780 REAL_VALUE_TO_TARGET_SINGLE (rv, l);
2781 value = l;
2782 }
2783
2784 else if (GET_MODE (x) == VOIDmode)
2785 value = CONST_DOUBLE_LOW (x);
2786
2787 else
ab532386 2788 fatal_insn ("bad insn in frv_print_operand, bad const_double", x);
36a05131
BS
2789 }
2790
2791 else
2792 value = 0;
2793
2794 switch (code)
2795 {
2796
2797 case '.':
87b483a1 2798 /* Output r0. */
36a05131
BS
2799 fputs (reg_names[GPR_R0], file);
2800 break;
2801
2802 case '#':
2803 fprintf (file, "%d", frv_print_operand_jump_hint (current_output_insn));
2804 break;
2805
0f6e5d45 2806 case '@':
87b483a1 2807 /* Output small data area base register (gr16). */
36a05131
BS
2808 fputs (reg_names[SDA_BASE_REG], file);
2809 break;
2810
2811 case '~':
87b483a1 2812 /* Output pic register (gr17). */
36a05131
BS
2813 fputs (reg_names[PIC_REGNO], file);
2814 break;
2815
2816 case '*':
87b483a1 2817 /* Output the temporary integer CCR register. */
36a05131
BS
2818 fputs (reg_names[ICR_TEMP], file);
2819 break;
2820
2821 case '&':
87b483a1 2822 /* Output the temporary integer CC register. */
36a05131
BS
2823 fputs (reg_names[ICC_TEMP], file);
2824 break;
2825
87b483a1 2826 /* case 'a': print an address. */
36a05131
BS
2827
2828 case 'C':
87b483a1 2829 /* Print appropriate test for integer branch false operation. */
036ff63f
RS
2830 fputs (comparison_string (reverse_condition (GET_CODE (x)),
2831 XEXP (x, 0)), file);
36a05131
BS
2832 break;
2833
36a05131 2834 case 'c':
87b483a1 2835 /* Print appropriate test for integer branch true operation. */
036ff63f 2836 fputs (comparison_string (GET_CODE (x), XEXP (x, 0)), file);
36a05131
BS
2837 break;
2838
2839 case 'e':
2840 /* Print 1 for a NE and 0 for an EQ to give the final argument
2841 for a conditional instruction. */
2842 if (GET_CODE (x) == NE)
2843 fputs ("1", file);
2844
2845 else if (GET_CODE (x) == EQ)
2846 fputs ("0", file);
2847
2848 else
ab532386 2849 fatal_insn ("bad insn to frv_print_operand, 'e' modifier:", x);
36a05131
BS
2850 break;
2851
2852 case 'F':
87b483a1 2853 /* Print appropriate test for floating point branch false operation. */
36a05131
BS
2854 switch (GET_CODE (x))
2855 {
2856 default:
ab532386 2857 fatal_insn ("bad insn to frv_print_operand, 'F' modifier:", x);
36a05131
BS
2858
2859 case EQ: fputs ("ne", file); break;
2860 case NE: fputs ("eq", file); break;
2861 case LT: fputs ("uge", file); break;
2862 case LE: fputs ("ug", file); break;
2863 case GT: fputs ("ule", file); break;
2864 case GE: fputs ("ul", file); break;
2865 }
2866 break;
2867
2868 case 'f':
87b483a1 2869 /* Print appropriate test for floating point branch true operation. */
36a05131
BS
2870 switch (GET_CODE (x))
2871 {
2872 default:
ab532386 2873 fatal_insn ("bad insn to frv_print_operand, 'f' modifier:", x);
36a05131
BS
2874
2875 case EQ: fputs ("eq", file); break;
2876 case NE: fputs ("ne", file); break;
2877 case LT: fputs ("lt", file); break;
2878 case LE: fputs ("le", file); break;
2879 case GT: fputs ("gt", file); break;
2880 case GE: fputs ("ge", file); break;
2881 }
2882 break;
2883
34208acf
AO
2884 case 'g':
2885 /* Print appropriate GOT function. */
2886 if (GET_CODE (x) != CONST_INT)
ab532386 2887 fatal_insn ("bad insn to frv_print_operand, 'g' modifier:", x);
34208acf
AO
2888 fputs (unspec_got_name (INTVAL (x)), file);
2889 break;
2890
36a05131
BS
2891 case 'I':
2892 /* Print 'i' if the operand is a constant, or is a memory reference that
87b483a1 2893 adds a constant. */
36a05131
BS
2894 if (GET_CODE (x) == MEM)
2895 x = ((GET_CODE (XEXP (x, 0)) == PLUS)
2896 ? XEXP (XEXP (x, 0), 1)
2897 : XEXP (x, 0));
34208acf
AO
2898 else if (GET_CODE (x) == PLUS)
2899 x = XEXP (x, 1);
36a05131
BS
2900
2901 switch (GET_CODE (x))
2902 {
2903 default:
2904 break;
2905
2906 case CONST_INT:
2907 case SYMBOL_REF:
2908 case CONST:
2909 fputs ("i", file);
2910 break;
2911 }
2912 break;
2913
2914 case 'i':
2915 /* For jump instructions, print 'i' if the operand is a constant or
87b483a1 2916 is an expression that adds a constant. */
36a05131
BS
2917 if (GET_CODE (x) == CONST_INT)
2918 fputs ("i", file);
2919
2920 else
2921 {
2922 if (GET_CODE (x) == CONST_INT
2923 || (GET_CODE (x) == PLUS
2924 && (GET_CODE (XEXP (x, 1)) == CONST_INT
2925 || GET_CODE (XEXP (x, 0)) == CONST_INT)))
2926 fputs ("i", file);
2927 }
2928 break;
2929
2930 case 'L':
2931 /* Print the lower register of a double word register pair */
2932 if (GET_CODE (x) == REG)
2933 fputs (reg_names[ REGNO (x)+1 ], file);
2934 else
ab532386 2935 fatal_insn ("bad insn to frv_print_operand, 'L' modifier:", x);
36a05131
BS
2936 break;
2937
87b483a1 2938 /* case 'l': print a LABEL_REF. */
36a05131
BS
2939
2940 case 'M':
2941 case 'N':
2942 /* Print a memory reference for ld/st/jmp, %N prints a memory reference
2943 for the second word of double memory operations. */
2944 offset = (code == 'M') ? 0 : UNITS_PER_WORD;
2945 switch (GET_CODE (x))
2946 {
2947 default:
ab532386 2948 fatal_insn ("bad insn to frv_print_operand, 'M/N' modifier:", x);
36a05131
BS
2949
2950 case MEM:
2951 frv_print_operand_memory_reference (file, XEXP (x, 0), offset);
2952 break;
2953
2954 case REG:
2955 case SUBREG:
2956 case CONST_INT:
2957 case PLUS:
2958 case SYMBOL_REF:
2959 frv_print_operand_memory_reference (file, x, offset);
2960 break;
2961 }
2962 break;
2963
2964 case 'O':
2965 /* Print the opcode of a command. */
2966 switch (GET_CODE (x))
2967 {
2968 default:
ab532386 2969 fatal_insn ("bad insn to frv_print_operand, 'O' modifier:", x);
36a05131
BS
2970
2971 case PLUS: fputs ("add", file); break;
2972 case MINUS: fputs ("sub", file); break;
2973 case AND: fputs ("and", file); break;
2974 case IOR: fputs ("or", file); break;
2975 case XOR: fputs ("xor", file); break;
2976 case ASHIFT: fputs ("sll", file); break;
2977 case ASHIFTRT: fputs ("sra", file); break;
2978 case LSHIFTRT: fputs ("srl", file); break;
2979 }
2980 break;
2981
87b483a1 2982 /* case 'n': negate and print a constant int. */
36a05131
BS
2983
2984 case 'P':
2985 /* Print PIC label using operand as the number. */
2986 if (GET_CODE (x) != CONST_INT)
ab532386 2987 fatal_insn ("bad insn to frv_print_operand, P modifier:", x);
36a05131
BS
2988
2989 fprintf (file, ".LCF%ld", (long)INTVAL (x));
2990 break;
2991
2992 case 'U':
87b483a1 2993 /* Print 'u' if the operand is a update load/store. */
36a05131
BS
2994 if (GET_CODE (x) == MEM && GET_CODE (XEXP (x, 0)) == PRE_MODIFY)
2995 fputs ("u", file);
2996 break;
2997
2998 case 'z':
87b483a1 2999 /* If value is 0, print gr0, otherwise it must be a register. */
36a05131
BS
3000 if (GET_CODE (x) == CONST_INT && INTVAL (x) == 0)
3001 fputs (reg_names[GPR_R0], file);
3002
3003 else if (GET_CODE (x) == REG)
3004 fputs (reg_names [REGNO (x)], file);
3005
3006 else
ab532386 3007 fatal_insn ("bad insn in frv_print_operand, z case", x);
36a05131
BS
3008 break;
3009
3010 case 'x':
87b483a1 3011 /* Print constant in hex. */
36a05131
BS
3012 if (GET_CODE (x) == CONST_INT || GET_CODE (x) == CONST_DOUBLE)
3013 {
3014 fprintf (file, "%s0x%.4lx", IMMEDIATE_PREFIX, (long) value);
3015 break;
3016 }
3017
87b483a1 3018 /* Fall through. */
36a05131
BS
3019
3020 case '\0':
3021 if (GET_CODE (x) == REG)
3022 fputs (reg_names [REGNO (x)], file);
3023
3024 else if (GET_CODE (x) == CONST_INT
3025 || GET_CODE (x) == CONST_DOUBLE)
3026 fprintf (file, "%s%ld", IMMEDIATE_PREFIX, (long) value);
3027
34208acf
AO
3028 else if (frv_const_unspec_p (x, &unspec))
3029 frv_output_const_unspec (file, &unspec);
3030
36a05131
BS
3031 else if (GET_CODE (x) == MEM)
3032 frv_print_operand_address (file, XEXP (x, 0));
3033
3034 else if (CONSTANT_ADDRESS_P (x))
3035 frv_print_operand_address (file, x);
3036
3037 else
ab532386 3038 fatal_insn ("bad insn in frv_print_operand, 0 case", x);
36a05131
BS
3039
3040 break;
3041
3042 default:
3043 fatal_insn ("frv_print_operand: unknown code", x);
3044 break;
3045 }
3046
3047 return;
3048}
3049
0fb30cb7
NF
3050static bool
3051frv_print_operand_punct_valid_p (unsigned char code)
3052{
3053 return (code == '.' || code == '#' || code == '@' || code == '~'
3054 || code == '*' || code == '&');
3055}
3056
36a05131
BS
3057\f
3058/* A C statement (sans semicolon) for initializing the variable CUM for the
3059 state at the beginning of the argument list. The variable has type
3060 `CUMULATIVE_ARGS'. The value of FNTYPE is the tree node for the data type
3061 of the function which will receive the args, or 0 if the args are to a
3062 compiler support library function. The value of INDIRECT is nonzero when
3063 processing an indirect call, for example a call through a function pointer.
3064 The value of INDIRECT is zero for a call to an explicitly named function, a
3065 library function call, or when `INIT_CUMULATIVE_ARGS' is used to find
3066 arguments for the function being compiled.
3067
3068 When processing a call to a compiler support library function, LIBNAME
3069 identifies which one. It is a `symbol_ref' rtx which contains the name of
3070 the function, as a string. LIBNAME is 0 when an ordinary C function call is
3071 being processed. Thus, each time this macro is called, either LIBNAME or
3072 FNTYPE is nonzero, but never both of them at once. */
3073
3074void
f2206911
KC
3075frv_init_cumulative_args (CUMULATIVE_ARGS *cum,
3076 tree fntype,
3077 rtx libname,
3078 tree fndecl,
3079 int incoming)
36a05131
BS
3080{
3081 *cum = FIRST_ARG_REGNUM;
3082
3083 if (TARGET_DEBUG_ARG)
3084 {
3085 fprintf (stderr, "\ninit_cumulative_args:");
563a317a 3086 if (!fndecl && fntype)
36a05131
BS
3087 fputs (" indirect", stderr);
3088
3089 if (incoming)
3090 fputs (" incoming", stderr);
3091
3092 if (fntype)
3093 {
3094 tree ret_type = TREE_TYPE (fntype);
3095 fprintf (stderr, " return=%s,",
8ad8afaf 3096 get_tree_code_name (TREE_CODE (ret_type)));
36a05131
BS
3097 }
3098
3099 if (libname && GET_CODE (libname) == SYMBOL_REF)
3100 fprintf (stderr, " libname=%s", XSTR (libname, 0));
3101
3102 if (cfun->returns_struct)
3103 fprintf (stderr, " return-struct");
3104
3105 putc ('\n', stderr);
3106 }
3107}
3108
3109\f
fe984136
RH
3110/* Return true if we should pass an argument on the stack rather than
3111 in registers. */
3112
3113static bool
ef4bddc2 3114frv_must_pass_in_stack (machine_mode mode, const_tree type)
fe984136
RH
3115{
3116 if (mode == BLKmode)
3117 return true;
3118 if (type == NULL)
3119 return false;
3120 return AGGREGATE_TYPE_P (type);
3121}
3122
36a05131
BS
3123/* If defined, a C expression that gives the alignment boundary, in bits, of an
3124 argument with the specified mode and type. If it is not defined,
3125 `PARM_BOUNDARY' is used for all arguments. */
3126
c2ed6cf8 3127static unsigned int
ef4bddc2 3128frv_function_arg_boundary (machine_mode mode ATTRIBUTE_UNUSED,
c2ed6cf8 3129 const_tree type ATTRIBUTE_UNUSED)
36a05131
BS
3130{
3131 return BITS_PER_WORD;
3132}
3133
88a1f47f 3134static rtx
ef4bddc2 3135frv_function_arg_1 (cumulative_args_t cum_v, machine_mode mode,
88a1f47f
NF
3136 const_tree type ATTRIBUTE_UNUSED, bool named,
3137 bool incoming ATTRIBUTE_UNUSED)
36a05131 3138{
d5cc9181
JR
3139 const CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
3140
ef4bddc2 3141 machine_mode xmode = (mode == BLKmode) ? SImode : mode;
36a05131
BS
3142 int arg_num = *cum;
3143 rtx ret;
3144 const char *debstr;
3145
3146 /* Return a marker for use in the call instruction. */
3147 if (xmode == VOIDmode)
3148 {
3149 ret = const0_rtx;
3150 debstr = "<0>";
3151 }
3152
3153 else if (arg_num <= LAST_ARG_REGNUM)
3154 {
f1c25d3b 3155 ret = gen_rtx_REG (xmode, arg_num);
36a05131
BS
3156 debstr = reg_names[arg_num];
3157 }
3158
3159 else
3160 {
3161 ret = NULL_RTX;
3162 debstr = "memory";
3163 }
3164
3165 if (TARGET_DEBUG_ARG)
3166 fprintf (stderr,
3167 "function_arg: words = %2d, mode = %4s, named = %d, size = %3d, arg = %s\n",
3168 arg_num, GET_MODE_NAME (mode), named, GET_MODE_SIZE (mode), debstr);
3169
3170 return ret;
3171}
3172
88a1f47f 3173static rtx
ef4bddc2 3174frv_function_arg (cumulative_args_t cum, machine_mode mode,
88a1f47f
NF
3175 const_tree type, bool named)
3176{
3177 return frv_function_arg_1 (cum, mode, type, named, false);
3178}
3179
3180static rtx
ef4bddc2 3181frv_function_incoming_arg (cumulative_args_t cum, machine_mode mode,
88a1f47f
NF
3182 const_tree type, bool named)
3183{
3184 return frv_function_arg_1 (cum, mode, type, named, true);
3185}
3186
36a05131
BS
3187\f
3188/* A C statement (sans semicolon) to update the summarizer variable CUM to
3189 advance past an argument in the argument list. The values MODE, TYPE and
3190 NAMED describe that argument. Once this is done, the variable CUM is
3191 suitable for analyzing the *following* argument with `FUNCTION_ARG', etc.
3192
3193 This macro need not do anything if the argument in question was passed on
3194 the stack. The compiler knows how to track the amount of stack space used
3195 for arguments without any special help. */
3196
88a1f47f 3197static void
d5cc9181 3198frv_function_arg_advance (cumulative_args_t cum_v,
ef4bddc2 3199 machine_mode mode,
88a1f47f
NF
3200 const_tree type ATTRIBUTE_UNUSED,
3201 bool named)
36a05131 3202{
d5cc9181
JR
3203 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
3204
ef4bddc2 3205 machine_mode xmode = (mode == BLKmode) ? SImode : mode;
36a05131
BS
3206 int bytes = GET_MODE_SIZE (xmode);
3207 int words = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
3208 int arg_num = *cum;
3209
3210 *cum = arg_num + words;
3211
3212 if (TARGET_DEBUG_ARG)
3213 fprintf (stderr,
3214 "function_adv: words = %2d, mode = %4s, named = %d, size = %3d\n",
3215 arg_num, GET_MODE_NAME (mode), named, words * UNITS_PER_WORD);
3216}
3217
3218\f
3219/* A C expression for the number of words, at the beginning of an argument,
3220 must be put in registers. The value must be zero for arguments that are
3221 passed entirely in registers or that are entirely pushed on the stack.
3222
3223 On some machines, certain arguments must be passed partially in registers
3224 and partially in memory. On these machines, typically the first N words of
3225 arguments are passed in registers, and the rest on the stack. If a
3226 multi-word argument (a `double' or a structure) crosses that boundary, its
3227 first few words must be passed in registers and the rest must be pushed.
3228 This macro tells the compiler when this occurs, and how many of the words
3229 should go in registers.
3230
3231 `FUNCTION_ARG' for these arguments should return the first register to be
3232 used by the caller for this argument; likewise `FUNCTION_INCOMING_ARG', for
3233 the called function. */
3234
78a52f11 3235static int
ef4bddc2 3236frv_arg_partial_bytes (cumulative_args_t cum, machine_mode mode,
78a52f11 3237 tree type ATTRIBUTE_UNUSED, bool named ATTRIBUTE_UNUSED)
36a05131 3238{
d5cc9181 3239
ef4bddc2 3240 machine_mode xmode = (mode == BLKmode) ? SImode : mode;
36a05131
BS
3241 int bytes = GET_MODE_SIZE (xmode);
3242 int words = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
d5cc9181 3243 int arg_num = *get_cumulative_args (cum);
36a05131
BS
3244 int ret;
3245
3246 ret = ((arg_num <= LAST_ARG_REGNUM && arg_num + words > LAST_ARG_REGNUM+1)
3247 ? LAST_ARG_REGNUM - arg_num + 1
3248 : 0);
78a52f11 3249 ret *= UNITS_PER_WORD;
36a05131
BS
3250
3251 if (TARGET_DEBUG_ARG && ret)
78a52f11 3252 fprintf (stderr, "frv_arg_partial_bytes: %d\n", ret);
36a05131
BS
3253
3254 return ret;
36a05131
BS
3255}
3256
219d92a4
AS
3257\f
3258/* Implements TARGET_FUNCTION_VALUE. */
3259
3260static rtx
3261frv_function_value (const_tree valtype,
3262 const_tree fn_decl_or_type ATTRIBUTE_UNUSED,
3263 bool outgoing ATTRIBUTE_UNUSED)
3264{
3265 return gen_rtx_REG (TYPE_MODE (valtype), RETURN_VALUE_REGNUM);
3266}
3267
3268\f
3269/* Implements TARGET_LIBCALL_VALUE. */
3270
3271static rtx
ef4bddc2 3272frv_libcall_value (machine_mode mode,
219d92a4
AS
3273 const_rtx fun ATTRIBUTE_UNUSED)
3274{
3275 return gen_rtx_REG (mode, RETURN_VALUE_REGNUM);
3276}
3277
3278\f
3279/* Implements FUNCTION_VALUE_REGNO_P. */
3280
3281bool
3282frv_function_value_regno_p (const unsigned int regno)
3283{
3284 return (regno == RETURN_VALUE_REGNUM);
3285}
36a05131
BS
3286\f
3287/* Return true if a register is ok to use as a base or index register. */
3288
3289static FRV_INLINE int
f2206911 3290frv_regno_ok_for_base_p (int regno, int strict_p)
36a05131
BS
3291{
3292 if (GPR_P (regno))
3293 return TRUE;
3294
3295 if (strict_p)
3296 return (reg_renumber[regno] >= 0 && GPR_P (reg_renumber[regno]));
3297
3298 if (regno == ARG_POINTER_REGNUM)
3299 return TRUE;
3300
3301 return (regno >= FIRST_PSEUDO_REGISTER);
3302}
3303
3304\f
3305/* A C compound statement with a conditional `goto LABEL;' executed if X (an
3306 RTX) is a legitimate memory address on the target machine for a memory
3307 operand of mode MODE.
3308
3309 It usually pays to define several simpler macros to serve as subroutines for
3310 this one. Otherwise it may be too complicated to understand.
3311
3312 This macro must exist in two variants: a strict variant and a non-strict
3313 one. The strict variant is used in the reload pass. It must be defined so
3314 that any pseudo-register that has not been allocated a hard register is
3315 considered a memory reference. In contexts where some kind of register is
3316 required, a pseudo-register with no hard register must be rejected.
3317
3318 The non-strict variant is used in other passes. It must be defined to
3319 accept all pseudo-registers in every context where some kind of register is
3320 required.
3321
3322 Compiler source files that want to use the strict variant of this macro
3323 define the macro `REG_OK_STRICT'. You should use an `#ifdef REG_OK_STRICT'
3324 conditional to define the strict variant in that case and the non-strict
3325 variant otherwise.
3326
36a05131
BS
3327 Normally, constant addresses which are the sum of a `symbol_ref' and an
3328 integer are stored inside a `const' RTX to mark them as constant.
3329 Therefore, there is no need to recognize such sums specifically as
3330 legitimate addresses. Normally you would simply recognize any `const' as
3331 legitimate.
3332
0fb30cb7
NF
3333 Usually `TARGET_PRINT_OPERAND_ADDRESS' is not prepared to handle
3334 constant sums that are not marked with `const'. It assumes that a
3335 naked `plus' indicates indexing. If so, then you *must* reject such
3336 naked constant sums as illegitimate addresses, so that none of them
3337 will be given to `TARGET_PRINT_OPERAND_ADDRESS'. */
36a05131
BS
3338
3339int
ef4bddc2 3340frv_legitimate_address_p_1 (machine_mode mode,
c6c3dba9
PB
3341 rtx x,
3342 int strict_p,
3343 int condexec_p,
3344 int allow_double_reg_p)
36a05131
BS
3345{
3346 rtx x0, x1;
3347 int ret = 0;
3348 HOST_WIDE_INT value;
3349 unsigned regno0;
3350
bef8809e
AH
3351 if (FRV_SYMBOL_REF_TLS_P (x))
3352 return 0;
3353
36a05131
BS
3354 switch (GET_CODE (x))
3355 {
3356 default:
3357 break;
3358
3359 case SUBREG:
3360 x = SUBREG_REG (x);
3361 if (GET_CODE (x) != REG)
3362 break;
3363
87b483a1 3364 /* Fall through. */
36a05131
BS
3365
3366 case REG:
3367 ret = frv_regno_ok_for_base_p (REGNO (x), strict_p);
3368 break;
3369
3370 case PRE_MODIFY:
3371 x0 = XEXP (x, 0);
3372 x1 = XEXP (x, 1);
3373 if (GET_CODE (x0) != REG
3374 || ! frv_regno_ok_for_base_p (REGNO (x0), strict_p)
3375 || GET_CODE (x1) != PLUS
3376 || ! rtx_equal_p (x0, XEXP (x1, 0))
3377 || GET_CODE (XEXP (x1, 1)) != REG
3378 || ! frv_regno_ok_for_base_p (REGNO (XEXP (x1, 1)), strict_p))
3379 break;
3380
3381 ret = 1;
3382 break;
3383
3384 case CONST_INT:
2300b9dd 3385 /* 12-bit immediate */
36a05131
BS
3386 if (condexec_p)
3387 ret = FALSE;
3388 else
3389 {
2f5b1308 3390 ret = IN_RANGE (INTVAL (x), -2048, 2047);
36a05131
BS
3391
3392 /* If we can't use load/store double operations, make sure we can
3393 address the second word. */
3394 if (ret && GET_MODE_SIZE (mode) > UNITS_PER_WORD)
2f5b1308
JR
3395 ret = IN_RANGE (INTVAL (x) + GET_MODE_SIZE (mode) - 1,
3396 -2048, 2047);
36a05131
BS
3397 }
3398 break;
3399
3400 case PLUS:
3401 x0 = XEXP (x, 0);
3402 x1 = XEXP (x, 1);
3403
3404 if (GET_CODE (x0) == SUBREG)
3405 x0 = SUBREG_REG (x0);
3406
3407 if (GET_CODE (x0) != REG)
3408 break;
3409
3410 regno0 = REGNO (x0);
3411 if (!frv_regno_ok_for_base_p (regno0, strict_p))
3412 break;
3413
3414 switch (GET_CODE (x1))
3415 {
3416 default:
3417 break;
3418
3419 case SUBREG:
3420 x1 = SUBREG_REG (x1);
3421 if (GET_CODE (x1) != REG)
3422 break;
3423
87b483a1 3424 /* Fall through. */
36a05131
BS
3425
3426 case REG:
87b483a1
KH
3427 /* Do not allow reg+reg addressing for modes > 1 word if we
3428 can't depend on having move double instructions. */
34208acf 3429 if (!allow_double_reg_p && GET_MODE_SIZE (mode) > UNITS_PER_WORD)
36a05131
BS
3430 ret = FALSE;
3431 else
3432 ret = frv_regno_ok_for_base_p (REGNO (x1), strict_p);
3433 break;
3434
3435 case CONST_INT:
2300b9dd 3436 /* 12-bit immediate */
36a05131
BS
3437 if (condexec_p)
3438 ret = FALSE;
3439 else
3440 {
3441 value = INTVAL (x1);
2f5b1308 3442 ret = IN_RANGE (value, -2048, 2047);
36a05131
BS
3443
3444 /* If we can't use load/store double operations, make sure we can
3445 address the second word. */
3446 if (ret && GET_MODE_SIZE (mode) > UNITS_PER_WORD)
2f5b1308 3447 ret = IN_RANGE (value + GET_MODE_SIZE (mode) - 1, -2048, 2047);
36a05131
BS
3448 }
3449 break;
3450
36a05131 3451 case CONST:
34208acf 3452 if (!condexec_p && got12_operand (x1, VOIDmode))
36a05131
BS
3453 ret = TRUE;
3454 break;
3455
3456 }
3457 break;
3458 }
3459
3460 if (TARGET_DEBUG_ADDR)
3461 {
331d9186 3462 fprintf (stderr, "\n========== legitimate_address_p, mode = %s, result = %d, addresses are %sstrict%s\n",
36a05131
BS
3463 GET_MODE_NAME (mode), ret, (strict_p) ? "" : "not ",
3464 (condexec_p) ? ", inside conditional code" : "");
3465 debug_rtx (x);
3466 }
3467
3468 return ret;
3469}
3470
c6c3dba9 3471bool
ef4bddc2 3472frv_legitimate_address_p (machine_mode mode, rtx x, bool strict_p)
c6c3dba9
PB
3473{
3474 return frv_legitimate_address_p_1 (mode, x, strict_p, FALSE, FALSE);
3475}
3476
bef8809e
AH
3477/* Given an ADDR, generate code to inline the PLT. */
3478static rtx
3479gen_inlined_tls_plt (rtx addr)
3480{
fdbe66f2 3481 rtx retval, dest;
bef8809e
AH
3482 rtx picreg = get_hard_reg_initial_val (Pmode, FDPIC_REG);
3483
3484
3485 dest = gen_reg_rtx (DImode);
3486
3487 if (flag_pic == 1)
3488 {
3489 /*
3490 -fpic version:
3491
3492 lddi.p @(gr15, #gottlsdesc12(ADDR)), gr8
3493 calll #gettlsoff(ADDR)@(gr8, gr0)
3494 */
3495 emit_insn (gen_tls_lddi (dest, addr, picreg));
3496 }
3497 else
3498 {
3499 /*
3500 -fPIC version:
3501
3502 sethi.p #gottlsdeschi(ADDR), gr8
3503 setlo #gottlsdesclo(ADDR), gr8
3504 ldd #tlsdesc(ADDR)@(gr15, gr8), gr8
3505 calll #gettlsoff(ADDR)@(gr8, gr0)
3506 */
3507 rtx reguse = gen_reg_rtx (Pmode);
3508 emit_insn (gen_tlsoff_hilo (reguse, addr, GEN_INT (R_FRV_GOTTLSDESCHI)));
3509 emit_insn (gen_tls_tlsdesc_ldd (dest, picreg, reguse, addr));
3510 }
3511
3512 retval = gen_reg_rtx (Pmode);
a701780f 3513 emit_insn (gen_tls_indirect_call (retval, addr, dest, picreg));
bef8809e
AH
3514 return retval;
3515}
3516
3517/* Emit a TLSMOFF or TLSMOFF12 offset, depending on -mTLS. Returns
3518 the destination address. */
3519static rtx
3520gen_tlsmoff (rtx addr, rtx reg)
3521{
3522 rtx dest = gen_reg_rtx (Pmode);
3523
3524 if (TARGET_BIG_TLS)
3525 {
3526 /* sethi.p #tlsmoffhi(x), grA
3527 setlo #tlsmofflo(x), grA
3528 */
3529 dest = gen_reg_rtx (Pmode);
3530 emit_insn (gen_tlsoff_hilo (dest, addr,
3531 GEN_INT (R_FRV_TLSMOFFHI)));
3532 dest = gen_rtx_PLUS (Pmode, dest, reg);
3533 }
3534 else
3535 {
3536 /* addi grB, #tlsmoff12(x), grC
3537 -or-
3538 ld/st @(grB, #tlsmoff12(x)), grC
3539 */
3540 dest = gen_reg_rtx (Pmode);
3541 emit_insn (gen_symGOTOFF2reg_i (dest, addr, reg,
3542 GEN_INT (R_FRV_TLSMOFF12)));
3543 }
3544 return dest;
3545}
3546
3547/* Generate code for a TLS address. */
3548static rtx
3549frv_legitimize_tls_address (rtx addr, enum tls_model model)
3550{
3551 rtx dest, tp = gen_rtx_REG (Pmode, 29);
3552 rtx picreg = get_hard_reg_initial_val (Pmode, 15);
3553
3554 switch (model)
3555 {
3556 case TLS_MODEL_INITIAL_EXEC:
3557 if (flag_pic == 1)
3558 {
3559 /* -fpic version.
3560 ldi @(gr15, #gottlsoff12(x)), gr5
3561 */
3562 dest = gen_reg_rtx (Pmode);
3563 emit_insn (gen_tls_load_gottlsoff12 (dest, addr, picreg));
3564 dest = gen_rtx_PLUS (Pmode, tp, dest);
3565 }
3566 else
3567 {
3568 /* -fPIC or anything else.
3569
3570 sethi.p #gottlsoffhi(x), gr14
3571 setlo #gottlsofflo(x), gr14
3572 ld #tlsoff(x)@(gr15, gr14), gr9
3573 */
3574 rtx tmp = gen_reg_rtx (Pmode);
3575 dest = gen_reg_rtx (Pmode);
3576 emit_insn (gen_tlsoff_hilo (tmp, addr,
3577 GEN_INT (R_FRV_GOTTLSOFF_HI)));
3578
3579 emit_insn (gen_tls_tlsoff_ld (dest, picreg, tmp, addr));
3580 dest = gen_rtx_PLUS (Pmode, tp, dest);
3581 }
3582 break;
3583 case TLS_MODEL_LOCAL_DYNAMIC:
3584 {
3585 rtx reg, retval;
3586
3587 if (TARGET_INLINE_PLT)
3588 retval = gen_inlined_tls_plt (GEN_INT (0));
3589 else
3590 {
3591 /* call #gettlsoff(0) */
3592 retval = gen_reg_rtx (Pmode);
3593 emit_insn (gen_call_gettlsoff (retval, GEN_INT (0), picreg));
3594 }
3595
3596 reg = gen_reg_rtx (Pmode);
3597 emit_insn (gen_rtx_SET (VOIDmode, reg,
3598 gen_rtx_PLUS (Pmode,
3599 retval, tp)));
3600
3601 dest = gen_tlsmoff (addr, reg);
3602
3603 /*
3604 dest = gen_reg_rtx (Pmode);
3605 emit_insn (gen_tlsoff_hilo (dest, addr,
3606 GEN_INT (R_FRV_TLSMOFFHI)));
3607 dest = gen_rtx_PLUS (Pmode, dest, reg);
3608 */
3609 break;
3610 }
3611 case TLS_MODEL_LOCAL_EXEC:
3612 dest = gen_tlsmoff (addr, gen_rtx_REG (Pmode, 29));
3613 break;
3614 case TLS_MODEL_GLOBAL_DYNAMIC:
3615 {
3616 rtx retval;
3617
3618 if (TARGET_INLINE_PLT)
3619 retval = gen_inlined_tls_plt (addr);
3620 else
3621 {
3622 /* call #gettlsoff(x) */
3623 retval = gen_reg_rtx (Pmode);
3624 emit_insn (gen_call_gettlsoff (retval, addr, picreg));
3625 }
3626 dest = gen_rtx_PLUS (Pmode, retval, tp);
3627 break;
3628 }
3629 default:
44e91694 3630 gcc_unreachable ();
bef8809e
AH
3631 }
3632
3633 return dest;
3634}
3635
2a2e3f05 3636rtx
bef8809e 3637frv_legitimize_address (rtx x,
2a2e3f05 3638 rtx oldx ATTRIBUTE_UNUSED,
ef4bddc2 3639 machine_mode mode ATTRIBUTE_UNUSED)
2a2e3f05 3640{
bef8809e
AH
3641 if (GET_CODE (x) == SYMBOL_REF)
3642 {
3643 enum tls_model model = SYMBOL_REF_TLS_MODEL (x);
3644 if (model != 0)
3645 return frv_legitimize_tls_address (x, model);
3646 }
3647
506d7b68 3648 return x;
2a2e3f05 3649}
36a05131 3650\f
34208acf
AO
3651/* Test whether a local function descriptor is canonical, i.e.,
3652 whether we can use FUNCDESC_GOTOFF to compute the address of the
3653 function. */
3654
3655static bool
3656frv_local_funcdesc_p (rtx fnx)
3657{
3658 tree fn;
3659 enum symbol_visibility vis;
3660 bool ret;
36a05131 3661
34208acf
AO
3662 if (! SYMBOL_REF_LOCAL_P (fnx))
3663 return FALSE;
3664
3665 fn = SYMBOL_REF_DECL (fnx);
3666
3667 if (! fn)
3668 return FALSE;
36a05131 3669
34208acf 3670 vis = DECL_VISIBILITY (fn);
36a05131 3671
34208acf
AO
3672 if (vis == VISIBILITY_PROTECTED)
3673 /* Private function descriptors for protected functions are not
3674 canonical. Temporarily change the visibility to global. */
3675 vis = VISIBILITY_DEFAULT;
3676 else if (flag_shlib)
3677 /* If we're already compiling for a shared library (that, unlike
3678 executables, can't assume that the existence of a definition
3679 implies local binding), we can skip the re-testing. */
3680 return TRUE;
36a05131 3681
34208acf 3682 ret = default_binds_local_p_1 (fn, flag_pic);
36a05131 3683
34208acf
AO
3684 DECL_VISIBILITY (fn) = vis;
3685
3686 return ret;
3687}
3688
3689/* Load the _gp symbol into DEST. SRC is supposed to be the FDPIC
3690 register. */
36a05131
BS
3691
3692rtx
34208acf
AO
3693frv_gen_GPsym2reg (rtx dest, rtx src)
3694{
3695 tree gp = get_identifier ("_gp");
3696 rtx gp_sym = gen_rtx_SYMBOL_REF (Pmode, IDENTIFIER_POINTER (gp));
36a05131 3697
34208acf
AO
3698 return gen_symGOT2reg (dest, gp_sym, src, GEN_INT (R_FRV_GOT12));
3699}
3700
3701static const char *
3702unspec_got_name (int i)
3703{
3704 switch (i)
36a05131 3705 {
34208acf
AO
3706 case R_FRV_GOT12: return "got12";
3707 case R_FRV_GOTHI: return "gothi";
3708 case R_FRV_GOTLO: return "gotlo";
3709 case R_FRV_FUNCDESC: return "funcdesc";
3710 case R_FRV_FUNCDESC_GOT12: return "gotfuncdesc12";
3711 case R_FRV_FUNCDESC_GOTHI: return "gotfuncdeschi";
3712 case R_FRV_FUNCDESC_GOTLO: return "gotfuncdesclo";
3713 case R_FRV_FUNCDESC_VALUE: return "funcdescvalue";
3714 case R_FRV_FUNCDESC_GOTOFF12: return "gotofffuncdesc12";
3715 case R_FRV_FUNCDESC_GOTOFFHI: return "gotofffuncdeschi";
3716 case R_FRV_FUNCDESC_GOTOFFLO: return "gotofffuncdesclo";
3717 case R_FRV_GOTOFF12: return "gotoff12";
3718 case R_FRV_GOTOFFHI: return "gotoffhi";
3719 case R_FRV_GOTOFFLO: return "gotofflo";
3720 case R_FRV_GPREL12: return "gprel12";
3721 case R_FRV_GPRELHI: return "gprelhi";
3722 case R_FRV_GPRELLO: return "gprello";
bef8809e
AH
3723 case R_FRV_GOTTLSOFF_HI: return "gottlsoffhi";
3724 case R_FRV_GOTTLSOFF_LO: return "gottlsofflo";
3725 case R_FRV_TLSMOFFHI: return "tlsmoffhi";
3726 case R_FRV_TLSMOFFLO: return "tlsmofflo";
3727 case R_FRV_TLSMOFF12: return "tlsmoff12";
3728 case R_FRV_TLSDESCHI: return "tlsdeschi";
3729 case R_FRV_TLSDESCLO: return "tlsdesclo";
3730 case R_FRV_GOTTLSDESCHI: return "gottlsdeschi";
3731 case R_FRV_GOTTLSDESCLO: return "gottlsdesclo";
44e91694 3732 default: gcc_unreachable ();
36a05131 3733 }
34208acf 3734}
36a05131 3735
34208acf
AO
3736/* Write the assembler syntax for UNSPEC to STREAM. Note that any offset
3737 is added inside the relocation operator. */
3738
3739static void
3740frv_output_const_unspec (FILE *stream, const struct frv_unspec *unspec)
3741{
3742 fprintf (stream, "#%s(", unspec_got_name (unspec->reloc));
0a81f074
RS
3743 output_addr_const (stream, plus_constant (Pmode, unspec->symbol,
3744 unspec->offset));
34208acf
AO
3745 fputs (")", stream);
3746}
3747
3748/* Implement FIND_BASE_TERM. See whether ORIG_X represents #gprel12(foo)
3749 or #gotoff12(foo) for some small data symbol foo. If so, return foo,
3750 otherwise return ORIG_X. */
3751
3752rtx
3753frv_find_base_term (rtx x)
3754{
3755 struct frv_unspec unspec;
3756
3757 if (frv_const_unspec_p (x, &unspec)
3758 && frv_small_data_reloc_p (unspec.symbol, unspec.reloc))
0a81f074 3759 return plus_constant (Pmode, unspec.symbol, unspec.offset);
34208acf
AO
3760
3761 return x;
36a05131
BS
3762}
3763
3764/* Return 1 if operand is a valid FRV address. CONDEXEC_P is true if
3765 the operand is used by a predicated instruction. */
3766
6d26dc3b 3767int
ef4bddc2 3768frv_legitimate_memory_operand (rtx op, machine_mode mode, int condexec_p)
36a05131
BS
3769{
3770 return ((GET_MODE (op) == mode || mode == VOIDmode)
3771 && GET_CODE (op) == MEM
c6c3dba9
PB
3772 && frv_legitimate_address_p_1 (mode, XEXP (op, 0),
3773 reload_completed, condexec_p, FALSE));
34208acf
AO
3774}
3775
3776void
764678d1 3777frv_expand_fdpic_call (rtx *operands, bool ret_value, bool sibcall)
34208acf
AO
3778{
3779 rtx lr = gen_rtx_REG (Pmode, LR_REGNO);
3780 rtx picreg = get_hard_reg_initial_val (SImode, FDPIC_REG);
3781 rtx c, rvrtx=0;
3782 rtx addr;
3783
3784 if (ret_value)
3785 {
3786 rvrtx = operands[0];
3787 operands ++;
3788 }
3789
3790 addr = XEXP (operands[0], 0);
3791
3792 /* Inline PLTs if we're optimizing for speed. We'd like to inline
3793 any calls that would involve a PLT, but can't tell, since we
3794 don't know whether an extern function is going to be provided by
3795 a separate translation unit or imported from a separate module.
3796 When compiling for shared libraries, if the function has default
3797 visibility, we assume it's overridable, so we inline the PLT, but
3798 for executables, we don't really have a way to make a good
3799 decision: a function is as likely to be imported from a shared
3800 library as it is to be defined in the executable itself. We
3801 assume executables will get global functions defined locally,
3802 whereas shared libraries will have them potentially overridden,
3803 so we only inline PLTs when compiling for shared libraries.
3804
3805 In order to mark a function as local to a shared library, any
3806 non-default visibility attribute suffices. Unfortunately,
3807 there's no simple way to tag a function declaration as ``in a
3808 different module'', which we could then use to trigger PLT
3809 inlining on executables. There's -minline-plt, but it affects
3810 all external functions, so one would have to also mark function
3811 declarations available in the same module with non-default
3812 visibility, which is advantageous in itself. */
764678d1
AO
3813 if (GET_CODE (addr) == SYMBOL_REF
3814 && ((!SYMBOL_REF_LOCAL_P (addr) && TARGET_INLINE_PLT)
3815 || sibcall))
34208acf
AO
3816 {
3817 rtx x, dest;
3818 dest = gen_reg_rtx (SImode);
3819 if (flag_pic != 1)
3820 x = gen_symGOTOFF2reg_hilo (dest, addr, OUR_FDPIC_REG,
3821 GEN_INT (R_FRV_FUNCDESC_GOTOFF12));
3822 else
3823 x = gen_symGOTOFF2reg (dest, addr, OUR_FDPIC_REG,
3824 GEN_INT (R_FRV_FUNCDESC_GOTOFF12));
3825 emit_insn (x);
ad516a74 3826 crtl->uses_pic_offset_table = TRUE;
34208acf 3827 addr = dest;
2396bce1 3828 }
34208acf
AO
3829 else if (GET_CODE (addr) == SYMBOL_REF)
3830 {
3831 /* These are always either local, or handled through a local
3832 PLT. */
3833 if (ret_value)
3834 c = gen_call_value_fdpicsi (rvrtx, addr, operands[1],
3835 operands[2], picreg, lr);
3836 else
3837 c = gen_call_fdpicsi (addr, operands[1], operands[2], picreg, lr);
3838 emit_call_insn (c);
3839 return;
3840 }
3841 else if (! ldd_address_operand (addr, Pmode))
3842 addr = force_reg (Pmode, addr);
3843
3844 picreg = gen_reg_rtx (DImode);
3845 emit_insn (gen_movdi_ldd (picreg, addr));
3846
764678d1
AO
3847 if (sibcall && ret_value)
3848 c = gen_sibcall_value_fdpicdi (rvrtx, picreg, const0_rtx);
3849 else if (sibcall)
3850 c = gen_sibcall_fdpicdi (picreg, const0_rtx);
3851 else if (ret_value)
34208acf
AO
3852 c = gen_call_value_fdpicdi (rvrtx, picreg, const0_rtx, lr);
3853 else
3854 c = gen_call_fdpicdi (picreg, const0_rtx, lr);
3855 emit_call_insn (c);
36a05131 3856}
36a05131 3857\f
6d26dc3b
KH
3858/* Look for a SYMBOL_REF of a function in an rtx. We always want to
3859 process these separately from any offsets, such that we add any
3860 offsets to the function descriptor (the actual pointer), not to the
3861 function address. */
36a05131 3862
6d26dc3b
KH
3863static bool
3864frv_function_symbol_referenced_p (rtx x)
36a05131 3865{
6d26dc3b
KH
3866 const char *format;
3867 int length;
3868 int j;
36a05131 3869
6d26dc3b
KH
3870 if (GET_CODE (x) == SYMBOL_REF)
3871 return SYMBOL_REF_FUNCTION_P (x);
34208acf 3872
6d26dc3b
KH
3873 length = GET_RTX_LENGTH (GET_CODE (x));
3874 format = GET_RTX_FORMAT (GET_CODE (x));
36a05131 3875
6d26dc3b 3876 for (j = 0; j < length; ++j)
36a05131 3877 {
6d26dc3b
KH
3878 switch (format[j])
3879 {
3880 case 'e':
3881 if (frv_function_symbol_referenced_p (XEXP (x, j)))
3882 return TRUE;
3883 break;
36a05131 3884
6d26dc3b
KH
3885 case 'V':
3886 case 'E':
3887 if (XVEC (x, j) != 0)
3888 {
3889 int k;
3890 for (k = 0; k < XVECLEN (x, j); ++k)
3891 if (frv_function_symbol_referenced_p (XVECEXP (x, j, k)))
3892 return TRUE;
3893 }
3894 break;
36a05131 3895
6d26dc3b
KH
3896 default:
3897 /* Nothing to do. */
3898 break;
3899 }
36a05131
BS
3900 }
3901
36a05131
BS
3902 return FALSE;
3903}
3904
6d26dc3b
KH
3905/* Return true if the memory operand is one that can be conditionally
3906 executed. */
36a05131 3907
f2206911 3908int
ef4bddc2 3909condexec_memory_operand (rtx op, machine_mode mode)
36a05131 3910{
ef4bddc2 3911 machine_mode op_mode = GET_MODE (op);
6d26dc3b 3912 rtx addr;
36a05131 3913
6d26dc3b 3914 if (mode != VOIDmode && op_mode != mode)
36a05131
BS
3915 return FALSE;
3916
6d26dc3b 3917 switch (op_mode)
36a05131
BS
3918 {
3919 default:
6d26dc3b 3920 return FALSE;
36a05131 3921
6d26dc3b
KH
3922 case QImode:
3923 case HImode:
3924 case SImode:
3925 case SFmode:
36a05131
BS
3926 break;
3927 }
3928
6d26dc3b 3929 if (GET_CODE (op) != MEM)
36a05131
BS
3930 return FALSE;
3931
6d26dc3b 3932 addr = XEXP (op, 0);
c6c3dba9 3933 return frv_legitimate_address_p_1 (mode, addr, reload_completed, TRUE, FALSE);
36a05131 3934}
36a05131
BS
3935\f
3936/* Return true if the bare return instruction can be used outside of the
3937 epilog code. For frv, we only do it if there was no stack allocation. */
3938
3939int
f2206911 3940direct_return_p (void)
36a05131
BS
3941{
3942 frv_stack_t *info;
3943
3944 if (!reload_completed)
3945 return FALSE;
3946
3947 info = frv_stack_info ();
3948 return (info->total_size == 0);
3949}
3950
3951\f
2a2e3f05 3952void
ef4bddc2 3953frv_emit_move (machine_mode mode, rtx dest, rtx src)
2a2e3f05 3954{
bef8809e
AH
3955 if (GET_CODE (src) == SYMBOL_REF)
3956 {
3957 enum tls_model model = SYMBOL_REF_TLS_MODEL (src);
3958 if (model != 0)
3959 src = frv_legitimize_tls_address (src, model);
3960 }
3961
2a2e3f05
AH
3962 switch (mode)
3963 {
3964 case SImode:
3965 if (frv_emit_movsi (dest, src))
3966 return;
3967 break;
3968
3969 case QImode:
3970 case HImode:
3971 case DImode:
3972 case SFmode:
3973 case DFmode:
3974 if (!reload_in_progress
3975 && !reload_completed
3976 && !register_operand (dest, mode)
3977 && !reg_or_0_operand (src, mode))
3978 src = copy_to_mode_reg (mode, src);
3979 break;
3980
3981 default:
44e91694 3982 gcc_unreachable ();
2a2e3f05
AH
3983 }
3984
3985 emit_insn (gen_rtx_SET (VOIDmode, dest, src));
3986}
3987
36a05131
BS
3988/* Emit code to handle a MOVSI, adding in the small data register or pic
3989 register if needed to load up addresses. Return TRUE if the appropriate
3990 instructions are emitted. */
3991
3992int
f2206911 3993frv_emit_movsi (rtx dest, rtx src)
36a05131
BS
3994{
3995 int base_regno = -1;
34208acf
AO
3996 int unspec = 0;
3997 rtx sym = src;
3998 struct frv_unspec old_unspec;
36a05131
BS
3999
4000 if (!reload_in_progress
4001 && !reload_completed
4002 && !register_operand (dest, SImode)
4003 && (!reg_or_0_operand (src, SImode)
4004 /* Virtual registers will almost always be replaced by an
4005 add instruction, so expose this to CSE by copying to
87b483a1 4006 an intermediate register. */
36a05131 4007 || (GET_CODE (src) == REG
2f5b1308
JR
4008 && IN_RANGE (REGNO (src),
4009 FIRST_VIRTUAL_REGISTER,
4010 LAST_VIRTUAL_POINTER_REGISTER))))
36a05131
BS
4011 {
4012 emit_insn (gen_rtx_SET (VOIDmode, dest, copy_to_mode_reg (SImode, src)));
4013 return TRUE;
4014 }
4015
4016 /* Explicitly add in the PIC or small data register if needed. */
4017 switch (GET_CODE (src))
4018 {
4019 default:
4020 break;
4021
4022 case LABEL_REF:
34208acf
AO
4023 handle_label:
4024 if (TARGET_FDPIC)
4025 {
4026 /* Using GPREL12, we use a single GOT entry for all symbols
4027 in read-only sections, but trade sequences such as:
4028
4029 sethi #gothi(label), gr#
4030 setlo #gotlo(label), gr#
4031 ld @(gr15,gr#), gr#
4032
4033 for
4034
4035 ld @(gr15,#got12(_gp)), gr#
4036 sethi #gprelhi(label), gr##
4037 setlo #gprello(label), gr##
4038 add gr#, gr##, gr##
4039
4040 We may often be able to share gr# for multiple
4041 computations of GPREL addresses, and we may often fold
4042 the final add into the pair of registers of a load or
4043 store instruction, so it's often profitable. Even when
4044 optimizing for size, we're trading a GOT entry for an
4045 additional instruction, which trades GOT space
4046 (read-write) for code size (read-only, shareable), as
4047 long as the symbol is not used in more than two different
4048 locations.
2396bce1 4049
34208acf
AO
4050 With -fpie/-fpic, we'd be trading a single load for a
4051 sequence of 4 instructions, because the offset of the
4ee31f1e 4052 label can't be assumed to be addressable with 12 bits, so
34208acf
AO
4053 we don't do this. */
4054 if (TARGET_GPREL_RO)
4055 unspec = R_FRV_GPREL12;
4056 else
4057 unspec = R_FRV_GOT12;
4058 }
4059 else if (flag_pic)
36a05131
BS
4060 base_regno = PIC_REGNO;
4061
4062 break;
4063
4064 case CONST:
34208acf
AO
4065 if (frv_const_unspec_p (src, &old_unspec))
4066 break;
36a05131 4067
34208acf
AO
4068 if (TARGET_FDPIC && frv_function_symbol_referenced_p (XEXP (src, 0)))
4069 {
4070 handle_whatever:
4071 src = force_reg (GET_MODE (XEXP (src, 0)), XEXP (src, 0));
4072 emit_move_insn (dest, src);
4073 return TRUE;
4074 }
4075 else
4076 {
4077 sym = XEXP (sym, 0);
4078 if (GET_CODE (sym) == PLUS
4079 && GET_CODE (XEXP (sym, 0)) == SYMBOL_REF
4080 && GET_CODE (XEXP (sym, 1)) == CONST_INT)
4081 sym = XEXP (sym, 0);
4082 if (GET_CODE (sym) == SYMBOL_REF)
4083 goto handle_sym;
4084 else if (GET_CODE (sym) == LABEL_REF)
4085 goto handle_label;
4086 else
4087 goto handle_whatever;
4088 }
36a05131
BS
4089 break;
4090
4091 case SYMBOL_REF:
34208acf
AO
4092 handle_sym:
4093 if (TARGET_FDPIC)
4094 {
bef8809e
AH
4095 enum tls_model model = SYMBOL_REF_TLS_MODEL (sym);
4096
4097 if (model != 0)
4098 {
4099 src = frv_legitimize_tls_address (src, model);
4100 emit_move_insn (dest, src);
4101 return TRUE;
4102 }
4103
34208acf
AO
4104 if (SYMBOL_REF_FUNCTION_P (sym))
4105 {
4106 if (frv_local_funcdesc_p (sym))
4107 unspec = R_FRV_FUNCDESC_GOTOFF12;
4108 else
4109 unspec = R_FRV_FUNCDESC_GOT12;
4110 }
4111 else
4112 {
4113 if (CONSTANT_POOL_ADDRESS_P (sym))
4114 switch (GET_CODE (get_pool_constant (sym)))
4115 {
4116 case CONST:
4117 case SYMBOL_REF:
4118 case LABEL_REF:
4119 if (flag_pic)
4120 {
4121 unspec = R_FRV_GOTOFF12;
4122 break;
4123 }
4124 /* Fall through. */
4125 default:
4126 if (TARGET_GPREL_RO)
4127 unspec = R_FRV_GPREL12;
4128 else
4129 unspec = R_FRV_GOT12;
4130 break;
4131 }
4132 else if (SYMBOL_REF_LOCAL_P (sym)
4133 && !SYMBOL_REF_EXTERNAL_P (sym)
4134 && SYMBOL_REF_DECL (sym)
4135 && (!DECL_P (SYMBOL_REF_DECL (sym))
4136 || !DECL_COMMON (SYMBOL_REF_DECL (sym))))
4137 {
4138 tree decl = SYMBOL_REF_DECL (sym);
4139 tree init = TREE_CODE (decl) == VAR_DECL
4140 ? DECL_INITIAL (decl)
4141 : TREE_CODE (decl) == CONSTRUCTOR
4142 ? decl : 0;
4143 int reloc = 0;
4144 bool named_section, readonly;
4145
4146 if (init && init != error_mark_node)
4147 reloc = compute_reloc_for_constant (init);
2396bce1 4148
34208acf
AO
4149 named_section = TREE_CODE (decl) == VAR_DECL
4150 && lookup_attribute ("section", DECL_ATTRIBUTES (decl));
4151 readonly = decl_readonly_section (decl, reloc);
2396bce1 4152
34208acf
AO
4153 if (named_section)
4154 unspec = R_FRV_GOT12;
4155 else if (!readonly)
4156 unspec = R_FRV_GOTOFF12;
4157 else if (readonly && TARGET_GPREL_RO)
4158 unspec = R_FRV_GPREL12;
4159 else
4160 unspec = R_FRV_GOT12;
4161 }
4162 else
4163 unspec = R_FRV_GOT12;
4164 }
4165 }
4166
4167 else if (SYMBOL_REF_SMALL_P (sym))
36a05131
BS
4168 base_regno = SDA_BASE_REG;
4169
4170 else if (flag_pic)
4171 base_regno = PIC_REGNO;
4172
4173 break;
4174 }
4175
4176 if (base_regno >= 0)
4177 {
34208acf
AO
4178 if (GET_CODE (sym) == SYMBOL_REF && SYMBOL_REF_SMALL_P (sym))
4179 emit_insn (gen_symGOTOFF2reg (dest, src,
4180 gen_rtx_REG (Pmode, base_regno),
4181 GEN_INT (R_FRV_GPREL12)));
4182 else
4183 emit_insn (gen_symGOTOFF2reg_hilo (dest, src,
4184 gen_rtx_REG (Pmode, base_regno),
4185 GEN_INT (R_FRV_GPREL12)));
36a05131 4186 if (base_regno == PIC_REGNO)
ad516a74 4187 crtl->uses_pic_offset_table = TRUE;
34208acf
AO
4188 return TRUE;
4189 }
36a05131 4190
34208acf
AO
4191 if (unspec)
4192 {
4193 rtx x;
4194
4195 /* Since OUR_FDPIC_REG is a pseudo register, we can't safely introduce
4196 new uses of it once reload has begun. */
44e91694 4197 gcc_assert (!reload_in_progress && !reload_completed);
34208acf
AO
4198
4199 switch (unspec)
4200 {
4201 case R_FRV_GOTOFF12:
4202 if (!frv_small_data_reloc_p (sym, unspec))
4203 x = gen_symGOTOFF2reg_hilo (dest, src, OUR_FDPIC_REG,
4204 GEN_INT (unspec));
4205 else
4206 x = gen_symGOTOFF2reg (dest, src, OUR_FDPIC_REG, GEN_INT (unspec));
4207 break;
4208 case R_FRV_GPREL12:
4209 if (!frv_small_data_reloc_p (sym, unspec))
4210 x = gen_symGPREL2reg_hilo (dest, src, OUR_FDPIC_REG,
4211 GEN_INT (unspec));
4212 else
4213 x = gen_symGPREL2reg (dest, src, OUR_FDPIC_REG, GEN_INT (unspec));
4214 break;
4215 case R_FRV_FUNCDESC_GOTOFF12:
4216 if (flag_pic != 1)
4217 x = gen_symGOTOFF2reg_hilo (dest, src, OUR_FDPIC_REG,
4218 GEN_INT (unspec));
4219 else
4220 x = gen_symGOTOFF2reg (dest, src, OUR_FDPIC_REG, GEN_INT (unspec));
4221 break;
4222 default:
4223 if (flag_pic != 1)
4224 x = gen_symGOT2reg_hilo (dest, src, OUR_FDPIC_REG,
4225 GEN_INT (unspec));
4226 else
4227 x = gen_symGOT2reg (dest, src, OUR_FDPIC_REG, GEN_INT (unspec));
4228 break;
4229 }
4230 emit_insn (x);
ad516a74 4231 crtl->uses_pic_offset_table = TRUE;
36a05131
BS
4232 return TRUE;
4233 }
4234
34208acf 4235
36a05131
BS
4236 return FALSE;
4237}
4238
4239\f
4240/* Return a string to output a single word move. */
4241
4242const char *
f2206911 4243output_move_single (rtx operands[], rtx insn)
36a05131
BS
4244{
4245 rtx dest = operands[0];
4246 rtx src = operands[1];
4247
4248 if (GET_CODE (dest) == REG)
4249 {
4250 int dest_regno = REGNO (dest);
ef4bddc2 4251 machine_mode mode = GET_MODE (dest);
36a05131
BS
4252
4253 if (GPR_P (dest_regno))
4254 {
4255 if (GET_CODE (src) == REG)
4256 {
4257 /* gpr <- some sort of register */
4258 int src_regno = REGNO (src);
4259
4260 if (GPR_P (src_regno))
4261 return "mov %1, %0";
4262
4263 else if (FPR_P (src_regno))
4264 return "movfg %1, %0";
4265
4266 else if (SPR_P (src_regno))
4267 return "movsg %1, %0";
4268 }
4269
4270 else if (GET_CODE (src) == MEM)
4271 {
4272 /* gpr <- memory */
4273 switch (mode)
4274 {
4275 default:
4276 break;
4277
4278 case QImode:
4279 return "ldsb%I1%U1 %M1,%0";
4280
4281 case HImode:
4282 return "ldsh%I1%U1 %M1,%0";
4283
4284 case SImode:
4285 case SFmode:
4286 return "ld%I1%U1 %M1, %0";
4287 }
4288 }
4289
4290 else if (GET_CODE (src) == CONST_INT
4291 || GET_CODE (src) == CONST_DOUBLE)
4292 {
4293 /* gpr <- integer/floating constant */
4294 HOST_WIDE_INT value;
4295
4296 if (GET_CODE (src) == CONST_INT)
4297 value = INTVAL (src);
4298
4299 else if (mode == SFmode)
4300 {
4301 REAL_VALUE_TYPE rv;
4302 long l;
4303
4304 REAL_VALUE_FROM_CONST_DOUBLE (rv, src);
4305 REAL_VALUE_TO_TARGET_SINGLE (rv, l);
4306 value = l;
4307 }
4308
4309 else
4310 value = CONST_DOUBLE_LOW (src);
4311
2f5b1308 4312 if (IN_RANGE (value, -32768, 32767))
36a05131
BS
4313 return "setlos %1, %0";
4314
4315 return "#";
4316 }
4317
4318 else if (GET_CODE (src) == SYMBOL_REF
4319 || GET_CODE (src) == LABEL_REF
4320 || GET_CODE (src) == CONST)
4321 {
36a05131
BS
4322 return "#";
4323 }
4324 }
4325
4326 else if (FPR_P (dest_regno))
4327 {
4328 if (GET_CODE (src) == REG)
4329 {
4330 /* fpr <- some sort of register */
4331 int src_regno = REGNO (src);
4332
4333 if (GPR_P (src_regno))
4334 return "movgf %1, %0";
4335
4336 else if (FPR_P (src_regno))
4337 {
4338 if (TARGET_HARD_FLOAT)
4339 return "fmovs %1, %0";
4340 else
4341 return "mor %1, %1, %0";
4342 }
4343 }
4344
4345 else if (GET_CODE (src) == MEM)
4346 {
4347 /* fpr <- memory */
4348 switch (mode)
4349 {
4350 default:
4351 break;
4352
4353 case QImode:
4354 return "ldbf%I1%U1 %M1,%0";
4355
4356 case HImode:
4357 return "ldhf%I1%U1 %M1,%0";
4358
4359 case SImode:
4360 case SFmode:
4361 return "ldf%I1%U1 %M1, %0";
4362 }
4363 }
4364
4365 else if (ZERO_P (src))
4366 return "movgf %., %0";
4367 }
4368
4369 else if (SPR_P (dest_regno))
4370 {
4371 if (GET_CODE (src) == REG)
4372 {
4373 /* spr <- some sort of register */
4374 int src_regno = REGNO (src);
4375
4376 if (GPR_P (src_regno))
4377 return "movgs %1, %0";
4378 }
c557edf4
RS
4379 else if (ZERO_P (src))
4380 return "movgs %., %0";
36a05131
BS
4381 }
4382 }
4383
4384 else if (GET_CODE (dest) == MEM)
4385 {
4386 if (GET_CODE (src) == REG)
4387 {
4388 int src_regno = REGNO (src);
ef4bddc2 4389 machine_mode mode = GET_MODE (dest);
36a05131
BS
4390
4391 if (GPR_P (src_regno))
4392 {
4393 switch (mode)
4394 {
4395 default:
4396 break;
4397
4398 case QImode:
4399 return "stb%I0%U0 %1, %M0";
4400
4401 case HImode:
4402 return "sth%I0%U0 %1, %M0";
4403
4404 case SImode:
4405 case SFmode:
4406 return "st%I0%U0 %1, %M0";
4407 }
4408 }
4409
4410 else if (FPR_P (src_regno))
4411 {
4412 switch (mode)
4413 {
4414 default:
4415 break;
4416
4417 case QImode:
4418 return "stbf%I0%U0 %1, %M0";
4419
4420 case HImode:
4421 return "sthf%I0%U0 %1, %M0";
4422
4423 case SImode:
4424 case SFmode:
4425 return "stf%I0%U0 %1, %M0";
4426 }
4427 }
4428 }
4429
4430 else if (ZERO_P (src))
4431 {
4432 switch (GET_MODE (dest))
4433 {
4434 default:
4435 break;
4436
4437 case QImode:
4438 return "stb%I0%U0 %., %M0";
4439
4440 case HImode:
4441 return "sth%I0%U0 %., %M0";
4442
4443 case SImode:
4444 case SFmode:
4445 return "st%I0%U0 %., %M0";
4446 }
4447 }
4448 }
4449
ab532386 4450 fatal_insn ("bad output_move_single operand", insn);
36a05131
BS
4451 return "";
4452}
4453
4454\f
4455/* Return a string to output a double word move. */
4456
4457const char *
f2206911 4458output_move_double (rtx operands[], rtx insn)
36a05131
BS
4459{
4460 rtx dest = operands[0];
4461 rtx src = operands[1];
ef4bddc2 4462 machine_mode mode = GET_MODE (dest);
36a05131
BS
4463
4464 if (GET_CODE (dest) == REG)
4465 {
4466 int dest_regno = REGNO (dest);
4467
4468 if (GPR_P (dest_regno))
4469 {
4470 if (GET_CODE (src) == REG)
4471 {
4472 /* gpr <- some sort of register */
4473 int src_regno = REGNO (src);
4474
4475 if (GPR_P (src_regno))
4476 return "#";
4477
4478 else if (FPR_P (src_regno))
4479 {
4480 if (((dest_regno - GPR_FIRST) & 1) == 0
4481 && ((src_regno - FPR_FIRST) & 1) == 0)
4482 return "movfgd %1, %0";
4483
4484 return "#";
4485 }
4486 }
4487
4488 else if (GET_CODE (src) == MEM)
4489 {
4490 /* gpr <- memory */
4491 if (dbl_memory_one_insn_operand (src, mode))
4492 return "ldd%I1%U1 %M1, %0";
4493
4494 return "#";
4495 }
4496
4497 else if (GET_CODE (src) == CONST_INT
4498 || GET_CODE (src) == CONST_DOUBLE)
4499 return "#";
4500 }
4501
4502 else if (FPR_P (dest_regno))
4503 {
4504 if (GET_CODE (src) == REG)
4505 {
4506 /* fpr <- some sort of register */
4507 int src_regno = REGNO (src);
4508
4509 if (GPR_P (src_regno))
4510 {
4511 if (((dest_regno - FPR_FIRST) & 1) == 0
4512 && ((src_regno - GPR_FIRST) & 1) == 0)
4513 return "movgfd %1, %0";
4514
4515 return "#";
4516 }
4517
4518 else if (FPR_P (src_regno))
4519 {
4520 if (TARGET_DOUBLE
4521 && ((dest_regno - FPR_FIRST) & 1) == 0
4522 && ((src_regno - FPR_FIRST) & 1) == 0)
4523 return "fmovd %1, %0";
4524
4525 return "#";
4526 }
4527 }
4528
4529 else if (GET_CODE (src) == MEM)
4530 {
4531 /* fpr <- memory */
4532 if (dbl_memory_one_insn_operand (src, mode))
4533 return "lddf%I1%U1 %M1, %0";
4534
4535 return "#";
4536 }
4537
4538 else if (ZERO_P (src))
4539 return "#";
4540 }
4541 }
4542
4543 else if (GET_CODE (dest) == MEM)
4544 {
4545 if (GET_CODE (src) == REG)
4546 {
4547 int src_regno = REGNO (src);
4548
4549 if (GPR_P (src_regno))
4550 {
4551 if (((src_regno - GPR_FIRST) & 1) == 0
4552 && dbl_memory_one_insn_operand (dest, mode))
4553 return "std%I0%U0 %1, %M0";
4554
4555 return "#";
4556 }
4557
4558 if (FPR_P (src_regno))
4559 {
4560 if (((src_regno - FPR_FIRST) & 1) == 0
4561 && dbl_memory_one_insn_operand (dest, mode))
4562 return "stdf%I0%U0 %1, %M0";
4563
4564 return "#";
4565 }
4566 }
4567
4568 else if (ZERO_P (src))
4569 {
4570 if (dbl_memory_one_insn_operand (dest, mode))
4571 return "std%I0%U0 %., %M0";
4572
4573 return "#";
4574 }
4575 }
4576
ab532386 4577 fatal_insn ("bad output_move_double operand", insn);
36a05131
BS
4578 return "";
4579}
4580
4581\f
4582/* Return a string to output a single word conditional move.
4583 Operand0 -- EQ/NE of ccr register and 0
4584 Operand1 -- CCR register
4585 Operand2 -- destination
4586 Operand3 -- source */
4587
4588const char *
f2206911 4589output_condmove_single (rtx operands[], rtx insn)
36a05131
BS
4590{
4591 rtx dest = operands[2];
4592 rtx src = operands[3];
4593
4594 if (GET_CODE (dest) == REG)
4595 {
4596 int dest_regno = REGNO (dest);
ef4bddc2 4597 machine_mode mode = GET_MODE (dest);
36a05131
BS
4598
4599 if (GPR_P (dest_regno))
4600 {
4601 if (GET_CODE (src) == REG)
4602 {
4603 /* gpr <- some sort of register */
4604 int src_regno = REGNO (src);
4605
4606 if (GPR_P (src_regno))
4607 return "cmov %z3, %2, %1, %e0";
4608
4609 else if (FPR_P (src_regno))
4610 return "cmovfg %3, %2, %1, %e0";
4611 }
4612
4613 else if (GET_CODE (src) == MEM)
4614 {
4615 /* gpr <- memory */
4616 switch (mode)
4617 {
4618 default:
4619 break;
4620
4621 case QImode:
4622 return "cldsb%I3%U3 %M3, %2, %1, %e0";
4623
4624 case HImode:
4625 return "cldsh%I3%U3 %M3, %2, %1, %e0";
4626
4627 case SImode:
4628 case SFmode:
4629 return "cld%I3%U3 %M3, %2, %1, %e0";
4630 }
4631 }
4632
4633 else if (ZERO_P (src))
4634 return "cmov %., %2, %1, %e0";
4635 }
4636
4637 else if (FPR_P (dest_regno))
4638 {
4639 if (GET_CODE (src) == REG)
4640 {
4641 /* fpr <- some sort of register */
4642 int src_regno = REGNO (src);
4643
4644 if (GPR_P (src_regno))
4645 return "cmovgf %3, %2, %1, %e0";
4646
4647 else if (FPR_P (src_regno))
4648 {
4649 if (TARGET_HARD_FLOAT)
4650 return "cfmovs %3,%2,%1,%e0";
4651 else
4652 return "cmor %3, %3, %2, %1, %e0";
4653 }
4654 }
4655
4656 else if (GET_CODE (src) == MEM)
4657 {
4658 /* fpr <- memory */
4659 if (mode == SImode || mode == SFmode)
4660 return "cldf%I3%U3 %M3, %2, %1, %e0";
4661 }
4662
4663 else if (ZERO_P (src))
4664 return "cmovgf %., %2, %1, %e0";
4665 }
4666 }
4667
4668 else if (GET_CODE (dest) == MEM)
4669 {
4670 if (GET_CODE (src) == REG)
4671 {
4672 int src_regno = REGNO (src);
ef4bddc2 4673 machine_mode mode = GET_MODE (dest);
36a05131
BS
4674
4675 if (GPR_P (src_regno))
4676 {
4677 switch (mode)
4678 {
4679 default:
4680 break;
4681
4682 case QImode:
4683 return "cstb%I2%U2 %3, %M2, %1, %e0";
4684
4685 case HImode:
4686 return "csth%I2%U2 %3, %M2, %1, %e0";
4687
4688 case SImode:
4689 case SFmode:
4690 return "cst%I2%U2 %3, %M2, %1, %e0";
4691 }
4692 }
4693
4694 else if (FPR_P (src_regno) && (mode == SImode || mode == SFmode))
4695 return "cstf%I2%U2 %3, %M2, %1, %e0";
4696 }
4697
4698 else if (ZERO_P (src))
4699 {
ef4bddc2 4700 machine_mode mode = GET_MODE (dest);
36a05131
BS
4701 switch (mode)
4702 {
4703 default:
4704 break;
4705
4706 case QImode:
4707 return "cstb%I2%U2 %., %M2, %1, %e0";
4708
4709 case HImode:
4710 return "csth%I2%U2 %., %M2, %1, %e0";
4711
4712 case SImode:
4713 case SFmode:
4714 return "cst%I2%U2 %., %M2, %1, %e0";
4715 }
4716 }
4717 }
4718
ab532386 4719 fatal_insn ("bad output_condmove_single operand", insn);
36a05131
BS
4720 return "";
4721}
4722
4723\f
4724/* Emit the appropriate code to do a comparison, returning the register the
4725 comparison was done it. */
4726
4727static rtx
f2206911 4728frv_emit_comparison (enum rtx_code test, rtx op0, rtx op1)
36a05131 4729{
ef4bddc2 4730 machine_mode cc_mode;
36a05131
BS
4731 rtx cc_reg;
4732
87b483a1 4733 /* Floating point doesn't have comparison against a constant. */
36a05131
BS
4734 if (GET_MODE (op0) == CC_FPmode && GET_CODE (op1) != REG)
4735 op1 = force_reg (GET_MODE (op0), op1);
4736
4737 /* Possibly disable using anything but a fixed register in order to work
4738 around cse moving comparisons past function calls. */
4739 cc_mode = SELECT_CC_MODE (test, op0, op1);
4740 cc_reg = ((TARGET_ALLOC_CC)
4741 ? gen_reg_rtx (cc_mode)
4742 : gen_rtx_REG (cc_mode,
4743 (cc_mode == CC_FPmode) ? FCC_FIRST : ICC_FIRST));
4744
4745 emit_insn (gen_rtx_SET (VOIDmode, cc_reg,
4746 gen_rtx_COMPARE (cc_mode, op0, op1)));
4747
4748 return cc_reg;
4749}
4750
4751\f
f90b7a5a 4752/* Emit code for a conditional branch.
36a05131
BS
4753 XXX: I originally wanted to add a clobber of a CCR register to use in
4754 conditional execution, but that confuses the rest of the compiler. */
4755
4756int
f90b7a5a 4757frv_emit_cond_branch (rtx operands[])
36a05131
BS
4758{
4759 rtx test_rtx;
4760 rtx label_ref;
4761 rtx if_else;
f90b7a5a
PB
4762 enum rtx_code test = GET_CODE (operands[0]);
4763 rtx cc_reg = frv_emit_comparison (test, operands[1], operands[2]);
ef4bddc2 4764 machine_mode cc_mode = GET_MODE (cc_reg);
36a05131
BS
4765
4766 /* Branches generate:
4767 (set (pc)
4768 (if_then_else (<test>, <cc_reg>, (const_int 0))
4769 (label_ref <branch_label>)
4770 (pc))) */
f90b7a5a 4771 label_ref = gen_rtx_LABEL_REF (VOIDmode, operands[3]);
1c563bed 4772 test_rtx = gen_rtx_fmt_ee (test, cc_mode, cc_reg, const0_rtx);
36a05131
BS
4773 if_else = gen_rtx_IF_THEN_ELSE (cc_mode, test_rtx, label_ref, pc_rtx);
4774 emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, if_else));
4775 return TRUE;
4776}
4777
4778\f
f90b7a5a 4779/* Emit code to set a gpr to 1/0 based on a comparison. */
36a05131
BS
4780
4781int
f90b7a5a 4782frv_emit_scc (rtx operands[])
36a05131
BS
4783{
4784 rtx set;
4785 rtx test_rtx;
4786 rtx clobber;
4787 rtx cr_reg;
f90b7a5a
PB
4788 enum rtx_code test = GET_CODE (operands[1]);
4789 rtx cc_reg = frv_emit_comparison (test, operands[2], operands[3]);
36a05131
BS
4790
4791 /* SCC instructions generate:
4792 (parallel [(set <target> (<test>, <cc_reg>, (const_int 0))
4793 (clobber (<ccr_reg>))]) */
4794 test_rtx = gen_rtx_fmt_ee (test, SImode, cc_reg, const0_rtx);
f90b7a5a 4795 set = gen_rtx_SET (VOIDmode, operands[0], test_rtx);
36a05131
BS
4796
4797 cr_reg = ((TARGET_ALLOC_CC)
4798 ? gen_reg_rtx (CC_CCRmode)
4799 : gen_rtx_REG (CC_CCRmode,
4800 ((GET_MODE (cc_reg) == CC_FPmode)
4801 ? FCR_FIRST
4802 : ICR_FIRST)));
4803
4804 clobber = gen_rtx_CLOBBER (VOIDmode, cr_reg);
4805 emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, set, clobber)));
4806 return TRUE;
4807}
4808
4809\f
4810/* Split a SCC instruction into component parts, returning a SEQUENCE to hold
839a4992 4811 the separate insns. */
36a05131
BS
4812
4813rtx
f2206911 4814frv_split_scc (rtx dest, rtx test, rtx cc_reg, rtx cr_reg, HOST_WIDE_INT value)
36a05131
BS
4815{
4816 rtx ret;
4817
4818 start_sequence ();
4819
4820 /* Set the appropriate CCR bit. */
4821 emit_insn (gen_rtx_SET (VOIDmode,
4822 cr_reg,
4823 gen_rtx_fmt_ee (GET_CODE (test),
4824 GET_MODE (cr_reg),
4825 cc_reg,
4826 const0_rtx)));
4827
4828 /* Move the value into the destination. */
4829 emit_move_insn (dest, GEN_INT (value));
4830
4831 /* Move 0 into the destination if the test failed */
4832 emit_insn (gen_rtx_COND_EXEC (VOIDmode,
4833 gen_rtx_EQ (GET_MODE (cr_reg),
4834 cr_reg,
4835 const0_rtx),
4836 gen_rtx_SET (VOIDmode, dest, const0_rtx)));
4837
4838 /* Finish up, return sequence. */
4839 ret = get_insns ();
4840 end_sequence ();
4841 return ret;
4842}
4843
4844\f
4845/* Emit the code for a conditional move, return TRUE if we could do the
4846 move. */
4847
4848int
f2206911 4849frv_emit_cond_move (rtx dest, rtx test_rtx, rtx src1, rtx src2)
36a05131
BS
4850{
4851 rtx set;
4852 rtx clobber_cc;
4853 rtx test2;
4854 rtx cr_reg;
4855 rtx if_rtx;
4856 enum rtx_code test = GET_CODE (test_rtx);
f90b7a5a
PB
4857 rtx cc_reg = frv_emit_comparison (test,
4858 XEXP (test_rtx, 0), XEXP (test_rtx, 1));
ef4bddc2 4859 machine_mode cc_mode = GET_MODE (cc_reg);
36a05131
BS
4860
4861 /* Conditional move instructions generate:
4862 (parallel [(set <target>
4863 (if_then_else (<test> <cc_reg> (const_int 0))
4864 <src1>
4865 <src2>))
4866 (clobber (<ccr_reg>))]) */
4867
4868 /* Handle various cases of conditional move involving two constants. */
4869 if (GET_CODE (src1) == CONST_INT && GET_CODE (src2) == CONST_INT)
4870 {
4871 HOST_WIDE_INT value1 = INTVAL (src1);
4872 HOST_WIDE_INT value2 = INTVAL (src2);
4873
87b483a1 4874 /* Having 0 as one of the constants can be done by loading the other
36a05131
BS
4875 constant, and optionally moving in gr0. */
4876 if (value1 == 0 || value2 == 0)
4877 ;
4878
4879 /* If the first value is within an addi range and also the difference
4880 between the two fits in an addi's range, load up the difference, then
4881 conditionally move in 0, and then unconditionally add the first
4882 value. */
2f5b1308
JR
4883 else if (IN_RANGE (value1, -2048, 2047)
4884 && IN_RANGE (value2 - value1, -2048, 2047))
36a05131
BS
4885 ;
4886
4887 /* If neither condition holds, just force the constant into a
4888 register. */
4889 else
4890 {
4891 src1 = force_reg (GET_MODE (dest), src1);
4892 src2 = force_reg (GET_MODE (dest), src2);
4893 }
4894 }
4895
4896 /* If one value is a register, insure the other value is either 0 or a
4897 register. */
4898 else
4899 {
4900 if (GET_CODE (src1) == CONST_INT && INTVAL (src1) != 0)
4901 src1 = force_reg (GET_MODE (dest), src1);
4902
4903 if (GET_CODE (src2) == CONST_INT && INTVAL (src2) != 0)
4904 src2 = force_reg (GET_MODE (dest), src2);
4905 }
4906
4907 test2 = gen_rtx_fmt_ee (test, cc_mode, cc_reg, const0_rtx);
4908 if_rtx = gen_rtx_IF_THEN_ELSE (GET_MODE (dest), test2, src1, src2);
4909
4910 set = gen_rtx_SET (VOIDmode, dest, if_rtx);
4911
4912 cr_reg = ((TARGET_ALLOC_CC)
4913 ? gen_reg_rtx (CC_CCRmode)
4914 : gen_rtx_REG (CC_CCRmode,
4915 (cc_mode == CC_FPmode) ? FCR_FIRST : ICR_FIRST));
4916
4917 clobber_cc = gen_rtx_CLOBBER (VOIDmode, cr_reg);
4918 emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, set, clobber_cc)));
4919 return TRUE;
4920}
4921
4922\f
839a4992 4923/* Split a conditional move into constituent parts, returning a SEQUENCE
36a05131
BS
4924 containing all of the insns. */
4925
4926rtx
f2206911 4927frv_split_cond_move (rtx operands[])
36a05131
BS
4928{
4929 rtx dest = operands[0];
4930 rtx test = operands[1];
4931 rtx cc_reg = operands[2];
4932 rtx src1 = operands[3];
4933 rtx src2 = operands[4];
4934 rtx cr_reg = operands[5];
4935 rtx ret;
ef4bddc2 4936 machine_mode cr_mode = GET_MODE (cr_reg);
36a05131
BS
4937
4938 start_sequence ();
4939
4940 /* Set the appropriate CCR bit. */
4941 emit_insn (gen_rtx_SET (VOIDmode,
4942 cr_reg,
4943 gen_rtx_fmt_ee (GET_CODE (test),
4944 GET_MODE (cr_reg),
4945 cc_reg,
4946 const0_rtx)));
4947
4948 /* Handle various cases of conditional move involving two constants. */
4949 if (GET_CODE (src1) == CONST_INT && GET_CODE (src2) == CONST_INT)
4950 {
4951 HOST_WIDE_INT value1 = INTVAL (src1);
4952 HOST_WIDE_INT value2 = INTVAL (src2);
4953
87b483a1 4954 /* Having 0 as one of the constants can be done by loading the other
36a05131
BS
4955 constant, and optionally moving in gr0. */
4956 if (value1 == 0)
4957 {
4958 emit_move_insn (dest, src2);
4959 emit_insn (gen_rtx_COND_EXEC (VOIDmode,
4960 gen_rtx_NE (cr_mode, cr_reg,
4961 const0_rtx),
4962 gen_rtx_SET (VOIDmode, dest, src1)));
4963 }
4964
4965 else if (value2 == 0)
4966 {
4967 emit_move_insn (dest, src1);
4968 emit_insn (gen_rtx_COND_EXEC (VOIDmode,
4969 gen_rtx_EQ (cr_mode, cr_reg,
4970 const0_rtx),
4971 gen_rtx_SET (VOIDmode, dest, src2)));
4972 }
4973
4974 /* If the first value is within an addi range and also the difference
4975 between the two fits in an addi's range, load up the difference, then
4976 conditionally move in 0, and then unconditionally add the first
4977 value. */
2f5b1308
JR
4978 else if (IN_RANGE (value1, -2048, 2047)
4979 && IN_RANGE (value2 - value1, -2048, 2047))
36a05131
BS
4980 {
4981 rtx dest_si = ((GET_MODE (dest) == SImode)
4982 ? dest
4983 : gen_rtx_SUBREG (SImode, dest, 0));
4984
4985 emit_move_insn (dest_si, GEN_INT (value2 - value1));
4986 emit_insn (gen_rtx_COND_EXEC (VOIDmode,
4987 gen_rtx_NE (cr_mode, cr_reg,
4988 const0_rtx),
4989 gen_rtx_SET (VOIDmode, dest_si,
4990 const0_rtx)));
4991 emit_insn (gen_addsi3 (dest_si, dest_si, src1));
4992 }
4993
4994 else
44e91694 4995 gcc_unreachable ();
36a05131
BS
4996 }
4997 else
4998 {
4999 /* Emit the conditional move for the test being true if needed. */
5000 if (! rtx_equal_p (dest, src1))
5001 emit_insn (gen_rtx_COND_EXEC (VOIDmode,
5002 gen_rtx_NE (cr_mode, cr_reg, const0_rtx),
5003 gen_rtx_SET (VOIDmode, dest, src1)));
5004
5005 /* Emit the conditional move for the test being false if needed. */
5006 if (! rtx_equal_p (dest, src2))
5007 emit_insn (gen_rtx_COND_EXEC (VOIDmode,
5008 gen_rtx_EQ (cr_mode, cr_reg, const0_rtx),
5009 gen_rtx_SET (VOIDmode, dest, src2)));
5010 }
5011
5012 /* Finish up, return sequence. */
5013 ret = get_insns ();
5014 end_sequence ();
5015 return ret;
5016}
5017
5018\f
5019/* Split (set DEST SOURCE), where DEST is a double register and SOURCE is a
5020 memory location that is not known to be dword-aligned. */
5021void
f2206911 5022frv_split_double_load (rtx dest, rtx source)
36a05131
BS
5023{
5024 int regno = REGNO (dest);
5025 rtx dest1 = gen_highpart (SImode, dest);
5026 rtx dest2 = gen_lowpart (SImode, dest);
5027 rtx address = XEXP (source, 0);
5028
5029 /* If the address is pre-modified, load the lower-numbered register
5030 first, then load the other register using an integer offset from
5031 the modified base register. This order should always be safe,
5032 since the pre-modification cannot affect the same registers as the
5033 load does.
5034
5035 The situation for other loads is more complicated. Loading one
5036 of the registers could affect the value of ADDRESS, so we must
5037 be careful which order we do them in. */
5038 if (GET_CODE (address) == PRE_MODIFY
5039 || ! refers_to_regno_p (regno, regno + 1, address, NULL))
5040 {
5041 /* It is safe to load the lower-numbered register first. */
5042 emit_move_insn (dest1, change_address (source, SImode, NULL));
5043 emit_move_insn (dest2, frv_index_memory (source, SImode, 1));
5044 }
5045 else
5046 {
5047 /* ADDRESS is not pre-modified and the address depends on the
5048 lower-numbered register. Load the higher-numbered register
5049 first. */
5050 emit_move_insn (dest2, frv_index_memory (source, SImode, 1));
5051 emit_move_insn (dest1, change_address (source, SImode, NULL));
5052 }
5053}
5054
5055/* Split (set DEST SOURCE), where DEST refers to a dword memory location
5056 and SOURCE is either a double register or the constant zero. */
5057void
f2206911 5058frv_split_double_store (rtx dest, rtx source)
36a05131
BS
5059{
5060 rtx dest1 = change_address (dest, SImode, NULL);
5061 rtx dest2 = frv_index_memory (dest, SImode, 1);
5062 if (ZERO_P (source))
5063 {
5064 emit_move_insn (dest1, CONST0_RTX (SImode));
5065 emit_move_insn (dest2, CONST0_RTX (SImode));
5066 }
5067 else
5068 {
5069 emit_move_insn (dest1, gen_highpart (SImode, source));
5070 emit_move_insn (dest2, gen_lowpart (SImode, source));
5071 }
5072}
5073
5074\f
5075/* Split a min/max operation returning a SEQUENCE containing all of the
5076 insns. */
5077
5078rtx
f2206911 5079frv_split_minmax (rtx operands[])
36a05131
BS
5080{
5081 rtx dest = operands[0];
5082 rtx minmax = operands[1];
5083 rtx src1 = operands[2];
5084 rtx src2 = operands[3];
5085 rtx cc_reg = operands[4];
5086 rtx cr_reg = operands[5];
5087 rtx ret;
5088 enum rtx_code test_code;
ef4bddc2 5089 machine_mode cr_mode = GET_MODE (cr_reg);
36a05131
BS
5090
5091 start_sequence ();
5092
87b483a1 5093 /* Figure out which test to use. */
36a05131
BS
5094 switch (GET_CODE (minmax))
5095 {
5096 default:
44e91694 5097 gcc_unreachable ();
36a05131
BS
5098
5099 case SMIN: test_code = LT; break;
5100 case SMAX: test_code = GT; break;
5101 case UMIN: test_code = LTU; break;
5102 case UMAX: test_code = GTU; break;
5103 }
5104
5105 /* Issue the compare instruction. */
5106 emit_insn (gen_rtx_SET (VOIDmode,
5107 cc_reg,
5108 gen_rtx_COMPARE (GET_MODE (cc_reg),
5109 src1, src2)));
5110
5111 /* Set the appropriate CCR bit. */
5112 emit_insn (gen_rtx_SET (VOIDmode,
5113 cr_reg,
5114 gen_rtx_fmt_ee (test_code,
5115 GET_MODE (cr_reg),
5116 cc_reg,
5117 const0_rtx)));
5118
9cd10576 5119 /* If are taking the min/max of a nonzero constant, load that first, and
36a05131
BS
5120 then do a conditional move of the other value. */
5121 if (GET_CODE (src2) == CONST_INT && INTVAL (src2) != 0)
5122 {
44e91694 5123 gcc_assert (!rtx_equal_p (dest, src1));
36a05131
BS
5124
5125 emit_move_insn (dest, src2);
5126 emit_insn (gen_rtx_COND_EXEC (VOIDmode,
5127 gen_rtx_NE (cr_mode, cr_reg, const0_rtx),
5128 gen_rtx_SET (VOIDmode, dest, src1)));
5129 }
5130
5131 /* Otherwise, do each half of the move. */
5132 else
5133 {
5134 /* Emit the conditional move for the test being true if needed. */
5135 if (! rtx_equal_p (dest, src1))
5136 emit_insn (gen_rtx_COND_EXEC (VOIDmode,
5137 gen_rtx_NE (cr_mode, cr_reg, const0_rtx),
5138 gen_rtx_SET (VOIDmode, dest, src1)));
5139
5140 /* Emit the conditional move for the test being false if needed. */
5141 if (! rtx_equal_p (dest, src2))
5142 emit_insn (gen_rtx_COND_EXEC (VOIDmode,
5143 gen_rtx_EQ (cr_mode, cr_reg, const0_rtx),
5144 gen_rtx_SET (VOIDmode, dest, src2)));
5145 }
5146
5147 /* Finish up, return sequence. */
5148 ret = get_insns ();
5149 end_sequence ();
5150 return ret;
5151}
5152
5153\f
5154/* Split an integer abs operation returning a SEQUENCE containing all of the
5155 insns. */
5156
5157rtx
f2206911 5158frv_split_abs (rtx operands[])
36a05131
BS
5159{
5160 rtx dest = operands[0];
5161 rtx src = operands[1];
5162 rtx cc_reg = operands[2];
5163 rtx cr_reg = operands[3];
5164 rtx ret;
5165
5166 start_sequence ();
5167
5168 /* Issue the compare < 0 instruction. */
5169 emit_insn (gen_rtx_SET (VOIDmode,
5170 cc_reg,
5171 gen_rtx_COMPARE (CCmode, src, const0_rtx)));
5172
5173 /* Set the appropriate CCR bit. */
5174 emit_insn (gen_rtx_SET (VOIDmode,
5175 cr_reg,
5176 gen_rtx_fmt_ee (LT, CC_CCRmode, cc_reg, const0_rtx)));
5177
87b483a1 5178 /* Emit the conditional negate if the value is negative. */
36a05131
BS
5179 emit_insn (gen_rtx_COND_EXEC (VOIDmode,
5180 gen_rtx_NE (CC_CCRmode, cr_reg, const0_rtx),
5181 gen_negsi2 (dest, src)));
5182
5183 /* Emit the conditional move for the test being false if needed. */
5184 if (! rtx_equal_p (dest, src))
5185 emit_insn (gen_rtx_COND_EXEC (VOIDmode,
5186 gen_rtx_EQ (CC_CCRmode, cr_reg, const0_rtx),
5187 gen_rtx_SET (VOIDmode, dest, src)));
5188
5189 /* Finish up, return sequence. */
5190 ret = get_insns ();
5191 end_sequence ();
5192 return ret;
5193}
5194
36a05131 5195\f
67a0732f
SB
5196/* Initialize machine-specific if-conversion data.
5197 On the FR-V, we don't have any extra fields per se, but it is useful hook to
36a05131
BS
5198 initialize the static storage. */
5199void
afa63532 5200frv_ifcvt_machdep_init (void *ce_info ATTRIBUTE_UNUSED)
36a05131
BS
5201{
5202 frv_ifcvt.added_insns_list = NULL_RTX;
5203 frv_ifcvt.cur_scratch_regs = 0;
5204 frv_ifcvt.num_nested_cond_exec = 0;
5205 frv_ifcvt.cr_reg = NULL_RTX;
5206 frv_ifcvt.nested_cc_reg = NULL_RTX;
5207 frv_ifcvt.extra_int_cr = NULL_RTX;
5208 frv_ifcvt.extra_fp_cr = NULL_RTX;
5209 frv_ifcvt.last_nested_if_cr = NULL_RTX;
5210}
5211
5212\f
1ae58c30 5213/* Internal function to add a potential insn to the list of insns to be inserted
36a05131
BS
5214 if the conditional execution conversion is successful. */
5215
5216static void
f2206911 5217frv_ifcvt_add_insn (rtx pattern, rtx insn, int before_p)
36a05131
BS
5218{
5219 rtx link = alloc_EXPR_LIST (VOIDmode, pattern, insn);
5220
87b483a1 5221 link->jump = before_p; /* Mark to add this before or after insn. */
36a05131
BS
5222 frv_ifcvt.added_insns_list = alloc_EXPR_LIST (VOIDmode, link,
5223 frv_ifcvt.added_insns_list);
5224
5225 if (TARGET_DEBUG_COND_EXEC)
5226 {
5227 fprintf (stderr,
5228 "\n:::::::::: frv_ifcvt_add_insn: add the following %s insn %d:\n",
5229 (before_p) ? "before" : "after",
5230 (int)INSN_UID (insn));
5231
5232 debug_rtx (pattern);
5233 }
5234}
5235
5236\f
5237/* A C expression to modify the code described by the conditional if
5238 information CE_INFO, possibly updating the tests in TRUE_EXPR, and
5239 FALSE_EXPR for converting if-then and if-then-else code to conditional
5240 instructions. Set either TRUE_EXPR or FALSE_EXPR to a null pointer if the
5241 tests cannot be converted. */
5242
5243void
84562394 5244frv_ifcvt_modify_tests (ce_if_block *ce_info, rtx *p_true, rtx *p_false)
36a05131
BS
5245{
5246 basic_block test_bb = ce_info->test_bb; /* test basic block */
5247 basic_block then_bb = ce_info->then_bb; /* THEN */
5248 basic_block else_bb = ce_info->else_bb; /* ELSE or NULL */
5249 basic_block join_bb = ce_info->join_bb; /* join block or NULL */
5250 rtx true_expr = *p_true;
5251 rtx cr;
5252 rtx cc;
5253 rtx nested_cc;
ef4bddc2 5254 machine_mode mode = GET_MODE (true_expr);
36a05131
BS
5255 int j;
5256 basic_block *bb;
5257 int num_bb;
5258 frv_tmp_reg_t *tmp_reg = &frv_ifcvt.tmp_reg;
5259 rtx check_insn;
5260 rtx sub_cond_exec_reg;
5261 enum rtx_code code;
5262 enum rtx_code code_true;
5263 enum rtx_code code_false;
5264 enum reg_class cc_class;
5265 enum reg_class cr_class;
5266 int cc_first;
5267 int cc_last;
a2041967 5268 reg_set_iterator rsi;
36a05131
BS
5269
5270 /* Make sure we are only dealing with hard registers. Also honor the
5271 -mno-cond-exec switch, and -mno-nested-cond-exec switches if
5272 applicable. */
0b2c18fe
RS
5273 if (!reload_completed || !TARGET_COND_EXEC
5274 || (!TARGET_NESTED_CE && ce_info->pass > 1))
36a05131
BS
5275 goto fail;
5276
5277 /* Figure out which registers we can allocate for our own purposes. Only
5278 consider registers that are not preserved across function calls and are
5279 not fixed. However, allow the ICC/ICR temporary registers to be allocated
87b483a1 5280 if we did not need to use them in reloading other registers. */
fad205ff 5281 memset (&tmp_reg->regs, 0, sizeof (tmp_reg->regs));
36a05131
BS
5282 COPY_HARD_REG_SET (tmp_reg->regs, call_used_reg_set);
5283 AND_COMPL_HARD_REG_SET (tmp_reg->regs, fixed_reg_set);
5284 SET_HARD_REG_BIT (tmp_reg->regs, ICC_TEMP);
5285 SET_HARD_REG_BIT (tmp_reg->regs, ICR_TEMP);
5286
5287 /* If this is a nested IF, we need to discover whether the CC registers that
5288 are set/used inside of the block are used anywhere else. If not, we can
5289 change them to be the CC register that is paired with the CR register that
5290 controls the outermost IF block. */
5291 if (ce_info->pass > 1)
5292 {
5293 CLEAR_HARD_REG_SET (frv_ifcvt.nested_cc_ok_rewrite);
5294 for (j = CC_FIRST; j <= CC_LAST; j++)
5295 if (TEST_HARD_REG_BIT (tmp_reg->regs, j))
5296 {
eedd7243 5297 if (REGNO_REG_SET_P (df_get_live_in (then_bb), j))
36a05131
BS
5298 continue;
5299
5e2d947c 5300 if (else_bb
eedd7243 5301 && REGNO_REG_SET_P (df_get_live_in (else_bb), j))
36a05131
BS
5302 continue;
5303
5e2d947c 5304 if (join_bb
eedd7243 5305 && REGNO_REG_SET_P (df_get_live_in (join_bb), j))
36a05131
BS
5306 continue;
5307
5308 SET_HARD_REG_BIT (frv_ifcvt.nested_cc_ok_rewrite, j);
5309 }
5310 }
5311
5312 for (j = 0; j < frv_ifcvt.cur_scratch_regs; j++)
5313 frv_ifcvt.scratch_regs[j] = NULL_RTX;
5314
5315 frv_ifcvt.added_insns_list = NULL_RTX;
5316 frv_ifcvt.cur_scratch_regs = 0;
5317
5318 bb = (basic_block *) alloca ((2 + ce_info->num_multiple_test_blocks)
5319 * sizeof (basic_block));
5320
5321 if (join_bb)
5322 {
38c28a25 5323 unsigned int regno;
36a05131
BS
5324
5325 /* Remove anything live at the beginning of the join block from being
5326 available for allocation. */
eedd7243 5327 EXECUTE_IF_SET_IN_REG_SET (df_get_live_in (join_bb), 0, regno, rsi)
a2041967
KH
5328 {
5329 if (regno < FIRST_PSEUDO_REGISTER)
5330 CLEAR_HARD_REG_BIT (tmp_reg->regs, regno);
5331 }
36a05131
BS
5332 }
5333
5334 /* Add in all of the blocks in multiple &&/|| blocks to be scanned. */
5335 num_bb = 0;
5336 if (ce_info->num_multiple_test_blocks)
5337 {
5338 basic_block multiple_test_bb = ce_info->last_test_bb;
5339
5340 while (multiple_test_bb != test_bb)
5341 {
5342 bb[num_bb++] = multiple_test_bb;
628f6a4e 5343 multiple_test_bb = EDGE_PRED (multiple_test_bb, 0)->src;
36a05131
BS
5344 }
5345 }
5346
5347 /* Add in the THEN and ELSE blocks to be scanned. */
5348 bb[num_bb++] = then_bb;
5349 if (else_bb)
5350 bb[num_bb++] = else_bb;
5351
5352 sub_cond_exec_reg = NULL_RTX;
5353 frv_ifcvt.num_nested_cond_exec = 0;
5354
5355 /* Scan all of the blocks for registers that must not be allocated. */
5356 for (j = 0; j < num_bb; j++)
5357 {
b32d5189
DM
5358 rtx_insn *last_insn = BB_END (bb[j]);
5359 rtx_insn *insn = BB_HEAD (bb[j]);
38c28a25 5360 unsigned int regno;
36a05131 5361
c263766c
RH
5362 if (dump_file)
5363 fprintf (dump_file, "Scanning %s block %d, start %d, end %d\n",
36a05131
BS
5364 (bb[j] == else_bb) ? "else" : ((bb[j] == then_bb) ? "then" : "test"),
5365 (int) bb[j]->index,
a813c111
SB
5366 (int) INSN_UID (BB_HEAD (bb[j])),
5367 (int) INSN_UID (BB_END (bb[j])));
36a05131
BS
5368
5369 /* Anything live at the beginning of the block is obviously unavailable
5370 for allocation. */
eedd7243 5371 EXECUTE_IF_SET_IN_REG_SET (df_get_live_in (bb[j]), 0, regno, rsi)
a2041967
KH
5372 {
5373 if (regno < FIRST_PSEUDO_REGISTER)
5374 CLEAR_HARD_REG_BIT (tmp_reg->regs, regno);
5375 }
36a05131 5376
87b483a1 5377 /* Loop through the insns in the block. */
36a05131
BS
5378 for (;;)
5379 {
5380 /* Mark any new registers that are created as being unavailable for
5381 allocation. Also see if the CC register used in nested IFs can be
5382 reallocated. */
5383 if (INSN_P (insn))
5384 {
5385 rtx pattern;
5386 rtx set;
5387 int skip_nested_if = FALSE;
7e8e4cf9 5388 HARD_REG_SET mentioned_regs;
36a05131 5389
7e8e4cf9
RS
5390 CLEAR_HARD_REG_SET (mentioned_regs);
5391 find_all_hard_regs (PATTERN (insn), &mentioned_regs);
5392 AND_COMPL_HARD_REG_SET (tmp_reg->regs, mentioned_regs);
36a05131
BS
5393
5394 pattern = PATTERN (insn);
5395 if (GET_CODE (pattern) == COND_EXEC)
5396 {
5397 rtx reg = XEXP (COND_EXEC_TEST (pattern), 0);
5398
5399 if (reg != sub_cond_exec_reg)
5400 {
5401 sub_cond_exec_reg = reg;
5402 frv_ifcvt.num_nested_cond_exec++;
5403 }
5404 }
5405
5406 set = single_set_pattern (pattern);
5407 if (set)
5408 {
5409 rtx dest = SET_DEST (set);
5410 rtx src = SET_SRC (set);
5411
5412 if (GET_CODE (dest) == REG)
5413 {
5414 int regno = REGNO (dest);
5415 enum rtx_code src_code = GET_CODE (src);
5416
5417 if (CC_P (regno) && src_code == COMPARE)
5418 skip_nested_if = TRUE;
5419
5420 else if (CR_P (regno)
5421 && (src_code == IF_THEN_ELSE
ec8e098d 5422 || COMPARISON_P (src)))
36a05131
BS
5423 skip_nested_if = TRUE;
5424 }
5425 }
5426
5427 if (! skip_nested_if)
7e8e4cf9
RS
5428 AND_COMPL_HARD_REG_SET (frv_ifcvt.nested_cc_ok_rewrite,
5429 mentioned_regs);
36a05131
BS
5430 }
5431
5432 if (insn == last_insn)
5433 break;
5434
5435 insn = NEXT_INSN (insn);
5436 }
5437 }
5438
5439 /* If this is a nested if, rewrite the CC registers that are available to
5440 include the ones that can be rewritten, to increase the chance of being
5441 able to allocate a paired CC/CR register combination. */
5442 if (ce_info->pass > 1)
5443 {
5444 for (j = CC_FIRST; j <= CC_LAST; j++)
5445 if (TEST_HARD_REG_BIT (frv_ifcvt.nested_cc_ok_rewrite, j))
5446 SET_HARD_REG_BIT (tmp_reg->regs, j);
5447 else
5448 CLEAR_HARD_REG_BIT (tmp_reg->regs, j);
5449 }
5450
c263766c 5451 if (dump_file)
36a05131
BS
5452 {
5453 int num_gprs = 0;
c263766c 5454 fprintf (dump_file, "Available GPRs: ");
36a05131
BS
5455
5456 for (j = GPR_FIRST; j <= GPR_LAST; j++)
5457 if (TEST_HARD_REG_BIT (tmp_reg->regs, j))
5458 {
c263766c 5459 fprintf (dump_file, " %d [%s]", j, reg_names[j]);
36a05131
BS
5460 if (++num_gprs > GPR_TEMP_NUM+2)
5461 break;
5462 }
5463
c263766c 5464 fprintf (dump_file, "%s\nAvailable CRs: ",
36a05131
BS
5465 (num_gprs > GPR_TEMP_NUM+2) ? " ..." : "");
5466
5467 for (j = CR_FIRST; j <= CR_LAST; j++)
5468 if (TEST_HARD_REG_BIT (tmp_reg->regs, j))
c263766c 5469 fprintf (dump_file, " %d [%s]", j, reg_names[j]);
36a05131 5470
c263766c 5471 fputs ("\n", dump_file);
36a05131
BS
5472
5473 if (ce_info->pass > 1)
5474 {
c263766c 5475 fprintf (dump_file, "Modifiable CCs: ");
36a05131
BS
5476 for (j = CC_FIRST; j <= CC_LAST; j++)
5477 if (TEST_HARD_REG_BIT (tmp_reg->regs, j))
c263766c 5478 fprintf (dump_file, " %d [%s]", j, reg_names[j]);
36a05131 5479
c263766c 5480 fprintf (dump_file, "\n%d nested COND_EXEC statements\n",
36a05131
BS
5481 frv_ifcvt.num_nested_cond_exec);
5482 }
5483 }
5484
5485 /* Allocate the appropriate temporary condition code register. Try to
5486 allocate the ICR/FCR register that corresponds to the ICC/FCC register so
5487 that conditional cmp's can be done. */
036ff63f 5488 if (mode == CCmode || mode == CC_UNSmode || mode == CC_NZmode)
36a05131
BS
5489 {
5490 cr_class = ICR_REGS;
5491 cc_class = ICC_REGS;
5492 cc_first = ICC_FIRST;
5493 cc_last = ICC_LAST;
5494 }
5495 else if (mode == CC_FPmode)
5496 {
5497 cr_class = FCR_REGS;
5498 cc_class = FCC_REGS;
5499 cc_first = FCC_FIRST;
5500 cc_last = FCC_LAST;
5501 }
5502 else
5503 {
5504 cc_first = cc_last = 0;
5505 cr_class = cc_class = NO_REGS;
5506 }
5507
5508 cc = XEXP (true_expr, 0);
5509 nested_cc = cr = NULL_RTX;
5510 if (cc_class != NO_REGS)
5511 {
5512 /* For nested IFs and &&/||, see if we can find a CC and CR register pair
5513 so we can execute a csubcc/caddcc/cfcmps instruction. */
5514 int cc_regno;
5515
5516 for (cc_regno = cc_first; cc_regno <= cc_last; cc_regno++)
5517 {
5518 int cr_regno = cc_regno - CC_FIRST + CR_FIRST;
5519
5520 if (TEST_HARD_REG_BIT (frv_ifcvt.tmp_reg.regs, cc_regno)
5521 && TEST_HARD_REG_BIT (frv_ifcvt.tmp_reg.regs, cr_regno))
5522 {
5523 frv_ifcvt.tmp_reg.next_reg[ (int)cr_class ] = cr_regno;
5524 cr = frv_alloc_temp_reg (tmp_reg, cr_class, CC_CCRmode, TRUE,
5525 TRUE);
5526
5527 frv_ifcvt.tmp_reg.next_reg[ (int)cc_class ] = cc_regno;
5528 nested_cc = frv_alloc_temp_reg (tmp_reg, cc_class, CCmode,
5529 TRUE, TRUE);
5530 break;
5531 }
5532 }
5533 }
5534
5535 if (! cr)
5536 {
c263766c
RH
5537 if (dump_file)
5538 fprintf (dump_file, "Could not allocate a CR temporary register\n");
36a05131
BS
5539
5540 goto fail;
5541 }
5542
c263766c
RH
5543 if (dump_file)
5544 fprintf (dump_file,
36a05131
BS
5545 "Will use %s for conditional execution, %s for nested comparisons\n",
5546 reg_names[ REGNO (cr)],
5547 (nested_cc) ? reg_names[ REGNO (nested_cc) ] : "<none>");
5548
5549 /* Set the CCR bit. Note for integer tests, we reverse the condition so that
5550 in an IF-THEN-ELSE sequence, we are testing the TRUE case against the CCR
5551 bit being true. We don't do this for floating point, because of NaNs. */
5552 code = GET_CODE (true_expr);
5553 if (GET_MODE (cc) != CC_FPmode)
5554 {
5555 code = reverse_condition (code);
5556 code_true = EQ;
5557 code_false = NE;
5558 }
5559 else
5560 {
5561 code_true = NE;
5562 code_false = EQ;
5563 }
5564
5565 check_insn = gen_rtx_SET (VOIDmode, cr,
5566 gen_rtx_fmt_ee (code, CC_CCRmode, cc, const0_rtx));
5567
5568 /* Record the check insn to be inserted later. */
a813c111 5569 frv_ifcvt_add_insn (check_insn, BB_END (test_bb), TRUE);
36a05131
BS
5570
5571 /* Update the tests. */
5572 frv_ifcvt.cr_reg = cr;
5573 frv_ifcvt.nested_cc_reg = nested_cc;
5574 *p_true = gen_rtx_fmt_ee (code_true, CC_CCRmode, cr, const0_rtx);
5575 *p_false = gen_rtx_fmt_ee (code_false, CC_CCRmode, cr, const0_rtx);
5576 return;
5577
5578 /* Fail, don't do this conditional execution. */
5579 fail:
5580 *p_true = NULL_RTX;
5581 *p_false = NULL_RTX;
c263766c
RH
5582 if (dump_file)
5583 fprintf (dump_file, "Disabling this conditional execution.\n");
36a05131
BS
5584
5585 return;
5586}
5587
5588\f
5589/* A C expression to modify the code described by the conditional if
5590 information CE_INFO, for the basic block BB, possibly updating the tests in
5591 TRUE_EXPR, and FALSE_EXPR for converting the && and || parts of if-then or
5592 if-then-else code to conditional instructions. Set either TRUE_EXPR or
5593 FALSE_EXPR to a null pointer if the tests cannot be converted. */
5594
5595/* p_true and p_false are given expressions of the form:
5596
5597 (and (eq:CC_CCR (reg:CC_CCR)
5598 (const_int 0))
5599 (eq:CC (reg:CC)
5600 (const_int 0))) */
5601
5602void
84562394 5603frv_ifcvt_modify_multiple_tests (ce_if_block *ce_info,
f2206911
KC
5604 basic_block bb,
5605 rtx *p_true,
5606 rtx *p_false)
36a05131
BS
5607{
5608 rtx old_true = XEXP (*p_true, 0);
5609 rtx old_false = XEXP (*p_false, 0);
5610 rtx true_expr = XEXP (*p_true, 1);
5611 rtx false_expr = XEXP (*p_false, 1);
5612 rtx test_expr;
5613 rtx old_test;
5614 rtx cr = XEXP (old_true, 0);
5615 rtx check_insn;
5616 rtx new_cr = NULL_RTX;
5617 rtx *p_new_cr = (rtx *)0;
5618 rtx if_else;
5619 rtx compare;
5620 rtx cc;
5621 enum reg_class cr_class;
ef4bddc2 5622 machine_mode mode = GET_MODE (true_expr);
36a05131
BS
5623 rtx (*logical_func)(rtx, rtx, rtx);
5624
5625 if (TARGET_DEBUG_COND_EXEC)
5626 {
5627 fprintf (stderr,
5628 "\n:::::::::: frv_ifcvt_modify_multiple_tests, before modification for %s\ntrue insn:\n",
5629 ce_info->and_and_p ? "&&" : "||");
5630
5631 debug_rtx (*p_true);
5632
5633 fputs ("\nfalse insn:\n", stderr);
5634 debug_rtx (*p_false);
5635 }
5636
0b2c18fe 5637 if (!TARGET_MULTI_CE)
36a05131
BS
5638 goto fail;
5639
5640 if (GET_CODE (cr) != REG)
5641 goto fail;
b16c1435 5642
036ff63f 5643 if (mode == CCmode || mode == CC_UNSmode || mode == CC_NZmode)
36a05131
BS
5644 {
5645 cr_class = ICR_REGS;
5646 p_new_cr = &frv_ifcvt.extra_int_cr;
5647 }
5648 else if (mode == CC_FPmode)
5649 {
5650 cr_class = FCR_REGS;
5651 p_new_cr = &frv_ifcvt.extra_fp_cr;
5652 }
5653 else
5654 goto fail;
5655
5656 /* Allocate a temp CR, reusing a previously allocated temp CR if we have 3 or
5657 more &&/|| tests. */
5658 new_cr = *p_new_cr;
5659 if (! new_cr)
5660 {
5661 new_cr = *p_new_cr = frv_alloc_temp_reg (&frv_ifcvt.tmp_reg, cr_class,
5662 CC_CCRmode, TRUE, TRUE);
5663 if (! new_cr)
5664 goto fail;
5665 }
5666
5667 if (ce_info->and_and_p)
5668 {
5669 old_test = old_false;
5670 test_expr = true_expr;
5671 logical_func = (GET_CODE (old_true) == EQ) ? gen_andcr : gen_andncr;
5672 *p_true = gen_rtx_NE (CC_CCRmode, cr, const0_rtx);
5673 *p_false = gen_rtx_EQ (CC_CCRmode, cr, const0_rtx);
5674 }
5675 else
5676 {
5677 old_test = old_false;
5678 test_expr = false_expr;
5679 logical_func = (GET_CODE (old_false) == EQ) ? gen_orcr : gen_orncr;
5680 *p_true = gen_rtx_EQ (CC_CCRmode, cr, const0_rtx);
5681 *p_false = gen_rtx_NE (CC_CCRmode, cr, const0_rtx);
5682 }
5683
5684 /* First add the andcr/andncr/orcr/orncr, which will be added after the
5685 conditional check instruction, due to frv_ifcvt_add_insn being a LIFO
5686 stack. */
a813c111 5687 frv_ifcvt_add_insn ((*logical_func) (cr, cr, new_cr), BB_END (bb), TRUE);
36a05131
BS
5688
5689 /* Now add the conditional check insn. */
5690 cc = XEXP (test_expr, 0);
5691 compare = gen_rtx_fmt_ee (GET_CODE (test_expr), CC_CCRmode, cc, const0_rtx);
5692 if_else = gen_rtx_IF_THEN_ELSE (CC_CCRmode, old_test, compare, const0_rtx);
5693
5694 check_insn = gen_rtx_SET (VOIDmode, new_cr, if_else);
5695
87b483a1 5696 /* Add the new check insn to the list of check insns that need to be
36a05131 5697 inserted. */
a813c111 5698 frv_ifcvt_add_insn (check_insn, BB_END (bb), TRUE);
36a05131
BS
5699
5700 if (TARGET_DEBUG_COND_EXEC)
5701 {
5702 fputs ("\n:::::::::: frv_ifcvt_modify_multiple_tests, after modification\ntrue insn:\n",
5703 stderr);
5704
5705 debug_rtx (*p_true);
5706
5707 fputs ("\nfalse insn:\n", stderr);
5708 debug_rtx (*p_false);
5709 }
5710
5711 return;
5712
5713 fail:
5714 *p_true = *p_false = NULL_RTX;
5715
87b483a1 5716 /* If we allocated a CR register, release it. */
36a05131
BS
5717 if (new_cr)
5718 {
5719 CLEAR_HARD_REG_BIT (frv_ifcvt.tmp_reg.regs, REGNO (new_cr));
5720 *p_new_cr = NULL_RTX;
5721 }
5722
5723 if (TARGET_DEBUG_COND_EXEC)
5724 fputs ("\n:::::::::: frv_ifcvt_modify_multiple_tests, failed.\n", stderr);
5725
5726 return;
5727}
5728
5729\f
5730/* Return a register which will be loaded with a value if an IF block is
5731 converted to conditional execution. This is used to rewrite instructions
5732 that use constants to ones that just use registers. */
5733
5734static rtx
f2206911 5735frv_ifcvt_load_value (rtx value, rtx insn ATTRIBUTE_UNUSED)
36a05131
BS
5736{
5737 int num_alloc = frv_ifcvt.cur_scratch_regs;
5738 int i;
5739 rtx reg;
5740
5741 /* We know gr0 == 0, so replace any errant uses. */
5742 if (value == const0_rtx)
5743 return gen_rtx_REG (SImode, GPR_FIRST);
5744
5745 /* First search all registers currently loaded to see if we have an
5746 applicable constant. */
5747 if (CONSTANT_P (value)
5748 || (GET_CODE (value) == REG && REGNO (value) == LR_REGNO))
5749 {
5750 for (i = 0; i < num_alloc; i++)
5751 {
5752 if (rtx_equal_p (SET_SRC (frv_ifcvt.scratch_regs[i]), value))
5753 return SET_DEST (frv_ifcvt.scratch_regs[i]);
5754 }
5755 }
5756
87b483a1 5757 /* Have we exhausted the number of registers available? */
36a05131
BS
5758 if (num_alloc >= GPR_TEMP_NUM)
5759 {
c263766c
RH
5760 if (dump_file)
5761 fprintf (dump_file, "Too many temporary registers allocated\n");
36a05131
BS
5762
5763 return NULL_RTX;
5764 }
5765
5766 /* Allocate the new register. */
5767 reg = frv_alloc_temp_reg (&frv_ifcvt.tmp_reg, GPR_REGS, SImode, TRUE, TRUE);
5768 if (! reg)
5769 {
c263766c
RH
5770 if (dump_file)
5771 fputs ("Could not find a scratch register\n", dump_file);
36a05131
BS
5772
5773 return NULL_RTX;
5774 }
5775
5776 frv_ifcvt.cur_scratch_regs++;
5777 frv_ifcvt.scratch_regs[num_alloc] = gen_rtx_SET (VOIDmode, reg, value);
5778
c263766c 5779 if (dump_file)
36a05131
BS
5780 {
5781 if (GET_CODE (value) == CONST_INT)
c263766c 5782 fprintf (dump_file, "Register %s will hold %ld\n",
36a05131
BS
5783 reg_names[ REGNO (reg)], (long)INTVAL (value));
5784
5785 else if (GET_CODE (value) == REG && REGNO (value) == LR_REGNO)
c263766c 5786 fprintf (dump_file, "Register %s will hold LR\n",
36a05131
BS
5787 reg_names[ REGNO (reg)]);
5788
5789 else
c263766c 5790 fprintf (dump_file, "Register %s will hold a saved value\n",
36a05131
BS
5791 reg_names[ REGNO (reg)]);
5792 }
5793
5794 return reg;
5795}
5796
5797\f
5798/* Update a MEM used in conditional code that might contain an offset to put
5799 the offset into a scratch register, so that the conditional load/store
5800 operations can be used. This function returns the original pointer if the
5801 MEM is valid to use in conditional code, NULL if we can't load up the offset
5802 into a temporary register, or the new MEM if we were successful. */
5803
5804static rtx
ef4bddc2 5805frv_ifcvt_rewrite_mem (rtx mem, machine_mode mode, rtx insn)
36a05131
BS
5806{
5807 rtx addr = XEXP (mem, 0);
5808
c6c3dba9 5809 if (!frv_legitimate_address_p_1 (mode, addr, reload_completed, TRUE, FALSE))
36a05131
BS
5810 {
5811 if (GET_CODE (addr) == PLUS)
5812 {
5813 rtx addr_op0 = XEXP (addr, 0);
5814 rtx addr_op1 = XEXP (addr, 1);
5815
34208acf 5816 if (GET_CODE (addr_op0) == REG && CONSTANT_P (addr_op1))
36a05131
BS
5817 {
5818 rtx reg = frv_ifcvt_load_value (addr_op1, insn);
5819 if (!reg)
5820 return NULL_RTX;
5821
5822 addr = gen_rtx_PLUS (Pmode, addr_op0, reg);
5823 }
5824
5825 else
5826 return NULL_RTX;
5827 }
5828
5829 else if (CONSTANT_P (addr))
5830 addr = frv_ifcvt_load_value (addr, insn);
5831
5832 else
5833 return NULL_RTX;
5834
5835 if (addr == NULL_RTX)
5836 return NULL_RTX;
5837
5838 else if (XEXP (mem, 0) != addr)
5839 return change_address (mem, mode, addr);
5840 }
5841
5842 return mem;
5843}
5844
5845\f
5846/* Given a PATTERN, return a SET expression if this PATTERN has only a single
5847 SET, possibly conditionally executed. It may also have CLOBBERs, USEs. */
5848
5849static rtx
f2206911 5850single_set_pattern (rtx pattern)
36a05131
BS
5851{
5852 rtx set;
5853 int i;
5854
5855 if (GET_CODE (pattern) == COND_EXEC)
5856 pattern = COND_EXEC_CODE (pattern);
5857
5858 if (GET_CODE (pattern) == SET)
5859 return pattern;
5860
5861 else if (GET_CODE (pattern) == PARALLEL)
5862 {
5863 for (i = 0, set = 0; i < XVECLEN (pattern, 0); i++)
5864 {
5865 rtx sub = XVECEXP (pattern, 0, i);
5866
5867 switch (GET_CODE (sub))
5868 {
5869 case USE:
5870 case CLOBBER:
5871 break;
5872
5873 case SET:
5874 if (set)
5875 return 0;
5876 else
5877 set = sub;
5878 break;
5879
5880 default:
5881 return 0;
5882 }
5883 }
5884 return set;
5885 }
5886
5887 return 0;
5888}
5889
5890\f
5891/* A C expression to modify the code described by the conditional if
5892 information CE_INFO with the new PATTERN in INSN. If PATTERN is a null
5893 pointer after the IFCVT_MODIFY_INSN macro executes, it is assumed that that
5894 insn cannot be converted to be executed conditionally. */
5895
5896rtx
84562394 5897frv_ifcvt_modify_insn (ce_if_block *ce_info,
f2206911
KC
5898 rtx pattern,
5899 rtx insn)
36a05131
BS
5900{
5901 rtx orig_ce_pattern = pattern;
5902 rtx set;
5903 rtx op0;
5904 rtx op1;
5905 rtx test;
5906
44e91694 5907 gcc_assert (GET_CODE (pattern) == COND_EXEC);
36a05131
BS
5908
5909 test = COND_EXEC_TEST (pattern);
5910 if (GET_CODE (test) == AND)
5911 {
5912 rtx cr = frv_ifcvt.cr_reg;
5913 rtx test_reg;
5914
5915 op0 = XEXP (test, 0);
5916 if (! rtx_equal_p (cr, XEXP (op0, 0)))
5917 goto fail;
5918
5919 op1 = XEXP (test, 1);
5920 test_reg = XEXP (op1, 0);
5921 if (GET_CODE (test_reg) != REG)
5922 goto fail;
5923
5924 /* Is this the first nested if block in this sequence? If so, generate
5925 an andcr or andncr. */
5926 if (! frv_ifcvt.last_nested_if_cr)
5927 {
5928 rtx and_op;
5929
5930 frv_ifcvt.last_nested_if_cr = test_reg;
5931 if (GET_CODE (op0) == NE)
5932 and_op = gen_andcr (test_reg, cr, test_reg);
5933 else
5934 and_op = gen_andncr (test_reg, cr, test_reg);
5935
5936 frv_ifcvt_add_insn (and_op, insn, TRUE);
5937 }
5938
5939 /* If this isn't the first statement in the nested if sequence, see if we
5940 are dealing with the same register. */
5941 else if (! rtx_equal_p (test_reg, frv_ifcvt.last_nested_if_cr))
5942 goto fail;
5943
5944 COND_EXEC_TEST (pattern) = test = op1;
5945 }
5946
5947 /* If this isn't a nested if, reset state variables. */
5948 else
5949 {
5950 frv_ifcvt.last_nested_if_cr = NULL_RTX;
5951 }
5952
5953 set = single_set_pattern (pattern);
5954 if (set)
5955 {
5956 rtx dest = SET_DEST (set);
5957 rtx src = SET_SRC (set);
ef4bddc2 5958 machine_mode mode = GET_MODE (dest);
36a05131 5959
87b483a1 5960 /* Check for normal binary operators. */
ec8e098d 5961 if (mode == SImode && ARITHMETIC_P (src))
36a05131
BS
5962 {
5963 op0 = XEXP (src, 0);
5964 op1 = XEXP (src, 1);
5965
34208acf 5966 if (integer_register_operand (op0, SImode) && CONSTANT_P (op1))
36a05131
BS
5967 {
5968 op1 = frv_ifcvt_load_value (op1, insn);
5969 if (op1)
5970 COND_EXEC_CODE (pattern)
5971 = gen_rtx_SET (VOIDmode, dest, gen_rtx_fmt_ee (GET_CODE (src),
5972 GET_MODE (src),
5973 op0, op1));
5974 else
5975 goto fail;
5976 }
5977 }
5978
5979 /* For multiply by a constant, we need to handle the sign extending
5980 correctly. Add a USE of the value after the multiply to prevent flow
5981 from cratering because only one register out of the two were used. */
5982 else if (mode == DImode && GET_CODE (src) == MULT)
5983 {
5984 op0 = XEXP (src, 0);
5985 op1 = XEXP (src, 1);
5986 if (GET_CODE (op0) == SIGN_EXTEND && GET_CODE (op1) == CONST_INT)
5987 {
5988 op1 = frv_ifcvt_load_value (op1, insn);
5989 if (op1)
5990 {
5991 op1 = gen_rtx_SIGN_EXTEND (DImode, op1);
5992 COND_EXEC_CODE (pattern)
5993 = gen_rtx_SET (VOIDmode, dest,
5994 gen_rtx_MULT (DImode, op0, op1));
5995 }
5996 else
5997 goto fail;
5998 }
5999
c41c1387 6000 frv_ifcvt_add_insn (gen_use (dest), insn, FALSE);
36a05131
BS
6001 }
6002
6003 /* If we are just loading a constant created for a nested conditional
6004 execution statement, just load the constant without any conditional
6005 execution, since we know that the constant will not interfere with any
6006 other registers. */
6007 else if (frv_ifcvt.scratch_insns_bitmap
6008 && bitmap_bit_p (frv_ifcvt.scratch_insns_bitmap,
5da1fd3d 6009 INSN_UID (insn))
5da1fd3d 6010 && REG_P (SET_DEST (set))
9a228f09
AO
6011 /* We must not unconditionally set a scratch reg chosen
6012 for a nested if-converted block if its incoming
6013 value from the TEST block (or the result of the THEN
6014 branch) could/should propagate to the JOIN block.
6015 It suffices to test whether the register is live at
6016 the JOIN point: if it's live there, we can infer
6017 that we set it in the former JOIN block of the
6018 nested if-converted block (otherwise it wouldn't
6019 have been available as a scratch register), and it
6020 is either propagated through or set in the other
6021 conditional block. It's probably not worth trying
6022 to catch the latter case, and it could actually
6023 limit scheduling of the combined block quite
6024 severely. */
6025 && ce_info->join_bb
eedd7243
RIL
6026 && ! (REGNO_REG_SET_P (df_get_live_in (ce_info->join_bb),
6027 REGNO (SET_DEST (set))))
9a228f09
AO
6028 /* Similarly, we must not unconditionally set a reg
6029 used as scratch in the THEN branch if the same reg
6030 is live in the ELSE branch. */
5da1fd3d
AO
6031 && (! ce_info->else_bb
6032 || BLOCK_FOR_INSN (insn) == ce_info->else_bb
eedd7243
RIL
6033 || ! (REGNO_REG_SET_P (df_get_live_in (ce_info->else_bb),
6034 REGNO (SET_DEST (set))))))
36a05131
BS
6035 pattern = set;
6036
6037 else if (mode == QImode || mode == HImode || mode == SImode
6038 || mode == SFmode)
6039 {
6040 int changed_p = FALSE;
6041
6042 /* Check for just loading up a constant */
6043 if (CONSTANT_P (src) && integer_register_operand (dest, mode))
6044 {
6045 src = frv_ifcvt_load_value (src, insn);
6046 if (!src)
6047 goto fail;
6048
6049 changed_p = TRUE;
6050 }
6051
6052 /* See if we need to fix up stores */
6053 if (GET_CODE (dest) == MEM)
6054 {
6055 rtx new_mem = frv_ifcvt_rewrite_mem (dest, mode, insn);
6056
6057 if (!new_mem)
6058 goto fail;
6059
6060 else if (new_mem != dest)
6061 {
6062 changed_p = TRUE;
6063 dest = new_mem;
6064 }
6065 }
6066
6067 /* See if we need to fix up loads */
6068 if (GET_CODE (src) == MEM)
6069 {
6070 rtx new_mem = frv_ifcvt_rewrite_mem (src, mode, insn);
6071
6072 if (!new_mem)
6073 goto fail;
6074
6075 else if (new_mem != src)
6076 {
6077 changed_p = TRUE;
6078 src = new_mem;
6079 }
6080 }
6081
6082 /* If either src or destination changed, redo SET. */
6083 if (changed_p)
6084 COND_EXEC_CODE (pattern) = gen_rtx_SET (VOIDmode, dest, src);
6085 }
6086
6087 /* Rewrite a nested set cccr in terms of IF_THEN_ELSE. Also deal with
6088 rewriting the CC register to be the same as the paired CC/CR register
6089 for nested ifs. */
ec8e098d 6090 else if (mode == CC_CCRmode && COMPARISON_P (src))
36a05131
BS
6091 {
6092 int regno = REGNO (XEXP (src, 0));
6093 rtx if_else;
6094
6095 if (ce_info->pass > 1
6096 && regno != (int)REGNO (frv_ifcvt.nested_cc_reg)
6097 && TEST_HARD_REG_BIT (frv_ifcvt.nested_cc_ok_rewrite, regno))
6098 {
6099 src = gen_rtx_fmt_ee (GET_CODE (src),
6100 CC_CCRmode,
6101 frv_ifcvt.nested_cc_reg,
6102 XEXP (src, 1));
6103 }
6104
6105 if_else = gen_rtx_IF_THEN_ELSE (CC_CCRmode, test, src, const0_rtx);
6106 pattern = gen_rtx_SET (VOIDmode, dest, if_else);
6107 }
6108
6109 /* Remap a nested compare instruction to use the paired CC/CR reg. */
6110 else if (ce_info->pass > 1
6111 && GET_CODE (dest) == REG
6112 && CC_P (REGNO (dest))
6113 && REGNO (dest) != REGNO (frv_ifcvt.nested_cc_reg)
6114 && TEST_HARD_REG_BIT (frv_ifcvt.nested_cc_ok_rewrite,
6115 REGNO (dest))
6116 && GET_CODE (src) == COMPARE)
6117 {
6118 PUT_MODE (frv_ifcvt.nested_cc_reg, GET_MODE (dest));
6119 COND_EXEC_CODE (pattern)
6120 = gen_rtx_SET (VOIDmode, frv_ifcvt.nested_cc_reg, copy_rtx (src));
6121 }
6122 }
6123
6124 if (TARGET_DEBUG_COND_EXEC)
6125 {
6126 rtx orig_pattern = PATTERN (insn);
6127
6128 PATTERN (insn) = pattern;
6129 fprintf (stderr,
6130 "\n:::::::::: frv_ifcvt_modify_insn: pass = %d, insn after modification:\n",
6131 ce_info->pass);
6132
6133 debug_rtx (insn);
6134 PATTERN (insn) = orig_pattern;
6135 }
6136
6137 return pattern;
6138
6139 fail:
6140 if (TARGET_DEBUG_COND_EXEC)
6141 {
6142 rtx orig_pattern = PATTERN (insn);
6143
6144 PATTERN (insn) = orig_ce_pattern;
6145 fprintf (stderr,
6146 "\n:::::::::: frv_ifcvt_modify_insn: pass = %d, insn could not be modified:\n",
6147 ce_info->pass);
6148
6149 debug_rtx (insn);
6150 PATTERN (insn) = orig_pattern;
6151 }
6152
6153 return NULL_RTX;
6154}
6155
6156\f
6157/* A C expression to perform any final machine dependent modifications in
6158 converting code to conditional execution in the code described by the
6159 conditional if information CE_INFO. */
6160
6161void
84562394 6162frv_ifcvt_modify_final (ce_if_block *ce_info ATTRIBUTE_UNUSED)
36a05131
BS
6163{
6164 rtx existing_insn;
6165 rtx check_insn;
6166 rtx p = frv_ifcvt.added_insns_list;
6167 int i;
6168
6169 /* Loop inserting the check insns. The last check insn is the first test,
6170 and is the appropriate place to insert constants. */
44e91694 6171 gcc_assert (p);
36a05131
BS
6172
6173 do
6174 {
6175 rtx check_and_insert_insns = XEXP (p, 0);
6176 rtx old_p = p;
6177
6178 check_insn = XEXP (check_and_insert_insns, 0);
6179 existing_insn = XEXP (check_and_insert_insns, 1);
6180 p = XEXP (p, 1);
6181
6182 /* The jump bit is used to say that the new insn is to be inserted BEFORE
6183 the existing insn, otherwise it is to be inserted AFTER. */
6184 if (check_and_insert_insns->jump)
6185 {
6186 emit_insn_before (check_insn, existing_insn);
6187 check_and_insert_insns->jump = 0;
6188 }
6189 else
6190 emit_insn_after (check_insn, existing_insn);
6191
6192 free_EXPR_LIST_node (check_and_insert_insns);
6193 free_EXPR_LIST_node (old_p);
6194 }
6195 while (p != NULL_RTX);
6196
6197 /* Load up any constants needed into temp gprs */
6198 for (i = 0; i < frv_ifcvt.cur_scratch_regs; i++)
6199 {
6200 rtx insn = emit_insn_before (frv_ifcvt.scratch_regs[i], existing_insn);
6201 if (! frv_ifcvt.scratch_insns_bitmap)
7b210806 6202 frv_ifcvt.scratch_insns_bitmap = BITMAP_ALLOC (NULL);
36a05131
BS
6203 bitmap_set_bit (frv_ifcvt.scratch_insns_bitmap, INSN_UID (insn));
6204 frv_ifcvt.scratch_regs[i] = NULL_RTX;
6205 }
6206
6207 frv_ifcvt.added_insns_list = NULL_RTX;
6208 frv_ifcvt.cur_scratch_regs = 0;
6209}
6210
6211\f
6212/* A C expression to cancel any machine dependent modifications in converting
6213 code to conditional execution in the code described by the conditional if
6214 information CE_INFO. */
6215
6216void
84562394 6217frv_ifcvt_modify_cancel (ce_if_block *ce_info ATTRIBUTE_UNUSED)
36a05131
BS
6218{
6219 int i;
6220 rtx p = frv_ifcvt.added_insns_list;
6221
6222 /* Loop freeing up the EXPR_LIST's allocated. */
6223 while (p != NULL_RTX)
6224 {
6225 rtx check_and_jump = XEXP (p, 0);
6226 rtx old_p = p;
6227
6228 p = XEXP (p, 1);
6229 free_EXPR_LIST_node (check_and_jump);
6230 free_EXPR_LIST_node (old_p);
6231 }
6232
6233 /* Release any temporary gprs allocated. */
6234 for (i = 0; i < frv_ifcvt.cur_scratch_regs; i++)
6235 frv_ifcvt.scratch_regs[i] = NULL_RTX;
6236
6237 frv_ifcvt.added_insns_list = NULL_RTX;
6238 frv_ifcvt.cur_scratch_regs = 0;
6239 return;
6240}
6241\f
6242/* A C expression for the size in bytes of the trampoline, as an integer.
6243 The template is:
6244
6245 setlo #0, <jmp_reg>
6246 setlo #0, <static_chain>
6247 sethi #0, <jmp_reg>
6248 sethi #0, <static_chain>
6249 jmpl @(gr0,<jmp_reg>) */
6250
6251int
f2206911 6252frv_trampoline_size (void)
36a05131 6253{
34208acf
AO
6254 if (TARGET_FDPIC)
6255 /* Allocate room for the function descriptor and the lddi
6256 instruction. */
6257 return 8 + 6 * 4;
6258 return 5 /* instructions */ * 4 /* instruction size. */;
36a05131
BS
6259}
6260
6261\f
6262/* A C statement to initialize the variable parts of a trampoline. ADDR is an
6263 RTX for the address of the trampoline; FNADDR is an RTX for the address of
6264 the nested function; STATIC_CHAIN is an RTX for the static chain value that
6265 should be passed to the function when it is called.
6266
6267 The template is:
6268
6269 setlo #0, <jmp_reg>
6270 setlo #0, <static_chain>
6271 sethi #0, <jmp_reg>
6272 sethi #0, <static_chain>
6273 jmpl @(gr0,<jmp_reg>) */
6274
e9d5fdb2
RH
6275static void
6276frv_trampoline_init (rtx m_tramp, tree fndecl, rtx static_chain)
36a05131 6277{
e9d5fdb2
RH
6278 rtx addr = XEXP (m_tramp, 0);
6279 rtx fnaddr = XEXP (DECL_RTL (fndecl), 0);
36a05131
BS
6280 rtx sc_reg = force_reg (Pmode, static_chain);
6281
6282 emit_library_call (gen_rtx_SYMBOL_REF (SImode, "__trampoline_setup"),
5c5e8419 6283 LCT_NORMAL, VOIDmode, 4,
36a05131
BS
6284 addr, Pmode,
6285 GEN_INT (frv_trampoline_size ()), SImode,
6286 fnaddr, Pmode,
6287 sc_reg, Pmode);
6288}
6289
6290\f
6291/* Many machines have some registers that cannot be copied directly to or from
6292 memory or even from other types of registers. An example is the `MQ'
6293 register, which on most machines, can only be copied to or from general
6294 registers, but not memory. Some machines allow copying all registers to and
6295 from memory, but require a scratch register for stores to some memory
6296 locations (e.g., those with symbolic address on the RT, and those with
981f6289 6297 certain symbolic address on the SPARC when compiling PIC). In some cases,
36a05131
BS
6298 both an intermediate and a scratch register are required.
6299
6300 You should define these macros to indicate to the reload phase that it may
6301 need to allocate at least one register for a reload in addition to the
6302 register to contain the data. Specifically, if copying X to a register
0a2aaacc 6303 RCLASS in MODE requires an intermediate register, you should define
36a05131
BS
6304 `SECONDARY_INPUT_RELOAD_CLASS' to return the largest register class all of
6305 whose registers can be used as intermediate registers or scratch registers.
6306
0a2aaacc 6307 If copying a register RCLASS in MODE to X requires an intermediate or scratch
36a05131
BS
6308 register, `SECONDARY_OUTPUT_RELOAD_CLASS' should be defined to return the
6309 largest register class required. If the requirements for input and output
6310 reloads are the same, the macro `SECONDARY_RELOAD_CLASS' should be used
6311 instead of defining both macros identically.
6312
6313 The values returned by these macros are often `GENERAL_REGS'. Return
6314 `NO_REGS' if no spare register is needed; i.e., if X can be directly copied
0a2aaacc 6315 to or from a register of RCLASS in MODE without requiring a scratch register.
36a05131
BS
6316 Do not define this macro if it would always return `NO_REGS'.
6317
6318 If a scratch register is required (either with or without an intermediate
6319 register), you should define patterns for `reload_inM' or `reload_outM', as
6320 required.. These patterns, which will normally be implemented with a
6321 `define_expand', should be similar to the `movM' patterns, except that
6322 operand 2 is the scratch register.
6323
6324 Define constraints for the reload register and scratch register that contain
6325 a single register class. If the original reload register (whose class is
0a2aaacc 6326 RCLASS) can meet the constraint given in the pattern, the value returned by
36a05131
BS
6327 these macros is used for the class of the scratch register. Otherwise, two
6328 additional reload registers are required. Their classes are obtained from
6329 the constraints in the insn pattern.
6330
6331 X might be a pseudo-register or a `subreg' of a pseudo-register, which could
6332 either be in a hard register or in memory. Use `true_regnum' to find out;
6333 it will return -1 if the pseudo is in memory and the hard register number if
6334 it is in a register.
6335
6336 These macros should not be used in the case where a particular class of
6337 registers can only be copied to memory and not to another class of
6338 registers. In that case, secondary reload registers are not needed and
6339 would not be helpful. Instead, a stack location must be used to perform the
43aa4e05 6340 copy and the `movM' pattern should use memory as an intermediate storage.
36a05131
BS
6341 This case often occurs between floating-point and general registers. */
6342
6343enum reg_class
0a2aaacc 6344frv_secondary_reload_class (enum reg_class rclass,
ef4bddc2 6345 machine_mode mode ATTRIBUTE_UNUSED,
35f2d8ef 6346 rtx x)
36a05131
BS
6347{
6348 enum reg_class ret;
6349
0a2aaacc 6350 switch (rclass)
36a05131
BS
6351 {
6352 default:
6353 ret = NO_REGS;
6354 break;
6355
6356 /* Accumulators/Accumulator guard registers need to go through floating
6357 point registers. */
6358 case QUAD_REGS:
36a05131
BS
6359 case GPR_REGS:
6360 ret = NO_REGS;
6361 if (x && GET_CODE (x) == REG)
6362 {
6363 int regno = REGNO (x);
6364
6365 if (ACC_P (regno) || ACCG_P (regno))
6366 ret = FPR_REGS;
6367 }
6368 break;
6369
9cd10576 6370 /* Nonzero constants should be loaded into an FPR through a GPR. */
36a05131 6371 case QUAD_FPR_REGS:
36a05131
BS
6372 if (x && CONSTANT_P (x) && !ZERO_P (x))
6373 ret = GPR_REGS;
6374 else
6375 ret = NO_REGS;
6376 break;
6377
6378 /* All of these types need gpr registers. */
6379 case ICC_REGS:
6380 case FCC_REGS:
6381 case CC_REGS:
6382 case ICR_REGS:
6383 case FCR_REGS:
6384 case CR_REGS:
6385 case LCR_REG:
6386 case LR_REG:
6387 ret = GPR_REGS;
6388 break;
6389
35f2d8ef 6390 /* The accumulators need fpr registers. */
36a05131
BS
6391 case QUAD_ACC_REGS:
6392 case ACCG_REGS:
6393 ret = FPR_REGS;
6394 break;
6395 }
6396
6397 return ret;
6398}
6399
35f2d8ef
NC
6400/* This hook exists to catch the case where secondary_reload_class() is
6401 called from init_reg_autoinc() in regclass.c - before the reload optabs
6402 have been initialised. */
6403
a87cf97e
JR
6404static reg_class_t
6405frv_secondary_reload (bool in_p, rtx x, reg_class_t reload_class_i,
ef4bddc2 6406 machine_mode reload_mode,
35f2d8ef
NC
6407 secondary_reload_info * sri)
6408{
6409 enum reg_class rclass = NO_REGS;
a87cf97e 6410 enum reg_class reload_class = (enum reg_class) reload_class_i;
35f2d8ef
NC
6411
6412 if (sri->prev_sri && sri->prev_sri->t_icode != CODE_FOR_nothing)
6413 {
6414 sri->icode = sri->prev_sri->t_icode;
6415 return NO_REGS;
6416 }
6417
6418 rclass = frv_secondary_reload_class (reload_class, reload_mode, x);
6419
6420 if (rclass != NO_REGS)
6421 {
f9621cc4
RS
6422 enum insn_code icode
6423 = direct_optab_handler (in_p ? reload_in_optab : reload_out_optab,
6424 reload_mode);
35f2d8ef
NC
6425 if (icode == 0)
6426 {
6427 /* This happens when then the reload_[in|out]_optabs have
6428 not been initialised. */
6429 sri->t_icode = CODE_FOR_nothing;
6430 return rclass;
6431 }
6432 }
6433
6434 /* Fall back to the default secondary reload handler. */
6435 return default_secondary_reload (in_p, x, reload_class, reload_mode, sri);
6436
6437}
36a05131 6438\f
c28350ab 6439/* Worker function for TARGET_CLASS_LIKELY_SPILLED_P. */
36a05131 6440
c28350ab
AS
6441static bool
6442frv_class_likely_spilled_p (reg_class_t rclass)
36a05131 6443{
0a2aaacc 6444 switch (rclass)
36a05131
BS
6445 {
6446 default:
6447 break;
6448
17c21957
AO
6449 case GR8_REGS:
6450 case GR9_REGS:
6451 case GR89_REGS:
6452 case FDPIC_FPTR_REGS:
6453 case FDPIC_REGS:
36a05131
BS
6454 case ICC_REGS:
6455 case FCC_REGS:
6456 case CC_REGS:
6457 case ICR_REGS:
6458 case FCR_REGS:
6459 case CR_REGS:
6460 case LCR_REG:
6461 case LR_REG:
6462 case SPR_REGS:
6463 case QUAD_ACC_REGS:
36a05131 6464 case ACCG_REGS:
c28350ab 6465 return true;
36a05131
BS
6466 }
6467
c28350ab 6468 return false;
36a05131
BS
6469}
6470
6471\f
6472/* An expression for the alignment of a structure field FIELD if the
7ec022b2 6473 alignment computed in the usual way is COMPUTED. GCC uses this
36a05131
BS
6474 value instead of the value in `BIGGEST_ALIGNMENT' or
6475 `BIGGEST_FIELD_ALIGNMENT', if defined, for structure fields only. */
6476
6477/* The definition type of the bit field data is either char, short, long or
6478 long long. The maximum bit size is the number of bits of its own type.
6479
6480 The bit field data is assigned to a storage unit that has an adequate size
6481 for bit field data retention and is located at the smallest address.
6482
6483 Consecutive bit field data are packed at consecutive bits having the same
6484 storage unit, with regard to the type, beginning with the MSB and continuing
6485 toward the LSB.
6486
6487 If a field to be assigned lies over a bit field type boundary, its
6488 assignment is completed by aligning it with a boundary suitable for the
6489 type.
6490
6491 When a bit field having a bit length of 0 is declared, it is forcibly
6492 assigned to the next storage unit.
6493
6494 e.g)
6495 struct {
6496 int a:2;
6497 int b:6;
6498 char c:4;
6499 int d:10;
6500 int :0;
6501 int f:2;
6502 } x;
6503
6504 +0 +1 +2 +3
6505 &x 00000000 00000000 00000000 00000000
6506 MLM----L
6507 a b
6508 &x+4 00000000 00000000 00000000 00000000
6509 M--L
6510 c
6511 &x+8 00000000 00000000 00000000 00000000
6512 M----------L
6513 d
6514 &x+12 00000000 00000000 00000000 00000000
6515 ML
6516 f
6517*/
6518
6519int
f2206911 6520frv_adjust_field_align (tree field, int computed)
36a05131 6521{
b16c1435
EC
6522 /* Make sure that the bitfield is not wider than the type. */
6523 if (DECL_BIT_FIELD (field)
25f93e18 6524 && !DECL_ARTIFICIAL (field))
36a05131
BS
6525 {
6526 tree parent = DECL_CONTEXT (field);
6527 tree prev = NULL_TREE;
6528 tree cur;
6529
910ad8de 6530 for (cur = TYPE_FIELDS (parent); cur && cur != field; cur = DECL_CHAIN (cur))
36a05131
BS
6531 {
6532 if (TREE_CODE (cur) != FIELD_DECL)
6533 continue;
6534
6535 prev = cur;
6536 }
6537
44e91694 6538 gcc_assert (cur);
36a05131
BS
6539
6540 /* If this isn't a :0 field and if the previous element is a bitfield
6541 also, see if the type is different, if so, we will need to align the
87b483a1 6542 bit-field to the next boundary. */
36a05131
BS
6543 if (prev
6544 && ! DECL_PACKED (field)
6545 && ! integer_zerop (DECL_SIZE (field))
6546 && DECL_BIT_FIELD_TYPE (field) != DECL_BIT_FIELD_TYPE (prev))
6547 {
6548 int prev_align = TYPE_ALIGN (TREE_TYPE (prev));
6549 int cur_align = TYPE_ALIGN (TREE_TYPE (field));
6550 computed = (prev_align > cur_align) ? prev_align : cur_align;
6551 }
6552 }
6553
6554 return computed;
6555}
6556
6557\f
6558/* A C expression that is nonzero if it is permissible to store a value of mode
6559 MODE in hard register number REGNO (or in several registers starting with
6560 that one). For a machine where all registers are equivalent, a suitable
6561 definition is
6562
6563 #define HARD_REGNO_MODE_OK(REGNO, MODE) 1
6564
6565 It is not necessary for this macro to check for the numbers of fixed
6566 registers, because the allocation mechanism considers them to be always
6567 occupied.
6568
6569 On some machines, double-precision values must be kept in even/odd register
6570 pairs. The way to implement that is to define this macro to reject odd
6571 register numbers for such modes.
6572
6573 The minimum requirement for a mode to be OK in a register is that the
6574 `movMODE' instruction pattern support moves between the register and any
6575 other hard register for which the mode is OK; and that moving a value into
6576 the register and back out not alter it.
6577
6578 Since the same instruction used to move `SImode' will work for all narrower
6579 integer modes, it is not necessary on any machine for `HARD_REGNO_MODE_OK'
6580 to distinguish between these modes, provided you define patterns `movhi',
6581 etc., to take advantage of this. This is useful because of the interaction
6582 between `HARD_REGNO_MODE_OK' and `MODES_TIEABLE_P'; it is very desirable for
6583 all integer modes to be tieable.
6584
6585 Many machines have special registers for floating point arithmetic. Often
6586 people assume that floating point machine modes are allowed only in floating
6587 point registers. This is not true. Any registers that can hold integers
6588 can safely *hold* a floating point machine mode, whether or not floating
6589 arithmetic can be done on it in those registers. Integer move instructions
6590 can be used to move the values.
6591
6592 On some machines, though, the converse is true: fixed-point machine modes
6593 may not go in floating registers. This is true if the floating registers
6594 normalize any value stored in them, because storing a non-floating value
6595 there would garble it. In this case, `HARD_REGNO_MODE_OK' should reject
6596 fixed-point machine modes in floating registers. But if the floating
6597 registers do not automatically normalize, if you can store any bit pattern
6598 in one and retrieve it unchanged without a trap, then any machine mode may
6599 go in a floating register, so you can define this macro to say so.
6600
6601 The primary significance of special floating registers is rather that they
6602 are the registers acceptable in floating point arithmetic instructions.
6603 However, this is of no concern to `HARD_REGNO_MODE_OK'. You handle it by
6604 writing the proper constraints for those instructions.
6605
6606 On some machines, the floating registers are especially slow to access, so
6607 that it is better to store a value in a stack frame than in such a register
6608 if floating point arithmetic is not being done. As long as the floating
6609 registers are not in class `GENERAL_REGS', they will not be used unless some
6610 pattern's constraint asks for one. */
6611
6612int
ef4bddc2 6613frv_hard_regno_mode_ok (int regno, machine_mode mode)
36a05131
BS
6614{
6615 int base;
6616 int mask;
6617
6618 switch (mode)
6619 {
6620 case CCmode:
6621 case CC_UNSmode:
036ff63f 6622 case CC_NZmode:
36a05131
BS
6623 return ICC_P (regno) || GPR_P (regno);
6624
6625 case CC_CCRmode:
6626 return CR_P (regno) || GPR_P (regno);
6627
6628 case CC_FPmode:
6629 return FCC_P (regno) || GPR_P (regno);
6630
6631 default:
6632 break;
6633 }
6634
6635 /* Set BASE to the first register in REGNO's class. Set MASK to the
6636 bits that must be clear in (REGNO - BASE) for the register to be
6637 well-aligned. */
6638 if (INTEGRAL_MODE_P (mode) || FLOAT_MODE_P (mode) || VECTOR_MODE_P (mode))
6639 {
6640 if (ACCG_P (regno))
6641 {
6642 /* ACCGs store one byte. Two-byte quantities must start in
6643 even-numbered registers, four-byte ones in registers whose
6644 numbers are divisible by four, and so on. */
6645 base = ACCG_FIRST;
6646 mask = GET_MODE_SIZE (mode) - 1;
6647 }
6648 else
6649 {
b16c1435
EC
6650 /* The other registers store one word. */
6651 if (GPR_P (regno) || regno == AP_FIRST)
36a05131
BS
6652 base = GPR_FIRST;
6653
6654 else if (FPR_P (regno))
6655 base = FPR_FIRST;
6656
6657 else if (ACC_P (regno))
6658 base = ACC_FIRST;
6659
b16c1435
EC
6660 else if (SPR_P (regno))
6661 return mode == SImode;
6662
87b483a1 6663 /* Fill in the table. */
36a05131
BS
6664 else
6665 return 0;
6666
6667 /* Anything smaller than an SI is OK in any word-sized register. */
6668 if (GET_MODE_SIZE (mode) < 4)
6669 return 1;
6670
6671 mask = (GET_MODE_SIZE (mode) / 4) - 1;
6672 }
6673 return (((regno - base) & mask) == 0);
6674 }
6675
6676 return 0;
6677}
6678
6679\f
6680/* A C expression for the number of consecutive hard registers, starting at
6681 register number REGNO, required to hold a value of mode MODE.
6682
6683 On a machine where all registers are exactly one word, a suitable definition
6684 of this macro is
6685
6686 #define HARD_REGNO_NREGS(REGNO, MODE) \
6687 ((GET_MODE_SIZE (MODE) + UNITS_PER_WORD - 1) \
6688 / UNITS_PER_WORD)) */
6689
6690/* On the FRV, make the CC_FP mode take 3 words in the integer registers, so
6691 that we can build the appropriate instructions to properly reload the
6692 values. Also, make the byte-sized accumulator guards use one guard
6693 for each byte. */
6694
6695int
ef4bddc2 6696frv_hard_regno_nregs (int regno, machine_mode mode)
36a05131
BS
6697{
6698 if (ACCG_P (regno))
6699 return GET_MODE_SIZE (mode);
6700 else
6701 return (GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
6702}
6703
6704\f
6705/* A C expression for the maximum number of consecutive registers of
0a2aaacc 6706 class RCLASS needed to hold a value of mode MODE.
36a05131
BS
6707
6708 This is closely related to the macro `HARD_REGNO_NREGS'. In fact, the value
0a2aaacc
KG
6709 of the macro `CLASS_MAX_NREGS (RCLASS, MODE)' should be the maximum value of
6710 `HARD_REGNO_NREGS (REGNO, MODE)' for all REGNO values in the class RCLASS.
36a05131
BS
6711
6712 This macro helps control the handling of multiple-word values in
6713 the reload pass.
6714
6715 This declaration is required. */
6716
6717int
ef4bddc2 6718frv_class_max_nregs (enum reg_class rclass, machine_mode mode)
36a05131 6719{
0a2aaacc 6720 if (rclass == ACCG_REGS)
36a05131
BS
6721 /* An N-byte value requires N accumulator guards. */
6722 return GET_MODE_SIZE (mode);
6723 else
6724 return (GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
6725}
6726
6727\f
6728/* A C expression that is nonzero if X is a legitimate constant for an
6729 immediate operand on the target machine. You can assume that X satisfies
6730 `CONSTANT_P', so you need not check this. In fact, `1' is a suitable
6731 definition for this macro on machines where anything `CONSTANT_P' is valid. */
6732
1a627b35 6733static bool
ef4bddc2 6734frv_legitimate_constant_p (machine_mode mode, rtx x)
36a05131 6735{
34208acf
AO
6736 /* frv_cannot_force_const_mem always returns true for FDPIC. This
6737 means that the move expanders will be expected to deal with most
6738 kinds of constant, regardless of what we return here.
6739
1a627b35 6740 However, among its other duties, frv_legitimate_constant_p decides whether
34208acf
AO
6741 a constant can be entered into reg_equiv_constant[]. If we return true,
6742 reload can create new instances of the constant whenever it likes.
6743
6744 The idea is therefore to accept as many constants as possible (to give
6745 reload more freedom) while rejecting constants that can only be created
6746 at certain times. In particular, anything with a symbolic component will
6747 require use of the pseudo FDPIC register, which is only available before
6748 reload. */
6749 if (TARGET_FDPIC)
6750 return LEGITIMATE_PIC_OPERAND_P (x);
6751
87b483a1 6752 /* All of the integer constants are ok. */
36a05131
BS
6753 if (GET_CODE (x) != CONST_DOUBLE)
6754 return TRUE;
6755
87b483a1 6756 /* double integer constants are ok. */
1a627b35 6757 if (GET_MODE (x) == VOIDmode || mode == DImode)
36a05131
BS
6758 return TRUE;
6759
87b483a1 6760 /* 0 is always ok. */
36a05131
BS
6761 if (x == CONST0_RTX (mode))
6762 return TRUE;
6763
6764 /* If floating point is just emulated, allow any constant, since it will be
87b483a1 6765 constructed in the GPRs. */
36a05131
BS
6766 if (!TARGET_HAS_FPRS)
6767 return TRUE;
6768
6769 if (mode == DFmode && !TARGET_DOUBLE)
6770 return TRUE;
6771
6772 /* Otherwise store the constant away and do a load. */
6773 return FALSE;
6774}
036ff63f
RS
6775
6776/* Implement SELECT_CC_MODE. Choose CC_FP for floating-point comparisons,
6777 CC_NZ for comparisons against zero in which a single Z or N flag test
6778 is enough, CC_UNS for other unsigned comparisons, and CC for other
6779 signed comparisons. */
6780
ef4bddc2 6781machine_mode
036ff63f
RS
6782frv_select_cc_mode (enum rtx_code code, rtx x, rtx y)
6783{
6784 if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
6785 return CC_FPmode;
6786
6787 switch (code)
6788 {
6789 case EQ:
6790 case NE:
6791 case LT:
6792 case GE:
6793 return y == const0_rtx ? CC_NZmode : CCmode;
6794
6795 case GTU:
6796 case GEU:
6797 case LTU:
6798 case LEU:
6799 return y == const0_rtx ? CC_NZmode : CC_UNSmode;
6800
6801 default:
6802 return CCmode;
6803 }
6804}
36a05131 6805\f
33124e84
AS
6806
6807/* Worker function for TARGET_REGISTER_MOVE_COST. */
36a05131
BS
6808
6809#define HIGH_COST 40
6810#define MEDIUM_COST 3
6811#define LOW_COST 1
6812
33124e84 6813static int
ef4bddc2 6814frv_register_move_cost (machine_mode mode ATTRIBUTE_UNUSED,
33124e84 6815 reg_class_t from, reg_class_t to)
36a05131
BS
6816{
6817 switch (from)
6818 {
6819 default:
6820 break;
6821
6822 case QUAD_REGS:
36a05131 6823 case GPR_REGS:
73d7e266
VM
6824 case GR8_REGS:
6825 case GR9_REGS:
6826 case GR89_REGS:
6827 case FDPIC_REGS:
6828 case FDPIC_FPTR_REGS:
6829 case FDPIC_CALL_REGS:
6830
36a05131
BS
6831 switch (to)
6832 {
6833 default:
6834 break;
6835
9b5db25d 6836 case QUAD_REGS:
36a05131 6837 case GPR_REGS:
73d7e266
VM
6838 case GR8_REGS:
6839 case GR9_REGS:
6840 case GR89_REGS:
6841 case FDPIC_REGS:
6842 case FDPIC_FPTR_REGS:
6843 case FDPIC_CALL_REGS:
6844
36a05131
BS
6845 return LOW_COST;
6846
36a05131
BS
6847 case FPR_REGS:
6848 return LOW_COST;
6849
6850 case LCR_REG:
6851 case LR_REG:
6852 case SPR_REGS:
6853 return LOW_COST;
6854 }
6855
9b5db25d 6856 case QUAD_FPR_REGS:
36a05131
BS
6857 switch (to)
6858 {
6859 default:
6860 break;
6861
6862 case QUAD_REGS:
36a05131 6863 case GPR_REGS:
73d7e266
VM
6864 case GR8_REGS:
6865 case GR9_REGS:
6866 case GR89_REGS:
6867 case FDPIC_REGS:
6868 case FDPIC_FPTR_REGS:
6869 case FDPIC_CALL_REGS:
6870
36a05131
BS
6871 case QUAD_ACC_REGS:
6872 case ACCG_REGS:
6873 return MEDIUM_COST;
6874
9b5db25d 6875 case QUAD_FPR_REGS:
36a05131
BS
6876 return LOW_COST;
6877 }
6878
6879 case LCR_REG:
6880 case LR_REG:
6881 case SPR_REGS:
6882 switch (to)
6883 {
6884 default:
6885 break;
6886
6887 case QUAD_REGS:
36a05131 6888 case GPR_REGS:
73d7e266
VM
6889 case GR8_REGS:
6890 case GR9_REGS:
6891 case GR89_REGS:
6892 case FDPIC_REGS:
6893 case FDPIC_FPTR_REGS:
6894 case FDPIC_CALL_REGS:
6895
36a05131
BS
6896 return MEDIUM_COST;
6897 }
6898
36a05131
BS
6899 case QUAD_ACC_REGS:
6900 case ACCG_REGS:
6901 switch (to)
6902 {
6903 default:
6904 break;
6905
9b5db25d 6906 case QUAD_FPR_REGS:
36a05131
BS
6907 return MEDIUM_COST;
6908
6909 }
6910 }
6911
6912 return HIGH_COST;
6913}
33124e84
AS
6914
6915/* Worker function for TARGET_MEMORY_MOVE_COST. */
6916
6917static int
ef4bddc2 6918frv_memory_move_cost (machine_mode mode ATTRIBUTE_UNUSED,
33124e84
AS
6919 reg_class_t rclass ATTRIBUTE_UNUSED,
6920 bool in ATTRIBUTE_UNUSED)
6921{
6922 return 4;
6923}
6924
36a05131
BS
6925\f
6926/* Implementation of TARGET_ASM_INTEGER. In the FRV case we need to
6927 use ".picptr" to generate safe relocations for PIC code. We also
6928 need a fixup entry for aligned (non-debugging) code. */
6929
6930static bool
f2206911 6931frv_assemble_integer (rtx value, unsigned int size, int aligned_p)
36a05131 6932{
34208acf 6933 if ((flag_pic || TARGET_FDPIC) && size == UNITS_PER_WORD)
36a05131
BS
6934 {
6935 if (GET_CODE (value) == CONST
6936 || GET_CODE (value) == SYMBOL_REF
6937 || GET_CODE (value) == LABEL_REF)
6938 {
34208acf
AO
6939 if (TARGET_FDPIC && GET_CODE (value) == SYMBOL_REF
6940 && SYMBOL_REF_FUNCTION_P (value))
6941 {
6942 fputs ("\t.picptr\tfuncdesc(", asm_out_file);
6943 output_addr_const (asm_out_file, value);
6944 fputs (")\n", asm_out_file);
6945 return true;
6946 }
6947 else if (TARGET_FDPIC && GET_CODE (value) == CONST
6948 && frv_function_symbol_referenced_p (value))
6949 return false;
6950 if (aligned_p && !TARGET_FDPIC)
36a05131
BS
6951 {
6952 static int label_num = 0;
6953 char buf[256];
6954 const char *p;
6955
6956 ASM_GENERATE_INTERNAL_LABEL (buf, "LCP", label_num++);
14966b94 6957 p = (* targetm.strip_name_encoding) (buf);
36a05131
BS
6958
6959 fprintf (asm_out_file, "%s:\n", p);
6960 fprintf (asm_out_file, "%s\n", FIXUP_SECTION_ASM_OP);
6961 fprintf (asm_out_file, "\t.picptr\t%s\n", p);
6962 fprintf (asm_out_file, "\t.previous\n");
6963 }
6964 assemble_integer_with_op ("\t.picptr\t", value);
6965 return true;
6966 }
6967 if (!aligned_p)
6968 {
6969 /* We've set the unaligned SI op to NULL, so we always have to
6970 handle the unaligned case here. */
6971 assemble_integer_with_op ("\t.4byte\t", value);
6972 return true;
6973 }
6974 }
6975 return default_assemble_integer (value, size, aligned_p);
6976}
6977
6978/* Function to set up the backend function structure. */
6979
6980static struct machine_function *
f2206911 6981frv_init_machine_status (void)
36a05131 6982{
766090c2 6983 return ggc_cleared_alloc<machine_function> ();
36a05131 6984}
ffb344c1 6985\f
28a60850
RS
6986/* Implement TARGET_SCHED_ISSUE_RATE. */
6987
c557edf4 6988int
28a60850
RS
6989frv_issue_rate (void)
6990{
6991 if (!TARGET_PACK)
6992 return 1;
6993
6994 switch (frv_cpu_type)
6995 {
6996 default:
6997 case FRV_CPU_FR300:
6998 case FRV_CPU_SIMPLE:
6999 return 1;
7000
7001 case FRV_CPU_FR400:
c557edf4
RS
7002 case FRV_CPU_FR405:
7003 case FRV_CPU_FR450:
28a60850
RS
7004 return 2;
7005
7006 case FRV_CPU_GENERIC:
7007 case FRV_CPU_FR500:
7008 case FRV_CPU_TOMCAT:
7009 return 4;
c557edf4
RS
7010
7011 case FRV_CPU_FR550:
7012 return 8;
28a60850
RS
7013 }
7014}
36a05131 7015\f
c557edf4 7016/* Return the value of INSN's acc_group attribute. */
36a05131 7017
c557edf4
RS
7018int
7019frv_acc_group (rtx insn)
7020{
7021 /* This distinction only applies to the FR550 packing constraints. */
cc665e56
RS
7022 if (frv_cpu_type == FRV_CPU_FR550)
7023 {
7024 subrtx_iterator::array_type array;
7025 FOR_EACH_SUBRTX (iter, array, PATTERN (insn), NONCONST)
7026 if (REG_P (*iter))
7027 {
7028 unsigned int regno = REGNO (*iter);
7029 /* If REGNO refers to an accumulator, return ACC_GROUP_ODD if
7030 the bit 2 of the register number is set and ACC_GROUP_EVEN if
7031 it is clear. */
7032 if (ACC_P (regno))
7033 return (regno - ACC_FIRST) & 4 ? ACC_GROUP_ODD : ACC_GROUP_EVEN;
7034 if (ACCG_P (regno))
7035 return (regno - ACCG_FIRST) & 4 ? ACC_GROUP_ODD : ACC_GROUP_EVEN;
7036 }
7037 }
7038 return ACC_GROUP_NONE;
c557edf4 7039}
36a05131 7040
c557edf4
RS
7041/* Return the index of the DFA unit in FRV_UNIT_NAMES[] that instruction
7042 INSN will try to claim first. Since this value depends only on the
7043 type attribute, we can cache the results in FRV_TYPE_TO_UNIT[]. */
36a05131 7044
c557edf4 7045static unsigned int
84034c69 7046frv_insn_unit (rtx_insn *insn)
c557edf4
RS
7047{
7048 enum attr_type type;
36a05131 7049
c557edf4
RS
7050 type = get_attr_type (insn);
7051 if (frv_type_to_unit[type] == ARRAY_SIZE (frv_unit_codes))
7052 {
7053 /* We haven't seen this type of instruction before. */
7054 state_t state;
7055 unsigned int unit;
36a05131 7056
c557edf4
RS
7057 /* Issue the instruction on its own to see which unit it prefers. */
7058 state = alloca (state_size ());
7059 state_reset (state);
7060 state_transition (state, insn);
36a05131 7061
c557edf4
RS
7062 /* Find out which unit was taken. */
7063 for (unit = 0; unit < ARRAY_SIZE (frv_unit_codes); unit++)
7064 if (cpu_unit_reservation_p (state, frv_unit_codes[unit]))
7065 break;
36a05131 7066
44e91694 7067 gcc_assert (unit != ARRAY_SIZE (frv_unit_codes));
36a05131 7068
c557edf4 7069 frv_type_to_unit[type] = unit;
36a05131 7070 }
c557edf4
RS
7071 return frv_type_to_unit[type];
7072}
36a05131 7073
c557edf4 7074/* Return true if INSN issues to a branch unit. */
36a05131 7075
c557edf4 7076static bool
84034c69 7077frv_issues_to_branch_unit_p (rtx_insn *insn)
c557edf4
RS
7078{
7079 return frv_unit_groups[frv_insn_unit (insn)] == GROUP_B;
7080}
7081\f
5c5e8419
JR
7082/* The instructions in the packet, partitioned into groups. */
7083struct frv_packet_group {
7084 /* How many instructions in the packet belong to this group. */
7085 unsigned int num_insns;
7086
7087 /* A list of the instructions that belong to this group, in the order
7088 they appear in the rtl stream. */
b32d5189 7089 rtx_insn *insns[ARRAY_SIZE (frv_unit_codes)];
5c5e8419
JR
7090
7091 /* The contents of INSNS after they have been sorted into the correct
7092 assembly-language order. Element X issues to unit X. The list may
7093 contain extra nops. */
b32d5189 7094 rtx_insn *sorted[ARRAY_SIZE (frv_unit_codes)];
5c5e8419
JR
7095
7096 /* The member of frv_nops[] to use in sorted[]. */
b32d5189 7097 rtx_insn *nop;
5c5e8419
JR
7098};
7099
c557edf4
RS
7100/* The current state of the packing pass, implemented by frv_pack_insns. */
7101static struct {
7102 /* The state of the pipeline DFA. */
7103 state_t dfa_state;
7104
7105 /* Which hardware registers are set within the current packet,
7106 and the conditions under which they are set. */
7107 regstate_t regstate[FIRST_PSEUDO_REGISTER];
7108
7109 /* The memory locations that have been modified so far in this
7110 packet. MEM is the memref and COND is the regstate_t condition
7111 under which it is set. */
7112 struct {
7113 rtx mem;
7114 regstate_t cond;
7115 } mems[2];
7116
7117 /* The number of valid entries in MEMS. The value is larger than
7118 ARRAY_SIZE (mems) if there were too many mems to record. */
7119 unsigned int num_mems;
7120
7121 /* The maximum number of instructions that can be packed together. */
7122 unsigned int issue_rate;
7123
7124 /* The instructions in the packet, partitioned into groups. */
5c5e8419 7125 struct frv_packet_group groups[NUM_GROUPS];
c557edf4
RS
7126
7127 /* The instructions that make up the current packet. */
b32d5189 7128 rtx_insn *insns[ARRAY_SIZE (frv_unit_codes)];
c557edf4
RS
7129 unsigned int num_insns;
7130} frv_packet;
7131
7132/* Return the regstate_t flags for the given COND_EXEC condition.
7133 Abort if the condition isn't in the right form. */
36a05131 7134
c557edf4
RS
7135static int
7136frv_cond_flags (rtx cond)
7137{
44e91694
NS
7138 gcc_assert ((GET_CODE (cond) == EQ || GET_CODE (cond) == NE)
7139 && GET_CODE (XEXP (cond, 0)) == REG
7140 && CR_P (REGNO (XEXP (cond, 0)))
7141 && XEXP (cond, 1) == const0_rtx);
7142 return ((REGNO (XEXP (cond, 0)) - CR_FIRST)
7143 | (GET_CODE (cond) == NE
7144 ? REGSTATE_IF_TRUE
7145 : REGSTATE_IF_FALSE));
c557edf4 7146}
36a05131 7147
36a05131 7148
c557edf4
RS
7149/* Return true if something accessed under condition COND2 can
7150 conflict with something written under condition COND1. */
36a05131 7151
c557edf4
RS
7152static bool
7153frv_regstate_conflict_p (regstate_t cond1, regstate_t cond2)
7154{
7155 /* If either reference was unconditional, we have a conflict. */
7156 if ((cond1 & REGSTATE_IF_EITHER) == 0
7157 || (cond2 & REGSTATE_IF_EITHER) == 0)
7158 return true;
7159
7160 /* The references might conflict if they were controlled by
7161 different CRs. */
7162 if ((cond1 & REGSTATE_CC_MASK) != (cond2 & REGSTATE_CC_MASK))
7163 return true;
7164
7165 /* They definitely conflict if they are controlled by the
7166 same condition. */
7167 if ((cond1 & cond2 & REGSTATE_IF_EITHER) != 0)
7168 return true;
7169
7170 return false;
36a05131
BS
7171}
7172
c557edf4
RS
7173
7174/* A for_each_rtx callback. Return 1 if *X depends on an instruction in
7175 the current packet. DATA points to a regstate_t that describes the
7176 condition under which *X might be set or used. */
36a05131
BS
7177
7178static int
c557edf4 7179frv_registers_conflict_p_1 (rtx *x, void *data)
36a05131 7180{
c557edf4
RS
7181 unsigned int regno, i;
7182 regstate_t cond;
36a05131 7183
c557edf4 7184 cond = *(regstate_t *) data;
36a05131 7185
c557edf4
RS
7186 if (GET_CODE (*x) == REG)
7187 FOR_EACH_REGNO (regno, *x)
7188 if ((frv_packet.regstate[regno] & REGSTATE_MODIFIED) != 0)
7189 if (frv_regstate_conflict_p (frv_packet.regstate[regno], cond))
7190 return 1;
36a05131 7191
c557edf4
RS
7192 if (GET_CODE (*x) == MEM)
7193 {
7194 /* If we ran out of memory slots, assume a conflict. */
7195 if (frv_packet.num_mems > ARRAY_SIZE (frv_packet.mems))
7196 return 1;
36a05131 7197
c557edf4
RS
7198 /* Check for output or true dependencies with earlier MEMs. */
7199 for (i = 0; i < frv_packet.num_mems; i++)
7200 if (frv_regstate_conflict_p (frv_packet.mems[i].cond, cond))
7201 {
53d9622b 7202 if (true_dependence (frv_packet.mems[i].mem, VOIDmode, *x))
c557edf4 7203 return 1;
36a05131 7204
c557edf4
RS
7205 if (output_dependence (frv_packet.mems[i].mem, *x))
7206 return 1;
7207 }
7208 }
36a05131 7209
c557edf4
RS
7210 /* The return values of calls aren't significant: they describe
7211 the effect of the call as a whole, not of the insn itself. */
7212 if (GET_CODE (*x) == SET && GET_CODE (SET_SRC (*x)) == CALL)
7213 {
7214 if (for_each_rtx (&SET_SRC (*x), frv_registers_conflict_p_1, data))
7215 return 1;
7216 return -1;
7217 }
36a05131 7218
c557edf4
RS
7219 /* Check subexpressions. */
7220 return 0;
7221}
36a05131 7222
36a05131 7223
c557edf4
RS
7224/* Return true if something in X might depend on an instruction
7225 in the current packet. */
36a05131 7226
c557edf4
RS
7227static bool
7228frv_registers_conflict_p (rtx x)
7229{
7230 regstate_t flags;
36a05131 7231
c557edf4
RS
7232 flags = 0;
7233 if (GET_CODE (x) == COND_EXEC)
7234 {
7235 if (for_each_rtx (&XEXP (x, 0), frv_registers_conflict_p_1, &flags))
7236 return true;
36a05131 7237
c557edf4
RS
7238 flags |= frv_cond_flags (XEXP (x, 0));
7239 x = XEXP (x, 1);
36a05131 7240 }
c557edf4
RS
7241 return for_each_rtx (&x, frv_registers_conflict_p_1, &flags);
7242}
36a05131
BS
7243
7244
c557edf4
RS
7245/* A note_stores callback. DATA points to the regstate_t condition
7246 under which X is modified. Update FRV_PACKET accordingly. */
36a05131 7247
c557edf4 7248static void
7bc980e1 7249frv_registers_update_1 (rtx x, const_rtx pat ATTRIBUTE_UNUSED, void *data)
c557edf4
RS
7250{
7251 unsigned int regno;
7252
7253 if (GET_CODE (x) == REG)
7254 FOR_EACH_REGNO (regno, x)
7255 frv_packet.regstate[regno] |= *(regstate_t *) data;
7256
7257 if (GET_CODE (x) == MEM)
36a05131 7258 {
c557edf4 7259 if (frv_packet.num_mems < ARRAY_SIZE (frv_packet.mems))
36a05131 7260 {
c557edf4
RS
7261 frv_packet.mems[frv_packet.num_mems].mem = x;
7262 frv_packet.mems[frv_packet.num_mems].cond = *(regstate_t *) data;
7263 }
7264 frv_packet.num_mems++;
7265 }
7266}
36a05131 7267
36a05131 7268
c557edf4
RS
7269/* Update the register state information for an instruction whose
7270 body is X. */
7271
7272static void
7273frv_registers_update (rtx x)
7274{
7275 regstate_t flags;
7276
7277 flags = REGSTATE_MODIFIED;
7278 if (GET_CODE (x) == COND_EXEC)
7279 {
7280 flags |= frv_cond_flags (XEXP (x, 0));
7281 x = XEXP (x, 1);
36a05131 7282 }
c557edf4
RS
7283 note_stores (x, frv_registers_update_1, &flags);
7284}
36a05131 7285
c557edf4
RS
7286
7287/* Initialize frv_packet for the start of a new packet. */
7288
7289static void
7290frv_start_packet (void)
7291{
7292 enum frv_insn_group group;
7293
7294 memset (frv_packet.regstate, 0, sizeof (frv_packet.regstate));
7295 frv_packet.num_mems = 0;
7296 frv_packet.num_insns = 0;
5c5e8419
JR
7297 for (group = GROUP_I; group < NUM_GROUPS;
7298 group = (enum frv_insn_group) (group + 1))
c557edf4 7299 frv_packet.groups[group].num_insns = 0;
36a05131
BS
7300}
7301
36a05131 7302
c557edf4
RS
7303/* Likewise for the start of a new basic block. */
7304
7305static void
7306frv_start_packet_block (void)
36a05131 7307{
c557edf4
RS
7308 state_reset (frv_packet.dfa_state);
7309 frv_start_packet ();
7310}
36a05131 7311
c557edf4
RS
7312
7313/* Finish the current packet, if any, and start a new one. Call
7314 HANDLE_PACKET with FRV_PACKET describing the completed packet. */
7315
7316static void
7317frv_finish_packet (void (*handle_packet) (void))
7318{
7319 if (frv_packet.num_insns > 0)
36a05131 7320 {
c557edf4
RS
7321 handle_packet ();
7322 state_transition (frv_packet.dfa_state, 0);
7323 frv_start_packet ();
7324 }
7325}
36a05131 7326
36a05131 7327
c557edf4
RS
7328/* Return true if INSN can be added to the current packet. Update
7329 the DFA state on success. */
36a05131 7330
c557edf4 7331static bool
84034c69 7332frv_pack_insn_p (rtx_insn *insn)
c557edf4
RS
7333{
7334 /* See if the packet is already as long as it can be. */
7335 if (frv_packet.num_insns == frv_packet.issue_rate)
7336 return false;
36a05131 7337
c557edf4
RS
7338 /* If the scheduler thought that an instruction should start a packet,
7339 it's usually a good idea to believe it. It knows much more about
7340 the latencies than we do.
36a05131 7341
c557edf4 7342 There are some exceptions though:
36a05131 7343
c557edf4
RS
7344 - Conditional instructions are scheduled on the assumption that
7345 they will be executed. This is usually a good thing, since it
c112cf2b 7346 tends to avoid unnecessary stalls in the conditional code.
c557edf4
RS
7347 But we want to pack conditional instructions as tightly as
7348 possible, in order to optimize the case where they aren't
7349 executed.
36a05131 7350
c557edf4
RS
7351 - The scheduler will always put branches on their own, even
7352 if there's no real dependency.
36a05131 7353
c557edf4
RS
7354 - There's no point putting a call in its own packet unless
7355 we have to. */
7356 if (frv_packet.num_insns > 0
b64925dc 7357 && NONJUMP_INSN_P (insn)
c557edf4
RS
7358 && GET_MODE (insn) == TImode
7359 && GET_CODE (PATTERN (insn)) != COND_EXEC)
7360 return false;
36a05131 7361
c557edf4
RS
7362 /* Check for register conflicts. Don't do this for setlo since any
7363 conflict will be with the partnering sethi, with which it can
7364 be packed. */
7365 if (get_attr_type (insn) != TYPE_SETLO)
7366 if (frv_registers_conflict_p (PATTERN (insn)))
7367 return false;
36a05131 7368
c557edf4
RS
7369 return state_transition (frv_packet.dfa_state, insn) < 0;
7370}
36a05131 7371
36a05131 7372
c557edf4 7373/* Add instruction INSN to the current packet. */
36a05131 7374
c557edf4 7375static void
b32d5189 7376frv_add_insn_to_packet (rtx_insn *insn)
c557edf4
RS
7377{
7378 struct frv_packet_group *packet_group;
7379
7380 packet_group = &frv_packet.groups[frv_unit_groups[frv_insn_unit (insn)]];
7381 packet_group->insns[packet_group->num_insns++] = insn;
7382 frv_packet.insns[frv_packet.num_insns++] = insn;
7383
7384 frv_registers_update (PATTERN (insn));
36a05131
BS
7385}
7386
c557edf4
RS
7387
7388/* Insert INSN (a member of frv_nops[]) into the current packet. If the
7389 packet ends in a branch or call, insert the nop before it, otherwise
7390 add to the end. */
36a05131
BS
7391
7392static void
b32d5189 7393frv_insert_nop_in_packet (rtx_insn *insn)
36a05131 7394{
c557edf4 7395 struct frv_packet_group *packet_group;
b32d5189 7396 rtx_insn *last;
c557edf4
RS
7397
7398 packet_group = &frv_packet.groups[frv_unit_groups[frv_insn_unit (insn)]];
7399 last = frv_packet.insns[frv_packet.num_insns - 1];
b64925dc 7400 if (! NONJUMP_INSN_P (last))
c557edf4
RS
7401 {
7402 insn = emit_insn_before (PATTERN (insn), last);
7403 frv_packet.insns[frv_packet.num_insns - 1] = insn;
7404 frv_packet.insns[frv_packet.num_insns++] = last;
7405 }
7406 else
7407 {
7408 insn = emit_insn_after (PATTERN (insn), last);
7409 frv_packet.insns[frv_packet.num_insns++] = insn;
7410 }
7411 packet_group->insns[packet_group->num_insns++] = insn;
7412}
7413
36a05131 7414
c557edf4
RS
7415/* If packing is enabled, divide the instructions into packets and
7416 return true. Call HANDLE_PACKET for each complete packet. */
7417
7418static bool
7419frv_for_each_packet (void (*handle_packet) (void))
7420{
b32d5189 7421 rtx_insn *insn, *next_insn;
c557edf4
RS
7422
7423 frv_packet.issue_rate = frv_issue_rate ();
7424
7425 /* Early exit if we don't want to pack insns. */
28a60850
RS
7426 if (!optimize
7427 || !flag_schedule_insns_after_reload
0b2c18fe 7428 || !TARGET_VLIW_BRANCH
c557edf4
RS
7429 || frv_packet.issue_rate == 1)
7430 return false;
36a05131 7431
c557edf4 7432 /* Set up the initial packing state. */
36a05131 7433 dfa_start ();
c557edf4 7434 frv_packet.dfa_state = alloca (state_size ());
36a05131 7435
c557edf4
RS
7436 frv_start_packet_block ();
7437 for (insn = get_insns (); insn != 0; insn = next_insn)
36a05131 7438 {
c557edf4
RS
7439 enum rtx_code code;
7440 bool eh_insn_p;
36a05131 7441
c557edf4
RS
7442 code = GET_CODE (insn);
7443 next_insn = NEXT_INSN (insn);
7444
7445 if (code == CODE_LABEL)
36a05131 7446 {
c557edf4
RS
7447 frv_finish_packet (handle_packet);
7448 frv_start_packet_block ();
7449 }
36a05131 7450
39718607 7451 if (INSN_P (insn))
c557edf4
RS
7452 switch (GET_CODE (PATTERN (insn)))
7453 {
7454 case USE:
7455 case CLOBBER:
c557edf4 7456 break;
36a05131 7457
c557edf4
RS
7458 default:
7459 /* Calls mustn't be packed on a TOMCAT. */
b64925dc 7460 if (CALL_P (insn) && frv_cpu_type == FRV_CPU_TOMCAT)
c557edf4
RS
7461 frv_finish_packet (handle_packet);
7462
7463 /* Since the last instruction in a packet determines the EH
7464 region, any exception-throwing instruction must come at
7465 the end of reordered packet. Insns that issue to a
7466 branch unit are bound to come last; for others it's
7467 too hard to predict. */
7468 eh_insn_p = (find_reg_note (insn, REG_EH_REGION, NULL) != NULL);
7469 if (eh_insn_p && !frv_issues_to_branch_unit_p (insn))
7470 frv_finish_packet (handle_packet);
7471
7472 /* Finish the current packet if we can't add INSN to it.
7473 Simulate cycles until INSN is ready to issue. */
7474 if (!frv_pack_insn_p (insn))
7475 {
7476 frv_finish_packet (handle_packet);
7477 while (!frv_pack_insn_p (insn))
7478 state_transition (frv_packet.dfa_state, 0);
7479 }
36a05131 7480
c557edf4
RS
7481 /* Add the instruction to the packet. */
7482 frv_add_insn_to_packet (insn);
7483
7484 /* Calls and jumps end a packet, as do insns that throw
7485 an exception. */
7486 if (code == CALL_INSN || code == JUMP_INSN || eh_insn_p)
7487 frv_finish_packet (handle_packet);
7488 break;
7489 }
7490 }
7491 frv_finish_packet (handle_packet);
7492 dfa_finish ();
7493 return true;
7494}
7495\f
7496/* Subroutine of frv_sort_insn_group. We are trying to sort
7497 frv_packet.groups[GROUP].sorted[0...NUM_INSNS-1] into assembly
7498 language order. We have already picked a new position for
7499 frv_packet.groups[GROUP].sorted[X] if bit X of ISSUED is set.
7500 These instructions will occupy elements [0, LOWER_SLOT) and
7501 [UPPER_SLOT, NUM_INSNS) of the final (sorted) array. STATE is
7502 the DFA state after issuing these instructions.
7503
7504 Try filling elements [LOWER_SLOT, UPPER_SLOT) with every permutation
7505 of the unused instructions. Return true if one such permutation gives
7506 a valid ordering, leaving the successful permutation in sorted[].
7507 Do not modify sorted[] until a valid permutation is found. */
7508
7509static bool
7510frv_sort_insn_group_1 (enum frv_insn_group group,
7511 unsigned int lower_slot, unsigned int upper_slot,
7512 unsigned int issued, unsigned int num_insns,
7513 state_t state)
7514{
7515 struct frv_packet_group *packet_group;
7516 unsigned int i;
7517 state_t test_state;
7518 size_t dfa_size;
b32d5189 7519 rtx_insn *insn;
c557edf4
RS
7520
7521 /* Early success if we've filled all the slots. */
7522 if (lower_slot == upper_slot)
7523 return true;
7524
7525 packet_group = &frv_packet.groups[group];
7526 dfa_size = state_size ();
7527 test_state = alloca (dfa_size);
7528
7529 /* Try issuing each unused instruction. */
7530 for (i = num_insns - 1; i + 1 != 0; i--)
7531 if (~issued & (1 << i))
7532 {
7533 insn = packet_group->sorted[i];
7534 memcpy (test_state, state, dfa_size);
7535 if (state_transition (test_state, insn) < 0
7536 && cpu_unit_reservation_p (test_state,
7537 NTH_UNIT (group, upper_slot - 1))
7538 && frv_sort_insn_group_1 (group, lower_slot, upper_slot - 1,
7539 issued | (1 << i), num_insns,
7540 test_state))
7541 {
7542 packet_group->sorted[upper_slot - 1] = insn;
7543 return true;
7544 }
7545 }
7546
7547 return false;
7548}
7549
7550/* Compare two instructions by their frv_insn_unit. */
7551
7552static int
7553frv_compare_insns (const void *first, const void *second)
7554{
84034c69
DM
7555 rtx_insn * const *insn1 = (rtx_insn * const *) first;
7556 rtx_insn * const *insn2 = (rtx_insn * const *) second;
c557edf4
RS
7557 return frv_insn_unit (*insn1) - frv_insn_unit (*insn2);
7558}
7559
7560/* Copy frv_packet.groups[GROUP].insns[] to frv_packet.groups[GROUP].sorted[]
7561 and sort it into assembly language order. See frv.md for a description of
7562 the algorithm. */
7563
7564static void
7565frv_sort_insn_group (enum frv_insn_group group)
7566{
7567 struct frv_packet_group *packet_group;
7568 unsigned int first, i, nop, max_unit, num_slots;
7569 state_t state, test_state;
7570 size_t dfa_size;
7571
7572 packet_group = &frv_packet.groups[group];
75d0ac8d
RS
7573
7574 /* Assume no nop is needed. */
7575 packet_group->nop = 0;
7576
c557edf4
RS
7577 if (packet_group->num_insns == 0)
7578 return;
7579
7580 /* Copy insns[] to sorted[]. */
7581 memcpy (packet_group->sorted, packet_group->insns,
7582 sizeof (rtx) * packet_group->num_insns);
7583
7584 /* Sort sorted[] by the unit that each insn tries to take first. */
7585 if (packet_group->num_insns > 1)
7586 qsort (packet_group->sorted, packet_group->num_insns,
7587 sizeof (rtx), frv_compare_insns);
7588
7589 /* That's always enough for branch and control insns. */
7590 if (group == GROUP_B || group == GROUP_C)
7591 return;
7592
7593 dfa_size = state_size ();
7594 state = alloca (dfa_size);
7595 test_state = alloca (dfa_size);
7596
7597 /* Find the highest FIRST such that sorted[0...FIRST-1] can issue
7598 consecutively and such that the DFA takes unit X when sorted[X]
7599 is added. Set STATE to the new DFA state. */
7600 state_reset (test_state);
7601 for (first = 0; first < packet_group->num_insns; first++)
7602 {
7603 memcpy (state, test_state, dfa_size);
7604 if (state_transition (test_state, packet_group->sorted[first]) >= 0
7605 || !cpu_unit_reservation_p (test_state, NTH_UNIT (group, first)))
7606 break;
7607 }
7608
7609 /* If all the instructions issued in ascending order, we're done. */
7610 if (first == packet_group->num_insns)
7611 return;
36a05131 7612
c557edf4
RS
7613 /* Add nops to the end of sorted[] and try each permutation until
7614 we find one that works. */
7615 for (nop = 0; nop < frv_num_nops; nop++)
7616 {
7617 max_unit = frv_insn_unit (frv_nops[nop]);
7618 if (frv_unit_groups[max_unit] == group)
36a05131 7619 {
c557edf4
RS
7620 packet_group->nop = frv_nops[nop];
7621 num_slots = UNIT_NUMBER (max_unit) + 1;
7622 for (i = packet_group->num_insns; i < num_slots; i++)
7623 packet_group->sorted[i] = frv_nops[nop];
7624 if (frv_sort_insn_group_1 (group, first, num_slots,
7625 (1 << first) - 1, num_slots, state))
7626 return;
36a05131 7627 }
c557edf4 7628 }
44e91694 7629 gcc_unreachable ();
c557edf4
RS
7630}
7631\f
7632/* Sort the current packet into assembly-language order. Set packing
7633 flags as appropriate. */
36a05131 7634
c557edf4
RS
7635static void
7636frv_reorder_packet (void)
7637{
7638 unsigned int cursor[NUM_GROUPS];
7639 rtx insns[ARRAY_SIZE (frv_unit_groups)];
7640 unsigned int unit, to, from;
7641 enum frv_insn_group group;
7642 struct frv_packet_group *packet_group;
7643
7644 /* First sort each group individually. */
5c5e8419
JR
7645 for (group = GROUP_I; group < NUM_GROUPS;
7646 group = (enum frv_insn_group) (group + 1))
c557edf4
RS
7647 {
7648 cursor[group] = 0;
7649 frv_sort_insn_group (group);
7650 }
7651
7652 /* Go through the unit template and try add an instruction from
7653 that unit's group. */
7654 to = 0;
7655 for (unit = 0; unit < ARRAY_SIZE (frv_unit_groups); unit++)
7656 {
7657 group = frv_unit_groups[unit];
7658 packet_group = &frv_packet.groups[group];
7659 if (cursor[group] < packet_group->num_insns)
36a05131 7660 {
c557edf4 7661 /* frv_reorg should have added nops for us. */
44e91694
NS
7662 gcc_assert (packet_group->sorted[cursor[group]]
7663 != packet_group->nop);
c557edf4 7664 insns[to++] = packet_group->sorted[cursor[group]++];
36a05131 7665 }
c557edf4 7666 }
36a05131 7667
44e91694 7668 gcc_assert (to == frv_packet.num_insns);
36a05131 7669
c557edf4
RS
7670 /* Clear the last instruction's packing flag, thus marking the end of
7671 a packet. Reorder the other instructions relative to it. */
7672 CLEAR_PACKING_FLAG (insns[to - 1]);
7673 for (from = 0; from < to - 1; from++)
7674 {
7675 remove_insn (insns[from]);
6fb5fa3c 7676 add_insn_before (insns[from], insns[to - 1], NULL);
c557edf4
RS
7677 SET_PACKING_FLAG (insns[from]);
7678 }
7679}
36a05131 7680
36a05131 7681
c557edf4
RS
7682/* Divide instructions into packets. Reorder the contents of each
7683 packet so that they are in the correct assembly-language order.
7684
7685 Since this pass can change the raw meaning of the rtl stream, it must
7686 only be called at the last minute, just before the instructions are
7687 written out. */
7688
7689static void
7690frv_pack_insns (void)
7691{
7692 if (frv_for_each_packet (frv_reorder_packet))
7693 frv_insn_packing_flag = 0;
7694 else
7695 frv_insn_packing_flag = -1;
7696}
7697\f
7698/* See whether we need to add nops to group GROUP in order to
7699 make a valid packet. */
7700
7701static void
7702frv_fill_unused_units (enum frv_insn_group group)
7703{
7704 unsigned int non_nops, nops, i;
7705 struct frv_packet_group *packet_group;
7706
7707 packet_group = &frv_packet.groups[group];
7708
7709 /* Sort the instructions into assembly-language order.
7710 Use nops to fill slots that are otherwise unused. */
7711 frv_sort_insn_group (group);
7712
7713 /* See how many nops are needed before the final useful instruction. */
7714 i = nops = 0;
7715 for (non_nops = 0; non_nops < packet_group->num_insns; non_nops++)
7716 while (packet_group->sorted[i++] == packet_group->nop)
7717 nops++;
7718
7719 /* Insert that many nops into the instruction stream. */
7720 while (nops-- > 0)
7721 frv_insert_nop_in_packet (packet_group->nop);
7722}
7723
38c28a25
AH
7724/* Return true if accesses IO1 and IO2 refer to the same doubleword. */
7725
7726static bool
7727frv_same_doubleword_p (const struct frv_io *io1, const struct frv_io *io2)
7728{
7729 if (io1->const_address != 0 && io2->const_address != 0)
7730 return io1->const_address == io2->const_address;
7731
7732 if (io1->var_address != 0 && io2->var_address != 0)
7733 return rtx_equal_p (io1->var_address, io2->var_address);
7734
7735 return false;
7736}
7737
7738/* Return true if operations IO1 and IO2 are guaranteed to complete
7739 in order. */
7740
7741static bool
7742frv_io_fixed_order_p (const struct frv_io *io1, const struct frv_io *io2)
7743{
7744 /* The order of writes is always preserved. */
7745 if (io1->type == FRV_IO_WRITE && io2->type == FRV_IO_WRITE)
7746 return true;
7747
7748 /* The order of reads isn't preserved. */
7749 if (io1->type != FRV_IO_WRITE && io2->type != FRV_IO_WRITE)
7750 return false;
7751
7752 /* One operation is a write and the other is (or could be) a read.
7753 The order is only guaranteed if the accesses are to the same
7754 doubleword. */
7755 return frv_same_doubleword_p (io1, io2);
7756}
7757
7758/* Generalize I/O operation X so that it covers both X and Y. */
7759
7760static void
7761frv_io_union (struct frv_io *x, const struct frv_io *y)
7762{
7763 if (x->type != y->type)
7764 x->type = FRV_IO_UNKNOWN;
7765 if (!frv_same_doubleword_p (x, y))
7766 {
7767 x->const_address = 0;
7768 x->var_address = 0;
7769 }
7770}
7771
7772/* Fill IO with information about the load or store associated with
7773 membar instruction INSN. */
7774
7775static void
647d790d 7776frv_extract_membar (struct frv_io *io, rtx_insn *insn)
38c28a25
AH
7777{
7778 extract_insn (insn);
5c5e8419 7779 io->type = (enum frv_io_type) INTVAL (recog_data.operand[2]);
38c28a25
AH
7780 io->const_address = INTVAL (recog_data.operand[1]);
7781 io->var_address = XEXP (recog_data.operand[0], 0);
7782}
7783
7784/* A note_stores callback for which DATA points to an rtx. Nullify *DATA
7785 if X is a register and *DATA depends on X. */
7786
7787static void
7bc980e1 7788frv_io_check_address (rtx x, const_rtx pat ATTRIBUTE_UNUSED, void *data)
38c28a25 7789{
5ead67f6 7790 rtx *other = (rtx *) data;
38c28a25
AH
7791
7792 if (REG_P (x) && *other != 0 && reg_overlap_mentioned_p (x, *other))
7793 *other = 0;
7794}
7795
7796/* A note_stores callback for which DATA points to a HARD_REG_SET.
7797 Remove every modified register from the set. */
7798
7799static void
7bc980e1 7800frv_io_handle_set (rtx x, const_rtx pat ATTRIBUTE_UNUSED, void *data)
38c28a25 7801{
5ead67f6 7802 HARD_REG_SET *set = (HARD_REG_SET *) data;
38c28a25
AH
7803 unsigned int regno;
7804
7805 if (REG_P (x))
7806 FOR_EACH_REGNO (regno, x)
7807 CLEAR_HARD_REG_BIT (*set, regno);
7808}
7809
7810/* A for_each_rtx callback for which DATA points to a HARD_REG_SET.
7811 Add every register in *X to the set. */
7812
7813static int
7814frv_io_handle_use_1 (rtx *x, void *data)
7815{
5ead67f6 7816 HARD_REG_SET *set = (HARD_REG_SET *) data;
38c28a25
AH
7817 unsigned int regno;
7818
7819 if (REG_P (*x))
7820 FOR_EACH_REGNO (regno, *x)
7821 SET_HARD_REG_BIT (*set, regno);
7822
7823 return 0;
7824}
7825
7826/* A note_stores callback that applies frv_io_handle_use_1 to an
7827 entire rhs value. */
7828
7829static void
7830frv_io_handle_use (rtx *x, void *data)
7831{
7832 for_each_rtx (x, frv_io_handle_use_1, data);
7833}
7834
7835/* Go through block BB looking for membars to remove. There are two
7836 cases where intra-block analysis is enough:
7837
7838 - a membar is redundant if it occurs between two consecutive I/O
7839 operations and if those operations are guaranteed to complete
7840 in order.
7841
7842 - a membar for a __builtin_read is redundant if the result is
7843 used before the next I/O operation is issued.
7844
7845 If the last membar in the block could not be removed, and there
7846 are guaranteed to be no I/O operations between that membar and
7847 the end of the block, store the membar in *LAST_MEMBAR, otherwise
7848 store null.
7849
7850 Describe the block's first I/O operation in *NEXT_IO. Describe
7851 an unknown operation if the block doesn't do any I/O. */
7852
7853static void
7854frv_optimize_membar_local (basic_block bb, struct frv_io *next_io,
647d790d 7855 rtx_insn **last_membar)
38c28a25
AH
7856{
7857 HARD_REG_SET used_regs;
b32d5189
DM
7858 rtx next_membar, set;
7859 rtx_insn *insn;
38c28a25
AH
7860 bool next_is_end_p;
7861
7862 /* NEXT_IO is the next I/O operation to be performed after the current
7863 instruction. It starts off as being an unknown operation. */
7864 memset (next_io, 0, sizeof (*next_io));
7865
7866 /* NEXT_IS_END_P is true if NEXT_IO describes the end of the block. */
7867 next_is_end_p = true;
7868
7869 /* If the current instruction is a __builtin_read or __builtin_write,
7870 NEXT_MEMBAR is the membar instruction associated with it. NEXT_MEMBAR
7871 is null if the membar has already been deleted.
7872
7873 Note that the initialization here should only be needed to
536fa7b7 7874 suppress warnings. */
38c28a25
AH
7875 next_membar = 0;
7876
7877 /* USED_REGS is the set of registers that are used before the
7878 next I/O instruction. */
7879 CLEAR_HARD_REG_SET (used_regs);
7880
7881 for (insn = BB_END (bb); insn != BB_HEAD (bb); insn = PREV_INSN (insn))
b64925dc 7882 if (CALL_P (insn))
38c28a25
AH
7883 {
7884 /* We can't predict what a call will do to volatile memory. */
7885 memset (next_io, 0, sizeof (struct frv_io));
7886 next_is_end_p = false;
7887 CLEAR_HARD_REG_SET (used_regs);
7888 }
7889 else if (INSN_P (insn))
7890 switch (recog_memoized (insn))
7891 {
7892 case CODE_FOR_optional_membar_qi:
7893 case CODE_FOR_optional_membar_hi:
7894 case CODE_FOR_optional_membar_si:
7895 case CODE_FOR_optional_membar_di:
7896 next_membar = insn;
7897 if (next_is_end_p)
7898 {
7899 /* Local information isn't enough to decide whether this
7900 membar is needed. Stash it away for later. */
7901 *last_membar = insn;
7902 frv_extract_membar (next_io, insn);
7903 next_is_end_p = false;
7904 }
7905 else
7906 {
7907 /* Check whether the I/O operation before INSN could be
7908 reordered with one described by NEXT_IO. If it can't,
7909 INSN will not be needed. */
7910 struct frv_io prev_io;
7911
7912 frv_extract_membar (&prev_io, insn);
7913 if (frv_io_fixed_order_p (&prev_io, next_io))
7914 {
7915 if (dump_file)
7916 fprintf (dump_file,
7917 ";; [Local] Removing membar %d since order"
7918 " of accesses is guaranteed\n",
7919 INSN_UID (next_membar));
7920
7921 insn = NEXT_INSN (insn);
7922 delete_insn (next_membar);
7923 next_membar = 0;
7924 }
7925 *next_io = prev_io;
7926 }
7927 break;
7928
7929 default:
7930 /* Invalidate NEXT_IO's address if it depends on something that
7931 is clobbered by INSN. */
7932 if (next_io->var_address)
7933 note_stores (PATTERN (insn), frv_io_check_address,
7934 &next_io->var_address);
7935
7936 /* If the next membar is associated with a __builtin_read,
7937 see if INSN reads from that address. If it does, and if
7938 the destination register is used before the next I/O access,
7939 there is no need for the membar. */
7940 set = PATTERN (insn);
7941 if (next_io->type == FRV_IO_READ
7942 && next_io->var_address != 0
7943 && next_membar != 0
7944 && GET_CODE (set) == SET
7945 && GET_CODE (SET_DEST (set)) == REG
7946 && TEST_HARD_REG_BIT (used_regs, REGNO (SET_DEST (set))))
7947 {
7948 rtx src;
7949
7950 src = SET_SRC (set);
7951 if (GET_CODE (src) == ZERO_EXTEND)
7952 src = XEXP (src, 0);
7953
7954 if (GET_CODE (src) == MEM
7955 && rtx_equal_p (XEXP (src, 0), next_io->var_address))
7956 {
7957 if (dump_file)
7958 fprintf (dump_file,
7959 ";; [Local] Removing membar %d since the target"
7960 " of %d is used before the I/O operation\n",
7961 INSN_UID (next_membar), INSN_UID (insn));
7962
7963 if (next_membar == *last_membar)
7964 *last_membar = 0;
7965
7966 delete_insn (next_membar);
7967 next_membar = 0;
7968 }
7969 }
7970
7971 /* If INSN has volatile references, forget about any registers
7972 that are used after it. Otherwise forget about uses that
7973 are (or might be) defined by INSN. */
7974 if (volatile_refs_p (PATTERN (insn)))
7975 CLEAR_HARD_REG_SET (used_regs);
7976 else
7977 note_stores (PATTERN (insn), frv_io_handle_set, &used_regs);
7978
7979 note_uses (&PATTERN (insn), frv_io_handle_use, &used_regs);
7980 break;
7981 }
7982}
7983
7984/* See if MEMBAR, the last membar instruction in BB, can be removed.
7985 FIRST_IO[X] describes the first operation performed by basic block X. */
7986
7987static void
7988frv_optimize_membar_global (basic_block bb, struct frv_io *first_io,
647d790d 7989 rtx_insn *membar)
38c28a25
AH
7990{
7991 struct frv_io this_io, next_io;
7992 edge succ;
7993 edge_iterator ei;
7994
7995 /* We need to keep the membar if there is an edge to the exit block. */
7996 FOR_EACH_EDGE (succ, ei, bb->succs)
7997 /* for (succ = bb->succ; succ != 0; succ = succ->succ_next) */
fefa31b5 7998 if (succ->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
38c28a25
AH
7999 return;
8000
8001 /* Work out the union of all successor blocks. */
8002 ei = ei_start (bb->succs);
8003 ei_cond (ei, &succ);
8004 /* next_io = first_io[bb->succ->dest->index]; */
8005 next_io = first_io[succ->dest->index];
8006 ei = ei_start (bb->succs);
8007 if (ei_cond (ei, &succ))
8008 {
8009 for (ei_next (&ei); ei_cond (ei, &succ); ei_next (&ei))
8010 /*for (succ = bb->succ->succ_next; succ != 0; succ = succ->succ_next)*/
8011 frv_io_union (&next_io, &first_io[succ->dest->index]);
8012 }
8013 else
8014 gcc_unreachable ();
8015
8016 frv_extract_membar (&this_io, membar);
8017 if (frv_io_fixed_order_p (&this_io, &next_io))
8018 {
8019 if (dump_file)
8020 fprintf (dump_file,
8021 ";; [Global] Removing membar %d since order of accesses"
8022 " is guaranteed\n", INSN_UID (membar));
8023
8024 delete_insn (membar);
8025 }
8026}
8027
8028/* Remove redundant membars from the current function. */
8029
8030static void
8031frv_optimize_membar (void)
8032{
8033 basic_block bb;
8034 struct frv_io *first_io;
647d790d 8035 rtx_insn **last_membar;
38c28a25
AH
8036
8037 compute_bb_for_insn ();
8b1c6fd7 8038 first_io = XCNEWVEC (struct frv_io, last_basic_block_for_fn (cfun));
647d790d 8039 last_membar = XCNEWVEC (rtx_insn *, last_basic_block_for_fn (cfun));
38c28a25 8040
11cd3bed 8041 FOR_EACH_BB_FN (bb, cfun)
38c28a25
AH
8042 frv_optimize_membar_local (bb, &first_io[bb->index],
8043 &last_membar[bb->index]);
8044
11cd3bed 8045 FOR_EACH_BB_FN (bb, cfun)
38c28a25
AH
8046 if (last_membar[bb->index] != 0)
8047 frv_optimize_membar_global (bb, first_io, last_membar[bb->index]);
8048
8049 free (first_io);
8050 free (last_membar);
8051}
8052\f
c557edf4
RS
8053/* Used by frv_reorg to keep track of the current packet's address. */
8054static unsigned int frv_packet_address;
36a05131 8055
c557edf4
RS
8056/* If the current packet falls through to a label, try to pad the packet
8057 with nops in order to fit the label's alignment requirements. */
8058
8059static void
8060frv_align_label (void)
8061{
8062 unsigned int alignment, target, nop;
b32d5189 8063 rtx_insn *x, *last, *barrier, *label;
c557edf4
RS
8064
8065 /* Walk forward to the start of the next packet. Set ALIGNMENT to the
8066 maximum alignment of that packet, LABEL to the last label between
8067 the packets, and BARRIER to the last barrier. */
8068 last = frv_packet.insns[frv_packet.num_insns - 1];
8069 label = barrier = 0;
8070 alignment = 4;
8071 for (x = NEXT_INSN (last); x != 0 && !INSN_P (x); x = NEXT_INSN (x))
8072 {
8073 if (LABEL_P (x))
36a05131 8074 {
c557edf4
RS
8075 unsigned int subalign = 1 << label_to_alignment (x);
8076 alignment = MAX (alignment, subalign);
8077 label = x;
36a05131 8078 }
c557edf4
RS
8079 if (BARRIER_P (x))
8080 barrier = x;
8081 }
36a05131 8082
c557edf4
RS
8083 /* If -malign-labels, and the packet falls through to an unaligned
8084 label, try introducing a nop to align that label to 8 bytes. */
8085 if (TARGET_ALIGN_LABELS
8086 && label != 0
8087 && barrier == 0
8088 && frv_packet.num_insns < frv_packet.issue_rate)
8089 alignment = MAX (alignment, 8);
36a05131 8090
c557edf4
RS
8091 /* Advance the address to the end of the current packet. */
8092 frv_packet_address += frv_packet.num_insns * 4;
36a05131 8093
c557edf4
RS
8094 /* Work out the target address, after alignment. */
8095 target = (frv_packet_address + alignment - 1) & -alignment;
8096
8097 /* If the packet falls through to the label, try to find an efficient
8098 padding sequence. */
8099 if (barrier == 0)
8100 {
8101 /* First try adding nops to the current packet. */
8102 for (nop = 0; nop < frv_num_nops; nop++)
8103 while (frv_packet_address < target && frv_pack_insn_p (frv_nops[nop]))
8104 {
8105 frv_insert_nop_in_packet (frv_nops[nop]);
8106 frv_packet_address += 4;
8107 }
8108
8109 /* If we still haven't reached the target, add some new packets that
8110 contain only nops. If there are two types of nop, insert an
8111 alternating sequence of frv_nops[0] and frv_nops[1], which will
8112 lead to packets like:
8113
8114 nop.p
8115 mnop.p/fnop.p
8116 nop.p
8117 mnop/fnop
8118
8119 etc. Just emit frv_nops[0] if that's the only nop we have. */
8120 last = frv_packet.insns[frv_packet.num_insns - 1];
8121 nop = 0;
8122 while (frv_packet_address < target)
8123 {
8124 last = emit_insn_after (PATTERN (frv_nops[nop]), last);
8125 frv_packet_address += 4;
8126 if (frv_num_nops > 1)
8127 nop ^= 1;
36a05131
BS
8128 }
8129 }
8130
c557edf4 8131 frv_packet_address = target;
36a05131
BS
8132}
8133
c557edf4
RS
8134/* Subroutine of frv_reorg, called after each packet has been constructed
8135 in frv_packet. */
8136
8137static void
8138frv_reorg_packet (void)
8139{
8140 frv_fill_unused_units (GROUP_I);
8141 frv_fill_unused_units (GROUP_FM);
8142 frv_align_label ();
8143}
8144
8145/* Add an instruction with pattern NOP to frv_nops[]. */
8146
8147static void
8148frv_register_nop (rtx nop)
8149{
e6eda746
DM
8150 rtx_insn *nop_insn = make_insn_raw (nop);
8151 SET_NEXT_INSN (nop_insn) = 0;
8152 SET_PREV_INSN (nop_insn) = 0;
8153 frv_nops[frv_num_nops++] = nop_insn;
c557edf4
RS
8154}
8155
8156/* Implement TARGET_MACHINE_DEPENDENT_REORG. Divide the instructions
8157 into packets and check whether we need to insert nops in order to
8158 fulfill the processor's issue requirements. Also, if the user has
8159 requested a certain alignment for a label, try to meet that alignment
8160 by inserting nops in the previous packet. */
8161
8162static void
8163frv_reorg (void)
8164{
38c28a25
AH
8165 if (optimize > 0 && TARGET_OPTIMIZE_MEMBAR && cfun->machine->has_membar_p)
8166 frv_optimize_membar ();
8167
c557edf4
RS
8168 frv_num_nops = 0;
8169 frv_register_nop (gen_nop ());
8170 if (TARGET_MEDIA)
8171 frv_register_nop (gen_mnop ());
8172 if (TARGET_HARD_FLOAT)
8173 frv_register_nop (gen_fnop ());
8174
8175 /* Estimate the length of each branch. Although this may change after
8176 we've inserted nops, it will only do so in big functions. */
8177 shorten_branches (get_insns ());
8178
8179 frv_packet_address = 0;
8180 frv_for_each_packet (frv_reorg_packet);
8181}
36a05131
BS
8182\f
8183#define def_builtin(name, type, code) \
c79efc4d 8184 add_builtin_function ((name), (type), (code), BUILT_IN_MD, NULL, NULL)
36a05131
BS
8185
8186struct builtin_description
8187{
8188 enum insn_code icode;
8189 const char *name;
8190 enum frv_builtins code;
8191 enum rtx_code comparison;
8192 unsigned int flag;
8193};
8194
8195/* Media intrinsics that take a single, constant argument. */
8196
8197static struct builtin_description bdesc_set[] =
8198{
5c5e8419 8199 { CODE_FOR_mhdsets, "__MHDSETS", FRV_BUILTIN_MHDSETS, UNKNOWN, 0 }
36a05131
BS
8200};
8201
87b483a1 8202/* Media intrinsics that take just one argument. */
36a05131
BS
8203
8204static struct builtin_description bdesc_1arg[] =
8205{
5c5e8419
JR
8206 { CODE_FOR_mnot, "__MNOT", FRV_BUILTIN_MNOT, UNKNOWN, 0 },
8207 { CODE_FOR_munpackh, "__MUNPACKH", FRV_BUILTIN_MUNPACKH, UNKNOWN, 0 },
8208 { CODE_FOR_mbtoh, "__MBTOH", FRV_BUILTIN_MBTOH, UNKNOWN, 0 },
8209 { CODE_FOR_mhtob, "__MHTOB", FRV_BUILTIN_MHTOB, UNKNOWN, 0},
8210 { CODE_FOR_mabshs, "__MABSHS", FRV_BUILTIN_MABSHS, UNKNOWN, 0 },
8211 { CODE_FOR_scutss, "__SCUTSS", FRV_BUILTIN_SCUTSS, UNKNOWN, 0 }
36a05131
BS
8212};
8213
87b483a1 8214/* Media intrinsics that take two arguments. */
36a05131
BS
8215
8216static struct builtin_description bdesc_2arg[] =
8217{
5c5e8419
JR
8218 { CODE_FOR_mand, "__MAND", FRV_BUILTIN_MAND, UNKNOWN, 0},
8219 { CODE_FOR_mor, "__MOR", FRV_BUILTIN_MOR, UNKNOWN, 0},
8220 { CODE_FOR_mxor, "__MXOR", FRV_BUILTIN_MXOR, UNKNOWN, 0},
8221 { CODE_FOR_maveh, "__MAVEH", FRV_BUILTIN_MAVEH, UNKNOWN, 0},
8222 { CODE_FOR_msaths, "__MSATHS", FRV_BUILTIN_MSATHS, UNKNOWN, 0},
8223 { CODE_FOR_msathu, "__MSATHU", FRV_BUILTIN_MSATHU, UNKNOWN, 0},
8224 { CODE_FOR_maddhss, "__MADDHSS", FRV_BUILTIN_MADDHSS, UNKNOWN, 0},
8225 { CODE_FOR_maddhus, "__MADDHUS", FRV_BUILTIN_MADDHUS, UNKNOWN, 0},
8226 { CODE_FOR_msubhss, "__MSUBHSS", FRV_BUILTIN_MSUBHSS, UNKNOWN, 0},
8227 { CODE_FOR_msubhus, "__MSUBHUS", FRV_BUILTIN_MSUBHUS, UNKNOWN, 0},
8228 { CODE_FOR_mqaddhss, "__MQADDHSS", FRV_BUILTIN_MQADDHSS, UNKNOWN, 0},
8229 { CODE_FOR_mqaddhus, "__MQADDHUS", FRV_BUILTIN_MQADDHUS, UNKNOWN, 0},
8230 { CODE_FOR_mqsubhss, "__MQSUBHSS", FRV_BUILTIN_MQSUBHSS, UNKNOWN, 0},
8231 { CODE_FOR_mqsubhus, "__MQSUBHUS", FRV_BUILTIN_MQSUBHUS, UNKNOWN, 0},
8232 { CODE_FOR_mpackh, "__MPACKH", FRV_BUILTIN_MPACKH, UNKNOWN, 0},
8233 { CODE_FOR_mcop1, "__Mcop1", FRV_BUILTIN_MCOP1, UNKNOWN, 0},
8234 { CODE_FOR_mcop2, "__Mcop2", FRV_BUILTIN_MCOP2, UNKNOWN, 0},
8235 { CODE_FOR_mwcut, "__MWCUT", FRV_BUILTIN_MWCUT, UNKNOWN, 0},
8236 { CODE_FOR_mqsaths, "__MQSATHS", FRV_BUILTIN_MQSATHS, UNKNOWN, 0},
8237 { CODE_FOR_mqlclrhs, "__MQLCLRHS", FRV_BUILTIN_MQLCLRHS, UNKNOWN, 0},
8238 { CODE_FOR_mqlmths, "__MQLMTHS", FRV_BUILTIN_MQLMTHS, UNKNOWN, 0},
8239 { CODE_FOR_smul, "__SMUL", FRV_BUILTIN_SMUL, UNKNOWN, 0},
8240 { CODE_FOR_umul, "__UMUL", FRV_BUILTIN_UMUL, UNKNOWN, 0},
8241 { CODE_FOR_addss, "__ADDSS", FRV_BUILTIN_ADDSS, UNKNOWN, 0},
8242 { CODE_FOR_subss, "__SUBSS", FRV_BUILTIN_SUBSS, UNKNOWN, 0},
8243 { CODE_FOR_slass, "__SLASS", FRV_BUILTIN_SLASS, UNKNOWN, 0},
8244 { CODE_FOR_scan, "__SCAN", FRV_BUILTIN_SCAN, UNKNOWN, 0}
c557edf4
RS
8245};
8246
8247/* Integer intrinsics that take two arguments and have no return value. */
8248
8249static struct builtin_description bdesc_int_void2arg[] =
8250{
5c5e8419
JR
8251 { CODE_FOR_smass, "__SMASS", FRV_BUILTIN_SMASS, UNKNOWN, 0},
8252 { CODE_FOR_smsss, "__SMSSS", FRV_BUILTIN_SMSSS, UNKNOWN, 0},
8253 { CODE_FOR_smu, "__SMU", FRV_BUILTIN_SMU, UNKNOWN, 0}
c557edf4
RS
8254};
8255
8256static struct builtin_description bdesc_prefetches[] =
8257{
5c5e8419
JR
8258 { CODE_FOR_frv_prefetch0, "__data_prefetch0", FRV_BUILTIN_PREFETCH0, UNKNOWN,
8259 0},
8260 { CODE_FOR_frv_prefetch, "__data_prefetch", FRV_BUILTIN_PREFETCH, UNKNOWN, 0}
36a05131
BS
8261};
8262
8263/* Media intrinsics that take two arguments, the first being an ACC number. */
8264
8265static struct builtin_description bdesc_cut[] =
8266{
5c5e8419
JR
8267 { CODE_FOR_mcut, "__MCUT", FRV_BUILTIN_MCUT, UNKNOWN, 0},
8268 { CODE_FOR_mcutss, "__MCUTSS", FRV_BUILTIN_MCUTSS, UNKNOWN, 0},
8269 { CODE_FOR_mdcutssi, "__MDCUTSSI", FRV_BUILTIN_MDCUTSSI, UNKNOWN, 0}
36a05131
BS
8270};
8271
87b483a1 8272/* Two-argument media intrinsics with an immediate second argument. */
36a05131
BS
8273
8274static struct builtin_description bdesc_2argimm[] =
8275{
5c5e8419
JR
8276 { CODE_FOR_mrotli, "__MROTLI", FRV_BUILTIN_MROTLI, UNKNOWN, 0},
8277 { CODE_FOR_mrotri, "__MROTRI", FRV_BUILTIN_MROTRI, UNKNOWN, 0},
8278 { CODE_FOR_msllhi, "__MSLLHI", FRV_BUILTIN_MSLLHI, UNKNOWN, 0},
8279 { CODE_FOR_msrlhi, "__MSRLHI", FRV_BUILTIN_MSRLHI, UNKNOWN, 0},
8280 { CODE_FOR_msrahi, "__MSRAHI", FRV_BUILTIN_MSRAHI, UNKNOWN, 0},
8281 { CODE_FOR_mexpdhw, "__MEXPDHW", FRV_BUILTIN_MEXPDHW, UNKNOWN, 0},
8282 { CODE_FOR_mexpdhd, "__MEXPDHD", FRV_BUILTIN_MEXPDHD, UNKNOWN, 0},
8283 { CODE_FOR_mdrotli, "__MDROTLI", FRV_BUILTIN_MDROTLI, UNKNOWN, 0},
8284 { CODE_FOR_mcplhi, "__MCPLHI", FRV_BUILTIN_MCPLHI, UNKNOWN, 0},
8285 { CODE_FOR_mcpli, "__MCPLI", FRV_BUILTIN_MCPLI, UNKNOWN, 0},
8286 { CODE_FOR_mhsetlos, "__MHSETLOS", FRV_BUILTIN_MHSETLOS, UNKNOWN, 0},
8287 { CODE_FOR_mhsetloh, "__MHSETLOH", FRV_BUILTIN_MHSETLOH, UNKNOWN, 0},
8288 { CODE_FOR_mhsethis, "__MHSETHIS", FRV_BUILTIN_MHSETHIS, UNKNOWN, 0},
8289 { CODE_FOR_mhsethih, "__MHSETHIH", FRV_BUILTIN_MHSETHIH, UNKNOWN, 0},
8290 { CODE_FOR_mhdseth, "__MHDSETH", FRV_BUILTIN_MHDSETH, UNKNOWN, 0},
8291 { CODE_FOR_mqsllhi, "__MQSLLHI", FRV_BUILTIN_MQSLLHI, UNKNOWN, 0},
8292 { CODE_FOR_mqsrahi, "__MQSRAHI", FRV_BUILTIN_MQSRAHI, UNKNOWN, 0}
36a05131
BS
8293};
8294
8295/* Media intrinsics that take two arguments and return void, the first argument
87b483a1 8296 being a pointer to 4 words in memory. */
36a05131
BS
8297
8298static struct builtin_description bdesc_void2arg[] =
8299{
5c5e8419
JR
8300 { CODE_FOR_mdunpackh, "__MDUNPACKH", FRV_BUILTIN_MDUNPACKH, UNKNOWN, 0},
8301 { CODE_FOR_mbtohe, "__MBTOHE", FRV_BUILTIN_MBTOHE, UNKNOWN, 0},
36a05131
BS
8302};
8303
8304/* Media intrinsics that take three arguments, the first being a const_int that
87b483a1 8305 denotes an accumulator, and that return void. */
36a05131
BS
8306
8307static struct builtin_description bdesc_void3arg[] =
8308{
5c5e8419
JR
8309 { CODE_FOR_mcpxrs, "__MCPXRS", FRV_BUILTIN_MCPXRS, UNKNOWN, 0},
8310 { CODE_FOR_mcpxru, "__MCPXRU", FRV_BUILTIN_MCPXRU, UNKNOWN, 0},
8311 { CODE_FOR_mcpxis, "__MCPXIS", FRV_BUILTIN_MCPXIS, UNKNOWN, 0},
8312 { CODE_FOR_mcpxiu, "__MCPXIU", FRV_BUILTIN_MCPXIU, UNKNOWN, 0},
8313 { CODE_FOR_mmulhs, "__MMULHS", FRV_BUILTIN_MMULHS, UNKNOWN, 0},
8314 { CODE_FOR_mmulhu, "__MMULHU", FRV_BUILTIN_MMULHU, UNKNOWN, 0},
8315 { CODE_FOR_mmulxhs, "__MMULXHS", FRV_BUILTIN_MMULXHS, UNKNOWN, 0},
8316 { CODE_FOR_mmulxhu, "__MMULXHU", FRV_BUILTIN_MMULXHU, UNKNOWN, 0},
8317 { CODE_FOR_mmachs, "__MMACHS", FRV_BUILTIN_MMACHS, UNKNOWN, 0},
8318 { CODE_FOR_mmachu, "__MMACHU", FRV_BUILTIN_MMACHU, UNKNOWN, 0},
8319 { CODE_FOR_mmrdhs, "__MMRDHS", FRV_BUILTIN_MMRDHS, UNKNOWN, 0},
8320 { CODE_FOR_mmrdhu, "__MMRDHU", FRV_BUILTIN_MMRDHU, UNKNOWN, 0},
8321 { CODE_FOR_mqcpxrs, "__MQCPXRS", FRV_BUILTIN_MQCPXRS, UNKNOWN, 0},
8322 { CODE_FOR_mqcpxru, "__MQCPXRU", FRV_BUILTIN_MQCPXRU, UNKNOWN, 0},
8323 { CODE_FOR_mqcpxis, "__MQCPXIS", FRV_BUILTIN_MQCPXIS, UNKNOWN, 0},
8324 { CODE_FOR_mqcpxiu, "__MQCPXIU", FRV_BUILTIN_MQCPXIU, UNKNOWN, 0},
8325 { CODE_FOR_mqmulhs, "__MQMULHS", FRV_BUILTIN_MQMULHS, UNKNOWN, 0},
8326 { CODE_FOR_mqmulhu, "__MQMULHU", FRV_BUILTIN_MQMULHU, UNKNOWN, 0},
8327 { CODE_FOR_mqmulxhs, "__MQMULXHS", FRV_BUILTIN_MQMULXHS, UNKNOWN, 0},
8328 { CODE_FOR_mqmulxhu, "__MQMULXHU", FRV_BUILTIN_MQMULXHU, UNKNOWN, 0},
8329 { CODE_FOR_mqmachs, "__MQMACHS", FRV_BUILTIN_MQMACHS, UNKNOWN, 0},
8330 { CODE_FOR_mqmachu, "__MQMACHU", FRV_BUILTIN_MQMACHU, UNKNOWN, 0},
8331 { CODE_FOR_mqxmachs, "__MQXMACHS", FRV_BUILTIN_MQXMACHS, UNKNOWN, 0},
8332 { CODE_FOR_mqxmacxhs, "__MQXMACXHS", FRV_BUILTIN_MQXMACXHS, UNKNOWN, 0},
8333 { CODE_FOR_mqmacxhs, "__MQMACXHS", FRV_BUILTIN_MQMACXHS, UNKNOWN, 0}
36a05131
BS
8334};
8335
8336/* Media intrinsics that take two accumulator numbers as argument and
8337 return void. */
8338
8339static struct builtin_description bdesc_voidacc[] =
8340{
5c5e8419
JR
8341 { CODE_FOR_maddaccs, "__MADDACCS", FRV_BUILTIN_MADDACCS, UNKNOWN, 0},
8342 { CODE_FOR_msubaccs, "__MSUBACCS", FRV_BUILTIN_MSUBACCS, UNKNOWN, 0},
8343 { CODE_FOR_masaccs, "__MASACCS", FRV_BUILTIN_MASACCS, UNKNOWN, 0},
8344 { CODE_FOR_mdaddaccs, "__MDADDACCS", FRV_BUILTIN_MDADDACCS, UNKNOWN, 0},
8345 { CODE_FOR_mdsubaccs, "__MDSUBACCS", FRV_BUILTIN_MDSUBACCS, UNKNOWN, 0},
8346 { CODE_FOR_mdasaccs, "__MDASACCS", FRV_BUILTIN_MDASACCS, UNKNOWN, 0}
36a05131
BS
8347};
8348
38c28a25
AH
8349/* Intrinsics that load a value and then issue a MEMBAR. The load is
8350 a normal move and the ICODE is for the membar. */
c14ff86e
AH
8351
8352static struct builtin_description bdesc_loads[] =
8353{
38c28a25 8354 { CODE_FOR_optional_membar_qi, "__builtin_read8",
5c5e8419 8355 FRV_BUILTIN_READ8, UNKNOWN, 0},
38c28a25 8356 { CODE_FOR_optional_membar_hi, "__builtin_read16",
5c5e8419 8357 FRV_BUILTIN_READ16, UNKNOWN, 0},
38c28a25 8358 { CODE_FOR_optional_membar_si, "__builtin_read32",
5c5e8419 8359 FRV_BUILTIN_READ32, UNKNOWN, 0},
38c28a25 8360 { CODE_FOR_optional_membar_di, "__builtin_read64",
5c5e8419 8361 FRV_BUILTIN_READ64, UNKNOWN, 0}
c14ff86e
AH
8362};
8363
8364/* Likewise stores. */
8365
8366static struct builtin_description bdesc_stores[] =
8367{
38c28a25 8368 { CODE_FOR_optional_membar_qi, "__builtin_write8",
5c5e8419 8369 FRV_BUILTIN_WRITE8, UNKNOWN, 0},
38c28a25 8370 { CODE_FOR_optional_membar_hi, "__builtin_write16",
5c5e8419 8371 FRV_BUILTIN_WRITE16, UNKNOWN, 0},
38c28a25 8372 { CODE_FOR_optional_membar_si, "__builtin_write32",
5c5e8419 8373 FRV_BUILTIN_WRITE32, UNKNOWN, 0},
38c28a25 8374 { CODE_FOR_optional_membar_di, "__builtin_write64",
5c5e8419 8375 FRV_BUILTIN_WRITE64, UNKNOWN, 0},
c14ff86e
AH
8376};
8377
87b483a1 8378/* Initialize media builtins. */
36a05131 8379
14966b94 8380static void
f2206911 8381frv_init_builtins (void)
36a05131 8382{
36a05131
BS
8383 tree accumulator = integer_type_node;
8384 tree integer = integer_type_node;
8385 tree voidt = void_type_node;
8386 tree uhalf = short_unsigned_type_node;
8387 tree sword1 = long_integer_type_node;
8388 tree uword1 = long_unsigned_type_node;
8389 tree sword2 = long_long_integer_type_node;
8390 tree uword2 = long_long_unsigned_type_node;
8391 tree uword4 = build_pointer_type (uword1);
c14ff86e
AH
8392 tree vptr = build_pointer_type (build_type_variant (void_type_node, 0, 1));
8393 tree ubyte = unsigned_char_type_node;
c557edf4 8394 tree iacc = integer_type_node;
36a05131
BS
8395
8396#define UNARY(RET, T1) \
e84a6fcf 8397 build_function_type_list (RET, T1, NULL_TREE)
36a05131
BS
8398
8399#define BINARY(RET, T1, T2) \
e84a6fcf 8400 build_function_type_list (RET, T1, T2, NULL_TREE)
36a05131
BS
8401
8402#define TRINARY(RET, T1, T2, T3) \
e84a6fcf 8403 build_function_type_list (RET, T1, T2, T3, NULL_TREE)
36a05131 8404
a738d848 8405#define QUAD(RET, T1, T2, T3, T4) \
4adf72f1 8406 build_function_type_list (RET, T1, T2, T3, T4, NULL_TREE)
a738d848 8407
e84a6fcf 8408 tree void_ftype_void = build_function_type_list (voidt, NULL_TREE);
36a05131
BS
8409
8410 tree void_ftype_acc = UNARY (voidt, accumulator);
8411 tree void_ftype_uw4_uw1 = BINARY (voidt, uword4, uword1);
8412 tree void_ftype_uw4_uw2 = BINARY (voidt, uword4, uword2);
8413 tree void_ftype_acc_uw1 = BINARY (voidt, accumulator, uword1);
8414 tree void_ftype_acc_acc = BINARY (voidt, accumulator, accumulator);
8415 tree void_ftype_acc_uw1_uw1 = TRINARY (voidt, accumulator, uword1, uword1);
8416 tree void_ftype_acc_sw1_sw1 = TRINARY (voidt, accumulator, sword1, sword1);
8417 tree void_ftype_acc_uw2_uw2 = TRINARY (voidt, accumulator, uword2, uword2);
8418 tree void_ftype_acc_sw2_sw2 = TRINARY (voidt, accumulator, sword2, sword2);
8419
8420 tree uw1_ftype_uw1 = UNARY (uword1, uword1);
8421 tree uw1_ftype_sw1 = UNARY (uword1, sword1);
8422 tree uw1_ftype_uw2 = UNARY (uword1, uword2);
8423 tree uw1_ftype_acc = UNARY (uword1, accumulator);
8424 tree uw1_ftype_uh_uh = BINARY (uword1, uhalf, uhalf);
8425 tree uw1_ftype_uw1_uw1 = BINARY (uword1, uword1, uword1);
8426 tree uw1_ftype_uw1_int = BINARY (uword1, uword1, integer);
8427 tree uw1_ftype_acc_uw1 = BINARY (uword1, accumulator, uword1);
8428 tree uw1_ftype_acc_sw1 = BINARY (uword1, accumulator, sword1);
8429 tree uw1_ftype_uw2_uw1 = BINARY (uword1, uword2, uword1);
8430 tree uw1_ftype_uw2_int = BINARY (uword1, uword2, integer);
8431
8432 tree sw1_ftype_int = UNARY (sword1, integer);
8433 tree sw1_ftype_sw1_sw1 = BINARY (sword1, sword1, sword1);
8434 tree sw1_ftype_sw1_int = BINARY (sword1, sword1, integer);
8435
8436 tree uw2_ftype_uw1 = UNARY (uword2, uword1);
8437 tree uw2_ftype_uw1_int = BINARY (uword2, uword1, integer);
8438 tree uw2_ftype_uw2_uw2 = BINARY (uword2, uword2, uword2);
8439 tree uw2_ftype_uw2_int = BINARY (uword2, uword2, integer);
8440 tree uw2_ftype_acc_int = BINARY (uword2, accumulator, integer);
a738d848 8441 tree uw2_ftype_uh_uh_uh_uh = QUAD (uword2, uhalf, uhalf, uhalf, uhalf);
36a05131
BS
8442
8443 tree sw2_ftype_sw2_sw2 = BINARY (sword2, sword2, sword2);
c557edf4
RS
8444 tree sw2_ftype_sw2_int = BINARY (sword2, sword2, integer);
8445 tree uw2_ftype_uw1_uw1 = BINARY (uword2, uword1, uword1);
8446 tree sw2_ftype_sw1_sw1 = BINARY (sword2, sword1, sword1);
8447 tree void_ftype_sw1_sw1 = BINARY (voidt, sword1, sword1);
8448 tree void_ftype_iacc_sw2 = BINARY (voidt, iacc, sword2);
8449 tree void_ftype_iacc_sw1 = BINARY (voidt, iacc, sword1);
8450 tree sw1_ftype_sw1 = UNARY (sword1, sword1);
8451 tree sw2_ftype_iacc = UNARY (sword2, iacc);
8452 tree sw1_ftype_iacc = UNARY (sword1, iacc);
8453 tree void_ftype_ptr = UNARY (voidt, const_ptr_type_node);
c14ff86e
AH
8454 tree uw1_ftype_vptr = UNARY (uword1, vptr);
8455 tree uw2_ftype_vptr = UNARY (uword2, vptr);
8456 tree void_ftype_vptr_ub = BINARY (voidt, vptr, ubyte);
8457 tree void_ftype_vptr_uh = BINARY (voidt, vptr, uhalf);
8458 tree void_ftype_vptr_uw1 = BINARY (voidt, vptr, uword1);
8459 tree void_ftype_vptr_uw2 = BINARY (voidt, vptr, uword2);
36a05131
BS
8460
8461 def_builtin ("__MAND", uw1_ftype_uw1_uw1, FRV_BUILTIN_MAND);
8462 def_builtin ("__MOR", uw1_ftype_uw1_uw1, FRV_BUILTIN_MOR);
8463 def_builtin ("__MXOR", uw1_ftype_uw1_uw1, FRV_BUILTIN_MXOR);
8464 def_builtin ("__MNOT", uw1_ftype_uw1, FRV_BUILTIN_MNOT);
8465 def_builtin ("__MROTLI", uw1_ftype_uw1_int, FRV_BUILTIN_MROTLI);
8466 def_builtin ("__MROTRI", uw1_ftype_uw1_int, FRV_BUILTIN_MROTRI);
8467 def_builtin ("__MWCUT", uw1_ftype_uw2_uw1, FRV_BUILTIN_MWCUT);
8468 def_builtin ("__MAVEH", uw1_ftype_uw1_uw1, FRV_BUILTIN_MAVEH);
8469 def_builtin ("__MSLLHI", uw1_ftype_uw1_int, FRV_BUILTIN_MSLLHI);
8470 def_builtin ("__MSRLHI", uw1_ftype_uw1_int, FRV_BUILTIN_MSRLHI);
8471 def_builtin ("__MSRAHI", sw1_ftype_sw1_int, FRV_BUILTIN_MSRAHI);
8472 def_builtin ("__MSATHS", sw1_ftype_sw1_sw1, FRV_BUILTIN_MSATHS);
8473 def_builtin ("__MSATHU", uw1_ftype_uw1_uw1, FRV_BUILTIN_MSATHU);
8474 def_builtin ("__MADDHSS", sw1_ftype_sw1_sw1, FRV_BUILTIN_MADDHSS);
8475 def_builtin ("__MADDHUS", uw1_ftype_uw1_uw1, FRV_BUILTIN_MADDHUS);
8476 def_builtin ("__MSUBHSS", sw1_ftype_sw1_sw1, FRV_BUILTIN_MSUBHSS);
8477 def_builtin ("__MSUBHUS", uw1_ftype_uw1_uw1, FRV_BUILTIN_MSUBHUS);
8478 def_builtin ("__MMULHS", void_ftype_acc_sw1_sw1, FRV_BUILTIN_MMULHS);
8479 def_builtin ("__MMULHU", void_ftype_acc_uw1_uw1, FRV_BUILTIN_MMULHU);
8480 def_builtin ("__MMULXHS", void_ftype_acc_sw1_sw1, FRV_BUILTIN_MMULXHS);
8481 def_builtin ("__MMULXHU", void_ftype_acc_uw1_uw1, FRV_BUILTIN_MMULXHU);
8482 def_builtin ("__MMACHS", void_ftype_acc_sw1_sw1, FRV_BUILTIN_MMACHS);
8483 def_builtin ("__MMACHU", void_ftype_acc_uw1_uw1, FRV_BUILTIN_MMACHU);
8484 def_builtin ("__MMRDHS", void_ftype_acc_sw1_sw1, FRV_BUILTIN_MMRDHS);
8485 def_builtin ("__MMRDHU", void_ftype_acc_uw1_uw1, FRV_BUILTIN_MMRDHU);
8486 def_builtin ("__MQADDHSS", sw2_ftype_sw2_sw2, FRV_BUILTIN_MQADDHSS);
8487 def_builtin ("__MQADDHUS", uw2_ftype_uw2_uw2, FRV_BUILTIN_MQADDHUS);
8488 def_builtin ("__MQSUBHSS", sw2_ftype_sw2_sw2, FRV_BUILTIN_MQSUBHSS);
8489 def_builtin ("__MQSUBHUS", uw2_ftype_uw2_uw2, FRV_BUILTIN_MQSUBHUS);
8490 def_builtin ("__MQMULHS", void_ftype_acc_sw2_sw2, FRV_BUILTIN_MQMULHS);
8491 def_builtin ("__MQMULHU", void_ftype_acc_uw2_uw2, FRV_BUILTIN_MQMULHU);
8492 def_builtin ("__MQMULXHS", void_ftype_acc_sw2_sw2, FRV_BUILTIN_MQMULXHS);
8493 def_builtin ("__MQMULXHU", void_ftype_acc_uw2_uw2, FRV_BUILTIN_MQMULXHU);
8494 def_builtin ("__MQMACHS", void_ftype_acc_sw2_sw2, FRV_BUILTIN_MQMACHS);
8495 def_builtin ("__MQMACHU", void_ftype_acc_uw2_uw2, FRV_BUILTIN_MQMACHU);
8496 def_builtin ("__MCPXRS", void_ftype_acc_sw1_sw1, FRV_BUILTIN_MCPXRS);
8497 def_builtin ("__MCPXRU", void_ftype_acc_uw1_uw1, FRV_BUILTIN_MCPXRU);
8498 def_builtin ("__MCPXIS", void_ftype_acc_sw1_sw1, FRV_BUILTIN_MCPXIS);
8499 def_builtin ("__MCPXIU", void_ftype_acc_uw1_uw1, FRV_BUILTIN_MCPXIU);
8500 def_builtin ("__MQCPXRS", void_ftype_acc_sw2_sw2, FRV_BUILTIN_MQCPXRS);
8501 def_builtin ("__MQCPXRU", void_ftype_acc_uw2_uw2, FRV_BUILTIN_MQCPXRU);
8502 def_builtin ("__MQCPXIS", void_ftype_acc_sw2_sw2, FRV_BUILTIN_MQCPXIS);
8503 def_builtin ("__MQCPXIU", void_ftype_acc_uw2_uw2, FRV_BUILTIN_MQCPXIU);
8504 def_builtin ("__MCUT", uw1_ftype_acc_uw1, FRV_BUILTIN_MCUT);
8505 def_builtin ("__MCUTSS", uw1_ftype_acc_sw1, FRV_BUILTIN_MCUTSS);
8506 def_builtin ("__MEXPDHW", uw1_ftype_uw1_int, FRV_BUILTIN_MEXPDHW);
8507 def_builtin ("__MEXPDHD", uw2_ftype_uw1_int, FRV_BUILTIN_MEXPDHD);
8508 def_builtin ("__MPACKH", uw1_ftype_uh_uh, FRV_BUILTIN_MPACKH);
8509 def_builtin ("__MUNPACKH", uw2_ftype_uw1, FRV_BUILTIN_MUNPACKH);
a738d848 8510 def_builtin ("__MDPACKH", uw2_ftype_uh_uh_uh_uh, FRV_BUILTIN_MDPACKH);
b16c1435 8511 def_builtin ("__MDUNPACKH", void_ftype_uw4_uw2, FRV_BUILTIN_MDUNPACKH);
36a05131
BS
8512 def_builtin ("__MBTOH", uw2_ftype_uw1, FRV_BUILTIN_MBTOH);
8513 def_builtin ("__MHTOB", uw1_ftype_uw2, FRV_BUILTIN_MHTOB);
8514 def_builtin ("__MBTOHE", void_ftype_uw4_uw1, FRV_BUILTIN_MBTOHE);
8515 def_builtin ("__MCLRACC", void_ftype_acc, FRV_BUILTIN_MCLRACC);
8516 def_builtin ("__MCLRACCA", void_ftype_void, FRV_BUILTIN_MCLRACCA);
8517 def_builtin ("__MRDACC", uw1_ftype_acc, FRV_BUILTIN_MRDACC);
8518 def_builtin ("__MRDACCG", uw1_ftype_acc, FRV_BUILTIN_MRDACCG);
8519 def_builtin ("__MWTACC", void_ftype_acc_uw1, FRV_BUILTIN_MWTACC);
8520 def_builtin ("__MWTACCG", void_ftype_acc_uw1, FRV_BUILTIN_MWTACCG);
8521 def_builtin ("__Mcop1", uw1_ftype_uw1_uw1, FRV_BUILTIN_MCOP1);
8522 def_builtin ("__Mcop2", uw1_ftype_uw1_uw1, FRV_BUILTIN_MCOP2);
8523 def_builtin ("__MTRAP", void_ftype_void, FRV_BUILTIN_MTRAP);
8524 def_builtin ("__MQXMACHS", void_ftype_acc_sw2_sw2, FRV_BUILTIN_MQXMACHS);
8525 def_builtin ("__MQXMACXHS", void_ftype_acc_sw2_sw2, FRV_BUILTIN_MQXMACXHS);
8526 def_builtin ("__MQMACXHS", void_ftype_acc_sw2_sw2, FRV_BUILTIN_MQMACXHS);
8527 def_builtin ("__MADDACCS", void_ftype_acc_acc, FRV_BUILTIN_MADDACCS);
8528 def_builtin ("__MSUBACCS", void_ftype_acc_acc, FRV_BUILTIN_MSUBACCS);
8529 def_builtin ("__MASACCS", void_ftype_acc_acc, FRV_BUILTIN_MASACCS);
8530 def_builtin ("__MDADDACCS", void_ftype_acc_acc, FRV_BUILTIN_MDADDACCS);
8531 def_builtin ("__MDSUBACCS", void_ftype_acc_acc, FRV_BUILTIN_MDSUBACCS);
8532 def_builtin ("__MDASACCS", void_ftype_acc_acc, FRV_BUILTIN_MDASACCS);
8533 def_builtin ("__MABSHS", uw1_ftype_sw1, FRV_BUILTIN_MABSHS);
8534 def_builtin ("__MDROTLI", uw2_ftype_uw2_int, FRV_BUILTIN_MDROTLI);
8535 def_builtin ("__MCPLHI", uw1_ftype_uw2_int, FRV_BUILTIN_MCPLHI);
8536 def_builtin ("__MCPLI", uw1_ftype_uw2_int, FRV_BUILTIN_MCPLI);
8537 def_builtin ("__MDCUTSSI", uw2_ftype_acc_int, FRV_BUILTIN_MDCUTSSI);
8538 def_builtin ("__MQSATHS", sw2_ftype_sw2_sw2, FRV_BUILTIN_MQSATHS);
8539 def_builtin ("__MHSETLOS", sw1_ftype_sw1_int, FRV_BUILTIN_MHSETLOS);
8540 def_builtin ("__MHSETHIS", sw1_ftype_sw1_int, FRV_BUILTIN_MHSETHIS);
8541 def_builtin ("__MHDSETS", sw1_ftype_int, FRV_BUILTIN_MHDSETS);
8542 def_builtin ("__MHSETLOH", uw1_ftype_uw1_int, FRV_BUILTIN_MHSETLOH);
8543 def_builtin ("__MHSETHIH", uw1_ftype_uw1_int, FRV_BUILTIN_MHSETHIH);
8544 def_builtin ("__MHDSETH", uw1_ftype_uw1_int, FRV_BUILTIN_MHDSETH);
c557edf4
RS
8545 def_builtin ("__MQLCLRHS", sw2_ftype_sw2_sw2, FRV_BUILTIN_MQLCLRHS);
8546 def_builtin ("__MQLMTHS", sw2_ftype_sw2_sw2, FRV_BUILTIN_MQLMTHS);
8547 def_builtin ("__MQSLLHI", uw2_ftype_uw2_int, FRV_BUILTIN_MQSLLHI);
8548 def_builtin ("__MQSRAHI", sw2_ftype_sw2_int, FRV_BUILTIN_MQSRAHI);
8549 def_builtin ("__SMUL", sw2_ftype_sw1_sw1, FRV_BUILTIN_SMUL);
8550 def_builtin ("__UMUL", uw2_ftype_uw1_uw1, FRV_BUILTIN_UMUL);
8551 def_builtin ("__SMASS", void_ftype_sw1_sw1, FRV_BUILTIN_SMASS);
8552 def_builtin ("__SMSSS", void_ftype_sw1_sw1, FRV_BUILTIN_SMSSS);
8553 def_builtin ("__SMU", void_ftype_sw1_sw1, FRV_BUILTIN_SMU);
8554 def_builtin ("__ADDSS", sw1_ftype_sw1_sw1, FRV_BUILTIN_ADDSS);
8555 def_builtin ("__SUBSS", sw1_ftype_sw1_sw1, FRV_BUILTIN_SUBSS);
8556 def_builtin ("__SLASS", sw1_ftype_sw1_sw1, FRV_BUILTIN_SLASS);
8557 def_builtin ("__SCAN", sw1_ftype_sw1_sw1, FRV_BUILTIN_SCAN);
8558 def_builtin ("__SCUTSS", sw1_ftype_sw1, FRV_BUILTIN_SCUTSS);
8559 def_builtin ("__IACCreadll", sw2_ftype_iacc, FRV_BUILTIN_IACCreadll);
8560 def_builtin ("__IACCreadl", sw1_ftype_iacc, FRV_BUILTIN_IACCreadl);
8561 def_builtin ("__IACCsetll", void_ftype_iacc_sw2, FRV_BUILTIN_IACCsetll);
8562 def_builtin ("__IACCsetl", void_ftype_iacc_sw1, FRV_BUILTIN_IACCsetl);
8563 def_builtin ("__data_prefetch0", void_ftype_ptr, FRV_BUILTIN_PREFETCH0);
8564 def_builtin ("__data_prefetch", void_ftype_ptr, FRV_BUILTIN_PREFETCH);
c14ff86e
AH
8565 def_builtin ("__builtin_read8", uw1_ftype_vptr, FRV_BUILTIN_READ8);
8566 def_builtin ("__builtin_read16", uw1_ftype_vptr, FRV_BUILTIN_READ16);
8567 def_builtin ("__builtin_read32", uw1_ftype_vptr, FRV_BUILTIN_READ32);
8568 def_builtin ("__builtin_read64", uw2_ftype_vptr, FRV_BUILTIN_READ64);
8569
8570 def_builtin ("__builtin_write8", void_ftype_vptr_ub, FRV_BUILTIN_WRITE8);
8571 def_builtin ("__builtin_write16", void_ftype_vptr_uh, FRV_BUILTIN_WRITE16);
8572 def_builtin ("__builtin_write32", void_ftype_vptr_uw1, FRV_BUILTIN_WRITE32);
8573 def_builtin ("__builtin_write64", void_ftype_vptr_uw2, FRV_BUILTIN_WRITE64);
36a05131
BS
8574
8575#undef UNARY
8576#undef BINARY
8577#undef TRINARY
a738d848 8578#undef QUAD
36a05131
BS
8579}
8580
c15c90bb
ZW
8581/* Set the names for various arithmetic operations according to the
8582 FRV ABI. */
8583static void
8584frv_init_libfuncs (void)
8585{
8586 set_optab_libfunc (smod_optab, SImode, "__modi");
8587 set_optab_libfunc (umod_optab, SImode, "__umodi");
8588
8589 set_optab_libfunc (add_optab, DImode, "__addll");
8590 set_optab_libfunc (sub_optab, DImode, "__subll");
8591 set_optab_libfunc (smul_optab, DImode, "__mulll");
8592 set_optab_libfunc (sdiv_optab, DImode, "__divll");
8593 set_optab_libfunc (smod_optab, DImode, "__modll");
8594 set_optab_libfunc (umod_optab, DImode, "__umodll");
8595 set_optab_libfunc (and_optab, DImode, "__andll");
8596 set_optab_libfunc (ior_optab, DImode, "__orll");
8597 set_optab_libfunc (xor_optab, DImode, "__xorll");
8598 set_optab_libfunc (one_cmpl_optab, DImode, "__notll");
8599
8600 set_optab_libfunc (add_optab, SFmode, "__addf");
8601 set_optab_libfunc (sub_optab, SFmode, "__subf");
8602 set_optab_libfunc (smul_optab, SFmode, "__mulf");
8603 set_optab_libfunc (sdiv_optab, SFmode, "__divf");
8604
8605 set_optab_libfunc (add_optab, DFmode, "__addd");
8606 set_optab_libfunc (sub_optab, DFmode, "__subd");
8607 set_optab_libfunc (smul_optab, DFmode, "__muld");
8608 set_optab_libfunc (sdiv_optab, DFmode, "__divd");
8609
85363ca0
ZW
8610 set_conv_libfunc (sext_optab, DFmode, SFmode, "__ftod");
8611 set_conv_libfunc (trunc_optab, SFmode, DFmode, "__dtof");
8612
8613 set_conv_libfunc (sfix_optab, SImode, SFmode, "__ftoi");
8614 set_conv_libfunc (sfix_optab, DImode, SFmode, "__ftoll");
8615 set_conv_libfunc (sfix_optab, SImode, DFmode, "__dtoi");
8616 set_conv_libfunc (sfix_optab, DImode, DFmode, "__dtoll");
8617
8618 set_conv_libfunc (ufix_optab, SImode, SFmode, "__ftoui");
09c55720
RS
8619 set_conv_libfunc (ufix_optab, DImode, SFmode, "__ftoull");
8620 set_conv_libfunc (ufix_optab, SImode, DFmode, "__dtoui");
8621 set_conv_libfunc (ufix_optab, DImode, DFmode, "__dtoull");
85363ca0
ZW
8622
8623 set_conv_libfunc (sfloat_optab, SFmode, SImode, "__itof");
8624 set_conv_libfunc (sfloat_optab, SFmode, DImode, "__lltof");
8625 set_conv_libfunc (sfloat_optab, DFmode, SImode, "__itod");
8626 set_conv_libfunc (sfloat_optab, DFmode, DImode, "__lltod");
c15c90bb
ZW
8627}
8628
36a05131
BS
8629/* Convert an integer constant to an accumulator register. ICODE is the
8630 code of the target instruction, OPNUM is the number of the
8631 accumulator operand and OPVAL is the constant integer. Try both
8632 ACC and ACCG registers; only report an error if neither fit the
8633 instruction. */
8634
8635static rtx
f2206911 8636frv_int_to_acc (enum insn_code icode, int opnum, rtx opval)
36a05131
BS
8637{
8638 rtx reg;
c557edf4
RS
8639 int i;
8640
0fa2e4df 8641 /* ACCs and ACCGs are implicit global registers if media intrinsics
c557edf4 8642 are being used. We set up this lazily to avoid creating lots of
c112cf2b 8643 unnecessary call_insn rtl in non-media code. */
c557edf4
RS
8644 for (i = 0; i <= ACC_MASK; i++)
8645 if ((i & ACC_MASK) == i)
8646 global_regs[i + ACC_FIRST] = global_regs[i + ACCG_FIRST] = 1;
36a05131
BS
8647
8648 if (GET_CODE (opval) != CONST_INT)
8649 {
8650 error ("accumulator is not a constant integer");
8651 return NULL_RTX;
8652 }
c557edf4 8653 if ((INTVAL (opval) & ~ACC_MASK) != 0)
36a05131
BS
8654 {
8655 error ("accumulator number is out of bounds");
8656 return NULL_RTX;
8657 }
8658
8659 reg = gen_rtx_REG (insn_data[icode].operand[opnum].mode,
8660 ACC_FIRST + INTVAL (opval));
8661 if (! (*insn_data[icode].operand[opnum].predicate) (reg, VOIDmode))
6fb5fa3c 8662 SET_REGNO (reg, ACCG_FIRST + INTVAL (opval));
36a05131
BS
8663
8664 if (! (*insn_data[icode].operand[opnum].predicate) (reg, VOIDmode))
8665 {
9e637a26 8666 error ("inappropriate accumulator for %qs", insn_data[icode].name);
36a05131
BS
8667 return NULL_RTX;
8668 }
8669 return reg;
8670}
8671
8672/* If an ACC rtx has mode MODE, return the mode that the matching ACCG
8673 should have. */
8674
ef4bddc2
RS
8675static machine_mode
8676frv_matching_accg_mode (machine_mode mode)
36a05131
BS
8677{
8678 switch (mode)
8679 {
8680 case V4SImode:
8681 return V4QImode;
8682
8683 case DImode:
8684 return HImode;
8685
8686 case SImode:
8687 return QImode;
8688
8689 default:
44e91694 8690 gcc_unreachable ();
36a05131
BS
8691 }
8692}
8693
38c28a25
AH
8694/* Given that a __builtin_read or __builtin_write function is accessing
8695 address ADDRESS, return the value that should be used as operand 1
8696 of the membar. */
8697
8698static rtx
8699frv_io_address_cookie (rtx address)
8700{
8701 return (GET_CODE (address) == CONST_INT
8702 ? GEN_INT (INTVAL (address) / 8 * 8)
8703 : const0_rtx);
8704}
8705
36a05131
BS
8706/* Return the accumulator guard that should be paired with accumulator
8707 register ACC. The mode of the returned register is in the same
8708 class as ACC, but is four times smaller. */
8709
8710rtx
f2206911 8711frv_matching_accg_for_acc (rtx acc)
36a05131
BS
8712{
8713 return gen_rtx_REG (frv_matching_accg_mode (GET_MODE (acc)),
8714 REGNO (acc) - ACC_FIRST + ACCG_FIRST);
8715}
8716
2396bce1
EC
8717/* Read the requested argument from the call EXP given by INDEX.
8718 Return the value as an rtx. */
36a05131
BS
8719
8720static rtx
2396bce1 8721frv_read_argument (tree exp, unsigned int index)
36a05131 8722{
5c5e8419 8723 return expand_normal (CALL_EXPR_ARG (exp, index));
36a05131
BS
8724}
8725
c557edf4
RS
8726/* Like frv_read_argument, but interpret the argument as the number
8727 of an IACC register and return a (reg:MODE ...) rtx for it. */
8728
8729static rtx
ef4bddc2 8730frv_read_iacc_argument (machine_mode mode, tree call,
2396bce1 8731 unsigned int index)
c557edf4
RS
8732{
8733 int i, regno;
8734 rtx op;
8735
2396bce1 8736 op = frv_read_argument (call, index);
c557edf4
RS
8737 if (GET_CODE (op) != CONST_INT
8738 || INTVAL (op) < 0
8739 || INTVAL (op) > IACC_LAST - IACC_FIRST
8740 || ((INTVAL (op) * 4) & (GET_MODE_SIZE (mode) - 1)) != 0)
8741 {
8742 error ("invalid IACC argument");
8743 op = const0_rtx;
8744 }
8745
0fa2e4df 8746 /* IACCs are implicit global registers. We set up this lazily to
c112cf2b 8747 avoid creating lots of unnecessary call_insn rtl when IACCs aren't
c557edf4
RS
8748 being used. */
8749 regno = INTVAL (op) + IACC_FIRST;
8750 for (i = 0; i < HARD_REGNO_NREGS (regno, mode); i++)
8751 global_regs[regno + i] = 1;
8752
8753 return gen_rtx_REG (mode, regno);
8754}
8755
36a05131
BS
8756/* Return true if OPVAL can be used for operand OPNUM of instruction ICODE.
8757 The instruction should require a constant operand of some sort. The
8758 function prints an error if OPVAL is not valid. */
8759
8760static int
f2206911 8761frv_check_constant_argument (enum insn_code icode, int opnum, rtx opval)
36a05131
BS
8762{
8763 if (GET_CODE (opval) != CONST_INT)
8764 {
9e637a26 8765 error ("%qs expects a constant argument", insn_data[icode].name);
36a05131
BS
8766 return FALSE;
8767 }
8768 if (! (*insn_data[icode].operand[opnum].predicate) (opval, VOIDmode))
8769 {
9e637a26 8770 error ("constant argument out of range for %qs", insn_data[icode].name);
36a05131
BS
8771 return FALSE;
8772 }
8773 return TRUE;
8774}
8775
8776/* Return a legitimate rtx for instruction ICODE's return value. Use TARGET
8777 if it's not null, has the right mode, and satisfies operand 0's
8778 predicate. */
8779
8780static rtx
f2206911 8781frv_legitimize_target (enum insn_code icode, rtx target)
36a05131 8782{
ef4bddc2 8783 machine_mode mode = insn_data[icode].operand[0].mode;
36a05131
BS
8784
8785 if (! target
8786 || GET_MODE (target) != mode
8787 || ! (*insn_data[icode].operand[0].predicate) (target, mode))
8788 return gen_reg_rtx (mode);
8789 else
8790 return target;
8791}
8792
8793/* Given that ARG is being passed as operand OPNUM to instruction ICODE,
839a4992 8794 check whether ARG satisfies the operand's constraints. If it doesn't,
36a05131
BS
8795 copy ARG to a temporary register and return that. Otherwise return ARG
8796 itself. */
8797
8798static rtx
f2206911 8799frv_legitimize_argument (enum insn_code icode, int opnum, rtx arg)
36a05131 8800{
ef4bddc2 8801 machine_mode mode = insn_data[icode].operand[opnum].mode;
36a05131
BS
8802
8803 if ((*insn_data[icode].operand[opnum].predicate) (arg, mode))
8804 return arg;
8805 else
8806 return copy_to_mode_reg (mode, arg);
8807}
8808
c14ff86e
AH
8809/* Return a volatile memory reference of mode MODE whose address is ARG. */
8810
8811static rtx
ef4bddc2 8812frv_volatile_memref (machine_mode mode, rtx arg)
c14ff86e
AH
8813{
8814 rtx mem;
8815
8816 mem = gen_rtx_MEM (mode, memory_address (mode, arg));
8817 MEM_VOLATILE_P (mem) = 1;
8818 return mem;
8819}
8820
36a05131
BS
8821/* Expand builtins that take a single, constant argument. At the moment,
8822 only MHDSETS falls into this category. */
8823
8824static rtx
2396bce1 8825frv_expand_set_builtin (enum insn_code icode, tree call, rtx target)
36a05131
BS
8826{
8827 rtx pat;
2396bce1 8828 rtx op0 = frv_read_argument (call, 0);
36a05131
BS
8829
8830 if (! frv_check_constant_argument (icode, 1, op0))
8831 return NULL_RTX;
8832
8833 target = frv_legitimize_target (icode, target);
8834 pat = GEN_FCN (icode) (target, op0);
8835 if (! pat)
8836 return NULL_RTX;
8837
8838 emit_insn (pat);
8839 return target;
8840}
8841
87b483a1 8842/* Expand builtins that take one operand. */
36a05131
BS
8843
8844static rtx
2396bce1 8845frv_expand_unop_builtin (enum insn_code icode, tree call, rtx target)
36a05131
BS
8846{
8847 rtx pat;
2396bce1 8848 rtx op0 = frv_read_argument (call, 0);
36a05131
BS
8849
8850 target = frv_legitimize_target (icode, target);
8851 op0 = frv_legitimize_argument (icode, 1, op0);
8852 pat = GEN_FCN (icode) (target, op0);
8853 if (! pat)
8854 return NULL_RTX;
8855
8856 emit_insn (pat);
8857 return target;
8858}
8859
87b483a1 8860/* Expand builtins that take two operands. */
36a05131
BS
8861
8862static rtx
2396bce1 8863frv_expand_binop_builtin (enum insn_code icode, tree call, rtx target)
36a05131
BS
8864{
8865 rtx pat;
2396bce1
EC
8866 rtx op0 = frv_read_argument (call, 0);
8867 rtx op1 = frv_read_argument (call, 1);
36a05131
BS
8868
8869 target = frv_legitimize_target (icode, target);
8870 op0 = frv_legitimize_argument (icode, 1, op0);
8871 op1 = frv_legitimize_argument (icode, 2, op1);
8872 pat = GEN_FCN (icode) (target, op0, op1);
8873 if (! pat)
8874 return NULL_RTX;
8875
8876 emit_insn (pat);
8877 return target;
8878}
8879
8880/* Expand cut-style builtins, which take two operands and an implicit ACCG
87b483a1 8881 one. */
36a05131
BS
8882
8883static rtx
2396bce1 8884frv_expand_cut_builtin (enum insn_code icode, tree call, rtx target)
36a05131
BS
8885{
8886 rtx pat;
2396bce1
EC
8887 rtx op0 = frv_read_argument (call, 0);
8888 rtx op1 = frv_read_argument (call, 1);
36a05131
BS
8889 rtx op2;
8890
8891 target = frv_legitimize_target (icode, target);
8892 op0 = frv_int_to_acc (icode, 1, op0);
8893 if (! op0)
8894 return NULL_RTX;
8895
8896 if (icode == CODE_FOR_mdcutssi || GET_CODE (op1) == CONST_INT)
8897 {
8898 if (! frv_check_constant_argument (icode, 2, op1))
8899 return NULL_RTX;
8900 }
8901 else
8902 op1 = frv_legitimize_argument (icode, 2, op1);
8903
8904 op2 = frv_matching_accg_for_acc (op0);
8905 pat = GEN_FCN (icode) (target, op0, op1, op2);
8906 if (! pat)
8907 return NULL_RTX;
8908
8909 emit_insn (pat);
8910 return target;
8911}
8912
87b483a1 8913/* Expand builtins that take two operands and the second is immediate. */
36a05131
BS
8914
8915static rtx
2396bce1 8916frv_expand_binopimm_builtin (enum insn_code icode, tree call, rtx target)
36a05131
BS
8917{
8918 rtx pat;
2396bce1
EC
8919 rtx op0 = frv_read_argument (call, 0);
8920 rtx op1 = frv_read_argument (call, 1);
36a05131
BS
8921
8922 if (! frv_check_constant_argument (icode, 2, op1))
8923 return NULL_RTX;
8924
8925 target = frv_legitimize_target (icode, target);
8926 op0 = frv_legitimize_argument (icode, 1, op0);
8927 pat = GEN_FCN (icode) (target, op0, op1);
8928 if (! pat)
8929 return NULL_RTX;
8930
8931 emit_insn (pat);
8932 return target;
8933}
8934
8935/* Expand builtins that take two operands, the first operand being a pointer to
87b483a1 8936 ints and return void. */
36a05131
BS
8937
8938static rtx
2396bce1 8939frv_expand_voidbinop_builtin (enum insn_code icode, tree call)
36a05131
BS
8940{
8941 rtx pat;
2396bce1
EC
8942 rtx op0 = frv_read_argument (call, 0);
8943 rtx op1 = frv_read_argument (call, 1);
ef4bddc2 8944 machine_mode mode0 = insn_data[icode].operand[0].mode;
36a05131
BS
8945 rtx addr;
8946
8947 if (GET_CODE (op0) != MEM)
8948 {
8949 rtx reg = op0;
8950
8951 if (! offsettable_address_p (0, mode0, op0))
8952 {
8953 reg = gen_reg_rtx (Pmode);
8954 emit_insn (gen_rtx_SET (VOIDmode, reg, op0));
8955 }
8956
8957 op0 = gen_rtx_MEM (SImode, reg);
8958 }
8959
8960 addr = XEXP (op0, 0);
8961 if (! offsettable_address_p (0, mode0, addr))
8962 addr = copy_to_mode_reg (Pmode, op0);
8963
8964 op0 = change_address (op0, V4SImode, addr);
8965 op1 = frv_legitimize_argument (icode, 1, op1);
8966 pat = GEN_FCN (icode) (op0, op1);
8967 if (! pat)
8968 return 0;
8969
8970 emit_insn (pat);
8971 return 0;
8972}
8973
c557edf4
RS
8974/* Expand builtins that take two long operands and return void. */
8975
8976static rtx
2396bce1 8977frv_expand_int_void2arg (enum insn_code icode, tree call)
c557edf4
RS
8978{
8979 rtx pat;
2396bce1
EC
8980 rtx op0 = frv_read_argument (call, 0);
8981 rtx op1 = frv_read_argument (call, 1);
c557edf4
RS
8982
8983 op0 = frv_legitimize_argument (icode, 1, op0);
8984 op1 = frv_legitimize_argument (icode, 1, op1);
8985 pat = GEN_FCN (icode) (op0, op1);
8986 if (! pat)
8987 return NULL_RTX;
8988
8989 emit_insn (pat);
8990 return NULL_RTX;
8991}
8992
8993/* Expand prefetch builtins. These take a single address as argument. */
8994
8995static rtx
2396bce1 8996frv_expand_prefetches (enum insn_code icode, tree call)
c557edf4
RS
8997{
8998 rtx pat;
2396bce1 8999 rtx op0 = frv_read_argument (call, 0);
c557edf4
RS
9000
9001 pat = GEN_FCN (icode) (force_reg (Pmode, op0));
9002 if (! pat)
9003 return 0;
9004
9005 emit_insn (pat);
9006 return 0;
9007}
9008
36a05131
BS
9009/* Expand builtins that take three operands and return void. The first
9010 argument must be a constant that describes a pair or quad accumulators. A
9011 fourth argument is created that is the accumulator guard register that
9012 corresponds to the accumulator. */
9013
9014static rtx
2396bce1 9015frv_expand_voidtriop_builtin (enum insn_code icode, tree call)
36a05131
BS
9016{
9017 rtx pat;
2396bce1
EC
9018 rtx op0 = frv_read_argument (call, 0);
9019 rtx op1 = frv_read_argument (call, 1);
9020 rtx op2 = frv_read_argument (call, 2);
36a05131
BS
9021 rtx op3;
9022
9023 op0 = frv_int_to_acc (icode, 0, op0);
9024 if (! op0)
9025 return NULL_RTX;
9026
9027 op1 = frv_legitimize_argument (icode, 1, op1);
9028 op2 = frv_legitimize_argument (icode, 2, op2);
9029 op3 = frv_matching_accg_for_acc (op0);
9030 pat = GEN_FCN (icode) (op0, op1, op2, op3);
9031 if (! pat)
9032 return NULL_RTX;
9033
9034 emit_insn (pat);
9035 return NULL_RTX;
9036}
9037
9038/* Expand builtins that perform accumulator-to-accumulator operations.
9039 These builtins take two accumulator numbers as argument and return
9040 void. */
9041
9042static rtx
2396bce1 9043frv_expand_voidaccop_builtin (enum insn_code icode, tree call)
36a05131
BS
9044{
9045 rtx pat;
2396bce1
EC
9046 rtx op0 = frv_read_argument (call, 0);
9047 rtx op1 = frv_read_argument (call, 1);
36a05131
BS
9048 rtx op2;
9049 rtx op3;
9050
9051 op0 = frv_int_to_acc (icode, 0, op0);
9052 if (! op0)
9053 return NULL_RTX;
9054
9055 op1 = frv_int_to_acc (icode, 1, op1);
9056 if (! op1)
9057 return NULL_RTX;
9058
9059 op2 = frv_matching_accg_for_acc (op0);
9060 op3 = frv_matching_accg_for_acc (op1);
9061 pat = GEN_FCN (icode) (op0, op1, op2, op3);
9062 if (! pat)
9063 return NULL_RTX;
9064
9065 emit_insn (pat);
9066 return NULL_RTX;
9067}
9068
38c28a25
AH
9069/* Expand a __builtin_read* function. ICODE is the instruction code for the
9070 membar and TARGET_MODE is the mode that the loaded value should have. */
c14ff86e
AH
9071
9072static rtx
ef4bddc2 9073frv_expand_load_builtin (enum insn_code icode, machine_mode target_mode,
2396bce1 9074 tree call, rtx target)
c14ff86e 9075{
2396bce1 9076 rtx op0 = frv_read_argument (call, 0);
38c28a25
AH
9077 rtx cookie = frv_io_address_cookie (op0);
9078
9079 if (target == 0 || !REG_P (target))
9080 target = gen_reg_rtx (target_mode);
9081 op0 = frv_volatile_memref (insn_data[icode].operand[0].mode, op0);
9082 convert_move (target, op0, 1);
9083 emit_insn (GEN_FCN (icode) (copy_rtx (op0), cookie, GEN_INT (FRV_IO_READ)));
9084 cfun->machine->has_membar_p = 1;
c14ff86e
AH
9085 return target;
9086}
9087
38c28a25 9088/* Likewise __builtin_write* functions. */
c14ff86e
AH
9089
9090static rtx
2396bce1 9091frv_expand_store_builtin (enum insn_code icode, tree call)
c14ff86e 9092{
2396bce1
EC
9093 rtx op0 = frv_read_argument (call, 0);
9094 rtx op1 = frv_read_argument (call, 1);
38c28a25 9095 rtx cookie = frv_io_address_cookie (op0);
c14ff86e 9096
38c28a25
AH
9097 op0 = frv_volatile_memref (insn_data[icode].operand[0].mode, op0);
9098 convert_move (op0, force_reg (insn_data[icode].operand[0].mode, op1), 1);
9099 emit_insn (GEN_FCN (icode) (copy_rtx (op0), cookie, GEN_INT (FRV_IO_WRITE)));
9100 cfun->machine->has_membar_p = 1;
c14ff86e
AH
9101 return NULL_RTX;
9102}
9103
a738d848
RS
9104/* Expand the MDPACKH builtin. It takes four unsigned short arguments and
9105 each argument forms one word of the two double-word input registers.
2396bce1
EC
9106 CALL is the tree for the call and TARGET, if nonnull, suggests a good place
9107 to put the return value. */
a738d848
RS
9108
9109static rtx
2396bce1 9110frv_expand_mdpackh_builtin (tree call, rtx target)
a738d848
RS
9111{
9112 enum insn_code icode = CODE_FOR_mdpackh;
9113 rtx pat, op0, op1;
2396bce1
EC
9114 rtx arg1 = frv_read_argument (call, 0);
9115 rtx arg2 = frv_read_argument (call, 1);
9116 rtx arg3 = frv_read_argument (call, 2);
9117 rtx arg4 = frv_read_argument (call, 3);
a738d848
RS
9118
9119 target = frv_legitimize_target (icode, target);
9120 op0 = gen_reg_rtx (DImode);
9121 op1 = gen_reg_rtx (DImode);
9122
0fa2e4df 9123 /* The high half of each word is not explicitly initialized, so indicate
a738d848 9124 that the input operands are not live before this point. */
c41c1387
RS
9125 emit_clobber (op0);
9126 emit_clobber (op1);
a738d848
RS
9127
9128 /* Move each argument into the low half of its associated input word. */
9129 emit_move_insn (simplify_gen_subreg (HImode, op0, DImode, 2), arg1);
9130 emit_move_insn (simplify_gen_subreg (HImode, op0, DImode, 6), arg2);
9131 emit_move_insn (simplify_gen_subreg (HImode, op1, DImode, 2), arg3);
9132 emit_move_insn (simplify_gen_subreg (HImode, op1, DImode, 6), arg4);
9133
9134 pat = GEN_FCN (icode) (target, op0, op1);
9135 if (! pat)
9136 return NULL_RTX;
9137
9138 emit_insn (pat);
9139 return target;
9140}
9141
36a05131
BS
9142/* Expand the MCLRACC builtin. This builtin takes a single accumulator
9143 number as argument. */
9144
9145static rtx
2396bce1 9146frv_expand_mclracc_builtin (tree call)
36a05131
BS
9147{
9148 enum insn_code icode = CODE_FOR_mclracc;
9149 rtx pat;
2396bce1 9150 rtx op0 = frv_read_argument (call, 0);
36a05131
BS
9151
9152 op0 = frv_int_to_acc (icode, 0, op0);
9153 if (! op0)
9154 return NULL_RTX;
9155
9156 pat = GEN_FCN (icode) (op0);
9157 if (pat)
9158 emit_insn (pat);
9159
9160 return NULL_RTX;
9161}
9162
9163/* Expand builtins that take no arguments. */
9164
9165static rtx
f2206911 9166frv_expand_noargs_builtin (enum insn_code icode)
36a05131 9167{
a556fd39 9168 rtx pat = GEN_FCN (icode) (const0_rtx);
36a05131
BS
9169 if (pat)
9170 emit_insn (pat);
9171
9172 return NULL_RTX;
9173}
9174
9175/* Expand MRDACC and MRDACCG. These builtins take a single accumulator
9176 number or accumulator guard number as argument and return an SI integer. */
9177
9178static rtx
2396bce1 9179frv_expand_mrdacc_builtin (enum insn_code icode, tree call)
36a05131
BS
9180{
9181 rtx pat;
9182 rtx target = gen_reg_rtx (SImode);
2396bce1 9183 rtx op0 = frv_read_argument (call, 0);
36a05131
BS
9184
9185 op0 = frv_int_to_acc (icode, 1, op0);
9186 if (! op0)
9187 return NULL_RTX;
9188
9189 pat = GEN_FCN (icode) (target, op0);
9190 if (! pat)
9191 return NULL_RTX;
9192
9193 emit_insn (pat);
9194 return target;
9195}
9196
9197/* Expand MWTACC and MWTACCG. These builtins take an accumulator or
9198 accumulator guard as their first argument and an SImode value as their
9199 second. */
9200
9201static rtx
2396bce1 9202frv_expand_mwtacc_builtin (enum insn_code icode, tree call)
36a05131
BS
9203{
9204 rtx pat;
2396bce1
EC
9205 rtx op0 = frv_read_argument (call, 0);
9206 rtx op1 = frv_read_argument (call, 1);
36a05131
BS
9207
9208 op0 = frv_int_to_acc (icode, 0, op0);
9209 if (! op0)
9210 return NULL_RTX;
9211
9212 op1 = frv_legitimize_argument (icode, 1, op1);
9213 pat = GEN_FCN (icode) (op0, op1);
9214 if (pat)
9215 emit_insn (pat);
9216
9217 return NULL_RTX;
9218}
9219
c557edf4
RS
9220/* Emit a move from SRC to DEST in SImode chunks. This can be used
9221 to move DImode values into and out of IACC0. */
9222
9223static void
9224frv_split_iacc_move (rtx dest, rtx src)
9225{
ef4bddc2 9226 machine_mode inner;
c557edf4
RS
9227 int i;
9228
9229 inner = GET_MODE (dest);
9230 for (i = 0; i < GET_MODE_SIZE (inner); i += GET_MODE_SIZE (SImode))
9231 emit_move_insn (simplify_gen_subreg (SImode, dest, inner, i),
9232 simplify_gen_subreg (SImode, src, inner, i));
9233}
9234
87b483a1 9235/* Expand builtins. */
36a05131 9236
14966b94 9237static rtx
f2206911
KC
9238frv_expand_builtin (tree exp,
9239 rtx target,
9240 rtx subtarget ATTRIBUTE_UNUSED,
ef4bddc2 9241 machine_mode mode ATTRIBUTE_UNUSED,
f2206911 9242 int ignore ATTRIBUTE_UNUSED)
36a05131 9243{
5039610b 9244 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
36a05131
BS
9245 unsigned fcode = (unsigned)DECL_FUNCTION_CODE (fndecl);
9246 unsigned i;
9247 struct builtin_description *d;
9248
c557edf4 9249 if (fcode < FRV_BUILTIN_FIRST_NONMEDIA && !TARGET_MEDIA)
36a05131
BS
9250 {
9251 error ("media functions are not available unless -mmedia is used");
9252 return NULL_RTX;
9253 }
9254
9255 switch (fcode)
9256 {
9257 case FRV_BUILTIN_MCOP1:
9258 case FRV_BUILTIN_MCOP2:
9259 case FRV_BUILTIN_MDUNPACKH:
9260 case FRV_BUILTIN_MBTOHE:
9261 if (! TARGET_MEDIA_REV1)
9262 {
9263 error ("this media function is only available on the fr500");
9264 return NULL_RTX;
9265 }
9266 break;
9267
9268 case FRV_BUILTIN_MQXMACHS:
9269 case FRV_BUILTIN_MQXMACXHS:
9270 case FRV_BUILTIN_MQMACXHS:
9271 case FRV_BUILTIN_MADDACCS:
9272 case FRV_BUILTIN_MSUBACCS:
9273 case FRV_BUILTIN_MASACCS:
9274 case FRV_BUILTIN_MDADDACCS:
9275 case FRV_BUILTIN_MDSUBACCS:
9276 case FRV_BUILTIN_MDASACCS:
9277 case FRV_BUILTIN_MABSHS:
9278 case FRV_BUILTIN_MDROTLI:
9279 case FRV_BUILTIN_MCPLHI:
9280 case FRV_BUILTIN_MCPLI:
9281 case FRV_BUILTIN_MDCUTSSI:
9282 case FRV_BUILTIN_MQSATHS:
9283 case FRV_BUILTIN_MHSETLOS:
9284 case FRV_BUILTIN_MHSETLOH:
9285 case FRV_BUILTIN_MHSETHIS:
9286 case FRV_BUILTIN_MHSETHIH:
9287 case FRV_BUILTIN_MHDSETS:
9288 case FRV_BUILTIN_MHDSETH:
9289 if (! TARGET_MEDIA_REV2)
9290 {
c557edf4
RS
9291 error ("this media function is only available on the fr400"
9292 " and fr550");
9293 return NULL_RTX;
9294 }
9295 break;
9296
9297 case FRV_BUILTIN_SMASS:
9298 case FRV_BUILTIN_SMSSS:
9299 case FRV_BUILTIN_SMU:
9300 case FRV_BUILTIN_ADDSS:
9301 case FRV_BUILTIN_SUBSS:
9302 case FRV_BUILTIN_SLASS:
9303 case FRV_BUILTIN_SCUTSS:
9304 case FRV_BUILTIN_IACCreadll:
9305 case FRV_BUILTIN_IACCreadl:
9306 case FRV_BUILTIN_IACCsetll:
9307 case FRV_BUILTIN_IACCsetl:
9308 if (!TARGET_FR405_BUILTINS)
9309 {
9310 error ("this builtin function is only available"
9311 " on the fr405 and fr450");
9312 return NULL_RTX;
9313 }
9314 break;
9315
9316 case FRV_BUILTIN_PREFETCH:
9317 if (!TARGET_FR500_FR550_BUILTINS)
9318 {
9319 error ("this builtin function is only available on the fr500"
9320 " and fr550");
9321 return NULL_RTX;
9322 }
9323 break;
9324
9325 case FRV_BUILTIN_MQLCLRHS:
9326 case FRV_BUILTIN_MQLMTHS:
9327 case FRV_BUILTIN_MQSLLHI:
9328 case FRV_BUILTIN_MQSRAHI:
9329 if (!TARGET_MEDIA_FR450)
9330 {
9331 error ("this builtin function is only available on the fr450");
36a05131
BS
9332 return NULL_RTX;
9333 }
9334 break;
9335
9336 default:
9337 break;
9338 }
9339
87b483a1 9340 /* Expand unique builtins. */
36a05131
BS
9341
9342 switch (fcode)
9343 {
9344 case FRV_BUILTIN_MTRAP:
9345 return frv_expand_noargs_builtin (CODE_FOR_mtrap);
9346
9347 case FRV_BUILTIN_MCLRACC:
2396bce1 9348 return frv_expand_mclracc_builtin (exp);
36a05131
BS
9349
9350 case FRV_BUILTIN_MCLRACCA:
9351 if (TARGET_ACC_8)
9352 return frv_expand_noargs_builtin (CODE_FOR_mclracca8);
9353 else
9354 return frv_expand_noargs_builtin (CODE_FOR_mclracca4);
9355
9356 case FRV_BUILTIN_MRDACC:
2396bce1 9357 return frv_expand_mrdacc_builtin (CODE_FOR_mrdacc, exp);
36a05131
BS
9358
9359 case FRV_BUILTIN_MRDACCG:
2396bce1 9360 return frv_expand_mrdacc_builtin (CODE_FOR_mrdaccg, exp);
36a05131
BS
9361
9362 case FRV_BUILTIN_MWTACC:
2396bce1 9363 return frv_expand_mwtacc_builtin (CODE_FOR_mwtacc, exp);
36a05131
BS
9364
9365 case FRV_BUILTIN_MWTACCG:
2396bce1 9366 return frv_expand_mwtacc_builtin (CODE_FOR_mwtaccg, exp);
36a05131 9367
a738d848 9368 case FRV_BUILTIN_MDPACKH:
2396bce1 9369 return frv_expand_mdpackh_builtin (exp, target);
a738d848 9370
c557edf4
RS
9371 case FRV_BUILTIN_IACCreadll:
9372 {
2396bce1 9373 rtx src = frv_read_iacc_argument (DImode, exp, 0);
c557edf4
RS
9374 if (target == 0 || !REG_P (target))
9375 target = gen_reg_rtx (DImode);
9376 frv_split_iacc_move (target, src);
9377 return target;
9378 }
9379
9380 case FRV_BUILTIN_IACCreadl:
2396bce1 9381 return frv_read_iacc_argument (SImode, exp, 0);
c557edf4
RS
9382
9383 case FRV_BUILTIN_IACCsetll:
9384 {
2396bce1
EC
9385 rtx dest = frv_read_iacc_argument (DImode, exp, 0);
9386 rtx src = frv_read_argument (exp, 1);
c557edf4
RS
9387 frv_split_iacc_move (dest, force_reg (DImode, src));
9388 return 0;
9389 }
9390
9391 case FRV_BUILTIN_IACCsetl:
9392 {
2396bce1
EC
9393 rtx dest = frv_read_iacc_argument (SImode, exp, 0);
9394 rtx src = frv_read_argument (exp, 1);
c557edf4
RS
9395 emit_move_insn (dest, force_reg (SImode, src));
9396 return 0;
9397 }
9398
36a05131
BS
9399 default:
9400 break;
9401 }
9402
87b483a1 9403 /* Expand groups of builtins. */
36a05131 9404
e97a46ce 9405 for (i = 0, d = bdesc_set; i < ARRAY_SIZE (bdesc_set); i++, d++)
36a05131 9406 if (d->code == fcode)
2396bce1 9407 return frv_expand_set_builtin (d->icode, exp, target);
36a05131 9408
e97a46ce 9409 for (i = 0, d = bdesc_1arg; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
36a05131 9410 if (d->code == fcode)
2396bce1 9411 return frv_expand_unop_builtin (d->icode, exp, target);
36a05131 9412
e97a46ce 9413 for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
36a05131 9414 if (d->code == fcode)
2396bce1 9415 return frv_expand_binop_builtin (d->icode, exp, target);
36a05131 9416
e97a46ce 9417 for (i = 0, d = bdesc_cut; i < ARRAY_SIZE (bdesc_cut); i++, d++)
36a05131 9418 if (d->code == fcode)
2396bce1 9419 return frv_expand_cut_builtin (d->icode, exp, target);
36a05131 9420
e97a46ce
KG
9421 for (i = 0, d = bdesc_2argimm; i < ARRAY_SIZE (bdesc_2argimm); i++, d++)
9422 if (d->code == fcode)
2396bce1 9423 return frv_expand_binopimm_builtin (d->icode, exp, target);
36a05131 9424
e97a46ce
KG
9425 for (i = 0, d = bdesc_void2arg; i < ARRAY_SIZE (bdesc_void2arg); i++, d++)
9426 if (d->code == fcode)
2396bce1 9427 return frv_expand_voidbinop_builtin (d->icode, exp);
36a05131 9428
e97a46ce
KG
9429 for (i = 0, d = bdesc_void3arg; i < ARRAY_SIZE (bdesc_void3arg); i++, d++)
9430 if (d->code == fcode)
2396bce1 9431 return frv_expand_voidtriop_builtin (d->icode, exp);
e97a46ce
KG
9432
9433 for (i = 0, d = bdesc_voidacc; i < ARRAY_SIZE (bdesc_voidacc); i++, d++)
9434 if (d->code == fcode)
2396bce1 9435 return frv_expand_voidaccop_builtin (d->icode, exp);
36a05131 9436
c557edf4
RS
9437 for (i = 0, d = bdesc_int_void2arg;
9438 i < ARRAY_SIZE (bdesc_int_void2arg); i++, d++)
9439 if (d->code == fcode)
2396bce1 9440 return frv_expand_int_void2arg (d->icode, exp);
c557edf4
RS
9441
9442 for (i = 0, d = bdesc_prefetches;
9443 i < ARRAY_SIZE (bdesc_prefetches); i++, d++)
9444 if (d->code == fcode)
2396bce1 9445 return frv_expand_prefetches (d->icode, exp);
c557edf4 9446
c14ff86e
AH
9447 for (i = 0, d = bdesc_loads; i < ARRAY_SIZE (bdesc_loads); i++, d++)
9448 if (d->code == fcode)
38c28a25 9449 return frv_expand_load_builtin (d->icode, TYPE_MODE (TREE_TYPE (exp)),
2396bce1 9450 exp, target);
c14ff86e
AH
9451
9452 for (i = 0, d = bdesc_stores; i < ARRAY_SIZE (bdesc_stores); i++, d++)
9453 if (d->code == fcode)
2396bce1 9454 return frv_expand_store_builtin (d->icode, exp);
c14ff86e 9455
36a05131
BS
9456 return 0;
9457}
14966b94 9458
b3fbfc07 9459static bool
3101faab 9460frv_in_small_data_p (const_tree decl)
b3fbfc07 9461{
0f6e5d45 9462 HOST_WIDE_INT size;
f961457f 9463 const char *section_name;
0f6e5d45
RH
9464
9465 /* Don't apply the -G flag to internal compiler structures. We
9466 should leave such structures in the main data section, partly
9467 for efficiency and partly because the size of some of them
9468 (such as C++ typeinfos) is not known until later. */
9469 if (TREE_CODE (decl) != VAR_DECL || DECL_ARTIFICIAL (decl))
9470 return false;
9471
0f6e5d45
RH
9472 /* If we already know which section the decl should be in, see if
9473 it's a small data section. */
9474 section_name = DECL_SECTION_NAME (decl);
9475 if (section_name)
9476 {
0f6e5d45
RH
9477 if (frv_string_begins_with (section_name, ".sdata"))
9478 return true;
9479 if (frv_string_begins_with (section_name, ".sbss"))
9480 return true;
68c0ab4f 9481 return false;
0f6e5d45 9482 }
b3fbfc07 9483
68c0ab4f 9484 size = int_size_in_bytes (TREE_TYPE (decl));
fa37ed29 9485 if (size > 0 && size <= g_switch_value)
68c0ab4f
RS
9486 return true;
9487
0f6e5d45 9488 return false;
b3fbfc07 9489}
3c50106f
RH
9490\f
9491static bool
f2206911
KC
9492frv_rtx_costs (rtx x,
9493 int code ATTRIBUTE_UNUSED,
9494 int outer_code ATTRIBUTE_UNUSED,
68f932c4 9495 int opno ATTRIBUTE_UNUSED,
f40751dd
JH
9496 int *total,
9497 bool speed ATTRIBUTE_UNUSED)
3c50106f 9498{
34208acf
AO
9499 if (outer_code == MEM)
9500 {
9501 /* Don't differentiate between memory addresses. All the ones
9502 we accept have equal cost. */
9503 *total = COSTS_N_INSNS (0);
9504 return true;
9505 }
9506
3c50106f
RH
9507 switch (code)
9508 {
9509 case CONST_INT:
2300b9dd 9510 /* Make 12-bit integers really cheap. */
2f5b1308 9511 if (IN_RANGE (INTVAL (x), -2048, 2047))
3c50106f
RH
9512 {
9513 *total = 0;
9514 return true;
9515 }
87b483a1 9516 /* Fall through. */
3c50106f
RH
9517
9518 case CONST:
9519 case LABEL_REF:
9520 case SYMBOL_REF:
9521 case CONST_DOUBLE:
9522 *total = COSTS_N_INSNS (2);
9523 return true;
9524
9525 case PLUS:
9526 case MINUS:
9527 case AND:
9528 case IOR:
9529 case XOR:
9530 case ASHIFT:
9531 case ASHIFTRT:
9532 case LSHIFTRT:
9533 case NOT:
9534 case NEG:
9535 case COMPARE:
9536 if (GET_MODE (x) == SImode)
9537 *total = COSTS_N_INSNS (1);
9538 else if (GET_MODE (x) == DImode)
9539 *total = COSTS_N_INSNS (2);
9540 else
9541 *total = COSTS_N_INSNS (3);
9542 return true;
9543
9544 case MULT:
9545 if (GET_MODE (x) == SImode)
9546 *total = COSTS_N_INSNS (2);
9547 else
9548 *total = COSTS_N_INSNS (6); /* guess */
9549 return true;
9550
9551 case DIV:
9552 case UDIV:
9553 case MOD:
9554 case UMOD:
9555 *total = COSTS_N_INSNS (18);
9556 return true;
9557
34208acf
AO
9558 case MEM:
9559 *total = COSTS_N_INSNS (3);
9560 return true;
9561
3c50106f
RH
9562 default:
9563 return false;
9564 }
9565}
90a63880
RH
9566\f
9567static void
f2206911 9568frv_asm_out_constructor (rtx symbol, int priority ATTRIBUTE_UNUSED)
90a63880 9569{
d6b5193b 9570 switch_to_section (ctors_section);
90a63880 9571 assemble_align (POINTER_SIZE);
34208acf
AO
9572 if (TARGET_FDPIC)
9573 {
44e91694
NS
9574 int ok = frv_assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, 1);
9575
9576 gcc_assert (ok);
34208acf
AO
9577 return;
9578 }
90a63880
RH
9579 assemble_integer_with_op ("\t.picptr\t", symbol);
9580}
9581
9582static void
f2206911 9583frv_asm_out_destructor (rtx symbol, int priority ATTRIBUTE_UNUSED)
90a63880 9584{
d6b5193b 9585 switch_to_section (dtors_section);
90a63880 9586 assemble_align (POINTER_SIZE);
34208acf
AO
9587 if (TARGET_FDPIC)
9588 {
44e91694 9589 int ok = frv_assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, 1);
2396bce1 9590
44e91694 9591 gcc_assert (ok);
34208acf
AO
9592 return;
9593 }
90a63880
RH
9594 assemble_integer_with_op ("\t.picptr\t", symbol);
9595}
8ac411c7
KH
9596
9597/* Worker function for TARGET_STRUCT_VALUE_RTX. */
9598
9599static rtx
9600frv_struct_value_rtx (tree fntype ATTRIBUTE_UNUSED,
9601 int incoming ATTRIBUTE_UNUSED)
9602{
9603 return gen_rtx_REG (Pmode, FRV_STRUCT_VALUE_REGNUM);
9604}
c557edf4 9605
bef8809e
AH
9606#define TLS_BIAS (2048 - 16)
9607
fdbe66f2 9608/* This is called from dwarf2out.c via TARGET_ASM_OUTPUT_DWARF_DTPREL.
bef8809e
AH
9609 We need to emit DTP-relative relocations. */
9610
fdbe66f2 9611static void
bef8809e
AH
9612frv_output_dwarf_dtprel (FILE *file, int size, rtx x)
9613{
44e91694 9614 gcc_assert (size == 4);
bef8809e
AH
9615 fputs ("\t.picptr\ttlsmoff(", file);
9616 /* We want the unbiased TLS offset, so add the bias to the
9617 expression, such that the implicit biasing cancels out. */
0a81f074 9618 output_addr_const (file, plus_constant (Pmode, x, TLS_BIAS));
bef8809e
AH
9619 fputs (")", file);
9620}
9621
c557edf4 9622#include "gt-frv.h"