OSDN Git Service

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