OSDN Git Service

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