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