OSDN Git Service

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