OSDN Git Service

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