OSDN Git Service

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