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