ece4fec5413f7caca32c9a91ce6d3811b8a5f4b0
[platform/upstream/gcc.git] / gcc / config / mips / mips.c
1 /* Subroutines used for MIPS code generation.
2    Copyright (C) 1989-2013 Free Software Foundation, Inc.
3    Contributed by A. Lichnewsky, lich@inria.inria.fr.
4    Changes by Michael Meissner, meissner@osf.org.
5    64-bit r4000 support by Ian Lance Taylor, ian@cygnus.com, and
6    Brendan Eich, brendan@microunity.com.
7
8 This file is part of GCC.
9
10 GCC is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3, or (at your option)
13 any later version.
14
15 GCC is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3.  If not see
22 <http://www.gnu.org/licenses/>.  */
23
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "tm.h"
28 #include "rtl.h"
29 #include "regs.h"
30 #include "hard-reg-set.h"
31 #include "insn-config.h"
32 #include "conditions.h"
33 #include "insn-attr.h"
34 #include "recog.h"
35 #include "output.h"
36 #include "tree.h"
37 #include "function.h"
38 #include "expr.h"
39 #include "optabs.h"
40 #include "libfuncs.h"
41 #include "flags.h"
42 #include "reload.h"
43 #include "tm_p.h"
44 #include "ggc.h"
45 #include "gstab.h"
46 #include "hash-table.h"
47 #include "debug.h"
48 #include "target.h"
49 #include "target-def.h"
50 #include "common/common-target.h"
51 #include "langhooks.h"
52 #include "sched-int.h"
53 #include "gimplify.h"
54 #include "bitmap.h"
55 #include "diagnostic.h"
56 #include "target-globals.h"
57 #include "opts.h"
58 #include "tree-pass.h"
59 #include "context.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    microMIPS LWM and SWM support 12-bit offsets (from -0x800 to 0x7ff),
83    so we use a maximum of 0x7f0 for TARGET_MICROMIPS.
84
85    MIPS16e SAVE and RESTORE instructions can adjust the stack pointer by
86    up to 0x7f8 bytes and can usually save or restore all the registers
87    that we need to save or restore.  (Note that we can only use these
88    instructions for o32, for which the stack alignment is 8 bytes.)
89
90    We use a maximum gap of 0x100 or 0x400 for MIPS16 code when SAVE and
91    RESTORE are not available.  We can then use unextended instructions
92    to save and restore registers, and to allocate and deallocate the top
93    part of the frame.  */
94 #define MIPS_MAX_FIRST_STACK_STEP                                       \
95   (!TARGET_COMPRESSION ? 0x7ff0                                         \
96    : TARGET_MICROMIPS || GENERATE_MIPS16E_SAVE_RESTORE ? 0x7f8          \
97    : TARGET_64BIT ? 0x100 : 0x400)
98
99 /* True if INSN is a mips.md pattern or asm statement.  */
100 /* ???  This test exists through the compiler, perhaps it should be
101         moved to rtl.h.  */
102 #define USEFUL_INSN_P(INSN)                                             \
103   (NONDEBUG_INSN_P (INSN)                                               \
104    && GET_CODE (PATTERN (INSN)) != USE                                  \
105    && GET_CODE (PATTERN (INSN)) != CLOBBER)
106
107 /* If INSN is a delayed branch sequence, return the first instruction
108    in the sequence, otherwise return INSN itself.  */
109 #define SEQ_BEGIN(INSN)                                                 \
110   (INSN_P (INSN) && GET_CODE (PATTERN (INSN)) == SEQUENCE               \
111    ? XVECEXP (PATTERN (INSN), 0, 0)                                     \
112    : (INSN))
113
114 /* Likewise for the last instruction in a delayed branch sequence.  */
115 #define SEQ_END(INSN)                                                   \
116   (INSN_P (INSN) && GET_CODE (PATTERN (INSN)) == SEQUENCE               \
117    ? XVECEXP (PATTERN (INSN), 0, XVECLEN (PATTERN (INSN), 0) - 1)       \
118    : (INSN))
119
120 /* Execute the following loop body with SUBINSN set to each instruction
121    between SEQ_BEGIN (INSN) and SEQ_END (INSN) inclusive.  */
122 #define FOR_EACH_SUBINSN(SUBINSN, INSN)                                 \
123   for ((SUBINSN) = SEQ_BEGIN (INSN);                                    \
124        (SUBINSN) != NEXT_INSN (SEQ_END (INSN));                         \
125        (SUBINSN) = NEXT_INSN (SUBINSN))
126
127 /* True if bit BIT is set in VALUE.  */
128 #define BITSET_P(VALUE, BIT) (((VALUE) & (1 << (BIT))) != 0)
129
130 /* Return the opcode for a ptr_mode load of the form:
131
132        l[wd]    DEST, OFFSET(BASE).  */
133 #define MIPS_LOAD_PTR(DEST, OFFSET, BASE)       \
134   (((ptr_mode == DImode ? 0x37 : 0x23) << 26)   \
135    | ((BASE) << 21)                             \
136    | ((DEST) << 16)                             \
137    | (OFFSET))
138
139 /* Return the opcode to move register SRC into register DEST.  */
140 #define MIPS_MOVE(DEST, SRC)            \
141   ((TARGET_64BIT ? 0x2d : 0x21)         \
142    | ((DEST) << 11)                     \
143    | ((SRC) << 21))
144
145 /* Return the opcode for:
146
147        lui      DEST, VALUE.  */
148 #define MIPS_LUI(DEST, VALUE) \
149   ((0xf << 26) | ((DEST) << 16) | (VALUE))
150
151 /* Return the opcode to jump to register DEST.  */
152 #define MIPS_JR(DEST) \
153   (((DEST) << 21) | 0x8)
154
155 /* Return the opcode for:
156
157        bal     . + (1 + OFFSET) * 4.  */
158 #define MIPS_BAL(OFFSET) \
159   ((0x1 << 26) | (0x11 << 16) | (OFFSET))
160
161 /* Return the usual opcode for a nop.  */
162 #define MIPS_NOP 0
163
164 /* Classifies an address.
165
166    ADDRESS_REG
167        A natural register + offset address.  The register satisfies
168        mips_valid_base_register_p and the offset is a const_arith_operand.
169
170    ADDRESS_LO_SUM
171        A LO_SUM rtx.  The first operand is a valid base register and
172        the second operand is a symbolic address.
173
174    ADDRESS_CONST_INT
175        A signed 16-bit constant address.
176
177    ADDRESS_SYMBOLIC:
178        A constant symbolic address.  */
179 enum mips_address_type {
180   ADDRESS_REG,
181   ADDRESS_LO_SUM,
182   ADDRESS_CONST_INT,
183   ADDRESS_SYMBOLIC
184 };
185
186 /* Macros to create an enumeration identifier for a function prototype.  */
187 #define MIPS_FTYPE_NAME1(A, B) MIPS_##A##_FTYPE_##B
188 #define MIPS_FTYPE_NAME2(A, B, C) MIPS_##A##_FTYPE_##B##_##C
189 #define MIPS_FTYPE_NAME3(A, B, C, D) MIPS_##A##_FTYPE_##B##_##C##_##D
190 #define MIPS_FTYPE_NAME4(A, B, C, D, E) MIPS_##A##_FTYPE_##B##_##C##_##D##_##E
191
192 /* Classifies the prototype of a built-in function.  */
193 enum mips_function_type {
194 #define DEF_MIPS_FTYPE(NARGS, LIST) MIPS_FTYPE_NAME##NARGS LIST,
195 #include "config/mips/mips-ftypes.def"
196 #undef DEF_MIPS_FTYPE
197   MIPS_MAX_FTYPE_MAX
198 };
199
200 /* Specifies how a built-in function should be converted into rtl.  */
201 enum mips_builtin_type {
202   /* The function corresponds directly to an .md pattern.  The return
203      value is mapped to operand 0 and the arguments are mapped to
204      operands 1 and above.  */
205   MIPS_BUILTIN_DIRECT,
206
207   /* The function corresponds directly to an .md pattern.  There is no return
208      value and the arguments are mapped to operands 0 and above.  */
209   MIPS_BUILTIN_DIRECT_NO_TARGET,
210
211   /* The function corresponds to a comparison instruction followed by
212      a mips_cond_move_tf_ps pattern.  The first two arguments are the
213      values to compare and the second two arguments are the vector
214      operands for the movt.ps or movf.ps instruction (in assembly order).  */
215   MIPS_BUILTIN_MOVF,
216   MIPS_BUILTIN_MOVT,
217
218   /* The function corresponds to a V2SF comparison instruction.  Operand 0
219      of this instruction is the result of the comparison, which has mode
220      CCV2 or CCV4.  The function arguments are mapped to operands 1 and
221      above.  The function's return value is an SImode boolean that is
222      true under the following conditions:
223
224      MIPS_BUILTIN_CMP_ANY: one of the registers is true
225      MIPS_BUILTIN_CMP_ALL: all of the registers are true
226      MIPS_BUILTIN_CMP_LOWER: the first register is true
227      MIPS_BUILTIN_CMP_UPPER: the second register is true.  */
228   MIPS_BUILTIN_CMP_ANY,
229   MIPS_BUILTIN_CMP_ALL,
230   MIPS_BUILTIN_CMP_UPPER,
231   MIPS_BUILTIN_CMP_LOWER,
232
233   /* As above, but the instruction only sets a single $fcc register.  */
234   MIPS_BUILTIN_CMP_SINGLE,
235
236   /* For generating bposge32 branch instructions in MIPS32 DSP ASE.  */
237   MIPS_BUILTIN_BPOSGE32
238 };
239
240 /* Invoke MACRO (COND) for each C.cond.fmt condition.  */
241 #define MIPS_FP_CONDITIONS(MACRO) \
242   MACRO (f),    \
243   MACRO (un),   \
244   MACRO (eq),   \
245   MACRO (ueq),  \
246   MACRO (olt),  \
247   MACRO (ult),  \
248   MACRO (ole),  \
249   MACRO (ule),  \
250   MACRO (sf),   \
251   MACRO (ngle), \
252   MACRO (seq),  \
253   MACRO (ngl),  \
254   MACRO (lt),   \
255   MACRO (nge),  \
256   MACRO (le),   \
257   MACRO (ngt)
258
259 /* Enumerates the codes above as MIPS_FP_COND_<X>.  */
260 #define DECLARE_MIPS_COND(X) MIPS_FP_COND_ ## X
261 enum mips_fp_condition {
262   MIPS_FP_CONDITIONS (DECLARE_MIPS_COND)
263 };
264
265 /* Index X provides the string representation of MIPS_FP_COND_<X>.  */
266 #define STRINGIFY(X) #X
267 static const char *const mips_fp_conditions[] = {
268   MIPS_FP_CONDITIONS (STRINGIFY)
269 };
270
271 /* Tuning information that is automatically derived from other sources
272    (such as the scheduler).  */
273 static struct {
274   /* The architecture and tuning settings that this structure describes.  */
275   enum processor arch;
276   enum processor tune;
277
278   /* True if this structure describes MIPS16 settings.  */
279   bool mips16_p;
280
281   /* True if the structure has been initialized.  */
282   bool initialized_p;
283
284   /* True if "MULT $0, $0" is preferable to "MTLO $0; MTHI $0"
285      when optimizing for speed.  */
286   bool fast_mult_zero_zero_p;
287 } mips_tuning_info;
288
289 /* Information about a function's frame layout.  */
290 struct GTY(())  mips_frame_info {
291   /* The size of the frame in bytes.  */
292   HOST_WIDE_INT total_size;
293
294   /* The number of bytes allocated to variables.  */
295   HOST_WIDE_INT var_size;
296
297   /* The number of bytes allocated to outgoing function arguments.  */
298   HOST_WIDE_INT args_size;
299
300   /* The number of bytes allocated to the .cprestore slot, or 0 if there
301      is no such slot.  */
302   HOST_WIDE_INT cprestore_size;
303
304   /* Bit X is set if the function saves or restores GPR X.  */
305   unsigned int mask;
306
307   /* Likewise FPR X.  */
308   unsigned int fmask;
309
310   /* Likewise doubleword accumulator X ($acX).  */
311   unsigned int acc_mask;
312
313   /* The number of GPRs, FPRs, doubleword accumulators and COP0
314      registers saved.  */
315   unsigned int num_gp;
316   unsigned int num_fp;
317   unsigned int num_acc;
318   unsigned int num_cop0_regs;
319
320   /* The offset of the topmost GPR, FPR, accumulator and COP0-register
321      save slots from the top of the frame, or zero if no such slots are
322      needed.  */
323   HOST_WIDE_INT gp_save_offset;
324   HOST_WIDE_INT fp_save_offset;
325   HOST_WIDE_INT acc_save_offset;
326   HOST_WIDE_INT cop0_save_offset;
327
328   /* Likewise, but giving offsets from the bottom of the frame.  */
329   HOST_WIDE_INT gp_sp_offset;
330   HOST_WIDE_INT fp_sp_offset;
331   HOST_WIDE_INT acc_sp_offset;
332   HOST_WIDE_INT cop0_sp_offset;
333
334   /* Similar, but the value passed to _mcount.  */
335   HOST_WIDE_INT ra_fp_offset;
336
337   /* The offset of arg_pointer_rtx from the bottom of the frame.  */
338   HOST_WIDE_INT arg_pointer_offset;
339
340   /* The offset of hard_frame_pointer_rtx from the bottom of the frame.  */
341   HOST_WIDE_INT hard_frame_pointer_offset;
342 };
343
344 struct GTY(())  machine_function {
345   /* The next floating-point condition-code register to allocate
346      for ISA_HAS_8CC targets, relative to ST_REG_FIRST.  */
347   unsigned int next_fcc;
348
349   /* The register returned by mips16_gp_pseudo_reg; see there for details.  */
350   rtx mips16_gp_pseudo_rtx;
351
352   /* The number of extra stack bytes taken up by register varargs.
353      This area is allocated by the callee at the very top of the frame.  */
354   int varargs_size;
355
356   /* The current frame information, calculated by mips_compute_frame_info.  */
357   struct mips_frame_info frame;
358
359   /* The register to use as the function's global pointer, or INVALID_REGNUM
360      if the function doesn't need one.  */
361   unsigned int global_pointer;
362
363   /* How many instructions it takes to load a label into $AT, or 0 if
364      this property hasn't yet been calculated.  */
365   unsigned int load_label_num_insns;
366
367   /* True if mips_adjust_insn_length should ignore an instruction's
368      hazard attribute.  */
369   bool ignore_hazard_length_p;
370
371   /* True if the whole function is suitable for .set noreorder and
372      .set nomacro.  */
373   bool all_noreorder_p;
374
375   /* True if the function has "inflexible" and "flexible" references
376      to the global pointer.  See mips_cfun_has_inflexible_gp_ref_p
377      and mips_cfun_has_flexible_gp_ref_p for details.  */
378   bool has_inflexible_gp_insn_p;
379   bool has_flexible_gp_insn_p;
380
381   /* True if the function's prologue must load the global pointer
382      value into pic_offset_table_rtx and store the same value in
383      the function's cprestore slot (if any).  Even if this value
384      is currently false, we may decide to set it to true later;
385      see mips_must_initialize_gp_p () for details.  */
386   bool must_initialize_gp_p;
387
388   /* True if the current function must restore $gp after any potential
389      clobber.  This value is only meaningful during the first post-epilogue
390      split_insns pass; see mips_must_initialize_gp_p () for details.  */
391   bool must_restore_gp_when_clobbered_p;
392
393   /* True if this is an interrupt handler.  */
394   bool interrupt_handler_p;
395
396   /* True if this is an interrupt handler that uses shadow registers.  */
397   bool use_shadow_register_set_p;
398
399   /* True if this is an interrupt handler that should keep interrupts
400      masked.  */
401   bool keep_interrupts_masked_p;
402
403   /* True if this is an interrupt handler that should use DERET
404      instead of ERET.  */
405   bool use_debug_exception_return_p;
406 };
407
408 /* Information about a single argument.  */
409 struct mips_arg_info {
410   /* True if the argument is passed in a floating-point register, or
411      would have been if we hadn't run out of registers.  */
412   bool fpr_p;
413
414   /* The number of words passed in registers, rounded up.  */
415   unsigned int reg_words;
416
417   /* For EABI, the offset of the first register from GP_ARG_FIRST or
418      FP_ARG_FIRST.  For other ABIs, the offset of the first register from
419      the start of the ABI's argument structure (see the CUMULATIVE_ARGS
420      comment for details).
421
422      The value is MAX_ARGS_IN_REGISTERS if the argument is passed entirely
423      on the stack.  */
424   unsigned int reg_offset;
425
426   /* The number of words that must be passed on the stack, rounded up.  */
427   unsigned int stack_words;
428
429   /* The offset from the start of the stack overflow area of the argument's
430      first stack word.  Only meaningful when STACK_WORDS is nonzero.  */
431   unsigned int stack_offset;
432 };
433
434 /* Information about an address described by mips_address_type.
435
436    ADDRESS_CONST_INT
437        No fields are used.
438
439    ADDRESS_REG
440        REG is the base register and OFFSET is the constant offset.
441
442    ADDRESS_LO_SUM
443        REG and OFFSET are the operands to the LO_SUM and SYMBOL_TYPE
444        is the type of symbol it references.
445
446    ADDRESS_SYMBOLIC
447        SYMBOL_TYPE is the type of symbol that the address references.  */
448 struct mips_address_info {
449   enum mips_address_type type;
450   rtx reg;
451   rtx offset;
452   enum mips_symbol_type symbol_type;
453 };
454
455 /* One stage in a constant building sequence.  These sequences have
456    the form:
457
458         A = VALUE[0]
459         A = A CODE[1] VALUE[1]
460         A = A CODE[2] VALUE[2]
461         ...
462
463    where A is an accumulator, each CODE[i] is a binary rtl operation
464    and each VALUE[i] is a constant integer.  CODE[0] is undefined.  */
465 struct mips_integer_op {
466   enum rtx_code code;
467   unsigned HOST_WIDE_INT value;
468 };
469
470 /* The largest number of operations needed to load an integer constant.
471    The worst accepted case for 64-bit constants is LUI,ORI,SLL,ORI,SLL,ORI.
472    When the lowest bit is clear, we can try, but reject a sequence with
473    an extra SLL at the end.  */
474 #define MIPS_MAX_INTEGER_OPS 7
475
476 /* Information about a MIPS16e SAVE or RESTORE instruction.  */
477 struct mips16e_save_restore_info {
478   /* The number of argument registers saved by a SAVE instruction.
479      0 for RESTORE instructions.  */
480   unsigned int nargs;
481
482   /* Bit X is set if the instruction saves or restores GPR X.  */
483   unsigned int mask;
484
485   /* The total number of bytes to allocate.  */
486   HOST_WIDE_INT size;
487 };
488
489 /* Costs of various operations on the different architectures.  */
490
491 struct mips_rtx_cost_data
492 {
493   unsigned short fp_add;
494   unsigned short fp_mult_sf;
495   unsigned short fp_mult_df;
496   unsigned short fp_div_sf;
497   unsigned short fp_div_df;
498   unsigned short int_mult_si;
499   unsigned short int_mult_di;
500   unsigned short int_div_si;
501   unsigned short int_div_di;
502   unsigned short branch_cost;
503   unsigned short memory_latency;
504 };
505
506 /* Global variables for machine-dependent things.  */
507
508 /* The -G setting, or the configuration's default small-data limit if
509    no -G option is given.  */
510 static unsigned int mips_small_data_threshold;
511
512 /* The number of file directives written by mips_output_filename.  */
513 int num_source_filenames;
514
515 /* The name that appeared in the last .file directive written by
516    mips_output_filename, or "" if mips_output_filename hasn't
517    written anything yet.  */
518 const char *current_function_file = "";
519
520 /* Arrays that map GCC register numbers to debugger register numbers.  */
521 int mips_dbx_regno[FIRST_PSEUDO_REGISTER];
522 int mips_dwarf_regno[FIRST_PSEUDO_REGISTER];
523
524 /* Information about the current function's epilogue, used only while
525    expanding it.  */
526 static struct {
527   /* A list of queued REG_CFA_RESTORE notes.  */
528   rtx cfa_restores;
529
530   /* The CFA is currently defined as CFA_REG + CFA_OFFSET.  */
531   rtx cfa_reg;
532   HOST_WIDE_INT cfa_offset;
533
534   /* The offset of the CFA from the stack pointer while restoring
535      registers.  */
536   HOST_WIDE_INT cfa_restore_sp_offset;
537 } mips_epilogue;
538
539 /* The nesting depth of the PRINT_OPERAND '%(', '%<' and '%[' constructs.  */
540 struct mips_asm_switch mips_noreorder = { "reorder", 0 };
541 struct mips_asm_switch mips_nomacro = { "macro", 0 };
542 struct mips_asm_switch mips_noat = { "at", 0 };
543
544 /* True if we're writing out a branch-likely instruction rather than a
545    normal branch.  */
546 static bool mips_branch_likely;
547
548 /* The current instruction-set architecture.  */
549 enum processor mips_arch;
550 const struct mips_cpu_info *mips_arch_info;
551
552 /* The processor that we should tune the code for.  */
553 enum processor mips_tune;
554 const struct mips_cpu_info *mips_tune_info;
555
556 /* The ISA level associated with mips_arch.  */
557 int mips_isa;
558
559 /* The architecture selected by -mipsN, or null if -mipsN wasn't used.  */
560 static const struct mips_cpu_info *mips_isa_option_info;
561
562 /* Which cost information to use.  */
563 static const struct mips_rtx_cost_data *mips_cost;
564
565 /* The ambient target flags, excluding MASK_MIPS16.  */
566 static int mips_base_target_flags;
567
568 /* The default compression mode.  */
569 unsigned int mips_base_compression_flags;
570
571 /* The ambient values of other global variables.  */
572 static int mips_base_schedule_insns; /* flag_schedule_insns */
573 static int mips_base_reorder_blocks_and_partition; /* flag_reorder... */
574 static int mips_base_move_loop_invariants; /* flag_move_loop_invariants */
575 static int mips_base_align_loops; /* align_loops */
576 static int mips_base_align_jumps; /* align_jumps */
577 static int mips_base_align_functions; /* align_functions */
578
579 /* Index [M][R] is true if register R is allowed to hold a value of mode M.  */
580 bool mips_hard_regno_mode_ok[(int) MAX_MACHINE_MODE][FIRST_PSEUDO_REGISTER];
581
582 /* Index C is true if character C is a valid PRINT_OPERAND punctation
583    character.  */
584 static bool mips_print_operand_punct[256];
585
586 static GTY (()) int mips_output_filename_first_time = 1;
587
588 /* mips_split_p[X] is true if symbols of type X can be split by
589    mips_split_symbol.  */
590 bool mips_split_p[NUM_SYMBOL_TYPES];
591
592 /* mips_split_hi_p[X] is true if the high parts of symbols of type X
593    can be split by mips_split_symbol.  */
594 bool mips_split_hi_p[NUM_SYMBOL_TYPES];
595
596 /* mips_use_pcrel_pool_p[X] is true if symbols of type X should be
597    forced into a PC-relative constant pool.  */
598 bool mips_use_pcrel_pool_p[NUM_SYMBOL_TYPES];
599
600 /* mips_lo_relocs[X] is the relocation to use when a symbol of type X
601    appears in a LO_SUM.  It can be null if such LO_SUMs aren't valid or
602    if they are matched by a special .md file pattern.  */
603 const char *mips_lo_relocs[NUM_SYMBOL_TYPES];
604
605 /* Likewise for HIGHs.  */
606 const char *mips_hi_relocs[NUM_SYMBOL_TYPES];
607
608 /* Target state for MIPS16.  */
609 struct target_globals *mips16_globals;
610
611 /* Cached value of can_issue_more. This is cached in mips_variable_issue hook
612    and returned from mips_sched_reorder2.  */
613 static int cached_can_issue_more;
614
615 /* True if the output uses __mips16_rdhwr.  */
616 static bool mips_need_mips16_rdhwr_p;
617
618 /* Index R is the smallest register class that contains register R.  */
619 const enum reg_class mips_regno_to_class[FIRST_PSEUDO_REGISTER] = {
620   LEA_REGS,     LEA_REGS,       M16_REGS,       V1_REG,
621   M16_REGS,     M16_REGS,       M16_REGS,       M16_REGS,
622   LEA_REGS,     LEA_REGS,       LEA_REGS,       LEA_REGS,
623   LEA_REGS,     LEA_REGS,       LEA_REGS,       LEA_REGS,
624   M16_REGS,     M16_REGS,       LEA_REGS,       LEA_REGS,
625   LEA_REGS,     LEA_REGS,       LEA_REGS,       LEA_REGS,
626   T_REG,        PIC_FN_ADDR_REG, LEA_REGS,      LEA_REGS,
627   LEA_REGS,     LEA_REGS,       LEA_REGS,       LEA_REGS,
628   FP_REGS,      FP_REGS,        FP_REGS,        FP_REGS,
629   FP_REGS,      FP_REGS,        FP_REGS,        FP_REGS,
630   FP_REGS,      FP_REGS,        FP_REGS,        FP_REGS,
631   FP_REGS,      FP_REGS,        FP_REGS,        FP_REGS,
632   FP_REGS,      FP_REGS,        FP_REGS,        FP_REGS,
633   FP_REGS,      FP_REGS,        FP_REGS,        FP_REGS,
634   FP_REGS,      FP_REGS,        FP_REGS,        FP_REGS,
635   FP_REGS,      FP_REGS,        FP_REGS,        FP_REGS,
636   MD0_REG,      MD1_REG,        NO_REGS,        ST_REGS,
637   ST_REGS,      ST_REGS,        ST_REGS,        ST_REGS,
638   ST_REGS,      ST_REGS,        ST_REGS,        NO_REGS,
639   NO_REGS,      FRAME_REGS,     FRAME_REGS,     NO_REGS,
640   COP0_REGS,    COP0_REGS,      COP0_REGS,      COP0_REGS,
641   COP0_REGS,    COP0_REGS,      COP0_REGS,      COP0_REGS,
642   COP0_REGS,    COP0_REGS,      COP0_REGS,      COP0_REGS,
643   COP0_REGS,    COP0_REGS,      COP0_REGS,      COP0_REGS,
644   COP0_REGS,    COP0_REGS,      COP0_REGS,      COP0_REGS,
645   COP0_REGS,    COP0_REGS,      COP0_REGS,      COP0_REGS,
646   COP0_REGS,    COP0_REGS,      COP0_REGS,      COP0_REGS,
647   COP0_REGS,    COP0_REGS,      COP0_REGS,      COP0_REGS,
648   COP2_REGS,    COP2_REGS,      COP2_REGS,      COP2_REGS,
649   COP2_REGS,    COP2_REGS,      COP2_REGS,      COP2_REGS,
650   COP2_REGS,    COP2_REGS,      COP2_REGS,      COP2_REGS,
651   COP2_REGS,    COP2_REGS,      COP2_REGS,      COP2_REGS,
652   COP2_REGS,    COP2_REGS,      COP2_REGS,      COP2_REGS,
653   COP2_REGS,    COP2_REGS,      COP2_REGS,      COP2_REGS,
654   COP2_REGS,    COP2_REGS,      COP2_REGS,      COP2_REGS,
655   COP2_REGS,    COP2_REGS,      COP2_REGS,      COP2_REGS,
656   COP3_REGS,    COP3_REGS,      COP3_REGS,      COP3_REGS,
657   COP3_REGS,    COP3_REGS,      COP3_REGS,      COP3_REGS,
658   COP3_REGS,    COP3_REGS,      COP3_REGS,      COP3_REGS,
659   COP3_REGS,    COP3_REGS,      COP3_REGS,      COP3_REGS,
660   COP3_REGS,    COP3_REGS,      COP3_REGS,      COP3_REGS,
661   COP3_REGS,    COP3_REGS,      COP3_REGS,      COP3_REGS,
662   COP3_REGS,    COP3_REGS,      COP3_REGS,      COP3_REGS,
663   COP3_REGS,    COP3_REGS,      COP3_REGS,      COP3_REGS,
664   DSP_ACC_REGS, DSP_ACC_REGS,   DSP_ACC_REGS,   DSP_ACC_REGS,
665   DSP_ACC_REGS, DSP_ACC_REGS,   ALL_REGS,       ALL_REGS,
666   ALL_REGS,     ALL_REGS,       ALL_REGS,       ALL_REGS
667 };
668
669 /* The value of TARGET_ATTRIBUTE_TABLE.  */
670 static const struct attribute_spec mips_attribute_table[] = {
671   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
672        om_diagnostic } */
673   { "long_call",   0, 0, false, true,  true,  NULL, false },
674   { "far",         0, 0, false, true,  true,  NULL, false },
675   { "near",        0, 0, false, true,  true,  NULL, false },
676   /* We would really like to treat "mips16" and "nomips16" as type
677      attributes, but GCC doesn't provide the hooks we need to support
678      the right conversion rules.  As declaration attributes, they affect
679      code generation but don't carry other semantics.  */
680   { "mips16",      0, 0, true,  false, false, NULL, false },
681   { "nomips16",    0, 0, true,  false, false, NULL, false },
682   { "micromips",   0, 0, true,  false, false, NULL, false },
683   { "nomicromips", 0, 0, true,  false, false, NULL, false },
684   { "nocompression", 0, 0, true,  false, false, NULL, false },
685   /* Allow functions to be specified as interrupt handlers */
686   { "interrupt",   0, 0, false, true,  true, NULL, false },
687   { "use_shadow_register_set",  0, 0, false, true,  true, NULL, false },
688   { "keep_interrupts_masked",   0, 0, false, true,  true, NULL, false },
689   { "use_debug_exception_return", 0, 0, false, true,  true, NULL, false },
690   { NULL,          0, 0, false, false, false, NULL, false }
691 };
692 \f
693 /* A table describing all the processors GCC knows about; see
694    mips-cpus.def for details.  */
695 static const struct mips_cpu_info mips_cpu_info_table[] = {
696 #define MIPS_CPU(NAME, CPU, ISA, FLAGS) \
697   { NAME, CPU, ISA, FLAGS },
698 #include "mips-cpus.def"
699 #undef MIPS_CPU
700 };
701
702 /* Default costs.  If these are used for a processor we should look
703    up the actual costs.  */
704 #define DEFAULT_COSTS COSTS_N_INSNS (6),  /* fp_add */       \
705                       COSTS_N_INSNS (7),  /* fp_mult_sf */   \
706                       COSTS_N_INSNS (8),  /* fp_mult_df */   \
707                       COSTS_N_INSNS (23), /* fp_div_sf */    \
708                       COSTS_N_INSNS (36), /* fp_div_df */    \
709                       COSTS_N_INSNS (10), /* int_mult_si */  \
710                       COSTS_N_INSNS (10), /* int_mult_di */  \
711                       COSTS_N_INSNS (69), /* int_div_si */   \
712                       COSTS_N_INSNS (69), /* int_div_di */   \
713                                        2, /* branch_cost */  \
714                                        4  /* memory_latency */
715
716 /* Floating-point costs for processors without an FPU.  Just assume that
717    all floating-point libcalls are very expensive.  */
718 #define SOFT_FP_COSTS COSTS_N_INSNS (256), /* fp_add */       \
719                       COSTS_N_INSNS (256), /* fp_mult_sf */   \
720                       COSTS_N_INSNS (256), /* fp_mult_df */   \
721                       COSTS_N_INSNS (256), /* fp_div_sf */    \
722                       COSTS_N_INSNS (256)  /* fp_div_df */
723
724 /* Costs to use when optimizing for size.  */
725 static const struct mips_rtx_cost_data mips_rtx_cost_optimize_size = {
726   COSTS_N_INSNS (1),            /* fp_add */
727   COSTS_N_INSNS (1),            /* fp_mult_sf */
728   COSTS_N_INSNS (1),            /* fp_mult_df */
729   COSTS_N_INSNS (1),            /* fp_div_sf */
730   COSTS_N_INSNS (1),            /* fp_div_df */
731   COSTS_N_INSNS (1),            /* int_mult_si */
732   COSTS_N_INSNS (1),            /* int_mult_di */
733   COSTS_N_INSNS (1),            /* int_div_si */
734   COSTS_N_INSNS (1),            /* int_div_di */
735                    2,           /* branch_cost */
736                    4            /* memory_latency */
737 };
738
739 /* Costs to use when optimizing for speed, indexed by processor.  */
740 static const struct mips_rtx_cost_data
741   mips_rtx_cost_data[NUM_PROCESSOR_VALUES] = {
742   { /* R3000 */
743     COSTS_N_INSNS (2),            /* fp_add */
744     COSTS_N_INSNS (4),            /* fp_mult_sf */
745     COSTS_N_INSNS (5),            /* fp_mult_df */
746     COSTS_N_INSNS (12),           /* fp_div_sf */
747     COSTS_N_INSNS (19),           /* fp_div_df */
748     COSTS_N_INSNS (12),           /* int_mult_si */
749     COSTS_N_INSNS (12),           /* int_mult_di */
750     COSTS_N_INSNS (35),           /* int_div_si */
751     COSTS_N_INSNS (35),           /* int_div_di */
752                      1,           /* branch_cost */
753                      4            /* memory_latency */
754   },
755   { /* 4KC */
756     SOFT_FP_COSTS,
757     COSTS_N_INSNS (6),            /* int_mult_si */
758     COSTS_N_INSNS (6),            /* int_mult_di */
759     COSTS_N_INSNS (36),           /* int_div_si */
760     COSTS_N_INSNS (36),           /* int_div_di */
761                      1,           /* branch_cost */
762                      4            /* memory_latency */
763   },
764   { /* 4KP */
765     SOFT_FP_COSTS,
766     COSTS_N_INSNS (36),           /* int_mult_si */
767     COSTS_N_INSNS (36),           /* int_mult_di */
768     COSTS_N_INSNS (37),           /* int_div_si */
769     COSTS_N_INSNS (37),           /* int_div_di */
770                      1,           /* branch_cost */
771                      4            /* memory_latency */
772   },
773   { /* 5KC */
774     SOFT_FP_COSTS,
775     COSTS_N_INSNS (4),            /* int_mult_si */
776     COSTS_N_INSNS (11),           /* int_mult_di */
777     COSTS_N_INSNS (36),           /* int_div_si */
778     COSTS_N_INSNS (68),           /* int_div_di */
779                      1,           /* branch_cost */
780                      4            /* memory_latency */
781   },
782   { /* 5KF */
783     COSTS_N_INSNS (4),            /* fp_add */
784     COSTS_N_INSNS (4),            /* fp_mult_sf */
785     COSTS_N_INSNS (5),            /* fp_mult_df */
786     COSTS_N_INSNS (17),           /* fp_div_sf */
787     COSTS_N_INSNS (32),           /* fp_div_df */
788     COSTS_N_INSNS (4),            /* int_mult_si */
789     COSTS_N_INSNS (11),           /* int_mult_di */
790     COSTS_N_INSNS (36),           /* int_div_si */
791     COSTS_N_INSNS (68),           /* int_div_di */
792                      1,           /* branch_cost */
793                      4            /* memory_latency */
794   },
795   { /* 20KC */
796     COSTS_N_INSNS (4),            /* fp_add */
797     COSTS_N_INSNS (4),            /* fp_mult_sf */
798     COSTS_N_INSNS (5),            /* fp_mult_df */
799     COSTS_N_INSNS (17),           /* fp_div_sf */
800     COSTS_N_INSNS (32),           /* fp_div_df */
801     COSTS_N_INSNS (4),            /* int_mult_si */
802     COSTS_N_INSNS (7),            /* int_mult_di */
803     COSTS_N_INSNS (42),           /* int_div_si */
804     COSTS_N_INSNS (72),           /* int_div_di */
805                      1,           /* branch_cost */
806                      4            /* memory_latency */
807   },
808   { /* 24KC */
809     SOFT_FP_COSTS,
810     COSTS_N_INSNS (5),            /* int_mult_si */
811     COSTS_N_INSNS (5),            /* int_mult_di */
812     COSTS_N_INSNS (41),           /* int_div_si */
813     COSTS_N_INSNS (41),           /* int_div_di */
814                      1,           /* branch_cost */
815                      4            /* memory_latency */
816   },
817   { /* 24KF2_1 */
818     COSTS_N_INSNS (8),            /* fp_add */
819     COSTS_N_INSNS (8),            /* fp_mult_sf */
820     COSTS_N_INSNS (10),           /* fp_mult_df */
821     COSTS_N_INSNS (34),           /* fp_div_sf */
822     COSTS_N_INSNS (64),           /* fp_div_df */
823     COSTS_N_INSNS (5),            /* int_mult_si */
824     COSTS_N_INSNS (5),            /* int_mult_di */
825     COSTS_N_INSNS (41),           /* int_div_si */
826     COSTS_N_INSNS (41),           /* int_div_di */
827                      1,           /* branch_cost */
828                      4            /* memory_latency */
829   },
830   { /* 24KF1_1 */
831     COSTS_N_INSNS (4),            /* fp_add */
832     COSTS_N_INSNS (4),            /* fp_mult_sf */
833     COSTS_N_INSNS (5),            /* fp_mult_df */
834     COSTS_N_INSNS (17),           /* fp_div_sf */
835     COSTS_N_INSNS (32),           /* fp_div_df */
836     COSTS_N_INSNS (5),            /* int_mult_si */
837     COSTS_N_INSNS (5),            /* int_mult_di */
838     COSTS_N_INSNS (41),           /* int_div_si */
839     COSTS_N_INSNS (41),           /* int_div_di */
840                      1,           /* branch_cost */
841                      4            /* memory_latency */
842   },
843   { /* 74KC */
844     SOFT_FP_COSTS,
845     COSTS_N_INSNS (5),            /* int_mult_si */
846     COSTS_N_INSNS (5),            /* int_mult_di */
847     COSTS_N_INSNS (41),           /* int_div_si */
848     COSTS_N_INSNS (41),           /* int_div_di */
849                      1,           /* branch_cost */
850                      4            /* memory_latency */
851   },
852   { /* 74KF2_1 */
853     COSTS_N_INSNS (8),            /* fp_add */
854     COSTS_N_INSNS (8),            /* fp_mult_sf */
855     COSTS_N_INSNS (10),           /* fp_mult_df */
856     COSTS_N_INSNS (34),           /* fp_div_sf */
857     COSTS_N_INSNS (64),           /* fp_div_df */
858     COSTS_N_INSNS (5),            /* int_mult_si */
859     COSTS_N_INSNS (5),            /* int_mult_di */
860     COSTS_N_INSNS (41),           /* int_div_si */
861     COSTS_N_INSNS (41),           /* int_div_di */
862                      1,           /* branch_cost */
863                      4            /* memory_latency */
864   },
865   { /* 74KF1_1 */
866     COSTS_N_INSNS (4),            /* fp_add */
867     COSTS_N_INSNS (4),            /* fp_mult_sf */
868     COSTS_N_INSNS (5),            /* fp_mult_df */
869     COSTS_N_INSNS (17),           /* fp_div_sf */
870     COSTS_N_INSNS (32),           /* fp_div_df */
871     COSTS_N_INSNS (5),            /* int_mult_si */
872     COSTS_N_INSNS (5),            /* int_mult_di */
873     COSTS_N_INSNS (41),           /* int_div_si */
874     COSTS_N_INSNS (41),           /* int_div_di */
875                      1,           /* branch_cost */
876                      4            /* memory_latency */
877   },
878   { /* 74KF3_2 */
879     COSTS_N_INSNS (6),            /* fp_add */
880     COSTS_N_INSNS (6),            /* fp_mult_sf */
881     COSTS_N_INSNS (7),            /* fp_mult_df */
882     COSTS_N_INSNS (25),           /* fp_div_sf */
883     COSTS_N_INSNS (48),           /* fp_div_df */
884     COSTS_N_INSNS (5),            /* int_mult_si */
885     COSTS_N_INSNS (5),            /* int_mult_di */
886     COSTS_N_INSNS (41),           /* int_div_si */
887     COSTS_N_INSNS (41),           /* int_div_di */
888                      1,           /* branch_cost */
889                      4            /* memory_latency */
890   },
891   { /* Loongson-2E */
892     DEFAULT_COSTS
893   },
894   { /* Loongson-2F */
895     DEFAULT_COSTS
896   },
897   { /* Loongson-3A */
898     DEFAULT_COSTS
899   },
900   { /* M4k */
901     DEFAULT_COSTS
902   },
903     /* Octeon */
904   {
905     SOFT_FP_COSTS,
906     COSTS_N_INSNS (5),            /* int_mult_si */
907     COSTS_N_INSNS (5),            /* int_mult_di */
908     COSTS_N_INSNS (72),           /* int_div_si */
909     COSTS_N_INSNS (72),           /* int_div_di */
910                      1,           /* branch_cost */
911                      4            /* memory_latency */
912   },
913     /* Octeon II */
914   {
915     SOFT_FP_COSTS,
916     COSTS_N_INSNS (6),            /* int_mult_si */
917     COSTS_N_INSNS (6),            /* int_mult_di */
918     COSTS_N_INSNS (18),           /* int_div_si */
919     COSTS_N_INSNS (35),           /* int_div_di */
920                      4,           /* branch_cost */
921                      4            /* memory_latency */
922   },
923   { /* R3900 */
924     COSTS_N_INSNS (2),            /* fp_add */
925     COSTS_N_INSNS (4),            /* fp_mult_sf */
926     COSTS_N_INSNS (5),            /* fp_mult_df */
927     COSTS_N_INSNS (12),           /* fp_div_sf */
928     COSTS_N_INSNS (19),           /* fp_div_df */
929     COSTS_N_INSNS (2),            /* int_mult_si */
930     COSTS_N_INSNS (2),            /* int_mult_di */
931     COSTS_N_INSNS (35),           /* int_div_si */
932     COSTS_N_INSNS (35),           /* int_div_di */
933                      1,           /* branch_cost */
934                      4            /* memory_latency */
935   },
936   { /* R6000 */
937     COSTS_N_INSNS (3),            /* fp_add */
938     COSTS_N_INSNS (5),            /* fp_mult_sf */
939     COSTS_N_INSNS (6),            /* fp_mult_df */
940     COSTS_N_INSNS (15),           /* fp_div_sf */
941     COSTS_N_INSNS (16),           /* fp_div_df */
942     COSTS_N_INSNS (17),           /* int_mult_si */
943     COSTS_N_INSNS (17),           /* int_mult_di */
944     COSTS_N_INSNS (38),           /* int_div_si */
945     COSTS_N_INSNS (38),           /* int_div_di */
946                      2,           /* branch_cost */
947                      6            /* memory_latency */
948   },
949   { /* R4000 */
950      COSTS_N_INSNS (6),           /* fp_add */
951      COSTS_N_INSNS (7),           /* fp_mult_sf */
952      COSTS_N_INSNS (8),           /* fp_mult_df */
953      COSTS_N_INSNS (23),          /* fp_div_sf */
954      COSTS_N_INSNS (36),          /* fp_div_df */
955      COSTS_N_INSNS (10),          /* int_mult_si */
956      COSTS_N_INSNS (10),          /* int_mult_di */
957      COSTS_N_INSNS (69),          /* int_div_si */
958      COSTS_N_INSNS (69),          /* int_div_di */
959                       2,          /* branch_cost */
960                       6           /* memory_latency */
961   },
962   { /* R4100 */
963     DEFAULT_COSTS
964   },
965   { /* R4111 */
966     DEFAULT_COSTS
967   },
968   { /* R4120 */
969     DEFAULT_COSTS
970   },
971   { /* R4130 */
972     /* The only costs that appear to be updated here are
973        integer multiplication.  */
974     SOFT_FP_COSTS,
975     COSTS_N_INSNS (4),            /* int_mult_si */
976     COSTS_N_INSNS (6),            /* int_mult_di */
977     COSTS_N_INSNS (69),           /* int_div_si */
978     COSTS_N_INSNS (69),           /* int_div_di */
979                      1,           /* branch_cost */
980                      4            /* memory_latency */
981   },
982   { /* R4300 */
983     DEFAULT_COSTS
984   },
985   { /* R4600 */
986     DEFAULT_COSTS
987   },
988   { /* R4650 */
989     DEFAULT_COSTS
990   },
991   { /* R4700 */
992     DEFAULT_COSTS
993   },
994   { /* R5000 */
995     COSTS_N_INSNS (6),            /* fp_add */
996     COSTS_N_INSNS (4),            /* fp_mult_sf */
997     COSTS_N_INSNS (5),            /* fp_mult_df */
998     COSTS_N_INSNS (23),           /* fp_div_sf */
999     COSTS_N_INSNS (36),           /* fp_div_df */
1000     COSTS_N_INSNS (5),            /* int_mult_si */
1001     COSTS_N_INSNS (5),            /* int_mult_di */
1002     COSTS_N_INSNS (36),           /* int_div_si */
1003     COSTS_N_INSNS (36),           /* int_div_di */
1004                      1,           /* branch_cost */
1005                      4            /* memory_latency */
1006   },
1007   { /* R5400 */
1008     COSTS_N_INSNS (6),            /* fp_add */
1009     COSTS_N_INSNS (5),            /* fp_mult_sf */
1010     COSTS_N_INSNS (6),            /* fp_mult_df */
1011     COSTS_N_INSNS (30),           /* fp_div_sf */
1012     COSTS_N_INSNS (59),           /* fp_div_df */
1013     COSTS_N_INSNS (3),            /* int_mult_si */
1014     COSTS_N_INSNS (4),            /* int_mult_di */
1015     COSTS_N_INSNS (42),           /* int_div_si */
1016     COSTS_N_INSNS (74),           /* int_div_di */
1017                      1,           /* branch_cost */
1018                      4            /* memory_latency */
1019   },
1020   { /* R5500 */
1021     COSTS_N_INSNS (6),            /* fp_add */
1022     COSTS_N_INSNS (5),            /* fp_mult_sf */
1023     COSTS_N_INSNS (6),            /* fp_mult_df */
1024     COSTS_N_INSNS (30),           /* fp_div_sf */
1025     COSTS_N_INSNS (59),           /* fp_div_df */
1026     COSTS_N_INSNS (5),            /* int_mult_si */
1027     COSTS_N_INSNS (9),            /* int_mult_di */
1028     COSTS_N_INSNS (42),           /* int_div_si */
1029     COSTS_N_INSNS (74),           /* int_div_di */
1030                      1,           /* branch_cost */
1031                      4            /* memory_latency */
1032   },
1033   { /* R5900 */
1034     COSTS_N_INSNS (4),            /* fp_add */
1035     COSTS_N_INSNS (4),            /* fp_mult_sf */
1036     COSTS_N_INSNS (256),          /* fp_mult_df */
1037     COSTS_N_INSNS (8),            /* fp_div_sf */
1038     COSTS_N_INSNS (256),          /* fp_div_df */
1039     COSTS_N_INSNS (4),            /* int_mult_si */
1040     COSTS_N_INSNS (256),          /* int_mult_di */
1041     COSTS_N_INSNS (37),           /* int_div_si */
1042     COSTS_N_INSNS (256),          /* int_div_di */
1043                      1,           /* branch_cost */
1044                      4            /* memory_latency */
1045   },
1046   { /* R7000 */
1047     /* The only costs that are changed here are
1048        integer multiplication.  */
1049     COSTS_N_INSNS (6),            /* fp_add */
1050     COSTS_N_INSNS (7),            /* fp_mult_sf */
1051     COSTS_N_INSNS (8),            /* fp_mult_df */
1052     COSTS_N_INSNS (23),           /* fp_div_sf */
1053     COSTS_N_INSNS (36),           /* fp_div_df */
1054     COSTS_N_INSNS (5),            /* int_mult_si */
1055     COSTS_N_INSNS (9),            /* int_mult_di */
1056     COSTS_N_INSNS (69),           /* int_div_si */
1057     COSTS_N_INSNS (69),           /* int_div_di */
1058                      1,           /* branch_cost */
1059                      4            /* memory_latency */
1060   },
1061   { /* R8000 */
1062     DEFAULT_COSTS
1063   },
1064   { /* R9000 */
1065     /* The only costs that are changed here are
1066        integer multiplication.  */
1067     COSTS_N_INSNS (6),            /* fp_add */
1068     COSTS_N_INSNS (7),            /* fp_mult_sf */
1069     COSTS_N_INSNS (8),            /* fp_mult_df */
1070     COSTS_N_INSNS (23),           /* fp_div_sf */
1071     COSTS_N_INSNS (36),           /* fp_div_df */
1072     COSTS_N_INSNS (3),            /* int_mult_si */
1073     COSTS_N_INSNS (8),            /* int_mult_di */
1074     COSTS_N_INSNS (69),           /* int_div_si */
1075     COSTS_N_INSNS (69),           /* int_div_di */
1076                      1,           /* branch_cost */
1077                      4            /* memory_latency */
1078   },
1079   { /* R1x000 */
1080     COSTS_N_INSNS (2),            /* fp_add */
1081     COSTS_N_INSNS (2),            /* fp_mult_sf */
1082     COSTS_N_INSNS (2),            /* fp_mult_df */
1083     COSTS_N_INSNS (12),           /* fp_div_sf */
1084     COSTS_N_INSNS (19),           /* fp_div_df */
1085     COSTS_N_INSNS (5),            /* int_mult_si */
1086     COSTS_N_INSNS (9),            /* int_mult_di */
1087     COSTS_N_INSNS (34),           /* int_div_si */
1088     COSTS_N_INSNS (66),           /* int_div_di */
1089                      1,           /* branch_cost */
1090                      4            /* memory_latency */
1091   },
1092   { /* SB1 */
1093     /* These costs are the same as the SB-1A below.  */
1094     COSTS_N_INSNS (4),            /* fp_add */
1095     COSTS_N_INSNS (4),            /* fp_mult_sf */
1096     COSTS_N_INSNS (4),            /* fp_mult_df */
1097     COSTS_N_INSNS (24),           /* fp_div_sf */
1098     COSTS_N_INSNS (32),           /* fp_div_df */
1099     COSTS_N_INSNS (3),            /* int_mult_si */
1100     COSTS_N_INSNS (4),            /* int_mult_di */
1101     COSTS_N_INSNS (36),           /* int_div_si */
1102     COSTS_N_INSNS (68),           /* int_div_di */
1103                      1,           /* branch_cost */
1104                      4            /* memory_latency */
1105   },
1106   { /* SB1-A */
1107     /* These costs are the same as the SB-1 above.  */
1108     COSTS_N_INSNS (4),            /* fp_add */
1109     COSTS_N_INSNS (4),            /* fp_mult_sf */
1110     COSTS_N_INSNS (4),            /* fp_mult_df */
1111     COSTS_N_INSNS (24),           /* fp_div_sf */
1112     COSTS_N_INSNS (32),           /* fp_div_df */
1113     COSTS_N_INSNS (3),            /* int_mult_si */
1114     COSTS_N_INSNS (4),            /* int_mult_di */
1115     COSTS_N_INSNS (36),           /* int_div_si */
1116     COSTS_N_INSNS (68),           /* int_div_di */
1117                      1,           /* branch_cost */
1118                      4            /* memory_latency */
1119   },
1120   { /* SR71000 */
1121     DEFAULT_COSTS
1122   },
1123   { /* XLR */
1124     SOFT_FP_COSTS,
1125     COSTS_N_INSNS (8),            /* int_mult_si */
1126     COSTS_N_INSNS (8),            /* int_mult_di */
1127     COSTS_N_INSNS (72),           /* int_div_si */
1128     COSTS_N_INSNS (72),           /* int_div_di */
1129                      1,           /* branch_cost */
1130                      4            /* memory_latency */
1131   },
1132   { /* XLP */
1133     /* These costs are the same as 5KF above.  */
1134     COSTS_N_INSNS (4),            /* fp_add */
1135     COSTS_N_INSNS (4),            /* fp_mult_sf */
1136     COSTS_N_INSNS (5),            /* fp_mult_df */
1137     COSTS_N_INSNS (17),           /* fp_div_sf */
1138     COSTS_N_INSNS (32),           /* fp_div_df */
1139     COSTS_N_INSNS (4),            /* int_mult_si */
1140     COSTS_N_INSNS (11),           /* int_mult_di */
1141     COSTS_N_INSNS (36),           /* int_div_si */
1142     COSTS_N_INSNS (68),           /* int_div_di */
1143                      1,           /* branch_cost */
1144                      4            /* memory_latency */
1145   }
1146 };
1147 \f
1148 static rtx mips_find_pic_call_symbol (rtx, rtx, bool);
1149 static int mips_register_move_cost (enum machine_mode, reg_class_t,
1150                                     reg_class_t);
1151 static unsigned int mips_function_arg_boundary (enum machine_mode, const_tree);
1152 \f
1153 /* This hash table keeps track of implicit "mips16" and "nomips16" attributes
1154    for -mflip_mips16.  It maps decl names onto a boolean mode setting.  */
1155 struct GTY (())  mflip_mips16_entry {
1156   const char *name;
1157   bool mips16_p;
1158 };
1159 static GTY ((param_is (struct mflip_mips16_entry))) htab_t mflip_mips16_htab;
1160
1161 /* Hash table callbacks for mflip_mips16_htab.  */
1162
1163 static hashval_t
1164 mflip_mips16_htab_hash (const void *entry)
1165 {
1166   return htab_hash_string (((const struct mflip_mips16_entry *) entry)->name);
1167 }
1168
1169 static int
1170 mflip_mips16_htab_eq (const void *entry, const void *name)
1171 {
1172   return strcmp (((const struct mflip_mips16_entry *) entry)->name,
1173                  (const char *) name) == 0;
1174 }
1175
1176 /* True if -mflip-mips16 should next add an attribute for the default MIPS16
1177    mode, false if it should next add an attribute for the opposite mode.  */
1178 static GTY(()) bool mips16_flipper;
1179
1180 /* DECL is a function that needs a default "mips16" or "nomips16" attribute
1181    for -mflip-mips16.  Return true if it should use "mips16" and false if
1182    it should use "nomips16".  */
1183
1184 static bool
1185 mflip_mips16_use_mips16_p (tree decl)
1186 {
1187   struct mflip_mips16_entry *entry;
1188   const char *name;
1189   hashval_t hash;
1190   void **slot;
1191   bool base_is_mips16 = (mips_base_compression_flags & MASK_MIPS16) != 0;
1192
1193   /* Use the opposite of the command-line setting for anonymous decls.  */
1194   if (!DECL_NAME (decl))
1195     return !base_is_mips16;
1196
1197   if (!mflip_mips16_htab)
1198     mflip_mips16_htab = htab_create_ggc (37, mflip_mips16_htab_hash,
1199                                          mflip_mips16_htab_eq, NULL);
1200
1201   name = IDENTIFIER_POINTER (DECL_NAME (decl));
1202   hash = htab_hash_string (name);
1203   slot = htab_find_slot_with_hash (mflip_mips16_htab, name, hash, INSERT);
1204   entry = (struct mflip_mips16_entry *) *slot;
1205   if (!entry)
1206     {
1207       mips16_flipper = !mips16_flipper;
1208       entry = ggc_alloc_mflip_mips16_entry ();
1209       entry->name = name;
1210       entry->mips16_p = mips16_flipper ? !base_is_mips16 : base_is_mips16;
1211       *slot = entry;
1212     }
1213   return entry->mips16_p;
1214 }
1215 \f
1216 /* Predicates to test for presence of "near" and "far"/"long_call"
1217    attributes on the given TYPE.  */
1218
1219 static bool
1220 mips_near_type_p (const_tree type)
1221 {
1222   return lookup_attribute ("near", TYPE_ATTRIBUTES (type)) != NULL;
1223 }
1224
1225 static bool
1226 mips_far_type_p (const_tree type)
1227 {
1228   return (lookup_attribute ("long_call", TYPE_ATTRIBUTES (type)) != NULL
1229           || lookup_attribute ("far", TYPE_ATTRIBUTES (type)) != NULL);
1230 }
1231
1232
1233 /* Check if the interrupt attribute is set for a function.  */
1234
1235 static bool
1236 mips_interrupt_type_p (tree type)
1237 {
1238   return lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type)) != NULL;
1239 }
1240
1241 /* Check if the attribute to use shadow register set is set for a function.  */
1242
1243 static bool
1244 mips_use_shadow_register_set_p (tree type)
1245 {
1246   return lookup_attribute ("use_shadow_register_set",
1247                            TYPE_ATTRIBUTES (type)) != NULL;
1248 }
1249
1250 /* Check if the attribute to keep interrupts masked is set for a function.  */
1251
1252 static bool
1253 mips_keep_interrupts_masked_p (tree type)
1254 {
1255   return lookup_attribute ("keep_interrupts_masked",
1256                            TYPE_ATTRIBUTES (type)) != NULL;
1257 }
1258
1259 /* Check if the attribute to use debug exception return is set for
1260    a function.  */
1261
1262 static bool
1263 mips_use_debug_exception_return_p (tree type)
1264 {
1265   return lookup_attribute ("use_debug_exception_return",
1266                            TYPE_ATTRIBUTES (type)) != NULL;
1267 }
1268
1269 /* Return the set of compression modes that are explicitly required
1270    by the attributes in ATTRIBUTES.  */
1271
1272 static unsigned int
1273 mips_get_compress_on_flags (tree attributes)
1274 {
1275   unsigned int flags = 0;
1276
1277   if (lookup_attribute ("mips16", attributes) != NULL)
1278     flags |= MASK_MIPS16;
1279
1280   if (lookup_attribute ("micromips", attributes) != NULL)
1281     flags |= MASK_MICROMIPS;
1282
1283   return flags;
1284 }
1285
1286 /* Return the set of compression modes that are explicitly forbidden
1287    by the attributes in ATTRIBUTES.  */
1288
1289 static unsigned int
1290 mips_get_compress_off_flags (tree attributes)
1291 {
1292   unsigned int flags = 0;
1293
1294   if (lookup_attribute ("nocompression", attributes) != NULL)
1295     flags |= MASK_MIPS16 | MASK_MICROMIPS;
1296
1297   if (lookup_attribute ("nomips16", attributes) != NULL)
1298     flags |= MASK_MIPS16;
1299
1300   if (lookup_attribute ("nomicromips", attributes) != NULL)
1301     flags |= MASK_MICROMIPS;
1302
1303   return flags;
1304 }
1305
1306 /* Return the compression mode that should be used for function DECL.
1307    Return the ambient setting if DECL is null.  */
1308
1309 static unsigned int
1310 mips_get_compress_mode (tree decl)
1311 {
1312   unsigned int flags, force_on;
1313
1314   flags = mips_base_compression_flags;
1315   if (decl)
1316     {
1317       /* Nested functions must use the same frame pointer as their
1318          parent and must therefore use the same ISA mode.  */
1319       tree parent = decl_function_context (decl);
1320       if (parent)
1321         decl = parent;
1322       force_on = mips_get_compress_on_flags (DECL_ATTRIBUTES (decl));
1323       if (force_on)
1324         return force_on;
1325       flags &= ~mips_get_compress_off_flags (DECL_ATTRIBUTES (decl));
1326     }
1327   return flags;
1328 }
1329
1330 /* Return the attribute name associated with MASK_MIPS16 and MASK_MICROMIPS
1331    flags FLAGS.  */
1332
1333 static const char *
1334 mips_get_compress_on_name (unsigned int flags)
1335 {
1336   if (flags == MASK_MIPS16)
1337     return "mips16";
1338   return "micromips";
1339 }
1340
1341 /* Return the attribute name that forbids MASK_MIPS16 and MASK_MICROMIPS
1342    flags FLAGS.  */
1343
1344 static const char *
1345 mips_get_compress_off_name (unsigned int flags)
1346 {
1347   if (flags == MASK_MIPS16)
1348     return "nomips16";
1349   if (flags == MASK_MICROMIPS)
1350     return "nomicromips";
1351   return "nocompression";
1352 }
1353
1354 /* Implement TARGET_COMP_TYPE_ATTRIBUTES.  */
1355
1356 static int
1357 mips_comp_type_attributes (const_tree type1, const_tree type2)
1358 {
1359   /* Disallow mixed near/far attributes.  */
1360   if (mips_far_type_p (type1) && mips_near_type_p (type2))
1361     return 0;
1362   if (mips_near_type_p (type1) && mips_far_type_p (type2))
1363     return 0;
1364   return 1;
1365 }
1366
1367 /* Implement TARGET_INSERT_ATTRIBUTES.  */
1368
1369 static void
1370 mips_insert_attributes (tree decl, tree *attributes)
1371 {
1372   const char *name;
1373   unsigned int compression_flags, nocompression_flags;
1374
1375   /* Check for "mips16" and "nomips16" attributes.  */
1376   compression_flags = mips_get_compress_on_flags (*attributes);
1377   nocompression_flags = mips_get_compress_off_flags (*attributes);
1378
1379   if (TREE_CODE (decl) != FUNCTION_DECL)
1380     {
1381       if (nocompression_flags)
1382         error ("%qs attribute only applies to functions",
1383                mips_get_compress_off_name (nocompression_flags));
1384
1385       if (compression_flags)
1386         error ("%qs attribute only applies to functions",
1387                mips_get_compress_on_name (nocompression_flags));
1388     }
1389   else
1390     {
1391       compression_flags |= mips_get_compress_on_flags (DECL_ATTRIBUTES (decl));
1392       nocompression_flags |=
1393         mips_get_compress_off_flags (DECL_ATTRIBUTES (decl));
1394
1395       if (compression_flags && nocompression_flags)
1396         error ("%qE cannot have both %qs and %qs attributes",
1397                DECL_NAME (decl), mips_get_compress_on_name (compression_flags),
1398                mips_get_compress_off_name (nocompression_flags));
1399
1400       if (compression_flags & MASK_MIPS16
1401           && compression_flags & MASK_MICROMIPS)
1402         error ("%qE cannot have both %qs and %qs attributes",
1403                DECL_NAME (decl), "mips16", "micromips");
1404
1405       if (TARGET_FLIP_MIPS16
1406           && !DECL_ARTIFICIAL (decl)
1407           && compression_flags == 0
1408           && nocompression_flags == 0)
1409         {
1410           /* Implement -mflip-mips16.  If DECL has neither a "nomips16" nor a
1411              "mips16" attribute, arbitrarily pick one.  We must pick the same
1412              setting for duplicate declarations of a function.  */
1413           name = mflip_mips16_use_mips16_p (decl) ? "mips16" : "nomips16";
1414           *attributes = tree_cons (get_identifier (name), NULL, *attributes);
1415           name = "nomicromips";
1416           *attributes = tree_cons (get_identifier (name), NULL, *attributes);
1417         }
1418     }
1419 }
1420
1421 /* Implement TARGET_MERGE_DECL_ATTRIBUTES.  */
1422
1423 static tree
1424 mips_merge_decl_attributes (tree olddecl, tree newdecl)
1425 {
1426   unsigned int diff;
1427
1428   diff = (mips_get_compress_on_flags (DECL_ATTRIBUTES (olddecl))
1429           ^ mips_get_compress_on_flags (DECL_ATTRIBUTES (newdecl)));
1430   if (diff)
1431     error ("%qE redeclared with conflicting %qs attributes",
1432            DECL_NAME (newdecl), mips_get_compress_on_name (diff));
1433
1434   diff = (mips_get_compress_off_flags (DECL_ATTRIBUTES (olddecl))
1435           ^ mips_get_compress_off_flags (DECL_ATTRIBUTES (newdecl)));
1436   if (diff)
1437     error ("%qE redeclared with conflicting %qs attributes",
1438            DECL_NAME (newdecl), mips_get_compress_off_name (diff));
1439
1440   return merge_attributes (DECL_ATTRIBUTES (olddecl),
1441                            DECL_ATTRIBUTES (newdecl));
1442 }
1443
1444 /* Implement TARGET_CAN_INLINE_P.  */
1445
1446 static bool
1447 mips_can_inline_p (tree caller, tree callee)
1448 {
1449   if (mips_get_compress_mode (callee) != mips_get_compress_mode (caller))
1450     return false;
1451   return default_target_can_inline_p (caller, callee);
1452 }
1453 \f
1454 /* If X is a PLUS of a CONST_INT, return the two terms in *BASE_PTR
1455    and *OFFSET_PTR.  Return X in *BASE_PTR and 0 in *OFFSET_PTR otherwise.  */
1456
1457 static void
1458 mips_split_plus (rtx x, rtx *base_ptr, HOST_WIDE_INT *offset_ptr)
1459 {
1460   if (GET_CODE (x) == PLUS && CONST_INT_P (XEXP (x, 1)))
1461     {
1462       *base_ptr = XEXP (x, 0);
1463       *offset_ptr = INTVAL (XEXP (x, 1));
1464     }
1465   else
1466     {
1467       *base_ptr = x;
1468       *offset_ptr = 0;
1469     }
1470 }
1471 \f
1472 static unsigned int mips_build_integer (struct mips_integer_op *,
1473                                         unsigned HOST_WIDE_INT);
1474
1475 /* A subroutine of mips_build_integer, with the same interface.
1476    Assume that the final action in the sequence should be a left shift.  */
1477
1478 static unsigned int
1479 mips_build_shift (struct mips_integer_op *codes, HOST_WIDE_INT value)
1480 {
1481   unsigned int i, shift;
1482
1483   /* Shift VALUE right until its lowest bit is set.  Shift arithmetically
1484      since signed numbers are easier to load than unsigned ones.  */
1485   shift = 0;
1486   while ((value & 1) == 0)
1487     value /= 2, shift++;
1488
1489   i = mips_build_integer (codes, value);
1490   codes[i].code = ASHIFT;
1491   codes[i].value = shift;
1492   return i + 1;
1493 }
1494
1495 /* As for mips_build_shift, but assume that the final action will be
1496    an IOR or PLUS operation.  */
1497
1498 static unsigned int
1499 mips_build_lower (struct mips_integer_op *codes, unsigned HOST_WIDE_INT value)
1500 {
1501   unsigned HOST_WIDE_INT high;
1502   unsigned int i;
1503
1504   high = value & ~(unsigned HOST_WIDE_INT) 0xffff;
1505   if (!LUI_OPERAND (high) && (value & 0x18000) == 0x18000)
1506     {
1507       /* The constant is too complex to load with a simple LUI/ORI pair,
1508          so we want to give the recursive call as many trailing zeros as
1509          possible.  In this case, we know bit 16 is set and that the
1510          low 16 bits form a negative number.  If we subtract that number
1511          from VALUE, we will clear at least the lowest 17 bits, maybe more.  */
1512       i = mips_build_integer (codes, CONST_HIGH_PART (value));
1513       codes[i].code = PLUS;
1514       codes[i].value = CONST_LOW_PART (value);
1515     }
1516   else
1517     {
1518       /* Either this is a simple LUI/ORI pair, or clearing the lowest 16
1519          bits gives a value with at least 17 trailing zeros.  */
1520       i = mips_build_integer (codes, high);
1521       codes[i].code = IOR;
1522       codes[i].value = value & 0xffff;
1523     }
1524   return i + 1;
1525 }
1526
1527 /* Fill CODES with a sequence of rtl operations to load VALUE.
1528    Return the number of operations needed.  */
1529
1530 static unsigned int
1531 mips_build_integer (struct mips_integer_op *codes,
1532                     unsigned HOST_WIDE_INT value)
1533 {
1534   if (SMALL_OPERAND (value)
1535       || SMALL_OPERAND_UNSIGNED (value)
1536       || LUI_OPERAND (value))
1537     {
1538       /* The value can be loaded with a single instruction.  */
1539       codes[0].code = UNKNOWN;
1540       codes[0].value = value;
1541       return 1;
1542     }
1543   else if ((value & 1) != 0 || LUI_OPERAND (CONST_HIGH_PART (value)))
1544     {
1545       /* Either the constant is a simple LUI/ORI combination or its
1546          lowest bit is set.  We don't want to shift in this case.  */
1547       return mips_build_lower (codes, value);
1548     }
1549   else if ((value & 0xffff) == 0)
1550     {
1551       /* The constant will need at least three actions.  The lowest
1552          16 bits are clear, so the final action will be a shift.  */
1553       return mips_build_shift (codes, value);
1554     }
1555   else
1556     {
1557       /* The final action could be a shift, add or inclusive OR.
1558          Rather than use a complex condition to select the best
1559          approach, try both mips_build_shift and mips_build_lower
1560          and pick the one that gives the shortest sequence.
1561          Note that this case is only used once per constant.  */
1562       struct mips_integer_op alt_codes[MIPS_MAX_INTEGER_OPS];
1563       unsigned int cost, alt_cost;
1564
1565       cost = mips_build_shift (codes, value);
1566       alt_cost = mips_build_lower (alt_codes, value);
1567       if (alt_cost < cost)
1568         {
1569           memcpy (codes, alt_codes, alt_cost * sizeof (codes[0]));
1570           cost = alt_cost;
1571         }
1572       return cost;
1573     }
1574 }
1575 \f
1576 /* Implement TARGET_LEGITIMATE_CONSTANT_P.  */
1577
1578 static bool
1579 mips_legitimate_constant_p (enum machine_mode mode ATTRIBUTE_UNUSED, rtx x)
1580 {
1581   return mips_const_insns (x) > 0;
1582 }
1583 \f
1584 /* Return a SYMBOL_REF for a MIPS16 function called NAME.  */
1585
1586 static rtx
1587 mips16_stub_function (const char *name)
1588 {
1589   rtx x;
1590
1591   x = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (name));
1592   SYMBOL_REF_FLAGS (x) |= (SYMBOL_FLAG_EXTERNAL | SYMBOL_FLAG_FUNCTION);
1593   return x;
1594 }
1595 \f
1596 /* Return true if symbols of type TYPE require a GOT access.  */
1597
1598 static bool
1599 mips_got_symbol_type_p (enum mips_symbol_type type)
1600 {
1601   switch (type)
1602     {
1603     case SYMBOL_GOT_PAGE_OFST:
1604     case SYMBOL_GOT_DISP:
1605       return true;
1606
1607     default:
1608       return false;
1609     }
1610 }
1611
1612 /* Return true if X is a thread-local symbol.  */
1613
1614 static bool
1615 mips_tls_symbol_p (rtx x)
1616 {
1617   return GET_CODE (x) == SYMBOL_REF && SYMBOL_REF_TLS_MODEL (x) != 0;
1618 }
1619
1620 /* Return true if SYMBOL_REF X is associated with a global symbol
1621    (in the STB_GLOBAL sense).  */
1622
1623 static bool
1624 mips_global_symbol_p (const_rtx x)
1625 {
1626   const_tree decl = SYMBOL_REF_DECL (x);
1627
1628   if (!decl)
1629     return !SYMBOL_REF_LOCAL_P (x) || SYMBOL_REF_EXTERNAL_P (x);
1630
1631   /* Weakref symbols are not TREE_PUBLIC, but their targets are global
1632      or weak symbols.  Relocations in the object file will be against
1633      the target symbol, so it's that symbol's binding that matters here.  */
1634   return DECL_P (decl) && (TREE_PUBLIC (decl) || DECL_WEAK (decl));
1635 }
1636
1637 /* Return true if function X is a libgcc MIPS16 stub function.  */
1638
1639 static bool
1640 mips16_stub_function_p (const_rtx x)
1641 {
1642   return (GET_CODE (x) == SYMBOL_REF
1643           && strncmp (XSTR (x, 0), "__mips16_", 9) == 0);
1644 }
1645
1646 /* Return true if function X is a locally-defined and locally-binding
1647    MIPS16 function.  */
1648
1649 static bool
1650 mips16_local_function_p (const_rtx x)
1651 {
1652   return (GET_CODE (x) == SYMBOL_REF
1653           && SYMBOL_REF_LOCAL_P (x)
1654           && !SYMBOL_REF_EXTERNAL_P (x)
1655           && (mips_get_compress_mode (SYMBOL_REF_DECL (x)) & MASK_MIPS16));
1656 }
1657
1658 /* Return true if SYMBOL_REF X binds locally.  */
1659
1660 static bool
1661 mips_symbol_binds_local_p (const_rtx x)
1662 {
1663   return (SYMBOL_REF_DECL (x)
1664           ? targetm.binds_local_p (SYMBOL_REF_DECL (x))
1665           : SYMBOL_REF_LOCAL_P (x));
1666 }
1667
1668 /* Return true if rtx constants of mode MODE should be put into a small
1669    data section.  */
1670
1671 static bool
1672 mips_rtx_constant_in_small_data_p (enum machine_mode mode)
1673 {
1674   return (!TARGET_EMBEDDED_DATA
1675           && TARGET_LOCAL_SDATA
1676           && GET_MODE_SIZE (mode) <= mips_small_data_threshold);
1677 }
1678
1679 /* Return true if X should not be moved directly into register $25.
1680    We need this because many versions of GAS will treat "la $25,foo" as
1681    part of a call sequence and so allow a global "foo" to be lazily bound.  */
1682
1683 bool
1684 mips_dangerous_for_la25_p (rtx x)
1685 {
1686   return (!TARGET_EXPLICIT_RELOCS
1687           && TARGET_USE_GOT
1688           && GET_CODE (x) == SYMBOL_REF
1689           && mips_global_symbol_p (x));
1690 }
1691
1692 /* Return true if calls to X might need $25 to be valid on entry.  */
1693
1694 bool
1695 mips_use_pic_fn_addr_reg_p (const_rtx x)
1696 {
1697   if (!TARGET_USE_PIC_FN_ADDR_REG)
1698     return false;
1699
1700   /* MIPS16 stub functions are guaranteed not to use $25.  */
1701   if (mips16_stub_function_p (x))
1702     return false;
1703
1704   if (GET_CODE (x) == SYMBOL_REF)
1705     {
1706       /* If PLTs and copy relocations are available, the static linker
1707          will make sure that $25 is valid on entry to the target function.  */
1708       if (TARGET_ABICALLS_PIC0)
1709         return false;
1710
1711       /* Locally-defined functions use absolute accesses to set up
1712          the global pointer.  */
1713       if (TARGET_ABSOLUTE_ABICALLS
1714           && mips_symbol_binds_local_p (x)
1715           && !SYMBOL_REF_EXTERNAL_P (x))
1716         return false;
1717     }
1718
1719   return true;
1720 }
1721
1722 /* Return the method that should be used to access SYMBOL_REF or
1723    LABEL_REF X in context CONTEXT.  */
1724
1725 static enum mips_symbol_type
1726 mips_classify_symbol (const_rtx x, enum mips_symbol_context context)
1727 {
1728   if (TARGET_RTP_PIC)
1729     return SYMBOL_GOT_DISP;
1730
1731   if (GET_CODE (x) == LABEL_REF)
1732     {
1733       /* Only return SYMBOL_PC_RELATIVE if we are generating MIPS16
1734          code and if we know that the label is in the current function's
1735          text section.  LABEL_REFs are used for jump tables as well as
1736          text labels, so we must check whether jump tables live in the
1737          text section.  */
1738       if (TARGET_MIPS16_SHORT_JUMP_TABLES
1739           && !LABEL_REF_NONLOCAL_P (x))
1740         return SYMBOL_PC_RELATIVE;
1741
1742       if (TARGET_ABICALLS && !TARGET_ABSOLUTE_ABICALLS)
1743         return SYMBOL_GOT_PAGE_OFST;
1744
1745       return SYMBOL_ABSOLUTE;
1746     }
1747
1748   gcc_assert (GET_CODE (x) == SYMBOL_REF);
1749
1750   if (SYMBOL_REF_TLS_MODEL (x))
1751     return SYMBOL_TLS;
1752
1753   if (CONSTANT_POOL_ADDRESS_P (x))
1754     {
1755       if (TARGET_MIPS16_TEXT_LOADS)
1756         return SYMBOL_PC_RELATIVE;
1757
1758       if (TARGET_MIPS16_PCREL_LOADS && context == SYMBOL_CONTEXT_MEM)
1759         return SYMBOL_PC_RELATIVE;
1760
1761       if (mips_rtx_constant_in_small_data_p (get_pool_mode (x)))
1762         return SYMBOL_GP_RELATIVE;
1763     }
1764
1765   /* Do not use small-data accesses for weak symbols; they may end up
1766      being zero.  */
1767   if (TARGET_GPOPT && SYMBOL_REF_SMALL_P (x) && !SYMBOL_REF_WEAK (x))
1768     return SYMBOL_GP_RELATIVE;
1769
1770   /* Don't use GOT accesses for locally-binding symbols when -mno-shared
1771      is in effect.  */
1772   if (TARGET_ABICALLS_PIC2
1773       && !(TARGET_ABSOLUTE_ABICALLS && mips_symbol_binds_local_p (x)))
1774     {
1775       /* There are three cases to consider:
1776
1777             - o32 PIC (either with or without explicit relocs)
1778             - n32/n64 PIC without explicit relocs
1779             - n32/n64 PIC with explicit relocs
1780
1781          In the first case, both local and global accesses will use an
1782          R_MIPS_GOT16 relocation.  We must correctly predict which of
1783          the two semantics (local or global) the assembler and linker
1784          will apply.  The choice depends on the symbol's binding rather
1785          than its visibility.
1786
1787          In the second case, the assembler will not use R_MIPS_GOT16
1788          relocations, but it chooses between local and global accesses
1789          in the same way as for o32 PIC.
1790
1791          In the third case we have more freedom since both forms of
1792          access will work for any kind of symbol.  However, there seems
1793          little point in doing things differently.  */
1794       if (mips_global_symbol_p (x))
1795         return SYMBOL_GOT_DISP;
1796
1797       return SYMBOL_GOT_PAGE_OFST;
1798     }
1799
1800   return SYMBOL_ABSOLUTE;
1801 }
1802
1803 /* Classify the base of symbolic expression X, given that X appears in
1804    context CONTEXT.  */
1805
1806 static enum mips_symbol_type
1807 mips_classify_symbolic_expression (rtx x, enum mips_symbol_context context)
1808 {
1809   rtx offset;
1810
1811   split_const (x, &x, &offset);
1812   if (UNSPEC_ADDRESS_P (x))
1813     return UNSPEC_ADDRESS_TYPE (x);
1814
1815   return mips_classify_symbol (x, context);
1816 }
1817
1818 /* Return true if OFFSET is within the range [0, ALIGN), where ALIGN
1819    is the alignment in bytes of SYMBOL_REF X.  */
1820
1821 static bool
1822 mips_offset_within_alignment_p (rtx x, HOST_WIDE_INT offset)
1823 {
1824   HOST_WIDE_INT align;
1825
1826   align = SYMBOL_REF_DECL (x) ? DECL_ALIGN_UNIT (SYMBOL_REF_DECL (x)) : 1;
1827   return IN_RANGE (offset, 0, align - 1);
1828 }
1829
1830 /* Return true if X is a symbolic constant that can be used in context
1831    CONTEXT.  If it is, store the type of the symbol in *SYMBOL_TYPE.  */
1832
1833 bool
1834 mips_symbolic_constant_p (rtx x, enum mips_symbol_context context,
1835                           enum mips_symbol_type *symbol_type)
1836 {
1837   rtx offset;
1838
1839   split_const (x, &x, &offset);
1840   if (UNSPEC_ADDRESS_P (x))
1841     {
1842       *symbol_type = UNSPEC_ADDRESS_TYPE (x);
1843       x = UNSPEC_ADDRESS (x);
1844     }
1845   else if (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF)
1846     {
1847       *symbol_type = mips_classify_symbol (x, context);
1848       if (*symbol_type == SYMBOL_TLS)
1849         return false;
1850     }
1851   else
1852     return false;
1853
1854   if (offset == const0_rtx)
1855     return true;
1856
1857   /* Check whether a nonzero offset is valid for the underlying
1858      relocations.  */
1859   switch (*symbol_type)
1860     {
1861     case SYMBOL_ABSOLUTE:
1862     case SYMBOL_64_HIGH:
1863     case SYMBOL_64_MID:
1864     case SYMBOL_64_LOW:
1865       /* If the target has 64-bit pointers and the object file only
1866          supports 32-bit symbols, the values of those symbols will be
1867          sign-extended.  In this case we can't allow an arbitrary offset
1868          in case the 32-bit value X + OFFSET has a different sign from X.  */
1869       if (Pmode == DImode && !ABI_HAS_64BIT_SYMBOLS)
1870         return offset_within_block_p (x, INTVAL (offset));
1871
1872       /* In other cases the relocations can handle any offset.  */
1873       return true;
1874
1875     case SYMBOL_PC_RELATIVE:
1876       /* Allow constant pool references to be converted to LABEL+CONSTANT.
1877          In this case, we no longer have access to the underlying constant,
1878          but the original symbol-based access was known to be valid.  */
1879       if (GET_CODE (x) == LABEL_REF)
1880         return true;
1881
1882       /* Fall through.  */
1883
1884     case SYMBOL_GP_RELATIVE:
1885       /* Make sure that the offset refers to something within the
1886          same object block.  This should guarantee that the final
1887          PC- or GP-relative offset is within the 16-bit limit.  */
1888       return offset_within_block_p (x, INTVAL (offset));
1889
1890     case SYMBOL_GOT_PAGE_OFST:
1891     case SYMBOL_GOTOFF_PAGE:
1892       /* If the symbol is global, the GOT entry will contain the symbol's
1893          address, and we will apply a 16-bit offset after loading it.
1894          If the symbol is local, the linker should provide enough local
1895          GOT entries for a 16-bit offset, but larger offsets may lead
1896          to GOT overflow.  */
1897       return SMALL_INT (offset);
1898
1899     case SYMBOL_TPREL:
1900     case SYMBOL_DTPREL:
1901       /* There is no carry between the HI and LO REL relocations, so the
1902          offset is only valid if we know it won't lead to such a carry.  */
1903       return mips_offset_within_alignment_p (x, INTVAL (offset));
1904
1905     case SYMBOL_GOT_DISP:
1906     case SYMBOL_GOTOFF_DISP:
1907     case SYMBOL_GOTOFF_CALL:
1908     case SYMBOL_GOTOFF_LOADGP:
1909     case SYMBOL_TLSGD:
1910     case SYMBOL_TLSLDM:
1911     case SYMBOL_GOTTPREL:
1912     case SYMBOL_TLS:
1913     case SYMBOL_HALF:
1914       return false;
1915     }
1916   gcc_unreachable ();
1917 }
1918 \f
1919 /* Like mips_symbol_insns, but treat extended MIPS16 instructions as a
1920    single instruction.  We rely on the fact that, in the worst case,
1921    all instructions involved in a MIPS16 address calculation are usually
1922    extended ones.  */
1923
1924 static int
1925 mips_symbol_insns_1 (enum mips_symbol_type type, enum machine_mode mode)
1926 {
1927   if (mips_use_pcrel_pool_p[(int) type])
1928     {
1929       if (mode == MAX_MACHINE_MODE)
1930         /* LEAs will be converted into constant-pool references by
1931            mips_reorg.  */
1932         type = SYMBOL_PC_RELATIVE;
1933       else
1934         /* The constant must be loaded and then dereferenced.  */
1935         return 0;
1936     }
1937
1938   switch (type)
1939     {
1940     case SYMBOL_ABSOLUTE:
1941       /* When using 64-bit symbols, we need 5 preparatory instructions,
1942          such as:
1943
1944              lui     $at,%highest(symbol)
1945              daddiu  $at,$at,%higher(symbol)
1946              dsll    $at,$at,16
1947              daddiu  $at,$at,%hi(symbol)
1948              dsll    $at,$at,16
1949
1950          The final address is then $at + %lo(symbol).  With 32-bit
1951          symbols we just need a preparatory LUI for normal mode and
1952          a preparatory LI and SLL for MIPS16.  */
1953       return ABI_HAS_64BIT_SYMBOLS ? 6 : TARGET_MIPS16 ? 3 : 2;
1954
1955     case SYMBOL_GP_RELATIVE:
1956       /* Treat GP-relative accesses as taking a single instruction on
1957          MIPS16 too; the copy of $gp can often be shared.  */
1958       return 1;
1959
1960     case SYMBOL_PC_RELATIVE:
1961       /* PC-relative constants can be only be used with ADDIUPC,
1962          DADDIUPC, LWPC and LDPC.  */
1963       if (mode == MAX_MACHINE_MODE
1964           || GET_MODE_SIZE (mode) == 4
1965           || GET_MODE_SIZE (mode) == 8)
1966         return 1;
1967
1968       /* The constant must be loaded using ADDIUPC or DADDIUPC first.  */
1969       return 0;
1970
1971     case SYMBOL_GOT_DISP:
1972       /* The constant will have to be loaded from the GOT before it
1973          is used in an address.  */
1974       if (mode != MAX_MACHINE_MODE)
1975         return 0;
1976
1977       /* Fall through.  */
1978
1979     case SYMBOL_GOT_PAGE_OFST:
1980       /* Unless -funit-at-a-time is in effect, we can't be sure whether the
1981          local/global classification is accurate.  The worst cases are:
1982
1983          (1) For local symbols when generating o32 or o64 code.  The assembler
1984              will use:
1985
1986                  lw           $at,%got(symbol)
1987                  nop
1988
1989              ...and the final address will be $at + %lo(symbol).
1990
1991          (2) For global symbols when -mxgot.  The assembler will use:
1992
1993                  lui     $at,%got_hi(symbol)
1994                  (d)addu $at,$at,$gp
1995
1996              ...and the final address will be $at + %got_lo(symbol).  */
1997       return 3;
1998
1999     case SYMBOL_GOTOFF_PAGE:
2000     case SYMBOL_GOTOFF_DISP:
2001     case SYMBOL_GOTOFF_CALL:
2002     case SYMBOL_GOTOFF_LOADGP:
2003     case SYMBOL_64_HIGH:
2004     case SYMBOL_64_MID:
2005     case SYMBOL_64_LOW:
2006     case SYMBOL_TLSGD:
2007     case SYMBOL_TLSLDM:
2008     case SYMBOL_DTPREL:
2009     case SYMBOL_GOTTPREL:
2010     case SYMBOL_TPREL:
2011     case SYMBOL_HALF:
2012       /* A 16-bit constant formed by a single relocation, or a 32-bit
2013          constant formed from a high 16-bit relocation and a low 16-bit
2014          relocation.  Use mips_split_p to determine which.  32-bit
2015          constants need an "lui; addiu" sequence for normal mode and
2016          an "li; sll; addiu" sequence for MIPS16 mode.  */
2017       return !mips_split_p[type] ? 1 : TARGET_MIPS16 ? 3 : 2;
2018
2019     case SYMBOL_TLS:
2020       /* We don't treat a bare TLS symbol as a constant.  */
2021       return 0;
2022     }
2023   gcc_unreachable ();
2024 }
2025
2026 /* If MODE is MAX_MACHINE_MODE, return the number of instructions needed
2027    to load symbols of type TYPE into a register.  Return 0 if the given
2028    type of symbol cannot be used as an immediate operand.
2029
2030    Otherwise, return the number of instructions needed to load or store
2031    values of mode MODE to or from addresses of type TYPE.  Return 0 if
2032    the given type of symbol is not valid in addresses.
2033
2034    In both cases, instruction counts are based off BASE_INSN_LENGTH.  */
2035
2036 static int
2037 mips_symbol_insns (enum mips_symbol_type type, enum machine_mode mode)
2038 {
2039   return mips_symbol_insns_1 (type, mode) * (TARGET_MIPS16 ? 2 : 1);
2040 }
2041 \f
2042 /* A for_each_rtx callback.  Stop the search if *X references a
2043    thread-local symbol.  */
2044
2045 static int
2046 mips_tls_symbol_ref_1 (rtx *x, void *data ATTRIBUTE_UNUSED)
2047 {
2048   return mips_tls_symbol_p (*x);
2049 }
2050
2051 /* Implement TARGET_CANNOT_FORCE_CONST_MEM.  */
2052
2053 static bool
2054 mips_cannot_force_const_mem (enum machine_mode mode, rtx x)
2055 {
2056   enum mips_symbol_type type;
2057   rtx base, offset;
2058
2059   /* There is no assembler syntax for expressing an address-sized
2060      high part.  */
2061   if (GET_CODE (x) == HIGH)
2062     return true;
2063
2064   /* As an optimization, reject constants that mips_legitimize_move
2065      can expand inline.
2066
2067      Suppose we have a multi-instruction sequence that loads constant C
2068      into register R.  If R does not get allocated a hard register, and
2069      R is used in an operand that allows both registers and memory
2070      references, reload will consider forcing C into memory and using
2071      one of the instruction's memory alternatives.  Returning false
2072      here will force it to use an input reload instead.  */
2073   if (CONST_INT_P (x) && mips_legitimate_constant_p (mode, x))
2074     return true;
2075
2076   split_const (x, &base, &offset);
2077   if (mips_symbolic_constant_p (base, SYMBOL_CONTEXT_LEA, &type))
2078     {
2079       /* See whether we explicitly want these symbols in the pool.  */
2080       if (mips_use_pcrel_pool_p[(int) type])
2081         return false;
2082
2083       /* The same optimization as for CONST_INT.  */
2084       if (SMALL_INT (offset) && mips_symbol_insns (type, MAX_MACHINE_MODE) > 0)
2085         return true;
2086
2087       /* If MIPS16 constant pools live in the text section, they should
2088          not refer to anything that might need run-time relocation.  */
2089       if (TARGET_MIPS16_PCREL_LOADS && mips_got_symbol_type_p (type))
2090         return true;
2091     }
2092
2093   /* TLS symbols must be computed by mips_legitimize_move.  */
2094   if (for_each_rtx (&x, &mips_tls_symbol_ref_1, NULL))
2095     return true;
2096
2097   return false;
2098 }
2099
2100 /* Implement TARGET_USE_BLOCKS_FOR_CONSTANT_P.  We can't use blocks for
2101    constants when we're using a per-function constant pool.  */
2102
2103 static bool
2104 mips_use_blocks_for_constant_p (enum machine_mode mode ATTRIBUTE_UNUSED,
2105                                 const_rtx x ATTRIBUTE_UNUSED)
2106 {
2107   return !TARGET_MIPS16_PCREL_LOADS;
2108 }
2109 \f
2110 /* Return true if register REGNO is a valid base register for mode MODE.
2111    STRICT_P is true if REG_OK_STRICT is in effect.  */
2112
2113 int
2114 mips_regno_mode_ok_for_base_p (int regno, enum machine_mode mode,
2115                                bool strict_p)
2116 {
2117   if (!HARD_REGISTER_NUM_P (regno))
2118     {
2119       if (!strict_p)
2120         return true;
2121       regno = reg_renumber[regno];
2122     }
2123
2124   /* These fake registers will be eliminated to either the stack or
2125      hard frame pointer, both of which are usually valid base registers.
2126      Reload deals with the cases where the eliminated form isn't valid.  */
2127   if (regno == ARG_POINTER_REGNUM || regno == FRAME_POINTER_REGNUM)
2128     return true;
2129
2130   /* In MIPS16 mode, the stack pointer can only address word and doubleword
2131      values, nothing smaller.  There are two problems here:
2132
2133        (a) Instantiating virtual registers can introduce new uses of the
2134            stack pointer.  If these virtual registers are valid addresses,
2135            the stack pointer should be too.
2136
2137        (b) Most uses of the stack pointer are not made explicit until
2138            FRAME_POINTER_REGNUM and ARG_POINTER_REGNUM have been eliminated.
2139            We don't know until that stage whether we'll be eliminating to the
2140            stack pointer (which needs the restriction) or the hard frame
2141            pointer (which doesn't).
2142
2143      All in all, it seems more consistent to only enforce this restriction
2144      during and after reload.  */
2145   if (TARGET_MIPS16 && regno == STACK_POINTER_REGNUM)
2146     return !strict_p || GET_MODE_SIZE (mode) == 4 || GET_MODE_SIZE (mode) == 8;
2147
2148   return TARGET_MIPS16 ? M16_REG_P (regno) : GP_REG_P (regno);
2149 }
2150
2151 /* Return true if X is a valid base register for mode MODE.
2152    STRICT_P is true if REG_OK_STRICT is in effect.  */
2153
2154 static bool
2155 mips_valid_base_register_p (rtx x, enum machine_mode mode, bool strict_p)
2156 {
2157   if (!strict_p && GET_CODE (x) == SUBREG)
2158     x = SUBREG_REG (x);
2159
2160   return (REG_P (x)
2161           && mips_regno_mode_ok_for_base_p (REGNO (x), mode, strict_p));
2162 }
2163
2164 /* Return true if, for every base register BASE_REG, (plus BASE_REG X)
2165    can address a value of mode MODE.  */
2166
2167 static bool
2168 mips_valid_offset_p (rtx x, enum machine_mode mode)
2169 {
2170   /* Check that X is a signed 16-bit number.  */
2171   if (!const_arith_operand (x, Pmode))
2172     return false;
2173
2174   /* We may need to split multiword moves, so make sure that every word
2175      is accessible.  */
2176   if (GET_MODE_SIZE (mode) > UNITS_PER_WORD
2177       && !SMALL_OPERAND (INTVAL (x) + GET_MODE_SIZE (mode) - UNITS_PER_WORD))
2178     return false;
2179
2180   return true;
2181 }
2182
2183 /* Return true if a LO_SUM can address a value of mode MODE when the
2184    LO_SUM symbol has type SYMBOL_TYPE.  */
2185
2186 static bool
2187 mips_valid_lo_sum_p (enum mips_symbol_type symbol_type, enum machine_mode mode)
2188 {
2189   /* Check that symbols of type SYMBOL_TYPE can be used to access values
2190      of mode MODE.  */
2191   if (mips_symbol_insns (symbol_type, mode) == 0)
2192     return false;
2193
2194   /* Check that there is a known low-part relocation.  */
2195   if (mips_lo_relocs[symbol_type] == NULL)
2196     return false;
2197
2198   /* We may need to split multiword moves, so make sure that each word
2199      can be accessed without inducing a carry.  This is mainly needed
2200      for o64, which has historically only guaranteed 64-bit alignment
2201      for 128-bit types.  */
2202   if (GET_MODE_SIZE (mode) > UNITS_PER_WORD
2203       && GET_MODE_BITSIZE (mode) > GET_MODE_ALIGNMENT (mode))
2204     return false;
2205
2206   return true;
2207 }
2208
2209 /* Return true if X is a valid address for machine mode MODE.  If it is,
2210    fill in INFO appropriately.  STRICT_P is true if REG_OK_STRICT is in
2211    effect.  */
2212
2213 static bool
2214 mips_classify_address (struct mips_address_info *info, rtx x,
2215                        enum machine_mode mode, bool strict_p)
2216 {
2217   switch (GET_CODE (x))
2218     {
2219     case REG:
2220     case SUBREG:
2221       info->type = ADDRESS_REG;
2222       info->reg = x;
2223       info->offset = const0_rtx;
2224       return mips_valid_base_register_p (info->reg, mode, strict_p);
2225
2226     case PLUS:
2227       info->type = ADDRESS_REG;
2228       info->reg = XEXP (x, 0);
2229       info->offset = XEXP (x, 1);
2230       return (mips_valid_base_register_p (info->reg, mode, strict_p)
2231               && mips_valid_offset_p (info->offset, mode));
2232
2233     case LO_SUM:
2234       info->type = ADDRESS_LO_SUM;
2235       info->reg = XEXP (x, 0);
2236       info->offset = XEXP (x, 1);
2237       /* We have to trust the creator of the LO_SUM to do something vaguely
2238          sane.  Target-independent code that creates a LO_SUM should also
2239          create and verify the matching HIGH.  Target-independent code that
2240          adds an offset to a LO_SUM must prove that the offset will not
2241          induce a carry.  Failure to do either of these things would be
2242          a bug, and we are not required to check for it here.  The MIPS
2243          backend itself should only create LO_SUMs for valid symbolic
2244          constants, with the high part being either a HIGH or a copy
2245          of _gp. */
2246       info->symbol_type
2247         = mips_classify_symbolic_expression (info->offset, SYMBOL_CONTEXT_MEM);
2248       return (mips_valid_base_register_p (info->reg, mode, strict_p)
2249               && mips_valid_lo_sum_p (info->symbol_type, mode));
2250
2251     case CONST_INT:
2252       /* Small-integer addresses don't occur very often, but they
2253          are legitimate if $0 is a valid base register.  */
2254       info->type = ADDRESS_CONST_INT;
2255       return !TARGET_MIPS16 && SMALL_INT (x);
2256
2257     case CONST:
2258     case LABEL_REF:
2259     case SYMBOL_REF:
2260       info->type = ADDRESS_SYMBOLIC;
2261       return (mips_symbolic_constant_p (x, SYMBOL_CONTEXT_MEM,
2262                                         &info->symbol_type)
2263               && mips_symbol_insns (info->symbol_type, mode) > 0
2264               && !mips_split_p[info->symbol_type]);
2265
2266     default:
2267       return false;
2268     }
2269 }
2270
2271 /* Implement TARGET_LEGITIMATE_ADDRESS_P.  */
2272
2273 static bool
2274 mips_legitimate_address_p (enum machine_mode mode, rtx x, bool strict_p)
2275 {
2276   struct mips_address_info addr;
2277
2278   return mips_classify_address (&addr, x, mode, strict_p);
2279 }
2280
2281 /* Return true if X is a legitimate $sp-based address for mode MDOE.  */
2282
2283 bool
2284 mips_stack_address_p (rtx x, enum machine_mode mode)
2285 {
2286   struct mips_address_info addr;
2287
2288   return (mips_classify_address (&addr, x, mode, false)
2289           && addr.type == ADDRESS_REG
2290           && addr.reg == stack_pointer_rtx);
2291 }
2292
2293 /* Return true if ADDR matches the pattern for the LWXS load scaled indexed
2294    address instruction.  Note that such addresses are not considered
2295    legitimate in the TARGET_LEGITIMATE_ADDRESS_P sense, because their use
2296    is so restricted.  */
2297
2298 static bool
2299 mips_lwxs_address_p (rtx addr)
2300 {
2301   if (ISA_HAS_LWXS
2302       && GET_CODE (addr) == PLUS
2303       && REG_P (XEXP (addr, 1)))
2304     {
2305       rtx offset = XEXP (addr, 0);
2306       if (GET_CODE (offset) == MULT
2307           && REG_P (XEXP (offset, 0))
2308           && CONST_INT_P (XEXP (offset, 1))
2309           && INTVAL (XEXP (offset, 1)) == 4)
2310         return true;
2311     }
2312   return false;
2313 }
2314
2315 /* Return true if ADDR matches the pattern for the L{B,H,W,D}{,U}X load 
2316    indexed address instruction.  Note that such addresses are
2317    not considered legitimate in the TARGET_LEGITIMATE_ADDRESS_P
2318    sense, because their use is so restricted.  */
2319
2320 static bool
2321 mips_lx_address_p (rtx addr, enum machine_mode mode)
2322 {
2323   if (GET_CODE (addr) != PLUS
2324       || !REG_P (XEXP (addr, 0))
2325       || !REG_P (XEXP (addr, 1)))
2326     return false;
2327   if (ISA_HAS_LBX && mode == QImode)
2328     return true;
2329   if (ISA_HAS_LHX && mode == HImode)
2330     return true;
2331   if (ISA_HAS_LWX && mode == SImode)
2332     return true;
2333   if (ISA_HAS_LDX && mode == DImode)
2334     return true;
2335   return false;
2336 }
2337 \f
2338 /* Return true if a value at OFFSET bytes from base register BASE can be
2339    accessed using an unextended MIPS16 instruction.  MODE is the mode of
2340    the value.
2341
2342    Usually the offset in an unextended instruction is a 5-bit field.
2343    The offset is unsigned and shifted left once for LH and SH, twice
2344    for LW and SW, and so on.  An exception is LWSP and SWSP, which have
2345    an 8-bit immediate field that's shifted left twice.  */
2346
2347 static bool
2348 mips16_unextended_reference_p (enum machine_mode mode, rtx base,
2349                                unsigned HOST_WIDE_INT offset)
2350 {
2351   if (mode != BLKmode && offset % GET_MODE_SIZE (mode) == 0)
2352     {
2353       if (GET_MODE_SIZE (mode) == 4 && base == stack_pointer_rtx)
2354         return offset < 256U * GET_MODE_SIZE (mode);
2355       return offset < 32U * GET_MODE_SIZE (mode);
2356     }
2357   return false;
2358 }
2359
2360 /* Return the number of instructions needed to load or store a value
2361    of mode MODE at address X, assuming that BASE_INSN_LENGTH is the
2362    length of one instruction.  Return 0 if X isn't valid for MODE.
2363    Assume that multiword moves may need to be split into word moves
2364    if MIGHT_SPLIT_P, otherwise assume that a single load or store is
2365    enough.  */
2366
2367 int
2368 mips_address_insns (rtx x, enum machine_mode mode, bool might_split_p)
2369 {
2370   struct mips_address_info addr;
2371   int factor;
2372
2373   /* BLKmode is used for single unaligned loads and stores and should
2374      not count as a multiword mode.  (GET_MODE_SIZE (BLKmode) is pretty
2375      meaningless, so we have to single it out as a special case one way
2376      or the other.)  */
2377   if (mode != BLKmode && might_split_p)
2378     factor = (GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
2379   else
2380     factor = 1;
2381
2382   if (mips_classify_address (&addr, x, mode, false))
2383     switch (addr.type)
2384       {
2385       case ADDRESS_REG:
2386         if (TARGET_MIPS16
2387             && !mips16_unextended_reference_p (mode, addr.reg,
2388                                                UINTVAL (addr.offset)))
2389           return factor * 2;
2390         return factor;
2391
2392       case ADDRESS_LO_SUM:
2393         return TARGET_MIPS16 ? factor * 2 : factor;
2394
2395       case ADDRESS_CONST_INT:
2396         return factor;
2397
2398       case ADDRESS_SYMBOLIC:
2399         return factor * mips_symbol_insns (addr.symbol_type, mode);
2400       }
2401   return 0;
2402 }
2403
2404 /* Return true if X fits within an unsigned field of BITS bits that is
2405    shifted left SHIFT bits before being used.  */
2406
2407 bool
2408 mips_unsigned_immediate_p (unsigned HOST_WIDE_INT x, int bits, int shift = 0)
2409 {
2410   return (x & ((1 << shift) - 1)) == 0 && x < ((unsigned) 1 << (shift + bits));
2411 }
2412
2413 /* Return true if X fits within a signed field of BITS bits that is
2414    shifted left SHIFT bits before being used.  */
2415
2416 bool
2417 mips_signed_immediate_p (unsigned HOST_WIDE_INT x, int bits, int shift = 0)
2418 {
2419   x += 1 << (bits + shift - 1);
2420   return mips_unsigned_immediate_p (x, bits, shift);
2421 }
2422
2423 /* Return true if X is legitimate for accessing values of mode MODE,
2424    if it is based on a MIPS16 register, and if the offset satisfies
2425    OFFSET_PREDICATE.  */
2426
2427 bool
2428 m16_based_address_p (rtx x, enum machine_mode mode,
2429                      insn_operand_predicate_fn offset_predicate)
2430 {
2431   struct mips_address_info addr;
2432
2433   return (mips_classify_address (&addr, x, mode, false)
2434           && addr.type == ADDRESS_REG
2435           && M16_REG_P (REGNO (addr.reg))
2436           && offset_predicate (addr.offset, mode));
2437 }
2438
2439 /* Return true if X is a legitimate address that conforms to the requirements
2440    for a microMIPS LWSP or SWSP insn.  */
2441
2442 bool
2443 lwsp_swsp_address_p (rtx x, enum machine_mode mode)
2444 {
2445   struct mips_address_info addr;
2446
2447   return (mips_classify_address (&addr, x, mode, false)
2448           && addr.type == ADDRESS_REG
2449           && REGNO (addr.reg) == STACK_POINTER_REGNUM
2450           && uw5_operand (addr.offset, mode));
2451 }
2452
2453 /* Return true if X is a legitimate address with a 12-bit offset.
2454    MODE is the mode of the value being accessed.  */
2455
2456 bool
2457 umips_12bit_offset_address_p (rtx x, enum machine_mode mode)
2458 {
2459   struct mips_address_info addr;
2460
2461   return (mips_classify_address (&addr, x, mode, false)
2462           && addr.type == ADDRESS_REG
2463           && CONST_INT_P (addr.offset)
2464           && UMIPS_12BIT_OFFSET_P (INTVAL (addr.offset)));
2465 }
2466
2467 /* Return the number of instructions needed to load constant X,
2468    assuming that BASE_INSN_LENGTH is the length of one instruction.
2469    Return 0 if X isn't a valid constant.  */
2470
2471 int
2472 mips_const_insns (rtx x)
2473 {
2474   struct mips_integer_op codes[MIPS_MAX_INTEGER_OPS];
2475   enum mips_symbol_type symbol_type;
2476   rtx offset;
2477
2478   switch (GET_CODE (x))
2479     {
2480     case HIGH:
2481       if (!mips_symbolic_constant_p (XEXP (x, 0), SYMBOL_CONTEXT_LEA,
2482                                      &symbol_type)
2483           || !mips_split_p[symbol_type])
2484         return 0;
2485
2486       /* This is simply an LUI for normal mode.  It is an extended
2487          LI followed by an extended SLL for MIPS16.  */
2488       return TARGET_MIPS16 ? 4 : 1;
2489
2490     case CONST_INT:
2491       if (TARGET_MIPS16)
2492         /* Unsigned 8-bit constants can be loaded using an unextended
2493            LI instruction.  Unsigned 16-bit constants can be loaded
2494            using an extended LI.  Negative constants must be loaded
2495            using LI and then negated.  */
2496         return (IN_RANGE (INTVAL (x), 0, 255) ? 1
2497                 : SMALL_OPERAND_UNSIGNED (INTVAL (x)) ? 2
2498                 : IN_RANGE (-INTVAL (x), 0, 255) ? 2
2499                 : SMALL_OPERAND_UNSIGNED (-INTVAL (x)) ? 3
2500                 : 0);
2501
2502       return mips_build_integer (codes, INTVAL (x));
2503
2504     case CONST_DOUBLE:
2505     case CONST_VECTOR:
2506       /* Allow zeros for normal mode, where we can use $0.  */
2507       return !TARGET_MIPS16 && x == CONST0_RTX (GET_MODE (x)) ? 1 : 0;
2508
2509     case CONST:
2510       if (CONST_GP_P (x))
2511         return 1;
2512
2513       /* See if we can refer to X directly.  */
2514       if (mips_symbolic_constant_p (x, SYMBOL_CONTEXT_LEA, &symbol_type))
2515         return mips_symbol_insns (symbol_type, MAX_MACHINE_MODE);
2516
2517       /* Otherwise try splitting the constant into a base and offset.
2518          If the offset is a 16-bit value, we can load the base address
2519          into a register and then use (D)ADDIU to add in the offset.
2520          If the offset is larger, we can load the base and offset
2521          into separate registers and add them together with (D)ADDU.
2522          However, the latter is only possible before reload; during
2523          and after reload, we must have the option of forcing the
2524          constant into the pool instead.  */
2525       split_const (x, &x, &offset);
2526       if (offset != 0)
2527         {
2528           int n = mips_const_insns (x);
2529           if (n != 0)
2530             {
2531               if (SMALL_INT (offset))
2532                 return n + 1;
2533               else if (!targetm.cannot_force_const_mem (GET_MODE (x), x))
2534                 return n + 1 + mips_build_integer (codes, INTVAL (offset));
2535             }
2536         }
2537       return 0;
2538
2539     case SYMBOL_REF:
2540     case LABEL_REF:
2541       return mips_symbol_insns (mips_classify_symbol (x, SYMBOL_CONTEXT_LEA),
2542                                 MAX_MACHINE_MODE);
2543
2544     default:
2545       return 0;
2546     }
2547 }
2548
2549 /* X is a doubleword constant that can be handled by splitting it into
2550    two words and loading each word separately.  Return the number of
2551    instructions required to do this, assuming that BASE_INSN_LENGTH
2552    is the length of one instruction.  */
2553
2554 int
2555 mips_split_const_insns (rtx x)
2556 {
2557   unsigned int low, high;
2558
2559   low = mips_const_insns (mips_subword (x, false));
2560   high = mips_const_insns (mips_subword (x, true));
2561   gcc_assert (low > 0 && high > 0);
2562   return low + high;
2563 }
2564
2565 /* Return the number of instructions needed to implement INSN,
2566    given that it loads from or stores to MEM.  Assume that
2567    BASE_INSN_LENGTH is the length of one instruction.  */
2568
2569 int
2570 mips_load_store_insns (rtx mem, rtx insn)
2571 {
2572   enum machine_mode mode;
2573   bool might_split_p;
2574   rtx set;
2575
2576   gcc_assert (MEM_P (mem));
2577   mode = GET_MODE (mem);
2578
2579   /* Try to prove that INSN does not need to be split.  */
2580   might_split_p = GET_MODE_SIZE (mode) > UNITS_PER_WORD;
2581   if (might_split_p)
2582     {
2583       set = single_set (insn);
2584       if (set && !mips_split_move_insn_p (SET_DEST (set), SET_SRC (set), insn))
2585         might_split_p = false;
2586     }
2587
2588   return mips_address_insns (XEXP (mem, 0), mode, might_split_p);
2589 }
2590
2591 /* Return the number of instructions needed for an integer division,
2592    assuming that BASE_INSN_LENGTH is the length of one instruction.  */
2593
2594 int
2595 mips_idiv_insns (void)
2596 {
2597   int count;
2598
2599   count = 1;
2600   if (TARGET_CHECK_ZERO_DIV)
2601     {
2602       if (GENERATE_DIVIDE_TRAPS)
2603         count++;
2604       else
2605         count += 2;
2606     }
2607
2608   if (TARGET_FIX_R4000 || TARGET_FIX_R4400)
2609     count++;
2610   return count;
2611 }
2612 \f
2613 /* Emit a move from SRC to DEST.  Assume that the move expanders can
2614    handle all moves if !can_create_pseudo_p ().  The distinction is
2615    important because, unlike emit_move_insn, the move expanders know
2616    how to force Pmode objects into the constant pool even when the
2617    constant pool address is not itself legitimate.  */
2618
2619 rtx
2620 mips_emit_move (rtx dest, rtx src)
2621 {
2622   return (can_create_pseudo_p ()
2623           ? emit_move_insn (dest, src)
2624           : emit_move_insn_1 (dest, src));
2625 }
2626
2627 /* Emit a move from SRC to DEST, splitting compound moves into individual
2628    instructions.  SPLIT_TYPE is the type of split to perform.  */
2629
2630 static void
2631 mips_emit_move_or_split (rtx dest, rtx src, enum mips_split_type split_type)
2632 {
2633   if (mips_split_move_p (dest, src, split_type))
2634     mips_split_move (dest, src, split_type);
2635   else
2636     mips_emit_move (dest, src);
2637 }
2638
2639 /* Emit an instruction of the form (set TARGET (CODE OP0)).  */
2640
2641 static void
2642 mips_emit_unary (enum rtx_code code, rtx target, rtx op0)
2643 {
2644   emit_insn (gen_rtx_SET (VOIDmode, target,
2645                           gen_rtx_fmt_e (code, GET_MODE (op0), op0)));
2646 }
2647
2648 /* Compute (CODE OP0) and store the result in a new register of mode MODE.
2649    Return that new register.  */
2650
2651 static rtx
2652 mips_force_unary (enum machine_mode mode, enum rtx_code code, rtx op0)
2653 {
2654   rtx reg;
2655
2656   reg = gen_reg_rtx (mode);
2657   mips_emit_unary (code, reg, op0);
2658   return reg;
2659 }
2660
2661 /* Emit an instruction of the form (set TARGET (CODE OP0 OP1)).  */
2662
2663 void
2664 mips_emit_binary (enum rtx_code code, rtx target, rtx op0, rtx op1)
2665 {
2666   emit_insn (gen_rtx_SET (VOIDmode, target,
2667                           gen_rtx_fmt_ee (code, GET_MODE (target), op0, op1)));
2668 }
2669
2670 /* Compute (CODE OP0 OP1) and store the result in a new register
2671    of mode MODE.  Return that new register.  */
2672
2673 static rtx
2674 mips_force_binary (enum machine_mode mode, enum rtx_code code, rtx op0, rtx op1)
2675 {
2676   rtx reg;
2677
2678   reg = gen_reg_rtx (mode);
2679   mips_emit_binary (code, reg, op0, op1);
2680   return reg;
2681 }
2682
2683 /* Copy VALUE to a register and return that register.  If new pseudos
2684    are allowed, copy it into a new register, otherwise use DEST.  */
2685
2686 static rtx
2687 mips_force_temporary (rtx dest, rtx value)
2688 {
2689   if (can_create_pseudo_p ())
2690     return force_reg (Pmode, value);
2691   else
2692     {
2693       mips_emit_move (dest, value);
2694       return dest;
2695     }
2696 }
2697
2698 /* Emit a call sequence with call pattern PATTERN and return the call
2699    instruction itself (which is not necessarily the last instruction
2700    emitted).  ORIG_ADDR is the original, unlegitimized address,
2701    ADDR is the legitimized form, and LAZY_P is true if the call
2702    address is lazily-bound.  */
2703
2704 static rtx
2705 mips_emit_call_insn (rtx pattern, rtx orig_addr, rtx addr, bool lazy_p)
2706 {
2707   rtx insn, reg;
2708
2709   insn = emit_call_insn (pattern);
2710
2711   if (TARGET_MIPS16 && mips_use_pic_fn_addr_reg_p (orig_addr))
2712     {
2713       /* MIPS16 JALRs only take MIPS16 registers.  If the target
2714          function requires $25 to be valid on entry, we must copy it
2715          there separately.  The move instruction can be put in the
2716          call's delay slot.  */
2717       reg = gen_rtx_REG (Pmode, PIC_FUNCTION_ADDR_REGNUM);
2718       emit_insn_before (gen_move_insn (reg, addr), insn);
2719       use_reg (&CALL_INSN_FUNCTION_USAGE (insn), reg);
2720     }
2721
2722   if (lazy_p)
2723     /* Lazy-binding stubs require $gp to be valid on entry.  */
2724     use_reg (&CALL_INSN_FUNCTION_USAGE (insn), pic_offset_table_rtx);
2725
2726   if (TARGET_USE_GOT)
2727     {
2728       /* See the comment above load_call<mode> for details.  */
2729       use_reg (&CALL_INSN_FUNCTION_USAGE (insn),
2730                gen_rtx_REG (Pmode, GOT_VERSION_REGNUM));
2731       emit_insn (gen_update_got_version ());
2732     }
2733   return insn;
2734 }
2735 \f
2736 /* Wrap symbol or label BASE in an UNSPEC address of type SYMBOL_TYPE,
2737    then add CONST_INT OFFSET to the result.  */
2738
2739 static rtx
2740 mips_unspec_address_offset (rtx base, rtx offset,
2741                             enum mips_symbol_type symbol_type)
2742 {
2743   base = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, base),
2744                          UNSPEC_ADDRESS_FIRST + symbol_type);
2745   if (offset != const0_rtx)
2746     base = gen_rtx_PLUS (Pmode, base, offset);
2747   return gen_rtx_CONST (Pmode, base);
2748 }
2749
2750 /* Return an UNSPEC address with underlying address ADDRESS and symbol
2751    type SYMBOL_TYPE.  */
2752
2753 rtx
2754 mips_unspec_address (rtx address, enum mips_symbol_type symbol_type)
2755 {
2756   rtx base, offset;
2757
2758   split_const (address, &base, &offset);
2759   return mips_unspec_address_offset (base, offset, symbol_type);
2760 }
2761
2762 /* If OP is an UNSPEC address, return the address to which it refers,
2763    otherwise return OP itself.  */
2764
2765 rtx
2766 mips_strip_unspec_address (rtx op)
2767 {
2768   rtx base, offset;
2769
2770   split_const (op, &base, &offset);
2771   if (UNSPEC_ADDRESS_P (base))
2772     op = plus_constant (Pmode, UNSPEC_ADDRESS (base), INTVAL (offset));
2773   return op;
2774 }
2775
2776 /* If mips_unspec_address (ADDR, SYMBOL_TYPE) is a 32-bit value, add the
2777    high part to BASE and return the result.  Just return BASE otherwise.
2778    TEMP is as for mips_force_temporary.
2779
2780    The returned expression can be used as the first operand to a LO_SUM.  */
2781
2782 static rtx
2783 mips_unspec_offset_high (rtx temp, rtx base, rtx addr,
2784                          enum mips_symbol_type symbol_type)
2785 {
2786   if (mips_split_p[symbol_type])
2787     {
2788       addr = gen_rtx_HIGH (Pmode, mips_unspec_address (addr, symbol_type));
2789       addr = mips_force_temporary (temp, addr);
2790       base = mips_force_temporary (temp, gen_rtx_PLUS (Pmode, addr, base));
2791     }
2792   return base;
2793 }
2794 \f
2795 /* Return an instruction that copies $gp into register REG.  We want
2796    GCC to treat the register's value as constant, so that its value
2797    can be rematerialized on demand.  */
2798
2799 static rtx
2800 gen_load_const_gp (rtx reg)
2801 {
2802   return PMODE_INSN (gen_load_const_gp, (reg));
2803 }
2804
2805 /* Return a pseudo register that contains the value of $gp throughout
2806    the current function.  Such registers are needed by MIPS16 functions,
2807    for which $gp itself is not a valid base register or addition operand.  */
2808
2809 static rtx
2810 mips16_gp_pseudo_reg (void)
2811 {
2812   if (cfun->machine->mips16_gp_pseudo_rtx == NULL_RTX)
2813     {
2814       rtx insn, scan;
2815
2816       cfun->machine->mips16_gp_pseudo_rtx = gen_reg_rtx (Pmode);
2817
2818       push_topmost_sequence ();
2819
2820       scan = get_insns ();
2821       while (NEXT_INSN (scan) && !INSN_P (NEXT_INSN (scan)))
2822         scan = NEXT_INSN (scan);
2823
2824       insn = gen_load_const_gp (cfun->machine->mips16_gp_pseudo_rtx);
2825       insn = emit_insn_after (insn, scan);
2826       INSN_LOCATION (insn) = 0;
2827
2828       pop_topmost_sequence ();
2829     }
2830
2831   return cfun->machine->mips16_gp_pseudo_rtx;
2832 }
2833
2834 /* Return a base register that holds pic_offset_table_rtx.
2835    TEMP, if nonnull, is a scratch Pmode base register.  */
2836
2837 rtx
2838 mips_pic_base_register (rtx temp)
2839 {
2840   if (!TARGET_MIPS16)
2841     return pic_offset_table_rtx;
2842
2843   if (currently_expanding_to_rtl)
2844     return mips16_gp_pseudo_reg ();
2845
2846   if (can_create_pseudo_p ())
2847     temp = gen_reg_rtx (Pmode);
2848
2849   if (TARGET_USE_GOT)
2850     /* The first post-reload split exposes all references to $gp
2851        (both uses and definitions).  All references must remain
2852        explicit after that point.
2853
2854        It is safe to introduce uses of $gp at any time, so for
2855        simplicity, we do that before the split too.  */
2856     mips_emit_move (temp, pic_offset_table_rtx);
2857   else
2858     emit_insn (gen_load_const_gp (temp));
2859   return temp;
2860 }
2861
2862 /* Return the RHS of a load_call<mode> insn.  */
2863
2864 static rtx
2865 mips_unspec_call (rtx reg, rtx symbol)
2866 {
2867   rtvec vec;
2868
2869   vec = gen_rtvec (3, reg, symbol, gen_rtx_REG (SImode, GOT_VERSION_REGNUM));
2870   return gen_rtx_UNSPEC (Pmode, vec, UNSPEC_LOAD_CALL);
2871 }
2872
2873 /* If SRC is the RHS of a load_call<mode> insn, return the underlying symbol
2874    reference.  Return NULL_RTX otherwise.  */
2875
2876 static rtx
2877 mips_strip_unspec_call (rtx src)
2878 {
2879   if (GET_CODE (src) == UNSPEC && XINT (src, 1) == UNSPEC_LOAD_CALL)
2880     return mips_strip_unspec_address (XVECEXP (src, 0, 1));
2881   return NULL_RTX;
2882 }
2883
2884 /* Create and return a GOT reference of type TYPE for address ADDR.
2885    TEMP, if nonnull, is a scratch Pmode base register.  */
2886
2887 rtx
2888 mips_got_load (rtx temp, rtx addr, enum mips_symbol_type type)
2889 {
2890   rtx base, high, lo_sum_symbol;
2891
2892   base = mips_pic_base_register (temp);
2893
2894   /* If we used the temporary register to load $gp, we can't use
2895      it for the high part as well.  */
2896   if (temp != NULL && reg_overlap_mentioned_p (base, temp))
2897     temp = NULL;
2898
2899   high = mips_unspec_offset_high (temp, base, addr, type);
2900   lo_sum_symbol = mips_unspec_address (addr, type);
2901
2902   if (type == SYMBOL_GOTOFF_CALL)
2903     return mips_unspec_call (high, lo_sum_symbol);
2904   else
2905     return PMODE_INSN (gen_unspec_got, (high, lo_sum_symbol));
2906 }
2907
2908 /* If MODE is MAX_MACHINE_MODE, ADDR appears as a move operand, otherwise
2909    it appears in a MEM of that mode.  Return true if ADDR is a legitimate
2910    constant in that context and can be split into high and low parts.
2911    If so, and if LOW_OUT is nonnull, emit the high part and store the
2912    low part in *LOW_OUT.  Leave *LOW_OUT unchanged otherwise.
2913
2914    TEMP is as for mips_force_temporary and is used to load the high
2915    part into a register.
2916
2917    When MODE is MAX_MACHINE_MODE, the low part is guaranteed to be
2918    a legitimize SET_SRC for an .md pattern, otherwise the low part
2919    is guaranteed to be a legitimate address for mode MODE.  */
2920
2921 bool
2922 mips_split_symbol (rtx temp, rtx addr, enum machine_mode mode, rtx *low_out)
2923 {
2924   enum mips_symbol_context context;
2925   enum mips_symbol_type symbol_type;
2926   rtx high;
2927
2928   context = (mode == MAX_MACHINE_MODE
2929              ? SYMBOL_CONTEXT_LEA
2930              : SYMBOL_CONTEXT_MEM);
2931   if (GET_CODE (addr) == HIGH && context == SYMBOL_CONTEXT_LEA)
2932     {
2933       addr = XEXP (addr, 0);
2934       if (mips_symbolic_constant_p (addr, context, &symbol_type)
2935           && mips_symbol_insns (symbol_type, mode) > 0
2936           && mips_split_hi_p[symbol_type])
2937         {
2938           if (low_out)
2939             switch (symbol_type)
2940               {
2941               case SYMBOL_GOT_PAGE_OFST:
2942                 /* The high part of a page/ofst pair is loaded from the GOT.  */
2943                 *low_out = mips_got_load (temp, addr, SYMBOL_GOTOFF_PAGE);
2944                 break;
2945
2946               default:
2947                 gcc_unreachable ();
2948               }
2949           return true;
2950         }
2951     }
2952   else
2953     {
2954       if (mips_symbolic_constant_p (addr, context, &symbol_type)
2955           && mips_symbol_insns (symbol_type, mode) > 0
2956           && mips_split_p[symbol_type])
2957         {
2958           if (low_out)
2959             switch (symbol_type)
2960               {
2961               case SYMBOL_GOT_DISP:
2962                 /* SYMBOL_GOT_DISP symbols are loaded from the GOT.  */
2963                 *low_out = mips_got_load (temp, addr, SYMBOL_GOTOFF_DISP);
2964                 break;
2965
2966               case SYMBOL_GP_RELATIVE:
2967                 high = mips_pic_base_register (temp);
2968                 *low_out = gen_rtx_LO_SUM (Pmode, high, addr);
2969                 break;
2970
2971               default:
2972                 high = gen_rtx_HIGH (Pmode, copy_rtx (addr));
2973                 high = mips_force_temporary (temp, high);
2974                 *low_out = gen_rtx_LO_SUM (Pmode, high, addr);
2975                 break;
2976               }
2977           return true;
2978         }
2979     }
2980   return false;
2981 }
2982
2983 /* Return a legitimate address for REG + OFFSET.  TEMP is as for
2984    mips_force_temporary; it is only needed when OFFSET is not a
2985    SMALL_OPERAND.  */
2986
2987 static rtx
2988 mips_add_offset (rtx temp, rtx reg, HOST_WIDE_INT offset)
2989 {
2990   if (!SMALL_OPERAND (offset))
2991     {
2992       rtx high;
2993
2994       if (TARGET_MIPS16)
2995         {
2996           /* Load the full offset into a register so that we can use
2997              an unextended instruction for the address itself.  */
2998           high = GEN_INT (offset);
2999           offset = 0;
3000         }
3001       else
3002         {
3003           /* Leave OFFSET as a 16-bit offset and put the excess in HIGH.
3004              The addition inside the macro CONST_HIGH_PART may cause an
3005              overflow, so we need to force a sign-extension check.  */
3006           high = gen_int_mode (CONST_HIGH_PART (offset), Pmode);
3007           offset = CONST_LOW_PART (offset);
3008         }
3009       high = mips_force_temporary (temp, high);
3010       reg = mips_force_temporary (temp, gen_rtx_PLUS (Pmode, high, reg));
3011     }
3012   return plus_constant (Pmode, reg, offset);
3013 }
3014 \f
3015 /* The __tls_get_attr symbol.  */
3016 static GTY(()) rtx mips_tls_symbol;
3017
3018 /* Return an instruction sequence that calls __tls_get_addr.  SYM is
3019    the TLS symbol we are referencing and TYPE is the symbol type to use
3020    (either global dynamic or local dynamic).  V0 is an RTX for the
3021    return value location.  */
3022
3023 static rtx
3024 mips_call_tls_get_addr (rtx sym, enum mips_symbol_type type, rtx v0)
3025 {
3026   rtx insn, loc, a0;
3027
3028   a0 = gen_rtx_REG (Pmode, GP_ARG_FIRST);
3029
3030   if (!mips_tls_symbol)
3031     mips_tls_symbol = init_one_libfunc ("__tls_get_addr");
3032
3033   loc = mips_unspec_address (sym, type);
3034
3035   start_sequence ();
3036
3037   emit_insn (gen_rtx_SET (Pmode, a0,
3038                           gen_rtx_LO_SUM (Pmode, pic_offset_table_rtx, loc)));
3039   insn = mips_expand_call (MIPS_CALL_NORMAL, v0, mips_tls_symbol,
3040                            const0_rtx, NULL_RTX, false);
3041   RTL_CONST_CALL_P (insn) = 1;
3042   use_reg (&CALL_INSN_FUNCTION_USAGE (insn), a0);
3043   insn = get_insns ();
3044
3045   end_sequence ();
3046
3047   return insn;
3048 }
3049
3050 /* Return a pseudo register that contains the current thread pointer.  */
3051
3052 rtx
3053 mips_expand_thread_pointer (rtx tp)
3054 {
3055   rtx fn;
3056
3057   if (TARGET_MIPS16)
3058     {
3059       mips_need_mips16_rdhwr_p = true;
3060       fn = mips16_stub_function ("__mips16_rdhwr");
3061       SYMBOL_REF_FLAGS (fn) |= SYMBOL_FLAG_LOCAL;
3062       if (!call_insn_operand (fn, VOIDmode))
3063         fn = force_reg (Pmode, fn);
3064       emit_insn (PMODE_INSN (gen_tls_get_tp_mips16, (tp, fn)));
3065     }
3066   else
3067     emit_insn (PMODE_INSN (gen_tls_get_tp, (tp)));
3068   return tp;
3069 }
3070
3071 static rtx
3072 mips_get_tp (void)
3073 {
3074   return mips_expand_thread_pointer (gen_reg_rtx (Pmode));
3075 }
3076
3077 /* Generate the code to access LOC, a thread-local SYMBOL_REF, and return
3078    its address.  The return value will be both a valid address and a valid
3079    SET_SRC (either a REG or a LO_SUM).  */
3080
3081 static rtx
3082 mips_legitimize_tls_address (rtx loc)
3083 {
3084   rtx dest, insn, v0, tp, tmp1, tmp2, eqv, offset;
3085   enum tls_model model;
3086
3087   model = SYMBOL_REF_TLS_MODEL (loc);
3088   /* Only TARGET_ABICALLS code can have more than one module; other
3089      code must be be static and should not use a GOT.  All TLS models
3090      reduce to local exec in this situation.  */
3091   if (!TARGET_ABICALLS)
3092     model = TLS_MODEL_LOCAL_EXEC;
3093
3094   switch (model)
3095     {
3096     case TLS_MODEL_GLOBAL_DYNAMIC:
3097       v0 = gen_rtx_REG (Pmode, GP_RETURN);
3098       insn = mips_call_tls_get_addr (loc, SYMBOL_TLSGD, v0);
3099       dest = gen_reg_rtx (Pmode);
3100       emit_libcall_block (insn, dest, v0, loc);
3101       break;
3102
3103     case TLS_MODEL_LOCAL_DYNAMIC:
3104       v0 = gen_rtx_REG (Pmode, GP_RETURN);
3105       insn = mips_call_tls_get_addr (loc, SYMBOL_TLSLDM, v0);
3106       tmp1 = gen_reg_rtx (Pmode);
3107
3108       /* Attach a unique REG_EQUIV, to allow the RTL optimizers to
3109          share the LDM result with other LD model accesses.  */
3110       eqv = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx),
3111                             UNSPEC_TLS_LDM);
3112       emit_libcall_block (insn, tmp1, v0, eqv);
3113
3114       offset = mips_unspec_address (loc, SYMBOL_DTPREL);
3115       if (mips_split_p[SYMBOL_DTPREL])
3116         {
3117           tmp2 = mips_unspec_offset_high (NULL, tmp1, loc, SYMBOL_DTPREL);
3118           dest = gen_rtx_LO_SUM (Pmode, tmp2, offset);
3119         }
3120       else
3121         dest = expand_binop (Pmode, add_optab, tmp1, offset,
3122                              0, 0, OPTAB_DIRECT);
3123       break;
3124
3125     case TLS_MODEL_INITIAL_EXEC:
3126       tp = mips_get_tp ();
3127       tmp1 = gen_reg_rtx (Pmode);
3128       tmp2 = mips_unspec_address (loc, SYMBOL_GOTTPREL);
3129       if (Pmode == DImode)
3130         emit_insn (gen_load_gotdi (tmp1, pic_offset_table_rtx, tmp2));
3131       else
3132         emit_insn (gen_load_gotsi (tmp1, pic_offset_table_rtx, tmp2));
3133       dest = gen_reg_rtx (Pmode);
3134       emit_insn (gen_add3_insn (dest, tmp1, tp));
3135       break;
3136
3137     case TLS_MODEL_LOCAL_EXEC:
3138       tmp1 = mips_get_tp ();
3139       offset = mips_unspec_address (loc, SYMBOL_TPREL);
3140       if (mips_split_p[SYMBOL_TPREL])
3141         {
3142           tmp2 = mips_unspec_offset_high (NULL, tmp1, loc, SYMBOL_TPREL);
3143           dest = gen_rtx_LO_SUM (Pmode, tmp2, offset);
3144         }
3145       else
3146         dest = expand_binop (Pmode, add_optab, tmp1, offset,
3147                              0, 0, OPTAB_DIRECT);
3148       break;
3149
3150     default:
3151       gcc_unreachable ();
3152     }
3153   return dest;
3154 }
3155 \f
3156 /* If X is not a valid address for mode MODE, force it into a register.  */
3157
3158 static rtx
3159 mips_force_address (rtx x, enum machine_mode mode)
3160 {
3161   if (!mips_legitimate_address_p (mode, x, false))
3162     x = force_reg (Pmode, x);
3163   return x;
3164 }
3165
3166 /* This function is used to implement LEGITIMIZE_ADDRESS.  If X can
3167    be legitimized in a way that the generic machinery might not expect,
3168    return a new address, otherwise return NULL.  MODE is the mode of
3169    the memory being accessed.  */
3170
3171 static rtx
3172 mips_legitimize_address (rtx x, rtx oldx ATTRIBUTE_UNUSED,
3173                          enum machine_mode mode)
3174 {
3175   rtx base, addr;
3176   HOST_WIDE_INT offset;
3177
3178   if (mips_tls_symbol_p (x))
3179     return mips_legitimize_tls_address (x);
3180
3181   /* See if the address can split into a high part and a LO_SUM.  */
3182   if (mips_split_symbol (NULL, x, mode, &addr))
3183     return mips_force_address (addr, mode);
3184
3185   /* Handle BASE + OFFSET using mips_add_offset.  */
3186   mips_split_plus (x, &base, &offset);
3187   if (offset != 0)
3188     {
3189       if (!mips_valid_base_register_p (base, mode, false))
3190         base = copy_to_mode_reg (Pmode, base);
3191       addr = mips_add_offset (NULL, base, offset);
3192       return mips_force_address (addr, mode);
3193     }
3194
3195   return x;
3196 }
3197
3198 /* Load VALUE into DEST.  TEMP is as for mips_force_temporary.  */
3199
3200 void
3201 mips_move_integer (rtx temp, rtx dest, unsigned HOST_WIDE_INT value)
3202 {
3203   struct mips_integer_op codes[MIPS_MAX_INTEGER_OPS];
3204   enum machine_mode mode;
3205   unsigned int i, num_ops;
3206   rtx x;
3207
3208   mode = GET_MODE (dest);
3209   num_ops = mips_build_integer (codes, value);
3210
3211   /* Apply each binary operation to X.  Invariant: X is a legitimate
3212      source operand for a SET pattern.  */
3213   x = GEN_INT (codes[0].value);
3214   for (i = 1; i < num_ops; i++)
3215     {
3216       if (!can_create_pseudo_p ())
3217         {
3218           emit_insn (gen_rtx_SET (VOIDmode, temp, x));
3219           x = temp;
3220         }
3221       else
3222         x = force_reg (mode, x);
3223       x = gen_rtx_fmt_ee (codes[i].code, mode, x, GEN_INT (codes[i].value));
3224     }
3225
3226   emit_insn (gen_rtx_SET (VOIDmode, dest, x));
3227 }
3228
3229 /* Subroutine of mips_legitimize_move.  Move constant SRC into register
3230    DEST given that SRC satisfies immediate_operand but doesn't satisfy
3231    move_operand.  */
3232
3233 static void
3234 mips_legitimize_const_move (enum machine_mode mode, rtx dest, rtx src)
3235 {
3236   rtx base, offset;
3237
3238   /* Split moves of big integers into smaller pieces.  */
3239   if (splittable_const_int_operand (src, mode))
3240     {
3241       mips_move_integer (dest, dest, INTVAL (src));
3242       return;
3243     }
3244
3245   /* Split moves of symbolic constants into high/low pairs.  */
3246   if (mips_split_symbol (dest, src, MAX_MACHINE_MODE, &src))
3247     {
3248       emit_insn (gen_rtx_SET (VOIDmode, dest, src));
3249       return;
3250     }
3251
3252   /* Generate the appropriate access sequences for TLS symbols.  */
3253   if (mips_tls_symbol_p (src))
3254     {
3255       mips_emit_move (dest, mips_legitimize_tls_address (src));
3256       return;
3257     }
3258
3259   /* If we have (const (plus symbol offset)), and that expression cannot
3260      be forced into memory, load the symbol first and add in the offset.
3261      In non-MIPS16 mode, prefer to do this even if the constant _can_ be
3262      forced into memory, as it usually produces better code.  */
3263   split_const (src, &base, &offset);
3264   if (offset != const0_rtx
3265       && (targetm.cannot_force_const_mem (mode, src)
3266           || (!TARGET_MIPS16 && can_create_pseudo_p ())))
3267     {
3268       base = mips_force_temporary (dest, base);
3269       mips_emit_move (dest, mips_add_offset (NULL, base, INTVAL (offset)));
3270       return;
3271     }
3272
3273   src = force_const_mem (mode, src);
3274
3275   /* When using explicit relocs, constant pool references are sometimes
3276      not legitimate addresses.  */
3277   mips_split_symbol (dest, XEXP (src, 0), mode, &XEXP (src, 0));
3278   mips_emit_move (dest, src);
3279 }
3280
3281 /* If (set DEST SRC) is not a valid move instruction, emit an equivalent
3282    sequence that is valid.  */
3283
3284 bool
3285 mips_legitimize_move (enum machine_mode mode, rtx dest, rtx src)
3286 {
3287   if (!register_operand (dest, mode) && !reg_or_0_operand (src, mode))
3288     {
3289       mips_emit_move (dest, force_reg (mode, src));
3290       return true;
3291     }
3292
3293   /* We need to deal with constants that would be legitimate
3294      immediate_operands but aren't legitimate move_operands.  */
3295   if (CONSTANT_P (src) && !move_operand (src, mode))
3296     {
3297       mips_legitimize_const_move (mode, dest, src);
3298       set_unique_reg_note (get_last_insn (), REG_EQUAL, copy_rtx (src));
3299       return true;
3300     }
3301   return false;
3302 }
3303 \f
3304 /* Return true if value X in context CONTEXT is a small-data address
3305    that can be rewritten as a LO_SUM.  */
3306
3307 static bool
3308 mips_rewrite_small_data_p (rtx x, enum mips_symbol_context context)
3309 {
3310   enum mips_symbol_type symbol_type;
3311
3312   return (mips_lo_relocs[SYMBOL_GP_RELATIVE]
3313           && !mips_split_p[SYMBOL_GP_RELATIVE]
3314           && mips_symbolic_constant_p (x, context, &symbol_type)
3315           && symbol_type == SYMBOL_GP_RELATIVE);
3316 }
3317
3318 /* A for_each_rtx callback for mips_small_data_pattern_p.  DATA is the
3319    containing MEM, or null if none.  */
3320
3321 static int
3322 mips_small_data_pattern_1 (rtx *loc, void *data)
3323 {
3324   enum mips_symbol_context context;
3325
3326   /* Ignore things like "g" constraints in asms.  We make no particular
3327      guarantee about which symbolic constants are acceptable as asm operands
3328      versus which must be forced into a GPR.  */
3329   if (GET_CODE (*loc) == LO_SUM || GET_CODE (*loc) == ASM_OPERANDS)
3330     return -1;
3331
3332   if (MEM_P (*loc))
3333     {
3334       if (for_each_rtx (&XEXP (*loc, 0), mips_small_data_pattern_1, *loc))
3335         return 1;
3336       return -1;
3337     }
3338
3339   context = data ? SYMBOL_CONTEXT_MEM : SYMBOL_CONTEXT_LEA;
3340   return mips_rewrite_small_data_p (*loc, context);
3341 }
3342
3343 /* Return true if OP refers to small data symbols directly, not through
3344    a LO_SUM.  */
3345
3346 bool
3347 mips_small_data_pattern_p (rtx op)
3348 {
3349   return for_each_rtx (&op, mips_small_data_pattern_1, NULL);
3350 }
3351
3352 /* A for_each_rtx callback, used by mips_rewrite_small_data.
3353    DATA is the containing MEM, or null if none.  */
3354
3355 static int
3356 mips_rewrite_small_data_1 (rtx *loc, void *data)
3357 {
3358   enum mips_symbol_context context;
3359
3360   if (MEM_P (*loc))
3361     {
3362       for_each_rtx (&XEXP (*loc, 0), mips_rewrite_small_data_1, *loc);
3363       return -1;
3364     }
3365
3366   context = data ? SYMBOL_CONTEXT_MEM : SYMBOL_CONTEXT_LEA;
3367   if (mips_rewrite_small_data_p (*loc, context))
3368     *loc = gen_rtx_LO_SUM (Pmode, pic_offset_table_rtx, *loc);
3369
3370   if (GET_CODE (*loc) == LO_SUM)
3371     return -1;
3372
3373   return 0;
3374 }
3375
3376 /* Rewrite instruction pattern PATTERN so that it refers to small data
3377    using explicit relocations.  */
3378
3379 rtx
3380 mips_rewrite_small_data (rtx pattern)
3381 {
3382   pattern = copy_insn (pattern);
3383   for_each_rtx (&pattern, mips_rewrite_small_data_1, NULL);
3384   return pattern;
3385 }
3386 \f
3387 /* The cost of loading values from the constant pool.  It should be
3388    larger than the cost of any constant we want to synthesize inline.  */
3389 #define CONSTANT_POOL_COST COSTS_N_INSNS (TARGET_MIPS16 ? 4 : 8)
3390
3391 /* Return the cost of X when used as an operand to the MIPS16 instruction
3392    that implements CODE.  Return -1 if there is no such instruction, or if
3393    X is not a valid immediate operand for it.  */
3394
3395 static int
3396 mips16_constant_cost (int code, HOST_WIDE_INT x)
3397 {
3398   switch (code)
3399     {
3400     case ASHIFT:
3401     case ASHIFTRT:
3402     case LSHIFTRT:
3403       /* Shifts by between 1 and 8 bits (inclusive) are unextended,
3404          other shifts are extended.  The shift patterns truncate the shift
3405          count to the right size, so there are no out-of-range values.  */
3406       if (IN_RANGE (x, 1, 8))
3407         return 0;
3408       return COSTS_N_INSNS (1);
3409
3410     case PLUS:
3411       if (IN_RANGE (x, -128, 127))
3412         return 0;
3413       if (SMALL_OPERAND (x))
3414         return COSTS_N_INSNS (1);
3415       return -1;
3416
3417     case LEU:
3418       /* Like LE, but reject the always-true case.  */
3419       if (x == -1)
3420         return -1;
3421     case LE:
3422       /* We add 1 to the immediate and use SLT.  */
3423       x += 1;
3424     case XOR:
3425       /* We can use CMPI for an xor with an unsigned 16-bit X.  */
3426     case LT:
3427     case LTU:
3428       if (IN_RANGE (x, 0, 255))
3429         return 0;
3430       if (SMALL_OPERAND_UNSIGNED (x))
3431         return COSTS_N_INSNS (1);
3432       return -1;
3433
3434     case EQ:
3435     case NE:
3436       /* Equality comparisons with 0 are cheap.  */
3437       if (x == 0)
3438         return 0;
3439       return -1;
3440
3441     default:
3442       return -1;
3443     }
3444 }
3445
3446 /* Return true if there is a non-MIPS16 instruction that implements CODE
3447    and if that instruction accepts X as an immediate operand.  */
3448
3449 static int
3450 mips_immediate_operand_p (int code, HOST_WIDE_INT x)
3451 {
3452   switch (code)
3453     {
3454     case ASHIFT:
3455     case ASHIFTRT:
3456     case LSHIFTRT:
3457       /* All shift counts are truncated to a valid constant.  */
3458       return true;
3459
3460     case ROTATE:
3461     case ROTATERT:
3462       /* Likewise rotates, if the target supports rotates at all.  */
3463       return ISA_HAS_ROR;
3464
3465     case AND:
3466     case IOR:
3467     case XOR:
3468       /* These instructions take 16-bit unsigned immediates.  */
3469       return SMALL_OPERAND_UNSIGNED (x);
3470
3471     case PLUS:
3472     case LT:
3473     case LTU:
3474       /* These instructions take 16-bit signed immediates.  */
3475       return SMALL_OPERAND (x);
3476
3477     case EQ:
3478     case NE:
3479     case GT:
3480     case GTU:
3481       /* The "immediate" forms of these instructions are really
3482          implemented as comparisons with register 0.  */
3483       return x == 0;
3484
3485     case GE:
3486     case GEU:
3487       /* Likewise, meaning that the only valid immediate operand is 1.  */
3488       return x == 1;
3489
3490     case LE:
3491       /* We add 1 to the immediate and use SLT.  */
3492       return SMALL_OPERAND (x + 1);
3493
3494     case LEU:
3495       /* Likewise SLTU, but reject the always-true case.  */
3496       return SMALL_OPERAND (x + 1) && x + 1 != 0;
3497
3498     case SIGN_EXTRACT:
3499     case ZERO_EXTRACT:
3500       /* The bit position and size are immediate operands.  */
3501       return ISA_HAS_EXT_INS;
3502
3503     default:
3504       /* By default assume that $0 can be used for 0.  */
3505       return x == 0;
3506     }
3507 }
3508
3509 /* Return the cost of binary operation X, given that the instruction
3510    sequence for a word-sized or smaller operation has cost SINGLE_COST
3511    and that the sequence of a double-word operation has cost DOUBLE_COST.
3512    If SPEED is true, optimize for speed otherwise optimize for size.  */
3513
3514 static int
3515 mips_binary_cost (rtx x, int single_cost, int double_cost, bool speed)
3516 {
3517   int cost;
3518
3519   if (GET_MODE_SIZE (GET_MODE (x)) == UNITS_PER_WORD * 2)
3520     cost = double_cost;
3521   else
3522     cost = single_cost;
3523   return (cost
3524           + set_src_cost (XEXP (x, 0), speed)
3525           + rtx_cost (XEXP (x, 1), GET_CODE (x), 1, speed));
3526 }
3527
3528 /* Return the cost of floating-point multiplications of mode MODE.  */
3529
3530 static int
3531 mips_fp_mult_cost (enum machine_mode mode)
3532 {
3533   return mode == DFmode ? mips_cost->fp_mult_df : mips_cost->fp_mult_sf;
3534 }
3535
3536 /* Return the cost of floating-point divisions of mode MODE.  */
3537
3538 static int
3539 mips_fp_div_cost (enum machine_mode mode)
3540 {
3541   return mode == DFmode ? mips_cost->fp_div_df : mips_cost->fp_div_sf;
3542 }
3543
3544 /* Return the cost of sign-extending OP to mode MODE, not including the
3545    cost of OP itself.  */
3546
3547 static int
3548 mips_sign_extend_cost (enum machine_mode mode, rtx op)
3549 {
3550   if (MEM_P (op))
3551     /* Extended loads are as cheap as unextended ones.  */
3552     return 0;
3553
3554   if (TARGET_64BIT && mode == DImode && GET_MODE (op) == SImode)
3555     /* A sign extension from SImode to DImode in 64-bit mode is free.  */
3556     return 0;
3557
3558   if (ISA_HAS_SEB_SEH || GENERATE_MIPS16E)
3559     /* We can use SEB or SEH.  */
3560     return COSTS_N_INSNS (1);
3561
3562   /* We need to use a shift left and a shift right.  */
3563   return COSTS_N_INSNS (TARGET_MIPS16 ? 4 : 2);
3564 }
3565
3566 /* Return the cost of zero-extending OP to mode MODE, not including the
3567    cost of OP itself.  */
3568
3569 static int
3570 mips_zero_extend_cost (enum machine_mode mode, rtx op)
3571 {
3572   if (MEM_P (op))
3573     /* Extended loads are as cheap as unextended ones.  */
3574     return 0;
3575
3576   if (TARGET_64BIT && mode == DImode && GET_MODE (op) == SImode)
3577     /* We need a shift left by 32 bits and a shift right by 32 bits.  */
3578     return COSTS_N_INSNS (TARGET_MIPS16 ? 4 : 2);
3579
3580   if (GENERATE_MIPS16E)
3581     /* We can use ZEB or ZEH.  */
3582     return COSTS_N_INSNS (1);
3583
3584   if (TARGET_MIPS16)
3585     /* We need to load 0xff or 0xffff into a register and use AND.  */
3586     return COSTS_N_INSNS (GET_MODE (op) == QImode ? 2 : 3);
3587
3588   /* We can use ANDI.  */
3589   return COSTS_N_INSNS (1);
3590 }
3591
3592 /* Return the cost of moving between two registers of mode MODE,
3593    assuming that the move will be in pieces of at most UNITS bytes.  */
3594
3595 static int
3596 mips_set_reg_reg_piece_cost (enum machine_mode mode, unsigned int units)
3597 {
3598   return COSTS_N_INSNS ((GET_MODE_SIZE (mode) + units - 1) / units);
3599 }
3600
3601 /* Return the cost of moving between two registers of mode MODE.  */
3602
3603 static int
3604 mips_set_reg_reg_cost (enum machine_mode mode)
3605 {
3606   switch (GET_MODE_CLASS (mode))
3607     {
3608     case MODE_CC:
3609       return mips_set_reg_reg_piece_cost (mode, GET_MODE_SIZE (CCmode));
3610
3611     case MODE_FLOAT:
3612     case MODE_COMPLEX_FLOAT:
3613     case MODE_VECTOR_FLOAT:
3614       if (TARGET_HARD_FLOAT)
3615         return mips_set_reg_reg_piece_cost (mode, UNITS_PER_HWFPVALUE);
3616       /* Fall through */
3617
3618     default:
3619       return mips_set_reg_reg_piece_cost (mode, UNITS_PER_WORD);
3620     }
3621 }
3622
3623 /* Return the cost of an operand X that can be trucated for free.
3624    SPEED says whether we're optimizing for size or speed.  */
3625
3626 static int
3627 mips_truncated_op_cost (rtx x, bool speed)
3628 {
3629   if (GET_CODE (x) == TRUNCATE)
3630     x = XEXP (x, 0);
3631   return set_src_cost (x, speed);
3632 }
3633
3634 /* Implement TARGET_RTX_COSTS.  */
3635
3636 static bool
3637 mips_rtx_costs (rtx x, int code, int outer_code, int opno ATTRIBUTE_UNUSED,
3638                 int *total, bool speed)
3639 {
3640   enum machine_mode mode = GET_MODE (x);
3641   bool float_mode_p = FLOAT_MODE_P (mode);
3642   int cost;
3643   rtx addr;
3644
3645   /* The cost of a COMPARE is hard to define for MIPS.  COMPAREs don't
3646      appear in the instruction stream, and the cost of a comparison is
3647      really the cost of the branch or scc condition.  At the time of
3648      writing, GCC only uses an explicit outer COMPARE code when optabs
3649      is testing whether a constant is expensive enough to force into a
3650      register.  We want optabs to pass such constants through the MIPS
3651      expanders instead, so make all constants very cheap here.  */
3652   if (outer_code == COMPARE)
3653     {
3654       gcc_assert (CONSTANT_P (x));
3655       *total = 0;
3656       return true;
3657     }
3658
3659   switch (code)
3660     {
3661     case CONST_INT:
3662       /* Treat *clear_upper32-style ANDs as having zero cost in the
3663          second operand.  The cost is entirely in the first operand.
3664
3665          ??? This is needed because we would otherwise try to CSE
3666          the constant operand.  Although that's the right thing for
3667          instructions that continue to be a register operation throughout
3668          compilation, it is disastrous for instructions that could
3669          later be converted into a memory operation.  */
3670       if (TARGET_64BIT
3671           && outer_code == AND
3672           && UINTVAL (x) == 0xffffffff)
3673         {
3674           *total = 0;
3675           return true;
3676         }
3677
3678       if (TARGET_MIPS16)
3679         {
3680           cost = mips16_constant_cost (outer_code, INTVAL (x));
3681           if (cost >= 0)
3682             {
3683               *total = cost;
3684               return true;
3685             }
3686         }
3687       else
3688         {
3689           /* When not optimizing for size, we care more about the cost
3690              of hot code, and hot code is often in a loop.  If a constant
3691              operand needs to be forced into a register, we will often be
3692              able to hoist the constant load out of the loop, so the load
3693              should not contribute to the cost.  */
3694           if (speed || mips_immediate_operand_p (outer_code, INTVAL (x)))
3695             {
3696               *total = 0;
3697               return true;
3698             }
3699         }
3700       /* Fall through.  */
3701
3702     case CONST:
3703     case SYMBOL_REF:
3704     case LABEL_REF:
3705     case CONST_DOUBLE:
3706       if (force_to_mem_operand (x, VOIDmode))
3707         {
3708           *total = COSTS_N_INSNS (1);
3709           return true;
3710         }
3711       cost = mips_const_insns (x);
3712       if (cost > 0)
3713         {
3714           /* If the constant is likely to be stored in a GPR, SETs of
3715              single-insn constants are as cheap as register sets; we
3716              never want to CSE them.
3717
3718              Don't reduce the cost of storing a floating-point zero in
3719              FPRs.  If we have a zero in an FPR for other reasons, we
3720              can get better cfg-cleanup and delayed-branch results by
3721              using it consistently, rather than using $0 sometimes and
3722              an FPR at other times.  Also, moves between floating-point
3723              registers are sometimes cheaper than (D)MTC1 $0.  */
3724           if (cost == 1
3725               && outer_code == SET
3726               && !(float_mode_p && TARGET_HARD_FLOAT))
3727             cost = 0;
3728           /* When non-MIPS16 code loads a constant N>1 times, we rarely
3729              want to CSE the constant itself.  It is usually better to
3730              have N copies of the last operation in the sequence and one
3731              shared copy of the other operations.  (Note that this is
3732              not true for MIPS16 code, where the final operation in the
3733              sequence is often an extended instruction.)
3734
3735              Also, if we have a CONST_INT, we don't know whether it is
3736              for a word or doubleword operation, so we cannot rely on
3737              the result of mips_build_integer.  */
3738           else if (!TARGET_MIPS16
3739                    && (outer_code == SET || mode == VOIDmode))
3740             cost = 1;
3741           *total = COSTS_N_INSNS (cost);
3742           return true;
3743         }
3744       /* The value will need to be fetched from the constant pool.  */
3745       *total = CONSTANT_POOL_COST;
3746       return true;
3747
3748     case MEM:
3749       /* If the address is legitimate, return the number of
3750          instructions it needs.  */
3751       addr = XEXP (x, 0);
3752       cost = mips_address_insns (addr, mode, true);
3753       if (cost > 0)
3754         {
3755           *total = COSTS_N_INSNS (cost + 1);
3756           return true;
3757         }
3758       /* Check for a scaled indexed address.  */
3759       if (mips_lwxs_address_p (addr)
3760           || mips_lx_address_p (addr, mode))
3761         {
3762           *total = COSTS_N_INSNS (2);
3763           return true;
3764         }
3765       /* Otherwise use the default handling.  */
3766       return false;
3767
3768     case FFS:
3769       *total = COSTS_N_INSNS (6);
3770       return false;
3771
3772     case NOT:
3773       *total = COSTS_N_INSNS (GET_MODE_SIZE (mode) > UNITS_PER_WORD ? 2 : 1);
3774       return false;
3775
3776     case AND:
3777       /* Check for a *clear_upper32 pattern and treat it like a zero
3778          extension.  See the pattern's comment for details.  */
3779       if (TARGET_64BIT
3780           && mode == DImode
3781           && CONST_INT_P (XEXP (x, 1))
3782           && UINTVAL (XEXP (x, 1)) == 0xffffffff)
3783         {
3784           *total = (mips_zero_extend_cost (mode, XEXP (x, 0))
3785                     + set_src_cost (XEXP (x, 0), speed));
3786           return true;
3787         }
3788       if (ISA_HAS_CINS && CONST_INT_P (XEXP (x, 1)))
3789         {
3790           rtx op = XEXP (x, 0);
3791           if (GET_CODE (op) == ASHIFT
3792               && CONST_INT_P (XEXP (op, 1))
3793               && mask_low_and_shift_p (mode, XEXP (x, 1), XEXP (op, 1), 32))
3794             {
3795               *total = COSTS_N_INSNS (1) + set_src_cost (XEXP (op, 0), speed);
3796               return true;
3797             }
3798         }
3799       /* (AND (NOT op0) (NOT op1) is a nor operation that can be done in
3800          a single instruction.  */
3801       if (!TARGET_MIPS16
3802           && GET_CODE (XEXP (x, 0)) == NOT
3803           && GET_CODE (XEXP (x, 1)) == NOT)
3804         {
3805           cost = GET_MODE_SIZE (mode) > UNITS_PER_WORD ? 2 : 1;
3806           *total = (COSTS_N_INSNS (cost)
3807                     + set_src_cost (XEXP (XEXP (x, 0), 0), speed)
3808                     + set_src_cost (XEXP (XEXP (x, 1), 0), speed));
3809           return true;
3810         }
3811             
3812       /* Fall through.  */
3813
3814     case IOR:
3815     case XOR:
3816       /* Double-word operations use two single-word operations.  */
3817       *total = mips_binary_cost (x, COSTS_N_INSNS (1), COSTS_N_INSNS (2),
3818                                  speed);
3819       return true;
3820
3821     case ASHIFT:
3822     case ASHIFTRT:
3823     case LSHIFTRT:
3824     case ROTATE:
3825     case ROTATERT:
3826       if (CONSTANT_P (XEXP (x, 1)))
3827         *total = mips_binary_cost (x, COSTS_N_INSNS (1), COSTS_N_INSNS (4),
3828                                    speed);
3829       else
3830         *total = mips_binary_cost (x, COSTS_N_INSNS (1), COSTS_N_INSNS (12),
3831                                    speed);
3832       return true;
3833
3834     case ABS:
3835       if (float_mode_p)
3836         *total = mips_cost->fp_add;
3837       else
3838         *total = COSTS_N_INSNS (4);
3839       return false;
3840
3841     case LO_SUM:
3842       /* Low-part immediates need an extended MIPS16 instruction.  */
3843       *total = (COSTS_N_INSNS (TARGET_MIPS16 ? 2 : 1)
3844                 + set_src_cost (XEXP (x, 0), speed));
3845       return true;
3846
3847     case LT:
3848     case LTU:
3849     case LE:
3850     case LEU:
3851     case GT:
3852     case GTU:
3853     case GE:
3854     case GEU:
3855     case EQ:
3856     case NE:
3857     case UNORDERED:
3858     case LTGT:
3859       /* Branch comparisons have VOIDmode, so use the first operand's
3860          mode instead.  */
3861       mode = GET_MODE (XEXP (x, 0));
3862       if (FLOAT_MODE_P (mode))
3863         {
3864           *total = mips_cost->fp_add;
3865           return false;
3866         }
3867       *total = mips_binary_cost (x, COSTS_N_INSNS (1), COSTS_N_INSNS (4),
3868                                  speed);
3869       return true;
3870
3871     case MINUS:
3872       if (float_mode_p
3873           && (ISA_HAS_NMADD4_NMSUB4 || ISA_HAS_NMADD3_NMSUB3)
3874           && TARGET_FUSED_MADD
3875           && !HONOR_NANS (mode)
3876           && !HONOR_SIGNED_ZEROS (mode))
3877         {
3878           /* See if we can use NMADD or NMSUB.  See mips.md for the
3879              associated patterns.  */
3880           rtx op0 = XEXP (x, 0);
3881           rtx op1 = XEXP (x, 1);
3882           if (GET_CODE (op0) == MULT && GET_CODE (XEXP (op0, 0)) == NEG)
3883             {
3884               *total = (mips_fp_mult_cost (mode)
3885                         + set_src_cost (XEXP (XEXP (op0, 0), 0), speed)
3886                         + set_src_cost (XEXP (op0, 1), speed)
3887                         + set_src_cost (op1, speed));
3888               return true;
3889             }
3890           if (GET_CODE (op1) == MULT)
3891             {
3892               *total = (mips_fp_mult_cost (mode)
3893                         + set_src_cost (op0, speed)
3894                         + set_src_cost (XEXP (op1, 0), speed)
3895                         + set_src_cost (XEXP (op1, 1), speed));
3896               return true;
3897             }
3898         }
3899       /* Fall through.  */
3900
3901     case PLUS:
3902       if (float_mode_p)
3903         {
3904           /* If this is part of a MADD or MSUB, treat the PLUS as
3905              being free.  */
3906           if ((ISA_HAS_FP_MADD4_MSUB4 || ISA_HAS_FP_MADD3_MSUB3)
3907               && TARGET_FUSED_MADD
3908               && GET_CODE (XEXP (x, 0)) == MULT)
3909             *total = 0;
3910           else
3911             *total = mips_cost->fp_add;
3912           return false;
3913         }
3914
3915       /* Double-word operations require three single-word operations and
3916          an SLTU.  The MIPS16 version then needs to move the result of
3917          the SLTU from $24 to a MIPS16 register.  */
3918       *total = mips_binary_cost (x, COSTS_N_INSNS (1),
3919                                  COSTS_N_INSNS (TARGET_MIPS16 ? 5 : 4),
3920                                  speed);
3921       return true;
3922
3923     case NEG:
3924       if (float_mode_p
3925           && (ISA_HAS_NMADD4_NMSUB4 || ISA_HAS_NMADD3_NMSUB3)
3926           && TARGET_FUSED_MADD
3927           && !HONOR_NANS (mode)
3928           && HONOR_SIGNED_ZEROS (mode))
3929         {
3930           /* See if we can use NMADD or NMSUB.  See mips.md for the
3931              associated patterns.  */
3932           rtx op = XEXP (x, 0);
3933           if ((GET_CODE (op) == PLUS || GET_CODE (op) == MINUS)
3934               && GET_CODE (XEXP (op, 0)) == MULT)
3935             {
3936               *total = (mips_fp_mult_cost (mode)
3937                         + set_src_cost (XEXP (XEXP (op, 0), 0), speed)
3938                         + set_src_cost (XEXP (XEXP (op, 0), 1), speed)
3939                         + set_src_cost (XEXP (op, 1), speed));
3940               return true;
3941             }
3942         }
3943
3944       if (float_mode_p)
3945         *total = mips_cost->fp_add;
3946       else
3947         *total = COSTS_N_INSNS (GET_MODE_SIZE (mode) > UNITS_PER_WORD ? 4 : 1);
3948       return false;
3949
3950     case MULT:
3951       if (float_mode_p)
3952         *total = mips_fp_mult_cost (mode);
3953       else if (mode == DImode && !TARGET_64BIT)
3954         /* Synthesized from 2 mulsi3s, 1 mulsidi3 and two additions,
3955            where the mulsidi3 always includes an MFHI and an MFLO.  */
3956         *total = (speed
3957                   ? mips_cost->int_mult_si * 3 + 6
3958                   : COSTS_N_INSNS (ISA_HAS_MUL3 ? 7 : 9));
3959       else if (!speed)
3960         *total = COSTS_N_INSNS (ISA_HAS_MUL3 ? 1 : 2) + 1;
3961       else if (mode == DImode)
3962         *total = mips_cost->int_mult_di;
3963       else
3964         *total = mips_cost->int_mult_si;
3965       return false;
3966
3967     case DIV:
3968       /* Check for a reciprocal.  */
3969       if (float_mode_p
3970           && ISA_HAS_FP4
3971           && flag_unsafe_math_optimizations
3972           && XEXP (x, 0) == CONST1_RTX (mode))
3973         {
3974           if (outer_code == SQRT || GET_CODE (XEXP (x, 1)) == SQRT)
3975             /* An rsqrt<mode>a or rsqrt<mode>b pattern.  Count the
3976                division as being free.  */
3977             *total = set_src_cost (XEXP (x, 1), speed);
3978           else
3979             *total = (mips_fp_div_cost (mode)
3980                       + set_src_cost (XEXP (x, 1), speed));
3981           return true;
3982         }
3983       /* Fall through.  */
3984
3985     case SQRT:
3986     case MOD:
3987       if (float_mode_p)
3988         {
3989           *total = mips_fp_div_cost (mode);
3990           return false;
3991         }
3992       /* Fall through.  */
3993
3994     case UDIV:
3995     case UMOD:
3996       if (!speed)
3997         {
3998           /* It is our responsibility to make division by a power of 2
3999              as cheap as 2 register additions if we want the division
4000              expanders to be used for such operations; see the setting
4001              of sdiv_pow2_cheap in optabs.c.  Using (D)DIV for MIPS16
4002              should always produce shorter code than using
4003              expand_sdiv2_pow2.  */
4004           if (TARGET_MIPS16
4005               && CONST_INT_P (XEXP (x, 1))
4006               && exact_log2 (INTVAL (XEXP (x, 1))) >= 0)
4007             {
4008               *total = COSTS_N_INSNS (2) + set_src_cost (XEXP (x, 0), speed);
4009               return true;
4010             }
4011           *total = COSTS_N_INSNS (mips_idiv_insns ());
4012         }
4013       else if (mode == DImode)
4014         *total = mips_cost->int_div_di;
4015       else
4016         *total = mips_cost->int_div_si;
4017       return false;
4018
4019     case SIGN_EXTEND:
4020       *total = mips_sign_extend_cost (mode, XEXP (x, 0));
4021       return false;
4022
4023     case ZERO_EXTEND:
4024       if (outer_code == SET
4025           && ISA_HAS_BADDU
4026           && GET_MODE (XEXP (x, 0)) == QImode
4027           && GET_CODE (XEXP (x, 0)) == PLUS)
4028         {
4029           rtx plus = XEXP (x, 0);
4030           *total = (COSTS_N_INSNS (1)
4031                     + mips_truncated_op_cost (XEXP (plus, 0), speed)
4032                     + mips_truncated_op_cost (XEXP (plus, 1), speed));
4033           return true;
4034         }
4035       *total = mips_zero_extend_cost (mode, XEXP (x, 0));
4036       return false;
4037
4038     case FLOAT:
4039     case UNSIGNED_FLOAT:
4040     case FIX:
4041     case FLOAT_EXTEND:
4042     case FLOAT_TRUNCATE:
4043       *total = mips_cost->fp_add;
4044       return false;
4045
4046     case SET:
4047       if (register_operand (SET_DEST (x), VOIDmode)
4048           && reg_or_0_operand (SET_SRC (x), VOIDmode))
4049         {
4050           *total = mips_set_reg_reg_cost (GET_MODE (SET_DEST (x)));
4051           return true;
4052         }
4053       return false;
4054
4055     default:
4056       return false;
4057     }
4058 }
4059
4060 /* Implement TARGET_ADDRESS_COST.  */
4061
4062 static int
4063 mips_address_cost (rtx addr, enum machine_mode mode,
4064                    addr_space_t as ATTRIBUTE_UNUSED,
4065                    bool speed ATTRIBUTE_UNUSED)
4066 {
4067   return mips_address_insns (addr, mode, false);
4068 }
4069 \f
4070 /* Information about a single instruction in a multi-instruction
4071    asm sequence.  */
4072 struct mips_multi_member {
4073   /* True if this is a label, false if it is code.  */
4074   bool is_label_p;
4075
4076   /* The output_asm_insn format of the instruction.  */
4077   const char *format;
4078
4079   /* The operands to the instruction.  */
4080   rtx operands[MAX_RECOG_OPERANDS];
4081 };
4082 typedef struct mips_multi_member mips_multi_member;
4083
4084 /* The instructions that make up the current multi-insn sequence.  */
4085 static vec<mips_multi_member> mips_multi_members;
4086
4087 /* How many instructions (as opposed to labels) are in the current
4088    multi-insn sequence.  */
4089 static unsigned int mips_multi_num_insns;
4090
4091 /* Start a new multi-insn sequence.  */
4092
4093 static void
4094 mips_multi_start (void)
4095 {
4096   mips_multi_members.truncate (0);
4097   mips_multi_num_insns = 0;
4098 }
4099
4100 /* Add a new, uninitialized member to the current multi-insn sequence.  */
4101
4102 static struct mips_multi_member *
4103 mips_multi_add (void)
4104 {
4105   mips_multi_member empty;
4106   return mips_multi_members.safe_push (empty);
4107 }
4108
4109 /* Add a normal insn with the given asm format to the current multi-insn
4110    sequence.  The other arguments are a null-terminated list of operands.  */
4111
4112 static void
4113 mips_multi_add_insn (const char *format, ...)
4114 {
4115   struct mips_multi_member *member;
4116   va_list ap;
4117   unsigned int i;
4118   rtx op;
4119
4120   member = mips_multi_add ();
4121   member->is_label_p = false;
4122   member->format = format;
4123   va_start (ap, format);
4124   i = 0;
4125   while ((op = va_arg (ap, rtx)))
4126     member->operands[i++] = op;
4127   va_end (ap);
4128   mips_multi_num_insns++;
4129 }
4130
4131 /* Add the given label definition to the current multi-insn sequence.
4132    The definition should include the colon.  */
4133
4134 static void
4135 mips_multi_add_label (const char *label)
4136 {
4137   struct mips_multi_member *member;
4138
4139   member = mips_multi_add ();
4140   member->is_label_p = true;
4141   member->format = label;
4142 }
4143
4144 /* Return the index of the last member of the current multi-insn sequence.  */
4145
4146 static unsigned int
4147 mips_multi_last_index (void)
4148 {
4149   return mips_multi_members.length () - 1;
4150 }
4151
4152 /* Add a copy of an existing instruction to the current multi-insn
4153    sequence.  I is the index of the instruction that should be copied.  */
4154
4155 static void
4156 mips_multi_copy_insn (unsigned int i)
4157 {
4158   struct mips_multi_member *member;
4159
4160   member = mips_multi_add ();
4161   memcpy (member, &mips_multi_members[i], sizeof (*member));
4162   gcc_assert (!member->is_label_p);
4163 }
4164
4165 /* Change the operand of an existing instruction in the current
4166    multi-insn sequence.  I is the index of the instruction,
4167    OP is the index of the operand, and X is the new value.  */
4168
4169 static void
4170 mips_multi_set_operand (unsigned int i, unsigned int op, rtx x)
4171 {
4172   mips_multi_members[i].operands[op] = x;
4173 }
4174
4175 /* Write out the asm code for the current multi-insn sequence.  */
4176
4177 static void
4178 mips_multi_write (void)
4179 {
4180   struct mips_multi_member *member;
4181   unsigned int i;
4182
4183   FOR_EACH_VEC_ELT (mips_multi_members, i, member)
4184     if (member->is_label_p)
4185       fprintf (asm_out_file, "%s\n", member->format);
4186     else
4187       output_asm_insn (member->format, member->operands);
4188 }
4189 \f
4190 /* Return one word of double-word value OP, taking into account the fixed
4191    endianness of certain registers.  HIGH_P is true to select the high part,
4192    false to select the low part.  */
4193
4194 rtx
4195 mips_subword (rtx op, bool high_p)
4196 {
4197   unsigned int byte, offset;
4198   enum machine_mode mode;
4199
4200   mode = GET_MODE (op);
4201   if (mode == VOIDmode)
4202     mode = TARGET_64BIT ? TImode : DImode;
4203
4204   if (TARGET_BIG_ENDIAN ? !high_p : high_p)
4205     byte = UNITS_PER_WORD;
4206   else
4207     byte = 0;
4208
4209   if (FP_REG_RTX_P (op))
4210     {
4211       /* Paired FPRs are always ordered little-endian.  */
4212       offset = (UNITS_PER_WORD < UNITS_PER_HWFPVALUE ? high_p : byte != 0);
4213       return gen_rtx_REG (word_mode, REGNO (op) + offset);
4214     }
4215
4216   if (MEM_P (op))
4217     return mips_rewrite_small_data (adjust_address (op, word_mode, byte));
4218
4219   return simplify_gen_subreg (word_mode, op, mode, byte);
4220 }
4221
4222 /* Return true if SRC should be moved into DEST using "MULT $0, $0".
4223    SPLIT_TYPE is the condition under which moves should be split.  */
4224
4225 static bool
4226 mips_mult_move_p (rtx dest, rtx src, enum mips_split_type split_type)
4227 {
4228   return ((split_type != SPLIT_FOR_SPEED
4229            || mips_tuning_info.fast_mult_zero_zero_p)
4230           && src == const0_rtx
4231           && REG_P (dest)
4232           && GET_MODE_SIZE (GET_MODE (dest)) == 2 * UNITS_PER_WORD
4233           && (ISA_HAS_DSP_MULT
4234               ? ACC_REG_P (REGNO (dest))
4235               : MD_REG_P (REGNO (dest))));
4236 }
4237
4238 /* Return true if a move from SRC to DEST should be split into two.
4239    SPLIT_TYPE describes the split condition.  */
4240
4241 bool
4242 mips_split_move_p (rtx dest, rtx src, enum mips_split_type split_type)
4243 {
4244   /* Check whether the move can be done using some variant of MULT $0,$0.  */
4245   if (mips_mult_move_p (dest, src, split_type))
4246     return false;
4247
4248   /* FPR-to-FPR moves can be done in a single instruction, if they're
4249      allowed at all.  */
4250   unsigned int size = GET_MODE_SIZE (GET_MODE (dest));
4251   if (size == 8 && FP_REG_RTX_P (src) && FP_REG_RTX_P (dest))
4252     return false;
4253
4254   /* Check for floating-point loads and stores.  */
4255   if (size == 8 && ISA_HAS_LDC1_SDC1)
4256     {
4257       if (FP_REG_RTX_P (dest) && MEM_P (src))
4258         return false;
4259       if (FP_REG_RTX_P (src) && MEM_P (dest))
4260         return false;
4261     }
4262
4263   /* Otherwise split all multiword moves.  */
4264   return size > UNITS_PER_WORD;
4265 }
4266
4267 /* Split a move from SRC to DEST, given that mips_split_move_p holds.
4268    SPLIT_TYPE describes the split condition.  */
4269
4270 void
4271 mips_split_move (rtx dest, rtx src, enum mips_split_type split_type)
4272 {
4273   rtx low_dest;
4274
4275   gcc_checking_assert (mips_split_move_p (dest, src, split_type));
4276   if (FP_REG_RTX_P (dest) || FP_REG_RTX_P (src))
4277     {
4278       if (!TARGET_64BIT && GET_MODE (dest) == DImode)
4279         emit_insn (gen_move_doubleword_fprdi (dest, src));
4280       else if (!TARGET_64BIT && GET_MODE (dest) == DFmode)
4281         emit_insn (gen_move_doubleword_fprdf (dest, src));
4282       else if (!TARGET_64BIT && GET_MODE (dest) == V2SFmode)
4283         emit_insn (gen_move_doubleword_fprv2sf (dest, src));
4284       else if (!TARGET_64BIT && GET_MODE (dest) == V2SImode)
4285         emit_insn (gen_move_doubleword_fprv2si (dest, src));
4286       else if (!TARGET_64BIT && GET_MODE (dest) == V4HImode)
4287         emit_insn (gen_move_doubleword_fprv4hi (dest, src));
4288       else if (!TARGET_64BIT && GET_MODE (dest) == V8QImode)
4289         emit_insn (gen_move_doubleword_fprv8qi (dest, src));
4290       else if (TARGET_64BIT && GET_MODE (dest) == TFmode)
4291         emit_insn (gen_move_doubleword_fprtf (dest, src));
4292       else
4293         gcc_unreachable ();
4294     }
4295   else if (REG_P (dest) && REGNO (dest) == MD_REG_FIRST)
4296     {
4297       low_dest = mips_subword (dest, false);
4298       mips_emit_move (low_dest, mips_subword (src, false));
4299       if (TARGET_64BIT)
4300         emit_insn (gen_mthidi_ti (dest, mips_subword (src, true), low_dest));
4301       else
4302         emit_insn (gen_mthisi_di (dest, mips_subword (src, true), low_dest));
4303     }
4304   else if (REG_P (src) && REGNO (src) == MD_REG_FIRST)
4305     {
4306       mips_emit_move (mips_subword (dest, false), mips_subword (src, false));
4307       if (TARGET_64BIT)
4308         emit_insn (gen_mfhidi_ti (mips_subword (dest, true), src));
4309       else
4310         emit_insn (gen_mfhisi_di (mips_subword (dest, true), src));
4311     }
4312   else
4313     {
4314       /* The operation can be split into two normal moves.  Decide in
4315          which order to do them.  */
4316       low_dest = mips_subword (dest, false);
4317       if (REG_P (low_dest)
4318           && reg_overlap_mentioned_p (low_dest, src))
4319         {
4320           mips_emit_move (mips_subword (dest, true), mips_subword (src, true));
4321           mips_emit_move (low_dest, mips_subword (src, false));
4322         }
4323       else
4324         {
4325           mips_emit_move (low_dest, mips_subword (src, false));
4326           mips_emit_move (mips_subword (dest, true), mips_subword (src, true));
4327         }
4328     }
4329 }
4330
4331 /* Return the split type for instruction INSN.  */
4332
4333 static enum mips_split_type
4334 mips_insn_split_type (rtx insn)
4335 {
4336   basic_block bb = BLOCK_FOR_INSN (insn);
4337   if (bb)
4338     {
4339       if (optimize_bb_for_speed_p (bb))
4340         return SPLIT_FOR_SPEED;
4341       else
4342         return SPLIT_FOR_SIZE;
4343     }
4344   /* Once CFG information has been removed, we should trust the optimization
4345      decisions made by previous passes and only split where necessary.  */
4346   return SPLIT_IF_NECESSARY;
4347 }
4348
4349 /* Return true if a move from SRC to DEST in INSN should be split.  */
4350
4351 bool
4352 mips_split_move_insn_p (rtx dest, rtx src, rtx insn)
4353 {
4354   return mips_split_move_p (dest, src, mips_insn_split_type (insn));
4355 }
4356
4357 /* Split a move from SRC to DEST in INSN, given that mips_split_move_insn_p
4358    holds.  */
4359
4360 void
4361 mips_split_move_insn (rtx dest, rtx src, rtx insn)
4362 {
4363   mips_split_move (dest, src, mips_insn_split_type (insn));
4364 }
4365 \f
4366 /* Return the appropriate instructions to move SRC into DEST.  Assume
4367    that SRC is operand 1 and DEST is operand 0.  */
4368
4369 const char *
4370 mips_output_move (rtx dest, rtx src)
4371 {
4372   enum rtx_code dest_code, src_code;
4373   enum machine_mode mode;
4374   enum mips_symbol_type symbol_type;
4375   bool dbl_p;
4376
4377   dest_code = GET_CODE (dest);
4378   src_code = GET_CODE (src);
4379   mode = GET_MODE (dest);
4380   dbl_p = (GET_MODE_SIZE (mode) == 8);
4381
4382   if (mips_split_move_p (dest, src, SPLIT_IF_NECESSARY))
4383     return "#";
4384
4385   if ((src_code == REG && GP_REG_P (REGNO (src)))
4386       || (!TARGET_MIPS16 && src == CONST0_RTX (mode)))
4387     {
4388       if (dest_code == REG)
4389         {
4390           if (GP_REG_P (REGNO (dest)))
4391             return "move\t%0,%z1";
4392
4393           if (mips_mult_move_p (dest, src, SPLIT_IF_NECESSARY))
4394             {
4395               if (ISA_HAS_DSP_MULT)
4396                 return "mult\t%q0,%.,%.";
4397               else
4398                 return "mult\t%.,%.";
4399             }
4400
4401           /* Moves to HI are handled by special .md insns.  */
4402           if (REGNO (dest) == LO_REGNUM)
4403             return "mtlo\t%z1";
4404
4405           if (DSP_ACC_REG_P (REGNO (dest)))
4406             {
4407               static char retval[] = "mt__\t%z1,%q0";
4408
4409               retval[2] = reg_names[REGNO (dest)][4];
4410               retval[3] = reg_names[REGNO (dest)][5];
4411               return retval;
4412             }
4413
4414           if (FP_REG_P (REGNO (dest)))
4415             return dbl_p ? "dmtc1\t%z1,%0" : "mtc1\t%z1,%0";
4416
4417           if (ALL_COP_REG_P (REGNO (dest)))
4418             {
4419               static char retval[] = "dmtc_\t%z1,%0";
4420
4421               retval[4] = COPNUM_AS_CHAR_FROM_REGNUM (REGNO (dest));
4422               return dbl_p ? retval : retval + 1;
4423             }
4424         }
4425       if (dest_code == MEM)
4426         switch (GET_MODE_SIZE (mode))
4427           {
4428           case 1: return "sb\t%z1,%0";
4429           case 2: return "sh\t%z1,%0";
4430           case 4: return "sw\t%z1,%0";
4431           case 8: return "sd\t%z1,%0";
4432           }
4433     }
4434   if (dest_code == REG && GP_REG_P (REGNO (dest)))
4435     {
4436       if (src_code == REG)
4437         {
4438           /* Moves from HI are handled by special .md insns.  */
4439           if (REGNO (src) == LO_REGNUM)
4440             {
4441               /* When generating VR4120 or VR4130 code, we use MACC and
4442                  DMACC instead of MFLO.  This avoids both the normal
4443                  MIPS III HI/LO hazards and the errata related to
4444                  -mfix-vr4130.  */
4445               if (ISA_HAS_MACCHI)
4446                 return dbl_p ? "dmacc\t%0,%.,%." : "macc\t%0,%.,%.";
4447               return "mflo\t%0";
4448             }
4449
4450           if (DSP_ACC_REG_P (REGNO (src)))
4451             {
4452               static char retval[] = "mf__\t%0,%q1";
4453
4454               retval[2] = reg_names[REGNO (src)][4];
4455               retval[3] = reg_names[REGNO (src)][5];
4456               return retval;
4457             }
4458
4459           if (FP_REG_P (REGNO (src)))
4460             return dbl_p ? "dmfc1\t%0,%1" : "mfc1\t%0,%1";
4461
4462           if (ALL_COP_REG_P (REGNO (src)))
4463             {
4464               static char retval[] = "dmfc_\t%0,%1";
4465
4466               retval[4] = COPNUM_AS_CHAR_FROM_REGNUM (REGNO (src));
4467               return dbl_p ? retval : retval + 1;
4468             }
4469         }
4470
4471       if (src_code == MEM)
4472         switch (GET_MODE_SIZE (mode))
4473           {
4474           case 1: return "lbu\t%0,%1";
4475           case 2: return "lhu\t%0,%1";
4476           case 4: return "lw\t%0,%1";
4477           case 8: return "ld\t%0,%1";
4478           }
4479
4480       if (src_code == CONST_INT)
4481         {
4482           /* Don't use the X format for the operand itself, because that
4483              will give out-of-range numbers for 64-bit hosts and 32-bit
4484              targets.  */
4485           if (!TARGET_MIPS16)
4486             return "li\t%0,%1\t\t\t# %X1";
4487
4488           if (SMALL_OPERAND_UNSIGNED (INTVAL (src)))
4489             return "li\t%0,%1";
4490
4491           if (SMALL_OPERAND_UNSIGNED (-INTVAL (src)))
4492             return "#";
4493         }
4494
4495       if (src_code == HIGH)
4496         return TARGET_MIPS16 ? "#" : "lui\t%0,%h1";
4497
4498       if (CONST_GP_P (src))
4499         return "move\t%0,%1";
4500
4501       if (mips_symbolic_constant_p (src, SYMBOL_CONTEXT_LEA, &symbol_type)
4502           && mips_lo_relocs[symbol_type] != 0)
4503         {
4504           /* A signed 16-bit constant formed by applying a relocation
4505              operator to a symbolic address.  */
4506           gcc_assert (!mips_split_p[symbol_type]);
4507           return "li\t%0,%R1";
4508         }
4509
4510       if (symbolic_operand (src, VOIDmode))
4511         {
4512           gcc_assert (TARGET_MIPS16
4513                       ? TARGET_MIPS16_TEXT_LOADS
4514                       : !TARGET_EXPLICIT_RELOCS);
4515           return dbl_p ? "dla\t%0,%1" : "la\t%0,%1";
4516         }
4517     }
4518   if (src_code == REG && FP_REG_P (REGNO (src)))
4519     {
4520       if (dest_code == REG && FP_REG_P (REGNO (dest)))
4521         {
4522           if (GET_MODE (dest) == V2SFmode)
4523             return "mov.ps\t%0,%1";
4524           else
4525             return dbl_p ? "mov.d\t%0,%1" : "mov.s\t%0,%1";
4526         }
4527
4528       if (dest_code == MEM)
4529         return dbl_p ? "sdc1\t%1,%0" : "swc1\t%1,%0";
4530     }
4531   if (dest_code == REG && FP_REG_P (REGNO (dest)))
4532     {
4533       if (src_code == MEM)
4534         return dbl_p ? "ldc1\t%0,%1" : "lwc1\t%0,%1";
4535     }
4536   if (dest_code == REG && ALL_COP_REG_P (REGNO (dest)) && src_code == MEM)
4537     {
4538       static char retval[] = "l_c_\t%0,%1";
4539
4540       retval[1] = (dbl_p ? 'd' : 'w');
4541       retval[3] = COPNUM_AS_CHAR_FROM_REGNUM (REGNO (dest));
4542       return retval;
4543     }
4544   if (dest_code == MEM && src_code == REG && ALL_COP_REG_P (REGNO (src)))
4545     {
4546       static char retval[] = "s_c_\t%1,%0";
4547
4548       retval[1] = (dbl_p ? 'd' : 'w');
4549       retval[3] = COPNUM_AS_CHAR_FROM_REGNUM (REGNO (src));
4550       return retval;
4551     }
4552   gcc_unreachable ();
4553 }
4554 \f
4555 /* Return true if CMP1 is a suitable second operand for integer ordering
4556    test CODE.  See also the *sCC patterns in mips.md.  */
4557
4558 static bool
4559 mips_int_order_operand_ok_p (enum rtx_code code, rtx cmp1)
4560 {
4561   switch (code)
4562     {
4563     case GT:
4564     case GTU:
4565       return reg_or_0_operand (cmp1, VOIDmode);
4566
4567     case GE:
4568     case GEU:
4569       return !TARGET_MIPS16 && cmp1 == const1_rtx;
4570
4571     case LT:
4572     case LTU:
4573       return arith_operand (cmp1, VOIDmode);
4574
4575     case LE:
4576       return sle_operand (cmp1, VOIDmode);
4577
4578     case LEU:
4579       return sleu_operand (cmp1, VOIDmode);
4580
4581     default:
4582       gcc_unreachable ();
4583     }
4584 }
4585
4586 /* Return true if *CMP1 (of mode MODE) is a valid second operand for
4587    integer ordering test *CODE, or if an equivalent combination can
4588    be formed by adjusting *CODE and *CMP1.  When returning true, update
4589    *CODE and *CMP1 with the chosen code and operand, otherwise leave
4590    them alone.  */
4591
4592 static bool
4593 mips_canonicalize_int_order_test (enum rtx_code *code, rtx *cmp1,
4594                                   enum machine_mode mode)
4595 {
4596   HOST_WIDE_INT plus_one;
4597
4598   if (mips_int_order_operand_ok_p (*code, *cmp1))
4599     return true;
4600
4601   if (CONST_INT_P (*cmp1))
4602     switch (*code)
4603       {
4604       case LE:
4605         plus_one = trunc_int_for_mode (UINTVAL (*cmp1) + 1, mode);
4606         if (INTVAL (*cmp1) < plus_one)
4607           {
4608             *code = LT;
4609             *cmp1 = force_reg (mode, GEN_INT (plus_one));
4610             return true;
4611           }
4612         break;
4613
4614       case LEU:
4615         plus_one = trunc_int_for_mode (UINTVAL (*cmp1) + 1, mode);
4616         if (plus_one != 0)
4617           {
4618             *code = LTU;
4619             *cmp1 = force_reg (mode, GEN_INT (plus_one));
4620             return true;
4621           }
4622         break;
4623
4624       default:
4625         break;
4626       }
4627   return false;
4628 }
4629
4630 /* Compare CMP0 and CMP1 using ordering test CODE and store the result
4631    in TARGET.  CMP0 and TARGET are register_operands.  If INVERT_PTR
4632    is nonnull, it's OK to set TARGET to the inverse of the result and
4633    flip *INVERT_PTR instead.  */
4634
4635 static void
4636 mips_emit_int_order_test (enum rtx_code code, bool *invert_ptr,
4637                           rtx target, rtx cmp0, rtx cmp1)
4638 {
4639   enum machine_mode mode;
4640
4641   /* First see if there is a MIPS instruction that can do this operation.
4642      If not, try doing the same for the inverse operation.  If that also
4643      fails, force CMP1 into a register and try again.  */
4644   mode = GET_MODE (cmp0);
4645   if (mips_canonicalize_int_order_test (&code, &cmp1, mode))
4646     mips_emit_binary (code, target, cmp0, cmp1);
4647   else
4648     {
4649       enum rtx_code inv_code = reverse_condition (code);
4650       if (!mips_canonicalize_int_order_test (&inv_code, &cmp1, mode))
4651         {
4652           cmp1 = force_reg (mode, cmp1);
4653           mips_emit_int_order_test (code, invert_ptr, target, cmp0, cmp1);
4654         }
4655       else if (invert_ptr == 0)
4656         {
4657           rtx inv_target;
4658
4659           inv_target = mips_force_binary (GET_MODE (target),
4660                                           inv_code, cmp0, cmp1);
4661           mips_emit_binary (XOR, target, inv_target, const1_rtx);
4662         }
4663       else
4664         {
4665           *invert_ptr = !*invert_ptr;
4666           mips_emit_binary (inv_code, target, cmp0, cmp1);
4667         }
4668     }
4669 }
4670
4671 /* Return a register that is zero iff CMP0 and CMP1 are equal.
4672    The register will have the same mode as CMP0.  */
4673
4674 static rtx
4675 mips_zero_if_equal (rtx cmp0, rtx cmp1)
4676 {
4677   if (cmp1 == const0_rtx)
4678     return cmp0;
4679
4680   if (uns_arith_operand (cmp1, VOIDmode))
4681     return expand_binop (GET_MODE (cmp0), xor_optab,
4682                          cmp0, cmp1, 0, 0, OPTAB_DIRECT);
4683
4684   return expand_binop (GET_MODE (cmp0), sub_optab,
4685                        cmp0, cmp1, 0, 0, OPTAB_DIRECT);
4686 }
4687
4688 /* Convert *CODE into a code that can be used in a floating-point
4689    scc instruction (C.cond.fmt).  Return true if the values of
4690    the condition code registers will be inverted, with 0 indicating
4691    that the condition holds.  */
4692
4693 static bool
4694 mips_reversed_fp_cond (enum rtx_code *code)
4695 {
4696   switch (*code)
4697     {
4698     case NE:
4699     case LTGT:
4700     case ORDERED:
4701       *code = reverse_condition_maybe_unordered (*code);
4702       return true;
4703
4704     default:
4705       return false;
4706     }
4707 }
4708
4709 /* Allocate a floating-point condition-code register of mode MODE.
4710
4711    These condition code registers are used for certain kinds
4712    of compound operation, such as compare and branches, vconds,
4713    and built-in functions.  At expand time, their use is entirely
4714    controlled by MIPS-specific code and is entirely internal
4715    to these compound operations.
4716
4717    We could (and did in the past) expose condition-code values
4718    as pseudo registers and leave the register allocator to pick
4719    appropriate registers.  The problem is that it is not practically
4720    possible for the rtl optimizers to guarantee that no spills will
4721    be needed, even when AVOID_CCMODE_COPIES is defined.  We would
4722    therefore need spill and reload sequences to handle the worst case.
4723
4724    Although such sequences do exist, they are very expensive and are
4725    not something we'd want to use.  This is especially true of CCV2 and
4726    CCV4, where all the shuffling would greatly outweigh whatever benefit
4727    the vectorization itself provides.
4728
4729    The main benefit of having more than one condition-code register
4730    is to allow the pipelining of operations, especially those involving
4731    comparisons and conditional moves.  We don't really expect the
4732    registers to be live for long periods, and certainly never want
4733    them to be live across calls.
4734
4735    Also, there should be no penalty attached to using all the available
4736    registers.  They are simply bits in the same underlying FPU control
4737    register.
4738
4739    We therefore expose the hardware registers from the outset and use
4740    a simple round-robin allocation scheme.  */
4741
4742 static rtx
4743 mips_allocate_fcc (enum machine_mode mode)
4744 {
4745   unsigned int regno, count;
4746
4747   gcc_assert (TARGET_HARD_FLOAT && ISA_HAS_8CC);
4748
4749   if (mode == CCmode)
4750     count = 1;
4751   else if (mode == CCV2mode)
4752     count = 2;
4753   else if (mode == CCV4mode)
4754     count = 4;
4755   else
4756     gcc_unreachable ();
4757
4758   cfun->machine->next_fcc += -cfun->machine->next_fcc & (count - 1);
4759   if (cfun->machine->next_fcc > ST_REG_LAST - ST_REG_FIRST)
4760     cfun->machine->next_fcc = 0;
4761   regno = ST_REG_FIRST + cfun->machine->next_fcc;
4762   cfun->machine->next_fcc += count;
4763   return gen_rtx_REG (mode, regno);
4764 }
4765
4766 /* Convert a comparison into something that can be used in a branch or
4767    conditional move.  On entry, *OP0 and *OP1 are the values being
4768    compared and *CODE is the code used to compare them.
4769
4770    Update *CODE, *OP0 and *OP1 so that they describe the final comparison.
4771    If NEED_EQ_NE_P, then only EQ or NE comparisons against zero are possible,
4772    otherwise any standard branch condition can be used.  The standard branch
4773    conditions are:
4774
4775       - EQ or NE between two registers.
4776       - any comparison between a register and zero.  */
4777
4778 static void
4779 mips_emit_compare (enum rtx_code *code, rtx *op0, rtx *op1, bool need_eq_ne_p)
4780 {
4781   rtx cmp_op0 = *op0;
4782   rtx cmp_op1 = *op1;
4783
4784   if (GET_MODE_CLASS (GET_MODE (*op0)) == MODE_INT)
4785     {
4786       if (!need_eq_ne_p && *op1 == const0_rtx)
4787         ;
4788       else if (*code == EQ || *code == NE)
4789         {
4790           if (need_eq_ne_p)
4791             {
4792               *op0 = mips_zero_if_equal (cmp_op0, cmp_op1);
4793               *op1 = const0_rtx;
4794             }
4795           else
4796             *op1 = force_reg (GET_MODE (cmp_op0), cmp_op1);
4797         }
4798       else
4799         {
4800           /* The comparison needs a separate scc instruction.  Store the
4801              result of the scc in *OP0 and compare it against zero.  */
4802           bool invert = false;
4803           *op0 = gen_reg_rtx (GET_MODE (cmp_op0));
4804           mips_emit_int_order_test (*code, &invert, *op0, cmp_op0, cmp_op1);
4805           *code = (invert ? EQ : NE);
4806           *op1 = const0_rtx;
4807         }
4808     }
4809   else if (ALL_FIXED_POINT_MODE_P (GET_MODE (cmp_op0)))
4810     {
4811       *op0 = gen_rtx_REG (CCDSPmode, CCDSP_CC_REGNUM);
4812       mips_emit_binary (*code, *op0, cmp_op0, cmp_op1);
4813       *code = NE;
4814       *op1 = const0_rtx;
4815     }
4816   else
4817     {
4818       enum rtx_code cmp_code;
4819
4820       /* Floating-point tests use a separate C.cond.fmt comparison to
4821          set a condition code register.  The branch or conditional move
4822          will then compare that register against zero.
4823
4824          Set CMP_CODE to the code of the comparison instruction and
4825          *CODE to the code that the branch or move should use.  */
4826       cmp_code = *code;
4827       *code = mips_reversed_fp_cond (&cmp_code) ? EQ : NE;
4828       *op0 = (ISA_HAS_8CC
4829               ? mips_allocate_fcc (CCmode)
4830               : gen_rtx_REG (CCmode, FPSW_REGNUM));
4831       *op1 = const0_rtx;
4832       mips_emit_binary (cmp_code, *op0, cmp_op0, cmp_op1);
4833     }
4834 }
4835 \f
4836 /* Try performing the comparison in OPERANDS[1], whose arms are OPERANDS[2]
4837    and OPERAND[3].  Store the result in OPERANDS[0].
4838
4839    On 64-bit targets, the mode of the comparison and target will always be
4840    SImode, thus possibly narrower than that of the comparison's operands.  */
4841
4842 void
4843 mips_expand_scc (rtx operands[])
4844 {
4845   rtx target = operands[0];
4846   enum rtx_code code = GET_CODE (operands[1]);
4847   rtx op0 = operands[2];
4848   rtx op1 = operands[3];
4849
4850   gcc_assert (GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT);
4851
4852   if (code == EQ || code == NE)
4853     {
4854       if (ISA_HAS_SEQ_SNE
4855           && reg_imm10_operand (op1, GET_MODE (op1)))
4856         mips_emit_binary (code, target, op0, op1);
4857       else
4858         {
4859           rtx zie = mips_zero_if_equal (op0, op1);
4860           mips_emit_binary (code, target, zie, const0_rtx);
4861         }
4862     }
4863   else
4864     mips_emit_int_order_test (code, 0, target, op0, op1);
4865 }
4866
4867 /* Compare OPERANDS[1] with OPERANDS[2] using comparison code
4868    CODE and jump to OPERANDS[3] if the condition holds.  */
4869
4870 void
4871 mips_expand_conditional_branch (rtx *operands)
4872 {
4873   enum rtx_code code = GET_CODE (operands[0]);
4874   rtx op0 = operands[1];
4875   rtx op1 = operands[2];
4876   rtx condition;
4877
4878   mips_emit_compare (&code, &op0, &op1, TARGET_MIPS16);
4879   condition = gen_rtx_fmt_ee (code, VOIDmode, op0, op1);
4880   emit_jump_insn (gen_condjump (condition, operands[3]));
4881 }
4882
4883 /* Implement:
4884
4885    (set temp (COND:CCV2 CMP_OP0 CMP_OP1))
4886    (set DEST (unspec [TRUE_SRC FALSE_SRC temp] UNSPEC_MOVE_TF_PS))  */
4887
4888 void
4889 mips_expand_vcondv2sf (rtx dest, rtx true_src, rtx false_src,
4890                        enum rtx_code cond, rtx cmp_op0, rtx cmp_op1)
4891 {
4892   rtx cmp_result;
4893   bool reversed_p;
4894
4895   reversed_p = mips_reversed_fp_cond (&cond);
4896   cmp_result = mips_allocate_fcc (CCV2mode);
4897   emit_insn (gen_scc_ps (cmp_result,
4898                          gen_rtx_fmt_ee (cond, VOIDmode, cmp_op0, cmp_op1)));
4899   if (reversed_p)
4900     emit_insn (gen_mips_cond_move_tf_ps (dest, false_src, true_src,
4901                                          cmp_result));
4902   else
4903     emit_insn (gen_mips_cond_move_tf_ps (dest, true_src, false_src,
4904                                          cmp_result));
4905 }
4906
4907 /* Perform the comparison in OPERANDS[1].  Move OPERANDS[2] into OPERANDS[0]
4908    if the condition holds, otherwise move OPERANDS[3] into OPERANDS[0].  */
4909
4910 void
4911 mips_expand_conditional_move (rtx *operands)
4912 {
4913   rtx cond;
4914   enum rtx_code code = GET_CODE (operands[1]);
4915   rtx op0 = XEXP (operands[1], 0);
4916   rtx op1 = XEXP (operands[1], 1);
4917
4918   mips_emit_compare (&code, &op0, &op1, true);
4919   cond = gen_rtx_fmt_ee (code, GET_MODE (op0), op0, op1);
4920   emit_insn (gen_rtx_SET (VOIDmode, operands[0],
4921                           gen_rtx_IF_THEN_ELSE (GET_MODE (operands[0]), cond,
4922                                                 operands[2], operands[3])));
4923 }
4924
4925 /* Perform the comparison in COMPARISON, then trap if the condition holds.  */
4926
4927 void
4928 mips_expand_conditional_trap (rtx comparison)
4929 {
4930   rtx op0, op1;
4931   enum machine_mode mode;
4932   enum rtx_code code;
4933
4934   /* MIPS conditional trap instructions don't have GT or LE flavors,
4935      so we must swap the operands and convert to LT and GE respectively.  */
4936   code = GET_CODE (comparison);
4937   switch (code)
4938     {
4939     case GT:
4940     case LE:
4941     case GTU:
4942     case LEU:
4943       code = swap_condition (code);
4944       op0 = XEXP (comparison, 1);
4945       op1 = XEXP (comparison, 0);
4946       break;
4947
4948     default:
4949       op0 = XEXP (comparison, 0);
4950       op1 = XEXP (comparison, 1);
4951       break;
4952     }
4953
4954   mode = GET_MODE (XEXP (comparison, 0));
4955   op0 = force_reg (mode, op0);
4956   if (!arith_operand (op1, mode))
4957     op1 = force_reg (mode, op1);
4958
4959   emit_insn (gen_rtx_TRAP_IF (VOIDmode,
4960                               gen_rtx_fmt_ee (code, mode, op0, op1),
4961                               const0_rtx));
4962 }
4963 \f
4964 /* Initialize *CUM for a call to a function of type FNTYPE.  */
4965
4966 void
4967 mips_init_cumulative_args (CUMULATIVE_ARGS *cum, tree fntype)
4968 {
4969   memset (cum, 0, sizeof (*cum));
4970   cum->prototype = (fntype && prototype_p (fntype));
4971   cum->gp_reg_found = (cum->prototype && stdarg_p (fntype));
4972 }
4973
4974 /* Fill INFO with information about a single argument.  CUM is the
4975    cumulative state for earlier arguments.  MODE is the mode of this
4976    argument and TYPE is its type (if known).  NAMED is true if this
4977    is a named (fixed) argument rather than a variable one.  */
4978
4979 static void
4980 mips_get_arg_info (struct mips_arg_info *info, const CUMULATIVE_ARGS *cum,
4981                    enum machine_mode mode, const_tree type, bool named)
4982 {
4983   bool doubleword_aligned_p;
4984   unsigned int num_bytes, num_words, max_regs;
4985
4986   /* Work out the size of the argument.  */
4987   num_bytes = type ? int_size_in_bytes (type) : GET_MODE_SIZE (mode);
4988   num_words = (num_bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
4989
4990   /* Decide whether it should go in a floating-point register, assuming
4991      one is free.  Later code checks for availability.
4992
4993      The checks against UNITS_PER_FPVALUE handle the soft-float and
4994      single-float cases.  */
4995   switch (mips_abi)
4996     {
4997     case ABI_EABI:
4998       /* The EABI conventions have traditionally been defined in terms
4999          of TYPE_MODE, regardless of the actual type.  */
5000       info->fpr_p = ((GET_MODE_CLASS (mode) == MODE_FLOAT
5001                       || mode == V2SFmode)
5002                      && GET_MODE_SIZE (mode) <= UNITS_PER_FPVALUE);
5003       break;
5004
5005     case ABI_32:
5006     case ABI_O64:
5007       /* Only leading floating-point scalars are passed in
5008          floating-point registers.  We also handle vector floats the same
5009          say, which is OK because they are not covered by the standard ABI.  */
5010       info->fpr_p = (!cum->gp_reg_found
5011                      && cum->arg_number < 2
5012                      && (type == 0
5013                          || SCALAR_FLOAT_TYPE_P (type)
5014                          || VECTOR_FLOAT_TYPE_P (type))
5015                      && (GET_MODE_CLASS (mode) == MODE_FLOAT
5016                          || mode == V2SFmode)
5017                      && GET_MODE_SIZE (mode) <= UNITS_PER_FPVALUE);
5018       break;
5019
5020     case ABI_N32:
5021     case ABI_64:
5022       /* Scalar, complex and vector floating-point types are passed in
5023          floating-point registers, as long as this is a named rather
5024          than a variable argument.  */
5025       info->fpr_p = (named
5026                      && (type == 0 || FLOAT_TYPE_P (type))
5027                      && (GET_MODE_CLASS (mode) == MODE_FLOAT
5028                          || GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
5029                          || mode == V2SFmode)
5030                      && GET_MODE_UNIT_SIZE (mode) <= UNITS_PER_FPVALUE);
5031
5032       /* ??? According to the ABI documentation, the real and imaginary
5033          parts of complex floats should be passed in individual registers.
5034          The real and imaginary parts of stack arguments are supposed
5035          to be contiguous and there should be an extra word of padding
5036          at the end.
5037
5038          This has two problems.  First, it makes it impossible to use a
5039          single "void *" va_list type, since register and stack arguments
5040          are passed differently.  (At the time of writing, MIPSpro cannot
5041          handle complex float varargs correctly.)  Second, it's unclear
5042          what should happen when there is only one register free.
5043
5044          For now, we assume that named complex floats should go into FPRs
5045          if there are two FPRs free, otherwise they should be passed in the
5046          same way as a struct containing two floats.  */
5047       if (info->fpr_p
5048           && GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
5049           && GET_MODE_UNIT_SIZE (mode) < UNITS_PER_FPVALUE)
5050         {
5051           if (cum->num_gprs >= MAX_ARGS_IN_REGISTERS - 1)
5052             info->fpr_p = false;
5053           else
5054             num_words = 2;
5055         }
5056       break;
5057
5058     default:
5059       gcc_unreachable ();
5060     }
5061
5062   /* See whether the argument has doubleword alignment.  */
5063   doubleword_aligned_p = (mips_function_arg_boundary (mode, type)
5064                           > BITS_PER_WORD);
5065
5066   /* Set REG_OFFSET to the register count we're interested in.
5067      The EABI allocates the floating-point registers separately,
5068      but the other ABIs allocate them like integer registers.  */
5069   info->reg_offset = (mips_abi == ABI_EABI && info->fpr_p
5070                       ? cum->num_fprs
5071                       : cum->num_gprs);
5072
5073   /* Advance to an even register if the argument is doubleword-aligned.  */
5074   if (doubleword_aligned_p)
5075     info->reg_offset += info->reg_offset & 1;
5076
5077   /* Work out the offset of a stack argument.  */
5078   info->stack_offset = cum->stack_words;
5079   if (doubleword_aligned_p)
5080     info->stack_offset += info->stack_offset & 1;
5081
5082   max_regs = MAX_ARGS_IN_REGISTERS - info->reg_offset;
5083
5084   /* Partition the argument between registers and stack.  */
5085   info->reg_words = MIN (num_words, max_regs);
5086   info->stack_words = num_words - info->reg_words;
5087 }
5088
5089 /* INFO describes a register argument that has the normal format for the
5090    argument's mode.  Return the register it uses, assuming that FPRs are
5091    available if HARD_FLOAT_P.  */
5092
5093 static unsigned int
5094 mips_arg_regno (const struct mips_arg_info *info, bool hard_float_p)
5095 {
5096   if (!info->fpr_p || !hard_float_p)
5097     return GP_ARG_FIRST + info->reg_offset;
5098   else if (mips_abi == ABI_32 && TARGET_DOUBLE_FLOAT && info->reg_offset > 0)
5099     /* In o32, the second argument is always passed in $f14
5100        for TARGET_DOUBLE_FLOAT, regardless of whether the
5101        first argument was a word or doubleword.  */
5102     return FP_ARG_FIRST + 2;
5103   else
5104     return FP_ARG_FIRST + info->reg_offset;
5105 }
5106
5107 /* Implement TARGET_STRICT_ARGUMENT_NAMING.  */
5108
5109 static bool
5110 mips_strict_argument_naming (cumulative_args_t ca ATTRIBUTE_UNUSED)
5111 {
5112   return !TARGET_OLDABI;
5113 }
5114
5115 /* Implement TARGET_FUNCTION_ARG.  */
5116
5117 static rtx
5118 mips_function_arg (cumulative_args_t cum_v, enum machine_mode mode,
5119                    const_tree type, bool named)
5120 {
5121   CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
5122   struct mips_arg_info info;
5123
5124   /* We will be called with a mode of VOIDmode after the last argument
5125      has been seen.  Whatever we return will be passed to the call expander.
5126      If we need a MIPS16 fp_code, return a REG with the code stored as
5127      the mode.  */
5128   if (mode == VOIDmode)
5129     {
5130       if (TARGET_MIPS16 && cum->fp_code != 0)
5131         return gen_rtx_REG ((enum machine_mode) cum->fp_code, 0);
5132       else
5133         return NULL;
5134     }
5135
5136   mips_get_arg_info (&info, cum, mode, type, named);
5137
5138   /* Return straight away if the whole argument is passed on the stack.  */
5139   if (info.reg_offset == MAX_ARGS_IN_REGISTERS)
5140     return NULL;
5141
5142   /* The n32 and n64 ABIs say that if any 64-bit chunk of the structure
5143      contains a double in its entirety, then that 64-bit chunk is passed
5144      in a floating-point register.  */
5145   if (TARGET_NEWABI
5146       && TARGET_HARD_FLOAT
5147       && named
5148       && type != 0
5149       && TREE_CODE (type) == RECORD_TYPE
5150       && TYPE_SIZE_UNIT (type)
5151       && host_integerp (TYPE_SIZE_UNIT (type), 1))
5152     {
5153       tree field;
5154
5155       /* First check to see if there is any such field.  */
5156       for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5157         if (TREE_CODE (field) == FIELD_DECL
5158             && SCALAR_FLOAT_TYPE_P (TREE_TYPE (field))
5159             && TYPE_PRECISION (TREE_TYPE (field)) == BITS_PER_WORD
5160             && host_integerp (bit_position (field), 0)
5161             && int_bit_position (field) % BITS_PER_WORD == 0)
5162           break;
5163
5164       if (field != 0)
5165         {
5166           /* Now handle the special case by returning a PARALLEL
5167              indicating where each 64-bit chunk goes.  INFO.REG_WORDS
5168              chunks are passed in registers.  */
5169           unsigned int i;
5170           HOST_WIDE_INT bitpos;
5171           rtx ret;
5172
5173           /* assign_parms checks the mode of ENTRY_PARM, so we must
5174              use the actual mode here.  */
5175           ret = gen_rtx_PARALLEL (mode, rtvec_alloc (info.reg_words));
5176
5177           bitpos = 0;
5178           field = TYPE_FIELDS (type);
5179           for (i = 0; i < info.reg_words; i++)
5180             {
5181               rtx reg;
5182
5183               for (; field; field = DECL_CHAIN (field))
5184                 if (TREE_CODE (field) == FIELD_DECL
5185                     && int_bit_position (field) >= bitpos)
5186                   break;
5187
5188               if (field
5189                   && int_bit_position (field) == bitpos
5190                   && SCALAR_FLOAT_TYPE_P (TREE_TYPE (field))
5191                   && TYPE_PRECISION (TREE_TYPE (field)) == BITS_PER_WORD)
5192                 reg = gen_rtx_REG (DFmode, FP_ARG_FIRST + info.reg_offset + i);
5193               else
5194                 reg = gen_rtx_REG (DImode, GP_ARG_FIRST + info.reg_offset + i);
5195
5196               XVECEXP (ret, 0, i)
5197                 = gen_rtx_EXPR_LIST (VOIDmode, reg,
5198                                      GEN_INT (bitpos / BITS_PER_UNIT));
5199
5200               bitpos += BITS_PER_WORD;
5201             }
5202           return ret;
5203         }
5204     }
5205
5206   /* Handle the n32/n64 conventions for passing complex floating-point
5207      arguments in FPR pairs.  The real part goes in the lower register
5208      and the imaginary part goes in the upper register.  */
5209   if (TARGET_NEWABI
5210       && info.fpr_p
5211       && GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
5212     {
5213       rtx real, imag;
5214       enum machine_mode inner;
5215       unsigned int regno;
5216
5217       inner = GET_MODE_INNER (mode);
5218       regno = FP_ARG_FIRST + info.reg_offset;
5219       if (info.reg_words * UNITS_PER_WORD == GET_MODE_SIZE (inner))
5220         {
5221           /* Real part in registers, imaginary part on stack.  */
5222           gcc_assert (info.stack_words == info.reg_words);
5223           return gen_rtx_REG (inner, regno);
5224         }
5225       else
5226         {
5227           gcc_assert (info.stack_words == 0);
5228           real = gen_rtx_EXPR_LIST (VOIDmode,
5229                                     gen_rtx_REG (inner, regno),
5230                                     const0_rtx);
5231           imag = gen_rtx_EXPR_LIST (VOIDmode,
5232                                     gen_rtx_REG (inner,
5233                                                  regno + info.reg_words / 2),
5234                                     GEN_INT (GET_MODE_SIZE (inner)));
5235           return gen_rtx_PARALLEL (mode, gen_rtvec (2, real, imag));
5236         }
5237     }
5238
5239   return gen_rtx_REG (mode, mips_arg_regno (&info, TARGET_HARD_FLOAT));
5240 }
5241
5242 /* Implement TARGET_FUNCTION_ARG_ADVANCE.  */
5243
5244 static void
5245 mips_function_arg_advance (cumulative_args_t cum_v, enum machine_mode mode,
5246                            const_tree type, bool named)
5247 {
5248   CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
5249   struct mips_arg_info info;
5250
5251   mips_get_arg_info (&info, cum, mode, type, named);
5252
5253   if (!info.fpr_p)
5254     cum->gp_reg_found = true;
5255
5256   /* See the comment above the CUMULATIVE_ARGS structure in mips.h for
5257      an explanation of what this code does.  It assumes that we're using
5258      either the o32 or the o64 ABI, both of which pass at most 2 arguments
5259      in FPRs.  */
5260   if (cum->arg_number < 2 && info.fpr_p)
5261     cum->fp_code += (mode == SFmode ? 1 : 2) << (cum->arg_number * 2);
5262
5263   /* Advance the register count.  This has the effect of setting
5264      num_gprs to MAX_ARGS_IN_REGISTERS if a doubleword-aligned
5265      argument required us to skip the final GPR and pass the whole
5266      argument on the stack.  */
5267   if (mips_abi != ABI_EABI || !info.fpr_p)
5268     cum->num_gprs = info.reg_offset + info.reg_words;
5269   else if (info.reg_words > 0)
5270     cum->num_fprs += MAX_FPRS_PER_FMT;
5271
5272   /* Advance the stack word count.  */
5273   if (info.stack_words > 0)
5274     cum->stack_words = info.stack_offset + info.stack_words;
5275
5276   cum->arg_number++;
5277 }
5278
5279 /* Implement TARGET_ARG_PARTIAL_BYTES.  */
5280
5281 static int
5282 mips_arg_partial_bytes (cumulative_args_t cum,
5283                         enum machine_mode mode, tree type, bool named)
5284 {
5285   struct mips_arg_info info;
5286
5287   mips_get_arg_info (&info, get_cumulative_args (cum), mode, type, named);
5288   return info.stack_words > 0 ? info.reg_words * UNITS_PER_WORD : 0;
5289 }
5290
5291 /* Implement TARGET_FUNCTION_ARG_BOUNDARY.  Every parameter gets at
5292    least PARM_BOUNDARY bits of alignment, but will be given anything up
5293    to STACK_BOUNDARY bits if the type requires it.  */
5294
5295 static unsigned int
5296 mips_function_arg_boundary (enum machine_mode mode, const_tree type)
5297 {
5298   unsigned int alignment;
5299
5300   alignment = type ? TYPE_ALIGN (type) : GET_MODE_ALIGNMENT (mode);
5301   if (alignment < PARM_BOUNDARY)
5302     alignment = PARM_BOUNDARY;
5303   if (alignment > STACK_BOUNDARY)
5304     alignment = STACK_BOUNDARY;
5305   return alignment;
5306 }
5307
5308 /* Return true if FUNCTION_ARG_PADDING (MODE, TYPE) should return
5309    upward rather than downward.  In other words, return true if the
5310    first byte of the stack slot has useful data, false if the last
5311    byte does.  */
5312
5313 bool
5314 mips_pad_arg_upward (enum machine_mode mode, const_tree type)
5315 {
5316   /* On little-endian targets, the first byte of every stack argument
5317      is passed in the first byte of the stack slot.  */
5318   if (!BYTES_BIG_ENDIAN)
5319     return true;
5320
5321   /* Otherwise, integral types are padded downward: the last byte of a
5322      stack argument is passed in the last byte of the stack slot.  */
5323   if (type != 0
5324       ? (INTEGRAL_TYPE_P (type)
5325          || POINTER_TYPE_P (type)
5326          || FIXED_POINT_TYPE_P (type))
5327       : (SCALAR_INT_MODE_P (mode)
5328          || ALL_SCALAR_FIXED_POINT_MODE_P (mode)))
5329     return false;
5330
5331   /* Big-endian o64 pads floating-point arguments downward.  */
5332   if (mips_abi == ABI_O64)
5333     if (type != 0 ? FLOAT_TYPE_P (type) : GET_MODE_CLASS (mode) == MODE_FLOAT)
5334       return false;
5335
5336   /* Other types are padded upward for o32, o64, n32 and n64.  */
5337   if (mips_abi != ABI_EABI)
5338     return true;
5339
5340   /* Arguments smaller than a stack slot are padded downward.  */
5341   if (mode != BLKmode)
5342     return GET_MODE_BITSIZE (mode) >= PARM_BOUNDARY;
5343   else
5344     return int_size_in_bytes (type) >= (PARM_BOUNDARY / BITS_PER_UNIT);
5345 }
5346
5347 /* Likewise BLOCK_REG_PADDING (MODE, TYPE, ...).  Return !BYTES_BIG_ENDIAN
5348    if the least significant byte of the register has useful data.  Return
5349    the opposite if the most significant byte does.  */
5350
5351 bool
5352 mips_pad_reg_upward (enum machine_mode mode, tree type)
5353 {
5354   /* No shifting is required for floating-point arguments.  */
5355   if (type != 0 ? FLOAT_TYPE_P (type) : GET_MODE_CLASS (mode) == MODE_FLOAT)
5356     return !BYTES_BIG_ENDIAN;
5357
5358   /* Otherwise, apply the same padding to register arguments as we do
5359      to stack arguments.  */
5360   return mips_pad_arg_upward (mode, type);
5361 }
5362
5363 /* Return nonzero when an argument must be passed by reference.  */
5364
5365 static bool
5366 mips_pass_by_reference (cumulative_args_t cum ATTRIBUTE_UNUSED,
5367                         enum machine_mode mode, const_tree type,
5368                         bool named ATTRIBUTE_UNUSED)
5369 {
5370   if (mips_abi == ABI_EABI)
5371     {
5372       int size;
5373
5374       /* ??? How should SCmode be handled?  */
5375       if (mode == DImode || mode == DFmode
5376           || mode == DQmode || mode == UDQmode
5377           || mode == DAmode || mode == UDAmode)
5378         return 0;
5379
5380       size = type ? int_size_in_bytes (type) : GET_MODE_SIZE (mode);
5381       return size == -1 || size > UNITS_PER_WORD;
5382     }
5383   else
5384     {
5385       /* If we have a variable-sized parameter, we have no choice.  */
5386       return targetm.calls.must_pass_in_stack (mode, type);
5387     }
5388 }
5389
5390 /* Implement TARGET_CALLEE_COPIES.  */
5391
5392 static bool
5393 mips_callee_copies (cumulative_args_t cum ATTRIBUTE_UNUSED,
5394                     enum machine_mode mode ATTRIBUTE_UNUSED,
5395                     const_tree type ATTRIBUTE_UNUSED, bool named)
5396 {
5397   return mips_abi == ABI_EABI && named;
5398 }
5399 \f
5400 /* See whether VALTYPE is a record whose fields should be returned in
5401    floating-point registers.  If so, return the number of fields and
5402    list them in FIELDS (which should have two elements).  Return 0
5403    otherwise.
5404
5405    For n32 & n64, a structure with one or two fields is returned in
5406    floating-point registers as long as every field has a floating-point
5407    type.  */
5408
5409 static int
5410 mips_fpr_return_fields (const_tree valtype, tree *fields)
5411 {
5412   tree field;
5413   int i;
5414
5415   if (!TARGET_NEWABI)
5416     return 0;
5417
5418   if (TREE_CODE (valtype) != RECORD_TYPE)
5419     return 0;
5420
5421   i = 0;
5422   for (field = TYPE_FIELDS (valtype); field != 0; field = DECL_CHAIN (field))
5423     {
5424       if (TREE_CODE (field) != FIELD_DECL)
5425         continue;
5426
5427       if (!SCALAR_FLOAT_TYPE_P (TREE_TYPE (field)))
5428         return 0;
5429
5430       if (i == 2)
5431         return 0;
5432
5433       fields[i++] = field;
5434     }
5435   return i;
5436 }
5437
5438 /* Implement TARGET_RETURN_IN_MSB.  For n32 & n64, we should return
5439    a value in the most significant part of $2/$3 if:
5440
5441       - the target is big-endian;
5442
5443       - the value has a structure or union type (we generalize this to
5444         cover aggregates from other languages too); and
5445
5446       - the structure is not returned in floating-point registers.  */
5447
5448 static bool
5449 mips_return_in_msb (const_tree valtype)
5450 {
5451   tree fields[2];
5452
5453   return (TARGET_NEWABI
5454           && TARGET_BIG_ENDIAN
5455           && AGGREGATE_TYPE_P (valtype)
5456           && mips_fpr_return_fields (valtype, fields) == 0);
5457 }
5458
5459 /* Return true if the function return value MODE will get returned in a
5460    floating-point register.  */
5461
5462 static bool
5463 mips_return_mode_in_fpr_p (enum machine_mode mode)
5464 {
5465   return ((GET_MODE_CLASS (mode) == MODE_FLOAT
5466            || mode == V2SFmode
5467            || GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
5468           && GET_MODE_UNIT_SIZE (mode) <= UNITS_PER_HWFPVALUE);
5469 }
5470
5471 /* Return the representation of an FPR return register when the
5472    value being returned in FP_RETURN has mode VALUE_MODE and the
5473    return type itself has mode TYPE_MODE.  On NewABI targets,
5474    the two modes may be different for structures like:
5475
5476        struct __attribute__((packed)) foo { float f; }
5477
5478    where we return the SFmode value of "f" in FP_RETURN, but where
5479    the structure itself has mode BLKmode.  */
5480
5481 static rtx
5482 mips_return_fpr_single (enum machine_mode type_mode,
5483                         enum machine_mode value_mode)
5484 {
5485   rtx x;
5486
5487   x = gen_rtx_REG (value_mode, FP_RETURN);
5488   if (type_mode != value_mode)
5489     {
5490       x = gen_rtx_EXPR_LIST (VOIDmode, x, const0_rtx);
5491       x = gen_rtx_PARALLEL (type_mode, gen_rtvec (1, x));
5492     }
5493   return x;
5494 }
5495
5496 /* Return a composite value in a pair of floating-point registers.
5497    MODE1 and OFFSET1 are the mode and byte offset for the first value,
5498    likewise MODE2 and OFFSET2 for the second.  MODE is the mode of the
5499    complete value.
5500
5501    For n32 & n64, $f0 always holds the first value and $f2 the second.
5502    Otherwise the values are packed together as closely as possible.  */
5503
5504 static rtx
5505 mips_return_fpr_pair (enum machine_mode mode,
5506                       enum machine_mode mode1, HOST_WIDE_INT offset1,
5507                       enum machine_mode mode2, HOST_WIDE_INT offset2)
5508 {
5509   int inc;
5510
5511   inc = (TARGET_NEWABI ? 2 : MAX_FPRS_PER_FMT);
5512   return gen_rtx_PARALLEL
5513     (mode,
5514      gen_rtvec (2,
5515                 gen_rtx_EXPR_LIST (VOIDmode,
5516                                    gen_rtx_REG (mode1, FP_RETURN),
5517                                    GEN_INT (offset1)),
5518                 gen_rtx_EXPR_LIST (VOIDmode,
5519                                    gen_rtx_REG (mode2, FP_RETURN + inc),
5520                                    GEN_INT (offset2))));
5521
5522 }
5523
5524 /* Implement TARGET_FUNCTION_VALUE and TARGET_LIBCALL_VALUE.
5525    For normal calls, VALTYPE is the return type and MODE is VOIDmode.
5526    For libcalls, VALTYPE is null and MODE is the mode of the return value.  */
5527
5528 static rtx
5529 mips_function_value_1 (const_tree valtype, const_tree fn_decl_or_type,
5530                        enum machine_mode mode)
5531 {
5532   if (valtype)
5533     {
5534       tree fields[2];
5535       int unsigned_p;
5536       const_tree func;
5537
5538       if (fn_decl_or_type && DECL_P (fn_decl_or_type))
5539         func = fn_decl_or_type;
5540       else
5541         func = NULL;
5542
5543       mode = TYPE_MODE (valtype);
5544       unsigned_p = TYPE_UNSIGNED (valtype);
5545
5546       /* Since TARGET_PROMOTE_FUNCTION_MODE unconditionally promotes,
5547          return values, promote the mode here too.  */
5548       mode = promote_function_mode (valtype, mode, &unsigned_p, func, 1);
5549
5550       /* Handle structures whose fields are returned in $f0/$f2.  */
5551       switch (mips_fpr_return_fields (valtype, fields))
5552         {
5553         case 1:
5554           return mips_return_fpr_single (mode,
5555                                          TYPE_MODE (TREE_TYPE (fields[0])));
5556
5557         case 2:
5558           return mips_return_fpr_pair (mode,
5559                                        TYPE_MODE (TREE_TYPE (fields[0])),
5560                                        int_byte_position (fields[0]),
5561                                        TYPE_MODE (TREE_TYPE (fields[1])),
5562                                        int_byte_position (fields[1]));
5563         }
5564
5565       /* If a value is passed in the most significant part of a register, see
5566          whether we have to round the mode up to a whole number of words.  */
5567       if (mips_return_in_msb (valtype))
5568         {
5569           HOST_WIDE_INT size = int_size_in_bytes (valtype);
5570           if (size % UNITS_PER_WORD != 0)
5571             {
5572               size += UNITS_PER_WORD - size % UNITS_PER_WORD;
5573               mode = mode_for_size (size * BITS_PER_UNIT, MODE_INT, 0);
5574             }
5575         }
5576
5577       /* For EABI, the class of return register depends entirely on MODE.
5578          For example, "struct { some_type x; }" and "union { some_type x; }"
5579          are returned in the same way as a bare "some_type" would be.
5580          Other ABIs only use FPRs for scalar, complex or vector types.  */
5581       if (mips_abi != ABI_EABI && !FLOAT_TYPE_P (valtype))
5582         return gen_rtx_REG (mode, GP_RETURN);
5583     }
5584
5585   if (!TARGET_MIPS16)
5586     {
5587       /* Handle long doubles for n32 & n64.  */
5588       if (mode == TFmode)
5589         return mips_return_fpr_pair (mode,
5590                                      DImode, 0,
5591                                      DImode, GET_MODE_SIZE (mode) / 2);
5592
5593       if (mips_return_mode_in_fpr_p (mode))
5594         {
5595           if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
5596             return mips_return_fpr_pair (mode,
5597                                          GET_MODE_INNER (mode), 0,
5598                                          GET_MODE_INNER (mode),
5599                                          GET_MODE_SIZE (mode) / 2);
5600           else
5601             return gen_rtx_REG (mode, FP_RETURN);
5602         }
5603     }
5604
5605   return gen_rtx_REG (mode, GP_RETURN);
5606 }
5607
5608 /* Implement TARGET_FUNCTION_VALUE.  */
5609
5610 static rtx
5611 mips_function_value (const_tree valtype, const_tree fn_decl_or_type,
5612                      bool outgoing ATTRIBUTE_UNUSED)
5613 {
5614   return mips_function_value_1 (valtype, fn_decl_or_type, VOIDmode);
5615 }
5616
5617 /* Implement TARGET_LIBCALL_VALUE.  */
5618
5619 static rtx
5620 mips_libcall_value (enum machine_mode mode, const_rtx fun ATTRIBUTE_UNUSED)
5621 {
5622   return mips_function_value_1 (NULL_TREE, NULL_TREE, mode);
5623 }
5624
5625 /* Implement TARGET_FUNCTION_VALUE_REGNO_P.
5626
5627    On the MIPS, R2 R3 and F0 F2 are the only register thus used.
5628    Currently, R2 and F0 are only implemented here (C has no complex type).  */
5629
5630 static bool
5631 mips_function_value_regno_p (const unsigned int regno)
5632 {
5633   if (regno == GP_RETURN
5634       || regno == FP_RETURN
5635       || (LONG_DOUBLE_TYPE_SIZE == 128
5636           && FP_RETURN != GP_RETURN
5637           && regno == FP_RETURN + 2))
5638     return true;
5639
5640   return false;
5641 }
5642
5643 /* Implement TARGET_RETURN_IN_MEMORY.  Under the o32 and o64 ABIs,
5644    all BLKmode objects are returned in memory.  Under the n32, n64
5645    and embedded ABIs, small structures are returned in a register.
5646    Objects with varying size must still be returned in memory, of
5647    course.  */
5648
5649 static bool
5650 mips_return_in_memory (const_tree type, const_tree fndecl ATTRIBUTE_UNUSED)
5651 {
5652   return (TARGET_OLDABI
5653           ? TYPE_MODE (type) == BLKmode
5654           : !IN_RANGE (int_size_in_bytes (type), 0, 2 * UNITS_PER_WORD));
5655 }
5656 \f
5657 /* Implement TARGET_SETUP_INCOMING_VARARGS.  */
5658
5659 static void
5660 mips_setup_incoming_varargs (cumulative_args_t cum, enum machine_mode mode,
5661                              tree type, int *pretend_size ATTRIBUTE_UNUSED,
5662                              int no_rtl)
5663 {
5664   CUMULATIVE_ARGS local_cum;
5665   int gp_saved, fp_saved;
5666
5667   /* The caller has advanced CUM up to, but not beyond, the last named
5668      argument.  Advance a local copy of CUM past the last "real" named
5669      argument, to find out how many registers are left over.  */
5670   local_cum = *get_cumulative_args (cum);
5671   mips_function_arg_advance (pack_cumulative_args (&local_cum), mode, type,
5672                              true);
5673
5674   /* Found out how many registers we need to save.  */
5675   gp_saved = MAX_ARGS_IN_REGISTERS - local_cum.num_gprs;
5676   fp_saved = (EABI_FLOAT_VARARGS_P
5677               ? MAX_ARGS_IN_REGISTERS - local_cum.num_fprs
5678               : 0);
5679
5680   if (!no_rtl)
5681     {
5682       if (gp_saved > 0)
5683         {
5684           rtx ptr, mem;
5685
5686           ptr = plus_constant (Pmode, virtual_incoming_args_rtx,
5687                                REG_PARM_STACK_SPACE (cfun->decl)
5688                                - gp_saved * UNITS_PER_WORD);
5689           mem = gen_frame_mem (BLKmode, ptr);
5690           set_mem_alias_set (mem, get_varargs_alias_set ());
5691
5692           move_block_from_reg (local_cum.num_gprs + GP_ARG_FIRST,
5693                                mem, gp_saved);
5694         }
5695       if (fp_saved > 0)
5696         {
5697           /* We can't use move_block_from_reg, because it will use
5698              the wrong mode.  */
5699           enum machine_mode mode;
5700           int off, i;
5701
5702           /* Set OFF to the offset from virtual_incoming_args_rtx of
5703              the first float register.  The FP save area lies below
5704              the integer one, and is aligned to UNITS_PER_FPVALUE bytes.  */
5705           off = (-gp_saved * UNITS_PER_WORD) & -UNITS_PER_FPVALUE;
5706           off -= fp_saved * UNITS_PER_FPREG;
5707
5708           mode = TARGET_SINGLE_FLOAT ? SFmode : DFmode;
5709
5710           for (i = local_cum.num_fprs; i < MAX_ARGS_IN_REGISTERS;
5711                i += MAX_FPRS_PER_FMT)
5712             {
5713               rtx ptr, mem;
5714
5715               ptr = plus_constant (Pmode, virtual_incoming_args_rtx, off);
5716               mem = gen_frame_mem (mode, ptr);
5717               set_mem_alias_set (mem, get_varargs_alias_set ());
5718               mips_emit_move (mem, gen_rtx_REG (mode, FP_ARG_FIRST + i));
5719               off += UNITS_PER_HWFPVALUE;
5720             }
5721         }
5722     }
5723   if (REG_PARM_STACK_SPACE (cfun->decl) == 0)
5724     cfun->machine->varargs_size = (gp_saved * UNITS_PER_WORD
5725                                    + fp_saved * UNITS_PER_FPREG);
5726 }
5727
5728 /* Implement TARGET_BUILTIN_VA_LIST.  */
5729
5730 static tree
5731 mips_build_builtin_va_list (void)
5732 {
5733   if (EABI_FLOAT_VARARGS_P)
5734     {
5735       /* We keep 3 pointers, and two offsets.
5736
5737          Two pointers are to the overflow area, which starts at the CFA.
5738          One of these is constant, for addressing into the GPR save area
5739          below it.  The other is advanced up the stack through the
5740          overflow region.
5741
5742          The third pointer is to the bottom of the GPR save area.
5743          Since the FPR save area is just below it, we can address
5744          FPR slots off this pointer.
5745
5746          We also keep two one-byte offsets, which are to be subtracted
5747          from the constant pointers to yield addresses in the GPR and
5748          FPR save areas.  These are downcounted as float or non-float
5749          arguments are used, and when they get to zero, the argument
5750          must be obtained from the overflow region.  */
5751       tree f_ovfl, f_gtop, f_ftop, f_goff, f_foff, f_res, record;
5752       tree array, index;
5753
5754       record = lang_hooks.types.make_type (RECORD_TYPE);
5755
5756       f_ovfl = build_decl (BUILTINS_LOCATION,
5757                            FIELD_DECL, get_identifier ("__overflow_argptr"),
5758                            ptr_type_node);
5759       f_gtop = build_decl (BUILTINS_LOCATION,
5760                            FIELD_DECL, get_identifier ("__gpr_top"),
5761                            ptr_type_node);
5762       f_ftop = build_decl (BUILTINS_LOCATION,
5763                            FIELD_DECL, get_identifier ("__fpr_top"),
5764                            ptr_type_node);
5765       f_goff = build_decl (BUILTINS_LOCATION,
5766                            FIELD_DECL, get_identifier ("__gpr_offset"),
5767                            unsigned_char_type_node);
5768       f_foff = build_decl (BUILTINS_LOCATION,
5769                            FIELD_DECL, get_identifier ("__fpr_offset"),
5770                            unsigned_char_type_node);
5771       /* Explicitly pad to the size of a pointer, so that -Wpadded won't
5772          warn on every user file.  */
5773       index = build_int_cst (NULL_TREE, GET_MODE_SIZE (ptr_mode) - 2 - 1);
5774       array = build_array_type (unsigned_char_type_node,
5775                                 build_index_type (index));
5776       f_res = build_decl (BUILTINS_LOCATION,
5777                           FIELD_DECL, get_identifier ("__reserved"), array);
5778
5779       DECL_FIELD_CONTEXT (f_ovfl) = record;
5780       DECL_FIELD_CONTEXT (f_gtop) = record;
5781       DECL_FIELD_CONTEXT (f_ftop) = record;
5782       DECL_FIELD_CONTEXT (f_goff) = record;
5783       DECL_FIELD_CONTEXT (f_foff) = record;
5784       DECL_FIELD_CONTEXT (f_res) = record;
5785
5786       TYPE_FIELDS (record) = f_ovfl;
5787       DECL_CHAIN (f_ovfl) = f_gtop;
5788       DECL_CHAIN (f_gtop) = f_ftop;
5789       DECL_CHAIN (f_ftop) = f_goff;
5790       DECL_CHAIN (f_goff) = f_foff;
5791       DECL_CHAIN (f_foff) = f_res;
5792
5793       layout_type (record);
5794       return record;
5795     }
5796   else
5797     /* Otherwise, we use 'void *'.  */
5798     return ptr_type_node;
5799 }
5800
5801 /* Implement TARGET_EXPAND_BUILTIN_VA_START.  */
5802
5803 static void
5804 mips_va_start (tree valist, rtx nextarg)
5805 {
5806   if (EABI_FLOAT_VARARGS_P)
5807     {
5808       const CUMULATIVE_ARGS *cum;
5809       tree f_ovfl, f_gtop, f_ftop, f_goff, f_foff;
5810       tree ovfl, gtop, ftop, goff, foff;
5811       tree t;
5812       int gpr_save_area_size;
5813       int fpr_save_area_size;
5814       int fpr_offset;
5815
5816       cum = &crtl->args.info;
5817       gpr_save_area_size
5818         = (MAX_ARGS_IN_REGISTERS - cum->num_gprs) * UNITS_PER_WORD;
5819       fpr_save_area_size
5820         = (MAX_ARGS_IN_REGISTERS - cum->num_fprs) * UNITS_PER_FPREG;
5821
5822       f_ovfl = TYPE_FIELDS (va_list_type_node);
5823       f_gtop = DECL_CHAIN (f_ovfl);
5824       f_ftop = DECL_CHAIN (f_gtop);
5825       f_goff = DECL_CHAIN (f_ftop);
5826       f_foff = DECL_CHAIN (f_goff);
5827
5828       ovfl = build3 (COMPONENT_REF, TREE_TYPE (f_ovfl), valist, f_ovfl,
5829                      NULL_TREE);
5830       gtop = build3 (COMPONENT_REF, TREE_TYPE (f_gtop), valist, f_gtop,
5831                      NULL_TREE);
5832       ftop = build3 (COMPONENT_REF, TREE_TYPE (f_ftop), valist, f_ftop,
5833                      NULL_TREE);
5834       goff = build3 (COMPONENT_REF, TREE_TYPE (f_goff), valist, f_goff,
5835                      NULL_TREE);
5836       foff = build3 (COMPONENT_REF, TREE_TYPE (f_foff), valist, f_foff,
5837                      NULL_TREE);
5838
5839       /* Emit code to initialize OVFL, which points to the next varargs
5840          stack argument.  CUM->STACK_WORDS gives the number of stack
5841          words used by named arguments.  */
5842       t = make_tree (TREE_TYPE (ovfl), virtual_incoming_args_rtx);
5843       if (cum->stack_words > 0)
5844         t = fold_build_pointer_plus_hwi (t, cum->stack_words * UNITS_PER_WORD);
5845       t = build2 (MODIFY_EXPR, TREE_TYPE (ovfl), ovfl, t);
5846       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5847
5848       /* Emit code to initialize GTOP, the top of the GPR save area.  */
5849       t = make_tree (TREE_TYPE (gtop), virtual_incoming_args_rtx);
5850       t = build2 (MODIFY_EXPR, TREE_TYPE (gtop), gtop, t);
5851       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5852
5853       /* Emit code to initialize FTOP, the top of the FPR save area.
5854          This address is gpr_save_area_bytes below GTOP, rounded
5855          down to the next fp-aligned boundary.  */
5856       t = make_tree (TREE_TYPE (ftop), virtual_incoming_args_rtx);
5857       fpr_offset = gpr_save_area_size + UNITS_PER_FPVALUE - 1;
5858       fpr_offset &= -UNITS_PER_FPVALUE;
5859       if (fpr_offset)
5860         t = fold_build_pointer_plus_hwi (t, -fpr_offset);
5861       t = build2 (MODIFY_EXPR, TREE_TYPE (ftop), ftop, t);
5862       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5863
5864       /* Emit code to initialize GOFF, the offset from GTOP of the
5865          next GPR argument.  */
5866       t = build2 (MODIFY_EXPR, TREE_TYPE (goff), goff,
5867                   build_int_cst (TREE_TYPE (goff), gpr_save_area_size));
5868       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5869
5870       /* Likewise emit code to initialize FOFF, the offset from FTOP
5871          of the next FPR argument.  */
5872       t = build2 (MODIFY_EXPR, TREE_TYPE (foff), foff,
5873                   build_int_cst (TREE_TYPE (foff), fpr_save_area_size));
5874       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5875     }
5876   else
5877     {
5878       nextarg = plus_constant (Pmode, nextarg, -cfun->machine->varargs_size);
5879       std_expand_builtin_va_start (valist, nextarg);
5880     }
5881 }
5882
5883 /* Like std_gimplify_va_arg_expr, but apply alignment to zero-sized
5884    types as well.  */
5885
5886 static tree
5887 mips_std_gimplify_va_arg_expr (tree valist, tree type, gimple_seq *pre_p,
5888                                gimple_seq *post_p)
5889 {
5890   tree addr, t, type_size, rounded_size, valist_tmp;
5891   unsigned HOST_WIDE_INT align, boundary;
5892   bool indirect;
5893
5894   indirect = pass_by_reference (NULL, TYPE_MODE (type), type, false);
5895   if (indirect)
5896     type = build_pointer_type (type);
5897
5898   align = PARM_BOUNDARY / BITS_PER_UNIT;
5899   boundary = targetm.calls.function_arg_boundary (TYPE_MODE (type), type);
5900
5901   /* When we align parameter on stack for caller, if the parameter
5902      alignment is beyond MAX_SUPPORTED_STACK_ALIGNMENT, it will be
5903      aligned at MAX_SUPPORTED_STACK_ALIGNMENT.  We will match callee
5904      here with caller.  */
5905   if (boundary > MAX_SUPPORTED_STACK_ALIGNMENT)
5906     boundary = MAX_SUPPORTED_STACK_ALIGNMENT;
5907
5908   boundary /= BITS_PER_UNIT;
5909
5910   /* Hoist the valist value into a temporary for the moment.  */
5911   valist_tmp = get_initialized_tmp_var (valist, pre_p, NULL);
5912
5913   /* va_list pointer is aligned to PARM_BOUNDARY.  If argument actually
5914      requires greater alignment, we must perform dynamic alignment.  */
5915   if (boundary > align)
5916     {
5917       t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
5918                   fold_build_pointer_plus_hwi (valist_tmp, boundary - 1));
5919       gimplify_and_add (t, pre_p);
5920
5921       t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
5922                   fold_build2 (BIT_AND_EXPR, TREE_TYPE (valist),
5923                                valist_tmp,
5924                                build_int_cst (TREE_TYPE (valist), -boundary)));
5925       gimplify_and_add (t, pre_p);
5926     }
5927   else
5928     boundary = align;
5929
5930   /* If the actual alignment is less than the alignment of the type,
5931      adjust the type accordingly so that we don't assume strict alignment
5932      when dereferencing the pointer.  */
5933   boundary *= BITS_PER_UNIT;
5934   if (boundary < TYPE_ALIGN (type))
5935     {
5936       type = build_variant_type_copy (type);
5937       TYPE_ALIGN (type) = boundary;
5938     }
5939
5940   /* Compute the rounded size of the type.  */
5941   type_size = size_in_bytes (type);
5942   rounded_size = round_up (type_size, align);
5943
5944   /* Reduce rounded_size so it's sharable with the postqueue.  */
5945   gimplify_expr (&rounded_size, pre_p, post_p, is_gimple_val, fb_rvalue);
5946
5947   /* Get AP.  */
5948   addr = valist_tmp;
5949   if (PAD_VARARGS_DOWN && !integer_zerop (rounded_size))
5950     {
5951       /* Small args are padded downward.  */
5952       t = fold_build2_loc (input_location, GT_EXPR, sizetype,
5953                        rounded_size, size_int (align));
5954       t = fold_build3 (COND_EXPR, sizetype, t, size_zero_node,
5955                        size_binop (MINUS_EXPR, rounded_size, type_size));
5956       addr = fold_build_pointer_plus (addr, t);
5957     }
5958
5959   /* Compute new value for AP.  */
5960   t = fold_build_pointer_plus (valist_tmp, rounded_size);
5961   t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist, t);
5962   gimplify_and_add (t, pre_p);
5963
5964   addr = fold_convert (build_pointer_type (type), addr);
5965
5966   if (indirect)
5967     addr = build_va_arg_indirect_ref (addr);
5968
5969   return build_va_arg_indirect_ref (addr);
5970 }
5971
5972 /* Implement TARGET_GIMPLIFY_VA_ARG_EXPR.  */
5973
5974 static tree
5975 mips_gimplify_va_arg_expr (tree valist, tree type, gimple_seq *pre_p,
5976                            gimple_seq *post_p)
5977 {
5978   tree addr;
5979   bool indirect_p;
5980
5981   indirect_p = pass_by_reference (NULL, TYPE_MODE (type), type, 0);
5982   if (indirect_p)
5983     type = build_pointer_type (type);
5984
5985   if (!EABI_FLOAT_VARARGS_P)
5986     addr = mips_std_gimplify_va_arg_expr (valist, type, pre_p, post_p);
5987   else
5988     {
5989       tree f_ovfl, f_gtop, f_ftop, f_goff, f_foff;
5990       tree ovfl, top, off, align;
5991       HOST_WIDE_INT size, rsize, osize;
5992       tree t, u;
5993
5994       f_ovfl = TYPE_FIELDS (va_list_type_node);
5995       f_gtop = DECL_CHAIN (f_ovfl);
5996       f_ftop = DECL_CHAIN (f_gtop);
5997       f_goff = DECL_CHAIN (f_ftop);
5998       f_foff = DECL_CHAIN (f_goff);
5999
6000       /* Let:
6001
6002          TOP be the top of the GPR or FPR save area;
6003          OFF be the offset from TOP of the next register;
6004          ADDR_RTX be the address of the argument;
6005          SIZE be the number of bytes in the argument type;
6006          RSIZE be the number of bytes used to store the argument
6007            when it's in the register save area; and
6008          OSIZE be the number of bytes used to store it when it's
6009            in the stack overflow area.
6010
6011          The code we want is:
6012
6013          1: off &= -rsize;        // round down
6014          2: if (off != 0)
6015          3:   {
6016          4:     addr_rtx = top - off + (BYTES_BIG_ENDIAN ? RSIZE - SIZE : 0);
6017          5:     off -= rsize;
6018          6:   }
6019          7: else
6020          8:   {
6021          9:     ovfl = ((intptr_t) ovfl + osize - 1) & -osize;
6022          10:    addr_rtx = ovfl + (BYTES_BIG_ENDIAN ? OSIZE - SIZE : 0);
6023          11:    ovfl += osize;
6024          14:  }
6025
6026          [1] and [9] can sometimes be optimized away.  */
6027
6028       ovfl = build3 (COMPONENT_REF, TREE_TYPE (f_ovfl), valist, f_ovfl,
6029                      NULL_TREE);
6030       size = int_size_in_bytes (type);
6031
6032       if (GET_MODE_CLASS (TYPE_MODE (type)) == MODE_FLOAT
6033           && GET_MODE_SIZE (TYPE_MODE (type)) <= UNITS_PER_FPVALUE)
6034         {
6035           top = build3 (COMPONENT_REF, TREE_TYPE (f_ftop),
6036                         unshare_expr (valist), f_ftop, NULL_TREE);
6037           off = build3 (COMPONENT_REF, TREE_TYPE (f_foff),
6038                         unshare_expr (valist), f_foff, NULL_TREE);
6039
6040           /* When va_start saves FPR arguments to the stack, each slot
6041              takes up UNITS_PER_HWFPVALUE bytes, regardless of the
6042              argument's precision.  */
6043           rsize = UNITS_PER_HWFPVALUE;
6044
6045           /* Overflow arguments are padded to UNITS_PER_WORD bytes
6046              (= PARM_BOUNDARY bits).  This can be different from RSIZE
6047              in two cases:
6048
6049              (1) On 32-bit targets when TYPE is a structure such as:
6050
6051              struct s { float f; };
6052
6053              Such structures are passed in paired FPRs, so RSIZE
6054              will be 8 bytes.  However, the structure only takes
6055              up 4 bytes of memory, so OSIZE will only be 4.
6056
6057              (2) In combinations such as -mgp64 -msingle-float
6058              -fshort-double.  Doubles passed in registers will then take
6059              up 4 (UNITS_PER_HWFPVALUE) bytes, but those passed on the
6060              stack take up UNITS_PER_WORD bytes.  */
6061           osize = MAX (GET_MODE_SIZE (TYPE_MODE (type)), UNITS_PER_WORD);
6062         }
6063       else
6064         {
6065           top = build3 (COMPONENT_REF, TREE_TYPE (f_gtop),
6066                         unshare_expr (valist), f_gtop, NULL_TREE);
6067           off = build3 (COMPONENT_REF, TREE_TYPE (f_goff),
6068                         unshare_expr (valist), f_goff, NULL_TREE);
6069           rsize = (size + UNITS_PER_WORD - 1) & -UNITS_PER_WORD;
6070           if (rsize > UNITS_PER_WORD)
6071             {
6072               /* [1] Emit code for: off &= -rsize.      */
6073               t = build2 (BIT_AND_EXPR, TREE_TYPE (off), unshare_expr (off),
6074                           build_int_cst (TREE_TYPE (off), -rsize));
6075               gimplify_assign (unshare_expr (off), t, pre_p);
6076             }
6077           osize = rsize;
6078         }
6079
6080       /* [2] Emit code to branch if off == 0.  */
6081       t = build2 (NE_EXPR, boolean_type_node, unshare_expr (off),
6082                   build_int_cst (TREE_TYPE (off), 0));
6083       addr = build3 (COND_EXPR, ptr_type_node, t, NULL_TREE, NULL_TREE);
6084
6085       /* [5] Emit code for: off -= rsize.  We do this as a form of
6086          post-decrement not available to C.  */
6087       t = fold_convert (TREE_TYPE (off), build_int_cst (NULL_TREE, rsize));
6088       t = build2 (POSTDECREMENT_EXPR, TREE_TYPE (off), off, t);
6089
6090       /* [4] Emit code for:
6091          addr_rtx = top - off + (BYTES_BIG_ENDIAN ? RSIZE - SIZE : 0).  */
6092       t = fold_convert (sizetype, t);
6093       t = fold_build1 (NEGATE_EXPR, sizetype, t);
6094       t = fold_build_pointer_plus (top, t);
6095       if (BYTES_BIG_ENDIAN && rsize > size)
6096         t = fold_build_pointer_plus_hwi (t, rsize - size);
6097       COND_EXPR_THEN (addr) = t;
6098
6099       if (osize > UNITS_PER_WORD)
6100         {
6101           /* [9] Emit: ovfl = ((intptr_t) ovfl + osize - 1) & -osize.  */
6102           t = fold_build_pointer_plus_hwi (unshare_expr (ovfl), osize - 1);
6103           u = build_int_cst (TREE_TYPE (t), -osize);
6104           t = build2 (BIT_AND_EXPR, TREE_TYPE (t), t, u);
6105           align = build2 (MODIFY_EXPR, TREE_TYPE (ovfl),
6106                           unshare_expr (ovfl), t);
6107         }
6108       else
6109         align = NULL;
6110
6111       /* [10, 11] Emit code for:
6112          addr_rtx = ovfl + (BYTES_BIG_ENDIAN ? OSIZE - SIZE : 0)
6113          ovfl += osize.  */
6114       u = fold_convert (TREE_TYPE (ovfl), build_int_cst (NULL_TREE, osize));
6115       t = build2 (POSTINCREMENT_EXPR, TREE_TYPE (ovfl), ovfl, u);
6116       if (BYTES_BIG_ENDIAN && osize > size)
6117         t = fold_build_pointer_plus_hwi (t, osize - size);
6118
6119       /* String [9] and [10, 11] together.  */
6120       if (align)
6121         t = build2 (COMPOUND_EXPR, TREE_TYPE (t), align, t);
6122       COND_EXPR_ELSE (addr) = t;
6123
6124       addr = fold_convert (build_pointer_type (type), addr);
6125       addr = build_va_arg_indirect_ref (addr);
6126     }
6127
6128   if (indirect_p)
6129     addr = build_va_arg_indirect_ref (addr);
6130
6131   return addr;
6132 }
6133 \f
6134 /* Declare a unique, locally-binding function called NAME, then start
6135    its definition.  */
6136
6137 static void
6138 mips_start_unique_function (const char *name)
6139 {
6140   tree decl;
6141
6142   decl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
6143                      get_identifier (name),
6144                      build_function_type_list (void_type_node, NULL_TREE));
6145   DECL_RESULT (decl) = build_decl (BUILTINS_LOCATION, RESULT_DECL,
6146                                    NULL_TREE, void_type_node);
6147   TREE_PUBLIC (decl) = 1;
6148   TREE_STATIC (decl) = 1;
6149
6150   DECL_COMDAT_GROUP (decl) = DECL_ASSEMBLER_NAME (decl);
6151
6152   targetm.asm_out.unique_section (decl, 0);
6153   switch_to_section (get_named_section (decl, NULL, 0));
6154
6155   targetm.asm_out.globalize_label (asm_out_file, name);
6156   fputs ("\t.hidden\t", asm_out_file);
6157   assemble_name (asm_out_file, name);
6158   putc ('\n', asm_out_file);
6159 }
6160
6161 /* Start a definition of function NAME.  MIPS16_P indicates whether the
6162    function contains MIPS16 code.  */
6163
6164 static void
6165 mips_start_function_definition (const char *name, bool mips16_p)
6166 {
6167   if (mips16_p)
6168     fprintf (asm_out_file, "\t.set\tmips16\n");
6169   else
6170     fprintf (asm_out_file, "\t.set\tnomips16\n");
6171
6172   if (TARGET_MICROMIPS)
6173     fprintf (asm_out_file, "\t.set\tmicromips\n");
6174 #ifdef HAVE_GAS_MICROMIPS
6175   else
6176     fprintf (asm_out_file, "\t.set\tnomicromips\n");
6177 #endif
6178
6179   if (!flag_inhibit_size_directive)
6180     {
6181       fputs ("\t.ent\t", asm_out_file);
6182       assemble_name (asm_out_file, name);
6183       fputs ("\n", asm_out_file);
6184     }
6185
6186   ASM_OUTPUT_TYPE_DIRECTIVE (asm_out_file, name, "function");
6187
6188   /* Start the definition proper.  */
6189   assemble_name (asm_out_file, name);
6190   fputs (":\n", asm_out_file);
6191 }
6192
6193 /* End a function definition started by mips_start_function_definition.  */
6194
6195 static void
6196 mips_end_function_definition (const char *name)
6197 {
6198   if (!flag_inhibit_size_directive)
6199     {
6200       fputs ("\t.end\t", asm_out_file);
6201       assemble_name (asm_out_file, name);
6202       fputs ("\n", asm_out_file);
6203     }
6204 }
6205 \f
6206 /* Output a definition of the __mips16_rdhwr function.  */
6207
6208 static void
6209 mips_output_mips16_rdhwr (void)
6210 {
6211   const char *name;
6212
6213   name = "__mips16_rdhwr";
6214   mips_start_unique_function (name);
6215   mips_start_function_definition (name, false);
6216   fprintf (asm_out_file,
6217            "\t.set\tpush\n"
6218            "\t.set\tmips32r2\n"
6219            "\t.set\tnoreorder\n"
6220            "\trdhwr\t$3,$29\n"
6221            "\t.set\tpop\n"
6222            "\tj\t$31\n");
6223   mips_end_function_definition (name);
6224 }
6225 \f
6226 /* Return true if calls to X can use R_MIPS_CALL* relocations.  */
6227
6228 static bool
6229 mips_ok_for_lazy_binding_p (rtx x)
6230 {
6231   return (TARGET_USE_GOT
6232           && GET_CODE (x) == SYMBOL_REF
6233           && !SYMBOL_REF_BIND_NOW_P (x)
6234           && !mips_symbol_binds_local_p (x));
6235 }
6236
6237 /* Load function address ADDR into register DEST.  TYPE is as for
6238    mips_expand_call.  Return true if we used an explicit lazy-binding
6239    sequence.  */
6240
6241 static bool
6242 mips_load_call_address (enum mips_call_type type, rtx dest, rtx addr)
6243 {
6244   /* If we're generating PIC, and this call is to a global function,
6245      try to allow its address to be resolved lazily.  This isn't
6246      possible for sibcalls when $gp is call-saved because the value
6247      of $gp on entry to the stub would be our caller's gp, not ours.  */
6248   if (TARGET_EXPLICIT_RELOCS
6249       && !(type == MIPS_CALL_SIBCALL && TARGET_CALL_SAVED_GP)
6250       && mips_ok_for_lazy_binding_p (addr))
6251     {
6252       addr = mips_got_load (dest, addr, SYMBOL_GOTOFF_CALL);
6253       emit_insn (gen_rtx_SET (VOIDmode, dest, addr));
6254       return true;
6255     }
6256   else
6257     {
6258       mips_emit_move (dest, addr);
6259       return false;
6260     }
6261 }
6262 \f
6263 /* Each locally-defined hard-float MIPS16 function has a local symbol
6264    associated with it.  This hash table maps the function symbol (FUNC)
6265    to the local symbol (LOCAL). */
6266 struct GTY(()) mips16_local_alias {
6267   rtx func;
6268   rtx local;
6269 };
6270 static GTY ((param_is (struct mips16_local_alias))) htab_t mips16_local_aliases;
6271
6272 /* Hash table callbacks for mips16_local_aliases.  */
6273
6274 static hashval_t
6275 mips16_local_aliases_hash (const void *entry)
6276 {
6277   const struct mips16_local_alias *alias;
6278
6279   alias = (const struct mips16_local_alias *) entry;
6280   return htab_hash_string (XSTR (alias->func, 0));
6281 }
6282
6283 static int
6284 mips16_local_aliases_eq (const void *entry1, const void *entry2)
6285 {
6286   const struct mips16_local_alias *alias1, *alias2;
6287
6288   alias1 = (const struct mips16_local_alias *) entry1;
6289   alias2 = (const struct mips16_local_alias *) entry2;
6290   return rtx_equal_p (alias1->func, alias2->func);
6291 }
6292
6293 /* FUNC is the symbol for a locally-defined hard-float MIPS16 function.
6294    Return a local alias for it, creating a new one if necessary.  */
6295
6296 static rtx
6297 mips16_local_alias (rtx func)
6298 {
6299   struct mips16_local_alias *alias, tmp_alias;
6300   void **slot;
6301
6302   /* Create the hash table if this is the first call.  */
6303   if (mips16_local_aliases == NULL)
6304     mips16_local_aliases = htab_create_ggc (37, mips16_local_aliases_hash,
6305                                             mips16_local_aliases_eq, NULL);
6306
6307   /* Look up the function symbol, creating a new entry if need be.  */
6308   tmp_alias.func = func;
6309   slot = htab_find_slot (mips16_local_aliases, &tmp_alias, INSERT);
6310   gcc_assert (slot != NULL);
6311
6312   alias = (struct mips16_local_alias *) *slot;
6313   if (alias == NULL)
6314     {
6315       const char *func_name, *local_name;
6316       rtx local;
6317
6318       /* Create a new SYMBOL_REF for the local symbol.  The choice of
6319          __fn_local_* is based on the __fn_stub_* names that we've
6320          traditionally used for the non-MIPS16 stub.  */
6321       func_name = targetm.strip_name_encoding (XSTR (func, 0));
6322       local_name = ACONCAT (("__fn_local_", func_name, NULL));
6323       local = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (local_name));
6324       SYMBOL_REF_FLAGS (local) = SYMBOL_REF_FLAGS (func) | SYMBOL_FLAG_LOCAL;
6325
6326       /* Create a new structure to represent the mapping.  */
6327       alias = ggc_alloc_mips16_local_alias ();
6328       alias->func = func;
6329       alias->local = local;
6330       *slot = alias;
6331     }
6332   return alias->local;
6333 }
6334 \f
6335 /* A chained list of functions for which mips16_build_call_stub has already
6336    generated a stub.  NAME is the name of the function and FP_RET_P is true
6337    if the function returns a value in floating-point registers.  */
6338 struct mips16_stub {
6339   struct mips16_stub *next;
6340   char *name;
6341   bool fp_ret_p;
6342 };
6343 static struct mips16_stub *mips16_stubs;
6344
6345 /* Return the two-character string that identifies floating-point
6346    return mode MODE in the name of a MIPS16 function stub.  */
6347
6348 static const char *
6349 mips16_call_stub_mode_suffix (enum machine_mode mode)
6350 {
6351   if (mode == SFmode)
6352     return "sf";
6353   else if (mode == DFmode)
6354     return "df";
6355   else if (mode == SCmode)
6356     return "sc";
6357   else if (mode == DCmode)
6358     return "dc";
6359   else if (mode == V2SFmode)
6360     return "df";
6361   else
6362     gcc_unreachable ();
6363 }
6364
6365 /* Write instructions to move a 32-bit value between general register
6366    GPREG and floating-point register FPREG.  DIRECTION is 't' to move
6367    from GPREG to FPREG and 'f' to move in the opposite direction.  */
6368
6369 static void
6370 mips_output_32bit_xfer (char direction, unsigned int gpreg, unsigned int fpreg)
6371 {
6372   fprintf (asm_out_file, "\tm%cc1\t%s,%s\n", direction,
6373            reg_names[gpreg], reg_names[fpreg]);
6374 }
6375
6376 /* Likewise for 64-bit values.  */
6377
6378 static void
6379 mips_output_64bit_xfer (char direction, unsigned int gpreg, unsigned int fpreg)
6380 {
6381   if (TARGET_64BIT)
6382     fprintf (asm_out_file, "\tdm%cc1\t%s,%s\n", direction,
6383              reg_names[gpreg], reg_names[fpreg]);
6384   else if (TARGET_FLOAT64)
6385     {
6386       fprintf (asm_out_file, "\tm%cc1\t%s,%s\n", direction,
6387                reg_names[gpreg + TARGET_BIG_ENDIAN], reg_names[fpreg]);
6388       fprintf (asm_out_file, "\tm%chc1\t%s,%s\n", direction,
6389                reg_names[gpreg + TARGET_LITTLE_ENDIAN], reg_names[fpreg]);
6390     }
6391   else
6392     {
6393       /* Move the least-significant word.  */
6394       fprintf (asm_out_file, "\tm%cc1\t%s,%s\n", direction,
6395                reg_names[gpreg + TARGET_BIG_ENDIAN], reg_names[fpreg]);
6396       /* ...then the most significant word.  */
6397       fprintf (asm_out_file, "\tm%cc1\t%s,%s\n", direction,
6398                reg_names[gpreg + TARGET_LITTLE_ENDIAN], reg_names[fpreg + 1]);
6399     }
6400 }
6401
6402 /* Write out code to move floating-point arguments into or out of
6403    general registers.  FP_CODE is the code describing which arguments
6404    are present (see the comment above the definition of CUMULATIVE_ARGS
6405    in mips.h).  DIRECTION is as for mips_output_32bit_xfer.  */
6406
6407 static void
6408 mips_output_args_xfer (int fp_code, char direction)
6409 {
6410   unsigned int gparg, fparg, f;
6411   CUMULATIVE_ARGS cum;
6412
6413   /* This code only works for o32 and o64.  */
6414   gcc_assert (TARGET_OLDABI);
6415
6416   mips_init_cumulative_args (&cum, NULL);
6417
6418   for (f = (unsigned int) fp_code; f != 0; f >>= 2)
6419     {
6420       enum machine_mode mode;
6421       struct mips_arg_info info;
6422
6423       if ((f & 3) == 1)
6424         mode = SFmode;
6425       else if ((f & 3) == 2)
6426         mode = DFmode;
6427       else
6428         gcc_unreachable ();
6429
6430       mips_get_arg_info (&info, &cum, mode, NULL, true);
6431       gparg = mips_arg_regno (&info, false);
6432       fparg = mips_arg_regno (&info, true);
6433
6434       if (mode == SFmode)
6435         mips_output_32bit_xfer (direction, gparg, fparg);
6436       else
6437         mips_output_64bit_xfer (direction, gparg, fparg);
6438
6439       mips_function_arg_advance (pack_cumulative_args (&cum), mode, NULL, true);
6440     }
6441 }
6442
6443 /* Write a MIPS16 stub for the current function.  This stub is used
6444    for functions which take arguments in the floating-point registers.
6445    It is normal-mode code that moves the floating-point arguments
6446    into the general registers and then jumps to the MIPS16 code.  */
6447
6448 static void
6449 mips16_build_function_stub (void)
6450 {
6451   const char *fnname, *alias_name, *separator;
6452   char *secname, *stubname;
6453   tree stubdecl;
6454   unsigned int f;
6455   rtx symbol, alias;
6456
6457   /* Create the name of the stub, and its unique section.  */
6458   symbol = XEXP (DECL_RTL (current_function_decl), 0);
6459   alias = mips16_local_alias (symbol);
6460
6461   fnname = targetm.strip_name_encoding (XSTR (symbol, 0));
6462   alias_name = targetm.strip_name_encoding (XSTR (alias, 0));
6463   secname = ACONCAT ((".mips16.fn.", fnname, NULL));
6464   stubname = ACONCAT (("__fn_stub_", fnname, NULL));
6465
6466   /* Build a decl for the stub.  */
6467   stubdecl = build_decl (BUILTINS_LOCATION,
6468                          FUNCTION_DECL, get_identifier (stubname),
6469                          build_function_type_list (void_type_node, NULL_TREE));
6470   DECL_SECTION_NAME (stubdecl) = build_string (strlen (secname), secname);
6471   DECL_RESULT (stubdecl) = build_decl (BUILTINS_LOCATION,
6472                                        RESULT_DECL, NULL_TREE, void_type_node);
6473
6474   /* Output a comment.  */
6475   fprintf (asm_out_file, "\t# Stub function for %s (",
6476            current_function_name ());
6477   separator = "";
6478   for (f = (unsigned int) crtl->args.info.fp_code; f != 0; f >>= 2)
6479     {
6480       fprintf (asm_out_file, "%s%s", separator,
6481                (f & 3) == 1 ? "float" : "double");
6482       separator = ", ";
6483     }
6484   fprintf (asm_out_file, ")\n");
6485
6486   /* Start the function definition.  */
6487   assemble_start_function (stubdecl, stubname);
6488   mips_start_function_definition (stubname, false);
6489
6490   /* If generating pic2 code, either set up the global pointer or
6491      switch to pic0.  */
6492   if (TARGET_ABICALLS_PIC2)
6493     {
6494       if (TARGET_ABSOLUTE_ABICALLS)
6495         fprintf (asm_out_file, "\t.option\tpic0\n");
6496       else
6497         {
6498           output_asm_insn ("%(.cpload\t%^%)", NULL);
6499           /* Emit an R_MIPS_NONE relocation to tell the linker what the
6500              target function is.  Use a local GOT access when loading the
6501              symbol, to cut down on the number of unnecessary GOT entries
6502              for stubs that aren't needed.  */
6503           output_asm_insn (".reloc\t0,R_MIPS_NONE,%0", &symbol);
6504           symbol = alias;
6505         }
6506     }
6507
6508   /* Load the address of the MIPS16 function into $25.  Do this first so
6509      that targets with coprocessor interlocks can use an MFC1 to fill the
6510      delay slot.  */
6511   output_asm_insn ("la\t%^,%0", &symbol);
6512
6513   /* Move the arguments from floating-point registers to general registers.  */
6514   mips_output_args_xfer (crtl->args.info.fp_code, 'f');
6515
6516   /* Jump to the MIPS16 function.  */
6517   output_asm_insn ("jr\t%^", NULL);
6518
6519   if (TARGET_ABICALLS_PIC2 && TARGET_ABSOLUTE_ABICALLS)
6520     fprintf (asm_out_file, "\t.option\tpic2\n");
6521
6522   mips_end_function_definition (stubname);
6523
6524   /* If the linker needs to create a dynamic symbol for the target
6525      function, it will associate the symbol with the stub (which,
6526      unlike the target function, follows the proper calling conventions).
6527      It is therefore useful to have a local alias for the target function,
6528      so that it can still be identified as MIPS16 code.  As an optimization,
6529      this symbol can also be used for indirect MIPS16 references from
6530      within this file.  */
6531   ASM_OUTPUT_DEF (asm_out_file, alias_name, fnname);
6532
6533   switch_to_section (function_section (current_function_decl));
6534 }
6535
6536 /* The current function is a MIPS16 function that returns a value in an FPR.
6537    Copy the return value from its soft-float to its hard-float location.
6538    libgcc2 has special non-MIPS16 helper functions for each case.  */
6539
6540 static void
6541 mips16_copy_fpr_return_value (void)
6542 {
6543   rtx fn, insn, retval;
6544   tree return_type;
6545   enum machine_mode return_mode;
6546   const char *name;
6547
6548   return_type = DECL_RESULT (current_function_decl);
6549   return_mode = DECL_MODE (return_type);
6550
6551   name = ACONCAT (("__mips16_ret_",
6552                    mips16_call_stub_mode_suffix (return_mode),
6553                    NULL));
6554   fn = mips16_stub_function (name);
6555
6556   /* The function takes arguments in $2 (and possibly $3), so calls
6557      to it cannot be lazily bound.  */
6558   SYMBOL_REF_FLAGS (fn) |= SYMBOL_FLAG_BIND_NOW;
6559
6560   /* Model the call as something that takes the GPR return value as
6561      argument and returns an "updated" value.  */
6562   retval = gen_rtx_REG (return_mode, GP_RETURN);
6563   insn = mips_expand_call (MIPS_CALL_EPILOGUE, retval, fn,
6564                            const0_rtx, NULL_RTX, false);
6565   use_reg (&CALL_INSN_FUNCTION_USAGE (insn), retval);
6566 }
6567
6568 /* Consider building a stub for a MIPS16 call to function *FN_PTR.
6569    RETVAL is the location of the return value, or null if this is
6570    a "call" rather than a "call_value".  ARGS_SIZE is the size of the
6571    arguments and FP_CODE is the code built by mips_function_arg;
6572    see the comment before the fp_code field in CUMULATIVE_ARGS for details.
6573
6574    There are three alternatives:
6575
6576    - If a stub was needed, emit the call and return the call insn itself.
6577
6578    - If we can avoid using a stub by redirecting the call, set *FN_PTR
6579      to the new target and return null.
6580
6581    - If *FN_PTR doesn't need a stub, return null and leave *FN_PTR
6582      unmodified.
6583
6584    A stub is needed for calls to functions that, in normal mode,
6585    receive arguments in FPRs or return values in FPRs.  The stub
6586    copies the arguments from their soft-float positions to their
6587    hard-float positions, calls the real function, then copies the
6588    return value from its hard-float position to its soft-float
6589    position.
6590
6591    We can emit a JAL to *FN_PTR even when *FN_PTR might need a stub.
6592    If *FN_PTR turns out to be to a non-MIPS16 function, the linker
6593    automatically redirects the JAL to the stub, otherwise the JAL
6594    continues to call FN directly.  */
6595
6596 static rtx
6597 mips16_build_call_stub (rtx retval, rtx *fn_ptr, rtx args_size, int fp_code)
6598 {
6599   const char *fnname;
6600   bool fp_ret_p;
6601   struct mips16_stub *l;
6602   rtx insn, fn;
6603
6604   /* We don't need to do anything if we aren't in MIPS16 mode, or if
6605      we were invoked with the -msoft-float option.  */
6606   if (!TARGET_MIPS16 || TARGET_SOFT_FLOAT_ABI)
6607     return NULL_RTX;
6608
6609   /* Figure out whether the value might come back in a floating-point
6610      register.  */
6611   fp_ret_p = retval && mips_return_mode_in_fpr_p (GET_MODE (retval));
6612
6613   /* We don't need to do anything if there were no floating-point
6614      arguments and the value will not be returned in a floating-point
6615      register.  */
6616   if (fp_code == 0 && !fp_ret_p)
6617     return NULL_RTX;
6618
6619   /* We don't need to do anything if this is a call to a special
6620      MIPS16 support function.  */
6621   fn = *fn_ptr;
6622   if (mips16_stub_function_p (fn))
6623     return NULL_RTX;
6624
6625   /* If we're calling a locally-defined MIPS16 function, we know that
6626      it will return values in both the "soft-float" and "hard-float"
6627      registers.  There is no need to use a stub to move the latter
6628      to the former.  */
6629   if (fp_code == 0 && mips16_local_function_p (fn))
6630     return NULL_RTX;
6631
6632   /* This code will only work for o32 and o64 abis.  The other ABI's
6633      require more sophisticated support.  */
6634   gcc_assert (TARGET_OLDABI);
6635
6636   /* If we're calling via a function pointer, use one of the magic
6637      libgcc.a stubs provided for each (FP_CODE, FP_RET_P) combination.
6638      Each stub expects the function address to arrive in register $2.  */
6639   if (GET_CODE (fn) != SYMBOL_REF
6640       || !call_insn_operand (fn, VOIDmode))
6641     {
6642       char buf[30];
6643       rtx stub_fn, insn, addr;
6644       bool lazy_p;
6645
6646       /* If this is a locally-defined and locally-binding function,
6647          avoid the stub by calling the local alias directly.  */
6648       if (mips16_local_function_p (fn))
6649         {
6650           *fn_ptr = mips16_local_alias (fn);
6651           return NULL_RTX;
6652         }
6653
6654       /* Create a SYMBOL_REF for the libgcc.a function.  */
6655       if (fp_ret_p)
6656         sprintf (buf, "__mips16_call_stub_%s_%d",
6657                  mips16_call_stub_mode_suffix (GET_MODE (retval)),
6658                  fp_code);
6659       else
6660         sprintf (buf, "__mips16_call_stub_%d", fp_code);
6661       stub_fn = mips16_stub_function (buf);
6662
6663       /* The function uses $2 as an argument, so calls to it
6664          cannot be lazily bound.  */
6665       SYMBOL_REF_FLAGS (stub_fn) |= SYMBOL_FLAG_BIND_NOW;
6666
6667       /* Load the target function into $2.  */
6668       addr = gen_rtx_REG (Pmode, GP_REG_FIRST + 2);
6669       lazy_p = mips_load_call_address (MIPS_CALL_NORMAL, addr, fn);
6670
6671       /* Emit the call.  */
6672       insn = mips_expand_call (MIPS_CALL_NORMAL, retval, stub_fn,
6673                                args_size, NULL_RTX, lazy_p);
6674
6675       /* Tell GCC that this call does indeed use the value of $2.  */
6676       use_reg (&CALL_INSN_FUNCTION_USAGE (insn), addr);
6677
6678       /* If we are handling a floating-point return value, we need to
6679          save $18 in the function prologue.  Putting a note on the
6680          call will mean that df_regs_ever_live_p ($18) will be true if the
6681          call is not eliminated, and we can check that in the prologue
6682          code.  */
6683       if (fp_ret_p)
6684         CALL_INSN_FUNCTION_USAGE (insn) =
6685           gen_rtx_EXPR_LIST (VOIDmode,
6686                              gen_rtx_CLOBBER (VOIDmode,
6687                                               gen_rtx_REG (word_mode, 18)),
6688                              CALL_INSN_FUNCTION_USAGE (insn));
6689
6690       return insn;
6691     }
6692
6693   /* We know the function we are going to call.  If we have already
6694      built a stub, we don't need to do anything further.  */
6695   fnname = targetm.strip_name_encoding (XSTR (fn, 0));
6696   for (l = mips16_stubs; l != NULL; l = l->next)
6697     if (strcmp (l->name, fnname) == 0)
6698       break;
6699
6700   if (l == NULL)
6701     {
6702       const char *separator;
6703       char *secname, *stubname;
6704       tree stubid, stubdecl;
6705       unsigned int f;
6706
6707       /* If the function does not return in FPRs, the special stub
6708          section is named
6709              .mips16.call.FNNAME
6710
6711          If the function does return in FPRs, the stub section is named
6712              .mips16.call.fp.FNNAME
6713
6714          Build a decl for the stub.  */
6715       secname = ACONCAT ((".mips16.call.", fp_ret_p ? "fp." : "",
6716                           fnname, NULL));
6717       stubname = ACONCAT (("__call_stub_", fp_ret_p ? "fp_" : "",
6718                            fnname, NULL));
6719       stubid = get_identifier (stubname);
6720       stubdecl = build_decl (BUILTINS_LOCATION,
6721                              FUNCTION_DECL, stubid,
6722                              build_function_type_list (void_type_node,
6723                                                        NULL_TREE));
6724       DECL_SECTION_NAME (stubdecl) = build_string (strlen (secname), secname);
6725       DECL_RESULT (stubdecl) = build_decl (BUILTINS_LOCATION,
6726                                            RESULT_DECL, NULL_TREE,
6727                                            void_type_node);
6728
6729       /* Output a comment.  */
6730       fprintf (asm_out_file, "\t# Stub function to call %s%s (",
6731                (fp_ret_p
6732                 ? (GET_MODE (retval) == SFmode ? "float " : "double ")
6733                 : ""),
6734                fnname);
6735       separator = "";
6736       for (f = (unsigned int) fp_code; f != 0; f >>= 2)
6737         {
6738           fprintf (asm_out_file, "%s%s", separator,
6739                    (f & 3) == 1 ? "float" : "double");
6740           separator = ", ";
6741         }
6742       fprintf (asm_out_file, ")\n");
6743
6744       /* Start the function definition.  */
6745       assemble_start_function (stubdecl, stubname);
6746       mips_start_function_definition (stubname, false);
6747
6748       if (fp_ret_p)
6749         {
6750           fprintf (asm_out_file, "\t.cfi_startproc\n");
6751
6752           /* Create a fake CFA 4 bytes below the stack pointer.
6753              This works around unwinders (like libgcc's) that expect
6754              the CFA for non-signal frames to be unique.  */
6755           fprintf (asm_out_file, "\t.cfi_def_cfa 29,-4\n");
6756
6757           /* "Save" $sp in itself so we don't use the fake CFA.
6758              This is: DW_CFA_val_expression r29, { DW_OP_reg29 }.  */
6759           fprintf (asm_out_file, "\t.cfi_escape 0x16,29,1,0x6d\n");
6760         }
6761       else
6762         {
6763           /* Load the address of the MIPS16 function into $25.  Do this
6764              first so that targets with coprocessor interlocks can use
6765              an MFC1 to fill the delay slot.  */
6766           if (TARGET_EXPLICIT_RELOCS)
6767             {
6768               output_asm_insn ("lui\t%^,%%hi(%0)", &fn);
6769               output_asm_insn ("addiu\t%^,%^,%%lo(%0)", &fn);
6770             }
6771           else
6772             output_asm_insn ("la\t%^,%0", &fn);
6773         }
6774
6775       /* Move the arguments from general registers to floating-point
6776          registers.  */
6777       mips_output_args_xfer (fp_code, 't');
6778
6779       if (fp_ret_p)
6780         {
6781           /* Save the return address in $18 and call the non-MIPS16 function.
6782              The stub's caller knows that $18 might be clobbered, even though
6783              $18 is usually a call-saved register.  */
6784           fprintf (asm_out_file, "\tmove\t%s,%s\n",
6785                    reg_names[GP_REG_FIRST + 18], reg_names[RETURN_ADDR_REGNUM]);
6786           output_asm_insn (MIPS_CALL ("jal", &fn, 0, -1), &fn);
6787           fprintf (asm_out_file, "\t.cfi_register 31,18\n");
6788
6789           /* Move the result from floating-point registers to
6790              general registers.  */
6791           switch (GET_MODE (retval))
6792             {
6793             case SCmode:
6794               mips_output_32bit_xfer ('f', GP_RETURN + TARGET_BIG_ENDIAN,
6795                                       TARGET_BIG_ENDIAN
6796                                       ? FP_REG_FIRST + MAX_FPRS_PER_FMT
6797                                       : FP_REG_FIRST);
6798               mips_output_32bit_xfer ('f', GP_RETURN + TARGET_LITTLE_ENDIAN,
6799                                       TARGET_LITTLE_ENDIAN
6800                                       ? FP_REG_FIRST + MAX_FPRS_PER_FMT
6801                                       : FP_REG_FIRST);
6802               if (GET_MODE (retval) == SCmode && TARGET_64BIT)
6803                 {
6804                   /* On 64-bit targets, complex floats are returned in
6805                      a single GPR, such that "sd" on a suitably-aligned
6806                      target would store the value correctly.  */
6807                   fprintf (asm_out_file, "\tdsll\t%s,%s,32\n",
6808                            reg_names[GP_RETURN + TARGET_BIG_ENDIAN],
6809                            reg_names[GP_RETURN + TARGET_BIG_ENDIAN]);
6810                   fprintf (asm_out_file, "\tdsll\t%s,%s,32\n",
6811                            reg_names[GP_RETURN + TARGET_LITTLE_ENDIAN],
6812                            reg_names[GP_RETURN + TARGET_LITTLE_ENDIAN]);
6813                   fprintf (asm_out_file, "\tdsrl\t%s,%s,32\n",
6814                            reg_names[GP_RETURN + TARGET_BIG_ENDIAN],
6815                            reg_names[GP_RETURN + TARGET_BIG_ENDIAN]);
6816                   fprintf (asm_out_file, "\tor\t%s,%s,%s\n",
6817                            reg_names[GP_RETURN],
6818                            reg_names[GP_RETURN],
6819                            reg_names[GP_RETURN + 1]);
6820                 }
6821               break;
6822
6823             case SFmode:
6824               mips_output_32bit_xfer ('f', GP_RETURN, FP_REG_FIRST);
6825               break;
6826
6827             case DCmode:
6828               mips_output_64bit_xfer ('f', GP_RETURN + (8 / UNITS_PER_WORD),
6829                                       FP_REG_FIRST + MAX_FPRS_PER_FMT);
6830               /* Fall though.  */
6831             case DFmode:
6832             case V2SFmode:
6833               mips_output_64bit_xfer ('f', GP_RETURN, FP_REG_FIRST);
6834               break;
6835
6836             default:
6837               gcc_unreachable ();
6838             }
6839           fprintf (asm_out_file, "\tjr\t%s\n", reg_names[GP_REG_FIRST + 18]);
6840           fprintf (asm_out_file, "\t.cfi_endproc\n");
6841         }
6842       else
6843         {
6844           /* Jump to the previously-loaded address.  */
6845           output_asm_insn ("jr\t%^", NULL);
6846         }
6847
6848 #ifdef ASM_DECLARE_FUNCTION_SIZE
6849       ASM_DECLARE_FUNCTION_SIZE (asm_out_file, stubname, stubdecl);
6850 #endif
6851
6852       mips_end_function_definition (stubname);
6853
6854       /* Record this stub.  */
6855       l = XNEW (struct mips16_stub);
6856       l->name = xstrdup (fnname);
6857       l->fp_ret_p = fp_ret_p;
6858       l->next = mips16_stubs;
6859       mips16_stubs = l;
6860     }
6861
6862   /* If we expect a floating-point return value, but we've built a
6863      stub which does not expect one, then we're in trouble.  We can't
6864      use the existing stub, because it won't handle the floating-point
6865      value.  We can't build a new stub, because the linker won't know
6866      which stub to use for the various calls in this object file.
6867      Fortunately, this case is illegal, since it means that a function
6868      was declared in two different ways in a single compilation.  */
6869   if (fp_ret_p && !l->fp_ret_p)
6870     error ("cannot handle inconsistent calls to %qs", fnname);
6871
6872   if (retval == NULL_RTX)
6873     insn = gen_call_internal_direct (fn, args_size);
6874   else
6875     insn = gen_call_value_internal_direct (retval, fn, args_size);
6876   insn = mips_emit_call_insn (insn, fn, fn, false);
6877
6878   /* If we are calling a stub which handles a floating-point return
6879      value, we need to arrange to save $18 in the prologue.  We do this
6880      by marking the function call as using the register.  The prologue
6881      will later see that it is used, and emit code to save it.  */
6882   if (fp_ret_p)
6883     CALL_INSN_FUNCTION_USAGE (insn) =
6884       gen_rtx_EXPR_LIST (VOIDmode,
6885                          gen_rtx_CLOBBER (VOIDmode,
6886                                           gen_rtx_REG (word_mode, 18)),
6887                          CALL_INSN_FUNCTION_USAGE (insn));
6888
6889   return insn;
6890 }
6891 \f
6892 /* Expand a call of type TYPE.  RESULT is where the result will go (null
6893    for "call"s and "sibcall"s), ADDR is the address of the function,
6894    ARGS_SIZE is the size of the arguments and AUX is the value passed
6895    to us by mips_function_arg.  LAZY_P is true if this call already
6896    involves a lazily-bound function address (such as when calling
6897    functions through a MIPS16 hard-float stub).
6898
6899    Return the call itself.  */
6900
6901 rtx
6902 mips_expand_call (enum mips_call_type type, rtx result, rtx addr,
6903                   rtx args_size, rtx aux, bool lazy_p)
6904 {
6905   rtx orig_addr, pattern, insn;
6906   int fp_code;
6907
6908   fp_code = aux == 0 ? 0 : (int) GET_MODE (aux);
6909   insn = mips16_build_call_stub (result, &addr, args_size, fp_code);
6910   if (insn)
6911     {
6912       gcc_assert (!lazy_p && type == MIPS_CALL_NORMAL);
6913       return insn;
6914     }
6915
6916   orig_addr = addr;
6917   if (!call_insn_operand (addr, VOIDmode))
6918     {
6919       if (type == MIPS_CALL_EPILOGUE)
6920         addr = MIPS_EPILOGUE_TEMP (Pmode);
6921       else
6922         addr = gen_reg_rtx (Pmode);
6923       lazy_p |= mips_load_call_address (type, addr, orig_addr);
6924     }
6925
6926   if (result == 0)
6927     {
6928       rtx (*fn) (rtx, rtx);
6929
6930       if (type == MIPS_CALL_SIBCALL)
6931         fn = gen_sibcall_internal;
6932       else
6933         fn = gen_call_internal;
6934
6935       pattern = fn (addr, args_size);
6936     }
6937   else if (GET_CODE (result) == PARALLEL && XVECLEN (result, 0) == 2)
6938     {
6939       /* Handle return values created by mips_return_fpr_pair.  */
6940       rtx (*fn) (rtx, rtx, rtx, rtx);
6941       rtx reg1, reg2;
6942
6943       if (type == MIPS_CALL_SIBCALL)
6944         fn = gen_sibcall_value_multiple_internal;
6945       else
6946         fn = gen_call_value_multiple_internal;
6947
6948       reg1 = XEXP (XVECEXP (result, 0, 0), 0);
6949       reg2 = XEXP (XVECEXP (result, 0, 1), 0);
6950       pattern = fn (reg1, addr, args_size, reg2);
6951     }
6952   else
6953     {
6954       rtx (*fn) (rtx, rtx, rtx);
6955
6956       if (type == MIPS_CALL_SIBCALL)
6957         fn = gen_sibcall_value_internal;
6958       else
6959         fn = gen_call_value_internal;
6960
6961       /* Handle return values created by mips_return_fpr_single.  */
6962       if (GET_CODE (result) == PARALLEL && XVECLEN (result, 0) == 1)
6963         result = XEXP (XVECEXP (result, 0, 0), 0);
6964       pattern = fn (result, addr, args_size);
6965     }
6966
6967   return mips_emit_call_insn (pattern, orig_addr, addr, lazy_p);
6968 }
6969
6970 /* Split call instruction INSN into a $gp-clobbering call and
6971    (where necessary) an instruction to restore $gp from its save slot.
6972    CALL_PATTERN is the pattern of the new call.  */
6973
6974 void
6975 mips_split_call (rtx insn, rtx call_pattern)
6976 {
6977   emit_call_insn (call_pattern);
6978   if (!find_reg_note (insn, REG_NORETURN, 0))
6979     /* Pick a temporary register that is suitable for both MIPS16 and
6980        non-MIPS16 code.  $4 and $5 are used for returning complex double
6981        values in soft-float code, so $6 is the first suitable candidate.  */
6982     mips_restore_gp_from_cprestore_slot (gen_rtx_REG (Pmode, GP_ARG_FIRST + 2));
6983 }
6984
6985 /* Return true if a call to DECL may need to use JALX.  */
6986
6987 static bool
6988 mips_call_may_need_jalx_p (tree decl)
6989 {
6990   /* If the current translation unit would use a different mode for DECL,
6991      assume that the call needs JALX.  */
6992   if (mips_get_compress_mode (decl) != TARGET_COMPRESSION)
6993     return true;
6994
6995   /* mips_get_compress_mode is always accurate for locally-binding
6996      functions in the current translation unit.  */
6997   if (!DECL_EXTERNAL (decl) && targetm.binds_local_p (decl))
6998     return false;
6999
7000   /* When -minterlink-compressed is in effect, assume that functions
7001      could use a different encoding mode unless an attribute explicitly
7002      tells us otherwise.  */
7003   if (TARGET_INTERLINK_COMPRESSED)
7004     {
7005       if (!TARGET_COMPRESSION
7006           && mips_get_compress_off_flags (DECL_ATTRIBUTES (decl)) ==0)
7007         return true;
7008       if (TARGET_COMPRESSION
7009           && mips_get_compress_on_flags (DECL_ATTRIBUTES (decl)) == 0)
7010         return true;
7011     }
7012
7013   return false;
7014 }
7015
7016 /* Implement TARGET_FUNCTION_OK_FOR_SIBCALL.  */
7017
7018 static bool
7019 mips_function_ok_for_sibcall (tree decl, tree exp ATTRIBUTE_UNUSED)
7020 {
7021   if (!TARGET_SIBCALLS)
7022     return false;
7023
7024   /* Interrupt handlers need special epilogue code and therefore can't
7025      use sibcalls.  */
7026   if (mips_interrupt_type_p (TREE_TYPE (current_function_decl)))
7027     return false;
7028
7029   /* Direct Js are only possible to functions that use the same ISA encoding.
7030      There is no JX counterpoart of JALX.  */
7031   if (decl
7032       && const_call_insn_operand (XEXP (DECL_RTL (decl), 0), VOIDmode)
7033       && mips_call_may_need_jalx_p (decl))
7034     return false;
7035
7036   /* Sibling calls should not prevent lazy binding.  Lazy-binding stubs
7037      require $gp to be valid on entry, so sibcalls can only use stubs
7038      if $gp is call-clobbered.  */
7039   if (decl
7040       && TARGET_CALL_SAVED_GP
7041       && !TARGET_ABICALLS_PIC0
7042       && !targetm.binds_local_p (decl))
7043     return false;
7044
7045   /* Otherwise OK.  */
7046   return true;
7047 }
7048 \f
7049 /* Emit code to move general operand SRC into condition-code
7050    register DEST given that SCRATCH is a scratch TFmode FPR.
7051    The sequence is:
7052
7053         FP1 = SRC
7054         FP2 = 0.0f
7055         DEST = FP2 < FP1
7056
7057    where FP1 and FP2 are single-precision FPRs taken from SCRATCH.  */
7058
7059 void
7060 mips_expand_fcc_reload (rtx dest, rtx src, rtx scratch)
7061 {
7062   rtx fp1, fp2;
7063
7064   /* Change the source to SFmode.  */
7065   if (MEM_P (src))
7066     src = adjust_address (src, SFmode, 0);
7067   else if (REG_P (src) || GET_CODE (src) == SUBREG)
7068     src = gen_rtx_REG (SFmode, true_regnum (src));
7069
7070   fp1 = gen_rtx_REG (SFmode, REGNO (scratch));
7071   fp2 = gen_rtx_REG (SFmode, REGNO (scratch) + MAX_FPRS_PER_FMT);
7072
7073   mips_emit_move (copy_rtx (fp1), src);
7074   mips_emit_move (copy_rtx (fp2), CONST0_RTX (SFmode));
7075   emit_insn (gen_slt_sf (dest, fp2, fp1));
7076 }
7077 \f
7078 /* Implement MOVE_BY_PIECES_P.  */
7079
7080 bool
7081 mips_move_by_pieces_p (unsigned HOST_WIDE_INT size, unsigned int align)
7082 {
7083   if (HAVE_movmemsi)
7084     {
7085       /* movmemsi is meant to generate code that is at least as good as
7086          move_by_pieces.  However, movmemsi effectively uses a by-pieces
7087          implementation both for moves smaller than a word and for
7088          word-aligned moves of no more than MIPS_MAX_MOVE_BYTES_STRAIGHT
7089          bytes.  We should allow the tree-level optimisers to do such
7090          moves by pieces, as it often exposes other optimization
7091          opportunities.  We might as well continue to use movmemsi at
7092          the rtl level though, as it produces better code when
7093          scheduling is disabled (such as at -O).  */
7094       if (currently_expanding_to_rtl)
7095         return false;
7096       if (align < BITS_PER_WORD)
7097         return size < UNITS_PER_WORD;
7098       return size <= MIPS_MAX_MOVE_BYTES_STRAIGHT;
7099     }
7100   /* The default value.  If this becomes a target hook, we should
7101      call the default definition instead.  */
7102   return (move_by_pieces_ninsns (size, align, MOVE_MAX_PIECES + 1)
7103           < (unsigned int) MOVE_RATIO (optimize_insn_for_speed_p ()));
7104 }
7105
7106 /* Implement STORE_BY_PIECES_P.  */
7107
7108 bool
7109 mips_store_by_pieces_p (unsigned HOST_WIDE_INT size, unsigned int align)
7110 {
7111   /* Storing by pieces involves moving constants into registers
7112      of size MIN (ALIGN, BITS_PER_WORD), then storing them.
7113      We need to decide whether it is cheaper to load the address of
7114      constant data into a register and use a block move instead.  */
7115
7116   /* If the data is only byte aligned, then:
7117
7118      (a1) A block move of less than 4 bytes would involve three 3 LBs and
7119           3 SBs.  We might as well use 3 single-instruction LIs and 3 SBs
7120           instead.
7121
7122      (a2) A block move of 4 bytes from aligned source data can use an
7123           LW/SWL/SWR sequence.  This is often better than the 4 LIs and
7124           4 SBs that we would generate when storing by pieces.  */
7125   if (align <= BITS_PER_UNIT)
7126     return size < 4;
7127
7128   /* If the data is 2-byte aligned, then:
7129
7130      (b1) A block move of less than 4 bytes would use a combination of LBs,
7131           LHs, SBs and SHs.  We get better code by using single-instruction
7132           LIs, SBs and SHs instead.
7133
7134      (b2) A block move of 4 bytes from aligned source data would again use
7135           an LW/SWL/SWR sequence.  In most cases, loading the address of
7136           the source data would require at least one extra instruction.
7137           It is often more efficient to use 2 single-instruction LIs and
7138           2 SHs instead.
7139
7140      (b3) A block move of up to 3 additional bytes would be like (b1).
7141
7142      (b4) A block move of 8 bytes from aligned source data can use two
7143           LW/SWL/SWR sequences or a single LD/SDL/SDR sequence.  Both
7144           sequences are better than the 4 LIs and 4 SHs that we'd generate
7145           when storing by pieces.
7146
7147      The reasoning for higher alignments is similar:
7148
7149      (c1) A block move of less than 4 bytes would be the same as (b1).
7150
7151      (c2) A block move of 4 bytes would use an LW/SW sequence.  Again,
7152           loading the address of the source data would typically require
7153           at least one extra instruction.  It is generally better to use
7154           LUI/ORI/SW instead.
7155
7156      (c3) A block move of up to 3 additional bytes would be like (b1).
7157
7158      (c4) A block move of 8 bytes can use two LW/SW sequences or a single
7159           LD/SD sequence, and in these cases we've traditionally preferred
7160           the memory copy over the more bulky constant moves.  */
7161   return size < 8;
7162 }
7163
7164 /* Emit straight-line code to move LENGTH bytes from SRC to DEST.
7165    Assume that the areas do not overlap.  */
7166
7167 static void
7168 mips_block_move_straight (rtx dest, rtx src, HOST_WIDE_INT length)
7169 {
7170   HOST_WIDE_INT offset, delta;
7171   unsigned HOST_WIDE_INT bits;
7172   int i;
7173   enum machine_mode mode;
7174   rtx *regs;
7175
7176   /* Work out how many bits to move at a time.  If both operands have
7177      half-word alignment, it is usually better to move in half words.
7178      For instance, lh/lh/sh/sh is usually better than lwl/lwr/swl/swr
7179      and lw/lw/sw/sw is usually better than ldl/ldr/sdl/sdr.
7180      Otherwise move word-sized chunks.  */
7181   if (MEM_ALIGN (src) == BITS_PER_WORD / 2
7182       && MEM_ALIGN (dest) == BITS_PER_WORD / 2)
7183     bits = BITS_PER_WORD / 2;
7184   else
7185     bits = BITS_PER_WORD;
7186
7187   mode = mode_for_size (bits, MODE_INT, 0);
7188   delta = bits / BITS_PER_UNIT;
7189
7190   /* Allocate a buffer for the temporary registers.  */
7191   regs = XALLOCAVEC (rtx, length / delta);
7192
7193   /* Load as many BITS-sized chunks as possible.  Use a normal load if
7194      the source has enough alignment, otherwise use left/right pairs.  */
7195   for (offset = 0, i = 0; offset + delta <= length; offset += delta, i++)
7196     {
7197       regs[i] = gen_reg_rtx (mode);
7198       if (MEM_ALIGN (src) >= bits)
7199         mips_emit_move (regs[i], adjust_address (src, mode, offset));
7200       else
7201         {
7202           rtx part = adjust_address (src, BLKmode, offset);
7203           set_mem_size (part, delta);
7204           if (!mips_expand_ext_as_unaligned_load (regs[i], part, bits, 0, 0))
7205             gcc_unreachable ();
7206         }
7207     }
7208
7209   /* Copy the chunks to the destination.  */
7210   for (offset = 0, i = 0; offset + delta <= length; offset += delta, i++)
7211     if (MEM_ALIGN (dest) >= bits)
7212       mips_emit_move (adjust_address (dest, mode, offset), regs[i]);
7213     else
7214       {
7215         rtx part = adjust_address (dest, BLKmode, offset);
7216         set_mem_size (part, delta);
7217         if (!mips_expand_ins_as_unaligned_store (part, regs[i], bits, 0))
7218           gcc_unreachable ();
7219       }
7220
7221   /* Mop up any left-over bytes.  */
7222   if (offset < length)
7223     {
7224       src = adjust_address (src, BLKmode, offset);
7225       dest = adjust_address (dest, BLKmode, offset);
7226       move_by_pieces (dest, src, length - offset,
7227                       MIN (MEM_ALIGN (src), MEM_ALIGN (dest)), 0);
7228     }
7229 }
7230
7231 /* Helper function for doing a loop-based block operation on memory
7232    reference MEM.  Each iteration of the loop will operate on LENGTH
7233    bytes of MEM.
7234
7235    Create a new base register for use within the loop and point it to
7236    the start of MEM.  Create a new memory reference that uses this
7237    register.  Store them in *LOOP_REG and *LOOP_MEM respectively.  */
7238
7239 static void
7240 mips_adjust_block_mem (rtx mem, HOST_WIDE_INT length,
7241                        rtx *loop_reg, rtx *loop_mem)
7242 {
7243   *loop_reg = copy_addr_to_reg (XEXP (mem, 0));
7244
7245   /* Although the new mem does not refer to a known location,
7246      it does keep up to LENGTH bytes of alignment.  */
7247   *loop_mem = change_address (mem, BLKmode, *loop_reg);
7248   set_mem_align (*loop_mem, MIN (MEM_ALIGN (mem), length * BITS_PER_UNIT));
7249 }
7250
7251 /* Move LENGTH bytes from SRC to DEST using a loop that moves BYTES_PER_ITER
7252    bytes at a time.  LENGTH must be at least BYTES_PER_ITER.  Assume that
7253    the memory regions do not overlap.  */
7254
7255 static void
7256 mips_block_move_loop (rtx dest, rtx src, HOST_WIDE_INT length,
7257                       HOST_WIDE_INT bytes_per_iter)
7258 {
7259   rtx label, src_reg, dest_reg, final_src, test;
7260   HOST_WIDE_INT leftover;
7261
7262   leftover = length % bytes_per_iter;
7263   length -= leftover;
7264
7265   /* Create registers and memory references for use within the loop.  */
7266   mips_adjust_block_mem (src, bytes_per_iter, &src_reg, &src);
7267   mips_adjust_block_mem (dest, bytes_per_iter, &dest_reg, &dest);
7268
7269   /* Calculate the value that SRC_REG should have after the last iteration
7270      of the loop.  */
7271   final_src = expand_simple_binop (Pmode, PLUS, src_reg, GEN_INT (length),
7272                                    0, 0, OPTAB_WIDEN);
7273
7274   /* Emit the start of the loop.  */
7275   label = gen_label_rtx ();
7276   emit_label (label);
7277
7278   /* Emit the loop body.  */
7279   mips_block_move_straight (dest, src, bytes_per_iter);
7280
7281   /* Move on to the next block.  */
7282   mips_emit_move (src_reg, plus_constant (Pmode, src_reg, bytes_per_iter));
7283   mips_emit_move (dest_reg, plus_constant (Pmode, dest_reg, bytes_per_iter));
7284
7285   /* Emit the loop condition.  */
7286   test = gen_rtx_NE (VOIDmode, src_reg, final_src);
7287   if (Pmode == DImode)
7288     emit_jump_insn (gen_cbranchdi4 (test, src_reg, final_src, label));
7289   else
7290     emit_jump_insn (gen_cbranchsi4 (test, src_reg, final_src, label));
7291
7292   /* Mop up any left-over bytes.  */
7293   if (leftover)
7294     mips_block_move_straight (dest, src, leftover);
7295 }
7296
7297 /* Expand a movmemsi instruction, which copies LENGTH bytes from
7298    memory reference SRC to memory reference DEST.  */
7299
7300 bool
7301 mips_expand_block_move (rtx dest, rtx src, rtx length)
7302 {
7303   if (CONST_INT_P (length))
7304     {
7305       if (INTVAL (length) <= MIPS_MAX_MOVE_BYTES_STRAIGHT)
7306         {
7307           mips_block_move_straight (dest, src, INTVAL (length));
7308           return true;
7309         }
7310       else if (optimize)
7311         {
7312           mips_block_move_loop (dest, src, INTVAL (length),
7313                                 MIPS_MAX_MOVE_BYTES_PER_LOOP_ITER);
7314           return true;
7315         }
7316     }
7317   return false;
7318 }
7319 \f
7320 /* Expand a loop of synci insns for the address range [BEGIN, END).  */
7321
7322 void
7323 mips_expand_synci_loop (rtx begin, rtx end)
7324 {
7325   rtx inc, label, end_label, cmp_result, mask, length;
7326
7327   /* Create end_label.  */
7328   end_label = gen_label_rtx ();
7329
7330   /* Check if begin equals end.  */
7331   cmp_result = gen_rtx_EQ (VOIDmode, begin, end);
7332   emit_jump_insn (gen_condjump (cmp_result, end_label));
7333
7334   /* Load INC with the cache line size (rdhwr INC,$1).  */
7335   inc = gen_reg_rtx (Pmode);
7336   emit_insn (PMODE_INSN (gen_rdhwr_synci_step, (inc)));
7337
7338   /* Check if inc is 0.  */
7339   cmp_result = gen_rtx_EQ (VOIDmode, inc, const0_rtx);
7340   emit_jump_insn (gen_condjump (cmp_result, end_label));
7341
7342   /* Calculate mask.  */
7343   mask = mips_force_unary (Pmode, NEG, inc);
7344
7345   /* Mask out begin by mask.  */
7346   begin = mips_force_binary (Pmode, AND, begin, mask);
7347
7348   /* Calculate length.  */
7349   length = mips_force_binary (Pmode, MINUS, end, begin);
7350
7351   /* Loop back to here.  */
7352   label = gen_label_rtx ();
7353   emit_label (label);
7354
7355   emit_insn (gen_synci (begin));
7356
7357   /* Update length.  */
7358   mips_emit_binary (MINUS, length, length, inc);
7359
7360   /* Update begin.  */
7361   mips_emit_binary (PLUS, begin, begin, inc);
7362
7363   /* Check if length is greater than 0.  */
7364   cmp_result = gen_rtx_GT (VOIDmode, length, const0_rtx);
7365   emit_jump_insn (gen_condjump (cmp_result, label));
7366
7367   emit_label (end_label);
7368 }
7369 \f
7370 /* Expand a QI or HI mode atomic memory operation.
7371
7372    GENERATOR contains a pointer to the gen_* function that generates
7373    the SI mode underlying atomic operation using masks that we
7374    calculate.
7375
7376    RESULT is the return register for the operation.  Its value is NULL
7377    if unused.
7378
7379    MEM is the location of the atomic access.
7380
7381    OLDVAL is the first operand for the operation.
7382
7383    NEWVAL is the optional second operand for the operation.  Its value
7384    is NULL if unused.  */
7385
7386 void
7387 mips_expand_atomic_qihi (union mips_gen_fn_ptrs generator,
7388                          rtx result, rtx mem, rtx oldval, rtx newval)
7389 {
7390   rtx orig_addr, memsi_addr, memsi, shift, shiftsi, unshifted_mask;
7391   rtx unshifted_mask_reg, mask, inverted_mask, si_op;
7392   rtx res = NULL;
7393   enum machine_mode mode;
7394
7395   mode = GET_MODE (mem);
7396
7397   /* Compute the address of the containing SImode value.  */
7398   orig_addr = force_reg (Pmode, XEXP (mem, 0));
7399   memsi_addr = mips_force_binary (Pmode, AND, orig_addr,
7400                                   force_reg (Pmode, GEN_INT (-4)));
7401
7402   /* Create a memory reference for it.  */
7403   memsi = gen_rtx_MEM (SImode, memsi_addr);
7404   set_mem_alias_set (memsi, ALIAS_SET_MEMORY_BARRIER);
7405   MEM_VOLATILE_P (memsi) = MEM_VOLATILE_P (mem);
7406
7407   /* Work out the byte offset of the QImode or HImode value,
7408      counting from the least significant byte.  */
7409   shift = mips_force_binary (Pmode, AND, orig_addr, GEN_INT (3));
7410   if (TARGET_BIG_ENDIAN)
7411     mips_emit_binary (XOR, shift, shift, GEN_INT (mode == QImode ? 3 : 2));
7412
7413   /* Multiply by eight to convert the shift value from bytes to bits.  */
7414   mips_emit_binary (ASHIFT, shift, shift, GEN_INT (3));
7415
7416   /* Make the final shift an SImode value, so that it can be used in
7417      SImode operations.  */
7418   shiftsi = force_reg (SImode, gen_lowpart (SImode, shift));
7419
7420   /* Set MASK to an inclusive mask of the QImode or HImode value.  */
7421   unshifted_mask = GEN_INT (GET_MODE_MASK (mode));
7422   unshifted_mask_reg = force_reg (SImode, unshifted_mask);
7423   mask = mips_force_binary (SImode, ASHIFT, unshifted_mask_reg, shiftsi);
7424
7425   /* Compute the equivalent exclusive mask.  */
7426   inverted_mask = gen_reg_rtx (SImode);
7427   emit_insn (gen_rtx_SET (VOIDmode, inverted_mask,
7428                           gen_rtx_NOT (SImode, mask)));
7429
7430   /* Shift the old value into place.  */
7431   if (oldval != const0_rtx)
7432     {
7433       oldval = convert_modes (SImode, mode, oldval, true);
7434       oldval = force_reg (SImode, oldval);
7435       oldval = mips_force_binary (SImode, ASHIFT, oldval, shiftsi);
7436     }
7437
7438   /* Do the same for the new value.  */
7439   if (newval && newval != const0_rtx)
7440     {
7441       newval = convert_modes (SImode, mode, newval, true);
7442       newval = force_reg (SImode, newval);
7443       newval = mips_force_binary (SImode, ASHIFT, newval, shiftsi);
7444     }
7445
7446   /* Do the SImode atomic access.  */
7447   if (result)
7448     res = gen_reg_rtx (SImode);
7449   if (newval)
7450     si_op = generator.fn_6 (res, memsi, mask, inverted_mask, oldval, newval);
7451   else if (result)
7452     si_op = generator.fn_5 (res, memsi, mask, inverted_mask, oldval);
7453   else
7454     si_op = generator.fn_4 (memsi, mask, inverted_mask, oldval);
7455
7456   emit_insn (si_op);
7457
7458   if (result)
7459     {
7460       /* Shift and convert the result.  */
7461       mips_emit_binary (AND, res, res, mask);
7462       mips_emit_binary (LSHIFTRT, res, res, shiftsi);
7463       mips_emit_move (result, gen_lowpart (GET_MODE (result), res));
7464     }
7465 }
7466
7467 /* Return true if it is possible to use left/right accesses for a
7468    bitfield of WIDTH bits starting BITPOS bits into BLKmode memory OP.
7469    When returning true, update *LEFT and *RIGHT as follows:
7470
7471    *LEFT is a QImode reference to the first byte if big endian or
7472    the last byte if little endian.  This address can be used in the
7473    left-side instructions (LWL, SWL, LDL, SDL).
7474
7475    *RIGHT is a QImode reference to the opposite end of the field and
7476    can be used in the patterning right-side instruction.  */
7477
7478 static bool
7479 mips_get_unaligned_mem (rtx op, HOST_WIDE_INT width, HOST_WIDE_INT bitpos,
7480                         rtx *left, rtx *right)
7481 {
7482   rtx first, last;
7483
7484   /* Check that the size is valid.  */
7485   if (width != 32 && (!TARGET_64BIT || width != 64))
7486     return false;
7487
7488   /* We can only access byte-aligned values.  Since we are always passed
7489      a reference to the first byte of the field, it is not necessary to
7490      do anything with BITPOS after this check.  */
7491   if (bitpos % BITS_PER_UNIT != 0)
7492     return false;
7493
7494   /* Reject aligned bitfields: we want to use a normal load or store
7495      instead of a left/right pair.  */
7496   if (MEM_ALIGN (op) >= width)
7497     return false;
7498
7499   /* Get references to both ends of the field.  */
7500   first = adjust_address (op, QImode, 0);
7501   last = adjust_address (op, QImode, width / BITS_PER_UNIT - 1);
7502
7503   /* Allocate to LEFT and RIGHT according to endianness.  LEFT should
7504      correspond to the MSB and RIGHT to the LSB.  */
7505   if (TARGET_BIG_ENDIAN)
7506     *left = first, *right = last;
7507   else
7508     *left = last, *right = first;
7509
7510   return true;
7511 }
7512
7513 /* Try to use left/right loads to expand an "extv" or "extzv" pattern.
7514    DEST, SRC, WIDTH and BITPOS are the operands passed to the expander;
7515    the operation is the equivalent of:
7516
7517       (set DEST (*_extract SRC WIDTH BITPOS))
7518
7519    Return true on success.  */
7520
7521 bool
7522 mips_expand_ext_as_unaligned_load (rtx dest, rtx src, HOST_WIDE_INT width,
7523                                    HOST_WIDE_INT bitpos, bool unsigned_p)
7524 {
7525   rtx left, right, temp;
7526   rtx dest1 = NULL_RTX;
7527
7528   /* If TARGET_64BIT, the destination of a 32-bit "extz" or "extzv" will
7529      be a DImode, create a new temp and emit a zero extend at the end.  */
7530   if (GET_MODE (dest) == DImode
7531       && REG_P (dest)
7532       && GET_MODE_BITSIZE (SImode) == width)
7533     {
7534       dest1 = dest;
7535       dest = gen_reg_rtx (SImode);
7536     }
7537
7538   if (!mips_get_unaligned_mem (src, width, bitpos, &left, &right))
7539     return false;
7540
7541   temp = gen_reg_rtx (GET_MODE (dest));
7542   if (GET_MODE (dest) == DImode)
7543     {
7544       emit_insn (gen_mov_ldl (temp, src, left));
7545       emit_insn (gen_mov_ldr (dest, copy_rtx (src), right, temp));
7546     }
7547   else
7548     {
7549       emit_insn (gen_mov_lwl (temp, src, left));
7550       emit_insn (gen_mov_lwr (dest, copy_rtx (src), right, temp));
7551     }
7552
7553   /* If we were loading 32bits and the original register was DI then
7554      sign/zero extend into the orignal dest.  */
7555   if (dest1)
7556     {
7557       if (unsigned_p)
7558         emit_insn (gen_zero_extendsidi2 (dest1, dest));
7559       else
7560         emit_insn (gen_extendsidi2 (dest1, dest));
7561     }
7562   return true;
7563 }
7564
7565 /* Try to use left/right stores to expand an "ins" pattern.  DEST, WIDTH,
7566    BITPOS and SRC are the operands passed to the expander; the operation
7567    is the equivalent of:
7568
7569        (set (zero_extract DEST WIDTH BITPOS) SRC)
7570
7571    Return true on success.  */
7572
7573 bool
7574 mips_expand_ins_as_unaligned_store (rtx dest, rtx src, HOST_WIDE_INT width,
7575                                     HOST_WIDE_INT bitpos)
7576 {
7577   rtx left, right;
7578   enum machine_mode mode;
7579
7580   if (!mips_get_unaligned_mem (dest, width, bitpos, &left, &right))
7581     return false;
7582
7583   mode = mode_for_size (width, MODE_INT, 0);
7584   src = gen_lowpart (mode, src);
7585   if (mode == DImode)
7586     {
7587       emit_insn (gen_mov_sdl (dest, src, left));
7588       emit_insn (gen_mov_sdr (copy_rtx (dest), copy_rtx (src), right));
7589     }
7590   else
7591     {
7592       emit_insn (gen_mov_swl (dest, src, left));
7593       emit_insn (gen_mov_swr (copy_rtx (dest), copy_rtx (src), right));
7594     }
7595   return true;
7596 }
7597
7598 /* Return true if X is a MEM with the same size as MODE.  */
7599
7600 bool
7601 mips_mem_fits_mode_p (enum machine_mode mode, rtx x)
7602 {
7603   return (MEM_P (x)
7604           && MEM_SIZE_KNOWN_P (x)
7605           && MEM_SIZE (x) == GET_MODE_SIZE (mode));
7606 }
7607
7608 /* Return true if (zero_extract OP WIDTH BITPOS) can be used as the
7609    source of an "ext" instruction or the destination of an "ins"
7610    instruction.  OP must be a register operand and the following
7611    conditions must hold:
7612
7613      0 <= BITPOS < GET_MODE_BITSIZE (GET_MODE (op))
7614      0 < WIDTH <= GET_MODE_BITSIZE (GET_MODE (op))
7615      0 < BITPOS + WIDTH <= GET_MODE_BITSIZE (GET_MODE (op))
7616
7617    Also reject lengths equal to a word as they are better handled
7618    by the move patterns.  */
7619
7620 bool
7621 mips_use_ins_ext_p (rtx op, HOST_WIDE_INT width, HOST_WIDE_INT bitpos)
7622 {
7623   if (!ISA_HAS_EXT_INS
7624       || !register_operand (op, VOIDmode)
7625       || GET_MODE_BITSIZE (GET_MODE (op)) > BITS_PER_WORD)
7626     return false;
7627
7628   if (!IN_RANGE (width, 1, GET_MODE_BITSIZE (GET_MODE (op)) - 1))
7629     return false;
7630
7631   if (bitpos < 0 || bitpos + width > GET_MODE_BITSIZE (GET_MODE (op)))
7632     return false;
7633
7634   return true;
7635 }
7636
7637 /* Check if MASK and SHIFT are valid in mask-low-and-shift-left
7638    operation if MAXLEN is the maxium length of consecutive bits that
7639    can make up MASK.  MODE is the mode of the operation.  See
7640    mask_low_and_shift_len for the actual definition.  */
7641
7642 bool
7643 mask_low_and_shift_p (enum machine_mode mode, rtx mask, rtx shift, int maxlen)
7644 {
7645   return IN_RANGE (mask_low_and_shift_len (mode, mask, shift), 1, maxlen);
7646 }
7647
7648 /* Return true iff OP1 and OP2 are valid operands together for the
7649    *and<MODE>3 and *and<MODE>3_mips16 patterns.  For the cases to consider,
7650    see the table in the comment before the pattern.  */
7651
7652 bool
7653 and_operands_ok (enum machine_mode mode, rtx op1, rtx op2)
7654 {
7655   return (memory_operand (op1, mode)
7656           ? and_load_operand (op2, mode)
7657           : and_reg_operand (op2, mode));
7658 }
7659
7660 /* The canonical form of a mask-low-and-shift-left operation is
7661    (and (ashift X SHIFT) MASK) where MASK has the lower SHIFT number of bits
7662    cleared.  Thus we need to shift MASK to the right before checking if it
7663    is a valid mask value.  MODE is the mode of the operation.  If true
7664    return the length of the mask, otherwise return -1.  */
7665
7666 int
7667 mask_low_and_shift_len (enum machine_mode mode, rtx mask, rtx shift)
7668 {
7669   HOST_WIDE_INT shval;
7670
7671   shval = INTVAL (shift) & (GET_MODE_BITSIZE (mode) - 1);
7672   return exact_log2 ((UINTVAL (mask) >> shval) + 1);
7673 }
7674 \f
7675 /* Return true if -msplit-addresses is selected and should be honored.
7676
7677    -msplit-addresses is a half-way house between explicit relocations
7678    and the traditional assembler macros.  It can split absolute 32-bit
7679    symbolic constants into a high/lo_sum pair but uses macros for other
7680    sorts of access.
7681
7682    Like explicit relocation support for REL targets, it relies
7683    on GNU extensions in the assembler and the linker.
7684
7685    Although this code should work for -O0, it has traditionally
7686    been treated as an optimization.  */
7687
7688 static bool
7689 mips_split_addresses_p (void)
7690 {
7691   return (TARGET_SPLIT_ADDRESSES
7692           && optimize
7693           && !TARGET_MIPS16
7694           && !flag_pic
7695           && !ABI_HAS_64BIT_SYMBOLS);
7696 }
7697
7698 /* (Re-)Initialize mips_split_p, mips_lo_relocs and mips_hi_relocs.  */
7699
7700 static void
7701 mips_init_relocs (void)
7702 {
7703   memset (mips_split_p, '\0', sizeof (mips_split_p));
7704   memset (mips_split_hi_p, '\0', sizeof (mips_split_hi_p));
7705   memset (mips_use_pcrel_pool_p, '\0', sizeof (mips_use_pcrel_pool_p));
7706   memset (mips_hi_relocs, '\0', sizeof (mips_hi_relocs));
7707   memset (mips_lo_relocs, '\0', sizeof (mips_lo_relocs));
7708
7709   if (TARGET_MIPS16_PCREL_LOADS)
7710     mips_use_pcrel_pool_p[SYMBOL_ABSOLUTE] = true;
7711   else
7712     {
7713       if (ABI_HAS_64BIT_SYMBOLS)
7714         {
7715           if (TARGET_EXPLICIT_RELOCS)
7716             {
7717               mips_split_p[SYMBOL_64_HIGH] = true;
7718               mips_hi_relocs[SYMBOL_64_HIGH] = "%highest(";
7719               mips_lo_relocs[SYMBOL_64_HIGH] = "%higher(";
7720
7721               mips_split_p[SYMBOL_64_MID] = true;
7722               mips_hi_relocs[SYMBOL_64_MID] = "%higher(";
7723               mips_lo_relocs[SYMBOL_64_MID] = "%hi(";
7724
7725               mips_split_p[SYMBOL_64_LOW] = true;
7726               mips_hi_relocs[SYMBOL_64_LOW] = "%hi(";
7727               mips_lo_relocs[SYMBOL_64_LOW] = "%lo(";
7728
7729               mips_split_p[SYMBOL_ABSOLUTE] = true;
7730               mips_lo_relocs[SYMBOL_ABSOLUTE] = "%lo(";
7731             }
7732         }
7733       else
7734         {
7735           if (TARGET_EXPLICIT_RELOCS
7736               || mips_split_addresses_p ()
7737               || TARGET_MIPS16)
7738             {
7739               mips_split_p[SYMBOL_ABSOLUTE] = true;
7740               mips_hi_relocs[SYMBOL_ABSOLUTE] = "%hi(";
7741               mips_lo_relocs[SYMBOL_ABSOLUTE] = "%lo(";
7742             }
7743         }
7744     }
7745
7746   if (TARGET_MIPS16)
7747     {
7748       /* The high part is provided by a pseudo copy of $gp.  */
7749       mips_split_p[SYMBOL_GP_RELATIVE] = true;
7750       mips_lo_relocs[SYMBOL_GP_RELATIVE] = "%gprel(";
7751     }
7752   else if (TARGET_EXPLICIT_RELOCS)
7753     /* Small data constants are kept whole until after reload,
7754        then lowered by mips_rewrite_small_data.  */
7755     mips_lo_relocs[SYMBOL_GP_RELATIVE] = "%gp_rel(";
7756
7757   if (TARGET_EXPLICIT_RELOCS)
7758     {
7759       mips_split_p[SYMBOL_GOT_PAGE_OFST] = true;
7760       if (TARGET_NEWABI)
7761         {
7762           mips_lo_relocs[SYMBOL_GOTOFF_PAGE] = "%got_page(";
7763           mips_lo_relocs[SYMBOL_GOT_PAGE_OFST] = "%got_ofst(";
7764         }
7765       else
7766         {
7767           mips_lo_relocs[SYMBOL_GOTOFF_PAGE] = "%got(";
7768           mips_lo_relocs[SYMBOL_GOT_PAGE_OFST] = "%lo(";
7769         }
7770       if (TARGET_MIPS16)
7771         /* Expose the use of $28 as soon as possible.  */
7772         mips_split_hi_p[SYMBOL_GOT_PAGE_OFST] = true;
7773
7774       if (TARGET_XGOT)
7775         {
7776           /* The HIGH and LO_SUM are matched by special .md patterns.  */
7777           mips_split_p[SYMBOL_GOT_DISP] = true;
7778
7779           mips_split_p[SYMBOL_GOTOFF_DISP] = true;
7780           mips_hi_relocs[SYMBOL_GOTOFF_DISP] = "%got_hi(";
7781           mips_lo_relocs[SYMBOL_GOTOFF_DISP] = "%got_lo(";
7782
7783           mips_split_p[SYMBOL_GOTOFF_CALL] = true;
7784           mips_hi_relocs[SYMBOL_GOTOFF_CALL] = "%call_hi(";
7785           mips_lo_relocs[SYMBOL_GOTOFF_CALL] = "%call_lo(";
7786         }
7787       else
7788         {
7789           if (TARGET_NEWABI)
7790             mips_lo_relocs[SYMBOL_GOTOFF_DISP] = "%got_disp(";
7791           else
7792             mips_lo_relocs[SYMBOL_GOTOFF_DISP] = "%got(";
7793           mips_lo_relocs[SYMBOL_GOTOFF_CALL] = "%call16(";
7794           if (TARGET_MIPS16)
7795             /* Expose the use of $28 as soon as possible.  */
7796             mips_split_p[SYMBOL_GOT_DISP] = true;
7797         }
7798     }
7799
7800   if (TARGET_NEWABI)
7801     {
7802       mips_split_p[SYMBOL_GOTOFF_LOADGP] = true;
7803       mips_hi_relocs[SYMBOL_GOTOFF_LOADGP] = "%hi(%neg(%gp_rel(";
7804       mips_lo_relocs[SYMBOL_GOTOFF_LOADGP] = "%lo(%neg(%gp_rel(";
7805     }
7806
7807   mips_lo_relocs[SYMBOL_TLSGD] = "%tlsgd(";
7808   mips_lo_relocs[SYMBOL_TLSLDM] = "%tlsldm(";
7809
7810   if (TARGET_MIPS16_PCREL_LOADS)
7811     {
7812       mips_use_pcrel_pool_p[SYMBOL_DTPREL] = true;
7813       mips_use_pcrel_pool_p[SYMBOL_TPREL] = true;
7814     }
7815   else
7816     {
7817       mips_split_p[SYMBOL_DTPREL] = true;
7818       mips_hi_relocs[SYMBOL_DTPREL] = "%dtprel_hi(";
7819       mips_lo_relocs[SYMBOL_DTPREL] = "%dtprel_lo(";
7820
7821       mips_split_p[SYMBOL_TPREL] = true;
7822       mips_hi_relocs[SYMBOL_TPREL] = "%tprel_hi(";
7823       mips_lo_relocs[SYMBOL_TPREL] = "%tprel_lo(";
7824     }
7825
7826   mips_lo_relocs[SYMBOL_GOTTPREL] = "%gottprel(";
7827   mips_lo_relocs[SYMBOL_HALF] = "%half(";
7828 }
7829
7830 /* Print symbolic operand OP, which is part of a HIGH or LO_SUM
7831    in context CONTEXT.  RELOCS is the array of relocations to use.  */
7832
7833 static void
7834 mips_print_operand_reloc (FILE *file, rtx op, enum mips_symbol_context context,
7835                           const char **relocs)
7836 {
7837   enum mips_symbol_type symbol_type;
7838   const char *p;
7839
7840   symbol_type = mips_classify_symbolic_expression (op, context);
7841   gcc_assert (relocs[symbol_type]);
7842
7843   fputs (relocs[symbol_type], file);
7844   output_addr_const (file, mips_strip_unspec_address (op));
7845   for (p = relocs[symbol_type]; *p != 0; p++)
7846     if (*p == '(')
7847       fputc (')', file);
7848 }
7849
7850 /* Start a new block with the given asm switch enabled.  If we need
7851    to print a directive, emit PREFIX before it and SUFFIX after it.  */
7852
7853 static void
7854 mips_push_asm_switch_1 (struct mips_asm_switch *asm_switch,
7855                         const char *prefix, const char *suffix)
7856 {
7857   if (asm_switch->nesting_level == 0)
7858     fprintf (asm_out_file, "%s.set\tno%s%s", prefix, asm_switch->name, suffix);
7859   asm_switch->nesting_level++;
7860 }
7861
7862 /* Likewise, but end a block.  */
7863
7864 static void
7865 mips_pop_asm_switch_1 (struct mips_asm_switch *asm_switch,
7866                        const char *prefix, const char *suffix)
7867 {
7868   gcc_assert (asm_switch->nesting_level);
7869   asm_switch->nesting_level--;
7870   if (asm_switch->nesting_level == 0)
7871     fprintf (asm_out_file, "%s.set\t%s%s", prefix, asm_switch->name, suffix);
7872 }
7873
7874 /* Wrappers around mips_push_asm_switch_1 and mips_pop_asm_switch_1
7875    that either print a complete line or print nothing.  */
7876
7877 void
7878 mips_push_asm_switch (struct mips_asm_switch *asm_switch)
7879 {
7880   mips_push_asm_switch_1 (asm_switch, "\t", "\n");
7881 }
7882
7883 void
7884 mips_pop_asm_switch (struct mips_asm_switch *asm_switch)
7885 {
7886   mips_pop_asm_switch_1 (asm_switch, "\t", "\n");
7887 }
7888
7889 /* Print the text for PRINT_OPERAND punctation character CH to FILE.
7890    The punctuation characters are:
7891
7892    '('  Start a nested ".set noreorder" block.
7893    ')'  End a nested ".set noreorder" block.
7894    '['  Start a nested ".set noat" block.
7895    ']'  End a nested ".set noat" block.
7896    '<'  Start a nested ".set nomacro" block.
7897    '>'  End a nested ".set nomacro" block.
7898    '*'  Behave like %(%< if generating a delayed-branch sequence.
7899    '#'  Print a nop if in a ".set noreorder" block.
7900    '/'  Like '#', but do nothing within a delayed-branch sequence.
7901    '?'  Print "l" if mips_branch_likely is true
7902    '~'  Print a nop if mips_branch_likely is true
7903    '.'  Print the name of the register with a hard-wired zero (zero or $0).
7904    '@'  Print the name of the assembler temporary register (at or $1).
7905    '^'  Print the name of the pic call-through register (t9 or $25).
7906    '+'  Print the name of the gp register (usually gp or $28).
7907    '$'  Print the name of the stack pointer register (sp or $29).
7908    ':'  Print "c" to use the compact version if the delay slot is a nop.
7909    '!'  Print "s" to use the short version if the delay slot contains a
7910         16-bit instruction.
7911
7912    See also mips_init_print_operand_pucnt.  */
7913
7914 static void
7915 mips_print_operand_punctuation (FILE *file, int ch)
7916 {
7917   switch (ch)
7918     {
7919     case '(':
7920       mips_push_asm_switch_1 (&mips_noreorder, "", "\n\t");
7921       break;
7922
7923     case ')':
7924       mips_pop_asm_switch_1 (&mips_noreorder, "\n\t", "");
7925       break;
7926
7927     case '[':
7928       mips_push_asm_switch_1 (&mips_noat, "", "\n\t");
7929       break;
7930
7931     case ']':
7932       mips_pop_asm_switch_1 (&mips_noat, "\n\t", "");
7933       break;
7934
7935     case '<':
7936       mips_push_asm_switch_1 (&mips_nomacro, "", "\n\t");
7937       break;
7938
7939     case '>':
7940       mips_pop_asm_switch_1 (&mips_nomacro, "\n\t", "");
7941       break;
7942
7943     case '*':
7944       if (final_sequence != 0)
7945         {
7946           mips_print_operand_punctuation (file, '(');
7947           mips_print_operand_punctuation (file, '<');
7948         }
7949       break;
7950
7951     case '#':
7952       if (mips_noreorder.nesting_level > 0)
7953         fputs ("\n\tnop", file);
7954       break;
7955
7956     case '/':
7957       /* Print an extra newline so that the delayed insn is separated
7958          from the following ones.  This looks neater and is consistent
7959          with non-nop delayed sequences.  */
7960       if (mips_noreorder.nesting_level > 0 && final_sequence == 0)
7961         fputs ("\n\tnop\n", file);
7962       break;
7963
7964     case '?':
7965       if (mips_branch_likely)
7966         putc ('l', file);
7967       break;
7968
7969     case '~':
7970       if (mips_branch_likely)
7971         fputs ("\n\tnop", file);
7972       break;
7973
7974     case '.':
7975       fputs (reg_names[GP_REG_FIRST + 0], file);
7976       break;
7977
7978     case '@':
7979       fputs (reg_names[AT_REGNUM], file);
7980       break;
7981
7982     case '^':
7983       fputs (reg_names[PIC_FUNCTION_ADDR_REGNUM], file);
7984       break;
7985
7986     case '+':
7987       fputs (reg_names[PIC_OFFSET_TABLE_REGNUM], file);
7988       break;
7989
7990     case '$':
7991       fputs (reg_names[STACK_POINTER_REGNUM], file);
7992       break;
7993
7994     case ':':
7995       /* When final_sequence is 0, the delay slot will be a nop.  We can
7996          use the compact version for microMIPS.  */
7997       if (final_sequence == 0)
7998         putc ('c', file);
7999       break;
8000
8001     case '!':
8002       /* If the delay slot instruction is short, then use the
8003          compact version.  */
8004       if (final_sequence == 0
8005           || get_attr_length (XVECEXP (final_sequence, 0, 1)) == 2)
8006         putc ('s', file);
8007       break;
8008
8009     default:
8010       gcc_unreachable ();
8011       break;
8012     }
8013 }
8014
8015 /* Initialize mips_print_operand_punct.  */
8016
8017 static void
8018 mips_init_print_operand_punct (void)
8019 {
8020   const char *p;
8021
8022   for (p = "()[]<>*#/?~.@^+$:!"; *p; p++)
8023     mips_print_operand_punct[(unsigned char) *p] = true;
8024 }
8025
8026 /* PRINT_OPERAND prefix LETTER refers to the integer branch instruction
8027    associated with condition CODE.  Print the condition part of the
8028    opcode to FILE.  */
8029
8030 static void
8031 mips_print_int_branch_condition (FILE *file, enum rtx_code code, int letter)
8032 {
8033   switch (code)
8034     {
8035     case EQ:
8036     case NE:
8037     case GT:
8038     case GE:
8039     case LT:
8040     case LE:
8041     case GTU:
8042     case GEU:
8043     case LTU:
8044     case LEU:
8045       /* Conveniently, the MIPS names for these conditions are the same
8046          as their RTL equivalents.  */
8047       fputs (GET_RTX_NAME (code), file);
8048       break;
8049
8050     default:
8051       output_operand_lossage ("'%%%c' is not a valid operand prefix", letter);
8052       break;
8053     }
8054 }
8055
8056 /* Likewise floating-point branches.  */
8057
8058 static void
8059 mips_print_float_branch_condition (FILE *file, enum rtx_code code, int letter)
8060 {
8061   switch (code)
8062     {
8063     case EQ:
8064       fputs ("c1f", file);
8065       break;
8066
8067     case NE:
8068       fputs ("c1t", file);
8069       break;
8070
8071     default:
8072       output_operand_lossage ("'%%%c' is not a valid operand prefix", letter);
8073       break;
8074     }
8075 }
8076
8077 /* Implement TARGET_PRINT_OPERAND_PUNCT_VALID_P.  */
8078
8079 static bool
8080 mips_print_operand_punct_valid_p (unsigned char code)
8081 {
8082   return mips_print_operand_punct[code];
8083 }
8084
8085 /* Implement TARGET_PRINT_OPERAND.  The MIPS-specific operand codes are:
8086
8087    'X'  Print CONST_INT OP in hexadecimal format.
8088    'x'  Print the low 16 bits of CONST_INT OP in hexadecimal format.
8089    'd'  Print CONST_INT OP in decimal.
8090    'm'  Print one less than CONST_INT OP in decimal.
8091    'h'  Print the high-part relocation associated with OP, after stripping
8092           any outermost HIGH.
8093    'R'  Print the low-part relocation associated with OP.
8094    'C'  Print the integer branch condition for comparison OP.
8095    'N'  Print the inverse of the integer branch condition for comparison OP.
8096    'F'  Print the FPU branch condition for comparison OP.
8097    'W'  Print the inverse of the FPU branch condition for comparison OP.
8098    'T'  Print 'f' for (eq:CC ...), 't' for (ne:CC ...),
8099               'z' for (eq:?I ...), 'n' for (ne:?I ...).
8100    't'  Like 'T', but with the EQ/NE cases reversed
8101    'Y'  Print mips_fp_conditions[INTVAL (OP)]
8102    'Z'  Print OP and a comma for ISA_HAS_8CC, otherwise print nothing.
8103    'q'  Print a DSP accumulator register.
8104    'D'  Print the second part of a double-word register or memory operand.
8105    'L'  Print the low-order register in a double-word register operand.
8106    'M'  Print high-order register in a double-word register operand.
8107    'z'  Print $0 if OP is zero, otherwise print OP normally.
8108    'b'  Print the address of a memory operand, without offset.  */
8109
8110 static void
8111 mips_print_operand (FILE *file, rtx op, int letter)
8112 {
8113   enum rtx_code code;
8114
8115   if (mips_print_operand_punct_valid_p (letter))
8116     {
8117       mips_print_operand_punctuation (file, letter);
8118       return;
8119     }
8120
8121   gcc_assert (op);
8122   code = GET_CODE (op);
8123
8124   switch (letter)
8125     {
8126     case 'X':
8127       if (CONST_INT_P (op))
8128         fprintf (file, HOST_WIDE_INT_PRINT_HEX, INTVAL (op));
8129       else
8130         output_operand_lossage ("invalid use of '%%%c'", letter);
8131       break;
8132
8133     case 'x':
8134       if (CONST_INT_P (op))
8135         fprintf (file, HOST_WIDE_INT_PRINT_HEX, INTVAL (op) & 0xffff);
8136       else
8137         output_operand_lossage ("invalid use of '%%%c'", letter);
8138       break;
8139
8140     case 'd':
8141       if (CONST_INT_P (op))
8142         fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (op));
8143       else
8144         output_operand_lossage ("invalid use of '%%%c'", letter);
8145       break;
8146
8147     case 'm':
8148       if (CONST_INT_P (op))
8149         fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (op) - 1);
8150       else
8151         output_operand_lossage ("invalid use of '%%%c'", letter);
8152       break;
8153
8154     case 'h':
8155       if (code == HIGH)
8156         op = XEXP (op, 0);
8157       mips_print_operand_reloc (file, op, SYMBOL_CONTEXT_LEA, mips_hi_relocs);
8158       break;
8159
8160     case 'R':
8161       mips_print_operand_reloc (file, op, SYMBOL_CONTEXT_LEA, mips_lo_relocs);
8162       break;
8163
8164     case 'C':
8165       mips_print_int_branch_condition (file, code, letter);
8166       break;
8167
8168     case 'N':
8169       mips_print_int_branch_condition (file, reverse_condition (code), letter);
8170       break;
8171
8172     case 'F':
8173       mips_print_float_branch_condition (file, code, letter);
8174       break;
8175
8176     case 'W':
8177       mips_print_float_branch_condition (file, reverse_condition (code),
8178                                          letter);
8179       break;
8180
8181     case 'T':
8182     case 't':
8183       {
8184         int truth = (code == NE) == (letter == 'T');
8185         fputc ("zfnt"[truth * 2 + (GET_MODE (op) == CCmode)], file);
8186       }
8187       break;
8188
8189     case 'Y':
8190       if (code == CONST_INT && UINTVAL (op) < ARRAY_SIZE (mips_fp_conditions))
8191         fputs (mips_fp_conditions[UINTVAL (op)], file);
8192       else
8193         output_operand_lossage ("'%%%c' is not a valid operand prefix",
8194                                 letter);
8195       break;
8196
8197     case 'Z':
8198       if (ISA_HAS_8CC)
8199         {
8200           mips_print_operand (file, op, 0);
8201           fputc (',', file);
8202         }
8203       break;
8204
8205     case 'q':
8206       if (code == REG && MD_REG_P (REGNO (op)))
8207         fprintf (file, "$ac0");
8208       else if (code == REG && DSP_ACC_REG_P (REGNO (op)))
8209         fprintf (file, "$ac%c", reg_names[REGNO (op)][3]);
8210       else
8211         output_operand_lossage ("invalid use of '%%%c'", letter);
8212       break;
8213
8214     default:
8215       switch (code)
8216         {
8217         case REG:
8218           {
8219             unsigned int regno = REGNO (op);
8220             if ((letter == 'M' && TARGET_LITTLE_ENDIAN)
8221                 || (letter == 'L' && TARGET_BIG_ENDIAN)
8222                 || letter == 'D')
8223               regno++;
8224             else if (letter && letter != 'z' && letter != 'M' && letter != 'L')
8225               output_operand_lossage ("invalid use of '%%%c'", letter);
8226             /* We need to print $0 .. $31 for COP0 registers.  */
8227             if (COP0_REG_P (regno))
8228               fprintf (file, "$%s", &reg_names[regno][4]);
8229             else
8230               fprintf (file, "%s", reg_names[regno]);
8231           }
8232           break;
8233
8234         case MEM:
8235           if (letter == 'D')
8236             output_address (plus_constant (Pmode, XEXP (op, 0), 4));
8237           else if (letter == 'b')
8238             {
8239               gcc_assert (REG_P (XEXP (op, 0)));
8240               mips_print_operand (file, XEXP (op, 0), 0);
8241             }
8242           else if (letter && letter != 'z')
8243             output_operand_lossage ("invalid use of '%%%c'", letter);
8244           else
8245             output_address (XEXP (op, 0));
8246           break;
8247
8248         default:
8249           if (letter == 'z' && op == CONST0_RTX (GET_MODE (op)))
8250             fputs (reg_names[GP_REG_FIRST], file);
8251           else if (letter && letter != 'z')
8252             output_operand_lossage ("invalid use of '%%%c'", letter);
8253           else if (CONST_GP_P (op))
8254             fputs (reg_names[GLOBAL_POINTER_REGNUM], file);
8255           else
8256             output_addr_const (file, mips_strip_unspec_address (op));
8257           break;
8258         }
8259     }
8260 }
8261
8262 /* Implement TARGET_PRINT_OPERAND_ADDRESS.  */
8263
8264 static void
8265 mips_print_operand_address (FILE *file, rtx x)
8266 {
8267   struct mips_address_info addr;
8268
8269   if (mips_classify_address (&addr, x, word_mode, true))
8270     switch (addr.type)
8271       {
8272       case ADDRESS_REG:
8273         mips_print_operand (file, addr.offset, 0);
8274         fprintf (file, "(%s)", reg_names[REGNO (addr.reg)]);
8275         return;
8276
8277       case ADDRESS_LO_SUM:
8278         mips_print_operand_reloc (file, addr.offset, SYMBOL_CONTEXT_MEM,
8279                                   mips_lo_relocs);
8280         fprintf (file, "(%s)", reg_names[REGNO (addr.reg)]);
8281         return;
8282
8283       case ADDRESS_CONST_INT:
8284         output_addr_const (file, x);
8285         fprintf (file, "(%s)", reg_names[GP_REG_FIRST]);
8286         return;
8287
8288       case ADDRESS_SYMBOLIC:
8289         output_addr_const (file, mips_strip_unspec_address (x));
8290         return;
8291       }
8292   gcc_unreachable ();
8293 }
8294 \f
8295 /* Implement TARGET_ENCODE_SECTION_INFO.  */
8296
8297 static void
8298 mips_encode_section_info (tree decl, rtx rtl, int first)
8299 {
8300   default_encode_section_info (decl, rtl, first);
8301
8302   if (TREE_CODE (decl) == FUNCTION_DECL)
8303     {
8304       rtx symbol = XEXP (rtl, 0);
8305       tree type = TREE_TYPE (decl);
8306
8307       /* Encode whether the symbol is short or long.  */
8308       if ((TARGET_LONG_CALLS && !mips_near_type_p (type))
8309           || mips_far_type_p (type))
8310         SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LONG_CALL;
8311     }
8312 }
8313
8314 /* Implement TARGET_SELECT_RTX_SECTION.  */
8315
8316 static section *
8317 mips_select_rtx_section (enum machine_mode mode, rtx x,
8318                          unsigned HOST_WIDE_INT align)
8319 {
8320   /* ??? Consider using mergeable small data sections.  */
8321   if (mips_rtx_constant_in_small_data_p (mode))
8322     return get_named_section (NULL, ".sdata", 0);
8323
8324   return default_elf_select_rtx_section (mode, x, align);
8325 }
8326
8327 /* Implement TARGET_ASM_FUNCTION_RODATA_SECTION.
8328
8329    The complication here is that, with the combination TARGET_ABICALLS
8330    && !TARGET_ABSOLUTE_ABICALLS && !TARGET_GPWORD, jump tables will use
8331    absolute addresses, and should therefore not be included in the
8332    read-only part of a DSO.  Handle such cases by selecting a normal
8333    data section instead of a read-only one.  The logic apes that in
8334    default_function_rodata_section.  */
8335
8336 static section *
8337 mips_function_rodata_section (tree decl)
8338 {
8339   if (!TARGET_ABICALLS || TARGET_ABSOLUTE_ABICALLS || TARGET_GPWORD)
8340     return default_function_rodata_section (decl);
8341
8342   if (decl && DECL_SECTION_NAME (decl))
8343     {
8344       const char *name = TREE_STRING_POINTER (DECL_SECTION_NAME (decl));
8345       if (DECL_ONE_ONLY (decl) && strncmp (name, ".gnu.linkonce.t.", 16) == 0)
8346         {
8347           char *rname = ASTRDUP (name);
8348           rname[14] = 'd';
8349           return get_section (rname, SECTION_LINKONCE | SECTION_WRITE, decl);
8350         }
8351       else if (flag_function_sections
8352                && flag_data_sections
8353                && strncmp (name, ".text.", 6) == 0)
8354         {
8355           char *rname = ASTRDUP (name);
8356           memcpy (rname + 1, "data", 4);
8357           return get_section (rname, SECTION_WRITE, decl);
8358         }
8359     }
8360   return data_section;
8361 }
8362
8363 /* Implement TARGET_IN_SMALL_DATA_P.  */
8364
8365 static bool
8366 mips_in_small_data_p (const_tree decl)
8367 {
8368   unsigned HOST_WIDE_INT size;
8369
8370   if (TREE_CODE (decl) == STRING_CST || TREE_CODE (decl) == FUNCTION_DECL)
8371     return false;
8372
8373   /* We don't yet generate small-data references for -mabicalls
8374      or VxWorks RTP code.  See the related -G handling in
8375      mips_option_override.  */
8376   if (TARGET_ABICALLS || TARGET_VXWORKS_RTP)
8377     return false;
8378
8379   if (TREE_CODE (decl) == VAR_DECL && DECL_SECTION_NAME (decl) != 0)
8380     {
8381       const char *name;
8382
8383       /* Reject anything that isn't in a known small-data section.  */
8384       name = TREE_STRING_POINTER (DECL_SECTION_NAME (decl));
8385       if (strcmp (name, ".sdata") != 0 && strcmp (name, ".sbss") != 0)
8386         return false;
8387
8388       /* If a symbol is defined externally, the assembler will use the
8389          usual -G rules when deciding how to implement macros.  */
8390       if (mips_lo_relocs[SYMBOL_GP_RELATIVE] || !DECL_EXTERNAL (decl))
8391         return true;
8392     }
8393   else if (TARGET_EMBEDDED_DATA)
8394     {
8395       /* Don't put constants into the small data section: we want them
8396          to be in ROM rather than RAM.  */
8397       if (TREE_CODE (decl) != VAR_DECL)
8398         return false;
8399
8400       if (TREE_READONLY (decl)
8401           && !TREE_SIDE_EFFECTS (decl)
8402           && (!DECL_INITIAL (decl) || TREE_CONSTANT (DECL_INITIAL (decl))))
8403         return false;
8404     }
8405
8406   /* Enforce -mlocal-sdata.  */
8407   if (!TARGET_LOCAL_SDATA && !TREE_PUBLIC (decl))
8408     return false;
8409
8410   /* Enforce -mextern-sdata.  */
8411   if (!TARGET_EXTERN_SDATA && DECL_P (decl))
8412     {
8413       if (DECL_EXTERNAL (decl))
8414         return false;
8415       if (DECL_COMMON (decl) && DECL_INITIAL (decl) == NULL)
8416         return false;
8417     }
8418
8419   /* We have traditionally not treated zero-sized objects as small data,
8420      so this is now effectively part of the ABI.  */
8421   size = int_size_in_bytes (TREE_TYPE (decl));
8422   return size > 0 && size <= mips_small_data_threshold;
8423 }
8424
8425 /* Implement TARGET_USE_ANCHORS_FOR_SYMBOL_P.  We don't want to use
8426    anchors for small data: the GP register acts as an anchor in that
8427    case.  We also don't want to use them for PC-relative accesses,
8428    where the PC acts as an anchor.  */
8429
8430 static bool
8431 mips_use_anchors_for_symbol_p (const_rtx symbol)
8432 {
8433   switch (mips_classify_symbol (symbol, SYMBOL_CONTEXT_MEM))
8434     {
8435     case SYMBOL_PC_RELATIVE:
8436     case SYMBOL_GP_RELATIVE:
8437       return false;
8438
8439     default:
8440       return default_use_anchors_for_symbol_p (symbol);
8441     }
8442 }
8443 \f
8444 /* The MIPS debug format wants all automatic variables and arguments
8445    to be in terms of the virtual frame pointer (stack pointer before
8446    any adjustment in the function), while the MIPS 3.0 linker wants
8447    the frame pointer to be the stack pointer after the initial
8448    adjustment.  So, we do the adjustment here.  The arg pointer (which
8449    is eliminated) points to the virtual frame pointer, while the frame
8450    pointer (which may be eliminated) points to the stack pointer after
8451    the initial adjustments.  */
8452
8453 HOST_WIDE_INT
8454 mips_debugger_offset (rtx addr, HOST_WIDE_INT offset)
8455 {
8456   rtx offset2 = const0_rtx;
8457   rtx reg = eliminate_constant_term (addr, &offset2);
8458
8459   if (offset == 0)
8460     offset = INTVAL (offset2);
8461
8462   if (reg == stack_pointer_rtx
8463       || reg == frame_pointer_rtx
8464       || reg == hard_frame_pointer_rtx)
8465     {
8466       offset -= cfun->machine->frame.total_size;
8467       if (reg == hard_frame_pointer_rtx)
8468         offset += cfun->machine->frame.hard_frame_pointer_offset;
8469     }
8470
8471   return offset;
8472 }
8473 \f
8474 /* Implement ASM_OUTPUT_EXTERNAL.  */
8475
8476 void
8477 mips_output_external (FILE *file, tree decl, const char *name)
8478 {
8479   default_elf_asm_output_external (file, decl, name);
8480
8481   /* We output the name if and only if TREE_SYMBOL_REFERENCED is
8482      set in order to avoid putting out names that are never really
8483      used. */
8484   if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
8485     {
8486       if (!TARGET_EXPLICIT_RELOCS && mips_in_small_data_p (decl))
8487         {
8488           /* When using assembler macros, emit .extern directives for
8489              all small-data externs so that the assembler knows how
8490              big they are.
8491
8492              In most cases it would be safe (though pointless) to emit
8493              .externs for other symbols too.  One exception is when an
8494              object is within the -G limit but declared by the user to
8495              be in a section other than .sbss or .sdata.  */
8496           fputs ("\t.extern\t", file);
8497           assemble_name (file, name);
8498           fprintf (file, ", " HOST_WIDE_INT_PRINT_DEC "\n",
8499                    int_size_in_bytes (TREE_TYPE (decl)));
8500         }
8501     }
8502 }
8503
8504 /* Implement TARGET_ASM_OUTPUT_SOURCE_FILENAME.  */
8505
8506 static void
8507 mips_output_filename (FILE *stream, const char *name)
8508 {
8509   /* If we are emitting DWARF-2, let dwarf2out handle the ".file"
8510      directives.  */
8511   if (write_symbols == DWARF2_DEBUG)
8512     return;
8513   else if (mips_output_filename_first_time)
8514     {
8515       mips_output_filename_first_time = 0;
8516       num_source_filenames += 1;
8517       current_function_file = name;
8518       fprintf (stream, "\t.file\t%d ", num_source_filenames);
8519       output_quoted_string (stream, name);
8520       putc ('\n', stream);
8521     }
8522   /* If we are emitting stabs, let dbxout.c handle this (except for
8523      the mips_output_filename_first_time case).  */
8524   else if (write_symbols == DBX_DEBUG)
8525     return;
8526   else if (name != current_function_file
8527            && strcmp (name, current_function_file) != 0)
8528     {
8529       num_source_filenames += 1;
8530       current_function_file = name;
8531       fprintf (stream, "\t.file\t%d ", num_source_filenames);
8532       output_quoted_string (stream, name);
8533       putc ('\n', stream);
8534     }
8535 }
8536
8537 /* Implement TARGET_ASM_OUTPUT_DWARF_DTPREL.  */
8538
8539 static void ATTRIBUTE_UNUSED
8540 mips_output_dwarf_dtprel (FILE *file, int size, rtx x)
8541 {
8542   switch (size)
8543     {
8544     case 4:
8545       fputs ("\t.dtprelword\t", file);
8546       break;
8547
8548     case 8:
8549       fputs ("\t.dtpreldword\t", file);
8550       break;
8551
8552     default:
8553       gcc_unreachable ();
8554     }
8555   output_addr_const (file, x);
8556   fputs ("+0x8000", file);
8557 }
8558
8559 /* Implement TARGET_DWARF_REGISTER_SPAN.  */
8560
8561 static rtx
8562 mips_dwarf_register_span (rtx reg)
8563 {
8564   rtx high, low;
8565   enum machine_mode mode;
8566
8567   /* By default, GCC maps increasing register numbers to increasing
8568      memory locations, but paired FPRs are always little-endian,
8569      regardless of the prevailing endianness.  */
8570   mode = GET_MODE (reg);
8571   if (FP_REG_P (REGNO (reg))
8572       && TARGET_BIG_ENDIAN
8573       && MAX_FPRS_PER_FMT > 1
8574       && GET_MODE_SIZE (mode) > UNITS_PER_FPREG)
8575     {
8576       gcc_assert (GET_MODE_SIZE (mode) == UNITS_PER_HWFPVALUE);
8577       high = mips_subword (reg, true);
8578       low = mips_subword (reg, false);
8579       return gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, high, low));
8580     }
8581
8582   return NULL_RTX;
8583 }
8584
8585 /* DSP ALU can bypass data with no delays for the following pairs. */
8586 enum insn_code dspalu_bypass_table[][2] =
8587 {
8588   {CODE_FOR_mips_addsc, CODE_FOR_mips_addwc},
8589   {CODE_FOR_mips_cmpu_eq_qb, CODE_FOR_mips_pick_qb},
8590   {CODE_FOR_mips_cmpu_lt_qb, CODE_FOR_mips_pick_qb},
8591   {CODE_FOR_mips_cmpu_le_qb, CODE_FOR_mips_pick_qb},
8592   {CODE_FOR_mips_cmp_eq_ph, CODE_FOR_mips_pick_ph},
8593   {CODE_FOR_mips_cmp_lt_ph, CODE_FOR_mips_pick_ph},
8594   {CODE_FOR_mips_cmp_le_ph, CODE_FOR_mips_pick_ph},
8595   {CODE_FOR_mips_wrdsp, CODE_FOR_mips_insv}
8596 };
8597
8598 int
8599 mips_dspalu_bypass_p (rtx out_insn, rtx in_insn)
8600 {
8601   int i;
8602   int num_bypass = ARRAY_SIZE (dspalu_bypass_table);
8603   enum insn_code out_icode = (enum insn_code) INSN_CODE (out_insn);
8604   enum insn_code in_icode = (enum insn_code) INSN_CODE (in_insn);
8605
8606   for (i = 0; i < num_bypass; i++)
8607     {
8608       if (out_icode == dspalu_bypass_table[i][0]
8609           && in_icode == dspalu_bypass_table[i][1])
8610        return true;
8611     }
8612
8613   return false;
8614 }
8615 /* Implement ASM_OUTPUT_ASCII.  */
8616
8617 void
8618 mips_output_ascii (FILE *stream, const char *string, size_t len)
8619 {
8620   size_t i;
8621   int cur_pos;
8622
8623   cur_pos = 17;
8624   fprintf (stream, "\t.ascii\t\"");
8625   for (i = 0; i < len; i++)
8626     {
8627       int c;
8628
8629       c = (unsigned char) string[i];
8630       if (ISPRINT (c))
8631         {
8632           if (c == '\\' || c == '\"')
8633             {
8634               putc ('\\', stream);
8635               cur_pos++;
8636             }
8637           putc (c, stream);
8638           cur_pos++;
8639         }
8640       else
8641         {
8642           fprintf (stream, "\\%03o", c);
8643           cur_pos += 4;
8644         }
8645
8646       if (cur_pos > 72 && i+1 < len)
8647         {
8648           cur_pos = 17;
8649           fprintf (stream, "\"\n\t.ascii\t\"");
8650         }
8651     }
8652   fprintf (stream, "\"\n");
8653 }
8654
8655 /* Return the pseudo-op for full SYMBOL_(D)TPREL address *ADDR.
8656    Update *ADDR with the operand that should be printed.  */
8657
8658 const char *
8659 mips_output_tls_reloc_directive (rtx *addr)
8660 {
8661   enum mips_symbol_type type;
8662
8663   type = mips_classify_symbolic_expression (*addr, SYMBOL_CONTEXT_LEA);
8664   *addr = mips_strip_unspec_address (*addr);
8665   switch (type)
8666     {
8667     case SYMBOL_DTPREL:
8668       return Pmode == SImode ? ".dtprelword\t%0" : ".dtpreldword\t%0";
8669
8670     case SYMBOL_TPREL:
8671       return Pmode == SImode ? ".tprelword\t%0" : ".tpreldword\t%0";
8672
8673     default:
8674       gcc_unreachable ();
8675     }
8676 }
8677
8678 /* Emit either a label, .comm, or .lcomm directive.  When using assembler
8679    macros, mark the symbol as written so that mips_asm_output_external
8680    won't emit an .extern for it.  STREAM is the output file, NAME is the
8681    name of the symbol, INIT_STRING is the string that should be written
8682    before the symbol and FINAL_STRING is the string that should be
8683    written after it.  FINAL_STRING is a printf format that consumes the
8684    remaining arguments.  */
8685
8686 void
8687 mips_declare_object (FILE *stream, const char *name, const char *init_string,
8688                      const char *final_string, ...)
8689 {
8690   va_list ap;
8691
8692   fputs (init_string, stream);
8693   assemble_name (stream, name);
8694   va_start (ap, final_string);
8695   vfprintf (stream, final_string, ap);
8696   va_end (ap);
8697
8698   if (!TARGET_EXPLICIT_RELOCS)
8699     {
8700       tree name_tree = get_identifier (name);
8701       TREE_ASM_WRITTEN (name_tree) = 1;
8702     }
8703 }
8704
8705 /* Declare a common object of SIZE bytes using asm directive INIT_STRING.
8706    NAME is the name of the object and ALIGN is the required alignment
8707    in bytes.  TAKES_ALIGNMENT_P is true if the directive takes a third
8708    alignment argument.  */
8709
8710 void
8711 mips_declare_common_object (FILE *stream, const char *name,
8712                             const char *init_string,
8713                             unsigned HOST_WIDE_INT size,
8714                             unsigned int align, bool takes_alignment_p)
8715 {
8716   if (!takes_alignment_p)
8717     {
8718       size += (align / BITS_PER_UNIT) - 1;
8719       size -= size % (align / BITS_PER_UNIT);
8720       mips_declare_object (stream, name, init_string,
8721                            "," HOST_WIDE_INT_PRINT_UNSIGNED "\n", size);
8722     }
8723   else
8724     mips_declare_object (stream, name, init_string,
8725                          "," HOST_WIDE_INT_PRINT_UNSIGNED ",%u\n",
8726                          size, align / BITS_PER_UNIT);
8727 }
8728
8729 /* Implement ASM_OUTPUT_ALIGNED_DECL_COMMON.  This is usually the same as the
8730    elfos.h version, but we also need to handle -muninit-const-in-rodata.  */
8731
8732 void
8733 mips_output_aligned_decl_common (FILE *stream, tree decl, const char *name,
8734                                  unsigned HOST_WIDE_INT size,
8735                                  unsigned int align)
8736 {
8737   /* If the target wants uninitialized const declarations in
8738      .rdata then don't put them in .comm.  */
8739   if (TARGET_EMBEDDED_DATA
8740       && TARGET_UNINIT_CONST_IN_RODATA
8741       && TREE_CODE (decl) == VAR_DECL
8742       && TREE_READONLY (decl)
8743       && (DECL_INITIAL (decl) == 0 || DECL_INITIAL (decl) == error_mark_node))
8744     {
8745       if (TREE_PUBLIC (decl) && DECL_NAME (decl))
8746         targetm.asm_out.globalize_label (stream, name);
8747
8748       switch_to_section (readonly_data_section);
8749       ASM_OUTPUT_ALIGN (stream, floor_log2 (align / BITS_PER_UNIT));
8750       mips_declare_object (stream, name, "",
8751                            ":\n\t.space\t" HOST_WIDE_INT_PRINT_UNSIGNED "\n",
8752                            size);
8753     }
8754   else
8755     mips_declare_common_object (stream, name, "\n\t.comm\t",
8756                                 size, align, true);
8757 }
8758
8759 #ifdef ASM_OUTPUT_SIZE_DIRECTIVE
8760 extern int size_directive_output;
8761
8762 /* Implement ASM_DECLARE_OBJECT_NAME.  This is like most of the standard ELF
8763    definitions except that it uses mips_declare_object to emit the label.  */
8764
8765 void
8766 mips_declare_object_name (FILE *stream, const char *name,
8767                           tree decl ATTRIBUTE_UNUSED)
8768 {
8769 #ifdef ASM_OUTPUT_TYPE_DIRECTIVE
8770   ASM_OUTPUT_TYPE_DIRECTIVE (stream, name, "object");
8771 #endif
8772
8773   size_directive_output = 0;
8774   if (!flag_inhibit_size_directive && DECL_SIZE (decl))
8775     {
8776       HOST_WIDE_INT size;
8777
8778       size_directive_output = 1;
8779       size = int_size_in_bytes (TREE_TYPE (decl));
8780       ASM_OUTPUT_SIZE_DIRECTIVE (stream, name, size);
8781     }
8782
8783   mips_declare_object (stream, name, "", ":\n");
8784 }
8785
8786 /* Implement ASM_FINISH_DECLARE_OBJECT.  This is generic ELF stuff.  */
8787
8788 void
8789 mips_finish_declare_object (FILE *stream, tree decl, int top_level, int at_end)
8790 {
8791   const char *name;
8792
8793   name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
8794   if (!flag_inhibit_size_directive
8795       && DECL_SIZE (decl) != 0
8796       && !at_end
8797       && top_level
8798       && DECL_INITIAL (decl) == error_mark_node
8799       && !size_directive_output)
8800     {
8801       HOST_WIDE_INT size;
8802
8803       size_directive_output = 1;
8804       size = int_size_in_bytes (TREE_TYPE (decl));
8805       ASM_OUTPUT_SIZE_DIRECTIVE (stream, name, size);
8806     }
8807 }
8808 #endif
8809 \f
8810 /* Return the FOO in the name of the ".mdebug.FOO" section associated
8811    with the current ABI.  */
8812
8813 static const char *
8814 mips_mdebug_abi_name (void)
8815 {
8816   switch (mips_abi)
8817     {
8818     case ABI_32:
8819       return "abi32";
8820     case ABI_O64:
8821       return "abiO64";
8822     case ABI_N32:
8823       return "abiN32";
8824     case ABI_64:
8825       return "abi64";
8826     case ABI_EABI:
8827       return TARGET_64BIT ? "eabi64" : "eabi32";
8828     default:
8829       gcc_unreachable ();
8830     }
8831 }
8832
8833 /* Implement TARGET_ASM_FILE_START.  */
8834
8835 static void
8836 mips_file_start (void)
8837 {
8838   default_file_start ();
8839
8840   /* Generate a special section to describe the ABI switches used to
8841      produce the resultant binary.  */
8842
8843   /* Record the ABI itself.  Modern versions of binutils encode
8844      this information in the ELF header flags, but GDB needs the
8845      information in order to correctly debug binaries produced by
8846      older binutils.  See the function mips_gdbarch_init in
8847      gdb/mips-tdep.c.  */
8848   fprintf (asm_out_file, "\t.section .mdebug.%s\n\t.previous\n",
8849            mips_mdebug_abi_name ());
8850
8851   /* There is no ELF header flag to distinguish long32 forms of the
8852      EABI from long64 forms.  Emit a special section to help tools
8853      such as GDB.  Do the same for o64, which is sometimes used with
8854      -mlong64.  */
8855   if (mips_abi == ABI_EABI || mips_abi == ABI_O64)
8856     fprintf (asm_out_file, "\t.section .gcc_compiled_long%d\n"
8857              "\t.previous\n", TARGET_LONG64 ? 64 : 32);
8858
8859   /* Record the NaN encoding.  */
8860   if (HAVE_AS_NAN || mips_nan != MIPS_IEEE_754_DEFAULT)
8861     fprintf (asm_out_file, "\t.nan\t%s\n",
8862              mips_nan == MIPS_IEEE_754_2008 ? "2008" : "legacy");
8863
8864 #ifdef HAVE_AS_GNU_ATTRIBUTE
8865   {
8866     int attr;
8867
8868     /* No floating-point operations, -mno-float.  */
8869     if (TARGET_NO_FLOAT)
8870       attr = 0;
8871     /* Soft-float code, -msoft-float.  */
8872     else if (!TARGET_HARD_FLOAT_ABI)
8873       attr = 3;
8874     /* Single-float code, -msingle-float.  */
8875     else if (!TARGET_DOUBLE_FLOAT)
8876       attr = 2;
8877     /* 64-bit FP registers on a 32-bit target, -mips32r2 -mfp64.  */
8878     else if (!TARGET_64BIT && TARGET_FLOAT64)
8879       attr = 4;
8880     /* Regular FP code, FP regs same size as GP regs, -mdouble-float.  */
8881     else
8882       attr = 1;
8883
8884     fprintf (asm_out_file, "\t.gnu_attribute 4, %d\n", attr);
8885   }
8886 #endif
8887
8888   /* If TARGET_ABICALLS, tell GAS to generate -KPIC code.  */
8889   if (TARGET_ABICALLS)
8890     {
8891       fprintf (asm_out_file, "\t.abicalls\n");
8892       if (TARGET_ABICALLS_PIC0)
8893         fprintf (asm_out_file, "\t.option\tpic0\n");
8894     }
8895
8896   if (flag_verbose_asm)
8897     fprintf (asm_out_file, "\n%s -G value = %d, Arch = %s, ISA = %d\n",
8898              ASM_COMMENT_START,
8899              mips_small_data_threshold, mips_arch_info->name, mips_isa);
8900 }
8901
8902 /* Implement TARGET_ASM_CODE_END.  */
8903
8904 static void
8905 mips_code_end (void)
8906 {
8907   if (mips_need_mips16_rdhwr_p)
8908     mips_output_mips16_rdhwr ();
8909 }
8910 \f
8911 /* Make the last instruction frame-related and note that it performs
8912    the operation described by FRAME_PATTERN.  */
8913
8914 static void
8915 mips_set_frame_expr (rtx frame_pattern)
8916 {
8917   rtx insn;
8918
8919   insn = get_last_insn ();
8920   RTX_FRAME_RELATED_P (insn) = 1;
8921   REG_NOTES (insn) = alloc_EXPR_LIST (REG_FRAME_RELATED_EXPR,
8922                                       frame_pattern,
8923                                       REG_NOTES (insn));
8924 }
8925
8926 /* Return a frame-related rtx that stores REG at MEM.
8927    REG must be a single register.  */
8928
8929 static rtx
8930 mips_frame_set (rtx mem, rtx reg)
8931 {
8932   rtx set;
8933
8934   set = gen_rtx_SET (VOIDmode, mem, reg);
8935   RTX_FRAME_RELATED_P (set) = 1;
8936
8937   return set;
8938 }
8939
8940 /* Record that the epilogue has restored call-saved register REG.  */
8941
8942 static void
8943 mips_add_cfa_restore (rtx reg)
8944 {
8945   mips_epilogue.cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg,
8946                                                mips_epilogue.cfa_restores);
8947 }
8948 \f
8949 /* If a MIPS16e SAVE or RESTORE instruction saves or restores register
8950    mips16e_s2_s8_regs[X], it must also save the registers in indexes
8951    X + 1 onwards.  Likewise mips16e_a0_a3_regs.  */
8952 static const unsigned char mips16e_s2_s8_regs[] = {
8953   30, 23, 22, 21, 20, 19, 18
8954 };
8955 static const unsigned char mips16e_a0_a3_regs[] = {
8956   4, 5, 6, 7
8957 };
8958
8959 /* A list of the registers that can be saved by the MIPS16e SAVE instruction,
8960    ordered from the uppermost in memory to the lowest in memory.  */
8961 static const unsigned char mips16e_save_restore_regs[] = {
8962   31, 30, 23, 22, 21, 20, 19, 18, 17, 16, 7, 6, 5, 4
8963 };
8964
8965 /* Return the index of the lowest X in the range [0, SIZE) for which
8966    bit REGS[X] is set in MASK.  Return SIZE if there is no such X.  */
8967
8968 static unsigned int
8969 mips16e_find_first_register (unsigned int mask, const unsigned char *regs,
8970                              unsigned int size)
8971 {
8972   unsigned int i;
8973
8974   for (i = 0; i < size; i++)
8975     if (BITSET_P (mask, regs[i]))
8976       break;
8977
8978   return i;
8979 }
8980
8981 /* *MASK_PTR is a mask of general-purpose registers and *NUM_REGS_PTR
8982    is the number of set bits.  If *MASK_PTR contains REGS[X] for some X
8983    in [0, SIZE), adjust *MASK_PTR and *NUM_REGS_PTR so that the same
8984    is true for all indexes (X, SIZE).  */
8985
8986 static void
8987 mips16e_mask_registers (unsigned int *mask_ptr, const unsigned char *regs,
8988                         unsigned int size, unsigned int *num_regs_ptr)
8989 {
8990   unsigned int i;
8991
8992   i = mips16e_find_first_register (*mask_ptr, regs, size);
8993   for (i++; i < size; i++)
8994     if (!BITSET_P (*mask_ptr, regs[i]))
8995       {
8996         *num_regs_ptr += 1;
8997         *mask_ptr |= 1 << regs[i];
8998       }
8999 }
9000
9001 /* Return a simplified form of X using the register values in REG_VALUES.
9002    REG_VALUES[R] is the last value assigned to hard register R, or null
9003    if R has not been modified.
9004
9005    This function is rather limited, but is good enough for our purposes.  */
9006
9007 static rtx
9008 mips16e_collect_propagate_value (rtx x, rtx *reg_values)
9009 {
9010   x = avoid_constant_pool_reference (x);
9011
9012   if (UNARY_P (x))
9013     {
9014       rtx x0 = mips16e_collect_propagate_value (XEXP (x, 0), reg_values);
9015       return simplify_gen_unary (GET_CODE (x), GET_MODE (x),
9016                                  x0, GET_MODE (XEXP (x, 0)));
9017     }
9018
9019   if (ARITHMETIC_P (x))
9020     {
9021       rtx x0 = mips16e_collect_propagate_value (XEXP (x, 0), reg_values);
9022       rtx x1 = mips16e_collect_propagate_value (XEXP (x, 1), reg_values);
9023       return simplify_gen_binary (GET_CODE (x), GET_MODE (x), x0, x1);
9024     }
9025
9026   if (REG_P (x)
9027       && reg_values[REGNO (x)]
9028       && !rtx_unstable_p (reg_values[REGNO (x)]))
9029     return reg_values[REGNO (x)];
9030
9031   return x;
9032 }
9033
9034 /* Return true if (set DEST SRC) stores an argument register into its
9035    caller-allocated save slot, storing the number of that argument
9036    register in *REGNO_PTR if so.  REG_VALUES is as for
9037    mips16e_collect_propagate_value.  */
9038
9039 static bool
9040 mips16e_collect_argument_save_p (rtx dest, rtx src, rtx *reg_values,
9041                                  unsigned int *regno_ptr)
9042 {
9043   unsigned int argno, regno;
9044   HOST_WIDE_INT offset, required_offset;
9045   rtx addr, base;
9046
9047   /* Check that this is a word-mode store.  */
9048   if (!MEM_P (dest) || !REG_P (src) || GET_MODE (dest) != word_mode)
9049     return false;
9050
9051   /* Check that the register being saved is an unmodified argument
9052      register.  */
9053   regno = REGNO (src);
9054   if (!IN_RANGE (regno, GP_ARG_FIRST, GP_ARG_LAST) || reg_values[regno])
9055     return false;
9056   argno = regno - GP_ARG_FIRST;
9057
9058   /* Check whether the address is an appropriate stack-pointer or
9059      frame-pointer access.  */
9060   addr = mips16e_collect_propagate_value (XEXP (dest, 0), reg_values);
9061   mips_split_plus (addr, &base, &offset);
9062   required_offset = cfun->machine->frame.total_size + argno * UNITS_PER_WORD;
9063   if (base == hard_frame_pointer_rtx)
9064     required_offset -= cfun->machine->frame.hard_frame_pointer_offset;
9065   else if (base != stack_pointer_rtx)
9066     return false;
9067   if (offset != required_offset)
9068     return false;
9069
9070   *regno_ptr = regno;
9071   return true;
9072 }
9073
9074 /* A subroutine of mips_expand_prologue, called only when generating
9075    MIPS16e SAVE instructions.  Search the start of the function for any
9076    instructions that save argument registers into their caller-allocated
9077    save slots.  Delete such instructions and return a value N such that
9078    saving [GP_ARG_FIRST, GP_ARG_FIRST + N) would make all the deleted
9079    instructions redundant.  */
9080
9081 static unsigned int
9082 mips16e_collect_argument_saves (void)
9083 {
9084   rtx reg_values[FIRST_PSEUDO_REGISTER];
9085   rtx insn, next, set, dest, src;
9086   unsigned int nargs, regno;
9087
9088   push_topmost_sequence ();
9089   nargs = 0;
9090   memset (reg_values, 0, sizeof (reg_values));
9091   for (insn = get_insns (); insn; insn = next)
9092     {
9093       next = NEXT_INSN (insn);
9094       if (NOTE_P (insn) || DEBUG_INSN_P (insn))
9095         continue;
9096
9097       if (!INSN_P (insn))
9098         break;
9099
9100       set = PATTERN (insn);
9101       if (GET_CODE (set) != SET)
9102         break;
9103
9104       dest = SET_DEST (set);
9105       src = SET_SRC (set);
9106       if (mips16e_collect_argument_save_p (dest, src, reg_values, &regno))
9107         {
9108           if (!BITSET_P (cfun->machine->frame.mask, regno))
9109             {
9110               delete_insn (insn);
9111               nargs = MAX (nargs, (regno - GP_ARG_FIRST) + 1);
9112             }
9113         }
9114       else if (REG_P (dest) && GET_MODE (dest) == word_mode)
9115         reg_values[REGNO (dest)]
9116           = mips16e_collect_propagate_value (src, reg_values);
9117       else
9118         break;
9119     }
9120   pop_topmost_sequence ();
9121
9122   return nargs;
9123 }
9124
9125 /* Return a move between register REGNO and memory location SP + OFFSET.
9126    REG_PARM_P is true if SP + OFFSET belongs to REG_PARM_STACK_SPACE.
9127    Make the move a load if RESTORE_P, otherwise make it a store.  */
9128
9129 static rtx
9130 mips16e_save_restore_reg (bool restore_p, bool reg_parm_p,
9131                           HOST_WIDE_INT offset, unsigned int regno)
9132 {
9133   rtx reg, mem;
9134
9135   mem = gen_frame_mem (SImode, plus_constant (Pmode, stack_pointer_rtx,
9136                                               offset));
9137   reg = gen_rtx_REG (SImode, regno);
9138   if (restore_p)
9139     {
9140       mips_add_cfa_restore (reg);
9141       return gen_rtx_SET (VOIDmode, reg, mem);
9142     }
9143   if (reg_parm_p)
9144     return gen_rtx_SET (VOIDmode, mem, reg);
9145   return mips_frame_set (mem, reg);
9146 }
9147
9148 /* Return RTL for a MIPS16e SAVE or RESTORE instruction; RESTORE_P says which.
9149    The instruction must:
9150
9151      - Allocate or deallocate SIZE bytes in total; SIZE is known
9152        to be nonzero.
9153
9154      - Save or restore as many registers in *MASK_PTR as possible.
9155        The instruction saves the first registers at the top of the
9156        allocated area, with the other registers below it.
9157
9158      - Save NARGS argument registers above the allocated area.
9159
9160    (NARGS is always zero if RESTORE_P.)
9161
9162    The SAVE and RESTORE instructions cannot save and restore all general
9163    registers, so there may be some registers left over for the caller to
9164    handle.  Destructively modify *MASK_PTR so that it contains the registers
9165    that still need to be saved or restored.  The caller can save these
9166    registers in the memory immediately below *OFFSET_PTR, which is a
9167    byte offset from the bottom of the allocated stack area.  */
9168
9169 static rtx
9170 mips16e_build_save_restore (bool restore_p, unsigned int *mask_ptr,
9171                             HOST_WIDE_INT *offset_ptr, unsigned int nargs,
9172                             HOST_WIDE_INT size)
9173 {
9174   rtx pattern, set;
9175   HOST_WIDE_INT offset, top_offset;
9176   unsigned int i, regno;
9177   int n;
9178
9179   gcc_assert (cfun->machine->frame.num_fp == 0);
9180
9181   /* Calculate the number of elements in the PARALLEL.  We need one element
9182      for the stack adjustment, one for each argument register save, and one
9183      for each additional register move.  */
9184   n = 1 + nargs;
9185   for (i = 0; i < ARRAY_SIZE (mips16e_save_restore_regs); i++)
9186     if (BITSET_P (*mask_ptr, mips16e_save_restore_regs[i]))
9187       n++;
9188
9189   /* Create the final PARALLEL.  */
9190   pattern = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (n));
9191   n = 0;
9192
9193   /* Add the stack pointer adjustment.  */
9194   set = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
9195                      plus_constant (Pmode, stack_pointer_rtx,
9196                                     restore_p ? size : -size));
9197   RTX_FRAME_RELATED_P (set) = 1;
9198   XVECEXP (pattern, 0, n++) = set;
9199
9200   /* Stack offsets in the PARALLEL are relative to the old stack pointer.  */
9201   top_offset = restore_p ? size : 0;
9202
9203   /* Save the arguments.  */
9204   for (i = 0; i < nargs; i++)
9205     {
9206       offset = top_offset + i * UNITS_PER_WORD;
9207       set = mips16e_save_restore_reg (restore_p, true, offset,
9208                                       GP_ARG_FIRST + i);
9209       XVECEXP (pattern, 0, n++) = set;
9210     }
9211
9212   /* Then fill in the other register moves.  */
9213   offset = top_offset;
9214   for (i = 0; i < ARRAY_SIZE (mips16e_save_restore_regs); i++)
9215     {
9216       regno = mips16e_save_restore_regs[i];
9217       if (BITSET_P (*mask_ptr, regno))
9218         {
9219           offset -= UNITS_PER_WORD;
9220           set = mips16e_save_restore_reg (restore_p, false, offset, regno);
9221           XVECEXP (pattern, 0, n++) = set;
9222           *mask_ptr &= ~(1 << regno);
9223         }
9224     }
9225
9226   /* Tell the caller what offset it should use for the remaining registers.  */
9227   *offset_ptr = size + (offset - top_offset);
9228
9229   gcc_assert (n == XVECLEN (pattern, 0));
9230
9231   return pattern;
9232 }
9233
9234 /* PATTERN is a PARALLEL whose first element adds ADJUST to the stack
9235    pointer.  Return true if PATTERN matches the kind of instruction
9236    generated by mips16e_build_save_restore.  If INFO is nonnull,
9237    initialize it when returning true.  */
9238
9239 bool
9240 mips16e_save_restore_pattern_p (rtx pattern, HOST_WIDE_INT adjust,
9241                                 struct mips16e_save_restore_info *info)
9242 {
9243   unsigned int i, nargs, mask, extra;
9244   HOST_WIDE_INT top_offset, save_offset, offset;
9245   rtx set, reg, mem, base;
9246   int n;
9247
9248   if (!GENERATE_MIPS16E_SAVE_RESTORE)
9249     return false;
9250
9251   /* Stack offsets in the PARALLEL are relative to the old stack pointer.  */
9252   top_offset = adjust > 0 ? adjust : 0;
9253
9254   /* Interpret all other members of the PARALLEL.  */
9255   save_offset = top_offset - UNITS_PER_WORD;
9256   mask = 0;
9257   nargs = 0;
9258   i = 0;
9259   for (n = 1; n < XVECLEN (pattern, 0); n++)
9260     {
9261       /* Check that we have a SET.  */
9262       set = XVECEXP (pattern, 0, n);
9263       if (GET_CODE (set) != SET)
9264         return false;
9265
9266       /* Check that the SET is a load (if restoring) or a store
9267          (if saving).  */
9268       mem = adjust > 0 ? SET_SRC (set) : SET_DEST (set);
9269       if (!MEM_P (mem))
9270         return false;
9271
9272       /* Check that the address is the sum of the stack pointer and a
9273          possibly-zero constant offset.  */
9274       mips_split_plus (XEXP (mem, 0), &base, &offset);
9275       if (base != stack_pointer_rtx)
9276         return false;
9277
9278       /* Check that SET's other operand is a register.  */
9279       reg = adjust > 0 ? SET_DEST (set) : SET_SRC (set);
9280       if (!REG_P (reg))
9281         return false;
9282
9283       /* Check for argument saves.  */
9284       if (offset == top_offset + nargs * UNITS_PER_WORD
9285           && REGNO (reg) == GP_ARG_FIRST + nargs)
9286         nargs++;
9287       else if (offset == save_offset)
9288         {
9289           while (mips16e_save_restore_regs[i++] != REGNO (reg))
9290             if (i == ARRAY_SIZE (mips16e_save_restore_regs))
9291               return false;
9292
9293           mask |= 1 << REGNO (reg);
9294           save_offset -= UNITS_PER_WORD;
9295         }
9296       else
9297         return false;
9298     }
9299
9300   /* Check that the restrictions on register ranges are met.  */
9301   extra = 0;
9302   mips16e_mask_registers (&mask, mips16e_s2_s8_regs,
9303                           ARRAY_SIZE (mips16e_s2_s8_regs), &extra);
9304   mips16e_mask_registers (&mask, mips16e_a0_a3_regs,
9305                           ARRAY_SIZE (mips16e_a0_a3_regs), &extra);
9306   if (extra != 0)
9307     return false;
9308
9309   /* Make sure that the topmost argument register is not saved twice.
9310      The checks above ensure that the same is then true for the other
9311      argument registers.  */
9312   if (nargs > 0 && BITSET_P (mask, GP_ARG_FIRST + nargs - 1))
9313     return false;
9314
9315   /* Pass back information, if requested.  */
9316   if (info)
9317     {
9318       info->nargs = nargs;
9319       info->mask = mask;
9320       info->size = (adjust > 0 ? adjust : -adjust);
9321     }
9322
9323   return true;
9324 }
9325
9326 /* Add a MIPS16e SAVE or RESTORE register-range argument to string S
9327    for the register range [MIN_REG, MAX_REG].  Return a pointer to
9328    the null terminator.  */
9329
9330 static char *
9331 mips16e_add_register_range (char *s, unsigned int min_reg,
9332                             unsigned int max_reg)
9333 {
9334   if (min_reg != max_reg)
9335     s += sprintf (s, ",%s-%s", reg_names[min_reg], reg_names[max_reg]);
9336   else
9337     s += sprintf (s, ",%s", reg_names[min_reg]);
9338   return s;
9339 }
9340
9341 /* Return the assembly instruction for a MIPS16e SAVE or RESTORE instruction.
9342    PATTERN and ADJUST are as for mips16e_save_restore_pattern_p.  */
9343
9344 const char *
9345 mips16e_output_save_restore (rtx pattern, HOST_WIDE_INT adjust)
9346 {
9347   static char buffer[300];
9348
9349   struct mips16e_save_restore_info info;
9350   unsigned int i, end;
9351   char *s;
9352
9353   /* Parse the pattern.  */
9354   if (!mips16e_save_restore_pattern_p (pattern, adjust, &info))
9355     gcc_unreachable ();
9356
9357   /* Add the mnemonic.  */
9358   s = strcpy (buffer, adjust > 0 ? "restore\t" : "save\t");
9359   s += strlen (s);
9360
9361   /* Save the arguments.  */
9362   if (info.nargs > 1)
9363     s += sprintf (s, "%s-%s,", reg_names[GP_ARG_FIRST],
9364                   reg_names[GP_ARG_FIRST + info.nargs - 1]);
9365   else if (info.nargs == 1)
9366     s += sprintf (s, "%s,", reg_names[GP_ARG_FIRST]);
9367
9368   /* Emit the amount of stack space to allocate or deallocate.  */
9369   s += sprintf (s, "%d", (int) info.size);
9370
9371   /* Save or restore $16.  */
9372   if (BITSET_P (info.mask, 16))
9373     s += sprintf (s, ",%s", reg_names[GP_REG_FIRST + 16]);
9374
9375   /* Save or restore $17.  */
9376   if (BITSET_P (info.mask, 17))
9377     s += sprintf (s, ",%s", reg_names[GP_REG_FIRST + 17]);
9378
9379   /* Save or restore registers in the range $s2...$s8, which
9380      mips16e_s2_s8_regs lists in decreasing order.  Note that this
9381      is a software register range; the hardware registers are not
9382      numbered consecutively.  */
9383   end = ARRAY_SIZE (mips16e_s2_s8_regs);
9384   i = mips16e_find_first_register (info.mask, mips16e_s2_s8_regs, end);
9385   if (i < end)
9386     s = mips16e_add_register_range (s, mips16e_s2_s8_regs[end - 1],
9387                                     mips16e_s2_s8_regs[i]);
9388
9389   /* Save or restore registers in the range $a0...$a3.  */
9390   end = ARRAY_SIZE (mips16e_a0_a3_regs);
9391   i = mips16e_find_first_register (info.mask, mips16e_a0_a3_regs, end);
9392   if (i < end)
9393     s = mips16e_add_register_range (s, mips16e_a0_a3_regs[i],
9394                                     mips16e_a0_a3_regs[end - 1]);
9395
9396   /* Save or restore $31.  */
9397   if (BITSET_P (info.mask, RETURN_ADDR_REGNUM))
9398     s += sprintf (s, ",%s", reg_names[RETURN_ADDR_REGNUM]);
9399
9400   return buffer;
9401 }
9402 \f
9403 /* Return true if the current function returns its value in a floating-point
9404    register in MIPS16 mode.  */
9405
9406 static bool
9407 mips16_cfun_returns_in_fpr_p (void)
9408 {
9409   tree return_type = DECL_RESULT (current_function_decl);
9410   return (TARGET_MIPS16
9411           && TARGET_HARD_FLOAT_ABI
9412           && !aggregate_value_p (return_type, current_function_decl)
9413           && mips_return_mode_in_fpr_p (DECL_MODE (return_type)));
9414 }
9415
9416 /* Return true if predicate PRED is true for at least one instruction.
9417    Cache the result in *CACHE, and assume that the result is true
9418    if *CACHE is already true.  */
9419
9420 static bool
9421 mips_find_gp_ref (bool *cache, bool (*pred) (rtx))
9422 {
9423   rtx insn;
9424
9425   if (!*cache)
9426     {
9427       push_topmost_sequence ();
9428       for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
9429         if (USEFUL_INSN_P (insn) && pred (insn))
9430           {
9431             *cache = true;
9432             break;
9433           }
9434       pop_topmost_sequence ();
9435     }
9436   return *cache;
9437 }
9438
9439 /* Return true if INSN refers to the global pointer in an "inflexible" way.
9440    See mips_cfun_has_inflexible_gp_ref_p for details.  */
9441
9442 static bool
9443 mips_insn_has_inflexible_gp_ref_p (rtx insn)
9444 {
9445   /* Uses of pic_offset_table_rtx in CALL_INSN_FUNCTION_USAGE
9446      indicate that the target could be a traditional MIPS
9447      lazily-binding stub.  */
9448   return find_reg_fusage (insn, USE, pic_offset_table_rtx);
9449 }
9450
9451 /* Return true if the current function refers to the global pointer
9452    in a way that forces $28 to be valid.  This means that we can't
9453    change the choice of global pointer, even for NewABI code.
9454
9455    One example of this (and one which needs several checks) is that
9456    $28 must be valid when calling traditional MIPS lazy-binding stubs.
9457    (This restriction does not apply to PLTs.)  */
9458
9459 static bool
9460 mips_cfun_has_inflexible_gp_ref_p (void)
9461 {
9462   /* If the function has a nonlocal goto, $28 must hold the correct
9463      global pointer for the target function.  That is, the target
9464      of the goto implicitly uses $28.  */
9465   if (crtl->has_nonlocal_goto)
9466     return true;
9467
9468   if (TARGET_ABICALLS_PIC2)
9469     {
9470       /* Symbolic accesses implicitly use the global pointer unless
9471          -mexplicit-relocs is in effect.  JAL macros to symbolic addresses
9472          might go to traditional MIPS lazy-binding stubs.  */
9473       if (!TARGET_EXPLICIT_RELOCS)
9474         return true;
9475
9476       /* FUNCTION_PROFILER includes a JAL to _mcount, which again
9477          can be lazily-bound.  */
9478       if (crtl->profile)
9479         return true;
9480
9481       /* MIPS16 functions that return in FPRs need to call an
9482          external libgcc routine.  This call is only made explict
9483          during mips_expand_epilogue, and it too might be lazily bound.  */
9484       if (mips16_cfun_returns_in_fpr_p ())
9485         return true;
9486     }
9487
9488   return mips_find_gp_ref (&cfun->machine->has_inflexible_gp_insn_p,
9489                            mips_insn_has_inflexible_gp_ref_p);
9490 }
9491
9492 /* Return true if INSN refers to the global pointer in a "flexible" way.
9493    See mips_cfun_has_flexible_gp_ref_p for details.  */
9494
9495 static bool
9496 mips_insn_has_flexible_gp_ref_p (rtx insn)
9497 {
9498   return (get_attr_got (insn) != GOT_UNSET
9499           || mips_small_data_pattern_p (PATTERN (insn))
9500           || reg_overlap_mentioned_p (pic_offset_table_rtx, PATTERN (insn)));
9501 }
9502
9503 /* Return true if the current function references the global pointer,
9504    but if those references do not inherently require the global pointer
9505    to be $28.  Assume !mips_cfun_has_inflexible_gp_ref_p ().  */
9506
9507 static bool
9508 mips_cfun_has_flexible_gp_ref_p (void)
9509 {
9510   /* Reload can sometimes introduce constant pool references
9511      into a function that otherwise didn't need them.  For example,
9512      suppose we have an instruction like:
9513
9514         (set (reg:DF R1) (float:DF (reg:SI R2)))
9515
9516      If R2 turns out to be a constant such as 1, the instruction may
9517      have a REG_EQUAL note saying that R1 == 1.0.  Reload then has
9518      the option of using this constant if R2 doesn't get allocated
9519      to a register.
9520
9521      In cases like these, reload will have added the constant to the
9522      pool but no instruction will yet refer to it.  */
9523   if (TARGET_ABICALLS_PIC2 && !reload_completed && crtl->uses_const_pool)
9524     return true;
9525
9526   return mips_find_gp_ref (&cfun->machine->has_flexible_gp_insn_p,
9527                            mips_insn_has_flexible_gp_ref_p);
9528 }
9529
9530 /* Return the register that should be used as the global pointer
9531    within this function.  Return INVALID_REGNUM if the function
9532    doesn't need a global pointer.  */
9533
9534 static unsigned int
9535 mips_global_pointer (void)
9536 {
9537   unsigned int regno;
9538
9539   /* $gp is always available unless we're using a GOT.  */
9540   if (!TARGET_USE_GOT)
9541     return GLOBAL_POINTER_REGNUM;
9542
9543   /* If there are inflexible references to $gp, we must use the
9544      standard register.  */
9545   if (mips_cfun_has_inflexible_gp_ref_p ())
9546     return GLOBAL_POINTER_REGNUM;
9547
9548   /* If there are no current references to $gp, then the only uses
9549      we can introduce later are those involved in long branches.  */
9550   if (TARGET_ABSOLUTE_JUMPS && !mips_cfun_has_flexible_gp_ref_p ())
9551     return INVALID_REGNUM;
9552
9553   /* If the global pointer is call-saved, try to use a call-clobbered
9554      alternative.  */
9555   if (TARGET_CALL_SAVED_GP && crtl->is_leaf)
9556     for (regno = GP_REG_FIRST; regno <= GP_REG_LAST; regno++)
9557       if (!df_regs_ever_live_p (regno)
9558           && call_really_used_regs[regno]
9559           && !fixed_regs[regno]
9560           && regno != PIC_FUNCTION_ADDR_REGNUM)
9561         return regno;
9562
9563   return GLOBAL_POINTER_REGNUM;
9564 }
9565
9566 /* Return true if the current function's prologue must load the global
9567    pointer value into pic_offset_table_rtx and store the same value in
9568    the function's cprestore slot (if any).
9569
9570    One problem we have to deal with is that, when emitting GOT-based
9571    position independent code, long-branch sequences will need to load
9572    the address of the branch target from the GOT.  We don't know until
9573    the very end of compilation whether (and where) the function needs
9574    long branches, so we must ensure that _any_ branch can access the
9575    global pointer in some form.  However, we do not want to pessimize
9576    the usual case in which all branches are short.
9577
9578    We handle this as follows:
9579
9580    (1) During reload, we set cfun->machine->global_pointer to
9581        INVALID_REGNUM if we _know_ that the current function
9582        doesn't need a global pointer.  This is only valid if
9583        long branches don't need the GOT.
9584
9585        Otherwise, we assume that we might need a global pointer
9586        and pick an appropriate register.
9587
9588    (2) If cfun->machine->global_pointer != INVALID_REGNUM,
9589        we ensure that the global pointer is available at every
9590        block boundary bar entry and exit.  We do this in one of two ways:
9591
9592        - If the function has a cprestore slot, we ensure that this
9593          slot is valid at every branch.  However, as explained in
9594          point (6) below, there is no guarantee that pic_offset_table_rtx
9595          itself is valid if new uses of the global pointer are introduced
9596          after the first post-epilogue split.
9597
9598          We guarantee that the cprestore slot is valid by loading it
9599          into a fake register, CPRESTORE_SLOT_REGNUM.  We then make
9600          this register live at every block boundary bar function entry
9601          and exit.  It is then invalid to move the load (and thus the
9602          preceding store) across a block boundary.
9603
9604        - If the function has no cprestore slot, we guarantee that
9605          pic_offset_table_rtx itself is valid at every branch.
9606
9607        See mips_eh_uses for the handling of the register liveness.
9608
9609    (3) During prologue and epilogue generation, we emit "ghost"
9610        placeholder instructions to manipulate the global pointer.
9611
9612    (4) During prologue generation, we set cfun->machine->must_initialize_gp_p
9613        and cfun->machine->must_restore_gp_when_clobbered_p if we already know
9614        that the function needs a global pointer.  (There is no need to set
9615        them earlier than this, and doing it as late as possible leads to
9616        fewer false positives.)
9617
9618    (5) If cfun->machine->must_initialize_gp_p is true during a
9619        split_insns pass, we split the ghost instructions into real
9620        instructions.  These split instructions can then be optimized in
9621        the usual way.  Otherwise, we keep the ghost instructions intact,
9622        and optimize for the case where they aren't needed.  We still
9623        have the option of splitting them later, if we need to introduce
9624        new uses of the global pointer.
9625
9626        For example, the scheduler ignores a ghost instruction that
9627        stores $28 to the stack, but it handles the split form of
9628        the ghost instruction as an ordinary store.
9629
9630    (6) [OldABI only.]  If cfun->machine->must_restore_gp_when_clobbered_p
9631        is true during the first post-epilogue split_insns pass, we split
9632        calls and restore_gp patterns into instructions that explicitly
9633        load pic_offset_table_rtx from the cprestore slot.  Otherwise,
9634        we split these patterns into instructions that _don't_ load from
9635        the cprestore slot.
9636
9637        If cfun->machine->must_restore_gp_when_clobbered_p is true at the
9638        time of the split, then any instructions that exist at that time
9639        can make free use of pic_offset_table_rtx.  However, if we want
9640        to introduce new uses of the global pointer after the split,
9641        we must explicitly load the value from the cprestore slot, since
9642        pic_offset_table_rtx itself might not be valid at a given point
9643        in the function.
9644
9645        The idea is that we want to be able to delete redundant
9646        loads from the cprestore slot in the usual case where no
9647        long branches are needed.
9648
9649    (7) If cfun->machine->must_initialize_gp_p is still false at the end
9650        of md_reorg, we decide whether the global pointer is needed for
9651        long branches.  If so, we set cfun->machine->must_initialize_gp_p
9652        to true and split the ghost instructions into real instructions
9653        at that stage.
9654
9655    Note that the ghost instructions must have a zero length for three reasons:
9656
9657    - Giving the length of the underlying $gp sequence might cause
9658      us to use long branches in cases where they aren't really needed.
9659
9660    - They would perturb things like alignment calculations.
9661
9662    - More importantly, the hazard detection in md_reorg relies on
9663      empty instructions having a zero length.
9664
9665    If we find a long branch and split the ghost instructions at the
9666    end of md_reorg, the split could introduce more long branches.
9667    That isn't a problem though, because we still do the split before
9668    the final shorten_branches pass.
9669
9670    This is extremely ugly, but it seems like the best compromise between
9671    correctness and efficiency.  */
9672
9673 bool
9674 mips_must_initialize_gp_p (void)
9675 {
9676   return cfun->machine->must_initialize_gp_p;
9677 }
9678
9679 /* Return true if REGNO is a register that is ordinarily call-clobbered
9680    but must nevertheless be preserved by an interrupt handler.  */
9681
9682 static bool
9683 mips_interrupt_extra_call_saved_reg_p (unsigned int regno)
9684 {
9685   if (MD_REG_P (regno))
9686     return true;
9687
9688   if (TARGET_DSP && DSP_ACC_REG_P (regno))
9689     return true;
9690
9691   if (GP_REG_P (regno) && !cfun->machine->use_shadow_register_set_p)
9692     {
9693       /* $0 is hard-wired.  */
9694       if (regno == GP_REG_FIRST)
9695         return false;
9696
9697       /* The interrupt handler can treat kernel registers as
9698          scratch registers.  */
9699       if (KERNEL_REG_P (regno))
9700         return false;
9701
9702       /* The function will return the stack pointer to its original value
9703          anyway.  */
9704       if (regno == STACK_POINTER_REGNUM)
9705         return false;
9706
9707       /* Otherwise, return true for registers that aren't ordinarily
9708          call-clobbered.  */
9709       return call_really_used_regs[regno];
9710     }
9711
9712   return false;
9713 }
9714
9715 /* Return true if the current function should treat register REGNO
9716    as call-saved.  */
9717
9718 static bool
9719 mips_cfun_call_saved_reg_p (unsigned int regno)
9720 {
9721   /* If the user makes an ordinarily-call-saved register global,
9722      that register is no longer call-saved.  */
9723   if (global_regs[regno])
9724     return false;
9725
9726   /* Interrupt handlers need to save extra registers.  */
9727   if (cfun->machine->interrupt_handler_p
9728       && mips_interrupt_extra_call_saved_reg_p (regno))
9729     return true;
9730
9731   /* call_insns preserve $28 unless they explicitly say otherwise,
9732      so call_really_used_regs[] treats $28 as call-saved.  However,
9733      we want the ABI property rather than the default call_insn
9734      property here.  */
9735   return (regno == GLOBAL_POINTER_REGNUM
9736           ? TARGET_CALL_SAVED_GP
9737           : !call_really_used_regs[regno]);
9738 }
9739
9740 /* Return true if the function body might clobber register REGNO.
9741    We know that REGNO is call-saved.  */
9742
9743 static bool
9744 mips_cfun_might_clobber_call_saved_reg_p (unsigned int regno)
9745 {
9746   /* Some functions should be treated as clobbering all call-saved
9747      registers.  */
9748   if (crtl->saves_all_registers)
9749     return true;
9750
9751   /* DF handles cases where a register is explicitly referenced in
9752      the rtl.  Incoming values are passed in call-clobbered registers,
9753      so we can assume that any live call-saved register is set within
9754      the function.  */
9755   if (df_regs_ever_live_p (regno))
9756     return true;
9757
9758   /* Check for registers that are clobbered by FUNCTION_PROFILER.
9759      These clobbers are not explicit in the rtl.  */
9760   if (crtl->profile && MIPS_SAVE_REG_FOR_PROFILING_P (regno))
9761     return true;
9762
9763   /* If we're using a call-saved global pointer, the function's
9764      prologue will need to set it up.  */
9765   if (cfun->machine->global_pointer == regno)
9766     return true;
9767
9768   /* The function's prologue will need to set the frame pointer if
9769      frame_pointer_needed.  */
9770   if (regno == HARD_FRAME_POINTER_REGNUM && frame_pointer_needed)
9771     return true;
9772
9773   /* If a MIPS16 function returns a value in FPRs, its epilogue
9774      will need to call an external libgcc routine.  This yet-to-be
9775      generated call_insn will clobber $31.  */
9776   if (regno == RETURN_ADDR_REGNUM && mips16_cfun_returns_in_fpr_p ())
9777     return true;
9778
9779   /* If REGNO is ordinarily call-clobbered, we must assume that any
9780      called function could modify it.  */
9781   if (cfun->machine->interrupt_handler_p
9782       && !crtl->is_leaf
9783       && mips_interrupt_extra_call_saved_reg_p (regno))
9784     return true;
9785
9786   return false;
9787 }
9788
9789 /* Return true if the current function must save register REGNO.  */
9790
9791 static bool
9792 mips_save_reg_p (unsigned int regno)
9793 {
9794   if (mips_cfun_call_saved_reg_p (regno))
9795     {
9796       if (mips_cfun_might_clobber_call_saved_reg_p (regno))
9797         return true;
9798
9799       /* Save both registers in an FPR pair if either one is used.  This is
9800          needed for the case when MIN_FPRS_PER_FMT == 1, which allows the odd
9801          register to be used without the even register.  */
9802       if (FP_REG_P (regno)
9803           && MAX_FPRS_PER_FMT == 2
9804           && mips_cfun_might_clobber_call_saved_reg_p (regno + 1))
9805         return true;
9806     }
9807
9808   /* We need to save the incoming return address if __builtin_eh_return
9809      is being used to set a different return address.  */
9810   if (regno == RETURN_ADDR_REGNUM && crtl->calls_eh_return)
9811     return true;
9812
9813   return false;
9814 }
9815
9816 /* Populate the current function's mips_frame_info structure.
9817
9818    MIPS stack frames look like:
9819
9820         +-------------------------------+
9821         |                               |
9822         |  incoming stack arguments     |
9823         |                               |
9824         +-------------------------------+
9825         |                               |
9826         |  caller-allocated save area   |
9827       A |  for register arguments       |
9828         |                               |
9829         +-------------------------------+ <-- incoming stack pointer
9830         |                               |
9831         |  callee-allocated save area   |
9832       B |  for arguments that are       |
9833         |  split between registers and  |
9834         |  the stack                    |
9835         |                               |
9836         +-------------------------------+ <-- arg_pointer_rtx
9837         |                               |
9838       C |  callee-allocated save area   |
9839         |  for register varargs         |
9840         |                               |
9841         +-------------------------------+ <-- frame_pointer_rtx
9842         |                               |       + cop0_sp_offset
9843         |  COP0 reg save area           |       + UNITS_PER_WORD
9844         |                               |
9845         +-------------------------------+ <-- frame_pointer_rtx + acc_sp_offset
9846         |                               |       + UNITS_PER_WORD
9847         |  accumulator save area        |
9848         |                               |
9849         +-------------------------------+ <-- stack_pointer_rtx + fp_sp_offset
9850         |                               |       + UNITS_PER_HWFPVALUE
9851         |  FPR save area                |
9852         |                               |
9853         +-------------------------------+ <-- stack_pointer_rtx + gp_sp_offset
9854         |                               |       + UNITS_PER_WORD
9855         |  GPR save area                |
9856         |                               |
9857         +-------------------------------+ <-- frame_pointer_rtx with
9858         |                               | \     -fstack-protector
9859         |  local variables              |  | var_size
9860         |                               | /
9861         +-------------------------------+
9862         |                               | \
9863         |  $gp save area                |  | cprestore_size
9864         |                               | /
9865       P +-------------------------------+ <-- hard_frame_pointer_rtx for
9866         |                               | \     MIPS16 code
9867         |  outgoing stack arguments     |  |
9868         |                               |  |
9869         +-------------------------------+  | args_size
9870         |                               |  |
9871         |  caller-allocated save area   |  |
9872         |  for register arguments       |  |
9873         |                               | /
9874         +-------------------------------+ <-- stack_pointer_rtx
9875                                               frame_pointer_rtx without
9876                                                 -fstack-protector
9877                                               hard_frame_pointer_rtx for
9878                                                 non-MIPS16 code.
9879
9880    At least two of A, B and C will be empty.
9881
9882    Dynamic stack allocations such as alloca insert data at point P.
9883    They decrease stack_pointer_rtx but leave frame_pointer_rtx and
9884    hard_frame_pointer_rtx unchanged.  */
9885
9886 static void
9887 mips_compute_frame_info (void)
9888 {
9889   struct mips_frame_info *frame;
9890   HOST_WIDE_INT offset, size;
9891   unsigned int regno, i;
9892
9893   /* Set this function's interrupt properties.  */
9894   if (mips_interrupt_type_p (TREE_TYPE (current_function_decl)))
9895     {
9896       if (!ISA_MIPS32R2)
9897         error ("the %<interrupt%> attribute requires a MIPS32r2 processor");
9898       else if (TARGET_HARD_FLOAT)
9899         error ("the %<interrupt%> attribute requires %<-msoft-float%>");
9900       else if (TARGET_MIPS16)
9901         error ("interrupt handlers cannot be MIPS16 functions");
9902       else
9903         {
9904           cfun->machine->interrupt_handler_p = true;
9905           cfun->machine->use_shadow_register_set_p =
9906             mips_use_shadow_register_set_p (TREE_TYPE (current_function_decl));
9907           cfun->machine->keep_interrupts_masked_p =
9908             mips_keep_interrupts_masked_p (TREE_TYPE (current_function_decl));
9909           cfun->machine->use_debug_exception_return_p =
9910             mips_use_debug_exception_return_p (TREE_TYPE
9911                                                (current_function_decl));
9912         }
9913     }
9914
9915   frame = &cfun->machine->frame;
9916   memset (frame, 0, sizeof (*frame));
9917   size = get_frame_size ();
9918
9919   cfun->machine->global_pointer = mips_global_pointer ();
9920
9921   /* The first two blocks contain the outgoing argument area and the $gp save
9922      slot.  This area isn't needed in leaf functions, but if the
9923      target-independent frame size is nonzero, we have already committed to
9924      allocating these in STARTING_FRAME_OFFSET for !FRAME_GROWS_DOWNWARD.  */
9925   if ((size == 0 || FRAME_GROWS_DOWNWARD) && crtl->is_leaf)
9926     {
9927       /* The MIPS 3.0 linker does not like functions that dynamically
9928          allocate the stack and have 0 for STACK_DYNAMIC_OFFSET, since it
9929          looks like we are trying to create a second frame pointer to the
9930          function, so allocate some stack space to make it happy.  */
9931       if (cfun->calls_alloca)
9932         frame->args_size = REG_PARM_STACK_SPACE (cfun->decl);
9933       else
9934         frame->args_size = 0;
9935       frame->cprestore_size = 0;
9936     }
9937   else
9938     {
9939       frame->args_size = crtl->outgoing_args_size;
9940       frame->cprestore_size = MIPS_GP_SAVE_AREA_SIZE;
9941     }
9942   offset = frame->args_size + frame->cprestore_size;
9943
9944   /* Move above the local variables.  */
9945   frame->var_size = MIPS_STACK_ALIGN (size);
9946   offset += frame->var_size;
9947
9948   /* Find out which GPRs we need to save.  */
9949   for (regno = GP_REG_FIRST; regno <= GP_REG_LAST; regno++)
9950     if (mips_save_reg_p (regno))
9951       {
9952         frame->num_gp++;
9953         frame->mask |= 1 << (regno - GP_REG_FIRST);
9954       }
9955
9956   /* If this function calls eh_return, we must also save and restore the
9957      EH data registers.  */
9958   if (crtl->calls_eh_return)
9959     for (i = 0; EH_RETURN_DATA_REGNO (i) != INVALID_REGNUM; i++)
9960       {
9961         frame->num_gp++;
9962         frame->mask |= 1 << (EH_RETURN_DATA_REGNO (i) - GP_REG_FIRST);
9963       }
9964
9965   /* The MIPS16e SAVE and RESTORE instructions have two ranges of registers:
9966      $a3-$a0 and $s2-$s8.  If we save one register in the range, we must
9967      save all later registers too.  */
9968   if (GENERATE_MIPS16E_SAVE_RESTORE)
9969     {
9970       mips16e_mask_registers (&frame->mask, mips16e_s2_s8_regs,
9971                               ARRAY_SIZE (mips16e_s2_s8_regs), &frame->num_gp);
9972       mips16e_mask_registers (&frame->mask, mips16e_a0_a3_regs,
9973                               ARRAY_SIZE (mips16e_a0_a3_regs), &frame->num_gp);
9974     }
9975
9976   /* Move above the GPR save area.  */
9977   if (frame->num_gp > 0)
9978     {
9979       offset += MIPS_STACK_ALIGN (frame->num_gp * UNITS_PER_WORD);
9980       frame->gp_sp_offset = offset - UNITS_PER_WORD;
9981     }
9982
9983   /* Find out which FPRs we need to save.  This loop must iterate over
9984      the same space as its companion in mips_for_each_saved_gpr_and_fpr.  */
9985   if (TARGET_HARD_FLOAT)
9986     for (regno = FP_REG_FIRST; regno <= FP_REG_LAST; regno += MAX_FPRS_PER_FMT)
9987       if (mips_save_reg_p (regno))
9988         {
9989           frame->num_fp += MAX_FPRS_PER_FMT;
9990           frame->fmask |= ~(~0 << MAX_FPRS_PER_FMT) << (regno - FP_REG_FIRST);
9991         }
9992
9993   /* Move above the FPR save area.  */
9994   if (frame->num_fp > 0)
9995     {
9996       offset += MIPS_STACK_ALIGN (frame->num_fp * UNITS_PER_FPREG);
9997       frame->fp_sp_offset = offset - UNITS_PER_HWFPVALUE;
9998     }
9999
10000   /* Add in space for the interrupt context information.  */
10001   if (cfun->machine->interrupt_handler_p)
10002     {
10003       /* Check HI/LO.  */
10004       if (mips_save_reg_p (LO_REGNUM) || mips_save_reg_p (HI_REGNUM))
10005         {
10006           frame->num_acc++;
10007           frame->acc_mask |= (1 << 0);
10008         }
10009
10010       /* Check accumulators 1, 2, 3.  */
10011       for (i = DSP_ACC_REG_FIRST; i <= DSP_ACC_REG_LAST; i += 2)
10012         if (mips_save_reg_p (i) || mips_save_reg_p (i + 1))
10013           {
10014             frame->num_acc++;
10015             frame->acc_mask |= 1 << (((i - DSP_ACC_REG_FIRST) / 2) + 1);
10016           }
10017
10018       /* All interrupt context functions need space to preserve STATUS.  */
10019       frame->num_cop0_regs++;
10020
10021       /* If we don't keep interrupts masked, we need to save EPC.  */
10022       if (!cfun->machine->keep_interrupts_masked_p)
10023         frame->num_cop0_regs++;
10024     }
10025
10026   /* Move above the accumulator save area.  */
10027   if (frame->num_acc > 0)
10028     {
10029       /* Each accumulator needs 2 words.  */
10030       offset += frame->num_acc * 2 * UNITS_PER_WORD;
10031       frame->acc_sp_offset = offset - UNITS_PER_WORD;
10032     }
10033
10034   /* Move above the COP0 register save area.  */
10035   if (frame->num_cop0_regs > 0)
10036     {
10037       offset += frame->num_cop0_regs * UNITS_PER_WORD;
10038       frame->cop0_sp_offset = offset - UNITS_PER_WORD;
10039     }
10040
10041   /* Move above the callee-allocated varargs save area.  */
10042   offset += MIPS_STACK_ALIGN (cfun->machine->varargs_size);
10043   frame->arg_pointer_offset = offset;
10044
10045   /* Move above the callee-allocated area for pretend stack arguments.  */
10046   offset += crtl->args.pretend_args_size;
10047   frame->total_size = offset;
10048
10049   /* Work out the offsets of the save areas from the top of the frame.  */
10050   if (frame->gp_sp_offset > 0)
10051     frame->gp_save_offset = frame->gp_sp_offset - offset;
10052   if (frame->fp_sp_offset > 0)
10053     frame->fp_save_offset = frame->fp_sp_offset - offset;
10054   if (frame->acc_sp_offset > 0)
10055     frame->acc_save_offset = frame->acc_sp_offset - offset;
10056   if (frame->num_cop0_regs > 0)
10057     frame->cop0_save_offset = frame->cop0_sp_offset - offset;
10058
10059   /* MIPS16 code offsets the frame pointer by the size of the outgoing
10060      arguments.  This tends to increase the chances of using unextended
10061      instructions for local variables and incoming arguments.  */
10062   if (TARGET_MIPS16)
10063     frame->hard_frame_pointer_offset = frame->args_size;
10064 }
10065
10066 /* Return the style of GP load sequence that is being used for the
10067    current function.  */
10068
10069 enum mips_loadgp_style
10070 mips_current_loadgp_style (void)
10071 {
10072   if (!TARGET_USE_GOT || cfun->machine->global_pointer == INVALID_REGNUM)
10073     return LOADGP_NONE;
10074
10075   if (TARGET_RTP_PIC)
10076     return LOADGP_RTP;
10077
10078   if (TARGET_ABSOLUTE_ABICALLS)
10079     return LOADGP_ABSOLUTE;
10080
10081   return TARGET_NEWABI ? LOADGP_NEWABI : LOADGP_OLDABI;
10082 }
10083
10084 /* Implement TARGET_FRAME_POINTER_REQUIRED.  */
10085
10086 static bool
10087 mips_frame_pointer_required (void)
10088 {
10089   /* If the function contains dynamic stack allocations, we need to
10090      use the frame pointer to access the static parts of the frame.  */
10091   if (cfun->calls_alloca)
10092     return true;
10093
10094   /* In MIPS16 mode, we need a frame pointer for a large frame; otherwise,
10095      reload may be unable to compute the address of a local variable,
10096      since there is no way to add a large constant to the stack pointer
10097      without using a second temporary register.  */
10098   if (TARGET_MIPS16)
10099     {
10100       mips_compute_frame_info ();
10101       if (!SMALL_OPERAND (cfun->machine->frame.total_size))
10102         return true;
10103     }
10104
10105   return false;
10106 }
10107
10108 /* Make sure that we're not trying to eliminate to the wrong hard frame
10109    pointer.  */
10110
10111 static bool
10112 mips_can_eliminate (const int from ATTRIBUTE_UNUSED, const int to)
10113 {
10114   return (to == HARD_FRAME_POINTER_REGNUM || to == STACK_POINTER_REGNUM);
10115 }
10116
10117 /* Implement INITIAL_ELIMINATION_OFFSET.  FROM is either the frame pointer
10118    or argument pointer.  TO is either the stack pointer or hard frame
10119    pointer.  */
10120
10121 HOST_WIDE_INT
10122 mips_initial_elimination_offset (int from, int to)
10123 {
10124   HOST_WIDE_INT offset;
10125
10126   mips_compute_frame_info ();
10127
10128   /* Set OFFSET to the offset from the end-of-prologue stack pointer.  */
10129   switch (from)
10130     {
10131     case FRAME_POINTER_REGNUM:
10132       if (FRAME_GROWS_DOWNWARD)
10133         offset = (cfun->machine->frame.args_size
10134                   + cfun->machine->frame.cprestore_size
10135                   + cfun->machine->frame.var_size);
10136       else
10137         offset = 0;
10138       break;
10139
10140     case ARG_POINTER_REGNUM:
10141       offset = cfun->machine->frame.arg_pointer_offset;
10142       break;
10143
10144     default:
10145       gcc_unreachable ();
10146     }
10147
10148   if (to == HARD_FRAME_POINTER_REGNUM)
10149     offset -= cfun->machine->frame.hard_frame_pointer_offset;
10150
10151   return offset;
10152 }
10153 \f
10154 /* Implement TARGET_EXTRA_LIVE_ON_ENTRY.  */
10155
10156 static void
10157 mips_extra_live_on_entry (bitmap regs)
10158 {
10159   if (TARGET_USE_GOT)
10160     {
10161       /* PIC_FUNCTION_ADDR_REGNUM is live if we need it to set up
10162          the global pointer.   */
10163       if (!TARGET_ABSOLUTE_ABICALLS)
10164         bitmap_set_bit (regs, PIC_FUNCTION_ADDR_REGNUM);
10165
10166       /* The prologue may set MIPS16_PIC_TEMP_REGNUM to the value of
10167          the global pointer.  */
10168       if (TARGET_MIPS16)
10169         bitmap_set_bit (regs, MIPS16_PIC_TEMP_REGNUM);
10170
10171       /* See the comment above load_call<mode> for details.  */
10172       bitmap_set_bit (regs, GOT_VERSION_REGNUM);
10173     }
10174 }
10175
10176 /* Implement RETURN_ADDR_RTX.  We do not support moving back to a
10177    previous frame.  */
10178
10179 rtx
10180 mips_return_addr (int count, rtx frame ATTRIBUTE_UNUSED)
10181 {
10182   if (count != 0)
10183     return const0_rtx;
10184
10185   return get_hard_reg_initial_val (Pmode, RETURN_ADDR_REGNUM);
10186 }
10187
10188 /* Emit code to change the current function's return address to
10189    ADDRESS.  SCRATCH is available as a scratch register, if needed.
10190    ADDRESS and SCRATCH are both word-mode GPRs.  */
10191
10192 void
10193 mips_set_return_address (rtx address, rtx scratch)
10194 {
10195   rtx slot_address;
10196
10197   gcc_assert (BITSET_P (cfun->machine->frame.mask, RETURN_ADDR_REGNUM));
10198   slot_address = mips_add_offset (scratch, stack_pointer_rtx,
10199                                   cfun->machine->frame.gp_sp_offset);
10200   mips_emit_move (gen_frame_mem (GET_MODE (address), slot_address), address);
10201 }
10202
10203 /* Return true if the current function has a cprestore slot.  */
10204
10205 bool
10206 mips_cfun_has_cprestore_slot_p (void)
10207 {
10208   return (cfun->machine->global_pointer != INVALID_REGNUM
10209           && cfun->machine->frame.cprestore_size > 0);
10210 }
10211
10212 /* Fill *BASE and *OFFSET such that *BASE + *OFFSET refers to the
10213    cprestore slot.  LOAD_P is true if the caller wants to load from
10214    the cprestore slot; it is false if the caller wants to store to
10215    the slot.  */
10216
10217 static void
10218 mips_get_cprestore_base_and_offset (rtx *base, HOST_WIDE_INT *offset,
10219                                     bool load_p)
10220 {
10221   const struct mips_frame_info *frame;
10222
10223   frame = &cfun->machine->frame;
10224   /* .cprestore always uses the stack pointer instead of the frame pointer.
10225      We have a free choice for direct stores for non-MIPS16 functions,
10226      and for MIPS16 functions whose cprestore slot is in range of the
10227      stack pointer.  Using the stack pointer would sometimes give more
10228      (early) scheduling freedom, but using the frame pointer would
10229      sometimes give more (late) scheduling freedom.  It's hard to
10230      predict which applies to a given function, so let's keep things
10231      simple.
10232
10233      Loads must always use the frame pointer in functions that call
10234      alloca, and there's little benefit to using the stack pointer
10235      otherwise.  */
10236   if (frame_pointer_needed && !(TARGET_CPRESTORE_DIRECTIVE && !load_p))
10237     {
10238       *base = hard_frame_pointer_rtx;
10239       *offset = frame->args_size - frame->hard_frame_pointer_offset;
10240     }
10241   else
10242     {
10243       *base = stack_pointer_rtx;
10244       *offset = frame->args_size;
10245     }
10246 }
10247
10248 /* Return true if X is the load or store address of the cprestore slot;
10249    LOAD_P says which.  */
10250
10251 bool
10252 mips_cprestore_address_p (rtx x, bool load_p)
10253 {
10254   rtx given_base, required_base;
10255   HOST_WIDE_INT given_offset, required_offset;
10256
10257   mips_split_plus (x, &given_base, &given_offset);
10258   mips_get_cprestore_base_and_offset (&required_base, &required_offset, load_p);
10259   return given_base == required_base && given_offset == required_offset;
10260 }
10261
10262 /* Return a MEM rtx for the cprestore slot.  LOAD_P is true if we are
10263    going to load from it, false if we are going to store to it.
10264    Use TEMP as a temporary register if need be.  */
10265
10266 static rtx
10267 mips_cprestore_slot (rtx temp, bool load_p)
10268 {
10269   rtx base;
10270   HOST_WIDE_INT offset;
10271
10272   mips_get_cprestore_base_and_offset (&base, &offset, load_p);
10273   return gen_frame_mem (Pmode, mips_add_offset (temp, base, offset));
10274 }
10275
10276 /* Emit instructions to save global pointer value GP into cprestore
10277    slot MEM.  OFFSET is the offset that MEM applies to the base register.
10278
10279    MEM may not be a legitimate address.  If it isn't, TEMP is a
10280    temporary register that can be used, otherwise it is a SCRATCH.  */
10281
10282 void
10283 mips_save_gp_to_cprestore_slot (rtx mem, rtx offset, rtx gp, rtx temp)
10284 {
10285   if (TARGET_CPRESTORE_DIRECTIVE)
10286     {
10287       gcc_assert (gp == pic_offset_table_rtx);
10288       emit_insn (PMODE_INSN (gen_cprestore, (mem, offset)));
10289     }
10290   else
10291     mips_emit_move (mips_cprestore_slot (temp, false), gp);
10292 }
10293
10294 /* Restore $gp from its save slot, using TEMP as a temporary base register
10295    if need be.  This function is for o32 and o64 abicalls only.
10296
10297    See mips_must_initialize_gp_p for details about how we manage the
10298    global pointer.  */
10299
10300 void
10301 mips_restore_gp_from_cprestore_slot (rtx temp)
10302 {
10303   gcc_assert (TARGET_ABICALLS && TARGET_OLDABI && epilogue_completed);
10304
10305   if (!cfun->machine->must_restore_gp_when_clobbered_p)
10306     {
10307       emit_note (NOTE_INSN_DELETED);
10308       return;
10309     }
10310
10311   if (TARGET_MIPS16)
10312     {
10313       mips_emit_move (temp, mips_cprestore_slot (temp, true));
10314       mips_emit_move (pic_offset_table_rtx, temp);
10315     }
10316   else
10317     mips_emit_move (pic_offset_table_rtx, mips_cprestore_slot (temp, true));
10318   if (!TARGET_EXPLICIT_RELOCS)
10319     emit_insn (gen_blockage ());
10320 }
10321 \f
10322 /* A function to save or store a register.  The first argument is the
10323    register and the second is the stack slot.  */
10324 typedef void (*mips_save_restore_fn) (rtx, rtx);
10325
10326 /* Use FN to save or restore register REGNO.  MODE is the register's
10327    mode and OFFSET is the offset of its save slot from the current
10328    stack pointer.  */
10329
10330 static void
10331 mips_save_restore_reg (enum machine_mode mode, int regno,
10332                        HOST_WIDE_INT offset, mips_save_restore_fn fn)
10333 {
10334   rtx mem;
10335
10336   mem = gen_frame_mem (mode, plus_constant (Pmode, stack_pointer_rtx,
10337                                             offset));
10338   fn (gen_rtx_REG (mode, regno), mem);
10339 }
10340
10341 /* Call FN for each accumlator that is saved by the current function.
10342    SP_OFFSET is the offset of the current stack pointer from the start
10343    of the frame.  */
10344
10345 static void
10346 mips_for_each_saved_acc (HOST_WIDE_INT sp_offset, mips_save_restore_fn fn)
10347 {
10348   HOST_WIDE_INT offset;
10349   int regno;
10350
10351   offset = cfun->machine->frame.acc_sp_offset - sp_offset;
10352   if (BITSET_P (cfun->machine->frame.acc_mask, 0))
10353     {
10354       mips_save_restore_reg (word_mode, LO_REGNUM, offset, fn);
10355       offset -= UNITS_PER_WORD;
10356       mips_save_restore_reg (word_mode, HI_REGNUM, offset, fn);
10357       offset -= UNITS_PER_WORD;
10358     }
10359
10360   for (regno = DSP_ACC_REG_FIRST; regno <= DSP_ACC_REG_LAST; regno++)
10361     if (BITSET_P (cfun->machine->frame.acc_mask,
10362                   ((regno - DSP_ACC_REG_FIRST) / 2) + 1))
10363       {
10364         mips_save_restore_reg (word_mode, regno, offset, fn);
10365         offset -= UNITS_PER_WORD;
10366       }
10367 }
10368
10369 /* Save register REG to MEM.  Make the instruction frame-related.  */
10370
10371 static void
10372 mips_save_reg (rtx reg, rtx mem)
10373 {
10374   if (GET_MODE (reg) == DFmode && !TARGET_FLOAT64)
10375     {
10376       rtx x1, x2;
10377
10378       mips_emit_move_or_split (mem, reg, SPLIT_IF_NECESSARY);
10379
10380       x1 = mips_frame_set (mips_subword (mem, false),
10381                            mips_subword (reg, false));
10382       x2 = mips_frame_set (mips_subword (mem, true),
10383                            mips_subword (reg, true));
10384       mips_set_frame_expr (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, x1, x2)));
10385     }
10386   else
10387     mips_emit_save_slot_move (mem, reg, MIPS_PROLOGUE_TEMP (GET_MODE (reg)));
10388 }
10389
10390 /* Capture the register combinations that are allowed in a SWM or LWM
10391    instruction.  The entries are ordered by number of registers set in
10392    the mask.  We also ignore the single register encodings because a
10393    normal SW/LW is preferred.  */
10394
10395 static const unsigned int umips_swm_mask[17] = {
10396   0xc0ff0000, 0x80ff0000, 0x40ff0000, 0x807f0000,
10397   0x00ff0000, 0x803f0000, 0x007f0000, 0x801f0000,
10398   0x003f0000, 0x800f0000, 0x001f0000, 0x80070000,
10399   0x000f0000, 0x80030000, 0x00070000, 0x80010000,
10400   0x00030000
10401 };
10402
10403 static const unsigned int umips_swm_encoding[17] = {
10404   25, 24, 9, 23, 8, 22, 7, 21, 6, 20, 5, 19, 4, 18, 3, 17, 2
10405 };
10406
10407 /* Try to use a microMIPS LWM or SWM instruction to save or restore
10408    as many GPRs in *MASK as possible.  *OFFSET is the offset from the
10409    stack pointer of the topmost save slot.
10410
10411    Remove from *MASK all registers that were handled using LWM and SWM.
10412    Update *OFFSET so that it points to the first unused save slot.  */
10413
10414 static bool
10415 umips_build_save_restore (mips_save_restore_fn fn,
10416                           unsigned *mask, HOST_WIDE_INT *offset)
10417 {
10418   int nregs;
10419   unsigned int i, j;
10420   rtx pattern, set, reg, mem;
10421   HOST_WIDE_INT this_offset;
10422   rtx this_base;
10423
10424   /* Try matching $16 to $31 (s0 to ra).  */
10425   for (i = 0; i < ARRAY_SIZE (umips_swm_mask); i++)
10426     if ((*mask & 0xffff0000) == umips_swm_mask[i])
10427       break;
10428
10429   if (i == ARRAY_SIZE (umips_swm_mask))
10430     return false;
10431
10432   /* Get the offset of the lowest save slot.  */
10433   nregs = (umips_swm_encoding[i] & 0xf) + (umips_swm_encoding[i] >> 4);
10434   this_offset = *offset - UNITS_PER_WORD * (nregs - 1);
10435
10436   /* LWM/SWM can only support offsets from -2048 to 2047.  */
10437   if (!UMIPS_12BIT_OFFSET_P (this_offset))
10438     return false;
10439
10440   /* Create the final PARALLEL.  */
10441   pattern = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (nregs));
10442   this_base = stack_pointer_rtx;
10443
10444   /* For registers $16-$23 and $30.  */
10445   for (j = 0; j < (umips_swm_encoding[i] & 0xf); j++)
10446     {
10447       HOST_WIDE_INT offset = this_offset + j * UNITS_PER_WORD;
10448       mem = gen_frame_mem (SImode, plus_constant (Pmode, this_base, offset));
10449       unsigned int regno = (j != 8) ? 16 + j : 30;
10450       *mask &= ~(1 << regno);
10451       reg = gen_rtx_REG (SImode, regno);
10452       if (fn == mips_save_reg)
10453         set = mips_frame_set (mem, reg);
10454       else
10455         {
10456           set = gen_rtx_SET (VOIDmode, reg, mem);
10457           mips_add_cfa_restore (reg);
10458         }
10459       XVECEXP (pattern, 0, j) = set;
10460     }
10461
10462   /* For register $31.  */
10463   if (umips_swm_encoding[i] >> 4)
10464     {
10465       HOST_WIDE_INT offset = this_offset + j * UNITS_PER_WORD;
10466       *mask &= ~(1 << 31);
10467       mem = gen_frame_mem (SImode, plus_constant (Pmode, this_base, offset));
10468       reg = gen_rtx_REG (SImode, 31);
10469       if (fn == mips_save_reg)
10470         set = mips_frame_set (mem, reg);
10471       else
10472         {
10473           set = gen_rtx_SET (VOIDmode, reg, mem);
10474           mips_add_cfa_restore (reg);
10475         }
10476       XVECEXP (pattern, 0, j) = set;
10477     }
10478
10479   pattern = emit_insn (pattern);
10480   if (fn == mips_save_reg)
10481     RTX_FRAME_RELATED_P (pattern) = 1;
10482
10483   /* Adjust the last offset.  */
10484   *offset -= UNITS_PER_WORD * nregs;
10485
10486   return true;
10487 }
10488
10489 /* Call FN for each register that is saved by the current function.
10490    SP_OFFSET is the offset of the current stack pointer from the start
10491    of the frame.  */
10492
10493 static void
10494 mips_for_each_saved_gpr_and_fpr (HOST_WIDE_INT sp_offset,
10495                                  mips_save_restore_fn fn)
10496 {
10497   enum machine_mode fpr_mode;
10498   int regno;
10499   const struct mips_frame_info *frame = &cfun->machine->frame;
10500   HOST_WIDE_INT offset;
10501   unsigned int mask;
10502
10503   /* Save registers starting from high to low.  The debuggers prefer at least
10504      the return register be stored at func+4, and also it allows us not to
10505      need a nop in the epilogue if at least one register is reloaded in
10506      addition to return address.  */
10507   offset = frame->gp_sp_offset - sp_offset;
10508   mask = frame->mask;
10509
10510   if (TARGET_MICROMIPS)
10511     umips_build_save_restore (fn, &mask, &offset);
10512
10513   for (regno = GP_REG_LAST; regno >= GP_REG_FIRST; regno--)
10514     if (BITSET_P (mask, regno - GP_REG_FIRST))
10515       {
10516         /* Record the ra offset for use by mips_function_profiler.  */
10517         if (regno == RETURN_ADDR_REGNUM)
10518           cfun->machine->frame.ra_fp_offset = offset + sp_offset;
10519         mips_save_restore_reg (word_mode, regno, offset, fn);
10520         offset -= UNITS_PER_WORD;
10521       }
10522
10523   /* This loop must iterate over the same space as its companion in
10524      mips_compute_frame_info.  */
10525   offset = cfun->machine->frame.fp_sp_offset - sp_offset;
10526   fpr_mode = (TARGET_SINGLE_FLOAT ? SFmode : DFmode);
10527   for (regno = FP_REG_LAST - MAX_FPRS_PER_FMT + 1;
10528        regno >= FP_REG_FIRST;
10529        regno -= MAX_FPRS_PER_FMT)
10530     if (BITSET_P (cfun->machine->frame.fmask, regno - FP_REG_FIRST))
10531       {
10532         mips_save_restore_reg (fpr_mode, regno, offset, fn);
10533         offset -= GET_MODE_SIZE (fpr_mode);
10534       }
10535 }
10536
10537 /* Return true if a move between register REGNO and its save slot (MEM)
10538    can be done in a single move.  LOAD_P is true if we are loading
10539    from the slot, false if we are storing to it.  */
10540
10541 static bool
10542 mips_direct_save_slot_move_p (unsigned int regno, rtx mem, bool load_p)
10543 {
10544   /* There is a specific MIPS16 instruction for saving $31 to the stack.  */
10545   if (TARGET_MIPS16 && !load_p && regno == RETURN_ADDR_REGNUM)
10546     return false;
10547
10548   return mips_secondary_reload_class (REGNO_REG_CLASS (regno),
10549                                       GET_MODE (mem), mem, load_p) == NO_REGS;
10550 }
10551
10552 /* Emit a move from SRC to DEST, given that one of them is a register
10553    save slot and that the other is a register.  TEMP is a temporary
10554    GPR of the same mode that is available if need be.  */
10555
10556 void
10557 mips_emit_save_slot_move (rtx dest, rtx src, rtx temp)
10558 {
10559   unsigned int regno;
10560   rtx mem;
10561
10562   if (REG_P (src))
10563     {
10564       regno = REGNO (src);
10565       mem = dest;
10566     }
10567   else
10568     {
10569       regno = REGNO (dest);
10570       mem = src;
10571     }
10572
10573   if (regno == cfun->machine->global_pointer && !mips_must_initialize_gp_p ())
10574     {
10575       /* We don't yet know whether we'll need this instruction or not.
10576          Postpone the decision by emitting a ghost move.  This move
10577          is specifically not frame-related; only the split version is.  */
10578       if (TARGET_64BIT)
10579         emit_insn (gen_move_gpdi (dest, src));
10580       else
10581         emit_insn (gen_move_gpsi (dest, src));
10582       return;
10583     }
10584
10585   if (regno == HI_REGNUM)
10586     {
10587       if (REG_P (dest))
10588         {
10589           mips_emit_move (temp, src);
10590           if (TARGET_64BIT)
10591             emit_insn (gen_mthisi_di (gen_rtx_REG (TImode, MD_REG_FIRST),
10592                                       temp, gen_rtx_REG (DImode, LO_REGNUM)));
10593           else
10594             emit_insn (gen_mthisi_di (gen_rtx_REG (DImode, MD_REG_FIRST),
10595                                       temp, gen_rtx_REG (SImode, LO_REGNUM)));
10596         }
10597       else
10598         {
10599           if (TARGET_64BIT)
10600             emit_insn (gen_mfhidi_ti (temp,
10601                                       gen_rtx_REG (TImode, MD_REG_FIRST)));
10602           else
10603             emit_insn (gen_mfhisi_di (temp,
10604                                       gen_rtx_REG (DImode, MD_REG_FIRST)));
10605           mips_emit_move (dest, temp);
10606         }
10607     }
10608   else if (mips_direct_save_slot_move_p (regno, mem, mem == src))
10609     mips_emit_move (dest, src);
10610   else
10611     {
10612       gcc_assert (!reg_overlap_mentioned_p (dest, temp));
10613       mips_emit_move (temp, src);
10614       mips_emit_move (dest, temp);
10615     }
10616   if (MEM_P (dest))
10617     mips_set_frame_expr (mips_frame_set (dest, src));
10618 }
10619 \f
10620 /* If we're generating n32 or n64 abicalls, and the current function
10621    does not use $28 as its global pointer, emit a cplocal directive.
10622    Use pic_offset_table_rtx as the argument to the directive.  */
10623
10624 static void
10625 mips_output_cplocal (void)
10626 {
10627   if (!TARGET_EXPLICIT_RELOCS
10628       && mips_must_initialize_gp_p ()
10629       && cfun->machine->global_pointer != GLOBAL_POINTER_REGNUM)
10630     output_asm_insn (".cplocal %+", 0);
10631 }
10632
10633 /* Implement TARGET_OUTPUT_FUNCTION_PROLOGUE.  */
10634
10635 static void
10636 mips_output_function_prologue (FILE *file, HOST_WIDE_INT size ATTRIBUTE_UNUSED)
10637 {
10638   const char *fnname;
10639
10640   /* In MIPS16 mode, we may need to generate a non-MIPS16 stub to handle
10641      floating-point arguments.  */
10642   if (TARGET_MIPS16
10643       && TARGET_HARD_FLOAT_ABI
10644       && crtl->args.info.fp_code != 0)
10645     mips16_build_function_stub ();
10646
10647   /* Get the function name the same way that toplev.c does before calling
10648      assemble_start_function.  This is needed so that the name used here
10649      exactly matches the name used in ASM_DECLARE_FUNCTION_NAME.  */
10650   fnname = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
10651   mips_start_function_definition (fnname, TARGET_MIPS16);
10652
10653   /* Output MIPS-specific frame information.  */
10654   if (!flag_inhibit_size_directive)
10655     {
10656       const struct mips_frame_info *frame;
10657
10658       frame = &cfun->machine->frame;
10659
10660       /* .frame FRAMEREG, FRAMESIZE, RETREG.  */
10661       fprintf (file,
10662                "\t.frame\t%s," HOST_WIDE_INT_PRINT_DEC ",%s\t\t"
10663                "# vars= " HOST_WIDE_INT_PRINT_DEC
10664                ", regs= %d/%d"
10665                ", args= " HOST_WIDE_INT_PRINT_DEC
10666                ", gp= " HOST_WIDE_INT_PRINT_DEC "\n",
10667                reg_names[frame_pointer_needed
10668                          ? HARD_FRAME_POINTER_REGNUM
10669                          : STACK_POINTER_REGNUM],
10670                (frame_pointer_needed
10671                 ? frame->total_size - frame->hard_frame_pointer_offset
10672                 : frame->total_size),
10673                reg_names[RETURN_ADDR_REGNUM],
10674                frame->var_size,
10675                frame->num_gp, frame->num_fp,
10676                frame->args_size,
10677                frame->cprestore_size);
10678
10679       /* .mask MASK, OFFSET.  */
10680       fprintf (file, "\t.mask\t0x%08x," HOST_WIDE_INT_PRINT_DEC "\n",
10681                frame->mask, frame->gp_save_offset);
10682
10683       /* .fmask MASK, OFFSET.  */
10684       fprintf (file, "\t.fmask\t0x%08x," HOST_WIDE_INT_PRINT_DEC "\n",
10685                frame->fmask, frame->fp_save_offset);
10686     }
10687
10688   /* Handle the initialization of $gp for SVR4 PIC, if applicable.
10689      Also emit the ".set noreorder; .set nomacro" sequence for functions
10690      that need it.  */
10691   if (mips_must_initialize_gp_p ()
10692       && mips_current_loadgp_style () == LOADGP_OLDABI)
10693     {
10694       if (TARGET_MIPS16)
10695         {
10696           /* This is a fixed-form sequence.  The position of the
10697              first two instructions is important because of the
10698              way _gp_disp is defined.  */
10699           output_asm_insn ("li\t$2,%%hi(_gp_disp)", 0);
10700           output_asm_insn ("addiu\t$3,$pc,%%lo(_gp_disp)", 0);
10701           output_asm_insn ("sll\t$2,16", 0);
10702           output_asm_insn ("addu\t$2,$3", 0);
10703         }
10704       else
10705         {
10706           /* .cpload must be in a .set noreorder but not a
10707              .set nomacro block.  */
10708           mips_push_asm_switch (&mips_noreorder);
10709           output_asm_insn (".cpload\t%^", 0);
10710           if (!cfun->machine->all_noreorder_p)
10711             mips_pop_asm_switch (&mips_noreorder);
10712           else
10713             mips_push_asm_switch (&mips_nomacro);
10714         }
10715     }
10716   else if (cfun->machine->all_noreorder_p)
10717     {
10718       mips_push_asm_switch (&mips_noreorder);
10719       mips_push_asm_switch (&mips_nomacro);
10720     }
10721
10722   /* Tell the assembler which register we're using as the global
10723      pointer.  This is needed for thunks, since they can use either
10724      explicit relocs or assembler macros.  */
10725   mips_output_cplocal ();
10726 }
10727
10728 /* Implement TARGET_OUTPUT_FUNCTION_EPILOGUE.  */
10729
10730 static void
10731 mips_output_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
10732                                HOST_WIDE_INT size ATTRIBUTE_UNUSED)
10733 {
10734   const char *fnname;
10735
10736   /* Reinstate the normal $gp.  */
10737   SET_REGNO (pic_offset_table_rtx, GLOBAL_POINTER_REGNUM);
10738   mips_output_cplocal ();
10739
10740   if (cfun->machine->all_noreorder_p)
10741     {
10742       mips_pop_asm_switch (&mips_nomacro);
10743       mips_pop_asm_switch (&mips_noreorder);
10744     }
10745
10746   /* Get the function name the same way that toplev.c does before calling
10747      assemble_start_function.  This is needed so that the name used here
10748      exactly matches the name used in ASM_DECLARE_FUNCTION_NAME.  */
10749   fnname = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
10750   mips_end_function_definition (fnname);
10751 }
10752 \f
10753 /* Emit an optimisation barrier for accesses to the current frame.  */
10754
10755 static void
10756 mips_frame_barrier (void)
10757 {
10758   emit_clobber (gen_frame_mem (BLKmode, stack_pointer_rtx));
10759 }
10760
10761
10762 /* The __gnu_local_gp symbol.  */
10763
10764 static GTY(()) rtx mips_gnu_local_gp;
10765
10766 /* If we're generating n32 or n64 abicalls, emit instructions
10767    to set up the global pointer.  */
10768
10769 static void
10770 mips_emit_loadgp (void)
10771 {
10772   rtx addr, offset, incoming_address, base, index, pic_reg;
10773
10774   pic_reg = TARGET_MIPS16 ? MIPS16_PIC_TEMP : pic_offset_table_rtx;
10775   switch (mips_current_loadgp_style ())
10776     {
10777     case LOADGP_ABSOLUTE:
10778       if (mips_gnu_local_gp == NULL)
10779         {
10780           mips_gnu_local_gp = gen_rtx_SYMBOL_REF (Pmode, "__gnu_local_gp");
10781           SYMBOL_REF_FLAGS (mips_gnu_local_gp) |= SYMBOL_FLAG_LOCAL;
10782         }
10783       emit_insn (PMODE_INSN (gen_loadgp_absolute,
10784                              (pic_reg, mips_gnu_local_gp)));
10785       break;
10786
10787     case LOADGP_OLDABI:
10788       /* Added by mips_output_function_prologue.  */
10789       break;
10790
10791     case LOADGP_NEWABI:
10792       addr = XEXP (DECL_RTL (current_function_decl), 0);
10793       offset = mips_unspec_address (addr, SYMBOL_GOTOFF_LOADGP);
10794       incoming_address = gen_rtx_REG (Pmode, PIC_FUNCTION_ADDR_REGNUM);
10795       emit_insn (PMODE_INSN (gen_loadgp_newabi,
10796                              (pic_reg, offset, incoming_address)));
10797       break;
10798
10799     case LOADGP_RTP:
10800       base = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (VXWORKS_GOTT_BASE));
10801       index = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (VXWORKS_GOTT_INDEX));
10802       emit_insn (PMODE_INSN (gen_loadgp_rtp, (pic_reg, base, index)));
10803       break;
10804
10805     default:
10806       return;
10807     }
10808
10809   if (TARGET_MIPS16)
10810     emit_insn (PMODE_INSN (gen_copygp_mips16,
10811                            (pic_offset_table_rtx, pic_reg)));
10812
10813   /* Emit a blockage if there are implicit uses of the GP register.
10814      This includes profiled functions, because FUNCTION_PROFILE uses
10815      a jal macro.  */
10816   if (!TARGET_EXPLICIT_RELOCS || crtl->profile)
10817     emit_insn (gen_loadgp_blockage ());
10818 }
10819
10820 #define PROBE_INTERVAL (1 << STACK_CHECK_PROBE_INTERVAL_EXP)
10821
10822 #if PROBE_INTERVAL > 32768
10823 #error Cannot use indexed addressing mode for stack probing
10824 #endif
10825
10826 /* Emit code to probe a range of stack addresses from FIRST to FIRST+SIZE,
10827    inclusive.  These are offsets from the current stack pointer.  */
10828
10829 static void
10830 mips_emit_probe_stack_range (HOST_WIDE_INT first, HOST_WIDE_INT size)
10831 {
10832   if (TARGET_MIPS16)
10833     sorry ("-fstack-check=specific not implemented for MIPS16");
10834
10835   /* See if we have a constant small number of probes to generate.  If so,
10836      that's the easy case.  */
10837   if (first + size <= 32768)
10838     {
10839       HOST_WIDE_INT i;
10840
10841       /* Probe at FIRST + N * PROBE_INTERVAL for values of N from 1 until
10842          it exceeds SIZE.  If only one probe is needed, this will not
10843          generate any code.  Then probe at FIRST + SIZE.  */
10844       for (i = PROBE_INTERVAL; i < size; i += PROBE_INTERVAL)
10845         emit_stack_probe (plus_constant (Pmode, stack_pointer_rtx,
10846                                          -(first + i)));
10847
10848       emit_stack_probe (plus_constant (Pmode, stack_pointer_rtx,
10849                                        -(first + size)));
10850     }
10851
10852   /* Otherwise, do the same as above, but in a loop.  Note that we must be
10853      extra careful with variables wrapping around because we might be at
10854      the very top (or the very bottom) of the address space and we have
10855      to be able to handle this case properly; in particular, we use an
10856      equality test for the loop condition.  */
10857   else
10858     {
10859       HOST_WIDE_INT rounded_size;
10860       rtx r3 = MIPS_PROLOGUE_TEMP (Pmode);
10861       rtx r12 = MIPS_PROLOGUE_TEMP2 (Pmode);
10862
10863       /* Sanity check for the addressing mode we're going to use.  */
10864       gcc_assert (first <= 32768);
10865
10866
10867       /* Step 1: round SIZE to the previous multiple of the interval.  */
10868
10869       rounded_size = size & -PROBE_INTERVAL;
10870
10871
10872       /* Step 2: compute initial and final value of the loop counter.  */
10873
10874       /* TEST_ADDR = SP + FIRST.  */
10875       emit_insn (gen_rtx_SET (VOIDmode, r3,
10876                               plus_constant (Pmode, stack_pointer_rtx,
10877                                              -first)));
10878
10879       /* LAST_ADDR = SP + FIRST + ROUNDED_SIZE.  */
10880       if (rounded_size > 32768)
10881         {
10882           emit_move_insn (r12, GEN_INT (rounded_size));
10883           emit_insn (gen_rtx_SET (VOIDmode, r12,
10884                                   gen_rtx_MINUS (Pmode, r3, r12)));
10885         }
10886       else
10887         emit_insn (gen_rtx_SET (VOIDmode, r12,
10888                                 plus_constant (Pmode, r3, -rounded_size)));
10889
10890
10891       /* Step 3: the loop
10892
10893         while (TEST_ADDR != LAST_ADDR)
10894           {
10895             TEST_ADDR = TEST_ADDR + PROBE_INTERVAL
10896             probe at TEST_ADDR
10897           }
10898
10899         probes at FIRST + N * PROBE_INTERVAL for values of N from 1
10900         until it is equal to ROUNDED_SIZE.  */
10901
10902       emit_insn (PMODE_INSN (gen_probe_stack_range, (r3, r3, r12)));
10903
10904
10905       /* Step 4: probe at FIRST + SIZE if we cannot assert at compile-time
10906          that SIZE is equal to ROUNDED_SIZE.  */
10907
10908       if (size != rounded_size)
10909         emit_stack_probe (plus_constant (Pmode, r12, rounded_size - size));
10910     }
10911
10912   /* Make sure nothing is scheduled before we are done.  */
10913   emit_insn (gen_blockage ());
10914 }
10915
10916 /* Probe a range of stack addresses from REG1 to REG2 inclusive.  These are
10917    absolute addresses.  */
10918
10919 const char *
10920 mips_output_probe_stack_range (rtx reg1, rtx reg2)
10921 {
10922   static int labelno = 0;
10923   char loop_lab[32], end_lab[32], tmp[64];
10924   rtx xops[2];
10925
10926   ASM_GENERATE_INTERNAL_LABEL (loop_lab, "LPSRL", labelno);
10927   ASM_GENERATE_INTERNAL_LABEL (end_lab, "LPSRE", labelno++);
10928
10929   ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, loop_lab);
10930
10931   /* Jump to END_LAB if TEST_ADDR == LAST_ADDR.  */
10932   xops[0] = reg1;
10933   xops[1] = reg2;
10934   strcpy (tmp, "%(%<beq\t%0,%1,");
10935   output_asm_insn (strcat (tmp, &end_lab[1]), xops);
10936  
10937   /* TEST_ADDR = TEST_ADDR + PROBE_INTERVAL.  */
10938   xops[1] = GEN_INT (-PROBE_INTERVAL);
10939   if (TARGET_64BIT && TARGET_LONG64)
10940     output_asm_insn ("daddiu\t%0,%0,%1", xops);
10941   else
10942     output_asm_insn ("addiu\t%0,%0,%1", xops);
10943
10944   /* Probe at TEST_ADDR and branch.  */
10945   fprintf (asm_out_file, "\tb\t");
10946   assemble_name_raw (asm_out_file, loop_lab);
10947   fputc ('\n', asm_out_file);
10948   if (TARGET_64BIT)
10949     output_asm_insn ("sd\t$0,0(%0)%)", xops);
10950   else
10951     output_asm_insn ("sw\t$0,0(%0)%)", xops);
10952
10953   ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, end_lab);
10954
10955   return "";
10956 }
10957
10958 /* A for_each_rtx callback.  Stop the search if *X is a kernel register.  */
10959
10960 static int
10961 mips_kernel_reg_p (rtx *x, void *data ATTRIBUTE_UNUSED)
10962 {
10963   return REG_P (*x) && KERNEL_REG_P (REGNO (*x));
10964 }
10965
10966 /* Expand the "prologue" pattern.  */
10967
10968 void
10969 mips_expand_prologue (void)
10970 {
10971   const struct mips_frame_info *frame;
10972   HOST_WIDE_INT size;
10973   unsigned int nargs;
10974   rtx insn;
10975
10976   if (cfun->machine->global_pointer != INVALID_REGNUM)
10977     {
10978       /* Check whether an insn uses pic_offset_table_rtx, either explicitly
10979          or implicitly.  If so, we can commit to using a global pointer
10980          straight away, otherwise we need to defer the decision.  */
10981       if (mips_cfun_has_inflexible_gp_ref_p ()
10982           || mips_cfun_has_flexible_gp_ref_p ())
10983         {
10984           cfun->machine->must_initialize_gp_p = true;
10985           cfun->machine->must_restore_gp_when_clobbered_p = true;
10986         }
10987
10988       SET_REGNO (pic_offset_table_rtx, cfun->machine->global_pointer);
10989     }
10990
10991   frame = &cfun->machine->frame;
10992   size = frame->total_size;
10993
10994   if (flag_stack_usage_info)
10995     current_function_static_stack_size = size;
10996
10997   if (flag_stack_check == STATIC_BUILTIN_STACK_CHECK)
10998     {
10999       if (crtl->is_leaf && !cfun->calls_alloca)
11000         {
11001           if (size > PROBE_INTERVAL && size > STACK_CHECK_PROTECT)
11002             mips_emit_probe_stack_range (STACK_CHECK_PROTECT,
11003                                          size - STACK_CHECK_PROTECT);
11004         }
11005       else if (size > 0)
11006         mips_emit_probe_stack_range (STACK_CHECK_PROTECT, size);
11007     }
11008
11009   /* Save the registers.  Allocate up to MIPS_MAX_FIRST_STACK_STEP
11010      bytes beforehand; this is enough to cover the register save area
11011      without going out of range.  */
11012   if (((frame->mask | frame->fmask | frame->acc_mask) != 0)
11013       || frame->num_cop0_regs > 0)
11014     {
11015       HOST_WIDE_INT step1;
11016
11017       step1 = MIN (size, MIPS_MAX_FIRST_STACK_STEP);
11018       if (GENERATE_MIPS16E_SAVE_RESTORE)
11019         {
11020           HOST_WIDE_INT offset;
11021           unsigned int mask, regno;
11022
11023           /* Try to merge argument stores into the save instruction.  */
11024           nargs = mips16e_collect_argument_saves ();
11025
11026           /* Build the save instruction.  */
11027           mask = frame->mask;
11028           insn = mips16e_build_save_restore (false, &mask, &offset,
11029                                              nargs, step1);
11030           RTX_FRAME_RELATED_P (emit_insn (insn)) = 1;
11031           mips_frame_barrier ();
11032           size -= step1;
11033
11034           /* Check if we need to save other registers.  */
11035           for (regno = GP_REG_FIRST; regno < GP_REG_LAST; regno++)
11036             if (BITSET_P (mask, regno - GP_REG_FIRST))
11037               {
11038                 offset -= UNITS_PER_WORD;
11039                 mips_save_restore_reg (word_mode, regno,
11040                                        offset, mips_save_reg);
11041               }
11042         }
11043       else
11044         {
11045           if (cfun->machine->interrupt_handler_p)
11046             {
11047               HOST_WIDE_INT offset;
11048               rtx mem;
11049
11050               /* If this interrupt is using a shadow register set, we need to
11051                  get the stack pointer from the previous register set.  */
11052               if (cfun->machine->use_shadow_register_set_p)
11053                 emit_insn (gen_mips_rdpgpr (stack_pointer_rtx,
11054                                             stack_pointer_rtx));
11055
11056               if (!cfun->machine->keep_interrupts_masked_p)
11057                 {
11058                   /* Move from COP0 Cause to K0.  */
11059                   emit_insn (gen_cop0_move (gen_rtx_REG (SImode, K0_REG_NUM),
11060                                             gen_rtx_REG (SImode,
11061                                                          COP0_CAUSE_REG_NUM)));
11062                   /* Move from COP0 EPC to K1.  */
11063                   emit_insn (gen_cop0_move (gen_rtx_REG (SImode, K1_REG_NUM),
11064                                             gen_rtx_REG (SImode,
11065                                                          COP0_EPC_REG_NUM)));
11066                 }
11067
11068               /* Allocate the first part of the frame.  */
11069               insn = gen_add3_insn (stack_pointer_rtx, stack_pointer_rtx,
11070                                     GEN_INT (-step1));
11071               RTX_FRAME_RELATED_P (emit_insn (insn)) = 1;
11072               mips_frame_barrier ();
11073               size -= step1;
11074
11075               /* Start at the uppermost location for saving.  */
11076               offset = frame->cop0_sp_offset - size;
11077               if (!cfun->machine->keep_interrupts_masked_p)
11078                 {
11079                   /* Push EPC into its stack slot.  */
11080                   mem = gen_frame_mem (word_mode,
11081                                        plus_constant (Pmode, stack_pointer_rtx,
11082                                                       offset));
11083                   mips_emit_move (mem, gen_rtx_REG (word_mode, K1_REG_NUM));
11084                   offset -= UNITS_PER_WORD;
11085                 }
11086
11087               /* Move from COP0 Status to K1.  */
11088               emit_insn (gen_cop0_move (gen_rtx_REG (SImode, K1_REG_NUM),
11089                                         gen_rtx_REG (SImode,
11090                                                      COP0_STATUS_REG_NUM)));
11091
11092               /* Right justify the RIPL in k0.  */
11093               if (!cfun->machine->keep_interrupts_masked_p)
11094                 emit_insn (gen_lshrsi3 (gen_rtx_REG (SImode, K0_REG_NUM),
11095                                         gen_rtx_REG (SImode, K0_REG_NUM),
11096                                         GEN_INT (CAUSE_IPL)));
11097
11098               /* Push Status into its stack slot.  */
11099               mem = gen_frame_mem (word_mode,
11100                                    plus_constant (Pmode, stack_pointer_rtx,
11101                                                   offset));
11102               mips_emit_move (mem, gen_rtx_REG (word_mode, K1_REG_NUM));
11103               offset -= UNITS_PER_WORD;
11104
11105               /* Insert the RIPL into our copy of SR (k1) as the new IPL.  */
11106               if (!cfun->machine->keep_interrupts_masked_p)
11107                 emit_insn (gen_insvsi (gen_rtx_REG (SImode, K1_REG_NUM),
11108                                        GEN_INT (6),
11109                                        GEN_INT (SR_IPL),
11110                                        gen_rtx_REG (SImode, K0_REG_NUM)));
11111
11112               if (!cfun->machine->keep_interrupts_masked_p)
11113                 /* Enable interrupts by clearing the KSU ERL and EXL bits.
11114                    IE is already the correct value, so we don't have to do
11115                    anything explicit.  */
11116                 emit_insn (gen_insvsi (gen_rtx_REG (SImode, K1_REG_NUM),
11117                                        GEN_INT (4),
11118                                        GEN_INT (SR_EXL),
11119                                        gen_rtx_REG (SImode, GP_REG_FIRST)));
11120               else
11121                 /* Disable interrupts by clearing the KSU, ERL, EXL,
11122                    and IE bits.  */
11123                 emit_insn (gen_insvsi (gen_rtx_REG (SImode, K1_REG_NUM),
11124                                        GEN_INT (5),
11125                                        GEN_INT (SR_IE),
11126                                        gen_rtx_REG (SImode, GP_REG_FIRST)));
11127             }
11128           else
11129             {
11130               insn = gen_add3_insn (stack_pointer_rtx,
11131                                     stack_pointer_rtx,
11132                                     GEN_INT (-step1));
11133               RTX_FRAME_RELATED_P (emit_insn (insn)) = 1;
11134               mips_frame_barrier ();
11135               size -= step1;
11136             }
11137           mips_for_each_saved_acc (size, mips_save_reg);
11138           mips_for_each_saved_gpr_and_fpr (size, mips_save_reg);
11139         }
11140     }
11141
11142   /* Allocate the rest of the frame.  */
11143   if (size > 0)
11144     {
11145       if (SMALL_OPERAND (-size))
11146         RTX_FRAME_RELATED_P (emit_insn (gen_add3_insn (stack_pointer_rtx,
11147                                                        stack_pointer_rtx,
11148                                                        GEN_INT (-size)))) = 1;
11149       else
11150         {
11151           mips_emit_move (MIPS_PROLOGUE_TEMP (Pmode), GEN_INT (size));
11152           if (TARGET_MIPS16)
11153             {
11154               /* There are no instructions to add or subtract registers
11155                  from the stack pointer, so use the frame pointer as a
11156                  temporary.  We should always be using a frame pointer
11157                  in this case anyway.  */
11158               gcc_assert (frame_pointer_needed);
11159               mips_emit_move (hard_frame_pointer_rtx, stack_pointer_rtx);
11160               emit_insn (gen_sub3_insn (hard_frame_pointer_rtx,
11161                                         hard_frame_pointer_rtx,
11162                                         MIPS_PROLOGUE_TEMP (Pmode)));
11163               mips_emit_move (stack_pointer_rtx, hard_frame_pointer_rtx);
11164             }
11165           else
11166             emit_insn (gen_sub3_insn (stack_pointer_rtx,
11167                                       stack_pointer_rtx,
11168                                       MIPS_PROLOGUE_TEMP (Pmode)));
11169
11170           /* Describe the combined effect of the previous instructions.  */
11171           mips_set_frame_expr
11172             (gen_rtx_SET (VOIDmode, stack_pointer_rtx,
11173                           plus_constant (Pmode, stack_pointer_rtx, -size)));
11174         }
11175       mips_frame_barrier ();
11176     }
11177
11178   /* Set up the frame pointer, if we're using one.  */
11179   if (frame_pointer_needed)
11180     {
11181       HOST_WIDE_INT offset;
11182
11183       offset = frame->hard_frame_pointer_offset;
11184       if (offset == 0)
11185         {
11186           insn = mips_emit_move (hard_frame_pointer_rtx, stack_pointer_rtx);
11187           RTX_FRAME_RELATED_P (insn) = 1;
11188         }
11189       else if (SMALL_OPERAND (offset))
11190         {
11191           insn = gen_add3_insn (hard_frame_pointer_rtx,
11192                                 stack_pointer_rtx, GEN_INT (offset));
11193           RTX_FRAME_RELATED_P (emit_insn (insn)) = 1;
11194         }
11195       else
11196         {
11197           mips_emit_move (MIPS_PROLOGUE_TEMP (Pmode), GEN_INT (offset));
11198           mips_emit_move (hard_frame_pointer_rtx, stack_pointer_rtx);
11199           emit_insn (gen_add3_insn (hard_frame_pointer_rtx,
11200                                     hard_frame_pointer_rtx,
11201                                     MIPS_PROLOGUE_TEMP (Pmode)));
11202           mips_set_frame_expr
11203             (gen_rtx_SET (VOIDmode, hard_frame_pointer_rtx,
11204                           plus_constant (Pmode, stack_pointer_rtx, offset)));
11205         }
11206     }
11207
11208   mips_emit_loadgp ();
11209
11210   /* Initialize the $gp save slot.  */
11211   if (mips_cfun_has_cprestore_slot_p ())
11212     {
11213       rtx base, mem, gp, temp;
11214       HOST_WIDE_INT offset;
11215
11216       mips_get_cprestore_base_and_offset (&base, &offset, false);
11217       mem = gen_frame_mem (Pmode, plus_constant (Pmode, base, offset));
11218       gp = TARGET_MIPS16 ? MIPS16_PIC_TEMP : pic_offset_table_rtx;
11219       temp = (SMALL_OPERAND (offset)
11220               ? gen_rtx_SCRATCH (Pmode)
11221               : MIPS_PROLOGUE_TEMP (Pmode));
11222       emit_insn (PMODE_INSN (gen_potential_cprestore,
11223                              (mem, GEN_INT (offset), gp, temp)));
11224
11225       mips_get_cprestore_base_and_offset (&base, &offset, true);
11226       mem = gen_frame_mem (Pmode, plus_constant (Pmode, base, offset));
11227       emit_insn (PMODE_INSN (gen_use_cprestore, (mem)));
11228     }
11229
11230   /* We need to search back to the last use of K0 or K1.  */
11231   if (cfun->machine->interrupt_handler_p)
11232     {
11233       for (insn = get_last_insn (); insn != NULL_RTX; insn = PREV_INSN (insn))
11234         if (INSN_P (insn)
11235             && for_each_rtx (&PATTERN (insn), mips_kernel_reg_p, NULL))
11236           break;
11237       /* Emit a move from K1 to COP0 Status after insn.  */
11238       gcc_assert (insn != NULL_RTX);
11239       emit_insn_after (gen_cop0_move (gen_rtx_REG (SImode, COP0_STATUS_REG_NUM),
11240                                       gen_rtx_REG (SImode, K1_REG_NUM)),
11241                        insn);
11242     }
11243
11244   /* If we are profiling, make sure no instructions are scheduled before
11245      the call to mcount.  */
11246   if (crtl->profile)
11247     emit_insn (gen_blockage ());
11248 }
11249 \f
11250 /* Attach all pending register saves to the previous instruction.
11251    Return that instruction.  */
11252
11253 static rtx
11254 mips_epilogue_emit_cfa_restores (void)
11255 {
11256   rtx insn;
11257
11258   insn = get_last_insn ();
11259   gcc_assert (insn && !REG_NOTES (insn));
11260   if (mips_epilogue.cfa_restores)
11261     {
11262       RTX_FRAME_RELATED_P (insn) = 1;
11263       REG_NOTES (insn) = mips_epilogue.cfa_restores;
11264       mips_epilogue.cfa_restores = 0;
11265     }
11266   return insn;
11267 }
11268
11269 /* Like mips_epilogue_emit_cfa_restores, but also record that the CFA is
11270    now at REG + OFFSET.  */
11271
11272 static void
11273 mips_epilogue_set_cfa (rtx reg, HOST_WIDE_INT offset)
11274 {
11275   rtx insn;
11276
11277   insn = mips_epilogue_emit_cfa_restores ();
11278   if (reg != mips_epilogue.cfa_reg || offset != mips_epilogue.cfa_offset)
11279     {
11280       RTX_FRAME_RELATED_P (insn) = 1;
11281       REG_NOTES (insn) = alloc_reg_note (REG_CFA_DEF_CFA,
11282                                          plus_constant (Pmode, reg, offset),
11283                                          REG_NOTES (insn));
11284       mips_epilogue.cfa_reg = reg;
11285       mips_epilogue.cfa_offset = offset;
11286     }
11287 }
11288
11289 /* Emit instructions to restore register REG from slot MEM.  Also update
11290    the cfa_restores list.  */
11291
11292 static void
11293 mips_restore_reg (rtx reg, rtx mem)
11294 {
11295   /* There's no MIPS16 instruction to load $31 directly.  Load into
11296      $7 instead and adjust the return insn appropriately.  */
11297   if (TARGET_MIPS16 && REGNO (reg) == RETURN_ADDR_REGNUM)
11298     reg = gen_rtx_REG (GET_MODE (reg), GP_REG_FIRST + 7);
11299   else if (GET_MODE (reg) == DFmode && !TARGET_FLOAT64)
11300     {
11301       mips_add_cfa_restore (mips_subword (reg, true));
11302       mips_add_cfa_restore (mips_subword (reg, false));
11303     }
11304   else
11305     mips_add_cfa_restore (reg);
11306
11307   mips_emit_save_slot_move (reg, mem, MIPS_EPILOGUE_TEMP (GET_MODE (reg)));
11308   if (REGNO (reg) == REGNO (mips_epilogue.cfa_reg))
11309     /* The CFA is currently defined in terms of the register whose
11310        value we have just restored.  Redefine the CFA in terms of
11311        the stack pointer.  */
11312     mips_epilogue_set_cfa (stack_pointer_rtx,
11313                            mips_epilogue.cfa_restore_sp_offset);
11314 }
11315
11316 /* Emit code to set the stack pointer to BASE + OFFSET, given that
11317    BASE + OFFSET is NEW_FRAME_SIZE bytes below the top of the frame.
11318    BASE, if not the stack pointer, is available as a temporary.  */
11319
11320 static void
11321 mips_deallocate_stack (rtx base, rtx offset, HOST_WIDE_INT new_frame_size)
11322 {
11323   if (base == stack_pointer_rtx && offset == const0_rtx)
11324     return;
11325
11326   mips_frame_barrier ();
11327   if (offset == const0_rtx)
11328     {
11329       emit_move_insn (stack_pointer_rtx, base);
11330       mips_epilogue_set_cfa (stack_pointer_rtx, new_frame_size);
11331     }
11332   else if (TARGET_MIPS16 && base != stack_pointer_rtx)
11333     {
11334       emit_insn (gen_add3_insn (base, base, offset));
11335       mips_epilogue_set_cfa (base, new_frame_size);
11336       emit_move_insn (stack_pointer_rtx, base);
11337     }
11338   else
11339     {
11340       emit_insn (gen_add3_insn (stack_pointer_rtx, base, offset));
11341       mips_epilogue_set_cfa (stack_pointer_rtx, new_frame_size);
11342     }
11343 }
11344
11345 /* Emit any instructions needed before a return.  */
11346
11347 void
11348 mips_expand_before_return (void)
11349 {
11350   /* When using a call-clobbered gp, we start out with unified call
11351      insns that include instructions to restore the gp.  We then split
11352      these unified calls after reload.  These split calls explicitly
11353      clobber gp, so there is no need to define
11354      PIC_OFFSET_TABLE_REG_CALL_CLOBBERED.
11355
11356      For consistency, we should also insert an explicit clobber of $28
11357      before return insns, so that the post-reload optimizers know that
11358      the register is not live on exit.  */
11359   if (TARGET_CALL_CLOBBERED_GP)
11360     emit_clobber (pic_offset_table_rtx);
11361 }
11362
11363 /* Expand an "epilogue" or "sibcall_epilogue" pattern; SIBCALL_P
11364    says which.  */
11365
11366 void
11367 mips_expand_epilogue (bool sibcall_p)
11368 {
11369   const struct mips_frame_info *frame;
11370   HOST_WIDE_INT step1, step2;
11371   rtx base, adjust, insn;
11372   bool use_jraddiusp_p = false;
11373
11374   if (!sibcall_p && mips_can_use_return_insn ())
11375     {
11376       emit_jump_insn (gen_return ());
11377       return;
11378     }
11379
11380   /* In MIPS16 mode, if the return value should go into a floating-point
11381      register, we need to call a helper routine to copy it over.  */
11382   if (mips16_cfun_returns_in_fpr_p ())
11383     mips16_copy_fpr_return_value ();
11384
11385   /* Split the frame into two.  STEP1 is the amount of stack we should
11386      deallocate before restoring the registers.  STEP2 is the amount we
11387      should deallocate afterwards.
11388
11389      Start off by assuming that no registers need to be restored.  */
11390   frame = &cfun->machine->frame;
11391   step1 = frame->total_size;
11392   step2 = 0;
11393
11394   /* Work out which register holds the frame address.  */
11395   if (!frame_pointer_needed)
11396     base = stack_pointer_rtx;
11397   else
11398     {
11399       base = hard_frame_pointer_rtx;
11400       step1 -= frame->hard_frame_pointer_offset;
11401     }
11402   mips_epilogue.cfa_reg = base;
11403   mips_epilogue.cfa_offset = step1;
11404   mips_epilogue.cfa_restores = NULL_RTX;
11405
11406   /* If we need to restore registers, deallocate as much stack as
11407      possible in the second step without going out of range.  */
11408   if ((frame->mask | frame->fmask | frame->acc_mask) != 0
11409       || frame->num_cop0_regs > 0)
11410     {
11411       step2 = MIN (step1, MIPS_MAX_FIRST_STACK_STEP);
11412       step1 -= step2;
11413     }
11414
11415   /* Get an rtx for STEP1 that we can add to BASE.  */
11416   adjust = GEN_INT (step1);
11417   if (!SMALL_OPERAND (step1))
11418     {
11419       mips_emit_move (MIPS_EPILOGUE_TEMP (Pmode), adjust);
11420       adjust = MIPS_EPILOGUE_TEMP (Pmode);
11421     }
11422   mips_deallocate_stack (base, adjust, step2);
11423
11424   /* If we're using addressing macros, $gp is implicitly used by all
11425      SYMBOL_REFs.  We must emit a blockage insn before restoring $gp
11426      from the stack.  */
11427   if (TARGET_CALL_SAVED_GP && !TARGET_EXPLICIT_RELOCS)
11428     emit_insn (gen_blockage ());
11429
11430   mips_epilogue.cfa_restore_sp_offset = step2;
11431   if (GENERATE_MIPS16E_SAVE_RESTORE && frame->mask != 0)
11432     {
11433       unsigned int regno, mask;
11434       HOST_WIDE_INT offset;
11435       rtx restore;
11436
11437       /* Generate the restore instruction.  */
11438       mask = frame->mask;
11439       restore = mips16e_build_save_restore (true, &mask, &offset, 0, step2);
11440
11441       /* Restore any other registers manually.  */
11442       for (regno = GP_REG_FIRST; regno < GP_REG_LAST; regno++)
11443         if (BITSET_P (mask, regno - GP_REG_FIRST))
11444           {
11445             offset -= UNITS_PER_WORD;
11446             mips_save_restore_reg (word_mode, regno, offset, mips_restore_reg);
11447           }
11448
11449       /* Restore the remaining registers and deallocate the final bit
11450          of the frame.  */
11451       mips_frame_barrier ();
11452       emit_insn (restore);
11453       mips_epilogue_set_cfa (stack_pointer_rtx, 0);
11454     }
11455   else
11456     {
11457       /* Restore the registers.  */
11458       mips_for_each_saved_acc (frame->total_size - step2, mips_restore_reg);
11459       mips_for_each_saved_gpr_and_fpr (frame->total_size - step2,
11460                                        mips_restore_reg);
11461
11462       if (cfun->machine->interrupt_handler_p)
11463         {
11464           HOST_WIDE_INT offset;
11465           rtx mem;
11466
11467           offset = frame->cop0_sp_offset - (frame->total_size - step2);
11468           if (!cfun->machine->keep_interrupts_masked_p)
11469             {
11470               /* Restore the original EPC.  */
11471               mem = gen_frame_mem (word_mode,
11472                                    plus_constant (Pmode, stack_pointer_rtx,
11473                                                   offset));
11474               mips_emit_move (gen_rtx_REG (word_mode, K0_REG_NUM), mem);
11475               offset -= UNITS_PER_WORD;
11476
11477               /* Move to COP0 EPC.  */
11478               emit_insn (gen_cop0_move (gen_rtx_REG (SImode, COP0_EPC_REG_NUM),
11479                                         gen_rtx_REG (SImode, K0_REG_NUM)));
11480             }
11481
11482           /* Restore the original Status.  */
11483           mem = gen_frame_mem (word_mode,
11484                                plus_constant (Pmode, stack_pointer_rtx,
11485                                               offset));
11486           mips_emit_move (gen_rtx_REG (word_mode, K0_REG_NUM), mem);
11487           offset -= UNITS_PER_WORD;
11488
11489           /* If we don't use shadow register set, we need to update SP.  */
11490           if (!cfun->machine->use_shadow_register_set_p)
11491             mips_deallocate_stack (stack_pointer_rtx, GEN_INT (step2), 0);
11492           else
11493             /* The choice of position is somewhat arbitrary in this case.  */
11494             mips_epilogue_emit_cfa_restores ();
11495
11496           /* Move to COP0 Status.  */
11497           emit_insn (gen_cop0_move (gen_rtx_REG (SImode, COP0_STATUS_REG_NUM),
11498                                     gen_rtx_REG (SImode, K0_REG_NUM)));
11499         }
11500       else if (TARGET_MICROMIPS
11501                && !crtl->calls_eh_return
11502                && !sibcall_p
11503                && step2 > 0
11504                && mips_unsigned_immediate_p (step2, 5, 2))
11505         use_jraddiusp_p = true;
11506       else
11507         /* Deallocate the final bit of the frame.  */
11508         mips_deallocate_stack (stack_pointer_rtx, GEN_INT (step2), 0);
11509     }
11510
11511   if (!use_jraddiusp_p)
11512     gcc_assert (!mips_epilogue.cfa_restores);
11513
11514   /* Add in the __builtin_eh_return stack adjustment.  We need to
11515      use a temporary in MIPS16 code.  */
11516   if (crtl->calls_eh_return)
11517     {
11518       if (TARGET_MIPS16)
11519         {
11520           mips_emit_move (MIPS_EPILOGUE_TEMP (Pmode), stack_pointer_rtx);
11521           emit_insn (gen_add3_insn (MIPS_EPILOGUE_TEMP (Pmode),
11522                                     MIPS_EPILOGUE_TEMP (Pmode),
11523                                     EH_RETURN_STACKADJ_RTX));
11524           mips_emit_move (stack_pointer_rtx, MIPS_EPILOGUE_TEMP (Pmode));
11525         }
11526       else
11527         emit_insn (gen_add3_insn (stack_pointer_rtx,
11528                                   stack_pointer_rtx,
11529                                   EH_RETURN_STACKADJ_RTX));
11530     }
11531
11532   if (!sibcall_p)
11533     {
11534       mips_expand_before_return ();
11535       if (cfun->machine->interrupt_handler_p)
11536         {
11537           /* Interrupt handlers generate eret or deret.  */
11538           if (cfun->machine->use_debug_exception_return_p)
11539             emit_jump_insn (gen_mips_deret ());
11540           else
11541             emit_jump_insn (gen_mips_eret ());
11542         }
11543       else
11544         {
11545           rtx pat;
11546
11547           /* When generating MIPS16 code, the normal
11548              mips_for_each_saved_gpr_and_fpr path will restore the return
11549              address into $7 rather than $31.  */
11550           if (TARGET_MIPS16
11551               && !GENERATE_MIPS16E_SAVE_RESTORE
11552               && BITSET_P (frame->mask, RETURN_ADDR_REGNUM))
11553             {
11554               /* simple_returns cannot rely on values that are only available
11555                  on paths through the epilogue (because return paths that do
11556                  not pass through the epilogue may nevertheless reuse a
11557                  simple_return that occurs at the end of the epilogue).
11558                  Use a normal return here instead.  */
11559               rtx reg = gen_rtx_REG (Pmode, GP_REG_FIRST + 7);
11560               pat = gen_return_internal (reg);
11561             }
11562           else if (use_jraddiusp_p)
11563             pat = gen_jraddiusp (GEN_INT (step2));
11564           else
11565             {
11566               rtx reg = gen_rtx_REG (Pmode, RETURN_ADDR_REGNUM);
11567               pat = gen_simple_return_internal (reg);
11568             }
11569           emit_jump_insn (pat);
11570           if (use_jraddiusp_p)
11571             mips_epilogue_set_cfa (stack_pointer_rtx, step2);
11572         }
11573     }
11574
11575   /* Search from the beginning to the first use of K0 or K1.  */
11576   if (cfun->machine->interrupt_handler_p
11577       && !cfun->machine->keep_interrupts_masked_p)
11578     {
11579       for (insn = get_insns (); insn != NULL_RTX; insn = NEXT_INSN (insn))
11580         if (INSN_P (insn)
11581             && for_each_rtx (&PATTERN(insn), mips_kernel_reg_p, NULL))
11582           break;
11583       gcc_assert (insn != NULL_RTX);
11584       /* Insert disable interrupts before the first use of K0 or K1.  */
11585       emit_insn_before (gen_mips_di (), insn);
11586       emit_insn_before (gen_mips_ehb (), insn);
11587     }
11588 }
11589 \f
11590 /* Return nonzero if this function is known to have a null epilogue.
11591    This allows the optimizer to omit jumps to jumps if no stack
11592    was created.  */
11593
11594 bool
11595 mips_can_use_return_insn (void)
11596 {
11597   /* Interrupt handlers need to go through the epilogue.  */
11598   if (cfun->machine->interrupt_handler_p)
11599     return false;
11600
11601   if (!reload_completed)
11602     return false;
11603
11604   if (crtl->profile)
11605     return false;
11606
11607   /* In MIPS16 mode, a function that returns a floating-point value
11608      needs to arrange to copy the return value into the floating-point
11609      registers.  */
11610   if (mips16_cfun_returns_in_fpr_p ())
11611     return false;
11612
11613   return cfun->machine->frame.total_size == 0;
11614 }
11615 \f
11616 /* Return true if register REGNO can store a value of mode MODE.
11617    The result of this function is cached in mips_hard_regno_mode_ok.  */
11618
11619 static bool
11620 mips_hard_regno_mode_ok_p (unsigned int regno, enum machine_mode mode)
11621 {
11622   unsigned int size;
11623   enum mode_class mclass;
11624
11625   if (mode == CCV2mode)
11626     return (ISA_HAS_8CC
11627             && ST_REG_P (regno)
11628             && (regno - ST_REG_FIRST) % 2 == 0);
11629
11630   if (mode == CCV4mode)
11631     return (ISA_HAS_8CC
11632             && ST_REG_P (regno)
11633             && (regno - ST_REG_FIRST) % 4 == 0);
11634
11635   if (mode == CCmode)
11636     return ISA_HAS_8CC ? ST_REG_P (regno) : regno == FPSW_REGNUM;
11637
11638   size = GET_MODE_SIZE (mode);
11639   mclass = GET_MODE_CLASS (mode);
11640
11641   if (GP_REG_P (regno))
11642     return ((regno - GP_REG_FIRST) & 1) == 0 || size <= UNITS_PER_WORD;
11643
11644   if (FP_REG_P (regno)
11645       && (((regno - FP_REG_FIRST) % MAX_FPRS_PER_FMT) == 0
11646           || (MIN_FPRS_PER_FMT == 1 && size <= UNITS_PER_FPREG)))
11647     {
11648       /* Allow 64-bit vector modes for Loongson-2E/2F.  */
11649       if (TARGET_LOONGSON_VECTORS
11650           && (mode == V2SImode
11651               || mode == V4HImode
11652               || mode == V8QImode
11653               || mode == DImode))
11654         return true;
11655
11656       if (mclass == MODE_FLOAT
11657           || mclass == MODE_COMPLEX_FLOAT
11658           || mclass == MODE_VECTOR_FLOAT)
11659         return size <= UNITS_PER_FPVALUE;
11660
11661       /* Allow integer modes that fit into a single register.  We need
11662          to put integers into FPRs when using instructions like CVT
11663          and TRUNC.  There's no point allowing sizes smaller than a word,
11664          because the FPU has no appropriate load/store instructions.  */
11665       if (mclass == MODE_INT)
11666         return size >= MIN_UNITS_PER_WORD && size <= UNITS_PER_FPREG;
11667     }
11668
11669   if (ACC_REG_P (regno)
11670       && (INTEGRAL_MODE_P (mode) || ALL_FIXED_POINT_MODE_P (mode)))
11671     {
11672       if (MD_REG_P (regno))
11673         {
11674           /* After a multiplication or division, clobbering HI makes
11675              the value of LO unpredictable, and vice versa.  This means
11676              that, for all interesting cases, HI and LO are effectively
11677              a single register.
11678
11679              We model this by requiring that any value that uses HI
11680              also uses LO.  */
11681           if (size <= UNITS_PER_WORD * 2)
11682             return regno == (size <= UNITS_PER_WORD ? LO_REGNUM : MD_REG_FIRST);
11683         }
11684       else
11685         {
11686           /* DSP accumulators do not have the same restrictions as
11687              HI and LO, so we can treat them as normal doubleword
11688              registers.  */
11689           if (size <= UNITS_PER_WORD)
11690             return true;
11691
11692           if (size <= UNITS_PER_WORD * 2
11693               && ((regno - DSP_ACC_REG_FIRST) & 1) == 0)
11694             return true;
11695         }
11696     }
11697
11698   if (ALL_COP_REG_P (regno))
11699     return mclass == MODE_INT && size <= UNITS_PER_WORD;
11700
11701   if (regno == GOT_VERSION_REGNUM)
11702     return mode == SImode;
11703
11704   return false;
11705 }
11706
11707 /* Implement HARD_REGNO_NREGS.  */
11708
11709 unsigned int
11710 mips_hard_regno_nregs (int regno, enum machine_mode mode)
11711 {
11712   if (ST_REG_P (regno))
11713     /* The size of FP status registers is always 4, because they only hold
11714        CCmode values, and CCmode is always considered to be 4 bytes wide.  */
11715     return (GET_MODE_SIZE (mode) + 3) / 4;
11716
11717   if (FP_REG_P (regno))
11718     return (GET_MODE_SIZE (mode) + UNITS_PER_FPREG - 1) / UNITS_PER_FPREG;
11719
11720   /* All other registers are word-sized.  */
11721   return (GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
11722 }
11723
11724 /* Implement CLASS_MAX_NREGS, taking the maximum of the cases
11725    in mips_hard_regno_nregs.  */
11726
11727 int
11728 mips_class_max_nregs (enum reg_class rclass, enum machine_mode mode)
11729 {
11730   int size;
11731   HARD_REG_SET left;
11732
11733   size = 0x8000;
11734   COPY_HARD_REG_SET (left, reg_class_contents[(int) rclass]);
11735   if (hard_reg_set_intersect_p (left, reg_class_contents[(int) ST_REGS]))
11736     {
11737       if (HARD_REGNO_MODE_OK (ST_REG_FIRST, mode))
11738         size = MIN (size, 4);
11739       AND_COMPL_HARD_REG_SET (left, reg_class_contents[(int) ST_REGS]);
11740     }
11741   if (hard_reg_set_intersect_p (left, reg_class_contents[(int) FP_REGS]))
11742     {
11743       if (HARD_REGNO_MODE_OK (FP_REG_FIRST, mode))
11744         size = MIN (size, UNITS_PER_FPREG);
11745       AND_COMPL_HARD_REG_SET (left, reg_class_contents[(int) FP_REGS]);
11746     }
11747   if (!hard_reg_set_empty_p (left))
11748     size = MIN (size, UNITS_PER_WORD);
11749   return (GET_MODE_SIZE (mode) + size - 1) / size;
11750 }
11751
11752 /* Implement CANNOT_CHANGE_MODE_CLASS.  */
11753
11754 bool
11755 mips_cannot_change_mode_class (enum machine_mode from,
11756                                enum machine_mode to,
11757                                enum reg_class rclass)
11758 {
11759   /* Allow conversions between different Loongson integer vectors,
11760      and between those vectors and DImode.  */
11761   if (GET_MODE_SIZE (from) == 8 && GET_MODE_SIZE (to) == 8
11762       && INTEGRAL_MODE_P (from) && INTEGRAL_MODE_P (to))
11763     return false;
11764
11765   /* Otherwise, there are several problems with changing the modes of
11766      values in floating-point registers:
11767
11768      - When a multi-word value is stored in paired floating-point
11769        registers, the first register always holds the low word.  We
11770        therefore can't allow FPRs to change between single-word and
11771        multi-word modes on big-endian targets.
11772
11773      - GCC assumes that each word of a multiword register can be
11774        accessed individually using SUBREGs.  This is not true for
11775        floating-point registers if they are bigger than a word.
11776
11777      - Loading a 32-bit value into a 64-bit floating-point register
11778        will not sign-extend the value, despite what LOAD_EXTEND_OP
11779        says.  We can't allow FPRs to change from SImode to a wider
11780        mode on 64-bit targets.
11781
11782      - If the FPU has already interpreted a value in one format, we
11783        must not ask it to treat the value as having a different
11784        format.
11785
11786      We therefore disallow all mode changes involving FPRs.  */
11787
11788   return reg_classes_intersect_p (FP_REGS, rclass);
11789 }
11790
11791 /* Implement target hook small_register_classes_for_mode_p.  */
11792
11793 static bool
11794 mips_small_register_classes_for_mode_p (enum machine_mode mode
11795                                         ATTRIBUTE_UNUSED)
11796 {
11797   return TARGET_MIPS16;
11798 }
11799
11800 /* Return true if moves in mode MODE can use the FPU's mov.fmt instruction.  */
11801
11802 static bool
11803 mips_mode_ok_for_mov_fmt_p (enum machine_mode mode)
11804 {
11805   switch (mode)
11806     {
11807     case SFmode:
11808       return TARGET_HARD_FLOAT;
11809
11810     case DFmode:
11811       return TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT;
11812
11813     case V2SFmode:
11814       return TARGET_HARD_FLOAT && TARGET_PAIRED_SINGLE_FLOAT;
11815
11816     default:
11817       return false;
11818     }
11819 }
11820
11821 /* Implement MODES_TIEABLE_P.  */
11822
11823 bool
11824 mips_modes_tieable_p (enum machine_mode mode1, enum machine_mode mode2)
11825 {
11826   /* FPRs allow no mode punning, so it's not worth tying modes if we'd
11827      prefer to put one of them in FPRs.  */
11828   return (mode1 == mode2
11829           || (!mips_mode_ok_for_mov_fmt_p (mode1)
11830               && !mips_mode_ok_for_mov_fmt_p (mode2)));
11831 }
11832
11833 /* Implement TARGET_PREFERRED_RELOAD_CLASS.  */
11834
11835 static reg_class_t
11836 mips_preferred_reload_class (rtx x, reg_class_t rclass)
11837 {
11838   if (mips_dangerous_for_la25_p (x) && reg_class_subset_p (LEA_REGS, rclass))
11839     return LEA_REGS;
11840
11841   if (reg_class_subset_p (FP_REGS, rclass)
11842       && mips_mode_ok_for_mov_fmt_p (GET_MODE (x)))
11843     return FP_REGS;
11844
11845   if (reg_class_subset_p (GR_REGS, rclass))
11846     rclass = GR_REGS;
11847
11848   if (TARGET_MIPS16 && reg_class_subset_p (M16_REGS, rclass))
11849     rclass = M16_REGS;
11850
11851   return rclass;
11852 }
11853
11854 /* RCLASS is a class involved in a REGISTER_MOVE_COST calculation.
11855    Return a "canonical" class to represent it in later calculations.  */
11856
11857 static reg_class_t
11858 mips_canonicalize_move_class (reg_class_t rclass)
11859 {
11860   /* All moves involving accumulator registers have the same cost.  */
11861   if (reg_class_subset_p (rclass, ACC_REGS))
11862     rclass = ACC_REGS;
11863
11864   /* Likewise promote subclasses of general registers to the most
11865      interesting containing class.  */
11866   if (TARGET_MIPS16 && reg_class_subset_p (rclass, M16_REGS))
11867     rclass = M16_REGS;
11868   else if (reg_class_subset_p (rclass, GENERAL_REGS))
11869     rclass = GENERAL_REGS;
11870
11871   return rclass;
11872 }
11873
11874 /* Return the cost of moving a value of mode MODE from a register of
11875    class FROM to a GPR.  Return 0 for classes that are unions of other
11876    classes handled by this function.  */
11877
11878 static int
11879 mips_move_to_gpr_cost (enum machine_mode mode ATTRIBUTE_UNUSED,
11880                        reg_class_t from)
11881 {
11882   switch (from)
11883     {
11884     case GENERAL_REGS:
11885       /* A MIPS16 MOVE instruction, or a non-MIPS16 MOVE macro.  */
11886       return 2;
11887
11888     case ACC_REGS:
11889       /* MFLO and MFHI.  */
11890       return 6;
11891
11892     case FP_REGS:
11893       /* MFC1, etc.  */
11894       return 4;
11895
11896     case ST_REGS:
11897       /* LUI followed by MOVF.  */
11898       return 4;
11899
11900     case COP0_REGS:
11901     case COP2_REGS:
11902     case COP3_REGS:
11903       /* This choice of value is historical.  */
11904       return 5;
11905
11906     default:
11907       return 0;
11908     }
11909 }
11910
11911 /* Return the cost of moving a value of mode MODE from a GPR to a
11912    register of class TO.  Return 0 for classes that are unions of
11913    other classes handled by this function.  */
11914
11915 static int
11916 mips_move_from_gpr_cost (enum machine_mode mode, reg_class_t to)
11917 {
11918   switch (to)
11919     {
11920     case GENERAL_REGS:
11921       /* A MIPS16 MOVE instruction, or a non-MIPS16 MOVE macro.  */
11922       return 2;
11923
11924     case ACC_REGS:
11925       /* MTLO and MTHI.  */
11926       return 6;
11927
11928     case FP_REGS:
11929       /* MTC1, etc.  */
11930       return 4;
11931
11932     case ST_REGS:
11933       /* A secondary reload through an FPR scratch.  */
11934       return (mips_register_move_cost (mode, GENERAL_REGS, FP_REGS)
11935               + mips_register_move_cost (mode, FP_REGS, ST_REGS));
11936
11937     case COP0_REGS:
11938     case COP2_REGS:
11939     case COP3_REGS:
11940       /* This choice of value is historical.  */
11941       return 5;
11942
11943     default:
11944       return 0;
11945     }
11946 }
11947
11948 /* Implement TARGET_REGISTER_MOVE_COST.  Return 0 for classes that are the
11949    maximum of the move costs for subclasses; regclass will work out
11950    the maximum for us.  */
11951
11952 static int
11953 mips_register_move_cost (enum machine_mode mode,
11954                          reg_class_t from, reg_class_t to)
11955 {
11956   reg_class_t dregs;
11957   int cost1, cost2;
11958
11959   from = mips_canonicalize_move_class (from);
11960   to = mips_canonicalize_move_class (to);
11961
11962   /* Handle moves that can be done without using general-purpose registers.  */
11963   if (from == FP_REGS)
11964     {
11965       if (to == FP_REGS && mips_mode_ok_for_mov_fmt_p (mode))
11966         /* MOV.FMT.  */
11967         return 4;
11968       if (to == ST_REGS)
11969         /* The sequence generated by mips_expand_fcc_reload.  */
11970         return 8;
11971     }
11972
11973   /* Handle cases in which only one class deviates from the ideal.  */
11974   dregs = TARGET_MIPS16 ? M16_REGS : GENERAL_REGS;
11975   if (from == dregs)
11976     return mips_move_from_gpr_cost (mode, to);
11977   if (to == dregs)
11978     return mips_move_to_gpr_cost (mode, from);
11979
11980   /* Handles cases that require a GPR temporary.  */
11981   cost1 = mips_move_to_gpr_cost (mode, from);
11982   if (cost1 != 0)
11983     {
11984       cost2 = mips_move_from_gpr_cost (mode, to);
11985       if (cost2 != 0)
11986         return cost1 + cost2;
11987     }
11988
11989   return 0;
11990 }
11991
11992 /* Implement TARGET_MEMORY_MOVE_COST.  */
11993
11994 static int
11995 mips_memory_move_cost (enum machine_mode mode, reg_class_t rclass, bool in)
11996 {
11997   return (mips_cost->memory_latency
11998           + memory_move_secondary_cost (mode, rclass, in));
11999
12000
12001 /* Return the register class required for a secondary register when
12002    copying between one of the registers in RCLASS and value X, which
12003    has mode MODE.  X is the source of the move if IN_P, otherwise it
12004    is the destination.  Return NO_REGS if no secondary register is
12005    needed.  */
12006
12007 enum reg_class
12008 mips_secondary_reload_class (enum reg_class rclass,
12009                              enum machine_mode mode, rtx x, bool in_p)
12010 {
12011   int regno;
12012
12013   /* If X is a constant that cannot be loaded into $25, it must be loaded
12014      into some other GPR.  No other register class allows a direct move.  */
12015   if (mips_dangerous_for_la25_p (x))
12016     return reg_class_subset_p (rclass, LEA_REGS) ? NO_REGS : LEA_REGS;
12017
12018   regno = true_regnum (x);
12019   if (TARGET_MIPS16)
12020     {
12021       /* In MIPS16 mode, every move must involve a member of M16_REGS.  */
12022       if (!reg_class_subset_p (rclass, M16_REGS) && !M16_REG_P (regno))
12023         return M16_REGS;
12024
12025       return NO_REGS;
12026     }
12027
12028   /* Copying from accumulator registers to anywhere other than a general
12029      register requires a temporary general register.  */
12030   if (reg_class_subset_p (rclass, ACC_REGS))
12031     return GP_REG_P (regno) ? NO_REGS : GR_REGS;
12032   if (ACC_REG_P (regno))
12033     return reg_class_subset_p (rclass, GR_REGS) ? NO_REGS : GR_REGS;
12034
12035   /* We can only copy a value to a condition code register from a
12036      floating-point register, and even then we require a scratch
12037      floating-point register.  We can only copy a value out of a
12038      condition-code register into a general register.  */
12039   if (reg_class_subset_p (rclass, ST_REGS))
12040     {
12041       if (in_p)
12042         return FP_REGS;
12043       return GP_REG_P (regno) ? NO_REGS : GR_REGS;
12044     }
12045   if (ST_REG_P (regno))
12046     {
12047       if (!in_p)
12048         return FP_REGS;
12049       return reg_class_subset_p (rclass, GR_REGS) ? NO_REGS : GR_REGS;
12050     }
12051
12052   if (reg_class_subset_p (rclass, FP_REGS))
12053     {
12054       if (MEM_P (x)
12055           && (GET_MODE_SIZE (mode) == 4 || GET_MODE_SIZE (mode) == 8))
12056         /* In this case we can use lwc1, swc1, ldc1 or sdc1.  We'll use
12057            pairs of lwc1s and swc1s if ldc1 and sdc1 are not supported.  */
12058         return NO_REGS;
12059
12060       if (GP_REG_P (regno) || x == CONST0_RTX (mode))
12061         /* In this case we can use mtc1, mfc1, dmtc1 or dmfc1.  */
12062         return NO_REGS;
12063
12064       if (CONSTANT_P (x) && !targetm.cannot_force_const_mem (mode, x))
12065         /* We can force the constant to memory and use lwc1
12066            and ldc1.  As above, we will use pairs of lwc1s if
12067            ldc1 is not supported.  */
12068         return NO_REGS;
12069
12070       if (FP_REG_P (regno) && mips_mode_ok_for_mov_fmt_p (mode))
12071         /* In this case we can use mov.fmt.  */
12072         return NO_REGS;
12073
12074       /* Otherwise, we need to reload through an integer register.  */
12075       return GR_REGS;
12076     }
12077   if (FP_REG_P (regno))
12078     return reg_class_subset_p (rclass, GR_REGS) ? NO_REGS : GR_REGS;
12079
12080   return NO_REGS;
12081 }
12082
12083 /* Implement TARGET_MODE_REP_EXTENDED.  */
12084
12085 static int
12086 mips_mode_rep_extended (enum machine_mode mode, enum machine_mode mode_rep)
12087 {
12088   /* On 64-bit targets, SImode register values are sign-extended to DImode.  */
12089   if (TARGET_64BIT && mode == SImode && mode_rep == DImode)
12090     return SIGN_EXTEND;
12091
12092   return UNKNOWN;
12093 }
12094 \f
12095 /* Implement TARGET_VALID_POINTER_MODE.  */
12096
12097 static bool
12098 mips_valid_pointer_mode (enum machine_mode mode)
12099 {
12100   return mode == SImode || (TARGET_64BIT && mode == DImode);
12101 }
12102
12103 /* Implement TARGET_VECTOR_MODE_SUPPORTED_P.  */
12104
12105 static bool
12106 mips_vector_mode_supported_p (enum machine_mode mode)
12107 {
12108   switch (mode)
12109     {
12110     case V2SFmode:
12111       return TARGET_PAIRED_SINGLE_FLOAT;
12112
12113     case V2HImode:
12114     case V4QImode:
12115     case V2HQmode:
12116     case V2UHQmode:
12117     case V2HAmode:
12118     case V2UHAmode:
12119     case V4QQmode:
12120     case V4UQQmode:
12121       return TARGET_DSP;
12122
12123     case V2SImode:
12124     case V4HImode:
12125     case V8QImode:
12126       return TARGET_LOONGSON_VECTORS;
12127
12128     default:
12129       return false;
12130     }
12131 }
12132
12133 /* Implement TARGET_SCALAR_MODE_SUPPORTED_P.  */
12134
12135 static bool
12136 mips_scalar_mode_supported_p (enum machine_mode mode)
12137 {
12138   if (ALL_FIXED_POINT_MODE_P (mode)
12139       && GET_MODE_PRECISION (mode) <= 2 * BITS_PER_WORD)
12140     return true;
12141
12142   return default_scalar_mode_supported_p (mode);
12143 }
12144 \f
12145 /* Implement TARGET_VECTORIZE_PREFERRED_SIMD_MODE.  */
12146
12147 static enum machine_mode
12148 mips_preferred_simd_mode (enum machine_mode mode ATTRIBUTE_UNUSED)
12149 {
12150   if (TARGET_PAIRED_SINGLE_FLOAT
12151       && mode == SFmode)
12152     return V2SFmode;
12153   return word_mode;
12154 }
12155
12156 /* Implement TARGET_INIT_LIBFUNCS.  */
12157
12158 static void
12159 mips_init_libfuncs (void)
12160 {
12161   if (TARGET_FIX_VR4120)
12162     {
12163       /* Register the special divsi3 and modsi3 functions needed to work
12164          around VR4120 division errata.  */
12165       set_optab_libfunc (sdiv_optab, SImode, "__vr4120_divsi3");
12166       set_optab_libfunc (smod_optab, SImode, "__vr4120_modsi3");
12167     }
12168
12169   if (TARGET_MIPS16 && TARGET_HARD_FLOAT_ABI)
12170     {
12171       /* Register the MIPS16 -mhard-float stubs.  */
12172       set_optab_libfunc (add_optab, SFmode, "__mips16_addsf3");
12173       set_optab_libfunc (sub_optab, SFmode, "__mips16_subsf3");
12174       set_optab_libfunc (smul_optab, SFmode, "__mips16_mulsf3");
12175       set_optab_libfunc (sdiv_optab, SFmode, "__mips16_divsf3");
12176
12177       set_optab_libfunc (eq_optab, SFmode, "__mips16_eqsf2");
12178       set_optab_libfunc (ne_optab, SFmode, "__mips16_nesf2");
12179       set_optab_libfunc (gt_optab, SFmode, "__mips16_gtsf2");
12180       set_optab_libfunc (ge_optab, SFmode, "__mips16_gesf2");
12181       set_optab_libfunc (lt_optab, SFmode, "__mips16_ltsf2");
12182       set_optab_libfunc (le_optab, SFmode, "__mips16_lesf2");
12183       set_optab_libfunc (unord_optab, SFmode, "__mips16_unordsf2");
12184
12185       set_conv_libfunc (sfix_optab, SImode, SFmode, "__mips16_fix_truncsfsi");
12186       set_conv_libfunc (sfloat_optab, SFmode, SImode, "__mips16_floatsisf");
12187       set_conv_libfunc (ufloat_optab, SFmode, SImode, "__mips16_floatunsisf");
12188
12189       if (TARGET_DOUBLE_FLOAT)
12190         {
12191           set_optab_libfunc (add_optab, DFmode, "__mips16_adddf3");
12192           set_optab_libfunc (sub_optab, DFmode, "__mips16_subdf3");
12193           set_optab_libfunc (smul_optab, DFmode, "__mips16_muldf3");
12194           set_optab_libfunc (sdiv_optab, DFmode, "__mips16_divdf3");
12195
12196           set_optab_libfunc (eq_optab, DFmode, "__mips16_eqdf2");
12197           set_optab_libfunc (ne_optab, DFmode, "__mips16_nedf2");
12198           set_optab_libfunc (gt_optab, DFmode, "__mips16_gtdf2");
12199           set_optab_libfunc (ge_optab, DFmode, "__mips16_gedf2");
12200           set_optab_libfunc (lt_optab, DFmode, "__mips16_ltdf2");
12201           set_optab_libfunc (le_optab, DFmode, "__mips16_ledf2");
12202           set_optab_libfunc (unord_optab, DFmode, "__mips16_unorddf2");
12203
12204           set_conv_libfunc (sext_optab, DFmode, SFmode,
12205                             "__mips16_extendsfdf2");
12206           set_conv_libfunc (trunc_optab, SFmode, DFmode,
12207                             "__mips16_truncdfsf2");
12208           set_conv_libfunc (sfix_optab, SImode, DFmode,
12209                             "__mips16_fix_truncdfsi");
12210           set_conv_libfunc (sfloat_optab, DFmode, SImode,
12211                             "__mips16_floatsidf");
12212           set_conv_libfunc (ufloat_optab, DFmode, SImode,
12213                             "__mips16_floatunsidf");
12214         }
12215     }
12216
12217   /* The MIPS16 ISA does not have an encoding for "sync", so we rely
12218      on an external non-MIPS16 routine to implement __sync_synchronize.
12219      Similarly for the rest of the ll/sc libfuncs.  */
12220   if (TARGET_MIPS16)
12221     {
12222       synchronize_libfunc = init_one_libfunc ("__sync_synchronize");
12223       init_sync_libfuncs (UNITS_PER_WORD);
12224     }
12225 }
12226
12227 /* Build up a multi-insn sequence that loads label TARGET into $AT.  */
12228
12229 static void
12230 mips_process_load_label (rtx target)
12231 {
12232   rtx base, gp, intop;
12233   HOST_WIDE_INT offset;
12234
12235   mips_multi_start ();
12236   switch (mips_abi)
12237     {
12238     case ABI_N32:
12239       mips_multi_add_insn ("lw\t%@,%%got_page(%0)(%+)", target, 0);
12240       mips_multi_add_insn ("addiu\t%@,%@,%%got_ofst(%0)", target, 0);
12241       break;
12242
12243     case ABI_64:
12244       mips_multi_add_insn ("ld\t%@,%%got_page(%0)(%+)", target, 0);
12245       mips_multi_add_insn ("daddiu\t%@,%@,%%got_ofst(%0)", target, 0);
12246       break;
12247
12248     default:
12249       gp = pic_offset_table_rtx;
12250       if (mips_cfun_has_cprestore_slot_p ())
12251         {
12252           gp = gen_rtx_REG (Pmode, AT_REGNUM);
12253           mips_get_cprestore_base_and_offset (&base, &offset, true);
12254           if (!SMALL_OPERAND (offset))
12255             {
12256               intop = GEN_INT (CONST_HIGH_PART (offset));
12257               mips_multi_add_insn ("lui\t%0,%1", gp, intop, 0);
12258               mips_multi_add_insn ("addu\t%0,%0,%1", gp, base, 0);
12259
12260               base = gp;
12261               offset = CONST_LOW_PART (offset);
12262             }
12263           intop = GEN_INT (offset);
12264           if (ISA_HAS_LOAD_DELAY)
12265             mips_multi_add_insn ("lw\t%0,%1(%2)%#", gp, intop, base, 0);
12266           else
12267             mips_multi_add_insn ("lw\t%0,%1(%2)", gp, intop, base, 0);
12268         }
12269       if (ISA_HAS_LOAD_DELAY)
12270         mips_multi_add_insn ("lw\t%@,%%got(%0)(%1)%#", target, gp, 0);
12271       else
12272         mips_multi_add_insn ("lw\t%@,%%got(%0)(%1)", target, gp, 0);
12273       mips_multi_add_insn ("addiu\t%@,%@,%%lo(%0)", target, 0);
12274       break;
12275     }
12276 }
12277
12278 /* Return the number of instructions needed to load a label into $AT.  */
12279
12280 static unsigned int
12281 mips_load_label_num_insns (void)
12282 {
12283   if (cfun->machine->load_label_num_insns == 0)
12284     {
12285       mips_process_load_label (pc_rtx);
12286       cfun->machine->load_label_num_insns = mips_multi_num_insns;
12287     }
12288   return cfun->machine->load_label_num_insns;
12289 }
12290
12291 /* Emit an asm sequence to start a noat block and load the address
12292    of a label into $1.  */
12293
12294 void
12295 mips_output_load_label (rtx target)
12296 {
12297   mips_push_asm_switch (&mips_noat);
12298   if (TARGET_EXPLICIT_RELOCS)
12299     {
12300       mips_process_load_label (target);
12301       mips_multi_write ();
12302     }
12303   else
12304     {
12305       if (Pmode == DImode)
12306         output_asm_insn ("dla\t%@,%0", &target);
12307       else
12308         output_asm_insn ("la\t%@,%0", &target);
12309     }
12310 }
12311
12312 /* Return the length of INSN.  LENGTH is the initial length computed by
12313    attributes in the machine-description file.  */
12314
12315 int
12316 mips_adjust_insn_length (rtx insn, int length)
12317 {
12318   /* mips.md uses MAX_PIC_BRANCH_LENGTH as a placeholder for the length
12319      of a PIC long-branch sequence.  Substitute the correct value.  */
12320   if (length == MAX_PIC_BRANCH_LENGTH
12321       && JUMP_P (insn)
12322       && INSN_CODE (insn) >= 0
12323       && get_attr_type (insn) == TYPE_BRANCH)
12324     {
12325       /* Add the branch-over instruction and its delay slot, if this
12326          is a conditional branch.  */
12327       length = simplejump_p (insn) ? 0 : 8;
12328
12329       /* Add the size of a load into $AT.  */
12330       length += BASE_INSN_LENGTH * mips_load_label_num_insns ();
12331
12332       /* Add the length of an indirect jump, ignoring the delay slot.  */
12333       length += TARGET_COMPRESSION ? 2 : 4;
12334     }
12335
12336   /* A unconditional jump has an unfilled delay slot if it is not part
12337      of a sequence.  A conditional jump normally has a delay slot, but
12338      does not on MIPS16.  */
12339   if (CALL_P (insn) || (TARGET_MIPS16 ? simplejump_p (insn) : JUMP_P (insn)))
12340     length += TARGET_MIPS16 ? 2 : 4;
12341
12342   /* See how many nops might be needed to avoid hardware hazards.  */
12343   if (!cfun->machine->ignore_hazard_length_p
12344       && INSN_P (insn)
12345       && INSN_CODE (insn) >= 0)
12346     switch (get_attr_hazard (insn))
12347       {
12348       case HAZARD_NONE:
12349         break;
12350
12351       case HAZARD_DELAY:
12352         length += NOP_INSN_LENGTH;
12353         break;
12354
12355       case HAZARD_HILO:
12356         length += NOP_INSN_LENGTH * 2;
12357         break;
12358       }
12359
12360   return length;
12361 }
12362
12363 /* Return the assembly code for INSN, which has the operands given by
12364    OPERANDS, and which branches to OPERANDS[0] if some condition is true.
12365    BRANCH_IF_TRUE is the asm template that should be used if OPERANDS[0]
12366    is in range of a direct branch.  BRANCH_IF_FALSE is an inverted
12367    version of BRANCH_IF_TRUE.  */
12368
12369 const char *
12370 mips_output_conditional_branch (rtx insn, rtx *operands,
12371                                 const char *branch_if_true,
12372                                 const char *branch_if_false)
12373 {
12374   unsigned int length;
12375   rtx taken, not_taken;
12376
12377   gcc_assert (LABEL_P (operands[0]));
12378
12379   length = get_attr_length (insn);
12380   if (length <= 8)
12381     {
12382       /* Just a simple conditional branch.  */
12383       mips_branch_likely = (final_sequence && INSN_ANNULLED_BRANCH_P (insn));
12384       return branch_if_true;
12385     }
12386
12387   /* Generate a reversed branch around a direct jump.  This fallback does
12388      not use branch-likely instructions.  */
12389   mips_branch_likely = false;
12390   not_taken = gen_label_rtx ();
12391   taken = operands[0];
12392
12393   /* Generate the reversed branch to NOT_TAKEN.  */
12394   operands[0] = not_taken;
12395   output_asm_insn (branch_if_false, operands);
12396
12397   /* If INSN has a delay slot, we must provide delay slots for both the
12398      branch to NOT_TAKEN and the conditional jump.  We must also ensure
12399      that INSN's delay slot is executed in the appropriate cases.  */
12400   if (final_sequence)
12401     {
12402       /* This first delay slot will always be executed, so use INSN's
12403          delay slot if is not annulled.  */
12404       if (!INSN_ANNULLED_BRANCH_P (insn))
12405         {
12406           final_scan_insn (XVECEXP (final_sequence, 0, 1),
12407                            asm_out_file, optimize, 1, NULL);
12408           INSN_DELETED_P (XVECEXP (final_sequence, 0, 1)) = 1;
12409         }
12410       else
12411         output_asm_insn ("nop", 0);
12412       fprintf (asm_out_file, "\n");
12413     }
12414
12415   /* Output the unconditional branch to TAKEN.  */
12416   if (TARGET_ABSOLUTE_JUMPS)
12417     output_asm_insn (MIPS_ABSOLUTE_JUMP ("j\t%0%/"), &taken);
12418   else
12419     {
12420       mips_output_load_label (taken);
12421       output_asm_insn ("jr\t%@%]%/", 0);
12422     }
12423
12424   /* Now deal with its delay slot; see above.  */
12425   if (final_sequence)
12426     {
12427       /* This delay slot will only be executed if the branch is taken.
12428          Use INSN's delay slot if is annulled.  */
12429       if (INSN_ANNULLED_BRANCH_P (insn))
12430         {
12431           final_scan_insn (XVECEXP (final_sequence, 0, 1),
12432                            asm_out_file, optimize, 1, NULL);
12433           INSN_DELETED_P (XVECEXP (final_sequence, 0, 1)) = 1;
12434         }
12435       else
12436         output_asm_insn ("nop", 0);
12437       fprintf (asm_out_file, "\n");
12438     }
12439
12440   /* Output NOT_TAKEN.  */
12441   targetm.asm_out.internal_label (asm_out_file, "L",
12442                                   CODE_LABEL_NUMBER (not_taken));
12443   return "";
12444 }
12445
12446 /* Return the assembly code for INSN, which branches to OPERANDS[0]
12447    if some ordering condition is true.  The condition is given by
12448    OPERANDS[1] if !INVERTED_P, otherwise it is the inverse of
12449    OPERANDS[1].  OPERANDS[2] is the comparison's first operand;
12450    its second is always zero.  */
12451
12452 const char *
12453 mips_output_order_conditional_branch (rtx insn, rtx *operands, bool inverted_p)
12454 {
12455   const char *branch[2];
12456
12457   /* Make BRANCH[1] branch to OPERANDS[0] when the condition is true.
12458      Make BRANCH[0] branch on the inverse condition.  */
12459   switch (GET_CODE (operands[1]))
12460     {
12461       /* These cases are equivalent to comparisons against zero.  */
12462     case LEU:
12463       inverted_p = !inverted_p;
12464       /* Fall through.  */
12465     case GTU:
12466       branch[!inverted_p] = MIPS_BRANCH ("bne", "%2,%.,%0");
12467       branch[inverted_p] = MIPS_BRANCH ("beq", "%2,%.,%0");
12468       break;
12469
12470       /* These cases are always true or always false.  */
12471     case LTU:
12472       inverted_p = !inverted_p;
12473       /* Fall through.  */
12474     case GEU:
12475       branch[!inverted_p] = MIPS_BRANCH ("beq", "%.,%.,%0");
12476       branch[inverted_p] = MIPS_BRANCH ("bne", "%.,%.,%0");
12477       break;
12478
12479     default:
12480       branch[!inverted_p] = MIPS_BRANCH ("b%C1z", "%2,%0");
12481       branch[inverted_p] = MIPS_BRANCH ("b%N1z", "%2,%0");
12482       break;
12483     }
12484   return mips_output_conditional_branch (insn, operands, branch[1], branch[0]);
12485 }
12486 \f
12487 /* Start a block of code that needs access to the LL, SC and SYNC
12488    instructions.  */
12489
12490 static void
12491 mips_start_ll_sc_sync_block (void)
12492 {
12493   if (!ISA_HAS_LL_SC)
12494     {
12495       output_asm_insn (".set\tpush", 0);
12496       if (TARGET_64BIT)
12497         output_asm_insn (".set\tmips3", 0);
12498       else
12499         output_asm_insn (".set\tmips2", 0);
12500     }
12501 }
12502
12503 /* End a block started by mips_start_ll_sc_sync_block.  */
12504
12505 static void
12506 mips_end_ll_sc_sync_block (void)
12507 {
12508   if (!ISA_HAS_LL_SC)
12509     output_asm_insn (".set\tpop", 0);
12510 }
12511
12512 /* Output and/or return the asm template for a sync instruction.  */
12513
12514 const char *
12515 mips_output_sync (void)
12516 {
12517   mips_start_ll_sc_sync_block ();
12518   output_asm_insn ("sync", 0);
12519   mips_end_ll_sc_sync_block ();
12520   return "";
12521 }
12522
12523 /* Return the asm template associated with sync_insn1 value TYPE.
12524    IS_64BIT_P is true if we want a 64-bit rather than 32-bit operation.  */
12525
12526 static const char *
12527 mips_sync_insn1_template (enum attr_sync_insn1 type, bool is_64bit_p)
12528 {
12529   switch (type)
12530     {
12531     case SYNC_INSN1_MOVE:
12532       return "move\t%0,%z2";
12533     case SYNC_INSN1_LI:
12534       return "li\t%0,%2";
12535     case SYNC_INSN1_ADDU:
12536       return is_64bit_p ? "daddu\t%0,%1,%z2" : "addu\t%0,%1,%z2";
12537     case SYNC_INSN1_ADDIU:
12538       return is_64bit_p ? "daddiu\t%0,%1,%2" : "addiu\t%0,%1,%2";
12539     case SYNC_INSN1_SUBU:
12540       return is_64bit_p ? "dsubu\t%0,%1,%z2" : "subu\t%0,%1,%z2";
12541     case SYNC_INSN1_AND:
12542       return "and\t%0,%1,%z2";
12543     case SYNC_INSN1_ANDI:
12544       return "andi\t%0,%1,%2";
12545     case SYNC_INSN1_OR:
12546       return "or\t%0,%1,%z2";
12547     case SYNC_INSN1_ORI:
12548       return "ori\t%0,%1,%2";
12549     case SYNC_INSN1_XOR:
12550       return "xor\t%0,%1,%z2";
12551     case SYNC_INSN1_XORI:
12552       return "xori\t%0,%1,%2";
12553     }
12554   gcc_unreachable ();
12555 }
12556
12557 /* Return the asm template associated with sync_insn2 value TYPE.  */
12558
12559 static const char *
12560 mips_sync_insn2_template (enum attr_sync_insn2 type)
12561 {
12562   switch (type)
12563     {
12564     case SYNC_INSN2_NOP:
12565       gcc_unreachable ();
12566     case SYNC_INSN2_AND:
12567       return "and\t%0,%1,%z2";
12568     case SYNC_INSN2_XOR:
12569       return "xor\t%0,%1,%z2";
12570     case SYNC_INSN2_NOT:
12571       return "nor\t%0,%1,%.";
12572     }
12573   gcc_unreachable ();
12574 }
12575
12576 /* OPERANDS are the operands to a sync loop instruction and INDEX is
12577    the value of the one of the sync_* attributes.  Return the operand
12578    referred to by the attribute, or DEFAULT_VALUE if the insn doesn't
12579    have the associated attribute.  */
12580
12581 static rtx
12582 mips_get_sync_operand (rtx *operands, int index, rtx default_value)
12583 {
12584   if (index > 0)
12585     default_value = operands[index - 1];
12586   return default_value;
12587 }
12588
12589 /* INSN is a sync loop with operands OPERANDS.  Build up a multi-insn
12590    sequence for it.  */
12591
12592 static void
12593 mips_process_sync_loop (rtx insn, rtx *operands)
12594 {
12595   rtx at, mem, oldval, newval, inclusive_mask, exclusive_mask;
12596   rtx required_oldval, insn1_op2, tmp1, tmp2, tmp3, cmp;
12597   unsigned int tmp3_insn;
12598   enum attr_sync_insn1 insn1;
12599   enum attr_sync_insn2 insn2;
12600   bool is_64bit_p;
12601   int memmodel_attr;
12602   enum memmodel model;
12603
12604   /* Read an operand from the sync_WHAT attribute and store it in
12605      variable WHAT.  DEFAULT is the default value if no attribute
12606      is specified.  */
12607 #define READ_OPERAND(WHAT, DEFAULT) \
12608   WHAT = mips_get_sync_operand (operands, (int) get_attr_sync_##WHAT (insn), \
12609                                 DEFAULT)
12610
12611   /* Read the memory.  */
12612   READ_OPERAND (mem, 0);
12613   gcc_assert (mem);
12614   is_64bit_p = (GET_MODE_BITSIZE (GET_MODE (mem)) == 64);
12615
12616   /* Read the other attributes.  */
12617   at = gen_rtx_REG (GET_MODE (mem), AT_REGNUM);
12618   READ_OPERAND (oldval, at);
12619   READ_OPERAND (cmp, 0);
12620   READ_OPERAND (newval, at);
12621   READ_OPERAND (inclusive_mask, 0);
12622   READ_OPERAND (exclusive_mask, 0);
12623   READ_OPERAND (required_oldval, 0);
12624   READ_OPERAND (insn1_op2, 0);
12625   insn1 = get_attr_sync_insn1 (insn);
12626   insn2 = get_attr_sync_insn2 (insn);
12627
12628   /* Don't bother setting CMP result that is never used.  */
12629   if (cmp && find_reg_note (insn, REG_UNUSED, cmp))
12630     cmp = 0;
12631
12632   memmodel_attr = get_attr_sync_memmodel (insn);
12633   switch (memmodel_attr)
12634     {
12635     case 10:
12636       model = MEMMODEL_ACQ_REL;
12637       break;
12638     case 11:
12639       model = MEMMODEL_ACQUIRE;
12640       break;
12641     default:
12642       model = (enum memmodel) INTVAL (operands[memmodel_attr]);
12643     }
12644
12645   mips_multi_start ();
12646
12647   /* Output the release side of the memory barrier.  */
12648   if (need_atomic_barrier_p (model, true))
12649     {
12650       if (required_oldval == 0 && TARGET_OCTEON)
12651         {
12652           /* Octeon doesn't reorder reads, so a full barrier can be
12653              created by using SYNCW to order writes combined with the
12654              write from the following SC.  When the SC successfully
12655              completes, we know that all preceding writes are also
12656              committed to the coherent memory system.  It is possible
12657              for a single SYNCW to fail, but a pair of them will never
12658              fail, so we use two.  */
12659           mips_multi_add_insn ("syncw", NULL);
12660           mips_multi_add_insn ("syncw", NULL);
12661         }
12662       else
12663         mips_multi_add_insn ("sync", NULL);
12664     }
12665
12666   /* Output the branch-back label.  */
12667   mips_multi_add_label ("1:");
12668
12669   /* OLDVAL = *MEM.  */
12670   mips_multi_add_insn (is_64bit_p ? "lld\t%0,%1" : "ll\t%0,%1",
12671                        oldval, mem, NULL);
12672
12673   /* if ((OLDVAL & INCLUSIVE_MASK) != REQUIRED_OLDVAL) goto 2.  */
12674   if (required_oldval)
12675     {
12676       if (inclusive_mask == 0)
12677         tmp1 = oldval;
12678       else
12679         {
12680           gcc_assert (oldval != at);
12681           mips_multi_add_insn ("and\t%0,%1,%2",
12682                                at, oldval, inclusive_mask, NULL);
12683           tmp1 = at;
12684         }
12685       mips_multi_add_insn ("bne\t%0,%z1,2f", tmp1, required_oldval, NULL);
12686
12687       /* CMP = 0 [delay slot].  */
12688       if (cmp)
12689         mips_multi_add_insn ("li\t%0,0", cmp, NULL);
12690     }
12691
12692   /* $TMP1 = OLDVAL & EXCLUSIVE_MASK.  */
12693   if (exclusive_mask == 0)
12694     tmp1 = const0_rtx;
12695   else
12696     {
12697       gcc_assert (oldval != at);
12698       mips_multi_add_insn ("and\t%0,%1,%z2",
12699                            at, oldval, exclusive_mask, NULL);
12700       tmp1 = at;
12701     }
12702
12703   /* $TMP2 = INSN1 (OLDVAL, INSN1_OP2).
12704
12705      We can ignore moves if $TMP4 != INSN1_OP2, since we'll still emit
12706      at least one instruction in that case.  */
12707   if (insn1 == SYNC_INSN1_MOVE
12708       && (tmp1 != const0_rtx || insn2 != SYNC_INSN2_NOP))
12709     tmp2 = insn1_op2;
12710   else
12711     {
12712       mips_multi_add_insn (mips_sync_insn1_template (insn1, is_64bit_p),
12713                            newval, oldval, insn1_op2, NULL);
12714       tmp2 = newval;
12715     }
12716
12717   /* $TMP3 = INSN2 ($TMP2, INCLUSIVE_MASK).  */
12718   if (insn2 == SYNC_INSN2_NOP)
12719     tmp3 = tmp2;
12720   else
12721     {
12722       mips_multi_add_insn (mips_sync_insn2_template (insn2),
12723                            newval, tmp2, inclusive_mask, NULL);
12724       tmp3 = newval;
12725     }
12726   tmp3_insn = mips_multi_last_index ();
12727
12728   /* $AT = $TMP1 | $TMP3.  */
12729   if (tmp1 == const0_rtx || tmp3 == const0_rtx)
12730     {
12731       mips_multi_set_operand (tmp3_insn, 0, at);
12732       tmp3 = at;
12733     }
12734   else
12735     {
12736       gcc_assert (tmp1 != tmp3);
12737       mips_multi_add_insn ("or\t%0,%1,%2", at, tmp1, tmp3, NULL);
12738     }
12739
12740   /* if (!commit (*MEM = $AT)) goto 1.
12741
12742      This will sometimes be a delayed branch; see the write code below
12743      for details.  */
12744   mips_multi_add_insn (is_64bit_p ? "scd\t%0,%1" : "sc\t%0,%1", at, mem, NULL);
12745   mips_multi_add_insn ("beq%?\t%0,%.,1b", at, NULL);
12746
12747   /* if (INSN1 != MOVE && INSN1 != LI) NEWVAL = $TMP3 [delay slot].  */
12748   if (insn1 != SYNC_INSN1_MOVE && insn1 != SYNC_INSN1_LI && tmp3 != newval)
12749     {
12750       mips_multi_copy_insn (tmp3_insn);
12751       mips_multi_set_operand (mips_multi_last_index (), 0, newval);
12752     }
12753   else if (!(required_oldval && cmp))
12754     mips_multi_add_insn ("nop", NULL);
12755
12756   /* CMP = 1 -- either standalone or in a delay slot.  */
12757   if (required_oldval && cmp)
12758     mips_multi_add_insn ("li\t%0,1", cmp, NULL);
12759
12760   /* Output the acquire side of the memory barrier.  */
12761   if (TARGET_SYNC_AFTER_SC && need_atomic_barrier_p (model, false))
12762     mips_multi_add_insn ("sync", NULL);
12763
12764   /* Output the exit label, if needed.  */
12765   if (required_oldval)
12766     mips_multi_add_label ("2:");
12767
12768 #undef READ_OPERAND
12769 }
12770
12771 /* Output and/or return the asm template for sync loop INSN, which has
12772    the operands given by OPERANDS.  */
12773
12774 const char *
12775 mips_output_sync_loop (rtx insn, rtx *operands)
12776 {
12777   mips_process_sync_loop (insn, operands);
12778
12779   /* Use branch-likely instructions to work around the LL/SC R10000
12780      errata.  */
12781   mips_branch_likely = TARGET_FIX_R10000;
12782
12783   mips_push_asm_switch (&mips_noreorder);
12784   mips_push_asm_switch (&mips_nomacro);
12785   mips_push_asm_switch (&mips_noat);
12786   mips_start_ll_sc_sync_block ();
12787
12788   mips_multi_write ();
12789
12790   mips_end_ll_sc_sync_block ();
12791   mips_pop_asm_switch (&mips_noat);
12792   mips_pop_asm_switch (&mips_nomacro);
12793   mips_pop_asm_switch (&mips_noreorder);
12794
12795   return "";
12796 }
12797
12798 /* Return the number of individual instructions in sync loop INSN,
12799    which has the operands given by OPERANDS.  */
12800
12801 unsigned int
12802 mips_sync_loop_insns (rtx insn, rtx *operands)
12803 {
12804   mips_process_sync_loop (insn, operands);
12805   return mips_multi_num_insns;
12806 }
12807 \f
12808 /* Return the assembly code for DIV or DDIV instruction DIVISION, which has
12809    the operands given by OPERANDS.  Add in a divide-by-zero check if needed.
12810
12811    When working around R4000 and R4400 errata, we need to make sure that
12812    the division is not immediately followed by a shift[1][2].  We also
12813    need to stop the division from being put into a branch delay slot[3].
12814    The easiest way to avoid both problems is to add a nop after the
12815    division.  When a divide-by-zero check is needed, this nop can be
12816    used to fill the branch delay slot.
12817
12818    [1] If a double-word or a variable shift executes immediately
12819        after starting an integer division, the shift may give an
12820        incorrect result.  See quotations of errata #16 and #28 from
12821        "MIPS R4000PC/SC Errata, Processor Revision 2.2 and 3.0"
12822        in mips.md for details.
12823
12824    [2] A similar bug to [1] exists for all revisions of the
12825        R4000 and the R4400 when run in an MC configuration.
12826        From "MIPS R4000MC Errata, Processor Revision 2.2 and 3.0":
12827
12828        "19. In this following sequence:
12829
12830                     ddiv                (or ddivu or div or divu)
12831                     dsll32              (or dsrl32, dsra32)
12832
12833             if an MPT stall occurs, while the divide is slipping the cpu
12834             pipeline, then the following double shift would end up with an
12835             incorrect result.
12836
12837             Workaround: The compiler needs to avoid generating any
12838             sequence with divide followed by extended double shift."
12839
12840        This erratum is also present in "MIPS R4400MC Errata, Processor
12841        Revision 1.0" and "MIPS R4400MC Errata, Processor Revision 2.0
12842        & 3.0" as errata #10 and #4, respectively.
12843
12844    [3] From "MIPS R4000PC/SC Errata, Processor Revision 2.2 and 3.0"
12845        (also valid for MIPS R4000MC processors):
12846
12847        "52. R4000SC: This bug does not apply for the R4000PC.
12848
12849             There are two flavors of this bug:
12850
12851             1) If the instruction just after divide takes an RF exception
12852                (tlb-refill, tlb-invalid) and gets an instruction cache
12853                miss (both primary and secondary) and the line which is
12854                currently in secondary cache at this index had the first
12855                data word, where the bits 5..2 are set, then R4000 would
12856                get a wrong result for the div.
12857
12858             ##1
12859                     nop
12860                     div r8, r9
12861                     -------------------         # end-of page. -tlb-refill
12862                     nop
12863             ##2
12864                     nop
12865                     div r8, r9
12866                     -------------------         # end-of page. -tlb-invalid
12867                     nop
12868
12869             2) If the divide is in the taken branch delay slot, where the
12870                target takes RF exception and gets an I-cache miss for the
12871                exception vector or where I-cache miss occurs for the
12872                target address, under the above mentioned scenarios, the
12873                div would get wrong results.
12874
12875             ##1
12876                     j   r2              # to next page mapped or unmapped
12877                     div r8,r9           # this bug would be there as long
12878                                         # as there is an ICache miss and
12879                     nop                 # the "data pattern" is present
12880
12881             ##2
12882                     beq r0, r0, NextPage        # to Next page
12883                     div r8,r9
12884                     nop
12885
12886             This bug is present for div, divu, ddiv, and ddivu
12887             instructions.
12888
12889             Workaround: For item 1), OS could make sure that the next page
12890             after the divide instruction is also mapped.  For item 2), the
12891             compiler could make sure that the divide instruction is not in
12892             the branch delay slot."
12893
12894        These processors have PRId values of 0x00004220 and 0x00004300 for
12895        the R4000 and 0x00004400, 0x00004500 and 0x00004600 for the R4400.  */
12896
12897 const char *
12898 mips_output_division (const char *division, rtx *operands)
12899 {
12900   const char *s;
12901
12902   s = division;
12903   if (TARGET_FIX_R4000 || TARGET_FIX_R4400)
12904     {
12905       output_asm_insn (s, operands);
12906       s = "nop";
12907     }
12908   if (TARGET_CHECK_ZERO_DIV)
12909     {
12910       if (TARGET_MIPS16)
12911         {
12912           output_asm_insn (s, operands);
12913           s = "bnez\t%2,1f\n\tbreak\t7\n1:";
12914         }
12915       else if (GENERATE_DIVIDE_TRAPS)
12916         {
12917           /* Avoid long replay penalty on load miss by putting the trap before
12918              the divide.  */
12919           if (TUNE_74K)
12920             output_asm_insn ("teq\t%2,%.,7", operands);
12921           else
12922             {
12923               output_asm_insn (s, operands);
12924               s = "teq\t%2,%.,7";
12925             }
12926         }
12927       else
12928         {
12929           output_asm_insn ("%(bne\t%2,%.,1f", operands);
12930           output_asm_insn (s, operands);
12931           s = "break\t7%)\n1:";
12932         }
12933     }
12934   return s;
12935 }
12936 \f
12937 /* Return true if IN_INSN is a multiply-add or multiply-subtract
12938    instruction and if OUT_INSN assigns to the accumulator operand.  */
12939
12940 bool
12941 mips_linked_madd_p (rtx out_insn, rtx in_insn)
12942 {
12943   enum attr_accum_in accum_in;
12944   int accum_in_opnum;
12945   rtx accum_in_op;
12946
12947   if (recog_memoized (in_insn) < 0)
12948     return false;
12949
12950   accum_in = get_attr_accum_in (in_insn);
12951   if (accum_in == ACCUM_IN_NONE)
12952     return false;
12953
12954   accum_in_opnum = accum_in - ACCUM_IN_0;
12955
12956   extract_insn (in_insn);
12957   gcc_assert (accum_in_opnum < recog_data.n_operands);
12958   accum_in_op = recog_data.operand[accum_in_opnum];
12959
12960   return reg_set_p (accum_in_op, out_insn);
12961 }
12962
12963 /* True if the dependency between OUT_INSN and IN_INSN is on the store
12964    data rather than the address.  We need this because the cprestore
12965    pattern is type "store", but is defined using an UNSPEC_VOLATILE,
12966    which causes the default routine to abort.  We just return false
12967    for that case.  */
12968
12969 bool
12970 mips_store_data_bypass_p (rtx out_insn, rtx in_insn)
12971 {
12972   if (GET_CODE (PATTERN (in_insn)) == UNSPEC_VOLATILE)
12973     return false;
12974
12975   return !store_data_bypass_p (out_insn, in_insn);
12976 }
12977 \f
12978
12979 /* Variables and flags used in scheduler hooks when tuning for
12980    Loongson 2E/2F.  */
12981 static struct
12982 {
12983   /* Variables to support Loongson 2E/2F round-robin [F]ALU1/2 dispatch
12984      strategy.  */
12985
12986   /* If true, then next ALU1/2 instruction will go to ALU1.  */
12987   bool alu1_turn_p;
12988
12989   /* If true, then next FALU1/2 unstruction will go to FALU1.  */
12990   bool falu1_turn_p;
12991
12992   /* Codes to query if [f]alu{1,2}_core units are subscribed or not.  */
12993   int alu1_core_unit_code;
12994   int alu2_core_unit_code;
12995   int falu1_core_unit_code;
12996   int falu2_core_unit_code;
12997
12998   /* True if current cycle has a multi instruction.
12999      This flag is used in mips_ls2_dfa_post_advance_cycle.  */
13000   bool cycle_has_multi_p;
13001
13002   /* Instructions to subscribe ls2_[f]alu{1,2}_turn_enabled units.
13003      These are used in mips_ls2_dfa_post_advance_cycle to initialize
13004      DFA state.
13005      E.g., when alu1_turn_enabled_insn is issued it makes next ALU1/2
13006      instruction to go ALU1.  */
13007   rtx alu1_turn_enabled_insn;
13008   rtx alu2_turn_enabled_insn;
13009   rtx falu1_turn_enabled_insn;
13010   rtx falu2_turn_enabled_insn;
13011 } mips_ls2;
13012
13013 /* Implement TARGET_SCHED_ADJUST_COST.  We assume that anti and output
13014    dependencies have no cost, except on the 20Kc where output-dependence
13015    is treated like input-dependence.  */
13016
13017 static int
13018 mips_adjust_cost (rtx insn ATTRIBUTE_UNUSED, rtx link,
13019                   rtx dep ATTRIBUTE_UNUSED, int cost)
13020 {
13021   if (REG_NOTE_KIND (link) == REG_DEP_OUTPUT
13022       && TUNE_20KC)
13023     return cost;
13024   if (REG_NOTE_KIND (link) != 0)
13025     return 0;
13026   return cost;
13027 }
13028
13029 /* Return the number of instructions that can be issued per cycle.  */
13030
13031 static int
13032 mips_issue_rate (void)
13033 {
13034   switch (mips_tune)
13035     {
13036     case PROCESSOR_74KC:
13037     case PROCESSOR_74KF2_1:
13038     case PROCESSOR_74KF1_1:
13039     case PROCESSOR_74KF3_2:
13040       /* The 74k is not strictly quad-issue cpu, but can be seen as one
13041          by the scheduler.  It can issue 1 ALU, 1 AGEN and 2 FPU insns,
13042          but in reality only a maximum of 3 insns can be issued as
13043          floating-point loads and stores also require a slot in the
13044          AGEN pipe.  */
13045     case PROCESSOR_R10000:
13046       /* All R10K Processors are quad-issue (being the first MIPS
13047          processors to support this feature). */
13048       return 4;
13049
13050     case PROCESSOR_20KC:
13051     case PROCESSOR_R4130:
13052     case PROCESSOR_R5400:
13053     case PROCESSOR_R5500:
13054     case PROCESSOR_R5900:
13055     case PROCESSOR_R7000:
13056     case PROCESSOR_R9000:
13057     case PROCESSOR_OCTEON:
13058     case PROCESSOR_OCTEON2:
13059       return 2;
13060
13061     case PROCESSOR_SB1:
13062     case PROCESSOR_SB1A:
13063       /* This is actually 4, but we get better performance if we claim 3.
13064          This is partly because of unwanted speculative code motion with the
13065          larger number, and partly because in most common cases we can't
13066          reach the theoretical max of 4.  */
13067       return 3;
13068
13069     case PROCESSOR_LOONGSON_2E:
13070     case PROCESSOR_LOONGSON_2F:
13071     case PROCESSOR_LOONGSON_3A:
13072       return 4;
13073
13074     case PROCESSOR_XLP:
13075       return (reload_completed ? 4 : 3);
13076
13077     default:
13078       return 1;
13079     }
13080 }
13081
13082 /* Implement TARGET_SCHED_INIT_DFA_POST_CYCLE_INSN hook for Loongson2.  */
13083
13084 static void
13085 mips_ls2_init_dfa_post_cycle_insn (void)
13086 {
13087   start_sequence ();
13088   emit_insn (gen_ls2_alu1_turn_enabled_insn ());
13089   mips_ls2.alu1_turn_enabled_insn = get_insns ();
13090   end_sequence ();
13091
13092   start_sequence ();
13093   emit_insn (gen_ls2_alu2_turn_enabled_insn ());
13094   mips_ls2.alu2_turn_enabled_insn = get_insns ();
13095   end_sequence ();
13096
13097   start_sequence ();
13098   emit_insn (gen_ls2_falu1_turn_enabled_insn ());
13099   mips_ls2.falu1_turn_enabled_insn = get_insns ();
13100   end_sequence ();
13101
13102   start_sequence ();
13103   emit_insn (gen_ls2_falu2_turn_enabled_insn ());
13104   mips_ls2.falu2_turn_enabled_insn = get_insns ();
13105   end_sequence ();
13106
13107   mips_ls2.alu1_core_unit_code = get_cpu_unit_code ("ls2_alu1_core");
13108   mips_ls2.alu2_core_unit_code = get_cpu_unit_code ("ls2_alu2_core");
13109   mips_ls2.falu1_core_unit_code = get_cpu_unit_code ("ls2_falu1_core");
13110   mips_ls2.falu2_core_unit_code = get_cpu_unit_code ("ls2_falu2_core");
13111 }
13112
13113 /* Implement TARGET_SCHED_INIT_DFA_POST_CYCLE_INSN hook.
13114    Init data used in mips_dfa_post_advance_cycle.  */
13115
13116 static void
13117 mips_init_dfa_post_cycle_insn (void)
13118 {
13119   if (TUNE_LOONGSON_2EF)
13120     mips_ls2_init_dfa_post_cycle_insn ();
13121 }
13122
13123 /* Initialize STATE when scheduling for Loongson 2E/2F.
13124    Support round-robin dispatch scheme by enabling only one of
13125    ALU1/ALU2 and one of FALU1/FALU2 units for ALU1/2 and FALU1/2 instructions
13126    respectively.  */
13127
13128 static void
13129 mips_ls2_dfa_post_advance_cycle (state_t state)
13130 {
13131   if (cpu_unit_reservation_p (state, mips_ls2.alu1_core_unit_code))
13132     {
13133       /* Though there are no non-pipelined ALU1 insns,
13134          we can get an instruction of type 'multi' before reload.  */
13135       gcc_assert (mips_ls2.cycle_has_multi_p);
13136       mips_ls2.alu1_turn_p = false;
13137     }
13138
13139   mips_ls2.cycle_has_multi_p = false;
13140
13141   if (cpu_unit_reservation_p (state, mips_ls2.alu2_core_unit_code))
13142     /* We have a non-pipelined alu instruction in the core,
13143        adjust round-robin counter.  */
13144     mips_ls2.alu1_turn_p = true;
13145
13146   if (mips_ls2.alu1_turn_p)
13147     {
13148       if (state_transition (state, mips_ls2.alu1_turn_enabled_insn) >= 0)
13149         gcc_unreachable ();
13150     }
13151   else
13152     {
13153       if (state_transition (state, mips_ls2.alu2_turn_enabled_insn) >= 0)
13154         gcc_unreachable ();
13155     }
13156
13157   if (cpu_unit_reservation_p (state, mips_ls2.falu1_core_unit_code))
13158     {
13159       /* There are no non-pipelined FALU1 insns.  */
13160       gcc_unreachable ();
13161       mips_ls2.falu1_turn_p = false;
13162     }
13163
13164   if (cpu_unit_reservation_p (state, mips_ls2.falu2_core_unit_code))
13165     /* We have a non-pipelined falu instruction in the core,
13166        adjust round-robin counter.  */
13167     mips_ls2.falu1_turn_p = true;
13168
13169   if (mips_ls2.falu1_turn_p)
13170     {
13171       if (state_transition (state, mips_ls2.falu1_turn_enabled_insn) >= 0)
13172         gcc_unreachable ();
13173     }
13174   else
13175     {
13176       if (state_transition (state, mips_ls2.falu2_turn_enabled_insn) >= 0)
13177         gcc_unreachable ();
13178     }
13179 }
13180
13181 /* Implement TARGET_SCHED_DFA_POST_ADVANCE_CYCLE.
13182    This hook is being called at the start of each cycle.  */
13183
13184 static void
13185 mips_dfa_post_advance_cycle (void)
13186 {
13187   if (TUNE_LOONGSON_2EF)
13188     mips_ls2_dfa_post_advance_cycle (curr_state);
13189 }
13190
13191 /* Implement TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD.  This should
13192    be as wide as the scheduling freedom in the DFA.  */
13193
13194 static int
13195 mips_multipass_dfa_lookahead (void)
13196 {
13197   /* Can schedule up to 4 of the 6 function units in any one cycle.  */
13198   if (TUNE_SB1)
13199     return 4;
13200
13201   if (TUNE_LOONGSON_2EF || TUNE_LOONGSON_3A)
13202     return 4;
13203
13204   if (TUNE_OCTEON)
13205     return 2;
13206
13207   return 0;
13208 }
13209 \f
13210 /* Remove the instruction at index LOWER from ready queue READY and
13211    reinsert it in front of the instruction at index HIGHER.  LOWER must
13212    be <= HIGHER.  */
13213
13214 static void
13215 mips_promote_ready (rtx *ready, int lower, int higher)
13216 {
13217   rtx new_head;
13218   int i;
13219
13220   new_head = ready[lower];
13221   for (i = lower; i < higher; i++)
13222     ready[i] = ready[i + 1];
13223   ready[i] = new_head;
13224 }
13225
13226 /* If the priority of the instruction at POS2 in the ready queue READY
13227    is within LIMIT units of that of the instruction at POS1, swap the
13228    instructions if POS2 is not already less than POS1.  */
13229
13230 static void
13231 mips_maybe_swap_ready (rtx *ready, int pos1, int pos2, int limit)
13232 {
13233   if (pos1 < pos2
13234       && INSN_PRIORITY (ready[pos1]) + limit >= INSN_PRIORITY (ready[pos2]))
13235     {
13236       rtx temp;
13237
13238       temp = ready[pos1];
13239       ready[pos1] = ready[pos2];
13240       ready[pos2] = temp;
13241     }
13242 }
13243 \f
13244 /* Used by TUNE_MACC_CHAINS to record the last scheduled instruction
13245    that may clobber hi or lo.  */
13246 static rtx mips_macc_chains_last_hilo;
13247
13248 /* A TUNE_MACC_CHAINS helper function.  Record that instruction INSN has
13249    been scheduled, updating mips_macc_chains_last_hilo appropriately.  */
13250
13251 static void
13252 mips_macc_chains_record (rtx insn)
13253 {
13254   if (get_attr_may_clobber_hilo (insn))
13255     mips_macc_chains_last_hilo = insn;
13256 }
13257
13258 /* A TUNE_MACC_CHAINS helper function.  Search ready queue READY, which
13259    has NREADY elements, looking for a multiply-add or multiply-subtract
13260    instruction that is cumulative with mips_macc_chains_last_hilo.
13261    If there is one, promote it ahead of anything else that might
13262    clobber hi or lo.  */
13263
13264 static void
13265 mips_macc_chains_reorder (rtx *ready, int nready)
13266 {
13267   int i, j;
13268
13269   if (mips_macc_chains_last_hilo != 0)
13270     for (i = nready - 1; i >= 0; i--)
13271       if (mips_linked_madd_p (mips_macc_chains_last_hilo, ready[i]))
13272         {
13273           for (j = nready - 1; j > i; j--)
13274             if (recog_memoized (ready[j]) >= 0
13275                 && get_attr_may_clobber_hilo (ready[j]))
13276               {
13277                 mips_promote_ready (ready, i, j);
13278                 break;
13279               }
13280           break;
13281         }
13282 }
13283 \f
13284 /* The last instruction to be scheduled.  */
13285 static rtx vr4130_last_insn;
13286
13287 /* A note_stores callback used by vr4130_true_reg_dependence_p.  DATA
13288    points to an rtx that is initially an instruction.  Nullify the rtx
13289    if the instruction uses the value of register X.  */
13290
13291 static void
13292 vr4130_true_reg_dependence_p_1 (rtx x, const_rtx pat ATTRIBUTE_UNUSED,
13293                                 void *data)
13294 {
13295   rtx *insn_ptr;
13296
13297   insn_ptr = (rtx *) data;
13298   if (REG_P (x)
13299       && *insn_ptr != 0
13300       && reg_referenced_p (x, PATTERN (*insn_ptr)))
13301     *insn_ptr = 0;
13302 }
13303
13304 /* Return true if there is true register dependence between vr4130_last_insn
13305    and INSN.  */
13306
13307 static bool
13308 vr4130_true_reg_dependence_p (rtx insn)
13309 {
13310   note_stores (PATTERN (vr4130_last_insn),
13311                vr4130_true_reg_dependence_p_1, &insn);
13312   return insn == 0;
13313 }
13314
13315 /* A TUNE_MIPS4130 helper function.  Given that INSN1 is at the head of
13316    the ready queue and that INSN2 is the instruction after it, return
13317    true if it is worth promoting INSN2 ahead of INSN1.  Look for cases
13318    in which INSN1 and INSN2 can probably issue in parallel, but for
13319    which (INSN2, INSN1) should be less sensitive to instruction
13320    alignment than (INSN1, INSN2).  See 4130.md for more details.  */
13321
13322 static bool
13323 vr4130_swap_insns_p (rtx insn1, rtx insn2)
13324 {
13325   sd_iterator_def sd_it;
13326   dep_t dep;
13327
13328   /* Check for the following case:
13329
13330      1) there is some other instruction X with an anti dependence on INSN1;
13331      2) X has a higher priority than INSN2; and
13332      3) X is an arithmetic instruction (and thus has no unit restrictions).
13333
13334      If INSN1 is the last instruction blocking X, it would better to
13335      choose (INSN1, X) over (INSN2, INSN1).  */
13336   FOR_EACH_DEP (insn1, SD_LIST_FORW, sd_it, dep)
13337     if (DEP_TYPE (dep) == REG_DEP_ANTI
13338         && INSN_PRIORITY (DEP_CON (dep)) > INSN_PRIORITY (insn2)
13339         && recog_memoized (DEP_CON (dep)) >= 0
13340         && get_attr_vr4130_class (DEP_CON (dep)) == VR4130_CLASS_ALU)
13341       return false;
13342
13343   if (vr4130_last_insn != 0
13344       && recog_memoized (insn1) >= 0
13345       && recog_memoized (insn2) >= 0)
13346     {
13347       /* See whether INSN1 and INSN2 use different execution units,
13348          or if they are both ALU-type instructions.  If so, they can
13349          probably execute in parallel.  */
13350       enum attr_vr4130_class class1 = get_attr_vr4130_class (insn1);
13351       enum attr_vr4130_class class2 = get_attr_vr4130_class (insn2);
13352       if (class1 != class2 || class1 == VR4130_CLASS_ALU)
13353         {
13354           /* If only one of the instructions has a dependence on
13355              vr4130_last_insn, prefer to schedule the other one first.  */
13356           bool dep1_p = vr4130_true_reg_dependence_p (insn1);
13357           bool dep2_p = vr4130_true_reg_dependence_p (insn2);
13358           if (dep1_p != dep2_p)
13359             return dep1_p;
13360
13361           /* Prefer to schedule INSN2 ahead of INSN1 if vr4130_last_insn
13362              is not an ALU-type instruction and if INSN1 uses the same
13363              execution unit.  (Note that if this condition holds, we already
13364              know that INSN2 uses a different execution unit.)  */
13365           if (class1 != VR4130_CLASS_ALU
13366               && recog_memoized (vr4130_last_insn) >= 0
13367               && class1 == get_attr_vr4130_class (vr4130_last_insn))
13368             return true;
13369         }
13370     }
13371   return false;
13372 }
13373
13374 /* A TUNE_MIPS4130 helper function.  (READY, NREADY) describes a ready
13375    queue with at least two instructions.  Swap the first two if
13376    vr4130_swap_insns_p says that it could be worthwhile.  */
13377
13378 static void
13379 vr4130_reorder (rtx *ready, int nready)
13380 {
13381   if (vr4130_swap_insns_p (ready[nready - 1], ready[nready - 2]))
13382     mips_promote_ready (ready, nready - 2, nready - 1);
13383 }
13384 \f
13385 /* Record whether last 74k AGEN instruction was a load or store.  */
13386 static enum attr_type mips_last_74k_agen_insn = TYPE_UNKNOWN;
13387
13388 /* Initialize mips_last_74k_agen_insn from INSN.  A null argument
13389    resets to TYPE_UNKNOWN state.  */
13390
13391 static void
13392 mips_74k_agen_init (rtx insn)
13393 {
13394   if (!insn || CALL_P (insn) || JUMP_P (insn))
13395     mips_last_74k_agen_insn = TYPE_UNKNOWN;
13396   else
13397     {
13398       enum attr_type type = get_attr_type (insn);
13399       if (type == TYPE_LOAD || type == TYPE_STORE)
13400         mips_last_74k_agen_insn = type;
13401     }
13402 }
13403
13404 /* A TUNE_74K helper function.  The 74K AGEN pipeline likes multiple
13405    loads to be grouped together, and multiple stores to be grouped
13406    together.  Swap things around in the ready queue to make this happen.  */
13407
13408 static void
13409 mips_74k_agen_reorder (rtx *ready, int nready)
13410 {
13411   int i;
13412   int store_pos, load_pos;
13413
13414   store_pos = -1;
13415   load_pos = -1;
13416
13417   for (i = nready - 1; i >= 0; i--)
13418     {
13419       rtx insn = ready[i];
13420       if (USEFUL_INSN_P (insn))
13421         switch (get_attr_type (insn))
13422           {
13423           case TYPE_STORE:
13424             if (store_pos == -1)
13425               store_pos = i;
13426             break;
13427
13428           case TYPE_LOAD:
13429             if (load_pos == -1)
13430               load_pos = i;
13431             break;
13432
13433           default:
13434             break;
13435           }
13436     }
13437
13438   if (load_pos == -1 || store_pos == -1)
13439     return;
13440
13441   switch (mips_last_74k_agen_insn)
13442     {
13443     case TYPE_UNKNOWN:
13444       /* Prefer to schedule loads since they have a higher latency.  */
13445     case TYPE_LOAD:
13446       /* Swap loads to the front of the queue.  */
13447       mips_maybe_swap_ready (ready, load_pos, store_pos, 4);
13448       break;
13449     case TYPE_STORE:
13450       /* Swap stores to the front of the queue.  */
13451       mips_maybe_swap_ready (ready, store_pos, load_pos, 4);
13452       break;
13453     default:
13454       break;
13455     }
13456 }
13457 \f
13458 /* Implement TARGET_SCHED_INIT.  */
13459
13460 static void
13461 mips_sched_init (FILE *file ATTRIBUTE_UNUSED, int verbose ATTRIBUTE_UNUSED,
13462                  int max_ready ATTRIBUTE_UNUSED)
13463 {
13464   mips_macc_chains_last_hilo = 0;
13465   vr4130_last_insn = 0;
13466   mips_74k_agen_init (NULL_RTX);
13467
13468   /* When scheduling for Loongson2, branch instructions go to ALU1,
13469      therefore basic block is most likely to start with round-robin counter
13470      pointed to ALU2.  */
13471   mips_ls2.alu1_turn_p = false;
13472   mips_ls2.falu1_turn_p = true;
13473 }
13474
13475 /* Subroutine used by TARGET_SCHED_REORDER and TARGET_SCHED_REORDER2.  */
13476
13477 static void
13478 mips_sched_reorder_1 (FILE *file ATTRIBUTE_UNUSED, int verbose ATTRIBUTE_UNUSED,
13479                       rtx *ready, int *nreadyp, int cycle ATTRIBUTE_UNUSED)
13480 {
13481   if (!reload_completed
13482       && TUNE_MACC_CHAINS
13483       && *nreadyp > 0)
13484     mips_macc_chains_reorder (ready, *nreadyp);
13485
13486   if (reload_completed
13487       && TUNE_MIPS4130
13488       && !TARGET_VR4130_ALIGN
13489       && *nreadyp > 1)
13490     vr4130_reorder (ready, *nreadyp);
13491
13492   if (TUNE_74K)
13493     mips_74k_agen_reorder (ready, *nreadyp);
13494 }
13495
13496 /* Implement TARGET_SCHED_REORDER.  */
13497
13498 static int
13499 mips_sched_reorder (FILE *file ATTRIBUTE_UNUSED, int verbose ATTRIBUTE_UNUSED,
13500                     rtx *ready, int *nreadyp, int cycle ATTRIBUTE_UNUSED)
13501 {
13502   mips_sched_reorder_1 (file, verbose, ready, nreadyp, cycle);
13503   return mips_issue_rate ();
13504 }
13505
13506 /* Implement TARGET_SCHED_REORDER2.  */
13507
13508 static int
13509 mips_sched_reorder2 (FILE *file ATTRIBUTE_UNUSED, int verbose ATTRIBUTE_UNUSED,
13510                      rtx *ready, int *nreadyp, int cycle ATTRIBUTE_UNUSED)
13511 {
13512   mips_sched_reorder_1 (file, verbose, ready, nreadyp, cycle);
13513   return cached_can_issue_more;
13514 }
13515
13516 /* Update round-robin counters for ALU1/2 and FALU1/2.  */
13517
13518 static void
13519 mips_ls2_variable_issue (rtx insn)
13520 {
13521   if (mips_ls2.alu1_turn_p)
13522     {
13523       if (cpu_unit_reservation_p (curr_state, mips_ls2.alu1_core_unit_code))
13524         mips_ls2.alu1_turn_p = false;
13525     }
13526   else
13527     {
13528       if (cpu_unit_reservation_p (curr_state, mips_ls2.alu2_core_unit_code))
13529         mips_ls2.alu1_turn_p = true;
13530     }
13531
13532   if (mips_ls2.falu1_turn_p)
13533     {
13534       if (cpu_unit_reservation_p (curr_state, mips_ls2.falu1_core_unit_code))
13535         mips_ls2.falu1_turn_p = false;
13536     }
13537   else
13538     {
13539       if (cpu_unit_reservation_p (curr_state, mips_ls2.falu2_core_unit_code))
13540         mips_ls2.falu1_turn_p = true;
13541     }
13542
13543   if (recog_memoized (insn) >= 0)
13544     mips_ls2.cycle_has_multi_p |= (get_attr_type (insn) == TYPE_MULTI);
13545 }
13546
13547 /* Implement TARGET_SCHED_VARIABLE_ISSUE.  */
13548
13549 static int
13550 mips_variable_issue (FILE *file ATTRIBUTE_UNUSED, int verbose ATTRIBUTE_UNUSED,
13551                      rtx insn, int more)
13552 {
13553   /* Ignore USEs and CLOBBERs; don't count them against the issue rate.  */
13554   if (USEFUL_INSN_P (insn))
13555     {
13556       if (get_attr_type (insn) != TYPE_GHOST)
13557         more--;
13558       if (!reload_completed && TUNE_MACC_CHAINS)
13559         mips_macc_chains_record (insn);
13560       vr4130_last_insn = insn;
13561       if (TUNE_74K)
13562         mips_74k_agen_init (insn);
13563       else if (TUNE_LOONGSON_2EF)
13564         mips_ls2_variable_issue (insn);
13565     }
13566
13567   /* Instructions of type 'multi' should all be split before
13568      the second scheduling pass.  */
13569   gcc_assert (!reload_completed
13570               || recog_memoized (insn) < 0
13571               || get_attr_type (insn) != TYPE_MULTI);
13572
13573   cached_can_issue_more = more;
13574   return more;
13575 }
13576 \f
13577 /* Given that we have an rtx of the form (prefetch ... WRITE LOCALITY),
13578    return the first operand of the associated PREF or PREFX insn.  */
13579
13580 rtx
13581 mips_prefetch_cookie (rtx write, rtx locality)
13582 {
13583   /* store_streamed / load_streamed.  */
13584   if (INTVAL (locality) <= 0)
13585     return GEN_INT (INTVAL (write) + 4);
13586
13587   /* store / load.  */
13588   if (INTVAL (locality) <= 2)
13589     return write;
13590
13591   /* store_retained / load_retained.  */
13592   return GEN_INT (INTVAL (write) + 6);
13593 }
13594 \f
13595 /* Flags that indicate when a built-in function is available.
13596
13597    BUILTIN_AVAIL_NON_MIPS16
13598         The function is available on the current target, but only
13599         in non-MIPS16 mode.  */
13600 #define BUILTIN_AVAIL_NON_MIPS16 1
13601
13602 /* Declare an availability predicate for built-in functions that
13603    require non-MIPS16 mode and also require COND to be true.
13604    NAME is the main part of the predicate's name.  */
13605 #define AVAIL_NON_MIPS16(NAME, COND)                                    \
13606  static unsigned int                                                    \
13607  mips_builtin_avail_##NAME (void)                                       \
13608  {                                                                      \
13609    return (COND) ? BUILTIN_AVAIL_NON_MIPS16 : 0;                        \
13610  }
13611
13612 /* This structure describes a single built-in function.  */
13613 struct mips_builtin_description {
13614   /* The code of the main .md file instruction.  See mips_builtin_type
13615      for more information.  */
13616   enum insn_code icode;
13617
13618   /* The floating-point comparison code to use with ICODE, if any.  */
13619   enum mips_fp_condition cond;
13620
13621   /* The name of the built-in function.  */
13622   const char *name;
13623
13624   /* Specifies how the function should be expanded.  */
13625   enum mips_builtin_type builtin_type;
13626
13627   /* The function's prototype.  */
13628   enum mips_function_type function_type;
13629
13630   /* Whether the function is available.  */
13631   unsigned int (*avail) (void);
13632 };
13633
13634 AVAIL_NON_MIPS16 (paired_single, TARGET_PAIRED_SINGLE_FLOAT)
13635 AVAIL_NON_MIPS16 (sb1_paired_single, TARGET_SB1 && TARGET_PAIRED_SINGLE_FLOAT)
13636 AVAIL_NON_MIPS16 (mips3d, TARGET_MIPS3D)
13637 AVAIL_NON_MIPS16 (dsp, TARGET_DSP)
13638 AVAIL_NON_MIPS16 (dspr2, TARGET_DSPR2)
13639 AVAIL_NON_MIPS16 (dsp_32, !TARGET_64BIT && TARGET_DSP)
13640 AVAIL_NON_MIPS16 (dsp_64, TARGET_64BIT && TARGET_DSP)
13641 AVAIL_NON_MIPS16 (dspr2_32, !TARGET_64BIT && TARGET_DSPR2)
13642 AVAIL_NON_MIPS16 (loongson, TARGET_LOONGSON_VECTORS)
13643 AVAIL_NON_MIPS16 (cache, TARGET_CACHE_BUILTIN)
13644
13645 /* Construct a mips_builtin_description from the given arguments.
13646
13647    INSN is the name of the associated instruction pattern, without the
13648    leading CODE_FOR_mips_.
13649
13650    CODE is the floating-point condition code associated with the
13651    function.  It can be 'f' if the field is not applicable.
13652
13653    NAME is the name of the function itself, without the leading
13654    "__builtin_mips_".
13655
13656    BUILTIN_TYPE and FUNCTION_TYPE are mips_builtin_description fields.
13657
13658    AVAIL is the name of the availability predicate, without the leading
13659    mips_builtin_avail_.  */
13660 #define MIPS_BUILTIN(INSN, COND, NAME, BUILTIN_TYPE,                    \
13661                      FUNCTION_TYPE, AVAIL)                              \
13662   { CODE_FOR_mips_ ## INSN, MIPS_FP_COND_ ## COND,                      \
13663     "__builtin_mips_" NAME, BUILTIN_TYPE, FUNCTION_TYPE,                \
13664     mips_builtin_avail_ ## AVAIL }
13665
13666 /* Define __builtin_mips_<INSN>, which is a MIPS_BUILTIN_DIRECT function
13667    mapped to instruction CODE_FOR_mips_<INSN>,  FUNCTION_TYPE and AVAIL
13668    are as for MIPS_BUILTIN.  */
13669 #define DIRECT_BUILTIN(INSN, FUNCTION_TYPE, AVAIL)                      \
13670   MIPS_BUILTIN (INSN, f, #INSN, MIPS_BUILTIN_DIRECT, FUNCTION_TYPE, AVAIL)
13671
13672 /* Define __builtin_mips_<INSN>_<COND>_{s,d} functions, both of which
13673    are subject to mips_builtin_avail_<AVAIL>.  */
13674 #define CMP_SCALAR_BUILTINS(INSN, COND, AVAIL)                          \
13675   MIPS_BUILTIN (INSN ## _cond_s, COND, #INSN "_" #COND "_s",            \
13676                 MIPS_BUILTIN_CMP_SINGLE, MIPS_INT_FTYPE_SF_SF, AVAIL),  \
13677   MIPS_BUILTIN (INSN ## _cond_d, COND, #INSN "_" #COND "_d",            \
13678                 MIPS_BUILTIN_CMP_SINGLE, MIPS_INT_FTYPE_DF_DF, AVAIL)
13679
13680 /* Define __builtin_mips_{any,all,upper,lower}_<INSN>_<COND>_ps.
13681    The lower and upper forms are subject to mips_builtin_avail_<AVAIL>
13682    while the any and all forms are subject to mips_builtin_avail_mips3d.  */
13683 #define CMP_PS_BUILTINS(INSN, COND, AVAIL)                              \
13684   MIPS_BUILTIN (INSN ## _cond_ps, COND, "any_" #INSN "_" #COND "_ps",   \
13685                 MIPS_BUILTIN_CMP_ANY, MIPS_INT_FTYPE_V2SF_V2SF,         \
13686                 mips3d),                                                \
13687   MIPS_BUILTIN (INSN ## _cond_ps, COND, "all_" #INSN "_" #COND "_ps",   \
13688                 MIPS_BUILTIN_CMP_ALL, MIPS_INT_FTYPE_V2SF_V2SF,         \
13689                 mips3d),                                                \
13690   MIPS_BUILTIN (INSN ## _cond_ps, COND, "lower_" #INSN "_" #COND "_ps", \
13691                 MIPS_BUILTIN_CMP_LOWER, MIPS_INT_FTYPE_V2SF_V2SF,       \
13692                 AVAIL),                                                 \
13693   MIPS_BUILTIN (INSN ## _cond_ps, COND, "upper_" #INSN "_" #COND "_ps", \
13694                 MIPS_BUILTIN_CMP_UPPER, MIPS_INT_FTYPE_V2SF_V2SF,       \
13695                 AVAIL)
13696
13697 /* Define __builtin_mips_{any,all}_<INSN>_<COND>_4s.  The functions
13698    are subject to mips_builtin_avail_mips3d.  */
13699 #define CMP_4S_BUILTINS(INSN, COND)                                     \
13700   MIPS_BUILTIN (INSN ## _cond_4s, COND, "any_" #INSN "_" #COND "_4s",   \
13701                 MIPS_BUILTIN_CMP_ANY,                                   \
13702                 MIPS_INT_FTYPE_V2SF_V2SF_V2SF_V2SF, mips3d),            \
13703   MIPS_BUILTIN (INSN ## _cond_4s, COND, "all_" #INSN "_" #COND "_4s",   \
13704                 MIPS_BUILTIN_CMP_ALL,                                   \
13705                 MIPS_INT_FTYPE_V2SF_V2SF_V2SF_V2SF, mips3d)
13706
13707 /* Define __builtin_mips_mov{t,f}_<INSN>_<COND>_ps.  The comparison
13708    instruction requires mips_builtin_avail_<AVAIL>.  */
13709 #define MOVTF_BUILTINS(INSN, COND, AVAIL)                               \
13710   MIPS_BUILTIN (INSN ## _cond_ps, COND, "movt_" #INSN "_" #COND "_ps",  \
13711                 MIPS_BUILTIN_MOVT, MIPS_V2SF_FTYPE_V2SF_V2SF_V2SF_V2SF, \
13712                 AVAIL),                                                 \
13713   MIPS_BUILTIN (INSN ## _cond_ps, COND, "movf_" #INSN "_" #COND "_ps",  \
13714                 MIPS_BUILTIN_MOVF, MIPS_V2SF_FTYPE_V2SF_V2SF_V2SF_V2SF, \
13715                 AVAIL)
13716
13717 /* Define all the built-in functions related to C.cond.fmt condition COND.  */
13718 #define CMP_BUILTINS(COND)                                              \
13719   MOVTF_BUILTINS (c, COND, paired_single),                              \
13720   MOVTF_BUILTINS (cabs, COND, mips3d),                                  \
13721   CMP_SCALAR_BUILTINS (cabs, COND, mips3d),                             \
13722   CMP_PS_BUILTINS (c, COND, paired_single),                             \
13723   CMP_PS_BUILTINS (cabs, COND, mips3d),                                 \
13724   CMP_4S_BUILTINS (c, COND),                                            \
13725   CMP_4S_BUILTINS (cabs, COND)
13726
13727 /* Define __builtin_mips_<INSN>, which is a MIPS_BUILTIN_DIRECT_NO_TARGET
13728    function mapped to instruction CODE_FOR_mips_<INSN>,  FUNCTION_TYPE
13729    and AVAIL are as for MIPS_BUILTIN.  */
13730 #define DIRECT_NO_TARGET_BUILTIN(INSN, FUNCTION_TYPE, AVAIL)            \
13731   MIPS_BUILTIN (INSN, f, #INSN, MIPS_BUILTIN_DIRECT_NO_TARGET,          \
13732                 FUNCTION_TYPE, AVAIL)
13733
13734 /* Define __builtin_mips_bposge<VALUE>.  <VALUE> is 32 for the MIPS32 DSP
13735    branch instruction.  AVAIL is as for MIPS_BUILTIN.  */
13736 #define BPOSGE_BUILTIN(VALUE, AVAIL)                                    \
13737   MIPS_BUILTIN (bposge, f, "bposge" #VALUE,                             \
13738                 MIPS_BUILTIN_BPOSGE ## VALUE, MIPS_SI_FTYPE_VOID, AVAIL)
13739
13740 /* Define a Loongson MIPS_BUILTIN_DIRECT function __builtin_loongson_<FN_NAME>
13741    for instruction CODE_FOR_loongson_<INSN>.  FUNCTION_TYPE is a
13742    builtin_description field.  */
13743 #define LOONGSON_BUILTIN_ALIAS(INSN, FN_NAME, FUNCTION_TYPE)            \
13744   { CODE_FOR_loongson_ ## INSN, MIPS_FP_COND_f,                         \
13745     "__builtin_loongson_" #FN_NAME, MIPS_BUILTIN_DIRECT,                \
13746     FUNCTION_TYPE, mips_builtin_avail_loongson }
13747
13748 /* Define a Loongson MIPS_BUILTIN_DIRECT function __builtin_loongson_<INSN>
13749    for instruction CODE_FOR_loongson_<INSN>.  FUNCTION_TYPE is a
13750    builtin_description field.  */
13751 #define LOONGSON_BUILTIN(INSN, FUNCTION_TYPE)                           \
13752   LOONGSON_BUILTIN_ALIAS (INSN, INSN, FUNCTION_TYPE)
13753
13754 /* Like LOONGSON_BUILTIN, but add _<SUFFIX> to the end of the function name.
13755    We use functions of this form when the same insn can be usefully applied
13756    to more than one datatype.  */
13757 #define LOONGSON_BUILTIN_SUFFIX(INSN, SUFFIX, FUNCTION_TYPE)            \
13758   LOONGSON_BUILTIN_ALIAS (INSN, INSN ## _ ## SUFFIX, FUNCTION_TYPE)
13759
13760 #define CODE_FOR_mips_sqrt_ps CODE_FOR_sqrtv2sf2
13761 #define CODE_FOR_mips_addq_ph CODE_FOR_addv2hi3
13762 #define CODE_FOR_mips_addu_qb CODE_FOR_addv4qi3
13763 #define CODE_FOR_mips_subq_ph CODE_FOR_subv2hi3
13764 #define CODE_FOR_mips_subu_qb CODE_FOR_subv4qi3
13765 #define CODE_FOR_mips_mul_ph CODE_FOR_mulv2hi3
13766 #define CODE_FOR_mips_mult CODE_FOR_mulsidi3_32bit
13767 #define CODE_FOR_mips_multu CODE_FOR_umulsidi3_32bit
13768
13769 #define CODE_FOR_loongson_packsswh CODE_FOR_vec_pack_ssat_v2si
13770 #define CODE_FOR_loongson_packsshb CODE_FOR_vec_pack_ssat_v4hi
13771 #define CODE_FOR_loongson_packushb CODE_FOR_vec_pack_usat_v4hi
13772 #define CODE_FOR_loongson_paddw CODE_FOR_addv2si3
13773 #define CODE_FOR_loongson_paddh CODE_FOR_addv4hi3
13774 #define CODE_FOR_loongson_paddb CODE_FOR_addv8qi3
13775 #define CODE_FOR_loongson_paddsh CODE_FOR_ssaddv4hi3
13776 #define CODE_FOR_loongson_paddsb CODE_FOR_ssaddv8qi3
13777 #define CODE_FOR_loongson_paddush CODE_FOR_usaddv4hi3
13778 #define CODE_FOR_loongson_paddusb CODE_FOR_usaddv8qi3
13779 #define CODE_FOR_loongson_pmaxsh CODE_FOR_smaxv4hi3
13780 #define CODE_FOR_loongson_pmaxub CODE_FOR_umaxv8qi3
13781 #define CODE_FOR_loongson_pminsh CODE_FOR_sminv4hi3
13782 #define CODE_FOR_loongson_pminub CODE_FOR_uminv8qi3
13783 #define CODE_FOR_loongson_pmulhuh CODE_FOR_umulv4hi3_highpart
13784 #define CODE_FOR_loongson_pmulhh CODE_FOR_smulv4hi3_highpart
13785 #define CODE_FOR_loongson_pmullh CODE_FOR_mulv4hi3
13786 #define CODE_FOR_loongson_psllh CODE_FOR_ashlv4hi3
13787 #define CODE_FOR_loongson_psllw CODE_FOR_ashlv2si3
13788 #define CODE_FOR_loongson_psrlh CODE_FOR_lshrv4hi3
13789 #define CODE_FOR_loongson_psrlw CODE_FOR_lshrv2si3
13790 #define CODE_FOR_loongson_psrah CODE_FOR_ashrv4hi3
13791 #define CODE_FOR_loongson_psraw CODE_FOR_ashrv2si3
13792 #define CODE_FOR_loongson_psubw CODE_FOR_subv2si3
13793 #define CODE_FOR_loongson_psubh CODE_FOR_subv4hi3
13794 #define CODE_FOR_loongson_psubb CODE_FOR_subv8qi3
13795 #define CODE_FOR_loongson_psubsh CODE_FOR_sssubv4hi3
13796 #define CODE_FOR_loongson_psubsb CODE_FOR_sssubv8qi3
13797 #define CODE_FOR_loongson_psubush CODE_FOR_ussubv4hi3
13798 #define CODE_FOR_loongson_psubusb CODE_FOR_ussubv8qi3
13799
13800 static const struct mips_builtin_description mips_builtins[] = {
13801   DIRECT_BUILTIN (pll_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, paired_single),
13802   DIRECT_BUILTIN (pul_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, paired_single),
13803   DIRECT_BUILTIN (plu_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, paired_single),
13804   DIRECT_BUILTIN (puu_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, paired_single),
13805   DIRECT_BUILTIN (cvt_ps_s, MIPS_V2SF_FTYPE_SF_SF, paired_single),
13806   DIRECT_BUILTIN (cvt_s_pl, MIPS_SF_FTYPE_V2SF, paired_single),
13807   DIRECT_BUILTIN (cvt_s_pu, MIPS_SF_FTYPE_V2SF, paired_single),
13808   DIRECT_BUILTIN (abs_ps, MIPS_V2SF_FTYPE_V2SF, paired_single),
13809
13810   DIRECT_BUILTIN (alnv_ps, MIPS_V2SF_FTYPE_V2SF_V2SF_INT, paired_single),
13811   DIRECT_BUILTIN (addr_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, mips3d),
13812   DIRECT_BUILTIN (mulr_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, mips3d),
13813   DIRECT_BUILTIN (cvt_pw_ps, MIPS_V2SF_FTYPE_V2SF, mips3d),
13814   DIRECT_BUILTIN (cvt_ps_pw, MIPS_V2SF_FTYPE_V2SF, mips3d),
13815
13816   DIRECT_BUILTIN (recip1_s, MIPS_SF_FTYPE_SF, mips3d),
13817   DIRECT_BUILTIN (recip1_d, MIPS_DF_FTYPE_DF, mips3d),
13818   DIRECT_BUILTIN (recip1_ps, MIPS_V2SF_FTYPE_V2SF, mips3d),
13819   DIRECT_BUILTIN (recip2_s, MIPS_SF_FTYPE_SF_SF, mips3d),
13820   DIRECT_BUILTIN (recip2_d, MIPS_DF_FTYPE_DF_DF, mips3d),
13821   DIRECT_BUILTIN (recip2_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, mips3d),
13822
13823   DIRECT_BUILTIN (rsqrt1_s, MIPS_SF_FTYPE_SF, mips3d),
13824   DIRECT_BUILTIN (rsqrt1_d, MIPS_DF_FTYPE_DF, mips3d),
13825   DIRECT_BUILTIN (rsqrt1_ps, MIPS_V2SF_FTYPE_V2SF, mips3d),
13826   DIRECT_BUILTIN (rsqrt2_s, MIPS_SF_FTYPE_SF_SF, mips3d),
13827   DIRECT_BUILTIN (rsqrt2_d, MIPS_DF_FTYPE_DF_DF, mips3d),
13828   DIRECT_BUILTIN (rsqrt2_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, mips3d),
13829
13830   MIPS_FP_CONDITIONS (CMP_BUILTINS),
13831
13832   /* Built-in functions for the SB-1 processor.  */
13833   DIRECT_BUILTIN (sqrt_ps, MIPS_V2SF_FTYPE_V2SF, sb1_paired_single),
13834
13835   /* Built-in functions for the DSP ASE (32-bit and 64-bit).  */
13836   DIRECT_BUILTIN (addq_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dsp),
13837   DIRECT_BUILTIN (addq_s_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dsp),
13838   DIRECT_BUILTIN (addq_s_w, MIPS_SI_FTYPE_SI_SI, dsp),
13839   DIRECT_BUILTIN (addu_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dsp),
13840   DIRECT_BUILTIN (addu_s_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dsp),
13841   DIRECT_BUILTIN (subq_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dsp),
13842   DIRECT_BUILTIN (subq_s_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dsp),
13843   DIRECT_BUILTIN (subq_s_w, MIPS_SI_FTYPE_SI_SI, dsp),
13844   DIRECT_BUILTIN (subu_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dsp),
13845   DIRECT_BUILTIN (subu_s_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dsp),
13846   DIRECT_BUILTIN (addsc, MIPS_SI_FTYPE_SI_SI, dsp),
13847   DIRECT_BUILTIN (addwc, MIPS_SI_FTYPE_SI_SI, dsp),
13848   DIRECT_BUILTIN (modsub, MIPS_SI_FTYPE_SI_SI, dsp),
13849   DIRECT_BUILTIN (raddu_w_qb, MIPS_SI_FTYPE_V4QI, dsp),
13850   DIRECT_BUILTIN (absq_s_ph, MIPS_V2HI_FTYPE_V2HI, dsp),
13851   DIRECT_BUILTIN (absq_s_w, MIPS_SI_FTYPE_SI, dsp),
13852   DIRECT_BUILTIN (precrq_qb_ph, MIPS_V4QI_FTYPE_V2HI_V2HI, dsp),
13853   DIRECT_BUILTIN (precrq_ph_w, MIPS_V2HI_FTYPE_SI_SI, dsp),
13854   DIRECT_BUILTIN (precrq_rs_ph_w, MIPS_V2HI_FTYPE_SI_SI, dsp),
13855   DIRECT_BUILTIN (precrqu_s_qb_ph, MIPS_V4QI_FTYPE_V2HI_V2HI, dsp),
13856   DIRECT_BUILTIN (preceq_w_phl, MIPS_SI_FTYPE_V2HI, dsp),
13857   DIRECT_BUILTIN (preceq_w_phr, MIPS_SI_FTYPE_V2HI, dsp),
13858   DIRECT_BUILTIN (precequ_ph_qbl, MIPS_V2HI_FTYPE_V4QI, dsp),
13859   DIRECT_BUILTIN (precequ_ph_qbr, MIPS_V2HI_FTYPE_V4QI, dsp),
13860   DIRECT_BUILTIN (precequ_ph_qbla, MIPS_V2HI_FTYPE_V4QI, dsp),
13861   DIRECT_BUILTIN (precequ_ph_qbra, MIPS_V2HI_FTYPE_V4QI, dsp),
13862   DIRECT_BUILTIN (preceu_ph_qbl, MIPS_V2HI_FTYPE_V4QI, dsp),
13863   DIRECT_BUILTIN (preceu_ph_qbr, MIPS_V2HI_FTYPE_V4QI, dsp),
13864   DIRECT_BUILTIN (preceu_ph_qbla, MIPS_V2HI_FTYPE_V4QI, dsp),
13865   DIRECT_BUILTIN (preceu_ph_qbra, MIPS_V2HI_FTYPE_V4QI, dsp),
13866   DIRECT_BUILTIN (shll_qb, MIPS_V4QI_FTYPE_V4QI_SI, dsp),
13867   DIRECT_BUILTIN (shll_ph, MIPS_V2HI_FTYPE_V2HI_SI, dsp),
13868   DIRECT_BUILTIN (shll_s_ph, MIPS_V2HI_FTYPE_V2HI_SI, dsp),
13869   DIRECT_BUILTIN (shll_s_w, MIPS_SI_FTYPE_SI_SI, dsp),
13870   DIRECT_BUILTIN (shrl_qb, MIPS_V4QI_FTYPE_V4QI_SI, dsp),
13871   DIRECT_BUILTIN (shra_ph, MIPS_V2HI_FTYPE_V2HI_SI, dsp),
13872   DIRECT_BUILTIN (shra_r_ph, MIPS_V2HI_FTYPE_V2HI_SI, dsp),
13873   DIRECT_BUILTIN (shra_r_w, MIPS_SI_FTYPE_SI_SI, dsp),
13874   DIRECT_BUILTIN (muleu_s_ph_qbl, MIPS_V2HI_FTYPE_V4QI_V2HI, dsp),
13875   DIRECT_BUILTIN (muleu_s_ph_qbr, MIPS_V2HI_FTYPE_V4QI_V2HI, dsp),
13876   DIRECT_BUILTIN (mulq_rs_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dsp),
13877   DIRECT_BUILTIN (muleq_s_w_phl, MIPS_SI_FTYPE_V2HI_V2HI, dsp),
13878   DIRECT_BUILTIN (muleq_s_w_phr, MIPS_SI_FTYPE_V2HI_V2HI, dsp),
13879   DIRECT_BUILTIN (bitrev, MIPS_SI_FTYPE_SI, dsp),
13880   DIRECT_BUILTIN (insv, MIPS_SI_FTYPE_SI_SI, dsp),
13881   DIRECT_BUILTIN (repl_qb, MIPS_V4QI_FTYPE_SI, dsp),
13882   DIRECT_BUILTIN (repl_ph, MIPS_V2HI_FTYPE_SI, dsp),
13883   DIRECT_NO_TARGET_BUILTIN (cmpu_eq_qb, MIPS_VOID_FTYPE_V4QI_V4QI, dsp),
13884   DIRECT_NO_TARGET_BUILTIN (cmpu_lt_qb, MIPS_VOID_FTYPE_V4QI_V4QI, dsp),
13885   DIRECT_NO_TARGET_BUILTIN (cmpu_le_qb, MIPS_VOID_FTYPE_V4QI_V4QI, dsp),
13886   DIRECT_BUILTIN (cmpgu_eq_qb, MIPS_SI_FTYPE_V4QI_V4QI, dsp),
13887   DIRECT_BUILTIN (cmpgu_lt_qb, MIPS_SI_FTYPE_V4QI_V4QI, dsp),
13888   DIRECT_BUILTIN (cmpgu_le_qb, MIPS_SI_FTYPE_V4QI_V4QI, dsp),
13889   DIRECT_NO_TARGET_BUILTIN (cmp_eq_ph, MIPS_VOID_FTYPE_V2HI_V2HI, dsp),
13890   DIRECT_NO_TARGET_BUILTIN (cmp_lt_ph, MIPS_VOID_FTYPE_V2HI_V2HI, dsp),
13891   DIRECT_NO_TARGET_BUILTIN (cmp_le_ph, MIPS_VOID_FTYPE_V2HI_V2HI, dsp),
13892   DIRECT_BUILTIN (pick_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dsp),
13893   DIRECT_BUILTIN (pick_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dsp),
13894   DIRECT_BUILTIN (packrl_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dsp),
13895   DIRECT_NO_TARGET_BUILTIN (wrdsp, MIPS_VOID_FTYPE_SI_SI, dsp),
13896   DIRECT_BUILTIN (rddsp, MIPS_SI_FTYPE_SI, dsp),
13897   DIRECT_BUILTIN (lbux, MIPS_SI_FTYPE_POINTER_SI, dsp),
13898   DIRECT_BUILTIN (lhx, MIPS_SI_FTYPE_POINTER_SI, dsp),
13899   DIRECT_BUILTIN (lwx, MIPS_SI_FTYPE_POINTER_SI, dsp),
13900   BPOSGE_BUILTIN (32, dsp),
13901
13902   /* The following are for the MIPS DSP ASE REV 2 (32-bit and 64-bit).  */
13903   DIRECT_BUILTIN (absq_s_qb, MIPS_V4QI_FTYPE_V4QI, dspr2),
13904   DIRECT_BUILTIN (addu_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
13905   DIRECT_BUILTIN (addu_s_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
13906   DIRECT_BUILTIN (adduh_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dspr2),
13907   DIRECT_BUILTIN (adduh_r_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dspr2),
13908   DIRECT_BUILTIN (append, MIPS_SI_FTYPE_SI_SI_SI, dspr2),
13909   DIRECT_BUILTIN (balign, MIPS_SI_FTYPE_SI_SI_SI, dspr2),
13910   DIRECT_BUILTIN (cmpgdu_eq_qb, MIPS_SI_FTYPE_V4QI_V4QI, dspr2),
13911   DIRECT_BUILTIN (cmpgdu_lt_qb, MIPS_SI_FTYPE_V4QI_V4QI, dspr2),
13912   DIRECT_BUILTIN (cmpgdu_le_qb, MIPS_SI_FTYPE_V4QI_V4QI, dspr2),
13913   DIRECT_BUILTIN (mul_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
13914   DIRECT_BUILTIN (mul_s_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
13915   DIRECT_BUILTIN (mulq_rs_w, MIPS_SI_FTYPE_SI_SI, dspr2),
13916   DIRECT_BUILTIN (mulq_s_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
13917   DIRECT_BUILTIN (mulq_s_w, MIPS_SI_FTYPE_SI_SI, dspr2),
13918   DIRECT_BUILTIN (precr_qb_ph, MIPS_V4QI_FTYPE_V2HI_V2HI, dspr2),
13919   DIRECT_BUILTIN (precr_sra_ph_w, MIPS_V2HI_FTYPE_SI_SI_SI, dspr2),
13920   DIRECT_BUILTIN (precr_sra_r_ph_w, MIPS_V2HI_FTYPE_SI_SI_SI, dspr2),
13921   DIRECT_BUILTIN (prepend, MIPS_SI_FTYPE_SI_SI_SI, dspr2),
13922   DIRECT_BUILTIN (shra_qb, MIPS_V4QI_FTYPE_V4QI_SI, dspr2),
13923   DIRECT_BUILTIN (shra_r_qb, MIPS_V4QI_FTYPE_V4QI_SI, dspr2),
13924   DIRECT_BUILTIN (shrl_ph, MIPS_V2HI_FTYPE_V2HI_SI, dspr2),
13925   DIRECT_BUILTIN (subu_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
13926   DIRECT_BUILTIN (subu_s_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
13927   DIRECT_BUILTIN (subuh_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dspr2),
13928   DIRECT_BUILTIN (subuh_r_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dspr2),
13929   DIRECT_BUILTIN (addqh_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
13930   DIRECT_BUILTIN (addqh_r_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
13931   DIRECT_BUILTIN (addqh_w, MIPS_SI_FTYPE_SI_SI, dspr2),
13932   DIRECT_BUILTIN (addqh_r_w, MIPS_SI_FTYPE_SI_SI, dspr2),
13933   DIRECT_BUILTIN (subqh_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
13934   DIRECT_BUILTIN (subqh_r_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
13935   DIRECT_BUILTIN (subqh_w, MIPS_SI_FTYPE_SI_SI, dspr2),
13936   DIRECT_BUILTIN (subqh_r_w, MIPS_SI_FTYPE_SI_SI, dspr2),
13937
13938   /* Built-in functions for the DSP ASE (32-bit only).  */
13939   DIRECT_BUILTIN (dpau_h_qbl, MIPS_DI_FTYPE_DI_V4QI_V4QI, dsp_32),
13940   DIRECT_BUILTIN (dpau_h_qbr, MIPS_DI_FTYPE_DI_V4QI_V4QI, dsp_32),
13941   DIRECT_BUILTIN (dpsu_h_qbl, MIPS_DI_FTYPE_DI_V4QI_V4QI, dsp_32),
13942   DIRECT_BUILTIN (dpsu_h_qbr, MIPS_DI_FTYPE_DI_V4QI_V4QI, dsp_32),
13943   DIRECT_BUILTIN (dpaq_s_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dsp_32),
13944   DIRECT_BUILTIN (dpsq_s_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dsp_32),
13945   DIRECT_BUILTIN (mulsaq_s_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dsp_32),
13946   DIRECT_BUILTIN (dpaq_sa_l_w, MIPS_DI_FTYPE_DI_SI_SI, dsp_32),
13947   DIRECT_BUILTIN (dpsq_sa_l_w, MIPS_DI_FTYPE_DI_SI_SI, dsp_32),
13948   DIRECT_BUILTIN (maq_s_w_phl, MIPS_DI_FTYPE_DI_V2HI_V2HI, dsp_32),
13949   DIRECT_BUILTIN (maq_s_w_phr, MIPS_DI_FTYPE_DI_V2HI_V2HI, dsp_32),
13950   DIRECT_BUILTIN (maq_sa_w_phl, MIPS_DI_FTYPE_DI_V2HI_V2HI, dsp_32),
13951   DIRECT_BUILTIN (maq_sa_w_phr, MIPS_DI_FTYPE_DI_V2HI_V2HI, dsp_32),
13952   DIRECT_BUILTIN (extr_w, MIPS_SI_FTYPE_DI_SI, dsp_32),
13953   DIRECT_BUILTIN (extr_r_w, MIPS_SI_FTYPE_DI_SI, dsp_32),
13954   DIRECT_BUILTIN (extr_rs_w, MIPS_SI_FTYPE_DI_SI, dsp_32),
13955   DIRECT_BUILTIN (extr_s_h, MIPS_SI_FTYPE_DI_SI, dsp_32),
13956   DIRECT_BUILTIN (extp, MIPS_SI_FTYPE_DI_SI, dsp_32),
13957   DIRECT_BUILTIN (extpdp, MIPS_SI_FTYPE_DI_SI, dsp_32),
13958   DIRECT_BUILTIN (shilo, MIPS_DI_FTYPE_DI_SI, dsp_32),
13959   DIRECT_BUILTIN (mthlip, MIPS_DI_FTYPE_DI_SI, dsp_32),
13960   DIRECT_BUILTIN (madd, MIPS_DI_FTYPE_DI_SI_SI, dsp_32),
13961   DIRECT_BUILTIN (maddu, MIPS_DI_FTYPE_DI_USI_USI, dsp_32),
13962   DIRECT_BUILTIN (msub, MIPS_DI_FTYPE_DI_SI_SI, dsp_32),
13963   DIRECT_BUILTIN (msubu, MIPS_DI_FTYPE_DI_USI_USI, dsp_32),
13964   DIRECT_BUILTIN (mult, MIPS_DI_FTYPE_SI_SI, dsp_32),
13965   DIRECT_BUILTIN (multu, MIPS_DI_FTYPE_USI_USI, dsp_32),
13966
13967   /* Built-in functions for the DSP ASE (64-bit only).  */
13968   DIRECT_BUILTIN (ldx, MIPS_DI_FTYPE_POINTER_SI, dsp_64),
13969
13970   /* The following are for the MIPS DSP ASE REV 2 (32-bit only).  */
13971   DIRECT_BUILTIN (dpa_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
13972   DIRECT_BUILTIN (dps_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
13973   DIRECT_BUILTIN (mulsa_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
13974   DIRECT_BUILTIN (dpax_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
13975   DIRECT_BUILTIN (dpsx_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
13976   DIRECT_BUILTIN (dpaqx_s_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
13977   DIRECT_BUILTIN (dpaqx_sa_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
13978   DIRECT_BUILTIN (dpsqx_s_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
13979   DIRECT_BUILTIN (dpsqx_sa_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
13980
13981   /* Builtin functions for ST Microelectronics Loongson-2E/2F cores.  */
13982   LOONGSON_BUILTIN (packsswh, MIPS_V4HI_FTYPE_V2SI_V2SI),
13983   LOONGSON_BUILTIN (packsshb, MIPS_V8QI_FTYPE_V4HI_V4HI),
13984   LOONGSON_BUILTIN (packushb, MIPS_UV8QI_FTYPE_UV4HI_UV4HI),
13985   LOONGSON_BUILTIN_SUFFIX (paddw, u, MIPS_UV2SI_FTYPE_UV2SI_UV2SI),
13986   LOONGSON_BUILTIN_SUFFIX (paddh, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
13987   LOONGSON_BUILTIN_SUFFIX (paddb, u, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
13988   LOONGSON_BUILTIN_SUFFIX (paddw, s, MIPS_V2SI_FTYPE_V2SI_V2SI),
13989   LOONGSON_BUILTIN_SUFFIX (paddh, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
13990   LOONGSON_BUILTIN_SUFFIX (paddb, s, MIPS_V8QI_FTYPE_V8QI_V8QI),
13991   LOONGSON_BUILTIN_SUFFIX (paddd, u, MIPS_UDI_FTYPE_UDI_UDI),
13992   LOONGSON_BUILTIN_SUFFIX (paddd, s, MIPS_DI_FTYPE_DI_DI),
13993   LOONGSON_BUILTIN (paddsh, MIPS_V4HI_FTYPE_V4HI_V4HI),
13994   LOONGSON_BUILTIN (paddsb, MIPS_V8QI_FTYPE_V8QI_V8QI),
13995   LOONGSON_BUILTIN (paddush, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
13996   LOONGSON_BUILTIN (paddusb, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
13997   LOONGSON_BUILTIN_ALIAS (pandn_d, pandn_ud, MIPS_UDI_FTYPE_UDI_UDI),
13998   LOONGSON_BUILTIN_ALIAS (pandn_w, pandn_uw, MIPS_UV2SI_FTYPE_UV2SI_UV2SI),
13999   LOONGSON_BUILTIN_ALIAS (pandn_h, pandn_uh, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14000   LOONGSON_BUILTIN_ALIAS (pandn_b, pandn_ub, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
14001   LOONGSON_BUILTIN_ALIAS (pandn_d, pandn_sd, MIPS_DI_FTYPE_DI_DI),
14002   LOONGSON_BUILTIN_ALIAS (pandn_w, pandn_sw, MIPS_V2SI_FTYPE_V2SI_V2SI),
14003   LOONGSON_BUILTIN_ALIAS (pandn_h, pandn_sh, MIPS_V4HI_FTYPE_V4HI_V4HI),
14004   LOONGSON_BUILTIN_ALIAS (pandn_b, pandn_sb, MIPS_V8QI_FTYPE_V8QI_V8QI),
14005   LOONGSON_BUILTIN (pavgh, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14006   LOONGSON_BUILTIN (pavgb, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
14007   LOONGSON_BUILTIN_SUFFIX (pcmpeqw, u, MIPS_UV2SI_FTYPE_UV2SI_UV2SI),
14008   LOONGSON_BUILTIN_SUFFIX (pcmpeqh, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14009   LOONGSON_BUILTIN_SUFFIX (pcmpeqb, u, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
14010   LOONGSON_BUILTIN_SUFFIX (pcmpeqw, s, MIPS_V2SI_FTYPE_V2SI_V2SI),
14011   LOONGSON_BUILTIN_SUFFIX (pcmpeqh, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
14012   LOONGSON_BUILTIN_SUFFIX (pcmpeqb, s, MIPS_V8QI_FTYPE_V8QI_V8QI),
14013   LOONGSON_BUILTIN_SUFFIX (pcmpgtw, u, MIPS_UV2SI_FTYPE_UV2SI_UV2SI),
14014   LOONGSON_BUILTIN_SUFFIX (pcmpgth, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14015   LOONGSON_BUILTIN_SUFFIX (pcmpgtb, u, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
14016   LOONGSON_BUILTIN_SUFFIX (pcmpgtw, s, MIPS_V2SI_FTYPE_V2SI_V2SI),
14017   LOONGSON_BUILTIN_SUFFIX (pcmpgth, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
14018   LOONGSON_BUILTIN_SUFFIX (pcmpgtb, s, MIPS_V8QI_FTYPE_V8QI_V8QI),
14019   LOONGSON_BUILTIN_SUFFIX (pextrh, u, MIPS_UV4HI_FTYPE_UV4HI_USI),
14020   LOONGSON_BUILTIN_SUFFIX (pextrh, s, MIPS_V4HI_FTYPE_V4HI_USI),
14021   LOONGSON_BUILTIN_SUFFIX (pinsrh_0, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14022   LOONGSON_BUILTIN_SUFFIX (pinsrh_1, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14023   LOONGSON_BUILTIN_SUFFIX (pinsrh_2, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14024   LOONGSON_BUILTIN_SUFFIX (pinsrh_3, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14025   LOONGSON_BUILTIN_SUFFIX (pinsrh_0, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
14026   LOONGSON_BUILTIN_SUFFIX (pinsrh_1, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
14027   LOONGSON_BUILTIN_SUFFIX (pinsrh_2, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
14028   LOONGSON_BUILTIN_SUFFIX (pinsrh_3, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
14029   LOONGSON_BUILTIN (pmaddhw, MIPS_V2SI_FTYPE_V4HI_V4HI),
14030   LOONGSON_BUILTIN (pmaxsh, MIPS_V4HI_FTYPE_V4HI_V4HI),
14031   LOONGSON_BUILTIN (pmaxub, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
14032   LOONGSON_BUILTIN (pminsh, MIPS_V4HI_FTYPE_V4HI_V4HI),
14033   LOONGSON_BUILTIN (pminub, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
14034   LOONGSON_BUILTIN_SUFFIX (pmovmskb, u, MIPS_UV8QI_FTYPE_UV8QI),
14035   LOONGSON_BUILTIN_SUFFIX (pmovmskb, s, MIPS_V8QI_FTYPE_V8QI),
14036   LOONGSON_BUILTIN (pmulhuh, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14037   LOONGSON_BUILTIN (pmulhh, MIPS_V4HI_FTYPE_V4HI_V4HI),
14038   LOONGSON_BUILTIN (pmullh, MIPS_V4HI_FTYPE_V4HI_V4HI),
14039   LOONGSON_BUILTIN (pmuluw, MIPS_UDI_FTYPE_UV2SI_UV2SI),
14040   LOONGSON_BUILTIN (pasubub, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
14041   LOONGSON_BUILTIN (biadd, MIPS_UV4HI_FTYPE_UV8QI),
14042   LOONGSON_BUILTIN (psadbh, MIPS_UV4HI_FTYPE_UV8QI_UV8QI),
14043   LOONGSON_BUILTIN_SUFFIX (pshufh, u, MIPS_UV4HI_FTYPE_UV4HI_UQI),
14044   LOONGSON_BUILTIN_SUFFIX (pshufh, s, MIPS_V4HI_FTYPE_V4HI_UQI),
14045   LOONGSON_BUILTIN_SUFFIX (psllh, u, MIPS_UV4HI_FTYPE_UV4HI_UQI),
14046   LOONGSON_BUILTIN_SUFFIX (psllh, s, MIPS_V4HI_FTYPE_V4HI_UQI),
14047   LOONGSON_BUILTIN_SUFFIX (psllw, u, MIPS_UV2SI_FTYPE_UV2SI_UQI),
14048   LOONGSON_BUILTIN_SUFFIX (psllw, s, MIPS_V2SI_FTYPE_V2SI_UQI),
14049   LOONGSON_BUILTIN_SUFFIX (psrah, u, MIPS_UV4HI_FTYPE_UV4HI_UQI),
14050   LOONGSON_BUILTIN_SUFFIX (psrah, s, MIPS_V4HI_FTYPE_V4HI_UQI),
14051   LOONGSON_BUILTIN_SUFFIX (psraw, u, MIPS_UV2SI_FTYPE_UV2SI_UQI),
14052   LOONGSON_BUILTIN_SUFFIX (psraw, s, MIPS_V2SI_FTYPE_V2SI_UQI),
14053   LOONGSON_BUILTIN_SUFFIX (psrlh, u, MIPS_UV4HI_FTYPE_UV4HI_UQI),
14054   LOONGSON_BUILTIN_SUFFIX (psrlh, s, MIPS_V4HI_FTYPE_V4HI_UQI),
14055   LOONGSON_BUILTIN_SUFFIX (psrlw, u, MIPS_UV2SI_FTYPE_UV2SI_UQI),
14056   LOONGSON_BUILTIN_SUFFIX (psrlw, s, MIPS_V2SI_FTYPE_V2SI_UQI),
14057   LOONGSON_BUILTIN_SUFFIX (psubw, u, MIPS_UV2SI_FTYPE_UV2SI_UV2SI),
14058   LOONGSON_BUILTIN_SUFFIX (psubh, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14059   LOONGSON_BUILTIN_SUFFIX (psubb, u, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
14060   LOONGSON_BUILTIN_SUFFIX (psubw, s, MIPS_V2SI_FTYPE_V2SI_V2SI),
14061   LOONGSON_BUILTIN_SUFFIX (psubh, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
14062   LOONGSON_BUILTIN_SUFFIX (psubb, s, MIPS_V8QI_FTYPE_V8QI_V8QI),
14063   LOONGSON_BUILTIN_SUFFIX (psubd, u, MIPS_UDI_FTYPE_UDI_UDI),
14064   LOONGSON_BUILTIN_SUFFIX (psubd, s, MIPS_DI_FTYPE_DI_DI),
14065   LOONGSON_BUILTIN (psubsh, MIPS_V4HI_FTYPE_V4HI_V4HI),
14066   LOONGSON_BUILTIN (psubsb, MIPS_V8QI_FTYPE_V8QI_V8QI),
14067   LOONGSON_BUILTIN (psubush, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14068   LOONGSON_BUILTIN (psubusb, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
14069   LOONGSON_BUILTIN_SUFFIX (punpckhbh, u, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
14070   LOONGSON_BUILTIN_SUFFIX (punpckhhw, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14071   LOONGSON_BUILTIN_SUFFIX (punpckhwd, u, MIPS_UV2SI_FTYPE_UV2SI_UV2SI),
14072   LOONGSON_BUILTIN_SUFFIX (punpckhbh, s, MIPS_V8QI_FTYPE_V8QI_V8QI),
14073   LOONGSON_BUILTIN_SUFFIX (punpckhhw, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
14074   LOONGSON_BUILTIN_SUFFIX (punpckhwd, s, MIPS_V2SI_FTYPE_V2SI_V2SI),
14075   LOONGSON_BUILTIN_SUFFIX (punpcklbh, u, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
14076   LOONGSON_BUILTIN_SUFFIX (punpcklhw, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14077   LOONGSON_BUILTIN_SUFFIX (punpcklwd, u, MIPS_UV2SI_FTYPE_UV2SI_UV2SI),
14078   LOONGSON_BUILTIN_SUFFIX (punpcklbh, s, MIPS_V8QI_FTYPE_V8QI_V8QI),
14079   LOONGSON_BUILTIN_SUFFIX (punpcklhw, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
14080   LOONGSON_BUILTIN_SUFFIX (punpcklwd, s, MIPS_V2SI_FTYPE_V2SI_V2SI),
14081
14082   /* Sundry other built-in functions.  */
14083   DIRECT_NO_TARGET_BUILTIN (cache, MIPS_VOID_FTYPE_SI_CVPOINTER, cache)
14084 };
14085
14086 /* Index I is the function declaration for mips_builtins[I], or null if the
14087    function isn't defined on this target.  */
14088 static GTY(()) tree mips_builtin_decls[ARRAY_SIZE (mips_builtins)];
14089
14090 /* MODE is a vector mode whose elements have type TYPE.  Return the type
14091    of the vector itself.  */
14092
14093 static tree
14094 mips_builtin_vector_type (tree type, enum machine_mode mode)
14095 {
14096   static tree types[2 * (int) MAX_MACHINE_MODE];
14097   int mode_index;
14098
14099   mode_index = (int) mode;
14100
14101   if (TREE_CODE (type) == INTEGER_TYPE && TYPE_UNSIGNED (type))
14102     mode_index += MAX_MACHINE_MODE;
14103
14104   if (types[mode_index] == NULL_TREE)
14105     types[mode_index] = build_vector_type_for_mode (type, mode);
14106   return types[mode_index];
14107 }
14108
14109 /* Return a type for 'const volatile void *'.  */
14110
14111 static tree
14112 mips_build_cvpointer_type (void)
14113 {
14114   static tree cache;
14115
14116   if (cache == NULL_TREE)
14117     cache = build_pointer_type (build_qualified_type
14118                                 (void_type_node,
14119                                  TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE));
14120   return cache;
14121 }
14122
14123 /* Source-level argument types.  */
14124 #define MIPS_ATYPE_VOID void_type_node
14125 #define MIPS_ATYPE_INT integer_type_node
14126 #define MIPS_ATYPE_POINTER ptr_type_node
14127 #define MIPS_ATYPE_CVPOINTER mips_build_cvpointer_type ()
14128
14129 /* Standard mode-based argument types.  */
14130 #define MIPS_ATYPE_UQI unsigned_intQI_type_node
14131 #define MIPS_ATYPE_SI intSI_type_node
14132 #define MIPS_ATYPE_USI unsigned_intSI_type_node
14133 #define MIPS_ATYPE_DI intDI_type_node
14134 #define MIPS_ATYPE_UDI unsigned_intDI_type_node
14135 #define MIPS_ATYPE_SF float_type_node
14136 #define MIPS_ATYPE_DF double_type_node
14137
14138 /* Vector argument types.  */
14139 #define MIPS_ATYPE_V2SF mips_builtin_vector_type (float_type_node, V2SFmode)
14140 #define MIPS_ATYPE_V2HI mips_builtin_vector_type (intHI_type_node, V2HImode)
14141 #define MIPS_ATYPE_V2SI mips_builtin_vector_type (intSI_type_node, V2SImode)
14142 #define MIPS_ATYPE_V4QI mips_builtin_vector_type (intQI_type_node, V4QImode)
14143 #define MIPS_ATYPE_V4HI mips_builtin_vector_type (intHI_type_node, V4HImode)
14144 #define MIPS_ATYPE_V8QI mips_builtin_vector_type (intQI_type_node, V8QImode)
14145 #define MIPS_ATYPE_UV2SI                                        \
14146   mips_builtin_vector_type (unsigned_intSI_type_node, V2SImode)
14147 #define MIPS_ATYPE_UV4HI                                        \
14148   mips_builtin_vector_type (unsigned_intHI_type_node, V4HImode)
14149 #define MIPS_ATYPE_UV8QI                                        \
14150   mips_builtin_vector_type (unsigned_intQI_type_node, V8QImode)
14151
14152 /* MIPS_FTYPE_ATYPESN takes N MIPS_FTYPES-like type codes and lists
14153    their associated MIPS_ATYPEs.  */
14154 #define MIPS_FTYPE_ATYPES1(A, B) \
14155   MIPS_ATYPE_##A, MIPS_ATYPE_##B
14156
14157 #define MIPS_FTYPE_ATYPES2(A, B, C) \
14158   MIPS_ATYPE_##A, MIPS_ATYPE_##B, MIPS_ATYPE_##C
14159
14160 #define MIPS_FTYPE_ATYPES3(A, B, C, D) \
14161   MIPS_ATYPE_##A, MIPS_ATYPE_##B, MIPS_ATYPE_##C, MIPS_ATYPE_##D
14162
14163 #define MIPS_FTYPE_ATYPES4(A, B, C, D, E) \
14164   MIPS_ATYPE_##A, MIPS_ATYPE_##B, MIPS_ATYPE_##C, MIPS_ATYPE_##D, \
14165   MIPS_ATYPE_##E
14166
14167 /* Return the function type associated with function prototype TYPE.  */
14168
14169 static tree
14170 mips_build_function_type (enum mips_function_type type)
14171 {
14172   static tree types[(int) MIPS_MAX_FTYPE_MAX];
14173
14174   if (types[(int) type] == NULL_TREE)
14175     switch (type)
14176       {
14177 #define DEF_MIPS_FTYPE(NUM, ARGS)                                       \
14178   case MIPS_FTYPE_NAME##NUM ARGS:                                       \
14179     types[(int) type]                                                   \
14180       = build_function_type_list (MIPS_FTYPE_ATYPES##NUM ARGS,          \
14181                                   NULL_TREE);                           \
14182     break;
14183 #include "config/mips/mips-ftypes.def"
14184 #undef DEF_MIPS_FTYPE
14185       default:
14186         gcc_unreachable ();
14187       }
14188
14189   return types[(int) type];
14190 }
14191
14192 /* Implement TARGET_INIT_BUILTINS.  */
14193
14194 static void
14195 mips_init_builtins (void)
14196 {
14197   const struct mips_builtin_description *d;
14198   unsigned int i;
14199
14200   /* Iterate through all of the bdesc arrays, initializing all of the
14201      builtin functions.  */
14202   for (i = 0; i < ARRAY_SIZE (mips_builtins); i++)
14203     {
14204       d = &mips_builtins[i];
14205       if (d->avail ())
14206         mips_builtin_decls[i]
14207           = add_builtin_function (d->name,
14208                                   mips_build_function_type (d->function_type),
14209                                   i, BUILT_IN_MD, NULL, NULL);
14210     }
14211 }
14212
14213 /* Implement TARGET_BUILTIN_DECL.  */
14214
14215 static tree
14216 mips_builtin_decl (unsigned int code, bool initialize_p ATTRIBUTE_UNUSED)
14217 {
14218   if (code >= ARRAY_SIZE (mips_builtins))
14219     return error_mark_node;
14220   return mips_builtin_decls[code];
14221 }
14222
14223 /* Take argument ARGNO from EXP's argument list and convert it into
14224    an expand operand.  Store the operand in *OP.  */
14225
14226 static void
14227 mips_prepare_builtin_arg (struct expand_operand *op, tree exp,
14228                           unsigned int argno)
14229 {
14230   tree arg;
14231   rtx value;
14232
14233   arg = CALL_EXPR_ARG (exp, argno);
14234   value = expand_normal (arg);
14235   create_input_operand (op, value, TYPE_MODE (TREE_TYPE (arg)));
14236 }
14237
14238 /* Expand instruction ICODE as part of a built-in function sequence.
14239    Use the first NOPS elements of OPS as the instruction's operands.
14240    HAS_TARGET_P is true if operand 0 is a target; it is false if the
14241    instruction has no target.
14242
14243    Return the target rtx if HAS_TARGET_P, otherwise return const0_rtx.  */
14244
14245 static rtx
14246 mips_expand_builtin_insn (enum insn_code icode, unsigned int nops,
14247                           struct expand_operand *ops, bool has_target_p)
14248 {
14249   if (!maybe_expand_insn (icode, nops, ops))
14250     {
14251       error ("invalid argument to built-in function");
14252       return has_target_p ? gen_reg_rtx (ops[0].mode) : const0_rtx;
14253     }
14254   return has_target_p ? ops[0].value : const0_rtx;
14255 }
14256
14257 /* Expand a floating-point comparison for built-in function call EXP.
14258    The first NARGS arguments are the values to be compared.  ICODE is
14259    the .md pattern that does the comparison and COND is the condition
14260    that is being tested.  Return an rtx for the result.  */
14261
14262 static rtx
14263 mips_expand_builtin_compare_1 (enum insn_code icode,
14264                                enum mips_fp_condition cond,
14265                                tree exp, int nargs)
14266 {
14267   struct expand_operand ops[MAX_RECOG_OPERANDS];
14268   rtx output;
14269   int opno, argno;
14270
14271   /* The instruction should have a target operand, an operand for each
14272      argument, and an operand for COND.  */
14273   gcc_assert (nargs + 2 == insn_data[(int) icode].n_generator_args);
14274
14275   output = mips_allocate_fcc (insn_data[(int) icode].operand[0].mode);
14276   opno = 0;
14277   create_fixed_operand (&ops[opno++], output);
14278   for (argno = 0; argno < nargs; argno++)
14279     mips_prepare_builtin_arg (&ops[opno++], exp, argno);
14280   create_integer_operand (&ops[opno++], (int) cond);
14281   return mips_expand_builtin_insn (icode, opno, ops, true);
14282 }
14283
14284 /* Expand a MIPS_BUILTIN_DIRECT or MIPS_BUILTIN_DIRECT_NO_TARGET function;
14285    HAS_TARGET_P says which.  EXP is the CALL_EXPR that calls the function
14286    and ICODE is the code of the associated .md pattern.  TARGET, if nonnull,
14287    suggests a good place to put the result.  */
14288
14289 static rtx
14290 mips_expand_builtin_direct (enum insn_code icode, rtx target, tree exp,
14291                             bool has_target_p)
14292 {
14293   struct expand_operand ops[MAX_RECOG_OPERANDS];
14294   int opno, argno;
14295
14296   /* Map any target to operand 0.  */
14297   opno = 0;
14298   if (has_target_p)
14299     create_output_operand (&ops[opno++], target, TYPE_MODE (TREE_TYPE (exp)));
14300
14301   /* Map the arguments to the other operands.  */
14302   gcc_assert (opno + call_expr_nargs (exp)
14303               == insn_data[icode].n_generator_args);
14304   for (argno = 0; argno < call_expr_nargs (exp); argno++)
14305     mips_prepare_builtin_arg (&ops[opno++], exp, argno);
14306
14307   return mips_expand_builtin_insn (icode, opno, ops, has_target_p);
14308 }
14309
14310 /* Expand a __builtin_mips_movt_*_ps or __builtin_mips_movf_*_ps
14311    function; TYPE says which.  EXP is the CALL_EXPR that calls the
14312    function, ICODE is the instruction that should be used to compare
14313    the first two arguments, and COND is the condition it should test.
14314    TARGET, if nonnull, suggests a good place to put the result.  */
14315
14316 static rtx
14317 mips_expand_builtin_movtf (enum mips_builtin_type type,
14318                            enum insn_code icode, enum mips_fp_condition cond,
14319                            rtx target, tree exp)
14320 {
14321   struct expand_operand ops[4];
14322   rtx cmp_result;
14323
14324   cmp_result = mips_expand_builtin_compare_1 (icode, cond, exp, 2);
14325   create_output_operand (&ops[0], target, TYPE_MODE (TREE_TYPE (exp)));
14326   if (type == MIPS_BUILTIN_MOVT)
14327     {
14328       mips_prepare_builtin_arg (&ops[2], exp, 2);
14329       mips_prepare_builtin_arg (&ops[1], exp, 3);
14330     }
14331   else
14332     {
14333       mips_prepare_builtin_arg (&ops[1], exp, 2);
14334       mips_prepare_builtin_arg (&ops[2], exp, 3);
14335     }
14336   create_fixed_operand (&ops[3], cmp_result);
14337   return mips_expand_builtin_insn (CODE_FOR_mips_cond_move_tf_ps,
14338                                    4, ops, true);
14339 }
14340
14341 /* Move VALUE_IF_TRUE into TARGET if CONDITION is true; move VALUE_IF_FALSE
14342    into TARGET otherwise.  Return TARGET.  */
14343
14344 static rtx
14345 mips_builtin_branch_and_move (rtx condition, rtx target,
14346                               rtx value_if_true, rtx value_if_false)
14347 {
14348   rtx true_label, done_label;
14349
14350   true_label = gen_label_rtx ();
14351   done_label = gen_label_rtx ();
14352
14353   /* First assume that CONDITION is false.  */
14354   mips_emit_move (target, value_if_false);
14355
14356   /* Branch to TRUE_LABEL if CONDITION is true and DONE_LABEL otherwise.  */
14357   emit_jump_insn (gen_condjump (condition, true_label));
14358   emit_jump_insn (gen_jump (done_label));
14359   emit_barrier ();
14360
14361   /* Fix TARGET if CONDITION is true.  */
14362   emit_label (true_label);
14363   mips_emit_move (target, value_if_true);
14364
14365   emit_label (done_label);
14366   return target;
14367 }
14368
14369 /* Expand a comparison built-in function of type BUILTIN_TYPE.  EXP is
14370    the CALL_EXPR that calls the function, ICODE is the code of the
14371    comparison instruction, and COND is the condition it should test.
14372    TARGET, if nonnull, suggests a good place to put the boolean result.  */
14373
14374 static rtx
14375 mips_expand_builtin_compare (enum mips_builtin_type builtin_type,
14376                              enum insn_code icode, enum mips_fp_condition cond,
14377                              rtx target, tree exp)
14378 {
14379   rtx offset, condition, cmp_result;
14380
14381   if (target == 0 || GET_MODE (target) != SImode)
14382     target = gen_reg_rtx (SImode);
14383   cmp_result = mips_expand_builtin_compare_1 (icode, cond, exp,
14384                                               call_expr_nargs (exp));
14385
14386   /* If the comparison sets more than one register, we define the result
14387      to be 0 if all registers are false and -1 if all registers are true.
14388      The value of the complete result is indeterminate otherwise.  */
14389   switch (builtin_type)
14390     {
14391     case MIPS_BUILTIN_CMP_ALL:
14392       condition = gen_rtx_NE (VOIDmode, cmp_result, constm1_rtx);
14393       return mips_builtin_branch_and_move (condition, target,
14394                                            const0_rtx, const1_rtx);
14395
14396     case MIPS_BUILTIN_CMP_UPPER:
14397     case MIPS_BUILTIN_CMP_LOWER:
14398       offset = GEN_INT (builtin_type == MIPS_BUILTIN_CMP_UPPER);
14399       condition = gen_single_cc (cmp_result, offset);
14400       return mips_builtin_branch_and_move (condition, target,
14401                                            const1_rtx, const0_rtx);
14402
14403     default:
14404       condition = gen_rtx_NE (VOIDmode, cmp_result, const0_rtx);
14405       return mips_builtin_branch_and_move (condition, target,
14406                                            const1_rtx, const0_rtx);
14407     }
14408 }
14409
14410 /* Expand a bposge built-in function of type BUILTIN_TYPE.  TARGET,
14411    if nonnull, suggests a good place to put the boolean result.  */
14412
14413 static rtx
14414 mips_expand_builtin_bposge (enum mips_builtin_type builtin_type, rtx target)
14415 {
14416   rtx condition, cmp_result;
14417   int cmp_value;
14418
14419   if (target == 0 || GET_MODE (target) != SImode)
14420     target = gen_reg_rtx (SImode);
14421
14422   cmp_result = gen_rtx_REG (CCDSPmode, CCDSP_PO_REGNUM);
14423
14424   if (builtin_type == MIPS_BUILTIN_BPOSGE32)
14425     cmp_value = 32;
14426   else
14427     gcc_assert (0);
14428
14429   condition = gen_rtx_GE (VOIDmode, cmp_result, GEN_INT (cmp_value));
14430   return mips_builtin_branch_and_move (condition, target,
14431                                        const1_rtx, const0_rtx);
14432 }
14433
14434 /* Implement TARGET_EXPAND_BUILTIN.  */
14435
14436 static rtx
14437 mips_expand_builtin (tree exp, rtx target, rtx subtarget ATTRIBUTE_UNUSED,
14438                      enum machine_mode mode, int ignore)
14439 {
14440   tree fndecl;
14441   unsigned int fcode, avail;
14442   const struct mips_builtin_description *d;
14443
14444   fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
14445   fcode = DECL_FUNCTION_CODE (fndecl);
14446   gcc_assert (fcode < ARRAY_SIZE (mips_builtins));
14447   d = &mips_builtins[fcode];
14448   avail = d->avail ();
14449   gcc_assert (avail != 0);
14450   if (TARGET_MIPS16)
14451     {
14452       error ("built-in function %qE not supported for MIPS16",
14453              DECL_NAME (fndecl));
14454       return ignore ? const0_rtx : CONST0_RTX (mode);
14455     }
14456   switch (d->builtin_type)
14457     {
14458     case MIPS_BUILTIN_DIRECT:
14459       return mips_expand_builtin_direct (d->icode, target, exp, true);
14460
14461     case MIPS_BUILTIN_DIRECT_NO_TARGET:
14462       return mips_expand_builtin_direct (d->icode, target, exp, false);
14463
14464     case MIPS_BUILTIN_MOVT:
14465     case MIPS_BUILTIN_MOVF:
14466       return mips_expand_builtin_movtf (d->builtin_type, d->icode,
14467                                         d->cond, target, exp);
14468
14469     case MIPS_BUILTIN_CMP_ANY:
14470     case MIPS_BUILTIN_CMP_ALL:
14471     case MIPS_BUILTIN_CMP_UPPER:
14472     case MIPS_BUILTIN_CMP_LOWER:
14473     case MIPS_BUILTIN_CMP_SINGLE:
14474       return mips_expand_builtin_compare (d->builtin_type, d->icode,
14475                                           d->cond, target, exp);
14476
14477     case MIPS_BUILTIN_BPOSGE32:
14478       return mips_expand_builtin_bposge (d->builtin_type, target);
14479     }
14480   gcc_unreachable ();
14481 }
14482 \f
14483 /* An entry in the MIPS16 constant pool.  VALUE is the pool constant,
14484    MODE is its mode, and LABEL is the CODE_LABEL associated with it.  */
14485 struct mips16_constant {
14486   struct mips16_constant *next;
14487   rtx value;
14488   rtx label;
14489   enum machine_mode mode;
14490 };
14491
14492 /* Information about an incomplete MIPS16 constant pool.  FIRST is the
14493    first constant, HIGHEST_ADDRESS is the highest address that the first
14494    byte of the pool can have, and INSN_ADDRESS is the current instruction
14495    address.  */
14496 struct mips16_constant_pool {
14497   struct mips16_constant *first;
14498   int highest_address;
14499   int insn_address;
14500 };
14501
14502 /* Add constant VALUE to POOL and return its label.  MODE is the
14503    value's mode (used for CONST_INTs, etc.).  */
14504
14505 static rtx
14506 mips16_add_constant (struct mips16_constant_pool *pool,
14507                      rtx value, enum machine_mode mode)
14508 {
14509   struct mips16_constant **p, *c;
14510   bool first_of_size_p;
14511
14512   /* See whether the constant is already in the pool.  If so, return the
14513      existing label, otherwise leave P pointing to the place where the
14514      constant should be added.
14515
14516      Keep the pool sorted in increasing order of mode size so that we can
14517      reduce the number of alignments needed.  */
14518   first_of_size_p = true;
14519   for (p = &pool->first; *p != 0; p = &(*p)->next)
14520     {
14521       if (mode == (*p)->mode && rtx_equal_p (value, (*p)->value))
14522         return (*p)->label;
14523       if (GET_MODE_SIZE (mode) < GET_MODE_SIZE ((*p)->mode))
14524         break;
14525       if (GET_MODE_SIZE (mode) == GET_MODE_SIZE ((*p)->mode))
14526         first_of_size_p = false;
14527     }
14528
14529   /* In the worst case, the constant needed by the earliest instruction
14530      will end up at the end of the pool.  The entire pool must then be
14531      accessible from that instruction.
14532
14533      When adding the first constant, set the pool's highest address to
14534      the address of the first out-of-range byte.  Adjust this address
14535      downwards each time a new constant is added.  */
14536   if (pool->first == 0)
14537     /* For LWPC, ADDIUPC and DADDIUPC, the base PC value is the address
14538        of the instruction with the lowest two bits clear.  The base PC
14539        value for LDPC has the lowest three bits clear.  Assume the worst
14540        case here; namely that the PC-relative instruction occupies the
14541        last 2 bytes in an aligned word.  */
14542     pool->highest_address = pool->insn_address - (UNITS_PER_WORD - 2) + 0x8000;
14543   pool->highest_address -= GET_MODE_SIZE (mode);
14544   if (first_of_size_p)
14545     /* Take into account the worst possible padding due to alignment.  */
14546     pool->highest_address -= GET_MODE_SIZE (mode) - 1;
14547
14548   /* Create a new entry.  */
14549   c = XNEW (struct mips16_constant);
14550   c->value = value;
14551   c->mode = mode;
14552   c->label = gen_label_rtx ();
14553   c->next = *p;
14554   *p = c;
14555
14556   return c->label;
14557 }
14558
14559 /* Output constant VALUE after instruction INSN and return the last
14560    instruction emitted.  MODE is the mode of the constant.  */
14561
14562 static rtx
14563 mips16_emit_constants_1 (enum machine_mode mode, rtx value, rtx insn)
14564 {
14565   if (SCALAR_INT_MODE_P (mode) || ALL_SCALAR_FIXED_POINT_MODE_P (mode))
14566     {
14567       rtx size = GEN_INT (GET_MODE_SIZE (mode));
14568       return emit_insn_after (gen_consttable_int (value, size), insn);
14569     }
14570
14571   if (SCALAR_FLOAT_MODE_P (mode))
14572     return emit_insn_after (gen_consttable_float (value), insn);
14573
14574   if (VECTOR_MODE_P (mode))
14575     {
14576       int i;
14577
14578       for (i = 0; i < CONST_VECTOR_NUNITS (value); i++)
14579         insn = mips16_emit_constants_1 (GET_MODE_INNER (mode),
14580                                         CONST_VECTOR_ELT (value, i), insn);
14581       return insn;
14582     }
14583
14584   gcc_unreachable ();
14585 }
14586
14587 /* Dump out the constants in CONSTANTS after INSN.  */
14588
14589 static void
14590 mips16_emit_constants (struct mips16_constant *constants, rtx insn)
14591 {
14592   struct mips16_constant *c, *next;
14593   int align;
14594
14595   align = 0;
14596   for (c = constants; c != NULL; c = next)
14597     {
14598       /* If necessary, increase the alignment of PC.  */
14599       if (align < GET_MODE_SIZE (c->mode))
14600         {
14601           int align_log = floor_log2 (GET_MODE_SIZE (c->mode));
14602           insn = emit_insn_after (gen_align (GEN_INT (align_log)), insn);
14603         }
14604       align = GET_MODE_SIZE (c->mode);
14605
14606       insn = emit_label_after (c->label, insn);
14607       insn = mips16_emit_constants_1 (c->mode, c->value, insn);
14608
14609       next = c->next;
14610       free (c);
14611     }
14612
14613   emit_barrier_after (insn);
14614 }
14615
14616 /* Return the length of instruction INSN.  */
14617
14618 static int
14619 mips16_insn_length (rtx insn)
14620 {
14621   if (JUMP_TABLE_DATA_P (insn))
14622     {
14623       rtx body = PATTERN (insn);
14624       if (GET_CODE (body) == ADDR_VEC)
14625         return GET_MODE_SIZE (GET_MODE (body)) * XVECLEN (body, 0);
14626       else if (GET_CODE (body) == ADDR_DIFF_VEC)
14627         return GET_MODE_SIZE (GET_MODE (body)) * XVECLEN (body, 1);
14628       else
14629         gcc_unreachable ();
14630     }
14631   return get_attr_length (insn);
14632 }
14633
14634 /* If *X is a symbolic constant that refers to the constant pool, add
14635    the constant to POOL and rewrite *X to use the constant's label.  */
14636
14637 static void
14638 mips16_rewrite_pool_constant (struct mips16_constant_pool *pool, rtx *x)
14639 {
14640   rtx base, offset, label;
14641
14642   split_const (*x, &base, &offset);
14643   if (GET_CODE (base) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (base))
14644     {
14645       label = mips16_add_constant (pool, copy_rtx (get_pool_constant (base)),
14646                                    get_pool_mode (base));
14647       base = gen_rtx_LABEL_REF (Pmode, label);
14648       *x = mips_unspec_address_offset (base, offset, SYMBOL_PC_RELATIVE);
14649     }
14650 }
14651
14652 /* This structure is used to communicate with mips16_rewrite_pool_refs.
14653    INSN is the instruction we're rewriting and POOL points to the current
14654    constant pool.  */
14655 struct mips16_rewrite_pool_refs_info {
14656   rtx insn;
14657   struct mips16_constant_pool *pool;
14658 };
14659
14660 /* Rewrite *X so that constant pool references refer to the constant's
14661    label instead.  DATA points to a mips16_rewrite_pool_refs_info
14662    structure.  */
14663
14664 static int
14665 mips16_rewrite_pool_refs (rtx *x, void *data)
14666 {
14667   struct mips16_rewrite_pool_refs_info *info =
14668     (struct mips16_rewrite_pool_refs_info *) data;
14669
14670   if (force_to_mem_operand (*x, Pmode))
14671     {
14672       rtx mem = force_const_mem (GET_MODE (*x), *x);
14673       validate_change (info->insn, x, mem, false);
14674     }
14675
14676   if (MEM_P (*x))
14677     {
14678       mips16_rewrite_pool_constant (info->pool, &XEXP (*x, 0));
14679       return -1;
14680     }
14681
14682   /* Don't rewrite the __mips16_rdwr symbol.  */
14683   if (GET_CODE (*x) == UNSPEC && XINT (*x, 1) == UNSPEC_TLS_GET_TP)
14684     return -1;
14685
14686   if (TARGET_MIPS16_TEXT_LOADS)
14687     mips16_rewrite_pool_constant (info->pool, x);
14688
14689   return GET_CODE (*x) == CONST ? -1 : 0;
14690 }
14691
14692 /* Return whether CFG is used in mips_reorg.  */
14693
14694 static bool
14695 mips_cfg_in_reorg (void)
14696 {
14697   return (mips_r10k_cache_barrier != R10K_CACHE_BARRIER_NONE
14698           || TARGET_RELAX_PIC_CALLS);
14699 }
14700
14701 /* Build MIPS16 constant pools.  Split the instructions if SPLIT_P,
14702    otherwise assume that they are already split.  */
14703
14704 static void
14705 mips16_lay_out_constants (bool split_p)
14706 {
14707   struct mips16_constant_pool pool;
14708   struct mips16_rewrite_pool_refs_info info;
14709   rtx insn, barrier;
14710
14711   if (!TARGET_MIPS16_PCREL_LOADS)
14712     return;
14713
14714   if (split_p)
14715     {
14716       if (mips_cfg_in_reorg ())
14717         split_all_insns ();
14718       else
14719         split_all_insns_noflow ();
14720     }
14721   barrier = 0;
14722   memset (&pool, 0, sizeof (pool));
14723   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
14724     {
14725       /* Rewrite constant pool references in INSN.  */
14726       if (USEFUL_INSN_P (insn))
14727         {
14728           info.insn = insn;
14729           info.pool = &pool;
14730           for_each_rtx (&PATTERN (insn), mips16_rewrite_pool_refs, &info);
14731         }
14732
14733       pool.insn_address += mips16_insn_length (insn);
14734
14735       if (pool.first != NULL)
14736         {
14737           /* If there are no natural barriers between the first user of
14738              the pool and the highest acceptable address, we'll need to
14739              create a new instruction to jump around the constant pool.
14740              In the worst case, this instruction will be 4 bytes long.
14741
14742              If it's too late to do this transformation after INSN,
14743              do it immediately before INSN.  */
14744           if (barrier == 0 && pool.insn_address + 4 > pool.highest_address)
14745             {
14746               rtx label, jump;
14747
14748               label = gen_label_rtx ();
14749
14750               jump = emit_jump_insn_before (gen_jump (label), insn);
14751               JUMP_LABEL (jump) = label;
14752               LABEL_NUSES (label) = 1;
14753               barrier = emit_barrier_after (jump);
14754
14755               emit_label_after (label, barrier);
14756               pool.insn_address += 4;
14757             }
14758
14759           /* See whether the constant pool is now out of range of the first
14760              user.  If so, output the constants after the previous barrier.
14761              Note that any instructions between BARRIER and INSN (inclusive)
14762              will use negative offsets to refer to the pool.  */
14763           if (pool.insn_address > pool.highest_address)
14764             {
14765               mips16_emit_constants (pool.first, barrier);
14766               pool.first = NULL;
14767               barrier = 0;
14768             }
14769           else if (BARRIER_P (insn))
14770             barrier = insn;
14771         }
14772     }
14773   mips16_emit_constants (pool.first, get_last_insn ());
14774 }
14775 \f
14776 /* Return true if it is worth r10k_simplify_address's while replacing
14777    an address with X.  We are looking for constants, and for addresses
14778    at a known offset from the incoming stack pointer.  */
14779
14780 static bool
14781 r10k_simplified_address_p (rtx x)
14782 {
14783   if (GET_CODE (x) == PLUS && CONST_INT_P (XEXP (x, 1)))
14784     x = XEXP (x, 0);
14785   return x == virtual_incoming_args_rtx || CONSTANT_P (x);
14786 }
14787
14788 /* X is an expression that appears in INSN.  Try to use the UD chains
14789    to simplify it, returning the simplified form on success and the
14790    original form otherwise.  Replace the incoming value of $sp with
14791    virtual_incoming_args_rtx (which should never occur in X otherwise).  */
14792
14793 static rtx
14794 r10k_simplify_address (rtx x, rtx insn)
14795 {
14796   rtx newx, op0, op1, set, def_insn, note;
14797   df_ref use, def;
14798   struct df_link *defs;
14799
14800   newx = NULL_RTX;
14801   if (UNARY_P (x))
14802     {
14803       op0 = r10k_simplify_address (XEXP (x, 0), insn);
14804       if (op0 != XEXP (x, 0))
14805         newx = simplify_gen_unary (GET_CODE (x), GET_MODE (x),
14806                                    op0, GET_MODE (XEXP (x, 0)));
14807     }
14808   else if (BINARY_P (x))
14809     {
14810       op0 = r10k_simplify_address (XEXP (x, 0), insn);
14811       op1 = r10k_simplify_address (XEXP (x, 1), insn);
14812       if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
14813         newx = simplify_gen_binary (GET_CODE (x), GET_MODE (x), op0, op1);
14814     }
14815   else if (GET_CODE (x) == LO_SUM)
14816     {
14817       /* LO_SUMs can be offset from HIGHs, if we know they won't
14818          overflow.  See mips_classify_address for the rationale behind
14819          the lax check.  */
14820       op0 = r10k_simplify_address (XEXP (x, 0), insn);
14821       if (GET_CODE (op0) == HIGH)
14822         newx = XEXP (x, 1);
14823     }
14824   else if (REG_P (x))
14825     {
14826       /* Uses are recorded by regno_reg_rtx, not X itself.  */
14827       use = df_find_use (insn, regno_reg_rtx[REGNO (x)]);
14828       gcc_assert (use);
14829       defs = DF_REF_CHAIN (use);
14830
14831       /* Require a single definition.  */
14832       if (defs && defs->next == NULL)
14833         {
14834           def = defs->ref;
14835           if (DF_REF_IS_ARTIFICIAL (def))
14836             {
14837               /* Replace the incoming value of $sp with
14838                  virtual_incoming_args_rtx.  */
14839               if (x == stack_pointer_rtx
14840                   && DF_REF_BB (def) == ENTRY_BLOCK_PTR)
14841                 newx = virtual_incoming_args_rtx;
14842             }
14843           else if (dominated_by_p (CDI_DOMINATORS, DF_REF_BB (use),
14844                                    DF_REF_BB (def)))
14845             {
14846               /* Make sure that DEF_INSN is a single set of REG.  */
14847               def_insn = DF_REF_INSN (def);
14848               if (NONJUMP_INSN_P (def_insn))
14849                 {
14850                   set = single_set (def_insn);
14851                   if (set && rtx_equal_p (SET_DEST (set), x))
14852                     {
14853                       /* Prefer to use notes, since the def-use chains
14854                          are often shorter.  */
14855                       note = find_reg_equal_equiv_note (def_insn);
14856                       if (note)
14857                         newx = XEXP (note, 0);
14858                       else
14859                         newx = SET_SRC (set);
14860                       newx = r10k_simplify_address (newx, def_insn);
14861                     }
14862                 }
14863             }
14864         }
14865     }
14866   if (newx && r10k_simplified_address_p (newx))
14867     return newx;
14868   return x;
14869 }
14870
14871 /* Return true if ADDRESS is known to be an uncached address
14872    on R10K systems.  */
14873
14874 static bool
14875 r10k_uncached_address_p (unsigned HOST_WIDE_INT address)
14876 {
14877   unsigned HOST_WIDE_INT upper;
14878
14879   /* Check for KSEG1.  */
14880   if (address + 0x60000000 < 0x20000000)
14881     return true;
14882
14883   /* Check for uncached XKPHYS addresses.  */
14884   if (Pmode == DImode)
14885     {
14886       upper = (address >> 40) & 0xf9ffff;
14887       if (upper == 0x900000 || upper == 0xb80000)
14888         return true;
14889     }
14890   return false;
14891 }
14892
14893 /* Return true if we can prove that an access to address X in instruction
14894    INSN would be safe from R10K speculation.  This X is a general
14895    expression; it might not be a legitimate address.  */
14896
14897 static bool
14898 r10k_safe_address_p (rtx x, rtx insn)
14899 {
14900   rtx base, offset;
14901   HOST_WIDE_INT offset_val;
14902
14903   x = r10k_simplify_address (x, insn);
14904
14905   /* Check for references to the stack frame.  It doesn't really matter
14906      how much of the frame has been allocated at INSN; -mr10k-cache-barrier
14907      allows us to assume that accesses to any part of the eventual frame
14908      is safe from speculation at any point in the function.  */
14909   mips_split_plus (x, &base, &offset_val);
14910   if (base == virtual_incoming_args_rtx
14911       && offset_val >= -cfun->machine->frame.total_size
14912       && offset_val < cfun->machine->frame.args_size)
14913     return true;
14914
14915   /* Check for uncached addresses.  */
14916   if (CONST_INT_P (x))
14917     return r10k_uncached_address_p (INTVAL (x));
14918
14919   /* Check for accesses to a static object.  */
14920   split_const (x, &base, &offset);
14921   return offset_within_block_p (base, INTVAL (offset));
14922 }
14923
14924 /* Return true if a MEM with MEM_EXPR EXPR and MEM_OFFSET OFFSET is
14925    an in-range access to an automatic variable, or to an object with
14926    a link-time-constant address.  */
14927
14928 static bool
14929 r10k_safe_mem_expr_p (tree expr, HOST_WIDE_INT offset)
14930 {
14931   HOST_WIDE_INT bitoffset, bitsize;
14932   tree inner, var_offset;
14933   enum machine_mode mode;
14934   int unsigned_p, volatile_p;
14935
14936   inner = get_inner_reference (expr, &bitsize, &bitoffset, &var_offset, &mode,
14937                                &unsigned_p, &volatile_p, false);
14938   if (!DECL_P (inner) || !DECL_SIZE_UNIT (inner) || var_offset)
14939     return false;
14940
14941   offset += bitoffset / BITS_PER_UNIT;
14942   return offset >= 0 && offset < tree_low_cst (DECL_SIZE_UNIT (inner), 1);
14943 }
14944
14945 /* A for_each_rtx callback for which DATA points to the instruction
14946    containing *X.  Stop the search if we find a MEM that is not safe
14947    from R10K speculation.  */
14948
14949 static int
14950 r10k_needs_protection_p_1 (rtx *loc, void *data)
14951 {
14952   rtx mem;
14953
14954   mem = *loc;
14955   if (!MEM_P (mem))
14956     return 0;
14957
14958   if (MEM_EXPR (mem)
14959       && MEM_OFFSET_KNOWN_P (mem)
14960       && r10k_safe_mem_expr_p (MEM_EXPR (mem), MEM_OFFSET (mem)))
14961     return -1;
14962
14963   if (r10k_safe_address_p (XEXP (mem, 0), (rtx) data))
14964     return -1;
14965
14966   return 1;
14967 }
14968
14969 /* A note_stores callback for which DATA points to an instruction pointer.
14970    If *DATA is nonnull, make it null if it X contains a MEM that is not
14971    safe from R10K speculation.  */
14972
14973 static void
14974 r10k_needs_protection_p_store (rtx x, const_rtx pat ATTRIBUTE_UNUSED,
14975                                void *data)
14976 {
14977   rtx *insn_ptr;
14978
14979   insn_ptr = (rtx *) data;
14980   if (*insn_ptr && for_each_rtx (&x, r10k_needs_protection_p_1, *insn_ptr))
14981     *insn_ptr = NULL_RTX;
14982 }
14983
14984 /* A for_each_rtx callback that iterates over the pattern of a CALL_INSN.
14985    Return nonzero if the call is not to a declared function.  */
14986
14987 static int
14988 r10k_needs_protection_p_call (rtx *loc, void *data ATTRIBUTE_UNUSED)
14989 {
14990   rtx x;
14991
14992   x = *loc;
14993   if (!MEM_P (x))
14994     return 0;
14995
14996   x = XEXP (x, 0);
14997   if (GET_CODE (x) == SYMBOL_REF && SYMBOL_REF_DECL (x))
14998     return -1;
14999
15000   return 1;
15001 }
15002
15003 /* Return true if instruction INSN needs to be protected by an R10K
15004    cache barrier.  */
15005
15006 static bool
15007 r10k_needs_protection_p (rtx insn)
15008 {
15009   if (CALL_P (insn))
15010     return for_each_rtx (&PATTERN (insn), r10k_needs_protection_p_call, NULL);
15011
15012   if (mips_r10k_cache_barrier == R10K_CACHE_BARRIER_STORE)
15013     {
15014       note_stores (PATTERN (insn), r10k_needs_protection_p_store, &insn);
15015       return insn == NULL_RTX;
15016     }
15017
15018   return for_each_rtx (&PATTERN (insn), r10k_needs_protection_p_1, insn);
15019 }
15020
15021 /* Return true if BB is only reached by blocks in PROTECTED_BBS and if every
15022    edge is unconditional.  */
15023
15024 static bool
15025 r10k_protected_bb_p (basic_block bb, sbitmap protected_bbs)
15026 {
15027   edge_iterator ei;
15028   edge e;
15029
15030   FOR_EACH_EDGE (e, ei, bb->preds)
15031     if (!single_succ_p (e->src)
15032         || !bitmap_bit_p (protected_bbs, e->src->index)
15033         || (e->flags & EDGE_COMPLEX) != 0)
15034       return false;
15035   return true;
15036 }
15037
15038 /* Implement -mr10k-cache-barrier= for the current function.  */
15039
15040 static void
15041 r10k_insert_cache_barriers (void)
15042 {
15043   int *rev_post_order;
15044   unsigned int i, n;
15045   basic_block bb;
15046   sbitmap protected_bbs;
15047   rtx insn, end, unprotected_region;
15048
15049   if (TARGET_MIPS16)
15050     {
15051       sorry ("%qs does not support MIPS16 code", "-mr10k-cache-barrier");
15052       return;
15053     }
15054
15055   /* Calculate dominators.  */
15056   calculate_dominance_info (CDI_DOMINATORS);
15057
15058   /* Bit X of PROTECTED_BBS is set if the last operation in basic block
15059      X is protected by a cache barrier.  */
15060   protected_bbs = sbitmap_alloc (last_basic_block);
15061   bitmap_clear (protected_bbs);
15062
15063   /* Iterate over the basic blocks in reverse post-order.  */
15064   rev_post_order = XNEWVEC (int, last_basic_block);
15065   n = pre_and_rev_post_order_compute (NULL, rev_post_order, false);
15066   for (i = 0; i < n; i++)
15067     {
15068       bb = BASIC_BLOCK (rev_post_order[i]);
15069
15070       /* If this block is only reached by unconditional edges, and if the
15071          source of every edge is protected, the beginning of the block is
15072          also protected.  */
15073       if (r10k_protected_bb_p (bb, protected_bbs))
15074         unprotected_region = NULL_RTX;
15075       else
15076         unprotected_region = pc_rtx;
15077       end = NEXT_INSN (BB_END (bb));
15078
15079       /* UNPROTECTED_REGION is:
15080
15081          - null if we are processing a protected region,
15082          - pc_rtx if we are processing an unprotected region but have
15083            not yet found the first instruction in it
15084          - the first instruction in an unprotected region otherwise.  */
15085       for (insn = BB_HEAD (bb); insn != end; insn = NEXT_INSN (insn))
15086         {
15087           if (unprotected_region && USEFUL_INSN_P (insn))
15088             {
15089               if (recog_memoized (insn) == CODE_FOR_mips_cache)
15090                 /* This CACHE instruction protects the following code.  */
15091                 unprotected_region = NULL_RTX;
15092               else
15093                 {
15094                   /* See if INSN is the first instruction in this
15095                      unprotected region.  */
15096                   if (unprotected_region == pc_rtx)
15097                     unprotected_region = insn;
15098
15099                   /* See if INSN needs to be protected.  If so,
15100                      we must insert a cache barrier somewhere between
15101                      PREV_INSN (UNPROTECTED_REGION) and INSN.  It isn't
15102                      clear which position is better performance-wise,
15103                      but as a tie-breaker, we assume that it is better
15104                      to allow delay slots to be back-filled where
15105                      possible, and that it is better not to insert
15106                      barriers in the middle of already-scheduled code.
15107                      We therefore insert the barrier at the beginning
15108                      of the region.  */
15109                   if (r10k_needs_protection_p (insn))
15110                     {
15111                       emit_insn_before (gen_r10k_cache_barrier (),
15112                                         unprotected_region);
15113                       unprotected_region = NULL_RTX;
15114                     }
15115                 }
15116             }
15117
15118           if (CALL_P (insn))
15119             /* The called function is not required to protect the exit path.
15120                The code that follows a call is therefore unprotected.  */
15121             unprotected_region = pc_rtx;
15122         }
15123
15124       /* Record whether the end of this block is protected.  */
15125       if (unprotected_region == NULL_RTX)
15126         bitmap_set_bit (protected_bbs, bb->index);
15127     }
15128   XDELETEVEC (rev_post_order);
15129
15130   sbitmap_free (protected_bbs);
15131
15132   free_dominance_info (CDI_DOMINATORS);
15133 }
15134 \f
15135 /* If INSN is a call, return the underlying CALL expr.  Return NULL_RTX
15136    otherwise.  If INSN has two call rtx, then store the second one in
15137    SECOND_CALL.  */
15138
15139 static rtx
15140 mips_call_expr_from_insn (rtx insn, rtx *second_call)
15141 {
15142   rtx x;
15143   rtx x2;
15144
15145   if (!CALL_P (insn))
15146     return NULL_RTX;
15147
15148   x = PATTERN (insn);
15149   if (GET_CODE (x) == PARALLEL)
15150     {
15151       /* Calls returning complex values have two CALL rtx.  Look for the second
15152          one here, and return it via the SECOND_CALL arg.  */
15153       x2 = XVECEXP (x, 0, 1);
15154       if (GET_CODE (x2) == SET)
15155         x2 = XEXP (x2, 1);
15156       if (GET_CODE (x2) == CALL)
15157         *second_call = x2;
15158
15159       x = XVECEXP (x, 0, 0);
15160     }
15161   if (GET_CODE (x) == SET)
15162     x = XEXP (x, 1);
15163   gcc_assert (GET_CODE (x) == CALL);
15164
15165   return x;
15166 }
15167
15168 /* REG is set in DEF.  See if the definition is one of the ways we load a
15169    register with a symbol address for a mips_use_pic_fn_addr_reg_p call.
15170    If it is, return the symbol reference of the function, otherwise return
15171    NULL_RTX.
15172
15173    If RECURSE_P is true, use mips_find_pic_call_symbol to interpret
15174    the values of source registers, otherwise treat such registers as
15175    having an unknown value.  */
15176
15177 static rtx
15178 mips_pic_call_symbol_from_set (df_ref def, rtx reg, bool recurse_p)
15179 {
15180   rtx def_insn, set;
15181
15182   if (DF_REF_IS_ARTIFICIAL (def))
15183     return NULL_RTX;
15184
15185   def_insn = DF_REF_INSN (def);
15186   set = single_set (def_insn);
15187   if (set && rtx_equal_p (SET_DEST (set), reg))
15188     {
15189       rtx note, src, symbol;
15190
15191       /* First see whether the source is a plain symbol.  This is used
15192          when calling symbols that are not lazily bound.  */
15193       src = SET_SRC (set);
15194       if (GET_CODE (src) == SYMBOL_REF)
15195         return src;
15196
15197       /* Handle %call16 references.  */
15198       symbol = mips_strip_unspec_call (src);
15199       if (symbol)
15200         {
15201           gcc_assert (GET_CODE (symbol) == SYMBOL_REF);
15202           return symbol;
15203         }
15204
15205       /* If we have something more complicated, look for a
15206          REG_EQUAL or REG_EQUIV note.  */
15207       note = find_reg_equal_equiv_note (def_insn);
15208       if (note && GET_CODE (XEXP (note, 0)) == SYMBOL_REF)
15209         return XEXP (note, 0);
15210
15211       /* Follow at most one simple register copy.  Such copies are
15212          interesting in cases like:
15213
15214              for (...)
15215                {
15216                  locally_binding_fn (...);
15217                }
15218
15219          and:
15220
15221              locally_binding_fn (...);
15222              ...
15223              locally_binding_fn (...);
15224
15225          where the load of locally_binding_fn can legitimately be
15226          hoisted or shared.  However, we do not expect to see complex
15227          chains of copies, so a full worklist solution to the problem
15228          would probably be overkill.  */
15229       if (recurse_p && REG_P (src))
15230         return mips_find_pic_call_symbol (def_insn, src, false);
15231     }
15232
15233   return NULL_RTX;
15234 }
15235
15236 /* Find the definition of the use of REG in INSN.  See if the definition
15237    is one of the ways we load a register with a symbol address for a
15238    mips_use_pic_fn_addr_reg_p call.  If it is return the symbol reference
15239    of the function, otherwise return NULL_RTX.  RECURSE_P is as for
15240    mips_pic_call_symbol_from_set.  */
15241
15242 static rtx
15243 mips_find_pic_call_symbol (rtx insn, rtx reg, bool recurse_p)
15244 {
15245   df_ref use;
15246   struct df_link *defs;
15247   rtx symbol;
15248
15249   use = df_find_use (insn, regno_reg_rtx[REGNO (reg)]);
15250   if (!use)
15251     return NULL_RTX;
15252   defs = DF_REF_CHAIN (use);
15253   if (!defs)
15254     return NULL_RTX;
15255   symbol = mips_pic_call_symbol_from_set (defs->ref, reg, recurse_p);
15256   if (!symbol)
15257     return NULL_RTX;
15258
15259   /* If we have more than one definition, they need to be identical.  */
15260   for (defs = defs->next; defs; defs = defs->next)
15261     {
15262       rtx other;
15263
15264       other = mips_pic_call_symbol_from_set (defs->ref, reg, recurse_p);
15265       if (!rtx_equal_p (symbol, other))
15266         return NULL_RTX;
15267     }
15268
15269   return symbol;
15270 }
15271
15272 /* Replace the args_size operand of the call expression CALL with the
15273    call-attribute UNSPEC and fill in SYMBOL as the function symbol.  */
15274
15275 static void
15276 mips_annotate_pic_call_expr (rtx call, rtx symbol)
15277 {
15278   rtx args_size;
15279
15280   args_size = XEXP (call, 1);
15281   XEXP (call, 1) = gen_rtx_UNSPEC (GET_MODE (args_size),
15282                                    gen_rtvec (2, args_size, symbol),
15283                                    UNSPEC_CALL_ATTR);
15284 }
15285
15286 /* OPERANDS[ARGS_SIZE_OPNO] is the arg_size operand of a CALL expression.  See
15287    if instead of the arg_size argument it contains the call attributes.  If
15288    yes return true along with setting OPERANDS[ARGS_SIZE_OPNO] to the function
15289    symbol from the call attributes.  Also return false if ARGS_SIZE_OPNO is
15290    -1.  */
15291
15292 bool
15293 mips_get_pic_call_symbol (rtx *operands, int args_size_opno)
15294 {
15295   rtx args_size, symbol;
15296
15297   if (!TARGET_RELAX_PIC_CALLS || args_size_opno == -1)
15298     return false;
15299
15300   args_size = operands[args_size_opno];
15301   if (GET_CODE (args_size) != UNSPEC)
15302     return false;
15303   gcc_assert (XINT (args_size, 1) == UNSPEC_CALL_ATTR);
15304
15305   symbol = XVECEXP (args_size, 0, 1);
15306   gcc_assert (GET_CODE (symbol) == SYMBOL_REF);
15307
15308   operands[args_size_opno] = symbol;
15309   return true;
15310 }
15311
15312 /* Use DF to annotate PIC indirect calls with the function symbol they
15313    dispatch to.  */
15314
15315 static void
15316 mips_annotate_pic_calls (void)
15317 {
15318   basic_block bb;
15319   rtx insn;
15320
15321   FOR_EACH_BB (bb)
15322     FOR_BB_INSNS (bb, insn)
15323     {
15324       rtx call, reg, symbol, second_call;
15325
15326       second_call = 0;
15327       call = mips_call_expr_from_insn (insn, &second_call);
15328       if (!call)
15329         continue;
15330       gcc_assert (MEM_P (XEXP (call, 0)));
15331       reg = XEXP (XEXP (call, 0), 0);
15332       if (!REG_P (reg))
15333         continue;
15334
15335       symbol = mips_find_pic_call_symbol (insn, reg, true);
15336       if (symbol)
15337         {
15338           mips_annotate_pic_call_expr (call, symbol);
15339           if (second_call)
15340             mips_annotate_pic_call_expr (second_call, symbol);
15341         }
15342     }
15343 }
15344 \f
15345 /* A temporary variable used by for_each_rtx callbacks, etc.  */
15346 static rtx mips_sim_insn;
15347
15348 /* A structure representing the state of the processor pipeline.
15349    Used by the mips_sim_* family of functions.  */
15350 struct mips_sim {
15351   /* The maximum number of instructions that can be issued in a cycle.
15352      (Caches mips_issue_rate.)  */
15353   unsigned int issue_rate;
15354
15355   /* The current simulation time.  */
15356   unsigned int time;
15357
15358   /* How many more instructions can be issued in the current cycle.  */
15359   unsigned int insns_left;
15360
15361   /* LAST_SET[X].INSN is the last instruction to set register X.
15362      LAST_SET[X].TIME is the time at which that instruction was issued.
15363      INSN is null if no instruction has yet set register X.  */
15364   struct {
15365     rtx insn;
15366     unsigned int time;
15367   } last_set[FIRST_PSEUDO_REGISTER];
15368
15369   /* The pipeline's current DFA state.  */
15370   state_t dfa_state;
15371 };
15372
15373 /* Reset STATE to the initial simulation state.  */
15374
15375 static void
15376 mips_sim_reset (struct mips_sim *state)
15377 {
15378   curr_state = state->dfa_state;
15379
15380   state->time = 0;
15381   state->insns_left = state->issue_rate;
15382   memset (&state->last_set, 0, sizeof (state->last_set));
15383   state_reset (curr_state);
15384
15385   targetm.sched.init (0, false, 0);
15386   advance_state (curr_state);
15387 }
15388
15389 /* Initialize STATE before its first use.  DFA_STATE points to an
15390    allocated but uninitialized DFA state.  */
15391
15392 static void
15393 mips_sim_init (struct mips_sim *state, state_t dfa_state)
15394 {
15395   if (targetm.sched.init_dfa_pre_cycle_insn)
15396     targetm.sched.init_dfa_pre_cycle_insn ();
15397
15398   if (targetm.sched.init_dfa_post_cycle_insn)
15399     targetm.sched.init_dfa_post_cycle_insn ();
15400
15401   state->issue_rate = mips_issue_rate ();
15402   state->dfa_state = dfa_state;
15403   mips_sim_reset (state);
15404 }
15405
15406 /* Advance STATE by one clock cycle.  */
15407
15408 static void
15409 mips_sim_next_cycle (struct mips_sim *state)
15410 {
15411   curr_state = state->dfa_state;
15412
15413   state->time++;
15414   state->insns_left = state->issue_rate;
15415   advance_state (curr_state);
15416 }
15417
15418 /* Advance simulation state STATE until instruction INSN can read
15419    register REG.  */
15420
15421 static void
15422 mips_sim_wait_reg (struct mips_sim *state, rtx insn, rtx reg)
15423 {
15424   unsigned int regno, end_regno;
15425
15426   end_regno = END_REGNO (reg);
15427   for (regno = REGNO (reg); regno < end_regno; regno++)
15428     if (state->last_set[regno].insn != 0)
15429       {
15430         unsigned int t;
15431
15432         t = (state->last_set[regno].time
15433              + insn_latency (state->last_set[regno].insn, insn));
15434         while (state->time < t)
15435           mips_sim_next_cycle (state);
15436     }
15437 }
15438
15439 /* A for_each_rtx callback.  If *X is a register, advance simulation state
15440    DATA until mips_sim_insn can read the register's value.  */
15441
15442 static int
15443 mips_sim_wait_regs_2 (rtx *x, void *data)
15444 {
15445   if (REG_P (*x))
15446     mips_sim_wait_reg ((struct mips_sim *) data, mips_sim_insn, *x);
15447   return 0;
15448 }
15449
15450 /* Call mips_sim_wait_regs_2 (R, DATA) for each register R mentioned in *X.  */
15451
15452 static void
15453 mips_sim_wait_regs_1 (rtx *x, void *data)
15454 {
15455   for_each_rtx (x, mips_sim_wait_regs_2, data);
15456 }
15457
15458 /* Advance simulation state STATE until all of INSN's register
15459    dependencies are satisfied.  */
15460
15461 static void
15462 mips_sim_wait_regs (struct mips_sim *state, rtx insn)
15463 {
15464   mips_sim_insn = insn;
15465   note_uses (&PATTERN (insn), mips_sim_wait_regs_1, state);
15466 }
15467
15468 /* Advance simulation state STATE until the units required by
15469    instruction INSN are available.  */
15470
15471 static void
15472 mips_sim_wait_units (struct mips_sim *state, rtx insn)
15473 {
15474   state_t tmp_state;
15475
15476   tmp_state = alloca (state_size ());
15477   while (state->insns_left == 0
15478          || (memcpy (tmp_state, state->dfa_state, state_size ()),
15479              state_transition (tmp_state, insn) >= 0))
15480     mips_sim_next_cycle (state);
15481 }
15482
15483 /* Advance simulation state STATE until INSN is ready to issue.  */
15484
15485 static void
15486 mips_sim_wait_insn (struct mips_sim *state, rtx insn)
15487 {
15488   mips_sim_wait_regs (state, insn);
15489   mips_sim_wait_units (state, insn);
15490 }
15491
15492 /* mips_sim_insn has just set X.  Update the LAST_SET array
15493    in simulation state DATA.  */
15494
15495 static void
15496 mips_sim_record_set (rtx x, const_rtx pat ATTRIBUTE_UNUSED, void *data)
15497 {
15498   struct mips_sim *state;
15499
15500   state = (struct mips_sim *) data;
15501   if (REG_P (x))
15502     {
15503       unsigned int regno, end_regno;
15504
15505       end_regno = END_REGNO (x);
15506       for (regno = REGNO (x); regno < end_regno; regno++)
15507         {
15508           state->last_set[regno].insn = mips_sim_insn;
15509           state->last_set[regno].time = state->time;
15510         }
15511     }
15512 }
15513
15514 /* Issue instruction INSN in scheduler state STATE.  Assume that INSN
15515    can issue immediately (i.e., that mips_sim_wait_insn has already
15516    been called).  */
15517
15518 static void
15519 mips_sim_issue_insn (struct mips_sim *state, rtx insn)
15520 {
15521   curr_state = state->dfa_state;
15522
15523   state_transition (curr_state, insn);
15524   state->insns_left = targetm.sched.variable_issue (0, false, insn,
15525                                                     state->insns_left);
15526
15527   mips_sim_insn = insn;
15528   note_stores (PATTERN (insn), mips_sim_record_set, state);
15529 }
15530
15531 /* Simulate issuing a NOP in state STATE.  */
15532
15533 static void
15534 mips_sim_issue_nop (struct mips_sim *state)
15535 {
15536   if (state->insns_left == 0)
15537     mips_sim_next_cycle (state);
15538   state->insns_left--;
15539 }
15540
15541 /* Update simulation state STATE so that it's ready to accept the instruction
15542    after INSN.  INSN should be part of the main rtl chain, not a member of a
15543    SEQUENCE.  */
15544
15545 static void
15546 mips_sim_finish_insn (struct mips_sim *state, rtx insn)
15547 {
15548   /* If INSN is a jump with an implicit delay slot, simulate a nop.  */
15549   if (JUMP_P (insn))
15550     mips_sim_issue_nop (state);
15551
15552   switch (GET_CODE (SEQ_BEGIN (insn)))
15553     {
15554     case CODE_LABEL:
15555     case CALL_INSN:
15556       /* We can't predict the processor state after a call or label.  */
15557       mips_sim_reset (state);
15558       break;
15559
15560     case JUMP_INSN:
15561       /* The delay slots of branch likely instructions are only executed
15562          when the branch is taken.  Therefore, if the caller has simulated
15563          the delay slot instruction, STATE does not really reflect the state
15564          of the pipeline for the instruction after the delay slot.  Also,
15565          branch likely instructions tend to incur a penalty when not taken,
15566          so there will probably be an extra delay between the branch and
15567          the instruction after the delay slot.  */
15568       if (INSN_ANNULLED_BRANCH_P (SEQ_BEGIN (insn)))
15569         mips_sim_reset (state);
15570       break;
15571
15572     default:
15573       break;
15574     }
15575 }
15576
15577 /* Use simulator state STATE to calculate the execution time of
15578    instruction sequence SEQ.  */
15579
15580 static unsigned int
15581 mips_seq_time (struct mips_sim *state, rtx seq)
15582 {
15583   mips_sim_reset (state);
15584   for (rtx insn = seq; insn; insn = NEXT_INSN (insn))
15585     {
15586       mips_sim_wait_insn (state, insn);
15587       mips_sim_issue_insn (state, insn);
15588     }
15589   return state->time;
15590 }
15591 \f
15592 /* Return the execution-time cost of mips_tuning_info.fast_mult_zero_zero_p
15593    setting SETTING, using STATE to simulate instruction sequences.  */
15594
15595 static unsigned int
15596 mips_mult_zero_zero_cost (struct mips_sim *state, bool setting)
15597 {
15598   mips_tuning_info.fast_mult_zero_zero_p = setting;
15599   start_sequence ();
15600
15601   enum machine_mode dword_mode = TARGET_64BIT ? TImode : DImode;
15602   rtx hilo = gen_rtx_REG (dword_mode, MD_REG_FIRST);
15603   mips_emit_move_or_split (hilo, const0_rtx, SPLIT_FOR_SPEED);
15604
15605   /* If the target provides mulsidi3_32bit then that's the most likely
15606      consumer of the result.  Test for bypasses.  */
15607   if (dword_mode == DImode && HAVE_maddsidi4)
15608     {
15609       rtx gpr = gen_rtx_REG (SImode, GP_REG_FIRST + 4);
15610       emit_insn (gen_maddsidi4 (hilo, gpr, gpr, hilo));
15611     }
15612
15613   unsigned int time = mips_seq_time (state, get_insns ());
15614   end_sequence ();
15615   return time;
15616 }
15617
15618 /* Check the relative speeds of "MULT $0,$0" and "MTLO $0; MTHI $0"
15619    and set up mips_tuning_info.fast_mult_zero_zero_p accordingly.
15620    Prefer MULT -- which is shorter -- in the event of a tie.  */
15621
15622 static void
15623 mips_set_fast_mult_zero_zero_p (struct mips_sim *state)
15624 {
15625   if (TARGET_MIPS16)
15626     /* No MTLO or MTHI available.  */
15627     mips_tuning_info.fast_mult_zero_zero_p = true;
15628   else
15629     {
15630       unsigned int true_time = mips_mult_zero_zero_cost (state, true);
15631       unsigned int false_time = mips_mult_zero_zero_cost (state, false);
15632       mips_tuning_info.fast_mult_zero_zero_p = (true_time <= false_time);
15633     }
15634 }
15635
15636 /* Set up costs based on the current architecture and tuning settings.  */
15637
15638 static void
15639 mips_set_tuning_info (void)
15640 {
15641   if (mips_tuning_info.initialized_p
15642       && mips_tuning_info.arch == mips_arch
15643       && mips_tuning_info.tune == mips_tune
15644       && mips_tuning_info.mips16_p == TARGET_MIPS16)
15645     return;
15646
15647   mips_tuning_info.arch = mips_arch;
15648   mips_tuning_info.tune = mips_tune;
15649   mips_tuning_info.mips16_p = TARGET_MIPS16;
15650   mips_tuning_info.initialized_p = true;
15651
15652   dfa_start ();
15653
15654   struct mips_sim state;
15655   mips_sim_init (&state, alloca (state_size ()));
15656
15657   mips_set_fast_mult_zero_zero_p (&state);
15658
15659   dfa_finish ();
15660 }
15661
15662 /* Implement TARGET_EXPAND_TO_RTL_HOOK.  */
15663
15664 static void
15665 mips_expand_to_rtl_hook (void)
15666 {
15667   /* We need to call this at a point where we can safely create sequences
15668      of instructions, so TARGET_OVERRIDE_OPTIONS is too early.  We also
15669      need to call it at a point where the DFA infrastructure is not
15670      already in use, so we can't just call it lazily on demand.
15671
15672      At present, mips_tuning_info is only needed during post-expand
15673      RTL passes such as split_insns, so this hook should be early enough.
15674      We may need to move the call elsewhere if mips_tuning_info starts
15675      to be used for other things (such as rtx_costs, or expanders that
15676      could be called during gimple optimization).  */
15677   mips_set_tuning_info ();
15678 }
15679 \f
15680 /* The VR4130 pipeline issues aligned pairs of instructions together,
15681    but it stalls the second instruction if it depends on the first.
15682    In order to cut down the amount of logic required, this dependence
15683    check is not based on a full instruction decode.  Instead, any non-SPECIAL
15684    instruction is assumed to modify the register specified by bits 20-16
15685    (which is usually the "rt" field).
15686
15687    In BEQ, BEQL, BNE and BNEL instructions, the rt field is actually an
15688    input, so we can end up with a false dependence between the branch
15689    and its delay slot.  If this situation occurs in instruction INSN,
15690    try to avoid it by swapping rs and rt.  */
15691
15692 static void
15693 vr4130_avoid_branch_rt_conflict (rtx insn)
15694 {
15695   rtx first, second;
15696
15697   first = SEQ_BEGIN (insn);
15698   second = SEQ_END (insn);
15699   if (JUMP_P (first)
15700       && NONJUMP_INSN_P (second)
15701       && GET_CODE (PATTERN (first)) == SET
15702       && GET_CODE (SET_DEST (PATTERN (first))) == PC
15703       && GET_CODE (SET_SRC (PATTERN (first))) == IF_THEN_ELSE)
15704     {
15705       /* Check for the right kind of condition.  */
15706       rtx cond = XEXP (SET_SRC (PATTERN (first)), 0);
15707       if ((GET_CODE (cond) == EQ || GET_CODE (cond) == NE)
15708           && REG_P (XEXP (cond, 0))
15709           && REG_P (XEXP (cond, 1))
15710           && reg_referenced_p (XEXP (cond, 1), PATTERN (second))
15711           && !reg_referenced_p (XEXP (cond, 0), PATTERN (second)))
15712         {
15713           /* SECOND mentions the rt register but not the rs register.  */
15714           rtx tmp = XEXP (cond, 0);
15715           XEXP (cond, 0) = XEXP (cond, 1);
15716           XEXP (cond, 1) = tmp;
15717         }
15718     }
15719 }
15720
15721 /* Implement -mvr4130-align.  Go through each basic block and simulate the
15722    processor pipeline.  If we find that a pair of instructions could execute
15723    in parallel, and the first of those instructions is not 8-byte aligned,
15724    insert a nop to make it aligned.  */
15725
15726 static void
15727 vr4130_align_insns (void)
15728 {
15729   struct mips_sim state;
15730   rtx insn, subinsn, last, last2, next;
15731   bool aligned_p;
15732
15733   dfa_start ();
15734
15735   /* LAST is the last instruction before INSN to have a nonzero length.
15736      LAST2 is the last such instruction before LAST.  */
15737   last = 0;
15738   last2 = 0;
15739
15740   /* ALIGNED_P is true if INSN is known to be at an aligned address.  */
15741   aligned_p = true;
15742
15743   mips_sim_init (&state, alloca (state_size ()));
15744   for (insn = get_insns (); insn != 0; insn = next)
15745     {
15746       unsigned int length;
15747
15748       next = NEXT_INSN (insn);
15749
15750       /* See the comment above vr4130_avoid_branch_rt_conflict for details.
15751          This isn't really related to the alignment pass, but we do it on
15752          the fly to avoid a separate instruction walk.  */
15753       vr4130_avoid_branch_rt_conflict (insn);
15754
15755       length = get_attr_length (insn);
15756       if (length > 0 && USEFUL_INSN_P (insn))
15757         FOR_EACH_SUBINSN (subinsn, insn)
15758           {
15759             mips_sim_wait_insn (&state, subinsn);
15760
15761             /* If we want this instruction to issue in parallel with the
15762                previous one, make sure that the previous instruction is
15763                aligned.  There are several reasons why this isn't worthwhile
15764                when the second instruction is a call:
15765
15766                   - Calls are less likely to be performance critical,
15767                   - There's a good chance that the delay slot can execute
15768                     in parallel with the call.
15769                   - The return address would then be unaligned.
15770
15771                In general, if we're going to insert a nop between instructions
15772                X and Y, it's better to insert it immediately after X.  That
15773                way, if the nop makes Y aligned, it will also align any labels
15774                between X and Y.  */
15775             if (state.insns_left != state.issue_rate
15776                 && !CALL_P (subinsn))
15777               {
15778                 if (subinsn == SEQ_BEGIN (insn) && aligned_p)
15779                   {
15780                     /* SUBINSN is the first instruction in INSN and INSN is
15781                        aligned.  We want to align the previous instruction
15782                        instead, so insert a nop between LAST2 and LAST.
15783
15784                        Note that LAST could be either a single instruction
15785                        or a branch with a delay slot.  In the latter case,
15786                        LAST, like INSN, is already aligned, but the delay
15787                        slot must have some extra delay that stops it from
15788                        issuing at the same time as the branch.  We therefore
15789                        insert a nop before the branch in order to align its
15790                        delay slot.  */
15791                     gcc_assert (last2);
15792                     emit_insn_after (gen_nop (), last2);
15793                     aligned_p = false;
15794                   }
15795                 else if (subinsn != SEQ_BEGIN (insn) && !aligned_p)
15796                   {
15797                     /* SUBINSN is the delay slot of INSN, but INSN is
15798                        currently unaligned.  Insert a nop between
15799                        LAST and INSN to align it.  */
15800                     gcc_assert (last);
15801                     emit_insn_after (gen_nop (), last);
15802                     aligned_p = true;
15803                   }
15804               }
15805             mips_sim_issue_insn (&state, subinsn);
15806           }
15807       mips_sim_finish_insn (&state, insn);
15808
15809       /* Update LAST, LAST2 and ALIGNED_P for the next instruction.  */
15810       length = get_attr_length (insn);
15811       if (length > 0)
15812         {
15813           /* If the instruction is an asm statement or multi-instruction
15814              mips.md patern, the length is only an estimate.  Insert an
15815              8 byte alignment after it so that the following instructions
15816              can be handled correctly.  */
15817           if (NONJUMP_INSN_P (SEQ_BEGIN (insn))
15818               && (recog_memoized (insn) < 0 || length >= 8))
15819             {
15820               next = emit_insn_after (gen_align (GEN_INT (3)), insn);
15821               next = NEXT_INSN (next);
15822               mips_sim_next_cycle (&state);
15823               aligned_p = true;
15824             }
15825           else if (length & 4)
15826             aligned_p = !aligned_p;
15827           last2 = last;
15828           last = insn;
15829         }
15830
15831       /* See whether INSN is an aligned label.  */
15832       if (LABEL_P (insn) && label_to_alignment (insn) >= 3)
15833         aligned_p = true;
15834     }
15835   dfa_finish ();
15836 }
15837 \f
15838 /* This structure records that the current function has a LO_SUM
15839    involving SYMBOL_REF or LABEL_REF BASE and that MAX_OFFSET is
15840    the largest offset applied to BASE by all such LO_SUMs.  */
15841 struct mips_lo_sum_offset {
15842   rtx base;
15843   HOST_WIDE_INT offset;
15844 };
15845
15846 /* Return a hash value for SYMBOL_REF or LABEL_REF BASE.  */
15847
15848 static hashval_t
15849 mips_hash_base (rtx base)
15850 {
15851   int do_not_record_p;
15852
15853   return hash_rtx (base, GET_MODE (base), &do_not_record_p, NULL, false);
15854 }
15855
15856 /* Hashtable helpers.  */
15857
15858 struct mips_lo_sum_offset_hasher : typed_free_remove <mips_lo_sum_offset>
15859 {
15860   typedef mips_lo_sum_offset value_type;
15861   typedef rtx_def compare_type;
15862   static inline hashval_t hash (const value_type *);
15863   static inline bool equal (const value_type *, const compare_type *);
15864 };
15865
15866 /* Hash-table callbacks for mips_lo_sum_offsets.  */
15867
15868 inline hashval_t
15869 mips_lo_sum_offset_hasher::hash (const value_type *entry)
15870 {
15871   return mips_hash_base (entry->base);
15872 }
15873
15874 inline bool
15875 mips_lo_sum_offset_hasher::equal (const value_type *entry,
15876                                   const compare_type *value)
15877 {
15878   return rtx_equal_p (entry->base, value);
15879 }
15880
15881 typedef hash_table <mips_lo_sum_offset_hasher> mips_offset_table;
15882
15883 /* Look up symbolic constant X in HTAB, which is a hash table of
15884    mips_lo_sum_offsets.  If OPTION is NO_INSERT, return true if X can be
15885    paired with a recorded LO_SUM, otherwise record X in the table.  */
15886
15887 static bool
15888 mips_lo_sum_offset_lookup (mips_offset_table htab, rtx x,
15889                            enum insert_option option)
15890 {
15891   rtx base, offset;
15892   mips_lo_sum_offset **slot;
15893   struct mips_lo_sum_offset *entry;
15894
15895   /* Split X into a base and offset.  */
15896   split_const (x, &base, &offset);
15897   if (UNSPEC_ADDRESS_P (base))
15898     base = UNSPEC_ADDRESS (base);
15899
15900   /* Look up the base in the hash table.  */
15901   slot = htab.find_slot_with_hash (base, mips_hash_base (base), option);
15902   if (slot == NULL)
15903     return false;
15904
15905   entry = (struct mips_lo_sum_offset *) *slot;
15906   if (option == INSERT)
15907     {
15908       if (entry == NULL)
15909         {
15910           entry = XNEW (struct mips_lo_sum_offset);
15911           entry->base = base;
15912           entry->offset = INTVAL (offset);
15913           *slot = entry;
15914         }
15915       else
15916         {
15917           if (INTVAL (offset) > entry->offset)
15918             entry->offset = INTVAL (offset);
15919         }
15920     }
15921   return INTVAL (offset) <= entry->offset;
15922 }
15923
15924 /* A for_each_rtx callback for which DATA is a mips_lo_sum_offset hash table.
15925    Record every LO_SUM in *LOC.  */
15926
15927 static int
15928 mips_record_lo_sum (rtx *loc, void *data)
15929 {
15930   if (GET_CODE (*loc) == LO_SUM)
15931     mips_lo_sum_offset_lookup (*(mips_offset_table*) data,
15932                                XEXP (*loc, 1), INSERT);
15933   return 0;
15934 }
15935
15936 /* Return true if INSN is a SET of an orphaned high-part relocation.
15937    HTAB is a hash table of mips_lo_sum_offsets that describes all the
15938    LO_SUMs in the current function.  */
15939
15940 static bool
15941 mips_orphaned_high_part_p (mips_offset_table htab, rtx insn)
15942 {
15943   enum mips_symbol_type type;
15944   rtx x, set;
15945
15946   set = single_set (insn);
15947   if (set)
15948     {
15949       /* Check for %his.  */
15950       x = SET_SRC (set);
15951       if (GET_CODE (x) == HIGH
15952           && absolute_symbolic_operand (XEXP (x, 0), VOIDmode))
15953         return !mips_lo_sum_offset_lookup (htab, XEXP (x, 0), NO_INSERT);
15954
15955       /* Check for local %gots (and %got_pages, which is redundant but OK).  */
15956       if (GET_CODE (x) == UNSPEC
15957           && XINT (x, 1) == UNSPEC_LOAD_GOT
15958           && mips_symbolic_constant_p (XVECEXP (x, 0, 1),
15959                                        SYMBOL_CONTEXT_LEA, &type)
15960           && type == SYMBOL_GOTOFF_PAGE)
15961         return !mips_lo_sum_offset_lookup (htab, XVECEXP (x, 0, 1), NO_INSERT);
15962     }
15963   return false;
15964 }
15965
15966 /* Subroutine of mips_reorg_process_insns.  If there is a hazard between
15967    INSN and a previous instruction, avoid it by inserting nops after
15968    instruction AFTER.
15969
15970    *DELAYED_REG and *HILO_DELAY describe the hazards that apply at
15971    this point.  If *DELAYED_REG is non-null, INSN must wait a cycle
15972    before using the value of that register.  *HILO_DELAY counts the
15973    number of instructions since the last hilo hazard (that is,
15974    the number of instructions since the last MFLO or MFHI).
15975
15976    After inserting nops for INSN, update *DELAYED_REG and *HILO_DELAY
15977    for the next instruction.
15978
15979    LO_REG is an rtx for the LO register, used in dependence checking.  */
15980
15981 static void
15982 mips_avoid_hazard (rtx after, rtx insn, int *hilo_delay,
15983                    rtx *delayed_reg, rtx lo_reg)
15984 {
15985   rtx pattern, set;
15986   int nops, ninsns;
15987
15988   pattern = PATTERN (insn);
15989
15990   /* Do not put the whole function in .set noreorder if it contains
15991      an asm statement.  We don't know whether there will be hazards
15992      between the asm statement and the gcc-generated code.  */
15993   if (GET_CODE (pattern) == ASM_INPUT || asm_noperands (pattern) >= 0)
15994     cfun->machine->all_noreorder_p = false;
15995
15996   /* Ignore zero-length instructions (barriers and the like).  */
15997   ninsns = get_attr_length (insn) / 4;
15998   if (ninsns == 0)
15999     return;
16000
16001   /* Work out how many nops are needed.  Note that we only care about
16002      registers that are explicitly mentioned in the instruction's pattern.
16003      It doesn't matter that calls use the argument registers or that they
16004      clobber hi and lo.  */
16005   if (*hilo_delay < 2 && reg_set_p (lo_reg, pattern))
16006     nops = 2 - *hilo_delay;
16007   else if (*delayed_reg != 0 && reg_referenced_p (*delayed_reg, pattern))
16008     nops = 1;
16009   else
16010     nops = 0;
16011
16012   /* Insert the nops between this instruction and the previous one.
16013      Each new nop takes us further from the last hilo hazard.  */
16014   *hilo_delay += nops;
16015   while (nops-- > 0)
16016     emit_insn_after (gen_hazard_nop (), after);
16017
16018   /* Set up the state for the next instruction.  */
16019   *hilo_delay += ninsns;
16020   *delayed_reg = 0;
16021   if (INSN_CODE (insn) >= 0)
16022     switch (get_attr_hazard (insn))
16023       {
16024       case HAZARD_NONE:
16025         break;
16026
16027       case HAZARD_HILO:
16028         *hilo_delay = 0;
16029         break;
16030
16031       case HAZARD_DELAY:
16032         set = single_set (insn);
16033         gcc_assert (set);
16034         *delayed_reg = SET_DEST (set);
16035         break;
16036       }
16037 }
16038
16039 /* Go through the instruction stream and insert nops where necessary.
16040    Also delete any high-part relocations whose partnering low parts
16041    are now all dead.  See if the whole function can then be put into
16042    .set noreorder and .set nomacro.  */
16043
16044 static void
16045 mips_reorg_process_insns (void)
16046 {
16047   rtx insn, last_insn, subinsn, next_insn, lo_reg, delayed_reg;
16048   int hilo_delay;
16049   mips_offset_table htab;
16050
16051   /* Force all instructions to be split into their final form.  */
16052   split_all_insns_noflow ();
16053
16054   /* Recalculate instruction lengths without taking nops into account.  */
16055   cfun->machine->ignore_hazard_length_p = true;
16056   shorten_branches (get_insns ());
16057
16058   cfun->machine->all_noreorder_p = true;
16059
16060   /* We don't track MIPS16 PC-relative offsets closely enough to make
16061      a good job of "set .noreorder" code in MIPS16 mode.  */
16062   if (TARGET_MIPS16)
16063     cfun->machine->all_noreorder_p = false;
16064
16065   /* Code that doesn't use explicit relocs can't be ".set nomacro".  */
16066   if (!TARGET_EXPLICIT_RELOCS)
16067     cfun->machine->all_noreorder_p = false;
16068
16069   /* Profiled functions can't be all noreorder because the profiler
16070      support uses assembler macros.  */
16071   if (crtl->profile)
16072     cfun->machine->all_noreorder_p = false;
16073
16074   /* Code compiled with -mfix-vr4120 or -mfix-24k can't be all noreorder
16075      because we rely on the assembler to work around some errata.
16076      The r5900 too has several bugs.  */
16077   if (TARGET_FIX_VR4120 || TARGET_FIX_24K || TARGET_MIPS5900)
16078     cfun->machine->all_noreorder_p = false;
16079
16080   /* The same is true for -mfix-vr4130 if we might generate MFLO or
16081      MFHI instructions.  Note that we avoid using MFLO and MFHI if
16082      the VR4130 MACC and DMACC instructions are available instead;
16083      see the *mfhilo_{si,di}_macc patterns.  */
16084   if (TARGET_FIX_VR4130 && !ISA_HAS_MACCHI)
16085     cfun->machine->all_noreorder_p = false;
16086
16087   htab.create (37);
16088
16089   /* Make a first pass over the instructions, recording all the LO_SUMs.  */
16090   for (insn = get_insns (); insn != 0; insn = NEXT_INSN (insn))
16091     FOR_EACH_SUBINSN (subinsn, insn)
16092       if (USEFUL_INSN_P (subinsn))
16093         for_each_rtx (&PATTERN (subinsn), mips_record_lo_sum, &htab);
16094
16095   last_insn = 0;
16096   hilo_delay = 2;
16097   delayed_reg = 0;
16098   lo_reg = gen_rtx_REG (SImode, LO_REGNUM);
16099
16100   /* Make a second pass over the instructions.  Delete orphaned
16101      high-part relocations or turn them into NOPs.  Avoid hazards
16102      by inserting NOPs.  */
16103   for (insn = get_insns (); insn != 0; insn = next_insn)
16104     {
16105       next_insn = NEXT_INSN (insn);
16106       if (USEFUL_INSN_P (insn))
16107         {
16108           if (GET_CODE (PATTERN (insn)) == SEQUENCE)
16109             {
16110               /* If we find an orphaned high-part relocation in a delay
16111                  slot, it's easier to turn that instruction into a NOP than
16112                  to delete it.  The delay slot will be a NOP either way.  */
16113               FOR_EACH_SUBINSN (subinsn, insn)
16114                 if (INSN_P (subinsn))
16115                   {
16116                     if (mips_orphaned_high_part_p (htab, subinsn))
16117                       {
16118                         PATTERN (subinsn) = gen_nop ();
16119                         INSN_CODE (subinsn) = CODE_FOR_nop;
16120                       }
16121                     mips_avoid_hazard (last_insn, subinsn, &hilo_delay,
16122                                        &delayed_reg, lo_reg);
16123                   }
16124               last_insn = insn;
16125             }
16126           else
16127             {
16128               /* INSN is a single instruction.  Delete it if it's an
16129                  orphaned high-part relocation.  */
16130               if (mips_orphaned_high_part_p (htab, insn))
16131                 delete_insn (insn);
16132               /* Also delete cache barriers if the last instruction
16133                  was an annulled branch.  INSN will not be speculatively
16134                  executed.  */
16135               else if (recog_memoized (insn) == CODE_FOR_r10k_cache_barrier
16136                        && last_insn
16137                        && JUMP_P (SEQ_BEGIN (last_insn))
16138                        && INSN_ANNULLED_BRANCH_P (SEQ_BEGIN (last_insn)))
16139                 delete_insn (insn);
16140               else
16141                 {
16142                   mips_avoid_hazard (last_insn, insn, &hilo_delay,
16143                                      &delayed_reg, lo_reg);
16144                   last_insn = insn;
16145                 }
16146             }
16147         }
16148     }
16149
16150   htab.dispose ();
16151 }
16152
16153 /* Return true if the function has a long branch instruction.  */
16154
16155 static bool
16156 mips_has_long_branch_p (void)
16157 {
16158   rtx insn, subinsn;
16159   int normal_length;
16160
16161   /* We need up-to-date instruction lengths.  */
16162   shorten_branches (get_insns ());
16163
16164   /* Look for a branch that is longer than normal.  The normal length for
16165      non-MIPS16 branches is 8, because the length includes the delay slot.
16166      It is 4 for MIPS16, because MIPS16 branches are extended instructions,
16167      but they have no delay slot.  */
16168   normal_length = (TARGET_MIPS16 ? 4 : 8);
16169   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
16170     FOR_EACH_SUBINSN (subinsn, insn)
16171       if (JUMP_P (subinsn)
16172           && get_attr_length (subinsn) > normal_length
16173           && (any_condjump_p (subinsn) || any_uncondjump_p (subinsn)))
16174         return true;
16175
16176   return false;
16177 }
16178
16179 /* If we are using a GOT, but have not decided to use a global pointer yet,
16180    see whether we need one to implement long branches.  Convert the ghost
16181    global-pointer instructions into real ones if so.  */
16182
16183 static bool
16184 mips_expand_ghost_gp_insns (void)
16185 {
16186   /* Quick exit if we already know that we will or won't need a
16187      global pointer.  */
16188   if (!TARGET_USE_GOT
16189       || cfun->machine->global_pointer == INVALID_REGNUM
16190       || mips_must_initialize_gp_p ())
16191     return false;
16192
16193   /* Run a full check for long branches.  */
16194   if (!mips_has_long_branch_p ())
16195     return false;
16196
16197   /* We've now established that we need $gp.  */
16198   cfun->machine->must_initialize_gp_p = true;
16199   split_all_insns_noflow ();
16200
16201   return true;
16202 }
16203
16204 /* Subroutine of mips_reorg to manage passes that require DF.  */
16205
16206 static void
16207 mips_df_reorg (void)
16208 {
16209   /* Create def-use chains.  */
16210   df_set_flags (DF_EQ_NOTES);
16211   df_chain_add_problem (DF_UD_CHAIN);
16212   df_analyze ();
16213
16214   if (TARGET_RELAX_PIC_CALLS)
16215     mips_annotate_pic_calls ();
16216
16217   if (mips_r10k_cache_barrier != R10K_CACHE_BARRIER_NONE)
16218     r10k_insert_cache_barriers ();
16219
16220   df_finish_pass (false);
16221 }
16222
16223 /* Emit code to load LABEL_REF SRC into MIPS16 register DEST.  This is
16224    called very late in mips_reorg, but the caller is required to run
16225    mips16_lay_out_constants on the result.  */
16226
16227 static void
16228 mips16_load_branch_target (rtx dest, rtx src)
16229 {
16230   if (TARGET_ABICALLS && !TARGET_ABSOLUTE_ABICALLS)
16231     {
16232       rtx page, low;
16233
16234       if (mips_cfun_has_cprestore_slot_p ())
16235         mips_emit_move (dest, mips_cprestore_slot (dest, true));
16236       else
16237         mips_emit_move (dest, pic_offset_table_rtx);
16238       page = mips_unspec_address (src, SYMBOL_GOTOFF_PAGE);
16239       low = mips_unspec_address (src, SYMBOL_GOT_PAGE_OFST);
16240       emit_insn (gen_rtx_SET (VOIDmode, dest,
16241                               PMODE_INSN (gen_unspec_got, (dest, page))));
16242       emit_insn (gen_rtx_SET (VOIDmode, dest,
16243                               gen_rtx_LO_SUM (Pmode, dest, low)));
16244     }
16245   else
16246     {
16247       src = mips_unspec_address (src, SYMBOL_ABSOLUTE);
16248       mips_emit_move (dest, src);
16249     }
16250 }
16251
16252 /* If we're compiling a MIPS16 function, look for and split any long branches.
16253    This must be called after all other instruction modifications in
16254    mips_reorg.  */
16255
16256 static void
16257 mips16_split_long_branches (void)
16258 {
16259   bool something_changed;
16260
16261   if (!TARGET_MIPS16)
16262     return;
16263
16264   /* Loop until the alignments for all targets are sufficient.  */
16265   do
16266     {
16267       rtx insn;
16268
16269       shorten_branches (get_insns ());
16270       something_changed = false;
16271       for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
16272         if (JUMP_P (insn)
16273             && get_attr_length (insn) > 4
16274             && (any_condjump_p (insn) || any_uncondjump_p (insn)))
16275           {
16276             rtx old_label, new_label, temp, saved_temp;
16277             rtx target, jump, jump_sequence;
16278
16279             start_sequence ();
16280
16281             /* Free up a MIPS16 register by saving it in $1.  */
16282             saved_temp = gen_rtx_REG (Pmode, AT_REGNUM);
16283             temp = gen_rtx_REG (Pmode, GP_REG_FIRST + 2);
16284             emit_move_insn (saved_temp, temp);
16285
16286             /* Load the branch target into TEMP.  */
16287             old_label = JUMP_LABEL (insn);
16288             target = gen_rtx_LABEL_REF (Pmode, old_label);
16289             mips16_load_branch_target (temp, target);
16290
16291             /* Jump to the target and restore the register's
16292                original value.  */
16293             jump = emit_jump_insn (PMODE_INSN (gen_indirect_jump_and_restore,
16294                                                (temp, temp, saved_temp)));
16295             JUMP_LABEL (jump) = old_label;
16296             LABEL_NUSES (old_label)++;
16297
16298             /* Rewrite any symbolic references that are supposed to use
16299                a PC-relative constant pool.  */
16300             mips16_lay_out_constants (false);
16301
16302             if (simplejump_p (insn))
16303               /* We're going to replace INSN with a longer form.  */
16304               new_label = NULL_RTX;
16305             else
16306               {
16307                 /* Create a branch-around label for the original
16308                    instruction.  */
16309                 new_label = gen_label_rtx ();
16310                 emit_label (new_label);
16311               }
16312
16313             jump_sequence = get_insns ();
16314             end_sequence ();
16315
16316             emit_insn_after (jump_sequence, insn);
16317             if (new_label)
16318               invert_jump (insn, new_label, false);
16319             else
16320               delete_insn (insn);
16321             something_changed = true;
16322           }
16323     }
16324   while (something_changed);
16325 }
16326
16327 /* Implement TARGET_MACHINE_DEPENDENT_REORG.  */
16328
16329 static void
16330 mips_reorg (void)
16331 {
16332   /* Restore the BLOCK_FOR_INSN pointers, which are needed by DF.  Also during
16333      insn splitting in mips16_lay_out_constants, DF insn info is only kept up
16334      to date if the CFG is available.  */
16335   if (mips_cfg_in_reorg ())
16336     compute_bb_for_insn ();
16337   mips16_lay_out_constants (true);
16338   if (mips_cfg_in_reorg ())
16339     {
16340       mips_df_reorg ();
16341       free_bb_for_insn ();
16342     }
16343 }
16344
16345 /* We use a machine specific pass to do a second machine dependent reorg
16346    pass after delay branch scheduling.  */
16347
16348 static unsigned int
16349 mips_machine_reorg2 (void)
16350 {
16351   mips_reorg_process_insns ();
16352   if (!TARGET_MIPS16
16353       && TARGET_EXPLICIT_RELOCS
16354       && TUNE_MIPS4130
16355       && TARGET_VR4130_ALIGN)
16356     vr4130_align_insns ();
16357   if (mips_expand_ghost_gp_insns ())
16358     /* The expansion could invalidate some of the VR4130 alignment
16359        optimizations, but this should be an extremely rare case anyhow.  */
16360     mips_reorg_process_insns ();
16361   mips16_split_long_branches ();
16362   return 0;
16363 }
16364
16365 namespace {
16366
16367 const pass_data pass_data_mips_machine_reorg2 =
16368 {
16369   RTL_PASS, /* type */
16370   "mach2", /* name */
16371   OPTGROUP_NONE, /* optinfo_flags */
16372   false, /* has_gate */
16373   true, /* has_execute */
16374   TV_MACH_DEP, /* tv_id */
16375   0, /* properties_required */
16376   0, /* properties_provided */
16377   0, /* properties_destroyed */
16378   0, /* todo_flags_start */
16379   TODO_verify_rtl_sharing, /* todo_flags_finish */
16380 };
16381
16382 class pass_mips_machine_reorg2 : public rtl_opt_pass
16383 {
16384 public:
16385   pass_mips_machine_reorg2(gcc::context *ctxt)
16386     : rtl_opt_pass(pass_data_mips_machine_reorg2, ctxt)
16387   {}
16388
16389   /* opt_pass methods: */
16390   unsigned int execute () { return mips_machine_reorg2 (); }
16391
16392 }; // class pass_mips_machine_reorg2
16393
16394 } // anon namespace
16395
16396 rtl_opt_pass *
16397 make_pass_mips_machine_reorg2 (gcc::context *ctxt)
16398 {
16399   return new pass_mips_machine_reorg2 (ctxt);
16400 }
16401
16402 \f
16403 /* Implement TARGET_ASM_OUTPUT_MI_THUNK.  Generate rtl rather than asm text
16404    in order to avoid duplicating too much logic from elsewhere.  */
16405
16406 static void
16407 mips_output_mi_thunk (FILE *file, tree thunk_fndecl ATTRIBUTE_UNUSED,
16408                       HOST_WIDE_INT delta, HOST_WIDE_INT vcall_offset,
16409                       tree function)
16410 {
16411   rtx this_rtx, temp1, temp2, insn, fnaddr;
16412   bool use_sibcall_p;
16413
16414   /* Pretend to be a post-reload pass while generating rtl.  */
16415   reload_completed = 1;
16416
16417   /* Mark the end of the (empty) prologue.  */
16418   emit_note (NOTE_INSN_PROLOGUE_END);
16419
16420   /* Determine if we can use a sibcall to call FUNCTION directly.  */
16421   fnaddr = XEXP (DECL_RTL (function), 0);
16422   use_sibcall_p = (mips_function_ok_for_sibcall (function, NULL)
16423                    && const_call_insn_operand (fnaddr, Pmode));
16424
16425   /* Determine if we need to load FNADDR from the GOT.  */
16426   if (!use_sibcall_p
16427       && (mips_got_symbol_type_p
16428           (mips_classify_symbol (fnaddr, SYMBOL_CONTEXT_LEA))))
16429     {
16430       /* Pick a global pointer.  Use a call-clobbered register if
16431          TARGET_CALL_SAVED_GP.  */
16432       cfun->machine->global_pointer
16433         = TARGET_CALL_SAVED_GP ? 15 : GLOBAL_POINTER_REGNUM;
16434       cfun->machine->must_initialize_gp_p = true;
16435       SET_REGNO (pic_offset_table_rtx, cfun->machine->global_pointer);
16436
16437       /* Set up the global pointer for n32 or n64 abicalls.  */
16438       mips_emit_loadgp ();
16439     }
16440
16441   /* We need two temporary registers in some cases.  */
16442   temp1 = gen_rtx_REG (Pmode, 2);
16443   temp2 = gen_rtx_REG (Pmode, 3);
16444
16445   /* Find out which register contains the "this" pointer.  */
16446   if (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function))
16447     this_rtx = gen_rtx_REG (Pmode, GP_ARG_FIRST + 1);
16448   else
16449     this_rtx = gen_rtx_REG (Pmode, GP_ARG_FIRST);
16450
16451   /* Add DELTA to THIS_RTX.  */
16452   if (delta != 0)
16453     {
16454       rtx offset = GEN_INT (delta);
16455       if (!SMALL_OPERAND (delta))
16456         {
16457           mips_emit_move (temp1, offset);
16458           offset = temp1;
16459         }
16460       emit_insn (gen_add3_insn (this_rtx, this_rtx, offset));
16461     }
16462
16463   /* If needed, add *(*THIS_RTX + VCALL_OFFSET) to THIS_RTX.  */
16464   if (vcall_offset != 0)
16465     {
16466       rtx addr;
16467
16468       /* Set TEMP1 to *THIS_RTX.  */
16469       mips_emit_move (temp1, gen_rtx_MEM (Pmode, this_rtx));
16470
16471       /* Set ADDR to a legitimate address for *THIS_RTX + VCALL_OFFSET.  */
16472       addr = mips_add_offset (temp2, temp1, vcall_offset);
16473
16474       /* Load the offset and add it to THIS_RTX.  */
16475       mips_emit_move (temp1, gen_rtx_MEM (Pmode, addr));
16476       emit_insn (gen_add3_insn (this_rtx, this_rtx, temp1));
16477     }
16478
16479   /* Jump to the target function.  Use a sibcall if direct jumps are
16480      allowed, otherwise load the address into a register first.  */
16481   if (use_sibcall_p)
16482     {
16483       insn = emit_call_insn (gen_sibcall_internal (fnaddr, const0_rtx));
16484       SIBLING_CALL_P (insn) = 1;
16485     }
16486   else
16487     {
16488       /* This is messy.  GAS treats "la $25,foo" as part of a call
16489          sequence and may allow a global "foo" to be lazily bound.
16490          The general move patterns therefore reject this combination.
16491
16492          In this context, lazy binding would actually be OK
16493          for TARGET_CALL_CLOBBERED_GP, but it's still wrong for
16494          TARGET_CALL_SAVED_GP; see mips_load_call_address.
16495          We must therefore load the address via a temporary
16496          register if mips_dangerous_for_la25_p.
16497
16498          If we jump to the temporary register rather than $25,
16499          the assembler can use the move insn to fill the jump's
16500          delay slot.
16501
16502          We can use the same technique for MIPS16 code, where $25
16503          is not a valid JR register.  */
16504       if (TARGET_USE_PIC_FN_ADDR_REG
16505           && !TARGET_MIPS16
16506           && !mips_dangerous_for_la25_p (fnaddr))
16507         temp1 = gen_rtx_REG (Pmode, PIC_FUNCTION_ADDR_REGNUM);
16508       mips_load_call_address (MIPS_CALL_SIBCALL, temp1, fnaddr);
16509
16510       if (TARGET_USE_PIC_FN_ADDR_REG
16511           && REGNO (temp1) != PIC_FUNCTION_ADDR_REGNUM)
16512         mips_emit_move (gen_rtx_REG (Pmode, PIC_FUNCTION_ADDR_REGNUM), temp1);
16513       emit_jump_insn (gen_indirect_jump (temp1));
16514     }
16515
16516   /* Run just enough of rest_of_compilation.  This sequence was
16517      "borrowed" from alpha.c.  */
16518   insn = get_insns ();
16519   split_all_insns_noflow ();
16520   mips16_lay_out_constants (true);
16521   shorten_branches (insn);
16522   final_start_function (insn, file, 1);
16523   final (insn, file, 1);
16524   final_end_function ();
16525
16526   /* Clean up the vars set above.  Note that final_end_function resets
16527      the global pointer for us.  */
16528   reload_completed = 0;
16529 }
16530 \f
16531
16532 /* The last argument passed to mips_set_compression_mode,
16533    or negative if the function hasn't been called yet.  */
16534 static unsigned int old_compression_mode = -1;
16535
16536 /* Set up the target-dependent global state for ISA mode COMPRESSION_MODE,
16537    which is either MASK_MIPS16 or MASK_MICROMIPS.  */
16538
16539 static void
16540 mips_set_compression_mode (unsigned int compression_mode)
16541 {
16542
16543   if (compression_mode == old_compression_mode)
16544     return;
16545
16546   /* Restore base settings of various flags.  */
16547   target_flags = mips_base_target_flags;
16548   flag_schedule_insns = mips_base_schedule_insns;
16549   flag_reorder_blocks_and_partition = mips_base_reorder_blocks_and_partition;
16550   flag_move_loop_invariants = mips_base_move_loop_invariants;
16551   align_loops = mips_base_align_loops;
16552   align_jumps = mips_base_align_jumps;
16553   align_functions = mips_base_align_functions;
16554   target_flags &= ~(MASK_MIPS16 | MASK_MICROMIPS);
16555   target_flags |= compression_mode;
16556
16557   if (compression_mode & MASK_MIPS16)
16558     {
16559       /* Switch to MIPS16 mode.  */
16560       target_flags |= MASK_MIPS16;
16561
16562       /* Turn off SYNCI if it was on, MIPS16 doesn't support it.  */
16563       target_flags &= ~MASK_SYNCI;
16564
16565       /* Don't run the scheduler before reload, since it tends to
16566          increase register pressure.  */
16567       flag_schedule_insns = 0;
16568
16569       /* Don't do hot/cold partitioning.  mips16_lay_out_constants expects
16570          the whole function to be in a single section.  */
16571       flag_reorder_blocks_and_partition = 0;
16572
16573       /* Don't move loop invariants, because it tends to increase
16574          register pressure.  It also introduces an extra move in cases
16575          where the constant is the first operand in a two-operand binary
16576          instruction, or when it forms a register argument to a functon
16577          call.  */
16578       flag_move_loop_invariants = 0;
16579
16580       target_flags |= MASK_EXPLICIT_RELOCS;
16581
16582       /* Experiments suggest we get the best overall section-anchor
16583          results from using the range of an unextended LW or SW.  Code
16584          that makes heavy use of byte or short accesses can do better
16585          with ranges of 0...31 and 0...63 respectively, but most code is
16586          sensitive to the range of LW and SW instead.  */
16587       targetm.min_anchor_offset = 0;
16588       targetm.max_anchor_offset = 127;
16589
16590       targetm.const_anchor = 0;
16591
16592       /* MIPS16 has no BAL instruction.  */
16593       target_flags &= ~MASK_RELAX_PIC_CALLS;
16594
16595       /* The R4000 errata don't apply to any known MIPS16 cores.
16596          It's simpler to make the R4000 fixes and MIPS16 mode
16597          mutually exclusive.  */
16598       target_flags &= ~MASK_FIX_R4000;
16599
16600       if (flag_pic && !TARGET_OLDABI)
16601         sorry ("MIPS16 PIC for ABIs other than o32 and o64");
16602
16603       if (TARGET_XGOT)
16604         sorry ("MIPS16 -mxgot code");
16605
16606       if (TARGET_HARD_FLOAT_ABI && !TARGET_OLDABI)
16607         sorry ("hard-float MIPS16 code for ABIs other than o32 and o64");
16608     }
16609   else
16610     {
16611       /* Switch to microMIPS or the standard encoding.  */
16612
16613       if (TARGET_MICROMIPS)
16614         /* Avoid branch likely.  */
16615         target_flags &= ~MASK_BRANCHLIKELY;
16616
16617       /* Provide default values for align_* for 64-bit targets.  */
16618       if (TARGET_64BIT)
16619         {
16620           if (align_loops == 0)
16621             align_loops = 8;
16622           if (align_jumps == 0)
16623             align_jumps = 8;
16624           if (align_functions == 0)
16625             align_functions = 8;
16626         }
16627
16628       targetm.min_anchor_offset = -32768;
16629       targetm.max_anchor_offset = 32767;
16630
16631       targetm.const_anchor = 0x8000;
16632     }
16633
16634   /* (Re)initialize MIPS target internals for new ISA.  */
16635   mips_init_relocs ();
16636
16637   if (compression_mode & MASK_MIPS16)
16638     {
16639       if (!mips16_globals)
16640         mips16_globals = save_target_globals_default_opts ();
16641       else
16642         restore_target_globals (mips16_globals);
16643     }
16644   else
16645     restore_target_globals (&default_target_globals);
16646
16647   old_compression_mode = compression_mode;
16648 }
16649
16650 /* Implement TARGET_SET_CURRENT_FUNCTION.  Decide whether the current
16651    function should use the MIPS16 or microMIPS ISA and switch modes
16652    accordingly.  */
16653
16654 static void
16655 mips_set_current_function (tree fndecl)
16656 {
16657   mips_set_compression_mode (mips_get_compress_mode (fndecl));
16658 }
16659 \f
16660 /* Allocate a chunk of memory for per-function machine-dependent data.  */
16661
16662 static struct machine_function *
16663 mips_init_machine_status (void)
16664 {
16665   return ggc_alloc_cleared_machine_function ();
16666 }
16667
16668 /* Return the processor associated with the given ISA level, or null
16669    if the ISA isn't valid.  */
16670
16671 static const struct mips_cpu_info *
16672 mips_cpu_info_from_isa (int isa)
16673 {
16674   unsigned int i;
16675
16676   for (i = 0; i < ARRAY_SIZE (mips_cpu_info_table); i++)
16677     if (mips_cpu_info_table[i].isa == isa)
16678       return mips_cpu_info_table + i;
16679
16680   return NULL;
16681 }
16682
16683 /* Return a mips_cpu_info entry determined by an option valued
16684    OPT.  */
16685
16686 static const struct mips_cpu_info *
16687 mips_cpu_info_from_opt (int opt)
16688 {
16689   switch (opt)
16690     {
16691     case MIPS_ARCH_OPTION_FROM_ABI:
16692       /* 'from-abi' selects the most compatible architecture for the
16693          given ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit
16694          ABIs.  For the EABIs, we have to decide whether we're using
16695          the 32-bit or 64-bit version.  */
16696       return mips_cpu_info_from_isa (ABI_NEEDS_32BIT_REGS ? 1
16697                                      : ABI_NEEDS_64BIT_REGS ? 3
16698                                      : (TARGET_64BIT ? 3 : 1));
16699
16700     case MIPS_ARCH_OPTION_NATIVE:
16701       gcc_unreachable ();
16702
16703     default:
16704       return &mips_cpu_info_table[opt];
16705     }
16706 }
16707
16708 /* Return a default mips_cpu_info entry, given that no -march= option
16709    was explicitly specified.  */
16710
16711 static const struct mips_cpu_info *
16712 mips_default_arch (void)
16713 {
16714 #if defined (MIPS_CPU_STRING_DEFAULT)
16715   unsigned int i;
16716   for (i = 0; i < ARRAY_SIZE (mips_cpu_info_table); i++)
16717     if (strcmp (mips_cpu_info_table[i].name, MIPS_CPU_STRING_DEFAULT) == 0)
16718       return mips_cpu_info_table + i;
16719   gcc_unreachable ();
16720 #elif defined (MIPS_ISA_DEFAULT)
16721   return mips_cpu_info_from_isa (MIPS_ISA_DEFAULT);
16722 #else
16723   /* 'from-abi' makes a good default: you get whatever the ABI
16724      requires.  */
16725   return mips_cpu_info_from_opt (MIPS_ARCH_OPTION_FROM_ABI);
16726 #endif
16727 }
16728
16729 /* Set up globals to generate code for the ISA or processor
16730    described by INFO.  */
16731
16732 static void
16733 mips_set_architecture (const struct mips_cpu_info *info)
16734 {
16735   if (info != 0)
16736     {
16737       mips_arch_info = info;
16738       mips_arch = info->cpu;
16739       mips_isa = info->isa;
16740     }
16741 }
16742
16743 /* Likewise for tuning.  */
16744
16745 static void
16746 mips_set_tune (const struct mips_cpu_info *info)
16747 {
16748   if (info != 0)
16749     {
16750       mips_tune_info = info;
16751       mips_tune = info->cpu;
16752     }
16753 }
16754
16755 /* Implement TARGET_OPTION_OVERRIDE.  */
16756
16757 static void
16758 mips_option_override (void)
16759 {
16760   int i, start, regno, mode;
16761
16762   if (global_options_set.x_mips_isa_option)
16763     mips_isa_option_info = &mips_cpu_info_table[mips_isa_option];
16764
16765 #ifdef SUBTARGET_OVERRIDE_OPTIONS
16766   SUBTARGET_OVERRIDE_OPTIONS;
16767 #endif
16768
16769   /* MIPS16 and microMIPS cannot coexist.  */
16770   if (TARGET_MICROMIPS && TARGET_MIPS16)
16771     error ("unsupported combination: %s", "-mips16 -mmicromips");
16772
16773   /* Save the base compression state and process flags as though we
16774      were generating uncompressed code.  */
16775   mips_base_compression_flags = TARGET_COMPRESSION;
16776   target_flags &= ~TARGET_COMPRESSION;
16777
16778   /* -mno-float overrides -mhard-float and -msoft-float.  */
16779   if (TARGET_NO_FLOAT)
16780     {
16781       target_flags |= MASK_SOFT_FLOAT_ABI;
16782       target_flags_explicit |= MASK_SOFT_FLOAT_ABI;
16783     }
16784
16785   if (TARGET_FLIP_MIPS16)
16786     TARGET_INTERLINK_COMPRESSED = 1;
16787
16788   /* Set the small data limit.  */
16789   mips_small_data_threshold = (global_options_set.x_g_switch_value
16790                                ? g_switch_value
16791                                : MIPS_DEFAULT_GVALUE);
16792
16793   /* The following code determines the architecture and register size.
16794      Similar code was added to GAS 2.14 (see tc-mips.c:md_after_parse_args()).
16795      The GAS and GCC code should be kept in sync as much as possible.  */
16796
16797   if (global_options_set.x_mips_arch_option)
16798     mips_set_architecture (mips_cpu_info_from_opt (mips_arch_option));
16799
16800   if (mips_isa_option_info != 0)
16801     {
16802       if (mips_arch_info == 0)
16803         mips_set_architecture (mips_isa_option_info);
16804       else if (mips_arch_info->isa != mips_isa_option_info->isa)
16805         error ("%<-%s%> conflicts with the other architecture options, "
16806                "which specify a %s processor",
16807                mips_isa_option_info->name,
16808                mips_cpu_info_from_isa (mips_arch_info->isa)->name);
16809     }
16810
16811   if (mips_arch_info == 0)
16812     mips_set_architecture (mips_default_arch ());
16813
16814   if (ABI_NEEDS_64BIT_REGS && !ISA_HAS_64BIT_REGS)
16815     error ("%<-march=%s%> is not compatible with the selected ABI",
16816            mips_arch_info->name);
16817
16818   /* Optimize for mips_arch, unless -mtune selects a different processor.  */
16819   if (global_options_set.x_mips_tune_option)
16820     mips_set_tune (mips_cpu_info_from_opt (mips_tune_option));
16821
16822   if (mips_tune_info == 0)
16823     mips_set_tune (mips_arch_info);
16824
16825   if ((target_flags_explicit & MASK_64BIT) != 0)
16826     {
16827       /* The user specified the size of the integer registers.  Make sure
16828          it agrees with the ABI and ISA.  */
16829       if (TARGET_64BIT && !ISA_HAS_64BIT_REGS)
16830         error ("%<-mgp64%> used with a 32-bit processor");
16831       else if (!TARGET_64BIT && ABI_NEEDS_64BIT_REGS)
16832         error ("%<-mgp32%> used with a 64-bit ABI");
16833       else if (TARGET_64BIT && ABI_NEEDS_32BIT_REGS)
16834         error ("%<-mgp64%> used with a 32-bit ABI");
16835     }
16836   else
16837     {
16838       /* Infer the integer register size from the ABI and processor.
16839          Restrict ourselves to 32-bit registers if that's all the
16840          processor has, or if the ABI cannot handle 64-bit registers.  */
16841       if (ABI_NEEDS_32BIT_REGS || !ISA_HAS_64BIT_REGS)
16842         target_flags &= ~MASK_64BIT;
16843       else
16844         target_flags |= MASK_64BIT;
16845     }
16846
16847   if ((target_flags_explicit & MASK_FLOAT64) != 0)
16848     {
16849       if (TARGET_SINGLE_FLOAT && TARGET_FLOAT64)
16850         error ("unsupported combination: %s", "-mfp64 -msingle-float");
16851       else if (TARGET_64BIT && TARGET_DOUBLE_FLOAT && !TARGET_FLOAT64)
16852         error ("unsupported combination: %s", "-mgp64 -mfp32 -mdouble-float");
16853       else if (!TARGET_64BIT && TARGET_FLOAT64)
16854         {
16855           if (!ISA_HAS_MXHC1)
16856             error ("%<-mgp32%> and %<-mfp64%> can only be combined if"
16857                    " the target supports the mfhc1 and mthc1 instructions");
16858           else if (mips_abi != ABI_32)
16859             error ("%<-mgp32%> and %<-mfp64%> can only be combined when using"
16860                    " the o32 ABI");
16861         }
16862     }
16863   else
16864     {
16865       /* -msingle-float selects 32-bit float registers.  Otherwise the
16866          float registers should be the same size as the integer ones.  */
16867       if (TARGET_64BIT && TARGET_DOUBLE_FLOAT)
16868         target_flags |= MASK_FLOAT64;
16869       else
16870         target_flags &= ~MASK_FLOAT64;
16871     }
16872
16873   /* End of code shared with GAS.  */
16874
16875   /* The R5900 FPU only supports single precision.  */
16876   if (TARGET_MIPS5900 && TARGET_HARD_FLOAT_ABI && TARGET_DOUBLE_FLOAT)
16877     error ("unsupported combination: %s",
16878            "-march=r5900 -mhard-float -mdouble-float");
16879
16880   /* If a -mlong* option was given, check that it matches the ABI,
16881      otherwise infer the -mlong* setting from the other options.  */
16882   if ((target_flags_explicit & MASK_LONG64) != 0)
16883     {
16884       if (TARGET_LONG64)
16885         {
16886           if (mips_abi == ABI_N32)
16887             error ("%qs is incompatible with %qs", "-mabi=n32", "-mlong64");
16888           else if (mips_abi == ABI_32)
16889             error ("%qs is incompatible with %qs", "-mabi=32", "-mlong64");
16890           else if (mips_abi == ABI_O64 && TARGET_ABICALLS)
16891             /* We have traditionally allowed non-abicalls code to use
16892                an LP64 form of o64.  However, it would take a bit more
16893                effort to support the combination of 32-bit GOT entries
16894                and 64-bit pointers, so we treat the abicalls case as
16895                an error.  */
16896             error ("the combination of %qs and %qs is incompatible with %qs",
16897                    "-mabi=o64", "-mabicalls", "-mlong64");
16898         }
16899       else
16900         {
16901           if (mips_abi == ABI_64)
16902             error ("%qs is incompatible with %qs", "-mabi=64", "-mlong32");
16903         }
16904     }
16905   else
16906     {
16907       if ((mips_abi == ABI_EABI && TARGET_64BIT) || mips_abi == ABI_64)
16908         target_flags |= MASK_LONG64;
16909       else
16910         target_flags &= ~MASK_LONG64;
16911     }
16912
16913   if (!TARGET_OLDABI)
16914     flag_pcc_struct_return = 0;
16915
16916   /* Decide which rtx_costs structure to use.  */
16917   if (optimize_size)
16918     mips_cost = &mips_rtx_cost_optimize_size;
16919   else
16920     mips_cost = &mips_rtx_cost_data[mips_tune];
16921
16922   /* If the user hasn't specified a branch cost, use the processor's
16923      default.  */
16924   if (mips_branch_cost == 0)
16925     mips_branch_cost = mips_cost->branch_cost;
16926
16927   /* If neither -mbranch-likely nor -mno-branch-likely was given
16928      on the command line, set MASK_BRANCHLIKELY based on the target
16929      architecture and tuning flags.  Annulled delay slots are a
16930      size win, so we only consider the processor-specific tuning
16931      for !optimize_size.  */
16932   if ((target_flags_explicit & MASK_BRANCHLIKELY) == 0)
16933     {
16934       if (ISA_HAS_BRANCHLIKELY
16935           && (optimize_size
16936               || (mips_tune_info->tune_flags & PTF_AVOID_BRANCHLIKELY) == 0))
16937         target_flags |= MASK_BRANCHLIKELY;
16938       else
16939         target_flags &= ~MASK_BRANCHLIKELY;
16940     }
16941   else if (TARGET_BRANCHLIKELY && !ISA_HAS_BRANCHLIKELY)
16942     warning (0, "the %qs architecture does not support branch-likely"
16943              " instructions", mips_arch_info->name);
16944
16945   /* If the user hasn't specified -mimadd or -mno-imadd set
16946      MASK_IMADD based on the target architecture and tuning
16947      flags.  */
16948   if ((target_flags_explicit & MASK_IMADD) == 0)
16949     {
16950       if (ISA_HAS_MADD_MSUB &&
16951           (mips_tune_info->tune_flags & PTF_AVOID_IMADD) == 0)
16952         target_flags |= MASK_IMADD;
16953       else
16954         target_flags &= ~MASK_IMADD;
16955     }
16956   else if (TARGET_IMADD && !ISA_HAS_MADD_MSUB)
16957     warning (0, "the %qs architecture does not support madd or msub"
16958              " instructions", mips_arch_info->name);
16959
16960   /* The effect of -mabicalls isn't defined for the EABI.  */
16961   if (mips_abi == ABI_EABI && TARGET_ABICALLS)
16962     {
16963       error ("unsupported combination: %s", "-mabicalls -mabi=eabi");
16964       target_flags &= ~MASK_ABICALLS;
16965     }
16966
16967   /* PIC requires -mabicalls.  */
16968   if (flag_pic)
16969     {
16970       if (mips_abi == ABI_EABI)
16971         error ("cannot generate position-independent code for %qs",
16972                "-mabi=eabi");
16973       else if (!TARGET_ABICALLS)
16974         error ("position-independent code requires %qs", "-mabicalls");
16975     }
16976
16977   if (TARGET_ABICALLS_PIC2)
16978     /* We need to set flag_pic for executables as well as DSOs
16979        because we may reference symbols that are not defined in
16980        the final executable.  (MIPS does not use things like
16981        copy relocs, for example.)
16982
16983        There is a body of code that uses __PIC__ to distinguish
16984        between -mabicalls and -mno-abicalls code.  The non-__PIC__
16985        variant is usually appropriate for TARGET_ABICALLS_PIC0, as
16986        long as any indirect jumps use $25.  */
16987     flag_pic = 1;
16988
16989   /* -mvr4130-align is a "speed over size" optimization: it usually produces
16990      faster code, but at the expense of more nops.  Enable it at -O3 and
16991      above.  */
16992   if (optimize > 2 && (target_flags_explicit & MASK_VR4130_ALIGN) == 0)
16993     target_flags |= MASK_VR4130_ALIGN;
16994
16995   /* Prefer a call to memcpy over inline code when optimizing for size,
16996      though see MOVE_RATIO in mips.h.  */
16997   if (optimize_size && (target_flags_explicit & MASK_MEMCPY) == 0)
16998     target_flags |= MASK_MEMCPY;
16999
17000   /* If we have a nonzero small-data limit, check that the -mgpopt
17001      setting is consistent with the other target flags.  */
17002   if (mips_small_data_threshold > 0)
17003     {
17004       if (!TARGET_GPOPT)
17005         {
17006           if (!TARGET_EXPLICIT_RELOCS)
17007             error ("%<-mno-gpopt%> needs %<-mexplicit-relocs%>");
17008
17009           TARGET_LOCAL_SDATA = false;
17010           TARGET_EXTERN_SDATA = false;
17011         }
17012       else
17013         {
17014           if (TARGET_VXWORKS_RTP)
17015             warning (0, "cannot use small-data accesses for %qs", "-mrtp");
17016
17017           if (TARGET_ABICALLS)
17018             warning (0, "cannot use small-data accesses for %qs",
17019                      "-mabicalls");
17020         }
17021     }
17022
17023   /* Pre-IEEE 754-2008 MIPS hardware has a quirky almost-IEEE format
17024      for all its floating point.  */
17025   if (mips_nan != MIPS_IEEE_754_2008)
17026     {
17027       REAL_MODE_FORMAT (SFmode) = &mips_single_format;
17028       REAL_MODE_FORMAT (DFmode) = &mips_double_format;
17029       REAL_MODE_FORMAT (TFmode) = &mips_quad_format;
17030     }
17031
17032   /* Make sure that the user didn't turn off paired single support when
17033      MIPS-3D support is requested.  */
17034   if (TARGET_MIPS3D
17035       && (target_flags_explicit & MASK_PAIRED_SINGLE_FLOAT)
17036       && !TARGET_PAIRED_SINGLE_FLOAT)
17037     error ("%<-mips3d%> requires %<-mpaired-single%>");
17038
17039   /* If TARGET_MIPS3D, enable MASK_PAIRED_SINGLE_FLOAT.  */
17040   if (TARGET_MIPS3D)
17041     target_flags |= MASK_PAIRED_SINGLE_FLOAT;
17042
17043   /* Make sure that when TARGET_PAIRED_SINGLE_FLOAT is true, TARGET_FLOAT64
17044      and TARGET_HARD_FLOAT_ABI are both true.  */
17045   if (TARGET_PAIRED_SINGLE_FLOAT && !(TARGET_FLOAT64 && TARGET_HARD_FLOAT_ABI))
17046     error ("%qs must be used with %qs",
17047            TARGET_MIPS3D ? "-mips3d" : "-mpaired-single",
17048            TARGET_HARD_FLOAT_ABI ? "-mfp64" : "-mhard-float");
17049
17050   /* Make sure that the ISA supports TARGET_PAIRED_SINGLE_FLOAT when it is
17051      enabled.  */
17052   if (TARGET_PAIRED_SINGLE_FLOAT && !ISA_HAS_PAIRED_SINGLE)
17053     warning (0, "the %qs architecture does not support paired-single"
17054              " instructions", mips_arch_info->name);
17055
17056   if (mips_r10k_cache_barrier != R10K_CACHE_BARRIER_NONE
17057       && !TARGET_CACHE_BUILTIN)
17058     {
17059       error ("%qs requires a target that provides the %qs instruction",
17060              "-mr10k-cache-barrier", "cache");
17061       mips_r10k_cache_barrier = R10K_CACHE_BARRIER_NONE;
17062     }
17063
17064   /* If TARGET_DSPR2, enable MASK_DSP.  */
17065   if (TARGET_DSPR2)
17066     target_flags |= MASK_DSP;
17067
17068   /* .eh_frame addresses should be the same width as a C pointer.
17069      Most MIPS ABIs support only one pointer size, so the assembler
17070      will usually know exactly how big an .eh_frame address is.
17071
17072      Unfortunately, this is not true of the 64-bit EABI.  The ABI was
17073      originally defined to use 64-bit pointers (i.e. it is LP64), and
17074      this is still the default mode.  However, we also support an n32-like
17075      ILP32 mode, which is selected by -mlong32.  The problem is that the
17076      assembler has traditionally not had an -mlong option, so it has
17077      traditionally not known whether we're using the ILP32 or LP64 form.
17078
17079      As it happens, gas versions up to and including 2.19 use _32-bit_
17080      addresses for EABI64 .cfi_* directives.  This is wrong for the
17081      default LP64 mode, so we can't use the directives by default.
17082      Moreover, since gas's current behavior is at odds with gcc's
17083      default behavior, it seems unwise to rely on future versions
17084      of gas behaving the same way.  We therefore avoid using .cfi
17085      directives for -mlong32 as well.  */
17086   if (mips_abi == ABI_EABI && TARGET_64BIT)
17087     flag_dwarf2_cfi_asm = 0;
17088
17089   /* .cfi_* directives generate a read-only section, so fall back on
17090      manual .eh_frame creation if we need the section to be writable.  */
17091   if (TARGET_WRITABLE_EH_FRAME)
17092     flag_dwarf2_cfi_asm = 0;
17093
17094   mips_init_print_operand_punct ();
17095
17096   /* Set up array to map GCC register number to debug register number.
17097      Ignore the special purpose register numbers.  */
17098
17099   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
17100     {
17101       mips_dbx_regno[i] = IGNORED_DWARF_REGNUM;
17102       if (GP_REG_P (i) || FP_REG_P (i) || ALL_COP_REG_P (i))
17103         mips_dwarf_regno[i] = i;
17104       else
17105         mips_dwarf_regno[i] = INVALID_REGNUM;
17106     }
17107
17108   start = GP_DBX_FIRST - GP_REG_FIRST;
17109   for (i = GP_REG_FIRST; i <= GP_REG_LAST; i++)
17110     mips_dbx_regno[i] = i + start;
17111
17112   start = FP_DBX_FIRST - FP_REG_FIRST;
17113   for (i = FP_REG_FIRST; i <= FP_REG_LAST; i++)
17114     mips_dbx_regno[i] = i + start;
17115
17116   /* Accumulator debug registers use big-endian ordering.  */
17117   mips_dbx_regno[HI_REGNUM] = MD_DBX_FIRST + 0;
17118   mips_dbx_regno[LO_REGNUM] = MD_DBX_FIRST + 1;
17119   mips_dwarf_regno[HI_REGNUM] = MD_REG_FIRST + 0;
17120   mips_dwarf_regno[LO_REGNUM] = MD_REG_FIRST + 1;
17121   for (i = DSP_ACC_REG_FIRST; i <= DSP_ACC_REG_LAST; i += 2)
17122     {
17123       mips_dwarf_regno[i + TARGET_LITTLE_ENDIAN] = i;
17124       mips_dwarf_regno[i + TARGET_BIG_ENDIAN] = i + 1;
17125     }
17126
17127   /* Set up mips_hard_regno_mode_ok.  */
17128   for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
17129     for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
17130       mips_hard_regno_mode_ok[mode][regno]
17131         = mips_hard_regno_mode_ok_p (regno, (enum machine_mode) mode);
17132
17133   /* Function to allocate machine-dependent function status.  */
17134   init_machine_status = &mips_init_machine_status;
17135
17136   /* Default to working around R4000 errata only if the processor
17137      was selected explicitly.  */
17138   if ((target_flags_explicit & MASK_FIX_R4000) == 0
17139       && strcmp (mips_arch_info->name, "r4000") == 0)
17140     target_flags |= MASK_FIX_R4000;
17141
17142   /* Default to working around R4400 errata only if the processor
17143      was selected explicitly.  */
17144   if ((target_flags_explicit & MASK_FIX_R4400) == 0
17145       && strcmp (mips_arch_info->name, "r4400") == 0)
17146     target_flags |= MASK_FIX_R4400;
17147
17148   /* Default to working around R10000 errata only if the processor
17149      was selected explicitly.  */
17150   if ((target_flags_explicit & MASK_FIX_R10000) == 0
17151       && strcmp (mips_arch_info->name, "r10000") == 0)
17152     target_flags |= MASK_FIX_R10000;
17153
17154   /* Make sure that branch-likely instructions available when using
17155      -mfix-r10000.  The instructions are not available if either:
17156
17157         1. -mno-branch-likely was passed.
17158         2. The selected ISA does not support branch-likely and
17159            the command line does not include -mbranch-likely.  */
17160   if (TARGET_FIX_R10000
17161       && ((target_flags_explicit & MASK_BRANCHLIKELY) == 0
17162           ? !ISA_HAS_BRANCHLIKELY
17163           : !TARGET_BRANCHLIKELY))
17164     sorry ("%qs requires branch-likely instructions", "-mfix-r10000");
17165
17166   if (TARGET_SYNCI && !ISA_HAS_SYNCI)
17167     {
17168       warning (0, "the %qs architecture does not support the synci "
17169                "instruction", mips_arch_info->name);
17170       target_flags &= ~MASK_SYNCI;
17171     }
17172
17173   /* Only optimize PIC indirect calls if they are actually required.  */
17174   if (!TARGET_USE_GOT || !TARGET_EXPLICIT_RELOCS)
17175     target_flags &= ~MASK_RELAX_PIC_CALLS;
17176
17177   /* Save base state of options.  */
17178   mips_base_target_flags = target_flags;
17179   mips_base_schedule_insns = flag_schedule_insns;
17180   mips_base_reorder_blocks_and_partition = flag_reorder_blocks_and_partition;
17181   mips_base_move_loop_invariants = flag_move_loop_invariants;
17182   mips_base_align_loops = align_loops;
17183   mips_base_align_jumps = align_jumps;
17184   mips_base_align_functions = align_functions;
17185
17186   /* Now select the ISA mode.
17187
17188      Do all CPP-sensitive stuff in uncompressed mode; we'll switch modes
17189      later if required.  */
17190   mips_set_compression_mode (0);
17191
17192   /* We register a second machine specific reorg pass after delay slot
17193      filling.  Registering the pass must be done at start up.  It's
17194      convenient to do it here.  */
17195   opt_pass *new_pass = make_pass_mips_machine_reorg2 (g);
17196   struct register_pass_info insert_pass_mips_machine_reorg2 =
17197     {
17198       new_pass,         /* pass */
17199       "dbr",                    /* reference_pass_name */
17200       1,                        /* ref_pass_instance_number */
17201       PASS_POS_INSERT_AFTER     /* po_op */
17202     };
17203   register_pass (&insert_pass_mips_machine_reorg2);
17204
17205   if (TARGET_HARD_FLOAT_ABI && TARGET_MIPS5900)
17206     REAL_MODE_FORMAT (SFmode) = &spu_single_format;
17207 }
17208
17209 /* Swap the register information for registers I and I + 1, which
17210    currently have the wrong endianness.  Note that the registers'
17211    fixedness and call-clobberedness might have been set on the
17212    command line.  */
17213
17214 static void
17215 mips_swap_registers (unsigned int i)
17216 {
17217   int tmpi;
17218   const char *tmps;
17219
17220 #define SWAP_INT(X, Y) (tmpi = (X), (X) = (Y), (Y) = tmpi)
17221 #define SWAP_STRING(X, Y) (tmps = (X), (X) = (Y), (Y) = tmps)
17222
17223   SWAP_INT (fixed_regs[i], fixed_regs[i + 1]);
17224   SWAP_INT (call_used_regs[i], call_used_regs[i + 1]);
17225   SWAP_INT (call_really_used_regs[i], call_really_used_regs[i + 1]);
17226   SWAP_STRING (reg_names[i], reg_names[i + 1]);
17227
17228 #undef SWAP_STRING
17229 #undef SWAP_INT
17230 }
17231
17232 /* Implement TARGET_CONDITIONAL_REGISTER_USAGE.  */
17233
17234 static void
17235 mips_conditional_register_usage (void)
17236 {
17237
17238   if (ISA_HAS_DSP)
17239     {
17240       /* These DSP control register fields are global.  */
17241       global_regs[CCDSP_PO_REGNUM] = 1;
17242       global_regs[CCDSP_SC_REGNUM] = 1;
17243     }
17244   else
17245     AND_COMPL_HARD_REG_SET (accessible_reg_set,
17246                             reg_class_contents[(int) DSP_ACC_REGS]);
17247
17248   if (!TARGET_HARD_FLOAT)
17249     {
17250       AND_COMPL_HARD_REG_SET (accessible_reg_set,
17251                               reg_class_contents[(int) FP_REGS]);
17252       AND_COMPL_HARD_REG_SET (accessible_reg_set,
17253                               reg_class_contents[(int) ST_REGS]);
17254     }
17255   else if (!ISA_HAS_8CC)
17256     {
17257       /* We only have a single condition-code register.  We implement
17258          this by fixing all the condition-code registers and generating
17259          RTL that refers directly to ST_REG_FIRST.  */
17260       AND_COMPL_HARD_REG_SET (accessible_reg_set,
17261                               reg_class_contents[(int) ST_REGS]);
17262       SET_HARD_REG_BIT (accessible_reg_set, FPSW_REGNUM);
17263       fixed_regs[FPSW_REGNUM] = call_used_regs[FPSW_REGNUM] = 1;
17264     }
17265   if (TARGET_MIPS16)
17266     {
17267       /* In MIPS16 mode, we prohibit the unused $s registers, since they
17268          are call-saved, and saving them via a MIPS16 register would
17269          probably waste more time than just reloading the value.
17270
17271          We permit the $t temporary registers when optimizing for speed
17272          but not when optimizing for space because using them results in
17273          code that is larger (but faster) then not using them.  We do
17274          allow $24 (t8) because it is used in CMP and CMPI instructions
17275          and $25 (t9) because it is used as the function call address in
17276          SVR4 PIC code.  */
17277
17278       fixed_regs[18] = call_used_regs[18] = 1;
17279       fixed_regs[19] = call_used_regs[19] = 1;
17280       fixed_regs[20] = call_used_regs[20] = 1;
17281       fixed_regs[21] = call_used_regs[21] = 1;
17282       fixed_regs[22] = call_used_regs[22] = 1;
17283       fixed_regs[23] = call_used_regs[23] = 1;
17284       fixed_regs[26] = call_used_regs[26] = 1;
17285       fixed_regs[27] = call_used_regs[27] = 1;
17286       fixed_regs[30] = call_used_regs[30] = 1;
17287       if (optimize_size)
17288         {
17289           fixed_regs[8] = call_used_regs[8] = 1;
17290           fixed_regs[9] = call_used_regs[9] = 1;
17291           fixed_regs[10] = call_used_regs[10] = 1;
17292           fixed_regs[11] = call_used_regs[11] = 1;
17293           fixed_regs[12] = call_used_regs[12] = 1;
17294           fixed_regs[13] = call_used_regs[13] = 1;
17295           fixed_regs[14] = call_used_regs[14] = 1;
17296           fixed_regs[15] = call_used_regs[15] = 1;
17297         }
17298
17299       /* Do not allow HI and LO to be treated as register operands.
17300          There are no MTHI or MTLO instructions (or any real need
17301          for them) and one-way registers cannot easily be reloaded.  */
17302       AND_COMPL_HARD_REG_SET (operand_reg_set,
17303                               reg_class_contents[(int) MD_REGS]);
17304     }
17305   /* $f20-$f23 are call-clobbered for n64.  */
17306   if (mips_abi == ABI_64)
17307     {
17308       int regno;
17309       for (regno = FP_REG_FIRST + 20; regno < FP_REG_FIRST + 24; regno++)
17310         call_really_used_regs[regno] = call_used_regs[regno] = 1;
17311     }
17312   /* Odd registers in the range $f21-$f31 (inclusive) are call-clobbered
17313      for n32.  */
17314   if (mips_abi == ABI_N32)
17315     {
17316       int regno;
17317       for (regno = FP_REG_FIRST + 21; regno <= FP_REG_FIRST + 31; regno+=2)
17318         call_really_used_regs[regno] = call_used_regs[regno] = 1;
17319     }
17320   /* Make sure that double-register accumulator values are correctly
17321      ordered for the current endianness.  */
17322   if (TARGET_LITTLE_ENDIAN)
17323     {
17324       unsigned int regno;
17325
17326       mips_swap_registers (MD_REG_FIRST);
17327       for (regno = DSP_ACC_REG_FIRST; regno <= DSP_ACC_REG_LAST; regno += 2)
17328         mips_swap_registers (regno);
17329     }
17330 }
17331
17332 /* When generating MIPS16 code, we want to allocate $24 (T_REG) before
17333    other registers for instructions for which it is possible.  This
17334    encourages the compiler to use CMP in cases where an XOR would
17335    require some register shuffling.  */
17336
17337 void
17338 mips_order_regs_for_local_alloc (void)
17339 {
17340   int i;
17341
17342   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
17343     reg_alloc_order[i] = i;
17344
17345   if (TARGET_MIPS16)
17346     {
17347       /* It really doesn't matter where we put register 0, since it is
17348          a fixed register anyhow.  */
17349       reg_alloc_order[0] = 24;
17350       reg_alloc_order[24] = 0;
17351     }
17352 }
17353
17354 /* Implement EH_USES.  */
17355
17356 bool
17357 mips_eh_uses (unsigned int regno)
17358 {
17359   if (reload_completed && !TARGET_ABSOLUTE_JUMPS)
17360     {
17361       /* We need to force certain registers to be live in order to handle
17362          PIC long branches correctly.  See mips_must_initialize_gp_p for
17363          details.  */
17364       if (mips_cfun_has_cprestore_slot_p ())
17365         {
17366           if (regno == CPRESTORE_SLOT_REGNUM)
17367             return true;
17368         }
17369       else
17370         {
17371           if (cfun->machine->global_pointer == regno)
17372             return true;
17373         }
17374     }
17375
17376   return false;
17377 }
17378
17379 /* Implement EPILOGUE_USES.  */
17380
17381 bool
17382 mips_epilogue_uses (unsigned int regno)
17383 {
17384   /* Say that the epilogue uses the return address register.  Note that
17385      in the case of sibcalls, the values "used by the epilogue" are
17386      considered live at the start of the called function.  */
17387   if (regno == RETURN_ADDR_REGNUM)
17388     return true;
17389
17390   /* If using a GOT, say that the epilogue also uses GOT_VERSION_REGNUM.
17391      See the comment above load_call<mode> for details.  */
17392   if (TARGET_USE_GOT && (regno) == GOT_VERSION_REGNUM)
17393     return true;
17394
17395   /* An interrupt handler must preserve some registers that are
17396      ordinarily call-clobbered.  */
17397   if (cfun->machine->interrupt_handler_p
17398       && mips_interrupt_extra_call_saved_reg_p (regno))
17399     return true;
17400
17401   return false;
17402 }
17403
17404 /* A for_each_rtx callback.  Stop the search if *X is an AT register.  */
17405
17406 static int
17407 mips_at_reg_p (rtx *x, void *data ATTRIBUTE_UNUSED)
17408 {
17409   return REG_P (*x) && REGNO (*x) == AT_REGNUM;
17410 }
17411
17412 /* Return true if INSN needs to be wrapped in ".set noat".
17413    INSN has NOPERANDS operands, stored in OPVEC.  */
17414
17415 static bool
17416 mips_need_noat_wrapper_p (rtx insn, rtx *opvec, int noperands)
17417 {
17418   int i;
17419
17420   if (recog_memoized (insn) >= 0)
17421     for (i = 0; i < noperands; i++)
17422       if (for_each_rtx (&opvec[i], mips_at_reg_p, NULL))
17423         return true;
17424   return false;
17425 }
17426
17427 /* Implement FINAL_PRESCAN_INSN.  */
17428
17429 void
17430 mips_final_prescan_insn (rtx insn, rtx *opvec, int noperands)
17431 {
17432   if (mips_need_noat_wrapper_p (insn, opvec, noperands))
17433     mips_push_asm_switch (&mips_noat);
17434 }
17435
17436 /* Implement TARGET_ASM_FINAL_POSTSCAN_INSN.  */
17437
17438 static void
17439 mips_final_postscan_insn (FILE *file ATTRIBUTE_UNUSED, rtx insn,
17440                           rtx *opvec, int noperands)
17441 {
17442   if (mips_need_noat_wrapper_p (insn, opvec, noperands))
17443     mips_pop_asm_switch (&mips_noat);
17444 }
17445
17446 /* Return the function that is used to expand the <u>mulsidi3 pattern.
17447    EXT_CODE is the code of the extension used.  Return NULL if widening
17448    multiplication shouldn't be used.  */
17449
17450 mulsidi3_gen_fn
17451 mips_mulsidi3_gen_fn (enum rtx_code ext_code)
17452 {
17453   bool signed_p;
17454
17455   signed_p = ext_code == SIGN_EXTEND;
17456   if (TARGET_64BIT)
17457     {
17458       /* Don't use widening multiplication with MULT when we have DMUL.  Even
17459          with the extension of its input operands DMUL is faster.  Note that
17460          the extension is not needed for signed multiplication.  In order to
17461          ensure that we always remove the redundant sign-extension in this
17462          case we still expand mulsidi3 for DMUL.  */
17463       if (ISA_HAS_DMUL3)
17464         return signed_p ? gen_mulsidi3_64bit_dmul : NULL;
17465       if (TARGET_MIPS16)
17466         return (signed_p
17467                 ? gen_mulsidi3_64bit_mips16
17468                 : gen_umulsidi3_64bit_mips16);
17469       if (TARGET_FIX_R4000)
17470         return NULL;
17471       return signed_p ? gen_mulsidi3_64bit : gen_umulsidi3_64bit;
17472     }
17473   else
17474     {
17475       if (TARGET_MIPS16)
17476         return (signed_p
17477                 ? gen_mulsidi3_32bit_mips16
17478                 : gen_umulsidi3_32bit_mips16);
17479       if (TARGET_FIX_R4000 && !ISA_HAS_DSP)
17480         return signed_p ? gen_mulsidi3_32bit_r4000 : gen_umulsidi3_32bit_r4000;
17481       return signed_p ? gen_mulsidi3_32bit : gen_umulsidi3_32bit;
17482     }
17483 }
17484
17485 /* Return true if PATTERN matches the kind of instruction generated by
17486    umips_build_save_restore.  SAVE_P is true for store.  */
17487
17488 bool
17489 umips_save_restore_pattern_p (bool save_p, rtx pattern)
17490 {
17491   int n;
17492   unsigned int i;
17493   HOST_WIDE_INT first_offset = 0;
17494   rtx first_base = 0;
17495   unsigned int regmask = 0;
17496
17497   for (n = 0; n < XVECLEN (pattern, 0); n++)
17498     {
17499       rtx set, reg, mem, this_base;
17500       HOST_WIDE_INT this_offset;
17501
17502       /* Check that we have a SET.  */
17503       set = XVECEXP (pattern, 0, n);
17504       if (GET_CODE (set) != SET)
17505         return false;
17506
17507       /* Check that the SET is a load (if restoring) or a store
17508          (if saving).  */
17509       mem = save_p ? SET_DEST (set) : SET_SRC (set);
17510       if (!MEM_P (mem) || MEM_VOLATILE_P (mem))
17511         return false;
17512
17513       /* Check that the address is the sum of base and a possibly-zero
17514          constant offset.  Determine if the offset is in range.  */
17515       mips_split_plus (XEXP (mem, 0), &this_base, &this_offset);
17516       if (!REG_P (this_base))
17517         return false;
17518
17519       if (n == 0)
17520         {
17521           if (!UMIPS_12BIT_OFFSET_P (this_offset))
17522             return false;
17523           first_base = this_base;
17524           first_offset = this_offset;
17525         }
17526       else
17527         {
17528           /* Check that the save slots are consecutive.  */
17529           if (REGNO (this_base) != REGNO (first_base)
17530               || this_offset != first_offset + UNITS_PER_WORD * n)
17531             return false;
17532         }
17533
17534       /* Check that SET's other operand is a register.  */
17535       reg = save_p ? SET_SRC (set) : SET_DEST (set);
17536       if (!REG_P (reg))
17537         return false;
17538
17539       regmask |= 1 << REGNO (reg);
17540     }
17541
17542   for (i = 0; i < ARRAY_SIZE (umips_swm_mask); i++)
17543     if (regmask == umips_swm_mask[i])
17544       return true;
17545
17546   return false;
17547 }
17548
17549 /* Return the assembly instruction for microMIPS LWM or SWM.
17550    SAVE_P and PATTERN are as for umips_save_restore_pattern_p.  */
17551
17552 const char *
17553 umips_output_save_restore (bool save_p, rtx pattern)
17554 {
17555   static char buffer[300];
17556   char *s;
17557   int n;
17558   HOST_WIDE_INT offset;
17559   rtx base, mem, set, last_set, last_reg;
17560
17561   /* Parse the pattern.  */
17562   gcc_assert (umips_save_restore_pattern_p (save_p, pattern));
17563
17564   s = strcpy (buffer, save_p ? "swm\t" : "lwm\t");
17565   s += strlen (s);
17566   n = XVECLEN (pattern, 0);
17567
17568   set = XVECEXP (pattern, 0, 0);
17569   mem = save_p ? SET_DEST (set) : SET_SRC (set);
17570   mips_split_plus (XEXP (mem, 0), &base, &offset);
17571
17572   last_set = XVECEXP (pattern, 0, n - 1);
17573   last_reg = save_p ? SET_SRC (last_set) : SET_DEST (last_set);
17574
17575   if (REGNO (last_reg) == 31)
17576     n--;
17577
17578   gcc_assert (n <= 9);
17579   if (n == 0)
17580     ;
17581   else if (n == 1)
17582     s += sprintf (s, "%s,", reg_names[16]);
17583   else if (n < 9)
17584     s += sprintf (s, "%s-%s,", reg_names[16], reg_names[15 + n]);
17585   else if (n == 9)
17586     s += sprintf (s, "%s-%s,%s,", reg_names[16], reg_names[23],
17587                   reg_names[30]);
17588
17589   if (REGNO (last_reg) == 31)
17590     s += sprintf (s, "%s,", reg_names[31]);
17591
17592   s += sprintf (s, "%d(%s)", (int)offset, reg_names[REGNO (base)]);
17593   return buffer;
17594 }
17595
17596 /* Return true if MEM1 and MEM2 use the same base register, and the
17597    offset of MEM2 equals the offset of MEM1 plus 4.  FIRST_REG is the
17598    register into (from) which the contents of MEM1 will be loaded
17599    (stored), depending on the value of LOAD_P.
17600    SWAP_P is true when the 1st and 2nd instructions are swapped.  */
17601
17602 static bool
17603 umips_load_store_pair_p_1 (bool load_p, bool swap_p,
17604                            rtx first_reg, rtx mem1, rtx mem2)
17605 {
17606   rtx base1, base2;
17607   HOST_WIDE_INT offset1, offset2;
17608
17609   if (!MEM_P (mem1) || !MEM_P (mem2))
17610     return false;
17611
17612   mips_split_plus (XEXP (mem1, 0), &base1, &offset1);
17613   mips_split_plus (XEXP (mem2, 0), &base2, &offset2);
17614
17615   if (!REG_P (base1) || !rtx_equal_p (base1, base2))
17616     return false;
17617
17618   /* Avoid invalid load pair instructions.  */
17619   if (load_p && REGNO (first_reg) == REGNO (base1))
17620     return false;
17621
17622   /* We must avoid this case for anti-dependence.
17623      Ex:  lw $3, 4($3)
17624           lw $2, 0($3)
17625      first_reg is $2, but the base is $3.  */
17626   if (load_p
17627       && swap_p
17628       && REGNO (first_reg) + 1 == REGNO (base1))
17629     return false;
17630
17631   if (offset2 != offset1 + 4)
17632     return false;
17633
17634   if (!UMIPS_12BIT_OFFSET_P (offset1))
17635     return false;
17636
17637   return true;
17638 }
17639
17640 /* OPERANDS describes the operands to a pair of SETs, in the order
17641    dest1, src1, dest2, src2.  Return true if the operands can be used
17642    in an LWP or SWP instruction; LOAD_P says which.  */
17643
17644 bool
17645 umips_load_store_pair_p (bool load_p, rtx *operands)
17646 {
17647   rtx reg1, reg2, mem1, mem2;
17648
17649   if (load_p)
17650     {
17651       reg1 = operands[0];
17652       reg2 = operands[2];
17653       mem1 = operands[1];
17654       mem2 = operands[3];
17655     }
17656   else
17657     {
17658       reg1 = operands[1];
17659       reg2 = operands[3];
17660       mem1 = operands[0];
17661       mem2 = operands[2];
17662     }
17663
17664   if (REGNO (reg2) == REGNO (reg1) + 1)
17665     return umips_load_store_pair_p_1 (load_p, false, reg1, mem1, mem2);
17666
17667   if (REGNO (reg1) == REGNO (reg2) + 1)
17668     return umips_load_store_pair_p_1 (load_p, true, reg2, mem2, mem1);
17669
17670   return false;
17671 }
17672
17673 /* Return the assembly instruction for a microMIPS LWP or SWP in which
17674    the first register is REG and the first memory slot is MEM.
17675    LOAD_P is true for LWP.  */
17676
17677 static void
17678 umips_output_load_store_pair_1 (bool load_p, rtx reg, rtx mem)
17679 {
17680   rtx ops[] = {reg, mem};
17681
17682   if (load_p)
17683     output_asm_insn ("lwp\t%0,%1", ops);
17684   else
17685     output_asm_insn ("swp\t%0,%1", ops);
17686 }
17687
17688 /* Output the assembly instruction for a microMIPS LWP or SWP instruction.
17689    LOAD_P and OPERANDS are as for umips_load_store_pair_p.  */
17690
17691 void
17692 umips_output_load_store_pair (bool load_p, rtx *operands)
17693 {
17694   rtx reg1, reg2, mem1, mem2;
17695   if (load_p)
17696     {
17697       reg1 = operands[0];
17698       reg2 = operands[2];
17699       mem1 = operands[1];
17700       mem2 = operands[3];
17701     }
17702   else
17703     {
17704       reg1 = operands[1];
17705       reg2 = operands[3];
17706       mem1 = operands[0];
17707       mem2 = operands[2];
17708     }
17709
17710   if (REGNO (reg2) == REGNO (reg1) + 1)
17711     {
17712       umips_output_load_store_pair_1 (load_p, reg1, mem1);
17713       return;
17714     }
17715
17716   gcc_assert (REGNO (reg1) == REGNO (reg2) + 1);
17717   umips_output_load_store_pair_1 (load_p, reg2, mem2);
17718 }
17719
17720 /* Return true if REG1 and REG2 match the criteria for a movep insn.  */
17721
17722 bool
17723 umips_movep_target_p (rtx reg1, rtx reg2)
17724 {
17725   int regno1, regno2, pair;
17726   unsigned int i;
17727   static const int match[8] = {
17728     0x00000060, /* 5, 6 */
17729     0x000000a0, /* 5, 7 */
17730     0x000000c0, /* 6, 7 */
17731     0x00200010, /* 4, 21 */
17732     0x00400010, /* 4, 22 */
17733     0x00000030, /* 4, 5 */
17734     0x00000050, /* 4, 6 */
17735     0x00000090  /* 4, 7 */
17736   };
17737
17738   if (!REG_P (reg1) || !REG_P (reg2))
17739     return false;
17740
17741   regno1 = REGNO (reg1);
17742   regno2 = REGNO (reg2);
17743
17744   if (!GP_REG_P (regno1) || !GP_REG_P (regno2))
17745     return false;
17746
17747   pair = (1 << regno1) | (1 << regno2);
17748
17749   for (i = 0; i < ARRAY_SIZE (match); i++)
17750     if (pair == match[i])
17751       return true;
17752
17753   return false;
17754 }
17755 \f
17756 /* Return the size in bytes of the trampoline code, padded to
17757    TRAMPOLINE_ALIGNMENT bits.  The static chain pointer and target
17758    function address immediately follow.  */
17759
17760 int
17761 mips_trampoline_code_size (void)
17762 {
17763   if (TARGET_USE_PIC_FN_ADDR_REG)
17764     return 4 * 4;
17765   else if (ptr_mode == DImode)
17766     return 8 * 4;
17767   else if (ISA_HAS_LOAD_DELAY)
17768     return 6 * 4;
17769   else
17770     return 4 * 4;
17771 }
17772
17773 /* Implement TARGET_TRAMPOLINE_INIT.  */
17774
17775 static void
17776 mips_trampoline_init (rtx m_tramp, tree fndecl, rtx chain_value)
17777 {
17778   rtx addr, end_addr, high, low, opcode, mem;
17779   rtx trampoline[8];
17780   unsigned int i, j;
17781   HOST_WIDE_INT end_addr_offset, static_chain_offset, target_function_offset;
17782
17783   /* Work out the offsets of the pointers from the start of the
17784      trampoline code.  */
17785   end_addr_offset = mips_trampoline_code_size ();
17786   static_chain_offset = end_addr_offset;
17787   target_function_offset = static_chain_offset + GET_MODE_SIZE (ptr_mode);
17788
17789   /* Get pointers to the beginning and end of the code block.  */
17790   addr = force_reg (Pmode, XEXP (m_tramp, 0));
17791   end_addr = mips_force_binary (Pmode, PLUS, addr, GEN_INT (end_addr_offset));
17792
17793 #define OP(X) gen_int_mode (X, SImode)
17794
17795   /* Build up the code in TRAMPOLINE.  */
17796   i = 0;
17797   if (TARGET_USE_PIC_FN_ADDR_REG)
17798     {
17799       /* $25 contains the address of the trampoline.  Emit code of the form:
17800
17801              l[wd]    $1, target_function_offset($25)
17802              l[wd]    $static_chain, static_chain_offset($25)
17803              jr       $1
17804              move     $25,$1.  */
17805       trampoline[i++] = OP (MIPS_LOAD_PTR (AT_REGNUM,
17806                                            target_function_offset,
17807                                            PIC_FUNCTION_ADDR_REGNUM));
17808       trampoline[i++] = OP (MIPS_LOAD_PTR (STATIC_CHAIN_REGNUM,
17809                                            static_chain_offset,
17810                                            PIC_FUNCTION_ADDR_REGNUM));
17811       trampoline[i++] = OP (MIPS_JR (AT_REGNUM));
17812       trampoline[i++] = OP (MIPS_MOVE (PIC_FUNCTION_ADDR_REGNUM, AT_REGNUM));
17813     }
17814   else if (ptr_mode == DImode)
17815     {
17816       /* It's too cumbersome to create the full 64-bit address, so let's
17817          instead use:
17818
17819              move    $1, $31
17820              bal     1f
17821              nop
17822          1:  l[wd]   $25, target_function_offset - 12($31)
17823              l[wd]   $static_chain, static_chain_offset - 12($31)
17824              jr      $25
17825              move    $31, $1
17826
17827         where 12 is the offset of "1:" from the start of the code block.  */
17828       trampoline[i++] = OP (MIPS_MOVE (AT_REGNUM, RETURN_ADDR_REGNUM));
17829       trampoline[i++] = OP (MIPS_BAL (1));
17830       trampoline[i++] = OP (MIPS_NOP);
17831       trampoline[i++] = OP (MIPS_LOAD_PTR (PIC_FUNCTION_ADDR_REGNUM,
17832                                            target_function_offset - 12,
17833                                            RETURN_ADDR_REGNUM));
17834       trampoline[i++] = OP (MIPS_LOAD_PTR (STATIC_CHAIN_REGNUM,
17835                                            static_chain_offset - 12,
17836                                            RETURN_ADDR_REGNUM));
17837       trampoline[i++] = OP (MIPS_JR (PIC_FUNCTION_ADDR_REGNUM));
17838       trampoline[i++] = OP (MIPS_MOVE (RETURN_ADDR_REGNUM, AT_REGNUM));
17839     }
17840   else
17841     {
17842       /* If the target has load delays, emit:
17843
17844              lui     $1, %hi(end_addr)
17845              lw      $25, %lo(end_addr + ...)($1)
17846              lw      $static_chain, %lo(end_addr + ...)($1)
17847              jr      $25
17848              nop
17849
17850          Otherwise emit:
17851
17852              lui     $1, %hi(end_addr)
17853              lw      $25, %lo(end_addr + ...)($1)
17854              jr      $25
17855              lw      $static_chain, %lo(end_addr + ...)($1).  */
17856
17857       /* Split END_ADDR into %hi and %lo values.  Trampolines are aligned
17858          to 64 bits, so the %lo value will have the bottom 3 bits clear.  */
17859       high = expand_simple_binop (SImode, PLUS, end_addr, GEN_INT (0x8000),
17860                                   NULL, false, OPTAB_WIDEN);
17861       high = expand_simple_binop (SImode, LSHIFTRT, high, GEN_INT (16),
17862                                   NULL, false, OPTAB_WIDEN);
17863       low = convert_to_mode (SImode, gen_lowpart (HImode, end_addr), true);
17864
17865       /* Emit the LUI.  */
17866       opcode = OP (MIPS_LUI (AT_REGNUM, 0));
17867       trampoline[i++] = expand_simple_binop (SImode, IOR, opcode, high,
17868                                              NULL, false, OPTAB_WIDEN);
17869
17870       /* Emit the load of the target function.  */
17871       opcode = OP (MIPS_LOAD_PTR (PIC_FUNCTION_ADDR_REGNUM,
17872                                   target_function_offset - end_addr_offset,
17873                                   AT_REGNUM));
17874       trampoline[i++] = expand_simple_binop (SImode, IOR, opcode, low,
17875                                              NULL, false, OPTAB_WIDEN);
17876
17877       /* Emit the JR here, if we can.  */
17878       if (!ISA_HAS_LOAD_DELAY)
17879         trampoline[i++] = OP (MIPS_JR (PIC_FUNCTION_ADDR_REGNUM));
17880
17881       /* Emit the load of the static chain register.  */
17882       opcode = OP (MIPS_LOAD_PTR (STATIC_CHAIN_REGNUM,
17883                                   static_chain_offset - end_addr_offset,
17884                                   AT_REGNUM));
17885       trampoline[i++] = expand_simple_binop (SImode, IOR, opcode, low,
17886                                              NULL, false, OPTAB_WIDEN);
17887
17888       /* Emit the JR, if we couldn't above.  */
17889       if (ISA_HAS_LOAD_DELAY)
17890         {
17891           trampoline[i++] = OP (MIPS_JR (PIC_FUNCTION_ADDR_REGNUM));
17892           trampoline[i++] = OP (MIPS_NOP);
17893         }
17894     }
17895
17896 #undef OP
17897
17898   /* Copy the trampoline code.  Leave any padding uninitialized.  */
17899   for (j = 0; j < i; j++)
17900     {
17901       mem = adjust_address (m_tramp, SImode, j * GET_MODE_SIZE (SImode));
17902       mips_emit_move (mem, trampoline[j]);
17903     }
17904
17905   /* Set up the static chain pointer field.  */
17906   mem = adjust_address (m_tramp, ptr_mode, static_chain_offset);
17907   mips_emit_move (mem, chain_value);
17908
17909   /* Set up the target function field.  */
17910   mem = adjust_address (m_tramp, ptr_mode, target_function_offset);
17911   mips_emit_move (mem, XEXP (DECL_RTL (fndecl), 0));
17912
17913   /* Flush the code part of the trampoline.  */
17914   emit_insn (gen_add3_insn (end_addr, addr, GEN_INT (TRAMPOLINE_SIZE)));
17915   emit_insn (gen_clear_cache (addr, end_addr));
17916 }
17917
17918 /* Implement FUNCTION_PROFILER.  */
17919
17920 void mips_function_profiler (FILE *file)
17921 {
17922   if (TARGET_MIPS16)
17923     sorry ("mips16 function profiling");
17924   if (TARGET_LONG_CALLS)
17925     {
17926       /* For TARGET_LONG_CALLS use $3 for the address of _mcount.  */
17927       if (Pmode == DImode)
17928         fprintf (file, "\tdla\t%s,_mcount\n", reg_names[3]);
17929       else
17930         fprintf (file, "\tla\t%s,_mcount\n", reg_names[3]);
17931     }
17932   mips_push_asm_switch (&mips_noat);
17933   fprintf (file, "\tmove\t%s,%s\t\t# save current return address\n",
17934            reg_names[AT_REGNUM], reg_names[RETURN_ADDR_REGNUM]);
17935   /* _mcount treats $2 as the static chain register.  */
17936   if (cfun->static_chain_decl != NULL)
17937     fprintf (file, "\tmove\t%s,%s\n", reg_names[2],
17938              reg_names[STATIC_CHAIN_REGNUM]);
17939   if (TARGET_MCOUNT_RA_ADDRESS)
17940     {
17941       /* If TARGET_MCOUNT_RA_ADDRESS load $12 with the address of the
17942          ra save location.  */
17943       if (cfun->machine->frame.ra_fp_offset == 0)
17944         /* ra not saved, pass zero.  */
17945         fprintf (file, "\tmove\t%s,%s\n", reg_names[12], reg_names[0]);
17946       else
17947         fprintf (file, "\t%s\t%s," HOST_WIDE_INT_PRINT_DEC "(%s)\n",
17948                  Pmode == DImode ? "dla" : "la", reg_names[12],
17949                  cfun->machine->frame.ra_fp_offset,
17950                  reg_names[STACK_POINTER_REGNUM]);
17951     }
17952   if (!TARGET_NEWABI)
17953     fprintf (file,
17954              "\t%s\t%s,%s,%d\t\t# _mcount pops 2 words from  stack\n",
17955              TARGET_64BIT ? "dsubu" : "subu",
17956              reg_names[STACK_POINTER_REGNUM],
17957              reg_names[STACK_POINTER_REGNUM],
17958              Pmode == DImode ? 16 : 8);
17959
17960   if (TARGET_LONG_CALLS)
17961     fprintf (file, "\tjalr\t%s\n", reg_names[3]);
17962   else
17963     fprintf (file, "\tjal\t_mcount\n");
17964   mips_pop_asm_switch (&mips_noat);
17965   /* _mcount treats $2 as the static chain register.  */
17966   if (cfun->static_chain_decl != NULL)
17967     fprintf (file, "\tmove\t%s,%s\n", reg_names[STATIC_CHAIN_REGNUM],
17968              reg_names[2]);
17969 }
17970
17971 /* Implement TARGET_SHIFT_TRUNCATION_MASK.  We want to keep the default
17972    behaviour of TARGET_SHIFT_TRUNCATION_MASK for non-vector modes even
17973    when TARGET_LOONGSON_VECTORS is true.  */
17974
17975 static unsigned HOST_WIDE_INT
17976 mips_shift_truncation_mask (enum machine_mode mode)
17977 {
17978   if (TARGET_LOONGSON_VECTORS && VECTOR_MODE_P (mode))
17979     return 0;
17980
17981   return GET_MODE_BITSIZE (mode) - 1;
17982 }
17983
17984 /* Implement TARGET_PREPARE_PCH_SAVE.  */
17985
17986 static void
17987 mips_prepare_pch_save (void)
17988 {
17989   /* We are called in a context where the current MIPS16 vs. non-MIPS16
17990      setting should be irrelevant.  The question then is: which setting
17991      makes most sense at load time?
17992
17993      The PCH is loaded before the first token is read.  We should never
17994      have switched into MIPS16 mode by that point, and thus should not
17995      have populated mips16_globals.  Nor can we load the entire contents
17996      of mips16_globals from the PCH file, because mips16_globals contains
17997      a combination of GGC and non-GGC data.
17998
17999      There is therefore no point in trying save the GGC part of
18000      mips16_globals to the PCH file, or to preserve MIPS16ness across
18001      the PCH save and load.  The loading compiler would not have access
18002      to the non-GGC parts of mips16_globals (either from the PCH file,
18003      or from a copy that the loading compiler generated itself) and would
18004      have to call target_reinit anyway.
18005
18006      It therefore seems best to switch back to non-MIPS16 mode at
18007      save time, and to ensure that mips16_globals remains null after
18008      a PCH load.  */
18009   mips_set_compression_mode (0);
18010   mips16_globals = 0;
18011 }
18012 \f
18013 /* Generate or test for an insn that supports a constant permutation.  */
18014
18015 #define MAX_VECT_LEN 8
18016
18017 struct expand_vec_perm_d
18018 {
18019   rtx target, op0, op1;
18020   unsigned char perm[MAX_VECT_LEN];
18021   enum machine_mode vmode;
18022   unsigned char nelt;
18023   bool one_vector_p;
18024   bool testing_p;
18025 };
18026
18027 /* Construct (set target (vec_select op0 (parallel perm))) and
18028    return true if that's a valid instruction in the active ISA.  */
18029
18030 static bool
18031 mips_expand_vselect (rtx target, rtx op0,
18032                      const unsigned char *perm, unsigned nelt)
18033 {
18034   rtx rperm[MAX_VECT_LEN], x;
18035   unsigned i;
18036
18037   for (i = 0; i < nelt; ++i)
18038     rperm[i] = GEN_INT (perm[i]);
18039
18040   x = gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelt, rperm));
18041   x = gen_rtx_VEC_SELECT (GET_MODE (target), op0, x);
18042   x = gen_rtx_SET (VOIDmode, target, x);
18043
18044   x = emit_insn (x);
18045   if (recog_memoized (x) < 0)
18046     {
18047       remove_insn (x);
18048       return false;
18049     }
18050   return true;
18051 }
18052
18053 /* Similar, but generate a vec_concat from op0 and op1 as well.  */
18054
18055 static bool
18056 mips_expand_vselect_vconcat (rtx target, rtx op0, rtx op1,
18057                              const unsigned char *perm, unsigned nelt)
18058 {
18059   enum machine_mode v2mode;
18060   rtx x;
18061
18062   v2mode = GET_MODE_2XWIDER_MODE (GET_MODE (op0));
18063   x = gen_rtx_VEC_CONCAT (v2mode, op0, op1);
18064   return mips_expand_vselect (target, x, perm, nelt);
18065 }
18066
18067 /* Recognize patterns for even-odd extraction.  */
18068
18069 static bool
18070 mips_expand_vpc_loongson_even_odd (struct expand_vec_perm_d *d)
18071 {
18072   unsigned i, odd, nelt = d->nelt;
18073   rtx t0, t1, t2, t3;
18074
18075   if (!(TARGET_HARD_FLOAT && TARGET_LOONGSON_VECTORS))
18076     return false;
18077   /* Even-odd for V2SI/V2SFmode is matched by interleave directly.  */
18078   if (nelt < 4)
18079     return false;
18080
18081   odd = d->perm[0];
18082   if (odd > 1)
18083     return false;
18084   for (i = 1; i < nelt; ++i)
18085     if (d->perm[i] != i * 2 + odd)
18086       return false;
18087
18088   if (d->testing_p)
18089     return true;
18090
18091   /* We need 2*log2(N)-1 operations to achieve odd/even with interleave. */
18092   t0 = gen_reg_rtx (d->vmode);
18093   t1 = gen_reg_rtx (d->vmode);
18094   switch (d->vmode)
18095     {
18096     case V4HImode:
18097       emit_insn (gen_loongson_punpckhhw (t0, d->op0, d->op1));
18098       emit_insn (gen_loongson_punpcklhw (t1, d->op0, d->op1));
18099       if (odd)
18100         emit_insn (gen_loongson_punpckhhw (d->target, t1, t0));
18101       else
18102         emit_insn (gen_loongson_punpcklhw (d->target, t1, t0));
18103       break;
18104
18105     case V8QImode:
18106       t2 = gen_reg_rtx (d->vmode);
18107       t3 = gen_reg_rtx (d->vmode);
18108       emit_insn (gen_loongson_punpckhbh (t0, d->op0, d->op1));
18109       emit_insn (gen_loongson_punpcklbh (t1, d->op0, d->op1));
18110       emit_insn (gen_loongson_punpckhbh (t2, t1, t0));
18111       emit_insn (gen_loongson_punpcklbh (t3, t1, t0));
18112       if (odd)
18113         emit_insn (gen_loongson_punpckhbh (d->target, t3, t2));
18114       else
18115         emit_insn (gen_loongson_punpcklbh (d->target, t3, t2));
18116       break;
18117
18118     default:
18119       gcc_unreachable ();
18120     }
18121   return true;
18122 }
18123
18124 /* Recognize patterns for the Loongson PSHUFH instruction.  */
18125
18126 static bool
18127 mips_expand_vpc_loongson_pshufh (struct expand_vec_perm_d *d)
18128 {
18129   unsigned i, mask;
18130   rtx rmask;
18131
18132   if (!(TARGET_HARD_FLOAT && TARGET_LOONGSON_VECTORS))
18133     return false;
18134   if (d->vmode != V4HImode)
18135     return false;
18136   if (d->testing_p)
18137     return true;
18138
18139   /* Convert the selector into the packed 8-bit form for pshufh.  */
18140   /* Recall that loongson is little-endian only.  No big-endian
18141      adjustment required.  */
18142   for (i = mask = 0; i < 4; i++)
18143     mask |= (d->perm[i] & 3) << (i * 2);
18144   rmask = force_reg (SImode, GEN_INT (mask));
18145
18146   if (d->one_vector_p)
18147     emit_insn (gen_loongson_pshufh (d->target, d->op0, rmask));
18148   else
18149     {
18150       rtx t0, t1, x, merge, rmerge[4];
18151
18152       t0 = gen_reg_rtx (V4HImode);
18153       t1 = gen_reg_rtx (V4HImode);
18154       emit_insn (gen_loongson_pshufh (t1, d->op1, rmask));
18155       emit_insn (gen_loongson_pshufh (t0, d->op0, rmask));
18156
18157       for (i = 0; i < 4; ++i)
18158         rmerge[i] = (d->perm[i] & 4 ? constm1_rtx : const0_rtx);
18159       merge = gen_rtx_CONST_VECTOR (V4HImode, gen_rtvec_v (4, rmerge));
18160       merge = force_reg (V4HImode, merge);
18161
18162       x = gen_rtx_AND (V4HImode, merge, t1);
18163       emit_insn (gen_rtx_SET (VOIDmode, t1, x));
18164
18165       x = gen_rtx_NOT (V4HImode, merge);
18166       x = gen_rtx_AND (V4HImode, x, t0);
18167       emit_insn (gen_rtx_SET (VOIDmode, t0, x));
18168
18169       x = gen_rtx_IOR (V4HImode, t0, t1);
18170       emit_insn (gen_rtx_SET (VOIDmode, d->target, x));
18171     }
18172
18173   return true;
18174 }
18175
18176 /* Recognize broadcast patterns for the Loongson.  */
18177
18178 static bool
18179 mips_expand_vpc_loongson_bcast (struct expand_vec_perm_d *d)
18180 {
18181   unsigned i, elt;
18182   rtx t0, t1;
18183
18184   if (!(TARGET_HARD_FLOAT && TARGET_LOONGSON_VECTORS))
18185     return false;
18186   /* Note that we've already matched V2SI via punpck and V4HI via pshufh.  */
18187   if (d->vmode != V8QImode)
18188     return false;
18189   if (!d->one_vector_p)
18190     return false;
18191
18192   elt = d->perm[0];
18193   for (i = 1; i < 8; ++i)
18194     if (d->perm[i] != elt)
18195       return false;
18196
18197   if (d->testing_p)
18198     return true;
18199
18200   /* With one interleave we put two of the desired element adjacent.  */
18201   t0 = gen_reg_rtx (V8QImode);
18202   if (elt < 4)
18203     emit_insn (gen_loongson_punpcklbh (t0, d->op0, d->op0));
18204   else
18205     emit_insn (gen_loongson_punpckhbh (t0, d->op0, d->op0));
18206
18207   /* Shuffle that one HImode element into all locations.  */
18208   elt &= 3;
18209   elt *= 0x55;
18210   t1 = gen_reg_rtx (V4HImode);
18211   emit_insn (gen_loongson_pshufh (t1, gen_lowpart (V4HImode, t0),
18212                                   force_reg (SImode, GEN_INT (elt))));
18213
18214   emit_move_insn (d->target, gen_lowpart (V8QImode, t1));
18215   return true;
18216 }
18217
18218 static bool
18219 mips_expand_vec_perm_const_1 (struct expand_vec_perm_d *d)
18220 {
18221   unsigned int i, nelt = d->nelt;
18222   unsigned char perm2[MAX_VECT_LEN];
18223
18224   if (d->one_vector_p)
18225     {
18226       /* Try interleave with alternating operands.  */
18227       memcpy (perm2, d->perm, sizeof(perm2));
18228       for (i = 1; i < nelt; i += 2)
18229         perm2[i] += nelt;
18230       if (mips_expand_vselect_vconcat (d->target, d->op0, d->op1, perm2, nelt))
18231         return true;
18232     }
18233   else
18234     {
18235       if (mips_expand_vselect_vconcat (d->target, d->op0, d->op1,
18236                                        d->perm, nelt))
18237         return true;
18238
18239       /* Try again with swapped operands.  */
18240       for (i = 0; i < nelt; ++i)
18241         perm2[i] = (d->perm[i] + nelt) & (2 * nelt - 1);
18242       if (mips_expand_vselect_vconcat (d->target, d->op1, d->op0, perm2, nelt))
18243         return true;
18244     }
18245
18246   if (mips_expand_vpc_loongson_even_odd (d))
18247     return true;
18248   if (mips_expand_vpc_loongson_pshufh (d))
18249     return true;
18250   if (mips_expand_vpc_loongson_bcast (d))
18251     return true;
18252   return false;
18253 }
18254
18255 /* Expand a vec_perm_const pattern.  */
18256
18257 bool
18258 mips_expand_vec_perm_const (rtx operands[4])
18259 {
18260   struct expand_vec_perm_d d;
18261   int i, nelt, which;
18262   unsigned char orig_perm[MAX_VECT_LEN];
18263   rtx sel;
18264   bool ok;
18265
18266   d.target = operands[0];
18267   d.op0 = operands[1];
18268   d.op1 = operands[2];
18269   sel = operands[3];
18270
18271   d.vmode = GET_MODE (d.target);
18272   gcc_assert (VECTOR_MODE_P (d.vmode));
18273   d.nelt = nelt = GET_MODE_NUNITS (d.vmode);
18274   d.testing_p = false;
18275
18276   for (i = which = 0; i < nelt; ++i)
18277     {
18278       rtx e = XVECEXP (sel, 0, i);
18279       int ei = INTVAL (e) & (2 * nelt - 1);
18280       which |= (ei < nelt ? 1 : 2);
18281       orig_perm[i] = ei;
18282     }
18283   memcpy (d.perm, orig_perm, MAX_VECT_LEN);
18284
18285   switch (which)
18286     {
18287     default:
18288       gcc_unreachable();
18289
18290     case 3:
18291       d.one_vector_p = false;
18292       if (!rtx_equal_p (d.op0, d.op1))
18293         break;
18294       /* FALLTHRU */
18295
18296     case 2:
18297       for (i = 0; i < nelt; ++i)
18298         d.perm[i] &= nelt - 1;
18299       d.op0 = d.op1;
18300       d.one_vector_p = true;
18301       break;
18302
18303     case 1:
18304       d.op1 = d.op0;
18305       d.one_vector_p = true;
18306       break;
18307     }
18308
18309   ok = mips_expand_vec_perm_const_1 (&d);
18310
18311   /* If we were given a two-vector permutation which just happened to
18312      have both input vectors equal, we folded this into a one-vector
18313      permutation.  There are several loongson patterns that are matched
18314      via direct vec_select+vec_concat expansion, but we do not have
18315      support in mips_expand_vec_perm_const_1 to guess the adjustment
18316      that should be made for a single operand.  Just try again with
18317      the original permutation.  */
18318   if (!ok && which == 3)
18319     {
18320       d.op0 = operands[1];
18321       d.op1 = operands[2];
18322       d.one_vector_p = false;
18323       memcpy (d.perm, orig_perm, MAX_VECT_LEN);
18324       ok = mips_expand_vec_perm_const_1 (&d);
18325     }
18326
18327   return ok;
18328 }
18329
18330 /* Implement TARGET_VECTORIZE_VEC_PERM_CONST_OK.  */
18331
18332 static bool
18333 mips_vectorize_vec_perm_const_ok (enum machine_mode vmode,
18334                                   const unsigned char *sel)
18335 {
18336   struct expand_vec_perm_d d;
18337   unsigned int i, nelt, which;
18338   bool ret;
18339
18340   d.vmode = vmode;
18341   d.nelt = nelt = GET_MODE_NUNITS (d.vmode);
18342   d.testing_p = true;
18343   memcpy (d.perm, sel, nelt);
18344
18345   /* Categorize the set of elements in the selector.  */
18346   for (i = which = 0; i < nelt; ++i)
18347     {
18348       unsigned char e = d.perm[i];
18349       gcc_assert (e < 2 * nelt);
18350       which |= (e < nelt ? 1 : 2);
18351     }
18352
18353   /* For all elements from second vector, fold the elements to first.  */
18354   if (which == 2)
18355     for (i = 0; i < nelt; ++i)
18356       d.perm[i] -= nelt;
18357
18358   /* Check whether the mask can be applied to the vector type.  */
18359   d.one_vector_p = (which != 3);
18360
18361   d.target = gen_raw_REG (d.vmode, LAST_VIRTUAL_REGISTER + 1);
18362   d.op1 = d.op0 = gen_raw_REG (d.vmode, LAST_VIRTUAL_REGISTER + 2);
18363   if (!d.one_vector_p)
18364     d.op1 = gen_raw_REG (d.vmode, LAST_VIRTUAL_REGISTER + 3);
18365
18366   start_sequence ();
18367   ret = mips_expand_vec_perm_const_1 (&d);
18368   end_sequence ();
18369
18370   return ret;
18371 }
18372
18373 /* Expand an integral vector unpack operation.  */
18374
18375 void
18376 mips_expand_vec_unpack (rtx operands[2], bool unsigned_p, bool high_p)
18377 {
18378   enum machine_mode imode = GET_MODE (operands[1]);
18379   rtx (*unpack) (rtx, rtx, rtx);
18380   rtx (*cmpgt) (rtx, rtx, rtx);
18381   rtx tmp, dest, zero;
18382
18383   switch (imode)
18384     {
18385     case V8QImode:
18386       if (high_p)
18387         unpack = gen_loongson_punpckhbh;
18388       else
18389         unpack = gen_loongson_punpcklbh;
18390       cmpgt = gen_loongson_pcmpgtb;
18391       break;
18392     case V4HImode:
18393       if (high_p)
18394         unpack = gen_loongson_punpckhhw;
18395       else
18396         unpack = gen_loongson_punpcklhw;
18397       cmpgt = gen_loongson_pcmpgth;
18398       break;
18399     default:
18400       gcc_unreachable ();
18401     }
18402
18403   zero = force_reg (imode, CONST0_RTX (imode));
18404   if (unsigned_p)
18405     tmp = zero;
18406   else
18407     {
18408       tmp = gen_reg_rtx (imode);
18409       emit_insn (cmpgt (tmp, zero, operands[1]));
18410     }
18411
18412   dest = gen_reg_rtx (imode);
18413   emit_insn (unpack (dest, operands[1], tmp));
18414
18415   emit_move_insn (operands[0], gen_lowpart (GET_MODE (operands[0]), dest));
18416 }
18417
18418 /* A subroutine of mips_expand_vec_init, match constant vector elements.  */
18419
18420 static inline bool
18421 mips_constant_elt_p (rtx x)
18422 {
18423   return CONST_INT_P (x) || GET_CODE (x) == CONST_DOUBLE;
18424 }
18425
18426 /* A subroutine of mips_expand_vec_init, expand via broadcast.  */
18427
18428 static void
18429 mips_expand_vi_broadcast (enum machine_mode vmode, rtx target, rtx elt)
18430 {
18431   struct expand_vec_perm_d d;
18432   rtx t1;
18433   bool ok;
18434
18435   if (elt != const0_rtx)
18436     elt = force_reg (GET_MODE_INNER (vmode), elt);
18437   if (REG_P (elt))
18438     elt = gen_lowpart (DImode, elt);
18439
18440   t1 = gen_reg_rtx (vmode);
18441   switch (vmode)
18442     {
18443     case V8QImode:
18444       emit_insn (gen_loongson_vec_init1_v8qi (t1, elt));
18445       break;
18446     case V4HImode:
18447       emit_insn (gen_loongson_vec_init1_v4hi (t1, elt));
18448       break;
18449     default:
18450       gcc_unreachable ();
18451     }
18452
18453   memset (&d, 0, sizeof (d));
18454   d.target = target;
18455   d.op0 = t1;
18456   d.op1 = t1;
18457   d.vmode = vmode;
18458   d.nelt = GET_MODE_NUNITS (vmode);
18459   d.one_vector_p = true;
18460
18461   ok = mips_expand_vec_perm_const_1 (&d);
18462   gcc_assert (ok);
18463 }
18464
18465 /* A subroutine of mips_expand_vec_init, replacing all of the non-constant
18466    elements of VALS with zeros, copy the constant vector to TARGET.  */
18467
18468 static void
18469 mips_expand_vi_constant (enum machine_mode vmode, unsigned nelt,
18470                          rtx target, rtx vals)
18471 {
18472   rtvec vec = shallow_copy_rtvec (XVEC (vals, 0));
18473   unsigned i;
18474
18475   for (i = 0; i < nelt; ++i)
18476     {
18477       if (!mips_constant_elt_p (RTVEC_ELT (vec, i)))
18478         RTVEC_ELT (vec, i) = const0_rtx;
18479     }
18480
18481   emit_move_insn (target, gen_rtx_CONST_VECTOR (vmode, vec));
18482 }
18483
18484
18485 /* A subroutine of mips_expand_vec_init, expand via pinsrh.  */
18486
18487 static void
18488 mips_expand_vi_loongson_one_pinsrh (rtx target, rtx vals, unsigned one_var)
18489 {
18490   mips_expand_vi_constant (V4HImode, 4, target, vals);
18491
18492   emit_insn (gen_vec_setv4hi (target, target, XVECEXP (vals, 0, one_var),
18493                               GEN_INT (one_var)));
18494 }
18495
18496 /* A subroutine of mips_expand_vec_init, expand anything via memory.  */
18497
18498 static void
18499 mips_expand_vi_general (enum machine_mode vmode, enum machine_mode imode,
18500                         unsigned nelt, unsigned nvar, rtx target, rtx vals)
18501 {
18502   rtx mem = assign_stack_temp (vmode, GET_MODE_SIZE (vmode));
18503   unsigned int i, isize = GET_MODE_SIZE (imode);
18504
18505   if (nvar < nelt)
18506     mips_expand_vi_constant (vmode, nelt, mem, vals);
18507
18508   for (i = 0; i < nelt; ++i)
18509     {
18510       rtx x = XVECEXP (vals, 0, i);
18511       if (!mips_constant_elt_p (x))
18512         emit_move_insn (adjust_address (mem, imode, i * isize), x);
18513     }
18514
18515   emit_move_insn (target, mem);
18516 }
18517
18518 /* Expand a vector initialization.  */
18519
18520 void
18521 mips_expand_vector_init (rtx target, rtx vals)
18522 {
18523   enum machine_mode vmode = GET_MODE (target);
18524   enum machine_mode imode = GET_MODE_INNER (vmode);
18525   unsigned i, nelt = GET_MODE_NUNITS (vmode);
18526   unsigned nvar = 0, one_var = -1u;
18527   bool all_same = true;
18528   rtx x;
18529
18530   for (i = 0; i < nelt; ++i)
18531     {
18532       x = XVECEXP (vals, 0, i);
18533       if (!mips_constant_elt_p (x))
18534         nvar++, one_var = i;
18535       if (i > 0 && !rtx_equal_p (x, XVECEXP (vals, 0, 0)))
18536         all_same = false;
18537     }
18538
18539   /* Load constants from the pool, or whatever's handy.  */
18540   if (nvar == 0)
18541     {
18542       emit_move_insn (target, gen_rtx_CONST_VECTOR (vmode, XVEC (vals, 0)));
18543       return;
18544     }
18545
18546   /* For two-part initialization, always use CONCAT.  */
18547   if (nelt == 2)
18548     {
18549       rtx op0 = force_reg (imode, XVECEXP (vals, 0, 0));
18550       rtx op1 = force_reg (imode, XVECEXP (vals, 0, 1));
18551       x = gen_rtx_VEC_CONCAT (vmode, op0, op1);
18552       emit_insn (gen_rtx_SET (VOIDmode, target, x));
18553       return;
18554     }
18555
18556   /* Loongson is the only cpu with vectors with more elements.  */
18557   gcc_assert (TARGET_HARD_FLOAT && TARGET_LOONGSON_VECTORS);
18558
18559   /* If all values are identical, broadcast the value.  */
18560   if (all_same)
18561     {
18562       mips_expand_vi_broadcast (vmode, target, XVECEXP (vals, 0, 0));
18563       return;
18564     }
18565
18566   /* If we've only got one non-variable V4HImode, use PINSRH.  */
18567   if (nvar == 1 && vmode == V4HImode)
18568     {
18569       mips_expand_vi_loongson_one_pinsrh (target, vals, one_var);
18570       return;
18571     }
18572
18573   mips_expand_vi_general (vmode, imode, nelt, nvar, target, vals);
18574 }
18575
18576 /* Expand a vector reduction.  */
18577
18578 void
18579 mips_expand_vec_reduc (rtx target, rtx in, rtx (*gen)(rtx, rtx, rtx))
18580 {
18581   enum machine_mode vmode = GET_MODE (in);
18582   unsigned char perm2[2];
18583   rtx last, next, fold, x;
18584   bool ok;
18585
18586   last = in;
18587   fold = gen_reg_rtx (vmode);
18588   switch (vmode)
18589     {
18590     case V2SFmode:
18591       /* Use PUL/PLU to produce { L, H } op { H, L }.
18592          By reversing the pair order, rather than a pure interleave high,
18593          we avoid erroneous exceptional conditions that we might otherwise
18594          produce from the computation of H op H.  */
18595       perm2[0] = 1;
18596       perm2[1] = 2;
18597       ok = mips_expand_vselect_vconcat (fold, last, last, perm2, 2);
18598       gcc_assert (ok);
18599       break;
18600
18601     case V2SImode:
18602       /* Use interleave to produce { H, L } op { H, H }.  */
18603       emit_insn (gen_loongson_punpckhwd (fold, last, last));
18604       break;
18605
18606     case V4HImode:
18607       /* Perform the first reduction with interleave,
18608          and subsequent reductions with shifts.  */
18609       emit_insn (gen_loongson_punpckhwd_hi (fold, last, last));
18610
18611       next = gen_reg_rtx (vmode);
18612       emit_insn (gen (next, last, fold));
18613       last = next;
18614
18615       fold = gen_reg_rtx (vmode);
18616       x = force_reg (SImode, GEN_INT (16));
18617       emit_insn (gen_vec_shr_v4hi (fold, last, x));
18618       break;
18619
18620     case V8QImode:
18621       emit_insn (gen_loongson_punpckhwd_qi (fold, last, last));
18622
18623       next = gen_reg_rtx (vmode);
18624       emit_insn (gen (next, last, fold));
18625       last = next;
18626
18627       fold = gen_reg_rtx (vmode);
18628       x = force_reg (SImode, GEN_INT (16));
18629       emit_insn (gen_vec_shr_v8qi (fold, last, x));
18630
18631       next = gen_reg_rtx (vmode);
18632       emit_insn (gen (next, last, fold));
18633       last = next;
18634
18635       fold = gen_reg_rtx (vmode);
18636       x = force_reg (SImode, GEN_INT (8));
18637       emit_insn (gen_vec_shr_v8qi (fold, last, x));
18638       break;
18639
18640     default:
18641       gcc_unreachable ();
18642     }
18643
18644   emit_insn (gen (target, last, fold));
18645 }
18646
18647 /* Expand a vector minimum/maximum.  */
18648
18649 void
18650 mips_expand_vec_minmax (rtx target, rtx op0, rtx op1,
18651                         rtx (*cmp) (rtx, rtx, rtx), bool min_p)
18652 {
18653   enum machine_mode vmode = GET_MODE (target);
18654   rtx tc, t0, t1, x;
18655
18656   tc = gen_reg_rtx (vmode);
18657   t0 = gen_reg_rtx (vmode);
18658   t1 = gen_reg_rtx (vmode);
18659
18660   /* op0 > op1 */
18661   emit_insn (cmp (tc, op0, op1));
18662
18663   x = gen_rtx_AND (vmode, tc, (min_p ? op1 : op0));
18664   emit_insn (gen_rtx_SET (VOIDmode, t0, x));
18665
18666   x = gen_rtx_NOT (vmode, tc);
18667   x = gen_rtx_AND (vmode, x, (min_p ? op0 : op1));
18668   emit_insn (gen_rtx_SET (VOIDmode, t1, x));
18669
18670   x = gen_rtx_IOR (vmode, t0, t1);
18671   emit_insn (gen_rtx_SET (VOIDmode, target, x));
18672 }
18673
18674 /* Implement TARGET_CASE_VALUES_THRESHOLD.  */
18675
18676 unsigned int
18677 mips_case_values_threshold (void)
18678 {
18679   /* In MIPS16 mode using a larger case threshold generates smaller code.  */
18680   if (TARGET_MIPS16 && optimize_size)
18681     return 10;
18682   else
18683     return default_case_values_threshold ();
18684 }
18685 \f
18686 /* Initialize the GCC target structure.  */
18687 #undef TARGET_ASM_ALIGNED_HI_OP
18688 #define TARGET_ASM_ALIGNED_HI_OP "\t.half\t"
18689 #undef TARGET_ASM_ALIGNED_SI_OP
18690 #define TARGET_ASM_ALIGNED_SI_OP "\t.word\t"
18691 #undef TARGET_ASM_ALIGNED_DI_OP
18692 #define TARGET_ASM_ALIGNED_DI_OP "\t.dword\t"
18693
18694 #undef TARGET_OPTION_OVERRIDE
18695 #define TARGET_OPTION_OVERRIDE mips_option_override
18696
18697 #undef TARGET_LEGITIMIZE_ADDRESS
18698 #define TARGET_LEGITIMIZE_ADDRESS mips_legitimize_address
18699
18700 #undef TARGET_ASM_FUNCTION_PROLOGUE
18701 #define TARGET_ASM_FUNCTION_PROLOGUE mips_output_function_prologue
18702 #undef TARGET_ASM_FUNCTION_EPILOGUE
18703 #define TARGET_ASM_FUNCTION_EPILOGUE mips_output_function_epilogue
18704 #undef TARGET_ASM_SELECT_RTX_SECTION
18705 #define TARGET_ASM_SELECT_RTX_SECTION mips_select_rtx_section
18706 #undef TARGET_ASM_FUNCTION_RODATA_SECTION
18707 #define TARGET_ASM_FUNCTION_RODATA_SECTION mips_function_rodata_section
18708
18709 #undef TARGET_SCHED_INIT
18710 #define TARGET_SCHED_INIT mips_sched_init
18711 #undef TARGET_SCHED_REORDER
18712 #define TARGET_SCHED_REORDER mips_sched_reorder
18713 #undef TARGET_SCHED_REORDER2
18714 #define TARGET_SCHED_REORDER2 mips_sched_reorder2
18715 #undef TARGET_SCHED_VARIABLE_ISSUE
18716 #define TARGET_SCHED_VARIABLE_ISSUE mips_variable_issue
18717 #undef TARGET_SCHED_ADJUST_COST
18718 #define TARGET_SCHED_ADJUST_COST mips_adjust_cost
18719 #undef TARGET_SCHED_ISSUE_RATE
18720 #define TARGET_SCHED_ISSUE_RATE mips_issue_rate
18721 #undef TARGET_SCHED_INIT_DFA_POST_CYCLE_INSN
18722 #define TARGET_SCHED_INIT_DFA_POST_CYCLE_INSN mips_init_dfa_post_cycle_insn
18723 #undef TARGET_SCHED_DFA_POST_ADVANCE_CYCLE
18724 #define TARGET_SCHED_DFA_POST_ADVANCE_CYCLE mips_dfa_post_advance_cycle
18725 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD
18726 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD \
18727   mips_multipass_dfa_lookahead
18728 #undef TARGET_SMALL_REGISTER_CLASSES_FOR_MODE_P
18729 #define TARGET_SMALL_REGISTER_CLASSES_FOR_MODE_P \
18730   mips_small_register_classes_for_mode_p
18731
18732 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
18733 #define TARGET_FUNCTION_OK_FOR_SIBCALL mips_function_ok_for_sibcall
18734
18735 #undef TARGET_INSERT_ATTRIBUTES
18736 #define TARGET_INSERT_ATTRIBUTES mips_insert_attributes
18737 #undef TARGET_MERGE_DECL_ATTRIBUTES
18738 #define TARGET_MERGE_DECL_ATTRIBUTES mips_merge_decl_attributes
18739 #undef TARGET_CAN_INLINE_P
18740 #define TARGET_CAN_INLINE_P mips_can_inline_p
18741 #undef TARGET_SET_CURRENT_FUNCTION
18742 #define TARGET_SET_CURRENT_FUNCTION mips_set_current_function
18743
18744 #undef TARGET_VALID_POINTER_MODE
18745 #define TARGET_VALID_POINTER_MODE mips_valid_pointer_mode
18746 #undef TARGET_REGISTER_MOVE_COST
18747 #define TARGET_REGISTER_MOVE_COST mips_register_move_cost
18748 #undef TARGET_MEMORY_MOVE_COST
18749 #define TARGET_MEMORY_MOVE_COST mips_memory_move_cost
18750 #undef TARGET_RTX_COSTS
18751 #define TARGET_RTX_COSTS mips_rtx_costs
18752 #undef TARGET_ADDRESS_COST
18753 #define TARGET_ADDRESS_COST mips_address_cost
18754
18755 #undef TARGET_IN_SMALL_DATA_P
18756 #define TARGET_IN_SMALL_DATA_P mips_in_small_data_p
18757
18758 #undef TARGET_MACHINE_DEPENDENT_REORG
18759 #define TARGET_MACHINE_DEPENDENT_REORG mips_reorg
18760
18761 #undef  TARGET_PREFERRED_RELOAD_CLASS
18762 #define TARGET_PREFERRED_RELOAD_CLASS mips_preferred_reload_class
18763
18764 #undef TARGET_EXPAND_TO_RTL_HOOK
18765 #define TARGET_EXPAND_TO_RTL_HOOK mips_expand_to_rtl_hook
18766 #undef TARGET_ASM_FILE_START
18767 #define TARGET_ASM_FILE_START mips_file_start
18768 #undef TARGET_ASM_FILE_START_FILE_DIRECTIVE
18769 #define TARGET_ASM_FILE_START_FILE_DIRECTIVE true
18770 #undef TARGET_ASM_CODE_END
18771 #define TARGET_ASM_CODE_END mips_code_end
18772
18773 #undef TARGET_INIT_LIBFUNCS
18774 #define TARGET_INIT_LIBFUNCS mips_init_libfuncs
18775
18776 #undef TARGET_BUILD_BUILTIN_VA_LIST
18777 #define TARGET_BUILD_BUILTIN_VA_LIST mips_build_builtin_va_list
18778 #undef TARGET_EXPAND_BUILTIN_VA_START
18779 #define TARGET_EXPAND_BUILTIN_VA_START mips_va_start
18780 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
18781 #define TARGET_GIMPLIFY_VA_ARG_EXPR mips_gimplify_va_arg_expr
18782
18783 #undef  TARGET_PROMOTE_FUNCTION_MODE
18784 #define TARGET_PROMOTE_FUNCTION_MODE default_promote_function_mode_always_promote
18785 #undef TARGET_PROMOTE_PROTOTYPES
18786 #define TARGET_PROMOTE_PROTOTYPES hook_bool_const_tree_true
18787
18788 #undef TARGET_FUNCTION_VALUE
18789 #define TARGET_FUNCTION_VALUE mips_function_value
18790 #undef TARGET_LIBCALL_VALUE
18791 #define TARGET_LIBCALL_VALUE mips_libcall_value
18792 #undef TARGET_FUNCTION_VALUE_REGNO_P
18793 #define TARGET_FUNCTION_VALUE_REGNO_P mips_function_value_regno_p
18794 #undef TARGET_RETURN_IN_MEMORY
18795 #define TARGET_RETURN_IN_MEMORY mips_return_in_memory
18796 #undef TARGET_RETURN_IN_MSB
18797 #define TARGET_RETURN_IN_MSB mips_return_in_msb
18798
18799 #undef TARGET_ASM_OUTPUT_MI_THUNK
18800 #define TARGET_ASM_OUTPUT_MI_THUNK mips_output_mi_thunk
18801 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
18802 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK hook_bool_const_tree_hwi_hwi_const_tree_true
18803
18804 #undef TARGET_PRINT_OPERAND
18805 #define TARGET_PRINT_OPERAND mips_print_operand
18806 #undef TARGET_PRINT_OPERAND_ADDRESS
18807 #define TARGET_PRINT_OPERAND_ADDRESS mips_print_operand_address
18808 #undef TARGET_PRINT_OPERAND_PUNCT_VALID_P
18809 #define TARGET_PRINT_OPERAND_PUNCT_VALID_P mips_print_operand_punct_valid_p
18810
18811 #undef TARGET_SETUP_INCOMING_VARARGS
18812 #define TARGET_SETUP_INCOMING_VARARGS mips_setup_incoming_varargs
18813 #undef TARGET_STRICT_ARGUMENT_NAMING
18814 #define TARGET_STRICT_ARGUMENT_NAMING mips_strict_argument_naming
18815 #undef TARGET_MUST_PASS_IN_STACK
18816 #define TARGET_MUST_PASS_IN_STACK must_pass_in_stack_var_size
18817 #undef TARGET_PASS_BY_REFERENCE
18818 #define TARGET_PASS_BY_REFERENCE mips_pass_by_reference
18819 #undef TARGET_CALLEE_COPIES
18820 #define TARGET_CALLEE_COPIES mips_callee_copies
18821 #undef TARGET_ARG_PARTIAL_BYTES
18822 #define TARGET_ARG_PARTIAL_BYTES mips_arg_partial_bytes
18823 #undef TARGET_FUNCTION_ARG
18824 #define TARGET_FUNCTION_ARG mips_function_arg
18825 #undef TARGET_FUNCTION_ARG_ADVANCE
18826 #define TARGET_FUNCTION_ARG_ADVANCE mips_function_arg_advance
18827 #undef TARGET_FUNCTION_ARG_BOUNDARY
18828 #define TARGET_FUNCTION_ARG_BOUNDARY mips_function_arg_boundary
18829
18830 #undef TARGET_MODE_REP_EXTENDED
18831 #define TARGET_MODE_REP_EXTENDED mips_mode_rep_extended
18832
18833 #undef TARGET_VECTOR_MODE_SUPPORTED_P
18834 #define TARGET_VECTOR_MODE_SUPPORTED_P mips_vector_mode_supported_p
18835
18836 #undef TARGET_SCALAR_MODE_SUPPORTED_P
18837 #define TARGET_SCALAR_MODE_SUPPORTED_P mips_scalar_mode_supported_p
18838
18839 #undef TARGET_VECTORIZE_PREFERRED_SIMD_MODE
18840 #define TARGET_VECTORIZE_PREFERRED_SIMD_MODE mips_preferred_simd_mode
18841
18842 #undef TARGET_INIT_BUILTINS
18843 #define TARGET_INIT_BUILTINS mips_init_builtins
18844 #undef TARGET_BUILTIN_DECL
18845 #define TARGET_BUILTIN_DECL mips_builtin_decl
18846 #undef TARGET_EXPAND_BUILTIN
18847 #define TARGET_EXPAND_BUILTIN mips_expand_builtin
18848
18849 #undef TARGET_HAVE_TLS
18850 #define TARGET_HAVE_TLS HAVE_AS_TLS
18851
18852 #undef TARGET_CANNOT_FORCE_CONST_MEM
18853 #define TARGET_CANNOT_FORCE_CONST_MEM mips_cannot_force_const_mem
18854
18855 #undef TARGET_LEGITIMATE_CONSTANT_P
18856 #define TARGET_LEGITIMATE_CONSTANT_P mips_legitimate_constant_p
18857
18858 #undef TARGET_ENCODE_SECTION_INFO
18859 #define TARGET_ENCODE_SECTION_INFO mips_encode_section_info
18860
18861 #undef TARGET_ATTRIBUTE_TABLE
18862 #define TARGET_ATTRIBUTE_TABLE mips_attribute_table
18863 /* All our function attributes are related to how out-of-line copies should
18864    be compiled or called.  They don't in themselves prevent inlining.  */
18865 #undef TARGET_FUNCTION_ATTRIBUTE_INLINABLE_P
18866 #define TARGET_FUNCTION_ATTRIBUTE_INLINABLE_P hook_bool_const_tree_true
18867
18868 #undef TARGET_EXTRA_LIVE_ON_ENTRY
18869 #define TARGET_EXTRA_LIVE_ON_ENTRY mips_extra_live_on_entry
18870
18871 #undef TARGET_USE_BLOCKS_FOR_CONSTANT_P
18872 #define TARGET_USE_BLOCKS_FOR_CONSTANT_P mips_use_blocks_for_constant_p
18873 #undef TARGET_USE_ANCHORS_FOR_SYMBOL_P
18874 #define TARGET_USE_ANCHORS_FOR_SYMBOL_P mips_use_anchors_for_symbol_p
18875
18876 #undef  TARGET_COMP_TYPE_ATTRIBUTES
18877 #define TARGET_COMP_TYPE_ATTRIBUTES mips_comp_type_attributes
18878
18879 #ifdef HAVE_AS_DTPRELWORD
18880 #undef TARGET_ASM_OUTPUT_DWARF_DTPREL
18881 #define TARGET_ASM_OUTPUT_DWARF_DTPREL mips_output_dwarf_dtprel
18882 #endif
18883 #undef TARGET_DWARF_REGISTER_SPAN
18884 #define TARGET_DWARF_REGISTER_SPAN mips_dwarf_register_span
18885
18886 #undef TARGET_ASM_FINAL_POSTSCAN_INSN
18887 #define TARGET_ASM_FINAL_POSTSCAN_INSN mips_final_postscan_insn
18888
18889 #undef TARGET_LEGITIMATE_ADDRESS_P
18890 #define TARGET_LEGITIMATE_ADDRESS_P     mips_legitimate_address_p
18891
18892 #undef TARGET_FRAME_POINTER_REQUIRED
18893 #define TARGET_FRAME_POINTER_REQUIRED mips_frame_pointer_required
18894
18895 #undef TARGET_CAN_ELIMINATE
18896 #define TARGET_CAN_ELIMINATE mips_can_eliminate
18897
18898 #undef TARGET_CONDITIONAL_REGISTER_USAGE
18899 #define TARGET_CONDITIONAL_REGISTER_USAGE mips_conditional_register_usage
18900
18901 #undef TARGET_TRAMPOLINE_INIT
18902 #define TARGET_TRAMPOLINE_INIT mips_trampoline_init
18903
18904 #undef TARGET_ASM_OUTPUT_SOURCE_FILENAME
18905 #define TARGET_ASM_OUTPUT_SOURCE_FILENAME mips_output_filename
18906
18907 #undef TARGET_SHIFT_TRUNCATION_MASK
18908 #define TARGET_SHIFT_TRUNCATION_MASK mips_shift_truncation_mask
18909
18910 #undef TARGET_PREPARE_PCH_SAVE
18911 #define TARGET_PREPARE_PCH_SAVE mips_prepare_pch_save
18912
18913 #undef TARGET_VECTORIZE_VEC_PERM_CONST_OK
18914 #define TARGET_VECTORIZE_VEC_PERM_CONST_OK mips_vectorize_vec_perm_const_ok
18915
18916 #undef TARGET_CASE_VALUES_THRESHOLD
18917 #define TARGET_CASE_VALUES_THRESHOLD mips_case_values_threshold
18918
18919 struct gcc_target targetm = TARGET_INITIALIZER;
18920 \f
18921 #include "gt-mips.h"