OSDN Git Service

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