OSDN Git Service

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