frv.c 279 KB
Newer Older
dberlin's avatar
dberlin committed
1
/* Copyright (C) 1997, 1998, 1999, 2000, 2001, 2003, 2004, 2005, 2006, 2007
2
   Free Software Foundation, Inc.
bernds's avatar
bernds committed
3 4
   Contributed by Red Hat, Inc.

kcook's avatar
 
kcook committed
5
This file is part of GCC.
bernds's avatar
bernds committed
6

kcook's avatar
 
kcook committed
7
GCC is free software; you can redistribute it and/or modify
bernds's avatar
bernds committed
8
it under the terms of the GNU General Public License as published by
9
the Free Software Foundation; either version 3, or (at your option)
bernds's avatar
bernds committed
10 11
any later version.

kcook's avatar
 
kcook committed
12
GCC is distributed in the hope that it will be useful,
bernds's avatar
bernds committed
13 14 15 16 17
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
18 19
along with GCC; see the file COPYING3.  If not see
<http://www.gnu.org/licenses/>.  */
bernds's avatar
bernds committed
20 21 22

#include "config.h"
#include "system.h"
23 24
#include "coretypes.h"
#include "tm.h"
bernds's avatar
bernds committed
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
#include "rtl.h"
#include "tree.h"
#include "regs.h"
#include "hard-reg-set.h"
#include "real.h"
#include "insn-config.h"
#include "conditions.h"
#include "insn-flags.h"
#include "output.h"
#include "insn-attr.h"
#include "flags.h"
#include "recog.h"
#include "reload.h"
#include "expr.h"
#include "obstack.h"
#include "except.h"
#include "function.h"
#include "optabs.h"
#include "toplev.h"
#include "basic-block.h"
#include "tm_p.h"
#include "ggc.h"
#include <ctype.h>
#include "target.h"
#include "target-def.h"
50
#include "targhooks.h"
51
#include "integrate.h"
52
#include "langhooks.h"
dberlin's avatar
dberlin committed
53
#include "df.h"
bernds's avatar
bernds committed
54 55 56 57 58

#ifndef FRV_INLINE
#define FRV_INLINE inline
#endif

59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
/* The maximum number of distinct NOP patterns.  There are three:
   nop, fnop and mnop.  */
#define NUM_NOP_PATTERNS 3

/* Classification of instructions and units: integer, floating-point/media,
   branch and control.  */
enum frv_insn_group { GROUP_I, GROUP_FM, GROUP_B, GROUP_C, NUM_GROUPS };

/* The DFA names of the units, in packet order.  */
static const char *const frv_unit_names[] =
{
  "c",
  "i0", "f0",
  "i1", "f1",
  "i2", "f2",
  "i3", "f3",
  "b0", "b1"
};

/* The classification of each unit in frv_unit_names[].  */
static const enum frv_insn_group frv_unit_groups[ARRAY_SIZE (frv_unit_names)] =
{
  GROUP_C,
  GROUP_I, GROUP_FM,
  GROUP_I, GROUP_FM,
  GROUP_I, GROUP_FM,
  GROUP_I, GROUP_FM,
  GROUP_B, GROUP_B
};

/* Return the DFA unit code associated with the Nth unit of integer
   or floating-point group GROUP,  */
#define NTH_UNIT(GROUP, N) frv_unit_codes[(GROUP) + (N) * 2 + 1]

/* Return the number of integer or floating-point unit UNIT
   (1 for I1, 2 for F2, etc.).  */
#define UNIT_NUMBER(UNIT) (((UNIT) - 1) / 2)

/* The DFA unit number for each unit in frv_unit_names[].  */
static int frv_unit_codes[ARRAY_SIZE (frv_unit_names)];

/* FRV_TYPE_TO_UNIT[T] is the last unit in frv_unit_names[] that can issue
   an instruction of type T.  The value is ARRAY_SIZE (frv_unit_names) if
   no instruction of type T has been seen.  */
static unsigned int frv_type_to_unit[TYPE_UNKNOWN + 1];

/* An array of dummy nop INSNs, one for each type of nop that the
   target supports.  */
static GTY(()) rtx frv_nops[NUM_NOP_PATTERNS];

/* The number of nop instructions in frv_nops[].  */
static unsigned int frv_num_nops;

112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
/* Information about one __builtin_read or __builtin_write access, or
   the combination of several such accesses.  The most general value
   is all-zeros (an unknown access to an unknown address).  */
struct frv_io {
  /* The type of access.  FRV_IO_UNKNOWN means the access can be either
     a read or a write.  */
  enum { FRV_IO_UNKNOWN, FRV_IO_READ, FRV_IO_WRITE } type;

  /* The constant address being accessed, or zero if not known.  */
  HOST_WIDE_INT const_address;

  /* The run-time address, as used in operand 0 of the membar pattern.  */
  rtx var_address;
};

127 128 129 130 131 132 133 134 135 136 137 138 139 140
/* Return true if instruction INSN should be packed with the following
   instruction.  */
#define PACKING_FLAG_P(INSN) (GET_MODE (INSN) == TImode)

/* Set the value of PACKING_FLAG_P(INSN).  */
#define SET_PACKING_FLAG(INSN) PUT_MODE (INSN, TImode)
#define CLEAR_PACKING_FLAG(INSN) PUT_MODE (INSN, VOIDmode)

/* Loop with REG set to each hard register in rtx X.  */
#define FOR_EACH_REGNO(REG, X)						\
  for (REG = REGNO (X);							\
       REG < REGNO (X) + HARD_REGNO_NREGS (REGNO (X), GET_MODE (X));	\
       REG++)

141 142 143 144 145 146 147 148 149 150
/* This structure contains machine specific function data.  */
struct machine_function GTY(())
{
  /* True if we have created an rtx that relies on the stack frame.  */
  int frame_needed;

  /* True if this function contains at least one __builtin_{read,write}*.  */
  bool has_membar_p;
};

bernds's avatar
bernds committed
151 152 153 154 155 156 157 158
/* Temporary register allocation support structure.  */
typedef struct frv_tmp_reg_struct
  {
    HARD_REG_SET regs;		/* possible registers to allocate */
    int next_reg[N_REG_CLASSES];	/* next register to allocate per class */
  }
frv_tmp_reg_t;

159
/* Register state information for VLIW re-packing phase.  */
bernds's avatar
bernds committed
160
#define REGSTATE_CC_MASK	0x07	/* Mask to isolate CCn for cond exec */
161 162 163 164
#define REGSTATE_MODIFIED	0x08	/* reg modified in current VLIW insn */
#define REGSTATE_IF_TRUE	0x10	/* reg modified in cond exec true */
#define REGSTATE_IF_FALSE	0x20	/* reg modified in cond exec false */

bernds's avatar
bernds committed
165 166
#define REGSTATE_IF_EITHER	(REGSTATE_IF_TRUE | REGSTATE_IF_FALSE)

167
typedef unsigned char regstate_t;
bernds's avatar
bernds committed
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200

/* Used in frv_frame_accessor_t to indicate the direction of a register-to-
   memory move.  */
enum frv_stack_op
{
  FRV_LOAD,
  FRV_STORE
};

/* Information required by frv_frame_access.  */
typedef struct
{
  /* This field is FRV_LOAD if registers are to be loaded from the stack and
     FRV_STORE if they should be stored onto the stack.  FRV_STORE implies
     the move is being done by the prologue code while FRV_LOAD implies it
     is being done by the epilogue.  */
  enum frv_stack_op op;

  /* The base register to use when accessing the stack.  This may be the
     frame pointer, stack pointer, or a temporary.  The choice of register
     depends on which part of the frame is being accessed and how big the
     frame is.  */
  rtx base;

  /* The offset of BASE from the bottom of the current frame, in bytes.  */
  int base_offset;
} frv_frame_accessor_t;

/* Define the information needed to generate branch and scc insns.  This is
   stored from the compare operation.  */
rtx frv_compare_op0;
rtx frv_compare_op1;

201
/* Conditional execution support gathered together in one structure.  */
bernds's avatar
bernds committed
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
typedef struct
  {
    /* Linked list of insns to add if the conditional execution conversion was
       successful.  Each link points to an EXPR_LIST which points to the pattern
       of the insn to add, and the insn to be inserted before.  */
    rtx added_insns_list;

    /* Identify which registers are safe to allocate for if conversions to
       conditional execution.  We keep the last allocated register in the
       register classes between COND_EXEC statements.  This will mean we allocate
       different registers for each different COND_EXEC group if we can.  This
       might allow the scheduler to intermix two different COND_EXEC sections.  */
    frv_tmp_reg_t tmp_reg;

    /* For nested IFs, identify which CC registers are used outside of setting
       via a compare isnsn, and using via a check insn.  This will allow us to
       know if we can rewrite the register to use a different register that will
       be paired with the CR register controlling the nested IF-THEN blocks.  */
    HARD_REG_SET nested_cc_ok_rewrite;

    /* Temporary registers allocated to hold constants during conditional
       execution.  */
    rtx scratch_regs[FIRST_PSEUDO_REGISTER];

    /* Current number of temp registers available.  */
    int cur_scratch_regs;

229
    /* Number of nested conditional execution blocks.  */
bernds's avatar
bernds committed
230 231 232 233 234
    int num_nested_cond_exec;

    /* Map of insns that set up constants in scratch registers.  */
    bitmap scratch_insns_bitmap;

235
    /* Conditional execution test register (CC0..CC7).  */
bernds's avatar
bernds committed
236 237 238 239 240 241
    rtx cr_reg;

    /* Conditional execution compare register that is paired with cr_reg, so that
       nested compares can be done.  The csubcc and caddcc instructions don't
       have enough bits to specify both a CC register to be set and a CR register
       to do the test on, so the same bit number is used for both.  Needless to
kazu's avatar
kazu committed
242
       say, this is rather inconvenient for GCC.  */
bernds's avatar
bernds committed
243 244 245 246 247 248 249
    rtx nested_cc_reg;

    /* Extra CR registers used for &&, ||.  */
    rtx extra_int_cr;
    rtx extra_fp_cr;

    /* Previous CR used in nested if, to make sure we are dealing with the same
250
       nested if as the previous statement.  */
bernds's avatar
bernds committed
251 252 253 254 255 256 257 258 259
    rtx last_nested_if_cr;
  }
frv_ifcvt_t;

static /* GTY(()) */ frv_ifcvt_t frv_ifcvt;

/* Map register number to smallest register class.  */
enum reg_class regno_reg_class[FIRST_PSEUDO_REGISTER];

260
/* Map class letter into register class.  */
bernds's avatar
bernds committed
261 262
enum reg_class reg_class_from_letter[256];

263
/* Cached value of frv_stack_info.  */
bernds's avatar
bernds committed
264 265 266 267 268 269
static frv_stack_t *frv_stack_cache = (frv_stack_t *)0;

/* -mcpu= support */
frv_cpu_t frv_cpu_type = CPU_TYPE;	/* value of -mcpu= */

/* Forward references */
270 271

static bool frv_handle_option			(size_t, const char *, int);
272 273
static int frv_default_flags_for_cpu		(void);
static int frv_string_begins_with		(tree, const char *);
274
static FRV_INLINE bool frv_small_data_reloc_p	(rtx, int);
bernds's avatar
bernds committed
275
static void frv_print_operand_memory_reference_reg
276 277 278
						(FILE *, rtx);
static void frv_print_operand_memory_reference	(FILE *, rtx, int);
static int frv_print_operand_jump_hint		(rtx);
279
static const char *comparison_string		(enum rtx_code, rtx);
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
static FRV_INLINE int frv_regno_ok_for_base_p	(int, int);
static rtx single_set_pattern			(rtx);
static int frv_function_contains_far_jump	(void);
static rtx frv_alloc_temp_reg			(frv_tmp_reg_t *,
						 enum reg_class,
						 enum machine_mode,
						 int, int);
static rtx frv_frame_offset_rtx			(int);
static rtx frv_frame_mem			(enum machine_mode, rtx, int);
static rtx frv_dwarf_store			(rtx, int);
static void frv_frame_insn			(rtx, rtx);
static void frv_frame_access			(frv_frame_accessor_t*,
						 rtx, int);
static void frv_frame_access_multi		(frv_frame_accessor_t*,
						 frv_stack_t *, int);
static void frv_frame_access_standard_regs	(enum frv_stack_op,
						 frv_stack_t *);
static struct machine_function *frv_init_machine_status		(void);
static rtx frv_int_to_acc			(enum insn_code, int, rtx);
static enum machine_mode frv_matching_accg_mode	(enum machine_mode);
300 301
static rtx frv_read_argument			(tree, unsigned int);
static rtx frv_read_iacc_argument		(enum machine_mode, tree, unsigned int);
302 303 304
static int frv_check_constant_argument		(enum insn_code, int, rtx);
static rtx frv_legitimize_target		(enum insn_code, rtx);
static rtx frv_legitimize_argument		(enum insn_code, int, rtx);
305
static rtx frv_legitimize_tls_address		(rtx, enum tls_model);
306 307 308 309 310 311
static rtx frv_expand_set_builtin		(enum insn_code, tree, rtx);
static rtx frv_expand_unop_builtin		(enum insn_code, tree, rtx);
static rtx frv_expand_binop_builtin		(enum insn_code, tree, rtx);
static rtx frv_expand_cut_builtin		(enum insn_code, tree, rtx);
static rtx frv_expand_binopimm_builtin		(enum insn_code, tree, rtx);
static rtx frv_expand_voidbinop_builtin		(enum insn_code, tree);
312 313
static rtx frv_expand_int_void2arg		(enum insn_code, tree);
static rtx frv_expand_prefetches		(enum insn_code, tree);
314 315 316 317 318 319
static rtx frv_expand_voidtriop_builtin		(enum insn_code, tree);
static rtx frv_expand_voidaccop_builtin		(enum insn_code, tree);
static rtx frv_expand_mclracc_builtin		(tree);
static rtx frv_expand_mrdacc_builtin		(enum insn_code, tree);
static rtx frv_expand_mwtacc_builtin		(enum insn_code, tree);
static rtx frv_expand_noargs_builtin		(enum insn_code);
320
static void frv_split_iacc_move			(rtx, rtx);
321 322 323 324 325
static rtx frv_emit_comparison			(enum rtx_code, rtx, rtx);
static int frv_clear_registers_used		(rtx *, void *);
static void frv_ifcvt_add_insn			(rtx, rtx, int);
static rtx frv_ifcvt_rewrite_mem		(rtx, enum machine_mode, rtx);
static rtx frv_ifcvt_load_value			(rtx, rtx);
326 327 328 329 330 331 332
static int frv_acc_group_1			(rtx *, void *);
static unsigned int frv_insn_unit		(rtx);
static bool frv_issues_to_branch_unit_p		(rtx);
static int frv_cond_flags 			(rtx);
static bool frv_regstate_conflict_p 		(regstate_t, regstate_t);
static int frv_registers_conflict_p_1 		(rtx *, void *);
static bool frv_registers_conflict_p 		(rtx);
333
static void frv_registers_update_1 		(rtx, const_rtx, void *);
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
static void frv_registers_update 		(rtx);
static void frv_start_packet 			(void);
static void frv_start_packet_block 		(void);
static void frv_finish_packet 			(void (*) (void));
static bool frv_pack_insn_p 			(rtx);
static void frv_add_insn_to_packet		(rtx);
static void frv_insert_nop_in_packet		(rtx);
static bool frv_for_each_packet 		(void (*) (void));
static bool frv_sort_insn_group_1		(enum frv_insn_group,
						 unsigned int, unsigned int,
						 unsigned int, unsigned int,
						 state_t);
static int frv_compare_insns			(const void *, const void *);
static void frv_sort_insn_group			(enum frv_insn_group);
static void frv_reorder_packet 			(void);
static void frv_fill_unused_units		(enum frv_insn_group);
static void frv_align_label 			(void);
static void frv_reorg_packet 			(void);
static void frv_register_nop			(rtx);
static void frv_reorg 				(void);
354 355 356 357 358 359 360 361
static void frv_pack_insns			(void);
static void frv_function_prologue		(FILE *, HOST_WIDE_INT);
static void frv_function_epilogue		(FILE *, HOST_WIDE_INT);
static bool frv_assemble_integer		(rtx, unsigned, int);
static void frv_init_builtins			(void);
static rtx frv_expand_builtin			(tree, rtx, rtx, enum machine_mode, int);
static void frv_init_libfuncs			(void);
static bool frv_in_small_data_p			(tree);
rth's avatar
gcc/  
rth committed
362
static void frv_asm_output_mi_thunk
363
  (FILE *, tree, HOST_WIDE_INT, HOST_WIDE_INT, tree);
364 365 366
static void frv_setup_incoming_varargs		(CUMULATIVE_ARGS *,
						 enum machine_mode,
						 tree, int *, int);
367
static rtx frv_expand_builtin_saveregs		(void);
368 369 370
static bool frv_rtx_costs			(rtx, int, int, int*);
static void frv_asm_out_constructor		(rtx, int);
static void frv_asm_out_destructor		(rtx, int);
371 372 373 374 375
static bool frv_function_symbol_referenced_p	(rtx);
static bool frv_cannot_force_const_mem		(rtx);
static const char *unspec_got_name		(int);
static void frv_output_const_unspec		(FILE *,
						 const struct frv_unspec *);
376
static bool frv_function_ok_for_sibcall		(tree, tree);
377
static rtx frv_struct_value_rtx			(tree, int);
378
static bool frv_must_pass_in_stack (enum machine_mode mode, tree type);
379 380
static int frv_arg_partial_bytes (CUMULATIVE_ARGS *, enum machine_mode,
				  tree, bool);
ebotcazou's avatar
ebotcazou committed
381 382
static void frv_output_dwarf_dtprel		(FILE *, int, rtx)
  ATTRIBUTE_UNUSED;
bernds's avatar
bernds committed
383

384 385 386 387 388 389 390
/* Allow us to easily change the default for -malloc-cc.  */
#ifndef DEFAULT_NO_ALLOC_CC
#define MASK_DEFAULT_ALLOC_CC	MASK_ALLOC_CC
#else
#define MASK_DEFAULT_ALLOC_CC	0
#endif

bernds's avatar
bernds committed
391 392 393 394 395 396 397
/* Initialize the GCC target structure.  */
#undef  TARGET_ASM_FUNCTION_PROLOGUE
#define TARGET_ASM_FUNCTION_PROLOGUE frv_function_prologue
#undef  TARGET_ASM_FUNCTION_EPILOGUE
#define TARGET_ASM_FUNCTION_EPILOGUE frv_function_epilogue
#undef  TARGET_ASM_INTEGER
#define TARGET_ASM_INTEGER frv_assemble_integer
398 399 400 401 402 403 404 405 406 407 408
#undef TARGET_DEFAULT_TARGET_FLAGS
#define TARGET_DEFAULT_TARGET_FLAGS		\
  (MASK_DEFAULT_ALLOC_CC			\
   | MASK_COND_MOVE				\
   | MASK_SCC					\
   | MASK_COND_EXEC				\
   | MASK_VLIW_BRANCH				\
   | MASK_MULTI_CE				\
   | MASK_NESTED_CE)
#undef TARGET_HANDLE_OPTION
#define TARGET_HANDLE_OPTION frv_handle_option
409 410 411 412
#undef TARGET_INIT_BUILTINS
#define TARGET_INIT_BUILTINS frv_init_builtins
#undef TARGET_EXPAND_BUILTIN
#define TARGET_EXPAND_BUILTIN frv_expand_builtin
zack's avatar
zack committed
413 414
#undef TARGET_INIT_LIBFUNCS
#define TARGET_INIT_LIBFUNCS frv_init_libfuncs
415 416
#undef TARGET_IN_SMALL_DATA_P
#define TARGET_IN_SMALL_DATA_P frv_in_small_data_p
417 418
#undef TARGET_RTX_COSTS
#define TARGET_RTX_COSTS frv_rtx_costs
419 420 421 422
#undef TARGET_ASM_CONSTRUCTOR
#define TARGET_ASM_CONSTRUCTOR frv_asm_out_constructor
#undef TARGET_ASM_DESTRUCTOR
#define TARGET_ASM_DESTRUCTOR frv_asm_out_destructor
bernds's avatar
bernds committed
423

424 425
#undef TARGET_ASM_OUTPUT_MI_THUNK
#define TARGET_ASM_OUTPUT_MI_THUNK frv_asm_output_mi_thunk
rth's avatar
gcc/  
rth committed
426 427
#undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
#define TARGET_ASM_CAN_OUTPUT_MI_THUNK default_can_output_mi_thunk_no_vcall
428

429 430
#undef  TARGET_SCHED_ISSUE_RATE
#define TARGET_SCHED_ISSUE_RATE frv_issue_rate
431

432 433
#undef TARGET_FUNCTION_OK_FOR_SIBCALL
#define TARGET_FUNCTION_OK_FOR_SIBCALL frv_function_ok_for_sibcall
434 435 436
#undef TARGET_CANNOT_FORCE_CONST_MEM
#define TARGET_CANNOT_FORCE_CONST_MEM frv_cannot_force_const_mem

437 438 439
#undef TARGET_HAVE_TLS
#define TARGET_HAVE_TLS HAVE_AS_TLS

440 441
#undef TARGET_STRUCT_VALUE_RTX
#define TARGET_STRUCT_VALUE_RTX frv_struct_value_rtx
442 443
#undef TARGET_MUST_PASS_IN_STACK
#define TARGET_MUST_PASS_IN_STACK frv_must_pass_in_stack
444 445
#undef TARGET_PASS_BY_REFERENCE
#define TARGET_PASS_BY_REFERENCE hook_pass_by_reference_must_pass_in_stack
446 447
#undef TARGET_ARG_PARTIAL_BYTES
#define TARGET_ARG_PARTIAL_BYTES frv_arg_partial_bytes
448 449 450

#undef TARGET_EXPAND_BUILTIN_SAVEREGS
#define TARGET_EXPAND_BUILTIN_SAVEREGS frv_expand_builtin_saveregs
451 452
#undef TARGET_SETUP_INCOMING_VARARGS
#define TARGET_SETUP_INCOMING_VARARGS frv_setup_incoming_varargs
453 454
#undef TARGET_MACHINE_DEPENDENT_REORG
#define TARGET_MACHINE_DEPENDENT_REORG frv_reorg
455

ebotcazou's avatar
ebotcazou committed
456 457 458 459 460
#if HAVE_AS_TLS
#undef TARGET_ASM_OUTPUT_DWARF_DTPREL
#define TARGET_ASM_OUTPUT_DWARF_DTPREL frv_output_dwarf_dtprel
#endif

bernds's avatar
bernds committed
461
struct gcc_target targetm = TARGET_INITIALIZER;
462 463 464 465

#define FRV_SYMBOL_REF_TLS_P(RTX) \
  (GET_CODE (RTX) == SYMBOL_REF && SYMBOL_REF_TLS_MODEL (RTX) != 0)

bernds's avatar
bernds committed
466

467 468 469 470 471 472 473 474 475 476
/* Any function call that satisfies the machine-independent
   requirements is eligible on FR-V.  */

static bool
frv_function_ok_for_sibcall (tree decl ATTRIBUTE_UNUSED,
			     tree exp ATTRIBUTE_UNUSED)
{
  return true;
}

477 478
/* Return true if SYMBOL is a small data symbol and relocation RELOC
   can be used to access it directly in a load or store.  */
bernds's avatar
bernds committed
479

480 481
static FRV_INLINE bool
frv_small_data_reloc_p (rtx symbol, int reloc)
bernds's avatar
bernds committed
482
{
483 484 485 486 487
  return (GET_CODE (symbol) == SYMBOL_REF
	  && SYMBOL_REF_SMALL_P (symbol)
	  && (!TARGET_FDPIC || flag_pic == 1)
	  && (reloc == R_FRV_GOTOFF12 || reloc == R_FRV_GPREL12));
}
bernds's avatar
bernds committed
488

489 490
/* Return true if X is a valid relocation unspec.  If it is, fill in UNSPEC
   appropriately.  */
bernds's avatar
bernds committed
491

492
bool
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
frv_const_unspec_p (rtx x, struct frv_unspec *unspec)
{
  if (GET_CODE (x) == CONST)
    {
      unspec->offset = 0;
      x = XEXP (x, 0);
      if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 1)) == CONST_INT)
	{
	  unspec->offset += INTVAL (XEXP (x, 1));
	  x = XEXP (x, 0);
	}
      if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_GOT)
	{
	  unspec->symbol = XVECEXP (x, 0, 0);
	  unspec->reloc = INTVAL (XVECEXP (x, 0, 1));
bernds's avatar
bernds committed
508

509 510
	  if (unspec->offset == 0)
	    return true;
bernds's avatar
bernds committed
511

512 513 514 515 516 517 518
	  if (frv_small_data_reloc_p (unspec->symbol, unspec->reloc)
	      && unspec->offset > 0
	      && (unsigned HOST_WIDE_INT) unspec->offset < g_switch_value)
	    return true;
	}
    }
  return false;
bernds's avatar
bernds committed
519 520
}

521 522 523
/* Decide whether we can force certain constants to memory.  If we
   decide we can't, the caller should be able to cope with it in
   another way.
bernds's avatar
bernds committed
524

525 526
   We never allow constants to be forced into memory for TARGET_FDPIC.
   This is necessary for several reasons:
bernds's avatar
bernds committed
527

528 529 530
   1. Since LEGITIMATE_CONSTANT_P rejects constant pool addresses, the
      target-independent code will try to force them into the constant
      pool, thus leading to infinite recursion.
bernds's avatar
bernds committed
531

532 533
   2. We can never introduce new constant pool references during reload.
      Any such reference would require use of the pseudo FDPIC register.
bernds's avatar
bernds committed
534

535 536 537 538 539 540 541 542 543 544
   3. We can't represent a constant added to a function pointer (which is
      not the same as a pointer to a function+constant).

   4. In many cases, it's more efficient to calculate the constant in-line.  */

static bool
frv_cannot_force_const_mem (rtx x ATTRIBUTE_UNUSED)
{
  return TARGET_FDPIC;
}
bernds's avatar
bernds committed
545

546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
/* Implement TARGET_HANDLE_OPTION.  */

static bool
frv_handle_option (size_t code, const char *arg, int value ATTRIBUTE_UNUSED)
{
  switch (code)
    {
    case OPT_mcpu_:
      if (strcmp (arg, "simple") == 0)
	frv_cpu_type = FRV_CPU_SIMPLE;
      else if (strcmp (arg, "tomcat") == 0)
	frv_cpu_type = FRV_CPU_TOMCAT;
      else if (strcmp (arg, "fr550") == 0)
	frv_cpu_type = FRV_CPU_FR550;
      else if (strcmp (arg, "fr500") == 0)
	frv_cpu_type = FRV_CPU_FR500;
      else if (strcmp (arg, "fr450") == 0)
	frv_cpu_type = FRV_CPU_FR450;
      else if (strcmp (arg, "fr405") == 0)
	frv_cpu_type = FRV_CPU_FR405;
      else if (strcmp (arg, "fr400") == 0)
	frv_cpu_type = FRV_CPU_FR400;
      else if (strcmp (arg, "fr300") == 0)
	frv_cpu_type = FRV_CPU_FR300;
      else if (strcmp (arg, "frv") == 0)
	frv_cpu_type = FRV_CPU_GENERIC;
      else
	return false;
      return true;

    default:
      return true;
    }
}

bernds's avatar
bernds committed
581
static int
582
frv_default_flags_for_cpu (void)
bernds's avatar
bernds committed
583 584 585 586 587 588
{
  switch (frv_cpu_type)
    {
    case FRV_CPU_GENERIC:
      return MASK_DEFAULT_FRV;

589 590 591
    case FRV_CPU_FR550:
      return MASK_DEFAULT_FR550;

bernds's avatar
bernds committed
592 593 594 595
    case FRV_CPU_FR500:
    case FRV_CPU_TOMCAT:
      return MASK_DEFAULT_FR500;

596 597 598 599
    case FRV_CPU_FR450:
      return MASK_DEFAULT_FR450;

    case FRV_CPU_FR405:
bernds's avatar
bernds committed
600 601 602 603 604 605
    case FRV_CPU_FR400:
      return MASK_DEFAULT_FR400;

    case FRV_CPU_FR300:
    case FRV_CPU_SIMPLE:
      return MASK_DEFAULT_SIMPLE;
606 607 608

    default:
      gcc_unreachable ();
bernds's avatar
bernds committed
609 610 611 612 613 614 615 616 617 618 619 620 621
    }
}

/* Sometimes certain combinations of command options do not make
   sense on a particular target machine.  You can define a macro
   `OVERRIDE_OPTIONS' to take account of this.  This macro, if
   defined, is executed once just after all the command options have
   been parsed.

   Don't use this macro to turn on various extra optimizations for
   `-O'.  That is what `OPTIMIZATION_OPTIONS' is for.  */

void
622
frv_override_options (void)
bernds's avatar
bernds committed
623
{
624 625
  int regno;
  unsigned int i;
bernds's avatar
bernds committed
626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645

  target_flags |= (frv_default_flags_for_cpu () & ~target_flags_explicit);

  /* -mlibrary-pic sets -fPIC and -G0 and also suppresses warnings from the
     linker about linking pic and non-pic code.  */
  if (TARGET_LIBPIC)
    {
      if (!flag_pic)		/* -fPIC */
	flag_pic = 2;

      if (! g_switch_set)	/* -G0 */
	{
	  g_switch_set = 1;
	  g_switch_value = 0;
	}
    }

  /* A C expression whose value is a register class containing hard
     register REGNO.  In general there is more than one such class;
     choose a class which is "minimal", meaning that no smaller class
646
     also contains the register.  */
bernds's avatar
bernds committed
647 648 649 650 651 652 653 654

  for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
    {
      enum reg_class class;

      if (GPR_P (regno))
	{
	  int gpr_reg = regno - GPR_FIRST;
655 656 657 658 659 660 661 662 663 664 665 666 667 668

	  if (gpr_reg == GR8_REG)
	    class = GR8_REGS;

	  else if (gpr_reg == GR9_REG)
	    class = GR9_REGS;

	  else if (gpr_reg == GR14_REG)
	    class = FDPIC_FPTR_REGS;

	  else if (gpr_reg == FDPIC_REGNO)
	    class = FDPIC_REGS;

	  else if ((gpr_reg & 3) == 0)
bernds's avatar
bernds committed
669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771
	    class = QUAD_REGS;

	  else if ((gpr_reg & 1) == 0)
	    class = EVEN_REGS;

	  else
	    class = GPR_REGS;
	}

      else if (FPR_P (regno))
	{
	  int fpr_reg = regno - GPR_FIRST;
	  if ((fpr_reg & 3) == 0)
	    class = QUAD_FPR_REGS;

	  else if ((fpr_reg & 1) == 0)
	    class = FEVEN_REGS;

	  else
	    class = FPR_REGS;
	}

      else if (regno == LR_REGNO)
	class = LR_REG;

      else if (regno == LCR_REGNO)
	class = LCR_REG;

      else if (ICC_P (regno))
	class = ICC_REGS;

      else if (FCC_P (regno))
	class = FCC_REGS;

      else if (ICR_P (regno))
	class = ICR_REGS;

      else if (FCR_P (regno))
	class = FCR_REGS;

      else if (ACC_P (regno))
	{
	  int r = regno - ACC_FIRST;
	  if ((r & 3) == 0)
	    class = QUAD_ACC_REGS;
	  else if ((r & 1) == 0)
	    class = EVEN_ACC_REGS;
	  else
	    class = ACC_REGS;
	}

      else if (ACCG_P (regno))
	class = ACCG_REGS;

      else
	class = NO_REGS;

      regno_reg_class[regno] = class;
    }

  /* Check for small data option */
  if (!g_switch_set)
    g_switch_value = SDATA_DEFAULT_SIZE;

  /* A C expression which defines the machine-dependent operand
     constraint letters for register classes.  If CHAR is such a
     letter, the value should be the register class corresponding to
     it.  Otherwise, the value should be `NO_REGS'.  The register
     letter `r', corresponding to class `GENERAL_REGS', will not be
     passed to this macro; you do not need to handle it.

     The following letters are unavailable, due to being used as
     constraints:
	'0'..'9'
	'<', '>'
	'E', 'F', 'G', 'H'
	'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P'
	'Q', 'R', 'S', 'T', 'U'
	'V', 'X'
	'g', 'i', 'm', 'n', 'o', 'p', 'r', 's' */

  for (i = 0; i < 256; i++)
    reg_class_from_letter[i] = NO_REGS;

  reg_class_from_letter['a'] = ACC_REGS;
  reg_class_from_letter['b'] = EVEN_ACC_REGS;
  reg_class_from_letter['c'] = CC_REGS;
  reg_class_from_letter['d'] = GPR_REGS;
  reg_class_from_letter['e'] = EVEN_REGS;
  reg_class_from_letter['f'] = FPR_REGS;
  reg_class_from_letter['h'] = FEVEN_REGS;
  reg_class_from_letter['l'] = LR_REG;
  reg_class_from_letter['q'] = QUAD_REGS;
  reg_class_from_letter['t'] = ICC_REGS;
  reg_class_from_letter['u'] = FCC_REGS;
  reg_class_from_letter['v'] = ICR_REGS;
  reg_class_from_letter['w'] = FCR_REGS;
  reg_class_from_letter['x'] = QUAD_FPR_REGS;
  reg_class_from_letter['y'] = LCR_REG;
  reg_class_from_letter['z'] = SPR_REGS;
  reg_class_from_letter['A'] = QUAD_ACC_REGS;
  reg_class_from_letter['B'] = ACCG_REGS;
  reg_class_from_letter['C'] = CR_REGS;
772 773
  reg_class_from_letter['W'] = FDPIC_CALL_REGS; /* gp14+15 */
  reg_class_from_letter['Z'] = FDPIC_REGS; /* gp15 */
bernds's avatar
bernds committed
774 775 776 777

  /* There is no single unaligned SI op for PIC code.  Sometimes we
     need to use ".4byte" and sometimes we need to use ".picptr".
     See frv_assemble_integer for details.  */
778
  if (flag_pic || TARGET_FDPIC)
bernds's avatar
bernds committed
779 780
    targetm.asm_out.unaligned_op.si = 0;

781 782 783
  if ((target_flags_explicit & MASK_LINKED_FP) == 0)
    target_flags |= MASK_LINKED_FP;

784 785 786
  if ((target_flags_explicit & MASK_OPTIMIZE_MEMBAR) == 0)
    target_flags |= MASK_OPTIMIZE_MEMBAR;

787 788 789 790 791 792
  for (i = 0; i < ARRAY_SIZE (frv_unit_names); i++)
    frv_unit_codes[i] = get_cpu_unit_code (frv_unit_names[i]);

  for (i = 0; i < ARRAY_SIZE (frv_type_to_unit); i++)
    frv_type_to_unit[i] = ARRAY_SIZE (frv_unit_codes);

bernds's avatar
bernds committed
793 794 795 796 797 798 799 800 801 802 803 804 805
  init_machine_status = frv_init_machine_status;
}


/* Some machines may desire to change what optimizations are performed for
   various optimization levels.  This macro, if defined, is executed once just
   after the optimization level is determined and before the remainder of the
   command options have been parsed.  Values set in this macro are used as the
   default values for the other command line options.

   LEVEL is the optimization level specified; 2 if `-O2' is specified, 1 if
   `-O' is specified, and 0 if neither is specified.

806
   SIZE is nonzero if `-Os' is specified, 0 otherwise.
bernds's avatar
bernds committed
807 808 809

   You should not use this macro to change options that are not
   machine-specific.  These should uniformly selected by the same optimization
810
   level on all supported machines.  Use this macro to enable machine-specific
bernds's avatar
bernds committed
811 812 813 814 815 816 817 818
   optimizations.

   *Do not examine `write_symbols' in this macro!* The debugging options are
   *not supposed to alter the generated code.  */

/* On the FRV, possibly disable VLIW packing which is done by the 2nd
   scheduling pass at the current time.  */
void
819
frv_optimization_options (int level, int size ATTRIBUTE_UNUSED)
bernds's avatar
bernds committed
820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835
{
  if (level >= 2)
    {
#ifdef DISABLE_SCHED2
      flag_schedule_insns_after_reload = 0;
#endif
#ifdef ENABLE_RCSP
      flag_rcsp = 1;
#endif
    }
}


/* Return true if NAME (a STRING_CST node) begins with PREFIX.  */

static int
836
frv_string_begins_with (tree name, const char *prefix)
bernds's avatar
bernds committed
837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865
{
  int prefix_len = strlen (prefix);

  /* Remember: NAME's length includes the null terminator.  */
  return (TREE_STRING_LENGTH (name) > prefix_len
	  && strncmp (TREE_STRING_POINTER (name), prefix, prefix_len) == 0);
}

/* Zero or more C statements that may conditionally modify two variables
   `fixed_regs' and `call_used_regs' (both of type `char []') after they have
   been initialized from the two preceding macros.

   This is necessary in case the fixed or call-clobbered registers depend on
   target flags.

   You need not define this macro if it has no work to do.

   If the usage of an entire class of registers depends on the target flags,
   you may indicate this to GCC by using this macro to modify `fixed_regs' and
   `call_used_regs' to 1 for each of the registers in the classes which should
   not be used by GCC.  Also define the macro `REG_CLASS_FROM_LETTER' to return
   `NO_REGS' if it is called with a letter for a class that shouldn't be used.

   (However, if this class is not included in `GENERAL_REGS' and all of the
   insn patterns whose constraints permit this class are controlled by target
   switches, then GCC will automatically avoid using these registers when the
   target switches are opposed to them.)  */

void
866
frv_conditional_register_usage (void)
bernds's avatar
bernds committed
867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888
{
  int i;

  for (i = GPR_FIRST + NUM_GPRS; i <= GPR_LAST; i++)
    fixed_regs[i] = call_used_regs[i] = 1;

  for (i = FPR_FIRST + NUM_FPRS; i <= FPR_LAST; i++)
    fixed_regs[i] = call_used_regs[i] = 1;

  /* Reserve the registers used for conditional execution.  At present, we need
     1 ICC and 1 ICR register.  */
  fixed_regs[ICC_TEMP] = call_used_regs[ICC_TEMP] = 1;
  fixed_regs[ICR_TEMP] = call_used_regs[ICR_TEMP] = 1;

  if (TARGET_FIXED_CC)
    {
      fixed_regs[ICC_FIRST] = call_used_regs[ICC_FIRST] = 1;
      fixed_regs[FCC_FIRST] = call_used_regs[FCC_FIRST] = 1;
      fixed_regs[ICR_FIRST] = call_used_regs[ICR_FIRST] = 1;
      fixed_regs[FCR_FIRST] = call_used_regs[FCR_FIRST] = 1;
    }

889 890 891 892
  if (TARGET_FDPIC)
    fixed_regs[GPR_FIRST + 16] = fixed_regs[GPR_FIRST + 17] =
      call_used_regs[GPR_FIRST + 16] = call_used_regs[GPR_FIRST + 17] = 0;

bernds's avatar
bernds committed
893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
#if 0
  /* If -fpic, SDA_BASE_REG is the PIC register.  */
  if (g_switch_value == 0 && !flag_pic)
    fixed_regs[SDA_BASE_REG] = call_used_regs[SDA_BASE_REG] = 0;

  if (!flag_pic)
    fixed_regs[PIC_REGNO] = call_used_regs[PIC_REGNO] = 0;
#endif
}


/*
 * Compute the stack frame layout
 *
 * Register setup:
 * +---------------+-----------------------+-----------------------+
 * |Register       |type                   |caller-save/callee-save|
 * +---------------+-----------------------+-----------------------+
 * |GR0            |Zero register          |        -              |
 * |GR1            |Stack pointer(SP)      |        -              |
 * |GR2            |Frame pointer(FP)      |        -              |
 * |GR3            |Hidden parameter       |        caller save    |
 * |GR4-GR7        |        -              |        caller save    |
 * |GR8-GR13       |Argument register      |        caller save    |
 * |GR14-GR15      |        -              |        caller save    |
 * |GR16-GR31      |        -              |        callee save    |
 * |GR32-GR47      |        -              |        caller save    |
 * |GR48-GR63      |        -              |        callee save    |
 * |FR0-FR15       |        -              |        caller save    |
 * |FR16-FR31      |        -              |        callee save    |
 * |FR32-FR47      |        -              |        caller save    |
 * |FR48-FR63      |        -              |        callee save    |
 * +---------------+-----------------------+-----------------------+
 *
 * Stack frame setup:
 * Low
 *     SP-> |-----------------------------------|
 *	    |         Argument area		|
 *	    |-----------------------------------|
 *	    |	 Register save area		|
 *	    |-----------------------------------|
 *	    |	Local variable save area	|
 *     FP-> |-----------------------------------|
 *	    |	    Old FP			|
 *	    |-----------------------------------|
 *	    |    Hidden parameter save area     |
 *	    |-----------------------------------|
 *	    | Return address(LR) storage area   |
 *	    |-----------------------------------|
 *	    |     Padding for alignment         |
 *	    |-----------------------------------|
 *	    |     Register argument area	|
 * OLD SP-> |-----------------------------------|
 *          |       Parameter area		|
 *          |-----------------------------------|
 * High
 *
 * Argument area/Parameter area:
 *
 * When a function is called, this area is used for argument transfer.  When
 * the argument is set up by the caller function, this area is referred to as
 * the argument area.  When the argument is referenced by the callee function,
 * this area is referred to as the parameter area.  The area is allocated when
 * all arguments cannot be placed on the argument register at the time of
 * argument transfer.
 *
 * Register save area:
 *
 * This is a register save area that must be guaranteed for the caller
 * function.  This area is not secured when the register save operation is not
 * needed.
 *
 * Local variable save area:
 *
 * This is the area for local variables and temporary variables.
 *
 * Old FP:
 *
 * This area stores the FP value of the caller function.
 *
 * Hidden parameter save area:
 *
 * This area stores the start address of the return value storage
 * area for a struct/union return function.
 * When a struct/union is used as the return value, the caller
 * function stores the return value storage area start address in
 * register GR3 and passes it to the caller function.
 * The callee function interprets the address stored in the GR3
 * as the return value storage area start address.
 * When register GR3 needs to be saved into memory, the callee
 * function saves it in the hidden parameter save area.  This
 * area is not secured when the save operation is not needed.
 *
 * Return address(LR) storage area:
 *
 * This area saves the LR.  The LR stores the address of a return to the caller
 * function for the purpose of function calling.
 *
 * Argument register area:
 *
 * This area saves the argument register.  This area is not secured when the
 * save operation is not needed.
 *
 * Argument:
 *
 * Arguments, the count of which equals the count of argument registers (6
 * words), are positioned in registers GR8 to GR13 and delivered to the callee
 * function.  When a struct/union return function is called, the return value
 * area address is stored in register GR3.  Arguments not placed in the
 * argument registers will be stored in the stack argument area for transfer
 * purposes.  When an 8-byte type argument is to be delivered using registers,
 * it is divided into two and placed in two registers for transfer.  When
 * argument registers must be saved to memory, the callee function secures an
 * argument register save area in the stack.  In this case, a continuous
 * argument register save area must be established in the parameter area.  The
 * argument register save area must be allocated as needed to cover the size of
 * the argument register to be saved.  If the function has a variable count of
 * arguments, it saves all argument registers in the argument register save
 * area.
 *
 * Argument Extension Format:
 *
 * When an argument is to be stored in the stack, its type is converted to an
 * extended type in accordance with the individual argument type.  The argument
 * is freed by the caller function after the return from the callee function is
 * made.
 *
 * +-----------------------+---------------+------------------------+
 * |    Argument Type      |Extended Type  |Stack Storage Size(byte)|
 * +-----------------------+---------------+------------------------+
 * |char                   |int            |        4		    |
 * |signed char            |int            |        4		    |
 * |unsigned char          |int            |        4		    |
 * |[signed] short int     |int            |        4		    |
 * |unsigned short int     |int            |        4		    |
 * |[signed] int           |No extension   |        4		    |
 * |unsigned int           |No extension   |        4		    |
 * |[signed] long int      |No extension   |        4		    |
 * |unsigned long int      |No extension   |        4		    |
 * |[signed] long long int |No extension   |        8		    |
 * |unsigned long long int |No extension   |        8		    |
 * |float                  |double         |        8		    |
 * |double                 |No extension   |        8		    |
 * |long double            |No extension   |        8		    |
 * |pointer                |No extension   |        4		    |
 * |struct/union           |-              |        4 (*1)	    |
 * +-----------------------+---------------+------------------------+
 *
 * When a struct/union is to be delivered as an argument, the caller copies it
 * to the local variable area and delivers the address of that area.
 *
 * Return Value:
 *
 * +-------------------------------+----------------------+
 * |Return Value Type              |Return Value Interface|
 * +-------------------------------+----------------------+
 * |void                           |None                  |
 * |[signed|unsigned] char         |GR8                   |
 * |[signed|unsigned] short int    |GR8                   |
 * |[signed|unsigned] int          |GR8                   |
 * |[signed|unsigned] long int     |GR8                   |
 * |pointer                        |GR8                   |
 * |[signed|unsigned] long long int|GR8 & GR9             |
 * |float                          |GR8                   |
 * |double                         |GR8 & GR9             |
 * |long double                    |GR8 & GR9             |
 * |struct/union                   |(*1)                  |
 * +-------------------------------+----------------------+
 *
 * When a struct/union is used as the return value, the caller function stores
 * the start address of the return value storage area into GR3 and then passes
 * it to the callee function.  The callee function interprets GR3 as the start
 * address of the return value storage area.  When this address needs to be
 * saved in memory, the callee function secures the hidden parameter save area
 * and saves the address in that area.
 */

frv_stack_t *
1071
frv_stack_info (void)
bernds's avatar
bernds committed
1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082
{
  static frv_stack_t info, zero_info;
  frv_stack_t *info_ptr	= &info;
  tree fndecl		= current_function_decl;
  int varargs_p		= 0;
  tree cur_arg;
  tree next_arg;
  int range;
  int alignment;
  int offset;

1083 1084
  /* If we've already calculated the values and reload is complete,
     just return now.  */
bernds's avatar
bernds committed
1085 1086 1087
  if (frv_stack_cache)
    return frv_stack_cache;

1088
  /* Zero all fields.  */
bernds's avatar
bernds committed
1089 1090
  info = zero_info;

1091
  /* Set up the register range information.  */
bernds's avatar
bernds committed
1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122
  info_ptr->regs[STACK_REGS_GPR].name         = "gpr";
  info_ptr->regs[STACK_REGS_GPR].first        = LAST_ARG_REGNUM + 1;
  info_ptr->regs[STACK_REGS_GPR].last         = GPR_LAST;
  info_ptr->regs[STACK_REGS_GPR].dword_p      = TRUE;

  info_ptr->regs[STACK_REGS_FPR].name         = "fpr";
  info_ptr->regs[STACK_REGS_FPR].first        = FPR_FIRST;
  info_ptr->regs[STACK_REGS_FPR].last         = FPR_LAST;
  info_ptr->regs[STACK_REGS_FPR].dword_p      = TRUE;

  info_ptr->regs[STACK_REGS_LR].name          = "lr";
  info_ptr->regs[STACK_REGS_LR].first         = LR_REGNO;
  info_ptr->regs[STACK_REGS_LR].last          = LR_REGNO;
  info_ptr->regs[STACK_REGS_LR].special_p     = 1;

  info_ptr->regs[STACK_REGS_CC].name          = "cc";
  info_ptr->regs[STACK_REGS_CC].first         = CC_FIRST;
  info_ptr->regs[STACK_REGS_CC].last          = CC_LAST;
  info_ptr->regs[STACK_REGS_CC].field_p       = TRUE;

  info_ptr->regs[STACK_REGS_LCR].name         = "lcr";
  info_ptr->regs[STACK_REGS_LCR].first        = LCR_REGNO;
  info_ptr->regs[STACK_REGS_LCR].last         = LCR_REGNO;

  info_ptr->regs[STACK_REGS_STDARG].name      = "stdarg";
  info_ptr->regs[STACK_REGS_STDARG].first     = FIRST_ARG_REGNUM;
  info_ptr->regs[STACK_REGS_STDARG].last      = LAST_ARG_REGNUM;
  info_ptr->regs[STACK_REGS_STDARG].dword_p   = 1;
  info_ptr->regs[STACK_REGS_STDARG].special_p = 1;

  info_ptr->regs[STACK_REGS_STRUCT].name      = "struct";
1123 1124
  info_ptr->regs[STACK_REGS_STRUCT].first     = FRV_STRUCT_VALUE_REGNUM;
  info_ptr->regs[STACK_REGS_STRUCT].last      = FRV_STRUCT_VALUE_REGNUM;
bernds's avatar
bernds committed
1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153
  info_ptr->regs[STACK_REGS_STRUCT].special_p = 1;

  info_ptr->regs[STACK_REGS_FP].name          = "fp";
  info_ptr->regs[STACK_REGS_FP].first         = FRAME_POINTER_REGNUM;
  info_ptr->regs[STACK_REGS_FP].last          = FRAME_POINTER_REGNUM;
  info_ptr->regs[STACK_REGS_FP].special_p     = 1;

  /* Determine if this is a stdarg function.  If so, allocate space to store
     the 6 arguments.  */
  if (cfun->stdarg)
    varargs_p = 1;

  else
    {
      /* Find the last argument, and see if it is __builtin_va_alist.  */
      for (cur_arg = DECL_ARGUMENTS (fndecl); cur_arg != (tree)0; cur_arg = next_arg)
	{
	  next_arg = TREE_CHAIN (cur_arg);
	  if (next_arg == (tree)0)
	    {
	      if (DECL_NAME (cur_arg)
		  && !strcmp (IDENTIFIER_POINTER (DECL_NAME (cur_arg)), "__builtin_va_alist"))
		varargs_p = 1;

	      break;
	    }
	}
    }

1154
  /* Iterate over all of the register ranges.  */
bernds's avatar
bernds committed
1155 1156 1157 1158 1159 1160 1161 1162 1163
  for (range = 0; range < STACK_REGS_MAX; range++)
    {
      frv_stack_regs_t *reg_ptr = &(info_ptr->regs[range]);
      int first = reg_ptr->first;
      int last = reg_ptr->last;
      int size_1word = 0;
      int size_2words = 0;
      int regno;

1164
      /* Calculate which registers need to be saved & save area size.  */
bernds's avatar
bernds committed
1165 1166 1167 1168 1169
      switch (range)
	{
	default:
	  for (regno = first; regno <= last; regno++)
	    {
dberlin's avatar
dberlin committed
1170
	      if ((df_regs_ever_live_p (regno) && !call_used_regs[regno])
bernds's avatar
bernds committed
1171 1172
		  || (current_function_calls_eh_return
		      && (regno >= FIRST_EH_REGNUM && regno <= LAST_EH_REGNUM))
1173 1174
		  || (!TARGET_FDPIC && flag_pic
		      && cfun->uses_pic_offset_table && regno == PIC_REGNO))
bernds's avatar
bernds committed
1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187
		{
		  info_ptr->save_p[regno] = REG_SAVE_1WORD;
		  size_1word += UNITS_PER_WORD;
		}
	    }
	  break;

	  /* Calculate whether we need to create a frame after everything else
             has been processed.  */
	case STACK_REGS_FP:
	  break;

	case STACK_REGS_LR:
dberlin's avatar
dberlin committed
1188
	  if (df_regs_ever_live_p (LR_REGNO)
bernds's avatar
bernds committed
1189
              || profile_flag
1190 1191 1192 1193 1194
	      /* This is set for __builtin_return_address, etc.  */
	      || cfun->machine->frame_needed
              || (TARGET_LINKED_FP && frame_pointer_needed)
              || (!TARGET_FDPIC && flag_pic
		  && cfun->uses_pic_offset_table))
bernds's avatar
bernds committed
1195 1196 1197 1198 1199 1200 1201 1202 1203
	    {
	      info_ptr->save_p[LR_REGNO] = REG_SAVE_1WORD;
	      size_1word += UNITS_PER_WORD;
	    }
	  break;

	case STACK_REGS_STDARG:
	  if (varargs_p)
	    {
1204 1205 1206
	      /* If this is a stdarg function with a non varardic
		 argument split between registers and the stack,
		 adjust the saved registers downward.  */
bernds's avatar
bernds committed
1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222
	      last -= (ADDR_ALIGN (cfun->pretend_args_size, UNITS_PER_WORD)
		       / UNITS_PER_WORD);

	      for (regno = first; regno <= last; regno++)
		{
		  info_ptr->save_p[regno] = REG_SAVE_1WORD;
		  size_1word += UNITS_PER_WORD;
		}

	      info_ptr->stdarg_size = size_1word;
	    }
	  break;

	case STACK_REGS_STRUCT:
	  if (cfun->returns_struct)
	    {
1223
	      info_ptr->save_p[FRV_STRUCT_VALUE_REGNUM] = REG_SAVE_1WORD;
bernds's avatar
bernds committed
1224 1225 1226 1227 1228 1229 1230 1231
	      size_1word += UNITS_PER_WORD;
	    }
	  break;
	}


      if (size_1word)
	{
1232
	  /* If this is a field, it only takes one word.  */
bernds's avatar
bernds committed
1233 1234 1235
	  if (reg_ptr->field_p)
	    size_1word = UNITS_PER_WORD;

1236
	  /* Determine which register pairs can be saved together.  */
bernds's avatar
bernds committed
1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287
	  else if (reg_ptr->dword_p && TARGET_DWORD)
	    {
	      for (regno = first; regno < last; regno += 2)
		{
		  if (info_ptr->save_p[regno] && info_ptr->save_p[regno+1])
		    {
		      size_2words += 2 * UNITS_PER_WORD;
		      size_1word -= 2 * UNITS_PER_WORD;
		      info_ptr->save_p[regno] = REG_SAVE_2WORDS;
		      info_ptr->save_p[regno+1] = REG_SAVE_NO_SAVE;
		    }
		}
	    }

	  reg_ptr->size_1word = size_1word;
	  reg_ptr->size_2words = size_2words;

	  if (! reg_ptr->special_p)
	    {
	      info_ptr->regs_size_1word += size_1word;
	      info_ptr->regs_size_2words += size_2words;
	    }
	}
    }

  /* Set up the sizes of each each field in the frame body, making the sizes
     of each be divisible by the size of a dword if dword operations might
     be used, or the size of a word otherwise.  */
  alignment = (TARGET_DWORD? 2 * UNITS_PER_WORD : UNITS_PER_WORD);

  info_ptr->parameter_size = ADDR_ALIGN (cfun->outgoing_args_size, alignment);
  info_ptr->regs_size = ADDR_ALIGN (info_ptr->regs_size_2words
				    + info_ptr->regs_size_1word,
				    alignment);
  info_ptr->vars_size = ADDR_ALIGN (get_frame_size (), alignment);

  info_ptr->pretend_size = cfun->pretend_args_size;

  /* Work out the size of the frame, excluding the header.  Both the frame
     body and register parameter area will be dword-aligned.  */
  info_ptr->total_size
    = (ADDR_ALIGN (info_ptr->parameter_size
		   + info_ptr->regs_size
		   + info_ptr->vars_size,
		   2 * UNITS_PER_WORD)
       + ADDR_ALIGN (info_ptr->pretend_size
		     + info_ptr->stdarg_size,
		     2 * UNITS_PER_WORD));

  /* See if we need to create a frame at all, if so add header area.  */
  if (info_ptr->total_size  > 0
1288
      || frame_pointer_needed
bernds's avatar
bernds committed
1289 1290 1291 1292 1293 1294 1295
      || info_ptr->regs[STACK_REGS_LR].size_1word > 0
      || info_ptr->regs[STACK_REGS_STRUCT].size_1word > 0)
    {
      offset = info_ptr->parameter_size;
      info_ptr->header_size = 4 * UNITS_PER_WORD;
      info_ptr->total_size += 4 * UNITS_PER_WORD;

1296
      /* Calculate the offsets to save normal register pairs.  */
bernds's avatar
bernds committed
1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317
      for (range = 0; range < STACK_REGS_MAX; range++)
	{
	  frv_stack_regs_t *reg_ptr = &(info_ptr->regs[range]);
	  if (! reg_ptr->special_p)
	    {
	      int first = reg_ptr->first;
	      int last = reg_ptr->last;
	      int regno;

	      for (regno = first; regno <= last; regno++)
		if (info_ptr->save_p[regno] == REG_SAVE_2WORDS
		    && regno != FRAME_POINTER_REGNUM
		    && (regno < FIRST_ARG_REGNUM
			|| regno > LAST_ARG_REGNUM))
		  {
		    info_ptr->reg_offset[regno] = offset;
		    offset += 2 * UNITS_PER_WORD;
		  }
	    }
	}

1318
      /* Calculate the offsets to save normal single registers.  */
bernds's avatar
bernds committed
1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361
      for (range = 0; range < STACK_REGS_MAX; range++)
	{
	  frv_stack_regs_t *reg_ptr = &(info_ptr->regs[range]);
	  if (! reg_ptr->special_p)
	    {
	      int first = reg_ptr->first;
	      int last = reg_ptr->last;
	      int regno;

	      for (regno = first; regno <= last; regno++)
		if (info_ptr->save_p[regno] == REG_SAVE_1WORD
		    && regno != FRAME_POINTER_REGNUM
		    && (regno < FIRST_ARG_REGNUM
			|| regno > LAST_ARG_REGNUM))
		  {
		    info_ptr->reg_offset[regno] = offset;
		    offset += UNITS_PER_WORD;
		  }
	    }
	}

      /* Calculate the offset to save the local variables at.  */
      offset = ADDR_ALIGN (offset, alignment);
      if (info_ptr->vars_size)
	{
	  info_ptr->vars_offset = offset;
	  offset += info_ptr->vars_size;
	}

      /* Align header to a dword-boundary.  */
      offset = ADDR_ALIGN (offset, 2 * UNITS_PER_WORD);

      /* Calculate the offsets in the fixed frame.  */
      info_ptr->save_p[FRAME_POINTER_REGNUM] = REG_SAVE_1WORD;
      info_ptr->reg_offset[FRAME_POINTER_REGNUM] = offset;
      info_ptr->regs[STACK_REGS_FP].size_1word = UNITS_PER_WORD;

      info_ptr->save_p[LR_REGNO] = REG_SAVE_1WORD;
      info_ptr->reg_offset[LR_REGNO] = offset + 2*UNITS_PER_WORD;
      info_ptr->regs[STACK_REGS_LR].size_1word = UNITS_PER_WORD;

      if (cfun->returns_struct)
	{
1362 1363
	  info_ptr->save_p[FRV_STRUCT_VALUE_REGNUM] = REG_SAVE_1WORD;
	  info_ptr->reg_offset[FRV_STRUCT_VALUE_REGNUM] = offset + UNITS_PER_WORD;
bernds's avatar
bernds committed
1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401
	  info_ptr->regs[STACK_REGS_STRUCT].size_1word = UNITS_PER_WORD;
	}

      /* Calculate the offsets to store the arguments passed in registers
         for stdarg functions.  The register pairs are first and the single
         register if any is last.  The register save area starts on a
         dword-boundary.  */
      if (info_ptr->stdarg_size)
	{
	  int first = info_ptr->regs[STACK_REGS_STDARG].first;
	  int last  = info_ptr->regs[STACK_REGS_STDARG].last;
	  int regno;

	  /* Skip the header.  */
	  offset += 4 * UNITS_PER_WORD;
	  for (regno = first; regno <= last; regno++)
	    {
	      if (info_ptr->save_p[regno] == REG_SAVE_2WORDS)
		{
		  info_ptr->reg_offset[regno] = offset;
		  offset += 2 * UNITS_PER_WORD;
		}
	      else if (info_ptr->save_p[regno] == REG_SAVE_1WORD)
		{
		  info_ptr->reg_offset[regno] = offset;
		  offset += UNITS_PER_WORD;
		}
	    }
	}
    }

  if (reload_completed)
    frv_stack_cache = info_ptr;

  return info_ptr;
}


1402
/* Print the information about the frv stack offsets, etc. when debugging.  */
bernds's avatar
bernds committed
1403 1404

void
1405
frv_debug_stack (frv_stack_t *info)
bernds's avatar
bernds committed
1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461
{
  int range;

  if (!info)
    info = frv_stack_info ();

  fprintf (stderr, "\nStack information for function %s:\n",
	   ((current_function_decl && DECL_NAME (current_function_decl))
	    ? IDENTIFIER_POINTER (DECL_NAME (current_function_decl))
	    : "<unknown>"));

  fprintf (stderr, "\ttotal_size\t= %6d\n", info->total_size);
  fprintf (stderr, "\tvars_size\t= %6d\n", info->vars_size);
  fprintf (stderr, "\tparam_size\t= %6d\n", info->parameter_size);
  fprintf (stderr, "\tregs_size\t= %6d, 1w = %3d, 2w = %3d\n",
	   info->regs_size, info->regs_size_1word, info->regs_size_2words);

  fprintf (stderr, "\theader_size\t= %6d\n", info->header_size);
  fprintf (stderr, "\tpretend_size\t= %6d\n", info->pretend_size);
  fprintf (stderr, "\tvars_offset\t= %6d\n", info->vars_offset);
  fprintf (stderr, "\tregs_offset\t= %6d\n", info->regs_offset);

  for (range = 0; range < STACK_REGS_MAX; range++)
    {
      frv_stack_regs_t *regs = &(info->regs[range]);
      if ((regs->size_1word + regs->size_2words) > 0)
	{
	  int first = regs->first;
	  int last  = regs->last;
	  int regno;

	  fprintf (stderr, "\t%s\tsize\t= %6d, 1w = %3d, 2w = %3d, save =",
		   regs->name, regs->size_1word + regs->size_2words,
		   regs->size_1word, regs->size_2words);

	  for (regno = first; regno <= last; regno++)
	    {
	      if (info->save_p[regno] == REG_SAVE_1WORD)
		fprintf (stderr, " %s (%d)", reg_names[regno],
			 info->reg_offset[regno]);

	      else if (info->save_p[regno] == REG_SAVE_2WORDS)
		fprintf (stderr, " %s-%s (%d)", reg_names[regno],
			 reg_names[regno+1], info->reg_offset[regno]);
	    }

	  fputc ('\n', stderr);
	}
    }

  fflush (stderr);
}




1462 1463 1464
/* Used during final to control the packing of insns.  The value is
   1 if the current instruction should be packed with the next one,
   0 if it shouldn't or -1 if packing is disabled altogether.  */
bernds's avatar
bernds committed
1465 1466 1467 1468 1469 1470

static int frv_insn_packing_flag;

/* True if the current function contains a far jump.  */

static int
1471
frv_function_contains_far_jump (void)
bernds's avatar
bernds committed
1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487
{
  rtx insn = get_insns ();
  while (insn != NULL
	 && !(GET_CODE (insn) == JUMP_INSN
	      /* Ignore tablejump patterns.  */
	      && GET_CODE (PATTERN (insn)) != ADDR_VEC
	      && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC
	      && get_attr_far_jump (insn) == FAR_JUMP_YES))
    insn = NEXT_INSN (insn);
  return (insn != NULL);
}

/* For the FRV, this function makes sure that a function with far jumps
   will return correctly.  It also does the VLIW packing.  */

static void
1488
frv_function_prologue (FILE *file, HOST_WIDE_INT size ATTRIBUTE_UNUSED)
bernds's avatar
bernds committed
1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500
{
  /* If no frame was created, check whether the function uses a call
     instruction to implement a far jump.  If so, save the link in gr3 and
     replace all returns to LR with returns to GR3.  GR3 is used because it
     is call-clobbered, because is not available to the register allocator,
     and because all functions that take a hidden argument pointer will have
     a stack frame.  */
  if (frv_stack_info ()->total_size == 0 && frv_function_contains_far_jump ())
    {
      rtx insn;

      /* Just to check that the above comment is true.  */
dberlin's avatar
dberlin committed
1501
      gcc_assert (!df_regs_ever_live_p (GPR_FIRST + 3));
bernds's avatar
bernds committed
1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520

      /* Generate the instruction that saves the link register.  */
      fprintf (file, "\tmovsg lr,gr3\n");

      /* Replace the LR with GR3 in *return_internal patterns.  The insn
	 will now return using jmpl @(gr3,0) rather than bralr.  We cannot
	 simply emit a different assembly directive because bralr and jmpl
	 execute in different units.  */
      for (insn = get_insns(); insn != NULL; insn = NEXT_INSN (insn))
	if (GET_CODE (insn) == JUMP_INSN)
	  {
	    rtx pattern = PATTERN (insn);
	    if (GET_CODE (pattern) == PARALLEL
		&& XVECLEN (pattern, 0) >= 2
		&& GET_CODE (XVECEXP (pattern, 0, 0)) == RETURN
		&& GET_CODE (XVECEXP (pattern, 0, 1)) == USE)
	      {
		rtx address = XEXP (XVECEXP (pattern, 0, 1), 0);
		if (GET_CODE (address) == REG && REGNO (address) == LR_REGNO)
dberlin's avatar
dberlin committed
1521
		  SET_REGNO (address, GPR_FIRST + 3);
bernds's avatar
bernds committed
1522 1523 1524 1525 1526
	      }
	  }
    }

  frv_pack_insns ();
1527 1528 1529

  /* Allow the garbage collector to free the nops created by frv_reorg.  */
  memset (frv_nops, 0, sizeof (frv_nops));
bernds's avatar
bernds committed
1530 1531 1532 1533 1534 1535
}


/* Return the next available temporary register in a given class.  */

static rtx
1536 1537 1538 1539 1540 1541
frv_alloc_temp_reg (
     frv_tmp_reg_t *info,	/* which registers are available */
     enum reg_class class,	/* register class desired */
     enum machine_mode mode,	/* mode to allocate register with */
     int mark_as_used,		/* register not available after allocation */
     int no_abort)		/* return NULL instead of aborting */
bernds's avatar
bernds committed
1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557
{
  int regno = info->next_reg[ (int)class ];
  int orig_regno = regno;
  HARD_REG_SET *reg_in_class = &reg_class_contents[ (int)class ];
  int i, nr;

  for (;;)
    {
      if (TEST_HARD_REG_BIT (*reg_in_class, regno)
	  && TEST_HARD_REG_BIT (info->regs, regno))
	  break;

      if (++regno >= FIRST_PSEUDO_REGISTER)
	regno = 0;
      if (regno == orig_regno)
	{
1558 1559
	  gcc_assert (no_abort);
	  return NULL_RTX;
bernds's avatar
bernds committed
1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580
	}
    }

  nr = HARD_REGNO_NREGS (regno, mode);
  info->next_reg[ (int)class ] = regno + nr;

  if (mark_as_used)
    for (i = 0; i < nr; i++)
      CLEAR_HARD_REG_BIT (info->regs, regno+i);

  return gen_rtx_REG (mode, regno);
}


/* Return an rtx with the value OFFSET, which will either be a register or a
   signed 12-bit integer.  It can be used as the second operand in an "add"
   instruction, or as the index in a load or store.

   The function returns a constant rtx if OFFSET is small enough, otherwise
   it loads the constant into register OFFSET_REGNO and returns that.  */
static rtx
1581
frv_frame_offset_rtx (int offset)
bernds's avatar
bernds committed
1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602
{
  rtx offset_rtx = GEN_INT (offset);
  if (IN_RANGE_P (offset, -2048, 2047))
    return offset_rtx;
  else
    {
      rtx reg_rtx = gen_rtx_REG (SImode, OFFSET_REGNO);
      if (IN_RANGE_P (offset, -32768, 32767))
	emit_insn (gen_movsi (reg_rtx, offset_rtx));
      else
	{
	  emit_insn (gen_movsi_high (reg_rtx, offset_rtx));
	  emit_insn (gen_movsi_lo_sum (reg_rtx, offset_rtx));
	}
      return reg_rtx;
    }
}

/* Generate (mem:MODE (plus:Pmode BASE (frv_frame_offset OFFSET)))).  The
   prologue and epilogue uses such expressions to access the stack.  */
static rtx
1603
frv_frame_mem (enum machine_mode mode, rtx base, int offset)
bernds's avatar
bernds committed
1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619
{
  return gen_rtx_MEM (mode, gen_rtx_PLUS (Pmode,
					  base,
					  frv_frame_offset_rtx (offset)));
}

/* Generate a frame-related expression:

	(set REG (mem (plus (sp) (const_int OFFSET)))).

   Such expressions are used in FRAME_RELATED_EXPR notes for more complex
   instructions.  Marking the expressions as frame-related is superfluous if
   the note contains just a single set.  But if the note contains a PARALLEL
   or SEQUENCE that has several sets, each set must be individually marked
   as frame-related.  */
static rtx
1620
frv_dwarf_store (rtx reg, int offset)
bernds's avatar
bernds committed
1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636
{
  rtx set = gen_rtx_SET (VOIDmode,
			 gen_rtx_MEM (GET_MODE (reg),
				      plus_constant (stack_pointer_rtx,
						     offset)),
			 reg);
  RTX_FRAME_RELATED_P (set) = 1;
  return set;
}

/* Emit a frame-related instruction whose pattern is PATTERN.  The
   instruction is the last in a sequence that cumulatively performs the
   operation described by DWARF_PATTERN.  The instruction is marked as
   frame-related and has a REG_FRAME_RELATED_EXPR note containing
   DWARF_PATTERN.  */
static void
1637
frv_frame_insn (rtx pattern, rtx dwarf_pattern)
bernds's avatar
bernds committed
1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655
{
  rtx insn = emit_insn (pattern);
  RTX_FRAME_RELATED_P (insn) = 1;
  REG_NOTES (insn) = alloc_EXPR_LIST (REG_FRAME_RELATED_EXPR,
				      dwarf_pattern,
				      REG_NOTES (insn));
}

/* Emit instructions that transfer REG to or from the memory location (sp +
   STACK_OFFSET).  The register is stored in memory if ACCESSOR->OP is
   FRV_STORE and loaded if it is FRV_LOAD.  Only the prologue uses this
   function to store registers and only the epilogue uses it to load them.

   The caller sets up ACCESSOR so that BASE is equal to (sp + BASE_OFFSET).
   The generated instruction will use BASE as its base register.  BASE may
   simply be the stack pointer, but if several accesses are being made to a
   region far away from the stack pointer, it may be more efficient to set
   up a temporary instead.
1656

bernds's avatar
bernds committed
1657 1658 1659 1660 1661 1662 1663
   Store instructions will be frame-related and will be annotated with the
   overall effect of the store.  Load instructions will be followed by a
   (use) to prevent later optimizations from zapping them.

   The function takes care of the moves to and from SPRs, using TEMP_REGNO
   as a temporary in such cases.  */
static void
1664
frv_frame_access (frv_frame_accessor_t *accessor, rtx reg, int stack_offset)
bernds's avatar
bernds committed
1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714
{
  enum machine_mode mode = GET_MODE (reg);
  rtx mem = frv_frame_mem (mode,
			   accessor->base,
			   stack_offset - accessor->base_offset);

  if (accessor->op == FRV_LOAD)
    {
      if (SPR_P (REGNO (reg)))
	{
	  rtx temp = gen_rtx_REG (mode, TEMP_REGNO);
	  emit_insn (gen_rtx_SET (VOIDmode, temp, mem));
	  emit_insn (gen_rtx_SET (VOIDmode, reg, temp));
	}
      else
	emit_insn (gen_rtx_SET (VOIDmode, reg, mem));
      emit_insn (gen_rtx_USE (VOIDmode, reg));
    }
  else
    {
      if (SPR_P (REGNO (reg)))
	{
	  rtx temp = gen_rtx_REG (mode, TEMP_REGNO);
	  emit_insn (gen_rtx_SET (VOIDmode, temp, reg));
	  frv_frame_insn (gen_rtx_SET (Pmode, mem, temp),
			  frv_dwarf_store (reg, stack_offset));
	}
      else if (GET_MODE (reg) == DImode)
	{
	  /* For DImode saves, the dwarf2 version needs to be a SEQUENCE
	     with a separate save for each register.  */
	  rtx reg1 = gen_rtx_REG (SImode, REGNO (reg));
	  rtx reg2 = gen_rtx_REG (SImode, REGNO (reg) + 1);
	  rtx set1 = frv_dwarf_store (reg1, stack_offset);
	  rtx set2 = frv_dwarf_store (reg2, stack_offset + 4);
	  frv_frame_insn (gen_rtx_SET (Pmode, mem, reg),
			  gen_rtx_PARALLEL (VOIDmode,
					    gen_rtvec (2, set1, set2)));
	}
      else
	frv_frame_insn (gen_rtx_SET (Pmode, mem, reg),
			frv_dwarf_store (reg, stack_offset));
    }
}

/* A function that uses frv_frame_access to transfer a group of registers to
   or from the stack.  ACCESSOR is passed directly to frv_frame_access, INFO
   is the stack information generated by frv_stack_info, and REG_SET is the
   number of the register set to transfer.  */
static void
1715 1716 1717
frv_frame_access_multi (frv_frame_accessor_t *accessor,
                        frv_stack_t *info,
                        int reg_set)
bernds's avatar
bernds committed
1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736
{
  frv_stack_regs_t *regs_info;
  int regno;

  regs_info = &info->regs[reg_set];
  for (regno = regs_info->first; regno <= regs_info->last; regno++)
    if (info->save_p[regno])
      frv_frame_access (accessor,
			info->save_p[regno] == REG_SAVE_2WORDS
			? gen_rtx_REG (DImode, regno)
			: gen_rtx_REG (SImode, regno),
			info->reg_offset[regno]);
}

/* Save or restore callee-saved registers that are kept outside the frame
   header.  The function saves the registers if OP is FRV_STORE and restores
   them if OP is FRV_LOAD.  INFO is the stack information generated by
   frv_stack_info.  */
static void
1737
frv_frame_access_standard_regs (enum frv_stack_op op, frv_stack_t *info)
bernds's avatar
bernds committed
1738 1739 1740 1741 1742 1743 1744 1745 1746
{
  frv_frame_accessor_t accessor;

  accessor.op = op;
  accessor.base = stack_pointer_rtx;
  accessor.base_offset = 0;
  frv_frame_access_multi (&accessor, info, STACK_REGS_GPR);
  frv_frame_access_multi (&accessor, info, STACK_REGS_FPR);
  frv_frame_access_multi (&accessor, info, STACK_REGS_LCR);
1747
}
bernds's avatar
bernds committed
1748 1749 1750 1751


/* Called after register allocation to add any instructions needed for the
   prologue.  Using a prologue insn is favored compared to putting all of the
1752 1753 1754 1755 1756
   instructions in the TARGET_ASM_FUNCTION_PROLOGUE target hook, since
   it allows the scheduler to intermix instructions with the saves of
   the caller saved registers.  In some cases, it might be necessary
   to emit a barrier instruction as the last insn to prevent such
   scheduling.
bernds's avatar
bernds committed
1757 1758 1759 1760

   Also any insns generated here should have RTX_FRAME_RELATED_P(insn) = 1
   so that the debug info generation code can handle them properly.  */
void
1761
frv_expand_prologue (void)
bernds's avatar
bernds committed
1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861
{
  frv_stack_t *info = frv_stack_info ();
  rtx sp = stack_pointer_rtx;
  rtx fp = frame_pointer_rtx;
  frv_frame_accessor_t accessor;

  if (TARGET_DEBUG_STACK)
    frv_debug_stack (info);

  if (info->total_size == 0)
    return;

  /* We're interested in three areas of the frame here:

         A: the register save area
	 B: the old FP
	 C: the header after B

     If the frame pointer isn't used, we'll have to set up A, B and C
     using the stack pointer.  If the frame pointer is used, we'll access
     them as follows:

         A: set up using sp
	 B: set up using sp or a temporary (see below)
	 C: set up using fp

     We set up B using the stack pointer if the frame is small enough.
     Otherwise, it's more efficient to copy the old stack pointer into a
     temporary and use that.

     Note that it's important to make sure the prologue and epilogue use the
     same registers to access A and C, since doing otherwise will confuse
     the aliasing code.  */

  /* Set up ACCESSOR for accessing region B above.  If the frame pointer
     isn't used, the same method will serve for C.  */
  accessor.op = FRV_STORE;
  if (frame_pointer_needed && info->total_size > 2048)
    {
      rtx insn;

      accessor.base = gen_rtx_REG (Pmode, OLD_SP_REGNO);
      accessor.base_offset = info->total_size;
      insn = emit_insn (gen_movsi (accessor.base, sp));
    }
  else
    {
      accessor.base = stack_pointer_rtx;
      accessor.base_offset = 0;
    }

  /* Allocate the stack space.  */
  {
    rtx asm_offset = frv_frame_offset_rtx (-info->total_size);
    rtx dwarf_offset = GEN_INT (-info->total_size);

    frv_frame_insn (gen_stack_adjust (sp, sp, asm_offset),
		    gen_rtx_SET (Pmode,
				 sp,
				 gen_rtx_PLUS (Pmode, sp, dwarf_offset)));
  }

  /* If the frame pointer is needed, store the old one at (sp + FP_OFFSET)
     and point the new one to that location.  */
  if (frame_pointer_needed)
    {
      int fp_offset = info->reg_offset[FRAME_POINTER_REGNUM];

      /* ASM_SRC and DWARF_SRC both point to the frame header.  ASM_SRC is
	 based on ACCESSOR.BASE but DWARF_SRC is always based on the stack
	 pointer.  */
      rtx asm_src = plus_constant (accessor.base,
				   fp_offset - accessor.base_offset);
      rtx dwarf_src = plus_constant (sp, fp_offset);

      /* Store the old frame pointer at (sp + FP_OFFSET).  */
      frv_frame_access (&accessor, fp, fp_offset);

      /* Set up the new frame pointer.  */
      frv_frame_insn (gen_rtx_SET (VOIDmode, fp, asm_src),
		      gen_rtx_SET (VOIDmode, fp, dwarf_src));

      /* Access region C from the frame pointer.  */
      accessor.base = fp;
      accessor.base_offset = fp_offset;
    }

  /* Set up region C.  */
  frv_frame_access_multi (&accessor, info, STACK_REGS_STRUCT);
  frv_frame_access_multi (&accessor, info, STACK_REGS_LR);
  frv_frame_access_multi (&accessor, info, STACK_REGS_STDARG);

  /* Set up region A.  */
  frv_frame_access_standard_regs (FRV_STORE, info);

  /* If this is a varargs/stdarg function, issue a blockage to prevent the
     scheduler from moving loads before the stores saving the registers.  */
  if (info->stdarg_size > 0)
    emit_insn (gen_blockage ());

1862
  /* Set up pic register/small data register for this function.  */
1863
  if (!TARGET_FDPIC && flag_pic && cfun->uses_pic_offset_table)
bernds's avatar
bernds committed
1864 1865 1866 1867 1868 1869 1870
    emit_insn (gen_pic_prologue (gen_rtx_REG (Pmode, PIC_REGNO),
				 gen_rtx_REG (Pmode, LR_REGNO),
				 gen_rtx_REG (SImode, OFFSET_REGNO)));
}


/* Under frv, all of the work is done via frv_expand_epilogue, but
kazu's avatar
kazu committed
1871
   this function provides a convenient place to do cleanup.  */
bernds's avatar
bernds committed
1872 1873

static void
1874 1875
frv_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
                       HOST_WIDE_INT size ATTRIBUTE_UNUSED)
bernds's avatar
bernds committed
1876 1877 1878
{
  frv_stack_cache = (frv_stack_t *)0;

1879
  /* Zap last used registers for conditional execution.  */
ghazi's avatar
cp:  
ghazi committed
1880
  memset (&frv_ifcvt.tmp_reg, 0, sizeof (frv_ifcvt.tmp_reg));
bernds's avatar
bernds committed
1881

1882
  /* Release the bitmap of created insns.  */
1883
  BITMAP_FREE (frv_ifcvt.scratch_insns_bitmap);
bernds's avatar
bernds committed
1884 1885 1886 1887
}


/* Called after register allocation to add any instructions needed for the
kazu's avatar
kazu committed
1888
   epilogue.  Using an epilogue insn is favored compared to putting all of the
1889 1890 1891 1892
   instructions in the TARGET_ASM_FUNCTION_PROLOGUE target hook, since
   it allows the scheduler to intermix instructions with the saves of
   the caller saved registers.  In some cases, it might be necessary
   to emit a barrier instruction as the last insn to prevent such
1893
   scheduling.  */
bernds's avatar
bernds committed
1894 1895

void
1896
frv_expand_epilogue (bool emit_return)
bernds's avatar
bernds committed
1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915
{
  frv_stack_t *info = frv_stack_info ();
  rtx fp = frame_pointer_rtx;
  rtx sp = stack_pointer_rtx;
  rtx return_addr;
  int fp_offset;

  fp_offset = info->reg_offset[FRAME_POINTER_REGNUM];

  /* Restore the stack pointer to its original value if alloca or the like
     is used.  */
  if (! current_function_sp_is_unchanging)
    emit_insn (gen_addsi3 (sp, fp, frv_frame_offset_rtx (-fp_offset)));

  /* Restore the callee-saved registers that were used in this function.  */
  frv_frame_access_standard_regs (FRV_LOAD, info);

  /* Set RETURN_ADDR to the address we should return to.  Set it to NULL if
     no return instruction should be emitted.  */
1916
  if (info->save_p[LR_REGNO])
bernds's avatar
bernds committed
1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958
    {
      int lr_offset;
      rtx mem;

      /* Use the same method to access the link register's slot as we did in
	 the prologue.  In other words, use the frame pointer if available,
	 otherwise use the stack pointer.

	 LR_OFFSET is the offset of the link register's slot from the start
	 of the frame and MEM is a memory rtx for it.  */
      lr_offset = info->reg_offset[LR_REGNO];
      if (frame_pointer_needed)
	mem = frv_frame_mem (Pmode, fp, lr_offset - fp_offset);
      else
	mem = frv_frame_mem (Pmode, sp, lr_offset);

      /* Load the old link register into a GPR.  */
      return_addr = gen_rtx_REG (Pmode, TEMP_REGNO);
      emit_insn (gen_rtx_SET (VOIDmode, return_addr, mem));
    }
  else
    return_addr = gen_rtx_REG (Pmode, LR_REGNO);

  /* Restore the old frame pointer.  Emit a USE afterwards to make sure
     the load is preserved.  */
  if (frame_pointer_needed)
    {
      emit_insn (gen_rtx_SET (VOIDmode, fp, gen_rtx_MEM (Pmode, fp)));
      emit_insn (gen_rtx_USE (VOIDmode, fp));
    }

  /* Deallocate the stack frame.  */
  if (info->total_size != 0)
    {
      rtx offset = frv_frame_offset_rtx (info->total_size);
      emit_insn (gen_stack_adjust (sp, sp, offset));
    }

  /* If this function uses eh_return, add the final stack adjustment now.  */
  if (current_function_calls_eh_return)
    emit_insn (gen_stack_adjust (sp, sp, EH_RETURN_STACKADJ_RTX));

1959
  if (emit_return)
bernds's avatar
bernds committed
1960
    emit_jump_insn (gen_epilogue_return (return_addr));
1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972
  else
    {
      rtx lr = return_addr;

      if (REGNO (return_addr) != LR_REGNO)
	{
	  lr = gen_rtx_REG (Pmode, LR_REGNO);
	  emit_move_insn (lr, return_addr);
	}

      emit_insn (gen_rtx_USE (VOIDmode, lr));
    }
bernds's avatar
bernds committed
1973 1974 1975
}


1976
/* Worker function for TARGET_ASM_OUTPUT_MI_THUNK.  */
bernds's avatar
bernds committed
1977

1978
static void
1979 1980 1981 1982 1983
frv_asm_output_mi_thunk (FILE *file,
                         tree thunk_fndecl ATTRIBUTE_UNUSED,
                         HOST_WIDE_INT delta,
                         HOST_WIDE_INT vcall_offset ATTRIBUTE_UNUSED,
                         tree function)
bernds's avatar
bernds committed
1984 1985 1986 1987
{
  const char *name_func = XSTR (XEXP (DECL_RTL (function), 0), 0);
  const char *name_arg0 = reg_names[FIRST_ARG_REGNUM];
  const char *name_jmp = reg_names[JUMP_REGNO];
1988
  const char *parallel = (frv_issue_rate () > 1 ? ".p" : "");
bernds's avatar
bernds committed
1989

1990
  /* Do the add using an addi if possible.  */
bernds's avatar
bernds committed
1991
  if (IN_RANGE_P (delta, -2048, 2047))
1992
    fprintf (file, "\taddi %s,#%d,%s\n", name_arg0, (int) delta, name_arg0);
bernds's avatar
bernds committed
1993 1994
  else
    {
1995 1996 1997 1998 1999
      const char *const name_add = reg_names[TEMP_REGNO];
      fprintf (file, "\tsethi%s #hi(" HOST_WIDE_INT_PRINT_DEC "),%s\n",
	       parallel, delta, name_add);
      fprintf (file, "\tsetlo #lo(" HOST_WIDE_INT_PRINT_DEC "),%s\n",
	       delta, name_add);
bernds's avatar
bernds committed
2000 2001 2002
      fprintf (file, "\tadd %s,%s,%s\n", name_add, name_arg0, name_arg0);
    }

2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027
  if (TARGET_FDPIC)
    {
      const char *name_pic = reg_names[FDPIC_REGNO];
      name_jmp = reg_names[FDPIC_FPTR_REGNO];

      if (flag_pic != 1)
	{
	  fprintf (file, "\tsethi%s #gotofffuncdeschi(", parallel);
	  assemble_name (file, name_func);
	  fprintf (file, "),%s\n", name_jmp);

	  fprintf (file, "\tsetlo #gotofffuncdesclo(");
	  assemble_name (file, name_func);
	  fprintf (file, "),%s\n", name_jmp);

	  fprintf (file, "\tldd @(%s,%s), %s\n", name_jmp, name_pic, name_jmp);
	}
      else
	{
	  fprintf (file, "\tlddo @(%s,#gotofffuncdesc12(", name_pic);
	  assemble_name (file, name_func);
	  fprintf (file, "\t)), %s\n", name_jmp);
	}
    }
  else if (!flag_pic)
bernds's avatar
bernds committed
2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062
    {
      fprintf (file, "\tsethi%s #hi(", parallel);
      assemble_name (file, name_func);
      fprintf (file, "),%s\n", name_jmp);

      fprintf (file, "\tsetlo #lo(");
      assemble_name (file, name_func);
      fprintf (file, "),%s\n", name_jmp);
    }
  else
    {
      /* Use JUMP_REGNO as a temporary PIC register.  */
      const char *name_lr = reg_names[LR_REGNO];
      const char *name_gppic = name_jmp;
      const char *name_tmp = reg_names[TEMP_REGNO];

      fprintf (file, "\tmovsg %s,%s\n", name_lr, name_tmp);
      fprintf (file, "\tcall 1f\n");
      fprintf (file, "1:\tmovsg %s,%s\n", name_lr, name_gppic);
      fprintf (file, "\tmovgs %s,%s\n", name_tmp, name_lr);
      fprintf (file, "\tsethi%s #gprelhi(1b),%s\n", parallel, name_tmp);
      fprintf (file, "\tsetlo #gprello(1b),%s\n", name_tmp);
      fprintf (file, "\tsub %s,%s,%s\n", name_gppic, name_tmp, name_gppic);

      fprintf (file, "\tsethi%s #gprelhi(", parallel);
      assemble_name (file, name_func);
      fprintf (file, "),%s\n", name_tmp);

      fprintf (file, "\tsetlo #gprello(");
      assemble_name (file, name_func);
      fprintf (file, "),%s\n", name_tmp);

      fprintf (file, "\tadd %s,%s,%s\n", name_gppic, name_tmp, name_jmp);
    }

2063
  /* Jump to the function address.  */
bernds's avatar
bernds committed
2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086
  fprintf (file, "\tjmpl @(%s,%s)\n", name_jmp, reg_names[GPR_FIRST+0]);
}


/* A C expression which is nonzero if a function must have and use a frame
   pointer.  This expression is evaluated in the reload pass.  If its value is
   nonzero the function will have a frame pointer.

   The expression can in principle examine the current function and decide
   according to the facts, but on most machines the constant 0 or the constant
   1 suffices.  Use 0 when the machine allows code to be generated with no
   frame pointer, and doing so saves some time or space.  Use 1 when there is
   no possible advantage to avoiding a frame pointer.

   In certain cases, the compiler does not know how to produce valid code
   without a frame pointer.  The compiler recognizes those cases and
   automatically gives the function a frame pointer regardless of what
   `FRAME_POINTER_REQUIRED' says.  You don't need to worry about them.

   In a function that does not require a frame pointer, the frame pointer
   register can be allocated for ordinary usage, unless you mark it as a fixed
   register.  See `FIXED_REGISTERS' for more information.  */

2087
/* On frv, create a frame whenever we need to create stack.  */
bernds's avatar
bernds committed
2088 2089

int
2090
frv_frame_pointer_required (void)
bernds's avatar
bernds committed
2091
{
2092 2093 2094 2095 2096
  /* If we forgoing the usual linkage requirements, we only need
     a frame pointer if the stack pointer might change.  */
  if (!TARGET_LINKED_FP)
    return !current_function_sp_is_unchanging;

bernds's avatar
bernds committed
2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108
  if (! current_function_is_leaf)
    return TRUE;

  if (get_frame_size () != 0)
    return TRUE;

  if (cfun->stdarg)
    return TRUE;

  if (!current_function_sp_is_unchanging)
    return TRUE;

2109
  if (!TARGET_FDPIC && flag_pic && cfun->uses_pic_offset_table)
bernds's avatar
bernds committed
2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128
    return TRUE;

  if (profile_flag)
    return TRUE;

  if (cfun->machine->frame_needed)
    return TRUE;

  return FALSE;
}


/* This macro is similar to `INITIAL_FRAME_POINTER_OFFSET'.  It specifies the
   initial difference between the specified pair of registers.  This macro must
   be defined if `ELIMINABLE_REGS' is defined.  */

/* See frv_stack_info for more details on the frv stack frame.  */

int
2129
frv_initial_elimination_offset (int from, int to)
bernds's avatar
bernds committed
2130 2131 2132 2133 2134 2135 2136 2137
{
  frv_stack_t *info = frv_stack_info ();
  int ret = 0;

  if (to == STACK_POINTER_REGNUM && from == ARG_POINTER_REGNUM)
    ret = info->total_size - info->pretend_size;

  else if (to == STACK_POINTER_REGNUM && from == FRAME_POINTER_REGNUM)
2138
    ret = info->reg_offset[FRAME_POINTER_REGNUM];
bernds's avatar
bernds committed
2139 2140 2141 2142 2143 2144 2145

  else if (to == FRAME_POINTER_REGNUM && from == ARG_POINTER_REGNUM)
    ret = (info->total_size
	   - info->reg_offset[FRAME_POINTER_REGNUM]
	   - info->pretend_size);

  else
2146
    gcc_unreachable ();
bernds's avatar
bernds committed
2147 2148 2149 2150 2151 2152 2153 2154 2155

  if (TARGET_DEBUG_STACK)
    fprintf (stderr, "Eliminate %s to %s by adding %d\n",
	     reg_names [from], reg_names[to], ret);

  return ret;
}


2156
/* Worker function for TARGET_SETUP_INCOMING_VARARGS.  */
bernds's avatar
bernds committed
2157

2158
static void
2159 2160 2161 2162 2163
frv_setup_incoming_varargs (CUMULATIVE_ARGS *cum,
                            enum machine_mode mode,
                            tree type ATTRIBUTE_UNUSED,
                            int *pretend_size,
                            int second_time)
bernds's avatar
bernds committed
2164 2165 2166 2167 2168 2169 2170 2171
{
  if (TARGET_DEBUG_ARG)
    fprintf (stderr,
	     "setup_vararg: words = %2d, mode = %4s, pretend_size = %d, second_time = %d\n",
	     *cum, GET_MODE_NAME (mode), *pretend_size, second_time);
}


2172
/* Worker function for TARGET_EXPAND_BUILTIN_SAVEREGS.  */
bernds's avatar
bernds committed
2173

2174
static rtx
2175
frv_expand_builtin_saveregs (void)
bernds's avatar
bernds committed
2176 2177 2178 2179 2180 2181 2182
{
  int offset = UNITS_PER_WORD * FRV_NUM_ARG_REGS;

  if (TARGET_DEBUG_ARG)
    fprintf (stderr, "expand_builtin_saveregs: offset from ap = %d\n",
	     offset);

2183
  return gen_rtx_PLUS (Pmode, virtual_incoming_args_rtx, GEN_INT (- offset));
bernds's avatar
bernds committed
2184 2185 2186 2187 2188 2189
}


/* Expand __builtin_va_start to do the va_start macro.  */

void
2190
frv_expand_builtin_va_start (tree valist, rtx nextarg)
bernds's avatar
bernds committed
2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205
{
  tree t;
  int num = cfun->args_info - FIRST_ARG_REGNUM - FRV_NUM_ARG_REGS;

  nextarg = gen_rtx_PLUS (Pmode, virtual_incoming_args_rtx,
			  GEN_INT (UNITS_PER_WORD * num));

  if (TARGET_DEBUG_ARG)
    {
      fprintf (stderr, "va_start: args_info = %d, num = %d\n",
	       cfun->args_info, num);

      debug_rtx (nextarg);
    }

2206
  t = build2 (GIMPLE_MODIFY_STMT, TREE_TYPE (valist), valist,
2207 2208
	      fold_convert (TREE_TYPE (valist),
			    make_tree (sizetype, nextarg)));
bernds's avatar
bernds committed
2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233
  TREE_SIDE_EFFECTS (t) = 1;

  expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
}


/* Expand a block move operation, and return 1 if successful.  Return 0
   if we should let the compiler generate normal code.

   operands[0] is the destination
   operands[1] is the source
   operands[2] is the length
   operands[3] is the alignment */

/* Maximum number of loads to do before doing the stores */
#ifndef MAX_MOVE_REG
#define MAX_MOVE_REG 4
#endif

/* Maximum number of total loads to do.  */
#ifndef TOTAL_MOVE_REG
#define TOTAL_MOVE_REG 8
#endif

int
2234
frv_expand_block_move (rtx operands[])
bernds's avatar
bernds committed
2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256
{
  rtx orig_dest = operands[0];
  rtx orig_src	= operands[1];
  rtx bytes_rtx	= operands[2];
  rtx align_rtx = operands[3];
  int constp	= (GET_CODE (bytes_rtx) == CONST_INT);
  int align;
  int bytes;
  int offset;
  int num_reg;
  int i;
  rtx src_reg;
  rtx dest_reg;
  rtx src_addr;
  rtx dest_addr;
  rtx src_mem;
  rtx dest_mem;
  rtx tmp_reg;
  rtx stores[MAX_MOVE_REG];
  int move_bytes;
  enum machine_mode mode;

2257
  /* If this is not a fixed size move, just call memcpy.  */
bernds's avatar
bernds committed
2258 2259 2260
  if (! constp)
    return FALSE;

2261 2262
  /* This should be a fixed size alignment.  */
  gcc_assert (GET_CODE (align_rtx) == CONST_INT);
bernds's avatar
bernds committed
2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281

  align = INTVAL (align_rtx);

  /* Anything to move? */
  bytes = INTVAL (bytes_rtx);
  if (bytes <= 0)
    return TRUE;

  /* Don't support real large moves.  */
  if (bytes > TOTAL_MOVE_REG*align)
    return FALSE;

  /* Move the address into scratch registers.  */
  dest_reg = copy_addr_to_reg (XEXP (orig_dest, 0));
  src_reg  = copy_addr_to_reg (XEXP (orig_src,  0));

  num_reg = offset = 0;
  for ( ; bytes > 0; (bytes -= move_bytes), (offset += move_bytes))
    {
2282
      /* Calculate the correct offset for src/dest.  */
bernds's avatar
bernds committed
2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329
      if (offset == 0)
	{
	  src_addr  = src_reg;
	  dest_addr = dest_reg;
	}
      else
	{
	  src_addr = plus_constant (src_reg, offset);
	  dest_addr = plus_constant (dest_reg, offset);
	}

      /* Generate the appropriate load and store, saving the stores
	 for later.  */
      if (bytes >= 4 && align >= 4)
	mode = SImode;
      else if (bytes >= 2 && align >= 2)
	mode = HImode;
      else
	mode = QImode;

      move_bytes = GET_MODE_SIZE (mode);
      tmp_reg = gen_reg_rtx (mode);
      src_mem = change_address (orig_src, mode, src_addr);
      dest_mem = change_address (orig_dest, mode, dest_addr);
      emit_insn (gen_rtx_SET (VOIDmode, tmp_reg, src_mem));
      stores[num_reg++] = gen_rtx_SET (VOIDmode, dest_mem, tmp_reg);

      if (num_reg >= MAX_MOVE_REG)
	{
	  for (i = 0; i < num_reg; i++)
	    emit_insn (stores[i]);
	  num_reg = 0;
	}
    }

  for (i = 0; i < num_reg; i++)
    emit_insn (stores[i]);

  return TRUE;
}


/* Expand a block clear operation, and return 1 if successful.  Return 0
   if we should let the compiler generate normal code.

   operands[0] is the destination
   operands[1] is the length
2330
   operands[3] is the alignment */
bernds's avatar
bernds committed
2331 2332

int
2333
frv_expand_block_clear (rtx operands[])
bernds's avatar
bernds committed
2334 2335 2336
{
  rtx orig_dest = operands[0];
  rtx bytes_rtx	= operands[1];
2337
  rtx align_rtx = operands[3];
bernds's avatar
bernds committed
2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348
  int constp	= (GET_CODE (bytes_rtx) == CONST_INT);
  int align;
  int bytes;
  int offset;
  int num_reg;
  rtx dest_reg;
  rtx dest_addr;
  rtx dest_mem;
  int clear_bytes;
  enum machine_mode mode;

2349
  /* If this is not a fixed size move, just call memcpy.  */
bernds's avatar
bernds committed
2350 2351 2352
  if (! constp)
    return FALSE;

2353 2354
  /* This should be a fixed size alignment.  */
  gcc_assert (GET_CODE (align_rtx) == CONST_INT);
bernds's avatar
bernds committed
2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372

  align = INTVAL (align_rtx);

  /* Anything to move? */
  bytes = INTVAL (bytes_rtx);
  if (bytes <= 0)
    return TRUE;

  /* Don't support real large clears.  */
  if (bytes > TOTAL_MOVE_REG*align)
    return FALSE;

  /* Move the address into a scratch register.  */
  dest_reg = copy_addr_to_reg (XEXP (orig_dest, 0));

  num_reg = offset = 0;
  for ( ; bytes > 0; (bytes -= clear_bytes), (offset += clear_bytes))
    {
2373
      /* Calculate the correct offset for src/dest.  */
bernds's avatar
bernds committed
2374 2375 2376 2377
      dest_addr = ((offset == 0)
		   ? dest_reg
		   : plus_constant (dest_reg, offset));

2378
      /* Generate the appropriate store of gr0.  */
bernds's avatar
bernds committed
2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395
      if (bytes >= 4 && align >= 4)
	mode = SImode;
      else if (bytes >= 2 && align >= 2)
	mode = HImode;
      else
	mode = QImode;

      clear_bytes = GET_MODE_SIZE (mode);
      dest_mem = change_address (orig_dest, mode, dest_addr);
      emit_insn (gen_rtx_SET (VOIDmode, dest_mem, const0_rtx));
    }

  return TRUE;
}


/* The following variable is used to output modifiers of assembler
2396
   code of the current output insn.  */
bernds's avatar
bernds committed
2397 2398 2399 2400

static rtx *frv_insn_operands;

/* The following function is used to add assembler insn code suffix .p
2401
   if it is necessary.  */
bernds's avatar
bernds committed
2402 2403

const char *
2404
frv_asm_output_opcode (FILE *f, const char *ptr)
bernds's avatar
bernds committed
2405 2406 2407
{
  int c;

2408
  if (frv_insn_packing_flag <= 0)
bernds's avatar
bernds committed
2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427
    return ptr;

  for (; *ptr && *ptr != ' ' && *ptr != '\t';)
    {
      c = *ptr++;
      if (c == '%' && ((*ptr >= 'a' && *ptr <= 'z')
		       || (*ptr >= 'A' && *ptr <= 'Z')))
	{
	  int letter = *ptr++;

	  c = atoi (ptr);
	  frv_print_operand (f, frv_insn_operands [c], letter);
	  while ((c = *ptr) >= '0' && c <= '9')
	    ptr++;
	}
      else
	fputc (c, f);
    }

2428
  fprintf (f, ".p");
bernds's avatar
bernds committed
2429 2430 2431 2432

  return ptr;
}

2433 2434
/* Set up the packing bit for the current output insn.  Note that this
   function is not called for asm insns.  */
bernds's avatar
bernds committed
2435 2436

void
2437 2438
frv_final_prescan_insn (rtx insn, rtx *opvec,
			int noperands ATTRIBUTE_UNUSED)
bernds's avatar
bernds committed
2439
{
2440
  if (INSN_P (insn))
bernds's avatar
bernds committed
2441
    {
2442 2443 2444 2445 2446 2447 2448 2449 2450 2451
      if (frv_insn_packing_flag >= 0)
	{
	  frv_insn_operands = opvec;
	  frv_insn_packing_flag = PACKING_FLAG_P (insn);
	}
      else if (recog_memoized (insn) >= 0
	       && get_attr_acc_group (insn) == ACC_GROUP_ODD)
	/* Packing optimizations have been disabled, but INSN can only
	   be issued in M1.  Insert an mnop in M0.  */
	fprintf (asm_out_file, "\tmnop.p\n");
bernds's avatar
bernds committed
2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466
    }
}



/* A C expression whose value is RTL representing the address in a stack frame
   where the pointer to the caller's frame is stored.  Assume that FRAMEADDR is
   an RTL expression for the address of the stack frame itself.

   If you don't define this macro, the default is to return the value of
   FRAMEADDR--that is, the stack frame address is also the address of the stack
   word that points to the previous frame.  */

/* The default is correct, but we need to make sure the frame gets created.  */
rtx
2467
frv_dynamic_chain_address (rtx frame)
bernds's avatar
bernds committed
2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484
{
  cfun->machine->frame_needed = 1;
  return frame;
}


/* A C expression whose value is RTL representing the value of the return
   address for the frame COUNT steps up from the current frame, after the
   prologue.  FRAMEADDR is the frame pointer of the COUNT frame, or the frame
   pointer of the COUNT - 1 frame if `RETURN_ADDR_IN_PREVIOUS_FRAME' is
   defined.

   The value of the expression must always be the correct address when COUNT is
   zero, but may be `NULL_RTX' if there is not way to determine the return
   address of other frames.  */

rtx
2485
frv_return_addr_rtx (int count, rtx frame)
bernds's avatar
bernds committed
2486
{
2487 2488
  if (count != 0)
    return const0_rtx;
bernds's avatar
bernds committed
2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501
  cfun->machine->frame_needed = 1;
  return gen_rtx_MEM (Pmode, plus_constant (frame, 8));
}

/* Given a memory reference MEMREF, interpret the referenced memory as
   an array of MODE values, and return a reference to the element
   specified by INDEX.  Assume that any pre-modification implicit in
   MEMREF has already happened.

   MEMREF must be a legitimate operand for modes larger than SImode.
   GO_IF_LEGITIMATE_ADDRESS forbids register+register addresses, which
   this function cannot handle.  */
rtx
2502
frv_index_memory (rtx memref, enum machine_mode mode, int index)
bernds's avatar
bernds committed
2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513
{
  rtx base = XEXP (memref, 0);
  if (GET_CODE (base) == PRE_MODIFY)
    base = XEXP (base, 0);
  return change_address (memref, mode,
			 plus_constant (base, index * GET_MODE_SIZE (mode)));
}


/* Print a memory address as an operand to reference that memory location.  */
void
2514
frv_print_operand_address (FILE * stream, rtx x)
bernds's avatar
bernds committed
2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541
{
  if (GET_CODE (x) == MEM)
    x = XEXP (x, 0);

  switch (GET_CODE (x))
    {
    case REG:
      fputs (reg_names [ REGNO (x)], stream);
      return;

    case CONST_INT:
      fprintf (stream, "%ld", (long) INTVAL (x));
      return;

    case SYMBOL_REF:
      assemble_name (stream, XSTR (x, 0));
      return;

    case LABEL_REF:
    case CONST:
      output_addr_const (stream, x);
      return;

    default:
      break;
    }

2542
  fatal_insn ("bad insn to frv_print_operand_address:", x);
bernds's avatar
bernds committed
2543 2544 2545 2546
}


static void
2547
frv_print_operand_memory_reference_reg (FILE * stream, rtx x)
bernds's avatar
bernds committed
2548 2549 2550 2551 2552
{
  int regno = true_regnum (x);
  if (GPR_P (regno))
    fputs (reg_names[regno], stream);
  else
2553
    fatal_insn ("bad register to frv_print_operand_memory_reference_reg:", x);
bernds's avatar
bernds committed
2554 2555 2556 2557 2558
}

/* Print a memory reference suitable for the ld/st instructions.  */

static void
2559
frv_print_operand_memory_reference (FILE * stream, rtx x, int addr_offset)
bernds's avatar
bernds committed
2560
{
2561
  struct frv_unspec unspec;
bernds's avatar
bernds committed
2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591
  rtx x0 = NULL_RTX;
  rtx x1 = NULL_RTX;

  switch (GET_CODE (x))
    {
    case SUBREG:
    case REG:
      x0 = x;
      break;

    case PRE_MODIFY:		/* (pre_modify (reg) (plus (reg) (reg))) */
      x0 = XEXP (x, 0);
      x1 = XEXP (XEXP (x, 1), 1);
      break;

    case CONST_INT:
      x1 = x;
      break;

    case PLUS:
      x0 = XEXP (x, 0);
      x1 = XEXP (x, 1);
      if (GET_CODE (x0) == CONST_INT)
	{
	  x0 = XEXP (x, 1);
	  x1 = XEXP (x, 0);
	}
      break;

    default:
2592
      fatal_insn ("bad insn to frv_print_operand_memory_reference:", x);
bernds's avatar
bernds committed
2593 2594 2595 2596 2597 2598 2599 2600 2601
      break;

    }

  if (addr_offset)
    {
      if (!x1)
	x1 = const0_rtx;
      else if (GET_CODE (x1) != CONST_INT)
2602
	fatal_insn ("bad insn to frv_print_operand_memory_reference:", x);
bernds's avatar
bernds committed
2603 2604 2605 2606 2607 2608 2609 2610
    }

  fputs ("@(", stream);
  if (!x0)
    fputs (reg_names[GPR_R0], stream);
  else if (GET_CODE (x0) == REG || GET_CODE (x0) == SUBREG)
    frv_print_operand_memory_reference_reg (stream, x0);
  else
2611
    fatal_insn ("bad insn to frv_print_operand_memory_reference:", x);
bernds's avatar
bernds committed
2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630

  fputs (",", stream);
  if (!x1)
    fputs (reg_names [GPR_R0], stream);

  else
    {
      switch (GET_CODE (x1))
	{
	case SUBREG:
	case REG:
	  frv_print_operand_memory_reference_reg (stream, x1);
	  break;

	case CONST_INT:
	  fprintf (stream, "%ld", (long) (INTVAL (x1) + addr_offset));
	  break;

	case CONST:
2631
	  if (!frv_const_unspec_p (x1, &unspec))
2632
	    fatal_insn ("bad insn to frv_print_operand_memory_reference:", x1);
2633
	  frv_output_const_unspec (stream, &unspec);
bernds's avatar
bernds committed
2634 2635 2636
	  break;

	default:
2637
	  fatal_insn ("bad insn to frv_print_operand_memory_reference:", x);
bernds's avatar
bernds committed
2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650
	}
    }

  fputs (")", stream);
}


/* Return 2 for likely branches and 0 for non-likely branches  */

#define FRV_JUMP_LIKELY 2
#define FRV_JUMP_NOT_LIKELY 0

static int
2651
frv_print_operand_jump_hint (rtx insn)
bernds's avatar
bernds committed
2652 2653 2654 2655 2656 2657 2658
{
  rtx note;
  rtx labelref;
  int ret;
  HOST_WIDE_INT prob = -1;
  enum { UNKNOWN, BACKWARD, FORWARD } jump_type = UNKNOWN;

2659
  gcc_assert (GET_CODE (insn) == JUMP_INSN);
bernds's avatar
bernds committed
2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713

  /* Assume any non-conditional jump is likely.  */
  if (! any_condjump_p (insn))
    ret = FRV_JUMP_LIKELY;

  else
    {
      labelref = condjump_label (insn);
      if (labelref)
	{
	  rtx label = XEXP (labelref, 0);
	  jump_type = (insn_current_address > INSN_ADDRESSES (INSN_UID (label))
		       ? BACKWARD
		       : FORWARD);
	}

      note = find_reg_note (insn, REG_BR_PROB, 0);
      if (!note)
	ret = ((jump_type == BACKWARD) ? FRV_JUMP_LIKELY : FRV_JUMP_NOT_LIKELY);

      else
	{
	  prob = INTVAL (XEXP (note, 0));
	  ret = ((prob >= (REG_BR_PROB_BASE / 2))
		 ? FRV_JUMP_LIKELY
		 : FRV_JUMP_NOT_LIKELY);
	}
    }

#if 0
  if (TARGET_DEBUG)
    {
      char *direction;

      switch (jump_type)
	{
	default:
	case UNKNOWN:	direction = "unknown jump direction";	break;
	case BACKWARD:	direction = "jump backward";		break;
	case FORWARD:	direction = "jump forward";		break;
	}

      fprintf (stderr,
	       "%s: uid %ld, %s, probability = %ld, max prob. = %ld, hint = %d\n",
	       IDENTIFIER_POINTER (DECL_NAME (current_function_decl)),
	       (long)INSN_UID (insn), direction, (long)prob,
	       (long)REG_BR_PROB_BASE, ret);
    }
#endif

  return ret;
}


2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736
/* Return the comparison operator to use for CODE given that the ICC
   register is OP0.  */

static const char *
comparison_string (enum rtx_code code, rtx op0)
{
  bool is_nz_p = GET_MODE (op0) == CC_NZmode;
  switch (code)
    {
    default:  output_operand_lossage ("bad condition code");
    case EQ:  return "eq";
    case NE:  return "ne";
    case LT:  return is_nz_p ? "n" : "lt";
    case LE:  return "le";
    case GT:  return "gt";
    case GE:  return is_nz_p ? "p" : "ge";
    case LTU: return is_nz_p ? "no" : "c";
    case LEU: return is_nz_p ? "eq" : "ls";
    case GTU: return is_nz_p ? "ne" : "hi";
    case GEU: return is_nz_p ? "ra" : "nc";
    }
}

kazu's avatar
kazu committed
2737
/* Print an operand to an assembler instruction.
bernds's avatar
bernds committed
2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769

   `%' followed by a letter and a digit says to output an operand in an
   alternate fashion.  Four letters have standard, built-in meanings described
   below.  The machine description macro `PRINT_OPERAND' can define additional
   letters with nonstandard meanings.

   `%cDIGIT' can be used to substitute an operand that is a constant value
   without the syntax that normally indicates an immediate operand.

   `%nDIGIT' is like `%cDIGIT' except that the value of the constant is negated
   before printing.

   `%aDIGIT' can be used to substitute an operand as if it were a memory
   reference, with the actual operand treated as the address.  This may be
   useful when outputting a "load address" instruction, because often the
   assembler syntax for such an instruction requires you to write the operand
   as if it were a memory reference.

   `%lDIGIT' is used to substitute a `label_ref' into a jump instruction.

   `%=' outputs a number which is unique to each instruction in the entire
   compilation.  This is useful for making local labels to be referred to more
   than once in a single template that generates multiple assembler
   instructions.

   `%' followed by a punctuation character specifies a substitution that does
   not use an operand.  Only one case is standard: `%%' outputs a `%' into the
   assembler code.  Other nonstandard cases can be defined in the
   `PRINT_OPERAND' macro.  You must also define which punctuation characters
   are valid with the `PRINT_OPERAND_PUNCT_VALID_P' macro.  */

void
2770
frv_print_operand (FILE * file, rtx x, int code)
bernds's avatar
bernds committed
2771
{
2772
  struct frv_unspec unspec;
bernds's avatar
bernds committed
2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797
  HOST_WIDE_INT value;
  int offset;

  if (code != 0 && !isalpha (code))
    value = 0;

  else if (GET_CODE (x) == CONST_INT)
    value = INTVAL (x);

  else if (GET_CODE (x) == CONST_DOUBLE)
    {
      if (GET_MODE (x) == SFmode)
	{
	  REAL_VALUE_TYPE rv;
	  long l;

	  REAL_VALUE_FROM_CONST_DOUBLE (rv, x);
	  REAL_VALUE_TO_TARGET_SINGLE (rv, l);
	  value = l;
	}

      else if (GET_MODE (x) == VOIDmode)
	value = CONST_DOUBLE_LOW (x);

      else
2798
        fatal_insn ("bad insn in frv_print_operand, bad const_double", x);
bernds's avatar
bernds committed
2799 2800 2801 2802 2803 2804 2805 2806 2807
    }

  else
    value = 0;

  switch (code)
    {

    case '.':
2808
      /* Output r0.  */
bernds's avatar
bernds committed
2809 2810 2811 2812 2813 2814 2815
      fputs (reg_names[GPR_R0], file);
      break;

    case '#':
      fprintf (file, "%d", frv_print_operand_jump_hint (current_output_insn));
      break;

2816
    case '@':
2817
      /* Output small data area base register (gr16).  */
bernds's avatar
bernds committed
2818 2819 2820 2821
      fputs (reg_names[SDA_BASE_REG], file);
      break;

    case '~':
2822
      /* Output pic register (gr17).  */
bernds's avatar
bernds committed
2823 2824 2825 2826
      fputs (reg_names[PIC_REGNO], file);
      break;

    case '*':
2827
      /* Output the temporary integer CCR register.  */
bernds's avatar
bernds committed
2828 2829 2830 2831
      fputs (reg_names[ICR_TEMP], file);
      break;

    case '&':
2832
      /* Output the temporary integer CC register.  */
bernds's avatar
bernds committed
2833 2834 2835
      fputs (reg_names[ICC_TEMP], file);
      break;

2836
    /* case 'a': print an address.  */
bernds's avatar
bernds committed
2837 2838

    case 'C':
2839
      /* Print appropriate test for integer branch false operation.  */
2840 2841
      fputs (comparison_string (reverse_condition (GET_CODE (x)),
				XEXP (x, 0)), file);
bernds's avatar
bernds committed
2842 2843 2844
      break;

    case 'c':
2845
      /* Print appropriate test for integer branch true operation.  */
2846
      fputs (comparison_string (GET_CODE (x), XEXP (x, 0)), file);
bernds's avatar
bernds committed
2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858
      break;

    case 'e':
      /* Print 1 for a NE and 0 for an EQ to give the final argument
	 for a conditional instruction.  */
      if (GET_CODE (x) == NE)
	fputs ("1", file);

      else if (GET_CODE (x) == EQ)
	fputs ("0", file);

      else
2859
	fatal_insn ("bad insn to frv_print_operand, 'e' modifier:", x);
bernds's avatar
bernds committed
2860 2861 2862
      break;

    case 'F':
2863
      /* Print appropriate test for floating point branch false operation.  */
bernds's avatar
bernds committed
2864 2865 2866
      switch (GET_CODE (x))
	{
	default:
2867
	  fatal_insn ("bad insn to frv_print_operand, 'F' modifier:", x);
bernds's avatar
bernds committed
2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878

	case EQ:  fputs ("ne",  file); break;
	case NE:  fputs ("eq",  file); break;
	case LT:  fputs ("uge", file); break;
	case LE:  fputs ("ug",  file); break;
	case GT:  fputs ("ule", file); break;
	case GE:  fputs ("ul",  file); break;
	}
      break;

    case 'f':
2879
      /* Print appropriate test for floating point branch true operation.  */
bernds's avatar
bernds committed
2880 2881 2882
      switch (GET_CODE (x))
	{
	default:
2883
	  fatal_insn ("bad insn to frv_print_operand, 'f' modifier:", x);
bernds's avatar
bernds committed
2884 2885 2886 2887 2888 2889 2890 2891 2892 2893

	case EQ:  fputs ("eq",  file); break;
	case NE:  fputs ("ne",  file); break;
	case LT:  fputs ("lt",  file); break;
	case LE:  fputs ("le",  file); break;
	case GT:  fputs ("gt",  file); break;
	case GE:  fputs ("ge",  file); break;
	}
      break;

2894 2895 2896
    case 'g':
      /* Print appropriate GOT function.  */
      if (GET_CODE (x) != CONST_INT)
2897
	fatal_insn ("bad insn to frv_print_operand, 'g' modifier:", x);
2898 2899 2900
      fputs (unspec_got_name (INTVAL (x)), file);
      break;

bernds's avatar
bernds committed
2901 2902
    case 'I':
      /* Print 'i' if the operand is a constant, or is a memory reference that
2903
         adds a constant.  */
bernds's avatar
bernds committed
2904 2905 2906 2907
      if (GET_CODE (x) == MEM)
	x = ((GET_CODE (XEXP (x, 0)) == PLUS)
	     ? XEXP (XEXP (x, 0), 1)
	     : XEXP (x, 0));
2908 2909
      else if (GET_CODE (x) == PLUS)
	x = XEXP (x, 1);
bernds's avatar
bernds committed
2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925

      switch (GET_CODE (x))
	{
	default:
	  break;

	case CONST_INT:
	case SYMBOL_REF:
	case CONST:
	  fputs ("i", file);
	  break;
	}
      break;

    case 'i':
      /* For jump instructions, print 'i' if the operand is a constant or
2926
         is an expression that adds a constant.  */
bernds's avatar
bernds committed
2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944
      if (GET_CODE (x) == CONST_INT)
        fputs ("i", file);

      else
        {
          if (GET_CODE (x) == CONST_INT
              || (GET_CODE (x) == PLUS
                  && (GET_CODE (XEXP (x, 1)) == CONST_INT
                      || GET_CODE (XEXP (x, 0)) == CONST_INT)))
            fputs ("i", file);
        }
      break;

    case 'L':
      /* Print the lower register of a double word register pair */
      if (GET_CODE (x) == REG)
	fputs (reg_names[ REGNO (x)+1 ], file);
      else
2945
	fatal_insn ("bad insn to frv_print_operand, 'L' modifier:", x);
bernds's avatar
bernds committed
2946 2947
      break;

2948
    /* case 'l': print a LABEL_REF.  */
bernds's avatar
bernds committed
2949 2950 2951 2952 2953 2954 2955 2956 2957

    case 'M':
    case 'N':
      /* Print a memory reference for ld/st/jmp, %N prints a memory reference
         for the second word of double memory operations.  */
      offset = (code == 'M') ? 0 : UNITS_PER_WORD;
      switch (GET_CODE (x))
	{
	default:
2958
	  fatal_insn ("bad insn to frv_print_operand, 'M/N' modifier:", x);
bernds's avatar
bernds committed
2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978

	case MEM:
	  frv_print_operand_memory_reference (file, XEXP (x, 0), offset);
	  break;

	case REG:
	case SUBREG:
	case CONST_INT:
	case PLUS:
        case SYMBOL_REF:
	  frv_print_operand_memory_reference (file, x, offset);
	  break;
	}
      break;

    case 'O':
      /* Print the opcode of a command.  */
      switch (GET_CODE (x))
	{
	default:
2979
	  fatal_insn ("bad insn to frv_print_operand, 'O' modifier:", x);
bernds's avatar
bernds committed
2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991

	case PLUS:     fputs ("add", file); break;
	case MINUS:    fputs ("sub", file); break;
	case AND:      fputs ("and", file); break;
	case IOR:      fputs ("or",  file); break;
	case XOR:      fputs ("xor", file); break;
	case ASHIFT:   fputs ("sll", file); break;
	case ASHIFTRT: fputs ("sra", file); break;
	case LSHIFTRT: fputs ("srl", file); break;
	}
      break;

2992
    /* case 'n': negate and print a constant int.  */
bernds's avatar
bernds committed
2993 2994 2995 2996

    case 'P':
      /* Print PIC label using operand as the number.  */
      if (GET_CODE (x) != CONST_INT)
2997
	fatal_insn ("bad insn to frv_print_operand, P modifier:", x);
bernds's avatar
bernds committed
2998 2999 3000 3001 3002

      fprintf (file, ".LCF%ld", (long)INTVAL (x));
      break;

    case 'U':
3003
      /* Print 'u' if the operand is a update load/store.  */
bernds's avatar
bernds committed
3004 3005 3006 3007 3008
      if (GET_CODE (x) == MEM && GET_CODE (XEXP (x, 0)) == PRE_MODIFY)
	fputs ("u", file);
      break;

    case 'z':
3009
      /* If value is 0, print gr0, otherwise it must be a register.  */
bernds's avatar
bernds committed
3010 3011 3012 3013 3014 3015 3016
      if (GET_CODE (x) == CONST_INT && INTVAL (x) == 0)
	fputs (reg_names[GPR_R0], file);

      else if (GET_CODE (x) == REG)
        fputs (reg_names [REGNO (x)], file);

      else
3017
        fatal_insn ("bad insn in frv_print_operand, z case", x);
bernds's avatar
bernds committed
3018 3019 3020
      break;

    case 'x':
3021
      /* Print constant in hex.  */
bernds's avatar
bernds committed
3022 3023 3024 3025 3026 3027
      if (GET_CODE (x) == CONST_INT || GET_CODE (x) == CONST_DOUBLE)
        {
	  fprintf (file, "%s0x%.4lx", IMMEDIATE_PREFIX, (long) value);
	  break;
	}

3028
      /* Fall through.  */
bernds's avatar
bernds committed
3029 3030 3031 3032 3033 3034 3035 3036 3037

    case '\0':
      if (GET_CODE (x) == REG)
        fputs (reg_names [REGNO (x)], file);

      else if (GET_CODE (x) == CONST_INT
              || GET_CODE (x) == CONST_DOUBLE)
        fprintf (file, "%s%ld", IMMEDIATE_PREFIX, (long) value);

3038 3039 3040
      else if (frv_const_unspec_p (x, &unspec))
	frv_output_const_unspec (file, &unspec);

bernds's avatar
bernds committed
3041 3042 3043 3044 3045 3046 3047
      else if (GET_CODE (x) == MEM)
        frv_print_operand_address (file, XEXP (x, 0));

      else if (CONSTANT_ADDRESS_P (x))
        frv_print_operand_address (file, x);

      else
3048
        fatal_insn ("bad insn in frv_print_operand, 0 case", x);
bernds's avatar
bernds committed
3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077

      break;

    default:
      fatal_insn ("frv_print_operand: unknown code", x);
      break;
    }

  return;
}


/* A C statement (sans semicolon) for initializing the variable CUM for the
   state at the beginning of the argument list.  The variable has type
   `CUMULATIVE_ARGS'.  The value of FNTYPE is the tree node for the data type
   of the function which will receive the args, or 0 if the args are to a
   compiler support library function.  The value of INDIRECT is nonzero when
   processing an indirect call, for example a call through a function pointer.
   The value of INDIRECT is zero for a call to an explicitly named function, a
   library function call, or when `INIT_CUMULATIVE_ARGS' is used to find
   arguments for the function being compiled.

   When processing a call to a compiler support library function, LIBNAME
   identifies which one.  It is a `symbol_ref' rtx which contains the name of
   the function, as a string.  LIBNAME is 0 when an ordinary C function call is
   being processed.  Thus, each time this macro is called, either LIBNAME or
   FNTYPE is nonzero, but never both of them at once.  */

void
3078 3079 3080 3081 3082
frv_init_cumulative_args (CUMULATIVE_ARGS *cum,
                          tree fntype,
                          rtx libname,
                          tree fndecl,
                          int incoming)
bernds's avatar
bernds committed
3083 3084 3085 3086 3087 3088
{
  *cum = FIRST_ARG_REGNUM;

  if (TARGET_DEBUG_ARG)
    {
      fprintf (stderr, "\ninit_cumulative_args:");
3089
      if (!fndecl && fntype)
bernds's avatar
bernds committed
3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112
	fputs (" indirect", stderr);

      if (incoming)
	fputs (" incoming", stderr);

      if (fntype)
	{
	  tree ret_type = TREE_TYPE (fntype);
	  fprintf (stderr, " return=%s,",
		   tree_code_name[ (int)TREE_CODE (ret_type) ]);
	}

      if (libname && GET_CODE (libname) == SYMBOL_REF)
	fprintf (stderr, " libname=%s", XSTR (libname, 0));

      if (cfun->returns_struct)
	fprintf (stderr, " return-struct");

      putc ('\n', stderr);
    }
}


3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125
/* Return true if we should pass an argument on the stack rather than
   in registers.  */

static bool
frv_must_pass_in_stack (enum machine_mode mode, tree type)
{
  if (mode == BLKmode)
    return true;
  if (type == NULL)
    return false;
  return AGGREGATE_TYPE_P (type);
}

bernds's avatar
bernds committed
3126 3127 3128 3129 3130
/* If defined, a C expression that gives the alignment boundary, in bits, of an
   argument with the specified mode and type.  If it is not defined,
   `PARM_BOUNDARY' is used for all arguments.  */

int
3131 3132
frv_function_arg_boundary (enum machine_mode mode ATTRIBUTE_UNUSED,
                           tree type ATTRIBUTE_UNUSED)
bernds's avatar
bernds committed
3133 3134 3135 3136 3137
{
  return BITS_PER_WORD;
}

rtx
3138 3139 3140 3141 3142
frv_function_arg (CUMULATIVE_ARGS *cum,
                  enum machine_mode mode,
                  tree type ATTRIBUTE_UNUSED,
                  int named,
                  int incoming ATTRIBUTE_UNUSED)
bernds's avatar
bernds committed
3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157
{
  enum machine_mode xmode = (mode == BLKmode) ? SImode : mode;
  int arg_num = *cum;
  rtx ret;
  const char *debstr;

  /* Return a marker for use in the call instruction.  */
  if (xmode == VOIDmode)
    {
      ret = const0_rtx;
      debstr = "<0>";
    }

  else if (arg_num <= LAST_ARG_REGNUM)
    {
3158
      ret = gen_rtx_REG (xmode, arg_num);
bernds's avatar
bernds committed
3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186
      debstr = reg_names[arg_num];
    }

  else
    {
      ret = NULL_RTX;
      debstr = "memory";
    }

  if (TARGET_DEBUG_ARG)
    fprintf (stderr,
	     "function_arg: words = %2d, mode = %4s, named = %d, size = %3d, arg = %s\n",
	     arg_num, GET_MODE_NAME (mode), named, GET_MODE_SIZE (mode), debstr);

  return ret;
}


/* A C statement (sans semicolon) to update the summarizer variable CUM to
   advance past an argument in the argument list.  The values MODE, TYPE and
   NAMED describe that argument.  Once this is done, the variable CUM is
   suitable for analyzing the *following* argument with `FUNCTION_ARG', etc.

   This macro need not do anything if the argument in question was passed on
   the stack.  The compiler knows how to track the amount of stack space used
   for arguments without any special help.  */

void
3187 3188 3189 3190
frv_function_arg_advance (CUMULATIVE_ARGS *cum,
                          enum machine_mode mode,
                          tree type ATTRIBUTE_UNUSED,
                          int named)
bernds's avatar
bernds committed
3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221
{
  enum machine_mode xmode = (mode == BLKmode) ? SImode : mode;
  int bytes = GET_MODE_SIZE (xmode);
  int words = (bytes + UNITS_PER_WORD  - 1) / UNITS_PER_WORD;
  int arg_num = *cum;

  *cum = arg_num + words;

  if (TARGET_DEBUG_ARG)
    fprintf (stderr,
	     "function_adv: words = %2d, mode = %4s, named = %d, size = %3d\n",
	     arg_num, GET_MODE_NAME (mode), named, words * UNITS_PER_WORD);
}


/* A C expression for the number of words, at the beginning of an argument,
   must be put in registers.  The value must be zero for arguments that are
   passed entirely in registers or that are entirely pushed on the stack.

   On some machines, certain arguments must be passed partially in registers
   and partially in memory.  On these machines, typically the first N words of
   arguments are passed in registers, and the rest on the stack.  If a
   multi-word argument (a `double' or a structure) crosses that boundary, its
   first few words must be passed in registers and the rest must be pushed.
   This macro tells the compiler when this occurs, and how many of the words
   should go in registers.

   `FUNCTION_ARG' for these arguments should return the first register to be
   used by the caller for this argument; likewise `FUNCTION_INCOMING_ARG', for
   the called function.  */

3222 3223 3224
static int
frv_arg_partial_bytes (CUMULATIVE_ARGS *cum, enum machine_mode mode,
		       tree type ATTRIBUTE_UNUSED, bool named ATTRIBUTE_UNUSED)
bernds's avatar
bernds committed
3225 3226 3227 3228 3229 3230 3231 3232 3233 3234
{
  enum machine_mode xmode = (mode == BLKmode) ? SImode : mode;
  int bytes = GET_MODE_SIZE (xmode);
  int words = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
  int arg_num = *cum;
  int ret;

  ret = ((arg_num <= LAST_ARG_REGNUM && arg_num + words > LAST_ARG_REGNUM+1)
	 ? LAST_ARG_REGNUM - arg_num + 1
	 : 0);
3235
  ret *= UNITS_PER_WORD;
bernds's avatar
bernds committed
3236 3237

  if (TARGET_DEBUG_ARG && ret)
3238
    fprintf (stderr, "frv_arg_partial_bytes: %d\n", ret);
bernds's avatar
bernds committed
3239 3240 3241 3242 3243 3244 3245 3246

  return ret;
}


/* Return true if a register is ok to use as a base or index register.  */

static FRV_INLINE int
3247
frv_regno_ok_for_base_p (int regno, int strict_p)
bernds's avatar
bernds committed
3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311
{
  if (GPR_P (regno))
    return TRUE;

  if (strict_p)
    return (reg_renumber[regno] >= 0 && GPR_P (reg_renumber[regno]));

  if (regno == ARG_POINTER_REGNUM)
    return TRUE;

  return (regno >= FIRST_PSEUDO_REGISTER);
}


/* A C compound statement with a conditional `goto LABEL;' executed if X (an
   RTX) is a legitimate memory address on the target machine for a memory
   operand of mode MODE.

   It usually pays to define several simpler macros to serve as subroutines for
   this one.  Otherwise it may be too complicated to understand.

   This macro must exist in two variants: a strict variant and a non-strict
   one.  The strict variant is used in the reload pass.  It must be defined so
   that any pseudo-register that has not been allocated a hard register is
   considered a memory reference.  In contexts where some kind of register is
   required, a pseudo-register with no hard register must be rejected.

   The non-strict variant is used in other passes.  It must be defined to
   accept all pseudo-registers in every context where some kind of register is
   required.

   Compiler source files that want to use the strict variant of this macro
   define the macro `REG_OK_STRICT'.  You should use an `#ifdef REG_OK_STRICT'
   conditional to define the strict variant in that case and the non-strict
   variant otherwise.

   Subroutines to check for acceptable registers for various purposes (one for
   base registers, one for index registers, and so on) are typically among the
   subroutines used to define `GO_IF_LEGITIMATE_ADDRESS'.  Then only these
   subroutine macros need have two variants; the higher levels of macros may be
   the same whether strict or not.

   Normally, constant addresses which are the sum of a `symbol_ref' and an
   integer are stored inside a `const' RTX to mark them as constant.
   Therefore, there is no need to recognize such sums specifically as
   legitimate addresses.  Normally you would simply recognize any `const' as
   legitimate.

   Usually `PRINT_OPERAND_ADDRESS' is not prepared to handle constant sums that
   are not marked with `const'.  It assumes that a naked `plus' indicates
   indexing.  If so, then you *must* reject such naked constant sums as
   illegitimate addresses, so that none of them will be given to
   `PRINT_OPERAND_ADDRESS'.

   On some machines, whether a symbolic address is legitimate depends on the
   section that the address refers to.  On these machines, define the macro
   `ENCODE_SECTION_INFO' to store the information into the `symbol_ref', and
   then check for it here.  When you see a `const', you will have to look
   inside it to find the `symbol_ref' in order to determine the section.

   The best way to modify the name string is by adding text to the beginning,
   with suitable punctuation to prevent any ambiguity.  Allocate the new name
   in `saveable_obstack'.  You will have to modify `ASM_OUTPUT_LABELREF' to
   remove and decode the added text and output the name accordingly, and define
3312
   `(* targetm.strip_name_encoding)' to access the original name string.
bernds's avatar
bernds committed
3313 3314 3315 3316 3317 3318

   You can check the information stored here into the `symbol_ref' in the
   definitions of the macros `GO_IF_LEGITIMATE_ADDRESS' and
   `PRINT_OPERAND_ADDRESS'.  */

int
3319 3320 3321
frv_legitimate_address_p (enum machine_mode mode,
                          rtx x,
                          int strict_p,
3322 3323
                          int condexec_p,
			  int allow_double_reg_p)
bernds's avatar
bernds committed
3324 3325 3326 3327 3328 3329
{
  rtx x0, x1;
  int ret = 0;
  HOST_WIDE_INT value;
  unsigned regno0;

3330 3331 3332
  if (FRV_SYMBOL_REF_TLS_P (x))
    return 0;

bernds's avatar
bernds committed
3333 3334 3335 3336 3337 3338 3339 3340 3341 3342
  switch (GET_CODE (x))
    {
    default:
      break;

    case SUBREG:
      x = SUBREG_REG (x);
      if (GET_CODE (x) != REG)
        break;

3343
      /* Fall through.  */
bernds's avatar
bernds committed
3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363

    case REG:
      ret = frv_regno_ok_for_base_p (REGNO (x), strict_p);
      break;

    case PRE_MODIFY:
      x0 = XEXP (x, 0);
      x1 = XEXP (x, 1);
      if (GET_CODE (x0) != REG
	  || ! frv_regno_ok_for_base_p (REGNO (x0), strict_p)
	  || GET_CODE (x1) != PLUS
	  || ! rtx_equal_p (x0, XEXP (x1, 0))
	  || GET_CODE (XEXP (x1, 1)) != REG
	  || ! frv_regno_ok_for_base_p (REGNO (XEXP (x1, 1)), strict_p))
	break;

      ret = 1;
      break;

    case CONST_INT:
3364
      /* 12-bit immediate */
bernds's avatar
bernds committed
3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402
      if (condexec_p)
	ret = FALSE;
      else
	{
	  ret = IN_RANGE_P (INTVAL (x), -2048, 2047);

	  /* If we can't use load/store double operations, make sure we can
	     address the second word.  */
	  if (ret && GET_MODE_SIZE (mode) > UNITS_PER_WORD)
	    ret = IN_RANGE_P (INTVAL (x) + GET_MODE_SIZE (mode) - 1,
			      -2048, 2047);
	}
      break;

    case PLUS:
      x0 = XEXP (x, 0);
      x1 = XEXP (x, 1);

      if (GET_CODE (x0) == SUBREG)
	x0 = SUBREG_REG (x0);

      if (GET_CODE (x0) != REG)
	break;

      regno0 = REGNO (x0);
      if (!frv_regno_ok_for_base_p (regno0, strict_p))
	break;

      switch (GET_CODE (x1))
	{
	default:
	  break;

	case SUBREG:
	  x1 = SUBREG_REG (x1);
	  if (GET_CODE (x1) != REG)
	    break;

3403
	  /* Fall through.  */
bernds's avatar
bernds committed
3404 3405

	case REG:
3406 3407
	  /* Do not allow reg+reg addressing for modes > 1 word if we
	     can't depend on having move double instructions.  */
3408
	  if (!allow_double_reg_p && GET_MODE_SIZE (mode) > UNITS_PER_WORD)
bernds's avatar
bernds committed
3409 3410 3411 3412 3413 3414
	    ret = FALSE;
	  else
	    ret = frv_regno_ok_for_base_p (REGNO (x1), strict_p);
	  break;

	case CONST_INT:
3415
          /* 12-bit immediate */
bernds's avatar
bernds committed
3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430
	  if (condexec_p)
	    ret = FALSE;
	  else
	    {
	      value = INTVAL (x1);
	      ret = IN_RANGE_P (value, -2048, 2047);

	      /* If we can't use load/store double operations, make sure we can
		 address the second word.  */
	      if (ret && GET_MODE_SIZE (mode) > UNITS_PER_WORD)
		ret = IN_RANGE_P (value + GET_MODE_SIZE (mode) - 1, -2048, 2047);
	    }
	  break;

	case CONST:
3431
	  if (!condexec_p && got12_operand (x1, VOIDmode))
bernds's avatar
bernds committed
3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449
	    ret = TRUE;
	  break;

	}
      break;
    }

  if (TARGET_DEBUG_ADDR)
    {
      fprintf (stderr, "\n========== GO_IF_LEGITIMATE_ADDRESS, mode = %s, result = %d, addresses are %sstrict%s\n",
	       GET_MODE_NAME (mode), ret, (strict_p) ? "" : "not ",
	       (condexec_p) ? ", inside conditional code" : "");
      debug_rtx (x);
    }

  return ret;
}

3450 3451 3452 3453
/* Given an ADDR, generate code to inline the PLT.  */
static rtx
gen_inlined_tls_plt (rtx addr)
{
ebotcazou's avatar
ebotcazou committed
3454
  rtx retval, dest;
3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485
  rtx picreg = get_hard_reg_initial_val (Pmode, FDPIC_REG);


  dest = gen_reg_rtx (DImode);

  if (flag_pic == 1)
    {
      /*
	-fpic version:

	lddi.p  @(gr15, #gottlsdesc12(ADDR)), gr8
	calll    #gettlsoff(ADDR)@(gr8, gr0)
      */
      emit_insn (gen_tls_lddi (dest, addr, picreg));
    }
  else
    {
      /*
	-fPIC version:

	sethi.p #gottlsdeschi(ADDR), gr8
	setlo   #gottlsdesclo(ADDR), gr8
	ldd     #tlsdesc(ADDR)@(gr15, gr8), gr8
	calll   #gettlsoff(ADDR)@(gr8, gr0)
      */
      rtx reguse = gen_reg_rtx (Pmode);
      emit_insn (gen_tlsoff_hilo (reguse, addr, GEN_INT (R_FRV_GOTTLSDESCHI)));
      emit_insn (gen_tls_tlsdesc_ldd (dest, picreg, reguse, addr));
    }

  retval = gen_reg_rtx (Pmode);
3486
  emit_insn (gen_tls_indirect_call (retval, addr, dest, picreg));
3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602
  return retval;
}

/* Emit a TLSMOFF or TLSMOFF12 offset, depending on -mTLS.  Returns
   the destination address.  */
static rtx
gen_tlsmoff (rtx addr, rtx reg)
{
  rtx dest = gen_reg_rtx (Pmode);

  if (TARGET_BIG_TLS)
    {
      /* sethi.p #tlsmoffhi(x), grA
	 setlo   #tlsmofflo(x), grA
      */
      dest = gen_reg_rtx (Pmode);
      emit_insn (gen_tlsoff_hilo (dest, addr,
				  GEN_INT (R_FRV_TLSMOFFHI)));
      dest = gen_rtx_PLUS (Pmode, dest, reg);
    }
  else
    {
      /* addi grB, #tlsmoff12(x), grC
	   -or-
	 ld/st @(grB, #tlsmoff12(x)), grC
      */
      dest = gen_reg_rtx (Pmode);
      emit_insn (gen_symGOTOFF2reg_i (dest, addr, reg,
				      GEN_INT (R_FRV_TLSMOFF12)));
    }
  return dest;
}

/* Generate code for a TLS address.  */
static rtx
frv_legitimize_tls_address (rtx addr, enum tls_model model)
{
  rtx dest, tp = gen_rtx_REG (Pmode, 29);
  rtx picreg = get_hard_reg_initial_val (Pmode, 15);

  switch (model)
    {
    case TLS_MODEL_INITIAL_EXEC:
      if (flag_pic == 1)
	{
	  /* -fpic version.
	     ldi @(gr15, #gottlsoff12(x)), gr5
	   */
	  dest = gen_reg_rtx (Pmode);
	  emit_insn (gen_tls_load_gottlsoff12 (dest, addr, picreg));
	  dest = gen_rtx_PLUS (Pmode, tp, dest);
	}
      else
	{
	  /* -fPIC or anything else.

	    sethi.p #gottlsoffhi(x), gr14
	    setlo   #gottlsofflo(x), gr14
	    ld      #tlsoff(x)@(gr15, gr14), gr9
	  */
	  rtx tmp = gen_reg_rtx (Pmode);
	  dest = gen_reg_rtx (Pmode);
	  emit_insn (gen_tlsoff_hilo (tmp, addr,
				      GEN_INT (R_FRV_GOTTLSOFF_HI)));

	  emit_insn (gen_tls_tlsoff_ld (dest, picreg, tmp, addr));
	  dest = gen_rtx_PLUS (Pmode, tp, dest);
	}
      break;
    case TLS_MODEL_LOCAL_DYNAMIC:
      {
	rtx reg, retval;

	if (TARGET_INLINE_PLT)
	  retval = gen_inlined_tls_plt (GEN_INT (0));
	else
	  {
	    /* call #gettlsoff(0) */
	    retval = gen_reg_rtx (Pmode);
	    emit_insn (gen_call_gettlsoff (retval, GEN_INT (0), picreg));
	  }

	reg = gen_reg_rtx (Pmode);
	emit_insn (gen_rtx_SET (VOIDmode, reg,
				gen_rtx_PLUS (Pmode,
					      retval, tp)));

	dest = gen_tlsmoff (addr, reg);

	/*
	dest = gen_reg_rtx (Pmode);
	emit_insn (gen_tlsoff_hilo (dest, addr,
				    GEN_INT (R_FRV_TLSMOFFHI)));
	dest = gen_rtx_PLUS (Pmode, dest, reg);
	*/
	break;
      }
    case TLS_MODEL_LOCAL_EXEC:
      dest = gen_tlsmoff (addr, gen_rtx_REG (Pmode, 29));
      break;
    case TLS_MODEL_GLOBAL_DYNAMIC:
      {
	rtx retval;

	if (TARGET_INLINE_PLT)
	  retval = gen_inlined_tls_plt (addr);
	else
	  {
	    /* call #gettlsoff(x) */
	    retval = gen_reg_rtx (Pmode);
	    emit_insn (gen_call_gettlsoff (retval, addr, picreg));
	  }
	dest = gen_rtx_PLUS (Pmode, retval, tp);
	break;
      }
    default:
3603
      gcc_unreachable ();
3604 3605 3606 3607 3608
    }

  return dest;
}

3609
rtx
3610
frv_legitimize_address (rtx x,
3611 3612 3613
			rtx oldx ATTRIBUTE_UNUSED,
			enum machine_mode mode ATTRIBUTE_UNUSED)
{
3614 3615 3616 3617 3618 3619 3620
  if (GET_CODE (x) == SYMBOL_REF)
    {
      enum tls_model model = SYMBOL_REF_TLS_MODEL (x);
      if (model != 0)
        return frv_legitimize_tls_address (x, model);
    }

3621 3622
  return NULL_RTX;
}
bernds's avatar
bernds committed
3623

3624 3625 3626 3627 3628 3629 3630 3631 3632 3633
/* Test whether a local function descriptor is canonical, i.e.,
   whether we can use FUNCDESC_GOTOFF to compute the address of the
   function.  */

static bool
frv_local_funcdesc_p (rtx fnx)
{
  tree fn;
  enum symbol_visibility vis;
  bool ret;
bernds's avatar
bernds committed
3634

3635 3636 3637 3638 3639 3640 3641
  if (! SYMBOL_REF_LOCAL_P (fnx))
    return FALSE;

  fn = SYMBOL_REF_DECL (fnx);

  if (! fn)
    return FALSE;
bernds's avatar
bernds committed
3642

3643
  vis = DECL_VISIBILITY (fn);
bernds's avatar
bernds committed
3644

3645 3646 3647 3648 3649 3650 3651 3652 3653
  if (vis == VISIBILITY_PROTECTED)
    /* Private function descriptors for protected functions are not
       canonical.  Temporarily change the visibility to global.  */
    vis = VISIBILITY_DEFAULT;
  else if (flag_shlib)
    /* If we're already compiling for a shared library (that, unlike
       executables, can't assume that the existence of a definition
       implies local binding), we can skip the re-testing.  */
    return TRUE;
bernds's avatar
bernds committed
3654

3655
  ret = default_binds_local_p_1 (fn, flag_pic);
bernds's avatar
bernds committed
3656

3657 3658 3659 3660 3661 3662 3663
  DECL_VISIBILITY (fn) = vis;

  return ret;
}

/* Load the _gp symbol into DEST.  SRC is supposed to be the FDPIC
   register.  */
bernds's avatar
bernds committed
3664 3665

rtx
3666 3667 3668 3669
frv_gen_GPsym2reg (rtx dest, rtx src)
{
  tree gp = get_identifier ("_gp");
  rtx gp_sym = gen_rtx_SYMBOL_REF (Pmode, IDENTIFIER_POINTER (gp));
bernds's avatar
bernds committed
3670

3671 3672 3673 3674 3675 3676 3677
  return gen_symGOT2reg (dest, gp_sym, src, GEN_INT (R_FRV_GOT12));
}

static const char *
unspec_got_name (int i)
{
  switch (i)
bernds's avatar
bernds committed
3678
    {
3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695
    case R_FRV_GOT12: return "got12";
    case R_FRV_GOTHI: return "gothi";
    case R_FRV_GOTLO: return "gotlo";
    case R_FRV_FUNCDESC: return "funcdesc";
    case R_FRV_FUNCDESC_GOT12: return "gotfuncdesc12";
    case R_FRV_FUNCDESC_GOTHI: return "gotfuncdeschi";
    case R_FRV_FUNCDESC_GOTLO: return "gotfuncdesclo";
    case R_FRV_FUNCDESC_VALUE: return "funcdescvalue";
    case R_FRV_FUNCDESC_GOTOFF12: return "gotofffuncdesc12";
    case R_FRV_FUNCDESC_GOTOFFHI: return "gotofffuncdeschi";
    case R_FRV_FUNCDESC_GOTOFFLO: return "gotofffuncdesclo";
    case R_FRV_GOTOFF12: return "gotoff12";
    case R_FRV_GOTOFFHI: return "gotoffhi";
    case R_FRV_GOTOFFLO: return "gotofflo";
    case R_FRV_GPREL12: return "gprel12";
    case R_FRV_GPRELHI: return "gprelhi";
    case R_FRV_GPRELLO: return "gprello";
3696 3697 3698 3699 3700 3701 3702 3703 3704
    case R_FRV_GOTTLSOFF_HI: return "gottlsoffhi";
    case R_FRV_GOTTLSOFF_LO: return "gottlsofflo";
    case R_FRV_TLSMOFFHI: return "tlsmoffhi";
    case R_FRV_TLSMOFFLO: return "tlsmofflo";
    case R_FRV_TLSMOFF12: return "tlsmoff12";
    case R_FRV_TLSDESCHI: return "tlsdeschi";
    case R_FRV_TLSDESCLO: return "tlsdesclo";
    case R_FRV_GOTTLSDESCHI: return "gottlsdeschi";
    case R_FRV_GOTTLSDESCLO: return "gottlsdesclo";
3705
    default: gcc_unreachable ();
bernds's avatar
bernds committed
3706
    }
3707
}
bernds's avatar
bernds committed
3708

3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733
/* Write the assembler syntax for UNSPEC to STREAM.  Note that any offset
   is added inside the relocation operator.  */

static void
frv_output_const_unspec (FILE *stream, const struct frv_unspec *unspec)
{
  fprintf (stream, "#%s(", unspec_got_name (unspec->reloc));
  output_addr_const (stream, plus_constant (unspec->symbol, unspec->offset));
  fputs (")", stream);
}

/* Implement FIND_BASE_TERM.  See whether ORIG_X represents #gprel12(foo)
   or #gotoff12(foo) for some small data symbol foo.  If so, return foo,
   otherwise return ORIG_X.  */

rtx
frv_find_base_term (rtx x)
{
  struct frv_unspec unspec;

  if (frv_const_unspec_p (x, &unspec)
      && frv_small_data_reloc_p (unspec.symbol, unspec.reloc))
    return plus_constant (unspec.symbol, unspec.offset);

  return x;
bernds's avatar
bernds committed
3734 3735 3736 3737 3738
}

/* Return 1 if operand is a valid FRV address.  CONDEXEC_P is true if
   the operand is used by a predicated instruction.  */

3739
int
3740
frv_legitimate_memory_operand (rtx op, enum machine_mode mode, int condexec_p)
bernds's avatar
bernds committed
3741 3742 3743 3744
{
  return ((GET_MODE (op) == mode || mode == VOIDmode)
	  && GET_CODE (op) == MEM
	  && frv_legitimate_address_p (mode, XEXP (op, 0),
3745 3746 3747 3748
				       reload_completed, condexec_p, FALSE));
}

void
3749
frv_expand_fdpic_call (rtx *operands, bool ret_value, bool sibcall)
3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784
{
  rtx lr = gen_rtx_REG (Pmode, LR_REGNO);
  rtx picreg = get_hard_reg_initial_val (SImode, FDPIC_REG);
  rtx c, rvrtx=0;
  rtx addr;

  if (ret_value)
    {
      rvrtx = operands[0];
      operands ++;
    }

  addr = XEXP (operands[0], 0);

  /* Inline PLTs if we're optimizing for speed.  We'd like to inline
     any calls that would involve a PLT, but can't tell, since we
     don't know whether an extern function is going to be provided by
     a separate translation unit or imported from a separate module.
     When compiling for shared libraries, if the function has default
     visibility, we assume it's overridable, so we inline the PLT, but
     for executables, we don't really have a way to make a good
     decision: a function is as likely to be imported from a shared
     library as it is to be defined in the executable itself.  We
     assume executables will get global functions defined locally,
     whereas shared libraries will have them potentially overridden,
     so we only inline PLTs when compiling for shared libraries.

     In order to mark a function as local to a shared library, any
     non-default visibility attribute suffices.  Unfortunately,
     there's no simple way to tag a function declaration as ``in a
     different module'', which we could then use to trigger PLT
     inlining on executables.  There's -minline-plt, but it affects
     all external functions, so one would have to also mark function
     declarations available in the same module with non-default
     visibility, which is advantageous in itself.  */
3785 3786 3787
  if (GET_CODE (addr) == SYMBOL_REF
      && ((!SYMBOL_REF_LOCAL_P (addr) && TARGET_INLINE_PLT)
	  || sibcall))
3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799
    {
      rtx x, dest;
      dest = gen_reg_rtx (SImode);
      if (flag_pic != 1)
	x = gen_symGOTOFF2reg_hilo (dest, addr, OUR_FDPIC_REG,
				    GEN_INT (R_FRV_FUNCDESC_GOTOFF12));
      else
	x = gen_symGOTOFF2reg (dest, addr, OUR_FDPIC_REG,
			       GEN_INT (R_FRV_FUNCDESC_GOTOFF12));
      emit_insn (x);
      cfun->uses_pic_offset_table = TRUE;
      addr = dest;
3800
    }
3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818
  else if (GET_CODE (addr) == SYMBOL_REF)
    {
      /* These are always either local, or handled through a local
	 PLT.  */
      if (ret_value)
	c = gen_call_value_fdpicsi (rvrtx, addr, operands[1],
				    operands[2], picreg, lr);
      else
	c = gen_call_fdpicsi (addr, operands[1], operands[2], picreg, lr);
      emit_call_insn (c);
      return;
    }
  else if (! ldd_address_operand (addr, Pmode))
    addr = force_reg (Pmode, addr);

  picreg = gen_reg_rtx (DImode);
  emit_insn (gen_movdi_ldd (picreg, addr));

3819 3820 3821 3822 3823
  if (sibcall && ret_value)
    c = gen_sibcall_value_fdpicdi (rvrtx, picreg, const0_rtx);
  else if (sibcall)
    c = gen_sibcall_fdpicdi (picreg, const0_rtx);
  else if (ret_value)
3824 3825 3826 3827
    c = gen_call_value_fdpicdi (rvrtx, picreg, const0_rtx, lr);
  else
    c = gen_call_fdpicdi (picreg, const0_rtx, lr);
  emit_call_insn (c);
bernds's avatar
bernds committed
3828 3829
}

3830 3831 3832 3833
/* Look for a SYMBOL_REF of a function in an rtx.  We always want to
   process these separately from any offsets, such that we add any
   offsets to the function descriptor (the actual pointer), not to the
   function address.  */
bernds's avatar
bernds committed
3834

3835 3836
static bool
frv_function_symbol_referenced_p (rtx x)
bernds's avatar
bernds committed
3837
{
3838 3839 3840
  const char *format;
  int length;
  int j;
bernds's avatar
bernds committed
3841

3842 3843
  if (GET_CODE (x) == SYMBOL_REF)
    return SYMBOL_REF_FUNCTION_P (x);
3844

3845 3846
  length = GET_RTX_LENGTH (GET_CODE (x));
  format = GET_RTX_FORMAT (GET_CODE (x));
bernds's avatar
bernds committed
3847

3848
  for (j = 0; j < length; ++j)
bernds's avatar
bernds committed
3849
    {
3850 3851 3852 3853 3854 3855
      switch (format[j])
	{
	case 'e':
	  if (frv_function_symbol_referenced_p (XEXP (x, j)))
	    return TRUE;
	  break;
bernds's avatar
bernds committed
3856

3857 3858 3859 3860 3861 3862 3863 3864 3865 3866
	case 'V':
	case 'E':
	  if (XVEC (x, j) != 0)
	    {
	      int k;
	      for (k = 0; k < XVECLEN (x, j); ++k)
		if (frv_function_symbol_referenced_p (XVECEXP (x, j, k)))
		  return TRUE;
	    }
	  break;
bernds's avatar
bernds committed
3867

3868 3869 3870 3871
	default:
	  /* Nothing to do.  */
	  break;
	}
bernds's avatar
bernds committed
3872 3873 3874 3875 3876
    }

  return FALSE;
}

3877 3878
/* Return true if the memory operand is one that can be conditionally
   executed.  */
bernds's avatar
bernds committed
3879

3880
int
3881
condexec_memory_operand (rtx op, enum machine_mode mode)
bernds's avatar
bernds committed
3882
{
3883 3884
  enum machine_mode op_mode = GET_MODE (op);
  rtx addr;
bernds's avatar
bernds committed
3885

3886
  if (mode != VOIDmode && op_mode != mode)
bernds's avatar
bernds committed
3887 3888
    return FALSE;

3889
  switch (op_mode)
bernds's avatar
bernds committed
3890 3891
    {
    default:
3892
      return FALSE;
bernds's avatar
bernds committed
3893

3894 3895 3896 3897
    case QImode:
    case HImode:
    case SImode:
    case SFmode:
bernds's avatar
bernds committed
3898 3899 3900
      break;
    }

3901
  if (GET_CODE (op) != MEM)
bernds's avatar
bernds committed
3902 3903
    return FALSE;

3904 3905
  addr = XEXP (op, 0);
  return frv_legitimate_address_p (mode, addr, reload_completed, TRUE, FALSE);
bernds's avatar
bernds committed
3906 3907 3908 3909 3910 3911
}

/* Return true if the bare return instruction can be used outside of the
   epilog code.  For frv, we only do it if there was no stack allocation.  */

int
3912
direct_return_p (void)
bernds's avatar
bernds committed
3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923
{
  frv_stack_t *info;

  if (!reload_completed)
    return FALSE;

  info = frv_stack_info ();
  return (info->total_size == 0);
}


3924 3925 3926
void
frv_emit_move (enum machine_mode mode, rtx dest, rtx src)
{
3927 3928 3929 3930 3931 3932 3933
  if (GET_CODE (src) == SYMBOL_REF)
    {
      enum tls_model model = SYMBOL_REF_TLS_MODEL (src);
      if (model != 0)
	src = frv_legitimize_tls_address (src, model);
    }

3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953
  switch (mode)
    {
    case SImode:
      if (frv_emit_movsi (dest, src))
	return;
      break;

    case QImode:
    case HImode:
    case DImode:
    case SFmode:
    case DFmode:
      if (!reload_in_progress
	  && !reload_completed
	  && !register_operand (dest, mode)
	  && !reg_or_0_operand (src, mode))
	src = copy_to_mode_reg (mode, src);
      break;

    default:
3954
      gcc_unreachable ();
3955 3956 3957 3958 3959
    }

  emit_insn (gen_rtx_SET (VOIDmode, dest, src));
}

bernds's avatar
bernds committed
3960 3961 3962 3963 3964
/* Emit code to handle a MOVSI, adding in the small data register or pic
   register if needed to load up addresses.  Return TRUE if the appropriate
   instructions are emitted.  */

int
3965
frv_emit_movsi (rtx dest, rtx src)
bernds's avatar
bernds committed
3966 3967
{
  int base_regno = -1;
3968 3969 3970
  int unspec = 0;
  rtx sym = src;
  struct frv_unspec old_unspec;
bernds's avatar
bernds committed
3971 3972 3973 3974 3975 3976 3977

  if (!reload_in_progress
      && !reload_completed
      && !register_operand (dest, SImode)
      && (!reg_or_0_operand (src, SImode)
	     /* Virtual registers will almost always be replaced by an
		add instruction, so expose this to CSE by copying to
3978
		an intermediate register.  */
bernds's avatar
bernds committed
3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994
	  || (GET_CODE (src) == REG
	      && IN_RANGE_P (REGNO (src),
			     FIRST_VIRTUAL_REGISTER,
			     LAST_VIRTUAL_REGISTER))))
    {
      emit_insn (gen_rtx_SET (VOIDmode, dest, copy_to_mode_reg (SImode, src)));
      return TRUE;
    }

  /* Explicitly add in the PIC or small data register if needed.  */
  switch (GET_CODE (src))
    {
    default:
      break;

    case LABEL_REF:
3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020
    handle_label:
      if (TARGET_FDPIC)
	{
	  /* Using GPREL12, we use a single GOT entry for all symbols
	     in read-only sections, but trade sequences such as:

	     sethi #gothi(label), gr#
	     setlo #gotlo(label), gr#
	     ld    @(gr15,gr#), gr#

	     for

	     ld    @(gr15,#got12(_gp)), gr#
	     sethi #gprelhi(label), gr##
	     setlo #gprello(label), gr##
	     add   gr#, gr##, gr##

	     We may often be able to share gr# for multiple
	     computations of GPREL addresses, and we may often fold
	     the final add into the pair of registers of a load or
	     store instruction, so it's often profitable.  Even when
	     optimizing for size, we're trading a GOT entry for an
	     additional instruction, which trades GOT space
	     (read-write) for code size (read-only, shareable), as
	     long as the symbol is not used in more than two different
	     locations.
4021

4022 4023
	     With -fpie/-fpic, we'd be trading a single load for a
	     sequence of 4 instructions, because the offset of the
kazu's avatar
gcc/  
kazu committed
4024
	     label can't be assumed to be addressable with 12 bits, so
4025 4026 4027 4028 4029 4030 4031
	     we don't do this.  */
	  if (TARGET_GPREL_RO)
	    unspec = R_FRV_GPREL12;
	  else
	    unspec = R_FRV_GOT12;
	}
      else if (flag_pic)
bernds's avatar
bernds committed
4032 4033 4034 4035 4036
	base_regno = PIC_REGNO;

      break;

    case CONST:
4037 4038
      if (frv_const_unspec_p (src, &old_unspec))
	break;
bernds's avatar
bernds committed
4039

4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060
      if (TARGET_FDPIC && frv_function_symbol_referenced_p (XEXP (src, 0)))
	{
	handle_whatever:
	  src = force_reg (GET_MODE (XEXP (src, 0)), XEXP (src, 0));
	  emit_move_insn (dest, src);
	  return TRUE;
	}
      else
	{
	  sym = XEXP (sym, 0);
	  if (GET_CODE (sym) == PLUS
	      && GET_CODE (XEXP (sym, 0)) == SYMBOL_REF
	      && GET_CODE (XEXP (sym, 1)) == CONST_INT)
	    sym = XEXP (sym, 0);
	  if (GET_CODE (sym) == SYMBOL_REF)
	    goto handle_sym;
	  else if (GET_CODE (sym) == LABEL_REF)
	    goto handle_label;
	  else
	    goto handle_whatever;
	}
bernds's avatar
bernds committed
4061 4062 4063
      break;

    case SYMBOL_REF:
4064 4065 4066
    handle_sym:
      if (TARGET_FDPIC)
	{
4067 4068 4069 4070 4071 4072 4073 4074 4075
	  enum tls_model model = SYMBOL_REF_TLS_MODEL (sym);

	  if (model != 0)
	    {
	      src = frv_legitimize_tls_address (src, model);
	      emit_move_insn (dest, src);
	      return TRUE;
	    }

4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119
	  if (SYMBOL_REF_FUNCTION_P (sym))
	    {
	      if (frv_local_funcdesc_p (sym))
		unspec = R_FRV_FUNCDESC_GOTOFF12;
	      else
		unspec = R_FRV_FUNCDESC_GOT12;
	    }
	  else
	    {
	      if (CONSTANT_POOL_ADDRESS_P (sym))
		switch (GET_CODE (get_pool_constant (sym)))
		  {
		  case CONST:
		  case SYMBOL_REF:
		  case LABEL_REF:
		    if (flag_pic)
		      {
			unspec = R_FRV_GOTOFF12;
			break;
		      }
		    /* Fall through.  */
		  default:
		    if (TARGET_GPREL_RO)
		      unspec = R_FRV_GPREL12;
		    else
		      unspec = R_FRV_GOT12;
		    break;
		  }
	      else if (SYMBOL_REF_LOCAL_P (sym)
		       && !SYMBOL_REF_EXTERNAL_P (sym)
		       && SYMBOL_REF_DECL (sym)
		       && (!DECL_P (SYMBOL_REF_DECL (sym))
			   || !DECL_COMMON (SYMBOL_REF_DECL (sym))))
		{
		  tree decl = SYMBOL_REF_DECL (sym);
		  tree init = TREE_CODE (decl) == VAR_DECL
		    ? DECL_INITIAL (decl)
		    : TREE_CODE (decl) == CONSTRUCTOR
		    ? decl : 0;
		  int reloc = 0;
		  bool named_section, readonly;

		  if (init && init != error_mark_node)
		    reloc = compute_reloc_for_constant (init);
4120

4121 4122 4123
		  named_section = TREE_CODE (decl) == VAR_DECL
		    && lookup_attribute ("section", DECL_ATTRIBUTES (decl));
		  readonly = decl_readonly_section (decl, reloc);
4124

4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139
		  if (named_section)
		    unspec = R_FRV_GOT12;
		  else if (!readonly)
		    unspec = R_FRV_GOTOFF12;
		  else if (readonly && TARGET_GPREL_RO)
		    unspec = R_FRV_GPREL12;
		  else
		    unspec = R_FRV_GOT12;
		}
	      else
		unspec = R_FRV_GOT12;
	    }
	}

      else if (SYMBOL_REF_SMALL_P (sym))
bernds's avatar
bernds committed
4140 4141 4142 4143 4144 4145 4146 4147 4148 4149
	base_regno = SDA_BASE_REG;

      else if (flag_pic)
	base_regno = PIC_REGNO;

      break;
    }

  if (base_regno >= 0)
    {
4150 4151 4152 4153 4154 4155 4156 4157
      if (GET_CODE (sym) == SYMBOL_REF && SYMBOL_REF_SMALL_P (sym))
	emit_insn (gen_symGOTOFF2reg (dest, src,
				      gen_rtx_REG (Pmode, base_regno),
				      GEN_INT (R_FRV_GPREL12)));
      else
	emit_insn (gen_symGOTOFF2reg_hilo (dest, src,
					   gen_rtx_REG (Pmode, base_regno),
					   GEN_INT (R_FRV_GPREL12)));
bernds's avatar
bernds committed
4158 4159
      if (base_regno == PIC_REGNO)
	cfun->uses_pic_offset_table = TRUE;
4160 4161
      return TRUE;
    }
bernds's avatar
bernds committed
4162

4163 4164 4165 4166 4167 4168
  if (unspec)
    {
      rtx x;

      /* Since OUR_FDPIC_REG is a pseudo register, we can't safely introduce
	 new uses of it once reload has begun.  */
4169
      gcc_assert (!reload_in_progress && !reload_completed);
4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203

      switch (unspec)
	{
	case R_FRV_GOTOFF12:
	  if (!frv_small_data_reloc_p (sym, unspec))
	    x = gen_symGOTOFF2reg_hilo (dest, src, OUR_FDPIC_REG,
					GEN_INT (unspec));
	  else
	    x = gen_symGOTOFF2reg (dest, src, OUR_FDPIC_REG, GEN_INT (unspec));
	  break;
	case R_FRV_GPREL12:
	  if (!frv_small_data_reloc_p (sym, unspec))
	    x = gen_symGPREL2reg_hilo (dest, src, OUR_FDPIC_REG,
				       GEN_INT (unspec));
	  else
	    x = gen_symGPREL2reg (dest, src, OUR_FDPIC_REG, GEN_INT (unspec));
	  break;
	case R_FRV_FUNCDESC_GOTOFF12:
	  if (flag_pic != 1)
	    x = gen_symGOTOFF2reg_hilo (dest, src, OUR_FDPIC_REG,
					GEN_INT (unspec));
	  else
	    x = gen_symGOTOFF2reg (dest, src, OUR_FDPIC_REG, GEN_INT (unspec));
	  break;
	default:
	  if (flag_pic != 1)
	    x = gen_symGOT2reg_hilo (dest, src, OUR_FDPIC_REG,
				     GEN_INT (unspec));
	  else
	    x = gen_symGOT2reg (dest, src, OUR_FDPIC_REG, GEN_INT (unspec));
	  break;
	}
      emit_insn (x);
      cfun->uses_pic_offset_table = TRUE;
bernds's avatar
bernds committed
4204 4205 4206
      return TRUE;
    }

4207

bernds's avatar
bernds committed
4208 4209 4210 4211 4212 4213 4214
  return FALSE;
}


/* Return a string to output a single word move.  */

const char *
4215
output_move_single (rtx operands[], rtx insn)
bernds's avatar
bernds committed
4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350
{
  rtx dest = operands[0];
  rtx src  = operands[1];

  if (GET_CODE (dest) == REG)
    {
      int dest_regno = REGNO (dest);
      enum machine_mode mode = GET_MODE (dest);

      if (GPR_P (dest_regno))
	{
	  if (GET_CODE (src) == REG)
	    {
	      /* gpr <- some sort of register */
	      int src_regno = REGNO (src);

	      if (GPR_P (src_regno))
		return "mov %1, %0";

	      else if (FPR_P (src_regno))
		return "movfg %1, %0";

	      else if (SPR_P (src_regno))
		return "movsg %1, %0";
	    }

	  else if (GET_CODE (src) == MEM)
	    {
	      /* gpr <- memory */
	      switch (mode)
		{
		default:
		  break;

		case QImode:
		  return "ldsb%I1%U1 %M1,%0";

		case HImode:
		  return "ldsh%I1%U1 %M1,%0";

		case SImode:
		case SFmode:
		  return "ld%I1%U1 %M1, %0";
		}
	    }

	  else if (GET_CODE (src) == CONST_INT
		   || GET_CODE (src) == CONST_DOUBLE)
	    {
	      /* gpr <- integer/floating constant */
	      HOST_WIDE_INT value;

	      if (GET_CODE (src) == CONST_INT)
		value = INTVAL (src);

	      else if (mode == SFmode)
		{
		  REAL_VALUE_TYPE rv;
		  long l;

		  REAL_VALUE_FROM_CONST_DOUBLE (rv, src);
		  REAL_VALUE_TO_TARGET_SINGLE (rv, l);
		  value = l;
		}

	      else
		value = CONST_DOUBLE_LOW (src);

	      if (IN_RANGE_P (value, -32768, 32767))
		return "setlos %1, %0";

	      return "#";
	    }

          else if (GET_CODE (src) == SYMBOL_REF
		   || GET_CODE (src) == LABEL_REF
		   || GET_CODE (src) == CONST)
	    {
	      return "#";
	    }
	}

      else if (FPR_P (dest_regno))
	{
	  if (GET_CODE (src) == REG)
	    {
	      /* fpr <- some sort of register */
	      int src_regno = REGNO (src);

	      if (GPR_P (src_regno))
		return "movgf %1, %0";

	      else if (FPR_P (src_regno))
		{
		  if (TARGET_HARD_FLOAT)
		    return "fmovs %1, %0";
		  else
		    return "mor %1, %1, %0";
		}
	    }

	  else if (GET_CODE (src) == MEM)
	    {
	      /* fpr <- memory */
	      switch (mode)
		{
		default:
		  break;

		case QImode:
		  return "ldbf%I1%U1 %M1,%0";

		case HImode:
		  return "ldhf%I1%U1 %M1,%0";

		case SImode:
		case SFmode:
		  return "ldf%I1%U1 %M1, %0";
		}
	    }

	  else if (ZERO_P (src))
	    return "movgf %., %0";
	}

      else if (SPR_P (dest_regno))
	{
	  if (GET_CODE (src) == REG)
	    {
	      /* spr <- some sort of register */
	      int src_regno = REGNO (src);

	      if (GPR_P (src_regno))
		return "movgs %1, %0";
	    }
4351 4352
	  else if (ZERO_P (src))
	    return "movgs %., %0";
bernds's avatar
bernds committed
4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421
	}
    }

  else if (GET_CODE (dest) == MEM)
    {
      if (GET_CODE (src) == REG)
	{
	  int src_regno = REGNO (src);
	  enum machine_mode mode = GET_MODE (dest);

	  if (GPR_P (src_regno))
	    {
	      switch (mode)
		{
		default:
		  break;

		case QImode:
		  return "stb%I0%U0 %1, %M0";

		case HImode:
		  return "sth%I0%U0 %1, %M0";

		case SImode:
		case SFmode:
		  return "st%I0%U0 %1, %M0";
		}
	    }

	  else if (FPR_P (src_regno))
	    {
	      switch (mode)
		{
		default:
		  break;

		case QImode:
		  return "stbf%I0%U0 %1, %M0";

		case HImode:
		  return "sthf%I0%U0 %1, %M0";

		case SImode:
		case SFmode:
		  return "stf%I0%U0 %1, %M0";
		}
	    }
	}

      else if (ZERO_P (src))
	{
	  switch (GET_MODE (dest))
	    {
	    default:
	      break;

	    case QImode:
	      return "stb%I0%U0 %., %M0";

	    case HImode:
	      return "sth%I0%U0 %., %M0";

	    case SImode:
	    case SFmode:
	      return "st%I0%U0 %., %M0";
	    }
	}
    }

4422
  fatal_insn ("bad output_move_single operand", insn);
bernds's avatar
bernds committed
4423 4424 4425 4426 4427 4428 4429
  return "";
}


/* Return a string to output a double word move.  */

const char *
4430
output_move_double (rtx operands[], rtx insn)
bernds's avatar
bernds committed
4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548
{
  rtx dest = operands[0];
  rtx src  = operands[1];
  enum machine_mode mode = GET_MODE (dest);

  if (GET_CODE (dest) == REG)
    {
      int dest_regno = REGNO (dest);

      if (GPR_P (dest_regno))
	{
	  if (GET_CODE (src) == REG)
	    {
	      /* gpr <- some sort of register */
	      int src_regno = REGNO (src);

	      if (GPR_P (src_regno))
		return "#";

	      else if (FPR_P (src_regno))
		{
		  if (((dest_regno - GPR_FIRST) & 1) == 0
		      && ((src_regno - FPR_FIRST) & 1) == 0)
		    return "movfgd %1, %0";

		  return "#";
		}
	    }

	  else if (GET_CODE (src) == MEM)
	    {
	      /* gpr <- memory */
	      if (dbl_memory_one_insn_operand (src, mode))
		return "ldd%I1%U1 %M1, %0";

	      return "#";
	    }

	  else if (GET_CODE (src) == CONST_INT
		   || GET_CODE (src) == CONST_DOUBLE)
	    return "#";
	}

      else if (FPR_P (dest_regno))
	{
	  if (GET_CODE (src) == REG)
	    {
	      /* fpr <- some sort of register */
	      int src_regno = REGNO (src);

	      if (GPR_P (src_regno))
		{
		  if (((dest_regno - FPR_FIRST) & 1) == 0
		      && ((src_regno - GPR_FIRST) & 1) == 0)
		    return "movgfd %1, %0";

		  return "#";
		}

	      else if (FPR_P (src_regno))
		{
		  if (TARGET_DOUBLE
		      && ((dest_regno - FPR_FIRST) & 1) == 0
		      && ((src_regno - FPR_FIRST) & 1) == 0)
		    return "fmovd %1, %0";

		  return "#";
		}
	    }

	  else if (GET_CODE (src) == MEM)
	    {
	      /* fpr <- memory */
	      if (dbl_memory_one_insn_operand (src, mode))
		return "lddf%I1%U1 %M1, %0";

	      return "#";
	    }

	  else if (ZERO_P (src))
	    return "#";
	}
    }

  else if (GET_CODE (dest) == MEM)
    {
      if (GET_CODE (src) == REG)
	{
	  int src_regno = REGNO (src);

	  if (GPR_P (src_regno))
	    {
	      if (((src_regno - GPR_FIRST) & 1) == 0
		  && dbl_memory_one_insn_operand (dest, mode))
		return "std%I0%U0 %1, %M0";

	      return "#";
	    }

	  if (FPR_P (src_regno))
	    {
	      if (((src_regno - FPR_FIRST) & 1) == 0
		  && dbl_memory_one_insn_operand (dest, mode))
		return "stdf%I0%U0 %1, %M0";

	      return "#";
	    }
	}

      else if (ZERO_P (src))
	{
	  if (dbl_memory_one_insn_operand (dest, mode))
	    return "std%I0%U0 %., %M0";

	  return "#";
	}
    }

4549
  fatal_insn ("bad output_move_double operand", insn);
bernds's avatar
bernds committed
4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560
  return "";
}


/* Return a string to output a single word conditional move.
   Operand0 -- EQ/NE of ccr register and 0
   Operand1 -- CCR register
   Operand2 -- destination
   Operand3 -- source  */

const char *
4561
output_condmove_single (rtx operands[], rtx insn)
bernds's avatar
bernds committed
4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690
{
  rtx dest = operands[2];
  rtx src  = operands[3];

  if (GET_CODE (dest) == REG)
    {
      int dest_regno = REGNO (dest);
      enum machine_mode mode = GET_MODE (dest);

      if (GPR_P (dest_regno))
	{
	  if (GET_CODE (src) == REG)
	    {
	      /* gpr <- some sort of register */
	      int src_regno = REGNO (src);

	      if (GPR_P (src_regno))
		return "cmov %z3, %2, %1, %e0";

	      else if (FPR_P (src_regno))
		return "cmovfg %3, %2, %1, %e0";
	    }

	  else if (GET_CODE (src) == MEM)
	    {
	      /* gpr <- memory */
	      switch (mode)
		{
		default:
		  break;

		case QImode:
		  return "cldsb%I3%U3 %M3, %2, %1, %e0";

		case HImode:
		  return "cldsh%I3%U3 %M3, %2, %1, %e0";

		case SImode:
		case SFmode:
		  return "cld%I3%U3 %M3, %2, %1, %e0";
		}
	    }

	  else if (ZERO_P (src))
	    return "cmov %., %2, %1, %e0";
	}

      else if (FPR_P (dest_regno))
	{
	  if (GET_CODE (src) == REG)
	    {
	      /* fpr <- some sort of register */
	      int src_regno = REGNO (src);

	      if (GPR_P (src_regno))
		return "cmovgf %3, %2, %1, %e0";

	      else if (FPR_P (src_regno))
		{
		  if (TARGET_HARD_FLOAT)
		    return "cfmovs %3,%2,%1,%e0";
		  else
		    return "cmor %3, %3, %2, %1, %e0";
		}
	    }

	  else if (GET_CODE (src) == MEM)
	    {
	      /* fpr <- memory */
	      if (mode == SImode || mode == SFmode)
		return "cldf%I3%U3 %M3, %2, %1, %e0";
	    }

	  else if (ZERO_P (src))
	    return "cmovgf %., %2, %1, %e0";
	}
    }

  else if (GET_CODE (dest) == MEM)
    {
      if (GET_CODE (src) == REG)
	{
	  int src_regno = REGNO (src);
	  enum machine_mode mode = GET_MODE (dest);

	  if (GPR_P (src_regno))
	    {
	      switch (mode)
		{
		default:
		  break;

		case QImode:
		  return "cstb%I2%U2 %3, %M2, %1, %e0";

		case HImode:
		  return "csth%I2%U2 %3, %M2, %1, %e0";

		case SImode:
		case SFmode:
		  return "cst%I2%U2 %3, %M2, %1, %e0";
		}
	    }

	  else if (FPR_P (src_regno) && (mode == SImode || mode == SFmode))
	    return "cstf%I2%U2 %3, %M2, %1, %e0";
	}

      else if (ZERO_P (src))
	{
	  enum machine_mode mode = GET_MODE (dest);
	  switch (mode)
	    {
	    default:
	      break;

	    case QImode:
	      return "cstb%I2%U2 %., %M2, %1, %e0";

	    case HImode:
	      return "csth%I2%U2 %., %M2, %1, %e0";

	    case SImode:
	    case SFmode:
	      return "cst%I2%U2 %., %M2, %1, %e0";
	    }
	}
    }

4691
  fatal_insn ("bad output_condmove_single operand", insn);
bernds's avatar
bernds committed
4692 4693 4694 4695 4696 4697 4698 4699
  return "";
}


/* Emit the appropriate code to do a comparison, returning the register the
   comparison was done it.  */

static rtx
4700
frv_emit_comparison (enum rtx_code test, rtx op0, rtx op1)
bernds's avatar
bernds committed
4701 4702 4703 4704
{
  enum machine_mode cc_mode;
  rtx cc_reg;

4705
  /* Floating point doesn't have comparison against a constant.  */
bernds's avatar
bernds committed
4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730
  if (GET_MODE (op0) == CC_FPmode && GET_CODE (op1) != REG)
    op1 = force_reg (GET_MODE (op0), op1);

  /* Possibly disable using anything but a fixed register in order to work
     around cse moving comparisons past function calls.  */
  cc_mode = SELECT_CC_MODE (test, op0, op1);
  cc_reg = ((TARGET_ALLOC_CC)
	    ? gen_reg_rtx (cc_mode)
	    : gen_rtx_REG (cc_mode,
			   (cc_mode == CC_FPmode) ? FCC_FIRST : ICC_FIRST));

  emit_insn (gen_rtx_SET (VOIDmode, cc_reg,
			  gen_rtx_COMPARE (cc_mode, op0, op1)));

  return cc_reg;
}


/* Emit code for a conditional branch.  The comparison operands were previously
   stored in frv_compare_op0 and frv_compare_op1.

   XXX: I originally wanted to add a clobber of a CCR register to use in
   conditional execution, but that confuses the rest of the compiler.  */

int
4731
frv_emit_cond_branch (enum rtx_code test, rtx label)
bernds's avatar
bernds committed
4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744
{
  rtx test_rtx;
  rtx label_ref;
  rtx if_else;
  rtx cc_reg = frv_emit_comparison (test, frv_compare_op0, frv_compare_op1);
  enum machine_mode cc_mode = GET_MODE (cc_reg);

  /* Branches generate:
	(set (pc)
	     (if_then_else (<test>, <cc_reg>, (const_int 0))
			    (label_ref <branch_label>)
			    (pc))) */
  label_ref = gen_rtx_LABEL_REF (VOIDmode, label);
4745
  test_rtx = gen_rtx_fmt_ee (test, cc_mode, cc_reg, const0_rtx);
bernds's avatar
bernds committed
4746 4747 4748 4749 4750 4751 4752 4753 4754 4755
  if_else = gen_rtx_IF_THEN_ELSE (cc_mode, test_rtx, label_ref, pc_rtx);
  emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, if_else));
  return TRUE;
}


/* Emit code to set a gpr to 1/0 based on a comparison.  The comparison
   operands were previously stored in frv_compare_op0 and frv_compare_op1.  */

int
4756
frv_emit_scc (enum rtx_code test, rtx target)
bernds's avatar
bernds committed
4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783
{
  rtx set;
  rtx test_rtx;
  rtx clobber;
  rtx cr_reg;
  rtx cc_reg = frv_emit_comparison (test, frv_compare_op0, frv_compare_op1);

  /* SCC instructions generate:
	(parallel [(set <target> (<test>, <cc_reg>, (const_int 0))
		   (clobber (<ccr_reg>))])  */
  test_rtx = gen_rtx_fmt_ee (test, SImode, cc_reg, const0_rtx);
  set = gen_rtx_SET (VOIDmode, target, test_rtx);

  cr_reg = ((TARGET_ALLOC_CC)
	    ? gen_reg_rtx (CC_CCRmode)
	    : gen_rtx_REG (CC_CCRmode,
			   ((GET_MODE (cc_reg) == CC_FPmode)
			    ? FCR_FIRST
			    : ICR_FIRST)));

  clobber = gen_rtx_CLOBBER (VOIDmode, cr_reg);
  emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, set, clobber)));
  return TRUE;
}


/* Split a SCC instruction into component parts, returning a SEQUENCE to hold
kazu's avatar
kazu committed
4784
   the separate insns.  */
bernds's avatar
bernds committed
4785 4786

rtx
4787
frv_split_scc (rtx dest, rtx test, rtx cc_reg, rtx cr_reg, HOST_WIDE_INT value)
bernds's avatar
bernds committed
4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821
{
  rtx ret;

  start_sequence ();

  /* Set the appropriate CCR bit.  */
  emit_insn (gen_rtx_SET (VOIDmode,
			  cr_reg,
			  gen_rtx_fmt_ee (GET_CODE (test),
					  GET_MODE (cr_reg),
					  cc_reg,
					  const0_rtx)));

  /* Move the value into the destination.  */
  emit_move_insn (dest, GEN_INT (value));

  /* Move 0 into the destination if the test failed */
  emit_insn (gen_rtx_COND_EXEC (VOIDmode,
				gen_rtx_EQ (GET_MODE (cr_reg),
					    cr_reg,
					    const0_rtx),
				gen_rtx_SET (VOIDmode, dest, const0_rtx)));

  /* Finish up, return sequence.  */
  ret = get_insns ();
  end_sequence ();
  return ret;
}


/* Emit the code for a conditional move, return TRUE if we could do the
   move.  */

int
4822
frv_emit_cond_move (rtx dest, rtx test_rtx, rtx src1, rtx src2)
bernds's avatar
bernds committed
4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845
{
  rtx set;
  rtx clobber_cc;
  rtx test2;
  rtx cr_reg;
  rtx if_rtx;
  enum rtx_code test = GET_CODE (test_rtx);
  rtx cc_reg = frv_emit_comparison (test, frv_compare_op0, frv_compare_op1);
  enum machine_mode cc_mode = GET_MODE (cc_reg);

  /* Conditional move instructions generate:
	(parallel [(set <target>
			(if_then_else (<test> <cc_reg> (const_int 0))
				      <src1>
				      <src2>))
		   (clobber (<ccr_reg>))])  */

  /* Handle various cases of conditional move involving two constants.  */
  if (GET_CODE (src1) == CONST_INT && GET_CODE (src2) == CONST_INT)
    {
      HOST_WIDE_INT value1 = INTVAL (src1);
      HOST_WIDE_INT value2 = INTVAL (src2);

4846
      /* Having 0 as one of the constants can be done by loading the other
bernds's avatar
bernds committed
4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894
         constant, and optionally moving in gr0.  */
      if (value1 == 0 || value2 == 0)
	;

      /* If the first value is within an addi range and also the difference
         between the two fits in an addi's range, load up the difference, then
         conditionally move in 0, and then unconditionally add the first
	 value.  */
      else if (IN_RANGE_P (value1, -2048, 2047)
	       && IN_RANGE_P (value2 - value1, -2048, 2047))
	;

      /* If neither condition holds, just force the constant into a
	 register.  */
      else
	{
	  src1 = force_reg (GET_MODE (dest), src1);
	  src2 = force_reg (GET_MODE (dest), src2);
	}
    }

  /* If one value is a register, insure the other value is either 0 or a
     register.  */
  else
    {
      if (GET_CODE (src1) == CONST_INT && INTVAL (src1) != 0)
	src1 = force_reg (GET_MODE (dest), src1);

      if (GET_CODE (src2) == CONST_INT && INTVAL (src2) != 0)
	src2 = force_reg (GET_MODE (dest), src2);
    }

  test2 = gen_rtx_fmt_ee (test, cc_mode, cc_reg, const0_rtx);
  if_rtx = gen_rtx_IF_THEN_ELSE (GET_MODE (dest), test2, src1, src2);

  set = gen_rtx_SET (VOIDmode, dest, if_rtx);

  cr_reg = ((TARGET_ALLOC_CC)
	    ? gen_reg_rtx (CC_CCRmode)
	    : gen_rtx_REG (CC_CCRmode,
			   (cc_mode == CC_FPmode) ? FCR_FIRST : ICR_FIRST));

  clobber_cc = gen_rtx_CLOBBER (VOIDmode, cr_reg);
  emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, set, clobber_cc)));
  return TRUE;
}


kazu's avatar
kazu committed
4895
/* Split a conditional move into constituent parts, returning a SEQUENCE
bernds's avatar
bernds committed
4896 4897 4898
   containing all of the insns.  */

rtx
4899
frv_split_cond_move (rtx operands[])
bernds's avatar
bernds committed
4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925
{
  rtx dest	= operands[0];
  rtx test	= operands[1];
  rtx cc_reg	= operands[2];
  rtx src1	= operands[3];
  rtx src2	= operands[4];
  rtx cr_reg	= operands[5];
  rtx ret;
  enum machine_mode cr_mode = GET_MODE (cr_reg);

  start_sequence ();

  /* Set the appropriate CCR bit.  */
  emit_insn (gen_rtx_SET (VOIDmode,
			  cr_reg,
			  gen_rtx_fmt_ee (GET_CODE (test),
					  GET_MODE (cr_reg),
					  cc_reg,
					  const0_rtx)));

  /* Handle various cases of conditional move involving two constants.  */
  if (GET_CODE (src1) == CONST_INT && GET_CODE (src2) == CONST_INT)
    {
      HOST_WIDE_INT value1 = INTVAL (src1);
      HOST_WIDE_INT value2 = INTVAL (src2);

4926
      /* Having 0 as one of the constants can be done by loading the other
bernds's avatar
bernds committed
4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966
         constant, and optionally moving in gr0.  */
      if (value1 == 0)
	{
	  emit_move_insn (dest, src2);
	  emit_insn (gen_rtx_COND_EXEC (VOIDmode,
					gen_rtx_NE (cr_mode, cr_reg,
						    const0_rtx),
					gen_rtx_SET (VOIDmode, dest, src1)));
	}

      else if (value2 == 0)
	{
	  emit_move_insn (dest, src1);
	  emit_insn (gen_rtx_COND_EXEC (VOIDmode,
					gen_rtx_EQ (cr_mode, cr_reg,
						    const0_rtx),
					gen_rtx_SET (VOIDmode, dest, src2)));
	}

      /* If the first value is within an addi range and also the difference
         between the two fits in an addi's range, load up the difference, then
         conditionally move in 0, and then unconditionally add the first
	 value.  */
      else if (IN_RANGE_P (value1, -2048, 2047)
	       && IN_RANGE_P (value2 - value1, -2048, 2047))
	{
	  rtx dest_si = ((GET_MODE (dest) == SImode)
			 ? dest
			 : gen_rtx_SUBREG (SImode, dest, 0));

	  emit_move_insn (dest_si, GEN_INT (value2 - value1));
	  emit_insn (gen_rtx_COND_EXEC (VOIDmode,
					gen_rtx_NE (cr_mode, cr_reg,
						    const0_rtx),
					gen_rtx_SET (VOIDmode, dest_si,
						     const0_rtx)));
	  emit_insn (gen_addsi3 (dest_si, dest_si, src1));
	}

      else
4967
	gcc_unreachable ();
bernds's avatar
bernds committed
4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993
    }
  else
    {
      /* Emit the conditional move for the test being true if needed.  */
      if (! rtx_equal_p (dest, src1))
	emit_insn (gen_rtx_COND_EXEC (VOIDmode,
				      gen_rtx_NE (cr_mode, cr_reg, const0_rtx),
				      gen_rtx_SET (VOIDmode, dest, src1)));

      /* Emit the conditional move for the test being false if needed.  */
      if (! rtx_equal_p (dest, src2))
	emit_insn (gen_rtx_COND_EXEC (VOIDmode,
				      gen_rtx_EQ (cr_mode, cr_reg, const0_rtx),
				      gen_rtx_SET (VOIDmode, dest, src2)));
    }

  /* Finish up, return sequence.  */
  ret = get_insns ();
  end_sequence ();
  return ret;
}


/* Split (set DEST SOURCE), where DEST is a double register and SOURCE is a
   memory location that is not known to be dword-aligned.  */
void
4994
frv_split_double_load (rtx dest, rtx source)
bernds's avatar
bernds committed
4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029
{
  int regno = REGNO (dest);
  rtx dest1 = gen_highpart (SImode, dest);
  rtx dest2 = gen_lowpart (SImode, dest);
  rtx address = XEXP (source, 0);

  /* If the address is pre-modified, load the lower-numbered register
     first, then load the other register using an integer offset from
     the modified base register.  This order should always be safe,
     since the pre-modification cannot affect the same registers as the
     load does.

     The situation for other loads is more complicated.  Loading one
     of the registers could affect the value of ADDRESS, so we must
     be careful which order we do them in.  */
  if (GET_CODE (address) == PRE_MODIFY
      || ! refers_to_regno_p (regno, regno + 1, address, NULL))
    {
      /* It is safe to load the lower-numbered register first.  */
      emit_move_insn (dest1, change_address (source, SImode, NULL));
      emit_move_insn (dest2, frv_index_memory (source, SImode, 1));
    }
  else
    {
      /* ADDRESS is not pre-modified and the address depends on the
         lower-numbered register.  Load the higher-numbered register
         first.  */
      emit_move_insn (dest2, frv_index_memory (source, SImode, 1));
      emit_move_insn (dest1, change_address (source, SImode, NULL));
    }
}

/* Split (set DEST SOURCE), where DEST refers to a dword memory location
   and SOURCE is either a double register or the constant zero.  */
void
5030
frv_split_double_store (rtx dest, rtx source)
bernds's avatar
bernds committed
5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050
{
  rtx dest1 = change_address (dest, SImode, NULL);
  rtx dest2 = frv_index_memory (dest, SImode, 1);
  if (ZERO_P (source))
    {
      emit_move_insn (dest1, CONST0_RTX (SImode));
      emit_move_insn (dest2, CONST0_RTX (SImode));
    }
  else
    {
      emit_move_insn (dest1, gen_highpart (SImode, source));
      emit_move_insn (dest2, gen_lowpart (SImode, source));
    }
}


/* Split a min/max operation returning a SEQUENCE containing all of the
   insns.  */

rtx
5051
frv_split_minmax (rtx operands[])
bernds's avatar
bernds committed
5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064
{
  rtx dest	= operands[0];
  rtx minmax	= operands[1];
  rtx src1	= operands[2];
  rtx src2	= operands[3];
  rtx cc_reg	= operands[4];
  rtx cr_reg	= operands[5];
  rtx ret;
  enum rtx_code test_code;
  enum machine_mode cr_mode = GET_MODE (cr_reg);

  start_sequence ();

5065
  /* Figure out which test to use.  */
bernds's avatar
bernds committed
5066 5067 5068
  switch (GET_CODE (minmax))
    {
    default:
5069
      gcc_unreachable ();
bernds's avatar
bernds committed
5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090

    case SMIN: test_code = LT;  break;
    case SMAX: test_code = GT;  break;
    case UMIN: test_code = LTU; break;
    case UMAX: test_code = GTU; break;
    }

  /* Issue the compare instruction.  */
  emit_insn (gen_rtx_SET (VOIDmode,
			  cc_reg,
			  gen_rtx_COMPARE (GET_MODE (cc_reg),
					   src1, src2)));

  /* Set the appropriate CCR bit.  */
  emit_insn (gen_rtx_SET (VOIDmode,
			  cr_reg,
			  gen_rtx_fmt_ee (test_code,
					  GET_MODE (cr_reg),
					  cc_reg,
					  const0_rtx)));

5091
  /* If are taking the min/max of a nonzero constant, load that first, and
bernds's avatar
bernds committed
5092 5093 5094
     then do a conditional move of the other value.  */
  if (GET_CODE (src2) == CONST_INT && INTVAL (src2) != 0)
    {
5095
      gcc_assert (!rtx_equal_p (dest, src1));
bernds's avatar
bernds committed
5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129

      emit_move_insn (dest, src2);
      emit_insn (gen_rtx_COND_EXEC (VOIDmode,
				    gen_rtx_NE (cr_mode, cr_reg, const0_rtx),
				    gen_rtx_SET (VOIDmode, dest, src1)));
    }

  /* Otherwise, do each half of the move.  */
  else
    {
      /* Emit the conditional move for the test being true if needed.  */
      if (! rtx_equal_p (dest, src1))
	emit_insn (gen_rtx_COND_EXEC (VOIDmode,
				      gen_rtx_NE (cr_mode, cr_reg, const0_rtx),
				      gen_rtx_SET (VOIDmode, dest, src1)));

      /* Emit the conditional move for the test being false if needed.  */
      if (! rtx_equal_p (dest, src2))
	emit_insn (gen_rtx_COND_EXEC (VOIDmode,
				      gen_rtx_EQ (cr_mode, cr_reg, const0_rtx),
				      gen_rtx_SET (VOIDmode, dest, src2)));
    }

  /* Finish up, return sequence.  */
  ret = get_insns ();
  end_sequence ();
  return ret;
}


/* Split an integer abs operation returning a SEQUENCE containing all of the
   insns.  */

rtx
5130
frv_split_abs (rtx operands[])
bernds's avatar
bernds committed
5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149
{
  rtx dest	= operands[0];
  rtx src	= operands[1];
  rtx cc_reg	= operands[2];
  rtx cr_reg	= operands[3];
  rtx ret;

  start_sequence ();

  /* Issue the compare < 0 instruction.  */
  emit_insn (gen_rtx_SET (VOIDmode,
			  cc_reg,
			  gen_rtx_COMPARE (CCmode, src, const0_rtx)));

  /* Set the appropriate CCR bit.  */
  emit_insn (gen_rtx_SET (VOIDmode,
			  cr_reg,
			  gen_rtx_fmt_ee (LT, CC_CCRmode, cc_reg, const0_rtx)));

5150
  /* Emit the conditional negate if the value is negative.  */
bernds's avatar
bernds committed
5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171
  emit_insn (gen_rtx_COND_EXEC (VOIDmode,
				gen_rtx_NE (CC_CCRmode, cr_reg, const0_rtx),
				gen_negsi2 (dest, src)));

  /* Emit the conditional move for the test being false if needed.  */
  if (! rtx_equal_p (dest, src))
    emit_insn (gen_rtx_COND_EXEC (VOIDmode,
				  gen_rtx_EQ (CC_CCRmode, cr_reg, const0_rtx),
				  gen_rtx_SET (VOIDmode, dest, src)));

  /* Finish up, return sequence.  */
  ret = get_insns ();
  end_sequence ();
  return ret;
}


/* An internal function called by for_each_rtx to clear in a hard_reg set each
   register used in an insn.  */

static int
5172
frv_clear_registers_used (rtx *ptr, void *data)
bernds's avatar
bernds committed
5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199
{
  if (GET_CODE (*ptr) == REG)
    {
      int regno = REGNO (*ptr);
      HARD_REG_SET *p_regs = (HARD_REG_SET *)data;

      if (regno < FIRST_PSEUDO_REGISTER)
	{
	  int reg_max = regno + HARD_REGNO_NREGS (regno, GET_MODE (*ptr));

	  while (regno < reg_max)
	    {
	      CLEAR_HARD_REG_BIT (*p_regs, regno);
	      regno++;
	    }
	}
    }

  return 0;
}


/* Initialize the extra fields provided by IFCVT_EXTRA_FIELDS.  */

/* On the FR-V, we don't have any extra fields per se, but it is useful hook to
   initialize the static storage.  */
void
5200
frv_ifcvt_init_extra_fields (ce_if_block_t *ce_info ATTRIBUTE_UNUSED)
bernds's avatar
bernds committed
5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212
{
  frv_ifcvt.added_insns_list = NULL_RTX;
  frv_ifcvt.cur_scratch_regs = 0;
  frv_ifcvt.num_nested_cond_exec = 0;
  frv_ifcvt.cr_reg = NULL_RTX;
  frv_ifcvt.nested_cc_reg = NULL_RTX;
  frv_ifcvt.extra_int_cr = NULL_RTX;
  frv_ifcvt.extra_fp_cr = NULL_RTX;
  frv_ifcvt.last_nested_if_cr = NULL_RTX;
}


5213
/* Internal function to add a potential insn to the list of insns to be inserted
bernds's avatar
bernds committed
5214 5215 5216
   if the conditional execution conversion is successful.  */

static void
5217
frv_ifcvt_add_insn (rtx pattern, rtx insn, int before_p)
bernds's avatar
bernds committed
5218 5219 5220
{
  rtx link = alloc_EXPR_LIST (VOIDmode, pattern, insn);

5221
  link->jump = before_p;	/* Mark to add this before or after insn.  */
bernds's avatar
bernds committed
5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243
  frv_ifcvt.added_insns_list = alloc_EXPR_LIST (VOIDmode, link,
						frv_ifcvt.added_insns_list);

  if (TARGET_DEBUG_COND_EXEC)
    {
      fprintf (stderr,
	       "\n:::::::::: frv_ifcvt_add_insn: add the following %s insn %d:\n",
	       (before_p) ? "before" : "after",
	       (int)INSN_UID (insn));

      debug_rtx (pattern);
    }
}


/* A C expression to modify the code described by the conditional if
   information CE_INFO, possibly updating the tests in TRUE_EXPR, and
   FALSE_EXPR for converting if-then and if-then-else code to conditional
   instructions.  Set either TRUE_EXPR or FALSE_EXPR to a null pointer if the
   tests cannot be converted.  */

void
5244
frv_ifcvt_modify_tests (ce_if_block_t *ce_info, rtx *p_true, rtx *p_false)
bernds's avatar
bernds committed
5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267
{
  basic_block test_bb = ce_info->test_bb;	/* test basic block */
  basic_block then_bb = ce_info->then_bb;	/* THEN */
  basic_block else_bb = ce_info->else_bb;	/* ELSE or NULL */
  basic_block join_bb = ce_info->join_bb;	/* join block or NULL */
  rtx true_expr = *p_true;
  rtx cr;
  rtx cc;
  rtx nested_cc;
  enum machine_mode mode = GET_MODE (true_expr);
  int j;
  basic_block *bb;
  int num_bb;
  frv_tmp_reg_t *tmp_reg = &frv_ifcvt.tmp_reg;
  rtx check_insn;
  rtx sub_cond_exec_reg;
  enum rtx_code code;
  enum rtx_code code_true;
  enum rtx_code code_false;
  enum reg_class cc_class;
  enum reg_class cr_class;
  int cc_first;
  int cc_last;
5268
  reg_set_iterator rsi;
bernds's avatar
bernds committed
5269 5270 5271 5272

  /* Make sure we are only dealing with hard registers.  Also honor the
     -mno-cond-exec switch, and -mno-nested-cond-exec switches if
     applicable.  */
5273 5274
  if (!reload_completed || !TARGET_COND_EXEC
      || (!TARGET_NESTED_CE && ce_info->pass > 1))
bernds's avatar
bernds committed
5275 5276 5277 5278 5279
    goto fail;

  /* Figure out which registers we can allocate for our own purposes.  Only
     consider registers that are not preserved across function calls and are
     not fixed.  However, allow the ICC/ICR temporary registers to be allocated
5280
     if we did not need to use them in reloading other registers.  */
ghazi's avatar
cp:  
ghazi committed
5281
  memset (&tmp_reg->regs, 0, sizeof (tmp_reg->regs));
bernds's avatar
bernds committed
5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296
  COPY_HARD_REG_SET (tmp_reg->regs, call_used_reg_set);
  AND_COMPL_HARD_REG_SET (tmp_reg->regs, fixed_reg_set);
  SET_HARD_REG_BIT (tmp_reg->regs, ICC_TEMP);
  SET_HARD_REG_BIT (tmp_reg->regs, ICR_TEMP);

  /* If this is a nested IF, we need to discover whether the CC registers that
     are set/used inside of the block are used anywhere else.  If not, we can
     change them to be the CC register that is paired with the CR register that
     controls the outermost IF block.  */
  if (ce_info->pass > 1)
    {
      CLEAR_HARD_REG_SET (frv_ifcvt.nested_cc_ok_rewrite);
      for (j = CC_FIRST; j <= CC_LAST; j++)
	if (TEST_HARD_REG_BIT (tmp_reg->regs, j))
	  {
5297
	    if (REGNO_REG_SET_P (df_get_live_in (then_bb), j))
bernds's avatar
bernds committed
5298 5299
	      continue;

hubicka's avatar
 
hubicka committed
5300
	    if (else_bb
5301
		&& REGNO_REG_SET_P (df_get_live_in (else_bb), j))
bernds's avatar
bernds committed
5302 5303
	      continue;

hubicka's avatar
 
hubicka committed
5304
	    if (join_bb
5305
		&& REGNO_REG_SET_P (df_get_live_in (join_bb), j))
bernds's avatar
bernds committed
5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322
	      continue;

	    SET_HARD_REG_BIT (frv_ifcvt.nested_cc_ok_rewrite, j);
	  }
    }

  for (j = 0; j < frv_ifcvt.cur_scratch_regs; j++)
    frv_ifcvt.scratch_regs[j] = NULL_RTX;

  frv_ifcvt.added_insns_list = NULL_RTX;
  frv_ifcvt.cur_scratch_regs = 0;

  bb = (basic_block *) alloca ((2 + ce_info->num_multiple_test_blocks)
			       * sizeof (basic_block));

  if (join_bb)
    {
5323
      unsigned int regno;
bernds's avatar
bernds committed
5324 5325 5326

      /* Remove anything live at the beginning of the join block from being
         available for allocation.  */
5327
      EXECUTE_IF_SET_IN_REG_SET (df_get_live_in (join_bb), 0, regno, rsi)
5328 5329 5330 5331
	{
	  if (regno < FIRST_PSEUDO_REGISTER)
	    CLEAR_HARD_REG_BIT (tmp_reg->regs, regno);
	}
bernds's avatar
bernds committed
5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342
    }

  /* Add in all of the blocks in multiple &&/|| blocks to be scanned.  */
  num_bb = 0;
  if (ce_info->num_multiple_test_blocks)
    {
      basic_block multiple_test_bb = ce_info->last_test_bb;

      while (multiple_test_bb != test_bb)
	{
	  bb[num_bb++] = multiple_test_bb;
bje's avatar
bje committed
5343
	  multiple_test_bb = EDGE_PRED (multiple_test_bb, 0)->src;
bernds's avatar
bernds committed
5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357
	}
    }

  /* Add in the THEN and ELSE blocks to be scanned.  */
  bb[num_bb++] = then_bb;
  if (else_bb)
    bb[num_bb++] = else_bb;

  sub_cond_exec_reg = NULL_RTX;
  frv_ifcvt.num_nested_cond_exec = 0;

  /* Scan all of the blocks for registers that must not be allocated.  */
  for (j = 0; j < num_bb; j++)
    {
5358 5359
      rtx last_insn = BB_END (bb[j]);
      rtx insn = BB_HEAD (bb[j]);
5360
      unsigned int regno;
bernds's avatar
bernds committed
5361

5362 5363
      if (dump_file)
	fprintf (dump_file, "Scanning %s block %d, start %d, end %d\n",
bernds's avatar
bernds committed
5364 5365
		 (bb[j] == else_bb) ? "else" : ((bb[j] == then_bb) ? "then" : "test"),
		 (int) bb[j]->index,
5366 5367
		 (int) INSN_UID (BB_HEAD (bb[j])),
		 (int) INSN_UID (BB_END (bb[j])));
bernds's avatar
bernds committed
5368 5369 5370

      /* Anything live at the beginning of the block is obviously unavailable
         for allocation.  */
5371
      EXECUTE_IF_SET_IN_REG_SET (df_get_live_in (bb[j]), 0, regno, rsi)
5372 5373 5374 5375
	{
	  if (regno < FIRST_PSEUDO_REGISTER)
	    CLEAR_HARD_REG_BIT (tmp_reg->regs, regno);
	}
bernds's avatar
bernds committed
5376

5377
      /* Loop through the insns in the block.  */
bernds's avatar
bernds committed
5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419
      for (;;)
	{
	  /* Mark any new registers that are created as being unavailable for
             allocation.  Also see if the CC register used in nested IFs can be
             reallocated.  */
	  if (INSN_P (insn))
	    {
	      rtx pattern;
	      rtx set;
	      int skip_nested_if = FALSE;

	      for_each_rtx (&PATTERN (insn), frv_clear_registers_used,
			    (void *)&tmp_reg->regs);

	      pattern = PATTERN (insn);
	      if (GET_CODE (pattern) == COND_EXEC)
		{
		  rtx reg = XEXP (COND_EXEC_TEST (pattern), 0);

		  if (reg != sub_cond_exec_reg)
		    {
		      sub_cond_exec_reg = reg;
		      frv_ifcvt.num_nested_cond_exec++;
		    }
		}

	      set = single_set_pattern (pattern);
	      if (set)
		{
		  rtx dest = SET_DEST (set);
		  rtx src = SET_SRC (set);

		  if (GET_CODE (dest) == REG)
		    {
		      int regno = REGNO (dest);
		      enum rtx_code src_code = GET_CODE (src);

		      if (CC_P (regno) && src_code == COMPARE)
			skip_nested_if = TRUE;

		      else if (CR_P (regno)
			       && (src_code == IF_THEN_ELSE
5420
				   || COMPARISON_P (src)))
bernds's avatar
bernds committed
5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448
			skip_nested_if = TRUE;
		    }
		}

	      if (! skip_nested_if)
		for_each_rtx (&PATTERN (insn), frv_clear_registers_used,
			      (void *)&frv_ifcvt.nested_cc_ok_rewrite);
	    }

	  if (insn == last_insn)
	    break;

	  insn = NEXT_INSN (insn);
	}
    }

  /* If this is a nested if, rewrite the CC registers that are available to
     include the ones that can be rewritten, to increase the chance of being
     able to allocate a paired CC/CR register combination.  */
  if (ce_info->pass > 1)
    {
      for (j = CC_FIRST; j <= CC_LAST; j++)
	if (TEST_HARD_REG_BIT (frv_ifcvt.nested_cc_ok_rewrite, j))
	  SET_HARD_REG_BIT (tmp_reg->regs, j);
	else
	  CLEAR_HARD_REG_BIT (tmp_reg->regs, j);
    }

5449
  if (dump_file)
bernds's avatar
bernds committed
5450 5451
    {
      int num_gprs = 0;
5452
      fprintf (dump_file, "Available GPRs: ");
bernds's avatar
bernds committed
5453 5454 5455 5456

      for (j = GPR_FIRST; j <= GPR_LAST; j++)
	if (TEST_HARD_REG_BIT (tmp_reg->regs, j))
	  {
5457
	    fprintf (dump_file, " %d [%s]", j, reg_names[j]);
bernds's avatar
bernds committed
5458 5459 5460 5461
	    if (++num_gprs > GPR_TEMP_NUM+2)
	      break;
	  }

5462
      fprintf (dump_file, "%s\nAvailable CRs:  ",
bernds's avatar
bernds committed
5463 5464 5465 5466
	       (num_gprs > GPR_TEMP_NUM+2) ? " ..." : "");

      for (j = CR_FIRST; j <= CR_LAST; j++)
	if (TEST_HARD_REG_BIT (tmp_reg->regs, j))
5467
	  fprintf (dump_file, " %d [%s]", j, reg_names[j]);
bernds's avatar
bernds committed
5468

5469
      fputs ("\n", dump_file);
bernds's avatar
bernds committed
5470 5471 5472

      if (ce_info->pass > 1)
	{
5473
	  fprintf (dump_file, "Modifiable CCs: ");
bernds's avatar
bernds committed
5474 5475
	  for (j = CC_FIRST; j <= CC_LAST; j++)
	    if (TEST_HARD_REG_BIT (tmp_reg->regs, j))
5476
	      fprintf (dump_file, " %d [%s]", j, reg_names[j]);
bernds's avatar
bernds committed
5477

5478
	  fprintf (dump_file, "\n%d nested COND_EXEC statements\n",
bernds's avatar
bernds committed
5479 5480 5481 5482 5483 5484 5485
		   frv_ifcvt.num_nested_cond_exec);
	}
    }

  /* Allocate the appropriate temporary condition code register.  Try to
     allocate the ICR/FCR register that corresponds to the ICC/FCC register so
     that conditional cmp's can be done.  */
5486
  if (mode == CCmode || mode == CC_UNSmode || mode == CC_NZmode)
bernds's avatar
bernds committed
5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534
    {
      cr_class = ICR_REGS;
      cc_class = ICC_REGS;
      cc_first = ICC_FIRST;
      cc_last = ICC_LAST;
    }
  else if (mode == CC_FPmode)
    {
      cr_class = FCR_REGS;
      cc_class = FCC_REGS;
      cc_first = FCC_FIRST;
      cc_last = FCC_LAST;
    }
  else
    {
      cc_first = cc_last = 0;
      cr_class = cc_class = NO_REGS;
    }

  cc = XEXP (true_expr, 0);
  nested_cc = cr = NULL_RTX;
  if (cc_class != NO_REGS)
    {
      /* For nested IFs and &&/||, see if we can find a CC and CR register pair
         so we can execute a csubcc/caddcc/cfcmps instruction.  */
      int cc_regno;

      for (cc_regno = cc_first; cc_regno <= cc_last; cc_regno++)
	{
	  int cr_regno = cc_regno - CC_FIRST + CR_FIRST;

	  if (TEST_HARD_REG_BIT (frv_ifcvt.tmp_reg.regs, cc_regno)
	      && TEST_HARD_REG_BIT (frv_ifcvt.tmp_reg.regs, cr_regno))
	    {
	      frv_ifcvt.tmp_reg.next_reg[ (int)cr_class ] = cr_regno;
	      cr = frv_alloc_temp_reg (tmp_reg, cr_class, CC_CCRmode, TRUE,
				       TRUE);

	      frv_ifcvt.tmp_reg.next_reg[ (int)cc_class ] = cc_regno;
	      nested_cc = frv_alloc_temp_reg (tmp_reg, cc_class, CCmode,
						  TRUE, TRUE);
	      break;
	    }
	}
    }

  if (! cr)
    {
5535 5536
      if (dump_file)
	fprintf (dump_file, "Could not allocate a CR temporary register\n");
bernds's avatar
bernds committed
5537 5538 5539 5540

      goto fail;
    }

5541 5542
  if (dump_file)
    fprintf (dump_file,
bernds's avatar
bernds committed
5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566
	     "Will use %s for conditional execution, %s for nested comparisons\n",
	     reg_names[ REGNO (cr)],
	     (nested_cc) ? reg_names[ REGNO (nested_cc) ] : "<none>");

  /* Set the CCR bit.  Note for integer tests, we reverse the condition so that
     in an IF-THEN-ELSE sequence, we are testing the TRUE case against the CCR
     bit being true.  We don't do this for floating point, because of NaNs.  */
  code = GET_CODE (true_expr);
  if (GET_MODE (cc) != CC_FPmode)
    {
      code = reverse_condition (code);
      code_true = EQ;
      code_false = NE;
    }
  else
    {
      code_true = NE;
      code_false = EQ;
    }

  check_insn = gen_rtx_SET (VOIDmode, cr,
			    gen_rtx_fmt_ee (code, CC_CCRmode, cc, const0_rtx));

  /* Record the check insn to be inserted later.  */
5567
  frv_ifcvt_add_insn (check_insn, BB_END (test_bb), TRUE);
bernds's avatar
bernds committed
5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579

  /* Update the tests.  */
  frv_ifcvt.cr_reg = cr;
  frv_ifcvt.nested_cc_reg = nested_cc;
  *p_true = gen_rtx_fmt_ee (code_true, CC_CCRmode, cr, const0_rtx);
  *p_false = gen_rtx_fmt_ee (code_false, CC_CCRmode, cr, const0_rtx);
  return;

  /* Fail, don't do this conditional execution.  */
 fail:
  *p_true = NULL_RTX;
  *p_false = NULL_RTX;
5580 5581
  if (dump_file)
    fprintf (dump_file, "Disabling this conditional execution.\n");
bernds's avatar
bernds committed
5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600

  return;
}


/* A C expression to modify the code described by the conditional if
   information CE_INFO, for the basic block BB, possibly updating the tests in
   TRUE_EXPR, and FALSE_EXPR for converting the && and || parts of if-then or
   if-then-else code to conditional instructions.  Set either TRUE_EXPR or
   FALSE_EXPR to a null pointer if the tests cannot be converted.  */

/* p_true and p_false are given expressions of the form:

	(and (eq:CC_CCR (reg:CC_CCR)
			(const_int 0))
	     (eq:CC (reg:CC)
		    (const_int 0))) */

void
5601 5602 5603 5604
frv_ifcvt_modify_multiple_tests (ce_if_block_t *ce_info,
                                 basic_block bb,
                                 rtx *p_true,
                                 rtx *p_false)
bernds's avatar
bernds committed
5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634
{
  rtx old_true = XEXP (*p_true, 0);
  rtx old_false = XEXP (*p_false, 0);
  rtx true_expr = XEXP (*p_true, 1);
  rtx false_expr = XEXP (*p_false, 1);
  rtx test_expr;
  rtx old_test;
  rtx cr = XEXP (old_true, 0);
  rtx check_insn;
  rtx new_cr = NULL_RTX;
  rtx *p_new_cr = (rtx *)0;
  rtx if_else;
  rtx compare;
  rtx cc;
  enum reg_class cr_class;
  enum machine_mode mode = GET_MODE (true_expr);
  rtx (*logical_func)(rtx, rtx, rtx);

  if (TARGET_DEBUG_COND_EXEC)
    {
      fprintf (stderr,
	       "\n:::::::::: frv_ifcvt_modify_multiple_tests, before modification for %s\ntrue insn:\n",
	       ce_info->and_and_p ? "&&" : "||");

      debug_rtx (*p_true);

      fputs ("\nfalse insn:\n", stderr);
      debug_rtx (*p_false);
    }

5635
  if (!TARGET_MULTI_CE)
bernds's avatar
bernds committed
5636 5637 5638 5639
    goto fail;

  if (GET_CODE (cr) != REG)
    goto fail;
5640

5641
  if (mode == CCmode || mode == CC_UNSmode || mode == CC_NZmode)
bernds's avatar
bernds committed
5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684
    {
      cr_class = ICR_REGS;
      p_new_cr = &frv_ifcvt.extra_int_cr;
    }
  else if (mode == CC_FPmode)
    {
      cr_class = FCR_REGS;
      p_new_cr = &frv_ifcvt.extra_fp_cr;
    }
  else
    goto fail;

  /* Allocate a temp CR, reusing a previously allocated temp CR if we have 3 or
     more &&/|| tests.  */
  new_cr = *p_new_cr;
  if (! new_cr)
    {
      new_cr = *p_new_cr = frv_alloc_temp_reg (&frv_ifcvt.tmp_reg, cr_class,
					       CC_CCRmode, TRUE, TRUE);
      if (! new_cr)
	goto fail;
    }

  if (ce_info->and_and_p)
    {
      old_test = old_false;
      test_expr = true_expr;
      logical_func = (GET_CODE (old_true) == EQ) ? gen_andcr : gen_andncr;
      *p_true = gen_rtx_NE (CC_CCRmode, cr, const0_rtx);
      *p_false = gen_rtx_EQ (CC_CCRmode, cr, const0_rtx);
    }
  else
    {
      old_test = old_false;
      test_expr = false_expr;
      logical_func = (GET_CODE (old_false) == EQ) ? gen_orcr : gen_orncr;
      *p_true = gen_rtx_EQ (CC_CCRmode, cr, const0_rtx);
      *p_false = gen_rtx_NE (CC_CCRmode, cr, const0_rtx);
    }

  /* First add the andcr/andncr/orcr/orncr, which will be added after the
     conditional check instruction, due to frv_ifcvt_add_insn being a LIFO
     stack.  */
5685
  frv_ifcvt_add_insn ((*logical_func) (cr, cr, new_cr), BB_END (bb), TRUE);
bernds's avatar
bernds committed
5686 5687 5688 5689 5690 5691 5692 5693

  /* Now add the conditional check insn.  */
  cc = XEXP (test_expr, 0);
  compare = gen_rtx_fmt_ee (GET_CODE (test_expr), CC_CCRmode, cc, const0_rtx);
  if_else = gen_rtx_IF_THEN_ELSE (CC_CCRmode, old_test, compare, const0_rtx);

  check_insn = gen_rtx_SET (VOIDmode, new_cr, if_else);

5694
  /* Add the new check insn to the list of check insns that need to be
bernds's avatar
bernds committed
5695
     inserted.  */
5696
  frv_ifcvt_add_insn (check_insn, BB_END (bb), TRUE);
bernds's avatar
bernds committed
5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713

  if (TARGET_DEBUG_COND_EXEC)
    {
      fputs ("\n:::::::::: frv_ifcvt_modify_multiple_tests, after modification\ntrue insn:\n",
	     stderr);

      debug_rtx (*p_true);

      fputs ("\nfalse insn:\n", stderr);
      debug_rtx (*p_false);
    }

  return;

 fail:
  *p_true = *p_false = NULL_RTX;

5714
  /* If we allocated a CR register, release it.  */
bernds's avatar
bernds committed
5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732
  if (new_cr)
    {
      CLEAR_HARD_REG_BIT (frv_ifcvt.tmp_reg.regs, REGNO (new_cr));
      *p_new_cr = NULL_RTX;
    }

  if (TARGET_DEBUG_COND_EXEC)
    fputs ("\n:::::::::: frv_ifcvt_modify_multiple_tests, failed.\n", stderr);

  return;
}


/* Return a register which will be loaded with a value if an IF block is
   converted to conditional execution.  This is used to rewrite instructions
   that use constants to ones that just use registers.  */

static rtx
5733
frv_ifcvt_load_value (rtx value, rtx insn ATTRIBUTE_UNUSED)
bernds's avatar
bernds committed
5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754
{
  int num_alloc = frv_ifcvt.cur_scratch_regs;
  int i;
  rtx reg;

  /* We know gr0 == 0, so replace any errant uses.  */
  if (value == const0_rtx)
    return gen_rtx_REG (SImode, GPR_FIRST);

  /* First search all registers currently loaded to see if we have an
     applicable constant.  */
  if (CONSTANT_P (value)
      || (GET_CODE (value) == REG && REGNO (value) == LR_REGNO))
    {
      for (i = 0; i < num_alloc; i++)
	{
	  if (rtx_equal_p (SET_SRC (frv_ifcvt.scratch_regs[i]), value))
	    return SET_DEST (frv_ifcvt.scratch_regs[i]);
	}
    }

5755
  /* Have we exhausted the number of registers available?  */
bernds's avatar
bernds committed
5756 5757
  if (num_alloc >= GPR_TEMP_NUM)
    {
5758 5759
      if (dump_file)
	fprintf (dump_file, "Too many temporary registers allocated\n");
bernds's avatar
bernds committed
5760 5761 5762 5763 5764 5765 5766 5767

      return NULL_RTX;
    }

  /* Allocate the new register.  */
  reg = frv_alloc_temp_reg (&frv_ifcvt.tmp_reg, GPR_REGS, SImode, TRUE, TRUE);
  if (! reg)
    {
5768 5769
      if (dump_file)
	fputs ("Could not find a scratch register\n", dump_file);
bernds's avatar
bernds committed
5770 5771 5772 5773 5774 5775 5776

      return NULL_RTX;
    }

  frv_ifcvt.cur_scratch_regs++;
  frv_ifcvt.scratch_regs[num_alloc] = gen_rtx_SET (VOIDmode, reg, value);

5777
  if (dump_file)
bernds's avatar
bernds committed
5778 5779
    {
      if (GET_CODE (value) == CONST_INT)
5780
	fprintf (dump_file, "Register %s will hold %ld\n",
bernds's avatar
bernds committed
5781 5782 5783
		 reg_names[ REGNO (reg)], (long)INTVAL (value));

      else if (GET_CODE (value) == REG && REGNO (value) == LR_REGNO)
5784
	fprintf (dump_file, "Register %s will hold LR\n",
bernds's avatar
bernds committed
5785 5786 5787
		 reg_names[ REGNO (reg)]);

      else
5788
	fprintf (dump_file, "Register %s will hold a saved value\n",
bernds's avatar
bernds committed
5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802
		 reg_names[ REGNO (reg)]);
    }

  return reg;
}


/* Update a MEM used in conditional code that might contain an offset to put
   the offset into a scratch register, so that the conditional load/store
   operations can be used.  This function returns the original pointer if the
   MEM is valid to use in conditional code, NULL if we can't load up the offset
   into a temporary register, or the new MEM if we were successful.  */

static rtx
5803
frv_ifcvt_rewrite_mem (rtx mem, enum machine_mode mode, rtx insn)
bernds's avatar
bernds committed
5804 5805 5806
{
  rtx addr = XEXP (mem, 0);

5807
  if (!frv_legitimate_address_p (mode, addr, reload_completed, TRUE, FALSE))
bernds's avatar
bernds committed
5808 5809 5810 5811 5812 5813
    {
      if (GET_CODE (addr) == PLUS)
	{
	  rtx addr_op0 = XEXP (addr, 0);
	  rtx addr_op1 = XEXP (addr, 1);

5814
	  if (GET_CODE (addr_op0) == REG && CONSTANT_P (addr_op1))
bernds's avatar
bernds committed
5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847
	    {
	      rtx reg = frv_ifcvt_load_value (addr_op1, insn);
	      if (!reg)
		return NULL_RTX;

	      addr = gen_rtx_PLUS (Pmode, addr_op0, reg);
	    }

	  else
	    return NULL_RTX;
	}

      else if (CONSTANT_P (addr))
	addr = frv_ifcvt_load_value (addr, insn);

      else
	return NULL_RTX;

      if (addr == NULL_RTX)
	return NULL_RTX;

      else if (XEXP (mem, 0) != addr)
	return change_address (mem, mode, addr);
    }

  return mem;
}


/* Given a PATTERN, return a SET expression if this PATTERN has only a single
   SET, possibly conditionally executed.  It may also have CLOBBERs, USEs.  */

static rtx
5848
single_set_pattern (rtx pattern)
bernds's avatar
bernds committed
5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894
{
  rtx set;
  int i;

  if (GET_CODE (pattern) == COND_EXEC)
    pattern = COND_EXEC_CODE (pattern);

  if (GET_CODE (pattern) == SET)
    return pattern;

  else if (GET_CODE (pattern) == PARALLEL)
    {
      for (i = 0, set = 0; i < XVECLEN (pattern, 0); i++)
	{
	  rtx sub = XVECEXP (pattern, 0, i);

	  switch (GET_CODE (sub))
	    {
	    case USE:
	    case CLOBBER:
	      break;

	    case SET:
	      if (set)
		return 0;
	      else
		set = sub;
	      break;

	    default:
	      return 0;
	    }
	}
      return set;
    }

  return 0;
}


/* A C expression to modify the code described by the conditional if
   information CE_INFO with the new PATTERN in INSN.  If PATTERN is a null
   pointer after the IFCVT_MODIFY_INSN macro executes, it is assumed that that
   insn cannot be converted to be executed conditionally.  */

rtx
5895
frv_ifcvt_modify_insn (ce_if_block_t *ce_info,
5896 5897
                       rtx pattern,
                       rtx insn)
bernds's avatar
bernds committed
5898 5899 5900 5901 5902 5903 5904
{
  rtx orig_ce_pattern = pattern;
  rtx set;
  rtx op0;
  rtx op1;
  rtx test;

5905
  gcc_assert (GET_CODE (pattern) == COND_EXEC);
bernds's avatar
bernds committed
5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957

  test = COND_EXEC_TEST (pattern);
  if (GET_CODE (test) == AND)
    {
      rtx cr = frv_ifcvt.cr_reg;
      rtx test_reg;

      op0 = XEXP (test, 0);
      if (! rtx_equal_p (cr, XEXP (op0, 0)))
	goto fail;

      op1 = XEXP (test, 1);
      test_reg = XEXP (op1, 0);
      if (GET_CODE (test_reg) != REG)
	goto fail;

      /* Is this the first nested if block in this sequence?  If so, generate
         an andcr or andncr.  */
      if (! frv_ifcvt.last_nested_if_cr)
	{
	  rtx and_op;

	  frv_ifcvt.last_nested_if_cr = test_reg;
	  if (GET_CODE (op0) == NE)
	    and_op = gen_andcr (test_reg, cr, test_reg);
	  else
	    and_op = gen_andncr (test_reg, cr, test_reg);

	  frv_ifcvt_add_insn (and_op, insn, TRUE);
	}

      /* If this isn't the first statement in the nested if sequence, see if we
         are dealing with the same register.  */
      else if (! rtx_equal_p (test_reg, frv_ifcvt.last_nested_if_cr))
	goto fail;

      COND_EXEC_TEST (pattern) = test = op1;
    }

  /* If this isn't a nested if, reset state variables.  */
  else
    {
      frv_ifcvt.last_nested_if_cr = NULL_RTX;
    }

  set = single_set_pattern (pattern);
  if (set)
    {
      rtx dest = SET_DEST (set);
      rtx src = SET_SRC (set);
      enum machine_mode mode = GET_MODE (dest);

5958
      /* Check for normal binary operators.  */
5959
      if (mode == SImode && ARITHMETIC_P (src))
bernds's avatar
bernds committed
5960 5961 5962 5963
	{
	  op0 = XEXP (src, 0);
	  op1 = XEXP (src, 1);

5964
	  if (integer_register_operand (op0, SImode) && CONSTANT_P (op1))
bernds's avatar
bernds committed
5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006
	    {
	      op1 = frv_ifcvt_load_value (op1, insn);
	      if (op1)
		COND_EXEC_CODE (pattern)
		  = gen_rtx_SET (VOIDmode, dest, gen_rtx_fmt_ee (GET_CODE (src),
								 GET_MODE (src),
								 op0, op1));
	      else
		goto fail;
	    }
	}

      /* For multiply by a constant, we need to handle the sign extending
         correctly.  Add a USE of the value after the multiply to prevent flow
         from cratering because only one register out of the two were used.  */
      else if (mode == DImode && GET_CODE (src) == MULT)
	{
	  op0 = XEXP (src, 0);
	  op1 = XEXP (src, 1);
	  if (GET_CODE (op0) == SIGN_EXTEND && GET_CODE (op1) == CONST_INT)
	    {
	      op1 = frv_ifcvt_load_value (op1, insn);
	      if (op1)
		{
		  op1 = gen_rtx_SIGN_EXTEND (DImode, op1);
		  COND_EXEC_CODE (pattern)
		    = gen_rtx_SET (VOIDmode, dest,
				   gen_rtx_MULT (DImode, op0, op1));
		}
	      else
		goto fail;
	    }

	  frv_ifcvt_add_insn (gen_rtx_USE (VOIDmode, dest), insn, FALSE);
	}

      /* If we are just loading a constant created for a nested conditional
         execution statement, just load the constant without any conditional
         execution, since we know that the constant will not interfere with any
         other registers.  */
      else if (frv_ifcvt.scratch_insns_bitmap
	       && bitmap_bit_p (frv_ifcvt.scratch_insns_bitmap,
6007 6008
				INSN_UID (insn))
	       && REG_P (SET_DEST (set))
6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023
	       /* We must not unconditionally set a scratch reg chosen
		  for a nested if-converted block if its incoming
		  value from the TEST block (or the result of the THEN
		  branch) could/should propagate to the JOIN block.
		  It suffices to test whether the register is live at
		  the JOIN point: if it's live there, we can infer
		  that we set it in the former JOIN block of the
		  nested if-converted block (otherwise it wouldn't
		  have been available as a scratch register), and it
		  is either propagated through or set in the other
		  conditional block.  It's probably not worth trying
		  to catch the latter case, and it could actually
		  limit scheduling of the combined block quite
		  severely.  */
	       && ce_info->join_bb
6024 6025
	       && ! (REGNO_REG_SET_P (df_get_live_in (ce_info->join_bb),
				      REGNO (SET_DEST (set))))
6026 6027 6028
	       /* Similarly, we must not unconditionally set a reg
		  used as scratch in the THEN branch if the same reg
		  is live in the ELSE branch.  */
6029 6030
	       && (! ce_info->else_bb
		   || BLOCK_FOR_INSN (insn) == ce_info->else_bb
6031 6032
		   || ! (REGNO_REG_SET_P (df_get_live_in (ce_info->else_bb),
					  REGNO (SET_DEST (set))))))
bernds's avatar
bernds committed
6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087
	pattern = set;

      else if (mode == QImode || mode == HImode || mode == SImode
	       || mode == SFmode)
	{
	  int changed_p = FALSE;

	  /* Check for just loading up a constant */
	  if (CONSTANT_P (src) && integer_register_operand (dest, mode))
	    {
	      src = frv_ifcvt_load_value (src, insn);
	      if (!src)
		goto fail;

	      changed_p = TRUE;
	    }

	  /* See if we need to fix up stores */
	  if (GET_CODE (dest) == MEM)
	    {
	      rtx new_mem = frv_ifcvt_rewrite_mem (dest, mode, insn);

	      if (!new_mem)
		goto fail;

	      else if (new_mem != dest)
		{
		  changed_p = TRUE;
		  dest = new_mem;
		}
	    }

	  /* See if we need to fix up loads */
	  if (GET_CODE (src) == MEM)
	    {
	      rtx new_mem = frv_ifcvt_rewrite_mem (src, mode, insn);

	      if (!new_mem)
		goto fail;

	      else if (new_mem != src)
		{
		  changed_p = TRUE;
		  src = new_mem;
		}
	    }

	  /* If either src or destination changed, redo SET.  */
	  if (changed_p)
	    COND_EXEC_CODE (pattern) = gen_rtx_SET (VOIDmode, dest, src);
	}

      /* Rewrite a nested set cccr in terms of IF_THEN_ELSE.  Also deal with
         rewriting the CC register to be the same as the paired CC/CR register
         for nested ifs.  */
6088
      else if (mode == CC_CCRmode && COMPARISON_P (src))
bernds's avatar
bernds committed
6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159
	{
	  int regno = REGNO (XEXP (src, 0));
	  rtx if_else;

	  if (ce_info->pass > 1
	      && regno != (int)REGNO (frv_ifcvt.nested_cc_reg)
	      && TEST_HARD_REG_BIT (frv_ifcvt.nested_cc_ok_rewrite, regno))
	    {
	      src = gen_rtx_fmt_ee (GET_CODE (src),
				    CC_CCRmode,
				    frv_ifcvt.nested_cc_reg,
				    XEXP (src, 1));
	    }

	  if_else = gen_rtx_IF_THEN_ELSE (CC_CCRmode, test, src, const0_rtx);
	  pattern = gen_rtx_SET (VOIDmode, dest, if_else);
	}

      /* Remap a nested compare instruction to use the paired CC/CR reg.  */
      else if (ce_info->pass > 1
	       && GET_CODE (dest) == REG
	       && CC_P (REGNO (dest))
	       && REGNO (dest) != REGNO (frv_ifcvt.nested_cc_reg)
	       && TEST_HARD_REG_BIT (frv_ifcvt.nested_cc_ok_rewrite,
				     REGNO (dest))
	       && GET_CODE (src) == COMPARE)
	{
	  PUT_MODE (frv_ifcvt.nested_cc_reg, GET_MODE (dest));
	  COND_EXEC_CODE (pattern)
	    = gen_rtx_SET (VOIDmode, frv_ifcvt.nested_cc_reg, copy_rtx (src));
	}
    }

  if (TARGET_DEBUG_COND_EXEC)
    {
      rtx orig_pattern = PATTERN (insn);

      PATTERN (insn) = pattern;
      fprintf (stderr,
	       "\n:::::::::: frv_ifcvt_modify_insn: pass = %d, insn after modification:\n",
	       ce_info->pass);

      debug_rtx (insn);
      PATTERN (insn) = orig_pattern;
    }

  return pattern;

 fail:
  if (TARGET_DEBUG_COND_EXEC)
    {
      rtx orig_pattern = PATTERN (insn);

      PATTERN (insn) = orig_ce_pattern;
      fprintf (stderr,
	       "\n:::::::::: frv_ifcvt_modify_insn: pass = %d, insn could not be modified:\n",
	       ce_info->pass);

      debug_rtx (insn);
      PATTERN (insn) = orig_pattern;
    }

  return NULL_RTX;
}


/* A C expression to perform any final machine dependent modifications in
   converting code to conditional execution in the code described by the
   conditional if information CE_INFO.  */

void
6160
frv_ifcvt_modify_final (ce_if_block_t *ce_info ATTRIBUTE_UNUSED)
bernds's avatar
bernds committed
6161 6162 6163 6164 6165 6166 6167 6168
{
  rtx existing_insn;
  rtx check_insn;
  rtx p = frv_ifcvt.added_insns_list;
  int i;

  /* Loop inserting the check insns.  The last check insn is the first test,
     and is the appropriate place to insert constants.  */
6169
  gcc_assert (p);
bernds's avatar
bernds committed
6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199

  do
    {
      rtx check_and_insert_insns = XEXP (p, 0);
      rtx old_p = p;

      check_insn = XEXP (check_and_insert_insns, 0);
      existing_insn = XEXP (check_and_insert_insns, 1);
      p = XEXP (p, 1);

      /* The jump bit is used to say that the new insn is to be inserted BEFORE
         the existing insn, otherwise it is to be inserted AFTER.  */
      if (check_and_insert_insns->jump)
	{
	  emit_insn_before (check_insn, existing_insn);
	  check_and_insert_insns->jump = 0;
	}
      else
	emit_insn_after (check_insn, existing_insn);

      free_EXPR_LIST_node (check_and_insert_insns);
      free_EXPR_LIST_node (old_p);
    }
  while (p != NULL_RTX);

  /* Load up any constants needed into temp gprs */
  for (i = 0; i < frv_ifcvt.cur_scratch_regs; i++)
    {
      rtx insn = emit_insn_before (frv_ifcvt.scratch_regs[i], existing_insn);
      if (! frv_ifcvt.scratch_insns_bitmap)
6200
	frv_ifcvt.scratch_insns_bitmap = BITMAP_ALLOC (NULL);
bernds's avatar
bernds committed
6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214
      bitmap_set_bit (frv_ifcvt.scratch_insns_bitmap, INSN_UID (insn));
      frv_ifcvt.scratch_regs[i] = NULL_RTX;
    }

  frv_ifcvt.added_insns_list = NULL_RTX;
  frv_ifcvt.cur_scratch_regs = 0;
}


/* A C expression to cancel any machine dependent modifications in converting
   code to conditional execution in the code described by the conditional if
   information CE_INFO.  */

void
6215
frv_ifcvt_modify_cancel (ce_if_block_t *ce_info ATTRIBUTE_UNUSED)
bernds's avatar
bernds committed
6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249
{
  int i;
  rtx p = frv_ifcvt.added_insns_list;

  /* Loop freeing up the EXPR_LIST's allocated.  */
  while (p != NULL_RTX)
    {
      rtx check_and_jump = XEXP (p, 0);
      rtx old_p = p;

      p = XEXP (p, 1);
      free_EXPR_LIST_node (check_and_jump);
      free_EXPR_LIST_node (old_p);
    }

  /* Release any temporary gprs allocated.  */
  for (i = 0; i < frv_ifcvt.cur_scratch_regs; i++)
    frv_ifcvt.scratch_regs[i] = NULL_RTX;

  frv_ifcvt.added_insns_list = NULL_RTX;
  frv_ifcvt.cur_scratch_regs = 0;
  return;
}

/* A C expression for the size in bytes of the trampoline, as an integer.
   The template is:

	setlo #0, <jmp_reg>
	setlo #0, <static_chain>
	sethi #0, <jmp_reg>
	sethi #0, <static_chain>
	jmpl @(gr0,<jmp_reg>) */

int
6250
frv_trampoline_size (void)
bernds's avatar
bernds committed
6251
{
6252 6253 6254 6255 6256
  if (TARGET_FDPIC)
    /* Allocate room for the function descriptor and the lddi
       instruction.  */
    return 8 + 6 * 4;
  return 5 /* instructions */ * 4 /* instruction size.  */;
bernds's avatar
bernds committed
6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273
}


/* A C statement to initialize the variable parts of a trampoline.  ADDR is an
   RTX for the address of the trampoline; FNADDR is an RTX for the address of
   the nested function; STATIC_CHAIN is an RTX for the static chain value that
   should be passed to the function when it is called.

   The template is:

	setlo #0, <jmp_reg>
	setlo #0, <static_chain>
	sethi #0, <jmp_reg>
	sethi #0, <static_chain>
	jmpl @(gr0,<jmp_reg>) */

void
6274
frv_initialize_trampoline (rtx addr, rtx fnaddr, rtx static_chain)
bernds's avatar
bernds committed
6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292
{
  rtx sc_reg = force_reg (Pmode, static_chain);

  emit_library_call (gen_rtx_SYMBOL_REF (SImode, "__trampoline_setup"),
		     FALSE, VOIDmode, 4,
		     addr, Pmode,
		     GEN_INT (frv_trampoline_size ()), SImode,
		     fnaddr, Pmode,
		     sc_reg, Pmode);
}


/* Many machines have some registers that cannot be copied directly to or from
   memory or even from other types of registers.  An example is the `MQ'
   register, which on most machines, can only be copied to or from general
   registers, but not memory.  Some machines allow copying all registers to and
   from memory, but require a scratch register for stores to some memory
   locations (e.g., those with symbolic address on the RT, and those with
6293
   certain symbolic address on the SPARC when compiling PIC).  In some cases,
bernds's avatar
bernds committed
6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335
   both an intermediate and a scratch register are required.

   You should define these macros to indicate to the reload phase that it may
   need to allocate at least one register for a reload in addition to the
   register to contain the data.  Specifically, if copying X to a register
   CLASS in MODE requires an intermediate register, you should define
   `SECONDARY_INPUT_RELOAD_CLASS' to return the largest register class all of
   whose registers can be used as intermediate registers or scratch registers.

   If copying a register CLASS in MODE to X requires an intermediate or scratch
   register, `SECONDARY_OUTPUT_RELOAD_CLASS' should be defined to return the
   largest register class required.  If the requirements for input and output
   reloads are the same, the macro `SECONDARY_RELOAD_CLASS' should be used
   instead of defining both macros identically.

   The values returned by these macros are often `GENERAL_REGS'.  Return
   `NO_REGS' if no spare register is needed; i.e., if X can be directly copied
   to or from a register of CLASS in MODE without requiring a scratch register.
   Do not define this macro if it would always return `NO_REGS'.

   If a scratch register is required (either with or without an intermediate
   register), you should define patterns for `reload_inM' or `reload_outM', as
   required..  These patterns, which will normally be implemented with a
   `define_expand', should be similar to the `movM' patterns, except that
   operand 2 is the scratch register.

   Define constraints for the reload register and scratch register that contain
   a single register class.  If the original reload register (whose class is
   CLASS) can meet the constraint given in the pattern, the value returned by
   these macros is used for the class of the scratch register.  Otherwise, two
   additional reload registers are required.  Their classes are obtained from
   the constraints in the insn pattern.

   X might be a pseudo-register or a `subreg' of a pseudo-register, which could
   either be in a hard register or in memory.  Use `true_regnum' to find out;
   it will return -1 if the pseudo is in memory and the hard register number if
   it is in a register.

   These macros should not be used in the case where a particular class of
   registers can only be copied to memory and not to another class of
   registers.  In that case, secondary reload registers are not needed and
   would not be helpful.  Instead, a stack location must be used to perform the
kazu's avatar
kazu committed
6336
   copy and the `movM' pattern should use memory as an intermediate storage.
bernds's avatar
bernds committed
6337 6338 6339
   This case often occurs between floating-point and general registers.  */

enum reg_class
6340 6341 6342 6343
frv_secondary_reload_class (enum reg_class class,
                            enum machine_mode mode ATTRIBUTE_UNUSED,
                            rtx x,
                            int in_p ATTRIBUTE_UNUSED)
bernds's avatar
bernds committed
6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367
{
  enum reg_class ret;

  switch (class)
    {
    default:
      ret = NO_REGS;
      break;

      /* Accumulators/Accumulator guard registers need to go through floating
         point registers.  */
    case QUAD_REGS:
    case EVEN_REGS:
    case GPR_REGS:
      ret = NO_REGS;
      if (x && GET_CODE (x) == REG)
	{
	  int regno = REGNO (x);

	  if (ACC_P (regno) || ACCG_P (regno))
	    ret = FPR_REGS;
	}
      break;

6368
      /* Nonzero constants should be loaded into an FPR through a GPR.  */
bernds's avatar
bernds committed
6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418
    case QUAD_FPR_REGS:
    case FEVEN_REGS:
    case FPR_REGS:
      if (x && CONSTANT_P (x) && !ZERO_P (x))
	ret = GPR_REGS;
      else
	ret = NO_REGS;
      break;

      /* All of these types need gpr registers.  */
    case ICC_REGS:
    case FCC_REGS:
    case CC_REGS:
    case ICR_REGS:
    case FCR_REGS:
    case CR_REGS:
    case LCR_REG:
    case LR_REG:
      ret = GPR_REGS;
      break;

      /* The accumulators need fpr registers */
    case ACC_REGS:
    case EVEN_ACC_REGS:
    case QUAD_ACC_REGS:
    case ACCG_REGS:
      ret = FPR_REGS;
      break;
    }

  return ret;
}


/* A C expression whose value is nonzero if pseudos that have been assigned to
   registers of class CLASS would likely be spilled because registers of CLASS
   are needed for spill registers.

   The default value of this macro returns 1 if CLASS has exactly one register
   and zero otherwise.  On most machines, this default should be used.  Only
   define this macro to some other expression if pseudo allocated by
   `local-alloc.c' end up in memory because their hard registers were needed
   for spill registers.  If this macro returns nonzero for those classes, those
   pseudos will only be allocated by `global.c', which knows how to reallocate
   the pseudo to another register.  If there would not be another register
   available for reallocation, you should not change the definition of this
   macro since the only effect of such a definition would be to slow down
   register allocation.  */

int
6419
frv_class_likely_spilled_p (enum reg_class class)
bernds's avatar
bernds committed
6420 6421 6422 6423 6424 6425
{
  switch (class)
    {
    default:
      break;

6426 6427 6428 6429 6430
    case GR8_REGS:
    case GR9_REGS:
    case GR89_REGS:
    case FDPIC_FPTR_REGS:
    case FDPIC_REGS:
bernds's avatar
bernds committed
6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451
    case ICC_REGS:
    case FCC_REGS:
    case CC_REGS:
    case ICR_REGS:
    case FCR_REGS:
    case CR_REGS:
    case LCR_REG:
    case LR_REG:
    case SPR_REGS:
    case QUAD_ACC_REGS:
    case EVEN_ACC_REGS:
    case ACC_REGS:
    case ACCG_REGS:
      return TRUE;
    }

  return FALSE;
}


/* An expression for the alignment of a structure field FIELD if the
kcook's avatar
 
kcook committed
6452
   alignment computed in the usual way is COMPUTED.  GCC uses this
bernds's avatar
bernds committed
6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498
   value instead of the value in `BIGGEST_ALIGNMENT' or
   `BIGGEST_FIELD_ALIGNMENT', if defined, for structure fields only.  */

/* The definition type of the bit field data is either char, short, long or
   long long. The maximum bit size is the number of bits of its own type.

   The bit field data is assigned to a storage unit that has an adequate size
   for bit field data retention and is located at the smallest address.

   Consecutive bit field data are packed at consecutive bits having the same
   storage unit, with regard to the type, beginning with the MSB and continuing
   toward the LSB.

   If a field to be assigned lies over a bit field type boundary, its
   assignment is completed by aligning it with a boundary suitable for the
   type.

   When a bit field having a bit length of 0 is declared, it is forcibly
   assigned to the next storage unit.

   e.g)
	struct {
		int	a:2;
		int	b:6;
		char	c:4;
		int	d:10;
		int	 :0;
		int	f:2;
	} x;

		+0	  +1	    +2	      +3
	&x	00000000  00000000  00000000  00000000
		MLM----L
		a    b
	&x+4	00000000  00000000  00000000  00000000
		M--L
		c
	&x+8	00000000  00000000  00000000  00000000
		M----------L
		d
	&x+12	00000000  00000000  00000000  00000000
		ML
		f
*/

int
6499
frv_adjust_field_align (tree field, int computed)
bernds's avatar
bernds committed
6500
{
6501 6502
  /* Make sure that the bitfield is not wider than the type.  */
  if (DECL_BIT_FIELD (field)
6503
      && !DECL_ARTIFICIAL (field))
bernds's avatar
bernds committed
6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516
    {
      tree parent = DECL_CONTEXT (field);
      tree prev = NULL_TREE;
      tree cur;

      for (cur = TYPE_FIELDS (parent); cur && cur != field; cur = TREE_CHAIN (cur))
	{
	  if (TREE_CODE (cur) != FIELD_DECL)
	    continue;

	  prev = cur;
	}

6517
      gcc_assert (cur);
bernds's avatar
bernds committed
6518 6519 6520

      /* If this isn't a :0 field and if the previous element is a bitfield
	 also, see if the type is different, if so, we will need to align the
6521
	 bit-field to the next boundary.  */
bernds's avatar
bernds committed
6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591
      if (prev
	  && ! DECL_PACKED (field)
	  && ! integer_zerop (DECL_SIZE (field))
	  && DECL_BIT_FIELD_TYPE (field) != DECL_BIT_FIELD_TYPE (prev))
	{
	  int prev_align = TYPE_ALIGN (TREE_TYPE (prev));
	  int cur_align  = TYPE_ALIGN (TREE_TYPE (field));
	  computed = (prev_align > cur_align) ? prev_align : cur_align;
	}
    }

  return computed;
}


/* A C expression that is nonzero if it is permissible to store a value of mode
   MODE in hard register number REGNO (or in several registers starting with
   that one).  For a machine where all registers are equivalent, a suitable
   definition is

        #define HARD_REGNO_MODE_OK(REGNO, MODE) 1

   It is not necessary for this macro to check for the numbers of fixed
   registers, because the allocation mechanism considers them to be always
   occupied.

   On some machines, double-precision values must be kept in even/odd register
   pairs.  The way to implement that is to define this macro to reject odd
   register numbers for such modes.

   The minimum requirement for a mode to be OK in a register is that the
   `movMODE' instruction pattern support moves between the register and any
   other hard register for which the mode is OK; and that moving a value into
   the register and back out not alter it.

   Since the same instruction used to move `SImode' will work for all narrower
   integer modes, it is not necessary on any machine for `HARD_REGNO_MODE_OK'
   to distinguish between these modes, provided you define patterns `movhi',
   etc., to take advantage of this.  This is useful because of the interaction
   between `HARD_REGNO_MODE_OK' and `MODES_TIEABLE_P'; it is very desirable for
   all integer modes to be tieable.

   Many machines have special registers for floating point arithmetic.  Often
   people assume that floating point machine modes are allowed only in floating
   point registers.  This is not true.  Any registers that can hold integers
   can safely *hold* a floating point machine mode, whether or not floating
   arithmetic can be done on it in those registers.  Integer move instructions
   can be used to move the values.

   On some machines, though, the converse is true: fixed-point machine modes
   may not go in floating registers.  This is true if the floating registers
   normalize any value stored in them, because storing a non-floating value
   there would garble it.  In this case, `HARD_REGNO_MODE_OK' should reject
   fixed-point machine modes in floating registers.  But if the floating
   registers do not automatically normalize, if you can store any bit pattern
   in one and retrieve it unchanged without a trap, then any machine mode may
   go in a floating register, so you can define this macro to say so.

   The primary significance of special floating registers is rather that they
   are the registers acceptable in floating point arithmetic instructions.
   However, this is of no concern to `HARD_REGNO_MODE_OK'.  You handle it by
   writing the proper constraints for those instructions.

   On some machines, the floating registers are especially slow to access, so
   that it is better to store a value in a stack frame than in such a register
   if floating point arithmetic is not being done.  As long as the floating
   registers are not in class `GENERAL_REGS', they will not be used unless some
   pattern's constraint asks for one.  */

int
6592
frv_hard_regno_mode_ok (int regno, enum machine_mode mode)
bernds's avatar
bernds committed
6593 6594 6595 6596 6597 6598 6599 6600
{
  int base;
  int mask;

  switch (mode)
    {
    case CCmode:
    case CC_UNSmode:
6601
    case CC_NZmode:
bernds's avatar
bernds committed
6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626 6627 6628
      return ICC_P (regno) || GPR_P (regno);

    case CC_CCRmode:
      return CR_P (regno) || GPR_P (regno);

    case CC_FPmode:
      return FCC_P (regno) || GPR_P (regno);

    default:
      break;
    }

  /* Set BASE to the first register in REGNO's class.  Set MASK to the
     bits that must be clear in (REGNO - BASE) for the register to be
     well-aligned.  */
  if (INTEGRAL_MODE_P (mode) || FLOAT_MODE_P (mode) || VECTOR_MODE_P (mode))
    {
      if (ACCG_P (regno))
	{
	  /* ACCGs store one byte.  Two-byte quantities must start in
	     even-numbered registers, four-byte ones in registers whose
	     numbers are divisible by four, and so on.  */
	  base = ACCG_FIRST;
	  mask = GET_MODE_SIZE (mode) - 1;
	}
      else
	{
6629 6630
	   /* The other registers store one word.  */
	  if (GPR_P (regno) || regno == AP_FIRST)
bernds's avatar
bernds committed
6631 6632 6633 6634 6635 6636 6637 6638
	    base = GPR_FIRST;

	  else if (FPR_P (regno))
	    base = FPR_FIRST;

	  else if (ACC_P (regno))
	    base = ACC_FIRST;

6639 6640 6641
	  else if (SPR_P (regno))
	    return mode == SImode;

6642
	  /* Fill in the table.  */
bernds's avatar
bernds committed
6643 6644 6645 6646 6647 6648 6649 6650 6651 6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668 6669 6670 6671 6672 6673 6674
	  else
	    return 0;

	  /* Anything smaller than an SI is OK in any word-sized register.  */
	  if (GET_MODE_SIZE (mode) < 4)
	    return 1;

	  mask = (GET_MODE_SIZE (mode) / 4) - 1;
	}
      return (((regno - base) & mask) == 0);
    }

  return 0;
}


/* A C expression for the number of consecutive hard registers, starting at
   register number REGNO, required to hold a value of mode MODE.

   On a machine where all registers are exactly one word, a suitable definition
   of this macro is

        #define HARD_REGNO_NREGS(REGNO, MODE)            \
           ((GET_MODE_SIZE (MODE) + UNITS_PER_WORD - 1)  \
            / UNITS_PER_WORD))  */

/* On the FRV, make the CC_FP mode take 3 words in the integer registers, so
   that we can build the appropriate instructions to properly reload the
   values.  Also, make the byte-sized accumulator guards use one guard
   for each byte.  */

int
6675
frv_hard_regno_nregs (int regno, enum machine_mode mode)
bernds's avatar
bernds committed
6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696
{
  if (ACCG_P (regno))
    return GET_MODE_SIZE (mode);
  else
    return (GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
}


/* A C expression for the maximum number of consecutive registers of
   class CLASS needed to hold a value of mode MODE.

   This is closely related to the macro `HARD_REGNO_NREGS'.  In fact, the value
   of the macro `CLASS_MAX_NREGS (CLASS, MODE)' should be the maximum value of
   `HARD_REGNO_NREGS (REGNO, MODE)' for all REGNO values in the class CLASS.

   This macro helps control the handling of multiple-word values in
   the reload pass.

   This declaration is required.  */

int
6697
frv_class_max_nregs (enum reg_class class, enum machine_mode mode)
bernds's avatar
bernds committed
6698 6699 6700 6701 6702 6703 6704 6705 6706 6707 6708 6709 6710 6711 6712
{
  if (class == ACCG_REGS)
    /* An N-byte value requires N accumulator guards.  */
    return GET_MODE_SIZE (mode);
  else
    return (GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
}


/* A C expression that is nonzero if X is a legitimate constant for an
   immediate operand on the target machine.  You can assume that X satisfies
   `CONSTANT_P', so you need not check this.  In fact, `1' is a suitable
   definition for this macro on machines where anything `CONSTANT_P' is valid.  */

int
6713
frv_legitimate_constant_p (rtx x)
bernds's avatar
bernds committed
6714 6715 6716
{
  enum machine_mode mode = GET_MODE (x);

6717 6718 6719 6720 6721 6722 6723 6724 6725 6726 6727 6728 6729 6730 6731 6732
  /* frv_cannot_force_const_mem always returns true for FDPIC.  This
     means that the move expanders will be expected to deal with most
     kinds of constant, regardless of what we return here.

     However, among its other duties, LEGITIMATE_CONSTANT_P decides whether
     a constant can be entered into reg_equiv_constant[].  If we return true,
     reload can create new instances of the constant whenever it likes.

     The idea is therefore to accept as many constants as possible (to give
     reload more freedom) while rejecting constants that can only be created
     at certain times.  In particular, anything with a symbolic component will
     require use of the pseudo FDPIC register, which is only available before
     reload.  */
  if (TARGET_FDPIC)
    return LEGITIMATE_PIC_OPERAND_P (x);

6733
  /* All of the integer constants are ok.  */
bernds's avatar
bernds committed
6734 6735 6736
  if (GET_CODE (x) != CONST_DOUBLE)
    return TRUE;

6737
  /* double integer constants are ok.  */
bernds's avatar
bernds committed
6738 6739 6740
  if (mode == VOIDmode || mode == DImode)
    return TRUE;

6741
  /* 0 is always ok.  */
bernds's avatar
bernds committed
6742 6743 6744 6745
  if (x == CONST0_RTX (mode))
    return TRUE;

  /* If floating point is just emulated, allow any constant, since it will be
6746
     constructed in the GPRs.  */
bernds's avatar
bernds committed
6747 6748 6749 6750 6751 6752 6753 6754 6755
  if (!TARGET_HAS_FPRS)
    return TRUE;

  if (mode == DFmode && !TARGET_DOUBLE)
    return TRUE;

  /* Otherwise store the constant away and do a load.  */
  return FALSE;
}
6756 6757 6758 6759 6760 6761 6762 6763 6764 6765 6766 6767 6768 6769 6770 6771 6772 6773 6774 6775 6776 6777 6778 6779 6780 6781 6782 6783 6784 6785

/* Implement SELECT_CC_MODE.  Choose CC_FP for floating-point comparisons,
   CC_NZ for comparisons against zero in which a single Z or N flag test
   is enough, CC_UNS for other unsigned comparisons, and CC for other
   signed comparisons.  */

enum machine_mode
frv_select_cc_mode (enum rtx_code code, rtx x, rtx y)
{
  if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
    return CC_FPmode;

  switch (code)
    {
    case EQ:
    case NE:
    case LT:
    case GE:
      return y == const0_rtx ? CC_NZmode : CCmode;

    case GTU:
    case GEU:
    case LTU:
    case LEU:
      return y == const0_rtx ? CC_NZmode : CC_UNSmode;

    default:
      return CCmode;
    }
}
bernds's avatar
bernds committed
6786 6787 6788 6789 6790 6791 6792 6793 6794 6795 6796 6797 6798 6799 6800 6801 6802 6803 6804 6805 6806 6807

/* A C expression for the cost of moving data from a register in class FROM to
   one in class TO.  The classes are expressed using the enumeration values
   such as `GENERAL_REGS'.  A value of 4 is the default; other values are
   interpreted relative to that.

   It is not required that the cost always equal 2 when FROM is the same as TO;
   on some machines it is expensive to move between registers if they are not
   general registers.

   If reload sees an insn consisting of a single `set' between two hard
   registers, and if `REGISTER_MOVE_COST' applied to their classes returns a
   value of 2, reload does not check to ensure that the constraints of the insn
   are met.  Setting a cost of other than 2 will allow reload to verify that
   the constraints are met.  You should do this if the `movM' pattern's
   constraints do not allow such copying.  */

#define HIGH_COST 40
#define MEDIUM_COST 3
#define LOW_COST 1

int
6808
frv_register_move_cost (enum reg_class from, enum reg_class to)
bernds's avatar
bernds committed
6809 6810 6811 6812 6813 6814 6815 6816 6817 6818 6819 6820 6821 6822 6823 6824 6825 6826 6827 6828 6829 6830 6831 6832 6833 6834 6835 6836 6837 6838 6839 6840 6841 6842 6843 6844 6845 6846 6847 6848 6849 6850 6851 6852 6853 6854 6855 6856 6857 6858 6859 6860 6861 6862 6863 6864 6865 6866 6867 6868 6869 6870 6871 6872 6873 6874 6875 6876 6877 6878 6879 6880 6881 6882 6883 6884 6885 6886 6887 6888 6889 6890 6891 6892 6893 6894 6895 6896
{
  switch (from)
    {
    default:
      break;

    case QUAD_REGS:
    case EVEN_REGS:
    case GPR_REGS:
      switch (to)
	{
	default:
	  break;

	case QUAD_REGS:
	case EVEN_REGS:
	case GPR_REGS:
	  return LOW_COST;

	case FEVEN_REGS:
	case FPR_REGS:
	  return LOW_COST;

	case LCR_REG:
	case LR_REG:
	case SPR_REGS:
	  return LOW_COST;
	}

    case FEVEN_REGS:
    case FPR_REGS:
      switch (to)
	{
	default:
	  break;

	case QUAD_REGS:
	case EVEN_REGS:
	case GPR_REGS:
	case ACC_REGS:
	case EVEN_ACC_REGS:
	case QUAD_ACC_REGS:
	case ACCG_REGS:
	  return MEDIUM_COST;

	case FEVEN_REGS:
	case FPR_REGS:
	  return LOW_COST;
	}

    case LCR_REG:
    case LR_REG:
    case SPR_REGS:
      switch (to)
	{
	default:
	  break;

	case QUAD_REGS:
	case EVEN_REGS:
	case GPR_REGS:
	  return MEDIUM_COST;
	}

    case ACC_REGS:
    case EVEN_ACC_REGS:
    case QUAD_ACC_REGS:
    case ACCG_REGS:
      switch (to)
	{
	default:
	  break;

	case FEVEN_REGS:
	case FPR_REGS:
	  return MEDIUM_COST;

	}
    }

  return HIGH_COST;
}

/* Implementation of TARGET_ASM_INTEGER.  In the FRV case we need to
   use ".picptr" to generate safe relocations for PIC code.  We also
   need a fixup entry for aligned (non-debugging) code.  */

static bool
6897
frv_assemble_integer (rtx value, unsigned int size, int aligned_p)
bernds's avatar
bernds committed
6898
{
6899
  if ((flag_pic || TARGET_FDPIC) && size == UNITS_PER_WORD)
bernds's avatar
bernds committed
6900 6901 6902 6903 6904
    {
      if (GET_CODE (value) == CONST
	  || GET_CODE (value) == SYMBOL_REF
	  || GET_CODE (value) == LABEL_REF)
	{
6905 6906 6907 6908 6909 6910 6911 6912 6913 6914 6915 6916
	  if (TARGET_FDPIC && GET_CODE (value) == SYMBOL_REF
	      && SYMBOL_REF_FUNCTION_P (value))
	    {
	      fputs ("\t.picptr\tfuncdesc(", asm_out_file);
	      output_addr_const (asm_out_file, value);
	      fputs (")\n", asm_out_file);
	      return true;
	    }
	  else if (TARGET_FDPIC && GET_CODE (value) == CONST
		   && frv_function_symbol_referenced_p (value))
	    return false;
	  if (aligned_p && !TARGET_FDPIC)
bernds's avatar
bernds committed
6917 6918 6919 6920 6921 6922
	    {
	      static int label_num = 0;
	      char buf[256];
	      const char *p;

	      ASM_GENERATE_INTERNAL_LABEL (buf, "LCP", label_num++);
6923
	      p = (* targetm.strip_name_encoding) (buf);
bernds's avatar
bernds committed
6924 6925 6926 6927 6928 6929 6930 6931 6932 6933 6934 6935 6936 6937 6938 6939 6940 6941 6942 6943 6944 6945 6946

	      fprintf (asm_out_file, "%s:\n", p);
	      fprintf (asm_out_file, "%s\n", FIXUP_SECTION_ASM_OP);
	      fprintf (asm_out_file, "\t.picptr\t%s\n", p);
	      fprintf (asm_out_file, "\t.previous\n");
	    }
	  assemble_integer_with_op ("\t.picptr\t", value);
	  return true;
	}
      if (!aligned_p)
	{
	  /* We've set the unaligned SI op to NULL, so we always have to
	     handle the unaligned case here.  */
	  assemble_integer_with_op ("\t.4byte\t", value);
	  return true;
	}
    }
  return default_assemble_integer (value, size, aligned_p);
}

/* Function to set up the backend function structure.  */

static struct machine_function *
6947
frv_init_machine_status (void)
bernds's avatar
bernds committed
6948 6949 6950
{
  return ggc_alloc_cleared (sizeof (struct machine_function));
}
6951

6952 6953
/* Implement TARGET_SCHED_ISSUE_RATE.  */

6954
int
6955 6956 6957 6958 6959 6960 6961 6962 6963 6964 6965 6966 6967
frv_issue_rate (void)
{
  if (!TARGET_PACK)
    return 1;

  switch (frv_cpu_type)
    {
    default:
    case FRV_CPU_FR300:
    case FRV_CPU_SIMPLE:
      return 1;

    case FRV_CPU_FR400:
6968 6969
    case FRV_CPU_FR405:
    case FRV_CPU_FR450:
6970 6971 6972 6973 6974 6975
      return 2;

    case FRV_CPU_GENERIC:
    case FRV_CPU_FR500:
    case FRV_CPU_TOMCAT:
      return 4;
6976 6977 6978

    case FRV_CPU_FR550:
      return 8;
6979 6980
    }
}
bernds's avatar
bernds committed
6981

6982 6983 6984 6985
/* A for_each_rtx callback.  If X refers to an accumulator, return
   ACC_GROUP_ODD if the bit 2 of the register number is set and
   ACC_GROUP_EVEN if it is clear.  Return 0 (ACC_GROUP_NONE)
   otherwise.  */
bernds's avatar
bernds committed
6986

6987 6988
static int
frv_acc_group_1 (rtx *x, void *data ATTRIBUTE_UNUSED)
bernds's avatar
bernds committed
6989
{
6990
  if (REG_P (*x))
bernds's avatar
bernds committed
6991
    {
6992 6993 6994 6995 6996 6997 6998
      if (ACC_P (REGNO (*x)))
	return (REGNO (*x) - ACC_FIRST) & 4 ? ACC_GROUP_ODD : ACC_GROUP_EVEN;
      if (ACCG_P (REGNO (*x)))
	return (REGNO (*x) - ACCG_FIRST) & 4 ? ACC_GROUP_ODD : ACC_GROUP_EVEN;
    }
  return 0;
}
bernds's avatar
bernds committed
6999

7000
/* Return the value of INSN's acc_group attribute.  */
bernds's avatar
bernds committed
7001

7002 7003 7004 7005 7006 7007 7008 7009
int
frv_acc_group (rtx insn)
{
  /* This distinction only applies to the FR550 packing constraints.  */
  if (frv_cpu_type != FRV_CPU_FR550)
    return ACC_GROUP_NONE;
  return for_each_rtx (&PATTERN (insn), frv_acc_group_1, 0);
}
bernds's avatar
bernds committed
7010

7011 7012 7013
/* Return the index of the DFA unit in FRV_UNIT_NAMES[] that instruction
   INSN will try to claim first.  Since this value depends only on the
   type attribute, we can cache the results in FRV_TYPE_TO_UNIT[].  */
bernds's avatar
bernds committed
7014

7015 7016 7017 7018
static unsigned int
frv_insn_unit (rtx insn)
{
  enum attr_type type;
bernds's avatar
bernds committed
7019

7020 7021 7022 7023 7024 7025
  type = get_attr_type (insn);
  if (frv_type_to_unit[type] == ARRAY_SIZE (frv_unit_codes))
    {
      /* We haven't seen this type of instruction before.  */
      state_t state;
      unsigned int unit;
bernds's avatar
bernds committed
7026

7027 7028 7029 7030
      /* Issue the instruction on its own to see which unit it prefers.  */
      state = alloca (state_size ());
      state_reset (state);
      state_transition (state, insn);
bernds's avatar
bernds committed
7031

7032 7033 7034 7035
      /* Find out which unit was taken.  */
      for (unit = 0; unit < ARRAY_SIZE (frv_unit_codes); unit++)
	if (cpu_unit_reservation_p (state, frv_unit_codes[unit]))
	  break;
bernds's avatar
bernds committed
7036

7037
      gcc_assert (unit != ARRAY_SIZE (frv_unit_codes));
bernds's avatar
bernds committed
7038

7039
      frv_type_to_unit[type] = unit;
bernds's avatar
bernds committed
7040
    }
7041 7042
  return frv_type_to_unit[type];
}
bernds's avatar
bernds committed
7043

7044
/* Return true if INSN issues to a branch unit.  */
bernds's avatar
bernds committed
7045

7046 7047 7048 7049 7050 7051 7052 7053 7054 7055 7056 7057 7058 7059 7060 7061 7062 7063 7064 7065 7066 7067 7068 7069 7070 7071 7072 7073 7074 7075 7076 7077 7078 7079 7080 7081 7082 7083 7084 7085 7086 7087 7088 7089 7090 7091 7092 7093 7094 7095 7096 7097 7098 7099 7100
static bool
frv_issues_to_branch_unit_p (rtx insn)
{
  return frv_unit_groups[frv_insn_unit (insn)] == GROUP_B;
}

/* The current state of the packing pass, implemented by frv_pack_insns.  */
static struct {
  /* The state of the pipeline DFA.  */
  state_t dfa_state;

  /* Which hardware registers are set within the current packet,
     and the conditions under which they are set.  */
  regstate_t regstate[FIRST_PSEUDO_REGISTER];

  /* The memory locations that have been modified so far in this
     packet.  MEM is the memref and COND is the regstate_t condition
     under which it is set.  */
  struct {
    rtx mem;
    regstate_t cond;
  } mems[2];

  /* The number of valid entries in MEMS.  The value is larger than
     ARRAY_SIZE (mems) if there were too many mems to record.  */
  unsigned int num_mems;

  /* The maximum number of instructions that can be packed together.  */
  unsigned int issue_rate;

  /* The instructions in the packet, partitioned into groups.  */
  struct frv_packet_group {
    /* How many instructions in the packet belong to this group.  */
    unsigned int num_insns;

    /* A list of the instructions that belong to this group, in the order
       they appear in the rtl stream.  */
    rtx insns[ARRAY_SIZE (frv_unit_codes)];

    /* The contents of INSNS after they have been sorted into the correct
       assembly-language order.  Element X issues to unit X.  The list may
       contain extra nops.  */
    rtx sorted[ARRAY_SIZE (frv_unit_codes)];

    /* The member of frv_nops[] to use in sorted[].  */
    rtx nop;
  } groups[NUM_GROUPS];

  /* The instructions that make up the current packet.  */
  rtx insns[ARRAY_SIZE (frv_unit_codes)];
  unsigned int num_insns;
} frv_packet;

/* Return the regstate_t flags for the given COND_EXEC condition.
   Abort if the condition isn't in the right form.  */
bernds's avatar
bernds committed
7101

7102 7103 7104
static int
frv_cond_flags (rtx cond)
{
7105 7106 7107 7108 7109 7110 7111 7112
  gcc_assert ((GET_CODE (cond) == EQ || GET_CODE (cond) == NE)
	      && GET_CODE (XEXP (cond, 0)) == REG
	      && CR_P (REGNO (XEXP (cond, 0)))
	      && XEXP (cond, 1) == const0_rtx);
  return ((REGNO (XEXP (cond, 0)) - CR_FIRST)
	  | (GET_CODE (cond) == NE
	     ? REGSTATE_IF_TRUE
	     : REGSTATE_IF_FALSE));
7113
}
bernds's avatar
bernds committed
7114 7115


7116 7117
/* Return true if something accessed under condition COND2 can
   conflict with something written under condition COND1.  */
bernds's avatar
bernds committed
7118

7119 7120 7121 7122 7123 7124 7125 7126 7127 7128 7129 7130 7131 7132 7133 7134 7135 7136 7137
static bool
frv_regstate_conflict_p (regstate_t cond1, regstate_t cond2)
{
  /* If either reference was unconditional, we have a conflict.  */
  if ((cond1 & REGSTATE_IF_EITHER) == 0
      || (cond2 & REGSTATE_IF_EITHER) == 0)
    return true;

  /* The references might conflict if they were controlled by
     different CRs.  */
  if ((cond1 & REGSTATE_CC_MASK) != (cond2 & REGSTATE_CC_MASK))
    return true;

  /* They definitely conflict if they are controlled by the
     same condition.  */
  if ((cond1 & cond2 & REGSTATE_IF_EITHER) != 0)
    return true;

  return false;
bernds's avatar
bernds committed
7138 7139
}

7140 7141 7142 7143

/* A for_each_rtx callback.  Return 1 if *X depends on an instruction in
   the current packet.  DATA points to a regstate_t that describes the
   condition under which *X might be set or used.  */
bernds's avatar
bernds committed
7144 7145

static int
7146
frv_registers_conflict_p_1 (rtx *x, void *data)
bernds's avatar
bernds committed
7147
{
7148 7149
  unsigned int regno, i;
  regstate_t cond;
bernds's avatar
bernds committed
7150

7151
  cond = *(regstate_t *) data;
bernds's avatar
bernds committed
7152

7153 7154 7155 7156 7157
  if (GET_CODE (*x) == REG)
    FOR_EACH_REGNO (regno, *x)
      if ((frv_packet.regstate[regno] & REGSTATE_MODIFIED) != 0)
	if (frv_regstate_conflict_p (frv_packet.regstate[regno], cond))
	  return 1;
bernds's avatar
bernds committed
7158

7159 7160 7161 7162 7163
  if (GET_CODE (*x) == MEM)
    {
      /* If we ran out of memory slots, assume a conflict.  */
      if (frv_packet.num_mems > ARRAY_SIZE (frv_packet.mems))
	return 1;
bernds's avatar
bernds committed
7164

7165 7166 7167 7168 7169 7170 7171
      /* Check for output or true dependencies with earlier MEMs.  */
      for (i = 0; i < frv_packet.num_mems; i++)
	if (frv_regstate_conflict_p (frv_packet.mems[i].cond, cond))
	  {
	    if (true_dependence (frv_packet.mems[i].mem, VOIDmode,
				 *x, rtx_varies_p))
	      return 1;
bernds's avatar
bernds committed
7172

7173 7174 7175 7176
	    if (output_dependence (frv_packet.mems[i].mem, *x))
	      return 1;
	  }
    }
bernds's avatar
bernds committed
7177

7178 7179 7180 7181 7182 7183 7184 7185
  /* The return values of calls aren't significant: they describe
     the effect of the call as a whole, not of the insn itself.  */
  if (GET_CODE (*x) == SET && GET_CODE (SET_SRC (*x)) == CALL)
    {
      if (for_each_rtx (&SET_SRC (*x), frv_registers_conflict_p_1, data))
	return 1;
      return -1;
    }
bernds's avatar
bernds committed
7186

7187 7188 7189
  /* Check subexpressions.  */
  return 0;
}
bernds's avatar
bernds committed
7190 7191


7192 7193
/* Return true if something in X might depend on an instruction
   in the current packet.  */
bernds's avatar
bernds committed
7194

7195 7196 7197 7198
static bool
frv_registers_conflict_p (rtx x)
{
  regstate_t flags;
bernds's avatar
bernds committed
7199

7200 7201 7202 7203 7204
  flags = 0;
  if (GET_CODE (x) == COND_EXEC)
    {
      if (for_each_rtx (&XEXP (x, 0), frv_registers_conflict_p_1, &flags))
	return true;
bernds's avatar
bernds committed
7205

7206 7207
      flags |= frv_cond_flags (XEXP (x, 0));
      x = XEXP (x, 1);
bernds's avatar
bernds committed
7208
    }
7209 7210
  return for_each_rtx (&x, frv_registers_conflict_p_1, &flags);
}
bernds's avatar
bernds committed
7211 7212


7213 7214
/* A note_stores callback.  DATA points to the regstate_t condition
   under which X is modified.  Update FRV_PACKET accordingly.  */
bernds's avatar
bernds committed
7215

7216
static void
7217
frv_registers_update_1 (rtx x, const_rtx pat ATTRIBUTE_UNUSED, void *data)
7218 7219 7220 7221 7222 7223 7224 7225
{
  unsigned int regno;

  if (GET_CODE (x) == REG)
    FOR_EACH_REGNO (regno, x)
      frv_packet.regstate[regno] |= *(regstate_t *) data;

  if (GET_CODE (x) == MEM)
bernds's avatar
bernds committed
7226
    {
7227
      if (frv_packet.num_mems < ARRAY_SIZE (frv_packet.mems))
bernds's avatar
bernds committed
7228
	{
7229 7230 7231 7232 7233 7234
	  frv_packet.mems[frv_packet.num_mems].mem = x;
	  frv_packet.mems[frv_packet.num_mems].cond = *(regstate_t *) data;
	}
      frv_packet.num_mems++;
    }
}
bernds's avatar
bernds committed
7235 7236


7237 7238 7239 7240 7241 7242 7243 7244 7245 7246 7247 7248 7249
/* Update the register state information for an instruction whose
   body is X.  */

static void
frv_registers_update (rtx x)
{
  regstate_t flags;

  flags = REGSTATE_MODIFIED;
  if (GET_CODE (x) == COND_EXEC)
    {
      flags |= frv_cond_flags (XEXP (x, 0));
      x = XEXP (x, 1);
bernds's avatar
bernds committed
7250
    }
7251 7252
  note_stores (x, frv_registers_update_1, &flags);
}
bernds's avatar
bernds committed
7253

7254 7255 7256 7257 7258 7259 7260 7261 7262 7263 7264 7265 7266

/* Initialize frv_packet for the start of a new packet.  */

static void
frv_start_packet (void)
{
  enum frv_insn_group group;

  memset (frv_packet.regstate, 0, sizeof (frv_packet.regstate));
  frv_packet.num_mems = 0;
  frv_packet.num_insns = 0;
  for (group = 0; group < NUM_GROUPS; group++)
    frv_packet.groups[group].num_insns = 0;
bernds's avatar
bernds committed
7267 7268 7269
}


7270 7271 7272 7273
/* Likewise for the start of a new basic block.  */

static void
frv_start_packet_block (void)
bernds's avatar
bernds committed
7274
{
7275 7276 7277
  state_reset (frv_packet.dfa_state);
  frv_start_packet ();
}
bernds's avatar
bernds committed
7278

7279 7280 7281 7282 7283 7284 7285 7286

/* Finish the current packet, if any, and start a new one.  Call
   HANDLE_PACKET with FRV_PACKET describing the completed packet.  */

static void
frv_finish_packet (void (*handle_packet) (void))
{
  if (frv_packet.num_insns > 0)
bernds's avatar
bernds committed
7287
    {
7288 7289 7290 7291 7292
      handle_packet ();
      state_transition (frv_packet.dfa_state, 0);
      frv_start_packet ();
    }
}
bernds's avatar
bernds committed
7293 7294


7295 7296
/* Return true if INSN can be added to the current packet.  Update
   the DFA state on success.  */
bernds's avatar
bernds committed
7297

7298 7299 7300 7301 7302 7303
static bool
frv_pack_insn_p (rtx insn)
{
  /* See if the packet is already as long as it can be.  */
  if (frv_packet.num_insns == frv_packet.issue_rate)
    return false;
bernds's avatar
bernds committed
7304

7305 7306 7307
  /* If the scheduler thought that an instruction should start a packet,
     it's usually a good idea to believe it.  It knows much more about
     the latencies than we do.
bernds's avatar
bernds committed
7308

7309
     There are some exceptions though:
bernds's avatar
bernds committed
7310

7311 7312
       - Conditional instructions are scheduled on the assumption that
	 they will be executed.  This is usually a good thing, since it
7313
	 tends to avoid unnecessary stalls in the conditional code.
7314 7315 7316
	 But we want to pack conditional instructions as tightly as
	 possible, in order to optimize the case where they aren't
	 executed.
bernds's avatar
bernds committed
7317

7318 7319
       - The scheduler will always put branches on their own, even
	 if there's no real dependency.
bernds's avatar
bernds committed
7320

7321 7322 7323 7324 7325 7326 7327
       - There's no point putting a call in its own packet unless
	 we have to.  */
  if (frv_packet.num_insns > 0
      && GET_CODE (insn) == INSN
      && GET_MODE (insn) == TImode
      && GET_CODE (PATTERN (insn)) != COND_EXEC)
    return false;
bernds's avatar
bernds committed
7328

7329 7330 7331 7332 7333 7334
  /* Check for register conflicts.  Don't do this for setlo since any
     conflict will be with the partnering sethi, with which it can
     be packed.  */
  if (get_attr_type (insn) != TYPE_SETLO)
    if (frv_registers_conflict_p (PATTERN (insn)))
      return false;
bernds's avatar
bernds committed
7335

7336 7337
  return state_transition (frv_packet.dfa_state, insn) < 0;
}
bernds's avatar
bernds committed
7338 7339


7340
/* Add instruction INSN to the current packet.  */
bernds's avatar
bernds committed
7341

7342 7343 7344 7345 7346 7347 7348 7349 7350 7351
static void
frv_add_insn_to_packet (rtx insn)
{
  struct frv_packet_group *packet_group;

  packet_group = &frv_packet.groups[frv_unit_groups[frv_insn_unit (insn)]];
  packet_group->insns[packet_group->num_insns++] = insn;
  frv_packet.insns[frv_packet.num_insns++] = insn;

  frv_registers_update (PATTERN (insn));
bernds's avatar
bernds committed
7352 7353
}

7354 7355 7356 7357

/* Insert INSN (a member of frv_nops[]) into the current packet.  If the
   packet ends in a branch or call, insert the nop before it, otherwise
   add to the end.  */
bernds's avatar
bernds committed
7358 7359

static void
7360
frv_insert_nop_in_packet (rtx insn)
bernds's avatar
bernds committed
7361
{
7362 7363 7364 7365 7366 7367 7368 7369 7370 7371 7372 7373 7374 7375 7376 7377 7378 7379 7380
  struct frv_packet_group *packet_group;
  rtx last;

  packet_group = &frv_packet.groups[frv_unit_groups[frv_insn_unit (insn)]];
  last = frv_packet.insns[frv_packet.num_insns - 1];
  if (GET_CODE (last) != INSN)
    {
      insn = emit_insn_before (PATTERN (insn), last);
      frv_packet.insns[frv_packet.num_insns - 1] = insn;
      frv_packet.insns[frv_packet.num_insns++] = last;
    }
  else
    {
      insn = emit_insn_after (PATTERN (insn), last);
      frv_packet.insns[frv_packet.num_insns++] = insn;
    }
  packet_group->insns[packet_group->num_insns++] = insn;
}

bernds's avatar
bernds committed
7381

7382 7383 7384 7385 7386 7387 7388 7389 7390 7391 7392
/* If packing is enabled, divide the instructions into packets and
   return true.  Call HANDLE_PACKET for each complete packet.  */

static bool
frv_for_each_packet (void (*handle_packet) (void))
{
  rtx insn, next_insn;

  frv_packet.issue_rate = frv_issue_rate ();

  /* Early exit if we don't want to pack insns.  */
7393 7394
  if (!optimize
      || !flag_schedule_insns_after_reload
7395
      || !TARGET_VLIW_BRANCH
7396 7397
      || frv_packet.issue_rate == 1)
    return false;
bernds's avatar
bernds committed
7398

7399
  /* Set up the initial packing state.  */
bernds's avatar
bernds committed
7400
  dfa_start ();
7401
  frv_packet.dfa_state = alloca (state_size ());
bernds's avatar
bernds committed
7402

7403 7404
  frv_start_packet_block ();
  for (insn = get_insns (); insn != 0; insn = next_insn)
bernds's avatar
bernds committed
7405
    {
7406 7407
      enum rtx_code code;
      bool eh_insn_p;
bernds's avatar
bernds committed
7408

7409 7410 7411 7412
      code = GET_CODE (insn);
      next_insn = NEXT_INSN (insn);

      if (code == CODE_LABEL)
bernds's avatar
bernds committed
7413
	{
7414 7415 7416
	  frv_finish_packet (handle_packet);
	  frv_start_packet_block ();
	}
bernds's avatar
bernds committed
7417

7418 7419 7420 7421 7422 7423 7424 7425
      if (INSN_P (insn))
	switch (GET_CODE (PATTERN (insn)))
	  {
	  case USE:
	  case CLOBBER:
	  case ADDR_VEC:
	  case ADDR_DIFF_VEC:
	    break;
bernds's avatar
bernds committed
7426

7427 7428 7429 7430 7431 7432 7433 7434 7435 7436 7437 7438 7439 7440 7441 7442 7443 7444 7445 7446 7447 7448
	  default:
	    /* Calls mustn't be packed on a TOMCAT.  */
	    if (GET_CODE (insn) == CALL_INSN && frv_cpu_type == FRV_CPU_TOMCAT)
	      frv_finish_packet (handle_packet);

	    /* Since the last instruction in a packet determines the EH
	       region, any exception-throwing instruction must come at
	       the end of reordered packet.  Insns that issue to a
	       branch unit are bound to come last; for others it's
	       too hard to predict.  */
	    eh_insn_p = (find_reg_note (insn, REG_EH_REGION, NULL) != NULL);
	    if (eh_insn_p && !frv_issues_to_branch_unit_p (insn))
	      frv_finish_packet (handle_packet);

	    /* Finish the current packet if we can't add INSN to it.
	       Simulate cycles until INSN is ready to issue.  */
	    if (!frv_pack_insn_p (insn))
	      {
		frv_finish_packet (handle_packet);
		while (!frv_pack_insn_p (insn))
		  state_transition (frv_packet.dfa_state, 0);
	      }
bernds's avatar
bernds committed
7449

7450 7451 7452 7453 7454 7455 7456 7457 7458 7459 7460 7461 7462 7463 7464 7465 7466 7467 7468 7469 7470 7471 7472 7473 7474 7475 7476 7477 7478 7479 7480 7481 7482 7483 7484 7485 7486 7487 7488 7489 7490 7491 7492 7493 7494 7495 7496 7497 7498 7499 7500 7501 7502 7503 7504 7505 7506 7507 7508 7509 7510 7511 7512 7513 7514 7515 7516 7517 7518 7519 7520 7521 7522 7523 7524 7525 7526 7527 7528 7529 7530 7531 7532 7533 7534 7535 7536 7537 7538 7539 7540
	    /* Add the instruction to the packet.  */
	    frv_add_insn_to_packet (insn);

	    /* Calls and jumps end a packet, as do insns that throw
	       an exception.  */
	    if (code == CALL_INSN || code == JUMP_INSN || eh_insn_p)
	      frv_finish_packet (handle_packet);
	    break;
	  }
    }
  frv_finish_packet (handle_packet);
  dfa_finish ();
  return true;
}

/* Subroutine of frv_sort_insn_group.  We are trying to sort
   frv_packet.groups[GROUP].sorted[0...NUM_INSNS-1] into assembly
   language order.  We have already picked a new position for
   frv_packet.groups[GROUP].sorted[X] if bit X of ISSUED is set.
   These instructions will occupy elements [0, LOWER_SLOT) and
   [UPPER_SLOT, NUM_INSNS) of the final (sorted) array.  STATE is
   the DFA state after issuing these instructions.

   Try filling elements [LOWER_SLOT, UPPER_SLOT) with every permutation
   of the unused instructions.  Return true if one such permutation gives
   a valid ordering, leaving the successful permutation in sorted[].
   Do not modify sorted[] until a valid permutation is found.  */

static bool
frv_sort_insn_group_1 (enum frv_insn_group group,
		       unsigned int lower_slot, unsigned int upper_slot,
		       unsigned int issued, unsigned int num_insns,
		       state_t state)
{
  struct frv_packet_group *packet_group;
  unsigned int i;
  state_t test_state;
  size_t dfa_size;
  rtx insn;

  /* Early success if we've filled all the slots.  */
  if (lower_slot == upper_slot)
    return true;

  packet_group = &frv_packet.groups[group];
  dfa_size = state_size ();
  test_state = alloca (dfa_size);

  /* Try issuing each unused instruction.  */
  for (i = num_insns - 1; i + 1 != 0; i--)
    if (~issued & (1 << i))
      {
	insn = packet_group->sorted[i];
	memcpy (test_state, state, dfa_size);
	if (state_transition (test_state, insn) < 0
	    && cpu_unit_reservation_p (test_state,
				       NTH_UNIT (group, upper_slot - 1))
	    && frv_sort_insn_group_1 (group, lower_slot, upper_slot - 1,
				      issued | (1 << i), num_insns,
				      test_state))
	  {
	    packet_group->sorted[upper_slot - 1] = insn;
	    return true;
	  }
      }

  return false;
}

/* Compare two instructions by their frv_insn_unit.  */

static int
frv_compare_insns (const void *first, const void *second)
{
  const rtx *insn1 = first, *insn2 = second;
  return frv_insn_unit (*insn1) - frv_insn_unit (*insn2);
}

/* Copy frv_packet.groups[GROUP].insns[] to frv_packet.groups[GROUP].sorted[]
   and sort it into assembly language order.  See frv.md for a description of
   the algorithm.  */

static void
frv_sort_insn_group (enum frv_insn_group group)
{
  struct frv_packet_group *packet_group;
  unsigned int first, i, nop, max_unit, num_slots;
  state_t state, test_state;
  size_t dfa_size;

  packet_group = &frv_packet.groups[group];
7541 7542 7543 7544

  /* Assume no nop is needed.  */
  packet_group->nop = 0;

7545 7546 7547 7548 7549 7550 7551 7552 7553 7554 7555 7556 7557 7558 7559 7560 7561 7562 7563 7564 7565 7566 7567 7568 7569 7570 7571 7572 7573 7574 7575 7576 7577 7578 7579
  if (packet_group->num_insns == 0)
    return;

  /* Copy insns[] to sorted[].  */
  memcpy (packet_group->sorted, packet_group->insns,
	  sizeof (rtx) * packet_group->num_insns);

  /* Sort sorted[] by the unit that each insn tries to take first.  */
  if (packet_group->num_insns > 1)
    qsort (packet_group->sorted, packet_group->num_insns,
	   sizeof (rtx), frv_compare_insns);

  /* That's always enough for branch and control insns.  */
  if (group == GROUP_B || group == GROUP_C)
    return;

  dfa_size = state_size ();
  state = alloca (dfa_size);
  test_state = alloca (dfa_size);

  /* Find the highest FIRST such that sorted[0...FIRST-1] can issue
     consecutively and such that the DFA takes unit X when sorted[X]
     is added.  Set STATE to the new DFA state.  */
  state_reset (test_state);
  for (first = 0; first < packet_group->num_insns; first++)
    {
      memcpy (state, test_state, dfa_size);
      if (state_transition (test_state, packet_group->sorted[first]) >= 0
	  || !cpu_unit_reservation_p (test_state, NTH_UNIT (group, first)))
	break;
    }

  /* If all the instructions issued in ascending order, we're done.  */
  if (first == packet_group->num_insns)
    return;
bernds's avatar
bernds committed
7580

7581 7582 7583 7584 7585 7586
  /* Add nops to the end of sorted[] and try each permutation until
     we find one that works.  */
  for (nop = 0; nop < frv_num_nops; nop++)
    {
      max_unit = frv_insn_unit (frv_nops[nop]);
      if (frv_unit_groups[max_unit] == group)
bernds's avatar
bernds committed
7587
	{
7588 7589 7590 7591 7592 7593 7594
	  packet_group->nop = frv_nops[nop];
	  num_slots = UNIT_NUMBER (max_unit) + 1;
	  for (i = packet_group->num_insns; i < num_slots; i++)
	    packet_group->sorted[i] = frv_nops[nop];
	  if (frv_sort_insn_group_1 (group, first, num_slots,
				     (1 << first) - 1, num_slots, state))
	    return;
bernds's avatar
bernds committed
7595
	}
7596
    }
7597
  gcc_unreachable ();
7598 7599 7600 7601
}

/* Sort the current packet into assembly-language order.  Set packing
   flags as appropriate.  */
bernds's avatar
bernds committed
7602

7603 7604 7605 7606 7607 7608 7609 7610 7611 7612 7613 7614 7615 7616 7617 7618 7619 7620 7621 7622 7623 7624 7625 7626
static void
frv_reorder_packet (void)
{
  unsigned int cursor[NUM_GROUPS];
  rtx insns[ARRAY_SIZE (frv_unit_groups)];
  unsigned int unit, to, from;
  enum frv_insn_group group;
  struct frv_packet_group *packet_group;

  /* First sort each group individually.  */
  for (group = 0; group < NUM_GROUPS; group++)
    {
      cursor[group] = 0;
      frv_sort_insn_group (group);
    }

  /* Go through the unit template and try add an instruction from
     that unit's group.  */
  to = 0;
  for (unit = 0; unit < ARRAY_SIZE (frv_unit_groups); unit++)
    {
      group = frv_unit_groups[unit];
      packet_group = &frv_packet.groups[group];
      if (cursor[group] < packet_group->num_insns)
bernds's avatar
bernds committed
7627
	{
7628
	  /* frv_reorg should have added nops for us.  */
7629 7630
	  gcc_assert (packet_group->sorted[cursor[group]]
		      != packet_group->nop);
7631
	  insns[to++] = packet_group->sorted[cursor[group]++];
bernds's avatar
bernds committed
7632
	}
7633
    }
bernds's avatar
bernds committed
7634

7635
  gcc_assert (to == frv_packet.num_insns);
bernds's avatar
bernds committed
7636

7637 7638 7639 7640 7641 7642
  /* Clear the last instruction's packing flag, thus marking the end of
     a packet.  Reorder the other instructions relative to it.  */
  CLEAR_PACKING_FLAG (insns[to - 1]);
  for (from = 0; from < to - 1; from++)
    {
      remove_insn (insns[from]);
dberlin's avatar
dberlin committed
7643
      add_insn_before (insns[from], insns[to - 1], NULL);
7644 7645 7646
      SET_PACKING_FLAG (insns[from]);
    }
}
bernds's avatar
bernds committed
7647 7648


7649 7650 7651 7652 7653 7654 7655 7656 7657 7658 7659 7660 7661 7662 7663 7664 7665 7666 7667 7668 7669 7670 7671 7672 7673 7674 7675 7676 7677 7678 7679 7680 7681 7682 7683 7684 7685 7686 7687 7688 7689 7690
/* Divide instructions into packets.  Reorder the contents of each
   packet so that they are in the correct assembly-language order.

   Since this pass can change the raw meaning of the rtl stream, it must
   only be called at the last minute, just before the instructions are
   written out.  */

static void
frv_pack_insns (void)
{
  if (frv_for_each_packet (frv_reorder_packet))
    frv_insn_packing_flag = 0;
  else
    frv_insn_packing_flag = -1;
}

/* See whether we need to add nops to group GROUP in order to
   make a valid packet.  */

static void
frv_fill_unused_units (enum frv_insn_group group)
{
  unsigned int non_nops, nops, i;
  struct frv_packet_group *packet_group;

  packet_group = &frv_packet.groups[group];

  /* Sort the instructions into assembly-language order.
     Use nops to fill slots that are otherwise unused.  */
  frv_sort_insn_group (group);

  /* See how many nops are needed before the final useful instruction.  */
  i = nops = 0;
  for (non_nops = 0; non_nops < packet_group->num_insns; non_nops++)
    while (packet_group->sorted[i++] == packet_group->nop)
      nops++;

  /* Insert that many nops into the instruction stream.  */
  while (nops-- > 0)
    frv_insert_nop_in_packet (packet_group->nop);
}

7691 7692 7693 7694 7695 7696 7697 7698 7699 7700 7701 7702 7703 7704 7705 7706 7707 7708 7709 7710 7711 7712 7713 7714 7715 7716 7717 7718 7719 7720 7721 7722 7723 7724 7725 7726 7727 7728 7729 7730 7731 7732 7733 7734 7735 7736 7737 7738 7739 7740 7741 7742 7743 7744 7745 7746 7747 7748 7749 7750 7751 7752 7753 7754
/* Return true if accesses IO1 and IO2 refer to the same doubleword.  */

static bool
frv_same_doubleword_p (const struct frv_io *io1, const struct frv_io *io2)
{
  if (io1->const_address != 0 && io2->const_address != 0)
    return io1->const_address == io2->const_address;

  if (io1->var_address != 0 && io2->var_address != 0)
    return rtx_equal_p (io1->var_address, io2->var_address);

  return false;
}

/* Return true if operations IO1 and IO2 are guaranteed to complete
   in order.  */

static bool
frv_io_fixed_order_p (const struct frv_io *io1, const struct frv_io *io2)
{
  /* The order of writes is always preserved.  */
  if (io1->type == FRV_IO_WRITE && io2->type == FRV_IO_WRITE)
    return true;

  /* The order of reads isn't preserved.  */
  if (io1->type != FRV_IO_WRITE && io2->type != FRV_IO_WRITE)
    return false;

  /* One operation is a write and the other is (or could be) a read.
     The order is only guaranteed if the accesses are to the same
     doubleword.  */
  return frv_same_doubleword_p (io1, io2);
}

/* Generalize I/O operation X so that it covers both X and Y. */

static void
frv_io_union (struct frv_io *x, const struct frv_io *y)
{
  if (x->type != y->type)
    x->type = FRV_IO_UNKNOWN;
  if (!frv_same_doubleword_p (x, y))
    {
      x->const_address = 0;
      x->var_address = 0;
    }
}

/* Fill IO with information about the load or store associated with
   membar instruction INSN.  */

static void
frv_extract_membar (struct frv_io *io, rtx insn)
{
  extract_insn (insn);
  io->type = INTVAL (recog_data.operand[2]);
  io->const_address = INTVAL (recog_data.operand[1]);
  io->var_address = XEXP (recog_data.operand[0], 0);
}

/* A note_stores callback for which DATA points to an rtx.  Nullify *DATA
   if X is a register and *DATA depends on X.  */

static void
7755
frv_io_check_address (rtx x, const_rtx pat ATTRIBUTE_UNUSED, void *data)
7756 7757 7758 7759 7760 7761 7762 7763 7764 7765 7766
{
  rtx *other = data;

  if (REG_P (x) && *other != 0 && reg_overlap_mentioned_p (x, *other))
    *other = 0;
}

/* A note_stores callback for which DATA points to a HARD_REG_SET.
   Remove every modified register from the set.  */

static void
7767
frv_io_handle_set (rtx x, const_rtx pat ATTRIBUTE_UNUSED, void *data)
7768 7769 7770 7771 7772 7773 7774 7775 7776 7777 7778 7779 7780 7781 7782 7783 7784 7785 7786 7787 7788 7789 7790 7791 7792 7793 7794 7795 7796 7797 7798 7799 7800 7801 7802 7803 7804 7805 7806 7807 7808 7809 7810 7811 7812 7813 7814 7815 7816 7817 7818 7819 7820 7821 7822 7823 7824 7825 7826 7827 7828 7829 7830 7831 7832 7833 7834 7835 7836 7837 7838 7839
{
  HARD_REG_SET *set = data;
  unsigned int regno;

  if (REG_P (x))
    FOR_EACH_REGNO (regno, x)
      CLEAR_HARD_REG_BIT (*set, regno);
}

/* A for_each_rtx callback for which DATA points to a HARD_REG_SET.
   Add every register in *X to the set.  */

static int
frv_io_handle_use_1 (rtx *x, void *data)
{
  HARD_REG_SET *set = data;
  unsigned int regno;

  if (REG_P (*x))
    FOR_EACH_REGNO (regno, *x)
      SET_HARD_REG_BIT (*set, regno);

  return 0;
}

/* A note_stores callback that applies frv_io_handle_use_1 to an
   entire rhs value.  */

static void
frv_io_handle_use (rtx *x, void *data)
{
  for_each_rtx (x, frv_io_handle_use_1, data);
}

/* Go through block BB looking for membars to remove.  There are two
   cases where intra-block analysis is enough:

   - a membar is redundant if it occurs between two consecutive I/O
   operations and if those operations are guaranteed to complete
   in order.

   - a membar for a __builtin_read is redundant if the result is
   used before the next I/O operation is issued.

   If the last membar in the block could not be removed, and there
   are guaranteed to be no I/O operations between that membar and
   the end of the block, store the membar in *LAST_MEMBAR, otherwise
   store null.

   Describe the block's first I/O operation in *NEXT_IO.  Describe
   an unknown operation if the block doesn't do any I/O.  */

static void
frv_optimize_membar_local (basic_block bb, struct frv_io *next_io,
			   rtx *last_membar)
{
  HARD_REG_SET used_regs;
  rtx next_membar, set, insn;
  bool next_is_end_p;

  /* NEXT_IO is the next I/O operation to be performed after the current
     instruction.  It starts off as being an unknown operation.  */
  memset (next_io, 0, sizeof (*next_io));

  /* NEXT_IS_END_P is true if NEXT_IO describes the end of the block.  */
  next_is_end_p = true;

  /* If the current instruction is a __builtin_read or __builtin_write,
     NEXT_MEMBAR is the membar instruction associated with it.  NEXT_MEMBAR
     is null if the membar has already been deleted.

     Note that the initialization here should only be needed to
7840
     suppress warnings.  */
7841 7842 7843 7844 7845 7846 7847 7848 7849 7850 7851 7852 7853 7854 7855 7856 7857 7858 7859 7860 7861 7862 7863 7864 7865 7866 7867 7868 7869 7870 7871 7872 7873 7874 7875 7876 7877 7878 7879 7880 7881 7882 7883 7884 7885 7886 7887 7888 7889 7890 7891 7892 7893 7894 7895 7896 7897 7898 7899 7900 7901 7902 7903 7904 7905 7906 7907 7908 7909 7910 7911 7912 7913 7914 7915 7916 7917 7918 7919 7920 7921 7922 7923 7924 7925 7926 7927 7928 7929 7930 7931 7932 7933 7934 7935 7936 7937 7938 7939 7940 7941 7942 7943 7944 7945 7946 7947 7948 7949 7950 7951 7952 7953 7954 7955 7956 7957 7958 7959 7960 7961 7962 7963 7964 7965 7966 7967 7968 7969 7970 7971 7972 7973 7974 7975 7976 7977 7978 7979 7980 7981 7982 7983 7984 7985 7986 7987 7988 7989 7990 7991 7992 7993 7994 7995 7996 7997 7998 7999 8000 8001 8002 8003 8004 8005 8006 8007 8008 8009 8010 8011 8012 8013 8014 8015 8016 8017 8018
  next_membar = 0;

  /* USED_REGS is the set of registers that are used before the
     next I/O instruction.  */
  CLEAR_HARD_REG_SET (used_regs);

  for (insn = BB_END (bb); insn != BB_HEAD (bb); insn = PREV_INSN (insn))
    if (GET_CODE (insn) == CALL_INSN)
      {
	/* We can't predict what a call will do to volatile memory.  */
	memset (next_io, 0, sizeof (struct frv_io));
	next_is_end_p = false;
	CLEAR_HARD_REG_SET (used_regs);
      }
    else if (INSN_P (insn))
      switch (recog_memoized (insn))
	{
	case CODE_FOR_optional_membar_qi:
	case CODE_FOR_optional_membar_hi:
	case CODE_FOR_optional_membar_si:
	case CODE_FOR_optional_membar_di:
	  next_membar = insn;
	  if (next_is_end_p)
	    {
	      /* Local information isn't enough to decide whether this
		 membar is needed.  Stash it away for later.  */
	      *last_membar = insn;
	      frv_extract_membar (next_io, insn);
	      next_is_end_p = false;
	    }
	  else
	    {
	      /* Check whether the I/O operation before INSN could be
		 reordered with one described by NEXT_IO.  If it can't,
		 INSN will not be needed.  */
	      struct frv_io prev_io;

	      frv_extract_membar (&prev_io, insn);
	      if (frv_io_fixed_order_p (&prev_io, next_io))
		{
		  if (dump_file)
		    fprintf (dump_file,
			     ";; [Local] Removing membar %d since order"
			     " of accesses is guaranteed\n",
			     INSN_UID (next_membar));

		  insn = NEXT_INSN (insn);
		  delete_insn (next_membar);
		  next_membar = 0;
		}
	      *next_io = prev_io;
	    }
	  break;

	default:
	  /* Invalidate NEXT_IO's address if it depends on something that
	     is clobbered by INSN.  */
	  if (next_io->var_address)
	    note_stores (PATTERN (insn), frv_io_check_address,
			 &next_io->var_address);

	  /* If the next membar is associated with a __builtin_read,
	     see if INSN reads from that address.  If it does, and if
	     the destination register is used before the next I/O access,
	     there is no need for the membar.  */
	  set = PATTERN (insn);
	  if (next_io->type == FRV_IO_READ
	      && next_io->var_address != 0
	      && next_membar != 0
	      && GET_CODE (set) == SET
	      && GET_CODE (SET_DEST (set)) == REG
	      && TEST_HARD_REG_BIT (used_regs, REGNO (SET_DEST (set))))
	    {
	      rtx src;

	      src = SET_SRC (set);
	      if (GET_CODE (src) == ZERO_EXTEND)
		src = XEXP (src, 0);

	      if (GET_CODE (src) == MEM
		  && rtx_equal_p (XEXP (src, 0), next_io->var_address))
		{
		  if (dump_file)
		    fprintf (dump_file,
			     ";; [Local] Removing membar %d since the target"
			     " of %d is used before the I/O operation\n",
			     INSN_UID (next_membar), INSN_UID (insn));

		  if (next_membar == *last_membar)
		    *last_membar = 0;

		  delete_insn (next_membar);
		  next_membar = 0;
		}
	    }

	  /* If INSN has volatile references, forget about any registers
	     that are used after it.  Otherwise forget about uses that
	     are (or might be) defined by INSN.  */
	  if (volatile_refs_p (PATTERN (insn)))
	    CLEAR_HARD_REG_SET (used_regs);
	  else
	    note_stores (PATTERN (insn), frv_io_handle_set, &used_regs);

	  note_uses (&PATTERN (insn), frv_io_handle_use, &used_regs);
	  break;
	}
}

/* See if MEMBAR, the last membar instruction in BB, can be removed.
   FIRST_IO[X] describes the first operation performed by basic block X.  */

static void
frv_optimize_membar_global (basic_block bb, struct frv_io *first_io,
			    rtx membar)
{
  struct frv_io this_io, next_io;
  edge succ;
  edge_iterator ei;

  /* We need to keep the membar if there is an edge to the exit block.  */
  FOR_EACH_EDGE (succ, ei, bb->succs)
  /* for (succ = bb->succ; succ != 0; succ = succ->succ_next) */
    if (succ->dest == EXIT_BLOCK_PTR)
      return;

  /* Work out the union of all successor blocks.  */
  ei = ei_start (bb->succs);
  ei_cond (ei, &succ);
  /* next_io = first_io[bb->succ->dest->index]; */
  next_io = first_io[succ->dest->index];
  ei = ei_start (bb->succs);
  if (ei_cond (ei, &succ))
    {
      for (ei_next (&ei); ei_cond (ei, &succ); ei_next (&ei))
	/*for (succ = bb->succ->succ_next; succ != 0; succ = succ->succ_next)*/
	frv_io_union (&next_io, &first_io[succ->dest->index]);
    }
  else
    gcc_unreachable ();

  frv_extract_membar (&this_io, membar);
  if (frv_io_fixed_order_p (&this_io, &next_io))
    {
      if (dump_file)
	fprintf (dump_file,
		 ";; [Global] Removing membar %d since order of accesses"
		 " is guaranteed\n", INSN_UID (membar));

      delete_insn (membar);
    }
}

/* Remove redundant membars from the current function.  */

static void
frv_optimize_membar (void)
{
  basic_block bb;
  struct frv_io *first_io;
  rtx *last_membar;

  compute_bb_for_insn ();
  first_io = xcalloc (last_basic_block, sizeof (struct frv_io));
  last_membar = xcalloc (last_basic_block, sizeof (rtx));

  FOR_EACH_BB (bb)
    frv_optimize_membar_local (bb, &first_io[bb->index],
			       &last_membar[bb->index]);

  FOR_EACH_BB (bb)
    if (last_membar[bb->index] != 0)
      frv_optimize_membar_global (bb, first_io, last_membar[bb->index]);

  free (first_io);
  free (last_membar);
}

8019 8020
/* Used by frv_reorg to keep track of the current packet's address.  */
static unsigned int frv_packet_address;
bernds's avatar
bernds committed
8021

8022 8023 8024 8025 8026 8027 8028 8029 8030 8031 8032 8033 8034 8035 8036 8037 8038 8039
/* If the current packet falls through to a label, try to pad the packet
   with nops in order to fit the label's alignment requirements.  */

static void
frv_align_label (void)
{
  unsigned int alignment, target, nop;
  rtx x, last, barrier, label;

  /* Walk forward to the start of the next packet.  Set ALIGNMENT to the
     maximum alignment of that packet, LABEL to the last label between
     the packets, and BARRIER to the last barrier.  */
  last = frv_packet.insns[frv_packet.num_insns - 1];
  label = barrier = 0;
  alignment = 4;
  for (x = NEXT_INSN (last); x != 0 && !INSN_P (x); x = NEXT_INSN (x))
    {
      if (LABEL_P (x))
bernds's avatar
bernds committed
8040
	{
8041 8042 8043
	  unsigned int subalign = 1 << label_to_alignment (x);
	  alignment = MAX (alignment, subalign);
	  label = x;
bernds's avatar
bernds committed
8044
	}
8045 8046 8047
      if (BARRIER_P (x))
	barrier = x;
    }
bernds's avatar
bernds committed
8048

8049 8050 8051 8052 8053 8054 8055
  /* If -malign-labels, and the packet falls through to an unaligned
     label, try introducing a nop to align that label to 8 bytes.  */
  if (TARGET_ALIGN_LABELS
      && label != 0
      && barrier == 0
      && frv_packet.num_insns < frv_packet.issue_rate)
    alignment = MAX (alignment, 8);
bernds's avatar
bernds committed
8056

8057 8058
  /* Advance the address to the end of the current packet.  */
  frv_packet_address += frv_packet.num_insns * 4;
bernds's avatar
bernds committed
8059

8060 8061 8062 8063 8064 8065 8066 8067 8068 8069 8070 8071 8072 8073 8074 8075 8076 8077 8078 8079 8080 8081 8082 8083 8084 8085 8086 8087 8088 8089 8090 8091 8092 8093
  /* Work out the target address, after alignment.  */
  target = (frv_packet_address + alignment - 1) & -alignment;

  /* If the packet falls through to the label, try to find an efficient
     padding sequence.  */
  if (barrier == 0)
    {
      /* First try adding nops to the current packet.  */
      for (nop = 0; nop < frv_num_nops; nop++)
	while (frv_packet_address < target && frv_pack_insn_p (frv_nops[nop]))
	  {
	    frv_insert_nop_in_packet (frv_nops[nop]);
	    frv_packet_address += 4;
	  }

      /* If we still haven't reached the target, add some new packets that
	 contain only nops.  If there are two types of nop, insert an
	 alternating sequence of frv_nops[0] and frv_nops[1], which will
	 lead to packets like:

		nop.p
		mnop.p/fnop.p
		nop.p
		mnop/fnop

	 etc.  Just emit frv_nops[0] if that's the only nop we have.  */
      last = frv_packet.insns[frv_packet.num_insns - 1];
      nop = 0;
      while (frv_packet_address < target)
	{
	  last = emit_insn_after (PATTERN (frv_nops[nop]), last);
	  frv_packet_address += 4;
	  if (frv_num_nops > 1)
	    nop ^= 1;
bernds's avatar
bernds committed
8094 8095 8096
	}
    }

8097
  frv_packet_address = target;
bernds's avatar
bernds committed
8098 8099
}

8100 8101 8102 8103 8104 8105 8106 8107 8108 8109 8110 8111 8112 8113 8114 8115 8116 8117 8118 8119 8120 8121 8122 8123 8124 8125 8126 8127 8128 8129 8130
/* Subroutine of frv_reorg, called after each packet has been constructed
   in frv_packet.  */

static void
frv_reorg_packet (void)
{
  frv_fill_unused_units (GROUP_I);
  frv_fill_unused_units (GROUP_FM);
  frv_align_label ();
}

/* Add an instruction with pattern NOP to frv_nops[].  */

static void
frv_register_nop (rtx nop)
{
  nop = make_insn_raw (nop);
  NEXT_INSN (nop) = 0;
  PREV_INSN (nop) = 0;
  frv_nops[frv_num_nops++] = nop;
}

/* Implement TARGET_MACHINE_DEPENDENT_REORG.  Divide the instructions
   into packets and check whether we need to insert nops in order to
   fulfill the processor's issue requirements.  Also, if the user has
   requested a certain alignment for a label, try to meet that alignment
   by inserting nops in the previous packet.  */

static void
frv_reorg (void)
{
8131 8132 8133
  if (optimize > 0 && TARGET_OPTIMIZE_MEMBAR && cfun->machine->has_membar_p)
    frv_optimize_membar ();

8134 8135 8136 8137 8138 8139 8140 8141 8142 8143 8144 8145 8146 8147
  frv_num_nops = 0;
  frv_register_nop (gen_nop ());
  if (TARGET_MEDIA)
    frv_register_nop (gen_mnop ());
  if (TARGET_HARD_FLOAT)
    frv_register_nop (gen_fnop ());

  /* Estimate the length of each branch.  Although this may change after
     we've inserted nops, it will only do so in big functions.  */
  shorten_branches (get_insns ());

  frv_packet_address = 0;
  frv_for_each_packet (frv_reorg_packet);
}
bernds's avatar
bernds committed
8148 8149

#define def_builtin(name, type, code) \
8150
  add_builtin_function ((name), (type), (code), BUILT_IN_MD, NULL, NULL)
bernds's avatar
bernds committed
8151 8152 8153 8154 8155 8156 8157 8158 8159 8160 8161 8162 8163 8164 8165 8166 8167

struct builtin_description
{
  enum insn_code icode;
  const char *name;
  enum frv_builtins code;
  enum rtx_code comparison;
  unsigned int flag;
};

/* Media intrinsics that take a single, constant argument.  */

static struct builtin_description bdesc_set[] =
{
  { CODE_FOR_mhdsets, "__MHDSETS", FRV_BUILTIN_MHDSETS, 0, 0 }
};

8168
/* Media intrinsics that take just one argument.  */
bernds's avatar
bernds committed
8169 8170 8171 8172 8173 8174 8175

static struct builtin_description bdesc_1arg[] =
{
  { CODE_FOR_mnot, "__MNOT", FRV_BUILTIN_MNOT, 0, 0 },
  { CODE_FOR_munpackh, "__MUNPACKH", FRV_BUILTIN_MUNPACKH, 0, 0 },
  { CODE_FOR_mbtoh, "__MBTOH", FRV_BUILTIN_MBTOH, 0, 0 },
  { CODE_FOR_mhtob, "__MHTOB", FRV_BUILTIN_MHTOB, 0, 0 },
8176 8177
  { CODE_FOR_mabshs, "__MABSHS", FRV_BUILTIN_MABSHS, 0, 0 },
  { CODE_FOR_scutss, "__SCUTSS", FRV_BUILTIN_SCUTSS, 0, 0 }
bernds's avatar
bernds committed
8178 8179
};

8180
/* Media intrinsics that take two arguments.  */
bernds's avatar
bernds committed
8181 8182 8183 8184 8185 8186 8187 8188 8189 8190 8191 8192 8193 8194 8195 8196 8197 8198 8199 8200 8201

static struct builtin_description bdesc_2arg[] =
{
  { CODE_FOR_mand, "__MAND", FRV_BUILTIN_MAND, 0, 0 },
  { CODE_FOR_mor, "__MOR", FRV_BUILTIN_MOR, 0, 0 },
  { CODE_FOR_mxor, "__MXOR", FRV_BUILTIN_MXOR, 0, 0 },
  { CODE_FOR_maveh, "__MAVEH", FRV_BUILTIN_MAVEH, 0, 0 },
  { CODE_FOR_msaths, "__MSATHS", FRV_BUILTIN_MSATHS, 0, 0 },
  { CODE_FOR_msathu, "__MSATHU", FRV_BUILTIN_MSATHU, 0, 0 },
  { CODE_FOR_maddhss, "__MADDHSS", FRV_BUILTIN_MADDHSS, 0, 0 },
  { CODE_FOR_maddhus, "__MADDHUS", FRV_BUILTIN_MADDHUS, 0, 0 },
  { CODE_FOR_msubhss, "__MSUBHSS", FRV_BUILTIN_MSUBHSS, 0, 0 },
  { CODE_FOR_msubhus, "__MSUBHUS", FRV_BUILTIN_MSUBHUS, 0, 0 },
  { CODE_FOR_mqaddhss, "__MQADDHSS", FRV_BUILTIN_MQADDHSS, 0, 0 },
  { CODE_FOR_mqaddhus, "__MQADDHUS", FRV_BUILTIN_MQADDHUS, 0, 0 },
  { CODE_FOR_mqsubhss, "__MQSUBHSS", FRV_BUILTIN_MQSUBHSS, 0, 0 },
  { CODE_FOR_mqsubhus, "__MQSUBHUS", FRV_BUILTIN_MQSUBHUS, 0, 0 },
  { CODE_FOR_mpackh, "__MPACKH", FRV_BUILTIN_MPACKH, 0, 0 },
  { CODE_FOR_mcop1, "__Mcop1", FRV_BUILTIN_MCOP1, 0, 0 },
  { CODE_FOR_mcop2, "__Mcop2", FRV_BUILTIN_MCOP2, 0, 0 },
  { CODE_FOR_mwcut, "__MWCUT", FRV_BUILTIN_MWCUT, 0, 0 },
8202 8203 8204 8205 8206 8207 8208 8209 8210 8211 8212 8213 8214 8215 8216 8217 8218 8219 8220 8221 8222 8223 8224 8225
  { CODE_FOR_mqsaths, "__MQSATHS", FRV_BUILTIN_MQSATHS, 0, 0 },
  { CODE_FOR_mqlclrhs, "__MQLCLRHS", FRV_BUILTIN_MQLCLRHS, 0, 0 },
  { CODE_FOR_mqlmths, "__MQLMTHS", FRV_BUILTIN_MQLMTHS, 0, 0 },
  { CODE_FOR_smul, "__SMUL", FRV_BUILTIN_SMUL, 0, 0 },
  { CODE_FOR_umul, "__UMUL", FRV_BUILTIN_UMUL, 0, 0 },
  { CODE_FOR_addss, "__ADDSS", FRV_BUILTIN_ADDSS, 0, 0 },
  { CODE_FOR_subss, "__SUBSS", FRV_BUILTIN_SUBSS, 0, 0 },
  { CODE_FOR_slass, "__SLASS", FRV_BUILTIN_SLASS, 0, 0 },
  { CODE_FOR_scan, "__SCAN", FRV_BUILTIN_SCAN, 0, 0 }
};

/* Integer intrinsics that take two arguments and have no return value.  */

static struct builtin_description bdesc_int_void2arg[] =
{
  { CODE_FOR_smass, "__SMASS", FRV_BUILTIN_SMASS, 0, 0 },
  { CODE_FOR_smsss, "__SMSSS", FRV_BUILTIN_SMSSS, 0, 0 },
  { CODE_FOR_smu, "__SMU", FRV_BUILTIN_SMU, 0, 0 }
};

static struct builtin_description bdesc_prefetches[] =
{
  { CODE_FOR_frv_prefetch0, "__data_prefetch0", FRV_BUILTIN_PREFETCH0, 0, 0 },
  { CODE_FOR_frv_prefetch, "__data_prefetch", FRV_BUILTIN_PREFETCH, 0, 0 }
bernds's avatar
bernds committed
8226 8227 8228 8229 8230 8231 8232 8233 8234 8235 8236
};

/* Media intrinsics that take two arguments, the first being an ACC number.  */

static struct builtin_description bdesc_cut[] =
{
  { CODE_FOR_mcut, "__MCUT", FRV_BUILTIN_MCUT, 0, 0 },
  { CODE_FOR_mcutss, "__MCUTSS", FRV_BUILTIN_MCUTSS, 0, 0 },
  { CODE_FOR_mdcutssi, "__MDCUTSSI", FRV_BUILTIN_MDCUTSSI, 0, 0 }
};

8237
/* Two-argument media intrinsics with an immediate second argument.  */
bernds's avatar
bernds committed
8238 8239 8240 8241 8242 8243 8244 8245 8246 8247 8248 8249 8250 8251 8252 8253 8254

static struct builtin_description bdesc_2argimm[] =
{
  { CODE_FOR_mrotli, "__MROTLI", FRV_BUILTIN_MROTLI, 0, 0 },
  { CODE_FOR_mrotri, "__MROTRI", FRV_BUILTIN_MROTRI, 0, 0 },
  { CODE_FOR_msllhi, "__MSLLHI", FRV_BUILTIN_MSLLHI, 0, 0 },
  { CODE_FOR_msrlhi, "__MSRLHI", FRV_BUILTIN_MSRLHI, 0, 0 },
  { CODE_FOR_msrahi, "__MSRAHI", FRV_BUILTIN_MSRAHI, 0, 0 },
  { CODE_FOR_mexpdhw, "__MEXPDHW", FRV_BUILTIN_MEXPDHW, 0, 0 },
  { CODE_FOR_mexpdhd, "__MEXPDHD", FRV_BUILTIN_MEXPDHD, 0, 0 },
  { CODE_FOR_mdrotli, "__MDROTLI", FRV_BUILTIN_MDROTLI, 0, 0 },
  { CODE_FOR_mcplhi, "__MCPLHI", FRV_BUILTIN_MCPLHI, 0, 0 },
  { CODE_FOR_mcpli, "__MCPLI", FRV_BUILTIN_MCPLI, 0, 0 },
  { CODE_FOR_mhsetlos, "__MHSETLOS", FRV_BUILTIN_MHSETLOS, 0, 0 },
  { CODE_FOR_mhsetloh, "__MHSETLOH", FRV_BUILTIN_MHSETLOH, 0, 0 },
  { CODE_FOR_mhsethis, "__MHSETHIS", FRV_BUILTIN_MHSETHIS, 0, 0 },
  { CODE_FOR_mhsethih, "__MHSETHIH", FRV_BUILTIN_MHSETHIH, 0, 0 },
8255 8256 8257
  { CODE_FOR_mhdseth, "__MHDSETH", FRV_BUILTIN_MHDSETH, 0, 0 },
  { CODE_FOR_mqsllhi, "__MQSLLHI", FRV_BUILTIN_MQSLLHI, 0, 0 },
  { CODE_FOR_mqsrahi, "__MQSRAHI", FRV_BUILTIN_MQSRAHI, 0, 0 }
bernds's avatar
bernds committed
8258 8259 8260
};

/* Media intrinsics that take two arguments and return void, the first argument
8261
   being a pointer to 4 words in memory.  */
bernds's avatar
bernds committed
8262 8263 8264 8265 8266 8267 8268 8269

static struct builtin_description bdesc_void2arg[] =
{
  { CODE_FOR_mdunpackh, "__MDUNPACKH", FRV_BUILTIN_MDUNPACKH, 0, 0 },
  { CODE_FOR_mbtohe, "__MBTOHE", FRV_BUILTIN_MBTOHE, 0, 0 },
};

/* Media intrinsics that take three arguments, the first being a const_int that
8270
   denotes an accumulator, and that return void.  */
bernds's avatar
bernds committed
8271 8272 8273 8274 8275 8276 8277 8278 8279 8280 8281 8282 8283 8284 8285 8286 8287 8288 8289 8290 8291 8292 8293 8294 8295 8296 8297 8298 8299 8300 8301 8302 8303 8304 8305 8306 8307 8308 8309 8310 8311 8312 8313

static struct builtin_description bdesc_void3arg[] =
{
  { CODE_FOR_mcpxrs, "__MCPXRS", FRV_BUILTIN_MCPXRS, 0, 0 },
  { CODE_FOR_mcpxru, "__MCPXRU", FRV_BUILTIN_MCPXRU, 0, 0 },
  { CODE_FOR_mcpxis, "__MCPXIS", FRV_BUILTIN_MCPXIS, 0, 0 },
  { CODE_FOR_mcpxiu, "__MCPXIU", FRV_BUILTIN_MCPXIU, 0, 0 },
  { CODE_FOR_mmulhs, "__MMULHS", FRV_BUILTIN_MMULHS, 0, 0 },
  { CODE_FOR_mmulhu, "__MMULHU", FRV_BUILTIN_MMULHU, 0, 0 },
  { CODE_FOR_mmulxhs, "__MMULXHS", FRV_BUILTIN_MMULXHS, 0, 0 },
  { CODE_FOR_mmulxhu, "__MMULXHU", FRV_BUILTIN_MMULXHU, 0, 0 },
  { CODE_FOR_mmachs, "__MMACHS", FRV_BUILTIN_MMACHS, 0, 0 },
  { CODE_FOR_mmachu, "__MMACHU", FRV_BUILTIN_MMACHU, 0, 0 },
  { CODE_FOR_mmrdhs, "__MMRDHS", FRV_BUILTIN_MMRDHS, 0, 0 },
  { CODE_FOR_mmrdhu, "__MMRDHU", FRV_BUILTIN_MMRDHU, 0, 0 },
  { CODE_FOR_mqcpxrs, "__MQCPXRS", FRV_BUILTIN_MQCPXRS, 0, 0 },
  { CODE_FOR_mqcpxru, "__MQCPXRU", FRV_BUILTIN_MQCPXRU, 0, 0 },
  { CODE_FOR_mqcpxis, "__MQCPXIS", FRV_BUILTIN_MQCPXIS, 0, 0 },
  { CODE_FOR_mqcpxiu, "__MQCPXIU", FRV_BUILTIN_MQCPXIU, 0, 0 },
  { CODE_FOR_mqmulhs, "__MQMULHS", FRV_BUILTIN_MQMULHS, 0, 0 },
  { CODE_FOR_mqmulhu, "__MQMULHU", FRV_BUILTIN_MQMULHU, 0, 0 },
  { CODE_FOR_mqmulxhs, "__MQMULXHS", FRV_BUILTIN_MQMULXHS, 0, 0 },
  { CODE_FOR_mqmulxhu, "__MQMULXHU", FRV_BUILTIN_MQMULXHU, 0, 0 },
  { CODE_FOR_mqmachs, "__MQMACHS", FRV_BUILTIN_MQMACHS, 0, 0 },
  { CODE_FOR_mqmachu, "__MQMACHU", FRV_BUILTIN_MQMACHU, 0, 0 },
  { CODE_FOR_mqxmachs, "__MQXMACHS", FRV_BUILTIN_MQXMACHS, 0, 0 },
  { CODE_FOR_mqxmacxhs, "__MQXMACXHS", FRV_BUILTIN_MQXMACXHS, 0, 0 },
  { CODE_FOR_mqmacxhs, "__MQMACXHS", FRV_BUILTIN_MQMACXHS, 0, 0 }
};

/* Media intrinsics that take two accumulator numbers as argument and
   return void.  */

static struct builtin_description bdesc_voidacc[] =
{
  { CODE_FOR_maddaccs, "__MADDACCS", FRV_BUILTIN_MADDACCS, 0, 0 },
  { CODE_FOR_msubaccs, "__MSUBACCS", FRV_BUILTIN_MSUBACCS, 0, 0 },
  { CODE_FOR_masaccs, "__MASACCS", FRV_BUILTIN_MASACCS, 0, 0 },
  { CODE_FOR_mdaddaccs, "__MDADDACCS", FRV_BUILTIN_MDADDACCS, 0, 0 },
  { CODE_FOR_mdsubaccs, "__MDSUBACCS", FRV_BUILTIN_MDSUBACCS, 0, 0 },
  { CODE_FOR_mdasaccs, "__MDASACCS", FRV_BUILTIN_MDASACCS, 0, 0 }
};

8314 8315
/* Intrinsics that load a value and then issue a MEMBAR.  The load is
   a normal move and the ICODE is for the membar.  */
8316 8317 8318

static struct builtin_description bdesc_loads[] =
{
8319 8320 8321 8322 8323 8324 8325 8326
  { CODE_FOR_optional_membar_qi, "__builtin_read8",
    FRV_BUILTIN_READ8, 0, 0 },
  { CODE_FOR_optional_membar_hi, "__builtin_read16",
    FRV_BUILTIN_READ16, 0, 0 },
  { CODE_FOR_optional_membar_si, "__builtin_read32",
    FRV_BUILTIN_READ32, 0, 0 },
  { CODE_FOR_optional_membar_di, "__builtin_read64",
    FRV_BUILTIN_READ64, 0, 0 }
8327 8328 8329 8330 8331 8332
};

/* Likewise stores.  */

static struct builtin_description bdesc_stores[] =
{
8333 8334 8335 8336 8337 8338 8339 8340
  { CODE_FOR_optional_membar_qi, "__builtin_write8",
    FRV_BUILTIN_WRITE8, 0, 0 },
  { CODE_FOR_optional_membar_hi, "__builtin_write16",
    FRV_BUILTIN_WRITE16, 0, 0 },
  { CODE_FOR_optional_membar_si, "__builtin_write32",
    FRV_BUILTIN_WRITE32, 0, 0 },
  { CODE_FOR_optional_membar_di, "__builtin_write64",
    FRV_BUILTIN_WRITE64, 0, 0 },
8341 8342
};

8343
/* Initialize media builtins.  */
bernds's avatar
bernds committed
8344

8345
static void
8346
frv_init_builtins (void)
bernds's avatar
bernds committed
8347 8348 8349 8350 8351 8352 8353 8354 8355 8356 8357
{
  tree endlink = void_list_node;
  tree accumulator = integer_type_node;
  tree integer = integer_type_node;
  tree voidt = void_type_node;
  tree uhalf = short_unsigned_type_node;
  tree sword1 = long_integer_type_node;
  tree uword1 = long_unsigned_type_node;
  tree sword2 = long_long_integer_type_node;
  tree uword2 = long_long_unsigned_type_node;
  tree uword4 = build_pointer_type (uword1);
8358 8359
  tree vptr   = build_pointer_type (build_type_variant (void_type_node, 0, 1));
  tree ubyte  = unsigned_char_type_node;
8360
  tree iacc   = integer_type_node;
bernds's avatar
bernds committed
8361 8362 8363 8364 8365 8366 8367 8368 8369 8370 8371 8372 8373

#define UNARY(RET, T1) \
  build_function_type (RET, tree_cons (NULL_TREE, T1, endlink))

#define BINARY(RET, T1, T2) \
  build_function_type (RET, tree_cons (NULL_TREE, T1, \
			    tree_cons (NULL_TREE, T2, endlink)))

#define TRINARY(RET, T1, T2, T3) \
  build_function_type (RET, tree_cons (NULL_TREE, T1, \
			    tree_cons (NULL_TREE, T2, \
			    tree_cons (NULL_TREE, T3, endlink))))

8374 8375 8376 8377 8378 8379
#define QUAD(RET, T1, T2, T3, T4) \
  build_function_type (RET, tree_cons (NULL_TREE, T1, \
			    tree_cons (NULL_TREE, T2, \
			    tree_cons (NULL_TREE, T3, \
			    tree_cons (NULL_TREE, T4, endlink)))))

bernds's avatar
bernds committed
8380 8381 8382 8383 8384 8385 8386 8387 8388 8389 8390 8391 8392 8393 8394 8395 8396 8397 8398 8399 8400 8401 8402 8403 8404 8405 8406 8407 8408 8409 8410 8411 8412
  tree void_ftype_void = build_function_type (voidt, endlink);

  tree void_ftype_acc = UNARY (voidt, accumulator);
  tree void_ftype_uw4_uw1 = BINARY (voidt, uword4, uword1);
  tree void_ftype_uw4_uw2 = BINARY (voidt, uword4, uword2);
  tree void_ftype_acc_uw1 = BINARY (voidt, accumulator, uword1);
  tree void_ftype_acc_acc = BINARY (voidt, accumulator, accumulator);
  tree void_ftype_acc_uw1_uw1 = TRINARY (voidt, accumulator, uword1, uword1);
  tree void_ftype_acc_sw1_sw1 = TRINARY (voidt, accumulator, sword1, sword1);
  tree void_ftype_acc_uw2_uw2 = TRINARY (voidt, accumulator, uword2, uword2);
  tree void_ftype_acc_sw2_sw2 = TRINARY (voidt, accumulator, sword2, sword2);

  tree uw1_ftype_uw1 = UNARY (uword1, uword1);
  tree uw1_ftype_sw1 = UNARY (uword1, sword1);
  tree uw1_ftype_uw2 = UNARY (uword1, uword2);
  tree uw1_ftype_acc = UNARY (uword1, accumulator);
  tree uw1_ftype_uh_uh = BINARY (uword1, uhalf, uhalf);
  tree uw1_ftype_uw1_uw1 = BINARY (uword1, uword1, uword1);
  tree uw1_ftype_uw1_int = BINARY (uword1, uword1, integer);
  tree uw1_ftype_acc_uw1 = BINARY (uword1, accumulator, uword1);
  tree uw1_ftype_acc_sw1 = BINARY (uword1, accumulator, sword1);
  tree uw1_ftype_uw2_uw1 = BINARY (uword1, uword2, uword1);
  tree uw1_ftype_uw2_int = BINARY (uword1, uword2, integer);

  tree sw1_ftype_int = UNARY (sword1, integer);
  tree sw1_ftype_sw1_sw1 = BINARY (sword1, sword1, sword1);
  tree sw1_ftype_sw1_int = BINARY (sword1, sword1, integer);

  tree uw2_ftype_uw1 = UNARY (uword2, uword1);
  tree uw2_ftype_uw1_int = BINARY (uword2, uword1, integer);
  tree uw2_ftype_uw2_uw2 = BINARY (uword2, uword2, uword2);
  tree uw2_ftype_uw2_int = BINARY (uword2, uword2, integer);
  tree uw2_ftype_acc_int = BINARY (uword2, accumulator, integer);
8413
  tree uw2_ftype_uh_uh_uh_uh = QUAD (uword2, uhalf, uhalf, uhalf, uhalf);
bernds's avatar
bernds committed
8414 8415

  tree sw2_ftype_sw2_sw2 = BINARY (sword2, sword2, sword2);
8416 8417 8418 8419 8420 8421 8422 8423 8424 8425
  tree sw2_ftype_sw2_int   = BINARY (sword2, sword2, integer);
  tree uw2_ftype_uw1_uw1   = BINARY (uword2, uword1, uword1);
  tree sw2_ftype_sw1_sw1   = BINARY (sword2, sword1, sword1);
  tree void_ftype_sw1_sw1  = BINARY (voidt, sword1, sword1);
  tree void_ftype_iacc_sw2 = BINARY (voidt, iacc, sword2);
  tree void_ftype_iacc_sw1 = BINARY (voidt, iacc, sword1);
  tree sw1_ftype_sw1       = UNARY (sword1, sword1);
  tree sw2_ftype_iacc      = UNARY (sword2, iacc);
  tree sw1_ftype_iacc      = UNARY (sword1, iacc);
  tree void_ftype_ptr      = UNARY (voidt, const_ptr_type_node);
8426 8427 8428 8429 8430 8431
  tree uw1_ftype_vptr      = UNARY (uword1, vptr);
  tree uw2_ftype_vptr      = UNARY (uword2, vptr);
  tree void_ftype_vptr_ub  = BINARY (voidt, vptr, ubyte);
  tree void_ftype_vptr_uh  = BINARY (voidt, vptr, uhalf);
  tree void_ftype_vptr_uw1 = BINARY (voidt, vptr, uword1);
  tree void_ftype_vptr_uw2 = BINARY (voidt, vptr, uword2);
bernds's avatar
bernds committed
8432 8433 8434 8435 8436 8437 8438 8439 8440 8441 8442 8443 8444 8445 8446 8447 8448 8449 8450 8451 8452 8453 8454 8455 8456 8457 8458 8459 8460 8461 8462 8463 8464 8465 8466 8467 8468 8469 8470 8471 8472 8473 8474 8475 8476 8477 8478 8479 8480 8481

  def_builtin ("__MAND", uw1_ftype_uw1_uw1, FRV_BUILTIN_MAND);
  def_builtin ("__MOR", uw1_ftype_uw1_uw1, FRV_BUILTIN_MOR);
  def_builtin ("__MXOR", uw1_ftype_uw1_uw1, FRV_BUILTIN_MXOR);
  def_builtin ("__MNOT", uw1_ftype_uw1, FRV_BUILTIN_MNOT);
  def_builtin ("__MROTLI", uw1_ftype_uw1_int, FRV_BUILTIN_MROTLI);
  def_builtin ("__MROTRI", uw1_ftype_uw1_int, FRV_BUILTIN_MROTRI);
  def_builtin ("__MWCUT", uw1_ftype_uw2_uw1, FRV_BUILTIN_MWCUT);
  def_builtin ("__MAVEH", uw1_ftype_uw1_uw1, FRV_BUILTIN_MAVEH);
  def_builtin ("__MSLLHI", uw1_ftype_uw1_int, FRV_BUILTIN_MSLLHI);
  def_builtin ("__MSRLHI", uw1_ftype_uw1_int, FRV_BUILTIN_MSRLHI);
  def_builtin ("__MSRAHI", sw1_ftype_sw1_int, FRV_BUILTIN_MSRAHI);
  def_builtin ("__MSATHS", sw1_ftype_sw1_sw1, FRV_BUILTIN_MSATHS);
  def_builtin ("__MSATHU", uw1_ftype_uw1_uw1, FRV_BUILTIN_MSATHU);
  def_builtin ("__MADDHSS", sw1_ftype_sw1_sw1, FRV_BUILTIN_MADDHSS);
  def_builtin ("__MADDHUS", uw1_ftype_uw1_uw1, FRV_BUILTIN_MADDHUS);
  def_builtin ("__MSUBHSS", sw1_ftype_sw1_sw1, FRV_BUILTIN_MSUBHSS);
  def_builtin ("__MSUBHUS", uw1_ftype_uw1_uw1, FRV_BUILTIN_MSUBHUS);
  def_builtin ("__MMULHS", void_ftype_acc_sw1_sw1, FRV_BUILTIN_MMULHS);
  def_builtin ("__MMULHU", void_ftype_acc_uw1_uw1, FRV_BUILTIN_MMULHU);
  def_builtin ("__MMULXHS", void_ftype_acc_sw1_sw1, FRV_BUILTIN_MMULXHS);
  def_builtin ("__MMULXHU", void_ftype_acc_uw1_uw1, FRV_BUILTIN_MMULXHU);
  def_builtin ("__MMACHS", void_ftype_acc_sw1_sw1, FRV_BUILTIN_MMACHS);
  def_builtin ("__MMACHU", void_ftype_acc_uw1_uw1, FRV_BUILTIN_MMACHU);
  def_builtin ("__MMRDHS", void_ftype_acc_sw1_sw1, FRV_BUILTIN_MMRDHS);
  def_builtin ("__MMRDHU", void_ftype_acc_uw1_uw1, FRV_BUILTIN_MMRDHU);
  def_builtin ("__MQADDHSS", sw2_ftype_sw2_sw2, FRV_BUILTIN_MQADDHSS);
  def_builtin ("__MQADDHUS", uw2_ftype_uw2_uw2, FRV_BUILTIN_MQADDHUS);
  def_builtin ("__MQSUBHSS", sw2_ftype_sw2_sw2, FRV_BUILTIN_MQSUBHSS);
  def_builtin ("__MQSUBHUS", uw2_ftype_uw2_uw2, FRV_BUILTIN_MQSUBHUS);
  def_builtin ("__MQMULHS", void_ftype_acc_sw2_sw2, FRV_BUILTIN_MQMULHS);
  def_builtin ("__MQMULHU", void_ftype_acc_uw2_uw2, FRV_BUILTIN_MQMULHU);
  def_builtin ("__MQMULXHS", void_ftype_acc_sw2_sw2, FRV_BUILTIN_MQMULXHS);
  def_builtin ("__MQMULXHU", void_ftype_acc_uw2_uw2, FRV_BUILTIN_MQMULXHU);
  def_builtin ("__MQMACHS", void_ftype_acc_sw2_sw2, FRV_BUILTIN_MQMACHS);
  def_builtin ("__MQMACHU", void_ftype_acc_uw2_uw2, FRV_BUILTIN_MQMACHU);
  def_builtin ("__MCPXRS", void_ftype_acc_sw1_sw1, FRV_BUILTIN_MCPXRS);
  def_builtin ("__MCPXRU", void_ftype_acc_uw1_uw1, FRV_BUILTIN_MCPXRU);
  def_builtin ("__MCPXIS", void_ftype_acc_sw1_sw1, FRV_BUILTIN_MCPXIS);
  def_builtin ("__MCPXIU", void_ftype_acc_uw1_uw1, FRV_BUILTIN_MCPXIU);
  def_builtin ("__MQCPXRS", void_ftype_acc_sw2_sw2, FRV_BUILTIN_MQCPXRS);
  def_builtin ("__MQCPXRU", void_ftype_acc_uw2_uw2, FRV_BUILTIN_MQCPXRU);
  def_builtin ("__MQCPXIS", void_ftype_acc_sw2_sw2, FRV_BUILTIN_MQCPXIS);
  def_builtin ("__MQCPXIU", void_ftype_acc_uw2_uw2, FRV_BUILTIN_MQCPXIU);
  def_builtin ("__MCUT", uw1_ftype_acc_uw1, FRV_BUILTIN_MCUT);
  def_builtin ("__MCUTSS", uw1_ftype_acc_sw1, FRV_BUILTIN_MCUTSS);
  def_builtin ("__MEXPDHW", uw1_ftype_uw1_int, FRV_BUILTIN_MEXPDHW);
  def_builtin ("__MEXPDHD", uw2_ftype_uw1_int, FRV_BUILTIN_MEXPDHD);
  def_builtin ("__MPACKH", uw1_ftype_uh_uh, FRV_BUILTIN_MPACKH);
  def_builtin ("__MUNPACKH", uw2_ftype_uw1, FRV_BUILTIN_MUNPACKH);
8482
  def_builtin ("__MDPACKH", uw2_ftype_uh_uh_uh_uh, FRV_BUILTIN_MDPACKH);
8483
  def_builtin ("__MDUNPACKH", void_ftype_uw4_uw2, FRV_BUILTIN_MDUNPACKH);
bernds's avatar
bernds committed
8484 8485 8486 8487 8488 8489 8490 8491 8492 8493 8494 8495 8496 8497 8498 8499 8500 8501 8502 8503 8504 8505 8506 8507 8508 8509 8510 8511 8512 8513 8514 8515 8516
  def_builtin ("__MBTOH", uw2_ftype_uw1, FRV_BUILTIN_MBTOH);
  def_builtin ("__MHTOB", uw1_ftype_uw2, FRV_BUILTIN_MHTOB);
  def_builtin ("__MBTOHE", void_ftype_uw4_uw1, FRV_BUILTIN_MBTOHE);
  def_builtin ("__MCLRACC", void_ftype_acc, FRV_BUILTIN_MCLRACC);
  def_builtin ("__MCLRACCA", void_ftype_void, FRV_BUILTIN_MCLRACCA);
  def_builtin ("__MRDACC", uw1_ftype_acc, FRV_BUILTIN_MRDACC);
  def_builtin ("__MRDACCG", uw1_ftype_acc, FRV_BUILTIN_MRDACCG);
  def_builtin ("__MWTACC", void_ftype_acc_uw1, FRV_BUILTIN_MWTACC);
  def_builtin ("__MWTACCG", void_ftype_acc_uw1, FRV_BUILTIN_MWTACCG);
  def_builtin ("__Mcop1", uw1_ftype_uw1_uw1, FRV_BUILTIN_MCOP1);
  def_builtin ("__Mcop2", uw1_ftype_uw1_uw1, FRV_BUILTIN_MCOP2);
  def_builtin ("__MTRAP", void_ftype_void, FRV_BUILTIN_MTRAP);
  def_builtin ("__MQXMACHS", void_ftype_acc_sw2_sw2, FRV_BUILTIN_MQXMACHS);
  def_builtin ("__MQXMACXHS", void_ftype_acc_sw2_sw2, FRV_BUILTIN_MQXMACXHS);
  def_builtin ("__MQMACXHS", void_ftype_acc_sw2_sw2, FRV_BUILTIN_MQMACXHS);
  def_builtin ("__MADDACCS", void_ftype_acc_acc, FRV_BUILTIN_MADDACCS);
  def_builtin ("__MSUBACCS", void_ftype_acc_acc, FRV_BUILTIN_MSUBACCS);
  def_builtin ("__MASACCS", void_ftype_acc_acc, FRV_BUILTIN_MASACCS);
  def_builtin ("__MDADDACCS", void_ftype_acc_acc, FRV_BUILTIN_MDADDACCS);
  def_builtin ("__MDSUBACCS", void_ftype_acc_acc, FRV_BUILTIN_MDSUBACCS);
  def_builtin ("__MDASACCS", void_ftype_acc_acc, FRV_BUILTIN_MDASACCS);
  def_builtin ("__MABSHS", uw1_ftype_sw1, FRV_BUILTIN_MABSHS);
  def_builtin ("__MDROTLI", uw2_ftype_uw2_int, FRV_BUILTIN_MDROTLI);
  def_builtin ("__MCPLHI", uw1_ftype_uw2_int, FRV_BUILTIN_MCPLHI);
  def_builtin ("__MCPLI", uw1_ftype_uw2_int, FRV_BUILTIN_MCPLI);
  def_builtin ("__MDCUTSSI", uw2_ftype_acc_int, FRV_BUILTIN_MDCUTSSI);
  def_builtin ("__MQSATHS", sw2_ftype_sw2_sw2, FRV_BUILTIN_MQSATHS);
  def_builtin ("__MHSETLOS", sw1_ftype_sw1_int, FRV_BUILTIN_MHSETLOS);
  def_builtin ("__MHSETHIS", sw1_ftype_sw1_int, FRV_BUILTIN_MHSETHIS);
  def_builtin ("__MHDSETS", sw1_ftype_int, FRV_BUILTIN_MHDSETS);
  def_builtin ("__MHSETLOH", uw1_ftype_uw1_int, FRV_BUILTIN_MHSETLOH);
  def_builtin ("__MHSETHIH", uw1_ftype_uw1_int, FRV_BUILTIN_MHSETHIH);
  def_builtin ("__MHDSETH", uw1_ftype_uw1_int, FRV_BUILTIN_MHDSETH);
8517 8518 8519 8520 8521 8522 8523 8524 8525 8526 8527 8528 8529 8530 8531 8532 8533 8534 8535 8536
  def_builtin ("__MQLCLRHS", sw2_ftype_sw2_sw2, FRV_BUILTIN_MQLCLRHS);
  def_builtin ("__MQLMTHS", sw2_ftype_sw2_sw2, FRV_BUILTIN_MQLMTHS);
  def_builtin ("__MQSLLHI", uw2_ftype_uw2_int, FRV_BUILTIN_MQSLLHI);
  def_builtin ("__MQSRAHI", sw2_ftype_sw2_int, FRV_BUILTIN_MQSRAHI);
  def_builtin ("__SMUL", sw2_ftype_sw1_sw1, FRV_BUILTIN_SMUL);
  def_builtin ("__UMUL", uw2_ftype_uw1_uw1, FRV_BUILTIN_UMUL);
  def_builtin ("__SMASS", void_ftype_sw1_sw1, FRV_BUILTIN_SMASS);
  def_builtin ("__SMSSS", void_ftype_sw1_sw1, FRV_BUILTIN_SMSSS);
  def_builtin ("__SMU", void_ftype_sw1_sw1, FRV_BUILTIN_SMU);
  def_builtin ("__ADDSS", sw1_ftype_sw1_sw1, FRV_BUILTIN_ADDSS);
  def_builtin ("__SUBSS", sw1_ftype_sw1_sw1, FRV_BUILTIN_SUBSS);
  def_builtin ("__SLASS", sw1_ftype_sw1_sw1, FRV_BUILTIN_SLASS);
  def_builtin ("__SCAN", sw1_ftype_sw1_sw1, FRV_BUILTIN_SCAN);
  def_builtin ("__SCUTSS", sw1_ftype_sw1, FRV_BUILTIN_SCUTSS);
  def_builtin ("__IACCreadll", sw2_ftype_iacc, FRV_BUILTIN_IACCreadll);
  def_builtin ("__IACCreadl", sw1_ftype_iacc, FRV_BUILTIN_IACCreadl);
  def_builtin ("__IACCsetll", void_ftype_iacc_sw2, FRV_BUILTIN_IACCsetll);
  def_builtin ("__IACCsetl", void_ftype_iacc_sw1, FRV_BUILTIN_IACCsetl);
  def_builtin ("__data_prefetch0", void_ftype_ptr, FRV_BUILTIN_PREFETCH0);
  def_builtin ("__data_prefetch", void_ftype_ptr, FRV_BUILTIN_PREFETCH);
8537 8538 8539 8540 8541 8542 8543 8544 8545
  def_builtin ("__builtin_read8", uw1_ftype_vptr, FRV_BUILTIN_READ8);
  def_builtin ("__builtin_read16", uw1_ftype_vptr, FRV_BUILTIN_READ16);
  def_builtin ("__builtin_read32", uw1_ftype_vptr, FRV_BUILTIN_READ32);
  def_builtin ("__builtin_read64", uw2_ftype_vptr, FRV_BUILTIN_READ64);

  def_builtin ("__builtin_write8", void_ftype_vptr_ub, FRV_BUILTIN_WRITE8);
  def_builtin ("__builtin_write16", void_ftype_vptr_uh, FRV_BUILTIN_WRITE16);
  def_builtin ("__builtin_write32", void_ftype_vptr_uw1, FRV_BUILTIN_WRITE32);
  def_builtin ("__builtin_write64", void_ftype_vptr_uw2, FRV_BUILTIN_WRITE64);
bernds's avatar
bernds committed
8546 8547 8548 8549

#undef UNARY
#undef BINARY
#undef TRINARY
8550
#undef QUAD
bernds's avatar
bernds committed
8551 8552
}

zack's avatar
zack committed
8553 8554 8555 8556 8557 8558 8559 8560 8561 8562 8563 8564 8565 8566 8567 8568 8569 8570 8571 8572 8573 8574 8575 8576 8577 8578 8579 8580 8581
/* Set the names for various arithmetic operations according to the
   FRV ABI.  */
static void
frv_init_libfuncs (void)
{
  set_optab_libfunc (smod_optab,     SImode, "__modi");
  set_optab_libfunc (umod_optab,     SImode, "__umodi");

  set_optab_libfunc (add_optab,      DImode, "__addll");
  set_optab_libfunc (sub_optab,      DImode, "__subll");
  set_optab_libfunc (smul_optab,     DImode, "__mulll");
  set_optab_libfunc (sdiv_optab,     DImode, "__divll");
  set_optab_libfunc (smod_optab,     DImode, "__modll");
  set_optab_libfunc (umod_optab,     DImode, "__umodll");
  set_optab_libfunc (and_optab,      DImode, "__andll");
  set_optab_libfunc (ior_optab,      DImode, "__orll");
  set_optab_libfunc (xor_optab,      DImode, "__xorll");
  set_optab_libfunc (one_cmpl_optab, DImode, "__notll");

  set_optab_libfunc (add_optab,      SFmode, "__addf");
  set_optab_libfunc (sub_optab,      SFmode, "__subf");
  set_optab_libfunc (smul_optab,     SFmode, "__mulf");
  set_optab_libfunc (sdiv_optab,     SFmode, "__divf");

  set_optab_libfunc (add_optab,      DFmode, "__addd");
  set_optab_libfunc (sub_optab,      DFmode, "__subd");
  set_optab_libfunc (smul_optab,     DFmode, "__muld");
  set_optab_libfunc (sdiv_optab,     DFmode, "__divd");

8582 8583 8584 8585 8586 8587 8588 8589 8590
  set_conv_libfunc (sext_optab,   DFmode, SFmode, "__ftod");
  set_conv_libfunc (trunc_optab,  SFmode, DFmode, "__dtof");

  set_conv_libfunc (sfix_optab,   SImode, SFmode, "__ftoi");
  set_conv_libfunc (sfix_optab,   DImode, SFmode, "__ftoll");
  set_conv_libfunc (sfix_optab,   SImode, DFmode, "__dtoi");
  set_conv_libfunc (sfix_optab,   DImode, DFmode, "__dtoll");

  set_conv_libfunc (ufix_optab,   SImode, SFmode, "__ftoui");
8591 8592 8593
  set_conv_libfunc (ufix_optab,   DImode, SFmode, "__ftoull");
  set_conv_libfunc (ufix_optab,   SImode, DFmode, "__dtoui");
  set_conv_libfunc (ufix_optab,   DImode, DFmode, "__dtoull");
8594 8595 8596 8597 8598

  set_conv_libfunc (sfloat_optab, SFmode, SImode, "__itof");
  set_conv_libfunc (sfloat_optab, SFmode, DImode, "__lltof");
  set_conv_libfunc (sfloat_optab, DFmode, SImode, "__itod");
  set_conv_libfunc (sfloat_optab, DFmode, DImode, "__lltod");
zack's avatar
zack committed
8599 8600
}

bernds's avatar
bernds committed
8601 8602 8603 8604 8605 8606 8607
/* Convert an integer constant to an accumulator register.  ICODE is the
   code of the target instruction, OPNUM is the number of the
   accumulator operand and OPVAL is the constant integer.  Try both
   ACC and ACCG registers; only report an error if neither fit the
   instruction.  */

static rtx
8608
frv_int_to_acc (enum insn_code icode, int opnum, rtx opval)
bernds's avatar
bernds committed
8609 8610
{
  rtx reg;
8611 8612
  int i;

8613
  /* ACCs and ACCGs are implicit global registers if media intrinsics
8614
     are being used.  We set up this lazily to avoid creating lots of
8615
     unnecessary call_insn rtl in non-media code.  */
8616 8617 8618
  for (i = 0; i <= ACC_MASK; i++)
    if ((i & ACC_MASK) == i)
      global_regs[i + ACC_FIRST] = global_regs[i + ACCG_FIRST] = 1;
bernds's avatar
bernds committed
8619 8620 8621 8622 8623 8624

  if (GET_CODE (opval) != CONST_INT)
    {
      error ("accumulator is not a constant integer");
      return NULL_RTX;
    }
8625
  if ((INTVAL (opval) & ~ACC_MASK) != 0)
bernds's avatar
bernds committed
8626 8627 8628 8629 8630 8631 8632 8633
    {
      error ("accumulator number is out of bounds");
      return NULL_RTX;
    }

  reg = gen_rtx_REG (insn_data[icode].operand[opnum].mode,
		     ACC_FIRST + INTVAL (opval));
  if (! (*insn_data[icode].operand[opnum].predicate) (reg, VOIDmode))
dberlin's avatar
dberlin committed
8634
    SET_REGNO (reg, ACCG_FIRST + INTVAL (opval));
bernds's avatar
bernds committed
8635 8636 8637

  if (! (*insn_data[icode].operand[opnum].predicate) (reg, VOIDmode))
    {
8638
      error ("inappropriate accumulator for %qs", insn_data[icode].name);
bernds's avatar
bernds committed
8639 8640 8641 8642 8643 8644 8645 8646 8647
      return NULL_RTX;
    }
  return reg;
}

/* If an ACC rtx has mode MODE, return the mode that the matching ACCG
   should have.  */

static enum machine_mode
8648
frv_matching_accg_mode (enum machine_mode mode)
bernds's avatar
bernds committed
8649 8650 8651 8652 8653 8654 8655 8656 8657 8658 8659 8660 8661
{
  switch (mode)
    {
    case V4SImode:
      return V4QImode;

    case DImode:
      return HImode;

    case SImode:
      return QImode;

    default:
8662
      gcc_unreachable ();
bernds's avatar
bernds committed
8663 8664 8665
    }
}

8666 8667 8668 8669 8670 8671 8672 8673 8674 8675 8676 8677
/* Given that a __builtin_read or __builtin_write function is accessing
   address ADDRESS, return the value that should be used as operand 1
   of the membar.  */

static rtx
frv_io_address_cookie (rtx address)
{
  return (GET_CODE (address) == CONST_INT
	  ? GEN_INT (INTVAL (address) / 8 * 8)
	  : const0_rtx);
}

bernds's avatar
bernds committed
8678 8679 8680 8681 8682
/* Return the accumulator guard that should be paired with accumulator
   register ACC.  The mode of the returned register is in the same
   class as ACC, but is four times smaller.  */

rtx
8683
frv_matching_accg_for_acc (rtx acc)
bernds's avatar
bernds committed
8684 8685 8686 8687 8688
{
  return gen_rtx_REG (frv_matching_accg_mode (GET_MODE (acc)),
		      REGNO (acc) - ACC_FIRST + ACCG_FIRST);
}

8689 8690
/* Read the requested argument from the call EXP given by INDEX.
   Return the value as an rtx.  */
bernds's avatar
bernds committed
8691 8692

static rtx
8693
frv_read_argument (tree exp, unsigned int index)
bernds's avatar
bernds committed
8694
{
8695 8696
  return expand_expr (CALL_EXPR_ARG (exp, index),
		      NULL_RTX, VOIDmode, 0);
bernds's avatar
bernds committed
8697 8698
}

8699 8700 8701 8702
/* Like frv_read_argument, but interpret the argument as the number
   of an IACC register and return a (reg:MODE ...) rtx for it.  */

static rtx
8703 8704
frv_read_iacc_argument (enum machine_mode mode, tree call,
			unsigned int index)
8705 8706 8707 8708
{
  int i, regno;
  rtx op;

8709
  op = frv_read_argument (call, index);
8710 8711 8712 8713 8714 8715 8716 8717 8718
  if (GET_CODE (op) != CONST_INT
      || INTVAL (op) < 0
      || INTVAL (op) > IACC_LAST - IACC_FIRST
      || ((INTVAL (op) * 4) & (GET_MODE_SIZE (mode) - 1)) != 0)
    {
      error ("invalid IACC argument");
      op = const0_rtx;
    }

8719
  /* IACCs are implicit global registers.  We set up this lazily to
8720
     avoid creating lots of unnecessary call_insn rtl when IACCs aren't
8721 8722 8723 8724 8725 8726 8727 8728
     being used.  */
  regno = INTVAL (op) + IACC_FIRST;
  for (i = 0; i < HARD_REGNO_NREGS (regno, mode); i++)
    global_regs[regno + i] = 1;

  return gen_rtx_REG (mode, regno);
}

bernds's avatar
bernds committed
8729 8730 8731 8732 8733
/* Return true if OPVAL can be used for operand OPNUM of instruction ICODE.
   The instruction should require a constant operand of some sort.  The
   function prints an error if OPVAL is not valid.  */

static int
8734
frv_check_constant_argument (enum insn_code icode, int opnum, rtx opval)
bernds's avatar
bernds committed
8735 8736 8737
{
  if (GET_CODE (opval) != CONST_INT)
    {
8738
      error ("%qs expects a constant argument", insn_data[icode].name);
bernds's avatar
bernds committed
8739 8740 8741 8742
      return FALSE;
    }
  if (! (*insn_data[icode].operand[opnum].predicate) (opval, VOIDmode))
    {
8743
      error ("constant argument out of range for %qs", insn_data[icode].name);
bernds's avatar
bernds committed
8744 8745 8746 8747 8748 8749 8750 8751 8752 8753
      return FALSE;
    }
  return TRUE;
}

/* Return a legitimate rtx for instruction ICODE's return value.  Use TARGET
   if it's not null, has the right mode, and satisfies operand 0's
   predicate.  */

static rtx
8754
frv_legitimize_target (enum insn_code icode, rtx target)
bernds's avatar
bernds committed
8755 8756 8757 8758 8759 8760 8761 8762 8763 8764 8765 8766
{
  enum machine_mode mode = insn_data[icode].operand[0].mode;

  if (! target
      || GET_MODE (target) != mode
      || ! (*insn_data[icode].operand[0].predicate) (target, mode))
    return gen_reg_rtx (mode);
  else
    return target;
}

/* Given that ARG is being passed as operand OPNUM to instruction ICODE,
kazu's avatar
kazu committed
8767
   check whether ARG satisfies the operand's constraints.  If it doesn't,
bernds's avatar
bernds committed
8768 8769 8770 8771
   copy ARG to a temporary register and return that.  Otherwise return ARG
   itself.  */

static rtx
8772
frv_legitimize_argument (enum insn_code icode, int opnum, rtx arg)
bernds's avatar
bernds committed
8773 8774 8775 8776 8777 8778 8779 8780 8781
{
  enum machine_mode mode = insn_data[icode].operand[opnum].mode;

  if ((*insn_data[icode].operand[opnum].predicate) (arg, mode))
    return arg;
  else
    return copy_to_mode_reg (mode, arg);
}

8782 8783 8784 8785 8786 8787 8788 8789 8790 8791 8792 8793
/* Return a volatile memory reference of mode MODE whose address is ARG.  */

static rtx
frv_volatile_memref (enum machine_mode mode, rtx arg)
{
  rtx mem;

  mem = gen_rtx_MEM (mode, memory_address (mode, arg));
  MEM_VOLATILE_P (mem) = 1;
  return mem;
}

bernds's avatar
bernds committed
8794 8795 8796 8797
/* Expand builtins that take a single, constant argument.  At the moment,
   only MHDSETS falls into this category.  */

static rtx
8798
frv_expand_set_builtin (enum insn_code icode, tree call, rtx target)
bernds's avatar
bernds committed
8799 8800
{
  rtx pat;
8801
  rtx op0 = frv_read_argument (call, 0);
bernds's avatar
bernds committed
8802 8803 8804 8805 8806 8807 8808 8809 8810 8811 8812 8813 8814

  if (! frv_check_constant_argument (icode, 1, op0))
    return NULL_RTX;

  target = frv_legitimize_target (icode, target);
  pat = GEN_FCN (icode) (target, op0);
  if (! pat)
    return NULL_RTX;

  emit_insn (pat);
  return target;
}

8815
/* Expand builtins that take one operand.  */
bernds's avatar
bernds committed
8816 8817

static rtx
8818
frv_expand_unop_builtin (enum insn_code icode, tree call, rtx target)
bernds's avatar
bernds committed
8819 8820
{
  rtx pat;
8821
  rtx op0 = frv_read_argument (call, 0);
bernds's avatar
bernds committed
8822 8823 8824 8825 8826 8827 8828 8829 8830 8831 8832

  target = frv_legitimize_target (icode, target);
  op0 = frv_legitimize_argument (icode, 1, op0);
  pat = GEN_FCN (icode) (target, op0);
  if (! pat)
    return NULL_RTX;

  emit_insn (pat);
  return target;
}

8833
/* Expand builtins that take two operands.  */
bernds's avatar
bernds committed
8834 8835

static rtx
8836
frv_expand_binop_builtin (enum insn_code icode, tree call, rtx target)
bernds's avatar
bernds committed
8837 8838
{
  rtx pat;
8839 8840
  rtx op0 = frv_read_argument (call, 0);
  rtx op1 = frv_read_argument (call, 1);
bernds's avatar
bernds committed
8841 8842 8843 8844 8845 8846 8847 8848 8849 8850 8851 8852 8853

  target = frv_legitimize_target (icode, target);
  op0 = frv_legitimize_argument (icode, 1, op0);
  op1 = frv_legitimize_argument (icode, 2, op1);
  pat = GEN_FCN (icode) (target, op0, op1);
  if (! pat)
    return NULL_RTX;

  emit_insn (pat);
  return target;
}

/* Expand cut-style builtins, which take two operands and an implicit ACCG
8854
   one.  */
bernds's avatar
bernds committed
8855 8856

static rtx
8857
frv_expand_cut_builtin (enum insn_code icode, tree call, rtx target)
bernds's avatar
bernds committed
8858 8859
{
  rtx pat;
8860 8861
  rtx op0 = frv_read_argument (call, 0);
  rtx op1 = frv_read_argument (call, 1);
bernds's avatar
bernds committed
8862 8863 8864 8865 8866 8867 8868 8869 8870 8871 8872 8873 8874 8875 8876 8877 8878 8879 8880 8881 8882 8883 8884 8885
  rtx op2;

  target = frv_legitimize_target (icode, target);
  op0 = frv_int_to_acc (icode, 1, op0);
  if (! op0)
    return NULL_RTX;

  if (icode == CODE_FOR_mdcutssi || GET_CODE (op1) == CONST_INT)
    {
      if (! frv_check_constant_argument (icode, 2, op1))
    	return NULL_RTX;
    }
  else
    op1 = frv_legitimize_argument (icode, 2, op1);

  op2 = frv_matching_accg_for_acc (op0);
  pat = GEN_FCN (icode) (target, op0, op1, op2);
  if (! pat)
    return NULL_RTX;

  emit_insn (pat);
  return target;
}

8886
/* Expand builtins that take two operands and the second is immediate.  */
bernds's avatar
bernds committed
8887 8888

static rtx
8889
frv_expand_binopimm_builtin (enum insn_code icode, tree call, rtx target)
bernds's avatar
bernds committed
8890 8891
{
  rtx pat;
8892 8893
  rtx op0 = frv_read_argument (call, 0);
  rtx op1 = frv_read_argument (call, 1);
bernds's avatar
bernds committed
8894 8895 8896 8897 8898 8899 8900 8901 8902 8903 8904 8905 8906 8907 8908

  if (! frv_check_constant_argument (icode, 2, op1))
    return NULL_RTX;

  target = frv_legitimize_target (icode, target);
  op0 = frv_legitimize_argument (icode, 1, op0);
  pat = GEN_FCN (icode) (target, op0, op1);
  if (! pat)
    return NULL_RTX;

  emit_insn (pat);
  return target;
}

/* Expand builtins that take two operands, the first operand being a pointer to
8909
   ints and return void.  */
bernds's avatar
bernds committed
8910 8911

static rtx
8912
frv_expand_voidbinop_builtin (enum insn_code icode, tree call)
bernds's avatar
bernds committed
8913 8914
{
  rtx pat;
8915 8916
  rtx op0 = frv_read_argument (call, 0);
  rtx op1 = frv_read_argument (call, 1);
bernds's avatar
bernds committed
8917 8918 8919 8920 8921 8922 8923 8924 8925 8926 8927 8928 8929 8930 8931 8932 8933 8934 8935 8936 8937 8938 8939 8940 8941 8942 8943 8944 8945 8946
  enum machine_mode mode0 = insn_data[icode].operand[0].mode;
  rtx addr;

  if (GET_CODE (op0) != MEM)
    {
      rtx reg = op0;

      if (! offsettable_address_p (0, mode0, op0))
	{
	  reg = gen_reg_rtx (Pmode);
	  emit_insn (gen_rtx_SET (VOIDmode, reg, op0));
	}

      op0 = gen_rtx_MEM (SImode, reg);
    }

  addr = XEXP (op0, 0);
  if (! offsettable_address_p (0, mode0, addr))
    addr = copy_to_mode_reg (Pmode, op0);

  op0 = change_address (op0, V4SImode, addr);
  op1 = frv_legitimize_argument (icode, 1, op1);
  pat = GEN_FCN (icode) (op0, op1);
  if (! pat)
    return 0;

  emit_insn (pat);
  return 0;
}

8947 8948 8949
/* Expand builtins that take two long operands and return void.  */

static rtx
8950
frv_expand_int_void2arg (enum insn_code icode, tree call)
8951 8952
{
  rtx pat;
8953 8954
  rtx op0 = frv_read_argument (call, 0);
  rtx op1 = frv_read_argument (call, 1);
8955 8956 8957 8958 8959 8960 8961 8962 8963 8964 8965 8966 8967 8968

  op0 = frv_legitimize_argument (icode, 1, op0);
  op1 = frv_legitimize_argument (icode, 1, op1);
  pat = GEN_FCN (icode) (op0, op1);
  if (! pat)
    return NULL_RTX;

  emit_insn (pat);
  return NULL_RTX;
}

/* Expand prefetch builtins.  These take a single address as argument.  */

static rtx
8969
frv_expand_prefetches (enum insn_code icode, tree call)
8970 8971
{
  rtx pat;
8972
  rtx op0 = frv_read_argument (call, 0);
8973 8974 8975 8976 8977 8978 8979 8980 8981

  pat = GEN_FCN (icode) (force_reg (Pmode, op0));
  if (! pat)
    return 0;

  emit_insn (pat);
  return 0;
}

bernds's avatar
bernds committed
8982 8983 8984 8985 8986 8987
/* Expand builtins that take three operands and return void.  The first
   argument must be a constant that describes a pair or quad accumulators.  A
   fourth argument is created that is the accumulator guard register that
   corresponds to the accumulator.  */

static rtx
8988
frv_expand_voidtriop_builtin (enum insn_code icode, tree call)
bernds's avatar
bernds committed
8989 8990
{
  rtx pat;
8991 8992 8993
  rtx op0 = frv_read_argument (call, 0);
  rtx op1 = frv_read_argument (call, 1);
  rtx op2 = frv_read_argument (call, 2);
bernds's avatar
bernds committed
8994 8995 8996 8997 8998 8999 9000 9001 9002 9003 9004 9005 9006 9007 9008 9009 9010 9011 9012 9013 9014 9015
  rtx op3;

  op0 = frv_int_to_acc (icode, 0, op0);
  if (! op0)
    return NULL_RTX;

  op1 = frv_legitimize_argument (icode, 1, op1);
  op2 = frv_legitimize_argument (icode, 2, op2);
  op3 = frv_matching_accg_for_acc (op0);
  pat = GEN_FCN (icode) (op0, op1, op2, op3);
  if (! pat)
    return NULL_RTX;

  emit_insn (pat);
  return NULL_RTX;
}

/* Expand builtins that perform accumulator-to-accumulator operations.
   These builtins take two accumulator numbers as argument and return
   void.  */

static rtx
9016
frv_expand_voidaccop_builtin (enum insn_code icode, tree call)
bernds's avatar
bernds committed
9017 9018
{
  rtx pat;
9019 9020
  rtx op0 = frv_read_argument (call, 0);
  rtx op1 = frv_read_argument (call, 1);
bernds's avatar
bernds committed
9021 9022 9023 9024 9025 9026 9027 9028 9029 9030 9031 9032 9033 9034 9035 9036 9037 9038 9039 9040 9041
  rtx op2;
  rtx op3;

  op0 = frv_int_to_acc (icode, 0, op0);
  if (! op0)
    return NULL_RTX;

  op1 = frv_int_to_acc (icode, 1, op1);
  if (! op1)
    return NULL_RTX;

  op2 = frv_matching_accg_for_acc (op0);
  op3 = frv_matching_accg_for_acc (op1);
  pat = GEN_FCN (icode) (op0, op1, op2, op3);
  if (! pat)
    return NULL_RTX;

  emit_insn (pat);
  return NULL_RTX;
}

9042 9043
/* Expand a __builtin_read* function.  ICODE is the instruction code for the
   membar and TARGET_MODE is the mode that the loaded value should have.  */
9044 9045

static rtx
9046
frv_expand_load_builtin (enum insn_code icode, enum machine_mode target_mode,
9047
                         tree call, rtx target)
9048
{
9049
  rtx op0 = frv_read_argument (call, 0);
9050 9051 9052 9053 9054 9055 9056 9057
  rtx cookie = frv_io_address_cookie (op0);

  if (target == 0 || !REG_P (target))
    target = gen_reg_rtx (target_mode);
  op0 = frv_volatile_memref (insn_data[icode].operand[0].mode, op0);
  convert_move (target, op0, 1);
  emit_insn (GEN_FCN (icode) (copy_rtx (op0), cookie, GEN_INT (FRV_IO_READ)));
  cfun->machine->has_membar_p = 1;
9058 9059 9060
  return target;
}

9061
/* Likewise __builtin_write* functions.  */
9062 9063

static rtx
9064
frv_expand_store_builtin (enum insn_code icode, tree call)
9065
{
9066 9067
  rtx op0 = frv_read_argument (call, 0);
  rtx op1 = frv_read_argument (call, 1);
9068
  rtx cookie = frv_io_address_cookie (op0);
9069

9070 9071 9072 9073
  op0 = frv_volatile_memref (insn_data[icode].operand[0].mode, op0);
  convert_move (op0, force_reg (insn_data[icode].operand[0].mode, op1), 1);
  emit_insn (GEN_FCN (icode) (copy_rtx (op0), cookie, GEN_INT (FRV_IO_WRITE)));
  cfun->machine->has_membar_p = 1;
9074 9075 9076
  return NULL_RTX;
}

9077 9078
/* Expand the MDPACKH builtin.  It takes four unsigned short arguments and
   each argument forms one word of the two double-word input registers.
9079 9080
   CALL is the tree for the call and TARGET, if nonnull, suggests a good place
   to put the return value.  */
9081 9082

static rtx
9083
frv_expand_mdpackh_builtin (tree call, rtx target)
9084 9085 9086
{
  enum insn_code icode = CODE_FOR_mdpackh;
  rtx pat, op0, op1;
9087 9088 9089 9090
  rtx arg1 = frv_read_argument (call, 0);
  rtx arg2 = frv_read_argument (call, 1);
  rtx arg3 = frv_read_argument (call, 2);
  rtx arg4 = frv_read_argument (call, 3);
9091 9092 9093 9094 9095

  target = frv_legitimize_target (icode, target);
  op0 = gen_reg_rtx (DImode);
  op1 = gen_reg_rtx (DImode);

9096
  /* The high half of each word is not explicitly initialized, so indicate
9097 9098 9099 9100 9101 9102 9103 9104 9105 9106 9107 9108 9109 9110 9111 9112 9113 9114
     that the input operands are not live before this point.  */
  emit_insn (gen_rtx_CLOBBER (DImode, op0));
  emit_insn (gen_rtx_CLOBBER (DImode, op1));

  /* Move each argument into the low half of its associated input word.  */
  emit_move_insn (simplify_gen_subreg (HImode, op0, DImode, 2), arg1);
  emit_move_insn (simplify_gen_subreg (HImode, op0, DImode, 6), arg2);
  emit_move_insn (simplify_gen_subreg (HImode, op1, DImode, 2), arg3);
  emit_move_insn (simplify_gen_subreg (HImode, op1, DImode, 6), arg4);

  pat = GEN_FCN (icode) (target, op0, op1);
  if (! pat)
    return NULL_RTX;

  emit_insn (pat);
  return target;
}

bernds's avatar
bernds committed
9115 9116 9117 9118
/* Expand the MCLRACC builtin.  This builtin takes a single accumulator
   number as argument.  */

static rtx
9119
frv_expand_mclracc_builtin (tree call)
bernds's avatar
bernds committed
9120 9121 9122
{
  enum insn_code icode = CODE_FOR_mclracc;
  rtx pat;
9123
  rtx op0 = frv_read_argument (call, 0);
bernds's avatar
bernds committed
9124 9125 9126 9127 9128 9129 9130 9131 9132 9133 9134 9135 9136 9137 9138

  op0 = frv_int_to_acc (icode, 0, op0);
  if (! op0)
    return NULL_RTX;

  pat = GEN_FCN (icode) (op0);
  if (pat)
    emit_insn (pat);

  return NULL_RTX;
}

/* Expand builtins that take no arguments.  */

static rtx
9139
frv_expand_noargs_builtin (enum insn_code icode)
bernds's avatar
bernds committed
9140
{
9141
  rtx pat = GEN_FCN (icode) (const0_rtx);
bernds's avatar
bernds committed
9142 9143 9144 9145 9146 9147 9148 9149 9150 9151
  if (pat)
    emit_insn (pat);

  return NULL_RTX;
}

/* Expand MRDACC and MRDACCG.  These builtins take a single accumulator
   number or accumulator guard number as argument and return an SI integer.  */

static rtx
9152
frv_expand_mrdacc_builtin (enum insn_code icode, tree call)
bernds's avatar
bernds committed
9153 9154 9155
{
  rtx pat;
  rtx target = gen_reg_rtx (SImode);
9156
  rtx op0 = frv_read_argument (call, 0);
bernds's avatar
bernds committed
9157 9158 9159 9160 9161 9162 9163 9164 9165 9166 9167 9168 9169 9170 9171 9172 9173 9174

  op0 = frv_int_to_acc (icode, 1, op0);
  if (! op0)
    return NULL_RTX;

  pat = GEN_FCN (icode) (target, op0);
  if (! pat)
    return NULL_RTX;

  emit_insn (pat);
  return target;
}

/* Expand MWTACC and MWTACCG.  These builtins take an accumulator or
   accumulator guard as their first argument and an SImode value as their
   second.  */

static rtx
9175
frv_expand_mwtacc_builtin (enum insn_code icode, tree call)
bernds's avatar
bernds committed
9176 9177
{
  rtx pat;
9178 9179
  rtx op0 = frv_read_argument (call, 0);
  rtx op1 = frv_read_argument (call, 1);
bernds's avatar
bernds committed
9180 9181 9182 9183 9184 9185 9186 9187 9188 9189 9190 9191 9192

  op0 = frv_int_to_acc (icode, 0, op0);
  if (! op0)
    return NULL_RTX;

  op1 = frv_legitimize_argument (icode, 1, op1);
  pat = GEN_FCN (icode) (op0, op1);
  if (pat)
    emit_insn (pat);

  return NULL_RTX;
}

9193 9194 9195 9196 9197 9198 9199 9200 9201 9202 9203 9204 9205 9206 9207
/* Emit a move from SRC to DEST in SImode chunks.  This can be used
   to move DImode values into and out of IACC0.  */

static void
frv_split_iacc_move (rtx dest, rtx src)
{
  enum machine_mode inner;
  int i;

  inner = GET_MODE (dest);
  for (i = 0; i < GET_MODE_SIZE (inner); i += GET_MODE_SIZE (SImode))
    emit_move_insn (simplify_gen_subreg (SImode, dest, inner, i),
		    simplify_gen_subreg (SImode, src, inner, i));
}

9208
/* Expand builtins.  */
bernds's avatar
bernds committed
9209

9210
static rtx
9211 9212 9213 9214 9215
frv_expand_builtin (tree exp,
                    rtx target,
                    rtx subtarget ATTRIBUTE_UNUSED,
                    enum machine_mode mode ATTRIBUTE_UNUSED,
                    int ignore ATTRIBUTE_UNUSED)
bernds's avatar
bernds committed
9216
{
9217
  tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
bernds's avatar
bernds committed
9218 9219 9220 9221
  unsigned fcode = (unsigned)DECL_FUNCTION_CODE (fndecl);
  unsigned i;
  struct builtin_description *d;

9222
  if (fcode < FRV_BUILTIN_FIRST_NONMEDIA && !TARGET_MEDIA)
bernds's avatar
bernds committed
9223 9224 9225 9226 9227 9228 9229 9230 9231 9232 9233 9234 9235 9236 9237 9238 9239 9240 9241 9242 9243 9244 9245 9246 9247 9248 9249 9250 9251 9252 9253 9254 9255 9256 9257 9258 9259 9260 9261 9262 9263
    {
      error ("media functions are not available unless -mmedia is used");
      return NULL_RTX;
    }

  switch (fcode)
    {
    case FRV_BUILTIN_MCOP1:
    case FRV_BUILTIN_MCOP2:
    case FRV_BUILTIN_MDUNPACKH:
    case FRV_BUILTIN_MBTOHE:
      if (! TARGET_MEDIA_REV1)
	{
	  error ("this media function is only available on the fr500");
	  return NULL_RTX;
	}
      break;

    case FRV_BUILTIN_MQXMACHS:
    case FRV_BUILTIN_MQXMACXHS:
    case FRV_BUILTIN_MQMACXHS:
    case FRV_BUILTIN_MADDACCS:
    case FRV_BUILTIN_MSUBACCS:
    case FRV_BUILTIN_MASACCS:
    case FRV_BUILTIN_MDADDACCS:
    case FRV_BUILTIN_MDSUBACCS:
    case FRV_BUILTIN_MDASACCS:
    case FRV_BUILTIN_MABSHS:
    case FRV_BUILTIN_MDROTLI:
    case FRV_BUILTIN_MCPLHI:
    case FRV_BUILTIN_MCPLI:
    case FRV_BUILTIN_MDCUTSSI:
    case FRV_BUILTIN_MQSATHS:
    case FRV_BUILTIN_MHSETLOS:
    case FRV_BUILTIN_MHSETLOH:
    case FRV_BUILTIN_MHSETHIS:
    case FRV_BUILTIN_MHSETHIH:
    case FRV_BUILTIN_MHDSETS:
    case FRV_BUILTIN_MHDSETH:
      if (! TARGET_MEDIA_REV2)
	{
9264 9265 9266 9267 9268 9269 9270 9271 9272 9273 9274 9275 9276 9277 9278 9279 9280 9281 9282 9283 9284 9285 9286 9287 9288 9289 9290 9291 9292 9293 9294 9295 9296 9297 9298 9299 9300 9301 9302 9303 9304
	  error ("this media function is only available on the fr400"
		 " and fr550");
	  return NULL_RTX;
	}
      break;

    case FRV_BUILTIN_SMASS:
    case FRV_BUILTIN_SMSSS:
    case FRV_BUILTIN_SMU:
    case FRV_BUILTIN_ADDSS:
    case FRV_BUILTIN_SUBSS:
    case FRV_BUILTIN_SLASS:
    case FRV_BUILTIN_SCUTSS:
    case FRV_BUILTIN_IACCreadll:
    case FRV_BUILTIN_IACCreadl:
    case FRV_BUILTIN_IACCsetll:
    case FRV_BUILTIN_IACCsetl:
      if (!TARGET_FR405_BUILTINS)
	{
	  error ("this builtin function is only available"
		 " on the fr405 and fr450");
	  return NULL_RTX;
	}
      break;

    case FRV_BUILTIN_PREFETCH:
      if (!TARGET_FR500_FR550_BUILTINS)
	{
	  error ("this builtin function is only available on the fr500"
		 " and fr550");
	  return NULL_RTX;
	}
      break;

    case FRV_BUILTIN_MQLCLRHS:
    case FRV_BUILTIN_MQLMTHS:
    case FRV_BUILTIN_MQSLLHI:
    case FRV_BUILTIN_MQSRAHI:
      if (!TARGET_MEDIA_FR450)
	{
	  error ("this builtin function is only available on the fr450");
bernds's avatar
bernds committed
9305 9306 9307 9308 9309 9310 9311 9312
	  return NULL_RTX;
	}
      break;

    default:
      break;
    }

9313
  /* Expand unique builtins.  */
bernds's avatar
bernds committed
9314 9315 9316 9317 9318 9319 9320

  switch (fcode)
    {
    case FRV_BUILTIN_MTRAP:
      return frv_expand_noargs_builtin (CODE_FOR_mtrap);

    case FRV_BUILTIN_MCLRACC:
9321
      return frv_expand_mclracc_builtin (exp);
bernds's avatar
bernds committed
9322 9323 9324 9325 9326 9327 9328 9329

    case FRV_BUILTIN_MCLRACCA:
      if (TARGET_ACC_8)
	return frv_expand_noargs_builtin (CODE_FOR_mclracca8);
      else
	return frv_expand_noargs_builtin (CODE_FOR_mclracca4);

    case FRV_BUILTIN_MRDACC:
9330
      return frv_expand_mrdacc_builtin (CODE_FOR_mrdacc, exp);
bernds's avatar
bernds committed
9331 9332

    case FRV_BUILTIN_MRDACCG:
9333
      return frv_expand_mrdacc_builtin (CODE_FOR_mrdaccg, exp);
bernds's avatar
bernds committed
9334 9335

    case FRV_BUILTIN_MWTACC:
9336
      return frv_expand_mwtacc_builtin (CODE_FOR_mwtacc, exp);
bernds's avatar
bernds committed
9337 9338

    case FRV_BUILTIN_MWTACCG:
9339
      return frv_expand_mwtacc_builtin (CODE_FOR_mwtaccg, exp);
bernds's avatar
bernds committed
9340

9341
    case FRV_BUILTIN_MDPACKH:
9342
      return frv_expand_mdpackh_builtin (exp, target);
9343

9344 9345
    case FRV_BUILTIN_IACCreadll:
      {
9346
	rtx src = frv_read_iacc_argument (DImode, exp, 0);
9347 9348 9349 9350 9351 9352 9353
	if (target == 0 || !REG_P (target))
	  target = gen_reg_rtx (DImode);
	frv_split_iacc_move (target, src);
	return target;
      }

    case FRV_BUILTIN_IACCreadl:
9354
      return frv_read_iacc_argument (SImode, exp, 0);
9355 9356 9357

    case FRV_BUILTIN_IACCsetll:
      {
9358 9359
	rtx dest = frv_read_iacc_argument (DImode, exp, 0);
	rtx src = frv_read_argument (exp, 1);
9360 9361 9362 9363 9364 9365
	frv_split_iacc_move (dest, force_reg (DImode, src));
	return 0;
      }

    case FRV_BUILTIN_IACCsetl:
      {
9366 9367
	rtx dest = frv_read_iacc_argument (SImode, exp, 0);
	rtx src = frv_read_argument (exp, 1);
9368 9369 9370 9371
	emit_move_insn (dest, force_reg (SImode, src));
	return 0;
      }

bernds's avatar
bernds committed
9372 9373 9374 9375
    default:
      break;
    }

9376
  /* Expand groups of builtins.  */
bernds's avatar
bernds committed
9377

9378
  for (i = 0, d = bdesc_set; i < ARRAY_SIZE (bdesc_set); i++, d++)
bernds's avatar
bernds committed
9379
    if (d->code == fcode)
9380
      return frv_expand_set_builtin (d->icode, exp, target);
bernds's avatar
bernds committed
9381

9382
  for (i = 0, d = bdesc_1arg; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
bernds's avatar
bernds committed
9383
    if (d->code == fcode)
9384
      return frv_expand_unop_builtin (d->icode, exp, target);
bernds's avatar
bernds committed
9385

9386
  for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
bernds's avatar
bernds committed
9387
    if (d->code == fcode)
9388
      return frv_expand_binop_builtin (d->icode, exp, target);
bernds's avatar
bernds committed
9389

9390
  for (i = 0, d = bdesc_cut; i < ARRAY_SIZE (bdesc_cut); i++, d++)
bernds's avatar
bernds committed
9391
    if (d->code == fcode)
9392
      return frv_expand_cut_builtin (d->icode, exp, target);
bernds's avatar
bernds committed
9393

9394 9395
  for (i = 0, d = bdesc_2argimm; i < ARRAY_SIZE (bdesc_2argimm); i++, d++)
    if (d->code == fcode)
9396
      return frv_expand_binopimm_builtin (d->icode, exp, target);
bernds's avatar
bernds committed
9397

9398 9399
  for (i = 0, d = bdesc_void2arg; i < ARRAY_SIZE (bdesc_void2arg); i++, d++)
    if (d->code == fcode)
9400
      return frv_expand_voidbinop_builtin (d->icode, exp);
bernds's avatar
bernds committed
9401

9402 9403
  for (i = 0, d = bdesc_void3arg; i < ARRAY_SIZE (bdesc_void3arg); i++, d++)
    if (d->code == fcode)
9404
      return frv_expand_voidtriop_builtin (d->icode, exp);
9405 9406 9407

  for (i = 0, d = bdesc_voidacc; i < ARRAY_SIZE (bdesc_voidacc); i++, d++)
    if (d->code == fcode)
9408
      return frv_expand_voidaccop_builtin (d->icode, exp);
bernds's avatar
bernds committed
9409

9410 9411 9412
  for (i = 0, d = bdesc_int_void2arg;
       i < ARRAY_SIZE (bdesc_int_void2arg); i++, d++)
    if (d->code == fcode)
9413
      return frv_expand_int_void2arg (d->icode, exp);
9414 9415 9416 9417

  for (i = 0, d = bdesc_prefetches;
       i < ARRAY_SIZE (bdesc_prefetches); i++, d++)
    if (d->code == fcode)
9418
      return frv_expand_prefetches (d->icode, exp);
9419

9420 9421
  for (i = 0, d = bdesc_loads; i < ARRAY_SIZE (bdesc_loads); i++, d++)
    if (d->code == fcode)
9422
      return frv_expand_load_builtin (d->icode, TYPE_MODE (TREE_TYPE (exp)),
9423
				      exp, target);
9424 9425 9426

  for (i = 0, d = bdesc_stores; i < ARRAY_SIZE (bdesc_stores); i++, d++)
    if (d->code == fcode)
9427
      return frv_expand_store_builtin (d->icode, exp);
9428

bernds's avatar
bernds committed
9429 9430
  return 0;
}
9431

9432
static bool
9433
frv_in_small_data_p (tree decl)
9434
{
9435 9436 9437 9438 9439 9440 9441 9442 9443 9444 9445 9446 9447 9448 9449
  HOST_WIDE_INT size;
  tree section_name;

  /* Don't apply the -G flag to internal compiler structures.  We
     should leave such structures in the main data section, partly
     for efficiency and partly because the size of some of them
     (such as C++ typeinfos) is not known until later.  */
  if (TREE_CODE (decl) != VAR_DECL || DECL_ARTIFICIAL (decl))
    return false;

  /* If we already know which section the decl should be in, see if
     it's a small data section.  */
  section_name = DECL_SECTION_NAME (decl);
  if (section_name)
    {
9450
      gcc_assert (TREE_CODE (section_name) == STRING_CST);
9451 9452 9453 9454
      if (frv_string_begins_with (section_name, ".sdata"))
	return true;
      if (frv_string_begins_with (section_name, ".sbss"))
	return true;
9455
      return false;
9456
    }
9457

9458 9459 9460 9461
  size = int_size_in_bytes (TREE_TYPE (decl));
  if (size > 0 && (unsigned HOST_WIDE_INT) size <= g_switch_value)
    return true;

9462
  return false;
9463
}
9464 9465

static bool
9466 9467 9468 9469
frv_rtx_costs (rtx x,
               int code ATTRIBUTE_UNUSED,
               int outer_code ATTRIBUTE_UNUSED,
               int *total)
9470
{
9471 9472 9473 9474 9475 9476 9477 9478
  if (outer_code == MEM)
    {
      /* Don't differentiate between memory addresses.  All the ones
	 we accept have equal cost.  */
      *total = COSTS_N_INSNS (0);
      return true;
    }

9479 9480 9481
  switch (code)
    {
    case CONST_INT:
9482
      /* Make 12-bit integers really cheap.  */
9483 9484 9485 9486 9487
      if (IN_RANGE_P (INTVAL (x), -2048, 2047))
	{
	  *total = 0;
	  return true;
	}
9488
      /* Fall through.  */
9489 9490 9491 9492 9493 9494 9495 9496 9497 9498 9499 9500 9501 9502 9503 9504 9505 9506 9507 9508 9509 9510 9511 9512 9513 9514 9515 9516 9517 9518 9519 9520 9521 9522 9523 9524 9525 9526 9527 9528 9529

    case CONST:
    case LABEL_REF:
    case SYMBOL_REF:
    case CONST_DOUBLE:
      *total = COSTS_N_INSNS (2);
      return true;

    case PLUS:
    case MINUS:
    case AND:
    case IOR:
    case XOR:
    case ASHIFT:
    case ASHIFTRT:
    case LSHIFTRT:
    case NOT:
    case NEG:
    case COMPARE:
      if (GET_MODE (x) == SImode)
	*total = COSTS_N_INSNS (1);
      else if (GET_MODE (x) == DImode)
        *total = COSTS_N_INSNS (2);
      else
        *total = COSTS_N_INSNS (3);
      return true;

    case MULT:
      if (GET_MODE (x) == SImode)
        *total = COSTS_N_INSNS (2);
      else
        *total = COSTS_N_INSNS (6);	/* guess */
      return true;

    case DIV:
    case UDIV:
    case MOD:
    case UMOD:
      *total = COSTS_N_INSNS (18);
      return true;

9530 9531 9532 9533
    case MEM:
      *total = COSTS_N_INSNS (3);
      return true;

9534 9535 9536 9537
    default:
      return false;
    }
}
9538 9539

static void
9540
frv_asm_out_constructor (rtx symbol, int priority ATTRIBUTE_UNUSED)
9541
{
9542
  switch_to_section (ctors_section);
9543
  assemble_align (POINTER_SIZE);
9544 9545
  if (TARGET_FDPIC)
    {
9546 9547 9548
      int ok = frv_assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, 1);

      gcc_assert (ok);
9549 9550
      return;
    }
9551 9552 9553 9554
  assemble_integer_with_op ("\t.picptr\t", symbol);
}

static void
9555
frv_asm_out_destructor (rtx symbol, int priority ATTRIBUTE_UNUSED)
9556
{
9557
  switch_to_section (dtors_section);
9558
  assemble_align (POINTER_SIZE);
9559 9560
  if (TARGET_FDPIC)
    {
9561
      int ok = frv_assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, 1);
9562

9563
      gcc_assert (ok);
9564 9565
      return;
    }
9566 9567
  assemble_integer_with_op ("\t.picptr\t", symbol);
}
9568 9569 9570 9571 9572 9573 9574 9575 9576

/* Worker function for TARGET_STRUCT_VALUE_RTX.  */

static rtx
frv_struct_value_rtx (tree fntype ATTRIBUTE_UNUSED,
		      int incoming ATTRIBUTE_UNUSED)
{
  return gen_rtx_REG (Pmode, FRV_STRUCT_VALUE_REGNUM);
}
9577

9578 9579
#define TLS_BIAS (2048 - 16)

ebotcazou's avatar
ebotcazou committed
9580
/* This is called from dwarf2out.c via TARGET_ASM_OUTPUT_DWARF_DTPREL.
9581 9582
   We need to emit DTP-relative relocations.  */

ebotcazou's avatar
ebotcazou committed
9583
static void
9584 9585
frv_output_dwarf_dtprel (FILE *file, int size, rtx x)
{
9586
  gcc_assert (size == 4);
9587 9588 9589 9590 9591 9592 9593
  fputs ("\t.picptr\ttlsmoff(", file);
  /* We want the unbiased TLS offset, so add the bias to the
     expression, such that the implicit biasing cancels out.  */
  output_addr_const (file, plus_constant (x, TLS_BIAS));
  fputs (")", file);
}

9594
#include "gt-frv.h"