OSDN Git Service

2010-05-05 Andrew Pinski <andrew.pinski@caviumnetworks.com>
[pf3gnuchains/gcc-fork.git] / gcc / config / mips / mips.c
1 /* Subroutines used for MIPS code generation.
2    Copyright (C) 1989, 1990, 1991, 1993, 1994, 1995, 1996, 1997, 1998,
3    1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4    Free Software Foundation, Inc.
5    Contributed by A. Lichnewsky, lich@inria.inria.fr.
6    Changes by Michael Meissner, meissner@osf.org.
7    64-bit r4000 support by Ian Lance Taylor, ian@cygnus.com, and
8    Brendan Eich, brendan@microunity.com.
9
10 This file is part of GCC.
11
12 GCC is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 3, or (at your option)
15 any later version.
16
17 GCC is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with GCC; see the file COPYING3.  If not see
24 <http://www.gnu.org/licenses/>.  */
25
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "tm.h"
30 #include <signal.h>
31 #include "rtl.h"
32 #include "regs.h"
33 #include "hard-reg-set.h"
34 #include "real.h"
35 #include "insn-config.h"
36 #include "conditions.h"
37 #include "insn-attr.h"
38 #include "recog.h"
39 #include "toplev.h"
40 #include "output.h"
41 #include "tree.h"
42 #include "function.h"
43 #include "expr.h"
44 #include "optabs.h"
45 #include "libfuncs.h"
46 #include "flags.h"
47 #include "reload.h"
48 #include "tm_p.h"
49 #include "ggc.h"
50 #include "gstab.h"
51 #include "hashtab.h"
52 #include "debug.h"
53 #include "target.h"
54 #include "target-def.h"
55 #include "integrate.h"
56 #include "langhooks.h"
57 #include "cfglayout.h"
58 #include "sched-int.h"
59 #include "gimple.h"
60 #include "bitmap.h"
61 #include "diagnostic.h"
62
63 /* True if X is an UNSPEC wrapper around a SYMBOL_REF or LABEL_REF.  */
64 #define UNSPEC_ADDRESS_P(X)                                     \
65   (GET_CODE (X) == UNSPEC                                       \
66    && XINT (X, 1) >= UNSPEC_ADDRESS_FIRST                       \
67    && XINT (X, 1) < UNSPEC_ADDRESS_FIRST + NUM_SYMBOL_TYPES)
68
69 /* Extract the symbol or label from UNSPEC wrapper X.  */
70 #define UNSPEC_ADDRESS(X) \
71   XVECEXP (X, 0, 0)
72
73 /* Extract the symbol type from UNSPEC wrapper X.  */
74 #define UNSPEC_ADDRESS_TYPE(X) \
75   ((enum mips_symbol_type) (XINT (X, 1) - UNSPEC_ADDRESS_FIRST))
76
77 /* The maximum distance between the top of the stack frame and the
78    value $sp has when we save and restore registers.
79
80    The value for normal-mode code must be a SMALL_OPERAND and must
81    preserve the maximum stack alignment.  We therefore use a value
82    of 0x7ff0 in this case.
83
84    MIPS16e SAVE and RESTORE instructions can adjust the stack pointer by
85    up to 0x7f8 bytes and can usually save or restore all the registers
86    that we need to save or restore.  (Note that we can only use these
87    instructions for o32, for which the stack alignment is 8 bytes.)
88
89    We use a maximum gap of 0x100 or 0x400 for MIPS16 code when SAVE and
90    RESTORE are not available.  We can then use unextended instructions
91    to save and restore registers, and to allocate and deallocate the top
92    part of the frame.  */
93 #define MIPS_MAX_FIRST_STACK_STEP                                       \
94   (!TARGET_MIPS16 ? 0x7ff0                                              \
95    : GENERATE_MIPS16E_SAVE_RESTORE ? 0x7f8                              \
96    : TARGET_64BIT ? 0x100 : 0x400)
97
98 /* True if INSN is a mips.md pattern or asm statement.  */
99 #define USEFUL_INSN_P(INSN)                                             \
100   (NONDEBUG_INSN_P (INSN)                                               \
101    && GET_CODE (PATTERN (INSN)) != USE                                  \
102    && GET_CODE (PATTERN (INSN)) != CLOBBER                              \
103    && GET_CODE (PATTERN (INSN)) != ADDR_VEC                             \
104    && GET_CODE (PATTERN (INSN)) != ADDR_DIFF_VEC)
105
106 /* If INSN is a delayed branch sequence, return the first instruction
107    in the sequence, otherwise return INSN itself.  */
108 #define SEQ_BEGIN(INSN)                                                 \
109   (INSN_P (INSN) && GET_CODE (PATTERN (INSN)) == SEQUENCE               \
110    ? XVECEXP (PATTERN (INSN), 0, 0)                                     \
111    : (INSN))
112
113 /* Likewise for the last instruction in a delayed branch sequence.  */
114 #define SEQ_END(INSN)                                                   \
115   (INSN_P (INSN) && GET_CODE (PATTERN (INSN)) == SEQUENCE               \
116    ? XVECEXP (PATTERN (INSN), 0, XVECLEN (PATTERN (INSN), 0) - 1)       \
117    : (INSN))
118
119 /* Execute the following loop body with SUBINSN set to each instruction
120    between SEQ_BEGIN (INSN) and SEQ_END (INSN) inclusive.  */
121 #define FOR_EACH_SUBINSN(SUBINSN, INSN)                                 \
122   for ((SUBINSN) = SEQ_BEGIN (INSN);                                    \
123        (SUBINSN) != NEXT_INSN (SEQ_END (INSN));                         \
124        (SUBINSN) = NEXT_INSN (SUBINSN))
125
126 /* True if bit BIT is set in VALUE.  */
127 #define BITSET_P(VALUE, BIT) (((VALUE) & (1 << (BIT))) != 0)
128
129 /* Return the opcode for a ptr_mode load of the form:
130
131        l[wd]    DEST, OFFSET(BASE).  */
132 #define MIPS_LOAD_PTR(DEST, OFFSET, BASE)       \
133   (((ptr_mode == DImode ? 0x37 : 0x23) << 26)   \
134    | ((BASE) << 21)                             \
135    | ((DEST) << 16)                             \
136    | (OFFSET))
137
138 /* Return the opcode to move register SRC into register DEST.  */
139 #define MIPS_MOVE(DEST, SRC)            \
140   ((TARGET_64BIT ? 0x2d : 0x21)         \
141    | ((DEST) << 11)                     \
142    | ((SRC) << 21))
143
144 /* Return the opcode for:
145
146        lui      DEST, VALUE.  */
147 #define MIPS_LUI(DEST, VALUE) \
148   ((0xf << 26) | ((DEST) << 16) | (VALUE))
149
150 /* Return the opcode to jump to register DEST.  */
151 #define MIPS_JR(DEST) \
152   (((DEST) << 21) | 0x8)
153
154 /* Return the opcode for:
155
156        bal     . + (1 + OFFSET) * 4.  */
157 #define MIPS_BAL(OFFSET) \
158   ((0x1 << 26) | (0x11 << 16) | (OFFSET))
159
160 /* Return the usual opcode for a nop.  */
161 #define MIPS_NOP 0
162
163 /* Classifies an address.
164
165    ADDRESS_REG
166        A natural register + offset address.  The register satisfies
167        mips_valid_base_register_p and the offset is a const_arith_operand.
168
169    ADDRESS_LO_SUM
170        A LO_SUM rtx.  The first operand is a valid base register and
171        the second operand is a symbolic address.
172
173    ADDRESS_CONST_INT
174        A signed 16-bit constant address.
175
176    ADDRESS_SYMBOLIC:
177        A constant symbolic address.  */
178 enum mips_address_type {
179   ADDRESS_REG,
180   ADDRESS_LO_SUM,
181   ADDRESS_CONST_INT,
182   ADDRESS_SYMBOLIC
183 };
184
185 /* Enumerates the setting of the -mr10k-cache-barrier option.  */
186 enum mips_r10k_cache_barrier_setting {
187   R10K_CACHE_BARRIER_NONE,
188   R10K_CACHE_BARRIER_STORE,
189   R10K_CACHE_BARRIER_LOAD_STORE
190 };
191
192 /* Macros to create an enumeration identifier for a function prototype.  */
193 #define MIPS_FTYPE_NAME1(A, B) MIPS_##A##_FTYPE_##B
194 #define MIPS_FTYPE_NAME2(A, B, C) MIPS_##A##_FTYPE_##B##_##C
195 #define MIPS_FTYPE_NAME3(A, B, C, D) MIPS_##A##_FTYPE_##B##_##C##_##D
196 #define MIPS_FTYPE_NAME4(A, B, C, D, E) MIPS_##A##_FTYPE_##B##_##C##_##D##_##E
197
198 /* Classifies the prototype of a built-in function.  */
199 enum mips_function_type {
200 #define DEF_MIPS_FTYPE(NARGS, LIST) MIPS_FTYPE_NAME##NARGS LIST,
201 #include "config/mips/mips-ftypes.def"
202 #undef DEF_MIPS_FTYPE
203   MIPS_MAX_FTYPE_MAX
204 };
205
206 /* Specifies how a built-in function should be converted into rtl.  */
207 enum mips_builtin_type {
208   /* The function corresponds directly to an .md pattern.  The return
209      value is mapped to operand 0 and the arguments are mapped to
210      operands 1 and above.  */
211   MIPS_BUILTIN_DIRECT,
212
213   /* The function corresponds directly to an .md pattern.  There is no return
214      value and the arguments are mapped to operands 0 and above.  */
215   MIPS_BUILTIN_DIRECT_NO_TARGET,
216
217   /* The function corresponds to a comparison instruction followed by
218      a mips_cond_move_tf_ps pattern.  The first two arguments are the
219      values to compare and the second two arguments are the vector
220      operands for the movt.ps or movf.ps instruction (in assembly order).  */
221   MIPS_BUILTIN_MOVF,
222   MIPS_BUILTIN_MOVT,
223
224   /* The function corresponds to a V2SF comparison instruction.  Operand 0
225      of this instruction is the result of the comparison, which has mode
226      CCV2 or CCV4.  The function arguments are mapped to operands 1 and
227      above.  The function's return value is an SImode boolean that is
228      true under the following conditions:
229
230      MIPS_BUILTIN_CMP_ANY: one of the registers is true
231      MIPS_BUILTIN_CMP_ALL: all of the registers are true
232      MIPS_BUILTIN_CMP_LOWER: the first register is true
233      MIPS_BUILTIN_CMP_UPPER: the second register is true.  */
234   MIPS_BUILTIN_CMP_ANY,
235   MIPS_BUILTIN_CMP_ALL,
236   MIPS_BUILTIN_CMP_UPPER,
237   MIPS_BUILTIN_CMP_LOWER,
238
239   /* As above, but the instruction only sets a single $fcc register.  */
240   MIPS_BUILTIN_CMP_SINGLE,
241
242   /* For generating bposge32 branch instructions in MIPS32 DSP ASE.  */
243   MIPS_BUILTIN_BPOSGE32
244 };
245
246 /* Invoke MACRO (COND) for each C.cond.fmt condition.  */
247 #define MIPS_FP_CONDITIONS(MACRO) \
248   MACRO (f),    \
249   MACRO (un),   \
250   MACRO (eq),   \
251   MACRO (ueq),  \
252   MACRO (olt),  \
253   MACRO (ult),  \
254   MACRO (ole),  \
255   MACRO (ule),  \
256   MACRO (sf),   \
257   MACRO (ngle), \
258   MACRO (seq),  \
259   MACRO (ngl),  \
260   MACRO (lt),   \
261   MACRO (nge),  \
262   MACRO (le),   \
263   MACRO (ngt)
264
265 /* Enumerates the codes above as MIPS_FP_COND_<X>.  */
266 #define DECLARE_MIPS_COND(X) MIPS_FP_COND_ ## X
267 enum mips_fp_condition {
268   MIPS_FP_CONDITIONS (DECLARE_MIPS_COND)
269 };
270
271 /* Index X provides the string representation of MIPS_FP_COND_<X>.  */
272 #define STRINGIFY(X) #X
273 static const char *const mips_fp_conditions[] = {
274   MIPS_FP_CONDITIONS (STRINGIFY)
275 };
276
277 /* Information about a function's frame layout.  */
278 struct GTY(())  mips_frame_info {
279   /* The size of the frame in bytes.  */
280   HOST_WIDE_INT total_size;
281
282   /* The number of bytes allocated to variables.  */
283   HOST_WIDE_INT var_size;
284
285   /* The number of bytes allocated to outgoing function arguments.  */
286   HOST_WIDE_INT args_size;
287
288   /* The number of bytes allocated to the .cprestore slot, or 0 if there
289      is no such slot.  */
290   HOST_WIDE_INT cprestore_size;
291
292   /* Bit X is set if the function saves or restores GPR X.  */
293   unsigned int mask;
294
295   /* Likewise FPR X.  */
296   unsigned int fmask;
297
298   /* Likewise doubleword accumulator X ($acX).  */
299   unsigned int acc_mask;
300
301   /* The number of GPRs, FPRs, doubleword accumulators and COP0
302      registers saved.  */
303   unsigned int num_gp;
304   unsigned int num_fp;
305   unsigned int num_acc;
306   unsigned int num_cop0_regs;
307
308   /* The offset of the topmost GPR, FPR, accumulator and COP0-register
309      save slots from the top of the frame, or zero if no such slots are
310      needed.  */
311   HOST_WIDE_INT gp_save_offset;
312   HOST_WIDE_INT fp_save_offset;
313   HOST_WIDE_INT acc_save_offset;
314   HOST_WIDE_INT cop0_save_offset;
315
316   /* Likewise, but giving offsets from the bottom of the frame.  */
317   HOST_WIDE_INT gp_sp_offset;
318   HOST_WIDE_INT fp_sp_offset;
319   HOST_WIDE_INT acc_sp_offset;
320   HOST_WIDE_INT cop0_sp_offset;
321
322   /* Similar, but the value passed to _mcount.  */
323   HOST_WIDE_INT ra_fp_offset;
324
325   /* The offset of arg_pointer_rtx from the bottom of the frame.  */
326   HOST_WIDE_INT arg_pointer_offset;
327
328   /* The offset of hard_frame_pointer_rtx from the bottom of the frame.  */
329   HOST_WIDE_INT hard_frame_pointer_offset;
330 };
331
332 struct GTY(())  machine_function {
333   /* The register returned by mips16_gp_pseudo_reg; see there for details.  */
334   rtx mips16_gp_pseudo_rtx;
335
336   /* The number of extra stack bytes taken up by register varargs.
337      This area is allocated by the callee at the very top of the frame.  */
338   int varargs_size;
339
340   /* The current frame information, calculated by mips_compute_frame_info.  */
341   struct mips_frame_info frame;
342
343   /* The register to use as the function's global pointer, or INVALID_REGNUM
344      if the function doesn't need one.  */
345   unsigned int global_pointer;
346
347   /* How many instructions it takes to load a label into $AT, or 0 if
348      this property hasn't yet been calculated.  */
349   unsigned int load_label_length;
350
351   /* True if mips_adjust_insn_length should ignore an instruction's
352      hazard attribute.  */
353   bool ignore_hazard_length_p;
354
355   /* True if the whole function is suitable for .set noreorder and
356      .set nomacro.  */
357   bool all_noreorder_p;
358
359   /* True if the function has "inflexible" and "flexible" references
360      to the global pointer.  See mips_cfun_has_inflexible_gp_ref_p
361      and mips_cfun_has_flexible_gp_ref_p for details.  */
362   bool has_inflexible_gp_insn_p;
363   bool has_flexible_gp_insn_p;
364
365   /* True if the function's prologue must load the global pointer
366      value into pic_offset_table_rtx and store the same value in
367      the function's cprestore slot (if any).  Even if this value
368      is currently false, we may decide to set it to true later;
369      see mips_must_initialize_gp_p () for details.  */
370   bool must_initialize_gp_p;
371
372   /* True if the current function must restore $gp after any potential
373      clobber.  This value is only meaningful during the first post-epilogue
374      split_insns pass; see mips_must_initialize_gp_p () for details.  */
375   bool must_restore_gp_when_clobbered_p;
376
377   /* True if we have emitted an instruction to initialize
378      mips16_gp_pseudo_rtx.  */
379   bool initialized_mips16_gp_pseudo_p;
380
381   /* True if this is an interrupt handler.  */
382   bool interrupt_handler_p;
383
384   /* True if this is an interrupt handler that uses shadow registers.  */
385   bool use_shadow_register_set_p;
386
387   /* True if this is an interrupt handler that should keep interrupts
388      masked.  */
389   bool keep_interrupts_masked_p;
390
391   /* True if this is an interrupt handler that should use DERET
392      instead of ERET.  */
393   bool use_debug_exception_return_p;
394 };
395
396 /* Information about a single argument.  */
397 struct mips_arg_info {
398   /* True if the argument is passed in a floating-point register, or
399      would have been if we hadn't run out of registers.  */
400   bool fpr_p;
401
402   /* The number of words passed in registers, rounded up.  */
403   unsigned int reg_words;
404
405   /* For EABI, the offset of the first register from GP_ARG_FIRST or
406      FP_ARG_FIRST.  For other ABIs, the offset of the first register from
407      the start of the ABI's argument structure (see the CUMULATIVE_ARGS
408      comment for details).
409
410      The value is MAX_ARGS_IN_REGISTERS if the argument is passed entirely
411      on the stack.  */
412   unsigned int reg_offset;
413
414   /* The number of words that must be passed on the stack, rounded up.  */
415   unsigned int stack_words;
416
417   /* The offset from the start of the stack overflow area of the argument's
418      first stack word.  Only meaningful when STACK_WORDS is nonzero.  */
419   unsigned int stack_offset;
420 };
421
422 /* Information about an address described by mips_address_type.
423
424    ADDRESS_CONST_INT
425        No fields are used.
426
427    ADDRESS_REG
428        REG is the base register and OFFSET is the constant offset.
429
430    ADDRESS_LO_SUM
431        REG and OFFSET are the operands to the LO_SUM and SYMBOL_TYPE
432        is the type of symbol it references.
433
434    ADDRESS_SYMBOLIC
435        SYMBOL_TYPE is the type of symbol that the address references.  */
436 struct mips_address_info {
437   enum mips_address_type type;
438   rtx reg;
439   rtx offset;
440   enum mips_symbol_type symbol_type;
441 };
442
443 /* One stage in a constant building sequence.  These sequences have
444    the form:
445
446         A = VALUE[0]
447         A = A CODE[1] VALUE[1]
448         A = A CODE[2] VALUE[2]
449         ...
450
451    where A is an accumulator, each CODE[i] is a binary rtl operation
452    and each VALUE[i] is a constant integer.  CODE[0] is undefined.  */
453 struct mips_integer_op {
454   enum rtx_code code;
455   unsigned HOST_WIDE_INT value;
456 };
457
458 /* The largest number of operations needed to load an integer constant.
459    The worst accepted case for 64-bit constants is LUI,ORI,SLL,ORI,SLL,ORI.
460    When the lowest bit is clear, we can try, but reject a sequence with
461    an extra SLL at the end.  */
462 #define MIPS_MAX_INTEGER_OPS 7
463
464 /* Information about a MIPS16e SAVE or RESTORE instruction.  */
465 struct mips16e_save_restore_info {
466   /* The number of argument registers saved by a SAVE instruction.
467      0 for RESTORE instructions.  */
468   unsigned int nargs;
469
470   /* Bit X is set if the instruction saves or restores GPR X.  */
471   unsigned int mask;
472
473   /* The total number of bytes to allocate.  */
474   HOST_WIDE_INT size;
475 };
476
477 /* Global variables for machine-dependent things.  */
478
479 /* The -G setting, or the configuration's default small-data limit if
480    no -G option is given.  */
481 static unsigned int mips_small_data_threshold;
482
483 /* The number of file directives written by mips_output_filename.  */
484 int num_source_filenames;
485
486 /* The name that appeared in the last .file directive written by
487    mips_output_filename, or "" if mips_output_filename hasn't
488    written anything yet.  */
489 const char *current_function_file = "";
490
491 /* A label counter used by PUT_SDB_BLOCK_START and PUT_SDB_BLOCK_END.  */
492 int sdb_label_count;
493
494 /* Arrays that map GCC register numbers to debugger register numbers.  */
495 int mips_dbx_regno[FIRST_PSEUDO_REGISTER];
496 int mips_dwarf_regno[FIRST_PSEUDO_REGISTER];
497
498 /* The nesting depth of the PRINT_OPERAND '%(', '%<' and '%[' constructs.  */
499 struct mips_asm_switch mips_noreorder = { "reorder", 0 };
500 struct mips_asm_switch mips_nomacro = { "macro", 0 };
501 struct mips_asm_switch mips_noat = { "at", 0 };
502
503 /* True if we're writing out a branch-likely instruction rather than a
504    normal branch.  */
505 static bool mips_branch_likely;
506
507 /* The current instruction-set architecture.  */
508 enum processor_type mips_arch;
509 const struct mips_cpu_info *mips_arch_info;
510
511 /* The processor that we should tune the code for.  */
512 enum processor_type mips_tune;
513 const struct mips_cpu_info *mips_tune_info;
514
515 /* The ISA level associated with mips_arch.  */
516 int mips_isa;
517
518 /* The architecture selected by -mipsN, or null if -mipsN wasn't used.  */
519 static const struct mips_cpu_info *mips_isa_option_info;
520
521 /* Which ABI to use.  */
522 int mips_abi = MIPS_ABI_DEFAULT;
523
524 /* Which cost information to use.  */
525 const struct mips_rtx_cost_data *mips_cost;
526
527 /* The ambient target flags, excluding MASK_MIPS16.  */
528 static int mips_base_target_flags;
529
530 /* True if MIPS16 is the default mode.  */
531 bool mips_base_mips16;
532
533 /* The ambient values of other global variables.  */
534 static int mips_base_schedule_insns; /* flag_schedule_insns */
535 static int mips_base_reorder_blocks_and_partition; /* flag_reorder... */
536 static int mips_base_move_loop_invariants; /* flag_move_loop_invariants */
537 static int mips_base_align_loops; /* align_loops */
538 static int mips_base_align_jumps; /* align_jumps */
539 static int mips_base_align_functions; /* align_functions */
540
541 /* The -mcode-readable setting.  */
542 enum mips_code_readable_setting mips_code_readable = CODE_READABLE_YES;
543
544 /* The -mr10k-cache-barrier setting.  */
545 static enum mips_r10k_cache_barrier_setting mips_r10k_cache_barrier;
546
547 /* Index [M][R] is true if register R is allowed to hold a value of mode M.  */
548 bool mips_hard_regno_mode_ok[(int) MAX_MACHINE_MODE][FIRST_PSEUDO_REGISTER];
549
550 /* Index C is true if character C is a valid PRINT_OPERAND punctation
551    character.  */
552 bool mips_print_operand_punct[256];
553
554 static GTY (()) int mips_output_filename_first_time = 1;
555
556 /* mips_split_p[X] is true if symbols of type X can be split by
557    mips_split_symbol.  */
558 bool mips_split_p[NUM_SYMBOL_TYPES];
559
560 /* mips_split_hi_p[X] is true if the high parts of symbols of type X
561    can be split by mips_split_symbol.  */
562 bool mips_split_hi_p[NUM_SYMBOL_TYPES];
563
564 /* mips_lo_relocs[X] is the relocation to use when a symbol of type X
565    appears in a LO_SUM.  It can be null if such LO_SUMs aren't valid or
566    if they are matched by a special .md file pattern.  */
567 static const char *mips_lo_relocs[NUM_SYMBOL_TYPES];
568
569 /* Likewise for HIGHs.  */
570 static const char *mips_hi_relocs[NUM_SYMBOL_TYPES];
571
572 /* Index R is the smallest register class that contains register R.  */
573 const enum reg_class mips_regno_to_class[FIRST_PSEUDO_REGISTER] = {
574   LEA_REGS,     LEA_REGS,       M16_REGS,       V1_REG,
575   M16_REGS,     M16_REGS,       M16_REGS,       M16_REGS,
576   LEA_REGS,     LEA_REGS,       LEA_REGS,       LEA_REGS,
577   LEA_REGS,     LEA_REGS,       LEA_REGS,       LEA_REGS,
578   M16_REGS,     M16_REGS,       LEA_REGS,       LEA_REGS,
579   LEA_REGS,     LEA_REGS,       LEA_REGS,       LEA_REGS,
580   T_REG,        PIC_FN_ADDR_REG, LEA_REGS,      LEA_REGS,
581   LEA_REGS,     LEA_REGS,       LEA_REGS,       LEA_REGS,
582   FP_REGS,      FP_REGS,        FP_REGS,        FP_REGS,
583   FP_REGS,      FP_REGS,        FP_REGS,        FP_REGS,
584   FP_REGS,      FP_REGS,        FP_REGS,        FP_REGS,
585   FP_REGS,      FP_REGS,        FP_REGS,        FP_REGS,
586   FP_REGS,      FP_REGS,        FP_REGS,        FP_REGS,
587   FP_REGS,      FP_REGS,        FP_REGS,        FP_REGS,
588   FP_REGS,      FP_REGS,        FP_REGS,        FP_REGS,
589   FP_REGS,      FP_REGS,        FP_REGS,        FP_REGS,
590   MD0_REG,      MD1_REG,        NO_REGS,        ST_REGS,
591   ST_REGS,      ST_REGS,        ST_REGS,        ST_REGS,
592   ST_REGS,      ST_REGS,        ST_REGS,        NO_REGS,
593   NO_REGS,      FRAME_REGS,     FRAME_REGS,     NO_REGS,
594   COP0_REGS,    COP0_REGS,      COP0_REGS,      COP0_REGS,
595   COP0_REGS,    COP0_REGS,      COP0_REGS,      COP0_REGS,
596   COP0_REGS,    COP0_REGS,      COP0_REGS,      COP0_REGS,
597   COP0_REGS,    COP0_REGS,      COP0_REGS,      COP0_REGS,
598   COP0_REGS,    COP0_REGS,      COP0_REGS,      COP0_REGS,
599   COP0_REGS,    COP0_REGS,      COP0_REGS,      COP0_REGS,
600   COP0_REGS,    COP0_REGS,      COP0_REGS,      COP0_REGS,
601   COP0_REGS,    COP0_REGS,      COP0_REGS,      COP0_REGS,
602   COP2_REGS,    COP2_REGS,      COP2_REGS,      COP2_REGS,
603   COP2_REGS,    COP2_REGS,      COP2_REGS,      COP2_REGS,
604   COP2_REGS,    COP2_REGS,      COP2_REGS,      COP2_REGS,
605   COP2_REGS,    COP2_REGS,      COP2_REGS,      COP2_REGS,
606   COP2_REGS,    COP2_REGS,      COP2_REGS,      COP2_REGS,
607   COP2_REGS,    COP2_REGS,      COP2_REGS,      COP2_REGS,
608   COP2_REGS,    COP2_REGS,      COP2_REGS,      COP2_REGS,
609   COP2_REGS,    COP2_REGS,      COP2_REGS,      COP2_REGS,
610   COP3_REGS,    COP3_REGS,      COP3_REGS,      COP3_REGS,
611   COP3_REGS,    COP3_REGS,      COP3_REGS,      COP3_REGS,
612   COP3_REGS,    COP3_REGS,      COP3_REGS,      COP3_REGS,
613   COP3_REGS,    COP3_REGS,      COP3_REGS,      COP3_REGS,
614   COP3_REGS,    COP3_REGS,      COP3_REGS,      COP3_REGS,
615   COP3_REGS,    COP3_REGS,      COP3_REGS,      COP3_REGS,
616   COP3_REGS,    COP3_REGS,      COP3_REGS,      COP3_REGS,
617   COP3_REGS,    COP3_REGS,      COP3_REGS,      COP3_REGS,
618   DSP_ACC_REGS, DSP_ACC_REGS,   DSP_ACC_REGS,   DSP_ACC_REGS,
619   DSP_ACC_REGS, DSP_ACC_REGS,   ALL_REGS,       ALL_REGS,
620   ALL_REGS,     ALL_REGS,       ALL_REGS,       ALL_REGS
621 };
622
623 /* The value of TARGET_ATTRIBUTE_TABLE.  */
624 static const struct attribute_spec mips_attribute_table[] = {
625   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
626   { "long_call",   0, 0, false, true,  true,  NULL },
627   { "far",         0, 0, false, true,  true,  NULL },
628   { "near",        0, 0, false, true,  true,  NULL },
629   /* We would really like to treat "mips16" and "nomips16" as type
630      attributes, but GCC doesn't provide the hooks we need to support
631      the right conversion rules.  As declaration attributes, they affect
632      code generation but don't carry other semantics.  */
633   { "mips16",      0, 0, true,  false, false, NULL },
634   { "nomips16",    0, 0, true,  false, false, NULL },
635   /* Allow functions to be specified as interrupt handlers */
636   { "interrupt",   0, 0, false, true,  true, NULL },
637   { "use_shadow_register_set",  0, 0, false, true,  true, NULL },
638   { "keep_interrupts_masked",   0, 0, false, true,  true, NULL },
639   { "use_debug_exception_return", 0, 0, false, true,  true, NULL },
640   { NULL,          0, 0, false, false, false, NULL }
641 };
642 \f
643 /* A table describing all the processors GCC knows about.  Names are
644    matched in the order listed.  The first mention of an ISA level is
645    taken as the canonical name for that ISA.
646
647    To ease comparison, please keep this table in the same order
648    as GAS's mips_cpu_info_table.  Please also make sure that
649    MIPS_ISA_LEVEL_SPEC and MIPS_ARCH_FLOAT_SPEC handle all -march
650    options correctly.  */
651 static const struct mips_cpu_info mips_cpu_info_table[] = {
652   /* Entries for generic ISAs.  */
653   { "mips1", PROCESSOR_R3000, 1, 0 },
654   { "mips2", PROCESSOR_R6000, 2, 0 },
655   { "mips3", PROCESSOR_R4000, 3, 0 },
656   { "mips4", PROCESSOR_R8000, 4, 0 },
657   /* Prefer not to use branch-likely instructions for generic MIPS32rX
658      and MIPS64rX code.  The instructions were officially deprecated
659      in revisions 2 and earlier, but revision 3 is likely to downgrade
660      that to a recommendation to avoid the instructions in code that
661      isn't tuned to a specific processor.  */
662   { "mips32", PROCESSOR_4KC, 32, PTF_AVOID_BRANCHLIKELY },
663   { "mips32r2", PROCESSOR_M4K, 33, PTF_AVOID_BRANCHLIKELY },
664   { "mips64", PROCESSOR_5KC, 64, PTF_AVOID_BRANCHLIKELY },
665   /* ??? For now just tune the generic MIPS64r2 for 5KC as well.   */
666   { "mips64r2", PROCESSOR_5KC, 65, PTF_AVOID_BRANCHLIKELY },
667
668   /* MIPS I processors.  */
669   { "r3000", PROCESSOR_R3000, 1, 0 },
670   { "r2000", PROCESSOR_R3000, 1, 0 },
671   { "r3900", PROCESSOR_R3900, 1, 0 },
672
673   /* MIPS II processors.  */
674   { "r6000", PROCESSOR_R6000, 2, 0 },
675
676   /* MIPS III processors.  */
677   { "r4000", PROCESSOR_R4000, 3, 0 },
678   { "vr4100", PROCESSOR_R4100, 3, 0 },
679   { "vr4111", PROCESSOR_R4111, 3, 0 },
680   { "vr4120", PROCESSOR_R4120, 3, 0 },
681   { "vr4130", PROCESSOR_R4130, 3, 0 },
682   { "vr4300", PROCESSOR_R4300, 3, 0 },
683   { "r4400", PROCESSOR_R4000, 3, 0 },
684   { "r4600", PROCESSOR_R4600, 3, 0 },
685   { "orion", PROCESSOR_R4600, 3, 0 },
686   { "r4650", PROCESSOR_R4650, 3, 0 },
687   /* ST Loongson 2E/2F processors.  */
688   { "loongson2e", PROCESSOR_LOONGSON_2E, 3, PTF_AVOID_BRANCHLIKELY },
689   { "loongson2f", PROCESSOR_LOONGSON_2F, 3, PTF_AVOID_BRANCHLIKELY },
690
691   /* MIPS IV processors. */
692   { "r8000", PROCESSOR_R8000, 4, 0 },
693   { "r10000", PROCESSOR_R10000, 4, 0 },
694   { "r12000", PROCESSOR_R10000, 4, 0 },
695   { "r14000", PROCESSOR_R10000, 4, 0 },
696   { "r16000", PROCESSOR_R10000, 4, 0 },
697   { "vr5000", PROCESSOR_R5000, 4, 0 },
698   { "vr5400", PROCESSOR_R5400, 4, 0 },
699   { "vr5500", PROCESSOR_R5500, 4, PTF_AVOID_BRANCHLIKELY },
700   { "rm7000", PROCESSOR_R7000, 4, 0 },
701   { "rm9000", PROCESSOR_R9000, 4, 0 },
702
703   /* MIPS32 processors.  */
704   { "4kc", PROCESSOR_4KC, 32, 0 },
705   { "4km", PROCESSOR_4KC, 32, 0 },
706   { "4kp", PROCESSOR_4KP, 32, 0 },
707   { "4ksc", PROCESSOR_4KC, 32, 0 },
708
709   /* MIPS32 Release 2 processors.  */
710   { "m4k", PROCESSOR_M4K, 33, 0 },
711   { "4kec", PROCESSOR_4KC, 33, 0 },
712   { "4kem", PROCESSOR_4KC, 33, 0 },
713   { "4kep", PROCESSOR_4KP, 33, 0 },
714   { "4ksd", PROCESSOR_4KC, 33, 0 },
715
716   { "24kc", PROCESSOR_24KC, 33, 0 },
717   { "24kf2_1", PROCESSOR_24KF2_1, 33, 0 },
718   { "24kf", PROCESSOR_24KF2_1, 33, 0 },
719   { "24kf1_1", PROCESSOR_24KF1_1, 33, 0 },
720   { "24kfx", PROCESSOR_24KF1_1, 33, 0 },
721   { "24kx", PROCESSOR_24KF1_1, 33, 0 },
722
723   { "24kec", PROCESSOR_24KC, 33, 0 }, /* 24K with DSP.  */
724   { "24kef2_1", PROCESSOR_24KF2_1, 33, 0 },
725   { "24kef", PROCESSOR_24KF2_1, 33, 0 },
726   { "24kef1_1", PROCESSOR_24KF1_1, 33, 0 },
727   { "24kefx", PROCESSOR_24KF1_1, 33, 0 },
728   { "24kex", PROCESSOR_24KF1_1, 33, 0 },
729
730   { "34kc", PROCESSOR_24KC, 33, 0 }, /* 34K with MT/DSP.  */
731   { "34kf2_1", PROCESSOR_24KF2_1, 33, 0 },
732   { "34kf", PROCESSOR_24KF2_1, 33, 0 },
733   { "34kf1_1", PROCESSOR_24KF1_1, 33, 0 },
734   { "34kfx", PROCESSOR_24KF1_1, 33, 0 },
735   { "34kx", PROCESSOR_24KF1_1, 33, 0 },
736
737   { "74kc", PROCESSOR_74KC, 33, 0 }, /* 74K with DSPr2.  */
738   { "74kf2_1", PROCESSOR_74KF2_1, 33, 0 },
739   { "74kf", PROCESSOR_74KF2_1, 33, 0 },
740   { "74kf1_1", PROCESSOR_74KF1_1, 33, 0 },
741   { "74kfx", PROCESSOR_74KF1_1, 33, 0 },
742   { "74kx", PROCESSOR_74KF1_1, 33, 0 },
743   { "74kf3_2", PROCESSOR_74KF3_2, 33, 0 },
744
745   { "1004kc", PROCESSOR_24KC, 33, 0 }, /* 1004K with MT/DSP.  */
746   { "1004kf2_1", PROCESSOR_24KF2_1, 33, 0 },
747   { "1004kf", PROCESSOR_24KF2_1, 33, 0 },
748   { "1004kf1_1", PROCESSOR_24KF1_1, 33, 0 },
749
750   /* MIPS64 processors.  */
751   { "5kc", PROCESSOR_5KC, 64, 0 },
752   { "5kf", PROCESSOR_5KF, 64, 0 },
753   { "20kc", PROCESSOR_20KC, 64, PTF_AVOID_BRANCHLIKELY },
754   { "sb1", PROCESSOR_SB1, 64, PTF_AVOID_BRANCHLIKELY },
755   { "sb1a", PROCESSOR_SB1A, 64, PTF_AVOID_BRANCHLIKELY },
756   { "sr71000", PROCESSOR_SR71000, 64, PTF_AVOID_BRANCHLIKELY },
757   { "xlr", PROCESSOR_XLR, 64, 0 },
758
759   /* MIPS64 Release 2 processors.  */
760   { "octeon", PROCESSOR_OCTEON, 65, PTF_AVOID_BRANCHLIKELY }
761 };
762
763 /* Default costs.  If these are used for a processor we should look
764    up the actual costs.  */
765 #define DEFAULT_COSTS COSTS_N_INSNS (6),  /* fp_add */       \
766                       COSTS_N_INSNS (7),  /* fp_mult_sf */   \
767                       COSTS_N_INSNS (8),  /* fp_mult_df */   \
768                       COSTS_N_INSNS (23), /* fp_div_sf */    \
769                       COSTS_N_INSNS (36), /* fp_div_df */    \
770                       COSTS_N_INSNS (10), /* int_mult_si */  \
771                       COSTS_N_INSNS (10), /* int_mult_di */  \
772                       COSTS_N_INSNS (69), /* int_div_si */   \
773                       COSTS_N_INSNS (69), /* int_div_di */   \
774                                        2, /* branch_cost */  \
775                                        4  /* memory_latency */
776
777 /* Floating-point costs for processors without an FPU.  Just assume that
778    all floating-point libcalls are very expensive.  */
779 #define SOFT_FP_COSTS COSTS_N_INSNS (256), /* fp_add */       \
780                       COSTS_N_INSNS (256), /* fp_mult_sf */   \
781                       COSTS_N_INSNS (256), /* fp_mult_df */   \
782                       COSTS_N_INSNS (256), /* fp_div_sf */    \
783                       COSTS_N_INSNS (256)  /* fp_div_df */
784
785 /* Costs to use when optimizing for size.  */
786 static const struct mips_rtx_cost_data mips_rtx_cost_optimize_size = {
787   COSTS_N_INSNS (1),            /* fp_add */
788   COSTS_N_INSNS (1),            /* fp_mult_sf */
789   COSTS_N_INSNS (1),            /* fp_mult_df */
790   COSTS_N_INSNS (1),            /* fp_div_sf */
791   COSTS_N_INSNS (1),            /* fp_div_df */
792   COSTS_N_INSNS (1),            /* int_mult_si */
793   COSTS_N_INSNS (1),            /* int_mult_di */
794   COSTS_N_INSNS (1),            /* int_div_si */
795   COSTS_N_INSNS (1),            /* int_div_di */
796                    2,           /* branch_cost */
797                    4            /* memory_latency */
798 };
799
800 /* Costs to use when optimizing for speed, indexed by processor.  */
801 static const struct mips_rtx_cost_data mips_rtx_cost_data[PROCESSOR_MAX] = {
802   { /* R3000 */
803     COSTS_N_INSNS (2),            /* fp_add */
804     COSTS_N_INSNS (4),            /* fp_mult_sf */
805     COSTS_N_INSNS (5),            /* fp_mult_df */
806     COSTS_N_INSNS (12),           /* fp_div_sf */
807     COSTS_N_INSNS (19),           /* fp_div_df */
808     COSTS_N_INSNS (12),           /* int_mult_si */
809     COSTS_N_INSNS (12),           /* int_mult_di */
810     COSTS_N_INSNS (35),           /* int_div_si */
811     COSTS_N_INSNS (35),           /* int_div_di */
812                      1,           /* branch_cost */
813                      4            /* memory_latency */
814   },
815   { /* 4KC */
816     SOFT_FP_COSTS,
817     COSTS_N_INSNS (6),            /* int_mult_si */
818     COSTS_N_INSNS (6),            /* int_mult_di */
819     COSTS_N_INSNS (36),           /* int_div_si */
820     COSTS_N_INSNS (36),           /* int_div_di */
821                      1,           /* branch_cost */
822                      4            /* memory_latency */
823   },
824   { /* 4KP */
825     SOFT_FP_COSTS,
826     COSTS_N_INSNS (36),           /* int_mult_si */
827     COSTS_N_INSNS (36),           /* int_mult_di */
828     COSTS_N_INSNS (37),           /* int_div_si */
829     COSTS_N_INSNS (37),           /* int_div_di */
830                      1,           /* branch_cost */
831                      4            /* memory_latency */
832   },
833   { /* 5KC */
834     SOFT_FP_COSTS,
835     COSTS_N_INSNS (4),            /* int_mult_si */
836     COSTS_N_INSNS (11),           /* int_mult_di */
837     COSTS_N_INSNS (36),           /* int_div_si */
838     COSTS_N_INSNS (68),           /* int_div_di */
839                      1,           /* branch_cost */
840                      4            /* memory_latency */
841   },
842   { /* 5KF */
843     COSTS_N_INSNS (4),            /* fp_add */
844     COSTS_N_INSNS (4),            /* fp_mult_sf */
845     COSTS_N_INSNS (5),            /* fp_mult_df */
846     COSTS_N_INSNS (17),           /* fp_div_sf */
847     COSTS_N_INSNS (32),           /* fp_div_df */
848     COSTS_N_INSNS (4),            /* int_mult_si */
849     COSTS_N_INSNS (11),           /* int_mult_di */
850     COSTS_N_INSNS (36),           /* int_div_si */
851     COSTS_N_INSNS (68),           /* int_div_di */
852                      1,           /* branch_cost */
853                      4            /* memory_latency */
854   },
855   { /* 20KC */
856     COSTS_N_INSNS (4),            /* fp_add */
857     COSTS_N_INSNS (4),            /* fp_mult_sf */
858     COSTS_N_INSNS (5),            /* fp_mult_df */
859     COSTS_N_INSNS (17),           /* fp_div_sf */
860     COSTS_N_INSNS (32),           /* fp_div_df */
861     COSTS_N_INSNS (4),            /* int_mult_si */
862     COSTS_N_INSNS (7),            /* int_mult_di */
863     COSTS_N_INSNS (42),           /* int_div_si */
864     COSTS_N_INSNS (72),           /* int_div_di */
865                      1,           /* branch_cost */
866                      4            /* memory_latency */
867   },
868   { /* 24KC */
869     SOFT_FP_COSTS,
870     COSTS_N_INSNS (5),            /* int_mult_si */
871     COSTS_N_INSNS (5),            /* int_mult_di */
872     COSTS_N_INSNS (41),           /* int_div_si */
873     COSTS_N_INSNS (41),           /* int_div_di */
874                      1,           /* branch_cost */
875                      4            /* memory_latency */
876   },
877   { /* 24KF2_1 */
878     COSTS_N_INSNS (8),            /* fp_add */
879     COSTS_N_INSNS (8),            /* fp_mult_sf */
880     COSTS_N_INSNS (10),           /* fp_mult_df */
881     COSTS_N_INSNS (34),           /* fp_div_sf */
882     COSTS_N_INSNS (64),           /* fp_div_df */
883     COSTS_N_INSNS (5),            /* int_mult_si */
884     COSTS_N_INSNS (5),            /* int_mult_di */
885     COSTS_N_INSNS (41),           /* int_div_si */
886     COSTS_N_INSNS (41),           /* int_div_di */
887                      1,           /* branch_cost */
888                      4            /* memory_latency */
889   },
890   { /* 24KF1_1 */
891     COSTS_N_INSNS (4),            /* fp_add */
892     COSTS_N_INSNS (4),            /* fp_mult_sf */
893     COSTS_N_INSNS (5),            /* fp_mult_df */
894     COSTS_N_INSNS (17),           /* fp_div_sf */
895     COSTS_N_INSNS (32),           /* fp_div_df */
896     COSTS_N_INSNS (5),            /* int_mult_si */
897     COSTS_N_INSNS (5),            /* int_mult_di */
898     COSTS_N_INSNS (41),           /* int_div_si */
899     COSTS_N_INSNS (41),           /* int_div_di */
900                      1,           /* branch_cost */
901                      4            /* memory_latency */
902   },
903   { /* 74KC */
904     SOFT_FP_COSTS,
905     COSTS_N_INSNS (5),            /* int_mult_si */
906     COSTS_N_INSNS (5),            /* int_mult_di */
907     COSTS_N_INSNS (41),           /* int_div_si */
908     COSTS_N_INSNS (41),           /* int_div_di */
909                      1,           /* branch_cost */
910                      4            /* memory_latency */
911   },
912   { /* 74KF2_1 */
913     COSTS_N_INSNS (8),            /* fp_add */
914     COSTS_N_INSNS (8),            /* fp_mult_sf */
915     COSTS_N_INSNS (10),           /* fp_mult_df */
916     COSTS_N_INSNS (34),           /* fp_div_sf */
917     COSTS_N_INSNS (64),           /* fp_div_df */
918     COSTS_N_INSNS (5),            /* int_mult_si */
919     COSTS_N_INSNS (5),            /* int_mult_di */
920     COSTS_N_INSNS (41),           /* int_div_si */
921     COSTS_N_INSNS (41),           /* int_div_di */
922                      1,           /* branch_cost */
923                      4            /* memory_latency */
924   },
925   { /* 74KF1_1 */
926     COSTS_N_INSNS (4),            /* fp_add */
927     COSTS_N_INSNS (4),            /* fp_mult_sf */
928     COSTS_N_INSNS (5),            /* fp_mult_df */
929     COSTS_N_INSNS (17),           /* fp_div_sf */
930     COSTS_N_INSNS (32),           /* fp_div_df */
931     COSTS_N_INSNS (5),            /* int_mult_si */
932     COSTS_N_INSNS (5),            /* int_mult_di */
933     COSTS_N_INSNS (41),           /* int_div_si */
934     COSTS_N_INSNS (41),           /* int_div_di */
935                      1,           /* branch_cost */
936                      4            /* memory_latency */
937   },
938   { /* 74KF3_2 */
939     COSTS_N_INSNS (6),            /* fp_add */
940     COSTS_N_INSNS (6),            /* fp_mult_sf */
941     COSTS_N_INSNS (7),            /* fp_mult_df */
942     COSTS_N_INSNS (25),           /* fp_div_sf */
943     COSTS_N_INSNS (48),           /* fp_div_df */
944     COSTS_N_INSNS (5),            /* int_mult_si */
945     COSTS_N_INSNS (5),            /* int_mult_di */
946     COSTS_N_INSNS (41),           /* int_div_si */
947     COSTS_N_INSNS (41),           /* int_div_di */
948                      1,           /* branch_cost */
949                      4            /* memory_latency */
950   },
951   { /* Loongson-2E */
952     DEFAULT_COSTS
953   },
954   { /* Loongson-2F */
955     DEFAULT_COSTS
956   },
957   { /* M4k */
958     DEFAULT_COSTS
959   },
960     /* Octeon */
961   {
962     SOFT_FP_COSTS,
963     COSTS_N_INSNS (5),            /* int_mult_si */
964     COSTS_N_INSNS (5),            /* int_mult_di */
965     COSTS_N_INSNS (72),           /* int_div_si */
966     COSTS_N_INSNS (72),           /* int_div_di */
967                      1,           /* branch_cost */
968                      4            /* memory_latency */
969   },
970   { /* R3900 */
971     COSTS_N_INSNS (2),            /* fp_add */
972     COSTS_N_INSNS (4),            /* fp_mult_sf */
973     COSTS_N_INSNS (5),            /* fp_mult_df */
974     COSTS_N_INSNS (12),           /* fp_div_sf */
975     COSTS_N_INSNS (19),           /* fp_div_df */
976     COSTS_N_INSNS (2),            /* int_mult_si */
977     COSTS_N_INSNS (2),            /* int_mult_di */
978     COSTS_N_INSNS (35),           /* int_div_si */
979     COSTS_N_INSNS (35),           /* int_div_di */
980                      1,           /* branch_cost */
981                      4            /* memory_latency */
982   },
983   { /* R6000 */
984     COSTS_N_INSNS (3),            /* fp_add */
985     COSTS_N_INSNS (5),            /* fp_mult_sf */
986     COSTS_N_INSNS (6),            /* fp_mult_df */
987     COSTS_N_INSNS (15),           /* fp_div_sf */
988     COSTS_N_INSNS (16),           /* fp_div_df */
989     COSTS_N_INSNS (17),           /* int_mult_si */
990     COSTS_N_INSNS (17),           /* int_mult_di */
991     COSTS_N_INSNS (38),           /* int_div_si */
992     COSTS_N_INSNS (38),           /* int_div_di */
993                      2,           /* branch_cost */
994                      6            /* memory_latency */
995   },
996   { /* R4000 */
997      COSTS_N_INSNS (6),           /* fp_add */
998      COSTS_N_INSNS (7),           /* fp_mult_sf */
999      COSTS_N_INSNS (8),           /* fp_mult_df */
1000      COSTS_N_INSNS (23),          /* fp_div_sf */
1001      COSTS_N_INSNS (36),          /* fp_div_df */
1002      COSTS_N_INSNS (10),          /* int_mult_si */
1003      COSTS_N_INSNS (10),          /* int_mult_di */
1004      COSTS_N_INSNS (69),          /* int_div_si */
1005      COSTS_N_INSNS (69),          /* int_div_di */
1006                       2,          /* branch_cost */
1007                       6           /* memory_latency */
1008   },
1009   { /* R4100 */
1010     DEFAULT_COSTS
1011   },
1012   { /* R4111 */
1013     DEFAULT_COSTS
1014   },
1015   { /* R4120 */
1016     DEFAULT_COSTS
1017   },
1018   { /* R4130 */
1019     /* The only costs that appear to be updated here are
1020        integer multiplication.  */
1021     SOFT_FP_COSTS,
1022     COSTS_N_INSNS (4),            /* int_mult_si */
1023     COSTS_N_INSNS (6),            /* int_mult_di */
1024     COSTS_N_INSNS (69),           /* int_div_si */
1025     COSTS_N_INSNS (69),           /* int_div_di */
1026                      1,           /* branch_cost */
1027                      4            /* memory_latency */
1028   },
1029   { /* R4300 */
1030     DEFAULT_COSTS
1031   },
1032   { /* R4600 */
1033     DEFAULT_COSTS
1034   },
1035   { /* R4650 */
1036     DEFAULT_COSTS
1037   },
1038   { /* R5000 */
1039     COSTS_N_INSNS (6),            /* fp_add */
1040     COSTS_N_INSNS (4),            /* fp_mult_sf */
1041     COSTS_N_INSNS (5),            /* fp_mult_df */
1042     COSTS_N_INSNS (23),           /* fp_div_sf */
1043     COSTS_N_INSNS (36),           /* fp_div_df */
1044     COSTS_N_INSNS (5),            /* int_mult_si */
1045     COSTS_N_INSNS (5),            /* int_mult_di */
1046     COSTS_N_INSNS (36),           /* int_div_si */
1047     COSTS_N_INSNS (36),           /* int_div_di */
1048                      1,           /* branch_cost */
1049                      4            /* memory_latency */
1050   },
1051   { /* R5400 */
1052     COSTS_N_INSNS (6),            /* fp_add */
1053     COSTS_N_INSNS (5),            /* fp_mult_sf */
1054     COSTS_N_INSNS (6),            /* fp_mult_df */
1055     COSTS_N_INSNS (30),           /* fp_div_sf */
1056     COSTS_N_INSNS (59),           /* fp_div_df */
1057     COSTS_N_INSNS (3),            /* int_mult_si */
1058     COSTS_N_INSNS (4),            /* int_mult_di */
1059     COSTS_N_INSNS (42),           /* int_div_si */
1060     COSTS_N_INSNS (74),           /* int_div_di */
1061                      1,           /* branch_cost */
1062                      4            /* memory_latency */
1063   },
1064   { /* R5500 */
1065     COSTS_N_INSNS (6),            /* fp_add */
1066     COSTS_N_INSNS (5),            /* fp_mult_sf */
1067     COSTS_N_INSNS (6),            /* fp_mult_df */
1068     COSTS_N_INSNS (30),           /* fp_div_sf */
1069     COSTS_N_INSNS (59),           /* fp_div_df */
1070     COSTS_N_INSNS (5),            /* int_mult_si */
1071     COSTS_N_INSNS (9),            /* int_mult_di */
1072     COSTS_N_INSNS (42),           /* int_div_si */
1073     COSTS_N_INSNS (74),           /* int_div_di */
1074                      1,           /* branch_cost */
1075                      4            /* memory_latency */
1076   },
1077   { /* R7000 */
1078     /* The only costs that are changed here are
1079        integer multiplication.  */
1080     COSTS_N_INSNS (6),            /* fp_add */
1081     COSTS_N_INSNS (7),            /* fp_mult_sf */
1082     COSTS_N_INSNS (8),            /* fp_mult_df */
1083     COSTS_N_INSNS (23),           /* fp_div_sf */
1084     COSTS_N_INSNS (36),           /* fp_div_df */
1085     COSTS_N_INSNS (5),            /* int_mult_si */
1086     COSTS_N_INSNS (9),            /* int_mult_di */
1087     COSTS_N_INSNS (69),           /* int_div_si */
1088     COSTS_N_INSNS (69),           /* int_div_di */
1089                      1,           /* branch_cost */
1090                      4            /* memory_latency */
1091   },
1092   { /* R8000 */
1093     DEFAULT_COSTS
1094   },
1095   { /* R9000 */
1096     /* The only costs that are changed here are
1097        integer multiplication.  */
1098     COSTS_N_INSNS (6),            /* fp_add */
1099     COSTS_N_INSNS (7),            /* fp_mult_sf */
1100     COSTS_N_INSNS (8),            /* fp_mult_df */
1101     COSTS_N_INSNS (23),           /* fp_div_sf */
1102     COSTS_N_INSNS (36),           /* fp_div_df */
1103     COSTS_N_INSNS (3),            /* int_mult_si */
1104     COSTS_N_INSNS (8),            /* int_mult_di */
1105     COSTS_N_INSNS (69),           /* int_div_si */
1106     COSTS_N_INSNS (69),           /* int_div_di */
1107                      1,           /* branch_cost */
1108                      4            /* memory_latency */
1109   },
1110   { /* R1x000 */
1111     COSTS_N_INSNS (2),            /* fp_add */
1112     COSTS_N_INSNS (2),            /* fp_mult_sf */
1113     COSTS_N_INSNS (2),            /* fp_mult_df */
1114     COSTS_N_INSNS (12),           /* fp_div_sf */
1115     COSTS_N_INSNS (19),           /* fp_div_df */
1116     COSTS_N_INSNS (5),            /* int_mult_si */
1117     COSTS_N_INSNS (9),            /* int_mult_di */
1118     COSTS_N_INSNS (34),           /* int_div_si */
1119     COSTS_N_INSNS (66),           /* int_div_di */
1120                      1,           /* branch_cost */
1121                      4            /* memory_latency */
1122   },
1123   { /* SB1 */
1124     /* These costs are the same as the SB-1A below.  */
1125     COSTS_N_INSNS (4),            /* fp_add */
1126     COSTS_N_INSNS (4),            /* fp_mult_sf */
1127     COSTS_N_INSNS (4),            /* fp_mult_df */
1128     COSTS_N_INSNS (24),           /* fp_div_sf */
1129     COSTS_N_INSNS (32),           /* fp_div_df */
1130     COSTS_N_INSNS (3),            /* int_mult_si */
1131     COSTS_N_INSNS (4),            /* int_mult_di */
1132     COSTS_N_INSNS (36),           /* int_div_si */
1133     COSTS_N_INSNS (68),           /* int_div_di */
1134                      1,           /* branch_cost */
1135                      4            /* memory_latency */
1136   },
1137   { /* SB1-A */
1138     /* These costs are the same as the SB-1 above.  */
1139     COSTS_N_INSNS (4),            /* fp_add */
1140     COSTS_N_INSNS (4),            /* fp_mult_sf */
1141     COSTS_N_INSNS (4),            /* fp_mult_df */
1142     COSTS_N_INSNS (24),           /* fp_div_sf */
1143     COSTS_N_INSNS (32),           /* fp_div_df */
1144     COSTS_N_INSNS (3),            /* int_mult_si */
1145     COSTS_N_INSNS (4),            /* int_mult_di */
1146     COSTS_N_INSNS (36),           /* int_div_si */
1147     COSTS_N_INSNS (68),           /* int_div_di */
1148                      1,           /* branch_cost */
1149                      4            /* memory_latency */
1150   },
1151   { /* SR71000 */
1152     DEFAULT_COSTS
1153   },
1154   { /* XLR */
1155     SOFT_FP_COSTS,
1156     COSTS_N_INSNS (8),            /* int_mult_si */
1157     COSTS_N_INSNS (8),            /* int_mult_di */
1158     COSTS_N_INSNS (72),           /* int_div_si */
1159     COSTS_N_INSNS (72),           /* int_div_di */
1160                      1,           /* branch_cost */
1161                      4            /* memory_latency */
1162   }
1163 };
1164 \f
1165 static rtx mips_find_pic_call_symbol (rtx, rtx);
1166 \f
1167 /* This hash table keeps track of implicit "mips16" and "nomips16" attributes
1168    for -mflip_mips16.  It maps decl names onto a boolean mode setting.  */
1169 struct GTY (())  mflip_mips16_entry {
1170   const char *name;
1171   bool mips16_p;
1172 };
1173 static GTY ((param_is (struct mflip_mips16_entry))) htab_t mflip_mips16_htab;
1174
1175 /* Hash table callbacks for mflip_mips16_htab.  */
1176
1177 static hashval_t
1178 mflip_mips16_htab_hash (const void *entry)
1179 {
1180   return htab_hash_string (((const struct mflip_mips16_entry *) entry)->name);
1181 }
1182
1183 static int
1184 mflip_mips16_htab_eq (const void *entry, const void *name)
1185 {
1186   return strcmp (((const struct mflip_mips16_entry *) entry)->name,
1187                  (const char *) name) == 0;
1188 }
1189
1190 /* True if -mflip-mips16 should next add an attribute for the default MIPS16
1191    mode, false if it should next add an attribute for the opposite mode.  */
1192 static GTY(()) bool mips16_flipper;
1193
1194 /* DECL is a function that needs a default "mips16" or "nomips16" attribute
1195    for -mflip-mips16.  Return true if it should use "mips16" and false if
1196    it should use "nomips16".  */
1197
1198 static bool
1199 mflip_mips16_use_mips16_p (tree decl)
1200 {
1201   struct mflip_mips16_entry *entry;
1202   const char *name;
1203   hashval_t hash;
1204   void **slot;
1205
1206   /* Use the opposite of the command-line setting for anonymous decls.  */
1207   if (!DECL_NAME (decl))
1208     return !mips_base_mips16;
1209
1210   if (!mflip_mips16_htab)
1211     mflip_mips16_htab = htab_create_ggc (37, mflip_mips16_htab_hash,
1212                                          mflip_mips16_htab_eq, NULL);
1213
1214   name = IDENTIFIER_POINTER (DECL_NAME (decl));
1215   hash = htab_hash_string (name);
1216   slot = htab_find_slot_with_hash (mflip_mips16_htab, name, hash, INSERT);
1217   entry = (struct mflip_mips16_entry *) *slot;
1218   if (!entry)
1219     {
1220       mips16_flipper = !mips16_flipper;
1221       entry = GGC_NEW (struct mflip_mips16_entry);
1222       entry->name = name;
1223       entry->mips16_p = mips16_flipper ? !mips_base_mips16 : mips_base_mips16;
1224       *slot = entry;
1225     }
1226   return entry->mips16_p;
1227 }
1228 \f
1229 /* Predicates to test for presence of "near" and "far"/"long_call"
1230    attributes on the given TYPE.  */
1231
1232 static bool
1233 mips_near_type_p (const_tree type)
1234 {
1235   return lookup_attribute ("near", TYPE_ATTRIBUTES (type)) != NULL;
1236 }
1237
1238 static bool
1239 mips_far_type_p (const_tree type)
1240 {
1241   return (lookup_attribute ("long_call", TYPE_ATTRIBUTES (type)) != NULL
1242           || lookup_attribute ("far", TYPE_ATTRIBUTES (type)) != NULL);
1243 }
1244
1245 /* Similar predicates for "mips16"/"nomips16" function attributes.  */
1246
1247 static bool
1248 mips_mips16_decl_p (const_tree decl)
1249 {
1250   return lookup_attribute ("mips16", DECL_ATTRIBUTES (decl)) != NULL;
1251 }
1252
1253 static bool
1254 mips_nomips16_decl_p (const_tree decl)
1255 {
1256   return lookup_attribute ("nomips16", DECL_ATTRIBUTES (decl)) != NULL;
1257 }
1258
1259 /* Check if the interrupt attribute is set for a function.  */
1260
1261 static bool
1262 mips_interrupt_type_p (tree type)
1263 {
1264   return lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type)) != NULL;
1265 }
1266
1267 /* Check if the attribute to use shadow register set is set for a function.  */
1268
1269 static bool
1270 mips_use_shadow_register_set_p (tree type)
1271 {
1272   return lookup_attribute ("use_shadow_register_set",
1273                            TYPE_ATTRIBUTES (type)) != NULL;
1274 }
1275
1276 /* Check if the attribute to keep interrupts masked is set for a function.  */
1277
1278 static bool
1279 mips_keep_interrupts_masked_p (tree type)
1280 {
1281   return lookup_attribute ("keep_interrupts_masked",
1282                            TYPE_ATTRIBUTES (type)) != NULL;
1283 }
1284
1285 /* Check if the attribute to use debug exception return is set for
1286    a function.  */
1287
1288 static bool
1289 mips_use_debug_exception_return_p (tree type)
1290 {
1291   return lookup_attribute ("use_debug_exception_return",
1292                            TYPE_ATTRIBUTES (type)) != NULL;
1293 }
1294
1295 /* Return true if function DECL is a MIPS16 function.  Return the ambient
1296    setting if DECL is null.  */
1297
1298 static bool
1299 mips_use_mips16_mode_p (tree decl)
1300 {
1301   if (decl)
1302     {
1303       /* Nested functions must use the same frame pointer as their
1304          parent and must therefore use the same ISA mode.  */
1305       tree parent = decl_function_context (decl);
1306       if (parent)
1307         decl = parent;
1308       if (mips_mips16_decl_p (decl))
1309         return true;
1310       if (mips_nomips16_decl_p (decl))
1311         return false;
1312     }
1313   return mips_base_mips16;
1314 }
1315
1316 /* Implement TARGET_COMP_TYPE_ATTRIBUTES.  */
1317
1318 static int
1319 mips_comp_type_attributes (const_tree type1, const_tree type2)
1320 {
1321   /* Disallow mixed near/far attributes.  */
1322   if (mips_far_type_p (type1) && mips_near_type_p (type2))
1323     return 0;
1324   if (mips_near_type_p (type1) && mips_far_type_p (type2))
1325     return 0;
1326   return 1;
1327 }
1328
1329 /* Implement TARGET_INSERT_ATTRIBUTES.  */
1330
1331 static void
1332 mips_insert_attributes (tree decl, tree *attributes)
1333 {
1334   const char *name;
1335   bool mips16_p, nomips16_p;
1336
1337   /* Check for "mips16" and "nomips16" attributes.  */
1338   mips16_p = lookup_attribute ("mips16", *attributes) != NULL;
1339   nomips16_p = lookup_attribute ("nomips16", *attributes) != NULL;
1340   if (TREE_CODE (decl) != FUNCTION_DECL)
1341     {
1342       if (mips16_p)
1343         error ("%qs attribute only applies to functions", "mips16");
1344       if (nomips16_p)
1345         error ("%qs attribute only applies to functions", "nomips16");
1346     }
1347   else
1348     {
1349       mips16_p |= mips_mips16_decl_p (decl);
1350       nomips16_p |= mips_nomips16_decl_p (decl);
1351       if (mips16_p || nomips16_p)
1352         {
1353           /* DECL cannot be simultaneously "mips16" and "nomips16".  */
1354           if (mips16_p && nomips16_p)
1355             error ("%qE cannot have both %<mips16%> and "
1356                    "%<nomips16%> attributes",
1357                    DECL_NAME (decl));
1358         }
1359       else if (TARGET_FLIP_MIPS16 && !DECL_ARTIFICIAL (decl))
1360         {
1361           /* Implement -mflip-mips16.  If DECL has neither a "nomips16" nor a
1362              "mips16" attribute, arbitrarily pick one.  We must pick the same
1363              setting for duplicate declarations of a function.  */
1364           name = mflip_mips16_use_mips16_p (decl) ? "mips16" : "nomips16";
1365           *attributes = tree_cons (get_identifier (name), NULL, *attributes);
1366         }
1367     }
1368 }
1369
1370 /* Implement TARGET_MERGE_DECL_ATTRIBUTES.  */
1371
1372 static tree
1373 mips_merge_decl_attributes (tree olddecl, tree newdecl)
1374 {
1375   /* The decls' "mips16" and "nomips16" attributes must match exactly.  */
1376   if (mips_mips16_decl_p (olddecl) != mips_mips16_decl_p (newdecl))
1377     error ("%qE redeclared with conflicting %qs attributes",
1378            DECL_NAME (newdecl), "mips16");
1379   if (mips_nomips16_decl_p (olddecl) != mips_nomips16_decl_p (newdecl))
1380     error ("%qE redeclared with conflicting %qs attributes",
1381            DECL_NAME (newdecl), "nomips16");
1382
1383   return merge_attributes (DECL_ATTRIBUTES (olddecl),
1384                            DECL_ATTRIBUTES (newdecl));
1385 }
1386 \f
1387 /* If X is a PLUS of a CONST_INT, return the two terms in *BASE_PTR
1388    and *OFFSET_PTR.  Return X in *BASE_PTR and 0 in *OFFSET_PTR otherwise.  */
1389
1390 static void
1391 mips_split_plus (rtx x, rtx *base_ptr, HOST_WIDE_INT *offset_ptr)
1392 {
1393   if (GET_CODE (x) == PLUS && CONST_INT_P (XEXP (x, 1)))
1394     {
1395       *base_ptr = XEXP (x, 0);
1396       *offset_ptr = INTVAL (XEXP (x, 1));
1397     }
1398   else
1399     {
1400       *base_ptr = x;
1401       *offset_ptr = 0;
1402     }
1403 }
1404 \f
1405 static unsigned int mips_build_integer (struct mips_integer_op *,
1406                                         unsigned HOST_WIDE_INT);
1407
1408 /* A subroutine of mips_build_integer, with the same interface.
1409    Assume that the final action in the sequence should be a left shift.  */
1410
1411 static unsigned int
1412 mips_build_shift (struct mips_integer_op *codes, HOST_WIDE_INT value)
1413 {
1414   unsigned int i, shift;
1415
1416   /* Shift VALUE right until its lowest bit is set.  Shift arithmetically
1417      since signed numbers are easier to load than unsigned ones.  */
1418   shift = 0;
1419   while ((value & 1) == 0)
1420     value /= 2, shift++;
1421
1422   i = mips_build_integer (codes, value);
1423   codes[i].code = ASHIFT;
1424   codes[i].value = shift;
1425   return i + 1;
1426 }
1427
1428 /* As for mips_build_shift, but assume that the final action will be
1429    an IOR or PLUS operation.  */
1430
1431 static unsigned int
1432 mips_build_lower (struct mips_integer_op *codes, unsigned HOST_WIDE_INT value)
1433 {
1434   unsigned HOST_WIDE_INT high;
1435   unsigned int i;
1436
1437   high = value & ~(unsigned HOST_WIDE_INT) 0xffff;
1438   if (!LUI_OPERAND (high) && (value & 0x18000) == 0x18000)
1439     {
1440       /* The constant is too complex to load with a simple LUI/ORI pair,
1441          so we want to give the recursive call as many trailing zeros as
1442          possible.  In this case, we know bit 16 is set and that the
1443          low 16 bits form a negative number.  If we subtract that number
1444          from VALUE, we will clear at least the lowest 17 bits, maybe more.  */
1445       i = mips_build_integer (codes, CONST_HIGH_PART (value));
1446       codes[i].code = PLUS;
1447       codes[i].value = CONST_LOW_PART (value);
1448     }
1449   else
1450     {
1451       /* Either this is a simple LUI/ORI pair, or clearing the lowest 16
1452          bits gives a value with at least 17 trailing zeros.  */
1453       i = mips_build_integer (codes, high);
1454       codes[i].code = IOR;
1455       codes[i].value = value & 0xffff;
1456     }
1457   return i + 1;
1458 }
1459
1460 /* Fill CODES with a sequence of rtl operations to load VALUE.
1461    Return the number of operations needed.  */
1462
1463 static unsigned int
1464 mips_build_integer (struct mips_integer_op *codes,
1465                     unsigned HOST_WIDE_INT value)
1466 {
1467   if (SMALL_OPERAND (value)
1468       || SMALL_OPERAND_UNSIGNED (value)
1469       || LUI_OPERAND (value))
1470     {
1471       /* The value can be loaded with a single instruction.  */
1472       codes[0].code = UNKNOWN;
1473       codes[0].value = value;
1474       return 1;
1475     }
1476   else if ((value & 1) != 0 || LUI_OPERAND (CONST_HIGH_PART (value)))
1477     {
1478       /* Either the constant is a simple LUI/ORI combination or its
1479          lowest bit is set.  We don't want to shift in this case.  */
1480       return mips_build_lower (codes, value);
1481     }
1482   else if ((value & 0xffff) == 0)
1483     {
1484       /* The constant will need at least three actions.  The lowest
1485          16 bits are clear, so the final action will be a shift.  */
1486       return mips_build_shift (codes, value);
1487     }
1488   else
1489     {
1490       /* The final action could be a shift, add or inclusive OR.
1491          Rather than use a complex condition to select the best
1492          approach, try both mips_build_shift and mips_build_lower
1493          and pick the one that gives the shortest sequence.
1494          Note that this case is only used once per constant.  */
1495       struct mips_integer_op alt_codes[MIPS_MAX_INTEGER_OPS];
1496       unsigned int cost, alt_cost;
1497
1498       cost = mips_build_shift (codes, value);
1499       alt_cost = mips_build_lower (alt_codes, value);
1500       if (alt_cost < cost)
1501         {
1502           memcpy (codes, alt_codes, alt_cost * sizeof (codes[0]));
1503           cost = alt_cost;
1504         }
1505       return cost;
1506     }
1507 }
1508 \f
1509 /* Return true if symbols of type TYPE require a GOT access.  */
1510
1511 static bool
1512 mips_got_symbol_type_p (enum mips_symbol_type type)
1513 {
1514   switch (type)
1515     {
1516     case SYMBOL_GOT_PAGE_OFST:
1517     case SYMBOL_GOT_DISP:
1518       return true;
1519
1520     default:
1521       return false;
1522     }
1523 }
1524
1525 /* Return true if X is a thread-local symbol.  */
1526
1527 static bool
1528 mips_tls_symbol_p (rtx x)
1529 {
1530   return GET_CODE (x) == SYMBOL_REF && SYMBOL_REF_TLS_MODEL (x) != 0;
1531 }
1532
1533 /* Return true if SYMBOL_REF X is associated with a global symbol
1534    (in the STB_GLOBAL sense).  */
1535
1536 static bool
1537 mips_global_symbol_p (const_rtx x)
1538 {
1539   const_tree decl = SYMBOL_REF_DECL (x);
1540
1541   if (!decl)
1542     return !SYMBOL_REF_LOCAL_P (x) || SYMBOL_REF_EXTERNAL_P (x);
1543
1544   /* Weakref symbols are not TREE_PUBLIC, but their targets are global
1545      or weak symbols.  Relocations in the object file will be against
1546      the target symbol, so it's that symbol's binding that matters here.  */
1547   return DECL_P (decl) && (TREE_PUBLIC (decl) || DECL_WEAK (decl));
1548 }
1549
1550 /* Return true if function X is a libgcc MIPS16 stub function.  */
1551
1552 static bool
1553 mips16_stub_function_p (const_rtx x)
1554 {
1555   return (GET_CODE (x) == SYMBOL_REF
1556           && strncmp (XSTR (x, 0), "__mips16_", 9) == 0);
1557 }
1558
1559 /* Return true if function X is a locally-defined and locally-binding
1560    MIPS16 function.  */
1561
1562 static bool
1563 mips16_local_function_p (const_rtx x)
1564 {
1565   return (GET_CODE (x) == SYMBOL_REF
1566           && SYMBOL_REF_LOCAL_P (x)
1567           && !SYMBOL_REF_EXTERNAL_P (x)
1568           && mips_use_mips16_mode_p (SYMBOL_REF_DECL (x)));
1569 }
1570
1571 /* Return true if SYMBOL_REF X binds locally.  */
1572
1573 static bool
1574 mips_symbol_binds_local_p (const_rtx x)
1575 {
1576   return (SYMBOL_REF_DECL (x)
1577           ? targetm.binds_local_p (SYMBOL_REF_DECL (x))
1578           : SYMBOL_REF_LOCAL_P (x));
1579 }
1580
1581 /* Return true if rtx constants of mode MODE should be put into a small
1582    data section.  */
1583
1584 static bool
1585 mips_rtx_constant_in_small_data_p (enum machine_mode mode)
1586 {
1587   return (!TARGET_EMBEDDED_DATA
1588           && TARGET_LOCAL_SDATA
1589           && GET_MODE_SIZE (mode) <= mips_small_data_threshold);
1590 }
1591
1592 /* Return true if X should not be moved directly into register $25.
1593    We need this because many versions of GAS will treat "la $25,foo" as
1594    part of a call sequence and so allow a global "foo" to be lazily bound.  */
1595
1596 bool
1597 mips_dangerous_for_la25_p (rtx x)
1598 {
1599   return (!TARGET_EXPLICIT_RELOCS
1600           && TARGET_USE_GOT
1601           && GET_CODE (x) == SYMBOL_REF
1602           && mips_global_symbol_p (x));
1603 }
1604
1605 /* Return true if calls to X might need $25 to be valid on entry.  */
1606
1607 bool
1608 mips_use_pic_fn_addr_reg_p (const_rtx x)
1609 {
1610   if (!TARGET_USE_PIC_FN_ADDR_REG)
1611     return false;
1612
1613   /* MIPS16 stub functions are guaranteed not to use $25.  */
1614   if (mips16_stub_function_p (x))
1615     return false;
1616
1617   if (GET_CODE (x) == SYMBOL_REF)
1618     {
1619       /* If PLTs and copy relocations are available, the static linker
1620          will make sure that $25 is valid on entry to the target function.  */
1621       if (TARGET_ABICALLS_PIC0)
1622         return false;
1623
1624       /* Locally-defined functions use absolute accesses to set up
1625          the global pointer.  */
1626       if (TARGET_ABSOLUTE_ABICALLS
1627           && mips_symbol_binds_local_p (x)
1628           && !SYMBOL_REF_EXTERNAL_P (x))
1629         return false;
1630     }
1631
1632   return true;
1633 }
1634
1635 /* Return the method that should be used to access SYMBOL_REF or
1636    LABEL_REF X in context CONTEXT.  */
1637
1638 static enum mips_symbol_type
1639 mips_classify_symbol (const_rtx x, enum mips_symbol_context context)
1640 {
1641   if (TARGET_RTP_PIC)
1642     return SYMBOL_GOT_DISP;
1643
1644   if (GET_CODE (x) == LABEL_REF)
1645     {
1646       /* LABEL_REFs are used for jump tables as well as text labels.
1647          Only return SYMBOL_PC_RELATIVE if we know the label is in
1648          the text section.  */
1649       if (TARGET_MIPS16_SHORT_JUMP_TABLES)
1650         return SYMBOL_PC_RELATIVE;
1651
1652       if (TARGET_ABICALLS && !TARGET_ABSOLUTE_ABICALLS)
1653         return SYMBOL_GOT_PAGE_OFST;
1654
1655       return SYMBOL_ABSOLUTE;
1656     }
1657
1658   gcc_assert (GET_CODE (x) == SYMBOL_REF);
1659
1660   if (SYMBOL_REF_TLS_MODEL (x))
1661     return SYMBOL_TLS;
1662
1663   if (CONSTANT_POOL_ADDRESS_P (x))
1664     {
1665       if (TARGET_MIPS16_TEXT_LOADS)
1666         return SYMBOL_PC_RELATIVE;
1667
1668       if (TARGET_MIPS16_PCREL_LOADS && context == SYMBOL_CONTEXT_MEM)
1669         return SYMBOL_PC_RELATIVE;
1670
1671       if (mips_rtx_constant_in_small_data_p (get_pool_mode (x)))
1672         return SYMBOL_GP_RELATIVE;
1673     }
1674
1675   /* Do not use small-data accesses for weak symbols; they may end up
1676      being zero.  */
1677   if (TARGET_GPOPT && SYMBOL_REF_SMALL_P (x) && !SYMBOL_REF_WEAK (x))
1678     return SYMBOL_GP_RELATIVE;
1679
1680   /* Don't use GOT accesses for locally-binding symbols when -mno-shared
1681      is in effect.  */
1682   if (TARGET_ABICALLS_PIC2
1683       && !(TARGET_ABSOLUTE_ABICALLS && mips_symbol_binds_local_p (x)))
1684     {
1685       /* There are three cases to consider:
1686
1687             - o32 PIC (either with or without explicit relocs)
1688             - n32/n64 PIC without explicit relocs
1689             - n32/n64 PIC with explicit relocs
1690
1691          In the first case, both local and global accesses will use an
1692          R_MIPS_GOT16 relocation.  We must correctly predict which of
1693          the two semantics (local or global) the assembler and linker
1694          will apply.  The choice depends on the symbol's binding rather
1695          than its visibility.
1696
1697          In the second case, the assembler will not use R_MIPS_GOT16
1698          relocations, but it chooses between local and global accesses
1699          in the same way as for o32 PIC.
1700
1701          In the third case we have more freedom since both forms of
1702          access will work for any kind of symbol.  However, there seems
1703          little point in doing things differently.  */
1704       if (mips_global_symbol_p (x))
1705         return SYMBOL_GOT_DISP;
1706
1707       return SYMBOL_GOT_PAGE_OFST;
1708     }
1709
1710   if (TARGET_MIPS16_PCREL_LOADS && context != SYMBOL_CONTEXT_CALL)
1711     return SYMBOL_FORCE_TO_MEM;
1712
1713   return SYMBOL_ABSOLUTE;
1714 }
1715
1716 /* Classify the base of symbolic expression X, given that X appears in
1717    context CONTEXT.  */
1718
1719 static enum mips_symbol_type
1720 mips_classify_symbolic_expression (rtx x, enum mips_symbol_context context)
1721 {
1722   rtx offset;
1723
1724   split_const (x, &x, &offset);
1725   if (UNSPEC_ADDRESS_P (x))
1726     return UNSPEC_ADDRESS_TYPE (x);
1727
1728   return mips_classify_symbol (x, context);
1729 }
1730
1731 /* Return true if OFFSET is within the range [0, ALIGN), where ALIGN
1732    is the alignment in bytes of SYMBOL_REF X.  */
1733
1734 static bool
1735 mips_offset_within_alignment_p (rtx x, HOST_WIDE_INT offset)
1736 {
1737   HOST_WIDE_INT align;
1738
1739   align = SYMBOL_REF_DECL (x) ? DECL_ALIGN_UNIT (SYMBOL_REF_DECL (x)) : 1;
1740   return IN_RANGE (offset, 0, align - 1);
1741 }
1742
1743 /* Return true if X is a symbolic constant that can be used in context
1744    CONTEXT.  If it is, store the type of the symbol in *SYMBOL_TYPE.  */
1745
1746 bool
1747 mips_symbolic_constant_p (rtx x, enum mips_symbol_context context,
1748                           enum mips_symbol_type *symbol_type)
1749 {
1750   rtx offset;
1751
1752   split_const (x, &x, &offset);
1753   if (UNSPEC_ADDRESS_P (x))
1754     {
1755       *symbol_type = UNSPEC_ADDRESS_TYPE (x);
1756       x = UNSPEC_ADDRESS (x);
1757     }
1758   else if (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF)
1759     {
1760       *symbol_type = mips_classify_symbol (x, context);
1761       if (*symbol_type == SYMBOL_TLS)
1762         return false;
1763     }
1764   else
1765     return false;
1766
1767   if (offset == const0_rtx)
1768     return true;
1769
1770   /* Check whether a nonzero offset is valid for the underlying
1771      relocations.  */
1772   switch (*symbol_type)
1773     {
1774     case SYMBOL_ABSOLUTE:
1775     case SYMBOL_FORCE_TO_MEM:
1776     case SYMBOL_32_HIGH:
1777     case SYMBOL_64_HIGH:
1778     case SYMBOL_64_MID:
1779     case SYMBOL_64_LOW:
1780       /* If the target has 64-bit pointers and the object file only
1781          supports 32-bit symbols, the values of those symbols will be
1782          sign-extended.  In this case we can't allow an arbitrary offset
1783          in case the 32-bit value X + OFFSET has a different sign from X.  */
1784       if (Pmode == DImode && !ABI_HAS_64BIT_SYMBOLS)
1785         return offset_within_block_p (x, INTVAL (offset));
1786
1787       /* In other cases the relocations can handle any offset.  */
1788       return true;
1789
1790     case SYMBOL_PC_RELATIVE:
1791       /* Allow constant pool references to be converted to LABEL+CONSTANT.
1792          In this case, we no longer have access to the underlying constant,
1793          but the original symbol-based access was known to be valid.  */
1794       if (GET_CODE (x) == LABEL_REF)
1795         return true;
1796
1797       /* Fall through.  */
1798
1799     case SYMBOL_GP_RELATIVE:
1800       /* Make sure that the offset refers to something within the
1801          same object block.  This should guarantee that the final
1802          PC- or GP-relative offset is within the 16-bit limit.  */
1803       return offset_within_block_p (x, INTVAL (offset));
1804
1805     case SYMBOL_GOT_PAGE_OFST:
1806     case SYMBOL_GOTOFF_PAGE:
1807       /* If the symbol is global, the GOT entry will contain the symbol's
1808          address, and we will apply a 16-bit offset after loading it.
1809          If the symbol is local, the linker should provide enough local
1810          GOT entries for a 16-bit offset, but larger offsets may lead
1811          to GOT overflow.  */
1812       return SMALL_INT (offset);
1813
1814     case SYMBOL_TPREL:
1815     case SYMBOL_DTPREL:
1816       /* There is no carry between the HI and LO REL relocations, so the
1817          offset is only valid if we know it won't lead to such a carry.  */
1818       return mips_offset_within_alignment_p (x, INTVAL (offset));
1819
1820     case SYMBOL_GOT_DISP:
1821     case SYMBOL_GOTOFF_DISP:
1822     case SYMBOL_GOTOFF_CALL:
1823     case SYMBOL_GOTOFF_LOADGP:
1824     case SYMBOL_TLSGD:
1825     case SYMBOL_TLSLDM:
1826     case SYMBOL_GOTTPREL:
1827     case SYMBOL_TLS:
1828     case SYMBOL_HALF:
1829       return false;
1830     }
1831   gcc_unreachable ();
1832 }
1833 \f
1834 /* Like mips_symbol_insns, but treat extended MIPS16 instructions as a
1835    single instruction.  We rely on the fact that, in the worst case,
1836    all instructions involved in a MIPS16 address calculation are usually
1837    extended ones.  */
1838
1839 static int
1840 mips_symbol_insns_1 (enum mips_symbol_type type, enum machine_mode mode)
1841 {
1842   switch (type)
1843     {
1844     case SYMBOL_ABSOLUTE:
1845       /* When using 64-bit symbols, we need 5 preparatory instructions,
1846          such as:
1847
1848              lui     $at,%highest(symbol)
1849              daddiu  $at,$at,%higher(symbol)
1850              dsll    $at,$at,16
1851              daddiu  $at,$at,%hi(symbol)
1852              dsll    $at,$at,16
1853
1854          The final address is then $at + %lo(symbol).  With 32-bit
1855          symbols we just need a preparatory LUI for normal mode and
1856          a preparatory LI and SLL for MIPS16.  */
1857       return ABI_HAS_64BIT_SYMBOLS ? 6 : TARGET_MIPS16 ? 3 : 2;
1858
1859     case SYMBOL_GP_RELATIVE:
1860       /* Treat GP-relative accesses as taking a single instruction on
1861          MIPS16 too; the copy of $gp can often be shared.  */
1862       return 1;
1863
1864     case SYMBOL_PC_RELATIVE:
1865       /* PC-relative constants can be only be used with ADDIUPC,
1866          DADDIUPC, LWPC and LDPC.  */
1867       if (mode == MAX_MACHINE_MODE
1868           || GET_MODE_SIZE (mode) == 4
1869           || GET_MODE_SIZE (mode) == 8)
1870         return 1;
1871
1872       /* The constant must be loaded using ADDIUPC or DADDIUPC first.  */
1873       return 0;
1874
1875     case SYMBOL_FORCE_TO_MEM:
1876       /* LEAs will be converted into constant-pool references by
1877          mips_reorg.  */
1878       if (mode == MAX_MACHINE_MODE)
1879         return 1;
1880
1881       /* The constant must be loaded and then dereferenced.  */
1882       return 0;
1883
1884     case SYMBOL_GOT_DISP:
1885       /* The constant will have to be loaded from the GOT before it
1886          is used in an address.  */
1887       if (mode != MAX_MACHINE_MODE)
1888         return 0;
1889
1890       /* Fall through.  */
1891
1892     case SYMBOL_GOT_PAGE_OFST:
1893       /* Unless -funit-at-a-time is in effect, we can't be sure whether the
1894          local/global classification is accurate.  The worst cases are:
1895
1896          (1) For local symbols when generating o32 or o64 code.  The assembler
1897              will use:
1898
1899                  lw           $at,%got(symbol)
1900                  nop
1901
1902              ...and the final address will be $at + %lo(symbol).
1903
1904          (2) For global symbols when -mxgot.  The assembler will use:
1905
1906                  lui     $at,%got_hi(symbol)
1907                  (d)addu $at,$at,$gp
1908
1909              ...and the final address will be $at + %got_lo(symbol).  */
1910       return 3;
1911
1912     case SYMBOL_GOTOFF_PAGE:
1913     case SYMBOL_GOTOFF_DISP:
1914     case SYMBOL_GOTOFF_CALL:
1915     case SYMBOL_GOTOFF_LOADGP:
1916     case SYMBOL_32_HIGH:
1917     case SYMBOL_64_HIGH:
1918     case SYMBOL_64_MID:
1919     case SYMBOL_64_LOW:
1920     case SYMBOL_TLSGD:
1921     case SYMBOL_TLSLDM:
1922     case SYMBOL_DTPREL:
1923     case SYMBOL_GOTTPREL:
1924     case SYMBOL_TPREL:
1925     case SYMBOL_HALF:
1926       /* A 16-bit constant formed by a single relocation, or a 32-bit
1927          constant formed from a high 16-bit relocation and a low 16-bit
1928          relocation.  Use mips_split_p to determine which.  32-bit
1929          constants need an "lui; addiu" sequence for normal mode and
1930          an "li; sll; addiu" sequence for MIPS16 mode.  */
1931       return !mips_split_p[type] ? 1 : TARGET_MIPS16 ? 3 : 2;
1932
1933     case SYMBOL_TLS:
1934       /* We don't treat a bare TLS symbol as a constant.  */
1935       return 0;
1936     }
1937   gcc_unreachable ();
1938 }
1939
1940 /* If MODE is MAX_MACHINE_MODE, return the number of instructions needed
1941    to load symbols of type TYPE into a register.  Return 0 if the given
1942    type of symbol cannot be used as an immediate operand.
1943
1944    Otherwise, return the number of instructions needed to load or store
1945    values of mode MODE to or from addresses of type TYPE.  Return 0 if
1946    the given type of symbol is not valid in addresses.
1947
1948    In both cases, treat extended MIPS16 instructions as two instructions.  */
1949
1950 static int
1951 mips_symbol_insns (enum mips_symbol_type type, enum machine_mode mode)
1952 {
1953   return mips_symbol_insns_1 (type, mode) * (TARGET_MIPS16 ? 2 : 1);
1954 }
1955 \f
1956 /* A for_each_rtx callback.  Stop the search if *X references a
1957    thread-local symbol.  */
1958
1959 static int
1960 mips_tls_symbol_ref_1 (rtx *x, void *data ATTRIBUTE_UNUSED)
1961 {
1962   return mips_tls_symbol_p (*x);
1963 }
1964
1965 /* Implement TARGET_CANNOT_FORCE_CONST_MEM.  */
1966
1967 static bool
1968 mips_cannot_force_const_mem (rtx x)
1969 {
1970   enum mips_symbol_type type;
1971   rtx base, offset;
1972
1973   /* There is no assembler syntax for expressing an address-sized
1974      high part.  */
1975   if (GET_CODE (x) == HIGH)
1976     return true;
1977
1978   /* As an optimization, reject constants that mips_legitimize_move
1979      can expand inline.
1980
1981      Suppose we have a multi-instruction sequence that loads constant C
1982      into register R.  If R does not get allocated a hard register, and
1983      R is used in an operand that allows both registers and memory
1984      references, reload will consider forcing C into memory and using
1985      one of the instruction's memory alternatives.  Returning false
1986      here will force it to use an input reload instead.  */
1987   if (CONST_INT_P (x) && LEGITIMATE_CONSTANT_P (x))
1988     return true;
1989
1990   split_const (x, &base, &offset);
1991   if (mips_symbolic_constant_p (base, SYMBOL_CONTEXT_LEA, &type)
1992       && type != SYMBOL_FORCE_TO_MEM)
1993     {
1994       /* The same optimization as for CONST_INT.  */
1995       if (SMALL_INT (offset) && mips_symbol_insns (type, MAX_MACHINE_MODE) > 0)
1996         return true;
1997
1998       /* If MIPS16 constant pools live in the text section, they should
1999          not refer to anything that might need run-time relocation.  */
2000       if (TARGET_MIPS16_PCREL_LOADS && mips_got_symbol_type_p (type))
2001         return true;
2002     }
2003
2004   /* TLS symbols must be computed by mips_legitimize_move.  */
2005   if (for_each_rtx (&x, &mips_tls_symbol_ref_1, NULL))
2006     return true;
2007
2008   return false;
2009 }
2010
2011 /* Implement TARGET_USE_BLOCKS_FOR_CONSTANT_P.  We can't use blocks for
2012    constants when we're using a per-function constant pool.  */
2013
2014 static bool
2015 mips_use_blocks_for_constant_p (enum machine_mode mode ATTRIBUTE_UNUSED,
2016                                 const_rtx x ATTRIBUTE_UNUSED)
2017 {
2018   return !TARGET_MIPS16_PCREL_LOADS;
2019 }
2020 \f
2021 /* Return true if register REGNO is a valid base register for mode MODE.
2022    STRICT_P is true if REG_OK_STRICT is in effect.  */
2023
2024 int
2025 mips_regno_mode_ok_for_base_p (int regno, enum machine_mode mode,
2026                                bool strict_p)
2027 {
2028   if (!HARD_REGISTER_NUM_P (regno))
2029     {
2030       if (!strict_p)
2031         return true;
2032       regno = reg_renumber[regno];
2033     }
2034
2035   /* These fake registers will be eliminated to either the stack or
2036      hard frame pointer, both of which are usually valid base registers.
2037      Reload deals with the cases where the eliminated form isn't valid.  */
2038   if (regno == ARG_POINTER_REGNUM || regno == FRAME_POINTER_REGNUM)
2039     return true;
2040
2041   /* In MIPS16 mode, the stack pointer can only address word and doubleword
2042      values, nothing smaller.  There are two problems here:
2043
2044        (a) Instantiating virtual registers can introduce new uses of the
2045            stack pointer.  If these virtual registers are valid addresses,
2046            the stack pointer should be too.
2047
2048        (b) Most uses of the stack pointer are not made explicit until
2049            FRAME_POINTER_REGNUM and ARG_POINTER_REGNUM have been eliminated.
2050            We don't know until that stage whether we'll be eliminating to the
2051            stack pointer (which needs the restriction) or the hard frame
2052            pointer (which doesn't).
2053
2054      All in all, it seems more consistent to only enforce this restriction
2055      during and after reload.  */
2056   if (TARGET_MIPS16 && regno == STACK_POINTER_REGNUM)
2057     return !strict_p || GET_MODE_SIZE (mode) == 4 || GET_MODE_SIZE (mode) == 8;
2058
2059   return TARGET_MIPS16 ? M16_REG_P (regno) : GP_REG_P (regno);
2060 }
2061
2062 /* Return true if X is a valid base register for mode MODE.
2063    STRICT_P is true if REG_OK_STRICT is in effect.  */
2064
2065 static bool
2066 mips_valid_base_register_p (rtx x, enum machine_mode mode, bool strict_p)
2067 {
2068   if (!strict_p && GET_CODE (x) == SUBREG)
2069     x = SUBREG_REG (x);
2070
2071   return (REG_P (x)
2072           && mips_regno_mode_ok_for_base_p (REGNO (x), mode, strict_p));
2073 }
2074
2075 /* Return true if, for every base register BASE_REG, (plus BASE_REG X)
2076    can address a value of mode MODE.  */
2077
2078 static bool
2079 mips_valid_offset_p (rtx x, enum machine_mode mode)
2080 {
2081   /* Check that X is a signed 16-bit number.  */
2082   if (!const_arith_operand (x, Pmode))
2083     return false;
2084
2085   /* We may need to split multiword moves, so make sure that every word
2086      is accessible.  */
2087   if (GET_MODE_SIZE (mode) > UNITS_PER_WORD
2088       && !SMALL_OPERAND (INTVAL (x) + GET_MODE_SIZE (mode) - UNITS_PER_WORD))
2089     return false;
2090
2091   return true;
2092 }
2093
2094 /* Return true if a LO_SUM can address a value of mode MODE when the
2095    LO_SUM symbol has type SYMBOL_TYPE.  */
2096
2097 static bool
2098 mips_valid_lo_sum_p (enum mips_symbol_type symbol_type, enum machine_mode mode)
2099 {
2100   /* Check that symbols of type SYMBOL_TYPE can be used to access values
2101      of mode MODE.  */
2102   if (mips_symbol_insns (symbol_type, mode) == 0)
2103     return false;
2104
2105   /* Check that there is a known low-part relocation.  */
2106   if (mips_lo_relocs[symbol_type] == NULL)
2107     return false;
2108
2109   /* We may need to split multiword moves, so make sure that each word
2110      can be accessed without inducing a carry.  This is mainly needed
2111      for o64, which has historically only guaranteed 64-bit alignment
2112      for 128-bit types.  */
2113   if (GET_MODE_SIZE (mode) > UNITS_PER_WORD
2114       && GET_MODE_BITSIZE (mode) > GET_MODE_ALIGNMENT (mode))
2115     return false;
2116
2117   return true;
2118 }
2119
2120 /* Return true if X is a valid address for machine mode MODE.  If it is,
2121    fill in INFO appropriately.  STRICT_P is true if REG_OK_STRICT is in
2122    effect.  */
2123
2124 static bool
2125 mips_classify_address (struct mips_address_info *info, rtx x,
2126                        enum machine_mode mode, bool strict_p)
2127 {
2128   switch (GET_CODE (x))
2129     {
2130     case REG:
2131     case SUBREG:
2132       info->type = ADDRESS_REG;
2133       info->reg = x;
2134       info->offset = const0_rtx;
2135       return mips_valid_base_register_p (info->reg, mode, strict_p);
2136
2137     case PLUS:
2138       info->type = ADDRESS_REG;
2139       info->reg = XEXP (x, 0);
2140       info->offset = XEXP (x, 1);
2141       return (mips_valid_base_register_p (info->reg, mode, strict_p)
2142               && mips_valid_offset_p (info->offset, mode));
2143
2144     case LO_SUM:
2145       info->type = ADDRESS_LO_SUM;
2146       info->reg = XEXP (x, 0);
2147       info->offset = XEXP (x, 1);
2148       /* We have to trust the creator of the LO_SUM to do something vaguely
2149          sane.  Target-independent code that creates a LO_SUM should also
2150          create and verify the matching HIGH.  Target-independent code that
2151          adds an offset to a LO_SUM must prove that the offset will not
2152          induce a carry.  Failure to do either of these things would be
2153          a bug, and we are not required to check for it here.  The MIPS
2154          backend itself should only create LO_SUMs for valid symbolic
2155          constants, with the high part being either a HIGH or a copy
2156          of _gp. */
2157       info->symbol_type
2158         = mips_classify_symbolic_expression (info->offset, SYMBOL_CONTEXT_MEM);
2159       return (mips_valid_base_register_p (info->reg, mode, strict_p)
2160               && mips_valid_lo_sum_p (info->symbol_type, mode));
2161
2162     case CONST_INT:
2163       /* Small-integer addresses don't occur very often, but they
2164          are legitimate if $0 is a valid base register.  */
2165       info->type = ADDRESS_CONST_INT;
2166       return !TARGET_MIPS16 && SMALL_INT (x);
2167
2168     case CONST:
2169     case LABEL_REF:
2170     case SYMBOL_REF:
2171       info->type = ADDRESS_SYMBOLIC;
2172       return (mips_symbolic_constant_p (x, SYMBOL_CONTEXT_MEM,
2173                                         &info->symbol_type)
2174               && mips_symbol_insns (info->symbol_type, mode) > 0
2175               && !mips_split_p[info->symbol_type]);
2176
2177     default:
2178       return false;
2179     }
2180 }
2181
2182 /* Implement TARGET_LEGITIMATE_ADDRESS_P.  */
2183
2184 static bool
2185 mips_legitimate_address_p (enum machine_mode mode, rtx x, bool strict_p)
2186 {
2187   struct mips_address_info addr;
2188
2189   return mips_classify_address (&addr, x, mode, strict_p);
2190 }
2191
2192 /* Return true if X is a legitimate $sp-based address for mode MDOE.  */
2193
2194 bool
2195 mips_stack_address_p (rtx x, enum machine_mode mode)
2196 {
2197   struct mips_address_info addr;
2198
2199   return (mips_classify_address (&addr, x, mode, false)
2200           && addr.type == ADDRESS_REG
2201           && addr.reg == stack_pointer_rtx);
2202 }
2203
2204 /* Return true if ADDR matches the pattern for the LWXS load scaled indexed
2205    address instruction.  Note that such addresses are not considered
2206    legitimate in the TARGET_LEGITIMATE_ADDRESS_P sense, because their use
2207    is so restricted.  */
2208
2209 static bool
2210 mips_lwxs_address_p (rtx addr)
2211 {
2212   if (ISA_HAS_LWXS
2213       && GET_CODE (addr) == PLUS
2214       && REG_P (XEXP (addr, 1)))
2215     {
2216       rtx offset = XEXP (addr, 0);
2217       if (GET_CODE (offset) == MULT
2218           && REG_P (XEXP (offset, 0))
2219           && CONST_INT_P (XEXP (offset, 1))
2220           && INTVAL (XEXP (offset, 1)) == 4)
2221         return true;
2222     }
2223   return false;
2224 }
2225 \f
2226 /* Return true if a value at OFFSET bytes from base register BASE can be
2227    accessed using an unextended MIPS16 instruction.  MODE is the mode of
2228    the value.
2229
2230    Usually the offset in an unextended instruction is a 5-bit field.
2231    The offset is unsigned and shifted left once for LH and SH, twice
2232    for LW and SW, and so on.  An exception is LWSP and SWSP, which have
2233    an 8-bit immediate field that's shifted left twice.  */
2234
2235 static bool
2236 mips16_unextended_reference_p (enum machine_mode mode, rtx base,
2237                                unsigned HOST_WIDE_INT offset)
2238 {
2239   if (offset % GET_MODE_SIZE (mode) == 0)
2240     {
2241       if (GET_MODE_SIZE (mode) == 4 && base == stack_pointer_rtx)
2242         return offset < 256U * GET_MODE_SIZE (mode);
2243       return offset < 32U * GET_MODE_SIZE (mode);
2244     }
2245   return false;
2246 }
2247
2248 /* Return the number of instructions needed to load or store a value
2249    of mode MODE at address X.  Return 0 if X isn't valid for MODE.
2250    Assume that multiword moves may need to be split into word moves
2251    if MIGHT_SPLIT_P, otherwise assume that a single load or store is
2252    enough.
2253
2254    For MIPS16 code, count extended instructions as two instructions.  */
2255
2256 int
2257 mips_address_insns (rtx x, enum machine_mode mode, bool might_split_p)
2258 {
2259   struct mips_address_info addr;
2260   int factor;
2261
2262   /* BLKmode is used for single unaligned loads and stores and should
2263      not count as a multiword mode.  (GET_MODE_SIZE (BLKmode) is pretty
2264      meaningless, so we have to single it out as a special case one way
2265      or the other.)  */
2266   if (mode != BLKmode && might_split_p)
2267     factor = (GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
2268   else
2269     factor = 1;
2270
2271   if (mips_classify_address (&addr, x, mode, false))
2272     switch (addr.type)
2273       {
2274       case ADDRESS_REG:
2275         if (TARGET_MIPS16
2276             && !mips16_unextended_reference_p (mode, addr.reg,
2277                                                UINTVAL (addr.offset)))
2278           return factor * 2;
2279         return factor;
2280
2281       case ADDRESS_LO_SUM:
2282         return TARGET_MIPS16 ? factor * 2 : factor;
2283
2284       case ADDRESS_CONST_INT:
2285         return factor;
2286
2287       case ADDRESS_SYMBOLIC:
2288         return factor * mips_symbol_insns (addr.symbol_type, mode);
2289       }
2290   return 0;
2291 }
2292
2293 /* Return the number of instructions needed to load constant X.
2294    Return 0 if X isn't a valid constant.  */
2295
2296 int
2297 mips_const_insns (rtx x)
2298 {
2299   struct mips_integer_op codes[MIPS_MAX_INTEGER_OPS];
2300   enum mips_symbol_type symbol_type;
2301   rtx offset;
2302
2303   switch (GET_CODE (x))
2304     {
2305     case HIGH:
2306       if (!mips_symbolic_constant_p (XEXP (x, 0), SYMBOL_CONTEXT_LEA,
2307                                      &symbol_type)
2308           || !mips_split_p[symbol_type])
2309         return 0;
2310
2311       /* This is simply an LUI for normal mode.  It is an extended
2312          LI followed by an extended SLL for MIPS16.  */
2313       return TARGET_MIPS16 ? 4 : 1;
2314
2315     case CONST_INT:
2316       if (TARGET_MIPS16)
2317         /* Unsigned 8-bit constants can be loaded using an unextended
2318            LI instruction.  Unsigned 16-bit constants can be loaded
2319            using an extended LI.  Negative constants must be loaded
2320            using LI and then negated.  */
2321         return (IN_RANGE (INTVAL (x), 0, 255) ? 1
2322                 : SMALL_OPERAND_UNSIGNED (INTVAL (x)) ? 2
2323                 : IN_RANGE (-INTVAL (x), 0, 255) ? 2
2324                 : SMALL_OPERAND_UNSIGNED (-INTVAL (x)) ? 3
2325                 : 0);
2326
2327       return mips_build_integer (codes, INTVAL (x));
2328
2329     case CONST_DOUBLE:
2330     case CONST_VECTOR:
2331       /* Allow zeros for normal mode, where we can use $0.  */
2332       return !TARGET_MIPS16 && x == CONST0_RTX (GET_MODE (x)) ? 1 : 0;
2333
2334     case CONST:
2335       if (CONST_GP_P (x))
2336         return 1;
2337
2338       /* See if we can refer to X directly.  */
2339       if (mips_symbolic_constant_p (x, SYMBOL_CONTEXT_LEA, &symbol_type))
2340         return mips_symbol_insns (symbol_type, MAX_MACHINE_MODE);
2341
2342       /* Otherwise try splitting the constant into a base and offset.
2343          If the offset is a 16-bit value, we can load the base address
2344          into a register and then use (D)ADDIU to add in the offset.
2345          If the offset is larger, we can load the base and offset
2346          into separate registers and add them together with (D)ADDU.
2347          However, the latter is only possible before reload; during
2348          and after reload, we must have the option of forcing the
2349          constant into the pool instead.  */
2350       split_const (x, &x, &offset);
2351       if (offset != 0)
2352         {
2353           int n = mips_const_insns (x);
2354           if (n != 0)
2355             {
2356               if (SMALL_INT (offset))
2357                 return n + 1;
2358               else if (!targetm.cannot_force_const_mem (x))
2359                 return n + 1 + mips_build_integer (codes, INTVAL (offset));
2360             }
2361         }
2362       return 0;
2363
2364     case SYMBOL_REF:
2365     case LABEL_REF:
2366       return mips_symbol_insns (mips_classify_symbol (x, SYMBOL_CONTEXT_LEA),
2367                                 MAX_MACHINE_MODE);
2368
2369     default:
2370       return 0;
2371     }
2372 }
2373
2374 /* X is a doubleword constant that can be handled by splitting it into
2375    two words and loading each word separately.  Return the number of
2376    instructions required to do this.  */
2377
2378 int
2379 mips_split_const_insns (rtx x)
2380 {
2381   unsigned int low, high;
2382
2383   low = mips_const_insns (mips_subword (x, false));
2384   high = mips_const_insns (mips_subword (x, true));
2385   gcc_assert (low > 0 && high > 0);
2386   return low + high;
2387 }
2388
2389 /* Return the number of instructions needed to implement INSN,
2390    given that it loads from or stores to MEM.  Count extended
2391    MIPS16 instructions as two instructions.  */
2392
2393 int
2394 mips_load_store_insns (rtx mem, rtx insn)
2395 {
2396   enum machine_mode mode;
2397   bool might_split_p;
2398   rtx set;
2399
2400   gcc_assert (MEM_P (mem));
2401   mode = GET_MODE (mem);
2402
2403   /* Try to prove that INSN does not need to be split.  */
2404   might_split_p = true;
2405   if (GET_MODE_BITSIZE (mode) == 64)
2406     {
2407       set = single_set (insn);
2408       if (set && !mips_split_64bit_move_p (SET_DEST (set), SET_SRC (set)))
2409         might_split_p = false;
2410     }
2411
2412   return mips_address_insns (XEXP (mem, 0), mode, might_split_p);
2413 }
2414
2415 /* Return the number of instructions needed for an integer division.  */
2416
2417 int
2418 mips_idiv_insns (void)
2419 {
2420   int count;
2421
2422   count = 1;
2423   if (TARGET_CHECK_ZERO_DIV)
2424     {
2425       if (GENERATE_DIVIDE_TRAPS)
2426         count++;
2427       else
2428         count += 2;
2429     }
2430
2431   if (TARGET_FIX_R4000 || TARGET_FIX_R4400)
2432     count++;
2433   return count;
2434 }
2435 \f
2436 /* Emit a move from SRC to DEST.  Assume that the move expanders can
2437    handle all moves if !can_create_pseudo_p ().  The distinction is
2438    important because, unlike emit_move_insn, the move expanders know
2439    how to force Pmode objects into the constant pool even when the
2440    constant pool address is not itself legitimate.  */
2441
2442 rtx
2443 mips_emit_move (rtx dest, rtx src)
2444 {
2445   return (can_create_pseudo_p ()
2446           ? emit_move_insn (dest, src)
2447           : emit_move_insn_1 (dest, src));
2448 }
2449
2450 /* Emit an instruction of the form (set TARGET (CODE OP0)).  */
2451
2452 static void
2453 mips_emit_unary (enum rtx_code code, rtx target, rtx op0)
2454 {
2455   emit_insn (gen_rtx_SET (VOIDmode, target,
2456                           gen_rtx_fmt_e (code, GET_MODE (op0), op0)));
2457 }
2458
2459 /* Compute (CODE OP0) and store the result in a new register of mode MODE.
2460    Return that new register.  */
2461
2462 static rtx
2463 mips_force_unary (enum machine_mode mode, enum rtx_code code, rtx op0)
2464 {
2465   rtx reg;
2466
2467   reg = gen_reg_rtx (mode);
2468   mips_emit_unary (code, reg, op0);
2469   return reg;
2470 }
2471
2472 /* Emit an instruction of the form (set TARGET (CODE OP0 OP1)).  */
2473
2474 static void
2475 mips_emit_binary (enum rtx_code code, rtx target, rtx op0, rtx op1)
2476 {
2477   emit_insn (gen_rtx_SET (VOIDmode, target,
2478                           gen_rtx_fmt_ee (code, GET_MODE (target), op0, op1)));
2479 }
2480
2481 /* Compute (CODE OP0 OP1) and store the result in a new register
2482    of mode MODE.  Return that new register.  */
2483
2484 static rtx
2485 mips_force_binary (enum machine_mode mode, enum rtx_code code, rtx op0, rtx op1)
2486 {
2487   rtx reg;
2488
2489   reg = gen_reg_rtx (mode);
2490   mips_emit_binary (code, reg, op0, op1);
2491   return reg;
2492 }
2493
2494 /* Copy VALUE to a register and return that register.  If new pseudos
2495    are allowed, copy it into a new register, otherwise use DEST.  */
2496
2497 static rtx
2498 mips_force_temporary (rtx dest, rtx value)
2499 {
2500   if (can_create_pseudo_p ())
2501     return force_reg (Pmode, value);
2502   else
2503     {
2504       mips_emit_move (dest, value);
2505       return dest;
2506     }
2507 }
2508
2509 /* Emit a call sequence with call pattern PATTERN and return the call
2510    instruction itself (which is not necessarily the last instruction
2511    emitted).  ORIG_ADDR is the original, unlegitimized address,
2512    ADDR is the legitimized form, and LAZY_P is true if the call
2513    address is lazily-bound.  */
2514
2515 static rtx
2516 mips_emit_call_insn (rtx pattern, rtx orig_addr, rtx addr, bool lazy_p)
2517 {
2518   rtx insn, reg;
2519
2520   insn = emit_call_insn (pattern);
2521
2522   if (TARGET_MIPS16 && mips_use_pic_fn_addr_reg_p (orig_addr))
2523     {
2524       /* MIPS16 JALRs only take MIPS16 registers.  If the target
2525          function requires $25 to be valid on entry, we must copy it
2526          there separately.  The move instruction can be put in the
2527          call's delay slot.  */
2528       reg = gen_rtx_REG (Pmode, PIC_FUNCTION_ADDR_REGNUM);
2529       emit_insn_before (gen_move_insn (reg, addr), insn);
2530       use_reg (&CALL_INSN_FUNCTION_USAGE (insn), reg);
2531     }
2532
2533   if (lazy_p)
2534     /* Lazy-binding stubs require $gp to be valid on entry.  */
2535     use_reg (&CALL_INSN_FUNCTION_USAGE (insn), pic_offset_table_rtx);
2536
2537   if (TARGET_USE_GOT)
2538     {
2539       /* See the comment above load_call<mode> for details.  */
2540       use_reg (&CALL_INSN_FUNCTION_USAGE (insn),
2541                gen_rtx_REG (Pmode, GOT_VERSION_REGNUM));
2542       emit_insn (gen_update_got_version ());
2543     }
2544   return insn;
2545 }
2546 \f
2547 /* Wrap symbol or label BASE in an UNSPEC address of type SYMBOL_TYPE,
2548    then add CONST_INT OFFSET to the result.  */
2549
2550 static rtx
2551 mips_unspec_address_offset (rtx base, rtx offset,
2552                             enum mips_symbol_type symbol_type)
2553 {
2554   base = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, base),
2555                          UNSPEC_ADDRESS_FIRST + symbol_type);
2556   if (offset != const0_rtx)
2557     base = gen_rtx_PLUS (Pmode, base, offset);
2558   return gen_rtx_CONST (Pmode, base);
2559 }
2560
2561 /* Return an UNSPEC address with underlying address ADDRESS and symbol
2562    type SYMBOL_TYPE.  */
2563
2564 rtx
2565 mips_unspec_address (rtx address, enum mips_symbol_type symbol_type)
2566 {
2567   rtx base, offset;
2568
2569   split_const (address, &base, &offset);
2570   return mips_unspec_address_offset (base, offset, symbol_type);
2571 }
2572
2573 /* If OP is an UNSPEC address, return the address to which it refers,
2574    otherwise return OP itself.  */
2575
2576 static rtx
2577 mips_strip_unspec_address (rtx op)
2578 {
2579   rtx base, offset;
2580
2581   split_const (op, &base, &offset);
2582   if (UNSPEC_ADDRESS_P (base))
2583     op = plus_constant (UNSPEC_ADDRESS (base), INTVAL (offset));
2584   return op;
2585 }
2586
2587 /* If mips_unspec_address (ADDR, SYMBOL_TYPE) is a 32-bit value, add the
2588    high part to BASE and return the result.  Just return BASE otherwise.
2589    TEMP is as for mips_force_temporary.
2590
2591    The returned expression can be used as the first operand to a LO_SUM.  */
2592
2593 static rtx
2594 mips_unspec_offset_high (rtx temp, rtx base, rtx addr,
2595                          enum mips_symbol_type symbol_type)
2596 {
2597   if (mips_split_p[symbol_type])
2598     {
2599       addr = gen_rtx_HIGH (Pmode, mips_unspec_address (addr, symbol_type));
2600       addr = mips_force_temporary (temp, addr);
2601       base = mips_force_temporary (temp, gen_rtx_PLUS (Pmode, addr, base));
2602     }
2603   return base;
2604 }
2605 \f
2606 /* Return an instruction that copies $gp into register REG.  We want
2607    GCC to treat the register's value as constant, so that its value
2608    can be rematerialized on demand.  */
2609
2610 static rtx
2611 gen_load_const_gp (rtx reg)
2612 {
2613   return (Pmode == SImode
2614           ? gen_load_const_gp_si (reg)
2615           : gen_load_const_gp_di (reg));
2616 }
2617
2618 /* Return a pseudo register that contains the value of $gp throughout
2619    the current function.  Such registers are needed by MIPS16 functions,
2620    for which $gp itself is not a valid base register or addition operand.  */
2621
2622 static rtx
2623 mips16_gp_pseudo_reg (void)
2624 {
2625   if (cfun->machine->mips16_gp_pseudo_rtx == NULL_RTX)
2626     cfun->machine->mips16_gp_pseudo_rtx = gen_reg_rtx (Pmode);
2627
2628   /* Don't emit an instruction to initialize the pseudo register if
2629      we are being called from the tree optimizers' cost-calculation
2630      routines.  */
2631   if (!cfun->machine->initialized_mips16_gp_pseudo_p
2632       && (current_ir_type () != IR_GIMPLE || currently_expanding_to_rtl))
2633     {
2634       rtx insn, scan;
2635
2636       push_topmost_sequence ();
2637
2638       scan = get_insns ();
2639       while (NEXT_INSN (scan) && !INSN_P (NEXT_INSN (scan)))
2640         scan = NEXT_INSN (scan);
2641
2642       insn = gen_load_const_gp (cfun->machine->mips16_gp_pseudo_rtx);
2643       emit_insn_after (insn, scan);
2644
2645       pop_topmost_sequence ();
2646
2647       cfun->machine->initialized_mips16_gp_pseudo_p = true;
2648     }
2649
2650   return cfun->machine->mips16_gp_pseudo_rtx;
2651 }
2652
2653 /* Return a base register that holds pic_offset_table_rtx.
2654    TEMP, if nonnull, is a scratch Pmode base register.  */
2655
2656 rtx
2657 mips_pic_base_register (rtx temp)
2658 {
2659   if (!TARGET_MIPS16)
2660     return pic_offset_table_rtx;
2661
2662   if (can_create_pseudo_p ())
2663     return mips16_gp_pseudo_reg ();
2664
2665   if (TARGET_USE_GOT)
2666     /* The first post-reload split exposes all references to $gp
2667        (both uses and definitions).  All references must remain
2668        explicit after that point.
2669
2670        It is safe to introduce uses of $gp at any time, so for
2671        simplicity, we do that before the split too.  */
2672     mips_emit_move (temp, pic_offset_table_rtx);
2673   else
2674     emit_insn (gen_load_const_gp (temp));
2675   return temp;
2676 }
2677
2678 /* Return the RHS of a load_call<mode> insn.  */
2679
2680 static rtx
2681 mips_unspec_call (rtx reg, rtx symbol)
2682 {
2683   rtvec vec;
2684
2685   vec = gen_rtvec (3, reg, symbol, gen_rtx_REG (SImode, GOT_VERSION_REGNUM));
2686   return gen_rtx_UNSPEC (Pmode, vec, UNSPEC_LOAD_CALL);
2687 }
2688
2689 /* If SRC is the RHS of a load_call<mode> insn, return the underlying symbol
2690    reference.  Return NULL_RTX otherwise.  */
2691
2692 static rtx
2693 mips_strip_unspec_call (rtx src)
2694 {
2695   if (GET_CODE (src) == UNSPEC && XINT (src, 1) == UNSPEC_LOAD_CALL)
2696     return mips_strip_unspec_address (XVECEXP (src, 0, 1));
2697   return NULL_RTX;
2698 }
2699
2700 /* Create and return a GOT reference of type TYPE for address ADDR.
2701    TEMP, if nonnull, is a scratch Pmode base register.  */
2702
2703 rtx
2704 mips_got_load (rtx temp, rtx addr, enum mips_symbol_type type)
2705 {
2706   rtx base, high, lo_sum_symbol;
2707
2708   base = mips_pic_base_register (temp);
2709
2710   /* If we used the temporary register to load $gp, we can't use
2711      it for the high part as well.  */
2712   if (temp != NULL && reg_overlap_mentioned_p (base, temp))
2713     temp = NULL;
2714
2715   high = mips_unspec_offset_high (temp, base, addr, type);
2716   lo_sum_symbol = mips_unspec_address (addr, type);
2717
2718   if (type == SYMBOL_GOTOFF_CALL)
2719     return mips_unspec_call (high, lo_sum_symbol);
2720   else
2721     return (Pmode == SImode
2722             ? gen_unspec_gotsi (high, lo_sum_symbol)
2723             : gen_unspec_gotdi (high, lo_sum_symbol));
2724 }
2725
2726 /* If MODE is MAX_MACHINE_MODE, ADDR appears as a move operand, otherwise
2727    it appears in a MEM of that mode.  Return true if ADDR is a legitimate
2728    constant in that context and can be split into high and low parts.
2729    If so, and if LOW_OUT is nonnull, emit the high part and store the
2730    low part in *LOW_OUT.  Leave *LOW_OUT unchanged otherwise.
2731
2732    TEMP is as for mips_force_temporary and is used to load the high
2733    part into a register.
2734
2735    When MODE is MAX_MACHINE_MODE, the low part is guaranteed to be
2736    a legitimize SET_SRC for an .md pattern, otherwise the low part
2737    is guaranteed to be a legitimate address for mode MODE.  */
2738
2739 bool
2740 mips_split_symbol (rtx temp, rtx addr, enum machine_mode mode, rtx *low_out)
2741 {
2742   enum mips_symbol_context context;
2743   enum mips_symbol_type symbol_type;
2744   rtx high;
2745
2746   context = (mode == MAX_MACHINE_MODE
2747              ? SYMBOL_CONTEXT_LEA
2748              : SYMBOL_CONTEXT_MEM);
2749   if (GET_CODE (addr) == HIGH && context == SYMBOL_CONTEXT_LEA)
2750     {
2751       addr = XEXP (addr, 0);
2752       if (mips_symbolic_constant_p (addr, context, &symbol_type)
2753           && mips_symbol_insns (symbol_type, mode) > 0
2754           && mips_split_hi_p[symbol_type])
2755         {
2756           if (low_out)
2757             switch (symbol_type)
2758               {
2759               case SYMBOL_GOT_PAGE_OFST:
2760                 /* The high part of a page/ofst pair is loaded from the GOT.  */
2761                 *low_out = mips_got_load (temp, addr, SYMBOL_GOTOFF_PAGE);
2762                 break;
2763
2764               default:
2765                 gcc_unreachable ();
2766               }
2767           return true;
2768         }
2769     }
2770   else
2771     {
2772       if (mips_symbolic_constant_p (addr, context, &symbol_type)
2773           && mips_symbol_insns (symbol_type, mode) > 0
2774           && mips_split_p[symbol_type])
2775         {
2776           if (low_out)
2777             switch (symbol_type)
2778               {
2779               case SYMBOL_GOT_DISP:
2780                 /* SYMBOL_GOT_DISP symbols are loaded from the GOT.  */
2781                 *low_out = mips_got_load (temp, addr, SYMBOL_GOTOFF_DISP);
2782                 break;
2783
2784               case SYMBOL_GP_RELATIVE:
2785                 high = mips_pic_base_register (temp);
2786                 *low_out = gen_rtx_LO_SUM (Pmode, high, addr);
2787                 break;
2788
2789               default:
2790                 high = gen_rtx_HIGH (Pmode, copy_rtx (addr));
2791                 high = mips_force_temporary (temp, high);
2792                 *low_out = gen_rtx_LO_SUM (Pmode, high, addr);
2793                 break;
2794               }
2795           return true;
2796         }
2797     }
2798   return false;
2799 }
2800
2801 /* Return a legitimate address for REG + OFFSET.  TEMP is as for
2802    mips_force_temporary; it is only needed when OFFSET is not a
2803    SMALL_OPERAND.  */
2804
2805 static rtx
2806 mips_add_offset (rtx temp, rtx reg, HOST_WIDE_INT offset)
2807 {
2808   if (!SMALL_OPERAND (offset))
2809     {
2810       rtx high;
2811
2812       if (TARGET_MIPS16)
2813         {
2814           /* Load the full offset into a register so that we can use
2815              an unextended instruction for the address itself.  */
2816           high = GEN_INT (offset);
2817           offset = 0;
2818         }
2819       else
2820         {
2821           /* Leave OFFSET as a 16-bit offset and put the excess in HIGH.
2822              The addition inside the macro CONST_HIGH_PART may cause an
2823              overflow, so we need to force a sign-extension check.  */
2824           high = gen_int_mode (CONST_HIGH_PART (offset), Pmode);
2825           offset = CONST_LOW_PART (offset);
2826         }
2827       high = mips_force_temporary (temp, high);
2828       reg = mips_force_temporary (temp, gen_rtx_PLUS (Pmode, high, reg));
2829     }
2830   return plus_constant (reg, offset);
2831 }
2832 \f
2833 /* The __tls_get_attr symbol.  */
2834 static GTY(()) rtx mips_tls_symbol;
2835
2836 /* Return an instruction sequence that calls __tls_get_addr.  SYM is
2837    the TLS symbol we are referencing and TYPE is the symbol type to use
2838    (either global dynamic or local dynamic).  V0 is an RTX for the
2839    return value location.  */
2840
2841 static rtx
2842 mips_call_tls_get_addr (rtx sym, enum mips_symbol_type type, rtx v0)
2843 {
2844   rtx insn, loc, a0;
2845
2846   a0 = gen_rtx_REG (Pmode, GP_ARG_FIRST);
2847
2848   if (!mips_tls_symbol)
2849     mips_tls_symbol = init_one_libfunc ("__tls_get_addr");
2850
2851   loc = mips_unspec_address (sym, type);
2852
2853   start_sequence ();
2854
2855   emit_insn (gen_rtx_SET (Pmode, a0,
2856                           gen_rtx_LO_SUM (Pmode, pic_offset_table_rtx, loc)));
2857   insn = mips_expand_call (MIPS_CALL_NORMAL, v0, mips_tls_symbol,
2858                            const0_rtx, NULL_RTX, false);
2859   RTL_CONST_CALL_P (insn) = 1;
2860   use_reg (&CALL_INSN_FUNCTION_USAGE (insn), a0);
2861   insn = get_insns ();
2862
2863   end_sequence ();
2864
2865   return insn;
2866 }
2867
2868 /* Return a pseudo register that contains the current thread pointer.  */
2869
2870 static rtx
2871 mips_get_tp (void)
2872 {
2873   rtx tp;
2874
2875   tp = gen_reg_rtx (Pmode);
2876   if (Pmode == DImode)
2877     emit_insn (gen_tls_get_tp_di (tp));
2878   else
2879     emit_insn (gen_tls_get_tp_si (tp));
2880   return tp;
2881 }
2882
2883 /* Generate the code to access LOC, a thread-local SYMBOL_REF, and return
2884    its address.  The return value will be both a valid address and a valid
2885    SET_SRC (either a REG or a LO_SUM).  */
2886
2887 static rtx
2888 mips_legitimize_tls_address (rtx loc)
2889 {
2890   rtx dest, insn, v0, tp, tmp1, tmp2, eqv;
2891   enum tls_model model;
2892
2893   if (TARGET_MIPS16)
2894     {
2895       sorry ("MIPS16 TLS");
2896       return gen_reg_rtx (Pmode);
2897     }
2898
2899   model = SYMBOL_REF_TLS_MODEL (loc);
2900   /* Only TARGET_ABICALLS code can have more than one module; other
2901      code must be be static and should not use a GOT.  All TLS models
2902      reduce to local exec in this situation.  */
2903   if (!TARGET_ABICALLS)
2904     model = TLS_MODEL_LOCAL_EXEC;
2905
2906   switch (model)
2907     {
2908     case TLS_MODEL_GLOBAL_DYNAMIC:
2909       v0 = gen_rtx_REG (Pmode, GP_RETURN);
2910       insn = mips_call_tls_get_addr (loc, SYMBOL_TLSGD, v0);
2911       dest = gen_reg_rtx (Pmode);
2912       emit_libcall_block (insn, dest, v0, loc);
2913       break;
2914
2915     case TLS_MODEL_LOCAL_DYNAMIC:
2916       v0 = gen_rtx_REG (Pmode, GP_RETURN);
2917       insn = mips_call_tls_get_addr (loc, SYMBOL_TLSLDM, v0);
2918       tmp1 = gen_reg_rtx (Pmode);
2919
2920       /* Attach a unique REG_EQUIV, to allow the RTL optimizers to
2921          share the LDM result with other LD model accesses.  */
2922       eqv = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx),
2923                             UNSPEC_TLS_LDM);
2924       emit_libcall_block (insn, tmp1, v0, eqv);
2925
2926       tmp2 = mips_unspec_offset_high (NULL, tmp1, loc, SYMBOL_DTPREL);
2927       dest = gen_rtx_LO_SUM (Pmode, tmp2,
2928                              mips_unspec_address (loc, SYMBOL_DTPREL));
2929       break;
2930
2931     case TLS_MODEL_INITIAL_EXEC:
2932       tp = mips_get_tp ();
2933       tmp1 = gen_reg_rtx (Pmode);
2934       tmp2 = mips_unspec_address (loc, SYMBOL_GOTTPREL);
2935       if (Pmode == DImode)
2936         emit_insn (gen_load_gotdi (tmp1, pic_offset_table_rtx, tmp2));
2937       else
2938         emit_insn (gen_load_gotsi (tmp1, pic_offset_table_rtx, tmp2));
2939       dest = gen_reg_rtx (Pmode);
2940       emit_insn (gen_add3_insn (dest, tmp1, tp));
2941       break;
2942
2943     case TLS_MODEL_LOCAL_EXEC:
2944       tp = mips_get_tp ();
2945       tmp1 = mips_unspec_offset_high (NULL, tp, loc, SYMBOL_TPREL);
2946       dest = gen_rtx_LO_SUM (Pmode, tmp1,
2947                              mips_unspec_address (loc, SYMBOL_TPREL));
2948       break;
2949
2950     default:
2951       gcc_unreachable ();
2952     }
2953   return dest;
2954 }
2955 \f
2956 /* If X is not a valid address for mode MODE, force it into a register.  */
2957
2958 static rtx
2959 mips_force_address (rtx x, enum machine_mode mode)
2960 {
2961   if (!mips_legitimate_address_p (mode, x, false))
2962     x = force_reg (Pmode, x);
2963   return x;
2964 }
2965
2966 /* This function is used to implement LEGITIMIZE_ADDRESS.  If X can
2967    be legitimized in a way that the generic machinery might not expect,
2968    return a new address, otherwise return NULL.  MODE is the mode of
2969    the memory being accessed.  */
2970
2971 static rtx
2972 mips_legitimize_address (rtx x, rtx oldx ATTRIBUTE_UNUSED,
2973                          enum machine_mode mode)
2974 {
2975   rtx base, addr;
2976   HOST_WIDE_INT offset;
2977
2978   if (mips_tls_symbol_p (x))
2979     return mips_legitimize_tls_address (x);
2980
2981   /* See if the address can split into a high part and a LO_SUM.  */
2982   if (mips_split_symbol (NULL, x, mode, &addr))
2983     return mips_force_address (addr, mode);
2984
2985   /* Handle BASE + OFFSET using mips_add_offset.  */
2986   mips_split_plus (x, &base, &offset);
2987   if (offset != 0)
2988     {
2989       if (!mips_valid_base_register_p (base, mode, false))
2990         base = copy_to_mode_reg (Pmode, base);
2991       addr = mips_add_offset (NULL, base, offset);
2992       return mips_force_address (addr, mode);
2993     }
2994
2995   return x;
2996 }
2997
2998 /* Load VALUE into DEST.  TEMP is as for mips_force_temporary.  */
2999
3000 void
3001 mips_move_integer (rtx temp, rtx dest, unsigned HOST_WIDE_INT value)
3002 {
3003   struct mips_integer_op codes[MIPS_MAX_INTEGER_OPS];
3004   enum machine_mode mode;
3005   unsigned int i, num_ops;
3006   rtx x;
3007
3008   mode = GET_MODE (dest);
3009   num_ops = mips_build_integer (codes, value);
3010
3011   /* Apply each binary operation to X.  Invariant: X is a legitimate
3012      source operand for a SET pattern.  */
3013   x = GEN_INT (codes[0].value);
3014   for (i = 1; i < num_ops; i++)
3015     {
3016       if (!can_create_pseudo_p ())
3017         {
3018           emit_insn (gen_rtx_SET (VOIDmode, temp, x));
3019           x = temp;
3020         }
3021       else
3022         x = force_reg (mode, x);
3023       x = gen_rtx_fmt_ee (codes[i].code, mode, x, GEN_INT (codes[i].value));
3024     }
3025
3026   emit_insn (gen_rtx_SET (VOIDmode, dest, x));
3027 }
3028
3029 /* Subroutine of mips_legitimize_move.  Move constant SRC into register
3030    DEST given that SRC satisfies immediate_operand but doesn't satisfy
3031    move_operand.  */
3032
3033 static void
3034 mips_legitimize_const_move (enum machine_mode mode, rtx dest, rtx src)
3035 {
3036   rtx base, offset;
3037
3038   /* Split moves of big integers into smaller pieces.  */
3039   if (splittable_const_int_operand (src, mode))
3040     {
3041       mips_move_integer (dest, dest, INTVAL (src));
3042       return;
3043     }
3044
3045   /* Split moves of symbolic constants into high/low pairs.  */
3046   if (mips_split_symbol (dest, src, MAX_MACHINE_MODE, &src))
3047     {
3048       emit_insn (gen_rtx_SET (VOIDmode, dest, src));
3049       return;
3050     }
3051
3052   /* Generate the appropriate access sequences for TLS symbols.  */
3053   if (mips_tls_symbol_p (src))
3054     {
3055       mips_emit_move (dest, mips_legitimize_tls_address (src));
3056       return;
3057     }
3058
3059   /* If we have (const (plus symbol offset)), and that expression cannot
3060      be forced into memory, load the symbol first and add in the offset.
3061      In non-MIPS16 mode, prefer to do this even if the constant _can_ be
3062      forced into memory, as it usually produces better code.  */
3063   split_const (src, &base, &offset);
3064   if (offset != const0_rtx
3065       && (targetm.cannot_force_const_mem (src)
3066           || (!TARGET_MIPS16 && can_create_pseudo_p ())))
3067     {
3068       base = mips_force_temporary (dest, base);
3069       mips_emit_move (dest, mips_add_offset (NULL, base, INTVAL (offset)));
3070       return;
3071     }
3072
3073   src = force_const_mem (mode, src);
3074
3075   /* When using explicit relocs, constant pool references are sometimes
3076      not legitimate addresses.  */
3077   mips_split_symbol (dest, XEXP (src, 0), mode, &XEXP (src, 0));
3078   mips_emit_move (dest, src);
3079 }
3080
3081 /* If (set DEST SRC) is not a valid move instruction, emit an equivalent
3082    sequence that is valid.  */
3083
3084 bool
3085 mips_legitimize_move (enum machine_mode mode, rtx dest, rtx src)
3086 {
3087   if (!register_operand (dest, mode) && !reg_or_0_operand (src, mode))
3088     {
3089       mips_emit_move (dest, force_reg (mode, src));
3090       return true;
3091     }
3092
3093   /* We need to deal with constants that would be legitimate
3094      immediate_operands but aren't legitimate move_operands.  */
3095   if (CONSTANT_P (src) && !move_operand (src, mode))
3096     {
3097       mips_legitimize_const_move (mode, dest, src);
3098       set_unique_reg_note (get_last_insn (), REG_EQUAL, copy_rtx (src));
3099       return true;
3100     }
3101   return false;
3102 }
3103 \f
3104 /* Return true if value X in context CONTEXT is a small-data address
3105    that can be rewritten as a LO_SUM.  */
3106
3107 static bool
3108 mips_rewrite_small_data_p (rtx x, enum mips_symbol_context context)
3109 {
3110   enum mips_symbol_type symbol_type;
3111
3112   return (mips_lo_relocs[SYMBOL_GP_RELATIVE]
3113           && !mips_split_p[SYMBOL_GP_RELATIVE]
3114           && mips_symbolic_constant_p (x, context, &symbol_type)
3115           && symbol_type == SYMBOL_GP_RELATIVE);
3116 }
3117
3118 /* A for_each_rtx callback for mips_small_data_pattern_p.  DATA is the
3119    containing MEM, or null if none.  */
3120
3121 static int
3122 mips_small_data_pattern_1 (rtx *loc, void *data)
3123 {
3124   enum mips_symbol_context context;
3125
3126   if (GET_CODE (*loc) == LO_SUM)
3127     return -1;
3128
3129   if (MEM_P (*loc))
3130     {
3131       if (for_each_rtx (&XEXP (*loc, 0), mips_small_data_pattern_1, *loc))
3132         return 1;
3133       return -1;
3134     }
3135
3136   context = data ? SYMBOL_CONTEXT_MEM : SYMBOL_CONTEXT_LEA;
3137   return mips_rewrite_small_data_p (*loc, context);
3138 }
3139
3140 /* Return true if OP refers to small data symbols directly, not through
3141    a LO_SUM.  */
3142
3143 bool
3144 mips_small_data_pattern_p (rtx op)
3145 {
3146   return for_each_rtx (&op, mips_small_data_pattern_1, NULL);
3147 }
3148
3149 /* A for_each_rtx callback, used by mips_rewrite_small_data.
3150    DATA is the containing MEM, or null if none.  */
3151
3152 static int
3153 mips_rewrite_small_data_1 (rtx *loc, void *data)
3154 {
3155   enum mips_symbol_context context;
3156
3157   if (MEM_P (*loc))
3158     {
3159       for_each_rtx (&XEXP (*loc, 0), mips_rewrite_small_data_1, *loc);
3160       return -1;
3161     }
3162
3163   context = data ? SYMBOL_CONTEXT_MEM : SYMBOL_CONTEXT_LEA;
3164   if (mips_rewrite_small_data_p (*loc, context))
3165     *loc = gen_rtx_LO_SUM (Pmode, pic_offset_table_rtx, *loc);
3166
3167   if (GET_CODE (*loc) == LO_SUM)
3168     return -1;
3169
3170   return 0;
3171 }
3172
3173 /* Rewrite instruction pattern PATTERN so that it refers to small data
3174    using explicit relocations.  */
3175
3176 rtx
3177 mips_rewrite_small_data (rtx pattern)
3178 {
3179   pattern = copy_insn (pattern);
3180   for_each_rtx (&pattern, mips_rewrite_small_data_1, NULL);
3181   return pattern;
3182 }
3183 \f
3184 /* We need a lot of little routines to check the range of MIPS16 immediate
3185    operands.  */
3186
3187 static int
3188 m16_check_op (rtx op, int low, int high, int mask)
3189 {
3190   return (CONST_INT_P (op)
3191           && IN_RANGE (INTVAL (op), low, high)
3192           && (INTVAL (op) & mask) == 0);
3193 }
3194
3195 int
3196 m16_uimm3_b (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3197 {
3198   return m16_check_op (op, 0x1, 0x8, 0);
3199 }
3200
3201 int
3202 m16_simm4_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3203 {
3204   return m16_check_op (op, -0x8, 0x7, 0);
3205 }
3206
3207 int
3208 m16_nsimm4_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3209 {
3210   return m16_check_op (op, -0x7, 0x8, 0);
3211 }
3212
3213 int
3214 m16_simm5_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3215 {
3216   return m16_check_op (op, -0x10, 0xf, 0);
3217 }
3218
3219 int
3220 m16_nsimm5_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3221 {
3222   return m16_check_op (op, -0xf, 0x10, 0);
3223 }
3224
3225 int
3226 m16_uimm5_4 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3227 {
3228   return m16_check_op (op, -0x10 << 2, 0xf << 2, 3);
3229 }
3230
3231 int
3232 m16_nuimm5_4 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3233 {
3234   return m16_check_op (op, -0xf << 2, 0x10 << 2, 3);
3235 }
3236
3237 int
3238 m16_simm8_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3239 {
3240   return m16_check_op (op, -0x80, 0x7f, 0);
3241 }
3242
3243 int
3244 m16_nsimm8_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3245 {
3246   return m16_check_op (op, -0x7f, 0x80, 0);
3247 }
3248
3249 int
3250 m16_uimm8_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3251 {
3252   return m16_check_op (op, 0x0, 0xff, 0);
3253 }
3254
3255 int
3256 m16_nuimm8_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3257 {
3258   return m16_check_op (op, -0xff, 0x0, 0);
3259 }
3260
3261 int
3262 m16_uimm8_m1_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3263 {
3264   return m16_check_op (op, -0x1, 0xfe, 0);
3265 }
3266
3267 int
3268 m16_uimm8_4 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3269 {
3270   return m16_check_op (op, 0x0, 0xff << 2, 3);
3271 }
3272
3273 int
3274 m16_nuimm8_4 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3275 {
3276   return m16_check_op (op, -0xff << 2, 0x0, 3);
3277 }
3278
3279 int
3280 m16_simm8_8 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3281 {
3282   return m16_check_op (op, -0x80 << 3, 0x7f << 3, 7);
3283 }
3284
3285 int
3286 m16_nsimm8_8 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3287 {
3288   return m16_check_op (op, -0x7f << 3, 0x80 << 3, 7);
3289 }
3290 \f
3291 /* The cost of loading values from the constant pool.  It should be
3292    larger than the cost of any constant we want to synthesize inline.  */
3293 #define CONSTANT_POOL_COST COSTS_N_INSNS (TARGET_MIPS16 ? 4 : 8)
3294
3295 /* Return the cost of X when used as an operand to the MIPS16 instruction
3296    that implements CODE.  Return -1 if there is no such instruction, or if
3297    X is not a valid immediate operand for it.  */
3298
3299 static int
3300 mips16_constant_cost (int code, HOST_WIDE_INT x)
3301 {
3302   switch (code)
3303     {
3304     case ASHIFT:
3305     case ASHIFTRT:
3306     case LSHIFTRT:
3307       /* Shifts by between 1 and 8 bits (inclusive) are unextended,
3308          other shifts are extended.  The shift patterns truncate the shift
3309          count to the right size, so there are no out-of-range values.  */
3310       if (IN_RANGE (x, 1, 8))
3311         return 0;
3312       return COSTS_N_INSNS (1);
3313
3314     case PLUS:
3315       if (IN_RANGE (x, -128, 127))
3316         return 0;
3317       if (SMALL_OPERAND (x))
3318         return COSTS_N_INSNS (1);
3319       return -1;
3320
3321     case LEU:
3322       /* Like LE, but reject the always-true case.  */
3323       if (x == -1)
3324         return -1;
3325     case LE:
3326       /* We add 1 to the immediate and use SLT.  */
3327       x += 1;
3328     case XOR:
3329       /* We can use CMPI for an xor with an unsigned 16-bit X.  */
3330     case LT:
3331     case LTU:
3332       if (IN_RANGE (x, 0, 255))
3333         return 0;
3334       if (SMALL_OPERAND_UNSIGNED (x))
3335         return COSTS_N_INSNS (1);
3336       return -1;
3337
3338     case EQ:
3339     case NE:
3340       /* Equality comparisons with 0 are cheap.  */
3341       if (x == 0)
3342         return 0;
3343       return -1;
3344
3345     default:
3346       return -1;
3347     }
3348 }
3349
3350 /* Return true if there is a non-MIPS16 instruction that implements CODE
3351    and if that instruction accepts X as an immediate operand.  */
3352
3353 static int
3354 mips_immediate_operand_p (int code, HOST_WIDE_INT x)
3355 {
3356   switch (code)
3357     {
3358     case ASHIFT:
3359     case ASHIFTRT:
3360     case LSHIFTRT:
3361       /* All shift counts are truncated to a valid constant.  */
3362       return true;
3363
3364     case ROTATE:
3365     case ROTATERT:
3366       /* Likewise rotates, if the target supports rotates at all.  */
3367       return ISA_HAS_ROR;
3368
3369     case AND:
3370     case IOR:
3371     case XOR:
3372       /* These instructions take 16-bit unsigned immediates.  */
3373       return SMALL_OPERAND_UNSIGNED (x);
3374
3375     case PLUS:
3376     case LT:
3377     case LTU:
3378       /* These instructions take 16-bit signed immediates.  */
3379       return SMALL_OPERAND (x);
3380
3381     case EQ:
3382     case NE:
3383     case GT:
3384     case GTU:
3385       /* The "immediate" forms of these instructions are really
3386          implemented as comparisons with register 0.  */
3387       return x == 0;
3388
3389     case GE:
3390     case GEU:
3391       /* Likewise, meaning that the only valid immediate operand is 1.  */
3392       return x == 1;
3393
3394     case LE:
3395       /* We add 1 to the immediate and use SLT.  */
3396       return SMALL_OPERAND (x + 1);
3397
3398     case LEU:
3399       /* Likewise SLTU, but reject the always-true case.  */
3400       return SMALL_OPERAND (x + 1) && x + 1 != 0;
3401
3402     case SIGN_EXTRACT:
3403     case ZERO_EXTRACT:
3404       /* The bit position and size are immediate operands.  */
3405       return ISA_HAS_EXT_INS;
3406
3407     default:
3408       /* By default assume that $0 can be used for 0.  */
3409       return x == 0;
3410     }
3411 }
3412
3413 /* Return the cost of binary operation X, given that the instruction
3414    sequence for a word-sized or smaller operation has cost SINGLE_COST
3415    and that the sequence of a double-word operation has cost DOUBLE_COST.
3416    If SPEED is true, optimize for speed otherwise optimize for size.  */
3417
3418 static int
3419 mips_binary_cost (rtx x, int single_cost, int double_cost, bool speed)
3420 {
3421   int cost;
3422
3423   if (GET_MODE_SIZE (GET_MODE (x)) == UNITS_PER_WORD * 2)
3424     cost = double_cost;
3425   else
3426     cost = single_cost;
3427   return (cost
3428           + rtx_cost (XEXP (x, 0), SET, speed)
3429           + rtx_cost (XEXP (x, 1), GET_CODE (x), speed));
3430 }
3431
3432 /* Return the cost of floating-point multiplications of mode MODE.  */
3433
3434 static int
3435 mips_fp_mult_cost (enum machine_mode mode)
3436 {
3437   return mode == DFmode ? mips_cost->fp_mult_df : mips_cost->fp_mult_sf;
3438 }
3439
3440 /* Return the cost of floating-point divisions of mode MODE.  */
3441
3442 static int
3443 mips_fp_div_cost (enum machine_mode mode)
3444 {
3445   return mode == DFmode ? mips_cost->fp_div_df : mips_cost->fp_div_sf;
3446 }
3447
3448 /* Return the cost of sign-extending OP to mode MODE, not including the
3449    cost of OP itself.  */
3450
3451 static int
3452 mips_sign_extend_cost (enum machine_mode mode, rtx op)
3453 {
3454   if (MEM_P (op))
3455     /* Extended loads are as cheap as unextended ones.  */
3456     return 0;
3457
3458   if (TARGET_64BIT && mode == DImode && GET_MODE (op) == SImode)
3459     /* A sign extension from SImode to DImode in 64-bit mode is free.  */
3460     return 0;
3461
3462   if (ISA_HAS_SEB_SEH || GENERATE_MIPS16E)
3463     /* We can use SEB or SEH.  */
3464     return COSTS_N_INSNS (1);
3465
3466   /* We need to use a shift left and a shift right.  */
3467   return COSTS_N_INSNS (TARGET_MIPS16 ? 4 : 2);
3468 }
3469
3470 /* Return the cost of zero-extending OP to mode MODE, not including the