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